MessageInput.svelte 32 KB

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