MessageInput.svelte 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. <script lang="ts">
  2. import DOMPurify from 'dompurify';
  3. import { marked } from 'marked';
  4. import { toast } from 'svelte-sonner';
  5. import { v4 as uuidv4 } from 'uuid';
  6. import { createPicker, getAuthToken } from '$lib/utils/google-drive-picker';
  7. import { pickAndDownloadFile } from '$lib/utils/onedrive-file-picker';
  8. import { onMount, tick, getContext, createEventDispatcher, onDestroy } from 'svelte';
  9. const dispatch = createEventDispatcher();
  10. import {
  11. type Model,
  12. mobile,
  13. settings,
  14. models,
  15. config,
  16. showCallOverlay,
  17. tools,
  18. toolServers,
  19. user as _user,
  20. showControls,
  21. TTSWorker,
  22. temporaryChatEnabled
  23. } from '$lib/stores';
  24. import {
  25. convertHeicToJpeg,
  26. compressImage,
  27. createMessagesList,
  28. extractContentFromFile,
  29. extractCurlyBraceWords,
  30. extractInputVariables,
  31. getAge,
  32. getCurrentDateTime,
  33. getFormattedDate,
  34. getFormattedTime,
  35. getUserPosition,
  36. getUserTimezone,
  37. getWeekday
  38. } from '$lib/utils';
  39. import { uploadFile } from '$lib/apis/files';
  40. import { generateAutoCompletion } from '$lib/apis';
  41. import { deleteFileById } from '$lib/apis/files';
  42. import { getSessionUser } from '$lib/apis/auths';
  43. import { getTools } from '$lib/apis/tools';
  44. import { WEBUI_BASE_URL, WEBUI_API_BASE_URL, PASTED_TEXT_CHARACTER_LIMIT } from '$lib/constants';
  45. import InputMenu from './MessageInput/InputMenu.svelte';
  46. import VoiceRecording from './MessageInput/VoiceRecording.svelte';
  47. import FilesOverlay from './MessageInput/FilesOverlay.svelte';
  48. import ToolServersModal from './ToolServersModal.svelte';
  49. import RichTextInput from '../common/RichTextInput.svelte';
  50. import Tooltip from '../common/Tooltip.svelte';
  51. import FileItem from '../common/FileItem.svelte';
  52. import Image from '../common/Image.svelte';
  53. import XMark from '../icons/XMark.svelte';
  54. import Headphone from '../icons/Headphone.svelte';
  55. import GlobeAlt from '../icons/GlobeAlt.svelte';
  56. import Photo from '../icons/Photo.svelte';
  57. import Wrench from '../icons/Wrench.svelte';
  58. import CommandLine from '../icons/CommandLine.svelte';
  59. import Sparkles from '../icons/Sparkles.svelte';
  60. import InputVariablesModal from './MessageInput/InputVariablesModal.svelte';
  61. import Voice from '../icons/Voice.svelte';
  62. import Terminal from '../icons/Terminal.svelte';
  63. import IntegrationsMenu from './MessageInput/IntegrationsMenu.svelte';
  64. import Component from '../icons/Component.svelte';
  65. import PlusAlt from '../icons/PlusAlt.svelte';
  66. import { KokoroWorker } from '$lib/workers/KokoroWorker';
  67. import { getSuggestionRenderer } from '../common/RichTextInput/suggestions';
  68. import CommandSuggestionList from './MessageInput/CommandSuggestionList.svelte';
  69. import Knobs from '../icons/Knobs.svelte';
  70. import ValvesModal from '../workspace/common/ValvesModal.svelte';
  71. const i18n = getContext('i18n');
  72. export let onChange: Function = () => {};
  73. export let createMessagePair: Function;
  74. export let stopResponse: Function;
  75. export let autoScroll = false;
  76. export let generating = false;
  77. export let atSelectedModel: Model | undefined = undefined;
  78. export let selectedModels: [''];
  79. let selectedModelIds = [];
  80. $: selectedModelIds = atSelectedModel !== undefined ? [atSelectedModel.id] : selectedModels;
  81. export let history;
  82. export let taskIds = null;
  83. export let prompt = '';
  84. export let files = [];
  85. export let selectedToolIds = [];
  86. export let selectedFilterIds = [];
  87. export let imageGenerationEnabled = false;
  88. export let webSearchEnabled = false;
  89. export let codeInterpreterEnabled = false;
  90. let showInputVariablesModal = false;
  91. let inputVariablesModalCallback = (variableValues) => {};
  92. let inputVariables = {};
  93. let inputVariableValues = {};
  94. let showValvesModal = false;
  95. let selectedValvesType = 'tool'; // 'tool' or 'function'
  96. let selectedValvesItemId = null;
  97. $: onChange({
  98. prompt,
  99. files: files
  100. .filter((file) => file.type !== 'image')
  101. .map((file) => {
  102. return {
  103. ...file,
  104. user: undefined,
  105. access_control: undefined
  106. };
  107. }),
  108. selectedToolIds,
  109. selectedFilterIds,
  110. imageGenerationEnabled,
  111. webSearchEnabled,
  112. codeInterpreterEnabled
  113. });
  114. const inputVariableHandler = async (text: string): Promise<string> => {
  115. inputVariables = extractInputVariables(text);
  116. // No variables? return the original text immediately.
  117. if (Object.keys(inputVariables).length === 0) {
  118. return text;
  119. }
  120. // Show modal and wait for the user's input.
  121. showInputVariablesModal = true;
  122. return await new Promise<string>((resolve) => {
  123. inputVariablesModalCallback = (variableValues) => {
  124. inputVariableValues = { ...inputVariableValues, ...variableValues };
  125. replaceVariables(inputVariableValues);
  126. showInputVariablesModal = false;
  127. resolve(text);
  128. };
  129. });
  130. };
  131. const textVariableHandler = async (text: string) => {
  132. if (text.includes('{{CLIPBOARD}}')) {
  133. const clipboardText = await navigator.clipboard.readText().catch((err) => {
  134. toast.error($i18n.t('Failed to read clipboard contents'));
  135. return '{{CLIPBOARD}}';
  136. });
  137. const clipboardItems = await navigator.clipboard.read();
  138. let imageUrl = null;
  139. for (const item of clipboardItems) {
  140. // Check for known image types
  141. for (const type of item.types) {
  142. if (type.startsWith('image/')) {
  143. const blob = await item.getType(type);
  144. imageUrl = URL.createObjectURL(blob);
  145. }
  146. }
  147. }
  148. if (imageUrl) {
  149. files = [
  150. ...files,
  151. {
  152. type: 'image',
  153. url: imageUrl
  154. }
  155. ];
  156. }
  157. text = text.replaceAll('{{CLIPBOARD}}', clipboardText);
  158. }
  159. if (text.includes('{{USER_LOCATION}}')) {
  160. let location;
  161. try {
  162. location = await getUserPosition();
  163. } catch (error) {
  164. toast.error($i18n.t('Location access not allowed'));
  165. location = 'LOCATION_UNKNOWN';
  166. }
  167. text = text.replaceAll('{{USER_LOCATION}}', String(location));
  168. }
  169. const sessionUser = await getSessionUser(localStorage.token);
  170. if (text.includes('{{USER_NAME}}')) {
  171. const name = sessionUser?.name || 'User';
  172. text = text.replaceAll('{{USER_NAME}}', name);
  173. }
  174. if (text.includes('{{USER_BIO}}')) {
  175. const bio = sessionUser?.bio || '';
  176. if (bio) {
  177. text = text.replaceAll('{{USER_BIO}}', bio);
  178. }
  179. }
  180. if (text.includes('{{USER_GENDER}}')) {
  181. const gender = sessionUser?.gender || '';
  182. if (gender) {
  183. text = text.replaceAll('{{USER_GENDER}}', gender);
  184. }
  185. }
  186. if (text.includes('{{USER_BIRTH_DATE}}')) {
  187. const birthDate = sessionUser?.date_of_birth || '';
  188. if (birthDate) {
  189. text = text.replaceAll('{{USER_BIRTH_DATE}}', birthDate);
  190. }
  191. }
  192. if (text.includes('{{USER_AGE}}')) {
  193. const birthDate = sessionUser?.date_of_birth || '';
  194. if (birthDate) {
  195. // calculate age using date
  196. const age = getAge(birthDate);
  197. text = text.replaceAll('{{USER_AGE}}', age);
  198. }
  199. }
  200. if (text.includes('{{USER_LANGUAGE}}')) {
  201. const language = localStorage.getItem('locale') || 'en-US';
  202. text = text.replaceAll('{{USER_LANGUAGE}}', language);
  203. }
  204. if (text.includes('{{CURRENT_DATE}}')) {
  205. const date = getFormattedDate();
  206. text = text.replaceAll('{{CURRENT_DATE}}', date);
  207. }
  208. if (text.includes('{{CURRENT_TIME}}')) {
  209. const time = getFormattedTime();
  210. text = text.replaceAll('{{CURRENT_TIME}}', time);
  211. }
  212. if (text.includes('{{CURRENT_DATETIME}}')) {
  213. const dateTime = getCurrentDateTime();
  214. text = text.replaceAll('{{CURRENT_DATETIME}}', dateTime);
  215. }
  216. if (text.includes('{{CURRENT_TIMEZONE}}')) {
  217. const timezone = getUserTimezone();
  218. text = text.replaceAll('{{CURRENT_TIMEZONE}}', timezone);
  219. }
  220. if (text.includes('{{CURRENT_WEEKDAY}}')) {
  221. const weekday = getWeekday();
  222. text = text.replaceAll('{{CURRENT_WEEKDAY}}', weekday);
  223. }
  224. return text;
  225. };
  226. const replaceVariables = (variables: Record<string, any>) => {
  227. console.log('Replacing variables:', variables);
  228. const chatInput = document.getElementById('chat-input');
  229. if (chatInput) {
  230. chatInputElement.replaceVariables(variables);
  231. chatInputElement.focus();
  232. }
  233. };
  234. export const setText = async (text?: string, cb?: (text: string) => void) => {
  235. const chatInput = document.getElementById('chat-input');
  236. if (chatInput) {
  237. if (text !== '') {
  238. text = await textVariableHandler(text || '');
  239. }
  240. chatInputElement?.setText(text);
  241. chatInputElement?.focus();
  242. if (text !== '') {
  243. text = await inputVariableHandler(text);
  244. }
  245. await tick();
  246. if (cb) await cb(text);
  247. }
  248. };
  249. const getCommand = () => {
  250. const chatInput = document.getElementById('chat-input');
  251. let word = '';
  252. if (chatInput) {
  253. word = chatInputElement?.getWordAtDocPos();
  254. }
  255. return word;
  256. };
  257. const replaceCommandWithText = (text) => {
  258. const chatInput = document.getElementById('chat-input');
  259. if (!chatInput) return;
  260. chatInputElement?.replaceCommandWithText(text);
  261. };
  262. const insertTextAtCursor = async (text: string) => {
  263. const chatInput = document.getElementById('chat-input');
  264. if (!chatInput) return;
  265. text = await textVariableHandler(text);
  266. if (command) {
  267. replaceCommandWithText(text);
  268. } else {
  269. chatInputElement?.insertContent(text);
  270. }
  271. await tick();
  272. text = await inputVariableHandler(text);
  273. await tick();
  274. const chatInputContainer = document.getElementById('chat-input-container');
  275. if (chatInputContainer) {
  276. chatInputContainer.scrollTop = chatInputContainer.scrollHeight;
  277. }
  278. await tick();
  279. if (chatInput) {
  280. chatInput.focus();
  281. chatInput.dispatchEvent(new Event('input'));
  282. const words = extractCurlyBraceWords(prompt);
  283. if (words.length > 0) {
  284. const word = words.at(0);
  285. await tick();
  286. } else {
  287. chatInput.scrollTop = chatInput.scrollHeight;
  288. }
  289. }
  290. };
  291. let command = '';
  292. export let showCommands = false;
  293. $: showCommands = ['/', '#', '@'].includes(command?.charAt(0)) || '\\#' === command?.slice(0, 2);
  294. let suggestions = null;
  295. let showTools = false;
  296. let loaded = false;
  297. let recording = false;
  298. let isComposing = false;
  299. // Safari has a bug where compositionend is not triggered correctly #16615
  300. // when using the virtual keyboard on iOS.
  301. let compositionEndedAt = -2e8;
  302. const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
  303. function inOrNearComposition(event: Event) {
  304. if (isComposing) {
  305. return true;
  306. }
  307. // See https://www.stum.de/2016/06/24/handling-ime-events-in-javascript/.
  308. // On Japanese input method editors (IMEs), the Enter key is used to confirm character
  309. // selection. On Safari, when Enter is pressed, compositionend and keydown events are
  310. // emitted. The keydown event triggers newline insertion, which we don't want.
  311. // This method returns true if the keydown event should be ignored.
  312. // We only ignore it once, as pressing Enter a second time *should* insert a newline.
  313. // Furthermore, the keydown event timestamp must be close to the compositionEndedAt timestamp.
  314. // This guards against the case where compositionend is triggered without the keyboard
  315. // (e.g. character confirmation may be done with the mouse), and keydown is triggered
  316. // afterwards- we wouldn't want to ignore the keydown event in this case.
  317. if (isSafari && Math.abs(event.timeStamp - compositionEndedAt) < 500) {
  318. compositionEndedAt = -2e8;
  319. return true;
  320. }
  321. return false;
  322. }
  323. let chatInputContainerElement;
  324. let chatInputElement;
  325. let filesInputElement;
  326. let commandsElement;
  327. let inputFiles;
  328. let dragged = false;
  329. let shiftKey = false;
  330. let user = null;
  331. export let placeholder = '';
  332. let visionCapableModels = [];
  333. $: visionCapableModels = (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).filter(
  334. (model) => $models.find((m) => m.id === model)?.info?.meta?.capabilities?.vision ?? true
  335. );
  336. let fileUploadCapableModels = [];
  337. $: fileUploadCapableModels = (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).filter(
  338. (model) => $models.find((m) => m.id === model)?.info?.meta?.capabilities?.file_upload ?? true
  339. );
  340. let webSearchCapableModels = [];
  341. $: webSearchCapableModels = (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).filter(
  342. (model) => $models.find((m) => m.id === model)?.info?.meta?.capabilities?.web_search ?? true
  343. );
  344. let imageGenerationCapableModels = [];
  345. $: imageGenerationCapableModels = (
  346. atSelectedModel?.id ? [atSelectedModel.id] : selectedModels
  347. ).filter(
  348. (model) =>
  349. $models.find((m) => m.id === model)?.info?.meta?.capabilities?.image_generation ?? true
  350. );
  351. let codeInterpreterCapableModels = [];
  352. $: codeInterpreterCapableModels = (
  353. atSelectedModel?.id ? [atSelectedModel.id] : selectedModels
  354. ).filter(
  355. (model) =>
  356. $models.find((m) => m.id === model)?.info?.meta?.capabilities?.code_interpreter ?? true
  357. );
  358. let toggleFilters = [];
  359. $: toggleFilters = (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels)
  360. .map((id) => ($models.find((model) => model.id === id) || {})?.filters ?? [])
  361. .reduce((acc, filters) => acc.filter((f1) => filters.some((f2) => f2.id === f1.id)));
  362. let showToolsButton = false;
  363. $: showToolsButton = ($tools ?? []).length > 0 || ($toolServers ?? []).length > 0;
  364. let showWebSearchButton = false;
  365. $: showWebSearchButton =
  366. (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).length ===
  367. webSearchCapableModels.length &&
  368. $config?.features?.enable_web_search &&
  369. ($_user.role === 'admin' || $_user?.permissions?.features?.web_search);
  370. let showImageGenerationButton = false;
  371. $: showImageGenerationButton =
  372. (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).length ===
  373. imageGenerationCapableModels.length &&
  374. $config?.features?.enable_image_generation &&
  375. ($_user.role === 'admin' || $_user?.permissions?.features?.image_generation);
  376. let showCodeInterpreterButton = false;
  377. $: showCodeInterpreterButton =
  378. (atSelectedModel?.id ? [atSelectedModel.id] : selectedModels).length ===
  379. codeInterpreterCapableModels.length &&
  380. $config?.features?.enable_code_interpreter &&
  381. ($_user.role === 'admin' || $_user?.permissions?.features?.code_interpreter);
  382. const scrollToBottom = () => {
  383. const element = document.getElementById('messages-container');
  384. element.scrollTo({
  385. top: element.scrollHeight,
  386. behavior: 'smooth'
  387. });
  388. };
  389. const screenCaptureHandler = async () => {
  390. try {
  391. // Request screen media
  392. const mediaStream = await navigator.mediaDevices.getDisplayMedia({
  393. video: { cursor: 'never' },
  394. audio: false
  395. });
  396. // Once the user selects a screen, temporarily create a video element
  397. const video = document.createElement('video');
  398. video.srcObject = mediaStream;
  399. // Ensure the video loads without affecting user experience or tab switching
  400. await video.play();
  401. // Set up the canvas to match the video dimensions
  402. const canvas = document.createElement('canvas');
  403. canvas.width = video.videoWidth;
  404. canvas.height = video.videoHeight;
  405. // Grab a single frame from the video stream using the canvas
  406. const context = canvas.getContext('2d');
  407. context.drawImage(video, 0, 0, canvas.width, canvas.height);
  408. // Stop all video tracks (stop screen sharing) after capturing the image
  409. mediaStream.getTracks().forEach((track) => track.stop());
  410. // bring back focus to this current tab, so that the user can see the screen capture
  411. window.focus();
  412. // Convert the canvas to a Base64 image URL
  413. const imageUrl = canvas.toDataURL('image/png');
  414. // Add the captured image to the files array to render it
  415. files = [...files, { type: 'image', url: imageUrl }];
  416. // Clean memory: Clear video srcObject
  417. video.srcObject = null;
  418. } catch (error) {
  419. // Handle any errors (e.g., user cancels screen sharing)
  420. console.error('Error capturing screen:', error);
  421. }
  422. };
  423. const uploadFileHandler = async (file, fullContext: boolean = false) => {
  424. if ($_user?.role !== 'admin' && !($_user?.permissions?.chat?.file_upload ?? true)) {
  425. toast.error($i18n.t('You do not have permission to upload files.'));
  426. return null;
  427. }
  428. if (fileUploadCapableModels.length !== selectedModels.length) {
  429. toast.error($i18n.t('Model(s) do not support file upload'));
  430. return null;
  431. }
  432. const tempItemId = uuidv4();
  433. const fileItem = {
  434. type: 'file',
  435. file: '',
  436. id: null,
  437. url: '',
  438. name: file.name,
  439. collection_name: '',
  440. status: 'uploading',
  441. size: file.size,
  442. error: '',
  443. itemId: tempItemId,
  444. ...(fullContext ? { context: 'full' } : {})
  445. };
  446. if (fileItem.size == 0) {
  447. toast.error($i18n.t('You cannot upload an empty file.'));
  448. return null;
  449. }
  450. files = [...files, fileItem];
  451. if (!$temporaryChatEnabled) {
  452. try {
  453. // If the file is an audio file, provide the language for STT.
  454. let metadata = null;
  455. if (
  456. (file.type.startsWith('audio/') || file.type.startsWith('video/')) &&
  457. $settings?.audio?.stt?.language
  458. ) {
  459. metadata = {
  460. language: $settings?.audio?.stt?.language
  461. };
  462. }
  463. // During the file upload, file content is automatically extracted.
  464. const uploadedFile = await uploadFile(localStorage.token, file, metadata);
  465. if (uploadedFile) {
  466. console.log('File upload completed:', {
  467. id: uploadedFile.id,
  468. name: fileItem.name,
  469. collection: uploadedFile?.meta?.collection_name
  470. });
  471. if (uploadedFile.error) {
  472. console.warn('File upload warning:', uploadedFile.error);
  473. toast.warning(uploadedFile.error);
  474. }
  475. fileItem.status = 'uploaded';
  476. fileItem.file = uploadedFile;
  477. fileItem.id = uploadedFile.id;
  478. fileItem.collection_name =
  479. uploadedFile?.meta?.collection_name || uploadedFile?.collection_name;
  480. fileItem.url = `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`;
  481. files = files;
  482. } else {
  483. files = files.filter((item) => item?.itemId !== tempItemId);
  484. }
  485. } catch (e) {
  486. toast.error(`${e}`);
  487. files = files.filter((item) => item?.itemId !== tempItemId);
  488. }
  489. } else {
  490. // If temporary chat is enabled, we just add the file to the list without uploading it.
  491. const content = await extractContentFromFile(file).catch((error) => {
  492. toast.error(
  493. $i18n.t('Failed to extract content from the file: {{error}}', { error: error })
  494. );
  495. return null;
  496. });
  497. if (content === null) {
  498. toast.error($i18n.t('Failed to extract content from the file.'));
  499. files = files.filter((item) => item?.itemId !== tempItemId);
  500. return null;
  501. } else {
  502. console.log('Extracted content from file:', {
  503. name: file.name,
  504. size: file.size,
  505. content: content
  506. });
  507. fileItem.status = 'uploaded';
  508. fileItem.type = 'text';
  509. fileItem.content = content;
  510. fileItem.id = uuidv4(); // Temporary ID for the file
  511. files = files;
  512. }
  513. }
  514. };
  515. const inputFilesHandler = async (inputFiles) => {
  516. console.log('Input files handler called with:', inputFiles);
  517. if (
  518. ($config?.file?.max_count ?? null) !== null &&
  519. files.length + inputFiles.length > $config?.file?.max_count
  520. ) {
  521. toast.error(
  522. $i18n.t(`You can only chat with a maximum of {{maxCount}} file(s) at a time.`, {
  523. maxCount: $config?.file?.max_count
  524. })
  525. );
  526. return;
  527. }
  528. inputFiles.forEach(async (file) => {
  529. console.log('Processing file:', {
  530. name: file.name,
  531. type: file.type,
  532. size: file.size,
  533. extension: file.name.split('.').at(-1)
  534. });
  535. if (
  536. ($config?.file?.max_size ?? null) !== null &&
  537. file.size > ($config?.file?.max_size ?? 0) * 1024 * 1024
  538. ) {
  539. console.log('File exceeds max size limit:', {
  540. fileSize: file.size,
  541. maxSize: ($config?.file?.max_size ?? 0) * 1024 * 1024
  542. });
  543. toast.error(
  544. $i18n.t(`File size should not exceed {{maxSize}} MB.`, {
  545. maxSize: $config?.file?.max_size
  546. })
  547. );
  548. return;
  549. }
  550. if (file['type'].startsWith('image/')) {
  551. if (visionCapableModels.length === 0) {
  552. toast.error($i18n.t('Selected model(s) do not support image inputs'));
  553. return;
  554. }
  555. const compressImageHandler = async (imageUrl, settings = {}, config = {}) => {
  556. // Quick shortcut so we don’t do unnecessary work.
  557. const settingsCompression = settings?.imageCompression ?? false;
  558. const configWidth = config?.file?.image_compression?.width ?? null;
  559. const configHeight = config?.file?.image_compression?.height ?? null;
  560. // If neither settings nor config wants compression, return original URL.
  561. if (!settingsCompression && !configWidth && !configHeight) {
  562. return imageUrl;
  563. }
  564. // Default to null (no compression unless set)
  565. let width = null;
  566. let height = null;
  567. // If user/settings want compression, pick their preferred size.
  568. if (settingsCompression) {
  569. width = settings?.imageCompressionSize?.width ?? null;
  570. height = settings?.imageCompressionSize?.height ?? null;
  571. }
  572. // Apply config limits as an upper bound if any
  573. if (configWidth && (width === null || width > configWidth)) {
  574. width = configWidth;
  575. }
  576. if (configHeight && (height === null || height > configHeight)) {
  577. height = configHeight;
  578. }
  579. // Do the compression if required
  580. if (width || height) {
  581. return await compressImage(imageUrl, width, height);
  582. }
  583. return imageUrl;
  584. };
  585. let reader = new FileReader();
  586. reader.onload = async (event) => {
  587. let imageUrl = event.target.result;
  588. imageUrl = await compressImageHandler(imageUrl, $settings, $config);
  589. files = [
  590. ...files,
  591. {
  592. type: 'image',
  593. url: `${imageUrl}`
  594. }
  595. ];
  596. };
  597. reader.readAsDataURL(file['type'] === 'image/heic' ? await convertHeicToJpeg(file) : file);
  598. } else {
  599. uploadFileHandler(file);
  600. }
  601. });
  602. };
  603. const onDragOver = (e) => {
  604. e.preventDefault();
  605. // Check if a file is being dragged.
  606. if (e.dataTransfer?.types?.includes('Files')) {
  607. dragged = true;
  608. } else {
  609. dragged = false;
  610. }
  611. };
  612. const onDragLeave = () => {
  613. dragged = false;
  614. };
  615. const onDrop = async (e) => {
  616. e.preventDefault();
  617. console.log(e);
  618. if (e.dataTransfer?.files) {
  619. const inputFiles = Array.from(e.dataTransfer?.files);
  620. if (inputFiles && inputFiles.length > 0) {
  621. console.log(inputFiles);
  622. inputFilesHandler(inputFiles);
  623. }
  624. }
  625. dragged = false;
  626. };
  627. const onKeyDown = (e) => {
  628. if (e.key === 'Shift') {
  629. shiftKey = true;
  630. }
  631. if (e.key === 'Escape') {
  632. console.log('Escape');
  633. dragged = false;
  634. }
  635. };
  636. const onKeyUp = (e) => {
  637. if (e.key === 'Shift') {
  638. shiftKey = false;
  639. }
  640. };
  641. const onFocus = () => {};
  642. const onBlur = () => {
  643. shiftKey = false;
  644. };
  645. onMount(async () => {
  646. suggestions = [
  647. {
  648. char: '@',
  649. render: getSuggestionRenderer(CommandSuggestionList, {
  650. i18n,
  651. onSelect: (e) => {
  652. const { type, data } = e;
  653. if (type === 'model') {
  654. atSelectedModel = data;
  655. }
  656. document.getElementById('chat-input')?.focus();
  657. },
  658. insertTextHandler: insertTextAtCursor,
  659. onUpload: (e) => {
  660. const { type, data } = e;
  661. if (type === 'file') {
  662. if (files.find((f) => f.id === data.id)) {
  663. return;
  664. }
  665. files = [
  666. ...files,
  667. {
  668. ...data,
  669. status: 'processed'
  670. }
  671. ];
  672. } else {
  673. dispatch('upload', e);
  674. }
  675. }
  676. })
  677. },
  678. {
  679. char: '/',
  680. render: getSuggestionRenderer(CommandSuggestionList, {
  681. i18n,
  682. onSelect: (e) => {
  683. const { type, data } = e;
  684. if (type === 'model') {
  685. atSelectedModel = data;
  686. }
  687. document.getElementById('chat-input')?.focus();
  688. },
  689. insertTextHandler: insertTextAtCursor,
  690. onUpload: (e) => {
  691. const { type, data } = e;
  692. if (type === 'file') {
  693. if (files.find((f) => f.id === data.id)) {
  694. return;
  695. }
  696. files = [
  697. ...files,
  698. {
  699. ...data,
  700. status: 'processed'
  701. }
  702. ];
  703. } else {
  704. dispatch('upload', e);
  705. }
  706. }
  707. })
  708. },
  709. {
  710. char: '#',
  711. render: getSuggestionRenderer(CommandSuggestionList, {
  712. i18n,
  713. onSelect: (e) => {
  714. const { type, data } = e;
  715. if (type === 'model') {
  716. atSelectedModel = data;
  717. }
  718. document.getElementById('chat-input')?.focus();
  719. },
  720. insertTextHandler: insertTextAtCursor,
  721. onUpload: (e) => {
  722. const { type, data } = e;
  723. if (type === 'file') {
  724. if (files.find((f) => f.id === data.id)) {
  725. return;
  726. }
  727. files = [
  728. ...files,
  729. {
  730. ...data,
  731. status: 'processed'
  732. }
  733. ];
  734. } else {
  735. dispatch('upload', e);
  736. }
  737. }
  738. })
  739. }
  740. ];
  741. console.log(suggestions);
  742. loaded = true;
  743. window.setTimeout(() => {
  744. const chatInput = document.getElementById('chat-input');
  745. chatInput?.focus();
  746. }, 0);
  747. window.addEventListener('keydown', onKeyDown);
  748. window.addEventListener('keyup', onKeyUp);
  749. window.addEventListener('focus', onFocus);
  750. window.addEventListener('blur', onBlur);
  751. await tick();
  752. const dropzoneElement = document.getElementById('chat-container');
  753. dropzoneElement?.addEventListener('dragover', onDragOver);
  754. dropzoneElement?.addEventListener('drop', onDrop);
  755. dropzoneElement?.addEventListener('dragleave', onDragLeave);
  756. await tools.set(await getTools(localStorage.token));
  757. });
  758. onDestroy(() => {
  759. console.log('destroy');
  760. window.removeEventListener('keydown', onKeyDown);
  761. window.removeEventListener('keyup', onKeyUp);
  762. window.removeEventListener('focus', onFocus);
  763. window.removeEventListener('blur', onBlur);
  764. const dropzoneElement = document.getElementById('chat-container');
  765. if (dropzoneElement) {
  766. dropzoneElement?.removeEventListener('dragover', onDragOver);
  767. dropzoneElement?.removeEventListener('drop', onDrop);
  768. dropzoneElement?.removeEventListener('dragleave', onDragLeave);
  769. }
  770. });
  771. </script>
  772. <FilesOverlay show={dragged} />
  773. <ToolServersModal bind:show={showTools} {selectedToolIds} />
  774. <InputVariablesModal
  775. bind:show={showInputVariablesModal}
  776. variables={inputVariables}
  777. onSave={inputVariablesModalCallback}
  778. />
  779. <ValvesModal
  780. bind:show={showValvesModal}
  781. userValves={true}
  782. type={selectedValvesType}
  783. id={selectedValvesItemId ?? null}
  784. on:save={async () => {
  785. await tick();
  786. }}
  787. />
  788. {#if loaded}
  789. <div class="w-full font-primary">
  790. <div class=" mx-auto inset-x-0 bg-transparent flex justify-center">
  791. <div
  792. class="flex flex-col px-3 {($settings?.widescreenMode ?? null)
  793. ? 'max-w-full'
  794. : 'max-w-6xl'} w-full"
  795. >
  796. <div class="relative">
  797. {#if autoScroll === false && history?.currentId}
  798. <div
  799. class=" absolute -top-12 left-0 right-0 flex justify-center z-30 pointer-events-none"
  800. >
  801. <button
  802. class=" bg-white border border-gray-100 dark:border-none dark:bg-white/20 p-1.5 rounded-full pointer-events-auto"
  803. on:click={() => {
  804. autoScroll = true;
  805. scrollToBottom();
  806. }}
  807. >
  808. <svg
  809. xmlns="http://www.w3.org/2000/svg"
  810. viewBox="0 0 20 20"
  811. fill="currentColor"
  812. class="w-5 h-5"
  813. >
  814. <path
  815. fill-rule="evenodd"
  816. 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"
  817. clip-rule="evenodd"
  818. />
  819. </svg>
  820. </button>
  821. </div>
  822. {/if}
  823. </div>
  824. </div>
  825. </div>
  826. <div class="bg-transparent">
  827. <div
  828. class="{($settings?.widescreenMode ?? null)
  829. ? 'max-w-full'
  830. : 'max-w-6xl'} px-2.5 mx-auto inset-x-0"
  831. >
  832. <div class="">
  833. <input
  834. bind:this={filesInputElement}
  835. bind:files={inputFiles}
  836. type="file"
  837. hidden
  838. multiple
  839. on:change={async () => {
  840. if (inputFiles && inputFiles.length > 0) {
  841. const _inputFiles = Array.from(inputFiles);
  842. inputFilesHandler(_inputFiles);
  843. } else {
  844. toast.error($i18n.t(`File not found.`));
  845. }
  846. filesInputElement.value = '';
  847. }}
  848. />
  849. {#if recording}
  850. <VoiceRecording
  851. bind:recording
  852. onCancel={async () => {
  853. recording = false;
  854. await tick();
  855. document.getElementById('chat-input')?.focus();
  856. }}
  857. onConfirm={async (data) => {
  858. const { text, filename } = data;
  859. recording = false;
  860. await tick();
  861. insertTextAtCursor(text);
  862. await tick();
  863. document.getElementById('chat-input')?.focus();
  864. if ($settings?.speechAutoSend ?? false) {
  865. dispatch('submit', prompt);
  866. }
  867. }}
  868. />
  869. {:else}
  870. <form
  871. class="w-full flex flex-col gap-1.5"
  872. on:submit|preventDefault={() => {
  873. // check if selectedModels support image input
  874. dispatch('submit', prompt);
  875. }}
  876. >
  877. <div
  878. id="message-input-container"
  879. class="flex-1 flex flex-col relative w-full shadow-lg rounded-3xl border {$temporaryChatEnabled
  880. ? 'border-dashed border-gray-100 dark:border-gray-800 hover:border-gray-200 focus-within:border-gray-200 hover:dark:border-gray-700 focus-within:dark:border-gray-700'
  881. : ' border-gray-100 dark:border-gray-850 hover:border-gray-200 focus-within:border-gray-100 hover:dark:border-gray-800 focus-within:dark:border-gray-800'} transition px-1 bg-white/5 dark:bg-gray-500/5 backdrop-blur-sm dark:text-gray-100"
  882. dir={$settings?.chatDirection ?? 'auto'}
  883. >
  884. {#if atSelectedModel !== undefined}
  885. <div class="px-3 pt-3 text-left w-full flex flex-col z-10">
  886. <div class="flex items-center justify-between w-full">
  887. <div class="pl-[1px] flex items-center gap-2 text-sm dark:text-gray-500">
  888. <img
  889. crossorigin="anonymous"
  890. alt="model profile"
  891. class="size-3.5 max-w-[28px] object-cover rounded-full"
  892. src={$models.find((model) => model.id === atSelectedModel.id)?.info?.meta
  893. ?.profile_image_url ??
  894. ($i18n.language === 'dg-DG'
  895. ? `${WEBUI_BASE_URL}/doge.png`
  896. : `${WEBUI_BASE_URL}/static/favicon.png`)}
  897. />
  898. <div class="translate-y-[0.5px]">
  899. <span class="">{atSelectedModel.name}</span>
  900. </div>
  901. </div>
  902. <div>
  903. <button
  904. class="flex items-center dark:text-gray-500"
  905. on:click={() => {
  906. atSelectedModel = undefined;
  907. }}
  908. >
  909. <XMark />
  910. </button>
  911. </div>
  912. </div>
  913. </div>
  914. {/if}
  915. {#if files.length > 0}
  916. <div class="mx-2 mt-2.5 pb-1.5 flex items-center flex-wrap gap-2">
  917. {#each files as file, fileIdx}
  918. {#if file.type === 'image'}
  919. <div class=" relative group">
  920. <div class="relative flex items-center">
  921. <Image
  922. src={file.url}
  923. alt=""
  924. imageClassName=" size-10 rounded-xl object-cover"
  925. />
  926. {#if atSelectedModel ? visionCapableModels.length === 0 : selectedModels.length !== visionCapableModels.length}
  927. <Tooltip
  928. className=" absolute top-1 left-1"
  929. content={$i18n.t('{{ models }}', {
  930. models: [
  931. ...(atSelectedModel ? [atSelectedModel] : selectedModels)
  932. ]
  933. .filter((id) => !visionCapableModels.includes(id))
  934. .join(', ')
  935. })}
  936. >
  937. <svg
  938. xmlns="http://www.w3.org/2000/svg"
  939. viewBox="0 0 24 24"
  940. fill="currentColor"
  941. aria-hidden="true"
  942. class="size-4 fill-yellow-300"
  943. >
  944. <path
  945. fill-rule="evenodd"
  946. 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"
  947. clip-rule="evenodd"
  948. />
  949. </svg>
  950. </Tooltip>
  951. {/if}
  952. </div>
  953. <div class=" absolute -top-1 -right-1">
  954. <button
  955. class=" bg-white text-black border border-white rounded-full {($settings?.highContrastMode ??
  956. false)
  957. ? ''
  958. : 'outline-hidden focus:outline-hidden group-hover:visible invisible transition'}"
  959. type="button"
  960. aria-label={$i18n.t('Remove file')}
  961. on:click={() => {
  962. files.splice(fileIdx, 1);
  963. files = files;
  964. }}
  965. >
  966. <svg
  967. xmlns="http://www.w3.org/2000/svg"
  968. viewBox="0 0 20 20"
  969. fill="currentColor"
  970. aria-hidden="true"
  971. class="size-4"
  972. >
  973. <path
  974. 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"
  975. />
  976. </svg>
  977. </button>
  978. </div>
  979. </div>
  980. {:else}
  981. <FileItem
  982. item={file}
  983. name={file.name}
  984. type={file.type}
  985. size={file?.size}
  986. loading={file.status === 'uploading'}
  987. dismissible={true}
  988. edit={true}
  989. small={true}
  990. modal={['file', 'collection'].includes(file?.type)}
  991. on:dismiss={async () => {
  992. // Remove from UI state
  993. files.splice(fileIdx, 1);
  994. files = files;
  995. }}
  996. on:click={() => {
  997. console.log(file);
  998. }}
  999. />
  1000. {/if}
  1001. {/each}
  1002. </div>
  1003. {/if}
  1004. <div class="px-2.5">
  1005. <div
  1006. class="scrollbar-hidden rtl:text-right ltr:text-left bg-transparent dark:text-gray-100 outline-hidden w-full pb-1 px-1 resize-none h-fit max-h-96 overflow-auto {files.length ===
  1007. 0
  1008. ? atSelectedModel !== undefined
  1009. ? 'pt-1.5'
  1010. : 'pt-2.5'
  1011. : ''}"
  1012. id="chat-input-container"
  1013. >
  1014. {#if suggestions}
  1015. {#key $settings?.richTextInput ?? true}
  1016. {#key $settings?.showFormattingToolbar ?? false}
  1017. <RichTextInput
  1018. bind:this={chatInputElement}
  1019. id="chat-input"
  1020. onChange={(e) => {
  1021. prompt = e.md;
  1022. command = getCommand();
  1023. }}
  1024. json={true}
  1025. richText={$settings?.richTextInput ?? true}
  1026. messageInput={true}
  1027. showFormattingToolbar={$settings?.showFormattingToolbar ?? false}
  1028. floatingMenuPlacement={'top-start'}
  1029. insertPromptAsRichText={$settings?.insertPromptAsRichText ?? false}
  1030. shiftEnter={!($settings?.ctrlEnterToSend ?? false) &&
  1031. !$mobile &&
  1032. !(
  1033. 'ontouchstart' in window ||
  1034. navigator.maxTouchPoints > 0 ||
  1035. navigator.msMaxTouchPoints > 0
  1036. )}
  1037. placeholder={placeholder ? placeholder : $i18n.t('Send a Message')}
  1038. largeTextAsFile={($settings?.largeTextAsFile ?? false) && !shiftKey}
  1039. autocomplete={$config?.features?.enable_autocomplete_generation &&
  1040. ($settings?.promptAutocomplete ?? false)}
  1041. generateAutoCompletion={async (text) => {
  1042. if (selectedModelIds.length === 0 || !selectedModelIds.at(0)) {
  1043. toast.error($i18n.t('Please select a model first.'));
  1044. }
  1045. const res = await generateAutoCompletion(
  1046. localStorage.token,
  1047. selectedModelIds.at(0),
  1048. text,
  1049. history?.currentId
  1050. ? createMessagesList(history, history.currentId)
  1051. : null
  1052. ).catch((error) => {
  1053. console.log(error);
  1054. return null;
  1055. });
  1056. console.log(res);
  1057. return res;
  1058. }}
  1059. {suggestions}
  1060. oncompositionstart={() => (isComposing = true)}
  1061. oncompositionend={(e) => {
  1062. compositionEndedAt = e.timeStamp;
  1063. isComposing = false;
  1064. }}
  1065. on:keydown={async (e) => {
  1066. e = e.detail.event;
  1067. const isCtrlPressed = e.ctrlKey || e.metaKey; // metaKey is for Cmd key on Mac
  1068. const suggestionsContainerElement =
  1069. document.getElementById('suggestions-container');
  1070. if (e.key === 'Escape') {
  1071. stopResponse();
  1072. }
  1073. // Command/Ctrl + Shift + Enter to submit a message pair
  1074. if (isCtrlPressed && e.key === 'Enter' && e.shiftKey) {
  1075. e.preventDefault();
  1076. createMessagePair(prompt);
  1077. }
  1078. // Check if Ctrl + R is pressed
  1079. if (prompt === '' && isCtrlPressed && e.key.toLowerCase() === 'r') {
  1080. e.preventDefault();
  1081. console.log('regenerate');
  1082. const regenerateButton = [
  1083. ...document.getElementsByClassName('regenerate-response-button')
  1084. ]?.at(-1);
  1085. regenerateButton?.click();
  1086. }
  1087. if (prompt === '' && e.key == 'ArrowUp') {
  1088. e.preventDefault();
  1089. const userMessageElement = [
  1090. ...document.getElementsByClassName('user-message')
  1091. ]?.at(-1);
  1092. if (userMessageElement) {
  1093. userMessageElement.scrollIntoView({ block: 'center' });
  1094. const editButton = [
  1095. ...document.getElementsByClassName('edit-user-message-button')
  1096. ]?.at(-1);
  1097. editButton?.click();
  1098. }
  1099. }
  1100. if (!suggestionsContainerElement) {
  1101. if (
  1102. !$mobile ||
  1103. !(
  1104. 'ontouchstart' in window ||
  1105. navigator.maxTouchPoints > 0 ||
  1106. navigator.msMaxTouchPoints > 0
  1107. )
  1108. ) {
  1109. if (inOrNearComposition(e)) {
  1110. return;
  1111. }
  1112. // Uses keyCode '13' for Enter key for chinese/japanese keyboards.
  1113. //
  1114. // Depending on the user's settings, it will send the message
  1115. // either when Enter is pressed or when Ctrl+Enter is pressed.
  1116. const enterPressed =
  1117. ($settings?.ctrlEnterToSend ?? false)
  1118. ? (e.key === 'Enter' || e.keyCode === 13) && isCtrlPressed
  1119. : (e.key === 'Enter' || e.keyCode === 13) && !e.shiftKey;
  1120. if (enterPressed) {
  1121. e.preventDefault();
  1122. if (prompt !== '' || files.length > 0) {
  1123. dispatch('submit', prompt);
  1124. }
  1125. }
  1126. }
  1127. }
  1128. if (e.key === 'Escape') {
  1129. console.log('Escape');
  1130. atSelectedModel = undefined;
  1131. selectedToolIds = [];
  1132. selectedFilterIds = [];
  1133. webSearchEnabled = false;
  1134. imageGenerationEnabled = false;
  1135. codeInterpreterEnabled = false;
  1136. }
  1137. }}
  1138. on:paste={async (e) => {
  1139. e = e.detail.event;
  1140. console.log(e);
  1141. const clipboardData = e.clipboardData || window.clipboardData;
  1142. if (clipboardData && clipboardData.items) {
  1143. for (const item of clipboardData.items) {
  1144. if (item.type.indexOf('image') !== -1) {
  1145. const blob = item.getAsFile();
  1146. const reader = new FileReader();
  1147. reader.onload = function (e) {
  1148. files = [
  1149. ...files,
  1150. {
  1151. type: 'image',
  1152. url: `${e.target.result}`
  1153. }
  1154. ];
  1155. };
  1156. reader.readAsDataURL(blob);
  1157. } else if (item?.kind === 'file') {
  1158. const file = item.getAsFile();
  1159. if (file) {
  1160. const _files = [file];
  1161. await inputFilesHandler(_files);
  1162. e.preventDefault();
  1163. }
  1164. } else if (item.type === 'text/plain') {
  1165. if (($settings?.largeTextAsFile ?? false) && !shiftKey) {
  1166. const text = clipboardData.getData('text/plain');
  1167. if (text.length > PASTED_TEXT_CHARACTER_LIMIT) {
  1168. e.preventDefault();
  1169. const blob = new Blob([text], { type: 'text/plain' });
  1170. const file = new File(
  1171. [blob],
  1172. `Pasted_Text_${Date.now()}.txt`,
  1173. {
  1174. type: 'text/plain'
  1175. }
  1176. );
  1177. await uploadFileHandler(file, true);
  1178. }
  1179. }
  1180. }
  1181. }
  1182. }
  1183. }}
  1184. />
  1185. {/key}
  1186. {/key}
  1187. {/if}
  1188. </div>
  1189. </div>
  1190. <div class=" flex justify-between mt-0.5 mb-2.5 mx-0.5 max-w-full" dir="ltr">
  1191. <div class="ml-1 self-end flex items-center flex-1 max-w-[80%]">
  1192. <InputMenu
  1193. bind:files
  1194. selectedModels={atSelectedModel ? [atSelectedModel.id] : selectedModels}
  1195. {fileUploadCapableModels}
  1196. {screenCaptureHandler}
  1197. {inputFilesHandler}
  1198. uploadFilesHandler={() => {
  1199. filesInputElement.click();
  1200. }}
  1201. uploadGoogleDriveHandler={async () => {
  1202. try {
  1203. const fileData = await createPicker();
  1204. if (fileData) {
  1205. const file = new File([fileData.blob], fileData.name, {
  1206. type: fileData.blob.type
  1207. });
  1208. await uploadFileHandler(file);
  1209. } else {
  1210. console.log('No file was selected from Google Drive');
  1211. }
  1212. } catch (error) {
  1213. console.error('Google Drive Error:', error);
  1214. toast.error(
  1215. $i18n.t('Error accessing Google Drive: {{error}}', {
  1216. error: error.message
  1217. })
  1218. );
  1219. }
  1220. }}
  1221. uploadOneDriveHandler={async (authorityType) => {
  1222. try {
  1223. const fileData = await pickAndDownloadFile(authorityType);
  1224. if (fileData) {
  1225. const file = new File([fileData.blob], fileData.name, {
  1226. type: fileData.blob.type || 'application/octet-stream'
  1227. });
  1228. await uploadFileHandler(file);
  1229. } else {
  1230. console.log('No file was selected from OneDrive');
  1231. }
  1232. } catch (error) {
  1233. console.error('OneDrive Error:', error);
  1234. }
  1235. }}
  1236. onUpload={async (e) => {
  1237. dispatch('upload', e);
  1238. }}
  1239. onClose={async () => {
  1240. await tick();
  1241. const chatInput = document.getElementById('chat-input');
  1242. chatInput?.focus();
  1243. }}
  1244. >
  1245. <div
  1246. id="input-menu-button"
  1247. class="bg-transparent hover:bg-gray-100 text-gray-700 dark:text-white dark:hover:bg-gray-800 rounded-full size-8 flex justify-center items-center outline-hidden focus:outline-hidden"
  1248. >
  1249. <PlusAlt className="size-5.5" />
  1250. </div>
  1251. </InputMenu>
  1252. <div
  1253. class="flex self-center w-[1px] h-4 mx-1 bg-gray-200/50 dark:bg-gray-800/50"
  1254. />
  1255. {#if showWebSearchButton || showImageGenerationButton || showCodeInterpreterButton || showToolsButton || (toggleFilters && toggleFilters.length > 0)}
  1256. <IntegrationsMenu
  1257. selectedModels={atSelectedModel ? [atSelectedModel.id] : selectedModels}
  1258. {toggleFilters}
  1259. {showWebSearchButton}
  1260. {showImageGenerationButton}
  1261. {showCodeInterpreterButton}
  1262. bind:selectedToolIds
  1263. bind:selectedFilterIds
  1264. bind:webSearchEnabled
  1265. bind:imageGenerationEnabled
  1266. bind:codeInterpreterEnabled
  1267. onShowValves={(e) => {
  1268. const { type, id } = e;
  1269. selectedValvesType = type;
  1270. selectedValvesItemId = id;
  1271. showValvesModal = true;
  1272. }}
  1273. onClose={async () => {
  1274. await tick();
  1275. const chatInput = document.getElementById('chat-input');
  1276. chatInput?.focus();
  1277. }}
  1278. >
  1279. <div
  1280. id="integration-menu-button"
  1281. class="bg-transparent hover:bg-gray-100 text-gray-700 dark:text-white dark:hover:bg-gray-800 rounded-full size-8 flex justify-center items-center outline-hidden focus:outline-hidden"
  1282. >
  1283. <Component className="size-4.5" strokeWidth="1.5" />
  1284. </div>
  1285. </IntegrationsMenu>
  1286. {/if}
  1287. {#if selectedModelIds.length === 1 && $models.find((m) => m.id === selectedModelIds[0])?.has_user_valves}
  1288. <div class="ml-1 flex gap-1.5">
  1289. <Tooltip content={$i18n.t('Valves')} placement="top">
  1290. <button
  1291. id="model-valves-button"
  1292. class="bg-transparent hover:bg-gray-100 text-gray-700 dark:text-white dark:hover:bg-gray-800 rounded-full size-8 flex justify-center items-center outline-hidden focus:outline-hidden"
  1293. on:click={() => {
  1294. selectedValvesType = 'function';
  1295. selectedValvesItemId = selectedModelIds[0]?.split('.')[0];
  1296. showValvesModal = true;
  1297. }}
  1298. >
  1299. <Knobs className="size-4" strokeWidth="1.5" />
  1300. </button>
  1301. </Tooltip>
  1302. </div>
  1303. {/if}
  1304. <div class="ml-1 flex gap-1.5">
  1305. {#if (selectedToolIds ?? []).length > 0}
  1306. <Tooltip
  1307. content={$i18n.t('{{COUNT}} Available Tools', {
  1308. COUNT: selectedToolIds.length
  1309. })}
  1310. >
  1311. <button
  1312. class="translate-y-[0.5px] px-1 flex gap-1 items-center text-gray-600 dark:text-gray-300 hover:text-gray-700 dark:hover:text-gray-200 rounded-lg self-center transition"
  1313. aria-label="Available Tools"
  1314. type="button"
  1315. on:click={() => {
  1316. showTools = !showTools;
  1317. }}
  1318. >
  1319. <Wrench className="size-4" strokeWidth="1.75" />
  1320. <span class="text-sm">
  1321. {selectedToolIds.length}
  1322. </span>
  1323. </button>
  1324. </Tooltip>
  1325. {/if}
  1326. {#each selectedFilterIds as filterId}
  1327. {@const filter = toggleFilters.find((f) => f.id === filterId)}
  1328. {#if filter}
  1329. <Tooltip content={filter?.name} placement="top">
  1330. <button
  1331. on:click|preventDefault={() => {
  1332. selectedFilterIds = selectedFilterIds.filter(
  1333. (id) => id !== filterId
  1334. );
  1335. }}
  1336. type="button"
  1337. class="group p-[7px] flex gap-1.5 items-center text-sm rounded-full transition-colors duration-300 focus:outline-hidden max-w-full overflow-hidden {selectedFilterIds.includes(
  1338. filterId
  1339. )
  1340. ? 'text-sky-500 dark:text-sky-300 bg-sky-50 hover:bg-sky-100 dark:bg-sky-400/10 dark:hover:bg-sky-600/10 border border-sky-200/40 dark:border-sky-500/20'
  1341. : 'bg-transparent text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-800 '} capitalize"
  1342. >
  1343. {#if filter?.icon}
  1344. <div class="size-4 items-center flex justify-center">
  1345. <img
  1346. src={filter.icon}
  1347. class="size-3.5 {filter.icon.includes('svg')
  1348. ? 'dark:invert-[80%]'
  1349. : ''}"
  1350. style="fill: currentColor;"
  1351. alt={filter.name}
  1352. />
  1353. </div>
  1354. {:else}
  1355. <Sparkles className="size-4" strokeWidth="1.75" />
  1356. {/if}
  1357. <div class="hidden group-hover:block">
  1358. <XMark className="size-4" strokeWidth="1.75" />
  1359. </div>
  1360. </button>
  1361. </Tooltip>
  1362. {/if}
  1363. {/each}
  1364. {#if webSearchEnabled}
  1365. <Tooltip content={$i18n.t('Web Search')} placement="top">
  1366. <button
  1367. on:click|preventDefault={() => (webSearchEnabled = !webSearchEnabled)}
  1368. type="button"
  1369. class="group p-[7px] flex gap-1.5 items-center text-sm rounded-full transition-colors duration-300 focus:outline-hidden max-w-full overflow-hidden {webSearchEnabled ||
  1370. ($settings?.webSearch ?? false) === 'always'
  1371. ? ' text-sky-500 dark:text-sky-300 bg-sky-50 hover:bg-sky-100 dark:bg-sky-400/10 dark:hover:bg-sky-600/10 border border-sky-200/40 dark:border-sky-500/20'
  1372. : 'bg-transparent text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-800 '}"
  1373. >
  1374. <GlobeAlt className="size-4" strokeWidth="1.75" />
  1375. <div class="hidden group-hover:block">
  1376. <XMark className="size-4" strokeWidth="1.75" />
  1377. </div>
  1378. </button>
  1379. </Tooltip>
  1380. {/if}
  1381. {#if imageGenerationEnabled}
  1382. <Tooltip content={$i18n.t('Image')} placement="top">
  1383. <button
  1384. on:click|preventDefault={() =>
  1385. (imageGenerationEnabled = !imageGenerationEnabled)}
  1386. type="button"
  1387. class="group p-[7px] flex gap-1.5 items-center text-sm rounded-full transition-colors duration-300 focus:outline-hidden max-w-full overflow-hidden {imageGenerationEnabled
  1388. ? ' text-sky-500 dark:text-sky-300 bg-sky-50 hover:bg-sky-100 dark:bg-sky-400/10 dark:hover:bg-sky-700/10 border border-sky-200/40 dark:border-sky-500/20'
  1389. : 'bg-transparent text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-800 '}"
  1390. >
  1391. <Photo className="size-4" strokeWidth="1.75" />
  1392. <div class="hidden group-hover:block">
  1393. <XMark className="size-4" strokeWidth="1.75" />
  1394. </div>
  1395. </button>
  1396. </Tooltip>
  1397. {/if}
  1398. {#if codeInterpreterEnabled}
  1399. <Tooltip content={$i18n.t('Code Interpreter')} placement="top">
  1400. <button
  1401. aria-label={codeInterpreterEnabled
  1402. ? $i18n.t('Disable Code Interpreter')
  1403. : $i18n.t('Enable Code Interpreter')}
  1404. aria-pressed={codeInterpreterEnabled}
  1405. on:click|preventDefault={() =>
  1406. (codeInterpreterEnabled = !codeInterpreterEnabled)}
  1407. type="button"
  1408. class=" group p-[7px] flex gap-1.5 items-center text-sm transition-colors duration-300 max-w-full overflow-hidden {codeInterpreterEnabled
  1409. ? ' text-sky-500 dark:text-sky-300 bg-sky-50 hover:bg-sky-100 dark:bg-sky-400/10 dark:hover:bg-sky-700/10 border border-sky-200/40 dark:border-sky-500/20'
  1410. : 'bg-transparent text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-800 '} {($settings?.highContrastMode ??
  1411. false)
  1412. ? 'm-1'
  1413. : 'focus:outline-hidden rounded-full'}"
  1414. >
  1415. <Terminal className="size-3.5" strokeWidth="2" />
  1416. <div class="hidden group-hover:block">
  1417. <XMark className="size-4" strokeWidth="1.75" />
  1418. </div>
  1419. </button>
  1420. </Tooltip>
  1421. {/if}
  1422. </div>
  1423. </div>
  1424. <div class="self-end flex space-x-1 mr-1 shrink-0">
  1425. {#if (!history?.currentId || history.messages[history.currentId]?.done == true) && ($_user?.role === 'admin' || ($_user?.permissions?.chat?.stt ?? true))}
  1426. <!-- {$i18n.t('Record voice')} -->
  1427. <Tooltip content={$i18n.t('Dictate')}>
  1428. <button
  1429. id="voice-input-button"
  1430. 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"
  1431. type="button"
  1432. on:click={async () => {
  1433. try {
  1434. let stream = await navigator.mediaDevices
  1435. .getUserMedia({ audio: true })
  1436. .catch(function (err) {
  1437. toast.error(
  1438. $i18n.t(
  1439. `Permission denied when accessing microphone: {{error}}`,
  1440. {
  1441. error: err
  1442. }
  1443. )
  1444. );
  1445. return null;
  1446. });
  1447. if (stream) {
  1448. recording = true;
  1449. const tracks = stream.getTracks();
  1450. tracks.forEach((track) => track.stop());
  1451. }
  1452. stream = null;
  1453. } catch {
  1454. toast.error($i18n.t('Permission denied when accessing microphone'));
  1455. }
  1456. }}
  1457. aria-label="Voice Input"
  1458. >
  1459. <svg
  1460. xmlns="http://www.w3.org/2000/svg"
  1461. viewBox="0 0 20 20"
  1462. fill="currentColor"
  1463. class="w-5 h-5 translate-y-[0.5px]"
  1464. >
  1465. <path d="M7 4a3 3 0 016 0v6a3 3 0 11-6 0V4z" />
  1466. <path
  1467. 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"
  1468. />
  1469. </svg>
  1470. </button>
  1471. </Tooltip>
  1472. {/if}
  1473. {#if (taskIds && taskIds.length > 0) || (history.currentId && history.messages[history.currentId]?.done != true) || generating}
  1474. <div class=" flex items-center">
  1475. <Tooltip content={$i18n.t('Stop')}>
  1476. <button
  1477. 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"
  1478. on:click={() => {
  1479. stopResponse();
  1480. }}
  1481. >
  1482. <svg
  1483. xmlns="http://www.w3.org/2000/svg"
  1484. viewBox="0 0 24 24"
  1485. fill="currentColor"
  1486. class="size-5"
  1487. >
  1488. <path
  1489. fill-rule="evenodd"
  1490. 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"
  1491. clip-rule="evenodd"
  1492. />
  1493. </svg>
  1494. </button>
  1495. </Tooltip>
  1496. </div>
  1497. {:else if prompt === '' && files.length === 0 && ($_user?.role === 'admin' || ($_user?.permissions?.chat?.call ?? true))}
  1498. <div class=" flex items-center">
  1499. <!-- {$i18n.t('Call')} -->
  1500. <Tooltip content={$i18n.t('Voice mode')}>
  1501. <button
  1502. class=" bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full p-1.5 self-center"
  1503. type="button"
  1504. on:click={async () => {
  1505. if (selectedModels.length > 1) {
  1506. toast.error($i18n.t('Select only one model to call'));
  1507. return;
  1508. }
  1509. if ($config.audio.stt.engine === 'web') {
  1510. toast.error(
  1511. $i18n.t('Call feature is not supported when using Web STT engine')
  1512. );
  1513. return;
  1514. }
  1515. // check if user has access to getUserMedia
  1516. try {
  1517. let stream = await navigator.mediaDevices.getUserMedia({
  1518. audio: true
  1519. });
  1520. // If the user grants the permission, proceed to show the call overlay
  1521. if (stream) {
  1522. const tracks = stream.getTracks();
  1523. tracks.forEach((track) => track.stop());
  1524. }
  1525. stream = null;
  1526. if ($settings.audio?.tts?.engine === 'browser-kokoro') {
  1527. // If the user has not initialized the TTS worker, initialize it
  1528. if (!$TTSWorker) {
  1529. await TTSWorker.set(
  1530. new KokoroWorker({
  1531. dtype: $settings.audio?.tts?.engineConfig?.dtype ?? 'fp32'
  1532. })
  1533. );
  1534. await $TTSWorker.init();
  1535. }
  1536. }
  1537. showCallOverlay.set(true);
  1538. showControls.set(true);
  1539. } catch (err) {
  1540. // If the user denies the permission or an error occurs, show an error message
  1541. toast.error(
  1542. $i18n.t('Permission denied when accessing media devices')
  1543. );
  1544. }
  1545. }}
  1546. aria-label={$i18n.t('Voice mode')}
  1547. >
  1548. <Voice className="size-5" strokeWidth="2.5" />
  1549. </button>
  1550. </Tooltip>
  1551. </div>
  1552. {:else}
  1553. <div class=" flex items-center">
  1554. <Tooltip content={$i18n.t('Send message')}>
  1555. <button
  1556. id="send-message-button"
  1557. class="{!(prompt === '' && files.length === 0)
  1558. ? 'bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 '
  1559. : 'text-white bg-gray-200 dark:text-gray-900 dark:bg-gray-700 disabled'} transition rounded-full p-1.5 self-center"
  1560. type="submit"
  1561. disabled={prompt === '' && files.length === 0}
  1562. >
  1563. <svg
  1564. xmlns="http://www.w3.org/2000/svg"
  1565. viewBox="0 0 16 16"
  1566. fill="currentColor"
  1567. class="size-5"
  1568. >
  1569. <path
  1570. fill-rule="evenodd"
  1571. 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"
  1572. clip-rule="evenodd"
  1573. />
  1574. </svg>
  1575. </button>
  1576. </Tooltip>
  1577. </div>
  1578. {/if}
  1579. </div>
  1580. </div>
  1581. </div>
  1582. {#if $config?.license_metadata?.input_footer}
  1583. <div class=" text-xs text-gray-500 text-center line-clamp-1 marked">
  1584. {@html DOMPurify.sanitize(marked($config?.license_metadata?.input_footer))}
  1585. </div>
  1586. {:else}
  1587. <div class="mb-1" />
  1588. {/if}
  1589. </form>
  1590. {/if}
  1591. </div>
  1592. </div>
  1593. </div>
  1594. </div>
  1595. {/if}