MessageInput.svelte 35 KB

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