MessageInput.svelte 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { onMount, tick, getContext } from 'svelte';
  4. import { mobile, modelfiles, settings, showSidebar } from '$lib/stores';
  5. import { blobToFile, calculateSHA256, findWordIndices } from '$lib/utils';
  6. import {
  7. uploadDocToVectorDB,
  8. uploadWebToVectorDB,
  9. uploadYoutubeTranscriptionToVectorDB
  10. } from '$lib/apis/rag';
  11. import { SUPPORTED_FILE_TYPE, SUPPORTED_FILE_EXTENSIONS, WEBUI_BASE_URL } from '$lib/constants';
  12. import { transcribeAudio } from '$lib/apis/audio';
  13. import Prompts from './MessageInput/PromptCommands.svelte';
  14. import Suggestions from './MessageInput/Suggestions.svelte';
  15. import AddFilesPlaceholder from '../AddFilesPlaceholder.svelte';
  16. import Documents from './MessageInput/Documents.svelte';
  17. import Models from './MessageInput/Models.svelte';
  18. import Tooltip from '../common/Tooltip.svelte';
  19. import XMark from '$lib/components/icons/XMark.svelte';
  20. const i18n = getContext('i18n');
  21. export let submitPrompt: Function;
  22. export let stopResponse: Function;
  23. export let autoScroll = true;
  24. export let selectedModel = '';
  25. let chatTextAreaElement: HTMLTextAreaElement;
  26. let filesInputElement;
  27. let promptsElement;
  28. let documentsElement;
  29. let modelsElement;
  30. let inputFiles;
  31. let dragged = false;
  32. let user = null;
  33. let chatInputPlaceholder = '';
  34. export let files = [];
  35. export let fileUploadEnabled = true;
  36. export let speechRecognitionEnabled = true;
  37. export let webSearchAvailable = false;
  38. export let useWebSearch = false;
  39. export let prompt = '';
  40. export let messages = [];
  41. let speechRecognition;
  42. $: if (prompt) {
  43. if (chatTextAreaElement) {
  44. chatTextAreaElement.style.height = '';
  45. chatTextAreaElement.style.height = Math.min(chatTextAreaElement.scrollHeight, 200) + 'px';
  46. }
  47. }
  48. let mediaRecorder;
  49. let audioChunks = [];
  50. let isRecording = false;
  51. const MIN_DECIBELS = -45;
  52. const scrollToBottom = () => {
  53. const element = document.getElementById('messages-container');
  54. element.scrollTop = element.scrollHeight;
  55. };
  56. const startRecording = async () => {
  57. const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
  58. mediaRecorder = new MediaRecorder(stream);
  59. mediaRecorder.onstart = () => {
  60. isRecording = true;
  61. console.log('Recording started');
  62. };
  63. mediaRecorder.ondataavailable = (event) => audioChunks.push(event.data);
  64. mediaRecorder.onstop = async () => {
  65. isRecording = false;
  66. console.log('Recording stopped');
  67. // Create a blob from the audio chunks
  68. const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
  69. const file = blobToFile(audioBlob, 'recording.wav');
  70. const res = await transcribeAudio(localStorage.token, file).catch((error) => {
  71. toast.error(error);
  72. return null;
  73. });
  74. if (res) {
  75. prompt = res.text;
  76. await tick();
  77. chatTextAreaElement?.focus();
  78. if (prompt !== '' && $settings?.speechAutoSend === true) {
  79. submitPrompt(prompt, user);
  80. }
  81. }
  82. // saveRecording(audioBlob);
  83. audioChunks = [];
  84. };
  85. // Start recording
  86. mediaRecorder.start();
  87. // Monitor silence
  88. monitorSilence(stream);
  89. };
  90. const monitorSilence = (stream) => {
  91. const audioContext = new AudioContext();
  92. const audioStreamSource = audioContext.createMediaStreamSource(stream);
  93. const analyser = audioContext.createAnalyser();
  94. analyser.minDecibels = MIN_DECIBELS;
  95. audioStreamSource.connect(analyser);
  96. const bufferLength = analyser.frequencyBinCount;
  97. const domainData = new Uint8Array(bufferLength);
  98. let lastSoundTime = Date.now();
  99. const detectSound = () => {
  100. analyser.getByteFrequencyData(domainData);
  101. if (domainData.some((value) => value > 0)) {
  102. lastSoundTime = Date.now();
  103. }
  104. if (isRecording && Date.now() - lastSoundTime > 3000) {
  105. mediaRecorder.stop();
  106. audioContext.close();
  107. return;
  108. }
  109. window.requestAnimationFrame(detectSound);
  110. };
  111. window.requestAnimationFrame(detectSound);
  112. };
  113. const saveRecording = (blob) => {
  114. const url = URL.createObjectURL(blob);
  115. const a = document.createElement('a');
  116. document.body.appendChild(a);
  117. a.style = 'display: none';
  118. a.href = url;
  119. a.download = 'recording.wav';
  120. a.click();
  121. window.URL.revokeObjectURL(url);
  122. };
  123. const speechRecognitionHandler = () => {
  124. // Check if SpeechRecognition is supported
  125. if (isRecording) {
  126. if (speechRecognition) {
  127. speechRecognition.stop();
  128. }
  129. if (mediaRecorder) {
  130. mediaRecorder.stop();
  131. }
  132. } else {
  133. isRecording = true;
  134. if ($settings?.audio?.STTEngine ?? '' !== '') {
  135. startRecording();
  136. } else {
  137. if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) {
  138. // Create a SpeechRecognition object
  139. speechRecognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
  140. // Set continuous to true for continuous recognition
  141. speechRecognition.continuous = true;
  142. // Set the timeout for turning off the recognition after inactivity (in milliseconds)
  143. const inactivityTimeout = 3000; // 3 seconds
  144. let timeoutId;
  145. // Start recognition
  146. speechRecognition.start();
  147. // Event triggered when speech is recognized
  148. speechRecognition.onresult = async (event) => {
  149. // Clear the inactivity timeout
  150. clearTimeout(timeoutId);
  151. // Handle recognized speech
  152. console.log(event);
  153. const transcript = event.results[Object.keys(event.results).length - 1][0].transcript;
  154. prompt = `${prompt}${transcript}`;
  155. await tick();
  156. chatTextAreaElement?.focus();
  157. // Restart the inactivity timeout
  158. timeoutId = setTimeout(() => {
  159. console.log('Speech recognition turned off due to inactivity.');
  160. speechRecognition.stop();
  161. }, inactivityTimeout);
  162. };
  163. // Event triggered when recognition is ended
  164. speechRecognition.onend = function () {
  165. // Restart recognition after it ends
  166. console.log('recognition ended');
  167. isRecording = false;
  168. if (prompt !== '' && $settings?.speechAutoSend === true) {
  169. submitPrompt(prompt, user);
  170. }
  171. };
  172. // Event triggered when an error occurs
  173. speechRecognition.onerror = function (event) {
  174. console.log(event);
  175. toast.error($i18n.t(`Speech recognition error: {{error}}`, { error: event.error }));
  176. isRecording = false;
  177. };
  178. } else {
  179. toast.error($i18n.t('SpeechRecognition API is not supported in this browser.'));
  180. }
  181. }
  182. }
  183. };
  184. const uploadDoc = async (file) => {
  185. console.log(file);
  186. const doc = {
  187. type: 'doc',
  188. name: file.name,
  189. collection_name: '',
  190. upload_status: false,
  191. error: ''
  192. };
  193. try {
  194. files = [...files, doc];
  195. if (['audio/mpeg', 'audio/wav'].includes(file['type'])) {
  196. const res = await transcribeAudio(localStorage.token, file).catch((error) => {
  197. toast.error(error);
  198. return null;
  199. });
  200. if (res) {
  201. console.log(res);
  202. const blob = new Blob([res.text], { type: 'text/plain' });
  203. file = blobToFile(blob, `${file.name}.txt`);
  204. }
  205. }
  206. const res = await uploadDocToVectorDB(localStorage.token, '', file);
  207. if (res) {
  208. doc.upload_status = true;
  209. doc.collection_name = res.collection_name;
  210. files = files;
  211. }
  212. } catch (e) {
  213. // Remove the failed doc from the files array
  214. files = files.filter((f) => f.name !== file.name);
  215. toast.error(e);
  216. }
  217. };
  218. const uploadWeb = async (url) => {
  219. console.log(url);
  220. const doc = {
  221. type: 'doc',
  222. name: url,
  223. collection_name: '',
  224. upload_status: false,
  225. url: url,
  226. error: ''
  227. };
  228. try {
  229. files = [...files, doc];
  230. const res = await uploadWebToVectorDB(localStorage.token, '', url);
  231. if (res) {
  232. doc.upload_status = true;
  233. doc.collection_name = res.collection_name;
  234. files = files;
  235. }
  236. } catch (e) {
  237. // Remove the failed doc from the files array
  238. files = files.filter((f) => f.name !== url);
  239. toast.error(e);
  240. }
  241. };
  242. const uploadYoutubeTranscription = async (url) => {
  243. console.log(url);
  244. const doc = {
  245. type: 'doc',
  246. name: url,
  247. collection_name: '',
  248. upload_status: false,
  249. url: url,
  250. error: ''
  251. };
  252. try {
  253. files = [...files, doc];
  254. const res = await uploadYoutubeTranscriptionToVectorDB(localStorage.token, url);
  255. if (res) {
  256. doc.upload_status = true;
  257. doc.collection_name = res.collection_name;
  258. files = files;
  259. }
  260. } catch (e) {
  261. // Remove the failed doc from the files array
  262. files = files.filter((f) => f.name !== url);
  263. toast.error(e);
  264. }
  265. };
  266. onMount(() => {
  267. console.log(document.getElementById('sidebar'));
  268. window.setTimeout(() => chatTextAreaElement?.focus(), 0);
  269. const dropZone = document.querySelector('body');
  270. const handleKeyDown = (event: KeyboardEvent) => {
  271. if (event.key === 'Escape') {
  272. console.log('Escape');
  273. dragged = false;
  274. }
  275. };
  276. const onDragOver = (e) => {
  277. e.preventDefault();
  278. dragged = true;
  279. };
  280. const onDragLeave = () => {
  281. dragged = false;
  282. };
  283. const onDrop = async (e) => {
  284. e.preventDefault();
  285. console.log(e);
  286. if (e.dataTransfer?.files) {
  287. const inputFiles = Array.from(e.dataTransfer?.files);
  288. if (inputFiles && inputFiles.length > 0) {
  289. inputFiles.forEach((file) => {
  290. console.log(file, file.name.split('.').at(-1));
  291. if (['image/gif', 'image/jpeg', 'image/png'].includes(file['type'])) {
  292. let reader = new FileReader();
  293. reader.onload = (event) => {
  294. files = [
  295. ...files,
  296. {
  297. type: 'image',
  298. url: `${event.target.result}`
  299. }
  300. ];
  301. };
  302. reader.readAsDataURL(file);
  303. } else if (
  304. SUPPORTED_FILE_TYPE.includes(file['type']) ||
  305. SUPPORTED_FILE_EXTENSIONS.includes(file.name.split('.').at(-1))
  306. ) {
  307. uploadDoc(file);
  308. } else {
  309. toast.error(
  310. $i18n.t(
  311. `Unknown File Type '{{file_type}}', but accepting and treating as plain text`,
  312. { file_type: file['type'] }
  313. )
  314. );
  315. uploadDoc(file);
  316. }
  317. });
  318. } else {
  319. toast.error($i18n.t(`File not found.`));
  320. }
  321. }
  322. dragged = false;
  323. };
  324. window.addEventListener('keydown', handleKeyDown);
  325. dropZone?.addEventListener('dragover', onDragOver);
  326. dropZone?.addEventListener('drop', onDrop);
  327. dropZone?.addEventListener('dragleave', onDragLeave);
  328. return () => {
  329. window.removeEventListener('keydown', handleKeyDown);
  330. dropZone?.removeEventListener('dragover', onDragOver);
  331. dropZone?.removeEventListener('drop', onDrop);
  332. dropZone?.removeEventListener('dragleave', onDragLeave);
  333. };
  334. });
  335. </script>
  336. {#if dragged}
  337. <div
  338. class="fixed {$showSidebar
  339. ? 'left-0 md:left-[260px] md:w-[calc(100%-260px)]'
  340. : 'left-0'} w-full h-full flex z-50 touch-none pointer-events-none"
  341. id="dropzone"
  342. role="region"
  343. aria-label="Drag and Drop Container"
  344. >
  345. <div class="absolute w-full h-full backdrop-blur bg-gray-800/40 flex justify-center">
  346. <div class="m-auto pt-64 flex flex-col justify-center">
  347. <div class="max-w-md">
  348. <AddFilesPlaceholder />
  349. </div>
  350. </div>
  351. </div>
  352. </div>
  353. {/if}
  354. <div class="fixed bottom-0 {$showSidebar ? 'left-0 md:left-[260px]' : 'left-0'} right-0">
  355. <div class="w-full">
  356. <div class="px-2.5 md:px-16 -mb-0.5 mx-auto inset-x-0 bg-transparent flex justify-center">
  357. <div class="flex flex-col max-w-5xl w-full">
  358. <div class="relative">
  359. {#if autoScroll === false && messages.length > 0}
  360. <div class=" absolute -top-12 left-0 right-0 flex justify-center z-30">
  361. <button
  362. class=" bg-white border border-gray-100 dark:border-none dark:bg-white/20 p-1.5 rounded-full"
  363. on:click={() => {
  364. autoScroll = true;
  365. scrollToBottom();
  366. }}
  367. >
  368. <svg
  369. xmlns="http://www.w3.org/2000/svg"
  370. viewBox="0 0 20 20"
  371. fill="currentColor"
  372. class="w-5 h-5"
  373. >
  374. <path
  375. fill-rule="evenodd"
  376. 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"
  377. clip-rule="evenodd"
  378. />
  379. </svg>
  380. </button>
  381. </div>
  382. {/if}
  383. </div>
  384. <div class="w-full relative">
  385. {#if prompt.charAt(0) === '/'}
  386. <Prompts bind:this={promptsElement} bind:prompt />
  387. {:else if prompt.charAt(0) === '#'}
  388. <Documents
  389. bind:this={documentsElement}
  390. bind:prompt
  391. on:youtube={(e) => {
  392. console.log(e);
  393. uploadYoutubeTranscription(e.detail);
  394. }}
  395. on:url={(e) => {
  396. console.log(e);
  397. uploadWeb(e.detail);
  398. }}
  399. on:select={(e) => {
  400. console.log(e);
  401. files = [
  402. ...files,
  403. {
  404. type: e?.detail?.type ?? 'doc',
  405. ...e.detail,
  406. upload_status: true
  407. }
  408. ];
  409. }}
  410. />
  411. {/if}
  412. <Models
  413. bind:this={modelsElement}
  414. bind:prompt
  415. bind:user
  416. bind:chatInputPlaceholder
  417. {messages}
  418. on:select={(e) => {
  419. selectedModel = e.detail;
  420. chatTextAreaElement?.focus();
  421. }}
  422. />
  423. {#if selectedModel !== ''}
  424. <div
  425. 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"
  426. >
  427. <div class="flex items-center gap-2 text-sm dark:text-gray-500">
  428. <img
  429. alt="model profile"
  430. class="size-5 max-w-[28px] object-cover rounded-full"
  431. src={$modelfiles.find((modelfile) => modelfile.tagName === selectedModel.id)
  432. ?.imageUrl ??
  433. ($i18n.language === 'dg-DG'
  434. ? `/doge.png`
  435. : `${WEBUI_BASE_URL}/static/favicon.png`)}
  436. />
  437. <div>
  438. Talking to <span class=" font-medium">{selectedModel.name} </span>
  439. </div>
  440. </div>
  441. <div>
  442. <button
  443. class="flex items-center"
  444. on:click={() => {
  445. selectedModel = '';
  446. }}
  447. >
  448. <XMark />
  449. </button>
  450. </div>
  451. </div>
  452. {/if}
  453. </div>
  454. </div>
  455. </div>
  456. <div class="bg-white dark:bg-gray-900">
  457. <div class="max-w-6xl px-2.5 md:px-16 mx-auto inset-x-0">
  458. <div class=" pb-2">
  459. <input
  460. bind:this={filesInputElement}
  461. bind:files={inputFiles}
  462. type="file"
  463. hidden
  464. multiple
  465. on:change={async () => {
  466. if (inputFiles && inputFiles.length > 0) {
  467. const _inputFiles = Array.from(inputFiles);
  468. _inputFiles.forEach((file) => {
  469. if (['image/gif', 'image/jpeg', 'image/png'].includes(file['type'])) {
  470. let reader = new FileReader();
  471. reader.onload = (event) => {
  472. files = [
  473. ...files,
  474. {
  475. type: 'image',
  476. url: `${event.target.result}`
  477. }
  478. ];
  479. inputFiles = null;
  480. filesInputElement.value = '';
  481. };
  482. reader.readAsDataURL(file);
  483. } else if (
  484. SUPPORTED_FILE_TYPE.includes(file['type']) ||
  485. SUPPORTED_FILE_EXTENSIONS.includes(file.name.split('.').at(-1))
  486. ) {
  487. uploadDoc(file);
  488. filesInputElement.value = '';
  489. } else {
  490. toast.error(
  491. $i18n.t(
  492. `Unknown File Type '{{file_type}}', but accepting and treating as plain text`,
  493. { file_type: file['type'] }
  494. )
  495. );
  496. uploadDoc(file);
  497. filesInputElement.value = '';
  498. }
  499. });
  500. } else {
  501. toast.error($i18n.t(`File not found.`));
  502. }
  503. }}
  504. />
  505. <form
  506. class=" flex flex-col relative w-full rounded-3xl px-1.5 border border-gray-100 dark:border-gray-850 bg-white dark:bg-gray-900 dark:text-gray-100"
  507. on:submit|preventDefault={() => {
  508. submitPrompt(prompt, user);
  509. }}
  510. >
  511. {#if files.length > 0}
  512. <div class="mx-2 mt-2 mb-1 flex flex-wrap gap-2">
  513. {#each files as file, fileIdx}
  514. <div class=" relative group">
  515. {#if file.type === 'image'}
  516. <img src={file.url} alt="input" class=" h-16 w-16 rounded-xl object-cover" />
  517. {:else if file.type === 'doc'}
  518. <div
  519. 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"
  520. >
  521. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  522. {#if file.upload_status}
  523. <svg
  524. xmlns="http://www.w3.org/2000/svg"
  525. viewBox="0 0 24 24"
  526. fill="currentColor"
  527. class="w-6 h-6"
  528. >
  529. <path
  530. fill-rule="evenodd"
  531. 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"
  532. clip-rule="evenodd"
  533. />
  534. <path
  535. 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"
  536. />
  537. </svg>
  538. {:else}
  539. <svg
  540. class=" w-6 h-6 translate-y-[0.5px]"
  541. fill="currentColor"
  542. viewBox="0 0 24 24"
  543. xmlns="http://www.w3.org/2000/svg"
  544. ><style>
  545. .spinner_qM83 {
  546. animation: spinner_8HQG 1.05s infinite;
  547. }
  548. .spinner_oXPr {
  549. animation-delay: 0.1s;
  550. }
  551. .spinner_ZTLf {
  552. animation-delay: 0.2s;
  553. }
  554. @keyframes spinner_8HQG {
  555. 0%,
  556. 57.14% {
  557. animation-timing-function: cubic-bezier(0.33, 0.66, 0.66, 1);
  558. transform: translate(0);
  559. }
  560. 28.57% {
  561. animation-timing-function: cubic-bezier(0.33, 0, 0.66, 0.33);
  562. transform: translateY(-6px);
  563. }
  564. 100% {
  565. transform: translate(0);
  566. }
  567. }
  568. </style><circle class="spinner_qM83" cx="4" cy="12" r="2.5" /><circle
  569. class="spinner_qM83 spinner_oXPr"
  570. cx="12"
  571. cy="12"
  572. r="2.5"
  573. /><circle
  574. class="spinner_qM83 spinner_ZTLf"
  575. cx="20"
  576. cy="12"
  577. r="2.5"
  578. /></svg
  579. >
  580. {/if}
  581. </div>
  582. <div class="flex flex-col justify-center -space-y-0.5">
  583. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  584. {file.name}
  585. </div>
  586. <div class=" text-gray-500 text-sm">{$i18n.t('Document')}</div>
  587. </div>
  588. </div>
  589. {:else if file.type === 'collection'}
  590. <div
  591. 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"
  592. >
  593. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  594. <svg
  595. xmlns="http://www.w3.org/2000/svg"
  596. viewBox="0 0 24 24"
  597. fill="currentColor"
  598. class="w-6 h-6"
  599. >
  600. <path
  601. 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"
  602. />
  603. <path
  604. 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"
  605. />
  606. </svg>
  607. </div>
  608. <div class="flex flex-col justify-center -space-y-0.5">
  609. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  610. {file?.title ?? `#${file.name}`}
  611. </div>
  612. <div class=" text-gray-500 text-sm">{$i18n.t('Collection')}</div>
  613. </div>
  614. </div>
  615. {/if}
  616. <div class=" absolute -top-1 -right-1">
  617. <button
  618. class=" bg-gray-400 text-white border border-white rounded-full group-hover:visible invisible transition"
  619. type="button"
  620. on:click={() => {
  621. files.splice(fileIdx, 1);
  622. files = files;
  623. }}
  624. >
  625. <svg
  626. xmlns="http://www.w3.org/2000/svg"
  627. viewBox="0 0 20 20"
  628. fill="currentColor"
  629. class="w-4 h-4"
  630. >
  631. <path
  632. 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"
  633. />
  634. </svg>
  635. </button>
  636. </div>
  637. </div>
  638. {/each}
  639. </div>
  640. {/if}
  641. <div class=" flex">
  642. {#if fileUploadEnabled}
  643. <div class=" self-end mb-2 ml-1">
  644. <Tooltip content={$i18n.t('Upload files')}>
  645. <button
  646. 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-1.5"
  647. type="button"
  648. on:click={() => {
  649. filesInputElement.click();
  650. }}
  651. >
  652. <svg
  653. xmlns="http://www.w3.org/2000/svg"
  654. viewBox="0 0 16 16"
  655. fill="currentColor"
  656. class="w-[1.2rem] h-[1.2rem]"
  657. >
  658. <path
  659. 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"
  660. />
  661. </svg>
  662. </button>
  663. </Tooltip>
  664. </div>
  665. {/if}
  666. <textarea
  667. id="chat-textarea"
  668. bind:this={chatTextAreaElement}
  669. class="scrollbar-hidden dark:bg-gray-900 dark:text-gray-100 outline-none w-full py-3 px-3 {fileUploadEnabled
  670. ? ''
  671. : ' pl-4'} rounded-xl resize-none h-[48px]"
  672. placeholder={chatInputPlaceholder !== ''
  673. ? chatInputPlaceholder
  674. : isRecording
  675. ? $i18n.t('Listening...')
  676. : $i18n.t('Send a Message')}
  677. bind:value={prompt}
  678. on:keypress={(e) => {
  679. if (
  680. !$mobile ||
  681. !(
  682. 'ontouchstart' in window ||
  683. navigator.maxTouchPoints > 0 ||
  684. navigator.msMaxTouchPoints > 0
  685. )
  686. ) {
  687. if (e.keyCode == 13 && !e.shiftKey) {
  688. e.preventDefault();
  689. }
  690. if (prompt !== '' && e.keyCode == 13 && !e.shiftKey) {
  691. submitPrompt(prompt, user);
  692. }
  693. }
  694. }}
  695. on:keydown={async (e) => {
  696. const isCtrlPressed = e.ctrlKey || e.metaKey; // metaKey is for Cmd key on Mac
  697. // Check if Ctrl + R is pressed
  698. if (prompt === '' && isCtrlPressed && e.key.toLowerCase() === 'r') {
  699. e.preventDefault();
  700. console.log('regenerate');
  701. const regenerateButton = [
  702. ...document.getElementsByClassName('regenerate-response-button')
  703. ]?.at(-1);
  704. regenerateButton?.click();
  705. }
  706. if (prompt === '' && e.key == 'ArrowUp') {
  707. e.preventDefault();
  708. const userMessageElement = [
  709. ...document.getElementsByClassName('user-message')
  710. ]?.at(-1);
  711. const editButton = [
  712. ...document.getElementsByClassName('edit-user-message-button')
  713. ]?.at(-1);
  714. console.log(userMessageElement);
  715. userMessageElement.scrollIntoView({ block: 'center' });
  716. editButton?.click();
  717. }
  718. if (['/', '#', '@'].includes(prompt.charAt(0)) && e.key === 'ArrowUp') {
  719. e.preventDefault();
  720. (promptsElement || documentsElement || modelsElement).selectUp();
  721. const commandOptionButton = [
  722. ...document.getElementsByClassName('selected-command-option-button')
  723. ]?.at(-1);
  724. commandOptionButton.scrollIntoView({ block: 'center' });
  725. }
  726. if (['/', '#', '@'].includes(prompt.charAt(0)) && e.key === 'ArrowDown') {
  727. e.preventDefault();
  728. (promptsElement || documentsElement || modelsElement).selectDown();
  729. const commandOptionButton = [
  730. ...document.getElementsByClassName('selected-command-option-button')
  731. ]?.at(-1);
  732. commandOptionButton.scrollIntoView({ block: 'center' });
  733. }
  734. if (['/', '#', '@'].includes(prompt.charAt(0)) && e.key === 'Enter') {
  735. e.preventDefault();
  736. const commandOptionButton = [
  737. ...document.getElementsByClassName('selected-command-option-button')
  738. ]?.at(-1);
  739. if (commandOptionButton) {
  740. commandOptionButton?.click();
  741. } else {
  742. document.getElementById('send-message-button')?.click();
  743. }
  744. }
  745. if (['/', '#', '@'].includes(prompt.charAt(0)) && e.key === 'Tab') {
  746. e.preventDefault();
  747. const commandOptionButton = [
  748. ...document.getElementsByClassName('selected-command-option-button')
  749. ]?.at(-1);
  750. commandOptionButton?.click();
  751. } else if (e.key === 'Tab') {
  752. const words = findWordIndices(prompt);
  753. if (words.length > 0) {
  754. const word = words.at(0);
  755. const fullPrompt = prompt;
  756. prompt = prompt.substring(0, word?.endIndex + 1);
  757. await tick();
  758. e.target.scrollTop = e.target.scrollHeight;
  759. prompt = fullPrompt;
  760. await tick();
  761. e.preventDefault();
  762. e.target.setSelectionRange(word?.startIndex, word.endIndex + 1);
  763. }
  764. e.target.style.height = '';
  765. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  766. }
  767. if (e.key === 'Escape') {
  768. console.log('Escape');
  769. selectedModel = '';
  770. }
  771. }}
  772. rows="1"
  773. on:input={(e) => {
  774. e.target.style.height = '';
  775. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  776. user = null;
  777. }}
  778. on:focus={(e) => {
  779. e.target.style.height = '';
  780. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  781. }}
  782. on:paste={(e) => {
  783. const clipboardData = e.clipboardData || window.clipboardData;
  784. if (clipboardData && clipboardData.items) {
  785. for (const item of clipboardData.items) {
  786. if (item.type.indexOf('image') !== -1) {
  787. const blob = item.getAsFile();
  788. const reader = new FileReader();
  789. reader.onload = function (e) {
  790. files = [
  791. ...files,
  792. {
  793. type: 'image',
  794. url: `${e.target.result}`
  795. }
  796. ];
  797. };
  798. reader.readAsDataURL(blob);
  799. }
  800. }
  801. }
  802. }}
  803. />
  804. <div class="self-end mb-2 flex space-x-1 mr-1">
  805. {#if messages.length == 0 || messages.at(-1).done == true}
  806. {#if webSearchAvailable}
  807. <Tooltip
  808. content={useWebSearch
  809. ? $i18n.t('Web Search Enabled')
  810. : $i18n.t('Web Search Disabled')}
  811. >
  812. {#if useWebSearch}
  813. <button
  814. id="toggle-websearch-button"
  815. 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"
  816. type="button"
  817. on:click={() => {
  818. useWebSearch = !useWebSearch;
  819. }}
  820. >
  821. <svg
  822. xmlns="http://www.w3.org/2000/svg"
  823. viewBox="0 0 24 24"
  824. fill="currentColor"
  825. class="w-5 h-5 translate-y-[0.5px]"
  826. >
  827. <path
  828. d="M21.721 12.752a9.711 9.711 0 0 0-.945-5.003 12.754 12.754 0 0 1-4.339 2.708 18.991 18.991 0 0 1-.214 4.772 17.165 17.165 0 0 0 5.498-2.477ZM14.634 15.55a17.324 17.324 0 0 0 .332-4.647c-.952.227-1.945.347-2.966.347-1.021 0-2.014-.12-2.966-.347a17.515 17.515 0 0 0 .332 4.647 17.385 17.385 0 0 0 5.268 0ZM9.772 17.119a18.963 18.963 0 0 0 4.456 0A17.182 17.182 0 0 1 12 21.724a17.18 17.18 0 0 1-2.228-4.605ZM7.777 15.23a18.87 18.87 0 0 1-.214-4.774 12.753 12.753 0 0 1-4.34-2.708 9.711 9.711 0 0 0-.944 5.004 17.165 17.165 0 0 0 5.498 2.477ZM21.356 14.752a9.765 9.765 0 0 1-7.478 6.817 18.64 18.64 0 0 0 1.988-4.718 18.627 18.627 0 0 0 5.49-2.098ZM2.644 14.752c1.682.971 3.53 1.688 5.49 2.099a18.64 18.64 0 0 0 1.988 4.718 9.765 9.765 0 0 1-7.478-6.816ZM13.878 2.43a9.755 9.755 0 0 1 6.116 3.986 11.267 11.267 0 0 1-3.746 2.504 18.63 18.63 0 0 0-2.37-6.49ZM12 2.276a17.152 17.152 0 0 1 2.805 7.121c-.897.23-1.837.353-2.805.353-.968 0-1.908-.122-2.805-.353A17.151 17.151 0 0 1 12 2.276ZM10.122 2.43a18.629 18.629 0 0 0-2.37 6.49 11.266 11.266 0 0 1-3.746-2.504 9.754 9.754 0 0 1 6.116-3.985Z"
  829. />
  830. </svg>
  831. </button>
  832. {:else}
  833. <button
  834. id="toggle-websearch-button"
  835. class=" {useWebSearch
  836. ? 'text-gray-600 dark:text-gray-300'
  837. : 'text-gray-300 dark:text-gray-600 disabled'} hover:bg-gray-50 dark:hover:bg-gray-850 transition rounded-full p-1.5 mr-0.5 self-center"
  838. type="button"
  839. on:click={() => {
  840. useWebSearch = !useWebSearch;
  841. }}
  842. >
  843. {#if useWebSearch}
  844. <svg
  845. xmlns="http://www.w3.org/2000/svg"
  846. viewBox="0 0 24 24"
  847. fill="currentColor"
  848. class="w-5 h-5 translate-y-[0.5px]"
  849. >
  850. <path
  851. d="M21.721 12.752a9.711 9.711 0 0 0-.945-5.003 12.754 12.754 0 0 1-4.339 2.708 18.991 18.991 0 0 1-.214 4.772 17.165 17.165 0 0 0 5.498-2.477ZM14.634 15.55a17.324 17.324 0 0 0 .332-4.647c-.952.227-1.945.347-2.966.347-1.021 0-2.014-.12-2.966-.347a17.515 17.515 0 0 0 .332 4.647 17.385 17.385 0 0 0 5.268 0ZM9.772 17.119a18.963 18.963 0 0 0 4.456 0A17.182 17.182 0 0 1 12 21.724a17.18 17.18 0 0 1-2.228-4.605ZM7.777 15.23a18.87 18.87 0 0 1-.214-4.774 12.753 12.753 0 0 1-4.34-2.708 9.711 9.711 0 0 0-.944 5.004 17.165 17.165 0 0 0 5.498 2.477ZM21.356 14.752a9.765 9.765 0 0 1-7.478 6.817 18.64 18.64 0 0 0 1.988-4.718 18.627 18.627 0 0 0 5.49-2.098ZM2.644 14.752c1.682.971 3.53 1.688 5.49 2.099a18.64 18.64 0 0 0 1.988 4.718 9.765 9.765 0 0 1-7.478-6.816ZM13.878 2.43a9.755 9.755 0 0 1 6.116 3.986 11.267 11.267 0 0 1-3.746 2.504 18.63 18.63 0 0 0-2.37-6.49ZM12 2.276a17.152 17.152 0 0 1 2.805 7.121c-.897.23-1.837.353-2.805.353-.968 0-1.908-.122-2.805-.353A17.151 17.151 0 0 1 12 2.276ZM10.122 2.43a18.629 18.629 0 0 0-2.37 6.49 11.266 11.266 0 0 1-3.746-2.504 9.754 9.754 0 0 1 6.116-3.985Z"
  852. />
  853. </svg>
  854. {:else}
  855. <svg
  856. xmlns="http://www.w3.org/2000/svg"
  857. fill="none"
  858. viewBox="0 0 24 24"
  859. stroke-width="1.5"
  860. stroke="currentColor"
  861. class="w-5 h-5 translate-y-[0.5px]"
  862. >
  863. <path
  864. stroke-linecap="round"
  865. stroke-linejoin="round"
  866. d="M12 21a9.004 9.004 0 0 0 8.716-6.747M12 21a9.004 9.004 0 0 1-8.716-6.747M12 21c2.485 0 4.5-4.03 4.5-9S14.485 3 12 3m0 18c-2.485 0-4.5-4.03-4.5-9S9.515 3 12 3m0 0a8.997 8.997 0 0 1 7.843 4.582M12 3a8.997 8.997 0 0 0-7.843 4.582m15.686 0A11.953 11.953 0 0 1 12 10.5c-2.998 0-5.74-1.1-7.843-2.918m15.686 0A8.959 8.959 0 0 1 21 12c0 .778-.099 1.533-.284 2.253m0 0A17.919 17.919 0 0 1 12 16.5c-3.162 0-6.133-.815-8.716-2.247m0 0A9.015 9.015 0 0 1 3 12c0-1.605.42-3.113 1.157-4.418"
  867. />
  868. </svg>
  869. {/if}
  870. </button>
  871. {/if}
  872. </Tooltip>
  873. {/if}
  874. <Tooltip content={$i18n.t('Record voice')}>
  875. {#if speechRecognitionEnabled}
  876. <button
  877. id="voice-input-button"
  878. 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"
  879. type="button"
  880. on:click={() => {
  881. speechRecognitionHandler();
  882. }}
  883. >
  884. {#if isRecording}
  885. <svg
  886. class=" w-5 h-5 translate-y-[0.5px]"
  887. fill="currentColor"
  888. viewBox="0 0 24 24"
  889. xmlns="http://www.w3.org/2000/svg"
  890. ><style>
  891. .spinner_qM83 {
  892. animation: spinner_8HQG 1.05s infinite;
  893. }
  894. .spinner_oXPr {
  895. animation-delay: 0.1s;
  896. }
  897. .spinner_ZTLf {
  898. animation-delay: 0.2s;
  899. }
  900. @keyframes spinner_8HQG {
  901. 0%,
  902. 57.14% {
  903. animation-timing-function: cubic-bezier(0.33, 0.66, 0.66, 1);
  904. transform: translate(0);
  905. }
  906. 28.57% {
  907. animation-timing-function: cubic-bezier(0.33, 0, 0.66, 0.33);
  908. transform: translateY(-6px);
  909. }
  910. 100% {
  911. transform: translate(0);
  912. }
  913. }
  914. </style><circle class="spinner_qM83" cx="4" cy="12" r="2.5" /><circle
  915. class="spinner_qM83 spinner_oXPr"
  916. cx="12"
  917. cy="12"
  918. r="2.5"
  919. /><circle
  920. class="spinner_qM83 spinner_ZTLf"
  921. cx="20"
  922. cy="12"
  923. r="2.5"
  924. /></svg
  925. >
  926. {:else}
  927. <svg
  928. xmlns="http://www.w3.org/2000/svg"
  929. viewBox="0 0 20 20"
  930. fill="currentColor"
  931. class="w-5 h-5 translate-y-[0.5px]"
  932. >
  933. <path d="M7 4a3 3 0 016 0v6a3 3 0 11-6 0V4z" />
  934. <path
  935. 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"
  936. />
  937. </svg>
  938. {/if}
  939. </button>
  940. {/if}
  941. </Tooltip>
  942. <Tooltip content={$i18n.t('Send message')}>
  943. <button
  944. id="send-message-button"
  945. class="{prompt !== ''
  946. ? 'bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 '
  947. : 'text-white bg-gray-100 dark:text-gray-900 dark:bg-gray-800 disabled'} transition rounded-full p-1.5 self-center"
  948. type="submit"
  949. disabled={prompt === ''}
  950. >
  951. <svg
  952. xmlns="http://www.w3.org/2000/svg"
  953. viewBox="0 0 16 16"
  954. fill="currentColor"
  955. class="w-5 h-5"
  956. >
  957. <path
  958. fill-rule="evenodd"
  959. 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"
  960. clip-rule="evenodd"
  961. />
  962. </svg>
  963. </button>
  964. </Tooltip>
  965. {:else}
  966. <button
  967. 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"
  968. on:click={stopResponse}
  969. >
  970. <svg
  971. xmlns="http://www.w3.org/2000/svg"
  972. viewBox="0 0 24 24"
  973. fill="currentColor"
  974. class="w-5 h-5"
  975. >
  976. <path
  977. fill-rule="evenodd"
  978. 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"
  979. clip-rule="evenodd"
  980. />
  981. </svg>
  982. </button>
  983. {/if}
  984. </div>
  985. </div>
  986. </form>
  987. <div class="mt-1.5 text-xs text-gray-500 text-center">
  988. {$i18n.t('LLMs can make mistakes. Verify important information.')}
  989. </div>
  990. </div>
  991. </div>
  992. </div>
  993. </div>
  994. </div>
  995. <style>
  996. .scrollbar-hidden:active::-webkit-scrollbar-thumb,
  997. .scrollbar-hidden:focus::-webkit-scrollbar-thumb,
  998. .scrollbar-hidden:hover::-webkit-scrollbar-thumb {
  999. visibility: visible;
  1000. }
  1001. .scrollbar-hidden::-webkit-scrollbar-thumb {
  1002. visibility: hidden;
  1003. }
  1004. </style>