MessageInput.svelte 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { onMount, tick, getContext, createEventDispatcher } from 'svelte';
  4. const dispatch = createEventDispatcher();
  5. import {
  6. type Model,
  7. mobile,
  8. settings,
  9. showSidebar,
  10. models,
  11. config,
  12. showCallOverlay,
  13. tools,
  14. user as _user
  15. } from '$lib/stores';
  16. import { blobToFile, findWordIndices } from '$lib/utils';
  17. import { transcribeAudio } from '$lib/apis/audio';
  18. import {
  19. getFileLimitSettings,
  20. processDocToVectorDB,
  21. } from '$lib/apis/rag';
  22. import { uploadFile } from '$lib/apis/files';
  23. import {
  24. SUPPORTED_FILE_TYPE,
  25. SUPPORTED_FILE_EXTENSIONS,
  26. WEBUI_BASE_URL,
  27. WEBUI_API_BASE_URL
  28. } from '$lib/constants';
  29. import Tooltip from '../common/Tooltip.svelte';
  30. import InputMenu from './MessageInput/InputMenu.svelte';
  31. import Headphone from '../icons/Headphone.svelte';
  32. import VoiceRecording from './MessageInput/VoiceRecording.svelte';
  33. import FileItem from '../common/FileItem.svelte';
  34. import FilesOverlay from './MessageInput/FilesOverlay.svelte';
  35. import Commands from './MessageInput/Commands.svelte';
  36. import XMark from '../icons/XMark.svelte';
  37. const i18n = getContext('i18n');
  38. export let transparentBackground = false;
  39. export let submitPrompt: Function;
  40. export let stopResponse: Function;
  41. export let autoScroll = false;
  42. export let atSelectedModel: Model | undefined;
  43. export let selectedModels: [''];
  44. let recording = false;
  45. let chatTextAreaElement: HTMLTextAreaElement;
  46. let filesInputElement;
  47. let commandsElement;
  48. let inputFiles;
  49. let fileLimitSettings;
  50. let dragged = false;
  51. let user = null;
  52. let chatInputPlaceholder = '';
  53. export let files = [];
  54. export let availableToolIds = [];
  55. export let selectedToolIds = [];
  56. export let webSearchEnabled = false;
  57. export let prompt = '';
  58. export let messages = [];
  59. let visionCapableModels = [];
  60. $: visionCapableModels = [...(atSelectedModel ? [atSelectedModel] : selectedModels)].filter(
  61. (model) => $models.find((m) => m.id === model)?.info?.meta?.capabilities?.vision ?? true
  62. );
  63. $: if (prompt) {
  64. if (chatTextAreaElement) {
  65. chatTextAreaElement.style.height = '';
  66. chatTextAreaElement.style.height = Math.min(chatTextAreaElement.scrollHeight, 200) + 'px';
  67. }
  68. }
  69. const scrollToBottom = () => {
  70. const element = document.getElementById('messages-container');
  71. element.scrollTo({
  72. top: element.scrollHeight,
  73. behavior: 'smooth'
  74. });
  75. };
  76. const uploadFileHandler = async (file) => {
  77. console.log(file);
  78. // Check if the file is an audio file and transcribe/convert it to text file
  79. if (['audio/mpeg', 'audio/wav'].includes(file['type'])) {
  80. const res = await transcribeAudio(localStorage.token, file).catch((error) => {
  81. toast.error(error);
  82. return null;
  83. });
  84. if (res) {
  85. console.log(res);
  86. const blob = new Blob([res.text], { type: 'text/plain' });
  87. file = blobToFile(blob, `${file.name}.txt`);
  88. }
  89. }
  90. const fileItem = {
  91. type: 'file',
  92. file: '',
  93. id: null,
  94. url: '',
  95. name: file.name,
  96. collection_name: '',
  97. status: '',
  98. size: file.size,
  99. error: ''
  100. };
  101. files = [...files, fileItem];
  102. try {
  103. const uploadedFile = await uploadFile(localStorage.token, file);
  104. if (uploadedFile) {
  105. fileItem.status = 'uploaded';
  106. fileItem.file = uploadedFile;
  107. fileItem.id = uploadedFile.id;
  108. fileItem.url = `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`;
  109. // TODO: Check if tools & functions have files support to skip this step to delegate file processing
  110. // Default Upload to VectorDB
  111. if (
  112. SUPPORTED_FILE_TYPE.includes(file['type']) ||
  113. SUPPORTED_FILE_EXTENSIONS.includes(file.name.split('.').at(-1))
  114. ) {
  115. processFileItem(fileItem);
  116. } else {
  117. toast.error(
  118. $i18n.t(`Unknown file type '{{file_type}}'. Proceeding with the file upload anyway.`, {
  119. file_type: file['type']
  120. })
  121. );
  122. processFileItem(fileItem);
  123. }
  124. } else {
  125. files = files.filter((item) => item.status !== null);
  126. }
  127. } catch (e) {
  128. toast.error(e);
  129. files = files.filter((item) => item.status !== null);
  130. }
  131. };
  132. const processFileItem = async (fileItem) => {
  133. try {
  134. const res = await processDocToVectorDB(localStorage.token, fileItem.id);
  135. if (res) {
  136. fileItem.status = 'processed';
  137. fileItem.collection_name = res.collection_name;
  138. files = files;
  139. }
  140. } catch (e) {
  141. // Remove the failed doc from the files array
  142. // files = files.filter((f) => f.id !== fileItem.id);
  143. toast.error(e);
  144. fileItem.status = 'processed';
  145. files = files;
  146. }
  147. };
  148. const processFileCountLimit = async (querySettings, inputFiles) => {
  149. const maxFiles = querySettings.max_file_count;
  150. const currentFilesCount = files.length;
  151. const inputFilesCount = inputFiles.length;
  152. const totalFilesCount = currentFilesCount + inputFilesCount;
  153. if (currentFilesCount >= maxFiles || totalFilesCount > maxFiles) {
  154. toast.error(`File count exceeds the limit of '${maxFiles}'. Please remove some files.`);
  155. if (currentFilesCount >= maxFiles) {
  156. return [false, null];
  157. }
  158. if (totalFilesCount > maxFiles) {
  159. inputFiles = inputFiles.slice(0, maxFiles - currentFilesCount);
  160. }
  161. }
  162. return [true, inputFiles];
  163. };
  164. const processFileSizeLimit = async (fileLimitSettings, file) => {
  165. if (file.size <= fileLimitSettings.max_file_size * 1024 * 1024) {
  166. if (['image/gif', 'image/webp', 'image/jpeg', 'image/png'].includes(file['type'])) {
  167. if (visionCapableModels.length === 0) {
  168. toast.error($i18n.t('Selected model(s) do not support image inputs'));
  169. return;
  170. }
  171. let reader = new FileReader();
  172. reader.onload = (event) => {
  173. files = [
  174. ...files,
  175. {
  176. type: 'image',
  177. url: `${event.target.result}`
  178. }
  179. ];
  180. };
  181. reader.readAsDataURL(file);
  182. } else {
  183. uploadFileHandler(file);
  184. }
  185. } else {
  186. toast.error(
  187. $i18n.t('File size exceeds the limit of {{size}}MB', {
  188. size: fileLimitSettings.max_file_size
  189. })
  190. );
  191. }
  192. };
  193. onMount(() => {
  194. const initFileLimitSettings = async () => {
  195. try {
  196. fileLimitSettings = await getFileLimitSettings(localStorage.token);
  197. } catch (error) {
  198. console.error('Error fetching query settings:', error);
  199. }
  200. };
  201. initFileLimitSettings();
  202. window.setTimeout(() => chatTextAreaElement?.focus(), 0);
  203. const dropZone = document.querySelector('body');
  204. const handleKeyDown = (event: KeyboardEvent) => {
  205. if (event.key === 'Escape') {
  206. console.log('Escape');
  207. dragged = false;
  208. }
  209. };
  210. const onDragOver = (e) => {
  211. e.preventDefault();
  212. dragged = true;
  213. };
  214. const onDragLeave = () => {
  215. dragged = false;
  216. };
  217. const onDrop = async (e) => {
  218. e.preventDefault();
  219. console.log(e);
  220. if (e.dataTransfer?.files) {
  221. const inputFiles = Array.from(e.dataTransfer?.files);
  222. if (inputFiles && inputFiles.length > 0) {
  223. console.log(inputFiles);
  224. const [canProcess, filesToProcess] = await processFileCountLimit(
  225. fileLimitSettings,
  226. inputFiles
  227. );
  228. if (!canProcess) {
  229. dragged = false;
  230. return;
  231. }
  232. console.log(filesToProcess);
  233. filesToProcess.forEach((file) => {
  234. console.log(file, file.name.split('.').at(-1));
  235. processFileSizeLimit(fileLimitSettings, file);
  236. });
  237. } else {
  238. toast.error($i18n.t(`File not found.`));
  239. }
  240. }
  241. dragged = false;
  242. };
  243. window.addEventListener('keydown', handleKeyDown);
  244. dropZone?.addEventListener('dragover', onDragOver);
  245. dropZone?.addEventListener('drop', onDrop);
  246. dropZone?.addEventListener('dragleave', onDragLeave);
  247. return () => {
  248. window.removeEventListener('keydown', handleKeyDown);
  249. dropZone?.removeEventListener('dragover', onDragOver);
  250. dropZone?.removeEventListener('drop', onDrop);
  251. dropZone?.removeEventListener('dragleave', onDragLeave);
  252. };
  253. });
  254. </script>
  255. <FilesOverlay show={dragged} />
  256. <div class="w-full font-primary">
  257. <div class=" -mb-0.5 mx-auto inset-x-0 bg-transparent flex justify-center">
  258. <div class="flex flex-col max-w-6xl px-2.5 md:px-6 w-full">
  259. <div class="relative">
  260. {#if autoScroll === false && messages.length > 0}
  261. <div
  262. class=" absolute -top-12 left-0 right-0 flex justify-center z-30 pointer-events-none"
  263. >
  264. <button
  265. class=" bg-white border border-gray-100 dark:border-none dark:bg-white/20 p-1.5 rounded-full pointer-events-auto"
  266. on:click={() => {
  267. autoScroll = true;
  268. scrollToBottom();
  269. }}
  270. >
  271. <svg
  272. xmlns="http://www.w3.org/2000/svg"
  273. viewBox="0 0 20 20"
  274. fill="currentColor"
  275. class="w-5 h-5"
  276. >
  277. <path
  278. fill-rule="evenodd"
  279. 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"
  280. clip-rule="evenodd"
  281. />
  282. </svg>
  283. </button>
  284. </div>
  285. {/if}
  286. </div>
  287. <div class="w-full relative">
  288. {#if atSelectedModel !== undefined}
  289. <div
  290. class="px-3 py-2.5 text-left w-full flex justify-between items-center absolute bottom-0.5 left-0 right-0 bg-gradient-to-t from-50% from-white dark:from-gray-900 z-10"
  291. >
  292. <div class="flex items-center gap-2 text-sm dark:text-gray-500">
  293. <img
  294. crossorigin="anonymous"
  295. alt="model profile"
  296. class="size-5 max-w-[28px] object-cover rounded-full"
  297. src={$models.find((model) => model.id === atSelectedModel.id)?.info?.meta
  298. ?.profile_image_url ??
  299. ($i18n.language === 'dg-DG'
  300. ? `/doge.png`
  301. : `${WEBUI_BASE_URL}/static/favicon.png`)}
  302. />
  303. <div>
  304. Talking to <span class=" font-medium">{atSelectedModel.name}</span>
  305. </div>
  306. </div>
  307. <div>
  308. <button
  309. class="flex items-center"
  310. on:click={() => {
  311. atSelectedModel = undefined;
  312. }}
  313. >
  314. <XMark />
  315. </button>
  316. </div>
  317. </div>
  318. {/if}
  319. <Commands
  320. bind:this={commandsElement}
  321. bind:prompt
  322. bind:files
  323. on:select={(e) => {
  324. const data = e.detail;
  325. if (data?.type === 'model') {
  326. atSelectedModel = data.data;
  327. }
  328. chatTextAreaElement?.focus();
  329. }}
  330. />
  331. </div>
  332. </div>
  333. </div>
  334. <div class="{transparentBackground ? 'bg-transparent' : 'bg-white dark:bg-gray-900'} ">
  335. <div class="max-w-6xl px-2.5 md:px-6 mx-auto inset-x-0">
  336. <div class=" pb-2">
  337. <input
  338. bind:this={filesInputElement}
  339. bind:files={inputFiles}
  340. type="file"
  341. hidden
  342. multiple
  343. on:change={async () => {
  344. if (inputFiles && inputFiles.length > 0) {
  345. const _inputFiles = Array.from(inputFiles);
  346. console.log(_inputFiles);
  347. const [canProcess, filesToProcess] = await processFileCountLimit(
  348. fileLimitSettings,
  349. _inputFiles
  350. );
  351. if (!canProcess) {
  352. filesInputElement.value = '';
  353. return;
  354. }
  355. console.log(filesToProcess);
  356. filesToProcess.forEach((file) => {
  357. console.log(file, file.name.split('.').at(-1));
  358. processFileSizeLimit(fileLimitSettings, file);
  359. });
  360. } else {
  361. toast.error($i18n.t(`File not found.`));
  362. }
  363. filesInputElement.value = '';
  364. }}
  365. />
  366. {#if recording}
  367. <VoiceRecording
  368. bind:recording
  369. on:cancel={async () => {
  370. recording = false;
  371. await tick();
  372. document.getElementById('chat-textarea')?.focus();
  373. }}
  374. on:confirm={async (e) => {
  375. const response = e.detail;
  376. prompt = `${prompt}${response} `;
  377. recording = false;
  378. await tick();
  379. document.getElementById('chat-textarea')?.focus();
  380. if ($settings?.speechAutoSend ?? false) {
  381. submitPrompt(prompt);
  382. }
  383. }}
  384. />
  385. {:else}
  386. <form
  387. class="w-full flex gap-1.5"
  388. on:submit|preventDefault={() => {
  389. // check if selectedModels support image input
  390. submitPrompt(prompt);
  391. }}
  392. >
  393. <div
  394. class="flex-1 flex flex-col relative w-full rounded-3xl px-1.5 bg-gray-50 dark:bg-gray-850 dark:text-gray-100"
  395. dir={$settings?.chatDirection ?? 'LTR'}
  396. >
  397. {#if files.length > 0}
  398. <div class="mx-1 mt-2.5 mb-1 flex flex-wrap gap-2">
  399. {#each files as file, fileIdx}
  400. {#if file.type === 'image'}
  401. <div class=" relative group">
  402. <div class="relative">
  403. <img
  404. src={file.url}
  405. alt="input"
  406. class=" h-16 w-16 rounded-xl object-cover"
  407. />
  408. {#if atSelectedModel ? visionCapableModels.length === 0 : selectedModels.length !== visionCapableModels.length}
  409. <Tooltip
  410. className=" absolute top-1 left-1"
  411. content={$i18n.t('{{ models }}', {
  412. models: [...(atSelectedModel ? [atSelectedModel] : selectedModels)]
  413. .filter((id) => !visionCapableModels.includes(id))
  414. .join(', ')
  415. })}
  416. >
  417. <svg
  418. xmlns="http://www.w3.org/2000/svg"
  419. viewBox="0 0 24 24"
  420. fill="currentColor"
  421. class="size-4 fill-yellow-300"
  422. >
  423. <path
  424. fill-rule="evenodd"
  425. 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"
  426. clip-rule="evenodd"
  427. />
  428. </svg>
  429. </Tooltip>
  430. {/if}
  431. </div>
  432. <div class=" absolute -top-1 -right-1">
  433. <button
  434. class=" bg-gray-400 text-white border border-white rounded-full group-hover:visible invisible transition"
  435. type="button"
  436. on:click={() => {
  437. files.splice(fileIdx, 1);
  438. files = files;
  439. }}
  440. >
  441. <svg
  442. xmlns="http://www.w3.org/2000/svg"
  443. viewBox="0 0 20 20"
  444. fill="currentColor"
  445. class="w-4 h-4"
  446. >
  447. <path
  448. 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"
  449. />
  450. </svg>
  451. </button>
  452. </div>
  453. </div>
  454. {:else}
  455. <FileItem
  456. name={file.name}
  457. type={file.type}
  458. size={file?.size}
  459. status={file.status}
  460. dismissible={true}
  461. on:dismiss={() => {
  462. files.splice(fileIdx, 1);
  463. files = files;
  464. }}
  465. />
  466. {/if}
  467. {/each}
  468. </div>
  469. {/if}
  470. <div class=" flex">
  471. <div class=" ml-0.5 self-end mb-1.5 flex space-x-1">
  472. <InputMenu
  473. bind:webSearchEnabled
  474. bind:selectedToolIds
  475. tools={$tools.reduce((a, e, i, arr) => {
  476. if (availableToolIds.includes(e.id) || ($_user?.role ?? 'user') === 'admin') {
  477. a[e.id] = {
  478. name: e.name,
  479. description: e.meta.description,
  480. enabled: false
  481. };
  482. }
  483. return a;
  484. }, {})}
  485. uploadFilesHandler={() => {
  486. filesInputElement.click();
  487. }}
  488. onClose={async () => {
  489. await tick();
  490. chatTextAreaElement?.focus();
  491. }}
  492. >
  493. <button
  494. class="bg-gray-50 hover:bg-gray-100 text-gray-800 dark:bg-gray-850 dark:text-white dark:hover:bg-gray-800 transition rounded-full p-2 outline-none focus:outline-none"
  495. type="button"
  496. >
  497. <svg
  498. xmlns="http://www.w3.org/2000/svg"
  499. viewBox="0 0 16 16"
  500. fill="currentColor"
  501. class="size-5"
  502. >
  503. <path
  504. d="M8.75 3.75a.75.75 0 0 0-1.5 0v3.5h-3.5a.75.75 0 0 0 0 1.5h3.5v3.5a.75.75 0 0 0 1.5 0v-3.5h3.5a.75.75 0 0 0 0-1.5h-3.5v-3.5Z"
  505. />
  506. </svg>
  507. </button>
  508. </InputMenu>
  509. </div>
  510. <textarea
  511. id="chat-textarea"
  512. bind:this={chatTextAreaElement}
  513. class="scrollbar-hidden bg-gray-50 dark:bg-gray-850 dark:text-gray-100 outline-none w-full py-3 px-1 rounded-xl resize-none h-[48px]"
  514. placeholder={chatInputPlaceholder !== ''
  515. ? chatInputPlaceholder
  516. : $i18n.t('Send a Message')}
  517. bind:value={prompt}
  518. on:keypress={(e) => {
  519. if (
  520. !$mobile ||
  521. !(
  522. 'ontouchstart' in window ||
  523. navigator.maxTouchPoints > 0 ||
  524. navigator.msMaxTouchPoints > 0
  525. )
  526. ) {
  527. // Prevent Enter key from creating a new line
  528. if (e.key === 'Enter' && !e.shiftKey) {
  529. e.preventDefault();
  530. }
  531. // Submit the prompt when Enter key is pressed
  532. if (prompt !== '' && e.key === 'Enter' && !e.shiftKey) {
  533. submitPrompt(prompt);
  534. }
  535. }
  536. }}
  537. on:keydown={async (e) => {
  538. const isCtrlPressed = e.ctrlKey || e.metaKey; // metaKey is for Cmd key on Mac
  539. const commandsContainerElement = document.getElementById('commands-container');
  540. // Check if Ctrl + R is pressed
  541. if (prompt === '' && isCtrlPressed && e.key.toLowerCase() === 'r') {
  542. e.preventDefault();
  543. console.log('regenerate');
  544. const regenerateButton = [
  545. ...document.getElementsByClassName('regenerate-response-button')
  546. ]?.at(-1);
  547. regenerateButton?.click();
  548. }
  549. if (prompt === '' && e.key == 'ArrowUp') {
  550. e.preventDefault();
  551. const userMessageElement = [
  552. ...document.getElementsByClassName('user-message')
  553. ]?.at(-1);
  554. const editButton = [
  555. ...document.getElementsByClassName('edit-user-message-button')
  556. ]?.at(-1);
  557. console.log(userMessageElement);
  558. userMessageElement.scrollIntoView({ block: 'center' });
  559. editButton?.click();
  560. }
  561. if (commandsContainerElement && e.key === 'ArrowUp') {
  562. e.preventDefault();
  563. commandsElement.selectUp();
  564. const commandOptionButton = [
  565. ...document.getElementsByClassName('selected-command-option-button')
  566. ]?.at(-1);
  567. commandOptionButton.scrollIntoView({ block: 'center' });
  568. }
  569. if (commandsContainerElement && e.key === 'ArrowDown') {
  570. e.preventDefault();
  571. commandsElement.selectDown();
  572. const commandOptionButton = [
  573. ...document.getElementsByClassName('selected-command-option-button')
  574. ]?.at(-1);
  575. commandOptionButton.scrollIntoView({ block: 'center' });
  576. }
  577. if (commandsContainerElement && e.key === 'Enter') {
  578. e.preventDefault();
  579. const commandOptionButton = [
  580. ...document.getElementsByClassName('selected-command-option-button')
  581. ]?.at(-1);
  582. if (e.shiftKey) {
  583. prompt = `${prompt}\n`;
  584. } else if (commandOptionButton) {
  585. commandOptionButton?.click();
  586. } else {
  587. document.getElementById('send-message-button')?.click();
  588. }
  589. }
  590. if (commandsContainerElement && e.key === 'Tab') {
  591. e.preventDefault();
  592. const commandOptionButton = [
  593. ...document.getElementsByClassName('selected-command-option-button')
  594. ]?.at(-1);
  595. commandOptionButton?.click();
  596. } else if (e.key === 'Tab') {
  597. const words = findWordIndices(prompt);
  598. if (words.length > 0) {
  599. const word = words.at(0);
  600. const fullPrompt = prompt;
  601. prompt = prompt.substring(0, word?.endIndex + 1);
  602. await tick();
  603. e.target.scrollTop = e.target.scrollHeight;
  604. prompt = fullPrompt;
  605. await tick();
  606. e.preventDefault();
  607. e.target.setSelectionRange(word?.startIndex, word.endIndex + 1);
  608. }
  609. e.target.style.height = '';
  610. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  611. }
  612. if (e.key === 'Escape') {
  613. console.log('Escape');
  614. atSelectedModel = undefined;
  615. }
  616. }}
  617. rows="1"
  618. on:input={async (e) => {
  619. e.target.style.height = '';
  620. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  621. user = null;
  622. }}
  623. on:focus={async (e) => {
  624. e.target.style.height = '';
  625. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  626. }}
  627. on:paste={async (e) => {
  628. const clipboardData = e.clipboardData || window.clipboardData;
  629. try {
  630. if (clipboardData && clipboardData.items) {
  631. const inputFiles = Array.from(clipboardData.items)
  632. .map((item) => item.getAsFile())
  633. .filter((file) => file);
  634. const [canProcess, filesToProcess] = await processFileCountLimit(
  635. fileLimitSettings,
  636. inputFiles
  637. );
  638. if (!canProcess) {
  639. return;
  640. }
  641. filesToProcess.forEach((file) => {
  642. console.log(file, file.name.split('.').at(-1));
  643. processFileSizeLimit(fileLimitSettings, file);
  644. });
  645. } else {
  646. toast.error($i18n.t(`File not found.`));
  647. }
  648. } catch (error) {
  649. console.error('Error processing files:', error);
  650. toast.error($i18n.t(`An error occurred while processing files.`));
  651. }
  652. }}
  653. />
  654. <div class="self-end mb-2 flex space-x-1 mr-1">
  655. {#if messages.length == 0 || messages.at(-1).done == true}
  656. <Tooltip content={$i18n.t('Record voice')}>
  657. <button
  658. id="voice-input-button"
  659. class=" text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-850 transition rounded-full p-1.5 mr-0.5 self-center"
  660. type="button"
  661. on:click={async () => {
  662. try {
  663. let stream = await navigator.mediaDevices
  664. .getUserMedia({ audio: true })
  665. .catch(function (err) {
  666. toast.error(
  667. $i18n.t(
  668. `Permission denied when accessing microphone: {{error}}`,
  669. {
  670. error: err
  671. }
  672. )
  673. );
  674. return null;
  675. });
  676. if (stream) {
  677. recording = true;
  678. const tracks = stream.getTracks();
  679. tracks.forEach((track) => track.stop());
  680. }
  681. stream = null;
  682. } catch {
  683. toast.error($i18n.t('Permission denied when accessing microphone'));
  684. }
  685. }}
  686. >
  687. <svg
  688. xmlns="http://www.w3.org/2000/svg"
  689. viewBox="0 0 20 20"
  690. fill="currentColor"
  691. class="w-5 h-5 translate-y-[0.5px]"
  692. >
  693. <path d="M7 4a3 3 0 016 0v6a3 3 0 11-6 0V4z" />
  694. <path
  695. 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"
  696. />
  697. </svg>
  698. </button>
  699. </Tooltip>
  700. {/if}
  701. </div>
  702. </div>
  703. </div>
  704. <div class="flex items-end w-10">
  705. {#if messages.length == 0 || messages.at(-1).done == true}
  706. {#if prompt === ''}
  707. <div class=" flex items-center mb-1">
  708. <Tooltip content={$i18n.t('Call')}>
  709. <button
  710. class=" text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-850 transition rounded-full p-2 self-center"
  711. type="button"
  712. on:click={async () => {
  713. if (selectedModels.length > 1) {
  714. toast.error($i18n.t('Select only one model to call'));
  715. return;
  716. }
  717. if ($config.audio.stt.engine === 'web') {
  718. toast.error(
  719. $i18n.t('Call feature is not supported when using Web STT engine')
  720. );
  721. return;
  722. }
  723. // check if user has access to getUserMedia
  724. try {
  725. let stream = await navigator.mediaDevices.getUserMedia({ audio: true });
  726. // If the user grants the permission, proceed to show the call overlay
  727. if (stream) {
  728. const tracks = stream.getTracks();
  729. tracks.forEach((track) => track.stop());
  730. }
  731. stream = null;
  732. showCallOverlay.set(true);
  733. dispatch('call');
  734. } catch (err) {
  735. // If the user denies the permission or an error occurs, show an error message
  736. toast.error($i18n.t('Permission denied when accessing media devices'));
  737. }
  738. }}
  739. >
  740. <Headphone className="size-6" />
  741. </button>
  742. </Tooltip>
  743. </div>
  744. {:else}
  745. <div class=" flex items-center mb-1">
  746. <Tooltip content={$i18n.t('Send message')}>
  747. <button
  748. id="send-message-button"
  749. class="{prompt !== ''
  750. ? 'bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 '
  751. : 'text-white bg-gray-200 dark:text-gray-900 dark:bg-gray-700 disabled'} transition rounded-full p-1.5 m-0.5 self-center"
  752. type="submit"
  753. disabled={prompt === ''}
  754. >
  755. <svg
  756. xmlns="http://www.w3.org/2000/svg"
  757. viewBox="0 0 16 16"
  758. fill="currentColor"
  759. class="size-6"
  760. >
  761. <path
  762. fill-rule="evenodd"
  763. 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"
  764. clip-rule="evenodd"
  765. />
  766. </svg>
  767. </button>
  768. </Tooltip>
  769. </div>
  770. {/if}
  771. {:else}
  772. <div class=" flex items-center mb-1.5">
  773. <button
  774. 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"
  775. on:click={() => {
  776. stopResponse();
  777. }}
  778. >
  779. <svg
  780. xmlns="http://www.w3.org/2000/svg"
  781. viewBox="0 0 24 24"
  782. fill="currentColor"
  783. class="size-6"
  784. >
  785. <path
  786. fill-rule="evenodd"
  787. 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"
  788. clip-rule="evenodd"
  789. />
  790. </svg>
  791. </button>
  792. </div>
  793. {/if}
  794. </div>
  795. </form>
  796. {/if}
  797. <div class="mt-1.5 text-xs text-gray-500 text-center line-clamp-1">
  798. {$i18n.t('LLMs can make mistakes. Verify important information.')}
  799. </div>
  800. </div>
  801. </div>
  802. </div>
  803. </div>
  804. <style>
  805. .scrollbar-hidden:active::-webkit-scrollbar-thumb,
  806. .scrollbar-hidden:focus::-webkit-scrollbar-thumb,
  807. .scrollbar-hidden:hover::-webkit-scrollbar-thumb {
  808. visibility: visible;
  809. }
  810. .scrollbar-hidden::-webkit-scrollbar-thumb {
  811. visibility: hidden;
  812. }
  813. </style>