UserMessage.svelte 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. import { user as _user } from '$lib/stores';
  9. const i18n = getContext('i18n');
  10. const dispatch = createEventDispatcher();
  11. export let user;
  12. export let message;
  13. export let siblings;
  14. export let isFirstMessage: boolean;
  15. export let readOnly: boolean;
  16. export let confirmEditMessage: Function;
  17. export let showPreviousMessage: Function;
  18. export let showNextMessage: Function;
  19. export let copyToClipboard: Function;
  20. let edit = false;
  21. let editedContent = '';
  22. let messageEditTextAreaElement: HTMLTextAreaElement;
  23. const editMessageHandler = async () => {
  24. edit = true;
  25. editedContent = message.content;
  26. await tick();
  27. messageEditTextAreaElement.style.height = '';
  28. messageEditTextAreaElement.style.height = `${messageEditTextAreaElement.scrollHeight}px`;
  29. messageEditTextAreaElement?.focus();
  30. };
  31. const editMessageConfirmHandler = async () => {
  32. confirmEditMessage(message.id, editedContent);
  33. edit = false;
  34. editedContent = '';
  35. };
  36. const cancelEditMessage = () => {
  37. edit = false;
  38. editedContent = '';
  39. };
  40. const deleteMessageHandler = async () => {
  41. dispatch('delete', message.id);
  42. };
  43. </script>
  44. <div class=" flex w-full user-message" dir={$settings.chatDirection}>
  45. {#if !($settings?.chatBubble ?? true)}
  46. <ProfileImage
  47. src={message.user
  48. ? $modelfiles.find((modelfile) => modelfile.tagName === message.user)?.imageUrl ??
  49. '/user.png'
  50. : user?.profile_image_url ?? '/user.png'}
  51. />
  52. {/if}
  53. <div class="w-full overflow-hidden">
  54. {#if !($settings?.chatBubble ?? true)}
  55. <div>
  56. <Name>
  57. {#if message.user}
  58. {#if $modelfiles.map((modelfile) => modelfile.tagName).includes(message.user)}
  59. {$modelfiles.find((modelfile) => modelfile.tagName === message.user)?.title}
  60. {:else}
  61. {$i18n.t('You')}
  62. <span class=" text-gray-500 text-sm font-medium">{message?.user ?? ''}</span>
  63. {/if}
  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 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"
  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 if file.type === 'doc'}
  89. <button
  90. 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"
  91. type="button"
  92. on:click={() => {
  93. if (file?.url) {
  94. window.open(file?.url, '_blank').focus();
  95. }
  96. }}
  97. >
  98. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  99. <svg
  100. xmlns="http://www.w3.org/2000/svg"
  101. viewBox="0 0 24 24"
  102. fill="currentColor"
  103. class="w-6 h-6"
  104. >
  105. <path
  106. fill-rule="evenodd"
  107. 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"
  108. clip-rule="evenodd"
  109. />
  110. <path
  111. 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"
  112. />
  113. </svg>
  114. </div>
  115. <div class="flex flex-col justify-center -space-y-0.5">
  116. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  117. {file.name}
  118. </div>
  119. <div class=" text-gray-500 text-sm">{$i18n.t('Document')}</div>
  120. </div>
  121. </button>
  122. {:else if file.type === 'collection'}
  123. <button
  124. 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"
  125. type="button"
  126. >
  127. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  128. <svg
  129. xmlns="http://www.w3.org/2000/svg"
  130. viewBox="0 0 24 24"
  131. fill="currentColor"
  132. class="w-6 h-6"
  133. >
  134. <path
  135. 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"
  136. />
  137. <path
  138. 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"
  139. />
  140. </svg>
  141. </div>
  142. <div class="flex flex-col justify-center -space-y-0.5">
  143. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  144. {file?.title ?? `#${file.name}`}
  145. </div>
  146. <div class=" text-gray-500 text-sm">{$i18n.t('Collection')}</div>
  147. </div>
  148. </button>
  149. {/if}
  150. </div>
  151. {/each}
  152. </div>
  153. {/if}
  154. {#if edit === true}
  155. <div class=" w-full bg-gray-50 dark:bg-gray-800 rounded-3xl px-5 py-3 mb-2">
  156. <textarea
  157. id="message-edit-{message.id}"
  158. bind:this={messageEditTextAreaElement}
  159. class=" bg-transparent outline-none w-full resize-none"
  160. bind:value={editedContent}
  161. on:input={(e) => {
  162. e.target.style.height = '';
  163. e.target.style.height = `${e.target.scrollHeight}px`;
  164. }}
  165. on:keydown={(e) => {
  166. if (e.key === 'Escape') {
  167. document.getElementById('close-edit-message-button')?.click();
  168. }
  169. const isCmdOrCtrlPressed = e.metaKey || e.ctrlKey;
  170. const isEnterPressed = e.key === 'Enter';
  171. if (isCmdOrCtrlPressed && isEnterPressed) {
  172. document.getElementById('save-edit-message-button')?.click();
  173. }
  174. }}
  175. />
  176. <div class=" mt-2 mb-1 flex justify-end space-x-1.5 text-sm font-medium">
  177. <button
  178. id="close-edit-message-button"
  179. class=" px-4 py-2 bg-gray-900 hover:bg-gray-850 text-gray-100 transition rounded-3xl"
  180. on:click={() => {
  181. cancelEditMessage();
  182. }}
  183. >
  184. {$i18n.t('Cancel')}
  185. </button>
  186. <button
  187. id="save-edit-message-button"
  188. class="px-4 py-2 bg-white hover:bg-gray-100 text-gray-800 transition rounded-3xl"
  189. on:click={() => {
  190. editMessageConfirmHandler();
  191. }}
  192. >
  193. {$i18n.t('Send')}
  194. </button>
  195. </div>
  196. </div>
  197. {:else}
  198. <div class="w-full">
  199. <div class="flex {$settings?.chatBubble ?? true ? 'justify-end' : ''} mb-2">
  200. <div
  201. class="rounded-3xl {$settings?.chatBubble ?? true
  202. ? `max-w-[90%] px-5 py-2 bg-gray-50 dark:bg-gray-850 ${
  203. message.files ? 'rounded-tr-lg' : ''
  204. }`
  205. : ''} "
  206. >
  207. <pre id="user-message">{message.content}</pre>
  208. </div>
  209. </div>
  210. <div
  211. class=" flex {$settings?.chatBubble ?? true
  212. ? 'justify-end'
  213. : ''} text-gray-600 dark:text-gray-500"
  214. >
  215. {#if !($settings?.chatBubble ?? true)}
  216. {#if siblings.length > 1}
  217. <div class="flex self-center" dir="ltr">
  218. <button
  219. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  220. on:click={() => {
  221. showPreviousMessage(message);
  222. }}
  223. >
  224. <svg
  225. xmlns="http://www.w3.org/2000/svg"
  226. fill="none"
  227. viewBox="0 0 24 24"
  228. stroke="currentColor"
  229. stroke-width="2.5"
  230. class="size-3.5"
  231. >
  232. <path
  233. stroke-linecap="round"
  234. stroke-linejoin="round"
  235. d="M15.75 19.5 8.25 12l7.5-7.5"
  236. />
  237. </svg>
  238. </button>
  239. <div class="text-sm tracking-widest font-semibold self-center dark:text-gray-100">
  240. {siblings.indexOf(message.id) + 1}/{siblings.length}
  241. </div>
  242. <button
  243. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  244. on:click={() => {
  245. showNextMessage(message);
  246. }}
  247. >
  248. <svg
  249. xmlns="http://www.w3.org/2000/svg"
  250. fill="none"
  251. viewBox="0 0 24 24"
  252. stroke="currentColor"
  253. stroke-width="2.5"
  254. class="size-3.5"
  255. >
  256. <path
  257. stroke-linecap="round"
  258. stroke-linejoin="round"
  259. d="m8.25 4.5 7.5 7.5-7.5 7.5"
  260. />
  261. </svg>
  262. </button>
  263. </div>
  264. {/if}
  265. {/if}
  266. {#if !readOnly}
  267. <Tooltip content={$i18n.t('Edit')} placement="bottom">
  268. <button
  269. 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"
  270. on:click={() => {
  271. editMessageHandler();
  272. }}
  273. >
  274. <svg
  275. xmlns="http://www.w3.org/2000/svg"
  276. fill="none"
  277. viewBox="0 0 24 24"
  278. stroke-width="2.3"
  279. stroke="currentColor"
  280. class="w-4 h-4"
  281. >
  282. <path
  283. stroke-linecap="round"
  284. stroke-linejoin="round"
  285. 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"
  286. />
  287. </svg>
  288. </button>
  289. </Tooltip>
  290. {/if}
  291. <Tooltip content={$i18n.t('Copy')} placement="bottom">
  292. <button
  293. 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"
  294. on:click={() => {
  295. copyToClipboard(message.content);
  296. }}
  297. >
  298. <svg
  299. xmlns="http://www.w3.org/2000/svg"
  300. fill="none"
  301. viewBox="0 0 24 24"
  302. stroke-width="2.3"
  303. stroke="currentColor"
  304. class="w-4 h-4"
  305. >
  306. <path
  307. stroke-linecap="round"
  308. stroke-linejoin="round"
  309. 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"
  310. />
  311. </svg>
  312. </button>
  313. </Tooltip>
  314. {#if !isFirstMessage && !readOnly}
  315. <Tooltip content={$i18n.t('Delete')} placement="bottom">
  316. <button
  317. class="invisible group-hover:visible p-1 rounded dark:hover:text-white hover:text-black transition"
  318. on:click={() => {
  319. deleteMessageHandler();
  320. }}
  321. >
  322. <svg
  323. xmlns="http://www.w3.org/2000/svg"
  324. fill="none"
  325. viewBox="0 0 24 24"
  326. stroke-width="2"
  327. stroke="currentColor"
  328. class="w-4 h-4"
  329. >
  330. <path
  331. stroke-linecap="round"
  332. stroke-linejoin="round"
  333. 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"
  334. />
  335. </svg>
  336. </button>
  337. </Tooltip>
  338. {/if}
  339. {#if $settings?.chatBubble ?? true}
  340. {#if siblings.length > 1}
  341. <div class="flex self-center" dir="ltr">
  342. <button
  343. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  344. on:click={() => {
  345. showPreviousMessage(message);
  346. }}
  347. >
  348. <svg
  349. xmlns="http://www.w3.org/2000/svg"
  350. fill="none"
  351. viewBox="0 0 24 24"
  352. stroke="currentColor"
  353. stroke-width="2.5"
  354. class="size-3.5"
  355. >
  356. <path
  357. stroke-linecap="round"
  358. stroke-linejoin="round"
  359. d="M15.75 19.5 8.25 12l7.5-7.5"
  360. />
  361. </svg>
  362. </button>
  363. <div class="text-sm tracking-widest font-semibold self-center dark:text-gray-100">
  364. {siblings.indexOf(message.id) + 1}/{siblings.length}
  365. </div>
  366. <button
  367. class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
  368. on:click={() => {
  369. showNextMessage(message);
  370. }}
  371. >
  372. <svg
  373. xmlns="http://www.w3.org/2000/svg"
  374. fill="none"
  375. viewBox="0 0 24 24"
  376. stroke="currentColor"
  377. stroke-width="2.5"
  378. class="size-3.5"
  379. >
  380. <path
  381. stroke-linecap="round"
  382. stroke-linejoin="round"
  383. d="m8.25 4.5 7.5 7.5-7.5 7.5"
  384. />
  385. </svg>
  386. </button>
  387. </div>
  388. {/if}
  389. {/if}
  390. </div>
  391. </div>
  392. {/if}
  393. </div>
  394. </div>
  395. </div>