MessageInput.svelte 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. window.setTimeout(() => chatTextAreaElement?.focus(), 0);
  268. const dropZone = document.querySelector('body');
  269. const handleKeyDown = (event: KeyboardEvent) => {
  270. if (event.key === 'Escape') {
  271. console.log('Escape');
  272. dragged = false;
  273. }
  274. };
  275. const onDragOver = (e) => {
  276. e.preventDefault();
  277. dragged = true;
  278. };
  279. const onDragLeave = () => {
  280. dragged = false;
  281. };
  282. const onDrop = async (e) => {
  283. e.preventDefault();
  284. console.log(e);
  285. if (e.dataTransfer?.files) {
  286. const inputFiles = Array.from(e.dataTransfer?.files);
  287. if (inputFiles && inputFiles.length > 0) {
  288. inputFiles.forEach((file) => {
  289. console.log(file, file.name.split('.').at(-1));
  290. if (['image/gif', 'image/webp', 'image/jpeg', 'image/png'].includes(file['type'])) {
  291. let reader = new FileReader();
  292. reader.onload = (event) => {
  293. files = [
  294. ...files,
  295. {
  296. type: 'image',
  297. url: `${event.target.result}`
  298. }
  299. ];
  300. };
  301. reader.readAsDataURL(file);
  302. } else if (
  303. SUPPORTED_FILE_TYPE.includes(file['type']) ||
  304. SUPPORTED_FILE_EXTENSIONS.includes(file.name.split('.').at(-1))
  305. ) {
  306. uploadDoc(file);
  307. } else {
  308. toast.error(
  309. $i18n.t(
  310. `Unknown File Type '{{file_type}}', but accepting and treating as plain text`,
  311. { file_type: file['type'] }
  312. )
  313. );
  314. uploadDoc(file);
  315. }
  316. });
  317. } else {
  318. toast.error($i18n.t(`File not found.`));
  319. }
  320. }
  321. dragged = false;
  322. };
  323. window.addEventListener('keydown', handleKeyDown);
  324. dropZone?.addEventListener('dragover', onDragOver);
  325. dropZone?.addEventListener('drop', onDrop);
  326. dropZone?.addEventListener('dragleave', onDragLeave);
  327. return () => {
  328. window.removeEventListener('keydown', handleKeyDown);
  329. dropZone?.removeEventListener('dragover', onDragOver);
  330. dropZone?.removeEventListener('drop', onDrop);
  331. dropZone?.removeEventListener('dragleave', onDragLeave);
  332. };
  333. });
  334. </script>
  335. {#if dragged}
  336. <div
  337. class="fixed {$showSidebar
  338. ? 'left-0 md:left-[260px] md:w-[calc(100%-260px)]'
  339. : 'left-0'} w-full h-full flex z-50 touch-none pointer-events-none"
  340. id="dropzone"
  341. role="region"
  342. aria-label="Drag and Drop Container"
  343. >
  344. <div class="absolute w-full h-full backdrop-blur bg-gray-800/40 flex justify-center">
  345. <div class="m-auto pt-64 flex flex-col justify-center">
  346. <div class="max-w-md">
  347. <AddFilesPlaceholder />
  348. </div>
  349. </div>
  350. </div>
  351. </div>
  352. {/if}
  353. <div class="fixed bottom-0 {$showSidebar ? 'left-0 md:left-[260px]' : 'left-0'} right-0">
  354. <div class="w-full">
  355. <div class="px-2.5 md:px-16 -mb-0.5 mx-auto inset-x-0 bg-transparent flex justify-center">
  356. <div class="flex flex-col max-w-5xl w-full">
  357. <div class="relative">
  358. {#if autoScroll === false && messages.length > 0}
  359. <div class=" absolute -top-12 left-0 right-0 flex justify-center z-30">
  360. <button
  361. class=" bg-white border border-gray-100 dark:border-none dark:bg-white/20 p-1.5 rounded-full"
  362. on:click={() => {
  363. autoScroll = true;
  364. scrollToBottom();
  365. }}
  366. >
  367. <svg
  368. xmlns="http://www.w3.org/2000/svg"
  369. viewBox="0 0 20 20"
  370. fill="currentColor"
  371. class="w-5 h-5"
  372. >
  373. <path
  374. fill-rule="evenodd"
  375. 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"
  376. clip-rule="evenodd"
  377. />
  378. </svg>
  379. </button>
  380. </div>
  381. {/if}
  382. </div>
  383. <div class="w-full relative">
  384. {#if prompt.charAt(0) === '/'}
  385. <Prompts bind:this={promptsElement} bind:prompt />
  386. {:else if prompt.charAt(0) === '#'}
  387. <Documents
  388. bind:this={documentsElement}
  389. bind:prompt
  390. on:youtube={(e) => {
  391. console.log(e);
  392. uploadYoutubeTranscription(e.detail);
  393. }}
  394. on:url={(e) => {
  395. console.log(e);
  396. uploadWeb(e.detail);
  397. }}
  398. on:select={(e) => {
  399. console.log(e);
  400. files = [
  401. ...files,
  402. {
  403. type: e?.detail?.type ?? 'doc',
  404. ...e.detail,
  405. upload_status: true
  406. }
  407. ];
  408. }}
  409. />
  410. {/if}
  411. <Models
  412. bind:this={modelsElement}
  413. bind:prompt
  414. bind:user
  415. bind:chatInputPlaceholder
  416. {messages}
  417. on:select={(e) => {
  418. selectedModel = e.detail;
  419. chatTextAreaElement?.focus();
  420. }}
  421. />
  422. {#if selectedModel !== ''}
  423. <div
  424. 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"
  425. >
  426. <div class="flex items-center gap-2 text-sm dark:text-gray-500">
  427. <img
  428. crossorigin="anonymous"
  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 (
  470. ['image/gif', 'image/webp', 'image/jpeg', 'image/png'].includes(file['type'])
  471. ) {
  472. let reader = new FileReader();
  473. reader.onload = (event) => {
  474. files = [
  475. ...files,
  476. {
  477. type: 'image',
  478. url: `${event.target.result}`
  479. }
  480. ];
  481. inputFiles = null;
  482. filesInputElement.value = '';
  483. };
  484. reader.readAsDataURL(file);
  485. } else if (
  486. SUPPORTED_FILE_TYPE.includes(file['type']) ||
  487. SUPPORTED_FILE_EXTENSIONS.includes(file.name.split('.').at(-1))
  488. ) {
  489. uploadDoc(file);
  490. filesInputElement.value = '';
  491. } else {
  492. toast.error(
  493. $i18n.t(
  494. `Unknown File Type '{{file_type}}', but accepting and treating as plain text`,
  495. { file_type: file['type'] }
  496. )
  497. );
  498. uploadDoc(file);
  499. filesInputElement.value = '';
  500. }
  501. });
  502. } else {
  503. toast.error($i18n.t(`File not found.`));
  504. }
  505. }}
  506. />
  507. <form
  508. dir={$settings?.chatDirection ?? 'LTR'}
  509. class=" flex flex-col relative w-full rounded-3xl px-1.5 bg-gray-50 dark:bg-gray-850 dark:text-gray-100"
  510. on:submit|preventDefault={() => {
  511. submitPrompt(prompt, user);
  512. }}
  513. >
  514. {#if files.length > 0}
  515. <div class="mx-2 mt-2 mb-1 flex flex-wrap gap-2">
  516. {#each files as file, fileIdx}
  517. <div class=" relative group">
  518. {#if file.type === 'image'}
  519. <img src={file.url} alt="input" class=" h-16 w-16 rounded-xl object-cover" />
  520. {:else if file.type === 'doc'}
  521. <div
  522. 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"
  523. >
  524. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  525. {#if file.upload_status}
  526. <svg
  527. xmlns="http://www.w3.org/2000/svg"
  528. viewBox="0 0 24 24"
  529. fill="currentColor"
  530. class="w-6 h-6"
  531. >
  532. <path
  533. fill-rule="evenodd"
  534. 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"
  535. clip-rule="evenodd"
  536. />
  537. <path
  538. 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"
  539. />
  540. </svg>
  541. {:else}
  542. <svg
  543. class=" w-6 h-6 translate-y-[0.5px]"
  544. fill="currentColor"
  545. viewBox="0 0 24 24"
  546. xmlns="http://www.w3.org/2000/svg"
  547. ><style>
  548. .spinner_qM83 {
  549. animation: spinner_8HQG 1.05s infinite;
  550. }
  551. .spinner_oXPr {
  552. animation-delay: 0.1s;
  553. }
  554. .spinner_ZTLf {
  555. animation-delay: 0.2s;
  556. }
  557. @keyframes spinner_8HQG {
  558. 0%,
  559. 57.14% {
  560. animation-timing-function: cubic-bezier(0.33, 0.66, 0.66, 1);
  561. transform: translate(0);
  562. }
  563. 28.57% {
  564. animation-timing-function: cubic-bezier(0.33, 0, 0.66, 0.33);
  565. transform: translateY(-6px);
  566. }
  567. 100% {
  568. transform: translate(0);
  569. }
  570. }
  571. </style><circle class="spinner_qM83" cx="4" cy="12" r="2.5" /><circle
  572. class="spinner_qM83 spinner_oXPr"
  573. cx="12"
  574. cy="12"
  575. r="2.5"
  576. /><circle
  577. class="spinner_qM83 spinner_ZTLf"
  578. cx="20"
  579. cy="12"
  580. r="2.5"
  581. /></svg
  582. >
  583. {/if}
  584. </div>
  585. <div class="flex flex-col justify-center -space-y-0.5">
  586. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  587. {file.name}
  588. </div>
  589. <div class=" text-gray-500 text-sm">{$i18n.t('Document')}</div>
  590. </div>
  591. </div>
  592. {:else if file.type === 'collection'}
  593. <div
  594. 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"
  595. >
  596. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  597. <svg
  598. xmlns="http://www.w3.org/2000/svg"
  599. viewBox="0 0 24 24"
  600. fill="currentColor"
  601. class="w-6 h-6"
  602. >
  603. <path
  604. 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"
  605. />
  606. <path
  607. 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"
  608. />
  609. </svg>
  610. </div>
  611. <div class="flex flex-col justify-center -space-y-0.5">
  612. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  613. {file?.title ?? `#${file.name}`}
  614. </div>
  615. <div class=" text-gray-500 text-sm">{$i18n.t('Collection')}</div>
  616. </div>
  617. </div>
  618. {/if}
  619. <div class=" absolute -top-1 -right-1">
  620. <button
  621. class=" bg-gray-400 text-white border border-white rounded-full group-hover:visible invisible transition"
  622. type="button"
  623. on:click={() => {
  624. files.splice(fileIdx, 1);
  625. files = files;
  626. }}
  627. >
  628. <svg
  629. xmlns="http://www.w3.org/2000/svg"
  630. viewBox="0 0 20 20"
  631. fill="currentColor"
  632. class="w-4 h-4"
  633. >
  634. <path
  635. 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"
  636. />
  637. </svg>
  638. </button>
  639. </div>
  640. </div>
  641. {/each}
  642. </div>
  643. {/if}
  644. <div class=" flex">
  645. {#if fileUploadEnabled}
  646. <div class=" self-end mb-2 ml-1">
  647. <Tooltip content={$i18n.t('Upload files')}>
  648. <button
  649. 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"
  650. type="button"
  651. on:click={() => {
  652. filesInputElement.click();
  653. }}
  654. >
  655. <svg
  656. xmlns="http://www.w3.org/2000/svg"
  657. viewBox="0 0 16 16"
  658. fill="currentColor"
  659. class="w-[1.2rem] h-[1.2rem]"
  660. >
  661. <path
  662. 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"
  663. />
  664. </svg>
  665. </button>
  666. </Tooltip>
  667. </div>
  668. {/if}
  669. <textarea
  670. id="chat-textarea"
  671. bind:this={chatTextAreaElement}
  672. class="scrollbar-hidden bg-gray-50 dark:bg-gray-850 dark:text-gray-100 outline-none w-full py-3 px-3 {fileUploadEnabled
  673. ? ''
  674. : ' pl-4'} rounded-xl resize-none h-[48px]"
  675. placeholder={chatInputPlaceholder !== ''
  676. ? chatInputPlaceholder
  677. : isRecording
  678. ? $i18n.t('Listening...')
  679. : $i18n.t('Send a Message')}
  680. bind:value={prompt}
  681. on:keypress={(e) => {
  682. if (
  683. !$mobile ||
  684. !(
  685. 'ontouchstart' in window ||
  686. navigator.maxTouchPoints > 0 ||
  687. navigator.msMaxTouchPoints > 0
  688. )
  689. ) {
  690. if (e.keyCode == 13 && !e.shiftKey) {
  691. e.preventDefault();
  692. }
  693. if (prompt !== '' && e.keyCode == 13 && !e.shiftKey) {
  694. submitPrompt(prompt, user);
  695. }
  696. }
  697. }}
  698. on:keydown={async (e) => {
  699. const isCtrlPressed = e.ctrlKey || e.metaKey; // metaKey is for Cmd key on Mac
  700. // Check if Ctrl + R is pressed
  701. if (prompt === '' && isCtrlPressed && e.key.toLowerCase() === 'r') {
  702. e.preventDefault();
  703. console.log('regenerate');
  704. const regenerateButton = [
  705. ...document.getElementsByClassName('regenerate-response-button')
  706. ]?.at(-1);
  707. regenerateButton?.click();
  708. }
  709. if (prompt === '' && e.key == 'ArrowUp') {
  710. e.preventDefault();
  711. const userMessageElement = [
  712. ...document.getElementsByClassName('user-message')
  713. ]?.at(-1);
  714. const editButton = [
  715. ...document.getElementsByClassName('edit-user-message-button')
  716. ]?.at(-1);
  717. console.log(userMessageElement);
  718. userMessageElement.scrollIntoView({ block: 'center' });
  719. editButton?.click();
  720. }
  721. if (['/', '#', '@'].includes(prompt.charAt(0)) && e.key === 'ArrowUp') {
  722. e.preventDefault();
  723. (promptsElement || documentsElement || modelsElement).selectUp();
  724. const commandOptionButton = [
  725. ...document.getElementsByClassName('selected-command-option-button')
  726. ]?.at(-1);
  727. commandOptionButton.scrollIntoView({ block: 'center' });
  728. }
  729. if (['/', '#', '@'].includes(prompt.charAt(0)) && e.key === 'ArrowDown') {
  730. e.preventDefault();
  731. (promptsElement || documentsElement || modelsElement).selectDown();
  732. const commandOptionButton = [
  733. ...document.getElementsByClassName('selected-command-option-button')
  734. ]?.at(-1);
  735. commandOptionButton.scrollIntoView({ block: 'center' });
  736. }
  737. if (['/', '#', '@'].includes(prompt.charAt(0)) && e.key === 'Enter') {
  738. e.preventDefault();
  739. const commandOptionButton = [
  740. ...document.getElementsByClassName('selected-command-option-button')
  741. ]?.at(-1);
  742. if (commandOptionButton) {
  743. commandOptionButton?.click();
  744. } else {
  745. document.getElementById('send-message-button')?.click();
  746. }
  747. }
  748. if (['/', '#', '@'].includes(prompt.charAt(0)) && e.key === 'Tab') {
  749. e.preventDefault();
  750. const commandOptionButton = [
  751. ...document.getElementsByClassName('selected-command-option-button')
  752. ]?.at(-1);
  753. commandOptionButton?.click();
  754. } else if (e.key === 'Tab') {
  755. const words = findWordIndices(prompt);
  756. if (words.length > 0) {
  757. const word = words.at(0);
  758. const fullPrompt = prompt;
  759. prompt = prompt.substring(0, word?.endIndex + 1);
  760. await tick();
  761. e.target.scrollTop = e.target.scrollHeight;
  762. prompt = fullPrompt;
  763. await tick();
  764. e.preventDefault();
  765. e.target.setSelectionRange(word?.startIndex, word.endIndex + 1);
  766. }
  767. e.target.style.height = '';
  768. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  769. }
  770. if (e.key === 'Escape') {
  771. console.log('Escape');
  772. selectedModel = '';
  773. }
  774. }}
  775. rows="1"
  776. on:input={(e) => {
  777. e.target.style.height = '';
  778. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  779. user = null;
  780. }}
  781. on:focus={(e) => {
  782. e.target.style.height = '';
  783. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  784. }}
  785. on:paste={(e) => {
  786. const clipboardData = e.clipboardData || window.clipboardData;
  787. if (clipboardData && clipboardData.items) {
  788. for (const item of clipboardData.items) {
  789. if (item.type.indexOf('image') !== -1) {
  790. const blob = item.getAsFile();
  791. const reader = new FileReader();
  792. reader.onload = function (e) {
  793. files = [
  794. ...files,
  795. {
  796. type: 'image',
  797. url: `${e.target.result}`
  798. }
  799. ];
  800. };
  801. reader.readAsDataURL(blob);
  802. }
  803. }
  804. }
  805. }}
  806. />
  807. <div class="self-end mb-2 flex space-x-1 mr-1">
  808. {#if messages.length == 0 || messages.at(-1).done == true}
  809. {#if webSearchAvailable}
  810. <Tooltip
  811. content={useWebSearch
  812. ? $i18n.t('Web Search Enabled')
  813. : $i18n.t('Web Search Disabled')}
  814. >
  815. {#if useWebSearch}
  816. <button
  817. id="toggle-websearch-button"
  818. 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"
  819. type="button"
  820. on:click={() => {
  821. useWebSearch = !useWebSearch;
  822. }}
  823. >
  824. <svg
  825. xmlns="http://www.w3.org/2000/svg"
  826. viewBox="0 0 24 24"
  827. fill="currentColor"
  828. class="w-5 h-5 translate-y-[0.5px]"
  829. >
  830. <path
  831. 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"
  832. />
  833. </svg>
  834. </button>
  835. {:else}
  836. <button
  837. id="toggle-websearch-button"
  838. class=" {useWebSearch
  839. ? 'text-gray-600 dark:text-gray-300'
  840. : '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"
  841. type="button"
  842. on:click={() => {
  843. useWebSearch = !useWebSearch;
  844. }}
  845. >
  846. {#if useWebSearch}
  847. <svg
  848. xmlns="http://www.w3.org/2000/svg"
  849. viewBox="0 0 24 24"
  850. fill="currentColor"
  851. class="w-5 h-5 translate-y-[0.5px]"
  852. >
  853. <path
  854. 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"
  855. />
  856. </svg>
  857. {:else}
  858. <svg
  859. xmlns="http://www.w3.org/2000/svg"
  860. fill="none"
  861. viewBox="0 0 24 24"
  862. stroke-width="1.5"
  863. stroke="currentColor"
  864. class="w-5 h-5 translate-y-[0.5px]"
  865. >
  866. <path
  867. stroke-linecap="round"
  868. stroke-linejoin="round"
  869. 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"
  870. />
  871. </svg>
  872. {/if}
  873. </button>
  874. {/if}
  875. </Tooltip>
  876. {/if}
  877. <Tooltip content={$i18n.t('Record voice')}>
  878. {#if speechRecognitionEnabled}
  879. <button
  880. id="voice-input-button"
  881. 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"
  882. type="button"
  883. on:click={() => {
  884. speechRecognitionHandler();
  885. }}
  886. >
  887. {#if isRecording}
  888. <svg
  889. class=" w-5 h-5 translate-y-[0.5px]"
  890. fill="currentColor"
  891. viewBox="0 0 24 24"
  892. xmlns="http://www.w3.org/2000/svg"
  893. ><style>
  894. .spinner_qM83 {
  895. animation: spinner_8HQG 1.05s infinite;
  896. }
  897. .spinner_oXPr {
  898. animation-delay: 0.1s;
  899. }
  900. .spinner_ZTLf {
  901. animation-delay: 0.2s;
  902. }
  903. @keyframes spinner_8HQG {
  904. 0%,
  905. 57.14% {
  906. animation-timing-function: cubic-bezier(0.33, 0.66, 0.66, 1);
  907. transform: translate(0);
  908. }
  909. 28.57% {
  910. animation-timing-function: cubic-bezier(0.33, 0, 0.66, 0.33);
  911. transform: translateY(-6px);
  912. }
  913. 100% {
  914. transform: translate(0);
  915. }
  916. }
  917. </style><circle class="spinner_qM83" cx="4" cy="12" r="2.5" /><circle
  918. class="spinner_qM83 spinner_oXPr"
  919. cx="12"
  920. cy="12"
  921. r="2.5"
  922. /><circle
  923. class="spinner_qM83 spinner_ZTLf"
  924. cx="20"
  925. cy="12"
  926. r="2.5"
  927. /></svg
  928. >
  929. {:else}
  930. <svg
  931. xmlns="http://www.w3.org/2000/svg"
  932. viewBox="0 0 20 20"
  933. fill="currentColor"
  934. class="w-5 h-5 translate-y-[0.5px]"
  935. >
  936. <path d="M7 4a3 3 0 016 0v6a3 3 0 11-6 0V4z" />
  937. <path
  938. 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"
  939. />
  940. </svg>
  941. {/if}
  942. </button>
  943. {/if}
  944. </Tooltip>
  945. <Tooltip content={$i18n.t('Send message')}>
  946. <button
  947. id="send-message-button"
  948. class="{prompt !== ''
  949. ? 'bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 '
  950. : 'text-white bg-gray-200 dark:text-gray-900 dark:bg-gray-700 disabled'} transition rounded-full p-1.5 self-center"
  951. type="submit"
  952. disabled={prompt === ''}
  953. >
  954. <svg
  955. xmlns="http://www.w3.org/2000/svg"
  956. viewBox="0 0 16 16"
  957. fill="currentColor"
  958. class="w-5 h-5"
  959. >
  960. <path
  961. fill-rule="evenodd"
  962. 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"
  963. clip-rule="evenodd"
  964. />
  965. </svg>
  966. </button>
  967. </Tooltip>
  968. {:else}
  969. <button
  970. 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"
  971. on:click={stopResponse}
  972. >
  973. <svg
  974. xmlns="http://www.w3.org/2000/svg"
  975. viewBox="0 0 24 24"
  976. fill="currentColor"
  977. class="w-5 h-5"
  978. >
  979. <path
  980. fill-rule="evenodd"
  981. 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"
  982. clip-rule="evenodd"
  983. />
  984. </svg>
  985. </button>
  986. {/if}
  987. </div>
  988. </div>
  989. </form>
  990. <div class="mt-1.5 text-xs text-gray-500 text-center">
  991. {$i18n.t('LLMs can make mistakes. Verify important information.')}
  992. </div>
  993. </div>
  994. </div>
  995. </div>
  996. </div>
  997. </div>
  998. <style>
  999. .scrollbar-hidden:active::-webkit-scrollbar-thumb,
  1000. .scrollbar-hidden:focus::-webkit-scrollbar-thumb,
  1001. .scrollbar-hidden:hover::-webkit-scrollbar-thumb {
  1002. visibility: visible;
  1003. }
  1004. .scrollbar-hidden::-webkit-scrollbar-thumb {
  1005. visibility: hidden;
  1006. }
  1007. </style>