ResponseMessage.svelte 30 KB

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