ResponseMessage.svelte 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import dayjs from 'dayjs';
  4. import { marked } from 'marked';
  5. import tippy from 'tippy.js';
  6. import auto_render from 'katex/dist/contrib/auto-render.mjs';
  7. import 'katex/dist/katex.min.css';
  8. import mermaid from 'mermaid';
  9. import { fade } from 'svelte/transition';
  10. import { createEventDispatcher } from 'svelte';
  11. import { onMount, tick, getContext } from 'svelte';
  12. const i18n = getContext('i18n');
  13. const dispatch = createEventDispatcher();
  14. import { config, models, settings, user } from '$lib/stores';
  15. import { synthesizeOpenAISpeech } from '$lib/apis/audio';
  16. import { imageGenerations } from '$lib/apis/images';
  17. import {
  18. approximateToHumanReadable,
  19. extractSentences,
  20. replaceTokens,
  21. revertSanitizedResponseContent,
  22. sanitizeResponseContent
  23. } from '$lib/utils';
  24. import { WEBUI_BASE_URL } from '$lib/constants';
  25. import Name from './Name.svelte';
  26. import ProfileImage from './ProfileImage.svelte';
  27. import Skeleton from './Skeleton.svelte';
  28. import CodeBlock from './CodeBlock.svelte';
  29. import Image from '$lib/components/common/Image.svelte';
  30. import Tooltip from '$lib/components/common/Tooltip.svelte';
  31. import RateComment from './RateComment.svelte';
  32. import CitationsModal from '$lib/components/chat/Messages/CitationsModal.svelte';
  33. import Spinner from '$lib/components/common/Spinner.svelte';
  34. import WebSearchResults from './ResponseMessage/WebSearchResults.svelte';
  35. import Sparkles from '$lib/components/icons/Sparkles.svelte';
  36. export let message;
  37. export let siblings;
  38. export let isLastMessage = true;
  39. export let readOnly = false;
  40. export let updateChatMessages: Function;
  41. export let confirmEditResponseMessage: Function;
  42. export let showPreviousMessage: Function;
  43. export let showNextMessage: Function;
  44. export let rateMessage: Function;
  45. export let copyToClipboard: Function;
  46. export let continueGeneration: Function;
  47. export let regenerateResponse: Function;
  48. export let chatActionHandler: Function;
  49. let model = null;
  50. $: model = $models.find((m) => m.id === message.model);
  51. let edit = false;
  52. let editedContent = '';
  53. let editTextAreaElement: HTMLTextAreaElement;
  54. let tooltipInstance = null;
  55. let sentencesAudio = {};
  56. let speaking = null;
  57. let speakingIdx = null;
  58. let loadingSpeech = false;
  59. let generatingImage = false;
  60. let showRateComment = false;
  61. let showCitationModal = false;
  62. let selectedCitation = null;
  63. $: tokens = marked.lexer(
  64. replaceTokens(sanitizeResponseContent(message?.content), model?.name, $user?.name)
  65. );
  66. const renderer = new marked.Renderer();
  67. // For code blocks with simple backticks
  68. renderer.codespan = (code) => {
  69. return `<code>${code.replaceAll('&amp;', '&')}</code>`;
  70. };
  71. // Open all links in a new tab/window (from https://github.com/markedjs/marked/issues/655#issuecomment-383226346)
  72. const origLinkRenderer = renderer.link;
  73. renderer.link = (href, title, text) => {
  74. const html = origLinkRenderer.call(renderer, href, title, text);
  75. return html.replace(/^<a /, '<a target="_blank" rel="nofollow" ');
  76. };
  77. const { extensions, ...defaults } = marked.getDefaults() as marked.MarkedOptions & {
  78. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  79. extensions: any;
  80. };
  81. $: if (message) {
  82. renderStyling();
  83. }
  84. const renderStyling = async () => {
  85. await tick();
  86. if (tooltipInstance) {
  87. tooltipInstance[0]?.destroy();
  88. }
  89. renderLatex();
  90. if (message.info) {
  91. let tooltipContent = '';
  92. if (message.info.openai) {
  93. tooltipContent = `prompt_tokens: ${message.info.prompt_tokens ?? 'N/A'}<br/>
  94. completion_tokens: ${message.info.completion_tokens ?? 'N/A'}<br/>
  95. total_tokens: ${message.info.total_tokens ?? 'N/A'}`;
  96. } else {
  97. tooltipContent = `response_token/s: ${
  98. `${
  99. Math.round(
  100. ((message.info.eval_count ?? 0) / (message.info.eval_duration / 1000000000)) * 100
  101. ) / 100
  102. } tokens` ?? 'N/A'
  103. }<br/>
  104. prompt_token/s: ${
  105. Math.round(
  106. ((message.info.prompt_eval_count ?? 0) /
  107. (message.info.prompt_eval_duration / 1000000000)) *
  108. 100
  109. ) / 100 ?? 'N/A'
  110. } tokens<br/>
  111. total_duration: ${
  112. Math.round(((message.info.total_duration ?? 0) / 1000000) * 100) / 100 ??
  113. 'N/A'
  114. }ms<br/>
  115. load_duration: ${
  116. Math.round(((message.info.load_duration ?? 0) / 1000000) * 100) / 100 ?? 'N/A'
  117. }ms<br/>
  118. prompt_eval_count: ${message.info.prompt_eval_count ?? 'N/A'}<br/>
  119. prompt_eval_duration: ${
  120. Math.round(((message.info.prompt_eval_duration ?? 0) / 1000000) * 100) /
  121. 100 ?? 'N/A'
  122. }ms<br/>
  123. eval_count: ${message.info.eval_count ?? 'N/A'}<br/>
  124. eval_duration: ${
  125. Math.round(((message.info.eval_duration ?? 0) / 1000000) * 100) / 100 ?? 'N/A'
  126. }ms<br/>
  127. approximate_total: ${approximateToHumanReadable(message.info.total_duration)}`;
  128. }
  129. tooltipInstance = tippy(`#info-${message.id}`, {
  130. content: `<span class="text-xs" id="tooltip-${message.id}">${tooltipContent}</span>`,
  131. allowHTML: true,
  132. theme: 'dark',
  133. arrow: false,
  134. offset: [0, 4]
  135. });
  136. }
  137. };
  138. const renderLatex = () => {
  139. let chatMessageElements = document
  140. .getElementById(`message-${message.id}`)
  141. ?.getElementsByClassName('chat-assistant');
  142. if (chatMessageElements) {
  143. for (const element of chatMessageElements) {
  144. auto_render(element, {
  145. // customised options
  146. // • auto-render specific keys, e.g.:
  147. delimiters: [
  148. { left: '$$', right: '$$', display: false },
  149. { left: '$ ', right: ' $', display: false },
  150. { left: '\\(', right: '\\)', display: false },
  151. { left: '\\[', right: '\\]', display: false },
  152. { left: '[ ', right: ' ]', display: false }
  153. ],
  154. // • rendering keys, e.g.:
  155. throwOnError: false
  156. });
  157. }
  158. }
  159. };
  160. const playAudio = (idx) => {
  161. return new Promise((res) => {
  162. speakingIdx = idx;
  163. const audio = sentencesAudio[idx];
  164. audio.play();
  165. audio.onended = async (e) => {
  166. await new Promise((r) => setTimeout(r, 300));
  167. if (Object.keys(sentencesAudio).length - 1 === idx) {
  168. speaking = null;
  169. }
  170. res(e);
  171. };
  172. });
  173. };
  174. const toggleSpeakMessage = async () => {
  175. if (speaking) {
  176. try {
  177. speechSynthesis.cancel();
  178. sentencesAudio[speakingIdx].pause();
  179. sentencesAudio[speakingIdx].currentTime = 0;
  180. } catch {}
  181. speaking = null;
  182. speakingIdx = null;
  183. } else {
  184. if ((message?.content ?? '').trim() !== '') {
  185. speaking = true;
  186. if ($config.audio.tts.engine !== '') {
  187. loadingSpeech = true;
  188. const sentences = extractSentences(message.content).reduce((mergedTexts, currentText) => {
  189. const lastIndex = mergedTexts.length - 1;
  190. if (lastIndex >= 0) {
  191. const previousText = mergedTexts[lastIndex];
  192. const wordCount = previousText.split(/\s+/).length;
  193. if (wordCount < 2) {
  194. mergedTexts[lastIndex] = previousText + ' ' + currentText;
  195. } else {
  196. mergedTexts.push(currentText);
  197. }
  198. } else {
  199. mergedTexts.push(currentText);
  200. }
  201. return mergedTexts;
  202. }, []);
  203. console.log(sentences);
  204. if (sentences.length > 0) {
  205. sentencesAudio = sentences.reduce((a, e, i, arr) => {
  206. a[i] = null;
  207. return a;
  208. }, {});
  209. let lastPlayedAudioPromise = Promise.resolve(); // Initialize a promise that resolves immediately
  210. for (const [idx, sentence] of sentences.entries()) {
  211. const res = await synthesizeOpenAISpeech(
  212. localStorage.token,
  213. $settings?.audio?.tts?.voice ?? $config?.audio?.tts?.voice,
  214. sentence
  215. ).catch((error) => {
  216. toast.error(error);
  217. speaking = null;
  218. loadingSpeech = false;
  219. return null;
  220. });
  221. if (res) {
  222. const blob = await res.blob();
  223. const blobUrl = URL.createObjectURL(blob);
  224. const audio = new Audio(blobUrl);
  225. sentencesAudio[idx] = audio;
  226. loadingSpeech = false;
  227. lastPlayedAudioPromise = lastPlayedAudioPromise.then(() => playAudio(idx));
  228. }
  229. }
  230. } else {
  231. speaking = null;
  232. loadingSpeech = false;
  233. }
  234. } else {
  235. let voices = [];
  236. const getVoicesLoop = setInterval(async () => {
  237. voices = await speechSynthesis.getVoices();
  238. if (voices.length > 0) {
  239. clearInterval(getVoicesLoop);
  240. const voice =
  241. voices
  242. ?.filter(
  243. (v) =>
  244. v.voiceURI === ($settings?.audio?.tts?.voice ?? $config?.audio?.tts?.voice)
  245. )
  246. ?.at(0) ?? undefined;
  247. console.log(voice);
  248. const speak = new SpeechSynthesisUtterance(message.content);
  249. console.log(speak);
  250. speak.onend = () => {
  251. speaking = null;
  252. if ($settings.conversationMode) {
  253. document.getElementById('voice-input-button')?.click();
  254. }
  255. };
  256. if (voice) {
  257. speak.voice = voice;
  258. }
  259. speechSynthesis.speak(speak);
  260. }
  261. }, 100);
  262. }
  263. } else {
  264. toast.error($i18n.t('No content to speak'));
  265. }
  266. }
  267. };
  268. const editMessageHandler = async () => {
  269. edit = true;
  270. editedContent = message.content;
  271. await tick();
  272. editTextAreaElement.style.height = '';
  273. editTextAreaElement.style.height = `${editTextAreaElement.scrollHeight}px`;
  274. };
  275. const editMessageConfirmHandler = async () => {
  276. if (editedContent === '') {
  277. editedContent = ' ';
  278. }
  279. confirmEditResponseMessage(message.id, editedContent);
  280. edit = false;
  281. editedContent = '';
  282. await tick();
  283. renderStyling();
  284. };
  285. const cancelEditMessage = async () => {
  286. edit = false;
  287. editedContent = '';
  288. await tick();
  289. renderStyling();
  290. };
  291. const generateImage = async (message) => {
  292. generatingImage = true;
  293. const res = await imageGenerations(localStorage.token, message.content).catch((error) => {
  294. toast.error(error);
  295. });
  296. console.log(res);
  297. if (res) {
  298. message.files = res.map((image) => ({
  299. type: 'image',
  300. url: `${image.url}`
  301. }));
  302. dispatch('save', message);
  303. }
  304. generatingImage = false;
  305. };
  306. $: if (!edit) {
  307. (async () => {
  308. await tick();
  309. renderStyling();
  310. await mermaid.run({
  311. querySelector: '.mermaid'
  312. });
  313. })();
  314. }
  315. onMount(async () => {
  316. await tick();
  317. renderStyling();
  318. await mermaid.run({
  319. querySelector: '.mermaid'
  320. });
  321. });
  322. </script>
  323. <CitationsModal bind:show={showCitationModal} citation={selectedCitation} />
  324. {#key message.id}
  325. <div
  326. class=" flex w-full message-{message.id}"
  327. id="message-{message.id}"
  328. dir={$settings.chatDirection}
  329. >
  330. <ProfileImage
  331. src={model?.info?.meta?.profile_image_url ??
  332. ($i18n.language === 'dg-DG' ? `/doge.png` : `${WEBUI_BASE_URL}/static/favicon.png`)}
  333. />
  334. <div class="w-full overflow-hidden pl-1">
  335. <Name>
  336. {model?.name ?? message.model}
  337. {#if message.timestamp}
  338. <span
  339. class=" self-center invisible group-hover:visible text-gray-400 text-xs font-medium uppercase"
  340. >
  341. {dayjs(message.timestamp * 1000).format($i18n.t('h:mm a'))}
  342. </span>
  343. {/if}
  344. </Name>
  345. {#if (message?.files ?? []).filter((f) => f.type === 'image').length > 0}
  346. <div class="my-2.5 w-full flex overflow-x-auto gap-2 flex-wrap">
  347. {#each message.files as file}
  348. <div>
  349. {#if file.type === 'image'}
  350. <Image src={file.url} />
  351. {/if}
  352. </div>
  353. {/each}
  354. </div>
  355. {/if}
  356. <div
  357. class="prose chat-{message.role} w-full max-w-full dark:prose-invert prose-headings:my-0 prose-headings:-mb-4 prose-p:m-0 prose-p:-mb-6 prose-pre:my-0 prose-table:my-0 prose-blockquote:my-0 prose-img:my-0 prose-ul:-my-4 prose-ol:-my-4 prose-li:-my-3 prose-ul:-mb-6 prose-ol:-mb-8 prose-ol:p-0 prose-li:-mb-4 whitespace-pre-line"
  358. >
  359. <div>
  360. {#if (message?.statusHistory ?? [...(message?.status ? [message?.status] : [])]).length > 0}
  361. {@const status = (
  362. message?.statusHistory ?? [...(message?.status ? [message?.status] : [])]
  363. ).at(-1)}
  364. <div class="flex items-center gap-2 pt-1 pb-1">
  365. {#if status.done === false}
  366. <div class="">
  367. <Spinner className="size-4" />
  368. </div>
  369. {/if}
  370. {#if status?.action === 'web_search' && status?.urls}
  371. <WebSearchResults {status}>
  372. <div class="flex flex-col justify-center -space-y-0.5">
  373. <div class="text-base line-clamp-1 text-wrap">
  374. {status?.description}
  375. </div>
  376. </div>
  377. </WebSearchResults>
  378. {:else}
  379. <div class="flex flex-col justify-center -space-y-0.5">
  380. <div class=" text-gray-500 dark:text-gray-500 text-base line-clamp-1 text-wrap">
  381. {status?.description}
  382. </div>
  383. </div>
  384. {/if}
  385. </div>
  386. {/if}
  387. {#if edit === true}
  388. <div class="w-full bg-gray-50 dark:bg-gray-800 rounded-3xl px-5 py-3 my-2">
  389. <textarea
  390. id="message-edit-{message.id}"
  391. bind:this={editTextAreaElement}
  392. class=" bg-transparent outline-none w-full resize-none"
  393. bind:value={editedContent}
  394. on:input={(e) => {
  395. e.target.style.height = '';
  396. e.target.style.height = `${e.target.scrollHeight}px`;
  397. }}
  398. on:keydown={(e) => {
  399. if (e.key === 'Escape') {
  400. document.getElementById('close-edit-message-button')?.click();
  401. }
  402. const isCmdOrCtrlPressed = e.metaKey || e.ctrlKey;
  403. const isEnterPressed = e.key === 'Enter';
  404. if (isCmdOrCtrlPressed && isEnterPressed) {
  405. document.getElementById('save-edit-message-button')?.click();
  406. }
  407. }}
  408. />
  409. <div class=" mt-2 mb-1 flex justify-end space-x-1.5 text-sm font-medium">
  410. <button
  411. id="close-edit-message-button"
  412. class="px-4 py-2 bg-white hover:bg-gray-100 text-gray-800 transition rounded-3xl"
  413. on:click={() => {
  414. cancelEditMessage();
  415. }}
  416. >
  417. {$i18n.t('Cancel')}
  418. </button>
  419. <button
  420. id="save-edit-message-button"
  421. class=" px-4 py-2 bg-gray-900 hover:bg-gray-850 text-gray-100 transition rounded-3xl"
  422. on:click={() => {
  423. editMessageConfirmHandler();
  424. }}
  425. >
  426. {$i18n.t('Save')}
  427. </button>
  428. </div>
  429. </div>
  430. {:else}
  431. <div class="w-full">
  432. {#if message.content === '' && !message.error}
  433. <Skeleton />
  434. {:else if message.content && message.error !== true}
  435. <!-- always show message contents even if there's an error -->
  436. <!-- unless message.error === true which is legacy error handling, where the error message is stored in message.content -->
  437. {#each tokens as token, tokenIdx}
  438. {#if token.type === 'code'}
  439. {#if token.lang === 'mermaid'}
  440. <pre class="mermaid">{revertSanitizedResponseContent(token.text)}</pre>
  441. {:else}
  442. <CodeBlock
  443. id={`${message.id}-${tokenIdx}`}
  444. lang={token?.lang ?? ''}
  445. code={revertSanitizedResponseContent(token?.text ?? '')}
  446. />
  447. {/if}
  448. {:else}
  449. {@html marked.parse(token.raw, {
  450. ...defaults,
  451. gfm: true,
  452. breaks: true,
  453. renderer
  454. })}
  455. {/if}
  456. {/each}
  457. {/if}
  458. {#if message.error}
  459. <div
  460. class="flex mt-2 mb-4 space-x-2 border px-4 py-3 border-red-800 bg-red-800/30 font-medium rounded-lg"
  461. >
  462. <svg
  463. xmlns="http://www.w3.org/2000/svg"
  464. fill="none"
  465. viewBox="0 0 24 24"
  466. stroke-width="1.5"
  467. stroke="currentColor"
  468. class="w-5 h-5 self-center"
  469. >
  470. <path
  471. stroke-linecap="round"
  472. stroke-linejoin="round"
  473. d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z"
  474. />
  475. </svg>
  476. <div class=" self-center">
  477. {message?.error?.content ?? message.content}
  478. </div>
  479. </div>
  480. {/if}
  481. {#if message.citations}
  482. <div class="mt-1 mb-2 w-full flex gap-1 items-center flex-wrap">
  483. {#each message.citations.reduce((acc, citation) => {
  484. citation.document.forEach((document, index) => {
  485. const metadata = citation.metadata?.[index];
  486. const id = metadata?.source ?? 'N/A';
  487. let source = citation?.source;
  488. if (metadata?.name) {
  489. source = { ...source, name: metadata.name };
  490. }
  491. // Check if ID looks like a URL
  492. if (id.startsWith('http://') || id.startsWith('https://')) {
  493. source = { name: id };
  494. }
  495. const existingSource = acc.find((item) => item.id === id);
  496. if (existingSource) {
  497. existingSource.document.push(document);
  498. existingSource.metadata.push(metadata);
  499. } else {
  500. acc.push( { id: id, source: source, document: [document], metadata: metadata ? [metadata] : [] } );
  501. }
  502. });
  503. return acc;
  504. }, []) as citation, idx}
  505. <div class="flex gap-1 text-xs font-semibold">
  506. <button
  507. class="flex dark:text-gray-300 py-1 px-1 bg-gray-50 hover:bg-gray-100 dark:bg-gray-850 dark:hover:bg-gray-800 transition rounded-xl"
  508. on:click={() => {
  509. showCitationModal = true;
  510. selectedCitation = citation;
  511. }}
  512. >
  513. <div class="bg-white dark:bg-gray-700 rounded-full size-4">
  514. {idx + 1}
  515. </div>
  516. <div class="flex-1 mx-2 line-clamp-1">
  517. {citation.source.name}
  518. </div>
  519. </button>
  520. </div>
  521. {/each}
  522. </div>
  523. {/if}
  524. {#if message.done || siblings.length > 1}
  525. <div
  526. class=" flex justify-start overflow-x-auto buttons text-gray-600 dark:text-gray-500"
  527. >
  528. {#if siblings.length > 1}
  529. <div class="flex self-center min-w-fit" dir="ltr">
  530. <button
  531. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  532. on:click={() => {
  533. showPreviousMessage(message);
  534. }}
  535. >
  536. <svg
  537. xmlns="http://www.w3.org/2000/svg"
  538. fill="none"
  539. viewBox="0 0 24 24"
  540. stroke="currentColor"
  541. stroke-width="2.5"
  542. class="size-3.5"
  543. >
  544. <path
  545. stroke-linecap="round"
  546. stroke-linejoin="round"
  547. d="M15.75 19.5 8.25 12l7.5-7.5"
  548. />
  549. </svg>
  550. </button>
  551. <div
  552. class="text-sm tracking-widest font-semibold self-center dark:text-gray-100 min-w-fit"
  553. >
  554. {siblings.indexOf(message.id) + 1}/{siblings.length}
  555. </div>
  556. <button
  557. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  558. on:click={() => {
  559. showNextMessage(message);
  560. }}
  561. >
  562. <svg
  563. xmlns="http://www.w3.org/2000/svg"
  564. fill="none"
  565. viewBox="0 0 24 24"
  566. stroke="currentColor"
  567. stroke-width="2.5"
  568. class="size-3.5"
  569. >
  570. <path
  571. stroke-linecap="round"
  572. stroke-linejoin="round"
  573. d="m8.25 4.5 7.5 7.5-7.5 7.5"
  574. />
  575. </svg>
  576. </button>
  577. </div>
  578. {/if}
  579. {#if message.done}
  580. {#if !readOnly}
  581. <Tooltip content={$i18n.t('Edit')} placement="bottom">
  582. <button
  583. class="{isLastMessage
  584. ? 'visible'
  585. : '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"
  586. on:click={() => {
  587. editMessageHandler();
  588. }}
  589. >
  590. <svg
  591. xmlns="http://www.w3.org/2000/svg"
  592. fill="none"
  593. viewBox="0 0 24 24"
  594. stroke-width="2.3"
  595. stroke="currentColor"
  596. class="w-4 h-4"
  597. >
  598. <path
  599. stroke-linecap="round"
  600. stroke-linejoin="round"
  601. 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"
  602. />
  603. </svg>
  604. </button>
  605. </Tooltip>
  606. {/if}
  607. <Tooltip content={$i18n.t('Copy')} placement="bottom">
  608. <button
  609. class="{isLastMessage
  610. ? 'visible'
  611. : '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"
  612. on:click={() => {
  613. copyToClipboard(message.content);
  614. }}
  615. >
  616. <svg
  617. xmlns="http://www.w3.org/2000/svg"
  618. fill="none"
  619. viewBox="0 0 24 24"
  620. stroke-width="2.3"
  621. stroke="currentColor"
  622. class="w-4 h-4"
  623. >
  624. <path
  625. stroke-linecap="round"
  626. stroke-linejoin="round"
  627. 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"
  628. />
  629. </svg>
  630. </button>
  631. </Tooltip>
  632. <Tooltip content={$i18n.t('Read Aloud')} placement="bottom">
  633. <button
  634. id="speak-button-{message.id}"
  635. class="{isLastMessage
  636. ? 'visible'
  637. : '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"
  638. on:click={() => {
  639. if (!loadingSpeech) {
  640. toggleSpeakMessage(message);
  641. }
  642. }}
  643. >
  644. {#if loadingSpeech}
  645. <svg
  646. class=" w-4 h-4"
  647. fill="currentColor"
  648. viewBox="0 0 24 24"
  649. xmlns="http://www.w3.org/2000/svg"
  650. ><style>
  651. .spinner_S1WN {
  652. animation: spinner_MGfb 0.8s linear infinite;
  653. animation-delay: -0.8s;
  654. }
  655. .spinner_Km9P {
  656. animation-delay: -0.65s;
  657. }
  658. .spinner_JApP {
  659. animation-delay: -0.5s;
  660. }
  661. @keyframes spinner_MGfb {
  662. 93.75%,
  663. 100% {
  664. opacity: 0.2;
  665. }
  666. }
  667. </style><circle class="spinner_S1WN" cx="4" cy="12" r="3" /><circle
  668. class="spinner_S1WN spinner_Km9P"
  669. cx="12"
  670. cy="12"
  671. r="3"
  672. /><circle
  673. class="spinner_S1WN spinner_JApP"
  674. cx="20"
  675. cy="12"
  676. r="3"
  677. /></svg
  678. >
  679. {:else if speaking}
  680. <svg
  681. xmlns="http://www.w3.org/2000/svg"
  682. fill="none"
  683. viewBox="0 0 24 24"
  684. stroke-width="2.3"
  685. stroke="currentColor"
  686. class="w-4 h-4"
  687. >
  688. <path
  689. stroke-linecap="round"
  690. stroke-linejoin="round"
  691. 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"
  692. />
  693. </svg>
  694. {:else}
  695. <svg
  696. xmlns="http://www.w3.org/2000/svg"
  697. fill="none"
  698. viewBox="0 0 24 24"
  699. stroke-width="2.3"
  700. stroke="currentColor"
  701. class="w-4 h-4"
  702. >
  703. <path
  704. stroke-linecap="round"
  705. stroke-linejoin="round"
  706. 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"
  707. />
  708. </svg>
  709. {/if}
  710. </button>
  711. </Tooltip>
  712. {#if $config?.features.enable_image_generation && !readOnly}
  713. <Tooltip content={$i18n.t('Generate Image')} placement="bottom">
  714. <button
  715. class="{isLastMessage
  716. ? 'visible'
  717. : '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"
  718. on:click={() => {
  719. if (!generatingImage) {
  720. generateImage(message);
  721. }
  722. }}
  723. >
  724. {#if generatingImage}
  725. <svg
  726. class=" w-4 h-4"
  727. fill="currentColor"
  728. viewBox="0 0 24 24"
  729. xmlns="http://www.w3.org/2000/svg"
  730. ><style>
  731. .spinner_S1WN {
  732. animation: spinner_MGfb 0.8s linear infinite;
  733. animation-delay: -0.8s;
  734. }
  735. .spinner_Km9P {
  736. animation-delay: -0.65s;
  737. }
  738. .spinner_JApP {
  739. animation-delay: -0.5s;
  740. }
  741. @keyframes spinner_MGfb {
  742. 93.75%,
  743. 100% {
  744. opacity: 0.2;
  745. }
  746. }
  747. </style><circle class="spinner_S1WN" cx="4" cy="12" r="3" /><circle
  748. class="spinner_S1WN spinner_Km9P"
  749. cx="12"
  750. cy="12"
  751. r="3"
  752. /><circle
  753. class="spinner_S1WN spinner_JApP"
  754. cx="20"
  755. cy="12"
  756. r="3"
  757. /></svg
  758. >
  759. {:else}
  760. <svg
  761. xmlns="http://www.w3.org/2000/svg"
  762. fill="none"
  763. viewBox="0 0 24 24"
  764. stroke-width="2.3"
  765. stroke="currentColor"
  766. class="w-4 h-4"
  767. >
  768. <path
  769. stroke-linecap="round"
  770. stroke-linejoin="round"
  771. 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"
  772. />
  773. </svg>
  774. {/if}
  775. </button>
  776. </Tooltip>
  777. {/if}
  778. {#if message.info}
  779. <Tooltip content={$i18n.t('Generation Info')} placement="bottom">
  780. <button
  781. class=" {isLastMessage
  782. ? 'visible'
  783. : '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"
  784. on:click={() => {
  785. console.log(message);
  786. }}
  787. id="info-{message.id}"
  788. >
  789. <svg
  790. xmlns="http://www.w3.org/2000/svg"
  791. fill="none"
  792. viewBox="0 0 24 24"
  793. stroke-width="2.3"
  794. stroke="currentColor"
  795. class="w-4 h-4"
  796. >
  797. <path
  798. stroke-linecap="round"
  799. stroke-linejoin="round"
  800. 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"
  801. />
  802. </svg>
  803. </button>
  804. </Tooltip>
  805. {/if}
  806. {#if !readOnly}
  807. <Tooltip content={$i18n.t('Good Response')} placement="bottom">
  808. <button
  809. class="{isLastMessage
  810. ? 'visible'
  811. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg {(message
  812. ?.annotation?.rating ?? null) === 1
  813. ? 'bg-gray-100 dark:bg-gray-800'
  814. : ''} dark:hover:text-white hover:text-black transition"
  815. on:click={() => {
  816. rateMessage(message.id, 1);
  817. showRateComment = true;
  818. window.setTimeout(() => {
  819. document
  820. .getElementById(`message-feedback-${message.id}`)
  821. ?.scrollIntoView();
  822. }, 0);
  823. }}
  824. >
  825. <svg
  826. stroke="currentColor"
  827. fill="none"
  828. stroke-width="2.3"
  829. viewBox="0 0 24 24"
  830. stroke-linecap="round"
  831. stroke-linejoin="round"
  832. class="w-4 h-4"
  833. xmlns="http://www.w3.org/2000/svg"
  834. ><path
  835. 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"
  836. /></svg
  837. >
  838. </button>
  839. </Tooltip>
  840. <Tooltip content={$i18n.t('Bad Response')} placement="bottom">
  841. <button
  842. class="{isLastMessage
  843. ? 'visible'
  844. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg {(message
  845. ?.annotation?.rating ?? null) === -1
  846. ? 'bg-gray-100 dark:bg-gray-800'
  847. : ''} dark:hover:text-white hover:text-black transition"
  848. on:click={() => {
  849. rateMessage(message.id, -1);
  850. showRateComment = true;
  851. window.setTimeout(() => {
  852. document
  853. .getElementById(`message-feedback-${message.id}`)
  854. ?.scrollIntoView();
  855. }, 0);
  856. }}
  857. >
  858. <svg
  859. stroke="currentColor"
  860. fill="none"
  861. stroke-width="2.3"
  862. viewBox="0 0 24 24"
  863. stroke-linecap="round"
  864. stroke-linejoin="round"
  865. class="w-4 h-4"
  866. xmlns="http://www.w3.org/2000/svg"
  867. ><path
  868. 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"
  869. /></svg
  870. >
  871. </button>
  872. </Tooltip>
  873. {#if isLastMessage}
  874. <Tooltip content={$i18n.t('Continue Response')} placement="bottom">
  875. <button
  876. type="button"
  877. class="{isLastMessage
  878. ? 'visible'
  879. : '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"
  880. on:click={() => {
  881. continueGeneration();
  882. }}
  883. >
  884. <svg
  885. xmlns="http://www.w3.org/2000/svg"
  886. fill="none"
  887. viewBox="0 0 24 24"
  888. stroke-width="2.3"
  889. stroke="currentColor"
  890. class="w-4 h-4"
  891. >
  892. <path
  893. stroke-linecap="round"
  894. stroke-linejoin="round"
  895. d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
  896. />
  897. <path
  898. stroke-linecap="round"
  899. stroke-linejoin="round"
  900. 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"
  901. />
  902. </svg>
  903. </button>
  904. </Tooltip>
  905. <Tooltip content={$i18n.t('Regenerate')} placement="bottom">
  906. <button
  907. type="button"
  908. class="{isLastMessage
  909. ? 'visible'
  910. : '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"
  911. on:click={() => {
  912. showRateComment = false;
  913. regenerateResponse(message);
  914. }}
  915. >
  916. <svg
  917. xmlns="http://www.w3.org/2000/svg"
  918. fill="none"
  919. viewBox="0 0 24 24"
  920. stroke-width="2.3"
  921. stroke="currentColor"
  922. class="w-4 h-4"
  923. >
  924. <path
  925. stroke-linecap="round"
  926. stroke-linejoin="round"
  927. 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"
  928. />
  929. </svg>
  930. </button>
  931. </Tooltip>
  932. {#each model?.actions ?? [] as action}
  933. <Tooltip content={action.name} placement="bottom">
  934. <button
  935. type="button"
  936. class="{isLastMessage
  937. ? 'visible'
  938. : '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"
  939. on:click={() => {
  940. dispatch('action', action.id);
  941. }}
  942. >
  943. {#if action.icon_url}
  944. <img
  945. src={action.icon_url}
  946. class="w-4 h-4 {action.icon_url.includes('svg')
  947. ? 'dark:invert-[80%]'
  948. : ''}"
  949. style="fill: currentColor;"
  950. alt={action.name}
  951. />
  952. {:else}
  953. <Sparkles strokeWidth="2.1" className="size-4" />
  954. {/if}
  955. </button>
  956. </Tooltip>
  957. {/each}
  958. {/if}
  959. {/if}
  960. {/if}
  961. </div>
  962. {/if}
  963. {#if message.done && showRateComment}
  964. <RateComment
  965. messageId={message.id}
  966. bind:show={showRateComment}
  967. bind:message
  968. on:submit={() => {
  969. updateChatMessages();
  970. }}
  971. />
  972. {/if}
  973. </div>
  974. {/if}
  975. </div>
  976. </div>
  977. </div>
  978. </div>
  979. {/key}
  980. <style>
  981. .buttons::-webkit-scrollbar {
  982. display: none; /* for Chrome, Safari and Opera */
  983. }
  984. .buttons {
  985. -ms-overflow-style: none; /* IE and Edge */
  986. scrollbar-width: none; /* Firefox */
  987. }
  988. </style>