ResponseMessage.svelte 33 KB

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