ResponseMessage.svelte 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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 { fade } from 'svelte/transition';
  9. import { createEventDispatcher } from 'svelte';
  10. import { onMount, tick, getContext } from 'svelte';
  11. const i18n = getContext('i18n');
  12. const dispatch = createEventDispatcher();
  13. import { config, settings } from '$lib/stores';
  14. import { synthesizeOpenAISpeech } from '$lib/apis/audio';
  15. import { imageGenerations } from '$lib/apis/images';
  16. import {
  17. approximateToHumanReadable,
  18. extractSentences,
  19. revertSanitizedResponseContent,
  20. sanitizeResponseContent
  21. } from '$lib/utils';
  22. import Name from './Name.svelte';
  23. import ProfileImage from './ProfileImage.svelte';
  24. import Skeleton from './Skeleton.svelte';
  25. import CodeBlock from './CodeBlock.svelte';
  26. import Image from '$lib/components/common/Image.svelte';
  27. import { WEBUI_BASE_URL } from '$lib/constants';
  28. import Tooltip from '$lib/components/common/Tooltip.svelte';
  29. import RateComment from './RateComment.svelte';
  30. import CitationsModal from '$lib/components/chat/Messages/CitationsModal.svelte';
  31. export let modelfiles = [];
  32. export let message;
  33. export let siblings;
  34. export let isLastMessage = true;
  35. export let readOnly = false;
  36. export let updateChatMessages: Function;
  37. export let confirmEditResponseMessage: Function;
  38. export let showPreviousMessage: Function;
  39. export let showNextMessage: Function;
  40. export let rateMessage: Function;
  41. export let copyToClipboard: Function;
  42. export let continueGeneration: Function;
  43. export let regenerateResponse: Function;
  44. let edit = false;
  45. let editedContent = '';
  46. let editTextAreaElement: HTMLTextAreaElement;
  47. let tooltipInstance = null;
  48. let sentencesAudio = {};
  49. let speaking = null;
  50. let speakingIdx = null;
  51. let loadingSpeech = false;
  52. let generatingImage = false;
  53. let showRateComment = false;
  54. let showCitations = {};
  55. // Backend returns a list of citations per collection, we flatten it to citations per source
  56. let flattenedCitations = {};
  57. $: tokens = marked.lexer(sanitizeResponseContent(message.content));
  58. const renderer = new marked.Renderer();
  59. // For code blocks with simple backticks
  60. renderer.codespan = (code) => {
  61. return `<code>${code.replaceAll('&amp;', '&')}</code>`;
  62. };
  63. const { extensions, ...defaults } = marked.getDefaults() as marked.MarkedOptions & {
  64. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  65. extensions: any;
  66. };
  67. $: if (message) {
  68. renderStyling();
  69. }
  70. const renderStyling = async () => {
  71. await tick();
  72. if (tooltipInstance) {
  73. tooltipInstance[0]?.destroy();
  74. }
  75. renderLatex();
  76. if (message.info) {
  77. tooltipInstance = tippy(`#info-${message.id}`, {
  78. content: `<span class="text-xs" id="tooltip-${message.id}">response_token/s: ${
  79. `${
  80. Math.round(
  81. ((message.info.eval_count ?? 0) / (message.info.eval_duration / 1000000000)) * 100
  82. ) / 100
  83. } tokens` ?? 'N/A'
  84. }<br/>
  85. prompt_token/s: ${
  86. Math.round(
  87. ((message.info.prompt_eval_count ?? 0) /
  88. (message.info.prompt_eval_duration / 1000000000)) *
  89. 100
  90. ) / 100 ?? 'N/A'
  91. } tokens<br/>
  92. total_duration: ${
  93. Math.round(((message.info.total_duration ?? 0) / 1000000) * 100) / 100 ??
  94. 'N/A'
  95. }ms<br/>
  96. load_duration: ${
  97. Math.round(((message.info.load_duration ?? 0) / 1000000) * 100) / 100 ?? 'N/A'
  98. }ms<br/>
  99. prompt_eval_count: ${message.info.prompt_eval_count ?? 'N/A'}<br/>
  100. prompt_eval_duration: ${
  101. Math.round(((message.info.prompt_eval_duration ?? 0) / 1000000) * 100) /
  102. 100 ?? 'N/A'
  103. }ms<br/>
  104. eval_count: ${message.info.eval_count ?? 'N/A'}<br/>
  105. eval_duration: ${
  106. Math.round(((message.info.eval_duration ?? 0) / 1000000) * 100) / 100 ?? 'N/A'
  107. }ms<br/>
  108. approximate_total: ${approximateToHumanReadable(
  109. message.info.total_duration
  110. )}</span>`,
  111. allowHTML: true
  112. });
  113. }
  114. if (message.citations) {
  115. for (const citation of message.citations) {
  116. const zipped = (citation?.document ?? []).map(function (document, index) {
  117. return [document, citation.metadata?.[index]];
  118. });
  119. for (const [document, metadata] of zipped) {
  120. const source = metadata?.source ?? 'N/A';
  121. if (source in flattenedCitations) {
  122. flattenedCitations[source].document.push(document);
  123. flattenedCitations[source].metadata.push(metadata);
  124. } else {
  125. flattenedCitations[source] = {
  126. document: [document],
  127. metadata: [metadata]
  128. };
  129. }
  130. }
  131. }
  132. console.log(flattenedCitations);
  133. console.log(Object.keys(flattenedCitations));
  134. }
  135. };
  136. const renderLatex = () => {
  137. let chatMessageElements = document
  138. .getElementById(`message-${message.id}`)
  139. ?.getElementsByClassName('chat-assistant');
  140. if (chatMessageElements) {
  141. for (const element of chatMessageElements) {
  142. auto_render(element, {
  143. // customised options
  144. // • auto-render specific keys, e.g.:
  145. delimiters: [
  146. { left: '$$', right: '$$', display: false },
  147. { left: '$ ', right: ' $', display: false },
  148. { left: '\\(', right: '\\)', display: false },
  149. { left: '\\[', right: '\\]', display: false },
  150. { left: '[ ', right: ' ]', display: false }
  151. ],
  152. // • rendering keys, e.g.:
  153. throwOnError: false
  154. });
  155. }
  156. }
  157. };
  158. const playAudio = (idx) => {
  159. return new Promise((res) => {
  160. speakingIdx = idx;
  161. const audio = sentencesAudio[idx];
  162. audio.play();
  163. audio.onended = async (e) => {
  164. await new Promise((r) => setTimeout(r, 300));
  165. if (Object.keys(sentencesAudio).length - 1 === idx) {
  166. speaking = null;
  167. if ($settings.conversationMode) {
  168. document.getElementById('voice-input-button')?.click();
  169. }
  170. }
  171. res(e);
  172. };
  173. });
  174. };
  175. const toggleSpeakMessage = async () => {
  176. if (speaking) {
  177. try {
  178. speechSynthesis.cancel();
  179. sentencesAudio[speakingIdx].pause();
  180. sentencesAudio[speakingIdx].currentTime = 0;
  181. } catch {}
  182. speaking = null;
  183. speakingIdx = null;
  184. } else {
  185. speaking = true;
  186. if ($settings?.audio?.TTSEngine === 'openai') {
  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. sentencesAudio = sentences.reduce((a, e, i, arr) => {
  205. a[i] = null;
  206. return a;
  207. }, {});
  208. let lastPlayedAudioPromise = Promise.resolve(); // Initialize a promise that resolves immediately
  209. for (const [idx, sentence] of sentences.entries()) {
  210. const res = await synthesizeOpenAISpeech(
  211. localStorage.token,
  212. $settings?.audio?.speaker,
  213. sentence
  214. ).catch((error) => {
  215. toast.error(error);
  216. speaking = null;
  217. loadingSpeech = false;
  218. return null;
  219. });
  220. if (res) {
  221. const blob = await res.blob();
  222. const blobUrl = URL.createObjectURL(blob);
  223. const audio = new Audio(blobUrl);
  224. sentencesAudio[idx] = audio;
  225. loadingSpeech = false;
  226. lastPlayedAudioPromise = lastPlayedAudioPromise.then(() => playAudio(idx));
  227. }
  228. }
  229. } else {
  230. let voices = [];
  231. const getVoicesLoop = setInterval(async () => {
  232. voices = await speechSynthesis.getVoices();
  233. if (voices.length > 0) {
  234. clearInterval(getVoicesLoop);
  235. const voice =
  236. voices?.filter((v) => v.name === $settings?.audio?.speaker)?.at(0) ?? undefined;
  237. const speak = new SpeechSynthesisUtterance(message.content);
  238. speak.onend = () => {
  239. speaking = null;
  240. if ($settings.conversationMode) {
  241. document.getElementById('voice-input-button')?.click();
  242. }
  243. };
  244. speak.voice = voice;
  245. speechSynthesis.speak(speak);
  246. }
  247. }, 100);
  248. }
  249. }
  250. };
  251. const editMessageHandler = async () => {
  252. edit = true;
  253. editedContent = message.content;
  254. await tick();
  255. editTextAreaElement.style.height = '';
  256. editTextAreaElement.style.height = `${editTextAreaElement.scrollHeight}px`;
  257. };
  258. const editMessageConfirmHandler = async () => {
  259. if (editedContent === '') {
  260. editedContent = ' ';
  261. }
  262. confirmEditResponseMessage(message.id, editedContent);
  263. edit = false;
  264. editedContent = '';
  265. await tick();
  266. renderStyling();
  267. };
  268. const cancelEditMessage = async () => {
  269. edit = false;
  270. editedContent = '';
  271. await tick();
  272. renderStyling();
  273. };
  274. const generateImage = async (message) => {
  275. generatingImage = true;
  276. const res = await imageGenerations(localStorage.token, message.content).catch((error) => {
  277. toast.error(error);
  278. });
  279. console.log(res);
  280. if (res) {
  281. message.files = res.map((image) => ({
  282. type: 'image',
  283. url: `${image.url}`
  284. }));
  285. dispatch('save', message);
  286. }
  287. generatingImage = false;
  288. };
  289. onMount(async () => {
  290. await tick();
  291. renderStyling();
  292. });
  293. </script>
  294. {#key message.id}
  295. <div class=" flex w-full message-{message.id}" id="message-{message.id}">
  296. <ProfileImage
  297. src={modelfiles[message.model]?.imageUrl ??
  298. ($i18n.language === 'dg-DG' ? `/doge.png` : `${WEBUI_BASE_URL}/static/favicon.png`)}
  299. />
  300. <div class="w-full overflow-hidden">
  301. <Name>
  302. {#if message.model in modelfiles}
  303. {modelfiles[message.model]?.title}
  304. {:else}
  305. {message.model ? ` ${message.model}` : ''}
  306. {/if}
  307. {#if message.timestamp}
  308. <span class=" invisible group-hover:visible text-gray-400 text-xs font-medium">
  309. {dayjs(message.timestamp * 1000).format($i18n.t('DD/MM/YYYY HH:mm'))}
  310. </span>
  311. {/if}
  312. </Name>
  313. {#if message.content === ''}
  314. <Skeleton />
  315. {:else}
  316. {#if message.files}
  317. <div class="my-2.5 w-full flex overflow-x-auto gap-2 flex-wrap">
  318. {#each message.files as file}
  319. <div>
  320. {#if file.type === 'image'}
  321. <Image src={file.url} />
  322. {/if}
  323. </div>
  324. {/each}
  325. </div>
  326. {/if}
  327. <div
  328. class="prose chat-{message.role} w-full max-w-full dark:prose-invert prose-headings:my-0 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"
  329. >
  330. <div>
  331. {#if edit === true}
  332. <div class=" w-full">
  333. <textarea
  334. id="message-edit-{message.id}"
  335. bind:this={editTextAreaElement}
  336. class=" bg-transparent outline-none w-full resize-none"
  337. bind:value={editedContent}
  338. on:input={(e) => {
  339. e.target.style.height = '';
  340. e.target.style.height = `${e.target.scrollHeight}px`;
  341. }}
  342. />
  343. <div class=" mt-2 mb-1 flex justify-center space-x-2 text-sm font-medium">
  344. <button
  345. class="px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  346. on:click={() => {
  347. editMessageConfirmHandler();
  348. }}
  349. >
  350. {$i18n.t('Save')}
  351. </button>
  352. <button
  353. class=" px-4 py-2 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-100 transition outline outline-1 outline-gray-200 dark:outline-gray-600 rounded-lg"
  354. on:click={() => {
  355. cancelEditMessage();
  356. }}
  357. >
  358. {$i18n.t('Cancel')}
  359. </button>
  360. </div>
  361. </div>
  362. {:else}
  363. <div class="w-full">
  364. {#if message?.error === true}
  365. <div
  366. 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"
  367. >
  368. <svg
  369. xmlns="http://www.w3.org/2000/svg"
  370. fill="none"
  371. viewBox="0 0 24 24"
  372. stroke-width="1.5"
  373. stroke="currentColor"
  374. class="w-5 h-5 self-center"
  375. >
  376. <path
  377. stroke-linecap="round"
  378. stroke-linejoin="round"
  379. d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z"
  380. />
  381. </svg>
  382. <div class=" self-center">
  383. {message.content}
  384. </div>
  385. </div>
  386. {:else}
  387. {#each tokens as token}
  388. {#if token.type === 'code'}
  389. <CodeBlock
  390. lang={token.lang}
  391. code={revertSanitizedResponseContent(token.text)}
  392. />
  393. {:else}
  394. {@html marked.parse(token.raw, {
  395. ...defaults,
  396. gfm: true,
  397. breaks: true,
  398. renderer
  399. })}
  400. {/if}
  401. {/each}
  402. <!-- {@html marked(message.content.replaceAll('\\', '\\\\'))} -->
  403. {/if}
  404. </div>
  405. {/if}
  406. </div>
  407. </div>
  408. {#if Object.keys(flattenedCitations).length > 0}
  409. <hr class=" dark:border-gray-800" />
  410. <div class="my-2.5 w-full flex overflow-x-auto gap-2 flex-wrap">
  411. {#each [...Object.keys(flattenedCitations)] as source, idx}
  412. <CitationsModal
  413. bind:show={showCitations[source]}
  414. citation={flattenedCitations[source]}
  415. />
  416. <div class="flex gap-1 text-xs font-semibold">
  417. <div>
  418. [{idx + 1}]
  419. </div>
  420. <button
  421. class="dark:text-gray-500 underline"
  422. on:click={() => {
  423. showCitations[source] = !showCitations[source];
  424. }}
  425. >
  426. {source}
  427. </button>
  428. </div>
  429. {/each}
  430. </div>
  431. {/if}
  432. {#if message.done}
  433. <div
  434. class=" flex justify-start space-x-1 overflow-x-auto buttons text-gray-700 dark:text-gray-500"
  435. >
  436. {#if siblings.length > 1}
  437. <div class="flex self-center min-w-fit">
  438. <button
  439. class="self-center dark:hover:text-white hover:text-black transition"
  440. on:click={() => {
  441. showPreviousMessage(message);
  442. }}
  443. >
  444. <svg
  445. xmlns="http://www.w3.org/2000/svg"
  446. viewBox="0 0 20 20"
  447. fill="currentColor"
  448. class="w-4 h-4"
  449. >
  450. <path
  451. fill-rule="evenodd"
  452. d="M12.79 5.23a.75.75 0 01-.02 1.06L8.832 10l3.938 3.71a.75.75 0 11-1.04 1.08l-4.5-4.25a.75.75 0 010-1.08l4.5-4.25a.75.75 0 011.06.02z"
  453. clip-rule="evenodd"
  454. />
  455. </svg>
  456. </button>
  457. <div class="text-xs font-bold self-center min-w-fit dark:text-gray-100">
  458. {siblings.indexOf(message.id) + 1} / {siblings.length}
  459. </div>
  460. <button
  461. class="self-center dark:hover:text-white hover:text-black transition"
  462. on:click={() => {
  463. showNextMessage(message);
  464. }}
  465. >
  466. <svg
  467. xmlns="http://www.w3.org/2000/svg"
  468. viewBox="0 0 20 20"
  469. fill="currentColor"
  470. class="w-4 h-4"
  471. >
  472. <path
  473. fill-rule="evenodd"
  474. d="M7.21 14.77a.75.75 0 01.02-1.06L11.168 10 7.23 6.29a.75.75 0 111.04-1.08l4.5 4.25a.75.75 0 010 1.08l-4.5 4.25a.75.75 0 01-1.06-.02z"
  475. clip-rule="evenodd"
  476. />
  477. </svg>
  478. </button>
  479. </div>
  480. {/if}
  481. {#if !readOnly}
  482. <Tooltip content={$i18n.t('Edit')} placement="bottom">
  483. <button
  484. class="{isLastMessage
  485. ? 'visible'
  486. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition"
  487. on:click={() => {
  488. editMessageHandler();
  489. }}
  490. >
  491. <svg
  492. xmlns="http://www.w3.org/2000/svg"
  493. fill="none"
  494. viewBox="0 0 24 24"
  495. stroke-width="2"
  496. stroke="currentColor"
  497. class="w-4 h-4"
  498. >
  499. <path
  500. stroke-linecap="round"
  501. stroke-linejoin="round"
  502. 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"
  503. />
  504. </svg>
  505. </button>
  506. </Tooltip>
  507. {/if}
  508. <Tooltip content={$i18n.t('Copy')} placement="bottom">
  509. <button
  510. class="{isLastMessage
  511. ? 'visible'
  512. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition copy-response-button"
  513. on:click={() => {
  514. copyToClipboard(message.content);
  515. }}
  516. >
  517. <svg
  518. xmlns="http://www.w3.org/2000/svg"
  519. fill="none"
  520. viewBox="0 0 24 24"
  521. stroke-width="2"
  522. stroke="currentColor"
  523. class="w-4 h-4"
  524. >
  525. <path
  526. stroke-linecap="round"
  527. stroke-linejoin="round"
  528. 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"
  529. />
  530. </svg>
  531. </button>
  532. </Tooltip>
  533. {#if !readOnly}
  534. <Tooltip content={$i18n.t('Good Response')} placement="bottom">
  535. <button
  536. class="{isLastMessage
  537. ? 'visible'
  538. : 'invisible group-hover:visible'} p-1 rounded {message?.annotation?.rating ===
  539. 1
  540. ? 'bg-gray-100 dark:bg-gray-800'
  541. : ''} dark:hover:text-white hover:text-black transition"
  542. on:click={() => {
  543. rateMessage(message.id, 1);
  544. showRateComment = true;
  545. window.setTimeout(() => {
  546. document.getElementById(`message-feedback-${message.id}`)?.scrollIntoView();
  547. }, 0);
  548. }}
  549. >
  550. <svg
  551. stroke="currentColor"
  552. fill="none"
  553. stroke-width="2"
  554. viewBox="0 0 24 24"
  555. stroke-linecap="round"
  556. stroke-linejoin="round"
  557. class="w-4 h-4"
  558. xmlns="http://www.w3.org/2000/svg"
  559. >
  560. <path
  561. 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"
  562. />
  563. </svg>
  564. </button>
  565. </Tooltip>
  566. <Tooltip content={$i18n.t('Bad Response')} placement="bottom">
  567. <button
  568. class="{isLastMessage
  569. ? 'visible'
  570. : 'invisible group-hover:visible'} p-1 rounded {message?.annotation?.rating ===
  571. -1
  572. ? 'bg-gray-100 dark:bg-gray-800'
  573. : ''} dark:hover:text-white hover:text-black transition"
  574. on:click={() => {
  575. rateMessage(message.id, -1);
  576. showRateComment = true;
  577. window.setTimeout(() => {
  578. document.getElementById(`message-feedback-${message.id}`)?.scrollIntoView();
  579. }, 0);
  580. }}
  581. >
  582. <svg
  583. stroke="currentColor"
  584. fill="none"
  585. stroke-width="2"
  586. viewBox="0 0 24 24"
  587. stroke-linecap="round"
  588. stroke-linejoin="round"
  589. class="w-4 h-4"
  590. xmlns="http://www.w3.org/2000/svg"
  591. >
  592. <path
  593. 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"
  594. />
  595. </svg>
  596. </button>
  597. </Tooltip>
  598. {/if}
  599. <Tooltip content={$i18n.t('Read Aloud')} placement="bottom">
  600. <button
  601. id="speak-button-{message.id}"
  602. class="{isLastMessage
  603. ? 'visible'
  604. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition"
  605. on:click={() => {
  606. if (!loadingSpeech) {
  607. toggleSpeakMessage(message);
  608. }
  609. }}
  610. >
  611. {#if loadingSpeech}
  612. <svg
  613. class=" w-4 h-4"
  614. fill="currentColor"
  615. viewBox="0 0 24 24"
  616. xmlns="http://www.w3.org/2000/svg"
  617. >
  618. <style>
  619. .spinner_S1WN {
  620. animation: spinner_MGfb 0.8s linear infinite;
  621. animation-delay: -0.8s;
  622. }
  623. .spinner_Km9P {
  624. animation-delay: -0.65s;
  625. }
  626. .spinner_JApP {
  627. animation-delay: -0.5s;
  628. }
  629. @keyframes spinner_MGfb {
  630. 93.75%,
  631. 100% {
  632. opacity: 0.2;
  633. }
  634. }
  635. </style>
  636. <circle class="spinner_S1WN" cx="4" cy="12" r="3" />
  637. <circle class="spinner_S1WN spinner_Km9P" cx="12" cy="12" r="3" />
  638. <circle class="spinner_S1WN spinner_JApP" cx="20" cy="12" r="3" />
  639. </svg>
  640. {:else if speaking}
  641. <svg
  642. xmlns="http://www.w3.org/2000/svg"
  643. fill="none"
  644. viewBox="0 0 24 24"
  645. stroke-width="2"
  646. stroke="currentColor"
  647. class="w-4 h-4"
  648. >
  649. <path
  650. stroke-linecap="round"
  651. stroke-linejoin="round"
  652. 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"
  653. />
  654. </svg>
  655. {:else}
  656. <svg
  657. xmlns="http://www.w3.org/2000/svg"
  658. fill="none"
  659. viewBox="0 0 24 24"
  660. stroke-width="2"
  661. stroke="currentColor"
  662. class="w-4 h-4"
  663. >
  664. <path
  665. stroke-linecap="round"
  666. stroke-linejoin="round"
  667. 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"
  668. />
  669. </svg>
  670. {/if}
  671. </button>
  672. </Tooltip>
  673. {#if $config.images && !readOnly}
  674. <Tooltip content="Generate Image" placement="bottom">
  675. <button
  676. class="{isLastMessage
  677. ? 'visible'
  678. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition"
  679. on:click={() => {
  680. if (!generatingImage) {
  681. generateImage(message);
  682. }
  683. }}
  684. >
  685. {#if generatingImage}
  686. <svg
  687. class=" w-4 h-4"
  688. fill="currentColor"
  689. viewBox="0 0 24 24"
  690. xmlns="http://www.w3.org/2000/svg"
  691. >
  692. <style>
  693. .spinner_S1WN {
  694. animation: spinner_MGfb 0.8s linear infinite;
  695. animation-delay: -0.8s;
  696. }
  697. .spinner_Km9P {
  698. animation-delay: -0.65s;
  699. }
  700. .spinner_JApP {
  701. animation-delay: -0.5s;
  702. }
  703. @keyframes spinner_MGfb {
  704. 93.75%,
  705. 100% {
  706. opacity: 0.2;
  707. }
  708. }
  709. </style>
  710. <circle class="spinner_S1WN" cx="4" cy="12" r="3" />
  711. <circle class="spinner_S1WN spinner_Km9P" cx="12" cy="12" r="3" />
  712. <circle class="spinner_S1WN spinner_JApP" cx="20" cy="12" r="3" />
  713. </svg>
  714. {:else}
  715. <svg
  716. xmlns="http://www.w3.org/2000/svg"
  717. fill="none"
  718. viewBox="0 0 24 24"
  719. stroke-width="2"
  720. stroke="currentColor"
  721. class="w-4 h-4"
  722. >
  723. <path
  724. stroke-linecap="round"
  725. stroke-linejoin="round"
  726. 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"
  727. />
  728. </svg>
  729. {/if}
  730. </button>
  731. </Tooltip>
  732. {/if}
  733. {#if message.info}
  734. <Tooltip content={$i18n.t('Generation Info')} placement="bottom">
  735. <button
  736. class=" {isLastMessage
  737. ? 'visible'
  738. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition whitespace-pre-wrap"
  739. on:click={() => {
  740. console.log(message);
  741. }}
  742. id="info-{message.id}"
  743. >
  744. <svg
  745. xmlns="http://www.w3.org/2000/svg"
  746. fill="none"
  747. viewBox="0 0 24 24"
  748. stroke-width="2"
  749. stroke="currentColor"
  750. class="w-4 h-4"
  751. >
  752. <path
  753. stroke-linecap="round"
  754. stroke-linejoin="round"
  755. 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"
  756. />
  757. </svg>
  758. </button>
  759. </Tooltip>
  760. {/if}
  761. {#if isLastMessage && !readOnly}
  762. <Tooltip content={$i18n.t('Continue Response')} placement="bottom">
  763. <button
  764. type="button"
  765. class="{isLastMessage
  766. ? 'visible'
  767. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition regenerate-response-button"
  768. on:click={() => {
  769. continueGeneration();
  770. }}
  771. >
  772. <svg
  773. xmlns="http://www.w3.org/2000/svg"
  774. fill="none"
  775. viewBox="0 0 24 24"
  776. stroke-width="2"
  777. stroke="currentColor"
  778. class="w-4 h-4"
  779. >
  780. <path
  781. stroke-linecap="round"
  782. stroke-linejoin="round"
  783. d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
  784. />
  785. <path
  786. stroke-linecap="round"
  787. stroke-linejoin="round"
  788. 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"
  789. />
  790. </svg>
  791. </button>
  792. </Tooltip>
  793. <Tooltip content={$i18n.t('Regenerate')} placement="bottom">
  794. <button
  795. type="button"
  796. class="{isLastMessage
  797. ? 'visible'
  798. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition regenerate-response-button"
  799. on:click={regenerateResponse}
  800. >
  801. <svg
  802. xmlns="http://www.w3.org/2000/svg"
  803. fill="none"
  804. viewBox="0 0 24 24"
  805. stroke-width="2"
  806. stroke="currentColor"
  807. class="w-4 h-4"
  808. >
  809. <path
  810. stroke-linecap="round"
  811. stroke-linejoin="round"
  812. 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"
  813. />
  814. </svg>
  815. </button>
  816. </Tooltip>
  817. {/if}
  818. </div>
  819. {/if}
  820. {#if showRateComment}
  821. <RateComment
  822. messageId={message.id}
  823. bind:show={showRateComment}
  824. bind:message
  825. on:submit={() => {
  826. updateChatMessages();
  827. }}
  828. />
  829. {/if}
  830. {/if}
  831. </div>
  832. </div>
  833. {/key}
  834. <style>
  835. .buttons::-webkit-scrollbar {
  836. display: none; /* for Chrome, Safari and Opera */
  837. }
  838. .buttons {
  839. -ms-overflow-style: none; /* IE and Edge */
  840. scrollbar-width: none; /* Firefox */
  841. }
  842. </style>