open-webui/src/lib/components/chat/MessageInput.svelte

1775 lines
56 KiB
Svelte
Raw Normal View History

2023-11-20 09:47:07 +08:00
<script lang="ts">
2025-06-04 06:36:31 +08:00
import DOMPurify from 'dompurify';
2025-06-04 06:46:30 +08:00
import { marked } from 'marked';
2025-06-04 06:36:31 +08:00
2024-03-01 17:18:07 +08:00
import { toast } from 'svelte-sonner';
2025-06-04 06:36:31 +08:00
2024-10-27 03:56:37 +08:00
import { v4 as uuidv4 } from 'uuid';
import { createPicker, getAuthToken } from '$lib/utils/google-drive-picker';
2025-02-24 22:14:10 +08:00
import { pickAndDownloadFile } from '$lib/utils/onedrive-file-picker';
2024-10-27 03:56:37 +08:00
2024-10-09 14:57:36 +08:00
import { onMount, tick, getContext, createEventDispatcher, onDestroy } from 'svelte';
2024-08-23 22:42:36 +08:00
const dispatch = createEventDispatcher();
2024-08-23 20:31:39 +08:00
2024-06-07 13:30:19 +08:00
import {
type Model,
mobile,
settings,
models,
config,
2024-06-11 15:18:45 +08:00
showCallOverlay,
2024-06-12 06:29:46 +08:00
tools,
toolServers,
2024-10-05 18:07:56 +08:00
user as _user,
2025-02-10 15:54:24 +08:00
showControls,
TTSWorker,
temporaryChatEnabled
2024-06-07 13:30:19 +08:00
} from '$lib/stores';
2024-11-17 08:51:55 +08:00
2025-04-04 23:11:54 +08:00
import {
convertHeicToJpeg,
2025-04-04 23:11:54 +08:00
compressImage,
createMessagesList,
extractContentFromFile,
extractCurlyBraceWords,
2025-07-09 05:49:43 +08:00
extractInputVariables,
2025-08-21 07:38:26 +08:00
getAge,
getCurrentDateTime,
getFormattedDate,
getFormattedTime,
getUserPosition,
getUserTimezone,
getWeekday
2025-04-04 23:11:54 +08:00
} from '$lib/utils';
2024-06-19 04:50:18 +08:00
import { uploadFile } from '$lib/apis/files';
2025-02-04 11:54:20 +08:00
import { generateAutoCompletion } from '$lib/apis';
import { deleteFileById } from '$lib/apis/files';
2025-09-12 19:56:31 +08:00
import { getSessionUser } from '$lib/apis/auths';
import { getTools } from '$lib/apis/tools';
2024-08-27 21:58:02 +08:00
import { WEBUI_BASE_URL, WEBUI_API_BASE_URL, PASTED_TEXT_CHARACTER_LIMIT } from '$lib/constants';
2024-05-02 17:20:57 +08:00
2024-05-28 04:22:24 +08:00
import InputMenu from './MessageInput/InputMenu.svelte';
2024-06-07 11:33:23 +08:00
import VoiceRecording from './MessageInput/VoiceRecording.svelte';
2024-07-17 18:02:54 +08:00
import FilesOverlay from './MessageInput/FilesOverlay.svelte';
2025-05-17 02:17:47 +08:00
import ToolServersModal from './ToolServersModal.svelte';
2025-02-04 11:54:20 +08:00
2024-10-19 14:54:35 +08:00
import RichTextInput from '../common/RichTextInput.svelte';
2025-02-04 11:54:20 +08:00
import Tooltip from '../common/Tooltip.svelte';
import FileItem from '../common/FileItem.svelte';
2024-12-18 17:27:32 +08:00
import Image from '../common/Image.svelte';
2025-02-04 11:54:20 +08:00
import XMark from '../icons/XMark.svelte';
import Headphone from '../icons/Headphone.svelte';
2025-02-04 11:34:36 +08:00
import GlobeAlt from '../icons/GlobeAlt.svelte';
2025-02-04 11:54:20 +08:00
import Photo from '../icons/Photo.svelte';
2025-05-17 02:17:47 +08:00
import Wrench from '../icons/Wrench.svelte';
2025-02-04 11:54:20 +08:00
import CommandLine from '../icons/CommandLine.svelte';
2025-05-17 03:21:08 +08:00
import Sparkles from '../icons/Sparkles.svelte';
2025-05-17 02:17:47 +08:00
import InputVariablesModal from './MessageInput/InputVariablesModal.svelte';
2025-07-22 15:22:01 +08:00
import Voice from '../icons/Voice.svelte';
2025-09-12 17:38:02 +08:00
import Terminal from '../icons/Terminal.svelte';
2025-09-12 19:56:31 +08:00
import IntegrationsMenu from './MessageInput/IntegrationsMenu.svelte';
2025-09-12 19:05:37 +08:00
import Component from '../icons/Component.svelte';
import PlusAlt from '../icons/PlusAlt.svelte';
2025-09-12 19:56:31 +08:00
import { KokoroWorker } from '$lib/workers/KokoroWorker';
2025-09-13 00:31:57 +08:00
import { getSuggestionRenderer } from '../common/RichTextInput/suggestions';
import CommandSuggestionList from './MessageInput/CommandSuggestionList.svelte';
2024-03-01 12:40:36 +08:00
const i18n = getContext('i18n');
2024-12-22 00:16:29 +08:00
export let onChange: Function = () => {};
export let createMessagePair: Function;
2023-11-20 09:47:07 +08:00
export let stopResponse: Function;
2024-08-22 23:37:47 +08:00
export let autoScroll = false;
2025-08-06 02:25:51 +08:00
export let generating = false;
2024-05-25 13:21:57 +08:00
2024-12-23 10:40:01 +08:00
export let atSelectedModel: Model | undefined = undefined;
export let selectedModels: [''];
2024-05-02 17:20:57 +08:00
2024-11-29 16:16:49 +08:00
let selectedModelIds = [];
$: selectedModelIds = atSelectedModel !== undefined ? [atSelectedModel.id] : selectedModels;
2024-10-05 18:07:56 +08:00
export let history;
2025-04-13 11:51:02 +08:00
export let taskIds = null;
2024-10-05 18:07:56 +08:00
export let prompt = '';
export let files = [];
2024-11-17 09:49:13 +08:00
2024-10-05 18:07:56 +08:00
export let selectedToolIds = [];
2025-05-17 02:43:42 +08:00
export let selectedFilterIds = [];
2025-01-16 15:32:13 +08:00
export let imageGenerationEnabled = false;
2024-10-05 18:07:56 +08:00
export let webSearchEnabled = false;
2025-02-03 17:14:38 +08:00
export let codeInterpreterEnabled = false;
2024-10-05 18:07:56 +08:00
let showInputVariablesModal = false;
let inputVariablesModalCallback = (variableValues) => {};
let inputVariables = {};
let inputVariableValues = {};
2024-12-22 00:16:29 +08:00
$: onChange({
prompt,
files: files
.filter((file) => file.type !== 'image')
.map((file) => {
return {
...file,
user: undefined,
access_control: undefined
};
}),
2024-12-22 00:16:29 +08:00
selectedToolIds,
2025-05-17 02:43:42 +08:00
selectedFilterIds,
2025-01-16 15:32:13 +08:00
imageGenerationEnabled,
2025-05-07 05:48:54 +08:00
webSearchEnabled,
2025-05-17 02:43:42 +08:00
codeInterpreterEnabled
2024-12-22 00:16:29 +08:00
});
const inputVariableHandler = async (text: string): Promise<string> => {
inputVariables = extractInputVariables(text);
// No variables? return the original text immediately.
if (Object.keys(inputVariables).length === 0) {
return text;
}
// Show modal and wait for the user's input.
showInputVariablesModal = true;
return await new Promise<string>((resolve) => {
inputVariablesModalCallback = (variableValues) => {
inputVariableValues = { ...inputVariableValues, ...variableValues };
replaceVariables(inputVariableValues);
showInputVariablesModal = false;
resolve(text);
};
});
};
const textVariableHandler = async (text: string) => {
if (text.includes('{{CLIPBOARD}}')) {
const clipboardText = await navigator.clipboard.readText().catch((err) => {
toast.error($i18n.t('Failed to read clipboard contents'));
return '{{CLIPBOARD}}';
});
const clipboardItems = await navigator.clipboard.read();
let imageUrl = null;
for (const item of clipboardItems) {
// Check for known image types
for (const type of item.types) {
if (type.startsWith('image/')) {
const blob = await item.getType(type);
imageUrl = URL.createObjectURL(blob);
}
}
}
if (imageUrl) {
files = [
...files,
{
type: 'image',
url: imageUrl
}
];
}
text = text.replaceAll('{{CLIPBOARD}}', clipboardText);
}
if (text.includes('{{USER_LOCATION}}')) {
let location;
try {
location = await getUserPosition();
} catch (error) {
toast.error($i18n.t('Location access not allowed'));
location = 'LOCATION_UNKNOWN';
}
text = text.replaceAll('{{USER_LOCATION}}', String(location));
}
2025-08-21 17:13:38 +08:00
const sessionUser = await getSessionUser(localStorage.token);
2025-08-21 07:38:26 +08:00
if (text.includes('{{USER_NAME}}')) {
2025-08-21 07:38:26 +08:00
const name = sessionUser?.name || 'User';
text = text.replaceAll('{{USER_NAME}}', name);
}
2025-08-21 07:38:26 +08:00
if (text.includes('{{USER_BIO}}')) {
const bio = sessionUser?.bio || '';
if (bio) {
text = text.replaceAll('{{USER_BIO}}', bio);
}
}
if (text.includes('{{USER_GENDER}}')) {
const gender = sessionUser?.gender || '';
if (gender) {
text = text.replaceAll('{{USER_GENDER}}', gender);
}
}
if (text.includes('{{USER_BIRTH_DATE}}')) {
const birthDate = sessionUser?.date_of_birth || '';
if (birthDate) {
text = text.replaceAll('{{USER_BIRTH_DATE}}', birthDate);
}
}
if (text.includes('{{USER_AGE}}')) {
const birthDate = sessionUser?.date_of_birth || '';
if (birthDate) {
// calculate age using date
const age = getAge(birthDate);
text = text.replaceAll('{{USER_AGE}}', age);
}
}
if (text.includes('{{USER_LANGUAGE}}')) {
const language = localStorage.getItem('locale') || 'en-US';
text = text.replaceAll('{{USER_LANGUAGE}}', language);
}
if (text.includes('{{CURRENT_DATE}}')) {
const date = getFormattedDate();
text = text.replaceAll('{{CURRENT_DATE}}', date);
}
if (text.includes('{{CURRENT_TIME}}')) {
const time = getFormattedTime();
text = text.replaceAll('{{CURRENT_TIME}}', time);
}
if (text.includes('{{CURRENT_DATETIME}}')) {
const dateTime = getCurrentDateTime();
text = text.replaceAll('{{CURRENT_DATETIME}}', dateTime);
}
if (text.includes('{{CURRENT_TIMEZONE}}')) {
const timezone = getUserTimezone();
text = text.replaceAll('{{CURRENT_TIMEZONE}}', timezone);
}
if (text.includes('{{CURRENT_WEEKDAY}}')) {
const weekday = getWeekday();
text = text.replaceAll('{{CURRENT_WEEKDAY}}', weekday);
}
return text;
};
const replaceVariables = (variables: Record<string, any>) => {
console.log('Replacing variables:', variables);
const chatInput = document.getElementById('chat-input');
if (chatInput) {
2025-09-13 00:54:34 +08:00
chatInputElement.replaceVariables(variables);
chatInputElement.focus();
}
};
export const setText = async (text?: string, cb?: (text: string) => void) => {
const chatInput = document.getElementById('chat-input');
if (chatInput) {
2025-09-16 05:35:53 +08:00
if (text !== '') {
text = await textVariableHandler(text || '');
}
2025-09-13 00:54:34 +08:00
chatInputElement?.setText(text);
chatInputElement?.focus();
2025-09-16 05:35:53 +08:00
if (text !== '') {
text = await inputVariableHandler(text);
}
await tick();
if (cb) await cb(text);
}
};
const getCommand = () => {
const chatInput = document.getElementById('chat-input');
let word = '';
if (chatInput) {
2025-09-13 00:54:34 +08:00
word = chatInputElement?.getWordAtDocPos();
}
return word;
};
const replaceCommandWithText = (text) => {
const chatInput = document.getElementById('chat-input');
if (!chatInput) return;
2025-09-13 00:54:34 +08:00
chatInputElement?.replaceCommandWithText(text);
};
const insertTextAtCursor = async (text: string) => {
const chatInput = document.getElementById('chat-input');
if (!chatInput) return;
text = await textVariableHandler(text);
if (command) {
replaceCommandWithText(text);
} else {
2025-09-13 00:54:34 +08:00
chatInputElement?.insertContent(text);
}
await tick();
2025-09-10 17:52:34 +08:00
text = await inputVariableHandler(text);
await tick();
const chatInputContainer = document.getElementById('chat-input-container');
if (chatInputContainer) {
chatInputContainer.scrollTop = chatInputContainer.scrollHeight;
}
await tick();
if (chatInput) {
chatInput.focus();
chatInput.dispatchEvent(new Event('input'));
const words = extractCurlyBraceWords(prompt);
if (words.length > 0) {
const word = words.at(0);
await tick();
} else {
chatInput.scrollTop = chatInput.scrollHeight;
}
}
};
let command = '';
export let showCommands = false;
$: showCommands = ['/', '#', '@'].includes(command?.charAt(0)) || '\\#' === command?.slice(0, 2);
2025-09-13 00:31:57 +08:00
let suggestions = null;
2025-04-05 15:05:47 +08:00
let showTools = false;
2025-03-29 03:40:56 +08:00
2024-11-17 08:01:02 +08:00
let loaded = false;
2024-06-07 11:33:23 +08:00
let recording = false;
2025-03-06 11:36:30 +08:00
let isComposing = false;
2025-08-25 14:37:51 +08:00
// Safari has a bug where compositionend is not triggered correctly #16615
// when using the virtual keyboard on iOS.
let compositionEndedAt = -2e8;
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
function inOrNearComposition(event: Event) {
if (isComposing) {
return true;
}
// See https://www.stum.de/2016/06/24/handling-ime-events-in-javascript/.
// On Japanese input method editors (IMEs), the Enter key is used to confirm character
// selection. On Safari, when Enter is pressed, compositionend and keydown events are
// emitted. The keydown event triggers newline insertion, which we don't want.
// This method returns true if the keydown event should be ignored.
// We only ignore it once, as pressing Enter a second time *should* insert a newline.
// Furthermore, the keydown event timestamp must be close to the compositionEndedAt timestamp.
// This guards against the case where compositionend is triggered without the keyboard
// (e.g. character confirmation may be done with the mouse), and keydown is triggered
// afterwards- we wouldn't want to ignore the keydown event in this case.
if (isSafari && Math.abs(event.timeStamp - compositionEndedAt) < 500) {
compositionEndedAt = -2e8;
return true;
}
return false;
}
2025-03-06 11:36:30 +08:00
2024-10-19 14:54:35 +08:00
let chatInputContainerElement;
2024-10-19 15:23:59 +08:00
let chatInputElement;
2023-11-25 16:21:07 +08:00
let filesInputElement;
2024-08-23 20:31:39 +08:00
let commandsElement;
2024-01-02 16:55:28 +08:00
2023-11-25 16:21:07 +08:00
let inputFiles;
2023-12-20 06:50:43 +08:00
let dragged = false;
let shiftKey = false;
2023-11-25 16:21:07 +08:00
2024-01-10 14:47:31 +08:00
let user = null;
2024-10-05 18:07:56 +08:00
export let placeholder = '';
2024-05-11 19:35:48 +08:00
2024-05-25 14:34:58 +08:00
let visionCapableModels = [];
2025-05-17 04:59:00 +08:00
$: visionCapableModels = (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).filter(
2024-05-25 14:34:58 +08:00
(model) => $models.find((m) => m.id === model)?.info?.meta?.capabilities?.vision ?? true
);
2025-05-17 05:13:13 +08:00
let fileUploadCapableModels = [];
$: fileUploadCapableModels = (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).filter(
(model) => $models.find((m) => m.id === model)?.info?.meta?.capabilities?.file_upload ?? true
);
2025-05-17 04:59:00 +08:00
let webSearchCapableModels = [];
$: webSearchCapableModels = (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).filter(
(model) => $models.find((m) => m.id === model)?.info?.meta?.capabilities?.web_search ?? true
);
let imageGenerationCapableModels = [];
$: imageGenerationCapableModels = (
atSelectedModel?.id ? [atSelectedModel.id] : selectedModels
).filter(
(model) =>
$models.find((m) => m.id === model)?.info?.meta?.capabilities?.image_generation ?? true
);
let codeInterpreterCapableModels = [];
$: codeInterpreterCapableModels = (
atSelectedModel?.id ? [atSelectedModel.id] : selectedModels
).filter(
(model) =>
$models.find((m) => m.id === model)?.info?.meta?.capabilities?.code_interpreter ?? true
);
2025-05-17 03:21:08 +08:00
let toggleFilters = [];
2025-05-17 04:59:00 +08:00
$: toggleFilters = (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels)
2025-05-17 03:21:08 +08:00
.map((id) => ($models.find((model) => model.id === id) || {})?.filters ?? [])
.reduce((acc, filters) => acc.filter((f1) => filters.some((f2) => f2.id === f1.id)));
2025-05-23 21:30:17 +08:00
let showToolsButton = false;
$: showToolsButton = ($tools ?? []).length > 0 || ($toolServers ?? []).length > 0;
2025-05-23 21:30:17 +08:00
let showWebSearchButton = false;
$: showWebSearchButton =
(atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).length ===
webSearchCapableModels.length &&
$config?.features?.enable_web_search &&
($_user.role === 'admin' || $_user?.permissions?.features?.web_search);
let showImageGenerationButton = false;
$: showImageGenerationButton =
(atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).length ===
imageGenerationCapableModels.length &&
$config?.features?.enable_image_generation &&
($_user.role === 'admin' || $_user?.permissions?.features?.image_generation);
let showCodeInterpreterButton = false;
$: showCodeInterpreterButton =
(atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).length ===
codeInterpreterCapableModels.length &&
$config?.features?.enable_code_interpreter &&
($_user.role === 'admin' || $_user?.permissions?.features?.code_interpreter);
2024-02-16 08:20:46 +08:00
const scrollToBottom = () => {
const element = document.getElementById('messages-container');
2024-08-22 23:37:47 +08:00
element.scrollTo({
top: element.scrollHeight,
behavior: 'smooth'
});
2024-02-16 08:20:46 +08:00
};
2024-12-19 01:15:23 +08:00
const screenCaptureHandler = async () => {
try {
// Request screen media
const mediaStream = await navigator.mediaDevices.getDisplayMedia({
video: { cursor: 'never' },
audio: false
});
// Once the user selects a screen, temporarily create a video element
const video = document.createElement('video');
video.srcObject = mediaStream;
// Ensure the video loads without affecting user experience or tab switching
await video.play();
// Set up the canvas to match the video dimensions
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
// Grab a single frame from the video stream using the canvas
const context = canvas.getContext('2d');
context.drawImage(video, 0, 0, canvas.width, canvas.height);
// Stop all video tracks (stop screen sharing) after capturing the image
mediaStream.getTracks().forEach((track) => track.stop());
// bring back focus to this current tab, so that the user can see the screen capture
window.focus();
// Convert the canvas to a Base64 image URL
const imageUrl = canvas.toDataURL('image/png');
// Add the captured image to the files array to render it
files = [...files, { type: 'image', url: imageUrl }];
// Clean memory: Clear video srcObject
video.srcObject = null;
} catch (error) {
// Handle any errors (e.g., user cancels screen sharing)
console.error('Error capturing screen:', error);
}
};
const uploadFileHandler = async (file, fullContext: boolean = false) => {
2024-11-17 13:43:57 +08:00
if ($_user?.role !== 'admin' && !($_user?.permissions?.chat?.file_upload ?? true)) {
2024-11-17 13:31:57 +08:00
toast.error($i18n.t('You do not have permission to upload files.'));
return null;
}
if (fileUploadCapableModels.length !== selectedModels.length) {
toast.error($i18n.t('Model(s) do not support file upload'));
return null;
}
2024-10-27 03:56:37 +08:00
const tempItemId = uuidv4();
const fileItem = {
type: 'file',
file: '',
id: null,
url: '',
name: file.name,
collection_name: '',
status: 'uploading',
size: file.size,
2024-10-27 03:56:37 +08:00
error: '',
itemId: tempItemId,
...(fullContext ? { context: 'full' } : {})
};
2024-10-27 03:56:37 +08:00
if (fileItem.size == 0) {
toast.error($i18n.t('You cannot upload an empty file.'));
return null;
}
files = [...files, fileItem];
2024-09-12 21:18:20 +08:00
if (!$temporaryChatEnabled) {
try {
// If the file is an audio file, provide the language for STT.
let metadata = null;
if (
(file.type.startsWith('audio/') || file.type.startsWith('video/')) &&
$settings?.audio?.stt?.language
) {
metadata = {
language: $settings?.audio?.stt?.language
};
}
2025-05-24 04:36:30 +08:00
// During the file upload, file content is automatically extracted.
const uploadedFile = await uploadFile(localStorage.token, file, metadata);
if (uploadedFile) {
console.log('File upload completed:', {
id: uploadedFile.id,
name: fileItem.name,
collection: uploadedFile?.meta?.collection_name
});
if (uploadedFile.error) {
console.warn('File upload warning:', uploadedFile.error);
toast.warning(uploadedFile.error);
}
2024-12-17 02:36:25 +08:00
fileItem.status = 'uploaded';
fileItem.file = uploadedFile;
fileItem.id = uploadedFile.id;
fileItem.collection_name =
uploadedFile?.meta?.collection_name || uploadedFile?.collection_name;
fileItem.url = `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`;
files = files;
} else {
files = files.filter((item) => item?.itemId !== tempItemId);
2024-10-27 04:05:54 +08:00
}
} catch (e) {
toast.error(`${e}`);
files = files.filter((item) => item?.itemId !== tempItemId);
}
} else {
// If temporary chat is enabled, we just add the file to the list without uploading it.
2025-09-05 17:55:04 +08:00
const content = await extractContentFromFile(file).catch((error) => {
toast.error(
$i18n.t('Failed to extract content from the file: {{error}}', { error: error })
);
return null;
});
if (content === null) {
toast.error($i18n.t('Failed to extract content from the file.'));
files = files.filter((item) => item?.itemId !== tempItemId);
return null;
} else {
console.log('Extracted content from file:', {
name: file.name,
size: file.size,
content: content
});
2024-10-27 04:05:54 +08:00
fileItem.status = 'uploaded';
fileItem.type = 'text';
fileItem.content = content;
fileItem.id = uuidv4(); // Temporary ID for the file
2024-10-04 13:22:22 +08:00
files = files;
2024-02-11 17:06:25 +08:00
}
2024-06-19 04:50:18 +08:00
}
};
2024-10-04 14:41:17 +08:00
2024-08-27 21:51:40 +08:00
const inputFilesHandler = async (inputFiles) => {
console.log('Input files handler called with:', inputFiles);
2025-06-16 17:28:31 +08:00
if (
($config?.file?.max_count ?? null) !== null &&
files.length + inputFiles.length > $config?.file?.max_count
) {
toast.error(
$i18n.t(`You can only chat with a maximum of {{maxCount}} file(s) at a time.`, {
maxCount: $config?.file?.max_count
})
);
return;
}
2025-07-03 04:04:13 +08:00
inputFiles.forEach(async (file) => {
console.log('Processing file:', {
name: file.name,
type: file.type,
size: file.size,
extension: file.name.split('.').at(-1)
});
2024-08-27 23:05:24 +08:00
if (
($config?.file?.max_size ?? null) !== null &&
file.size > ($config?.file?.max_size ?? 0) * 1024 * 1024
) {
console.log('File exceeds max size limit:', {
fileSize: file.size,
maxSize: ($config?.file?.max_size ?? 0) * 1024 * 1024
});
2024-08-27 23:05:24 +08:00
toast.error(
$i18n.t(`File size should not exceed {{maxSize}} MB.`, {
maxSize: $config?.file?.max_size
})
);
return;
}
2025-07-03 04:04:13 +08:00
if (file['type'].startsWith('image/')) {
if (visionCapableModels.length === 0) {
toast.error($i18n.t('Selected model(s) do not support image inputs'));
return;
}
2024-12-25 14:28:14 +08:00
2025-07-03 04:04:13 +08:00
const compressImageHandler = async (imageUrl, settings = {}, config = {}) => {
// Quick shortcut so we dont do unnecessary work.
const settingsCompression = settings?.imageCompression ?? false;
const configWidth = config?.file?.image_compression?.width ?? null;
const configHeight = config?.file?.image_compression?.height ?? null;
2025-06-16 20:52:57 +08:00
2025-07-03 04:04:13 +08:00
// If neither settings nor config wants compression, return original URL.
if (!settingsCompression && !configWidth && !configHeight) {
return imageUrl;
}
2024-12-25 14:28:14 +08:00
2025-07-03 04:04:13 +08:00
// Default to null (no compression unless set)
let width = null;
let height = null;
// If user/settings want compression, pick their preferred size.
if (settingsCompression) {
width = settings?.imageCompressionSize?.width ?? null;
height = settings?.imageCompressionSize?.height ?? null;
}
// Apply config limits as an upper bound if any
if (configWidth && (width === null || width > configWidth)) {
width = configWidth;
}
if (configHeight && (height === null || height > configHeight)) {
height = configHeight;
}
// Do the compression if required
if (width || height) {
return await compressImage(imageUrl, width, height);
2024-12-25 14:28:14 +08:00
}
2025-07-03 04:04:13 +08:00
return imageUrl;
};
let reader = new FileReader();
reader.onload = async (event) => {
let imageUrl = event.target.result;
imageUrl = await compressImageHandler(imageUrl, $settings, $config);
2024-12-25 14:28:14 +08:00
files = [
...files,
{
type: 'image',
2024-12-25 14:28:14 +08:00
url: `${imageUrl}`
}
];
};
reader.readAsDataURL(file['type'] === 'image/heic' ? await convertHeicToJpeg(file) : file);
} else {
uploadFileHandler(file);
}
2024-08-27 21:51:40 +08:00
});
};
2024-10-09 14:57:36 +08:00
const onDragOver = (e) => {
e.preventDefault();
// Check if a file is being dragged.
if (e.dataTransfer?.types?.includes('Files')) {
dragged = true;
} else {
dragged = false;
}
2024-10-09 14:57:36 +08:00
};
2024-04-07 16:03:16 +08:00
2024-10-09 14:57:36 +08:00
const onDragLeave = () => {
dragged = false;
};
2024-01-08 17:12:02 +08:00
2024-10-09 14:57:36 +08:00
const onDrop = async (e) => {
e.preventDefault();
console.log(e);
2023-12-20 06:50:43 +08:00
2024-10-09 14:57:36 +08:00
if (e.dataTransfer?.files) {
const inputFiles = Array.from(e.dataTransfer?.files);
if (inputFiles && inputFiles.length > 0) {
console.log(inputFiles);
inputFilesHandler(inputFiles);
2023-12-20 06:50:43 +08:00
}
2024-10-09 14:57:36 +08:00
}
2023-12-20 06:50:43 +08:00
2024-10-09 14:57:36 +08:00
dragged = false;
};
const onKeyDown = (e) => {
if (e.key === 'Shift') {
shiftKey = true;
}
if (e.key === 'Escape') {
console.log('Escape');
dragged = false;
}
};
const onKeyUp = (e) => {
if (e.key === 'Shift') {
shiftKey = false;
}
};
const onFocus = () => {};
const onBlur = () => {
shiftKey = false;
};
2024-11-17 08:01:02 +08:00
onMount(async () => {
2025-09-13 00:31:57 +08:00
suggestions = [
{
char: '@',
render: getSuggestionRenderer(CommandSuggestionList, {
i18n,
onSelect: (e) => {
const { type, data } = e;
if (type === 'model') {
atSelectedModel = data;
}
document.getElementById('chat-input')?.focus();
},
insertTextHandler: insertTextAtCursor,
onUpload: (e) => {
const { type, data } = e;
if (type === 'file') {
if (files.find((f) => f.id === data.id)) {
return;
}
files = [
...files,
{
...data,
status: 'processed'
}
];
} else {
dispatch('upload', e);
}
}
})
},
{
char: '/',
render: getSuggestionRenderer(CommandSuggestionList, {
i18n,
onSelect: (e) => {
const { type, data } = e;
if (type === 'model') {
atSelectedModel = data;
}
document.getElementById('chat-input')?.focus();
},
insertTextHandler: insertTextAtCursor,
onUpload: (e) => {
const { type, data } = e;
if (type === 'file') {
if (files.find((f) => f.id === data.id)) {
return;
}
files = [
...files,
{
...data,
status: 'processed'
}
];
} else {
dispatch('upload', e);
}
}
})
},
{
char: '#',
render: getSuggestionRenderer(CommandSuggestionList, {
i18n,
onSelect: (e) => {
const { type, data } = e;
if (type === 'model') {
atSelectedModel = data;
}
document.getElementById('chat-input')?.focus();
},
insertTextHandler: insertTextAtCursor,
onUpload: (e) => {
const { type, data } = e;
if (type === 'file') {
if (files.find((f) => f.id === data.id)) {
return;
}
files = [
...files,
{
...data,
status: 'processed'
}
];
} else {
dispatch('upload', e);
}
}
})
}
];
console.log(suggestions);
2024-11-17 08:01:02 +08:00
loaded = true;
2024-10-19 14:54:35 +08:00
window.setTimeout(() => {
const chatInput = document.getElementById('chat-input');
chatInput?.focus();
}, 0);
2023-12-20 06:53:14 +08:00
window.addEventListener('keydown', onKeyDown);
window.addEventListener('keyup', onKeyUp);
window.addEventListener('focus', onFocus);
window.addEventListener('blur', onBlur);
2024-04-07 16:03:16 +08:00
2024-11-18 14:06:58 +08:00
await tick();
const dropzoneElement = document.getElementById('chat-container');
2024-10-09 14:57:36 +08:00
2024-11-18 14:06:58 +08:00
dropzoneElement?.addEventListener('dragover', onDragOver);
dropzoneElement?.addEventListener('drop', onDrop);
dropzoneElement?.addEventListener('dragleave', onDragLeave);
await tools.set(await getTools(localStorage.token));
2024-10-09 14:57:36 +08:00
});
2024-01-08 17:12:02 +08:00
2024-10-09 14:57:36 +08:00
onDestroy(() => {
2024-11-17 13:50:31 +08:00
console.log('destroy');
window.removeEventListener('keydown', onKeyDown);
window.removeEventListener('keyup', onKeyUp);
window.removeEventListener('focus', onFocus);
window.removeEventListener('blur', onBlur);
2024-04-07 16:03:16 +08:00
2024-11-18 14:06:58 +08:00
const dropzoneElement = document.getElementById('chat-container');
2024-10-09 14:57:36 +08:00
2024-11-17 13:50:31 +08:00
if (dropzoneElement) {
dropzoneElement?.removeEventListener('dragover', onDragOver);
dropzoneElement?.removeEventListener('drop', onDrop);
dropzoneElement?.removeEventListener('dragleave', onDragLeave);
}
2023-12-20 06:50:43 +08:00
});
2023-11-20 09:47:07 +08:00
</script>
2024-07-17 18:02:54 +08:00
<FilesOverlay show={dragged} />
2025-04-05 15:05:47 +08:00
<ToolServersModal bind:show={showTools} {selectedToolIds} />
2025-09-14 15:08:23 +08:00
<InputVariablesModal
bind:show={showInputVariablesModal}
variables={inputVariables}
onSave={inputVariablesModalCallback}
/>
2025-03-29 03:40:56 +08:00
2024-11-17 08:01:02 +08:00
{#if loaded}
<div class="w-full font-primary">
2024-12-02 15:29:21 +08:00
<div class=" mx-auto inset-x-0 bg-transparent flex justify-center">
2024-12-25 12:30:30 +08:00
<div
class="flex flex-col px-3 {($settings?.widescreenMode ?? null)
? 'max-w-full'
: 'max-w-6xl'} w-full"
>
2024-11-17 08:01:02 +08:00
<div class="relative">
{#if autoScroll === false && history?.currentId}
<div
class=" absolute -top-12 left-0 right-0 flex justify-center z-30 pointer-events-none"
2024-06-02 09:11:54 +08:00
>
<button
2024-11-17 08:01:02 +08:00
class=" bg-white border border-gray-100 dark:border-none dark:bg-white/20 p-1.5 rounded-full pointer-events-auto"
2024-06-02 09:11:54 +08:00
on:click={() => {
2024-11-17 08:01:02 +08:00
autoScroll = true;
scrollToBottom();
2024-06-02 09:11:54 +08:00
}}
>
2024-11-17 08:01:02 +08:00
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="w-5 h-5"
>
<path
fill-rule="evenodd"
d="M10 3a.75.75 0 01.75.75v10.638l3.96-4.158a.75.75 0 111.08 1.04l-5.25 5.5a.75.75 0 01-1.08 0l-5.25-5.5a.75.75 0 111.08-1.04l3.96 4.158V3.75A.75.75 0 0110 3z"
clip-rule="evenodd"
2024-11-11 11:11:06 +08:00
/>
2024-11-17 08:01:02 +08:00
</svg>
2024-06-02 09:11:54 +08:00
</button>
</div>
2024-11-17 08:01:02 +08:00
{/if}
</div>
2023-12-14 08:21:50 +08:00
</div>
2024-01-02 16:55:28 +08:00
</div>
2024-05-02 17:20:57 +08:00
2025-08-08 23:58:53 +08:00
<div class="bg-transparent">
2024-12-25 11:20:38 +08:00
<div
class="{($settings?.widescreenMode ?? null)
? 'max-w-full'
: 'max-w-6xl'} px-2.5 mx-auto inset-x-0"
>
2024-11-17 08:01:02 +08:00
<div class="">
<input
bind:this={filesInputElement}
bind:files={inputFiles}
type="file"
hidden
multiple
on:change={async () => {
if (inputFiles && inputFiles.length > 0) {
const _inputFiles = Array.from(inputFiles);
inputFilesHandler(_inputFiles);
} else {
toast.error($i18n.t(`File not found.`));
}
2024-06-19 04:50:18 +08:00
2024-11-17 08:01:02 +08:00
filesInputElement.value = '';
}}
/>
2024-06-07 11:33:23 +08:00
2024-11-17 08:01:02 +08:00
{#if recording}
<VoiceRecording
bind:recording
2025-05-04 02:53:23 +08:00
onCancel={async () => {
2024-11-17 08:01:02 +08:00
recording = false;
2024-06-07 11:33:23 +08:00
2024-11-17 08:01:02 +08:00
await tick();
document.getElementById('chat-input')?.focus();
}}
2025-05-04 02:53:23 +08:00
onConfirm={async (data) => {
const { text, filename } = data;
2024-06-07 11:33:23 +08:00
2024-11-17 08:01:02 +08:00
recording = false;
2024-06-07 11:33:23 +08:00
2025-07-11 16:15:13 +08:00
await tick();
insertTextAtCursor(text);
2024-11-17 08:01:02 +08:00
await tick();
document.getElementById('chat-input')?.focus();
2024-06-07 12:56:09 +08:00
2024-11-17 08:01:02 +08:00
if ($settings?.speechAutoSend ?? false) {
dispatch('submit', prompt);
}
}}
/>
{:else}
<form
2025-06-04 06:36:31 +08:00
class="w-full flex flex-col gap-1.5"
2024-11-17 08:01:02 +08:00
on:submit|preventDefault={() => {
// check if selectedModels support image input
2024-10-05 18:07:56 +08:00
dispatch('submit', prompt);
2024-11-17 08:01:02 +08:00
}}
2024-06-07 11:33:23 +08:00
>
2024-11-17 08:01:02 +08:00
<div
2025-09-16 02:25:36 +08:00
class="flex-1 flex flex-col relative w-full shadow-lg rounded-3xl border {$temporaryChatEnabled
2025-09-16 02:36:51 +08:00
? 'border-dashed border-gray-100 dark:border-gray-800 hover:border-gray-200 focus-within:border-gray-200 hover:dark:border-gray-700 focus-within:dark:border-gray-700'
2025-09-23 02:46:47 +08:00
: ' border-gray-100 dark:border-gray-850 hover:border-gray-200 focus-within:border-gray-100 hover:dark:border-gray-800 focus-within:dark:border-gray-800'} transition px-1 bg-white/5 dark:bg-gray-500/5 backdrop-blur-sm dark:text-gray-100"
2025-04-07 08:02:39 +08:00
dir={$settings?.chatDirection ?? 'auto'}
2024-11-17 08:01:02 +08:00
>
2025-09-13 00:31:57 +08:00
{#if atSelectedModel !== undefined}
<div class="px-3 pt-3 text-left w-full flex flex-col z-10">
<div class="flex items-center justify-between w-full">
<div class="pl-[1px] flex items-center gap-2 text-sm dark:text-gray-500">
<img
crossorigin="anonymous"
alt="model profile"
class="size-3.5 max-w-[28px] object-cover rounded-full"
src={$models.find((model) => model.id === atSelectedModel.id)?.info?.meta
?.profile_image_url ??
($i18n.language === 'dg-DG'
? `${WEBUI_BASE_URL}/doge.png`
: `${WEBUI_BASE_URL}/static/favicon.png`)}
/>
<div class="translate-y-[0.5px]">
<span class="">{atSelectedModel.name}</span>
</div>
</div>
<div>
<button
class="flex items-center dark:text-gray-500"
on:click={() => {
atSelectedModel = undefined;
}}
>
<XMark />
</button>
</div>
</div>
</div>
{/if}
2024-11-17 08:01:02 +08:00
{#if files.length > 0}
2025-09-13 06:42:10 +08:00
<div class="mx-2 mt-2.5 pb-1.5 flex items-center flex-wrap gap-2">
2024-11-17 08:01:02 +08:00
{#each files as file, fileIdx}
{#if file.type === 'image'}
<div class=" relative group">
2025-01-23 03:04:12 +08:00
<div class="relative flex items-center">
2024-12-18 17:27:32 +08:00
<Image
2024-11-17 08:01:02 +08:00
src={file.url}
alt=""
2025-09-13 00:31:57 +08:00
imageClassName=" size-10 rounded-xl object-cover"
2024-11-17 08:01:02 +08:00
/>
{#if atSelectedModel ? visionCapableModels.length === 0 : selectedModels.length !== visionCapableModels.length}
<Tooltip
className=" absolute top-1 left-1"
content={$i18n.t('{{ models }}', {
models: [
...(atSelectedModel ? [atSelectedModel] : selectedModels)
]
.filter((id) => !visionCapableModels.includes(id))
.join(', ')
})}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
aria-hidden="true"
2024-11-17 08:01:02 +08:00
class="size-4 fill-yellow-300"
>
<path
fill-rule="evenodd"
d="M9.401 3.003c1.155-2 4.043-2 5.197 0l7.355 12.748c1.154 2-.29 4.5-2.599 4.5H4.645c-2.309 0-3.752-2.5-2.598-4.5L9.4 3.003ZM12 8.25a.75.75 0 0 1 .75.75v3.75a.75.75 0 0 1-1.5 0V9a.75.75 0 0 1 .75-.75Zm0 8.25a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5Z"
clip-rule="evenodd"
/>
</svg>
</Tooltip>
{/if}
</div>
<div class=" absolute -top-1 -right-1">
<button
class=" bg-white text-black border border-white rounded-full {($settings?.highContrastMode ??
false)
? ''
: 'outline-hidden focus:outline-hidden group-hover:visible invisible transition'}"
2024-11-17 08:01:02 +08:00
type="button"
aria-label={$i18n.t('Remove file')}
2024-11-17 08:01:02 +08:00
on:click={() => {
files.splice(fileIdx, 1);
files = files;
}}
2024-06-07 11:33:23 +08:00
>
<svg
xmlns="http://www.w3.org/2000/svg"
2024-11-17 08:01:02 +08:00
viewBox="0 0 20 20"
2024-06-07 11:33:23 +08:00
fill="currentColor"
aria-hidden="true"
2025-02-10 14:19:02 +08:00
class="size-4"
2024-06-07 11:33:23 +08:00
>
<path
2024-11-17 08:01:02 +08:00
d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
2024-06-07 11:33:23 +08:00
/>
</svg>
2024-11-17 08:01:02 +08:00
</button>
</div>
2024-06-07 11:33:23 +08:00
</div>
2024-11-17 08:01:02 +08:00
{:else}
<FileItem
item={file}
name={file.name}
type={file.type}
size={file?.size}
loading={file.status === 'uploading'}
dismissible={true}
edit={true}
2025-09-13 00:31:57 +08:00
small={true}
2025-07-09 05:31:28 +08:00
modal={['file', 'collection'].includes(file?.type)}
2025-01-14 01:21:00 +08:00
on:dismiss={async () => {
2025-01-23 03:04:12 +08:00
// Remove from UI state
files.splice(fileIdx, 1);
files = files;
2024-11-17 08:01:02 +08:00
}}
on:click={() => {
console.log(file);
}}
/>
{/if}
{/each}
</div>
{/if}
2024-06-02 09:11:54 +08:00
2025-02-04 12:06:17 +08:00
<div class="px-2.5">
2025-09-13 00:54:34 +08:00
<div
2025-09-16 06:21:02 +08:00
class="scrollbar-hidden rtl:text-right ltr:text-left bg-transparent dark:text-gray-100 outline-hidden w-full pb-1 px-1 resize-none h-fit max-h-96 overflow-auto {files.length ===
2025-09-13 06:42:10 +08:00
0
2025-09-13 07:09:27 +08:00
? atSelectedModel !== undefined
? 'pt-1.5'
: 'pt-2.5'
2025-09-13 06:42:10 +08:00
: ''}"
2025-09-13 00:54:34 +08:00
id="chat-input-container"
>
{#if suggestions}
{#key $settings?.richTextInput ?? true}
2025-09-13 00:31:57 +08:00
{#key $settings?.showFormattingToolbar ?? false}
<RichTextInput
bind:this={chatInputElement}
id="chat-input"
onChange={(e) => {
prompt = e.md;
command = getCommand();
}}
json={true}
2025-09-13 00:54:34 +08:00
richText={$settings?.richTextInput ?? true}
2025-09-13 00:31:57 +08:00
messageInput={true}
showFormattingToolbar={$settings?.showFormattingToolbar ?? false}
floatingMenuPlacement={'top-start'}
insertPromptAsRichText={$settings?.insertPromptAsRichText ?? false}
shiftEnter={!($settings?.ctrlEnterToSend ?? false) &&
(!$mobile ||
!(
'ontouchstart' in window ||
navigator.maxTouchPoints > 0 ||
navigator.msMaxTouchPoints > 0
))}
placeholder={placeholder ? placeholder : $i18n.t('Send a Message')}
largeTextAsFile={($settings?.largeTextAsFile ?? false) && !shiftKey}
autocomplete={$config?.features?.enable_autocomplete_generation &&
($settings?.promptAutocomplete ?? false)}
generateAutoCompletion={async (text) => {
if (selectedModelIds.length === 0 || !selectedModelIds.at(0)) {
toast.error($i18n.t('Please select a model first.'));
2024-11-26 14:43:34 +08:00
}
2025-08-06 16:21:18 +08:00
2025-09-13 00:31:57 +08:00
const res = await generateAutoCompletion(
localStorage.token,
selectedModelIds.at(0),
text,
history?.currentId
? createMessagesList(history, history.currentId)
: null
).catch((error) => {
console.log(error);
return null;
});
console.log(res);
return res;
}}
{suggestions}
oncompositionstart={() => (isComposing = true)}
oncompositionend={(e) => {
compositionEndedAt = e.timeStamp;
isComposing = false;
}}
on:keydown={async (e) => {
e = e.detail.event;
const isCtrlPressed = e.ctrlKey || e.metaKey; // metaKey is for Cmd key on Mac
const suggestionsContainerElement =
document.getElementById('suggestions-container');
if (e.key === 'Escape') {
stopResponse();
2025-03-06 11:36:30 +08:00
}
2025-09-13 00:31:57 +08:00
// Command/Ctrl + Shift + Enter to submit a message pair
if (isCtrlPressed && e.key === 'Enter' && e.shiftKey) {
2025-08-06 16:21:18 +08:00
e.preventDefault();
2025-09-13 00:31:57 +08:00
createMessagePair(prompt);
2025-08-06 16:21:18 +08:00
}
2025-09-13 00:31:57 +08:00
// Check if Ctrl + R is pressed
if (prompt === '' && isCtrlPressed && e.key.toLowerCase() === 'r') {
2024-11-26 14:43:34 +08:00
e.preventDefault();
2025-09-13 00:31:57 +08:00
console.log('regenerate');
2025-08-06 16:21:18 +08:00
2025-09-13 00:31:57 +08:00
const regenerateButton = [
...document.getElementsByClassName('regenerate-response-button')
2025-08-06 16:21:18 +08:00
]?.at(-1);
2025-09-13 00:31:57 +08:00
regenerateButton?.click();
2025-08-06 16:21:18 +08:00
}
2025-09-13 00:31:57 +08:00
if (prompt === '' && e.key == 'ArrowUp') {
2025-08-06 16:21:18 +08:00
e.preventDefault();
2025-09-13 00:31:57 +08:00
const userMessageElement = [
...document.getElementsByClassName('user-message')
2025-08-06 16:21:18 +08:00
]?.at(-1);
2025-09-13 00:31:57 +08:00
if (userMessageElement) {
userMessageElement.scrollIntoView({ block: 'center' });
const editButton = [
...document.getElementsByClassName('edit-user-message-button')
]?.at(-1);
editButton?.click();
}
2024-11-26 14:43:34 +08:00
}
2025-08-06 16:21:18 +08:00
2025-09-13 00:31:57 +08:00
if (!suggestionsContainerElement) {
if (
!$mobile ||
!(
'ontouchstart' in window ||
navigator.maxTouchPoints > 0 ||
navigator.msMaxTouchPoints > 0
)
) {
if (inOrNearComposition(e)) {
return;
2025-08-06 16:21:18 +08:00
}
2025-09-13 00:31:57 +08:00
// Uses keyCode '13' for Enter key for chinese/japanese keyboards.
//
// Depending on the user's settings, it will send the message
// either when Enter is pressed or when Ctrl+Enter is pressed.
const enterPressed =
($settings?.ctrlEnterToSend ?? false)
? (e.key === 'Enter' || e.keyCode === 13) && isCtrlPressed
: (e.key === 'Enter' || e.keyCode === 13) && !e.shiftKey;
2025-09-13 00:31:57 +08:00
if (enterPressed) {
2025-08-06 16:21:18 +08:00
e.preventDefault();
2025-09-13 00:31:57 +08:00
if (prompt !== '' || files.length > 0) {
dispatch('submit', prompt);
}
2025-08-06 16:21:18 +08:00
}
2025-09-13 00:31:57 +08:00
}
}
2025-08-06 16:21:18 +08:00
2025-09-13 00:31:57 +08:00
if (e.key === 'Escape') {
console.log('Escape');
atSelectedModel = undefined;
selectedToolIds = [];
selectedFilterIds = [];
webSearchEnabled = false;
imageGenerationEnabled = false;
codeInterpreterEnabled = false;
}
}}
on:paste={async (e) => {
e = e.detail.event;
console.log(e);
const clipboardData = e.clipboardData || window.clipboardData;
if (clipboardData && clipboardData.items) {
for (const item of clipboardData.items) {
if (item.type.indexOf('image') !== -1) {
const blob = item.getAsFile();
const reader = new FileReader();
reader.onload = function (e) {
files = [
...files,
2025-08-06 16:21:18 +08:00
{
2025-09-13 00:31:57 +08:00
type: 'image',
url: `${e.target.result}`
2025-08-06 16:21:18 +08:00
}
2025-09-13 00:31:57 +08:00
];
};
reader.readAsDataURL(blob);
} else if (item?.kind === 'file') {
const file = item.getAsFile();
if (file) {
const _files = [file];
await inputFilesHandler(_files);
e.preventDefault();
}
} else if (item.type === 'text/plain') {
if (($settings?.largeTextAsFile ?? false) && !shiftKey) {
const text = clipboardData.getData('text/plain');
if (text.length > PASTED_TEXT_CHARACTER_LIMIT) {
e.preventDefault();
const blob = new Blob([text], { type: 'text/plain' });
const file = new File(
[blob],
`Pasted_Text_${Date.now()}.txt`,
{
type: 'text/plain'
}
);
await uploadFileHandler(file, true);
}
2025-08-06 16:21:18 +08:00
}
}
}
2024-11-17 08:01:02 +08:00
}
2025-09-13 00:31:57 +08:00
}}
/>
{/key}
2025-09-13 00:54:34 +08:00
{/key}
{/if}
</div>
2025-02-04 11:18:31 +08:00
</div>
2025-05-23 21:30:17 +08:00
<div class=" flex justify-between mt-0.5 mb-2.5 mx-0.5 max-w-full" dir="ltr">
<div class="ml-1 self-end flex items-center flex-1 max-w-[80%]">
2025-02-04 11:18:31 +08:00
<InputMenu
2025-09-14 16:06:02 +08:00
bind:files
2025-05-17 05:13:13 +08:00
selectedModels={atSelectedModel ? [atSelectedModel.id] : selectedModels}
{fileUploadCapableModels}
2025-02-04 11:18:31 +08:00
{screenCaptureHandler}
2025-02-04 12:06:17 +08:00
{inputFilesHandler}
2025-02-04 11:18:31 +08:00
uploadFilesHandler={() => {
filesInputElement.click();
}}
uploadGoogleDriveHandler={async () => {
try {
const fileData = await createPicker();
if (fileData) {
const file = new File([fileData.blob], fileData.name, {
type: fileData.blob.type
});
await uploadFileHandler(file);
} else {
console.log('No file was selected from Google Drive');
}
} catch (error) {
console.error('Google Drive Error:', error);
toast.error(
$i18n.t('Error accessing Google Drive: {{error}}', {
error: error.message
})
);
}
}}
2025-04-14 23:27:59 +08:00
uploadOneDriveHandler={async (authorityType) => {
2025-02-24 16:27:37 +08:00
try {
2025-04-14 23:27:59 +08:00
const fileData = await pickAndDownloadFile(authorityType);
2025-02-24 16:27:37 +08:00
if (fileData) {
const file = new File([fileData.blob], fileData.name, {
2025-02-24 22:14:10 +08:00
type: fileData.blob.type || 'application/octet-stream'
2025-02-24 16:27:37 +08:00
});
await uploadFileHandler(file);
} else {
console.log('No file was selected from OneDrive');
}
} catch (error) {
console.error('OneDrive Error:', error);
}
}}
2025-09-25 00:11:26 +08:00
onUpload={async (e) => {
dispatch('upload', e);
}}
2025-02-04 11:18:31 +08:00
onClose={async () => {
await tick();
const chatInput = document.getElementById('chat-input');
chatInput?.focus();
}}
>
<div
2025-09-12 19:05:37 +08:00
class="bg-transparent hover:bg-gray-100 text-gray-700 dark:text-white dark:hover:bg-gray-800 rounded-full size-8 flex justify-center items-center outline-hidden focus:outline-hidden"
2025-02-04 11:18:31 +08:00
>
2025-09-12 19:05:37 +08:00
<PlusAlt className="size-5.5" />
</div>
2025-02-04 11:18:31 +08:00
</InputMenu>
2025-02-04 11:34:36 +08:00
2025-09-23 02:46:47 +08:00
<div
class="flex self-center w-[1px] h-4 mx-1 bg-gray-200/50 dark:bg-gray-800/50"
/>
2025-09-12 19:05:37 +08:00
2025-09-13 23:04:07 +08:00
{#if showWebSearchButton || showImageGenerationButton || showCodeInterpreterButton || showToolsButton || (toggleFilters && toggleFilters.length > 0)}
<IntegrationsMenu
selectedModels={atSelectedModel ? [atSelectedModel.id] : selectedModels}
{toggleFilters}
{showWebSearchButton}
{showImageGenerationButton}
{showCodeInterpreterButton}
bind:selectedToolIds
bind:selectedFilterIds
bind:webSearchEnabled
bind:imageGenerationEnabled
bind:codeInterpreterEnabled
onClose={async () => {
await tick();
const chatInput = document.getElementById('chat-input');
chatInput?.focus();
}}
2025-09-12 19:05:37 +08:00
>
2025-09-13 23:04:07 +08:00
<div
class="bg-transparent hover:bg-gray-100 text-gray-700 dark:text-white dark:hover:bg-gray-800 rounded-full size-8 flex justify-center items-center outline-hidden focus:outline-hidden"
>
<Component className="size-4.5" strokeWidth="1.5" />
</div>
</IntegrationsMenu>
{/if}
2025-09-12 19:05:37 +08:00
2025-09-13 02:00:33 +08:00
<div class="ml-1 flex gap-1.5">
{#if (selectedToolIds ?? []).length > 0}
2025-09-12 19:05:37 +08:00
<Tooltip
content={$i18n.t('{{COUNT}} Available Tools', {
COUNT: selectedToolIds.length
2025-09-12 19:05:37 +08:00
})}
>
<button
2025-09-12 19:41:12 +08:00
class="translate-y-[0.5px] px-1 flex gap-1 items-center text-gray-600 dark:text-gray-300 hover:text-gray-700 dark:hover:text-gray-200 rounded-lg self-center transition"
2025-09-12 19:05:37 +08:00
aria-label="Available Tools"
type="button"
on:click={() => {
showTools = !showTools;
}}
2025-04-05 15:05:47 +08:00
>
2025-09-12 19:05:37 +08:00
<Wrench className="size-4" strokeWidth="1.75" />
2025-04-05 15:05:47 +08:00
2025-09-12 19:41:12 +08:00
<span class="text-sm">
{selectedToolIds.length}
2025-09-12 19:05:37 +08:00
</span>
</button>
</Tooltip>
{/if}
2025-04-05 15:05:47 +08:00
2025-09-12 19:05:37 +08:00
{#each selectedFilterIds as filterId}
{@const filter = toggleFilters.find((f) => f.id === filterId)}
{#if filter}
2025-09-12 19:41:12 +08:00
<Tooltip content={filter?.name} placement="top">
2025-05-17 03:21:08 +08:00
<button
on:click|preventDefault={() => {
2025-09-12 19:05:37 +08:00
selectedFilterIds = selectedFilterIds.filter(
(id) => id !== filterId
);
2025-05-17 03:21:08 +08:00
}}
type="button"
2025-09-13 03:28:31 +08:00
class="group p-[7px] flex gap-1.5 items-center text-sm rounded-full transition-colors duration-300 focus:outline-hidden max-w-full overflow-hidden hover:bg-gray-50 dark:hover:bg-gray-800 {selectedFilterIds.includes(
2025-09-12 19:05:37 +08:00
filterId
2025-05-17 03:21:08 +08:00
)
2025-09-13 03:28:31 +08:00
? 'text-sky-500 dark:text-sky-300 bg-sky-50 dark:bg-sky-400/10 border border-sky-200/40 dark:border-sky-500/20'
2025-05-23 21:30:17 +08:00
: 'bg-transparent text-gray-600 dark:text-gray-300 '} capitalize"
2025-05-17 03:21:08 +08:00
>
2025-05-17 03:59:24 +08:00
{#if filter?.icon}
2025-05-23 21:30:17 +08:00
<div class="size-4 items-center flex justify-center">
2025-05-17 03:21:08 +08:00
<img
2025-05-17 03:59:24 +08:00
src={filter.icon}
2025-05-23 21:30:17 +08:00
class="size-3.5 {filter.icon.includes('svg')
2025-05-17 03:21:08 +08:00
? 'dark:invert-[80%]'
: ''}"
style="fill: currentColor;"
2025-05-17 03:59:24 +08:00
alt={filter.name}
2025-05-17 03:21:08 +08:00
/>
</div>
{:else}
2025-05-23 21:30:17 +08:00
<Sparkles className="size-4" strokeWidth="1.75" />
2025-05-17 03:21:08 +08:00
{/if}
2025-09-12 19:05:37 +08:00
<div class="hidden group-hover:block">
<XMark className="size-4" strokeWidth="1.75" />
</div>
2025-02-05 17:03:40 +08:00
</button>
</Tooltip>
{/if}
2025-09-12 19:05:37 +08:00
{/each}
{#if webSearchEnabled}
2025-09-12 19:41:12 +08:00
<Tooltip content={$i18n.t('Web Search')} placement="top">
2025-09-12 19:05:37 +08:00
<button
on:click|preventDefault={() => (webSearchEnabled = !webSearchEnabled)}
type="button"
2025-09-13 03:28:31 +08:00
class="group p-[7px] flex gap-1.5 items-center text-sm rounded-full transition-colors duration-300 focus:outline-hidden max-w-full overflow-hidden hover:bg-gray-50 dark:hover:bg-gray-800 {webSearchEnabled ||
2025-09-12 19:05:37 +08:00
($settings?.webSearch ?? false) === 'always'
2025-09-13 03:28:31 +08:00
? ' text-sky-500 dark:text-sky-300 bg-sky-50 dark:bg-sky-400/10 border border-sky-200/40 dark:border-sky-500/20'
2025-09-12 19:05:37 +08:00
: 'bg-transparent text-gray-600 dark:text-gray-300 '}"
>
<GlobeAlt className="size-4" strokeWidth="1.75" />
<div class="hidden group-hover:block">
<XMark className="size-4" strokeWidth="1.75" />
</div>
</button>
</Tooltip>
{/if}
{#if imageGenerationEnabled}
2025-09-12 19:41:12 +08:00
<Tooltip content={$i18n.t('Image')} placement="top">
2025-09-12 19:05:37 +08:00
<button
on:click|preventDefault={() =>
(imageGenerationEnabled = !imageGenerationEnabled)}
type="button"
2025-09-13 03:28:31 +08:00
class="group p-[7px] flex gap-1.5 items-center text-sm rounded-full transition-colors duration-300 focus:outline-hidden max-w-full overflow-hidden hover:bg-gray-50 dark:hover:bg-gray-800 {imageGenerationEnabled
? ' text-sky-500 dark:text-sky-300 bg-sky-50 dark:bg-sky-400/10 border border-sky-200/40 dark:border-sky-500/20'
2025-09-12 19:05:37 +08:00
: 'bg-transparent text-gray-600 dark:text-gray-300 '}"
>
<Photo className="size-4" strokeWidth="1.75" />
<div class="hidden group-hover:block">
<XMark className="size-4" strokeWidth="1.75" />
</div>
</button>
</Tooltip>
{/if}
{#if codeInterpreterEnabled}
2025-09-12 19:41:12 +08:00
<Tooltip content={$i18n.t('Code Interpreter')} placement="top">
2025-09-12 19:05:37 +08:00
<button
aria-label={codeInterpreterEnabled
? $i18n.t('Disable Code Interpreter')
: $i18n.t('Enable Code Interpreter')}
aria-pressed={codeInterpreterEnabled}
on:click|preventDefault={() =>
(codeInterpreterEnabled = !codeInterpreterEnabled)}
type="button"
2025-09-13 03:28:31 +08:00
class=" group p-[7px] flex gap-1.5 items-center text-sm transition-colors duration-300 max-w-full overflow-hidden hover:bg-gray-50 dark:hover:bg-gray-800 {codeInterpreterEnabled
? ' text-sky-500 dark:text-sky-300 bg-sky-50 dark:bg-sky-400/10 border border-sky-200/40 dark:border-sky-500/20'
2025-09-12 19:05:37 +08:00
: 'bg-transparent text-gray-600 dark:text-gray-300 '} {($settings?.highContrastMode ??
false)
? 'm-1'
: 'focus:outline-hidden rounded-full'}"
>
<Terminal className="size-3.5" strokeWidth="2" />
<div class="hidden group-hover:block">
<XMark className="size-4" strokeWidth="1.75" />
</div>
</button>
</Tooltip>
{/if}
</div>
2025-02-04 11:18:31 +08:00
</div>
2024-05-02 17:20:57 +08:00
2025-02-16 11:27:25 +08:00
<div class="self-end flex space-x-1 mr-1 shrink-0">
2025-04-14 16:40:22 +08:00
{#if (!history?.currentId || history.messages[history.currentId]?.done == true) && ($_user?.role === 'admin' || ($_user?.permissions?.chat?.stt ?? true))}
2025-05-24 05:40:01 +08:00
<!-- {$i18n.t('Record voice')} -->
<Tooltip content={$i18n.t('Dictate')}>
2024-11-17 08:01:02 +08:00
<button
id="voice-input-button"
2024-12-02 15:16:00 +08:00
class=" text-gray-600 dark:text-gray-300 hover:text-gray-700 dark:hover:text-gray-200 transition rounded-full p-1.5 mr-0.5 self-center"
2024-11-17 08:01:02 +08:00
type="button"
on:click={async () => {
try {
let stream = await navigator.mediaDevices
.getUserMedia({ audio: true })
.catch(function (err) {
toast.error(
$i18n.t(
`Permission denied when accessing microphone: {{error}}`,
{
error: err
}
)
);
return null;
});
if (stream) {
recording = true;
const tracks = stream.getTracks();
tracks.forEach((track) => track.stop());
}
stream = null;
} catch {
toast.error($i18n.t('Permission denied when accessing microphone'));
2024-06-08 06:18:45 +08:00
}
2024-11-17 08:01:02 +08:00
}}
aria-label="Voice Input"
2024-06-02 09:11:54 +08:00
>
2024-11-17 08:01:02 +08:00
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="w-5 h-5 translate-y-[0.5px]"
>
<path d="M7 4a3 3 0 016 0v6a3 3 0 11-6 0V4z" />
<path
d="M5.5 9.643a.75.75 0 00-1.5 0V10c0 3.06 2.29 5.585 5.25 5.954V17.5h-1.5a.75.75 0 000 1.5h4.5a.75.75 0 000-1.5h-1.5v-1.546A6.001 6.001 0 0016 10v-.357a.75.75 0 00-1.5 0V10a4.5 4.5 0 01-9 0v-.357z"
/>
</svg>
</button>
</Tooltip>
{/if}
2024-06-08 06:12:34 +08:00
2025-08-06 02:25:51 +08:00
{#if (taskIds && taskIds.length > 0) || (history.currentId && history.messages[history.currentId]?.done != true) || generating}
2025-04-13 11:51:02 +08:00
<div class=" flex items-center">
<Tooltip content={$i18n.t('Stop')}>
<button
class="bg-white hover:bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-white dark:hover:bg-gray-800 transition rounded-full p-1.5"
on:click={() => {
stopResponse();
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
class="size-5"
>
<path
fill-rule="evenodd"
d="M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12zm6-2.438c0-.724.588-1.312 1.313-1.312h4.874c.725 0 1.313.588 1.313 1.313v4.874c0 .725-.588 1.313-1.313 1.313H9.564a1.312 1.312 0 01-1.313-1.313V9.564z"
clip-rule="evenodd"
/>
</svg>
</button>
</Tooltip>
</div>
2025-04-14 16:40:22 +08:00
{:else if prompt === '' && files.length === 0 && ($_user?.role === 'admin' || ($_user?.permissions?.chat?.call ?? true))}
2025-04-13 11:51:02 +08:00
<div class=" flex items-center">
2025-05-24 05:40:01 +08:00
<!-- {$i18n.t('Call')} -->
<Tooltip content={$i18n.t('Voice mode')}>
2025-04-13 11:51:02 +08:00
<button
class=" bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full p-1.5 self-center"
type="button"
on:click={async () => {
if (selectedModels.length > 1) {
toast.error($i18n.t('Select only one model to call'));
2024-08-24 00:22:50 +08:00
2025-04-13 11:51:02 +08:00
return;
}
2024-06-08 11:57:15 +08:00
2025-04-13 11:51:02 +08:00
if ($config.audio.stt.engine === 'web') {
toast.error(
$i18n.t('Call feature is not supported when using Web STT engine')
);
2024-06-08 11:57:15 +08:00
2025-04-13 11:51:02 +08:00
return;
}
// check if user has access to getUserMedia
try {
let stream = await navigator.mediaDevices.getUserMedia({
audio: true
});
// If the user grants the permission, proceed to show the call overlay
2024-11-17 08:01:02 +08:00
2025-04-13 11:51:02 +08:00
if (stream) {
const tracks = stream.getTracks();
tracks.forEach((track) => track.stop());
}
2024-08-24 00:22:50 +08:00
2025-04-13 11:51:02 +08:00
stream = null;
2024-08-23 22:42:36 +08:00
2025-04-13 11:51:02 +08:00
if ($settings.audio?.tts?.engine === 'browser-kokoro') {
// If the user has not initialized the TTS worker, initialize it
if (!$TTSWorker) {
await TTSWorker.set(
new KokoroWorker({
dtype: $settings.audio?.tts?.engineConfig?.dtype ?? 'fp32'
})
);
2025-02-24 05:32:19 +08:00
2025-04-13 11:51:02 +08:00
await $TTSWorker.init();
2025-02-10 15:54:24 +08:00
}
2024-12-02 15:16:00 +08:00
}
2025-04-13 11:51:02 +08:00
showCallOverlay.set(true);
showControls.set(true);
} catch (err) {
// If the user denies the permission or an error occurs, show an error message
toast.error(
$i18n.t('Permission denied when accessing media devices')
);
}
}}
aria-label={$i18n.t('Voice mode')}
2025-04-13 11:51:02 +08:00
>
2025-07-22 15:22:01 +08:00
<Voice className="size-5" strokeWidth="2.5" />
2025-04-13 11:51:02 +08:00
</button>
</Tooltip>
</div>
2024-12-02 15:16:00 +08:00
{:else}
<div class=" flex items-center">
2025-04-13 11:51:02 +08:00
<Tooltip content={$i18n.t('Send message')}>
2024-12-02 15:16:00 +08:00
<button
2025-04-13 11:51:02 +08:00
id="send-message-button"
class="{!(prompt === '' && files.length === 0)
? 'bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 '
: 'text-white bg-gray-200 dark:text-gray-900 dark:bg-gray-700 disabled'} transition rounded-full p-1.5 self-center"
type="submit"
disabled={prompt === '' && files.length === 0}
2024-11-17 08:01:02 +08:00
>
2024-12-02 15:16:00 +08:00
<svg
xmlns="http://www.w3.org/2000/svg"
2025-04-13 11:51:02 +08:00
viewBox="0 0 16 16"
2024-12-02 15:16:00 +08:00
fill="currentColor"
2025-02-04 17:03:41 +08:00
class="size-5"
2024-12-02 15:16:00 +08:00
>
<path
fill-rule="evenodd"
2025-04-13 11:51:02 +08:00
d="M8 14a.75.75 0 0 1-.75-.75V4.56L4.03 7.78a.75.75 0 0 1-1.06-1.06l4.5-4.5a.75.75 0 0 1 1.06 0l4.5 4.5a.75.75 0 0 1-1.06 1.06L8.75 4.56v8.69A.75.75 0 0 1 8 14Z"
2024-12-02 15:16:00 +08:00
clip-rule="evenodd"
/>
</svg>
</button>
</Tooltip>
</div>
{/if}
2024-06-07 11:33:23 +08:00
</div>
</div>
2024-11-17 08:01:02 +08:00
</div>
2025-06-04 06:36:31 +08:00
{#if $config?.license_metadata?.input_footer}
2025-06-04 06:51:42 +08:00
<div class=" text-xs text-gray-500 text-center line-clamp-1 marked">
2025-06-04 06:46:30 +08:00
{@html DOMPurify.sanitize(marked($config?.license_metadata?.input_footer))}
2025-06-04 06:36:31 +08:00
</div>
{:else}
<div class="mb-1" />
{/if}
2024-11-17 08:01:02 +08:00
</form>
{/if}
</div>
2023-11-20 09:47:07 +08:00
</div>
</div>
</div>
2024-11-17 08:01:02 +08:00
{/if}