ResponseMessage.svelte 33 KB

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