ResponseMessage.svelte 36 KB

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