ResponseMessage.svelte 32 KB

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