ResponseMessage.svelte 27 KB

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