MessageInput.svelte 31 KB

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