ResponseMessage.svelte 30 KB

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