MessageInput.svelte 41 KB

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