ResponseMessage.svelte 32 KB

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