ResponseMessage.svelte 16 KB

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