MessageInput.svelte 24 KB

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