ResponseMessage.svelte 31 KB

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