MessageInput.svelte 33 KB

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