MessageInput.svelte 27 KB

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