MessageInput.svelte 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { onMount, tick, getContext } from 'svelte';
  4. import {
  5. type Model,
  6. mobile,
  7. settings,
  8. showSidebar,
  9. models,
  10. config,
  11. showCallOverlay,
  12. tools
  13. } from '$lib/stores';
  14. import { blobToFile, calculateSHA256, findWordIndices } from '$lib/utils';
  15. import {
  16. uploadDocToVectorDB,
  17. uploadWebToVectorDB,
  18. uploadYoutubeTranscriptionToVectorDB
  19. } from '$lib/apis/rag';
  20. import { SUPPORTED_FILE_TYPE, SUPPORTED_FILE_EXTENSIONS, WEBUI_BASE_URL } from '$lib/constants';
  21. import Prompts from './MessageInput/PromptCommands.svelte';
  22. import Suggestions from './MessageInput/Suggestions.svelte';
  23. import AddFilesPlaceholder from '../AddFilesPlaceholder.svelte';
  24. import Documents from './MessageInput/Documents.svelte';
  25. import Models from './MessageInput/Models.svelte';
  26. import Tooltip from '../common/Tooltip.svelte';
  27. import XMark from '$lib/components/icons/XMark.svelte';
  28. import InputMenu from './MessageInput/InputMenu.svelte';
  29. import Headphone from '../icons/Headphone.svelte';
  30. import VoiceRecording from './MessageInput/VoiceRecording.svelte';
  31. import { transcribeAudio } from '$lib/apis/audio';
  32. const i18n = getContext('i18n');
  33. export let submitPrompt: Function;
  34. export let stopResponse: Function;
  35. export let autoScroll = true;
  36. export let atSelectedModel: Model | undefined;
  37. export let selectedModels: [''];
  38. let recording = false;
  39. let chatTextAreaElement: HTMLTextAreaElement;
  40. let filesInputElement;
  41. let promptsElement;
  42. let documentsElement;
  43. let modelsElement;
  44. let inputFiles;
  45. let dragged = false;
  46. let user = null;
  47. let chatInputPlaceholder = '';
  48. export let files = [];
  49. export let availableTools = {};
  50. export let selectedToolIds = [];
  51. export let webSearchEnabled = false;
  52. export let prompt = '';
  53. export let messages = [];
  54. let visionCapableModels = [];
  55. $: visionCapableModels = [...(atSelectedModel ? [atSelectedModel] : selectedModels)].filter(
  56. (model) => $models.find((m) => m.id === model)?.info?.meta?.capabilities?.vision ?? true
  57. );
  58. $: if (prompt) {
  59. if (chatTextAreaElement) {
  60. chatTextAreaElement.style.height = '';
  61. chatTextAreaElement.style.height = Math.min(chatTextAreaElement.scrollHeight, 200) + 'px';
  62. }
  63. }
  64. const scrollToBottom = () => {
  65. const element = document.getElementById('messages-container');
  66. element.scrollTop = element.scrollHeight;
  67. };
  68. const uploadDoc = async (file) => {
  69. console.log(file);
  70. const doc = {
  71. type: 'doc',
  72. name: file.name,
  73. collection_name: '',
  74. upload_status: false,
  75. error: ''
  76. };
  77. try {
  78. files = [...files, doc];
  79. if (['audio/mpeg', 'audio/wav'].includes(file['type'])) {
  80. const res = await transcribeAudio(localStorage.token, file).catch((error) => {
  81. toast.error(error);
  82. return null;
  83. });
  84. if (res) {
  85. console.log(res);
  86. const blob = new Blob([res.text], { type: 'text/plain' });
  87. file = blobToFile(blob, `${file.name}.txt`);
  88. }
  89. }
  90. const res = await uploadDocToVectorDB(localStorage.token, '', file);
  91. if (res) {
  92. doc.upload_status = true;
  93. doc.collection_name = res.collection_name;
  94. files = files;
  95. }
  96. } catch (e) {
  97. // Remove the failed doc from the files array
  98. files = files.filter((f) => f.name !== file.name);
  99. toast.error(e);
  100. }
  101. };
  102. const uploadWeb = async (url) => {
  103. console.log(url);
  104. const doc = {
  105. type: 'doc',
  106. name: url,
  107. collection_name: '',
  108. upload_status: false,
  109. url: url,
  110. error: ''
  111. };
  112. try {
  113. files = [...files, doc];
  114. const res = await uploadWebToVectorDB(localStorage.token, '', url);
  115. if (res) {
  116. doc.upload_status = true;
  117. doc.collection_name = res.collection_name;
  118. files = files;
  119. }
  120. } catch (e) {
  121. // Remove the failed doc from the files array
  122. files = files.filter((f) => f.name !== url);
  123. toast.error(e);
  124. }
  125. };
  126. const uploadYoutubeTranscription = async (url) => {
  127. console.log(url);
  128. const doc = {
  129. type: 'doc',
  130. name: url,
  131. collection_name: '',
  132. upload_status: false,
  133. url: url,
  134. error: ''
  135. };
  136. try {
  137. files = [...files, doc];
  138. const res = await uploadYoutubeTranscriptionToVectorDB(localStorage.token, url);
  139. if (res) {
  140. doc.upload_status = true;
  141. doc.collection_name = res.collection_name;
  142. files = files;
  143. }
  144. } catch (e) {
  145. // Remove the failed doc from the files array
  146. files = files.filter((f) => f.name !== url);
  147. toast.error(e);
  148. }
  149. };
  150. onMount(() => {
  151. window.setTimeout(() => chatTextAreaElement?.focus(), 0);
  152. const dropZone = document.querySelector('body');
  153. const handleKeyDown = (event: KeyboardEvent) => {
  154. if (event.key === 'Escape') {
  155. console.log('Escape');
  156. dragged = false;
  157. }
  158. };
  159. const onDragOver = (e) => {
  160. e.preventDefault();
  161. dragged = true;
  162. };
  163. const onDragLeave = () => {
  164. dragged = false;
  165. };
  166. const onDrop = async (e) => {
  167. e.preventDefault();
  168. console.log(e);
  169. if (e.dataTransfer?.files) {
  170. const inputFiles = Array.from(e.dataTransfer?.files);
  171. if (inputFiles && inputFiles.length > 0) {
  172. inputFiles.forEach((file) => {
  173. console.log(file, file.name.split('.').at(-1));
  174. if (['image/gif', 'image/webp', 'image/jpeg', 'image/png'].includes(file['type'])) {
  175. if (visionCapableModels.length === 0) {
  176. toast.error($i18n.t('Selected model(s) do not support image inputs'));
  177. return;
  178. }
  179. let reader = new FileReader();
  180. reader.onload = (event) => {
  181. files = [
  182. ...files,
  183. {
  184. type: 'image',
  185. url: `${event.target.result}`
  186. }
  187. ];
  188. };
  189. reader.readAsDataURL(file);
  190. } else if (
  191. SUPPORTED_FILE_TYPE.includes(file['type']) ||
  192. SUPPORTED_FILE_EXTENSIONS.includes(file.name.split('.').at(-1))
  193. ) {
  194. uploadDoc(file);
  195. } else {
  196. toast.error(
  197. $i18n.t(
  198. `Unknown File Type '{{file_type}}', but accepting and treating as plain text`,
  199. { file_type: file['type'] }
  200. )
  201. );
  202. uploadDoc(file);
  203. }
  204. });
  205. } else {
  206. toast.error($i18n.t(`File not found.`));
  207. }
  208. }
  209. dragged = false;
  210. };
  211. window.addEventListener('keydown', handleKeyDown);
  212. dropZone?.addEventListener('dragover', onDragOver);
  213. dropZone?.addEventListener('drop', onDrop);
  214. dropZone?.addEventListener('dragleave', onDragLeave);
  215. return () => {
  216. window.removeEventListener('keydown', handleKeyDown);
  217. dropZone?.removeEventListener('dragover', onDragOver);
  218. dropZone?.removeEventListener('drop', onDrop);
  219. dropZone?.removeEventListener('dragleave', onDragLeave);
  220. };
  221. });
  222. </script>
  223. {#if dragged}
  224. <div
  225. class="fixed {$showSidebar
  226. ? 'left-0 md:left-[260px] md:w-[calc(100%-260px)]'
  227. : 'left-0'} w-full h-full flex z-50 touch-none pointer-events-none"
  228. id="dropzone"
  229. role="region"
  230. aria-label="Drag and Drop Container"
  231. >
  232. <div class="absolute w-full h-full backdrop-blur bg-gray-800/40 flex justify-center">
  233. <div class="m-auto pt-64 flex flex-col justify-center">
  234. <div class="max-w-md">
  235. <AddFilesPlaceholder />
  236. </div>
  237. </div>
  238. </div>
  239. </div>
  240. {/if}
  241. <div class="w-full">
  242. <div class=" -mb-0.5 mx-auto inset-x-0 bg-transparent flex justify-center">
  243. <div class="flex flex-col max-w-6xl px-2.5 md:px-6 w-full">
  244. <div class="relative">
  245. {#if autoScroll === false && messages.length > 0}
  246. <div class=" absolute -top-12 left-0 right-0 flex justify-center z-30">
  247. <button
  248. class=" bg-white border border-gray-100 dark:border-none dark:bg-white/20 p-1.5 rounded-full"
  249. on:click={() => {
  250. autoScroll = true;
  251. scrollToBottom();
  252. }}
  253. >
  254. <svg
  255. xmlns="http://www.w3.org/2000/svg"
  256. viewBox="0 0 20 20"
  257. fill="currentColor"
  258. class="w-5 h-5"
  259. >
  260. <path
  261. fill-rule="evenodd"
  262. 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"
  263. clip-rule="evenodd"
  264. />
  265. </svg>
  266. </button>
  267. </div>
  268. {/if}
  269. </div>
  270. <div class="w-full relative">
  271. {#if prompt.charAt(0) === '/'}
  272. <Prompts bind:this={promptsElement} bind:prompt bind:files />
  273. {:else if prompt.charAt(0) === '#'}
  274. <Documents
  275. bind:this={documentsElement}
  276. bind:prompt
  277. on:youtube={(e) => {
  278. console.log(e);
  279. uploadYoutubeTranscription(e.detail);
  280. }}
  281. on:url={(e) => {
  282. console.log(e);
  283. uploadWeb(e.detail);
  284. }}
  285. on:select={(e) => {
  286. console.log(e);
  287. files = [
  288. ...files,
  289. {
  290. type: e?.detail?.type ?? 'doc',
  291. ...e.detail,
  292. upload_status: true
  293. }
  294. ];
  295. }}
  296. />
  297. {/if}
  298. <Models
  299. bind:this={modelsElement}
  300. bind:prompt
  301. bind:user
  302. bind:chatInputPlaceholder
  303. {messages}
  304. on:select={(e) => {
  305. atSelectedModel = e.detail;
  306. chatTextAreaElement?.focus();
  307. }}
  308. />
  309. {#if atSelectedModel !== undefined}
  310. <div
  311. class="px-3 py-2.5 text-left w-full flex justify-between items-center absolute bottom-0 left-0 right-0 bg-gradient-to-t from-50% from-white dark:from-gray-900"
  312. >
  313. <div class="flex items-center gap-2 text-sm dark:text-gray-500">
  314. <img
  315. crossorigin="anonymous"
  316. alt="model profile"
  317. class="size-5 max-w-[28px] object-cover rounded-full"
  318. src={$models.find((model) => model.id === atSelectedModel.id)?.info?.meta
  319. ?.profile_image_url ??
  320. ($i18n.language === 'dg-DG'
  321. ? `/doge.png`
  322. : `${WEBUI_BASE_URL}/static/favicon.png`)}
  323. />
  324. <div>
  325. Talking to <span class=" font-medium">{atSelectedModel.name}</span>
  326. </div>
  327. </div>
  328. <div>
  329. <button
  330. class="flex items-center"
  331. on:click={() => {
  332. atSelectedModel = undefined;
  333. }}
  334. >
  335. <XMark />
  336. </button>
  337. </div>
  338. </div>
  339. {/if}
  340. </div>
  341. </div>
  342. </div>
  343. <div class="bg-white dark:bg-gray-900">
  344. <div class="max-w-6xl px-2.5 md:px-6 mx-auto inset-x-0">
  345. <div class=" pb-2">
  346. <input
  347. bind:this={filesInputElement}
  348. bind:files={inputFiles}
  349. type="file"
  350. hidden
  351. multiple
  352. on:change={async () => {
  353. if (inputFiles && inputFiles.length > 0) {
  354. const _inputFiles = Array.from(inputFiles);
  355. _inputFiles.forEach((file) => {
  356. if (['image/gif', 'image/webp', 'image/jpeg', 'image/png'].includes(file['type'])) {
  357. if (visionCapableModels.length === 0) {
  358. toast.error($i18n.t('Selected model(s) do not support image inputs'));
  359. inputFiles = null;
  360. filesInputElement.value = '';
  361. return;
  362. }
  363. let reader = new FileReader();
  364. reader.onload = (event) => {
  365. files = [
  366. ...files,
  367. {
  368. type: 'image',
  369. url: `${event.target.result}`
  370. }
  371. ];
  372. inputFiles = null;
  373. filesInputElement.value = '';
  374. };
  375. reader.readAsDataURL(file);
  376. } else if (
  377. SUPPORTED_FILE_TYPE.includes(file['type']) ||
  378. SUPPORTED_FILE_EXTENSIONS.includes(file.name.split('.').at(-1))
  379. ) {
  380. uploadDoc(file);
  381. filesInputElement.value = '';
  382. } else {
  383. toast.error(
  384. $i18n.t(
  385. `Unknown File Type '{{file_type}}', but accepting and treating as plain text`,
  386. { file_type: file['type'] }
  387. )
  388. );
  389. uploadDoc(file);
  390. filesInputElement.value = '';
  391. }
  392. });
  393. } else {
  394. toast.error($i18n.t(`File not found.`));
  395. }
  396. }}
  397. />
  398. {#if recording}
  399. <VoiceRecording
  400. bind:recording
  401. on:cancel={async () => {
  402. recording = false;
  403. await tick();
  404. document.getElementById('chat-textarea')?.focus();
  405. }}
  406. on:confirm={async (e) => {
  407. const response = e.detail;
  408. prompt = `${prompt}${response} `;
  409. recording = false;
  410. await tick();
  411. document.getElementById('chat-textarea')?.focus();
  412. if ($settings?.speechAutoSend ?? false) {
  413. submitPrompt(prompt, user);
  414. }
  415. }}
  416. />
  417. {:else}
  418. <form
  419. class="w-full flex gap-1.5"
  420. on:submit|preventDefault={() => {
  421. // check if selectedModels support image input
  422. submitPrompt(prompt, user);
  423. }}
  424. >
  425. <div
  426. class="flex-1 flex flex-col relative w-full rounded-3xl px-1.5 bg-gray-50 dark:bg-gray-850 dark:text-gray-100"
  427. dir={$settings?.chatDirection ?? 'LTR'}
  428. >
  429. {#if files.length > 0}
  430. <div class="mx-2 mt-2 mb-1 flex flex-wrap gap-2">
  431. {#each files as file, fileIdx}
  432. <div class=" relative group">
  433. {#if file.type === 'image'}
  434. <div class="relative">
  435. <img
  436. src={file.url}
  437. alt="input"
  438. class=" h-16 w-16 rounded-xl object-cover"
  439. />
  440. {#if atSelectedModel ? visionCapableModels.length === 0 : selectedModels.length !== visionCapableModels.length}
  441. <Tooltip
  442. className=" absolute top-1 left-1"
  443. content={$i18n.t('{{ models }}', {
  444. models: [...(atSelectedModel ? [atSelectedModel] : selectedModels)]
  445. .filter((id) => !visionCapableModels.includes(id))
  446. .join(', ')
  447. })}
  448. >
  449. <svg
  450. xmlns="http://www.w3.org/2000/svg"
  451. viewBox="0 0 24 24"
  452. fill="currentColor"
  453. class="size-4 fill-yellow-300"
  454. >
  455. <path
  456. fill-rule="evenodd"
  457. d="M9.401 3.003c1.155-2 4.043-2 5.197 0l7.355 12.748c1.154 2-.29 4.5-2.599 4.5H4.645c-2.309 0-3.752-2.5-2.598-4.5L9.4 3.003ZM12 8.25a.75.75 0 0 1 .75.75v3.75a.75.75 0 0 1-1.5 0V9a.75.75 0 0 1 .75-.75Zm0 8.25a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5Z"
  458. clip-rule="evenodd"
  459. />
  460. </svg>
  461. </Tooltip>
  462. {/if}
  463. </div>
  464. {:else if file.type === 'doc'}
  465. <div
  466. 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"
  467. >
  468. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  469. {#if file.upload_status}
  470. <svg
  471. xmlns="http://www.w3.org/2000/svg"
  472. viewBox="0 0 24 24"
  473. fill="currentColor"
  474. class="w-6 h-6"
  475. >
  476. <path
  477. fill-rule="evenodd"
  478. 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"
  479. clip-rule="evenodd"
  480. />
  481. <path
  482. 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"
  483. />
  484. </svg>
  485. {:else}
  486. <svg
  487. class=" w-6 h-6 translate-y-[0.5px]"
  488. fill="currentColor"
  489. viewBox="0 0 24 24"
  490. xmlns="http://www.w3.org/2000/svg"
  491. ><style>
  492. .spinner_qM83 {
  493. animation: spinner_8HQG 1.05s infinite;
  494. }
  495. .spinner_oXPr {
  496. animation-delay: 0.1s;
  497. }
  498. .spinner_ZTLf {
  499. animation-delay: 0.2s;
  500. }
  501. @keyframes spinner_8HQG {
  502. 0%,
  503. 57.14% {
  504. animation-timing-function: cubic-bezier(0.33, 0.66, 0.66, 1);
  505. transform: translate(0);
  506. }
  507. 28.57% {
  508. animation-timing-function: cubic-bezier(0.33, 0, 0.66, 0.33);
  509. transform: translateY(-6px);
  510. }
  511. 100% {
  512. transform: translate(0);
  513. }
  514. }
  515. </style><circle
  516. class="spinner_qM83"
  517. cx="4"
  518. cy="12"
  519. r="2.5"
  520. /><circle
  521. class="spinner_qM83 spinner_oXPr"
  522. cx="12"
  523. cy="12"
  524. r="2.5"
  525. /><circle
  526. class="spinner_qM83 spinner_ZTLf"
  527. cx="20"
  528. cy="12"
  529. r="2.5"
  530. /></svg
  531. >
  532. {/if}
  533. </div>
  534. <div class="flex flex-col justify-center -space-y-0.5">
  535. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  536. {file.name}
  537. </div>
  538. <div class=" text-gray-500 text-sm">{$i18n.t('Document')}</div>
  539. </div>
  540. </div>
  541. {:else if file.type === 'collection'}
  542. <div
  543. 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"
  544. >
  545. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  546. <svg
  547. xmlns="http://www.w3.org/2000/svg"
  548. viewBox="0 0 24 24"
  549. fill="currentColor"
  550. class="w-6 h-6"
  551. >
  552. <path
  553. 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"
  554. />
  555. <path
  556. 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"
  557. />
  558. </svg>
  559. </div>
  560. <div class="flex flex-col justify-center -space-y-0.5">
  561. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  562. {file?.title ?? `#${file.name}`}
  563. </div>
  564. <div class=" text-gray-500 text-sm">{$i18n.t('Collection')}</div>
  565. </div>
  566. </div>
  567. {/if}
  568. <div class=" absolute -top-1 -right-1">
  569. <button
  570. class=" bg-gray-400 text-white border border-white rounded-full group-hover:visible invisible transition"
  571. type="button"
  572. on:click={() => {
  573. files.splice(fileIdx, 1);
  574. files = files;
  575. }}
  576. >
  577. <svg
  578. xmlns="http://www.w3.org/2000/svg"
  579. viewBox="0 0 20 20"
  580. fill="currentColor"
  581. class="w-4 h-4"
  582. >
  583. <path
  584. 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"
  585. />
  586. </svg>
  587. </button>
  588. </div>
  589. </div>
  590. {/each}
  591. </div>
  592. {/if}
  593. <div class=" flex">
  594. <div class=" ml-0.5 self-end mb-1.5 flex space-x-1">
  595. <InputMenu
  596. bind:webSearchEnabled
  597. bind:selectedToolIds
  598. tools={availableTools}
  599. uploadFilesHandler={() => {
  600. filesInputElement.click();
  601. }}
  602. onClose={async () => {
  603. await tick();
  604. chatTextAreaElement?.focus();
  605. }}
  606. >
  607. <button
  608. 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-2 outline-none focus:outline-none"
  609. type="button"
  610. >
  611. <svg
  612. xmlns="http://www.w3.org/2000/svg"
  613. viewBox="0 0 16 16"
  614. fill="currentColor"
  615. class="size-5"
  616. >
  617. <path
  618. 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"
  619. />
  620. </svg>
  621. </button>
  622. </InputMenu>
  623. </div>
  624. <textarea
  625. id="chat-textarea"
  626. bind:this={chatTextAreaElement}
  627. class="scrollbar-hidden bg-gray-50 dark:bg-gray-850 dark:text-gray-100 outline-none w-full py-3 px-1 rounded-xl resize-none h-[48px]"
  628. placeholder={chatInputPlaceholder !== ''
  629. ? chatInputPlaceholder
  630. : $i18n.t('Send a Message')}
  631. bind:value={prompt}
  632. on:keypress={(e) => {
  633. if (
  634. !$mobile ||
  635. !(
  636. 'ontouchstart' in window ||
  637. navigator.maxTouchPoints > 0 ||
  638. navigator.msMaxTouchPoints > 0
  639. )
  640. ) {
  641. // Prevent Enter key from creating a new line
  642. if (e.key === 'Enter' && !e.shiftKey) {
  643. e.preventDefault();
  644. }
  645. // Submit the prompt when Enter key is pressed
  646. if (prompt !== '' && e.key === 'Enter' && !e.shiftKey) {
  647. submitPrompt(prompt, user);
  648. }
  649. }
  650. }}
  651. on:keydown={async (e) => {
  652. const isCtrlPressed = e.ctrlKey || e.metaKey; // metaKey is for Cmd key on Mac
  653. // Check if Ctrl + R is pressed
  654. if (prompt === '' && isCtrlPressed && e.key.toLowerCase() === 'r') {
  655. e.preventDefault();
  656. console.log('regenerate');
  657. const regenerateButton = [
  658. ...document.getElementsByClassName('regenerate-response-button')
  659. ]?.at(-1);
  660. regenerateButton?.click();
  661. }
  662. if (prompt === '' && e.key == 'ArrowUp') {
  663. e.preventDefault();
  664. const userMessageElement = [
  665. ...document.getElementsByClassName('user-message')
  666. ]?.at(-1);
  667. const editButton = [
  668. ...document.getElementsByClassName('edit-user-message-button')
  669. ]?.at(-1);
  670. console.log(userMessageElement);
  671. userMessageElement.scrollIntoView({ block: 'center' });
  672. editButton?.click();
  673. }
  674. if (['/', '#', '@'].includes(prompt.charAt(0)) && e.key === 'ArrowUp') {
  675. e.preventDefault();
  676. (promptsElement || documentsElement || modelsElement).selectUp();
  677. const commandOptionButton = [
  678. ...document.getElementsByClassName('selected-command-option-button')
  679. ]?.at(-1);
  680. commandOptionButton.scrollIntoView({ block: 'center' });
  681. }
  682. if (['/', '#', '@'].includes(prompt.charAt(0)) && e.key === 'ArrowDown') {
  683. e.preventDefault();
  684. (promptsElement || documentsElement || modelsElement).selectDown();
  685. const commandOptionButton = [
  686. ...document.getElementsByClassName('selected-command-option-button')
  687. ]?.at(-1);
  688. commandOptionButton.scrollIntoView({ block: 'center' });
  689. }
  690. if (['/', '#', '@'].includes(prompt.charAt(0)) && e.key === 'Enter') {
  691. e.preventDefault();
  692. const commandOptionButton = [
  693. ...document.getElementsByClassName('selected-command-option-button')
  694. ]?.at(-1);
  695. if (e.shiftKey) {
  696. prompt = `${prompt}\n`;
  697. } else if (commandOptionButton) {
  698. commandOptionButton?.click();
  699. } else {
  700. document.getElementById('send-message-button')?.click();
  701. }
  702. }
  703. if (['/', '#', '@'].includes(prompt.charAt(0)) && e.key === 'Tab') {
  704. e.preventDefault();
  705. const commandOptionButton = [
  706. ...document.getElementsByClassName('selected-command-option-button')
  707. ]?.at(-1);
  708. commandOptionButton?.click();
  709. } else if (e.key === 'Tab') {
  710. const words = findWordIndices(prompt);
  711. if (words.length > 0) {
  712. const word = words.at(0);
  713. const fullPrompt = prompt;
  714. prompt = prompt.substring(0, word?.endIndex + 1);
  715. await tick();
  716. e.target.scrollTop = e.target.scrollHeight;
  717. prompt = fullPrompt;
  718. await tick();
  719. e.preventDefault();
  720. e.target.setSelectionRange(word?.startIndex, word.endIndex + 1);
  721. }
  722. e.target.style.height = '';
  723. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  724. }
  725. if (e.key === 'Escape') {
  726. console.log('Escape');
  727. atSelectedModel = undefined;
  728. }
  729. }}
  730. rows="1"
  731. on:input={(e) => {
  732. e.target.style.height = '';
  733. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  734. user = null;
  735. }}
  736. on:focus={(e) => {
  737. e.target.style.height = '';
  738. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  739. }}
  740. on:paste={(e) => {
  741. const clipboardData = e.clipboardData || window.clipboardData;
  742. if (clipboardData && clipboardData.items) {
  743. for (const item of clipboardData.items) {
  744. if (item.type.indexOf('image') !== -1) {
  745. const blob = item.getAsFile();
  746. const reader = new FileReader();
  747. reader.onload = function (e) {
  748. files = [
  749. ...files,
  750. {
  751. type: 'image',
  752. url: `${e.target.result}`
  753. }
  754. ];
  755. };
  756. reader.readAsDataURL(blob);
  757. }
  758. }
  759. }
  760. }}
  761. />
  762. <div class="self-end mb-2 flex space-x-1 mr-1">
  763. {#if messages.length == 0 || messages.at(-1).done == true}
  764. <Tooltip content={$i18n.t('Record voice')}>
  765. <button
  766. id="voice-input-button"
  767. 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"
  768. type="button"
  769. on:click={async () => {
  770. try {
  771. const res = await navigator.mediaDevices
  772. .getUserMedia({ audio: true })
  773. .catch(function (err) {
  774. toast.error(
  775. $i18n.t(
  776. `Permission denied when accessing microphone: {{error}}`,
  777. {
  778. error: err
  779. }
  780. )
  781. );
  782. return null;
  783. });
  784. if (res) {
  785. recording = true;
  786. }
  787. } catch {
  788. toast.error($i18n.t('Permission denied when accessing microphone'));
  789. }
  790. }}
  791. >
  792. <svg
  793. xmlns="http://www.w3.org/2000/svg"
  794. viewBox="0 0 20 20"
  795. fill="currentColor"
  796. class="w-5 h-5 translate-y-[0.5px]"
  797. >
  798. <path d="M7 4a3 3 0 016 0v6a3 3 0 11-6 0V4z" />
  799. <path
  800. 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"
  801. />
  802. </svg>
  803. </button>
  804. </Tooltip>
  805. {/if}
  806. </div>
  807. </div>
  808. </div>
  809. <div class="flex items-end w-10">
  810. {#if messages.length == 0 || messages.at(-1).done == true}
  811. {#if prompt === ''}
  812. <div class=" flex items-center mb-1">
  813. <Tooltip content={$i18n.t('Call')}>
  814. <button
  815. class=" text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-850 transition rounded-full p-2 self-center"
  816. type="button"
  817. on:click={async () => {
  818. if (selectedModels.length > 1) {
  819. toast.error($i18n.t('Select only one model to call'));
  820. return;
  821. }
  822. if ($config.audio.stt.engine === 'web') {
  823. toast.error(
  824. $i18n.t('Call feature is not supported when using Web STT engine')
  825. );
  826. return;
  827. }
  828. // check if user has access to getUserMedia
  829. try {
  830. await navigator.mediaDevices.getUserMedia({ audio: true, video: true });
  831. // If the user grants the permission, proceed to show the call overlay
  832. showCallOverlay.set(true);
  833. } catch (err) {
  834. // If the user denies the permission or an error occurs, show an error message
  835. toast.error($i18n.t('Permission denied when accessing media devices'));
  836. }
  837. }}
  838. >
  839. <Headphone className="size-6" />
  840. </button>
  841. </Tooltip>
  842. </div>
  843. {:else}
  844. <div class=" flex items-center mb-1">
  845. <Tooltip content={$i18n.t('Send message')}>
  846. <button
  847. id="send-message-button"
  848. class="{prompt !== ''
  849. ? 'bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 '
  850. : 'text-white bg-gray-200 dark:text-gray-900 dark:bg-gray-700 disabled'} transition rounded-full p-1.5 m-0.5 self-center"
  851. type="submit"
  852. disabled={prompt === ''}
  853. >
  854. <svg
  855. xmlns="http://www.w3.org/2000/svg"
  856. viewBox="0 0 16 16"
  857. fill="currentColor"
  858. class="size-6"
  859. >
  860. <path
  861. fill-rule="evenodd"
  862. 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"
  863. clip-rule="evenodd"
  864. />
  865. </svg>
  866. </button>
  867. </Tooltip>
  868. </div>
  869. {/if}
  870. {:else}
  871. <div class=" flex items-center mb-1.5">
  872. <button
  873. 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"
  874. on:click={() => {
  875. stopResponse();
  876. }}
  877. >
  878. <svg
  879. xmlns="http://www.w3.org/2000/svg"
  880. viewBox="0 0 24 24"
  881. fill="currentColor"
  882. class="size-6"
  883. >
  884. <path
  885. fill-rule="evenodd"
  886. 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"
  887. clip-rule="evenodd"
  888. />
  889. </svg>
  890. </button>
  891. </div>
  892. {/if}
  893. </div>
  894. </form>
  895. {/if}
  896. <div class="mt-1.5 text-xs text-gray-500 text-center line-clamp-1">
  897. {$i18n.t('LLMs can make mistakes. Verify important information.')}
  898. </div>
  899. </div>
  900. </div>
  901. </div>
  902. </div>
  903. <style>
  904. .scrollbar-hidden:active::-webkit-scrollbar-thumb,
  905. .scrollbar-hidden:focus::-webkit-scrollbar-thumb,
  906. .scrollbar-hidden:hover::-webkit-scrollbar-thumb {
  907. visibility: visible;
  908. }
  909. .scrollbar-hidden::-webkit-scrollbar-thumb {
  910. visibility: hidden;
  911. }
  912. </style>