MessageInput.svelte 46 KB

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