ResponseMessage.svelte 33 KB

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