MessageInput.svelte 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. bind:value={prompt}
  526. on:enter={async (e) => {
  527. const commandsContainerElement =
  528. document.getElementById('commands-container');
  529. if (commandsContainerElement) {
  530. e.preventDefault();
  531. const commandOptionButton = [
  532. ...document.getElementsByClassName('selected-command-option-button')
  533. ]?.at(-1);
  534. if (commandOptionButton) {
  535. commandOptionButton?.click();
  536. return;
  537. }
  538. }
  539. if (prompt !== '') {
  540. dispatch('submit', prompt);
  541. }
  542. }}
  543. on:keypress={(e) => {
  544. e = e.detail.event;
  545. }}
  546. on:keydown={async (e) => {
  547. e = e.detail.event;
  548. const isCtrlPressed = e.ctrlKey || e.metaKey; // metaKey is for Cmd key on Mac
  549. const commandsContainerElement =
  550. document.getElementById('commands-container');
  551. // Command/Ctrl + Shift + Enter to submit a message pair
  552. if (isCtrlPressed && e.key === 'Enter' && e.shiftKey) {
  553. e.preventDefault();
  554. createMessagePair(prompt);
  555. }
  556. // Check if Ctrl + R is pressed
  557. if (prompt === '' && isCtrlPressed && e.key.toLowerCase() === 'r') {
  558. e.preventDefault();
  559. console.log('regenerate');
  560. const regenerateButton = [
  561. ...document.getElementsByClassName('regenerate-response-button')
  562. ]?.at(-1);
  563. regenerateButton?.click();
  564. }
  565. if (prompt === '' && e.key == 'ArrowUp') {
  566. e.preventDefault();
  567. const userMessageElement = [
  568. ...document.getElementsByClassName('user-message')
  569. ]?.at(-1);
  570. const editButton = [
  571. ...document.getElementsByClassName('edit-user-message-button')
  572. ]?.at(-1);
  573. console.log(userMessageElement);
  574. userMessageElement.scrollIntoView({ block: 'center' });
  575. editButton?.click();
  576. }
  577. if (commandsContainerElement && e.key === 'ArrowUp') {
  578. e.preventDefault();
  579. commandsElement.selectUp();
  580. const commandOptionButton = [
  581. ...document.getElementsByClassName('selected-command-option-button')
  582. ]?.at(-1);
  583. commandOptionButton.scrollIntoView({ block: 'center' });
  584. }
  585. if (commandsContainerElement && e.key === 'ArrowDown') {
  586. e.preventDefault();
  587. commandsElement.selectDown();
  588. const commandOptionButton = [
  589. ...document.getElementsByClassName('selected-command-option-button')
  590. ]?.at(-1);
  591. commandOptionButton.scrollIntoView({ block: 'center' });
  592. }
  593. if (commandsContainerElement && e.key === 'Tab') {
  594. e.preventDefault();
  595. const commandOptionButton = [
  596. ...document.getElementsByClassName('selected-command-option-button')
  597. ]?.at(-1);
  598. commandOptionButton?.click();
  599. }
  600. if (e.key === 'Escape') {
  601. console.log('Escape');
  602. atSelectedModel = undefined;
  603. selectedToolIds = [];
  604. webSearchEnabled = false;
  605. }
  606. }}
  607. on:paste={async (e) => {
  608. e = e.detail.event;
  609. console.log(e);
  610. const clipboardData = e.clipboardData || window.clipboardData;
  611. if (clipboardData && clipboardData.items) {
  612. for (const item of clipboardData.items) {
  613. if (item.type.indexOf('image') !== -1) {
  614. const blob = item.getAsFile();
  615. const reader = new FileReader();
  616. reader.onload = function (e) {
  617. files = [
  618. ...files,
  619. {
  620. type: 'image',
  621. url: `${e.target.result}`
  622. }
  623. ];
  624. };
  625. reader.readAsDataURL(blob);
  626. } else if (item.type === 'text/plain') {
  627. if ($settings?.largeTextAsFile ?? false) {
  628. const text = clipboardData.getData('text/plain');
  629. if (text.length > PASTED_TEXT_CHARACTER_LIMIT) {
  630. e.preventDefault();
  631. const blob = new Blob([text], { type: 'text/plain' });
  632. const file = new File([blob], `Pasted_Text_${Date.now()}.txt`, {
  633. type: 'text/plain'
  634. });
  635. await uploadFileHandler(file, true);
  636. }
  637. }
  638. }
  639. }
  640. }
  641. }}
  642. />
  643. </div>
  644. {:else}
  645. <textarea
  646. id="chat-input"
  647. bind:this={chatInputElement}
  648. 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]"
  649. placeholder={placeholder ? placeholder : $i18n.t('Send a Message')}
  650. bind:value={prompt}
  651. on:keypress={(e) => {
  652. if (
  653. !$mobile ||
  654. !(
  655. 'ontouchstart' in window ||
  656. navigator.maxTouchPoints > 0 ||
  657. navigator.msMaxTouchPoints > 0
  658. )
  659. ) {
  660. // Prevent Enter key from creating a new line
  661. if (e.key === 'Enter' && !e.shiftKey) {
  662. e.preventDefault();
  663. }
  664. // Submit the prompt when Enter key is pressed
  665. if (prompt !== '' && e.key === 'Enter' && !e.shiftKey) {
  666. dispatch('submit', prompt);
  667. }
  668. }
  669. }}
  670. on:keydown={async (e) => {
  671. const isCtrlPressed = e.ctrlKey || e.metaKey; // metaKey is for Cmd key on Mac
  672. const commandsContainerElement =
  673. document.getElementById('commands-container');
  674. // Command/Ctrl + Shift + Enter to submit a message pair
  675. if (isCtrlPressed && e.key === 'Enter' && e.shiftKey) {
  676. e.preventDefault();
  677. createMessagePair(prompt);
  678. }
  679. // Check if Ctrl + R is pressed
  680. if (prompt === '' && isCtrlPressed && e.key.toLowerCase() === 'r') {
  681. e.preventDefault();
  682. console.log('regenerate');
  683. const regenerateButton = [
  684. ...document.getElementsByClassName('regenerate-response-button')
  685. ]?.at(-1);
  686. regenerateButton?.click();
  687. }
  688. if (prompt === '' && e.key == 'ArrowUp') {
  689. e.preventDefault();
  690. const userMessageElement = [
  691. ...document.getElementsByClassName('user-message')
  692. ]?.at(-1);
  693. const editButton = [
  694. ...document.getElementsByClassName('edit-user-message-button')
  695. ]?.at(-1);
  696. console.log(userMessageElement);
  697. userMessageElement.scrollIntoView({ block: 'center' });
  698. editButton?.click();
  699. }
  700. if (commandsContainerElement && e.key === 'ArrowUp') {
  701. e.preventDefault();
  702. commandsElement.selectUp();
  703. const commandOptionButton = [
  704. ...document.getElementsByClassName('selected-command-option-button')
  705. ]?.at(-1);
  706. commandOptionButton.scrollIntoView({ block: 'center' });
  707. }
  708. if (commandsContainerElement && e.key === 'ArrowDown') {
  709. e.preventDefault();
  710. commandsElement.selectDown();
  711. const commandOptionButton = [
  712. ...document.getElementsByClassName('selected-command-option-button')
  713. ]?.at(-1);
  714. commandOptionButton.scrollIntoView({ block: 'center' });
  715. }
  716. if (commandsContainerElement && e.key === 'Enter') {
  717. e.preventDefault();
  718. const commandOptionButton = [
  719. ...document.getElementsByClassName('selected-command-option-button')
  720. ]?.at(-1);
  721. if (e.shiftKey) {
  722. prompt = `${prompt}\n`;
  723. } else if (commandOptionButton) {
  724. commandOptionButton?.click();
  725. } else {
  726. document.getElementById('send-message-button')?.click();
  727. }
  728. }
  729. if (commandsContainerElement && e.key === 'Tab') {
  730. e.preventDefault();
  731. const commandOptionButton = [
  732. ...document.getElementsByClassName('selected-command-option-button')
  733. ]?.at(-1);
  734. commandOptionButton?.click();
  735. } else if (e.key === 'Tab') {
  736. const words = findWordIndices(prompt);
  737. if (words.length > 0) {
  738. const word = words.at(0);
  739. const fullPrompt = prompt;
  740. prompt = prompt.substring(0, word?.endIndex + 1);
  741. await tick();
  742. e.target.scrollTop = e.target.scrollHeight;
  743. prompt = fullPrompt;
  744. await tick();
  745. e.preventDefault();
  746. e.target.setSelectionRange(word?.startIndex, word.endIndex + 1);
  747. }
  748. e.target.style.height = '';
  749. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  750. }
  751. if (e.key === 'Escape') {
  752. console.log('Escape');
  753. atSelectedModel = undefined;
  754. selectedToolIds = [];
  755. webSearchEnabled = false;
  756. }
  757. }}
  758. rows="1"
  759. on:input={async (e) => {
  760. e.target.style.height = '';
  761. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  762. }}
  763. on:focus={async (e) => {
  764. e.target.style.height = '';
  765. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  766. }}
  767. on:paste={async (e) => {
  768. const clipboardData = e.clipboardData || window.clipboardData;
  769. if (clipboardData && clipboardData.items) {
  770. for (const item of clipboardData.items) {
  771. if (item.type.indexOf('image') !== -1) {
  772. const blob = item.getAsFile();
  773. const reader = new FileReader();
  774. reader.onload = function (e) {
  775. files = [
  776. ...files,
  777. {
  778. type: 'image',
  779. url: `${e.target.result}`
  780. }
  781. ];
  782. };
  783. reader.readAsDataURL(blob);
  784. } else if (item.type === 'text/plain') {
  785. if ($settings?.largeTextAsFile ?? false) {
  786. const text = clipboardData.getData('text/plain');
  787. if (text.length > PASTED_TEXT_CHARACTER_LIMIT) {
  788. e.preventDefault();
  789. const blob = new Blob([text], { type: 'text/plain' });
  790. const file = new File([blob], `Pasted_Text_${Date.now()}.txt`, {
  791. type: 'text/plain'
  792. });
  793. await uploadFileHandler(file, true);
  794. }
  795. }
  796. }
  797. }
  798. }
  799. }}
  800. />
  801. {/if}
  802. <div class="self-end mb-2 flex space-x-1 mr-1">
  803. {#if !history?.currentId || history.messages[history.currentId]?.done == true}
  804. <Tooltip content={$i18n.t('Record voice')}>
  805. <button
  806. id="voice-input-button"
  807. 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"
  808. type="button"
  809. on:click={async () => {
  810. try {
  811. let stream = await navigator.mediaDevices
  812. .getUserMedia({ audio: true })
  813. .catch(function (err) {
  814. toast.error(
  815. $i18n.t(
  816. `Permission denied when accessing microphone: {{error}}`,
  817. {
  818. error: err
  819. }
  820. )
  821. );
  822. return null;
  823. });
  824. if (stream) {
  825. recording = true;
  826. const tracks = stream.getTracks();
  827. tracks.forEach((track) => track.stop());
  828. }
  829. stream = null;
  830. } catch {
  831. toast.error($i18n.t('Permission denied when accessing microphone'));
  832. }
  833. }}
  834. aria-label="Voice Input"
  835. >
  836. <svg
  837. xmlns="http://www.w3.org/2000/svg"
  838. viewBox="0 0 20 20"
  839. fill="currentColor"
  840. class="w-5 h-5 translate-y-[0.5px]"
  841. >
  842. <path d="M7 4a3 3 0 016 0v6a3 3 0 11-6 0V4z" />
  843. <path
  844. 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"
  845. />
  846. </svg>
  847. </button>
  848. </Tooltip>
  849. {/if}
  850. </div>
  851. </div>
  852. </div>
  853. <div class="flex items-end w-10">
  854. {#if !history.currentId || history.messages[history.currentId]?.done == true}
  855. {#if prompt === ''}
  856. <div class=" flex items-center mb-1">
  857. <Tooltip content={$i18n.t('Call')}>
  858. <button
  859. class=" text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-850 transition rounded-full p-2 self-center"
  860. type="button"
  861. on:click={async () => {
  862. if (selectedModels.length > 1) {
  863. toast.error($i18n.t('Select only one model to call'));
  864. return;
  865. }
  866. if ($config.audio.stt.engine === 'web') {
  867. toast.error(
  868. $i18n.t('Call feature is not supported when using Web STT engine')
  869. );
  870. return;
  871. }
  872. // check if user has access to getUserMedia
  873. try {
  874. let stream = await navigator.mediaDevices.getUserMedia({
  875. audio: true
  876. });
  877. // If the user grants the permission, proceed to show the call overlay
  878. if (stream) {
  879. const tracks = stream.getTracks();
  880. tracks.forEach((track) => track.stop());
  881. }
  882. stream = null;
  883. showCallOverlay.set(true);
  884. showControls.set(true);
  885. } catch (err) {
  886. // If the user denies the permission or an error occurs, show an error message
  887. toast.error(
  888. $i18n.t('Permission denied when accessing media devices')
  889. );
  890. }
  891. }}
  892. aria-label="Call"
  893. >
  894. <Headphone className="size-6" />
  895. </button>
  896. </Tooltip>
  897. </div>
  898. {:else}
  899. <div class=" flex items-center mb-1">
  900. <Tooltip content={$i18n.t('Send message')}>
  901. <button
  902. id="send-message-button"
  903. class="{prompt !== ''
  904. ? 'bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 '
  905. : '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"
  906. type="submit"
  907. disabled={prompt === ''}
  908. >
  909. <svg
  910. xmlns="http://www.w3.org/2000/svg"
  911. viewBox="0 0 16 16"
  912. fill="currentColor"
  913. class="size-6"
  914. >
  915. <path
  916. fill-rule="evenodd"
  917. 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"
  918. clip-rule="evenodd"
  919. />
  920. </svg>
  921. </button>
  922. </Tooltip>
  923. </div>
  924. {/if}
  925. {:else}
  926. <div class=" flex items-center mb-1.5">
  927. <Tooltip content={$i18n.t('Stop')}>
  928. <button
  929. 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"
  930. on:click={() => {
  931. stopResponse();
  932. }}
  933. >
  934. <svg
  935. xmlns="http://www.w3.org/2000/svg"
  936. viewBox="0 0 24 24"
  937. fill="currentColor"
  938. class="size-6"
  939. >
  940. <path
  941. fill-rule="evenodd"
  942. 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"
  943. clip-rule="evenodd"
  944. />
  945. </svg>
  946. </button>
  947. </Tooltip>
  948. </div>
  949. {/if}
  950. </div>
  951. </form>
  952. {/if}
  953. </div>
  954. </div>
  955. </div>
  956. </div>
  957. {/if}