ResponseMessage.svelte 32 KB

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