MessageInput.svelte 25 KB

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