UserMessage.svelte 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <script lang="ts">
  2. import dayjs from 'dayjs';
  3. import { tick, createEventDispatcher, getContext } from 'svelte';
  4. import Name from './Name.svelte';
  5. import ProfileImage from './ProfileImage.svelte';
  6. import { models, settings } from '$lib/stores';
  7. import Tooltip from '$lib/components/common/Tooltip.svelte';
  8. import { user as _user } from '$lib/stores';
  9. import { getFileContentById } from '$lib/apis/files';
  10. import FileItem from '$lib/components/common/FileItem.svelte';
  11. import { marked } from 'marked';
  12. import { processResponseContent, replaceTokens } from '$lib/utils';
  13. import MarkdownTokens from './MarkdownTokens.svelte';
  14. const i18n = getContext('i18n');
  15. const dispatch = createEventDispatcher();
  16. export let user;
  17. export let message;
  18. export let siblings;
  19. export let isFirstMessage: boolean;
  20. export let readOnly: boolean;
  21. export let confirmEditMessage: Function;
  22. export let showPreviousMessage: Function;
  23. export let showNextMessage: Function;
  24. export let copyToClipboard: Function;
  25. let edit = false;
  26. let editedContent = '';
  27. let messageEditTextAreaElement: HTMLTextAreaElement;
  28. const editMessageHandler = async () => {
  29. edit = true;
  30. editedContent = message.content;
  31. await tick();
  32. messageEditTextAreaElement.style.height = '';
  33. messageEditTextAreaElement.style.height = `${messageEditTextAreaElement.scrollHeight}px`;
  34. messageEditTextAreaElement?.focus();
  35. };
  36. const editMessageConfirmHandler = async () => {
  37. confirmEditMessage(message.id, editedContent);
  38. edit = false;
  39. editedContent = '';
  40. };
  41. const cancelEditMessage = () => {
  42. edit = false;
  43. editedContent = '';
  44. };
  45. const deleteMessageHandler = async () => {
  46. dispatch('delete', message.id);
  47. };
  48. </script>
  49. <div class=" flex w-full user-message" dir={$settings.chatDirection}>
  50. {#if !($settings?.chatBubble ?? true)}
  51. <ProfileImage
  52. src={message.user
  53. ? ($models.find((m) => m.id === message.user)?.info?.meta?.profile_image_url ?? '/user.png')
  54. : (user?.profile_image_url ?? '/user.png')}
  55. />
  56. {/if}
  57. <div class="w-full overflow-hidden pl-1">
  58. {#if !($settings?.chatBubble ?? true)}
  59. <div>
  60. <Name>
  61. {#if message.user}
  62. {$i18n.t('You')}
  63. <span class=" text-gray-500 text-sm font-medium">{message?.user ?? ''}</span>
  64. {:else if $settings.showUsername || $_user.name !== user.name}
  65. {user.name}
  66. {:else}
  67. {$i18n.t('You')}
  68. {/if}
  69. {#if message.timestamp}
  70. <span
  71. class=" invisible group-hover:visible text-gray-400 text-xs font-medium uppercase"
  72. >
  73. {dayjs(message.timestamp * 1000).format($i18n.t('h:mm a'))}
  74. </span>
  75. {/if}
  76. </Name>
  77. </div>
  78. {/if}
  79. <div
  80. class="prose chat-{message.role} w-full max-w-full dark:prose-invert prose-p:my-0 prose-img:my-1 prose-headings:my-1 prose-pre:my-0 prose-table:my-0 prose-blockquote:my-0 prose-ul:-my-0 prose-ol:-my-0 prose-li:-my-0 whitespace-pre-line"
  81. >
  82. {#if message.files}
  83. <div class="mt-2.5 mb-1 w-full flex flex-col justify-end overflow-x-auto gap-1 flex-wrap">
  84. {#each message.files as file}
  85. <div class={($settings?.chatBubble ?? true) ? 'self-end' : ''}>
  86. {#if file.type === 'image'}
  87. <img src={file.url} alt="input" class=" max-h-96 rounded-lg" draggable="false" />
  88. {:else}
  89. <FileItem
  90. url={file.url}
  91. name={file.name}
  92. type={file.type}
  93. size={file?.size}
  94. colorClassName="bg-white dark:bg-gray-850 "
  95. />
  96. {/if}
  97. </div>
  98. {/each}
  99. </div>
  100. {/if}
  101. {#if edit === true}
  102. <div class=" w-full bg-gray-50 dark:bg-gray-800 rounded-3xl px-5 py-3 mb-2">
  103. <textarea
  104. id="message-edit-{message.id}"
  105. bind:this={messageEditTextAreaElement}
  106. class=" bg-transparent outline-none w-full resize-none"
  107. bind:value={editedContent}
  108. on:input={(e) => {
  109. e.target.style.height = '';
  110. e.target.style.height = `${e.target.scrollHeight}px`;
  111. }}
  112. on:keydown={(e) => {
  113. if (e.key === 'Escape') {
  114. document.getElementById('close-edit-message-button')?.click();
  115. }
  116. const isCmdOrCtrlPressed = e.metaKey || e.ctrlKey;
  117. const isEnterPressed = e.key === 'Enter';
  118. if (isCmdOrCtrlPressed && isEnterPressed) {
  119. document.getElementById('save-edit-message-button')?.click();
  120. }
  121. }}
  122. />
  123. <div class=" mt-2 mb-1 flex justify-end space-x-1.5 text-sm font-medium">
  124. <button
  125. id="close-edit-message-button"
  126. 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"
  127. on:click={() => {
  128. cancelEditMessage();
  129. }}
  130. >
  131. {$i18n.t('Cancel')}
  132. </button>
  133. <button
  134. id="save-edit-message-button"
  135. 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"
  136. on:click={() => {
  137. editMessageConfirmHandler();
  138. }}
  139. >
  140. {$i18n.t('Send')}
  141. </button>
  142. </div>
  143. </div>
  144. {:else}
  145. <div class="w-full">
  146. <div class="flex {($settings?.chatBubble ?? true) ? 'justify-end pb-1' : 'w-full'}">
  147. <div
  148. class="rounded-3xl {($settings?.chatBubble ?? true)
  149. ? `max-w-[90%] px-5 py-2 bg-gray-50 dark:bg-gray-850 ${
  150. message.files ? 'rounded-tr-lg' : ''
  151. }`
  152. : ' w-full'}"
  153. >
  154. {#if message.content}
  155. <div class="">
  156. {#key message.id}
  157. <MarkdownTokens
  158. id={message.id}
  159. tokens={marked.lexer(processResponseContent(message?.content))}
  160. />
  161. {/key}
  162. </div>
  163. {/if}
  164. </div>
  165. </div>
  166. <div
  167. class=" flex {($settings?.chatBubble ?? true)
  168. ? 'justify-end'
  169. : ''} text-gray-600 dark:text-gray-500"
  170. >
  171. {#if !($settings?.chatBubble ?? true)}
  172. {#if siblings.length > 1}
  173. <div class="flex self-center" dir="ltr">
  174. <button
  175. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  176. on:click={() => {
  177. showPreviousMessage(message);
  178. }}
  179. >
  180. <svg
  181. xmlns="http://www.w3.org/2000/svg"
  182. fill="none"
  183. viewBox="0 0 24 24"
  184. stroke="currentColor"
  185. stroke-width="2.5"
  186. class="size-3.5"
  187. >
  188. <path
  189. stroke-linecap="round"
  190. stroke-linejoin="round"
  191. d="M15.75 19.5 8.25 12l7.5-7.5"
  192. />
  193. </svg>
  194. </button>
  195. <div class="text-sm tracking-widest font-semibold self-center dark:text-gray-100">
  196. {siblings.indexOf(message.id) + 1}/{siblings.length}
  197. </div>
  198. <button
  199. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  200. on:click={() => {
  201. showNextMessage(message);
  202. }}
  203. >
  204. <svg
  205. xmlns="http://www.w3.org/2000/svg"
  206. fill="none"
  207. viewBox="0 0 24 24"
  208. stroke="currentColor"
  209. stroke-width="2.5"
  210. class="size-3.5"
  211. >
  212. <path
  213. stroke-linecap="round"
  214. stroke-linejoin="round"
  215. d="m8.25 4.5 7.5 7.5-7.5 7.5"
  216. />
  217. </svg>
  218. </button>
  219. </div>
  220. {/if}
  221. {/if}
  222. {#if !readOnly}
  223. <Tooltip content={$i18n.t('Edit')} placement="bottom">
  224. <button
  225. class="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 edit-user-message-button"
  226. on:click={() => {
  227. editMessageHandler();
  228. }}
  229. >
  230. <svg
  231. xmlns="http://www.w3.org/2000/svg"
  232. fill="none"
  233. viewBox="0 0 24 24"
  234. stroke-width="2.3"
  235. stroke="currentColor"
  236. class="w-4 h-4"
  237. >
  238. <path
  239. stroke-linecap="round"
  240. stroke-linejoin="round"
  241. 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"
  242. />
  243. </svg>
  244. </button>
  245. </Tooltip>
  246. {/if}
  247. <Tooltip content={$i18n.t('Copy')} placement="bottom">
  248. <button
  249. class="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"
  250. on:click={() => {
  251. copyToClipboard(message.content);
  252. }}
  253. >
  254. <svg
  255. xmlns="http://www.w3.org/2000/svg"
  256. fill="none"
  257. viewBox="0 0 24 24"
  258. stroke-width="2.3"
  259. stroke="currentColor"
  260. class="w-4 h-4"
  261. >
  262. <path
  263. stroke-linecap="round"
  264. stroke-linejoin="round"
  265. 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"
  266. />
  267. </svg>
  268. </button>
  269. </Tooltip>
  270. {#if !isFirstMessage && !readOnly}
  271. <Tooltip content={$i18n.t('Delete')} placement="bottom">
  272. <button
  273. class="invisible group-hover:visible p-1 rounded dark:hover:text-white hover:text-black transition"
  274. on:click={() => {
  275. deleteMessageHandler();
  276. }}
  277. >
  278. <svg
  279. xmlns="http://www.w3.org/2000/svg"
  280. fill="none"
  281. viewBox="0 0 24 24"
  282. stroke-width="2"
  283. stroke="currentColor"
  284. class="w-4 h-4"
  285. >
  286. <path
  287. stroke-linecap="round"
  288. stroke-linejoin="round"
  289. d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0"
  290. />
  291. </svg>
  292. </button>
  293. </Tooltip>
  294. {/if}
  295. {#if $settings?.chatBubble ?? true}
  296. {#if siblings.length > 1}
  297. <div class="flex self-center" dir="ltr">
  298. <button
  299. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  300. on:click={() => {
  301. showPreviousMessage(message);
  302. }}
  303. >
  304. <svg
  305. xmlns="http://www.w3.org/2000/svg"
  306. fill="none"
  307. viewBox="0 0 24 24"
  308. stroke="currentColor"
  309. stroke-width="2.5"
  310. class="size-3.5"
  311. >
  312. <path
  313. stroke-linecap="round"
  314. stroke-linejoin="round"
  315. d="M15.75 19.5 8.25 12l7.5-7.5"
  316. />
  317. </svg>
  318. </button>
  319. <div class="text-sm tracking-widest font-semibold self-center dark:text-gray-100">
  320. {siblings.indexOf(message.id) + 1}/{siblings.length}
  321. </div>
  322. <button
  323. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  324. on:click={() => {
  325. showNextMessage(message);
  326. }}
  327. >
  328. <svg
  329. xmlns="http://www.w3.org/2000/svg"
  330. fill="none"
  331. viewBox="0 0 24 24"
  332. stroke="currentColor"
  333. stroke-width="2.5"
  334. class="size-3.5"
  335. >
  336. <path
  337. stroke-linecap="round"
  338. stroke-linejoin="round"
  339. d="m8.25 4.5 7.5 7.5-7.5 7.5"
  340. />
  341. </svg>
  342. </button>
  343. </div>
  344. {/if}
  345. {/if}
  346. </div>
  347. </div>
  348. {/if}
  349. </div>
  350. </div>
  351. </div>