ResponseMessage.svelte 34 KB

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