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. $settings?.audio?.model
  193. ).catch((error) => {
  194. toast.error(error);
  195. speaking = null;
  196. loadingSpeech = false;
  197. return null;
  198. });
  199. if (res) {
  200. const blob = await res.blob();
  201. const blobUrl = URL.createObjectURL(blob);
  202. const audio = new Audio(blobUrl);
  203. sentencesAudio[idx] = audio;
  204. loadingSpeech = false;
  205. lastPlayedAudioPromise = lastPlayedAudioPromise.then(() => playAudio(idx));
  206. }
  207. }
  208. } else {
  209. let voices = [];
  210. const getVoicesLoop = setInterval(async () => {
  211. voices = await speechSynthesis.getVoices();
  212. if (voices.length > 0) {
  213. clearInterval(getVoicesLoop);
  214. const voice =
  215. voices?.filter((v) => v.name === $settings?.audio?.speaker)?.at(0) ?? undefined;
  216. const speak = new SpeechSynthesisUtterance(message.content);
  217. speak.onend = () => {
  218. speaking = null;
  219. if ($settings.conversationMode) {
  220. document.getElementById('voice-input-button')?.click();
  221. }
  222. };
  223. speak.voice = voice;
  224. speechSynthesis.speak(speak);
  225. }
  226. }, 100);
  227. }
  228. }
  229. };
  230. const editMessageHandler = async () => {
  231. edit = true;
  232. editedContent = message.content;
  233. await tick();
  234. editTextAreaElement.style.height = '';
  235. editTextAreaElement.style.height = `${editTextAreaElement.scrollHeight}px`;
  236. };
  237. const editMessageConfirmHandler = async () => {
  238. if (editedContent === '') {
  239. editedContent = ' ';
  240. }
  241. confirmEditResponseMessage(message.id, editedContent);
  242. edit = false;
  243. editedContent = '';
  244. await tick();
  245. renderStyling();
  246. };
  247. const cancelEditMessage = async () => {
  248. edit = false;
  249. editedContent = '';
  250. await tick();
  251. renderStyling();
  252. };
  253. const generateImage = async (message) => {
  254. generatingImage = true;
  255. const res = await imageGenerations(localStorage.token, message.content).catch((error) => {
  256. toast.error(error);
  257. });
  258. console.log(res);
  259. if (res) {
  260. message.files = res.map((image) => ({
  261. type: 'image',
  262. url: `${image.url}`
  263. }));
  264. dispatch('save', message);
  265. }
  266. generatingImage = false;
  267. };
  268. onMount(async () => {
  269. await tick();
  270. renderStyling();
  271. });
  272. </script>
  273. <CitationsModal bind:show={showCitationModal} citation={selectedCitation} />
  274. {#key message.id}
  275. <div class=" flex w-full message-{message.id}" id="message-{message.id}">
  276. <ProfileImage
  277. src={modelfiles[message.model]?.imageUrl ??
  278. ($i18n.language === 'dg-DG' ? `/doge.png` : `${WEBUI_BASE_URL}/static/favicon.png`)}
  279. />
  280. <div class="w-full overflow-hidden">
  281. <Name>
  282. {#if message.model in modelfiles}
  283. {modelfiles[message.model]?.title}
  284. {:else}
  285. {message.model ? ` ${message.model}` : ''}
  286. {/if}
  287. {#if message.timestamp}
  288. <span class=" invisible group-hover:visible text-gray-400 text-xs font-medium">
  289. {dayjs(message.timestamp * 1000).format($i18n.t('DD/MM/YYYY HH:mm'))}
  290. </span>
  291. {/if}
  292. </Name>
  293. {#if message.files}
  294. <div class="my-2.5 w-full flex overflow-x-auto gap-2 flex-wrap">
  295. {#each message.files as file}
  296. <div>
  297. {#if file.type === 'image'}
  298. <Image src={file.url} />
  299. {/if}
  300. </div>
  301. {/each}
  302. </div>
  303. {/if}
  304. <div
  305. 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"
  306. >
  307. <div>
  308. {#if edit === true}
  309. <div class=" w-full">
  310. <textarea
  311. id="message-edit-{message.id}"
  312. bind:this={editTextAreaElement}
  313. class=" bg-transparent outline-none w-full resize-none"
  314. bind:value={editedContent}
  315. on:input={(e) => {
  316. e.target.style.height = '';
  317. e.target.style.height = `${e.target.scrollHeight}px`;
  318. }}
  319. />
  320. <div class=" mt-2 mb-1 flex justify-center space-x-2 text-sm font-medium">
  321. <button
  322. class="px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  323. on:click={() => {
  324. editMessageConfirmHandler();
  325. }}
  326. >
  327. {$i18n.t('Save')}
  328. </button>
  329. <button
  330. 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"
  331. on:click={() => {
  332. cancelEditMessage();
  333. }}
  334. >
  335. {$i18n.t('Cancel')}
  336. </button>
  337. </div>
  338. </div>
  339. {:else}
  340. <div class="w-full">
  341. {#if message?.error === true}
  342. <div
  343. 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"
  344. >
  345. <svg
  346. xmlns="http://www.w3.org/2000/svg"
  347. fill="none"
  348. viewBox="0 0 24 24"
  349. stroke-width="1.5"
  350. stroke="currentColor"
  351. class="w-5 h-5 self-center"
  352. >
  353. <path
  354. stroke-linecap="round"
  355. stroke-linejoin="round"
  356. d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z"
  357. />
  358. </svg>
  359. <div class=" self-center">
  360. {message.content}
  361. </div>
  362. </div>
  363. {:else if message.content === ''}
  364. <Skeleton />
  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. {/if}
  382. {#if message.citations}
  383. <div class="mt-1 mb-2 w-full flex gap-1 items-center">
  384. {#each message.citations.reduce((acc, citation) => {
  385. citation.document.forEach((document, index) => {
  386. const metadata = citation.metadata?.[index];
  387. const id = metadata?.source ?? 'N/A';
  388. const existingSource = acc.find((item) => item.id === id);
  389. if (existingSource) {
  390. existingSource.document.push(document);
  391. existingSource.metadata.push(metadata);
  392. } else {
  393. acc.push( { id: id, source: citation?.source, document: [document], metadata: metadata ? [metadata] : [] } );
  394. }
  395. });
  396. return acc;
  397. }, []) as citation, idx}
  398. <div class="flex gap-1 text-xs font-semibold">
  399. <button
  400. 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"
  401. on:click={() => {
  402. showCitationModal = true;
  403. selectedCitation = citation;
  404. }}
  405. >
  406. <div class="bg-white dark:bg-gray-700 rounded-full size-4">
  407. {idx + 1}
  408. </div>
  409. <div class="flex-1 mx-2 line-clamp-1">
  410. {citation.source.name}
  411. </div>
  412. </button>
  413. </div>
  414. {/each}
  415. </div>
  416. {/if}
  417. {#if message.done || siblings.length > 1}
  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 message.done}
  467. {#if !readOnly}
  468. <Tooltip content={$i18n.t('Edit')} placement="bottom">
  469. <button
  470. class="{isLastMessage
  471. ? 'visible'
  472. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition"
  473. on:click={() => {
  474. editMessageHandler();
  475. }}
  476. >
  477. <svg
  478. xmlns="http://www.w3.org/2000/svg"
  479. fill="none"
  480. viewBox="0 0 24 24"
  481. stroke-width="2"
  482. stroke="currentColor"
  483. class="w-4 h-4"
  484. >
  485. <path
  486. stroke-linecap="round"
  487. stroke-linejoin="round"
  488. 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"
  489. />
  490. </svg>
  491. </button>
  492. </Tooltip>
  493. {/if}
  494. <Tooltip content={$i18n.t('Copy')} placement="bottom">
  495. <button
  496. class="{isLastMessage
  497. ? 'visible'
  498. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition copy-response-button"
  499. on:click={() => {
  500. copyToClipboard(message.content);
  501. }}
  502. >
  503. <svg
  504. xmlns="http://www.w3.org/2000/svg"
  505. fill="none"
  506. viewBox="0 0 24 24"
  507. stroke-width="2"
  508. stroke="currentColor"
  509. class="w-4 h-4"
  510. >
  511. <path
  512. stroke-linecap="round"
  513. stroke-linejoin="round"
  514. 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"
  515. />
  516. </svg>
  517. </button>
  518. </Tooltip>
  519. {#if !readOnly}
  520. <Tooltip content={$i18n.t('Good Response')} placement="bottom">
  521. <button
  522. class="{isLastMessage
  523. ? 'visible'
  524. : 'invisible group-hover:visible'} p-1 rounded {message?.annotation
  525. ?.rating === 1
  526. ? 'bg-gray-100 dark:bg-gray-800'
  527. : ''} dark:hover:text-white hover:text-black transition"
  528. on:click={() => {
  529. rateMessage(message.id, 1);
  530. showRateComment = true;
  531. window.setTimeout(() => {
  532. document
  533. .getElementById(`message-feedback-${message.id}`)
  534. ?.scrollIntoView();
  535. }, 0);
  536. }}
  537. >
  538. <svg
  539. stroke="currentColor"
  540. fill="none"
  541. stroke-width="2"
  542. viewBox="0 0 24 24"
  543. stroke-linecap="round"
  544. stroke-linejoin="round"
  545. class="w-4 h-4"
  546. xmlns="http://www.w3.org/2000/svg"
  547. ><path
  548. 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"
  549. /></svg
  550. >
  551. </button>
  552. </Tooltip>
  553. <Tooltip content={$i18n.t('Bad Response')} placement="bottom">
  554. <button
  555. class="{isLastMessage
  556. ? 'visible'
  557. : 'invisible group-hover:visible'} p-1 rounded {message?.annotation
  558. ?.rating === -1
  559. ? 'bg-gray-100 dark:bg-gray-800'
  560. : ''} dark:hover:text-white hover:text-black transition"
  561. on:click={() => {
  562. rateMessage(message.id, -1);
  563. showRateComment = true;
  564. window.setTimeout(() => {
  565. document
  566. .getElementById(`message-feedback-${message.id}`)
  567. ?.scrollIntoView();
  568. }, 0);
  569. }}
  570. >
  571. <svg
  572. stroke="currentColor"
  573. fill="none"
  574. stroke-width="2"
  575. viewBox="0 0 24 24"
  576. stroke-linecap="round"
  577. stroke-linejoin="round"
  578. class="w-4 h-4"
  579. xmlns="http://www.w3.org/2000/svg"
  580. ><path
  581. 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"
  582. /></svg
  583. >
  584. </button>
  585. </Tooltip>
  586. {/if}
  587. <Tooltip content={$i18n.t('Read Aloud')} placement="bottom">
  588. <button
  589. id="speak-button-{message.id}"
  590. class="{isLastMessage
  591. ? 'visible'
  592. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition"
  593. on:click={() => {
  594. if (!loadingSpeech) {
  595. toggleSpeakMessage(message);
  596. }
  597. }}
  598. >
  599. {#if loadingSpeech}
  600. <svg
  601. class=" w-4 h-4"
  602. fill="currentColor"
  603. viewBox="0 0 24 24"
  604. xmlns="http://www.w3.org/2000/svg"
  605. ><style>
  606. .spinner_S1WN {
  607. animation: spinner_MGfb 0.8s linear infinite;
  608. animation-delay: -0.8s;
  609. }
  610. .spinner_Km9P {
  611. animation-delay: -0.65s;
  612. }
  613. .spinner_JApP {
  614. animation-delay: -0.5s;
  615. }
  616. @keyframes spinner_MGfb {
  617. 93.75%,
  618. 100% {
  619. opacity: 0.2;
  620. }
  621. }
  622. </style><circle class="spinner_S1WN" cx="4" cy="12" r="3" /><circle
  623. class="spinner_S1WN spinner_Km9P"
  624. cx="12"
  625. cy="12"
  626. r="3"
  627. /><circle
  628. class="spinner_S1WN spinner_JApP"
  629. cx="20"
  630. cy="12"
  631. r="3"
  632. /></svg
  633. >
  634. {:else if speaking}
  635. <svg
  636. xmlns="http://www.w3.org/2000/svg"
  637. fill="none"
  638. viewBox="0 0 24 24"
  639. stroke-width="2"
  640. stroke="currentColor"
  641. class="w-4 h-4"
  642. >
  643. <path
  644. stroke-linecap="round"
  645. stroke-linejoin="round"
  646. 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"
  647. />
  648. </svg>
  649. {:else}
  650. <svg
  651. xmlns="http://www.w3.org/2000/svg"
  652. fill="none"
  653. viewBox="0 0 24 24"
  654. stroke-width="2"
  655. stroke="currentColor"
  656. class="w-4 h-4"
  657. >
  658. <path
  659. stroke-linecap="round"
  660. stroke-linejoin="round"
  661. 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"
  662. />
  663. </svg>
  664. {/if}
  665. </button>
  666. </Tooltip>
  667. {#if $config.images && !readOnly}
  668. <Tooltip content="Generate Image" placement="bottom">
  669. <button
  670. class="{isLastMessage
  671. ? 'visible'
  672. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition"
  673. on:click={() => {
  674. if (!generatingImage) {
  675. generateImage(message);
  676. }
  677. }}
  678. >
  679. {#if generatingImage}
  680. <svg
  681. class=" w-4 h-4"
  682. fill="currentColor"
  683. viewBox="0 0 24 24"
  684. xmlns="http://www.w3.org/2000/svg"
  685. ><style>
  686. .spinner_S1WN {
  687. animation: spinner_MGfb 0.8s linear infinite;
  688. animation-delay: -0.8s;
  689. }
  690. .spinner_Km9P {
  691. animation-delay: -0.65s;
  692. }
  693. .spinner_JApP {
  694. animation-delay: -0.5s;
  695. }
  696. @keyframes spinner_MGfb {
  697. 93.75%,
  698. 100% {
  699. opacity: 0.2;
  700. }
  701. }
  702. </style><circle class="spinner_S1WN" cx="4" cy="12" r="3" /><circle
  703. class="spinner_S1WN spinner_Km9P"
  704. cx="12"
  705. cy="12"
  706. r="3"
  707. /><circle
  708. class="spinner_S1WN spinner_JApP"
  709. cx="20"
  710. cy="12"
  711. r="3"
  712. /></svg
  713. >
  714. {:else}
  715. <svg
  716. xmlns="http://www.w3.org/2000/svg"
  717. fill="none"
  718. viewBox="0 0 24 24"
  719. stroke-width="2"
  720. stroke="currentColor"
  721. class="w-4 h-4"
  722. >
  723. <path
  724. stroke-linecap="round"
  725. stroke-linejoin="round"
  726. d="m2.25 15.75 5.159-5.159a2.25 2.25 0 0 1 3.182 0l5.159 5.159m-1.5-1.5 1.409-1.409a2.25 2.25 0 0 1 3.182 0l2.909 2.909m-18 3.75h16.5a1.5 1.5 0 0 0 1.5-1.5V6a1.5 1.5 0 0 0-1.5-1.5H3.75A1.5 1.5 0 0 0 2.25 6v12a1.5 1.5 0 0 0 1.5 1.5Zm10.5-11.25h.008v.008h-.008V8.25Zm.375 0a.375.375 0 1 1-.75 0 .375.375 0 0 1 .75 0Z"
  727. />
  728. </svg>
  729. {/if}
  730. </button>
  731. </Tooltip>
  732. {/if}
  733. {#if message.info}
  734. <Tooltip content={$i18n.t('Generation Info')} placement="bottom">
  735. <button
  736. class=" {isLastMessage
  737. ? 'visible'
  738. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition whitespace-pre-wrap"
  739. on:click={() => {
  740. console.log(message);
  741. }}
  742. id="info-{message.id}"
  743. >
  744. <svg
  745. xmlns="http://www.w3.org/2000/svg"
  746. fill="none"
  747. viewBox="0 0 24 24"
  748. stroke-width="2"
  749. stroke="currentColor"
  750. class="w-4 h-4"
  751. >
  752. <path
  753. stroke-linecap="round"
  754. stroke-linejoin="round"
  755. d="M11.25 11.25l.041-.02a.75.75 0 011.063.852l-.708 2.836a.75.75 0 001.063.853l.041-.021M21 12a9 9 0 11-18 0 9 9 0 0118 0zm-9-3.75h.008v.008H12V8.25z"
  756. />
  757. </svg>
  758. </button>
  759. </Tooltip>
  760. {/if}
  761. {#if isLastMessage && !readOnly}
  762. <Tooltip content={$i18n.t('Continue Response')} placement="bottom">
  763. <button
  764. type="button"
  765. class="{isLastMessage
  766. ? 'visible'
  767. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition regenerate-response-button"
  768. on:click={() => {
  769. continueGeneration();
  770. }}
  771. >
  772. <svg
  773. xmlns="http://www.w3.org/2000/svg"
  774. fill="none"
  775. viewBox="0 0 24 24"
  776. stroke-width="2"
  777. stroke="currentColor"
  778. class="w-4 h-4"
  779. >
  780. <path
  781. stroke-linecap="round"
  782. stroke-linejoin="round"
  783. d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
  784. />
  785. <path
  786. stroke-linecap="round"
  787. stroke-linejoin="round"
  788. d="M15.91 11.672a.375.375 0 0 1 0 .656l-5.603 3.113a.375.375 0 0 1-.557-.328V8.887c0-.286.307-.466.557-.327l5.603 3.112Z"
  789. />
  790. </svg>
  791. </button>
  792. </Tooltip>
  793. <Tooltip content={$i18n.t('Regenerate')} placement="bottom">
  794. <button
  795. type="button"
  796. class="{isLastMessage
  797. ? 'visible'
  798. : 'invisible group-hover:visible'} p-1 rounded dark:hover:text-white hover:text-black transition regenerate-response-button"
  799. on:click={regenerateResponse}
  800. >
  801. <svg
  802. xmlns="http://www.w3.org/2000/svg"
  803. fill="none"
  804. viewBox="0 0 24 24"
  805. stroke-width="2"
  806. stroke="currentColor"
  807. class="w-4 h-4"
  808. >
  809. <path
  810. stroke-linecap="round"
  811. stroke-linejoin="round"
  812. d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99"
  813. />
  814. </svg>
  815. </button>
  816. </Tooltip>
  817. {/if}
  818. {/if}
  819. </div>
  820. {/if}
  821. {#if showRateComment}
  822. <RateComment
  823. messageId={message.id}
  824. bind:show={showRateComment}
  825. bind:message
  826. on:submit={() => {
  827. updateChatMessages();
  828. }}
  829. />
  830. {/if}
  831. </div>
  832. {/if}
  833. </div>
  834. </div>
  835. </div>
  836. </div>
  837. {/key}
  838. <style>
  839. .buttons::-webkit-scrollbar {
  840. display: none; /* for Chrome, Safari and Opera */
  841. }
  842. .buttons {
  843. -ms-overflow-style: none; /* IE and Edge */
  844. scrollbar-width: none; /* Firefox */
  845. }
  846. </style>