MessageInput.svelte 26 KB

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