ResponseMessage.svelte 31 KB

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