MessageInput.svelte 68 KB

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