ResponseMessage.svelte 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <script lang="ts">
  2. import { marked } from 'marked';
  3. import tippy from 'tippy.js';
  4. import hljs from 'highlight.js';
  5. import 'highlight.js/styles/github-dark.min.css';
  6. import auto_render from 'katex/dist/contrib/auto-render.mjs';
  7. import 'katex/dist/katex.min.css';
  8. import Name from './Name.svelte';
  9. import ProfileImage from './ProfileImage.svelte';
  10. import Skeleton from './Skeleton.svelte';
  11. import { onMount, tick } from 'svelte';
  12. export let modelfiles = [];
  13. export let message;
  14. export let siblings;
  15. export let isLastMessage = true;
  16. export let confirmEditResponseMessage: Function;
  17. export let showPreviousMessage: Function;
  18. export let showNextMessage: Function;
  19. export let rateMessage: Function;
  20. export let copyToClipboard: Function;
  21. export let regenerateResponse: Function;
  22. let edit = false;
  23. let editedContent = '';
  24. let tooltipInstance = null;
  25. let speaking = null;
  26. $: if (message) {
  27. edit = false;
  28. editedContent = '';
  29. renderStyling();
  30. }
  31. const renderStyling = async () => {
  32. await tick();
  33. if (tooltipInstance) {
  34. tooltipInstance[0].destroy();
  35. }
  36. renderLatex();
  37. hljs.highlightAll();
  38. createCopyCodeBlockButton();
  39. if (message.info) {
  40. tooltipInstance = tippy(`#info-${message.id}`, {
  41. content: `<span class="text-xs" id="tooltip-${message.id}">token/s: ${
  42. `${
  43. Math.round(
  44. ((message.info.eval_count ?? 0) / (message.info.eval_duration / 1000000000)) * 100
  45. ) / 100
  46. } tokens` ?? 'N/A'
  47. }<br/>
  48. total_duration: ${
  49. Math.round(((message.info.total_duration ?? 0) / 1000000) * 100) / 100 ??
  50. 'N/A'
  51. }ms<br/>
  52. load_duration: ${
  53. Math.round(((message.info.load_duration ?? 0) / 1000000) * 100) / 100 ?? 'N/A'
  54. }ms<br/>
  55. prompt_eval_count: ${message.info.prompt_eval_count ?? 'N/A'}<br/>
  56. prompt_eval_duration: ${
  57. Math.round(((message.info.prompt_eval_duration ?? 0) / 1000000) * 100) /
  58. 100 ?? 'N/A'
  59. }ms<br/>
  60. eval_count: ${message.info.eval_count ?? 'N/A'}<br/>
  61. eval_duration: ${
  62. Math.round(((message.info.eval_duration ?? 0) / 1000000) * 100) / 100 ?? 'N/A'
  63. }ms</span>`,
  64. allowHTML: true
  65. });
  66. }
  67. };
  68. const createCopyCodeBlockButton = () => {
  69. // use a class selector if available
  70. let blocks = document.querySelectorAll('pre');
  71. blocks.forEach((block) => {
  72. // only add button if browser supports Clipboard API
  73. if (block.childNodes.length < 2 && block.id !== 'user-message') {
  74. let code = block.querySelector('code');
  75. code.style.borderTopRightRadius = 0;
  76. code.style.borderTopLeftRadius = 0;
  77. code.style.whiteSpace = 'pre';
  78. let topBarDiv = document.createElement('div');
  79. topBarDiv.style.backgroundColor = '#202123';
  80. topBarDiv.style.overflowX = 'auto';
  81. topBarDiv.style.display = 'flex';
  82. topBarDiv.style.justifyContent = 'space-between';
  83. topBarDiv.style.padding = '0 1rem';
  84. topBarDiv.style.paddingTop = '4px';
  85. topBarDiv.style.borderTopRightRadius = '8px';
  86. topBarDiv.style.borderTopLeftRadius = '8px';
  87. let langDiv = document.createElement('div');
  88. let codeClassNames = code?.className.split(' ');
  89. langDiv.textContent =
  90. codeClassNames[0] === 'hljs' ? codeClassNames[1].slice(9) : codeClassNames[0].slice(9);
  91. langDiv.style.color = 'white';
  92. langDiv.style.margin = '4px';
  93. langDiv.style.fontSize = '0.75rem';
  94. let button = document.createElement('button');
  95. button.className = 'copy-code-button';
  96. button.textContent = 'Copy Code';
  97. button.style.background = 'none';
  98. button.style.fontSize = '0.75rem';
  99. button.style.border = 'none';
  100. button.style.margin = '4px';
  101. button.style.cursor = 'pointer';
  102. button.style.color = '#ddd';
  103. button.addEventListener('click', () => copyCode(block, button));
  104. topBarDiv.appendChild(langDiv);
  105. topBarDiv.appendChild(button);
  106. block.prepend(topBarDiv);
  107. }
  108. });
  109. async function copyCode(block, button) {
  110. let code = block.querySelector('code');
  111. let text = code.innerText;
  112. await copyToClipboard(text);
  113. // visual feedback that task is completed
  114. button.innerText = 'Copied!';
  115. setTimeout(() => {
  116. button.innerText = 'Copy Code';
  117. }, 1000);
  118. }
  119. };
  120. const renderLatex = () => {
  121. let chatMessageElements = document.getElementsByClassName('chat-assistant');
  122. // let lastChatMessageElement = chatMessageElements[chatMessageElements.length - 1];
  123. for (const element of chatMessageElements) {
  124. auto_render(element, {
  125. // customised options
  126. // • auto-render specific keys, e.g.:
  127. delimiters: [
  128. { left: '$$', right: '$$', display: true },
  129. // { left: '$', right: '$', display: false },
  130. { left: '\\(', right: '\\)', display: true },
  131. { left: '\\[', right: '\\]', display: true }
  132. ],
  133. // • rendering keys, e.g.:
  134. throwOnError: false
  135. });
  136. }
  137. };
  138. const toggleSpeakMessage = async () => {
  139. if (speaking) {
  140. speechSynthesis.cancel();
  141. speaking = null;
  142. } else {
  143. speaking = true;
  144. const speak = new SpeechSynthesisUtterance(message.content);
  145. speechSynthesis.speak(speak);
  146. }
  147. };
  148. const editMessageHandler = async () => {
  149. edit = true;
  150. editedContent = message.content;
  151. await tick();
  152. const editElement = document.getElementById(`message-edit-${message.id}`);
  153. editElement.style.height = '';
  154. editElement.style.height = `${editElement.scrollHeight}px`;
  155. };
  156. const editMessageConfirmHandler = async () => {
  157. confirmEditResponseMessage(message.id, editedContent);
  158. edit = false;
  159. editedContent = '';
  160. await tick();
  161. renderStyling();
  162. };
  163. const cancelEditMessage = async () => {
  164. edit = false;
  165. editedContent = '';
  166. await tick();
  167. renderStyling();
  168. };
  169. onMount(async () => {
  170. await tick();
  171. renderStyling();
  172. });
  173. </script>
  174. <div class=" flex w-full message-{message.id}">
  175. <ProfileImage src={modelfiles[message.model]?.imageUrl ?? '/favicon.png'} />
  176. <div class="w-full overflow-hidden">
  177. <Name>
  178. {#if message.model in modelfiles}
  179. {modelfiles[message.model]?.title}
  180. {:else}
  181. Ollama <span class=" text-gray-500 text-sm font-medium"
  182. >{message.model ? ` ${message.model}` : ''}</span
  183. >
  184. {/if}
  185. </Name>
  186. {#if message.content === ''}
  187. <Skeleton />
  188. {:else}
  189. <div
  190. 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"
  191. >
  192. <div>
  193. {#if edit === true}
  194. <div class=" w-full">
  195. <textarea
  196. id="message-edit-{message.id}"
  197. class=" bg-transparent outline-none w-full resize-none"
  198. bind:value={editedContent}
  199. on:input={(e) => {
  200. e.target.style.height = `${e.target.scrollHeight}px`;
  201. }}
  202. />
  203. <div class=" mt-2 mb-1 flex justify-center space-x-2 text-sm font-medium">
  204. <button
  205. class="px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-gray-100 transition rounded-lg"
  206. on:click={() => {
  207. editMessageConfirmHandler();
  208. }}
  209. >
  210. Save
  211. </button>
  212. <button
  213. 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"
  214. on:click={() => {
  215. cancelEditMessage();
  216. }}
  217. >
  218. Cancel
  219. </button>
  220. </div>
  221. </div>
  222. {:else}
  223. <div class="w-full">
  224. {#if message?.error === true}
  225. <div
  226. 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"
  227. >
  228. <svg
  229. xmlns="http://www.w3.org/2000/svg"
  230. fill="none"
  231. viewBox="0 0 24 24"
  232. stroke-width="1.5"
  233. stroke="currentColor"
  234. class="w-5 h-5 self-center"
  235. >
  236. <path
  237. stroke-linecap="round"
  238. stroke-linejoin="round"
  239. d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z"
  240. />
  241. </svg>
  242. <div class=" self-center">
  243. {message.content}
  244. </div>
  245. </div>
  246. {:else}
  247. {@html marked(message.content.replace('\\\\', '\\\\\\'))}
  248. {/if}
  249. {#if message.done}
  250. <div class=" flex justify-start space-x-1 -mt-2">
  251. {#if siblings.length > 1}
  252. <div class="flex self-center">
  253. <button
  254. class="self-center"
  255. on:click={() => {
  256. showPreviousMessage(message);
  257. }}
  258. >
  259. <svg
  260. xmlns="http://www.w3.org/2000/svg"
  261. viewBox="0 0 20 20"
  262. fill="currentColor"
  263. class="w-4 h-4"
  264. >
  265. <path
  266. fill-rule="evenodd"
  267. 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"
  268. clip-rule="evenodd"
  269. />
  270. </svg>
  271. </button>
  272. <div class="text-xs font-bold self-center">
  273. {siblings.indexOf(message.id) + 1} / {siblings.length}
  274. </div>
  275. <button
  276. class="self-center"
  277. on:click={() => {
  278. showNextMessage(message);
  279. }}
  280. >
  281. <svg
  282. xmlns="http://www.w3.org/2000/svg"
  283. viewBox="0 0 20 20"
  284. fill="currentColor"
  285. class="w-4 h-4"
  286. >
  287. <path
  288. fill-rule="evenodd"
  289. 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"
  290. clip-rule="evenodd"
  291. />
  292. </svg>
  293. </button>
  294. </div>
  295. {/if}
  296. <button
  297. class="{isLastMessage
  298. ? 'visible'
  299. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition"
  300. on:click={() => {
  301. editMessageHandler();
  302. }}
  303. >
  304. <svg
  305. xmlns="http://www.w3.org/2000/svg"
  306. fill="none"
  307. viewBox="0 0 24 24"
  308. stroke-width="1.5"
  309. stroke="currentColor"
  310. class="w-4 h-4"
  311. >
  312. <path
  313. stroke-linecap="round"
  314. stroke-linejoin="round"
  315. 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"
  316. />
  317. </svg>
  318. </button>
  319. <button
  320. class="{isLastMessage
  321. ? 'visible'
  322. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition copy-response-button"
  323. on:click={() => {
  324. copyToClipboard(message.content);
  325. }}
  326. >
  327. <svg
  328. xmlns="http://www.w3.org/2000/svg"
  329. fill="none"
  330. viewBox="0 0 24 24"
  331. stroke-width="1.5"
  332. stroke="currentColor"
  333. class="w-4 h-4"
  334. >
  335. <path
  336. stroke-linecap="round"
  337. stroke-linejoin="round"
  338. 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"
  339. />
  340. </svg>
  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="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"
  363. /></svg
  364. >
  365. </button>
  366. <button
  367. class="{isLastMessage
  368. ? 'visible'
  369. : 'invisible group-hover:visible'} p-1 rounded {message.rating === -1
  370. ? 'bg-gray-100 dark:bg-gray-900'
  371. : ''} transition"
  372. on:click={() => {
  373. rateMessage(message.id, -1);
  374. }}
  375. >
  376. <svg
  377. stroke="currentColor"
  378. fill="none"
  379. stroke-width="2"
  380. viewBox="0 0 24 24"
  381. stroke-linecap="round"
  382. stroke-linejoin="round"
  383. class="w-4 h-4"
  384. xmlns="http://www.w3.org/2000/svg"
  385. ><path
  386. 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"
  387. /></svg
  388. >
  389. </button>
  390. <button
  391. class="{isLastMessage
  392. ? 'visible'
  393. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition"
  394. on:click={() => {
  395. toggleSpeakMessage(message);
  396. }}
  397. >
  398. {#if speaking}
  399. <svg
  400. xmlns="http://www.w3.org/2000/svg"
  401. fill="none"
  402. viewBox="0 0 24 24"
  403. stroke-width="1.5"
  404. stroke="currentColor"
  405. class="w-4 h-4"
  406. >
  407. <path
  408. stroke-linecap="round"
  409. stroke-linejoin="round"
  410. 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"
  411. />
  412. </svg>
  413. {:else}
  414. <svg
  415. xmlns="http://www.w3.org/2000/svg"
  416. fill="none"
  417. viewBox="0 0 24 24"
  418. stroke-width="1.5"
  419. stroke="currentColor"
  420. class="w-4 h-4"
  421. >
  422. <path
  423. stroke-linecap="round"
  424. stroke-linejoin="round"
  425. 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"
  426. />
  427. </svg>
  428. {/if}
  429. </button>
  430. {#if message.info}
  431. <button
  432. class=" {isLastMessage
  433. ? 'visible'
  434. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition whitespace-pre-wrap"
  435. on:click={() => {
  436. console.log(message);
  437. }}
  438. id="info-{message.id}"
  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="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"
  452. />
  453. </svg>
  454. </button>
  455. {/if}
  456. {#if isLastMessage}
  457. <button
  458. type="button"
  459. class="{isLastMessage
  460. ? 'visible'
  461. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition"
  462. on:click={regenerateResponse}
  463. >
  464. <svg
  465. xmlns="http://www.w3.org/2000/svg"
  466. fill="none"
  467. viewBox="0 0 24 24"
  468. stroke-width="1.5"
  469. stroke="currentColor"
  470. class="w-4 h-4"
  471. >
  472. <path
  473. stroke-linecap="round"
  474. stroke-linejoin="round"
  475. 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"
  476. />
  477. </svg>
  478. </button>
  479. {/if}
  480. </div>
  481. {/if}
  482. </div>
  483. {/if}
  484. </div>
  485. </div>
  486. {/if}
  487. </div>
  488. </div>