MessageInput.svelte 28 KB

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