ResponseMessage.svelte 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import dayjs from 'dayjs';
  4. import { createEventDispatcher } from 'svelte';
  5. import { onMount, tick, getContext } from 'svelte';
  6. const i18n = getContext<Writable<i18nType>>('i18n');
  7. const dispatch = createEventDispatcher();
  8. import { config, models, settings, user } from '$lib/stores';
  9. import { synthesizeOpenAISpeech } from '$lib/apis/audio';
  10. import { imageGenerations } from '$lib/apis/images';
  11. import {
  12. copyToClipboard as _copyToClipboard,
  13. approximateToHumanReadable,
  14. getMessageContentParts,
  15. sanitizeResponseContent,
  16. createMessagesList
  17. } from '$lib/utils';
  18. import { WEBUI_BASE_URL } from '$lib/constants';
  19. import Name from './Name.svelte';
  20. import ProfileImage from './ProfileImage.svelte';
  21. import Skeleton from './Skeleton.svelte';
  22. import Image from '$lib/components/common/Image.svelte';
  23. import Tooltip from '$lib/components/common/Tooltip.svelte';
  24. import RateComment from './RateComment.svelte';
  25. import Spinner from '$lib/components/common/Spinner.svelte';
  26. import WebSearchResults from './ResponseMessage/WebSearchResults.svelte';
  27. import Sparkles from '$lib/components/icons/Sparkles.svelte';
  28. import Error from './Error.svelte';
  29. import Citations from './Citations.svelte';
  30. import CodeExecutions from './CodeExecutions.svelte';
  31. import type { Writable } from 'svelte/store';
  32. import type { i18n as i18nType } from 'i18next';
  33. import ContentRenderer from './ContentRenderer.svelte';
  34. import { createNewFeedback, getFeedbackById, updateFeedbackById } from '$lib/apis/evaluations';
  35. import { getChatById } from '$lib/apis/chats';
  36. import { generateTags } from '$lib/apis';
  37. interface MessageType {
  38. id: string;
  39. model: string;
  40. content: string;
  41. files?: { type: string; url: string }[];
  42. timestamp: number;
  43. role: string;
  44. statusHistory?: {
  45. done: boolean;
  46. action: string;
  47. description: string;
  48. urls?: string[];
  49. query?: string;
  50. }[];
  51. status?: {
  52. done: boolean;
  53. action: string;
  54. description: string;
  55. urls?: string[];
  56. query?: string;
  57. };
  58. done: boolean;
  59. error?: boolean | { content: string };
  60. sources?: string[];
  61. code_executions?: {
  62. uuid: string;
  63. name: string;
  64. code: string;
  65. language?: string;
  66. result?: {
  67. error?: string;
  68. output?: string;
  69. files?: { name: string; url: string }[];
  70. };
  71. }[];
  72. info?: {
  73. openai?: boolean;
  74. prompt_tokens?: number;
  75. completion_tokens?: number;
  76. total_tokens?: number;
  77. eval_count?: number;
  78. eval_duration?: number;
  79. prompt_eval_count?: number;
  80. prompt_eval_duration?: number;
  81. total_duration?: number;
  82. load_duration?: number;
  83. usage?: unknown;
  84. };
  85. annotation?: { type: string; rating: number };
  86. }
  87. export let chatId = '';
  88. export let history;
  89. export let messageId;
  90. let message: MessageType = JSON.parse(JSON.stringify(history.messages[messageId]));
  91. $: if (history.messages) {
  92. if (JSON.stringify(message) !== JSON.stringify(history.messages[messageId])) {
  93. message = JSON.parse(JSON.stringify(history.messages[messageId]));
  94. }
  95. }
  96. export let siblings;
  97. export let showPreviousMessage: Function;
  98. export let showNextMessage: Function;
  99. export let updateChat: Function;
  100. export let editMessage: Function;
  101. export let saveMessage: Function;
  102. export let rateMessage: Function;
  103. export let actionMessage: Function;
  104. export let submitMessage: Function;
  105. export let continueResponse: Function;
  106. export let regenerateResponse: Function;
  107. export let addMessages: Function;
  108. export let isLastMessage = true;
  109. export let readOnly = false;
  110. let model = null;
  111. $: model = $models.find((m) => m.id === message.model);
  112. let edit = false;
  113. let editedContent = '';
  114. let editTextAreaElement: HTMLTextAreaElement;
  115. let audioParts: Record<number, HTMLAudioElement | null> = {};
  116. let speaking = false;
  117. let speakingIdx: number | undefined;
  118. let loadingSpeech = false;
  119. let generatingImage = false;
  120. let showRateComment = false;
  121. const copyToClipboard = async (text) => {
  122. const res = await _copyToClipboard(text);
  123. if (res) {
  124. toast.success($i18n.t('Copying to clipboard was successful!'));
  125. }
  126. };
  127. const playAudio = (idx: number) => {
  128. return new Promise<void>((res) => {
  129. speakingIdx = idx;
  130. const audio = audioParts[idx];
  131. if (!audio) {
  132. return res();
  133. }
  134. audio.play();
  135. audio.onended = async () => {
  136. await new Promise((r) => setTimeout(r, 300));
  137. if (Object.keys(audioParts).length - 1 === idx) {
  138. speaking = false;
  139. }
  140. res();
  141. };
  142. });
  143. };
  144. const toggleSpeakMessage = async () => {
  145. if (speaking) {
  146. try {
  147. speechSynthesis.cancel();
  148. if (speakingIdx !== undefined && audioParts[speakingIdx]) {
  149. audioParts[speakingIdx]!.pause();
  150. audioParts[speakingIdx]!.currentTime = 0;
  151. }
  152. } catch {}
  153. speaking = false;
  154. speakingIdx = undefined;
  155. return;
  156. }
  157. if (!(message?.content ?? '').trim().length) {
  158. toast.info($i18n.t('No content to speak'));
  159. return;
  160. }
  161. speaking = true;
  162. if ($config.audio.tts.engine !== '') {
  163. loadingSpeech = true;
  164. const messageContentParts: string[] = getMessageContentParts(
  165. message.content,
  166. $config?.audio?.tts?.split_on ?? 'punctuation'
  167. );
  168. if (!messageContentParts.length) {
  169. console.log('No content to speak');
  170. toast.info($i18n.t('No content to speak'));
  171. speaking = false;
  172. loadingSpeech = false;
  173. return;
  174. }
  175. console.debug('Prepared message content for TTS', messageContentParts);
  176. audioParts = messageContentParts.reduce(
  177. (acc, _sentence, idx) => {
  178. acc[idx] = null;
  179. return acc;
  180. },
  181. {} as typeof audioParts
  182. );
  183. let lastPlayedAudioPromise = Promise.resolve(); // Initialize a promise that resolves immediately
  184. for (const [idx, sentence] of messageContentParts.entries()) {
  185. const res = await synthesizeOpenAISpeech(
  186. localStorage.token,
  187. $settings?.audio?.tts?.defaultVoice === $config.audio.tts.voice
  188. ? ($settings?.audio?.tts?.voice ?? $config?.audio?.tts?.voice)
  189. : $config?.audio?.tts?.voice,
  190. sentence
  191. ).catch((error) => {
  192. console.error(error);
  193. toast.error(error);
  194. speaking = false;
  195. loadingSpeech = false;
  196. });
  197. if (res) {
  198. const blob = await res.blob();
  199. const blobUrl = URL.createObjectURL(blob);
  200. const audio = new Audio(blobUrl);
  201. audio.playbackRate = $settings.audio?.tts?.playbackRate ?? 1;
  202. audioParts[idx] = audio;
  203. loadingSpeech = false;
  204. lastPlayedAudioPromise = lastPlayedAudioPromise.then(() => playAudio(idx));
  205. }
  206. }
  207. } else {
  208. let voices = [];
  209. const getVoicesLoop = setInterval(() => {
  210. voices = speechSynthesis.getVoices();
  211. if (voices.length > 0) {
  212. clearInterval(getVoicesLoop);
  213. const voice =
  214. voices
  215. ?.filter(
  216. (v) => v.voiceURI === ($settings?.audio?.tts?.voice ?? $config?.audio?.tts?.voice)
  217. )
  218. ?.at(0) ?? undefined;
  219. console.log(voice);
  220. const speak = new SpeechSynthesisUtterance(message.content);
  221. speak.rate = $settings.audio?.tts?.playbackRate ?? 1;
  222. console.log(speak);
  223. speak.onend = () => {
  224. speaking = false;
  225. if ($settings.conversationMode) {
  226. document.getElementById('voice-input-button')?.click();
  227. }
  228. };
  229. if (voice) {
  230. speak.voice = voice;
  231. }
  232. speechSynthesis.speak(speak);
  233. }
  234. }, 100);
  235. }
  236. };
  237. const editMessageHandler = async () => {
  238. edit = true;
  239. editedContent = message.content;
  240. await tick();
  241. editTextAreaElement.style.height = '';
  242. editTextAreaElement.style.height = `${editTextAreaElement.scrollHeight}px`;
  243. };
  244. const editMessageConfirmHandler = async () => {
  245. editMessage(message.id, editedContent ? editedContent : '', false);
  246. edit = false;
  247. editedContent = '';
  248. await tick();
  249. };
  250. const saveAsCopyHandler = async () => {
  251. editMessage(message.id, editedContent ? editedContent : '');
  252. edit = false;
  253. editedContent = '';
  254. await tick();
  255. };
  256. const cancelEditMessage = async () => {
  257. edit = false;
  258. editedContent = '';
  259. await tick();
  260. };
  261. const generateImage = async (message: MessageType) => {
  262. generatingImage = true;
  263. const res = await imageGenerations(localStorage.token, message.content).catch((error) => {
  264. toast.error(error);
  265. });
  266. console.log(res);
  267. if (res) {
  268. const files = res.map((image) => ({
  269. type: 'image',
  270. url: `${image.url}`
  271. }));
  272. saveMessage(message.id, {
  273. ...message,
  274. files: files
  275. });
  276. }
  277. generatingImage = false;
  278. };
  279. let feedbackLoading = false;
  280. const feedbackHandler = async (rating: number | null = null, details: object | null = null) => {
  281. feedbackLoading = true;
  282. console.log('Feedback', rating, details);
  283. const updatedMessage = {
  284. ...message,
  285. annotation: {
  286. ...(message?.annotation ?? {}),
  287. ...(rating !== null ? { rating: rating } : {}),
  288. ...(details ? details : {})
  289. }
  290. };
  291. const chat = await getChatById(localStorage.token, chatId).catch((error) => {
  292. toast.error(error);
  293. });
  294. if (!chat) {
  295. return;
  296. }
  297. const messages = createMessagesList(history, message.id);
  298. let feedbackItem = {
  299. type: 'rating',
  300. data: {
  301. ...(updatedMessage?.annotation ? updatedMessage.annotation : {}),
  302. model_id: message?.selectedModelId ?? message.model,
  303. ...(history.messages[message.parentId].childrenIds.length > 1
  304. ? {
  305. sibling_model_ids: history.messages[message.parentId].childrenIds
  306. .filter((id) => id !== message.id)
  307. .map((id) => history.messages[id]?.selectedModelId ?? history.messages[id].model)
  308. }
  309. : {})
  310. },
  311. meta: {
  312. arena: message ? message.arena : false,
  313. model_id: message.model,
  314. message_id: message.id,
  315. message_index: messages.length,
  316. chat_id: chatId
  317. },
  318. snapshot: {
  319. chat: chat
  320. }
  321. };
  322. const baseModels = [
  323. feedbackItem.data.model_id,
  324. ...(feedbackItem.data.sibling_model_ids ?? [])
  325. ].reduce((acc, modelId) => {
  326. const model = $models.find((m) => m.id === modelId);
  327. if (model) {
  328. acc[model.id] = model?.info?.base_model_id ?? null;
  329. } else {
  330. // Log or handle cases where corresponding model is not found
  331. console.warn(`Model with ID ${modelId} not found`);
  332. }
  333. return acc;
  334. }, {});
  335. feedbackItem.meta.base_models = baseModels;
  336. let feedback = null;
  337. if (message?.feedbackId) {
  338. feedback = await updateFeedbackById(
  339. localStorage.token,
  340. message.feedbackId,
  341. feedbackItem
  342. ).catch((error) => {
  343. toast.error(error);
  344. });
  345. } else {
  346. feedback = await createNewFeedback(localStorage.token, feedbackItem).catch((error) => {
  347. toast.error(error);
  348. });
  349. if (feedback) {
  350. updatedMessage.feedbackId = feedback.id;
  351. }
  352. }
  353. console.log(updatedMessage);
  354. saveMessage(message.id, updatedMessage);
  355. await tick();
  356. if (!details) {
  357. showRateComment = true;
  358. if (!updatedMessage.annotation?.tags) {
  359. // attempt to generate tags
  360. const tags = await generateTags(localStorage.token, message.model, messages, chatId).catch(
  361. (error) => {
  362. console.error(error);
  363. return [];
  364. }
  365. );
  366. console.log(tags);
  367. if (tags) {
  368. updatedMessage.annotation.tags = tags;
  369. feedbackItem.data.tags = tags;
  370. saveMessage(message.id, updatedMessage);
  371. await updateFeedbackById(
  372. localStorage.token,
  373. updatedMessage.feedbackId,
  374. feedbackItem
  375. ).catch((error) => {
  376. toast.error(error);
  377. });
  378. }
  379. }
  380. }
  381. feedbackLoading = false;
  382. };
  383. $: if (!edit) {
  384. (async () => {
  385. await tick();
  386. })();
  387. }
  388. onMount(async () => {
  389. // console.log('ResponseMessage mounted');
  390. await tick();
  391. });
  392. </script>
  393. {#key message.id}
  394. <div
  395. class=" flex w-full message-{message.id}"
  396. id="message-{message.id}"
  397. dir={$settings.chatDirection}
  398. >
  399. <div class={`flex-shrink-0 ${($settings?.chatDirection ?? 'LTR') === 'LTR' ? 'mr-3' : 'ml-3'}`}>
  400. <ProfileImage
  401. src={model?.info?.meta?.profile_image_url ??
  402. ($i18n.language === 'dg-DG' ? `/doge.png` : `${WEBUI_BASE_URL}/static/favicon.png`)}
  403. className={'size-8'}
  404. />
  405. </div>
  406. <div class="flex-auto w-0 pl-1">
  407. <Name>
  408. {model?.name ?? message.model}
  409. {#if message.timestamp}
  410. <span
  411. class=" self-center invisible group-hover:visible text-gray-400 text-xs font-medium uppercase ml-0.5 -mt-0.5"
  412. >
  413. {dayjs(message.timestamp * 1000).format($i18n.t('h:mm a'))}
  414. </span>
  415. {/if}
  416. </Name>
  417. <div>
  418. {#if message?.files && message.files?.filter((f) => f.type === 'image').length > 0}
  419. <div class="my-2.5 w-full flex overflow-x-auto gap-2 flex-wrap">
  420. {#each message.files as file}
  421. <div>
  422. {#if file.type === 'image'}
  423. <Image src={file.url} alt={message.content} />
  424. {/if}
  425. </div>
  426. {/each}
  427. </div>
  428. {/if}
  429. <div class="chat-{message.role} w-full min-w-full markdown-prose">
  430. <div>
  431. {#if (message?.statusHistory ?? [...(message?.status ? [message?.status] : [])]).length > 0}
  432. {@const status = (
  433. message?.statusHistory ?? [...(message?.status ? [message?.status] : [])]
  434. ).at(-1)}
  435. {#if !status?.hidden}
  436. <div class="status-description flex items-center gap-2 py-0.5">
  437. {#if status?.done === false}
  438. <div class="">
  439. <Spinner className="size-4" />
  440. </div>
  441. {/if}
  442. {#if status?.action === 'web_search' && status?.urls}
  443. <WebSearchResults {status}>
  444. <div class="flex flex-col justify-center -space-y-0.5">
  445. <div
  446. class="{status?.done === false
  447. ? 'shimmer'
  448. : ''} text-base line-clamp-1 text-wrap"
  449. >
  450. <!-- $i18n.t("Generating search query") -->
  451. <!-- $i18n.t("No search query generated") -->
  452. <!-- $i18n.t('Searched {{count}} sites') -->
  453. {#if status?.description.includes('{{count}}')}
  454. {$i18n.t(status?.description, {
  455. count: status?.urls.length
  456. })}
  457. {:else if status?.description === 'No search query generated'}
  458. {$i18n.t('No search query generated')}
  459. {:else if status?.description === 'Generating search query'}
  460. {$i18n.t('Generating search query')}
  461. {:else}
  462. {status?.description}
  463. {/if}
  464. </div>
  465. </div>
  466. </WebSearchResults>
  467. {:else if status?.action === 'knowledge_search'}
  468. <div class="flex flex-col justify-center -space-y-0.5">
  469. <div
  470. class="{status?.done === false
  471. ? 'shimmer'
  472. : ''} text-gray-500 dark:text-gray-500 text-base line-clamp-1 text-wrap"
  473. >
  474. {$i18n.t(`Searching Knowledge for "{{searchQuery}}"`, {
  475. searchQuery: status.query
  476. })}
  477. </div>
  478. </div>
  479. {:else}
  480. <div class="flex flex-col justify-center -space-y-0.5">
  481. <div
  482. class="{status?.done === false
  483. ? 'shimmer'
  484. : ''} text-gray-500 dark:text-gray-500 text-base line-clamp-1 text-wrap"
  485. >
  486. <!-- $i18n.t(`Searching "{{searchQuery}}"`) -->
  487. {#if status?.description.includes('{{searchQuery}}')}
  488. {$i18n.t(status?.description, {
  489. searchQuery: status?.query
  490. })}
  491. {:else if status?.description === 'No search query generated'}
  492. {$i18n.t('No search query generated')}
  493. {:else if status?.description === 'Generating search query'}
  494. {$i18n.t('Generating search query')}
  495. {:else}
  496. {status?.description}
  497. {/if}
  498. </div>
  499. </div>
  500. {/if}
  501. </div>
  502. {/if}
  503. {/if}
  504. {#if edit === true}
  505. <div class="w-full bg-gray-50 dark:bg-gray-800 rounded-3xl px-5 py-3 my-2">
  506. <textarea
  507. id="message-edit-{message.id}"
  508. bind:this={editTextAreaElement}
  509. class=" bg-transparent outline-none w-full resize-none"
  510. bind:value={editedContent}
  511. on:input={(e) => {
  512. e.target.style.height = '';
  513. e.target.style.height = `${e.target.scrollHeight}px`;
  514. }}
  515. on:keydown={(e) => {
  516. if (e.key === 'Escape') {
  517. document.getElementById('close-edit-message-button')?.click();
  518. }
  519. const isCmdOrCtrlPressed = e.metaKey || e.ctrlKey;
  520. const isEnterPressed = e.key === 'Enter';
  521. if (isCmdOrCtrlPressed && isEnterPressed) {
  522. document.getElementById('confirm-edit-message-button')?.click();
  523. }
  524. }}
  525. />
  526. <div class=" mt-2 mb-1 flex justify-between text-sm font-medium">
  527. <div>
  528. <button
  529. id="save-new-message-button"
  530. class=" px-4 py-2 bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 border dark:border-gray-700 text-gray-700 dark:text-gray-200 transition rounded-3xl"
  531. on:click={() => {
  532. saveAsCopyHandler();
  533. }}
  534. >
  535. {$i18n.t('Save As Copy')}
  536. </button>
  537. </div>
  538. <div class="flex space-x-1.5">
  539. <button
  540. id="close-edit-message-button"
  541. class="px-4 py-2 bg-white dark:bg-gray-900 hover:bg-gray-100 text-gray-800 dark:text-gray-100 transition rounded-3xl"
  542. on:click={() => {
  543. cancelEditMessage();
  544. }}
  545. >
  546. {$i18n.t('Cancel')}
  547. </button>
  548. <button
  549. id="confirm-edit-message-button"
  550. class=" px-4 py-2 bg-gray-900 dark:bg-white hover:bg-gray-850 text-gray-100 dark:text-gray-800 transition rounded-3xl"
  551. on:click={() => {
  552. editMessageConfirmHandler();
  553. }}
  554. >
  555. {$i18n.t('Save')}
  556. </button>
  557. </div>
  558. </div>
  559. </div>
  560. {:else}
  561. <div class="w-full flex flex-col relative" id="response-content-container">
  562. {#if message.content === '' && !message.error}
  563. <Skeleton />
  564. {:else if message.content && message.error !== true}
  565. <!-- always show message contents even if there's an error -->
  566. <!-- unless message.error === true which is legacy error handling, where the error message is stored in message.content -->
  567. <ContentRenderer
  568. id={message.id}
  569. {history}
  570. content={message.content}
  571. sources={message.sources}
  572. floatingButtons={message?.done}
  573. save={!readOnly}
  574. {model}
  575. onSourceClick={(e) => {
  576. console.log(e);
  577. const sourceButton = document.getElementById(`source-${e}`);
  578. if (sourceButton) {
  579. sourceButton.click();
  580. }
  581. }}
  582. onAddMessages={({ modelId, parentId, messages }) => {
  583. addMessages({ modelId, parentId, messages });
  584. }}
  585. on:update={(e) => {
  586. const { raw, oldContent, newContent } = e.detail;
  587. history.messages[message.id].content = history.messages[
  588. message.id
  589. ].content.replace(raw, raw.replace(oldContent, newContent));
  590. updateChat();
  591. }}
  592. on:select={(e) => {
  593. const { type, content } = e.detail;
  594. if (type === 'explain') {
  595. submitMessage(
  596. message.id,
  597. `Explain this section to me in more detail\n\n\`\`\`\n${content}\n\`\`\``
  598. );
  599. } else if (type === 'ask') {
  600. const input = e.detail?.input ?? '';
  601. submitMessage(message.id, `\`\`\`\n${content}\n\`\`\`\n${input}`);
  602. }
  603. }}
  604. />
  605. {/if}
  606. {#if message?.error}
  607. <Error content={message?.error?.content ?? message.content} />
  608. {/if}
  609. {#if (message?.sources || message?.citations) && (model?.info?.meta?.capabilities?.citations ?? true)}
  610. <Citations sources={message?.sources ?? message?.citations} />
  611. {/if}
  612. {#if message.code_executions}
  613. <CodeExecutions codeExecutions={message.code_executions} />
  614. {/if}
  615. </div>
  616. {/if}
  617. </div>
  618. </div>
  619. {#if !edit}
  620. {#if message.done || siblings.length > 1}
  621. <div
  622. class=" flex justify-start overflow-x-auto buttons text-gray-600 dark:text-gray-500 mt-0.5"
  623. >
  624. {#if siblings.length > 1}
  625. <div class="flex self-center min-w-fit" dir="ltr">
  626. <button
  627. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  628. on:click={() => {
  629. showPreviousMessage(message);
  630. }}
  631. >
  632. <svg
  633. xmlns="http://www.w3.org/2000/svg"
  634. fill="none"
  635. viewBox="0 0 24 24"
  636. stroke="currentColor"
  637. stroke-width="2.5"
  638. class="size-3.5"
  639. >
  640. <path
  641. stroke-linecap="round"
  642. stroke-linejoin="round"
  643. d="M15.75 19.5 8.25 12l7.5-7.5"
  644. />
  645. </svg>
  646. </button>
  647. <div
  648. class="text-sm tracking-widest font-semibold self-center dark:text-gray-100 min-w-fit"
  649. >
  650. {siblings.indexOf(message.id) + 1}/{siblings.length}
  651. </div>
  652. <button
  653. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  654. on:click={() => {
  655. showNextMessage(message);
  656. }}
  657. >
  658. <svg
  659. xmlns="http://www.w3.org/2000/svg"
  660. fill="none"
  661. viewBox="0 0 24 24"
  662. stroke="currentColor"
  663. stroke-width="2.5"
  664. class="size-3.5"
  665. >
  666. <path
  667. stroke-linecap="round"
  668. stroke-linejoin="round"
  669. d="m8.25 4.5 7.5 7.5-7.5 7.5"
  670. />
  671. </svg>
  672. </button>
  673. </div>
  674. {/if}
  675. {#if message.done}
  676. {#if !readOnly}
  677. {#if $user.role === 'user' ? ($user?.permissions?.chat?.edit ?? true) : true}
  678. <Tooltip content={$i18n.t('Edit')} placement="bottom">
  679. <button
  680. class="{isLastMessage
  681. ? 'visible'
  682. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition"
  683. on:click={() => {
  684. editMessageHandler();
  685. }}
  686. >
  687. <svg
  688. xmlns="http://www.w3.org/2000/svg"
  689. fill="none"
  690. viewBox="0 0 24 24"
  691. stroke-width="2.3"
  692. stroke="currentColor"
  693. class="w-4 h-4"
  694. >
  695. <path
  696. stroke-linecap="round"
  697. stroke-linejoin="round"
  698. d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487zm0 0L19.5 7.125"
  699. />
  700. </svg>
  701. </button>
  702. </Tooltip>
  703. {/if}
  704. {/if}
  705. <Tooltip content={$i18n.t('Copy')} placement="bottom">
  706. <button
  707. class="{isLastMessage
  708. ? 'visible'
  709. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition copy-response-button"
  710. on:click={() => {
  711. copyToClipboard(message.content);
  712. }}
  713. >
  714. <svg
  715. xmlns="http://www.w3.org/2000/svg"
  716. fill="none"
  717. viewBox="0 0 24 24"
  718. stroke-width="2.3"
  719. stroke="currentColor"
  720. class="w-4 h-4"
  721. >
  722. <path
  723. stroke-linecap="round"
  724. stroke-linejoin="round"
  725. d="M15.666 3.888A2.25 2.25 0 0013.5 2.25h-3c-1.03 0-1.9.693-2.166 1.638m7.332 0c.055.194.084.4.084.612v0a.75.75 0 01-.75.75H9a.75.75 0 01-.75-.75v0c0-.212.03-.418.084-.612m7.332 0c.646.049 1.288.11 1.927.184 1.1.128 1.907 1.077 1.907 2.185V19.5a2.25 2.25 0 01-2.25 2.25H6.75A2.25 2.25 0 014.5 19.5V6.257c0-1.108.806-2.057 1.907-2.185a48.208 48.208 0 011.927-.184"
  726. />
  727. </svg>
  728. </button>
  729. </Tooltip>
  730. <Tooltip content={$i18n.t('Read Aloud')} placement="bottom">
  731. <button
  732. id="speak-button-{message.id}"
  733. class="{isLastMessage
  734. ? 'visible'
  735. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition"
  736. on:click={() => {
  737. if (!loadingSpeech) {
  738. toggleSpeakMessage();
  739. }
  740. }}
  741. >
  742. {#if loadingSpeech}
  743. <svg
  744. class=" w-4 h-4"
  745. fill="currentColor"
  746. viewBox="0 0 24 24"
  747. xmlns="http://www.w3.org/2000/svg"
  748. >
  749. <style>
  750. .spinner_S1WN {
  751. animation: spinner_MGfb 0.8s linear infinite;
  752. animation-delay: -0.8s;
  753. }
  754. .spinner_Km9P {
  755. animation-delay: -0.65s;
  756. }
  757. .spinner_JApP {
  758. animation-delay: -0.5s;
  759. }
  760. @keyframes spinner_MGfb {
  761. 93.75%,
  762. 100% {
  763. opacity: 0.2;
  764. }
  765. }
  766. </style>
  767. <circle class="spinner_S1WN" cx="4" cy="12" r="3" />
  768. <circle class="spinner_S1WN spinner_Km9P" cx="12" cy="12" r="3" />
  769. <circle class="spinner_S1WN spinner_JApP" cx="20" cy="12" r="3" />
  770. </svg>
  771. {:else if speaking}
  772. <svg
  773. xmlns="http://www.w3.org/2000/svg"
  774. fill="none"
  775. viewBox="0 0 24 24"
  776. stroke-width="2.3"
  777. stroke="currentColor"
  778. class="w-4 h-4"
  779. >
  780. <path
  781. stroke-linecap="round"
  782. stroke-linejoin="round"
  783. d="M17.25 9.75 19.5 12m0 0 2.25 2.25M19.5 12l2.25-2.25M19.5 12l-2.25 2.25m-10.5-6 4.72-4.72a.75.75 0 0 1 1.28.53v15.88a.75.75 0 0 1-1.28.53l-4.72-4.72H4.51c-.88 0-1.704-.507-1.938-1.354A9.009 9.009 0 0 1 2.25 12c0-.83.112-1.633.322-2.396C2.806 8.756 3.63 8.25 4.51 8.25H6.75Z"
  784. />
  785. </svg>
  786. {:else}
  787. <svg
  788. xmlns="http://www.w3.org/2000/svg"
  789. fill="none"
  790. viewBox="0 0 24 24"
  791. stroke-width="2.3"
  792. stroke="currentColor"
  793. class="w-4 h-4"
  794. >
  795. <path
  796. stroke-linecap="round"
  797. stroke-linejoin="round"
  798. d="M19.114 5.636a9 9 0 010 12.728M16.463 8.288a5.25 5.25 0 010 7.424M6.75 8.25l4.72-4.72a.75.75 0 011.28.53v15.88a.75.75 0 01-1.28.53l-4.72-4.72H4.51c-.88 0-1.704-.507-1.938-1.354A9.01 9.01 0 012.25 12c0-.83.112-1.633.322-2.396C2.806 8.756 3.63 8.25 4.51 8.25H6.75z"
  799. />
  800. </svg>
  801. {/if}
  802. </button>
  803. </Tooltip>
  804. {#if $config?.features.enable_image_generation && !readOnly}
  805. <Tooltip content={$i18n.t('Generate Image')} placement="bottom">
  806. <button
  807. class="{isLastMessage
  808. ? 'visible'
  809. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition"
  810. on:click={() => {
  811. if (!generatingImage) {
  812. generateImage(message);
  813. }
  814. }}
  815. >
  816. {#if generatingImage}
  817. <svg
  818. class=" w-4 h-4"
  819. fill="currentColor"
  820. viewBox="0 0 24 24"
  821. xmlns="http://www.w3.org/2000/svg"
  822. >
  823. <style>
  824. .spinner_S1WN {
  825. animation: spinner_MGfb 0.8s linear infinite;
  826. animation-delay: -0.8s;
  827. }
  828. .spinner_Km9P {
  829. animation-delay: -0.65s;
  830. }
  831. .spinner_JApP {
  832. animation-delay: -0.5s;
  833. }
  834. @keyframes spinner_MGfb {
  835. 93.75%,
  836. 100% {
  837. opacity: 0.2;
  838. }
  839. }
  840. </style>
  841. <circle class="spinner_S1WN" cx="4" cy="12" r="3" />
  842. <circle class="spinner_S1WN spinner_Km9P" cx="12" cy="12" r="3" />
  843. <circle class="spinner_S1WN spinner_JApP" cx="20" cy="12" r="3" />
  844. </svg>
  845. {:else}
  846. <svg
  847. xmlns="http://www.w3.org/2000/svg"
  848. fill="none"
  849. viewBox="0 0 24 24"
  850. stroke-width="2.3"
  851. stroke="currentColor"
  852. class="w-4 h-4"
  853. >
  854. <path
  855. stroke-linecap="round"
  856. stroke-linejoin="round"
  857. d="m2.25 15.75 5.159-5.159a2.25 2.25 0 0 1 3.182 0l5.159 5.159m-1.5-1.5 1.409-1.409a2.25 2.25 0 0 1 3.182 0l2.909 2.909m-18 3.75h16.5a1.5 1.5 0 0 0 1.5-1.5V6a1.5 1.5 0 0 0-1.5-1.5H3.75A1.5 1.5 0 0 0 2.25 6v12a1.5 1.5 0 0 0 1.5 1.5Zm10.5-11.25h.008v.008h-.008V8.25Zm.375 0a.375.375 0 1 1-.75 0 .375.375 0 0 1 .75 0Z"
  858. />
  859. </svg>
  860. {/if}
  861. </button>
  862. </Tooltip>
  863. {/if}
  864. {#if message.usage}
  865. <Tooltip
  866. content={message.usage
  867. ? `<pre>${sanitizeResponseContent(
  868. JSON.stringify(message.usage, null, 2)
  869. .replace(/"([^(")"]+)":/g, '$1:')
  870. .slice(1, -1)
  871. .split('\n')
  872. .map((line) => line.slice(2))
  873. .map((line) => (line.endsWith(',') ? line.slice(0, -1) : line))
  874. .join('\n')
  875. )}</pre>`
  876. : ''}
  877. placement="bottom"
  878. >
  879. <button
  880. class=" {isLastMessage
  881. ? 'visible'
  882. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition whitespace-pre-wrap"
  883. on:click={() => {
  884. console.log(message);
  885. }}
  886. id="info-{message.id}"
  887. >
  888. <svg
  889. xmlns="http://www.w3.org/2000/svg"
  890. fill="none"
  891. viewBox="0 0 24 24"
  892. stroke-width="2.3"
  893. stroke="currentColor"
  894. class="w-4 h-4"
  895. >
  896. <path
  897. stroke-linecap="round"
  898. stroke-linejoin="round"
  899. d="M11.25 11.25l.041-.02a.75.75 0 011.063.852l-.708 2.836a.75.75 0 001.063.853l.041-.021M21 12a9 9 0 11-18 0 9 9 0 0118 0zm-9-3.75h.008v.008H12V8.25z"
  900. />
  901. </svg>
  902. </button>
  903. </Tooltip>
  904. {/if}
  905. {#if !readOnly}
  906. {#if $config?.features.enable_message_rating ?? true}
  907. <Tooltip content={$i18n.t('Good Response')} placement="bottom">
  908. <button
  909. class="{isLastMessage
  910. ? 'visible'
  911. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg {(
  912. message?.annotation?.rating ?? ''
  913. ).toString() === '1'
  914. ? 'bg-gray-100 dark:bg-gray-800'
  915. : ''} dark:hover:text-white hover:text-black transition disabled:cursor-progress disabled:hover:bg-transparent"
  916. disabled={feedbackLoading}
  917. on:click={async () => {
  918. await feedbackHandler(1);
  919. window.setTimeout(() => {
  920. document
  921. .getElementById(`message-feedback-${message.id}`)
  922. ?.scrollIntoView();
  923. }, 0);
  924. }}
  925. >
  926. <svg
  927. stroke="currentColor"
  928. fill="none"
  929. stroke-width="2.3"
  930. viewBox="0 0 24 24"
  931. stroke-linecap="round"
  932. stroke-linejoin="round"
  933. class="w-4 h-4"
  934. xmlns="http://www.w3.org/2000/svg"
  935. >
  936. <path
  937. d="M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3"
  938. />
  939. </svg>
  940. </button>
  941. </Tooltip>
  942. <Tooltip content={$i18n.t('Bad Response')} placement="bottom">
  943. <button
  944. class="{isLastMessage
  945. ? 'visible'
  946. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg {(
  947. message?.annotation?.rating ?? ''
  948. ).toString() === '-1'
  949. ? 'bg-gray-100 dark:bg-gray-800'
  950. : ''} dark:hover:text-white hover:text-black transition disabled:cursor-progress disabled:hover:bg-transparent"
  951. disabled={feedbackLoading}
  952. on:click={async () => {
  953. await feedbackHandler(-1);
  954. window.setTimeout(() => {
  955. document
  956. .getElementById(`message-feedback-${message.id}`)
  957. ?.scrollIntoView();
  958. }, 0);
  959. }}
  960. >
  961. <svg
  962. stroke="currentColor"
  963. fill="none"
  964. stroke-width="2.3"
  965. viewBox="0 0 24 24"
  966. stroke-linecap="round"
  967. stroke-linejoin="round"
  968. class="w-4 h-4"
  969. xmlns="http://www.w3.org/2000/svg"
  970. >
  971. <path
  972. d="M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17"
  973. />
  974. </svg>
  975. </button>
  976. </Tooltip>
  977. {/if}
  978. {#if isLastMessage}
  979. <Tooltip content={$i18n.t('Continue Response')} placement="bottom">
  980. <button
  981. type="button"
  982. id="continue-response-button"
  983. class="{isLastMessage
  984. ? 'visible'
  985. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition regenerate-response-button"
  986. on:click={() => {
  987. continueResponse();
  988. }}
  989. >
  990. <svg
  991. xmlns="http://www.w3.org/2000/svg"
  992. fill="none"
  993. viewBox="0 0 24 24"
  994. stroke-width="2.3"
  995. stroke="currentColor"
  996. class="w-4 h-4"
  997. >
  998. <path
  999. stroke-linecap="round"
  1000. stroke-linejoin="round"
  1001. d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
  1002. />
  1003. <path
  1004. stroke-linecap="round"
  1005. stroke-linejoin="round"
  1006. d="M15.91 11.672a.375.375 0 0 1 0 .656l-5.603 3.113a.375.375 0 0 1-.557-.328V8.887c0-.286.307-.466.557-.327l5.603 3.112Z"
  1007. />
  1008. </svg>
  1009. </button>
  1010. </Tooltip>
  1011. {/if}
  1012. <Tooltip content={$i18n.t('Regenerate')} placement="bottom">
  1013. <button
  1014. type="button"
  1015. class="{isLastMessage
  1016. ? 'visible'
  1017. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition regenerate-response-button"
  1018. on:click={() => {
  1019. showRateComment = false;
  1020. regenerateResponse(message);
  1021. (model?.actions ?? []).forEach((action) => {
  1022. dispatch('action', {
  1023. id: action.id,
  1024. event: {
  1025. id: 'regenerate-response',
  1026. data: {
  1027. messageId: message.id
  1028. }
  1029. }
  1030. });
  1031. });
  1032. }}
  1033. >
  1034. <svg
  1035. xmlns="http://www.w3.org/2000/svg"
  1036. fill="none"
  1037. viewBox="0 0 24 24"
  1038. stroke-width="2.3"
  1039. stroke="currentColor"
  1040. class="w-4 h-4"
  1041. >
  1042. <path
  1043. stroke-linecap="round"
  1044. stroke-linejoin="round"
  1045. d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99"
  1046. />
  1047. </svg>
  1048. </button>
  1049. </Tooltip>
  1050. {#if isLastMessage}
  1051. {#each model?.actions ?? [] as action}
  1052. <Tooltip content={action.name} placement="bottom">
  1053. <button
  1054. type="button"
  1055. class="{isLastMessage
  1056. ? 'visible'
  1057. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition regenerate-response-button"
  1058. on:click={() => {
  1059. actionMessage(action.id, message);
  1060. }}
  1061. >
  1062. {#if action.icon_url}
  1063. <img
  1064. src={action.icon_url}
  1065. class="w-4 h-4 {action.icon_url.includes('svg')
  1066. ? 'dark:invert-[80%]'
  1067. : ''}"
  1068. style="fill: currentColor;"
  1069. alt={action.name}
  1070. />
  1071. {:else}
  1072. <Sparkles strokeWidth="2.1" className="size-4" />
  1073. {/if}
  1074. </button>
  1075. </Tooltip>
  1076. {/each}
  1077. {/if}
  1078. {/if}
  1079. {/if}
  1080. </div>
  1081. {/if}
  1082. {#if message.done && showRateComment}
  1083. <RateComment
  1084. bind:message
  1085. bind:show={showRateComment}
  1086. on:save={async (e) => {
  1087. await feedbackHandler(null, {
  1088. ...e.detail
  1089. });
  1090. }}
  1091. />
  1092. {/if}
  1093. {/if}
  1094. </div>
  1095. </div>
  1096. </div>
  1097. {/key}
  1098. <style>
  1099. .buttons::-webkit-scrollbar {
  1100. display: none; /* for Chrome, Safari and Opera */
  1101. }
  1102. .buttons {
  1103. -ms-overflow-style: none; /* IE and Edge */
  1104. scrollbar-width: none; /* Firefox */
  1105. }
  1106. @keyframes shimmer {
  1107. 0% {
  1108. background-position: 200% 0;
  1109. }
  1110. 100% {
  1111. background-position: -200% 0;
  1112. }
  1113. }
  1114. .shimmer {
  1115. background: linear-gradient(90deg, #9a9b9e 25%, #2a2929 50%, #9a9b9e 75%);
  1116. background-size: 200% 100%;
  1117. background-clip: text;
  1118. -webkit-background-clip: text;
  1119. -webkit-text-fill-color: transparent;
  1120. animation: shimmer 4s linear infinite;
  1121. color: #818286; /* Fallback color */
  1122. }
  1123. :global(.dark) .shimmer {
  1124. background: linear-gradient(90deg, #818286 25%, #eae5e5 50%, #818286 75%);
  1125. background-size: 200% 100%;
  1126. background-clip: text;
  1127. -webkit-background-clip: text;
  1128. -webkit-text-fill-color: transparent;
  1129. animation: shimmer 4s linear infinite;
  1130. color: #a1a3a7; /* Darker fallback color for dark mode */
  1131. }
  1132. @keyframes smoothFadeIn {
  1133. 0% {
  1134. opacity: 0;
  1135. transform: translateY(-10px);
  1136. }
  1137. 100% {
  1138. opacity: 1;
  1139. transform: translateY(0);
  1140. }
  1141. }
  1142. .status-description {
  1143. animation: smoothFadeIn 0.2s forwards;
  1144. }
  1145. </style>