ResponseMessage.svelte 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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-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?.error === true}
  385. <div
  386. 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"
  387. >
  388. <svg
  389. xmlns="http://www.w3.org/2000/svg"
  390. fill="none"
  391. viewBox="0 0 24 24"
  392. stroke-width="1.5"
  393. stroke="currentColor"
  394. class="w-5 h-5 self-center"
  395. >
  396. <path
  397. stroke-linecap="round"
  398. stroke-linejoin="round"
  399. d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z"
  400. />
  401. </svg>
  402. <div class=" self-center">
  403. {message.content}
  404. </div>
  405. </div>
  406. {:else if message.content === ''}
  407. <Skeleton />
  408. {:else}
  409. {#each tokens as token, tokenIdx}
  410. {#if token.type === 'code'}
  411. <CodeBlock
  412. id={`${message.id}-${tokenIdx}`}
  413. lang={token?.lang ?? ''}
  414. code={revertSanitizedResponseContent(token?.text ?? '')}
  415. />
  416. {:else}
  417. {@html marked.parse(token.raw, {
  418. ...defaults,
  419. gfm: true,
  420. breaks: true,
  421. renderer
  422. })}
  423. {/if}
  424. {/each}
  425. {/if}
  426. {#if message.citations}
  427. <div class="mt-1 mb-2 w-full flex gap-1 items-center flex-wrap">
  428. {#each message.citations.reduce((acc, citation) => {
  429. citation.document.forEach((document, index) => {
  430. const metadata = citation.metadata?.[index];
  431. const id = metadata?.source ?? 'N/A';
  432. let source = citation?.source;
  433. // Check if ID looks like a URL
  434. if (id.startsWith('http://') || id.startsWith('https://')) {
  435. source = { name: id };
  436. }
  437. const existingSource = acc.find((item) => item.id === id);
  438. if (existingSource) {
  439. existingSource.document.push(document);
  440. existingSource.metadata.push(metadata);
  441. } else {
  442. acc.push( { id: id, source: source, document: [document], metadata: metadata ? [metadata] : [] } );
  443. }
  444. });
  445. return acc;
  446. }, []) as citation, idx}
  447. <div class="flex gap-1 text-xs font-semibold">
  448. <button
  449. 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"
  450. on:click={() => {
  451. showCitationModal = true;
  452. selectedCitation = citation;
  453. }}
  454. >
  455. <div class="bg-white dark:bg-gray-700 rounded-full size-4">
  456. {idx + 1}
  457. </div>
  458. <div class="flex-1 mx-2 line-clamp-1">
  459. {citation.source.name}
  460. </div>
  461. </button>
  462. </div>
  463. {/each}
  464. </div>
  465. {/if}
  466. {#if message.done || siblings.length > 1}
  467. <div
  468. class=" flex justify-start overflow-x-auto buttons text-gray-600 dark:text-gray-500"
  469. >
  470. {#if siblings.length > 1}
  471. <div class="flex self-center min-w-fit" dir="ltr">
  472. <button
  473. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  474. on:click={() => {
  475. showPreviousMessage(message);
  476. }}
  477. >
  478. <svg
  479. xmlns="http://www.w3.org/2000/svg"
  480. fill="none"
  481. viewBox="0 0 24 24"
  482. stroke="currentColor"
  483. stroke-width="2.5"
  484. class="size-3.5"
  485. >
  486. <path
  487. stroke-linecap="round"
  488. stroke-linejoin="round"
  489. d="M15.75 19.5 8.25 12l7.5-7.5"
  490. />
  491. </svg>
  492. </button>
  493. <div
  494. class="text-sm tracking-widest font-semibold self-center dark:text-gray-100 min-w-fit"
  495. >
  496. {siblings.indexOf(message.id) + 1}/{siblings.length}
  497. </div>
  498. <button
  499. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  500. on:click={() => {
  501. showNextMessage(message);
  502. }}
  503. >
  504. <svg
  505. xmlns="http://www.w3.org/2000/svg"
  506. fill="none"
  507. viewBox="0 0 24 24"
  508. stroke="currentColor"
  509. stroke-width="2.5"
  510. class="size-3.5"
  511. >
  512. <path
  513. stroke-linecap="round"
  514. stroke-linejoin="round"
  515. d="m8.25 4.5 7.5 7.5-7.5 7.5"
  516. />
  517. </svg>
  518. </button>
  519. </div>
  520. {/if}
  521. {#if message.done}
  522. {#if !readOnly}
  523. <Tooltip content={$i18n.t('Edit')} placement="bottom">
  524. <button
  525. class="{isLastMessage
  526. ? 'visible'
  527. : '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"
  528. on:click={() => {
  529. editMessageHandler();
  530. }}
  531. >
  532. <svg
  533. xmlns="http://www.w3.org/2000/svg"
  534. fill="none"
  535. viewBox="0 0 24 24"
  536. stroke-width="2.3"
  537. stroke="currentColor"
  538. class="w-4 h-4"
  539. >
  540. <path
  541. stroke-linecap="round"
  542. stroke-linejoin="round"
  543. 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"
  544. />
  545. </svg>
  546. </button>
  547. </Tooltip>
  548. {/if}
  549. <Tooltip content={$i18n.t('Copy')} placement="bottom">
  550. <button
  551. class="{isLastMessage
  552. ? 'visible'
  553. : '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"
  554. on:click={() => {
  555. copyToClipboard(message.content);
  556. }}
  557. >
  558. <svg
  559. xmlns="http://www.w3.org/2000/svg"
  560. fill="none"
  561. viewBox="0 0 24 24"
  562. stroke-width="2.3"
  563. stroke="currentColor"
  564. class="w-4 h-4"
  565. >
  566. <path
  567. stroke-linecap="round"
  568. stroke-linejoin="round"
  569. 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"
  570. />
  571. </svg>
  572. </button>
  573. </Tooltip>
  574. <Tooltip content={$i18n.t('Read Aloud')} placement="bottom">
  575. <button
  576. id="speak-button-{message.id}"
  577. class="{isLastMessage
  578. ? 'visible'
  579. : '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"
  580. on:click={() => {
  581. if (!loadingSpeech) {
  582. toggleSpeakMessage(message);
  583. }
  584. }}
  585. >
  586. {#if loadingSpeech}
  587. <svg
  588. class=" w-4 h-4"
  589. fill="currentColor"
  590. viewBox="0 0 24 24"
  591. xmlns="http://www.w3.org/2000/svg"
  592. ><style>
  593. .spinner_S1WN {
  594. animation: spinner_MGfb 0.8s linear infinite;
  595. animation-delay: -0.8s;
  596. }
  597. .spinner_Km9P {
  598. animation-delay: -0.65s;
  599. }
  600. .spinner_JApP {
  601. animation-delay: -0.5s;
  602. }
  603. @keyframes spinner_MGfb {
  604. 93.75%,
  605. 100% {
  606. opacity: 0.2;
  607. }
  608. }
  609. </style><circle class="spinner_S1WN" cx="4" cy="12" r="3" /><circle
  610. class="spinner_S1WN spinner_Km9P"
  611. cx="12"
  612. cy="12"
  613. r="3"
  614. /><circle
  615. class="spinner_S1WN spinner_JApP"
  616. cx="20"
  617. cy="12"
  618. r="3"
  619. /></svg
  620. >
  621. {:else if speaking}
  622. <svg
  623. xmlns="http://www.w3.org/2000/svg"
  624. fill="none"
  625. viewBox="0 0 24 24"
  626. stroke-width="2.3"
  627. stroke="currentColor"
  628. class="w-4 h-4"
  629. >
  630. <path
  631. stroke-linecap="round"
  632. stroke-linejoin="round"
  633. 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"
  634. />
  635. </svg>
  636. {:else}
  637. <svg
  638. xmlns="http://www.w3.org/2000/svg"
  639. fill="none"
  640. viewBox="0 0 24 24"
  641. stroke-width="2.3"
  642. stroke="currentColor"
  643. class="w-4 h-4"
  644. >
  645. <path
  646. stroke-linecap="round"
  647. stroke-linejoin="round"
  648. 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"
  649. />
  650. </svg>
  651. {/if}
  652. </button>
  653. </Tooltip>
  654. {#if $config?.features.enable_image_generation && !readOnly}
  655. <Tooltip content="Generate Image" placement="bottom">
  656. <button
  657. class="{isLastMessage
  658. ? 'visible'
  659. : '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"
  660. on:click={() => {
  661. if (!generatingImage) {
  662. generateImage(message);
  663. }
  664. }}
  665. >
  666. {#if generatingImage}
  667. <svg
  668. class=" w-4 h-4"
  669. fill="currentColor"
  670. viewBox="0 0 24 24"
  671. xmlns="http://www.w3.org/2000/svg"
  672. ><style>
  673. .spinner_S1WN {
  674. animation: spinner_MGfb 0.8s linear infinite;
  675. animation-delay: -0.8s;
  676. }
  677. .spinner_Km9P {
  678. animation-delay: -0.65s;
  679. }
  680. .spinner_JApP {
  681. animation-delay: -0.5s;
  682. }
  683. @keyframes spinner_MGfb {
  684. 93.75%,
  685. 100% {
  686. opacity: 0.2;
  687. }
  688. }
  689. </style><circle class="spinner_S1WN" cx="4" cy="12" r="3" /><circle
  690. class="spinner_S1WN spinner_Km9P"
  691. cx="12"
  692. cy="12"
  693. r="3"
  694. /><circle
  695. class="spinner_S1WN spinner_JApP"
  696. cx="20"
  697. cy="12"
  698. r="3"
  699. /></svg
  700. >
  701. {:else}
  702. <svg
  703. xmlns="http://www.w3.org/2000/svg"
  704. fill="none"
  705. viewBox="0 0 24 24"
  706. stroke-width="2.3"
  707. stroke="currentColor"
  708. class="w-4 h-4"
  709. >
  710. <path
  711. stroke-linecap="round"
  712. stroke-linejoin="round"
  713. 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"
  714. />
  715. </svg>
  716. {/if}
  717. </button>
  718. </Tooltip>
  719. {/if}
  720. {#if message.info}
  721. <Tooltip content={$i18n.t('Generation Info')} placement="bottom">
  722. <button
  723. class=" {isLastMessage
  724. ? 'visible'
  725. : '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"
  726. on:click={() => {
  727. console.log(message);
  728. }}
  729. id="info-{message.id}"
  730. >
  731. <svg
  732. xmlns="http://www.w3.org/2000/svg"
  733. fill="none"
  734. viewBox="0 0 24 24"
  735. stroke-width="2.3"
  736. stroke="currentColor"
  737. class="w-4 h-4"
  738. >
  739. <path
  740. stroke-linecap="round"
  741. stroke-linejoin="round"
  742. 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"
  743. />
  744. </svg>
  745. </button>
  746. </Tooltip>
  747. {/if}
  748. {#if !readOnly}
  749. <Tooltip content={$i18n.t('Good Response')} placement="bottom">
  750. <button
  751. class="{isLastMessage
  752. ? 'visible'
  753. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg {message
  754. ?.annotation?.rating === 1
  755. ? 'bg-gray-100 dark:bg-gray-800'
  756. : ''} dark:hover:text-white hover:text-black transition"
  757. on:click={() => {
  758. rateMessage(message.id, 1);
  759. showRateComment = true;
  760. window.setTimeout(() => {
  761. document
  762. .getElementById(`message-feedback-${message.id}`)
  763. ?.scrollIntoView();
  764. }, 0);
  765. }}
  766. >
  767. <svg
  768. stroke="currentColor"
  769. fill="none"
  770. stroke-width="2.3"
  771. viewBox="0 0 24 24"
  772. stroke-linecap="round"
  773. stroke-linejoin="round"
  774. class="w-4 h-4"
  775. xmlns="http://www.w3.org/2000/svg"
  776. ><path
  777. 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"
  778. /></svg
  779. >
  780. </button>
  781. </Tooltip>
  782. <Tooltip content={$i18n.t('Bad Response')} placement="bottom">
  783. <button
  784. class="{isLastMessage
  785. ? 'visible'
  786. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg {message
  787. ?.annotation?.rating === -1
  788. ? 'bg-gray-100 dark:bg-gray-800'
  789. : ''} dark:hover:text-white hover:text-black transition"
  790. on:click={() => {
  791. rateMessage(message.id, -1);
  792. showRateComment = true;
  793. window.setTimeout(() => {
  794. document
  795. .getElementById(`message-feedback-${message.id}`)
  796. ?.scrollIntoView();
  797. }, 0);
  798. }}
  799. >
  800. <svg
  801. stroke="currentColor"
  802. fill="none"
  803. stroke-width="2.3"
  804. viewBox="0 0 24 24"
  805. stroke-linecap="round"
  806. stroke-linejoin="round"
  807. class="w-4 h-4"
  808. xmlns="http://www.w3.org/2000/svg"
  809. ><path
  810. 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"
  811. /></svg
  812. >
  813. </button>
  814. </Tooltip>
  815. {/if}
  816. {#if isLastMessage && !readOnly}
  817. <Tooltip content={$i18n.t('Continue Response')} placement="bottom">
  818. <button
  819. type="button"
  820. class="{isLastMessage
  821. ? 'visible'
  822. : '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"
  823. on:click={() => {
  824. continueGeneration();
  825. }}
  826. >
  827. <svg
  828. xmlns="http://www.w3.org/2000/svg"
  829. fill="none"
  830. viewBox="0 0 24 24"
  831. stroke-width="2.3"
  832. stroke="currentColor"
  833. class="w-4 h-4"
  834. >
  835. <path
  836. stroke-linecap="round"
  837. stroke-linejoin="round"
  838. d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
  839. />
  840. <path
  841. stroke-linecap="round"
  842. stroke-linejoin="round"
  843. 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"
  844. />
  845. </svg>
  846. </button>
  847. </Tooltip>
  848. <Tooltip content={$i18n.t('Regenerate')} placement="bottom">
  849. <button
  850. type="button"
  851. class="{isLastMessage
  852. ? 'visible'
  853. : '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"
  854. on:click={() => {
  855. regenerateResponse(message);
  856. }}
  857. >
  858. <svg
  859. xmlns="http://www.w3.org/2000/svg"
  860. fill="none"
  861. viewBox="0 0 24 24"
  862. stroke-width="2.3"
  863. stroke="currentColor"
  864. class="w-4 h-4"
  865. >
  866. <path
  867. stroke-linecap="round"
  868. stroke-linejoin="round"
  869. 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"
  870. />
  871. </svg>
  872. </button>
  873. </Tooltip>
  874. {/if}
  875. {/if}
  876. </div>
  877. {/if}
  878. {#if message.done && showRateComment}
  879. <RateComment
  880. messageId={message.id}
  881. bind:show={showRateComment}
  882. bind:message
  883. on:submit={() => {
  884. updateChatMessages();
  885. }}
  886. />
  887. {/if}
  888. </div>
  889. {/if}
  890. </div>
  891. </div>
  892. </div>
  893. </div>
  894. {/key}
  895. <style>
  896. .buttons::-webkit-scrollbar {
  897. display: none; /* for Chrome, Safari and Opera */
  898. }
  899. .buttons {
  900. -ms-overflow-style: none; /* IE and Edge */
  901. scrollbar-width: none; /* Firefox */
  902. }
  903. </style>