MessageInput.svelte 31 KB

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