ResponseMessage.svelte 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import dayjs from 'dayjs';
  4. import { createEventDispatcher } from 'svelte';
  5. import { onMount, tick, getContext } from 'svelte';
  6. const i18n = getContext<Writable<i18nType>>('i18n');
  7. const dispatch = createEventDispatcher();
  8. import { config, models, settings, user } from '$lib/stores';
  9. import { synthesizeOpenAISpeech } from '$lib/apis/audio';
  10. import { imageGenerations } from '$lib/apis/images';
  11. import {
  12. approximateToHumanReadable,
  13. extractParagraphsForAudio,
  14. extractSentencesForAudio,
  15. cleanText,
  16. getMessageContentParts
  17. } from '$lib/utils';
  18. import { WEBUI_BASE_URL } from '$lib/constants';
  19. import Name from './Name.svelte';
  20. import ProfileImage from './ProfileImage.svelte';
  21. import Skeleton from './Skeleton.svelte';
  22. import Image from '$lib/components/common/Image.svelte';
  23. import Tooltip from '$lib/components/common/Tooltip.svelte';
  24. import RateComment from './RateComment.svelte';
  25. import Spinner from '$lib/components/common/Spinner.svelte';
  26. import WebSearchResults from './ResponseMessage/WebSearchResults.svelte';
  27. import Sparkles from '$lib/components/icons/Sparkles.svelte';
  28. import Markdown from './Markdown.svelte';
  29. import Error from './Error.svelte';
  30. import Citations from './Citations.svelte';
  31. import type { Writable } from 'svelte/store';
  32. import type { i18n as i18nType } from 'i18next';
  33. interface MessageType {
  34. id: string;
  35. model: string;
  36. content: string;
  37. files?: { type: string; url: string }[];
  38. timestamp: number;
  39. role: string;
  40. statusHistory?: {
  41. done: boolean;
  42. action: string;
  43. description: string;
  44. urls?: string[];
  45. query?: string;
  46. }[];
  47. status?: {
  48. done: boolean;
  49. action: string;
  50. description: string;
  51. urls?: string[];
  52. query?: string;
  53. };
  54. done: boolean;
  55. error?: boolean | { content: string };
  56. citations?: string[];
  57. info?: {
  58. openai?: boolean;
  59. prompt_tokens?: number;
  60. completion_tokens?: number;
  61. total_tokens?: number;
  62. eval_count?: number;
  63. eval_duration?: number;
  64. prompt_eval_count?: number;
  65. prompt_eval_duration?: number;
  66. total_duration?: number;
  67. load_duration?: number;
  68. };
  69. annotation?: { type: string; rating: number };
  70. }
  71. export let message: MessageType;
  72. export let siblings;
  73. export let isLastMessage = true;
  74. export let readOnly = false;
  75. export let updateChatMessages: Function;
  76. export let confirmEditResponseMessage: Function;
  77. export let saveNewResponseMessage: Function;
  78. export let showPreviousMessage: Function;
  79. export let showNextMessage: Function;
  80. export let rateMessage: Function;
  81. export let copyToClipboard: Function;
  82. export let continueGeneration: Function;
  83. export let regenerateResponse: Function;
  84. let model = null;
  85. $: model = $models.find((m) => m.id === message.model);
  86. let edit = false;
  87. let editedContent = '';
  88. let editTextAreaElement: HTMLTextAreaElement;
  89. let audioParts: Record<number, HTMLAudioElement | null> = {};
  90. let speaking = false;
  91. let speakingIdx: number | undefined;
  92. let loadingSpeech = false;
  93. let generatingImage = false;
  94. let showRateComment = false;
  95. const playAudio = (idx: number) => {
  96. return new Promise<void>((res) => {
  97. speakingIdx = idx;
  98. const audio = audioParts[idx];
  99. if (!audio) {
  100. return res();
  101. }
  102. audio.play();
  103. audio.onended = async () => {
  104. await new Promise((r) => setTimeout(r, 300));
  105. if (Object.keys(audioParts).length - 1 === idx) {
  106. speaking = false;
  107. }
  108. res();
  109. };
  110. });
  111. };
  112. const toggleSpeakMessage = async () => {
  113. if (speaking) {
  114. try {
  115. speechSynthesis.cancel();
  116. if (speakingIdx !== undefined && audioParts[speakingIdx]) {
  117. audioParts[speakingIdx]!.pause();
  118. audioParts[speakingIdx]!.currentTime = 0;
  119. }
  120. } catch {}
  121. speaking = false;
  122. speakingIdx = undefined;
  123. return;
  124. }
  125. if (!(message?.content ?? '').trim().length) {
  126. toast.info($i18n.t('No content to speak'));
  127. return;
  128. }
  129. speaking = true;
  130. if ($config.audio.tts.engine !== '') {
  131. loadingSpeech = true;
  132. const messageContentParts: string[] = getMessageContentParts(
  133. message.content,
  134. $config?.audio?.tts?.split_on ?? 'punctuation'
  135. );
  136. if (!messageContentParts.length) {
  137. console.log('No content to speak');
  138. toast.info($i18n.t('No content to speak'));
  139. speaking = false;
  140. loadingSpeech = false;
  141. return;
  142. }
  143. console.debug('Prepared message content for TTS', messageContentParts);
  144. audioParts = messageContentParts.reduce(
  145. (acc, _sentence, idx) => {
  146. acc[idx] = null;
  147. return acc;
  148. },
  149. {} as typeof audioParts
  150. );
  151. let lastPlayedAudioPromise = Promise.resolve(); // Initialize a promise that resolves immediately
  152. for (const [idx, sentence] of messageContentParts.entries()) {
  153. const res = await synthesizeOpenAISpeech(
  154. localStorage.token,
  155. $settings?.audio?.tts?.defaultVoice === $config.audio.tts.voice
  156. ? ($settings?.audio?.tts?.voice ?? $config?.audio?.tts?.voice)
  157. : $config?.audio?.tts?.voice,
  158. sentence
  159. ).catch((error) => {
  160. console.error(error);
  161. toast.error(error);
  162. speaking = false;
  163. loadingSpeech = false;
  164. });
  165. if (res) {
  166. const blob = await res.blob();
  167. const blobUrl = URL.createObjectURL(blob);
  168. const audio = new Audio(blobUrl);
  169. audioParts[idx] = audio;
  170. loadingSpeech = false;
  171. lastPlayedAudioPromise = lastPlayedAudioPromise.then(() => playAudio(idx));
  172. }
  173. }
  174. } else {
  175. let voices = [];
  176. const getVoicesLoop = setInterval(() => {
  177. voices = speechSynthesis.getVoices();
  178. if (voices.length > 0) {
  179. clearInterval(getVoicesLoop);
  180. const voice =
  181. voices
  182. ?.filter(
  183. (v) => v.voiceURI === ($settings?.audio?.tts?.voice ?? $config?.audio?.tts?.voice)
  184. )
  185. ?.at(0) ?? undefined;
  186. console.log(voice);
  187. const speak = new SpeechSynthesisUtterance(message.content);
  188. console.log(speak);
  189. speak.onend = () => {
  190. speaking = false;
  191. if ($settings.conversationMode) {
  192. document.getElementById('voice-input-button')?.click();
  193. }
  194. };
  195. if (voice) {
  196. speak.voice = voice;
  197. }
  198. speechSynthesis.speak(speak);
  199. }
  200. }, 100);
  201. }
  202. };
  203. const editMessageHandler = async () => {
  204. edit = true;
  205. editedContent = message.content;
  206. await tick();
  207. editTextAreaElement.style.height = '';
  208. editTextAreaElement.style.height = `${editTextAreaElement.scrollHeight}px`;
  209. };
  210. const editMessageConfirmHandler = async () => {
  211. if (editedContent === '') {
  212. editedContent = ' ';
  213. }
  214. confirmEditResponseMessage(message.id, editedContent);
  215. edit = false;
  216. editedContent = '';
  217. await tick();
  218. };
  219. const saveNewMessageHandler = async () => {
  220. saveNewResponseMessage(message, editedContent);
  221. edit = false;
  222. editedContent = '';
  223. await tick();
  224. };
  225. const cancelEditMessage = async () => {
  226. edit = false;
  227. editedContent = '';
  228. await tick();
  229. };
  230. const generateImage = async (message: MessageType) => {
  231. generatingImage = true;
  232. const res = await imageGenerations(localStorage.token, message.content).catch((error) => {
  233. toast.error(error);
  234. });
  235. console.log(res);
  236. if (res) {
  237. message.files = res.map((image) => ({
  238. type: 'image',
  239. url: `${image.url}`
  240. }));
  241. dispatch('save', message);
  242. }
  243. generatingImage = false;
  244. };
  245. $: if (!edit) {
  246. (async () => {
  247. await tick();
  248. })();
  249. }
  250. onMount(async () => {
  251. await tick();
  252. });
  253. </script>
  254. {#key message.id}
  255. <div
  256. class=" flex w-full message-{message.id}"
  257. id="message-{message.id}"
  258. dir={$settings.chatDirection}
  259. >
  260. <ProfileImage
  261. src={model?.info?.meta?.profile_image_url ??
  262. ($i18n.language === 'dg-DG' ? `/doge.png` : `${WEBUI_BASE_URL}/static/favicon.png`)}
  263. />
  264. <div class="w-full overflow-hidden pl-1">
  265. <Name>
  266. {model?.name ?? message.model}
  267. {#if message.timestamp}
  268. <span
  269. class=" self-center invisible group-hover:visible text-gray-400 text-xs font-medium uppercase ml-0.5 -mt-0.5"
  270. >
  271. {dayjs(message.timestamp * 1000).format($i18n.t('h:mm a'))}
  272. </span>
  273. {/if}
  274. </Name>
  275. <div>
  276. {#if message?.files && message.files?.filter((f) => f.type === 'image').length > 0}
  277. <div class="my-2.5 w-full flex overflow-x-auto gap-2 flex-wrap">
  278. {#each message.files as file}
  279. <div>
  280. {#if file.type === 'image'}
  281. <Image src={file.url} alt={message.content} />
  282. {/if}
  283. </div>
  284. {/each}
  285. </div>
  286. {/if}
  287. <div class="chat-{message.role} w-full min-w-full markdown-prose">
  288. <div>
  289. {#if (message?.statusHistory ?? [...(message?.status ? [message?.status] : [])]).length > 0}
  290. {@const status = (
  291. message?.statusHistory ?? [...(message?.status ? [message?.status] : [])]
  292. ).at(-1)}
  293. <div class="status-description flex items-center gap-2 pt-0.5 pb-1">
  294. {#if status?.done === false}
  295. <div class="">
  296. <Spinner className="size-4" />
  297. </div>
  298. {/if}
  299. {#if status?.action === 'web_search' && status?.urls}
  300. <WebSearchResults {status}>
  301. <div class="flex flex-col justify-center -space-y-0.5">
  302. <div
  303. class="{status?.done === false
  304. ? 'shimmer'
  305. : ''} text-base line-clamp-1 text-wrap"
  306. >
  307. {status?.description}
  308. </div>
  309. </div>
  310. </WebSearchResults>
  311. {:else}
  312. <div class="flex flex-col justify-center -space-y-0.5">
  313. <div
  314. class="{status?.done === false
  315. ? 'shimmer'
  316. : ''} text-gray-500 dark:text-gray-500 text-base line-clamp-1 text-wrap"
  317. >
  318. {status?.description}
  319. </div>
  320. </div>
  321. {/if}
  322. </div>
  323. {/if}
  324. {#if edit === true}
  325. <div class="w-full bg-gray-50 dark:bg-gray-800 rounded-3xl px-5 py-3 my-2">
  326. <textarea
  327. id="message-edit-{message.id}"
  328. bind:this={editTextAreaElement}
  329. class=" bg-transparent outline-none w-full resize-none"
  330. bind:value={editedContent}
  331. on:input={(e) => {
  332. e.target.style.height = '';
  333. e.target.style.height = `${e.target.scrollHeight}px`;
  334. }}
  335. on:keydown={(e) => {
  336. if (e.key === 'Escape') {
  337. document.getElementById('close-edit-message-button')?.click();
  338. }
  339. const isCmdOrCtrlPressed = e.metaKey || e.ctrlKey;
  340. const isEnterPressed = e.key === 'Enter';
  341. if (isCmdOrCtrlPressed && isEnterPressed) {
  342. document.getElementById('save-edit-message-button')?.click();
  343. }
  344. }}
  345. />
  346. <div class=" mt-2 mb-1 flex justify-between text-sm font-medium">
  347. <div>
  348. <button
  349. id="close-edit-message-button"
  350. class=" px-4 py-2 bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 border dark:border-gray-700 text-gray-700 dark:text-gray-200 transition rounded-3xl"
  351. on:click={() => {
  352. saveNewMessageHandler();
  353. }}
  354. >
  355. {$i18n.t('Save New Message')}
  356. </button>
  357. </div>
  358. <div class="flex space-x-1.5">
  359. <button
  360. id="close-edit-message-button"
  361. class="px-4 py-2 bg-white dark:bg-gray-900 hover:bg-gray-100 text-gray-800 dark:text-gray-100 transition rounded-3xl"
  362. on:click={() => {
  363. cancelEditMessage();
  364. }}
  365. >
  366. {$i18n.t('Cancel')}
  367. </button>
  368. <button
  369. id="confirm-edit-message-button"
  370. class=" px-4 py-2 bg-gray-900 dark:bg-white hover:bg-gray-850 text-gray-100 dark:text-gray-800 transition rounded-3xl"
  371. on:click={() => {
  372. editMessageConfirmHandler();
  373. }}
  374. >
  375. {$i18n.t('Save')}
  376. </button>
  377. </div>
  378. </div>
  379. </div>
  380. {:else}
  381. <div class="w-full flex flex-col">
  382. {#if message.content === '' && !message.error}
  383. <Skeleton />
  384. {:else if message.content && message.error !== true}
  385. <!-- always show message contents even if there's an error -->
  386. <!-- unless message.error === true which is legacy error handling, where the error message is stored in message.content -->
  387. <Markdown id={message.id} content={message.content} {model} />
  388. {/if}
  389. {#if message.error}
  390. <Error content={message?.error?.content ?? message.content} />
  391. {/if}
  392. {#if message.citations}
  393. <Citations citations={message.citations} />
  394. {/if}
  395. </div>
  396. {/if}
  397. </div>
  398. </div>
  399. {#if !edit}
  400. {#if message.done || siblings.length > 1}
  401. <div
  402. class=" flex justify-start overflow-x-auto buttons text-gray-600 dark:text-gray-500 mt-0.5"
  403. >
  404. {#if siblings.length > 1}
  405. <div class="flex self-center min-w-fit" dir="ltr">
  406. <button
  407. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  408. on:click={() => {
  409. showPreviousMessage(message);
  410. }}
  411. >
  412. <svg
  413. xmlns="http://www.w3.org/2000/svg"
  414. fill="none"
  415. viewBox="0 0 24 24"
  416. stroke="currentColor"
  417. stroke-width="2.5"
  418. class="size-3.5"
  419. >
  420. <path
  421. stroke-linecap="round"
  422. stroke-linejoin="round"
  423. d="M15.75 19.5 8.25 12l7.5-7.5"
  424. />
  425. </svg>
  426. </button>
  427. <div
  428. class="text-sm tracking-widest font-semibold self-center dark:text-gray-100 min-w-fit"
  429. >
  430. {siblings.indexOf(message.id) + 1}/{siblings.length}
  431. </div>
  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. showNextMessage(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="m8.25 4.5 7.5 7.5-7.5 7.5"
  450. />
  451. </svg>
  452. </button>
  453. </div>
  454. {/if}
  455. {#if message.done}
  456. {#if !readOnly}
  457. {#if $user.role === 'user' ? ($config?.permissions?.chat?.editing ?? true) : true}
  458. <Tooltip content={$i18n.t('Edit')} placement="bottom">
  459. <button
  460. class="{isLastMessage
  461. ? 'visible'
  462. : '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"
  463. on:click={() => {
  464. editMessageHandler();
  465. }}
  466. >
  467. <svg
  468. xmlns="http://www.w3.org/2000/svg"
  469. fill="none"
  470. viewBox="0 0 24 24"
  471. stroke-width="2.3"
  472. stroke="currentColor"
  473. class="w-4 h-4"
  474. >
  475. <path
  476. stroke-linecap="round"
  477. stroke-linejoin="round"
  478. 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"
  479. />
  480. </svg>
  481. </button>
  482. </Tooltip>
  483. {/if}
  484. {/if}
  485. <Tooltip content={$i18n.t('Copy')} placement="bottom">
  486. <button
  487. class="{isLastMessage
  488. ? 'visible'
  489. : '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"
  490. on:click={() => {
  491. copyToClipboard(message.content);
  492. }}
  493. >
  494. <svg
  495. xmlns="http://www.w3.org/2000/svg"
  496. fill="none"
  497. viewBox="0 0 24 24"
  498. stroke-width="2.3"
  499. stroke="currentColor"
  500. class="w-4 h-4"
  501. >
  502. <path
  503. stroke-linecap="round"
  504. stroke-linejoin="round"
  505. 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"
  506. />
  507. </svg>
  508. </button>
  509. </Tooltip>
  510. <Tooltip content={$i18n.t('Read Aloud')} placement="bottom">
  511. <button
  512. id="speak-button-{message.id}"
  513. class="{isLastMessage
  514. ? 'visible'
  515. : '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"
  516. on:click={() => {
  517. if (!loadingSpeech) {
  518. toggleSpeakMessage();
  519. }
  520. }}
  521. >
  522. {#if loadingSpeech}
  523. <svg
  524. class=" w-4 h-4"
  525. fill="currentColor"
  526. viewBox="0 0 24 24"
  527. xmlns="http://www.w3.org/2000/svg"
  528. ><style>
  529. .spinner_S1WN {
  530. animation: spinner_MGfb 0.8s linear infinite;
  531. animation-delay: -0.8s;
  532. }
  533. .spinner_Km9P {
  534. animation-delay: -0.65s;
  535. }
  536. .spinner_JApP {
  537. animation-delay: -0.5s;
  538. }
  539. @keyframes spinner_MGfb {
  540. 93.75%,
  541. 100% {
  542. opacity: 0.2;
  543. }
  544. }
  545. </style><circle class="spinner_S1WN" cx="4" cy="12" r="3" /><circle
  546. class="spinner_S1WN spinner_Km9P"
  547. cx="12"
  548. cy="12"
  549. r="3"
  550. /><circle class="spinner_S1WN spinner_JApP" cx="20" cy="12" r="3" /></svg
  551. >
  552. {:else if speaking}
  553. <svg
  554. xmlns="http://www.w3.org/2000/svg"
  555. fill="none"
  556. viewBox="0 0 24 24"
  557. stroke-width="2.3"
  558. stroke="currentColor"
  559. class="w-4 h-4"
  560. >
  561. <path
  562. stroke-linecap="round"
  563. stroke-linejoin="round"
  564. 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"
  565. />
  566. </svg>
  567. {:else}
  568. <svg
  569. xmlns="http://www.w3.org/2000/svg"
  570. fill="none"
  571. viewBox="0 0 24 24"
  572. stroke-width="2.3"
  573. stroke="currentColor"
  574. class="w-4 h-4"
  575. >
  576. <path
  577. stroke-linecap="round"
  578. stroke-linejoin="round"
  579. 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"
  580. />
  581. </svg>
  582. {/if}
  583. </button>
  584. </Tooltip>
  585. {#if $config?.features.enable_image_generation && !readOnly}
  586. <Tooltip content={$i18n.t('Generate Image')} placement="bottom">
  587. <button
  588. class="{isLastMessage
  589. ? 'visible'
  590. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition"
  591. on:click={() => {
  592. if (!generatingImage) {
  593. generateImage(message);
  594. }
  595. }}
  596. >
  597. {#if generatingImage}
  598. <svg
  599. class=" w-4 h-4"
  600. fill="currentColor"
  601. viewBox="0 0 24 24"
  602. xmlns="http://www.w3.org/2000/svg"
  603. ><style>
  604. .spinner_S1WN {
  605. animation: spinner_MGfb 0.8s linear infinite;
  606. animation-delay: -0.8s;
  607. }
  608. .spinner_Km9P {
  609. animation-delay: -0.65s;
  610. }
  611. .spinner_JApP {
  612. animation-delay: -0.5s;
  613. }
  614. @keyframes spinner_MGfb {
  615. 93.75%,
  616. 100% {
  617. opacity: 0.2;
  618. }
  619. }
  620. </style><circle class="spinner_S1WN" cx="4" cy="12" r="3" /><circle
  621. class="spinner_S1WN spinner_Km9P"
  622. cx="12"
  623. cy="12"
  624. r="3"
  625. /><circle class="spinner_S1WN spinner_JApP" cx="20" cy="12" r="3" /></svg
  626. >
  627. {:else}
  628. <svg
  629. xmlns="http://www.w3.org/2000/svg"
  630. fill="none"
  631. viewBox="0 0 24 24"
  632. stroke-width="2.3"
  633. stroke="currentColor"
  634. class="w-4 h-4"
  635. >
  636. <path
  637. stroke-linecap="round"
  638. stroke-linejoin="round"
  639. 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"
  640. />
  641. </svg>
  642. {/if}
  643. </button>
  644. </Tooltip>
  645. {/if}
  646. {#if message.info}
  647. <Tooltip
  648. content={message.info.openai
  649. ? `prompt_tokens: ${message.info.prompt_tokens ?? 'N/A'}<br/>
  650. completion_tokens: ${message.info.completion_tokens ?? 'N/A'}<br/>
  651. total_tokens: ${message.info.total_tokens ?? 'N/A'}`
  652. : `response_token/s: ${
  653. `${
  654. Math.round(
  655. ((message.info.eval_count ?? 0) /
  656. ((message.info.eval_duration ?? 0) / 1000000000)) *
  657. 100
  658. ) / 100
  659. } tokens` ?? 'N/A'
  660. }<br/>
  661. prompt_token/s: ${
  662. Math.round(
  663. ((message.info.prompt_eval_count ?? 0) /
  664. ((message.info.prompt_eval_duration ?? 0) / 1000000000)) *
  665. 100
  666. ) / 100 ?? 'N/A'
  667. } tokens<br/>
  668. total_duration: ${
  669. Math.round(((message.info.total_duration ?? 0) / 1000000) * 100) / 100 ?? 'N/A'
  670. }ms<br/>
  671. load_duration: ${
  672. Math.round(((message.info.load_duration ?? 0) / 1000000) * 100) / 100 ?? 'N/A'
  673. }ms<br/>
  674. prompt_eval_count: ${message.info.prompt_eval_count ?? 'N/A'}<br/>
  675. prompt_eval_duration: ${
  676. Math.round(((message.info.prompt_eval_duration ?? 0) / 1000000) * 100) / 100 ??
  677. 'N/A'
  678. }ms<br/>
  679. eval_count: ${message.info.eval_count ?? 'N/A'}<br/>
  680. eval_duration: ${
  681. Math.round(((message.info.eval_duration ?? 0) / 1000000) * 100) / 100 ?? 'N/A'
  682. }ms<br/>
  683. approximate_total: ${approximateToHumanReadable(message.info.total_duration ?? 0)}`}
  684. placement="top"
  685. >
  686. <Tooltip content={$i18n.t('Generation Info')} placement="bottom">
  687. <button
  688. class=" {isLastMessage
  689. ? 'visible'
  690. : '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"
  691. on:click={() => {
  692. console.log(message);
  693. }}
  694. id="info-{message.id}"
  695. >
  696. <svg
  697. xmlns="http://www.w3.org/2000/svg"
  698. fill="none"
  699. viewBox="0 0 24 24"
  700. stroke-width="2.3"
  701. stroke="currentColor"
  702. class="w-4 h-4"
  703. >
  704. <path
  705. stroke-linecap="round"
  706. stroke-linejoin="round"
  707. 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"
  708. />
  709. </svg>
  710. </button>
  711. </Tooltip>
  712. </Tooltip>
  713. {/if}
  714. {#if !readOnly}
  715. {#if $config?.features.enable_message_rating ?? true}
  716. <Tooltip content={$i18n.t('Good Response')} placement="bottom">
  717. <button
  718. class="{isLastMessage
  719. ? 'visible'
  720. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg {(message
  721. ?.annotation?.rating ?? null) === 1
  722. ? 'bg-gray-100 dark:bg-gray-800'
  723. : ''} dark:hover:text-white hover:text-black transition"
  724. on:click={async () => {
  725. await rateMessage(message.id, 1);
  726. (model?.actions ?? [])
  727. .filter((action) => action?.__webui__ ?? false)
  728. .forEach((action) => {
  729. dispatch('action', {
  730. id: action.id,
  731. event: {
  732. id: 'good-response',
  733. data: {
  734. messageId: message.id
  735. }
  736. }
  737. });
  738. });
  739. showRateComment = true;
  740. window.setTimeout(() => {
  741. document
  742. .getElementById(`message-feedback-${message.id}`)
  743. ?.scrollIntoView();
  744. }, 0);
  745. }}
  746. >
  747. <svg
  748. stroke="currentColor"
  749. fill="none"
  750. stroke-width="2.3"
  751. viewBox="0 0 24 24"
  752. stroke-linecap="round"
  753. stroke-linejoin="round"
  754. class="w-4 h-4"
  755. xmlns="http://www.w3.org/2000/svg"
  756. ><path
  757. 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"
  758. /></svg
  759. >
  760. </button>
  761. </Tooltip>
  762. <Tooltip content={$i18n.t('Bad Response')} placement="bottom">
  763. <button
  764. class="{isLastMessage
  765. ? 'visible'
  766. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg {(message
  767. ?.annotation?.rating ?? null) === -1
  768. ? 'bg-gray-100 dark:bg-gray-800'
  769. : ''} dark:hover:text-white hover:text-black transition"
  770. on:click={async () => {
  771. await rateMessage(message.id, -1);
  772. (model?.actions ?? [])
  773. .filter((action) => action?.__webui__ ?? false)
  774. .forEach((action) => {
  775. dispatch('action', {
  776. id: action.id,
  777. event: {
  778. id: 'bad-response',
  779. data: {
  780. messageId: message.id
  781. }
  782. }
  783. });
  784. });
  785. showRateComment = true;
  786. window.setTimeout(() => {
  787. document
  788. .getElementById(`message-feedback-${message.id}`)
  789. ?.scrollIntoView();
  790. }, 0);
  791. }}
  792. >
  793. <svg
  794. stroke="currentColor"
  795. fill="none"
  796. stroke-width="2.3"
  797. viewBox="0 0 24 24"
  798. stroke-linecap="round"
  799. stroke-linejoin="round"
  800. class="w-4 h-4"
  801. xmlns="http://www.w3.org/2000/svg"
  802. ><path
  803. 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"
  804. /></svg
  805. >
  806. </button>
  807. </Tooltip>
  808. {/if}
  809. {#if isLastMessage}
  810. <Tooltip content={$i18n.t('Continue Response')} placement="bottom">
  811. <button
  812. type="button"
  813. id="continue-response-button"
  814. class="{isLastMessage
  815. ? 'visible'
  816. : 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition regenerate-response-button"
  817. on:click={() => {
  818. continueGeneration();
  819. (model?.actions ?? [])
  820. .filter((action) => action?.__webui__ ?? false)
  821. .forEach((action) => {
  822. dispatch('action', {
  823. id: action.id,
  824. event: {
  825. id: 'continue-response',
  826. data: {
  827. messageId: message.id
  828. }
  829. }
  830. });
  831. });
  832. }}
  833. >
  834. <svg
  835. xmlns="http://www.w3.org/2000/svg"
  836. fill="none"
  837. viewBox="0 0 24 24"
  838. stroke-width="2.3"
  839. stroke="currentColor"
  840. class="w-4 h-4"
  841. >
  842. <path
  843. stroke-linecap="round"
  844. stroke-linejoin="round"
  845. d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
  846. />
  847. <path
  848. stroke-linecap="round"
  849. stroke-linejoin="round"
  850. 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"
  851. />
  852. </svg>
  853. </button>
  854. </Tooltip>
  855. <Tooltip content={$i18n.t('Regenerate')} placement="bottom">
  856. <button
  857. type="button"
  858. class="{isLastMessage
  859. ? 'visible'
  860. : '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"
  861. on:click={() => {
  862. showRateComment = false;
  863. regenerateResponse(message);
  864. (model?.actions ?? [])
  865. .filter((action) => action?.__webui__ ?? false)
  866. .forEach((action) => {
  867. dispatch('action', {
  868. id: action.id,
  869. event: {
  870. id: 'regenerate-response',
  871. data: {
  872. messageId: message.id
  873. }
  874. }
  875. });
  876. });
  877. }}
  878. >
  879. <svg
  880. xmlns="http://www.w3.org/2000/svg"
  881. fill="none"
  882. viewBox="0 0 24 24"
  883. stroke-width="2.3"
  884. stroke="currentColor"
  885. class="w-4 h-4"
  886. >
  887. <path
  888. stroke-linecap="round"
  889. stroke-linejoin="round"
  890. 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"
  891. />
  892. </svg>
  893. </button>
  894. </Tooltip>
  895. {#each (model?.actions ?? []).filter((action) => !(action?.__webui__ ?? false)) as action}
  896. <Tooltip content={action.name} placement="bottom">
  897. <button
  898. type="button"
  899. class="{isLastMessage
  900. ? 'visible'
  901. : '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"
  902. on:click={() => {
  903. dispatch('action', action.id);
  904. }}
  905. >
  906. {#if action.icon_url}
  907. <img
  908. src={action.icon_url}
  909. class="w-4 h-4 {action.icon_url.includes('svg')
  910. ? 'dark:invert-[80%]'
  911. : ''}"
  912. style="fill: currentColor;"
  913. alt={action.name}
  914. />
  915. {:else}
  916. <Sparkles strokeWidth="2.1" className="size-4" />
  917. {/if}
  918. </button>
  919. </Tooltip>
  920. {/each}
  921. {/if}
  922. {/if}
  923. {/if}
  924. </div>
  925. {/if}
  926. {#if message.done && showRateComment}
  927. <RateComment
  928. messageId={message.id}
  929. bind:show={showRateComment}
  930. bind:message
  931. on:submit={(e) => {
  932. updateChatMessages();
  933. (model?.actions ?? [])
  934. .filter((action) => action?.__webui__ ?? false)
  935. .forEach((action) => {
  936. dispatch('action', {
  937. id: action.id,
  938. event: {
  939. id: 'rate-comment',
  940. data: {
  941. messageId: message.id,
  942. comment: e.detail.comment,
  943. reason: e.detail.reason
  944. }
  945. }
  946. });
  947. });
  948. }}
  949. />
  950. {/if}
  951. {/if}
  952. </div>
  953. </div>
  954. </div>
  955. {/key}
  956. <style>
  957. .buttons::-webkit-scrollbar {
  958. display: none; /* for Chrome, Safari and Opera */
  959. }
  960. .buttons {
  961. -ms-overflow-style: none; /* IE and Edge */
  962. scrollbar-width: none; /* Firefox */
  963. }
  964. @keyframes shimmer {
  965. 0% {
  966. background-position: 200% 0;
  967. }
  968. 100% {
  969. background-position: -200% 0;
  970. }
  971. }
  972. .shimmer {
  973. background: linear-gradient(90deg, #9a9b9e 25%, #2a2929 50%, #9a9b9e 75%);
  974. background-size: 200% 100%;
  975. background-clip: text;
  976. -webkit-background-clip: text;
  977. -webkit-text-fill-color: transparent;
  978. animation: shimmer 4s linear infinite;
  979. color: #818286; /* Fallback color */
  980. }
  981. :global(.dark) .shimmer {
  982. background: linear-gradient(90deg, #818286 25%, #eae5e5 50%, #818286 75%);
  983. background-size: 200% 100%;
  984. background-clip: text;
  985. -webkit-background-clip: text;
  986. -webkit-text-fill-color: transparent;
  987. animation: shimmer 4s linear infinite;
  988. color: #a1a3a7; /* Darker fallback color for dark mode */
  989. }
  990. @keyframes smoothFadeIn {
  991. 0% {
  992. opacity: 0;
  993. transform: translateY(-10px);
  994. }
  995. 100% {
  996. opacity: 1;
  997. transform: translateY(0);
  998. }
  999. }
  1000. .status-description {
  1001. animation: smoothFadeIn 0.2s forwards;
  1002. }
  1003. </style>