UserMessage.svelte 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 { modelfiles, settings } from '$lib/stores';
  7. import Tooltip from '$lib/components/common/Tooltip.svelte';
  8. const i18n = getContext('i18n');
  9. const dispatch = createEventDispatcher();
  10. export let user;
  11. export let message;
  12. export let siblings;
  13. export let isFirstMessage: boolean;
  14. export let readOnly: boolean;
  15. export let confirmEditMessage: Function;
  16. export let showPreviousMessage: Function;
  17. export let showNextMessage: Function;
  18. export let copyToClipboard: Function;
  19. let edit = false;
  20. let editedContent = '';
  21. let messageEditTextAreaElement: HTMLTextAreaElement;
  22. const editMessageHandler = async () => {
  23. edit = true;
  24. editedContent = message.content;
  25. await tick();
  26. messageEditTextAreaElement.style.height = '';
  27. messageEditTextAreaElement.style.height = `${messageEditTextAreaElement.scrollHeight}px`;
  28. messageEditTextAreaElement?.focus();
  29. };
  30. const editMessageConfirmHandler = async () => {
  31. confirmEditMessage(message.id, editedContent);
  32. edit = false;
  33. editedContent = '';
  34. };
  35. const cancelEditMessage = () => {
  36. edit = false;
  37. editedContent = '';
  38. };
  39. const deleteMessageHandler = async () => {
  40. dispatch('delete', message.id);
  41. };
  42. </script>
  43. <div class=" flex w-full user-message">
  44. {#if !($settings?.chatBubble ?? true)}
  45. <ProfileImage
  46. src={message.user
  47. ? $modelfiles.find((modelfile) => modelfile.tagName === message.user)?.imageUrl ??
  48. '/user.png'
  49. : user?.profile_image_url ?? '/user.png'}
  50. />
  51. {/if}
  52. <div class="w-full overflow-hidden">
  53. {#if !($settings?.chatBubble ?? true)}
  54. <div>
  55. <Name>
  56. {#if message.user}
  57. {#if $modelfiles.map((modelfile) => modelfile.tagName).includes(message.user)}
  58. {$modelfiles.find((modelfile) => modelfile.tagName === message.user)?.title}
  59. {:else}
  60. {$i18n.t('You')}
  61. <span class=" text-gray-500 text-sm font-medium">{message?.user ?? ''}</span>
  62. {/if}
  63. {:else if $settings.showUsername}
  64. {user.name}
  65. {:else}
  66. {$i18n.t('You')}
  67. {/if}
  68. {#if message.timestamp}
  69. <span
  70. class=" invisible group-hover:visible text-gray-400 text-xs font-medium uppercase"
  71. >
  72. {dayjs(message.timestamp * 1000).format($i18n.t('h:mm a'))}
  73. </span>
  74. {/if}
  75. </Name>
  76. </div>
  77. {/if}
  78. <div
  79. class="prose chat-{message.role} w-full max-w-full flex flex-col justify-end 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"
  80. >
  81. {#if message.files}
  82. <div class="mt-2.5 mb-1 w-full flex flex-col justify-end overflow-x-auto gap-1 flex-wrap">
  83. {#each message.files as file}
  84. <div class={$settings?.chatBubble ?? true ? 'self-end' : ''}>
  85. {#if file.type === 'image'}
  86. <img src={file.url} alt="input" class=" max-h-96 rounded-lg" draggable="false" />
  87. {:else if file.type === 'doc'}
  88. <button
  89. class="h-16 w-72 flex items-center space-x-3 px-2.5 dark:bg-gray-850 rounded-xl border border-gray-200 dark:border-none text-left"
  90. type="button"
  91. on:click={() => {
  92. if (file?.url) {
  93. window.open(file?.url, '_blank').focus();
  94. }
  95. }}
  96. >
  97. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  98. <svg
  99. xmlns="http://www.w3.org/2000/svg"
  100. viewBox="0 0 24 24"
  101. fill="currentColor"
  102. class="w-6 h-6"
  103. >
  104. <path
  105. fill-rule="evenodd"
  106. d="M5.625 1.5c-1.036 0-1.875.84-1.875 1.875v17.25c0 1.035.84 1.875 1.875 1.875h12.75c1.035 0 1.875-.84 1.875-1.875V12.75A3.75 3.75 0 0 0 16.5 9h-1.875a1.875 1.875 0 0 1-1.875-1.875V5.25A3.75 3.75 0 0 0 9 1.5H5.625ZM7.5 15a.75.75 0 0 1 .75-.75h7.5a.75.75 0 0 1 0 1.5h-7.5A.75.75 0 0 1 7.5 15Zm.75 2.25a.75.75 0 0 0 0 1.5H12a.75.75 0 0 0 0-1.5H8.25Z"
  107. clip-rule="evenodd"
  108. />
  109. <path
  110. d="M12.971 1.816A5.23 5.23 0 0 1 14.25 5.25v1.875c0 .207.168.375.375.375H16.5a5.23 5.23 0 0 1 3.434 1.279 9.768 9.768 0 0 0-6.963-6.963Z"
  111. />
  112. </svg>
  113. </div>
  114. <div class="flex flex-col justify-center -space-y-0.5">
  115. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  116. {file.name}
  117. </div>
  118. <div class=" text-gray-500 text-sm">{$i18n.t('Document')}</div>
  119. </div>
  120. </button>
  121. {:else if file.type === 'collection'}
  122. <button
  123. class="h-16 w-72 flex items-center space-x-3 px-2.5 dark:bg-gray-600 rounded-xl border border-gray-200 dark:border-none text-left"
  124. type="button"
  125. >
  126. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  127. <svg
  128. xmlns="http://www.w3.org/2000/svg"
  129. viewBox="0 0 24 24"
  130. fill="currentColor"
  131. class="w-6 h-6"
  132. >
  133. <path
  134. d="M7.5 3.375c0-1.036.84-1.875 1.875-1.875h.375a3.75 3.75 0 0 1 3.75 3.75v1.875C13.5 8.161 14.34 9 15.375 9h1.875A3.75 3.75 0 0 1 21 12.75v3.375C21 17.16 20.16 18 19.125 18h-9.75A1.875 1.875 0 0 1 7.5 16.125V3.375Z"
  135. />
  136. <path
  137. d="M15 5.25a5.23 5.23 0 0 0-1.279-3.434 9.768 9.768 0 0 1 6.963 6.963A5.23 5.23 0 0 0 17.25 7.5h-1.875A.375.375 0 0 1 15 7.125V5.25ZM4.875 6H6v10.125A3.375 3.375 0 0 0 9.375 19.5H16.5v1.125c0 1.035-.84 1.875-1.875 1.875h-9.75A1.875 1.875 0 0 1 3 20.625V7.875C3 6.839 3.84 6 4.875 6Z"
  138. />
  139. </svg>
  140. </div>
  141. <div class="flex flex-col justify-center -space-y-0.5">
  142. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  143. {file?.title ?? `#${file.name}`}
  144. </div>
  145. <div class=" text-gray-500 text-sm">{$i18n.t('Collection')}</div>
  146. </div>
  147. </button>
  148. {:else if file.type === 'websearch'}
  149. <button
  150. class="h-16 w-[15rem] flex items-center space-x-3 px-2.5 dark:bg-gray-600 rounded-xl border border-gray-200 dark:border-none text-left"
  151. type="button"
  152. >
  153. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  154. <svg
  155. xmlns="http://www.w3.org/2000/svg"
  156. viewBox="0 0 24 24"
  157. fill="currentColor"
  158. class="w-6 h-6"
  159. >
  160. <path d="M11.625 16.5a1.875 1.875 0 1 0 0-3.75 1.875 1.875 0 0 0 0 3.75Z" />
  161. <path
  162. fill-rule="evenodd"
  163. d="M5.625 1.5H9a3.75 3.75 0 0 1 3.75 3.75v1.875c0 1.036.84 1.875 1.875 1.875H16.5a3.75 3.75 0 0 1 3.75 3.75v7.875c0 1.035-.84 1.875-1.875 1.875H5.625a1.875 1.875 0 0 1-1.875-1.875V3.375c0-1.036.84-1.875 1.875-1.875Zm6 16.5c.66 0 1.277-.19 1.797-.518l1.048 1.048a.75.75 0 0 0 1.06-1.06l-1.047-1.048A3.375 3.375 0 1 0 11.625 18Z"
  164. clip-rule="evenodd"
  165. />
  166. <path
  167. d="M14.25 5.25a5.23 5.23 0 0 0-1.279-3.434 9.768 9.768 0 0 1 6.963 6.963A5.23 5.23 0 0 0 16.5 7.5h-1.875a.375.375 0 0 1-.375-.375V5.25Z"
  168. />
  169. </svg>
  170. </div>
  171. <div class="flex flex-col justify-center -space-y-0.5">
  172. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  173. {file.name}
  174. </div>
  175. <div class=" text-gray-500 text-sm">{$i18n.t('Search Results')}</div>
  176. </div>
  177. </button>
  178. {/if}
  179. </div>
  180. {/each}
  181. </div>
  182. {/if}
  183. {#if edit === true}
  184. <div class=" w-full bg-gray-50 dark:bg-gray-800 rounded-3xl px-5 py-3 mb-2">
  185. <textarea
  186. id="message-edit-{message.id}"
  187. bind:this={messageEditTextAreaElement}
  188. class=" bg-transparent outline-none w-full resize-none"
  189. bind:value={editedContent}
  190. on:input={(e) => {
  191. e.target.style.height = '';
  192. e.target.style.height = `${e.target.scrollHeight}px`;
  193. }}
  194. on:keydown={(e) => {
  195. if (e.key === 'Escape') {
  196. document.getElementById('close-edit-message-button')?.click();
  197. }
  198. const isCmdOrCtrlPressed = e.metaKey || e.ctrlKey;
  199. const isEnterPressed = e.key === 'Enter';
  200. if (isCmdOrCtrlPressed && isEnterPressed) {
  201. document.getElementById('save-edit-message-button')?.click();
  202. }
  203. }}
  204. />
  205. <div class=" mt-2 mb-1 flex justify-end space-x-1.5 text-sm font-medium">
  206. <button
  207. id="close-edit-message-button"
  208. class=" px-4 py-2 bg-gray-900 hover:bg-gray-850 text-gray-100 transition rounded-3xl"
  209. on:click={() => {
  210. cancelEditMessage();
  211. }}
  212. >
  213. {$i18n.t('Cancel')}
  214. </button>
  215. <button
  216. id="save-edit-message-button"
  217. class="px-4 py-2 bg-white hover:bg-gray-100 text-gray-800 transition rounded-3xl"
  218. on:click={() => {
  219. editMessageConfirmHandler();
  220. }}
  221. >
  222. {$i18n.t('Send')}
  223. </button>
  224. </div>
  225. </div>
  226. {:else}
  227. <div class="w-full">
  228. <div class="flex {$settings?.chatBubble ?? true ? 'justify-end' : ''} mb-2">
  229. <div
  230. class="rounded-3xl {$settings?.chatBubble ?? true
  231. ? `max-w-[90%] px-5 py-2 bg-gray-50 dark:bg-gray-850 ${
  232. message.files ? 'rounded-tr-lg' : ''
  233. }`
  234. : ''} "
  235. >
  236. <pre id="user-message">{message.content}</pre>
  237. </div>
  238. </div>
  239. <div
  240. class=" flex {$settings?.chatBubble ?? true
  241. ? 'justify-end'
  242. : ''} space-x-1 text-gray-700 dark:text-gray-500"
  243. >
  244. {#if !($settings?.chatBubble ?? true)}
  245. {#if siblings.length > 1}
  246. <div class="flex self-center">
  247. <button
  248. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  249. on:click={() => {
  250. showPreviousMessage(message);
  251. }}
  252. >
  253. <svg
  254. xmlns="http://www.w3.org/2000/svg"
  255. fill="none"
  256. viewBox="0 0 24 24"
  257. stroke="currentColor"
  258. stroke-width="2.5"
  259. class="size-3.5"
  260. >
  261. <path
  262. stroke-linecap="round"
  263. stroke-linejoin="round"
  264. d="M15.75 19.5 8.25 12l7.5-7.5"
  265. />
  266. </svg>
  267. </button>
  268. <div class="text-sm tracking-widest font-semibold self-center dark:text-gray-100">
  269. {siblings.indexOf(message.id) + 1}/{siblings.length}
  270. </div>
  271. <button
  272. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  273. on:click={() => {
  274. showNextMessage(message);
  275. }}
  276. >
  277. <svg
  278. xmlns="http://www.w3.org/2000/svg"
  279. fill="none"
  280. viewBox="0 0 24 24"
  281. stroke="currentColor"
  282. stroke-width="2.5"
  283. class="size-3.5"
  284. >
  285. <path
  286. stroke-linecap="round"
  287. stroke-linejoin="round"
  288. d="m8.25 4.5 7.5 7.5-7.5 7.5"
  289. />
  290. </svg>
  291. </button>
  292. </div>
  293. {/if}
  294. {/if}
  295. {#if !readOnly}
  296. <Tooltip content={$i18n.t('Edit')} placement="bottom">
  297. <button
  298. class="invisible group-hover:visible p-1 rounded dark:hover:text-white hover:text-black transition edit-user-message-button"
  299. on:click={() => {
  300. editMessageHandler();
  301. }}
  302. >
  303. <svg
  304. xmlns="http://www.w3.org/2000/svg"
  305. fill="none"
  306. viewBox="0 0 24 24"
  307. stroke-width="2"
  308. stroke="currentColor"
  309. class="w-4 h-4"
  310. >
  311. <path
  312. stroke-linecap="round"
  313. stroke-linejoin="round"
  314. 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"
  315. />
  316. </svg>
  317. </button>
  318. </Tooltip>
  319. {/if}
  320. <Tooltip content={$i18n.t('Copy')} placement="bottom">
  321. <button
  322. class="invisible group-hover:visible p-1 rounded dark:hover:text-white hover:text-black transition"
  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="2"
  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. </Tooltip>
  343. {#if !isFirstMessage && !readOnly}
  344. <Tooltip content={$i18n.t('Delete')} placement="bottom">
  345. <button
  346. class="invisible group-hover:visible p-1 rounded dark:hover:text-white hover:text-black transition"
  347. on:click={() => {
  348. deleteMessageHandler();
  349. }}
  350. >
  351. <svg
  352. xmlns="http://www.w3.org/2000/svg"
  353. fill="none"
  354. viewBox="0 0 24 24"
  355. stroke-width="2"
  356. stroke="currentColor"
  357. class="w-4 h-4"
  358. >
  359. <path
  360. stroke-linecap="round"
  361. stroke-linejoin="round"
  362. 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"
  363. />
  364. </svg>
  365. </button>
  366. </Tooltip>
  367. {/if}
  368. {#if $settings?.chatBubble ?? true}
  369. {#if siblings.length > 1}
  370. <div class="flex self-center">
  371. <button
  372. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  373. on:click={() => {
  374. showPreviousMessage(message);
  375. }}
  376. >
  377. <svg
  378. xmlns="http://www.w3.org/2000/svg"
  379. fill="none"
  380. viewBox="0 0 24 24"
  381. stroke="currentColor"
  382. stroke-width="2.5"
  383. class="size-3.5"
  384. >
  385. <path
  386. stroke-linecap="round"
  387. stroke-linejoin="round"
  388. d="M15.75 19.5 8.25 12l7.5-7.5"
  389. />
  390. </svg>
  391. </button>
  392. <div class="text-sm tracking-widest font-semibold self-center dark:text-gray-100">
  393. {siblings.indexOf(message.id) + 1}/{siblings.length}
  394. </div>
  395. <button
  396. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  397. on:click={() => {
  398. showNextMessage(message);
  399. }}
  400. >
  401. <svg
  402. xmlns="http://www.w3.org/2000/svg"
  403. fill="none"
  404. viewBox="0 0 24 24"
  405. stroke="currentColor"
  406. stroke-width="2.5"
  407. class="size-3.5"
  408. >
  409. <path
  410. stroke-linecap="round"
  411. stroke-linejoin="round"
  412. d="m8.25 4.5 7.5 7.5-7.5 7.5"
  413. />
  414. </svg>
  415. </button>
  416. </div>
  417. {/if}
  418. {/if}
  419. </div>
  420. </div>
  421. {/if}
  422. </div>
  423. </div>
  424. </div>