MessageInput.svelte 28 KB

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