ResponseMessage.svelte 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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 class=" flex w-full message-{message.id}" id="message-{message.id}" dir="{$settings.chatDirection}">
  276. <ProfileImage
  277. src={modelfiles[message.model]?.imageUrl ??
  278. ($i18n.language === 'dg-DG' ? `/doge.png` : `${WEBUI_BASE_URL}/static/favicon.png`)}
  279. />
  280. <div class="w-full overflow-hidden pl-1">
  281. <Name>
  282. {#if message.model in modelfiles}
  283. {modelfiles[message.model]?.title}
  284. {:else}
  285. {message.model ? ` ${message.model}` : ''}
  286. {/if}
  287. {#if message.timestamp}
  288. <span
  289. class=" self-center invisible group-hover:visible text-gray-400 text-xs font-medium uppercase"
  290. >
  291. {dayjs(message.timestamp * 1000).format($i18n.t('h:mm a'))}
  292. </span>
  293. {/if}
  294. </Name>
  295. {#if message.files}
  296. <div class="my-2.5 w-full flex overflow-x-auto gap-2 flex-wrap">
  297. {#each message.files as file}
  298. <div>
  299. {#if file.type === 'image'}
  300. <Image src={file.url} />
  301. {/if}
  302. </div>
  303. {/each}
  304. </div>
  305. {/if}
  306. <div
  307. 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"
  308. >
  309. <div>
  310. {#if edit === true}
  311. <div class="w-full bg-gray-50 dark:bg-gray-800 rounded-3xl px-5 py-3 my-2">
  312. <textarea
  313. id="message-edit-{message.id}"
  314. bind:this={editTextAreaElement}
  315. class=" bg-transparent outline-none w-full resize-none"
  316. bind:value={editedContent}
  317. on:input={(e) => {
  318. e.target.style.height = '';
  319. e.target.style.height = `${e.target.scrollHeight}px`;
  320. }}
  321. />
  322. <div class=" mt-2 mb-1 flex justify-end space-x-1.5 text-sm font-medium">
  323. <button
  324. id="close-edit-message-button"
  325. class=" px-4 py-2 bg-gray-900 hover:bg-gray-850 text-gray-100 transition rounded-3xl"
  326. on:click={() => {
  327. cancelEditMessage();
  328. }}
  329. >
  330. {$i18n.t('Cancel')}
  331. </button>
  332. <button
  333. id="save-edit-message-button"
  334. class="px-4 py-2 bg-white hover:bg-gray-100 text-gray-800 transition rounded-3xl"
  335. on:click={() => {
  336. editMessageConfirmHandler();
  337. }}
  338. >
  339. {$i18n.t('Save')}
  340. </button>
  341. </div>
  342. </div>
  343. {:else}
  344. <div class="w-full">
  345. {#if message?.error === true}
  346. <div
  347. 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"
  348. >
  349. <svg
  350. xmlns="http://www.w3.org/2000/svg"
  351. fill="none"
  352. viewBox="0 0 24 24"
  353. stroke-width="1.5"
  354. stroke="currentColor"
  355. class="w-5 h-5 self-center"
  356. >
  357. <path
  358. stroke-linecap="round"
  359. stroke-linejoin="round"
  360. d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z"
  361. />
  362. </svg>
  363. <div class=" self-center">
  364. {message.content}
  365. </div>
  366. </div>
  367. {:else if message.content === ''}
  368. <Skeleton />
  369. {:else}
  370. {#each tokens as token}
  371. {#if token.type === 'code'}
  372. <CodeBlock
  373. lang={token.lang}
  374. code={revertSanitizedResponseContent(token.text)}
  375. />
  376. {:else}
  377. {@html marked.parse(token.raw, {
  378. ...defaults,
  379. gfm: true,
  380. breaks: true,
  381. renderer
  382. })}
  383. {/if}
  384. {/each}
  385. {/if}
  386. {#if message.citations}
  387. <div class="mt-1 mb-2 w-full flex gap-1 items-center">
  388. {#each message.citations.reduce((acc, citation) => {
  389. citation.document.forEach((document, index) => {
  390. const metadata = citation.metadata?.[index];
  391. const id = metadata?.source ?? 'N/A';
  392. const existingSource = acc.find((item) => item.id === id);
  393. if (existingSource) {
  394. existingSource.document.push(document);
  395. existingSource.metadata.push(metadata);
  396. } else {
  397. acc.push( { id: id, source: citation?.source, document: [document], metadata: metadata ? [metadata] : [] } );
  398. }
  399. });
  400. return acc;
  401. }, []) as citation, idx}
  402. <div class="flex gap-1 text-xs font-semibold">
  403. <button
  404. 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"
  405. on:click={() => {
  406. showCitationModal = true;
  407. selectedCitation = citation;
  408. }}
  409. >
  410. <div class="bg-white dark:bg-gray-700 rounded-full size-4">
  411. {idx + 1}
  412. </div>
  413. <div class="flex-1 mx-2 line-clamp-1">
  414. {citation.source.name}
  415. </div>
  416. </button>
  417. </div>
  418. {/each}
  419. </div>
  420. {/if}
  421. {#if message.done || siblings.length > 1}
  422. <div
  423. class=" flex justify-start overflow-x-auto buttons text-gray-600 dark:text-gray-500"
  424. >
  425. {#if siblings.length > 1}
  426. <div class="flex self-center">
  427. <button
  428. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  429. on:click={() => {
  430. showPreviousMessage(message);
  431. }}
  432. >
  433. <svg
  434. xmlns="http://www.w3.org/2000/svg"
  435. fill="none"
  436. viewBox="0 0 24 24"
  437. stroke="currentColor"
  438. stroke-width="2.5"
  439. class="size-3.5"
  440. >
  441. <path
  442. stroke-linecap="round"
  443. stroke-linejoin="round"
  444. d="M15.75 19.5 8.25 12l7.5-7.5"
  445. />
  446. </svg>
  447. </button>
  448. <div
  449. class="text-sm tracking-widest font-semibold self-center dark:text-gray-100"
  450. >
  451. {siblings.indexOf(message.id) + 1}/{siblings.length}
  452. </div>
  453. <button
  454. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  455. on:click={() => {
  456. showNextMessage(message);
  457. }}
  458. >
  459. <svg
  460. xmlns="http://www.w3.org/2000/svg"
  461. fill="none"
  462. viewBox="0 0 24 24"
  463. stroke="currentColor"
  464. stroke-width="2.5"
  465. class="size-3.5"
  466. >
  467. <path
  468. stroke-linecap="round"
  469. stroke-linejoin="round"
  470. d="m8.25 4.5 7.5 7.5-7.5 7.5"
  471. />
  472. </svg>
  473. </button>
  474. </div>
  475. {/if}
  476. {#if message.done}
  477. {#if !readOnly}
  478. <Tooltip content={$i18n.t('Edit')} placement="bottom">
  479. <button
  480. class="{isLastMessage
  481. ? 'visible'
  482. : '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"
  483. on:click={() => {
  484. editMessageHandler();
  485. }}
  486. >
  487. <svg
  488. xmlns="http://www.w3.org/2000/svg"
  489. fill="none"
  490. viewBox="0 0 24 24"
  491. stroke-width="2.3"
  492. stroke="currentColor"
  493. class="w-4 h-4"
  494. >
  495. <path
  496. stroke-linecap="round"
  497. stroke-linejoin="round"
  498. 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"
  499. />
  500. </svg>
  501. </button>
  502. </Tooltip>
  503. {/if}
  504. <Tooltip content={$i18n.t('Copy')} placement="bottom">
  505. <button
  506. class="{isLastMessage
  507. ? 'visible'
  508. : '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"
  509. on:click={() => {
  510. copyToClipboard(message.content);
  511. }}
  512. >
  513. <svg
  514. xmlns="http://www.w3.org/2000/svg"
  515. fill="none"
  516. viewBox="0 0 24 24"
  517. stroke-width="2.3"
  518. stroke="currentColor"
  519. class="w-4 h-4"
  520. >
  521. <path
  522. stroke-linecap="round"
  523. stroke-linejoin="round"
  524. 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"
  525. />
  526. </svg>
  527. </button>
  528. </Tooltip>
  529. <Tooltip content={$i18n.t('Read Aloud')} placement="bottom">
  530. <button
  531. id="speak-button-{message.id}"
  532. class="{isLastMessage
  533. ? 'visible'
  534. : '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"
  535. on:click={() => {
  536. if (!loadingSpeech) {
  537. toggleSpeakMessage(message);
  538. }
  539. }}
  540. >
  541. {#if loadingSpeech}
  542. <svg
  543. class=" w-4 h-4"
  544. fill="currentColor"
  545. viewBox="0 0 24 24"
  546. xmlns="http://www.w3.org/2000/svg"
  547. ><style>
  548. .spinner_S1WN {
  549. animation: spinner_MGfb 0.8s linear infinite;
  550. animation-delay: -0.8s;
  551. }
  552. .spinner_Km9P {
  553. animation-delay: -0.65s;
  554. }
  555. .spinner_JApP {
  556. animation-delay: -0.5s;
  557. }
  558. @keyframes spinner_MGfb {
  559. 93.75%,
  560. 100% {
  561. opacity: 0.2;
  562. }
  563. }
  564. </style><circle class="spinner_S1WN" cx="4" cy="12" r="3" /><circle
  565. class="spinner_S1WN spinner_Km9P"
  566. cx="12"
  567. cy="12"
  568. r="3"
  569. /><circle
  570. class="spinner_S1WN spinner_JApP"
  571. cx="20"
  572. cy="12"
  573. r="3"
  574. /></svg
  575. >
  576. {:else if speaking}
  577. <svg
  578. xmlns="http://www.w3.org/2000/svg"
  579. fill="none"
  580. viewBox="0 0 24 24"
  581. stroke-width="2.3"
  582. stroke="currentColor"
  583. class="w-4 h-4"
  584. >
  585. <path
  586. stroke-linecap="round"
  587. stroke-linejoin="round"
  588. 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"
  589. />
  590. </svg>
  591. {:else}
  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="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"
  604. />
  605. </svg>
  606. {/if}
  607. </button>
  608. </Tooltip>
  609. {#if $config.images && !readOnly}
  610. <Tooltip content="Generate Image" placement="bottom">
  611. <button
  612. class="{isLastMessage
  613. ? 'visible'
  614. : '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"
  615. on:click={() => {
  616. if (!generatingImage) {
  617. generateImage(message);
  618. }
  619. }}
  620. >
  621. {#if generatingImage}
  622. <svg
  623. class=" w-4 h-4"
  624. fill="currentColor"
  625. viewBox="0 0 24 24"
  626. xmlns="http://www.w3.org/2000/svg"
  627. ><style>
  628. .spinner_S1WN {
  629. animation: spinner_MGfb 0.8s linear infinite;
  630. animation-delay: -0.8s;
  631. }
  632. .spinner_Km9P {
  633. animation-delay: -0.65s;
  634. }
  635. .spinner_JApP {
  636. animation-delay: -0.5s;
  637. }
  638. @keyframes spinner_MGfb {
  639. 93.75%,
  640. 100% {
  641. opacity: 0.2;
  642. }
  643. }
  644. </style><circle class="spinner_S1WN" cx="4" cy="12" r="3" /><circle
  645. class="spinner_S1WN spinner_Km9P"
  646. cx="12"
  647. cy="12"
  648. r="3"
  649. /><circle
  650. class="spinner_S1WN spinner_JApP"
  651. cx="20"
  652. cy="12"
  653. r="3"
  654. /></svg
  655. >
  656. {:else}
  657. <svg
  658. xmlns="http://www.w3.org/2000/svg"
  659. fill="none"
  660. viewBox="0 0 24 24"
  661. stroke-width="2.3"
  662. stroke="currentColor"
  663. class="w-4 h-4"
  664. >
  665. <path
  666. stroke-linecap="round"
  667. stroke-linejoin="round"
  668. 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"
  669. />
  670. </svg>
  671. {/if}
  672. </button>
  673. </Tooltip>
  674. {/if}
  675. {#if message.info}
  676. <Tooltip content={$i18n.t('Generation Info')} placement="bottom">
  677. <button
  678. class=" {isLastMessage
  679. ? 'visible'
  680. : '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"
  681. on:click={() => {
  682. console.log(message);
  683. }}
  684. id="info-{message.id}"
  685. >
  686. <svg
  687. xmlns="http://www.w3.org/2000/svg"
  688. fill="none"
  689. viewBox="0 0 24 24"
  690. stroke-width="2.3"
  691. stroke="currentColor"
  692. class="w-4 h-4"
  693. >
  694. <path
  695. stroke-linecap="round"
  696. stroke-linejoin="round"
  697. 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"
  698. />
  699. </svg>
  700. </button>
  701. </Tooltip>
  702. {/if}
  703. {#if !readOnly}
  704. <Tooltip content={$i18n.t('Good Response')} placement="bottom">
  705. <button
  706. class="{isLastMessage
  707. ? 'visible'
  708. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg {message
  709. ?.annotation?.rating === 1
  710. ? 'bg-gray-100 dark:bg-gray-800'
  711. : ''} dark:hover:text-white hover:text-black transition"
  712. on:click={() => {
  713. rateMessage(message.id, 1);
  714. showRateComment = true;
  715. window.setTimeout(() => {
  716. document
  717. .getElementById(`message-feedback-${message.id}`)
  718. ?.scrollIntoView();
  719. }, 0);
  720. }}
  721. >
  722. <svg
  723. stroke="currentColor"
  724. fill="none"
  725. stroke-width="2.3"
  726. viewBox="0 0 24 24"
  727. stroke-linecap="round"
  728. stroke-linejoin="round"
  729. class="w-4 h-4"
  730. xmlns="http://www.w3.org/2000/svg"
  731. ><path
  732. 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"
  733. /></svg
  734. >
  735. </button>
  736. </Tooltip>
  737. <Tooltip content={$i18n.t('Bad Response')} placement="bottom">
  738. <button
  739. class="{isLastMessage
  740. ? 'visible'
  741. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg {message
  742. ?.annotation?.rating === -1
  743. ? 'bg-gray-100 dark:bg-gray-800'
  744. : ''} dark:hover:text-white hover:text-black transition"
  745. on:click={() => {
  746. rateMessage(message.id, -1);
  747. showRateComment = true;
  748. window.setTimeout(() => {
  749. document
  750. .getElementById(`message-feedback-${message.id}`)
  751. ?.scrollIntoView();
  752. }, 0);
  753. }}
  754. >
  755. <svg
  756. stroke="currentColor"
  757. fill="none"
  758. stroke-width="2.3"
  759. viewBox="0 0 24 24"
  760. stroke-linecap="round"
  761. stroke-linejoin="round"
  762. class="w-4 h-4"
  763. xmlns="http://www.w3.org/2000/svg"
  764. ><path
  765. 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"
  766. /></svg
  767. >
  768. </button>
  769. </Tooltip>
  770. {/if}
  771. {#if isLastMessage && !readOnly}
  772. <Tooltip content={$i18n.t('Continue Response')} placement="bottom">
  773. <button
  774. type="button"
  775. class="{isLastMessage
  776. ? 'visible'
  777. : '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"
  778. on:click={() => {
  779. continueGeneration();
  780. }}
  781. >
  782. <svg
  783. xmlns="http://www.w3.org/2000/svg"
  784. fill="none"
  785. viewBox="0 0 24 24"
  786. stroke-width="2.3"
  787. stroke="currentColor"
  788. class="w-4 h-4"
  789. >
  790. <path
  791. stroke-linecap="round"
  792. stroke-linejoin="round"
  793. d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
  794. />
  795. <path
  796. stroke-linecap="round"
  797. stroke-linejoin="round"
  798. 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"
  799. />
  800. </svg>
  801. </button>
  802. </Tooltip>
  803. <Tooltip content={$i18n.t('Regenerate')} placement="bottom">
  804. <button
  805. type="button"
  806. class="{isLastMessage
  807. ? 'visible'
  808. : '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"
  809. on:click={regenerateResponse}
  810. >
  811. <svg
  812. xmlns="http://www.w3.org/2000/svg"
  813. fill="none"
  814. viewBox="0 0 24 24"
  815. stroke-width="2.3"
  816. stroke="currentColor"
  817. class="w-4 h-4"
  818. >
  819. <path
  820. stroke-linecap="round"
  821. stroke-linejoin="round"
  822. 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"
  823. />
  824. </svg>
  825. </button>
  826. </Tooltip>
  827. {/if}
  828. {/if}
  829. </div>
  830. {/if}
  831. {#if message.done && showRateComment}
  832. <RateComment
  833. messageId={message.id}
  834. bind:show={showRateComment}
  835. bind:message
  836. on:submit={() => {
  837. updateChatMessages();
  838. }}
  839. />
  840. {/if}
  841. </div>
  842. {/if}
  843. </div>
  844. </div>
  845. </div>
  846. </div>
  847. {/key}
  848. <style>
  849. .buttons::-webkit-scrollbar {
  850. display: none; /* for Chrome, Safari and Opera */
  851. }
  852. .buttons {
  853. -ms-overflow-style: none; /* IE and Edge */
  854. scrollbar-width: none; /* Firefox */
  855. }
  856. </style>