ResponseMessage.svelte 29 KB

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