MessageInput.svelte 32 KB

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