MessageInput.svelte 27 KB

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