MessageInput.svelte 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. <script lang="ts">
  2. import DOMPurify from 'dompurify';
  3. import { marked } from 'marked';
  4. import { toast } from 'svelte-sonner';
  5. import { v4 as uuidv4 } from 'uuid';
  6. import { createPicker, getAuthToken } from '$lib/utils/google-drive-picker';
  7. import { pickAndDownloadFile } from '$lib/utils/onedrive-file-picker';
  8. import { onMount, tick, getContext, createEventDispatcher, onDestroy } from 'svelte';
  9. const dispatch = createEventDispatcher();
  10. import {
  11. type Model,
  12. mobile,
  13. settings,
  14. showSidebar,
  15. models,
  16. config,
  17. showCallOverlay,
  18. tools,
  19. user as _user,
  20. showControls,
  21. TTSWorker
  22. } from '$lib/stores';
  23. import {
  24. blobToFile,
  25. compressImage,
  26. createMessagesList,
  27. extractCurlyBraceWords
  28. } from '$lib/utils';
  29. import { uploadFile } from '$lib/apis/files';
  30. import { generateAutoCompletion } from '$lib/apis';
  31. import { deleteFileById } from '$lib/apis/files';
  32. import { WEBUI_BASE_URL, WEBUI_API_BASE_URL, PASTED_TEXT_CHARACTER_LIMIT } from '$lib/constants';
  33. import InputMenu from './MessageInput/InputMenu.svelte';
  34. import VoiceRecording from './MessageInput/VoiceRecording.svelte';
  35. import FilesOverlay from './MessageInput/FilesOverlay.svelte';
  36. import Commands from './MessageInput/Commands.svelte';
  37. import ToolServersModal from './ToolServersModal.svelte';
  38. import RichTextInput from '../common/RichTextInput.svelte';
  39. import Tooltip from '../common/Tooltip.svelte';
  40. import FileItem from '../common/FileItem.svelte';
  41. import Image from '../common/Image.svelte';
  42. import XMark from '../icons/XMark.svelte';
  43. import Headphone from '../icons/Headphone.svelte';
  44. import GlobeAlt from '../icons/GlobeAlt.svelte';
  45. import Photo from '../icons/Photo.svelte';
  46. import Wrench from '../icons/Wrench.svelte';
  47. import CommandLine from '../icons/CommandLine.svelte';
  48. import Sparkles from '../icons/Sparkles.svelte';
  49. import { KokoroWorker } from '$lib/workers/KokoroWorker';
  50. const i18n = getContext('i18n');
  51. export let transparentBackground = false;
  52. export let onChange: Function = () => {};
  53. export let createMessagePair: Function;
  54. export let stopResponse: Function;
  55. export let autoScroll = false;
  56. export let atSelectedModel: Model | undefined = undefined;
  57. export let selectedModels: [''];
  58. let selectedModelIds = [];
  59. $: selectedModelIds = atSelectedModel !== undefined ? [atSelectedModel.id] : selectedModels;
  60. export let history;
  61. export let taskIds = null;
  62. export let prompt = '';
  63. export let files = [];
  64. export let toolServers = [];
  65. export let selectedToolIds = [];
  66. export let selectedFilterIds = [];
  67. export let imageGenerationEnabled = false;
  68. export let webSearchEnabled = false;
  69. export let codeInterpreterEnabled = false;
  70. $: onChange({
  71. prompt,
  72. files: files
  73. .filter((file) => file.type !== 'image')
  74. .map((file) => {
  75. return {
  76. ...file,
  77. user: undefined,
  78. access_control: undefined
  79. };
  80. }),
  81. selectedToolIds,
  82. selectedFilterIds,
  83. imageGenerationEnabled,
  84. webSearchEnabled,
  85. codeInterpreterEnabled
  86. });
  87. let showTools = false;
  88. let loaded = false;
  89. let recording = false;
  90. let isComposing = false;
  91. let chatInputContainerElement;
  92. let chatInputElement;
  93. let filesInputElement;
  94. let commandsElement;
  95. let inputFiles;
  96. let dragged = false;
  97. let shiftKey = false;
  98. let user = null;
  99. export let placeholder = '';
  100. let visionCapableModels = [];
  101. $: visionCapableModels = (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).filter(
  102. (model) => $models.find((m) => m.id === model)?.info?.meta?.capabilities?.vision ?? true
  103. );
  104. let fileUploadCapableModels = [];
  105. $: fileUploadCapableModels = (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).filter(
  106. (model) => $models.find((m) => m.id === model)?.info?.meta?.capabilities?.file_upload ?? true
  107. );
  108. let webSearchCapableModels = [];
  109. $: webSearchCapableModels = (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).filter(
  110. (model) => $models.find((m) => m.id === model)?.info?.meta?.capabilities?.web_search ?? true
  111. );
  112. let imageGenerationCapableModels = [];
  113. $: imageGenerationCapableModels = (
  114. atSelectedModel?.id ? [atSelectedModel.id] : selectedModels
  115. ).filter(
  116. (model) =>
  117. $models.find((m) => m.id === model)?.info?.meta?.capabilities?.image_generation ?? true
  118. );
  119. let codeInterpreterCapableModels = [];
  120. $: codeInterpreterCapableModels = (
  121. atSelectedModel?.id ? [atSelectedModel.id] : selectedModels
  122. ).filter(
  123. (model) =>
  124. $models.find((m) => m.id === model)?.info?.meta?.capabilities?.code_interpreter ?? true
  125. );
  126. let toggleFilters = [];
  127. $: toggleFilters = (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels)
  128. .map((id) => ($models.find((model) => model.id === id) || {})?.filters ?? [])
  129. .reduce((acc, filters) => acc.filter((f1) => filters.some((f2) => f2.id === f1.id)));
  130. let showToolsButton = false;
  131. $: showToolsButton = toolServers.length + selectedToolIds.length > 0;
  132. let showWebSearchButton = false;
  133. $: showWebSearchButton =
  134. (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).length ===
  135. webSearchCapableModels.length &&
  136. $config?.features?.enable_web_search &&
  137. ($_user.role === 'admin' || $_user?.permissions?.features?.web_search);
  138. let showImageGenerationButton = false;
  139. $: showImageGenerationButton =
  140. (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).length ===
  141. imageGenerationCapableModels.length &&
  142. $config?.features?.enable_image_generation &&
  143. ($_user.role === 'admin' || $_user?.permissions?.features?.image_generation);
  144. let showCodeInterpreterButton = false;
  145. $: showCodeInterpreterButton =
  146. (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).length ===
  147. codeInterpreterCapableModels.length &&
  148. $config?.features?.enable_code_interpreter &&
  149. ($_user.role === 'admin' || $_user?.permissions?.features?.code_interpreter);
  150. const scrollToBottom = () => {
  151. const element = document.getElementById('messages-container');
  152. element.scrollTo({
  153. top: element.scrollHeight,
  154. behavior: 'smooth'
  155. });
  156. };
  157. const screenCaptureHandler = async () => {
  158. try {
  159. // Request screen media
  160. const mediaStream = await navigator.mediaDevices.getDisplayMedia({
  161. video: { cursor: 'never' },
  162. audio: false
  163. });
  164. // Once the user selects a screen, temporarily create a video element
  165. const video = document.createElement('video');
  166. video.srcObject = mediaStream;
  167. // Ensure the video loads without affecting user experience or tab switching
  168. await video.play();
  169. // Set up the canvas to match the video dimensions
  170. const canvas = document.createElement('canvas');
  171. canvas.width = video.videoWidth;
  172. canvas.height = video.videoHeight;
  173. // Grab a single frame from the video stream using the canvas
  174. const context = canvas.getContext('2d');
  175. context.drawImage(video, 0, 0, canvas.width, canvas.height);
  176. // Stop all video tracks (stop screen sharing) after capturing the image
  177. mediaStream.getTracks().forEach((track) => track.stop());
  178. // bring back focus to this current tab, so that the user can see the screen capture
  179. window.focus();
  180. // Convert the canvas to a Base64 image URL
  181. const imageUrl = canvas.toDataURL('image/png');
  182. // Add the captured image to the files array to render it
  183. files = [...files, { type: 'image', url: imageUrl }];
  184. // Clean memory: Clear video srcObject
  185. video.srcObject = null;
  186. } catch (error) {
  187. // Handle any errors (e.g., user cancels screen sharing)
  188. console.error('Error capturing screen:', error);
  189. }
  190. };
  191. const uploadFileHandler = async (file, fullContext: boolean = false) => {
  192. if ($_user?.role !== 'admin' && !($_user?.permissions?.chat?.file_upload ?? true)) {
  193. toast.error($i18n.t('You do not have permission to upload files.'));
  194. return null;
  195. }
  196. const tempItemId = uuidv4();
  197. const fileItem = {
  198. type: 'file',
  199. file: '',
  200. id: null,
  201. url: '',
  202. name: file.name,
  203. collection_name: '',
  204. status: 'uploading',
  205. size: file.size,
  206. error: '',
  207. itemId: tempItemId,
  208. ...(fullContext ? { context: 'full' } : {})
  209. };
  210. if (fileItem.size == 0) {
  211. toast.error($i18n.t('You cannot upload an empty file.'));
  212. return null;
  213. }
  214. files = [...files, fileItem];
  215. try {
  216. // If the file is an audio file, provide the language for STT.
  217. let metadata = null;
  218. if (
  219. (file.type.startsWith('audio/') || file.type.startsWith('video/')) &&
  220. $settings?.audio?.stt?.language
  221. ) {
  222. metadata = {
  223. language: $settings?.audio?.stt?.language
  224. };
  225. }
  226. // During the file upload, file content is automatically extracted.
  227. const uploadedFile = await uploadFile(localStorage.token, file, metadata);
  228. if (uploadedFile) {
  229. console.log('File upload completed:', {
  230. id: uploadedFile.id,
  231. name: fileItem.name,
  232. collection: uploadedFile?.meta?.collection_name
  233. });
  234. if (uploadedFile.error) {
  235. console.warn('File upload warning:', uploadedFile.error);
  236. toast.warning(uploadedFile.error);
  237. }
  238. fileItem.status = 'uploaded';
  239. fileItem.file = uploadedFile;
  240. fileItem.id = uploadedFile.id;
  241. fileItem.collection_name =
  242. uploadedFile?.meta?.collection_name || uploadedFile?.collection_name;
  243. fileItem.url = `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`;
  244. files = files;
  245. } else {
  246. files = files.filter((item) => item?.itemId !== tempItemId);
  247. }
  248. } catch (e) {
  249. toast.error(`${e}`);
  250. files = files.filter((item) => item?.itemId !== tempItemId);
  251. }
  252. };
  253. const inputFilesHandler = async (inputFiles) => {
  254. console.log('Input files handler called with:', inputFiles);
  255. if (
  256. ($config?.file?.max_count ?? null) !== null &&
  257. files.length + inputFiles.length > $config?.file?.max_count
  258. ) {
  259. toast.error(
  260. $i18n.t(`You can only chat with a maximum of {{maxCount}} file(s) at a time.`, {
  261. maxCount: $config?.file?.max_count
  262. })
  263. );
  264. return;
  265. }
  266. inputFiles.forEach((file) => {
  267. console.log('Processing file:', {
  268. name: file.name,
  269. type: file.type,
  270. size: file.size,
  271. extension: file.name.split('.').at(-1)
  272. });
  273. if (
  274. ($config?.file?.max_size ?? null) !== null &&
  275. file.size > ($config?.file?.max_size ?? 0) * 1024 * 1024
  276. ) {
  277. console.log('File exceeds max size limit:', {
  278. fileSize: file.size,
  279. maxSize: ($config?.file?.max_size ?? 0) * 1024 * 1024
  280. });
  281. toast.error(
  282. $i18n.t(`File size should not exceed {{maxSize}} MB.`, {
  283. maxSize: $config?.file?.max_size
  284. })
  285. );
  286. return;
  287. }
  288. if (
  289. ['image/gif', 'image/webp', 'image/jpeg', 'image/png', 'image/avif'].includes(file['type'])
  290. ) {
  291. if (visionCapableModels.length === 0) {
  292. toast.error($i18n.t('Selected model(s) do not support image inputs'));
  293. return;
  294. }
  295. let reader = new FileReader();
  296. reader.onload = async (event) => {
  297. let imageUrl = event.target.result;
  298. if (
  299. ($settings?.imageCompression ?? false) ||
  300. ($config?.file?.image_compression?.width ?? null) ||
  301. ($config?.file?.image_compression?.height ?? null)
  302. ) {
  303. let width = null;
  304. let height = null;
  305. if ($settings?.imageCompression ?? false) {
  306. width = $settings?.imageCompressionSize?.width ?? null;
  307. height = $settings?.imageCompressionSize?.height ?? null;
  308. }
  309. if (
  310. ($config?.file?.image_compression?.width ?? null) ||
  311. ($config?.file?.image_compression?.height ?? null)
  312. ) {
  313. if (width > ($config?.file?.image_compression?.width ?? null)) {
  314. width = $config?.file?.image_compression?.width ?? null;
  315. }
  316. if (height > ($config?.file?.image_compression?.height ?? null)) {
  317. height = $config?.file?.image_compression?.height ?? null;
  318. }
  319. }
  320. if (width || height) {
  321. imageUrl = await compressImage(imageUrl, width, height);
  322. }
  323. }
  324. files = [
  325. ...files,
  326. {
  327. type: 'image',
  328. url: `${imageUrl}`
  329. }
  330. ];
  331. };
  332. reader.readAsDataURL(file);
  333. } else {
  334. uploadFileHandler(file);
  335. }
  336. });
  337. };
  338. const onDragOver = (e) => {
  339. e.preventDefault();
  340. // Check if a file is being dragged.
  341. if (e.dataTransfer?.types?.includes('Files')) {
  342. dragged = true;
  343. } else {
  344. dragged = false;
  345. }
  346. };
  347. const onDragLeave = () => {
  348. dragged = false;
  349. };
  350. const onDrop = async (e) => {
  351. e.preventDefault();
  352. console.log(e);
  353. if (e.dataTransfer?.files) {
  354. const inputFiles = Array.from(e.dataTransfer?.files);
  355. if (inputFiles && inputFiles.length > 0) {
  356. console.log(inputFiles);
  357. inputFilesHandler(inputFiles);
  358. }
  359. }
  360. dragged = false;
  361. };
  362. const onKeyDown = (e) => {
  363. if (e.key === 'Shift') {
  364. shiftKey = true;
  365. }
  366. if (e.key === 'Escape') {
  367. console.log('Escape');
  368. dragged = false;
  369. }
  370. };
  371. const onKeyUp = (e) => {
  372. if (e.key === 'Shift') {
  373. shiftKey = false;
  374. }
  375. };
  376. const onFocus = () => {};
  377. const onBlur = () => {
  378. shiftKey = false;
  379. };
  380. onMount(async () => {
  381. loaded = true;
  382. window.setTimeout(() => {
  383. const chatInput = document.getElementById('chat-input');
  384. chatInput?.focus();
  385. }, 0);
  386. window.addEventListener('keydown', onKeyDown);
  387. window.addEventListener('keyup', onKeyUp);
  388. window.addEventListener('focus', onFocus);
  389. window.addEventListener('blur', onBlur);
  390. await tick();
  391. const dropzoneElement = document.getElementById('chat-container');
  392. dropzoneElement?.addEventListener('dragover', onDragOver);
  393. dropzoneElement?.addEventListener('drop', onDrop);
  394. dropzoneElement?.addEventListener('dragleave', onDragLeave);
  395. });
  396. onDestroy(() => {
  397. console.log('destroy');
  398. window.removeEventListener('keydown', onKeyDown);
  399. window.removeEventListener('keyup', onKeyUp);
  400. window.removeEventListener('focus', onFocus);
  401. window.removeEventListener('blur', onBlur);
  402. const dropzoneElement = document.getElementById('chat-container');
  403. if (dropzoneElement) {
  404. dropzoneElement?.removeEventListener('dragover', onDragOver);
  405. dropzoneElement?.removeEventListener('drop', onDrop);
  406. dropzoneElement?.removeEventListener('dragleave', onDragLeave);
  407. }
  408. });
  409. </script>
  410. <FilesOverlay show={dragged} />
  411. <ToolServersModal bind:show={showTools} {selectedToolIds} />
  412. {#if loaded}
  413. <div class="w-full font-primary">
  414. <div class=" mx-auto inset-x-0 bg-transparent flex justify-center">
  415. <div
  416. class="flex flex-col px-3 {($settings?.widescreenMode ?? null)
  417. ? 'max-w-full'
  418. : 'max-w-6xl'} w-full"
  419. >
  420. <div class="relative">
  421. {#if autoScroll === false && history?.currentId}
  422. <div
  423. class=" absolute -top-12 left-0 right-0 flex justify-center z-30 pointer-events-none"
  424. >
  425. <button
  426. class=" bg-white border border-gray-100 dark:border-none dark:bg-white/20 p-1.5 rounded-full pointer-events-auto"
  427. on:click={() => {
  428. autoScroll = true;
  429. scrollToBottom();
  430. }}
  431. >
  432. <svg
  433. xmlns="http://www.w3.org/2000/svg"
  434. viewBox="0 0 20 20"
  435. fill="currentColor"
  436. class="w-5 h-5"
  437. >
  438. <path
  439. fill-rule="evenodd"
  440. 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"
  441. clip-rule="evenodd"
  442. />
  443. </svg>
  444. </button>
  445. </div>
  446. {/if}
  447. </div>
  448. <div class="w-full relative">
  449. {#if atSelectedModel !== undefined}
  450. <div
  451. class="px-3 pb-0.5 pt-1.5 text-left w-full flex flex-col absolute bottom-0 left-0 right-0 bg-linear-to-t from-white dark:from-gray-900 z-10"
  452. >
  453. <div class="flex items-center justify-between w-full">
  454. <div class="pl-[1px] flex items-center gap-2 text-sm dark:text-gray-500">
  455. <img
  456. crossorigin="anonymous"
  457. alt="model profile"
  458. class="size-3.5 max-w-[28px] object-cover rounded-full"
  459. src={$models.find((model) => model.id === atSelectedModel.id)?.info?.meta
  460. ?.profile_image_url ??
  461. ($i18n.language === 'dg-DG'
  462. ? `/doge.png`
  463. : `${WEBUI_BASE_URL}/static/favicon.png`)}
  464. />
  465. <div class="translate-y-[0.5px]">
  466. Talking to <span class=" font-medium">{atSelectedModel.name}</span>
  467. </div>
  468. </div>
  469. <div>
  470. <button
  471. class="flex items-center dark:text-gray-500"
  472. on:click={() => {
  473. atSelectedModel = undefined;
  474. }}
  475. >
  476. <XMark />
  477. </button>
  478. </div>
  479. </div>
  480. </div>
  481. {/if}
  482. <Commands
  483. bind:this={commandsElement}
  484. bind:prompt
  485. bind:files
  486. on:upload={(e) => {
  487. dispatch('upload', e.detail);
  488. }}
  489. on:select={(e) => {
  490. const data = e.detail;
  491. if (data?.type === 'model') {
  492. atSelectedModel = data.data;
  493. }
  494. const chatInputElement = document.getElementById('chat-input');
  495. chatInputElement?.focus();
  496. }}
  497. />
  498. </div>
  499. </div>
  500. </div>
  501. <div class="{transparentBackground ? 'bg-transparent' : 'bg-white dark:bg-gray-900'} ">
  502. <div
  503. class="{($settings?.widescreenMode ?? null)
  504. ? 'max-w-full'
  505. : 'max-w-6xl'} px-2.5 mx-auto inset-x-0"
  506. >
  507. <div class="">
  508. <input
  509. bind:this={filesInputElement}
  510. bind:files={inputFiles}
  511. type="file"
  512. hidden
  513. multiple
  514. on:change={async () => {
  515. if (inputFiles && inputFiles.length > 0) {
  516. const _inputFiles = Array.from(inputFiles);
  517. inputFilesHandler(_inputFiles);
  518. } else {
  519. toast.error($i18n.t(`File not found.`));
  520. }
  521. filesInputElement.value = '';
  522. }}
  523. />
  524. {#if recording}
  525. <VoiceRecording
  526. bind:recording
  527. onCancel={async () => {
  528. recording = false;
  529. await tick();
  530. document.getElementById('chat-input')?.focus();
  531. }}
  532. onConfirm={async (data) => {
  533. const { text, filename } = data;
  534. prompt = `${prompt}${text} `;
  535. recording = false;
  536. await tick();
  537. document.getElementById('chat-input')?.focus();
  538. if ($settings?.speechAutoSend ?? false) {
  539. dispatch('submit', prompt);
  540. }
  541. }}
  542. />
  543. {:else}
  544. <form
  545. class="w-full flex flex-col gap-1.5"
  546. on:submit|preventDefault={() => {
  547. // check if selectedModels support image input
  548. dispatch('submit', prompt);
  549. }}
  550. >
  551. <div
  552. class="flex-1 flex flex-col relative w-full shadow-lg rounded-3xl border border-gray-50 dark:border-gray-850 hover:border-gray-100 focus-within:border-gray-100 hover:dark:border-gray-800 focus-within:dark:border-gray-800 transition px-1 bg-white/90 dark:bg-gray-400/5 dark:text-gray-100"
  553. dir={$settings?.chatDirection ?? 'auto'}
  554. >
  555. {#if files.length > 0}
  556. <div class="mx-2 mt-2.5 -mb-1 flex items-center flex-wrap gap-2">
  557. {#each files as file, fileIdx}
  558. {#if file.type === 'image'}
  559. <div class=" relative group">
  560. <div class="relative flex items-center">
  561. <Image
  562. src={file.url}
  563. alt=""
  564. imageClassName=" size-14 rounded-xl object-cover"
  565. />
  566. {#if atSelectedModel ? visionCapableModels.length === 0 : selectedModels.length !== visionCapableModels.length}
  567. <Tooltip
  568. className=" absolute top-1 left-1"
  569. content={$i18n.t('{{ models }}', {
  570. models: [
  571. ...(atSelectedModel ? [atSelectedModel] : selectedModels)
  572. ]
  573. .filter((id) => !visionCapableModels.includes(id))
  574. .join(', ')
  575. })}
  576. >
  577. <svg
  578. xmlns="http://www.w3.org/2000/svg"
  579. viewBox="0 0 24 24"
  580. fill="currentColor"
  581. aria-hidden="true"
  582. class="size-4 fill-yellow-300"
  583. >
  584. <path
  585. fill-rule="evenodd"
  586. 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"
  587. clip-rule="evenodd"
  588. />
  589. </svg>
  590. </Tooltip>
  591. {/if}
  592. </div>
  593. <div class=" absolute -top-1 -right-1">
  594. <button
  595. class=" bg-white text-black border border-white rounded-full {($settings?.highContrastMode ??
  596. false)
  597. ? ''
  598. : 'outline-hidden focus:outline-hidden group-hover:visible invisible transition'}"
  599. type="button"
  600. aria-label={$i18n.t('Remove file')}
  601. on:click={() => {
  602. files.splice(fileIdx, 1);
  603. files = files;
  604. }}
  605. >
  606. <svg
  607. xmlns="http://www.w3.org/2000/svg"
  608. viewBox="0 0 20 20"
  609. fill="currentColor"
  610. aria-hidden="true"
  611. class="size-4"
  612. >
  613. <path
  614. 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"
  615. />
  616. </svg>
  617. </button>
  618. </div>
  619. </div>
  620. {:else}
  621. <FileItem
  622. item={file}
  623. name={file.name}
  624. type={file.type}
  625. size={file?.size}
  626. loading={file.status === 'uploading'}
  627. dismissible={true}
  628. edit={true}
  629. on:dismiss={async () => {
  630. try {
  631. if (file.type !== 'collection' && !file?.collection) {
  632. if (file.id) {
  633. // This will handle both file deletion and Chroma cleanup
  634. await deleteFileById(localStorage.token, file.id);
  635. }
  636. }
  637. } catch (error) {
  638. console.error('Error deleting file:', error);
  639. }
  640. // Remove from UI state
  641. files.splice(fileIdx, 1);
  642. files = files;
  643. }}
  644. on:click={() => {
  645. console.log(file);
  646. }}
  647. />
  648. {/if}
  649. {/each}
  650. </div>
  651. {/if}
  652. <div class="px-2.5">
  653. {#if $settings?.richTextInput ?? true}
  654. <div
  655. class="scrollbar-hidden rtl:text-right ltr:text-left bg-transparent dark:text-gray-100 outline-hidden w-full pt-2.5 pb-[5px] px-1 resize-none h-fit max-h-80 overflow-auto"
  656. id="chat-input-container"
  657. >
  658. <RichTextInput
  659. bind:this={chatInputElement}
  660. bind:value={prompt}
  661. id="chat-input"
  662. messageInput={true}
  663. shiftEnter={!($settings?.ctrlEnterToSend ?? false) &&
  664. (!$mobile ||
  665. !(
  666. 'ontouchstart' in window ||
  667. navigator.maxTouchPoints > 0 ||
  668. navigator.msMaxTouchPoints > 0
  669. ))}
  670. placeholder={placeholder ? placeholder : $i18n.t('Send a Message')}
  671. largeTextAsFile={($settings?.largeTextAsFile ?? false) && !shiftKey}
  672. autocomplete={$config?.features?.enable_autocomplete_generation &&
  673. ($settings?.promptAutocomplete ?? false)}
  674. generateAutoCompletion={async (text) => {
  675. if (selectedModelIds.length === 0 || !selectedModelIds.at(0)) {
  676. toast.error($i18n.t('Please select a model first.'));
  677. }
  678. const res = await generateAutoCompletion(
  679. localStorage.token,
  680. selectedModelIds.at(0),
  681. text,
  682. history?.currentId
  683. ? createMessagesList(history, history.currentId)
  684. : null
  685. ).catch((error) => {
  686. console.log(error);
  687. return null;
  688. });
  689. console.log(res);
  690. return res;
  691. }}
  692. oncompositionstart={() => (isComposing = true)}
  693. oncompositionend={() => (isComposing = false)}
  694. on:keydown={async (e) => {
  695. e = e.detail.event;
  696. const isCtrlPressed = e.ctrlKey || e.metaKey; // metaKey is for Cmd key on Mac
  697. const commandsContainerElement =
  698. document.getElementById('commands-container');
  699. if (e.key === 'Escape') {
  700. stopResponse();
  701. }
  702. // Command/Ctrl + Shift + Enter to submit a message pair
  703. if (isCtrlPressed && e.key === 'Enter' && e.shiftKey) {
  704. e.preventDefault();
  705. createMessagePair(prompt);
  706. }
  707. // Check if Ctrl + R is pressed
  708. if (prompt === '' && isCtrlPressed && e.key.toLowerCase() === 'r') {
  709. e.preventDefault();
  710. console.log('regenerate');
  711. const regenerateButton = [
  712. ...document.getElementsByClassName('regenerate-response-button')
  713. ]?.at(-1);
  714. regenerateButton?.click();
  715. }
  716. if (prompt === '' && e.key == 'ArrowUp') {
  717. e.preventDefault();
  718. const userMessageElement = [
  719. ...document.getElementsByClassName('user-message')
  720. ]?.at(-1);
  721. if (userMessageElement) {
  722. userMessageElement.scrollIntoView({ block: 'center' });
  723. const editButton = [
  724. ...document.getElementsByClassName('edit-user-message-button')
  725. ]?.at(-1);
  726. editButton?.click();
  727. }
  728. }
  729. if (commandsContainerElement) {
  730. if (commandsContainerElement && e.key === 'ArrowUp') {
  731. e.preventDefault();
  732. commandsElement.selectUp();
  733. const commandOptionButton = [
  734. ...document.getElementsByClassName('selected-command-option-button')
  735. ]?.at(-1);
  736. commandOptionButton.scrollIntoView({ block: 'center' });
  737. }
  738. if (commandsContainerElement && e.key === 'ArrowDown') {
  739. e.preventDefault();
  740. commandsElement.selectDown();
  741. const commandOptionButton = [
  742. ...document.getElementsByClassName('selected-command-option-button')
  743. ]?.at(-1);
  744. commandOptionButton.scrollIntoView({ block: 'center' });
  745. }
  746. if (commandsContainerElement && e.key === 'Tab') {
  747. e.preventDefault();
  748. const commandOptionButton = [
  749. ...document.getElementsByClassName('selected-command-option-button')
  750. ]?.at(-1);
  751. commandOptionButton?.click();
  752. }
  753. if (commandsContainerElement && e.key === 'Enter') {
  754. e.preventDefault();
  755. const commandOptionButton = [
  756. ...document.getElementsByClassName('selected-command-option-button')
  757. ]?.at(-1);
  758. if (commandOptionButton) {
  759. commandOptionButton?.click();
  760. } else {
  761. document.getElementById('send-message-button')?.click();
  762. }
  763. }
  764. } else {
  765. if (
  766. !$mobile ||
  767. !(
  768. 'ontouchstart' in window ||
  769. navigator.maxTouchPoints > 0 ||
  770. navigator.msMaxTouchPoints > 0
  771. )
  772. ) {
  773. if (isComposing) {
  774. return;
  775. }
  776. // Uses keyCode '13' for Enter key for chinese/japanese keyboards.
  777. //
  778. // Depending on the user's settings, it will send the message
  779. // either when Enter is pressed or when Ctrl+Enter is pressed.
  780. const enterPressed =
  781. ($settings?.ctrlEnterToSend ?? false)
  782. ? (e.key === 'Enter' || e.keyCode === 13) && isCtrlPressed
  783. : (e.key === 'Enter' || e.keyCode === 13) && !e.shiftKey;
  784. if (enterPressed) {
  785. e.preventDefault();
  786. if (prompt !== '' || files.length > 0) {
  787. dispatch('submit', prompt);
  788. }
  789. }
  790. }
  791. }
  792. if (e.key === 'Escape') {
  793. console.log('Escape');
  794. atSelectedModel = undefined;
  795. selectedToolIds = [];
  796. selectedFilterIds = [];
  797. webSearchEnabled = false;
  798. imageGenerationEnabled = false;
  799. codeInterpreterEnabled = false;
  800. }
  801. }}
  802. on:paste={async (e) => {
  803. e = e.detail.event;
  804. console.log(e);
  805. const clipboardData = e.clipboardData || window.clipboardData;
  806. if (clipboardData && clipboardData.items) {
  807. for (const item of clipboardData.items) {
  808. if (item.type.indexOf('image') !== -1) {
  809. const blob = item.getAsFile();
  810. const reader = new FileReader();
  811. reader.onload = function (e) {
  812. files = [
  813. ...files,
  814. {
  815. type: 'image',
  816. url: `${e.target.result}`
  817. }
  818. ];
  819. };
  820. reader.readAsDataURL(blob);
  821. } else if (item.type === 'text/plain') {
  822. if (($settings?.largeTextAsFile ?? false) && !shiftKey) {
  823. const text = clipboardData.getData('text/plain');
  824. if (text.length > PASTED_TEXT_CHARACTER_LIMIT) {
  825. e.preventDefault();
  826. const blob = new Blob([text], { type: 'text/plain' });
  827. const file = new File([blob], `Pasted_Text_${Date.now()}.txt`, {
  828. type: 'text/plain'
  829. });
  830. await uploadFileHandler(file, true);
  831. }
  832. }
  833. }
  834. }
  835. }
  836. }}
  837. />
  838. </div>
  839. {:else}
  840. <textarea
  841. id="chat-input"
  842. dir={$settings?.chatDirection ?? 'auto'}
  843. bind:this={chatInputElement}
  844. class="scrollbar-hidden bg-transparent dark:text-gray-200 outline-hidden w-full pt-3 px-1 resize-none"
  845. placeholder={placeholder ? placeholder : $i18n.t('Send a Message')}
  846. bind:value={prompt}
  847. on:compositionstart={() => (isComposing = true)}
  848. on:compositionend={() => (isComposing = false)}
  849. on:keydown={async (e) => {
  850. const isCtrlPressed = e.ctrlKey || e.metaKey; // metaKey is for Cmd key on Mac
  851. const commandsContainerElement =
  852. document.getElementById('commands-container');
  853. if (e.key === 'Escape') {
  854. stopResponse();
  855. }
  856. // Command/Ctrl + Shift + Enter to submit a message pair
  857. if (isCtrlPressed && e.key === 'Enter' && e.shiftKey) {
  858. e.preventDefault();
  859. createMessagePair(prompt);
  860. }
  861. // Check if Ctrl + R is pressed
  862. if (prompt === '' && isCtrlPressed && e.key.toLowerCase() === 'r') {
  863. e.preventDefault();
  864. console.log('regenerate');
  865. const regenerateButton = [
  866. ...document.getElementsByClassName('regenerate-response-button')
  867. ]?.at(-1);
  868. regenerateButton?.click();
  869. }
  870. if (prompt === '' && e.key == 'ArrowUp') {
  871. e.preventDefault();
  872. const userMessageElement = [
  873. ...document.getElementsByClassName('user-message')
  874. ]?.at(-1);
  875. const editButton = [
  876. ...document.getElementsByClassName('edit-user-message-button')
  877. ]?.at(-1);
  878. console.log(userMessageElement);
  879. userMessageElement?.scrollIntoView({ block: 'center' });
  880. editButton?.click();
  881. }
  882. if (commandsContainerElement) {
  883. if (commandsContainerElement && e.key === 'ArrowUp') {
  884. e.preventDefault();
  885. commandsElement.selectUp();
  886. const container = document.getElementById('command-options-container');
  887. const commandOptionButton = [
  888. ...document.getElementsByClassName('selected-command-option-button')
  889. ]?.at(-1);
  890. if (commandOptionButton && container) {
  891. const elTop = commandOptionButton.offsetTop;
  892. const elHeight = commandOptionButton.offsetHeight;
  893. const containerHeight = container.clientHeight;
  894. // Center the selected button in the container
  895. container.scrollTop = elTop - containerHeight / 2 + elHeight / 2;
  896. }
  897. }
  898. if (commandsContainerElement && e.key === 'ArrowDown') {
  899. e.preventDefault();
  900. commandsElement.selectDown();
  901. const container = document.getElementById('command-options-container');
  902. const commandOptionButton = [
  903. ...document.getElementsByClassName('selected-command-option-button')
  904. ]?.at(-1);
  905. if (commandOptionButton && container) {
  906. const elTop = commandOptionButton.offsetTop;
  907. const elHeight = commandOptionButton.offsetHeight;
  908. const containerHeight = container.clientHeight;
  909. // Center the selected button in the container
  910. container.scrollTop = elTop - containerHeight / 2 + elHeight / 2;
  911. }
  912. }
  913. if (commandsContainerElement && e.key === 'Enter') {
  914. e.preventDefault();
  915. const commandOptionButton = [
  916. ...document.getElementsByClassName('selected-command-option-button')
  917. ]?.at(-1);
  918. if (e.shiftKey) {
  919. prompt = `${prompt}\n`;
  920. } else if (commandOptionButton) {
  921. commandOptionButton?.click();
  922. } else {
  923. document.getElementById('send-message-button')?.click();
  924. }
  925. }
  926. if (commandsContainerElement && e.key === 'Tab') {
  927. e.preventDefault();
  928. const commandOptionButton = [
  929. ...document.getElementsByClassName('selected-command-option-button')
  930. ]?.at(-1);
  931. commandOptionButton?.click();
  932. }
  933. } else {
  934. if (
  935. !$mobile ||
  936. !(
  937. 'ontouchstart' in window ||
  938. navigator.maxTouchPoints > 0 ||
  939. navigator.msMaxTouchPoints > 0
  940. )
  941. ) {
  942. if (isComposing) {
  943. return;
  944. }
  945. // Prevent Enter key from creating a new line
  946. const isCtrlPressed = e.ctrlKey || e.metaKey;
  947. const enterPressed =
  948. ($settings?.ctrlEnterToSend ?? false)
  949. ? (e.key === 'Enter' || e.keyCode === 13) && isCtrlPressed
  950. : (e.key === 'Enter' || e.keyCode === 13) && !e.shiftKey;
  951. if (enterPressed) {
  952. e.preventDefault();
  953. }
  954. // Submit the prompt when Enter key is pressed
  955. if ((prompt !== '' || files.length > 0) && enterPressed) {
  956. dispatch('submit', prompt);
  957. }
  958. }
  959. }
  960. if (e.key === 'Tab') {
  961. const words = extractCurlyBraceWords(prompt);
  962. if (words.length > 0) {
  963. const word = words.at(0);
  964. const fullPrompt = prompt;
  965. prompt = prompt.substring(0, word?.endIndex + 1);
  966. await tick();
  967. e.target.scrollTop = e.target.scrollHeight;
  968. prompt = fullPrompt;
  969. await tick();
  970. e.preventDefault();
  971. e.target.setSelectionRange(word?.startIndex, word.endIndex + 1);
  972. }
  973. e.target.style.height = '';
  974. e.target.style.height = Math.min(e.target.scrollHeight, 320) + 'px';
  975. }
  976. if (e.key === 'Escape') {
  977. console.log('Escape');
  978. atSelectedModel = undefined;
  979. selectedToolIds = [];
  980. selectedFilterIds = [];
  981. webSearchEnabled = false;
  982. imageGenerationEnabled = false;
  983. codeInterpreterEnabled = false;
  984. }
  985. }}
  986. rows="1"
  987. on:input={async (e) => {
  988. e.target.style.height = '';
  989. e.target.style.height = Math.min(e.target.scrollHeight, 320) + 'px';
  990. }}
  991. on:focus={async (e) => {
  992. e.target.style.height = '';
  993. e.target.style.height = Math.min(e.target.scrollHeight, 320) + 'px';
  994. }}
  995. on:paste={async (e) => {
  996. const clipboardData = e.clipboardData || window.clipboardData;
  997. if (clipboardData && clipboardData.items) {
  998. for (const item of clipboardData.items) {
  999. if (item.type.indexOf('image') !== -1) {
  1000. const blob = item.getAsFile();
  1001. const reader = new FileReader();
  1002. reader.onload = function (e) {
  1003. files = [
  1004. ...files,
  1005. {
  1006. type: 'image',
  1007. url: `${e.target.result}`
  1008. }
  1009. ];
  1010. };
  1011. reader.readAsDataURL(blob);
  1012. } else if (item.type === 'text/plain') {
  1013. if (($settings?.largeTextAsFile ?? false) && !shiftKey) {
  1014. const text = clipboardData.getData('text/plain');
  1015. if (text.length > PASTED_TEXT_CHARACTER_LIMIT) {
  1016. e.preventDefault();
  1017. const blob = new Blob([text], { type: 'text/plain' });
  1018. const file = new File([blob], `Pasted_Text_${Date.now()}.txt`, {
  1019. type: 'text/plain'
  1020. });
  1021. await uploadFileHandler(file, true);
  1022. }
  1023. }
  1024. }
  1025. }
  1026. }
  1027. }}
  1028. />
  1029. {/if}
  1030. </div>
  1031. <div class=" flex justify-between mt-0.5 mb-2.5 mx-0.5 max-w-full" dir="ltr">
  1032. <div class="ml-1 self-end flex items-center flex-1 max-w-[80%]">
  1033. <InputMenu
  1034. bind:selectedToolIds
  1035. selectedModels={atSelectedModel ? [atSelectedModel.id] : selectedModels}
  1036. {fileUploadCapableModels}
  1037. {screenCaptureHandler}
  1038. {inputFilesHandler}
  1039. uploadFilesHandler={() => {
  1040. filesInputElement.click();
  1041. }}
  1042. uploadGoogleDriveHandler={async () => {
  1043. try {
  1044. const fileData = await createPicker();
  1045. if (fileData) {
  1046. const file = new File([fileData.blob], fileData.name, {
  1047. type: fileData.blob.type
  1048. });
  1049. await uploadFileHandler(file);
  1050. } else {
  1051. console.log('No file was selected from Google Drive');
  1052. }
  1053. } catch (error) {
  1054. console.error('Google Drive Error:', error);
  1055. toast.error(
  1056. $i18n.t('Error accessing Google Drive: {{error}}', {
  1057. error: error.message
  1058. })
  1059. );
  1060. }
  1061. }}
  1062. uploadOneDriveHandler={async (authorityType) => {
  1063. try {
  1064. const fileData = await pickAndDownloadFile(authorityType);
  1065. if (fileData) {
  1066. const file = new File([fileData.blob], fileData.name, {
  1067. type: fileData.blob.type || 'application/octet-stream'
  1068. });
  1069. await uploadFileHandler(file);
  1070. } else {
  1071. console.log('No file was selected from OneDrive');
  1072. }
  1073. } catch (error) {
  1074. console.error('OneDrive Error:', error);
  1075. }
  1076. }}
  1077. onClose={async () => {
  1078. await tick();
  1079. const chatInput = document.getElementById('chat-input');
  1080. chatInput?.focus();
  1081. }}
  1082. >
  1083. <button
  1084. class="bg-transparent hover:bg-gray-100 text-gray-800 dark:text-white dark:hover:bg-gray-800 transition rounded-full p-1.5 outline-hidden focus:outline-hidden"
  1085. type="button"
  1086. aria-label={$i18n.t('More Available Tools')}
  1087. >
  1088. <svg
  1089. xmlns="http://www.w3.org/2000/svg"
  1090. viewBox="0 0 20 20"
  1091. aria-hidden="true"
  1092. fill="currentColor"
  1093. class="size-5"
  1094. >
  1095. <path
  1096. d="M10.75 4.75a.75.75 0 0 0-1.5 0v4.5h-4.5a.75.75 0 0 0 0 1.5h4.5v4.5a.75.75 0 0 0 1.5 0v-4.5h4.5a.75.75 0 0 0 0-1.5h-4.5v-4.5Z"
  1097. />
  1098. </svg>
  1099. </button>
  1100. </InputMenu>
  1101. {#if $_user && (showToolsButton || (toggleFilters && toggleFilters.length > 0) || showWebSearchButton || showImageGenerationButton || showCodeInterpreterButton)}
  1102. <div
  1103. class="flex self-center w-[1px] h-4 mx-1.5 bg-gray-50 dark:bg-gray-800"
  1104. />
  1105. <div class="flex gap-1 items-center overflow-x-auto scrollbar-none flex-1">
  1106. {#if showToolsButton}
  1107. <Tooltip
  1108. content={$i18n.t('{{COUNT}} Available Tools', {
  1109. COUNT: toolServers.length + selectedToolIds.length
  1110. })}
  1111. >
  1112. <button
  1113. class="translate-y-[0.5px] flex gap-1 items-center text-gray-600 dark:text-gray-300 hover:text-gray-700 dark:hover:text-gray-200 rounded-lg p-1 self-center transition"
  1114. aria-label="Available Tools"
  1115. type="button"
  1116. on:click={() => {
  1117. showTools = !showTools;
  1118. }}
  1119. >
  1120. <Wrench className="size-4" strokeWidth="1.75" />
  1121. <span class="text-sm font-medium text-gray-600 dark:text-gray-300">
  1122. {toolServers.length + selectedToolIds.length}
  1123. </span>
  1124. </button>
  1125. </Tooltip>
  1126. {/if}
  1127. {#each toggleFilters as filter, filterIdx (filter.id)}
  1128. <Tooltip content={filter?.description} placement="top">
  1129. <button
  1130. on:click|preventDefault={() => {
  1131. if (selectedFilterIds.includes(filter.id)) {
  1132. selectedFilterIds = selectedFilterIds.filter(
  1133. (id) => id !== filter.id
  1134. );
  1135. } else {
  1136. selectedFilterIds = [...selectedFilterIds, filter.id];
  1137. }
  1138. }}
  1139. type="button"
  1140. class="px-2 @xl:px-2.5 py-2 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(
  1141. filter.id
  1142. )
  1143. ? 'text-sky-500 dark:text-sky-300 bg-sky-50 dark:bg-sky-200/5'
  1144. : 'bg-transparent text-gray-600 dark:text-gray-300 '} capitalize"
  1145. >
  1146. {#if filter?.icon}
  1147. <div class="size-4 items-center flex justify-center">
  1148. <img
  1149. src={filter.icon}
  1150. class="size-3.5 {filter.icon.includes('svg')
  1151. ? 'dark:invert-[80%]'
  1152. : ''}"
  1153. style="fill: currentColor;"
  1154. alt={filter.name}
  1155. />
  1156. </div>
  1157. {:else}
  1158. <Sparkles className="size-4" strokeWidth="1.75" />
  1159. {/if}
  1160. <span
  1161. class="hidden @xl:block whitespace-nowrap overflow-hidden text-ellipsis leading-none pr-0.5"
  1162. >{filter?.name}</span
  1163. >
  1164. </button>
  1165. </Tooltip>
  1166. {/each}
  1167. {#if showWebSearchButton}
  1168. <Tooltip content={$i18n.t('Search the internet')} placement="top">
  1169. <button
  1170. on:click|preventDefault={() => (webSearchEnabled = !webSearchEnabled)}
  1171. type="button"
  1172. class="px-2 @xl:px-2.5 py-2 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 ||
  1173. ($settings?.webSearch ?? false) === 'always'
  1174. ? ' text-sky-500 dark:text-sky-300 bg-sky-50 dark:bg-sky-200/5'
  1175. : 'bg-transparent text-gray-600 dark:text-gray-300 '}"
  1176. >
  1177. <GlobeAlt className="size-4" strokeWidth="1.75" />
  1178. <span
  1179. class="hidden @xl:block whitespace-nowrap overflow-hidden text-ellipsis leading-none pr-0.5"
  1180. >{$i18n.t('Web Search')}</span
  1181. >
  1182. </button>
  1183. </Tooltip>
  1184. {/if}
  1185. {#if showImageGenerationButton}
  1186. <Tooltip content={$i18n.t('Generate an image')} placement="top">
  1187. <button
  1188. on:click|preventDefault={() =>
  1189. (imageGenerationEnabled = !imageGenerationEnabled)}
  1190. type="button"
  1191. class="px-2 @xl:px-2.5 py-2 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
  1192. ? ' text-sky-500 dark:text-sky-300 bg-sky-50 dark:bg-sky-200/5'
  1193. : 'bg-transparent text-gray-600 dark:text-gray-300 '}"
  1194. >
  1195. <Photo className="size-4" strokeWidth="1.75" />
  1196. <span
  1197. class="hidden @xl:block whitespace-nowrap overflow-hidden text-ellipsis leading-none pr-0.5"
  1198. >{$i18n.t('Image')}</span
  1199. >
  1200. </button>
  1201. </Tooltip>
  1202. {/if}
  1203. {#if showCodeInterpreterButton}
  1204. <Tooltip content={$i18n.t('Execute code for analysis')} placement="top">
  1205. <button
  1206. aria-label={codeInterpreterEnabled
  1207. ? $i18n.t('Disable Code Interpreter')
  1208. : $i18n.t('Enable Code Interpreter')}
  1209. aria-pressed={codeInterpreterEnabled}
  1210. on:click|preventDefault={() =>
  1211. (codeInterpreterEnabled = !codeInterpreterEnabled)}
  1212. type="button"
  1213. class="px-2 @xl:px-2.5 py-2 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 {codeInterpreterEnabled
  1214. ? ' text-sky-500 dark:text-sky-300 bg-sky-50 dark:bg-sky-200/5'
  1215. : 'bg-transparent text-gray-600 dark:text-gray-300 '}"
  1216. >
  1217. <CommandLine className="size-4" strokeWidth="1.75" />
  1218. <span
  1219. class="hidden @xl:block whitespace-nowrap overflow-hidden text-ellipsis leading-none pr-0.5"
  1220. >{$i18n.t('Code Interpreter')}</span
  1221. >
  1222. </button>
  1223. </Tooltip>
  1224. {/if}
  1225. </div>
  1226. {/if}
  1227. </div>
  1228. <div class="self-end flex space-x-1 mr-1 shrink-0">
  1229. {#if (!history?.currentId || history.messages[history.currentId]?.done == true) && ($_user?.role === 'admin' || ($_user?.permissions?.chat?.stt ?? true))}
  1230. <!-- {$i18n.t('Record voice')} -->
  1231. <Tooltip content={$i18n.t('Dictate')}>
  1232. <button
  1233. id="voice-input-button"
  1234. 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"
  1235. type="button"
  1236. on:click={async () => {
  1237. try {
  1238. let stream = await navigator.mediaDevices
  1239. .getUserMedia({ audio: true })
  1240. .catch(function (err) {
  1241. toast.error(
  1242. $i18n.t(
  1243. `Permission denied when accessing microphone: {{error}}`,
  1244. {
  1245. error: err
  1246. }
  1247. )
  1248. );
  1249. return null;
  1250. });
  1251. if (stream) {
  1252. recording = true;
  1253. const tracks = stream.getTracks();
  1254. tracks.forEach((track) => track.stop());
  1255. }
  1256. stream = null;
  1257. } catch {
  1258. toast.error($i18n.t('Permission denied when accessing microphone'));
  1259. }
  1260. }}
  1261. aria-label="Voice Input"
  1262. >
  1263. <svg
  1264. xmlns="http://www.w3.org/2000/svg"
  1265. viewBox="0 0 20 20"
  1266. fill="currentColor"
  1267. class="w-5 h-5 translate-y-[0.5px]"
  1268. >
  1269. <path d="M7 4a3 3 0 016 0v6a3 3 0 11-6 0V4z" />
  1270. <path
  1271. 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"
  1272. />
  1273. </svg>
  1274. </button>
  1275. </Tooltip>
  1276. {/if}
  1277. {#if (taskIds && taskIds.length > 0) || (history.currentId && history.messages[history.currentId]?.done != true)}
  1278. <div class=" flex items-center">
  1279. <Tooltip content={$i18n.t('Stop')}>
  1280. <button
  1281. 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"
  1282. on:click={() => {
  1283. stopResponse();
  1284. }}
  1285. >
  1286. <svg
  1287. xmlns="http://www.w3.org/2000/svg"
  1288. viewBox="0 0 24 24"
  1289. fill="currentColor"
  1290. class="size-5"
  1291. >
  1292. <path
  1293. fill-rule="evenodd"
  1294. 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"
  1295. clip-rule="evenodd"
  1296. />
  1297. </svg>
  1298. </button>
  1299. </Tooltip>
  1300. </div>
  1301. {:else if prompt === '' && files.length === 0 && ($_user?.role === 'admin' || ($_user?.permissions?.chat?.call ?? true))}
  1302. <div class=" flex items-center">
  1303. <!-- {$i18n.t('Call')} -->
  1304. <Tooltip content={$i18n.t('Voice mode')}>
  1305. <button
  1306. 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"
  1307. type="button"
  1308. on:click={async () => {
  1309. if (selectedModels.length > 1) {
  1310. toast.error($i18n.t('Select only one model to call'));
  1311. return;
  1312. }
  1313. if ($config.audio.stt.engine === 'web') {
  1314. toast.error(
  1315. $i18n.t('Call feature is not supported when using Web STT engine')
  1316. );
  1317. return;
  1318. }
  1319. // check if user has access to getUserMedia
  1320. try {
  1321. let stream = await navigator.mediaDevices.getUserMedia({
  1322. audio: true
  1323. });
  1324. // If the user grants the permission, proceed to show the call overlay
  1325. if (stream) {
  1326. const tracks = stream.getTracks();
  1327. tracks.forEach((track) => track.stop());
  1328. }
  1329. stream = null;
  1330. if ($settings.audio?.tts?.engine === 'browser-kokoro') {
  1331. // If the user has not initialized the TTS worker, initialize it
  1332. if (!$TTSWorker) {
  1333. await TTSWorker.set(
  1334. new KokoroWorker({
  1335. dtype: $settings.audio?.tts?.engineConfig?.dtype ?? 'fp32'
  1336. })
  1337. );
  1338. await $TTSWorker.init();
  1339. }
  1340. }
  1341. showCallOverlay.set(true);
  1342. showControls.set(true);
  1343. } catch (err) {
  1344. // If the user denies the permission or an error occurs, show an error message
  1345. toast.error(
  1346. $i18n.t('Permission denied when accessing media devices')
  1347. );
  1348. }
  1349. }}
  1350. aria-label={$i18n.t('Voice mode')}
  1351. >
  1352. <Headphone className="size-5" />
  1353. </button>
  1354. </Tooltip>
  1355. </div>
  1356. {:else}
  1357. <div class=" flex items-center">
  1358. <Tooltip content={$i18n.t('Send message')}>
  1359. <button
  1360. id="send-message-button"
  1361. class="{!(prompt === '' && files.length === 0)
  1362. ? 'bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 '
  1363. : 'text-white bg-gray-200 dark:text-gray-900 dark:bg-gray-700 disabled'} transition rounded-full p-1.5 self-center"
  1364. type="submit"
  1365. disabled={prompt === '' && files.length === 0}
  1366. >
  1367. <svg
  1368. xmlns="http://www.w3.org/2000/svg"
  1369. viewBox="0 0 16 16"
  1370. fill="currentColor"
  1371. class="size-5"
  1372. >
  1373. <path
  1374. fill-rule="evenodd"
  1375. 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"
  1376. clip-rule="evenodd"
  1377. />
  1378. </svg>
  1379. </button>
  1380. </Tooltip>
  1381. </div>
  1382. {/if}
  1383. </div>
  1384. </div>
  1385. </div>
  1386. {#if $config?.license_metadata?.input_footer}
  1387. <div class=" text-xs text-gray-500 text-center line-clamp-1 marked">
  1388. {@html DOMPurify.sanitize(marked($config?.license_metadata?.input_footer))}
  1389. </div>
  1390. {:else}
  1391. <div class="mb-1" />
  1392. {/if}
  1393. </form>
  1394. {/if}
  1395. </div>
  1396. </div>
  1397. </div>
  1398. </div>
  1399. {/if}