ResponseMessage.svelte 32 KB

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