ResponseMessage.svelte 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <script lang="ts">
  2. import dayjs from 'dayjs';
  3. import { marked } from 'marked';
  4. import { settings, voices } from '$lib/stores';
  5. import tippy from 'tippy.js';
  6. import auto_render from 'katex/dist/contrib/auto-render.mjs';
  7. import 'katex/dist/katex.min.css';
  8. import { onMount, tick } from 'svelte';
  9. import Name from './Name.svelte';
  10. import ProfileImage from './ProfileImage.svelte';
  11. import Skeleton from './Skeleton.svelte';
  12. import CodeBlock from './CodeBlock.svelte';
  13. export let modelfiles = [];
  14. export let message;
  15. export let siblings;
  16. export let isLastMessage = true;
  17. export let confirmEditResponseMessage: Function;
  18. export let showPreviousMessage: Function;
  19. export let showNextMessage: Function;
  20. export let rateMessage: Function;
  21. export let copyToClipboard: Function;
  22. export let regenerateResponse: Function;
  23. let edit = false;
  24. let editedContent = '';
  25. let tooltipInstance = null;
  26. let speaking = null;
  27. $: tokens = marked.lexer(message.content);
  28. const renderer = new marked.Renderer();
  29. // For code blocks with simple backticks
  30. renderer.codespan = (code) => {
  31. return `<code>${code.replaceAll('&amp;', '&')}</code>`;
  32. };
  33. const { extensions, ...defaults } = marked.getDefaults() as marked.MarkedOptions & {
  34. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  35. extensions: any;
  36. };
  37. $: if (message) {
  38. renderStyling();
  39. }
  40. const renderStyling = async () => {
  41. await tick();
  42. if (tooltipInstance) {
  43. tooltipInstance[0].destroy();
  44. }
  45. renderLatex();
  46. if (message.info) {
  47. tooltipInstance = tippy(`#info-${message.id}`, {
  48. content: `<span class="text-xs" id="tooltip-${message.id}">token/s: ${
  49. `${
  50. Math.round(
  51. ((message.info.eval_count ?? 0) / (message.info.eval_duration / 1000000000)) * 100
  52. ) / 100
  53. } tokens` ?? 'N/A'
  54. }<br/>
  55. total_duration: ${
  56. Math.round(((message.info.total_duration ?? 0) / 1000000) * 100) / 100 ??
  57. 'N/A'
  58. }ms<br/>
  59. load_duration: ${
  60. Math.round(((message.info.load_duration ?? 0) / 1000000) * 100) / 100 ?? 'N/A'
  61. }ms<br/>
  62. prompt_eval_count: ${message.info.prompt_eval_count ?? 'N/A'}<br/>
  63. prompt_eval_duration: ${
  64. Math.round(((message.info.prompt_eval_duration ?? 0) / 1000000) * 100) /
  65. 100 ?? 'N/A'
  66. }ms<br/>
  67. eval_count: ${message.info.eval_count ?? 'N/A'}<br/>
  68. eval_duration: ${
  69. Math.round(((message.info.eval_duration ?? 0) / 1000000) * 100) / 100 ?? 'N/A'
  70. }ms</span>`,
  71. allowHTML: true
  72. });
  73. }
  74. };
  75. const renderLatex = () => {
  76. let chatMessageElements = document.getElementsByClassName('chat-assistant');
  77. // let lastChatMessageElement = chatMessageElements[chatMessageElements.length - 1];
  78. for (const element of chatMessageElements) {
  79. auto_render(element, {
  80. // customised options
  81. // • auto-render specific keys, e.g.:
  82. delimiters: [
  83. { left: '$$', right: '$$', display: true },
  84. // { left: '$', right: '$', display: false },
  85. { left: '\\(', right: '\\)', display: true },
  86. { left: '\\[', right: '\\]', display: true }
  87. ],
  88. // • rendering keys, e.g.:
  89. throwOnError: false
  90. });
  91. }
  92. };
  93. const toggleSpeakMessage = async () => {
  94. if (speaking) {
  95. speechSynthesis.cancel();
  96. speaking = null;
  97. } else {
  98. speaking = true;
  99. const speak = new SpeechSynthesisUtterance(message.content);
  100. const voice = $voices?.filter((v) => v.name === $settings?.speakVoice)?.at(0) ?? undefined;
  101. speak.voice = voice;
  102. speechSynthesis.speak(speak);
  103. }
  104. };
  105. const editMessageHandler = async () => {
  106. edit = true;
  107. editedContent = message.content;
  108. await tick();
  109. const editElement = document.getElementById(`message-edit-${message.id}`);
  110. editElement.style.height = '';
  111. editElement.style.height = `${editElement.scrollHeight}px`;
  112. };
  113. const editMessageConfirmHandler = async () => {
  114. confirmEditResponseMessage(message.id, editedContent);
  115. edit = false;
  116. editedContent = '';
  117. await tick();
  118. renderStyling();
  119. };
  120. const cancelEditMessage = async () => {
  121. edit = false;
  122. editedContent = '';
  123. await tick();
  124. renderStyling();
  125. };
  126. onMount(async () => {
  127. await tick();
  128. renderStyling();
  129. });
  130. </script>
  131. {#key message.id}
  132. <div class=" flex w-full message-{message.id}">
  133. <ProfileImage src={modelfiles[message.model]?.imageUrl ?? '/favicon.png'} />
  134. <div class="w-full overflow-hidden">
  135. <Name>
  136. {#if message.model in modelfiles}
  137. {modelfiles[message.model]?.title}
  138. {:else}
  139. Ollama <span class=" text-gray-500 text-sm font-medium"
  140. >{message.model ? ` ${message.model}` : ''}</span
  141. >
  142. {/if}
  143. {#if message.timestamp}
  144. <span class=" invisible group-hover:visible text-gray-400 text-xs font-medium">
  145. {dayjs(message.timestamp * 1000).format('DD/MM/YYYY HH:MM')}
  146. </span>
  147. {/if}
  148. </Name>
  149. {#if message.content === ''}
  150. <Skeleton />
  151. {:else}
  152. <div
  153. class="prose chat-{message.role} w-full max-w-full dark:prose-invert prose-headings:my-0 prose-p:my-0 prose-p:-mb-4 prose-pre:my-0 prose-table:my-0 prose-blockquote:my-0 prose-img:my-0 prose-ul:-my-4 prose-ol:-my-4 prose-li:-my-3 prose-ul:-mb-6 prose-ol:-mb-6 prose-li:-mb-4 whitespace-pre-line"
  154. >
  155. <div>
  156. {#if edit === true}
  157. <div class=" w-full">
  158. <textarea
  159. id="message-edit-{message.id}"
  160. class=" bg-transparent outline-none w-full resize-none"
  161. bind:value={editedContent}
  162. on:input={(e) => {
  163. e.target.style.height = `${e.target.scrollHeight}px`;
  164. }}
  165. />
  166. <div class=" mt-2 mb-1 flex justify-center space-x-2 text-sm font-medium">
  167. <button
  168. class="px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-gray-100 transition rounded-lg"
  169. on:click={() => {
  170. editMessageConfirmHandler();
  171. }}
  172. >
  173. Save
  174. </button>
  175. <button
  176. class=" px-4 py-2 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-100 transition outline outline-1 outline-gray-200 dark:outline-gray-600 rounded-lg"
  177. on:click={() => {
  178. cancelEditMessage();
  179. }}
  180. >
  181. Cancel
  182. </button>
  183. </div>
  184. </div>
  185. {:else}
  186. <div class="w-full">
  187. {#if message?.error === true}
  188. <div
  189. class="flex mt-2 mb-4 space-x-2 border px-4 py-3 border-red-800 bg-red-800/30 font-medium rounded-lg"
  190. >
  191. <svg
  192. xmlns="http://www.w3.org/2000/svg"
  193. fill="none"
  194. viewBox="0 0 24 24"
  195. stroke-width="1.5"
  196. stroke="currentColor"
  197. class="w-5 h-5 self-center"
  198. >
  199. <path
  200. stroke-linecap="round"
  201. stroke-linejoin="round"
  202. d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z"
  203. />
  204. </svg>
  205. <div class=" self-center">
  206. {message.content}
  207. </div>
  208. </div>
  209. {:else}
  210. {#each tokens as token}
  211. {#if token.type === 'code'}
  212. <!-- {token.text} -->
  213. <CodeBlock lang={token.lang} code={token.text} />
  214. {:else}
  215. {@html marked.parse(token.raw, {
  216. ...defaults,
  217. gfm: true,
  218. breaks: true,
  219. renderer
  220. })}
  221. {/if}
  222. {/each}
  223. <!-- {@html marked(message.content.replaceAll('\\', '\\\\'))} -->
  224. {/if}
  225. {#if message.done}
  226. <div class=" flex justify-start space-x-1 -mt-2">
  227. {#if siblings.length > 1}
  228. <div class="flex self-center">
  229. <button
  230. class="self-center"
  231. on:click={() => {
  232. showPreviousMessage(message);
  233. }}
  234. >
  235. <svg
  236. xmlns="http://www.w3.org/2000/svg"
  237. viewBox="0 0 20 20"
  238. fill="currentColor"
  239. class="w-4 h-4"
  240. >
  241. <path
  242. fill-rule="evenodd"
  243. d="M12.79 5.23a.75.75 0 01-.02 1.06L8.832 10l3.938 3.71a.75.75 0 11-1.04 1.08l-4.5-4.25a.75.75 0 010-1.08l4.5-4.25a.75.75 0 011.06.02z"
  244. clip-rule="evenodd"
  245. />
  246. </svg>
  247. </button>
  248. <div class="text-xs font-bold self-center">
  249. {siblings.indexOf(message.id) + 1} / {siblings.length}
  250. </div>
  251. <button
  252. class="self-center"
  253. on:click={() => {
  254. showNextMessage(message);
  255. }}
  256. >
  257. <svg
  258. xmlns="http://www.w3.org/2000/svg"
  259. viewBox="0 0 20 20"
  260. fill="currentColor"
  261. class="w-4 h-4"
  262. >
  263. <path
  264. fill-rule="evenodd"
  265. d="M7.21 14.77a.75.75 0 01.02-1.06L11.168 10 7.23 6.29a.75.75 0 111.04-1.08l4.5 4.25a.75.75 0 010 1.08l-4.5 4.25a.75.75 0 01-1.06-.02z"
  266. clip-rule="evenodd"
  267. />
  268. </svg>
  269. </button>
  270. </div>
  271. {/if}
  272. <button
  273. class="{isLastMessage
  274. ? 'visible'
  275. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition"
  276. on:click={() => {
  277. editMessageHandler();
  278. }}
  279. >
  280. <svg
  281. xmlns="http://www.w3.org/2000/svg"
  282. fill="none"
  283. viewBox="0 0 24 24"
  284. stroke-width="1.5"
  285. stroke="currentColor"
  286. class="w-4 h-4"
  287. >
  288. <path
  289. stroke-linecap="round"
  290. stroke-linejoin="round"
  291. 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"
  292. />
  293. </svg>
  294. </button>
  295. <button
  296. class="{isLastMessage
  297. ? 'visible'
  298. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition copy-response-button"
  299. on:click={() => {
  300. copyToClipboard(message.content);
  301. }}
  302. >
  303. <svg
  304. xmlns="http://www.w3.org/2000/svg"
  305. fill="none"
  306. viewBox="0 0 24 24"
  307. stroke-width="1.5"
  308. stroke="currentColor"
  309. class="w-4 h-4"
  310. >
  311. <path
  312. stroke-linecap="round"
  313. stroke-linejoin="round"
  314. 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"
  315. />
  316. </svg>
  317. </button>
  318. <button
  319. class="{isLastMessage
  320. ? 'visible'
  321. : 'invisible group-hover:visible'} p-1 rounded {message.rating === 1
  322. ? 'bg-gray-100 dark:bg-gray-900'
  323. : ''} transition"
  324. on:click={() => {
  325. rateMessage(message.id, 1);
  326. }}
  327. >
  328. <svg
  329. stroke="currentColor"
  330. fill="none"
  331. stroke-width="2"
  332. viewBox="0 0 24 24"
  333. stroke-linecap="round"
  334. stroke-linejoin="round"
  335. class="w-4 h-4"
  336. xmlns="http://www.w3.org/2000/svg"
  337. ><path
  338. 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"
  339. /></svg
  340. >
  341. </button>
  342. <button
  343. class="{isLastMessage
  344. ? 'visible'
  345. : 'invisible group-hover:visible'} p-1 rounded {message.rating === -1
  346. ? 'bg-gray-100 dark:bg-gray-900'
  347. : ''} transition"
  348. on:click={() => {
  349. rateMessage(message.id, -1);
  350. }}
  351. >
  352. <svg
  353. stroke="currentColor"
  354. fill="none"
  355. stroke-width="2"
  356. viewBox="0 0 24 24"
  357. stroke-linecap="round"
  358. stroke-linejoin="round"
  359. class="w-4 h-4"
  360. xmlns="http://www.w3.org/2000/svg"
  361. ><path
  362. 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"
  363. /></svg
  364. >
  365. </button>
  366. <button
  367. class="{isLastMessage
  368. ? 'visible'
  369. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition"
  370. on:click={() => {
  371. toggleSpeakMessage(message);
  372. }}
  373. >
  374. {#if speaking}
  375. <svg
  376. xmlns="http://www.w3.org/2000/svg"
  377. fill="none"
  378. viewBox="0 0 24 24"
  379. stroke-width="1.5"
  380. stroke="currentColor"
  381. class="w-4 h-4"
  382. >
  383. <path
  384. stroke-linecap="round"
  385. stroke-linejoin="round"
  386. 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"
  387. />
  388. </svg>
  389. {:else}
  390. <svg
  391. xmlns="http://www.w3.org/2000/svg"
  392. fill="none"
  393. viewBox="0 0 24 24"
  394. stroke-width="1.5"
  395. stroke="currentColor"
  396. class="w-4 h-4"
  397. >
  398. <path
  399. stroke-linecap="round"
  400. stroke-linejoin="round"
  401. 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"
  402. />
  403. </svg>
  404. {/if}
  405. </button>
  406. {#if message.info}
  407. <button
  408. class=" {isLastMessage
  409. ? 'visible'
  410. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition whitespace-pre-wrap"
  411. on:click={() => {
  412. console.log(message);
  413. }}
  414. id="info-{message.id}"
  415. >
  416. <svg
  417. xmlns="http://www.w3.org/2000/svg"
  418. fill="none"
  419. viewBox="0 0 24 24"
  420. stroke-width="1.5"
  421. stroke="currentColor"
  422. class="w-4 h-4"
  423. >
  424. <path
  425. stroke-linecap="round"
  426. stroke-linejoin="round"
  427. 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"
  428. />
  429. </svg>
  430. </button>
  431. {/if}
  432. {#if isLastMessage}
  433. <button
  434. type="button"
  435. class="{isLastMessage
  436. ? 'visible'
  437. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition regenerate-response-button"
  438. on:click={regenerateResponse}
  439. >
  440. <svg
  441. xmlns="http://www.w3.org/2000/svg"
  442. fill="none"
  443. viewBox="0 0 24 24"
  444. stroke-width="1.5"
  445. stroke="currentColor"
  446. class="w-4 h-4"
  447. >
  448. <path
  449. stroke-linecap="round"
  450. stroke-linejoin="round"
  451. 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"
  452. />
  453. </svg>
  454. </button>
  455. {/if}
  456. </div>
  457. {/if}
  458. </div>
  459. {/if}
  460. </div>
  461. </div>
  462. {/if}
  463. </div>
  464. </div>
  465. {/key}