ResponseMessage.svelte 30 KB

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