Chats.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <script lang="ts">
  2. import fileSaver from 'file-saver';
  3. const { saveAs } = fileSaver;
  4. import { resetVectorDB } from '$lib/apis/rag';
  5. import { chats, user } from '$lib/stores';
  6. import {
  7. createNewChat,
  8. deleteAllChats,
  9. getAllChats,
  10. getAllUserChats,
  11. getChatList
  12. } from '$lib/apis/chats';
  13. import { getImportOrigin, convertOpenAIChats } from '$lib/utils';
  14. import { onMount, getContext } from 'svelte';
  15. import { goto } from '$app/navigation';
  16. import { toast } from 'svelte-sonner';
  17. const i18n = getContext('i18n');
  18. export let saveSettings: Function;
  19. // Chats
  20. let saveChatHistory = true;
  21. let importFiles;
  22. let showDeleteConfirm = false;
  23. $: if (importFiles) {
  24. console.log(importFiles);
  25. let reader = new FileReader();
  26. reader.onload = (event) => {
  27. let chats = JSON.parse(event.target.result);
  28. console.log(chats);
  29. if (getImportOrigin(chats) == 'openai') {
  30. try {
  31. chats = convertOpenAIChats(chats);
  32. } catch (error) {
  33. console.log('Unable to import chats:', error);
  34. }
  35. }
  36. importChats(chats);
  37. };
  38. if (importFiles.length > 0) {
  39. reader.readAsText(importFiles[0]);
  40. }
  41. }
  42. const importChats = async (_chats) => {
  43. for (const chat of _chats) {
  44. console.log(chat);
  45. if (chat.chat) {
  46. await createNewChat(localStorage.token, chat.chat);
  47. } else {
  48. await createNewChat(localStorage.token, chat);
  49. }
  50. }
  51. await chats.set(await getChatList(localStorage.token));
  52. };
  53. const exportChats = async () => {
  54. let blob = new Blob([JSON.stringify(await getAllChats(localStorage.token))], {
  55. type: 'application/json'
  56. });
  57. saveAs(blob, `chat-export-${Date.now()}.json`);
  58. };
  59. const exportAllUserChats = async () => {
  60. let blob = new Blob([JSON.stringify(await getAllUserChats(localStorage.token))], {
  61. type: 'application/json'
  62. });
  63. saveAs(blob, `all-chats-export-${Date.now()}.json`);
  64. };
  65. const deleteChats = async () => {
  66. await goto('/');
  67. await deleteAllChats(localStorage.token).catch((error) => {
  68. toast.error(error);
  69. });
  70. await chats.set(await getChatList(localStorage.token));
  71. };
  72. const toggleSaveChatHistory = async () => {
  73. saveChatHistory = !saveChatHistory;
  74. console.log(saveChatHistory);
  75. if (saveChatHistory === false) {
  76. await goto('/');
  77. }
  78. saveSettings({ saveChatHistory: saveChatHistory });
  79. };
  80. onMount(async () => {
  81. let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');
  82. saveChatHistory = settings.saveChatHistory ?? true;
  83. });
  84. </script>
  85. <div class="flex flex-col h-full justify-between space-y-3 text-sm">
  86. <div class=" space-y-2">
  87. <div
  88. class="flex flex-col justify-between rounded-md items-center py-2 px-3.5 w-full transition"
  89. >
  90. <div class="flex w-full justify-between">
  91. <div class=" self-center text-sm font-medium">{$i18n.t('Chat History')}</div>
  92. <button
  93. class="p-1 px-3 text-xs flex rounded transition"
  94. type="button"
  95. on:click={() => {
  96. toggleSaveChatHistory();
  97. }}
  98. >
  99. {#if saveChatHistory === true}
  100. <svg
  101. xmlns="http://www.w3.org/2000/svg"
  102. viewBox="0 0 16 16"
  103. fill="currentColor"
  104. class="w-4 h-4"
  105. >
  106. <path d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z" />
  107. <path
  108. fill-rule="evenodd"
  109. d="M1.38 8.28a.87.87 0 0 1 0-.566 7.003 7.003 0 0 1 13.238.006.87.87 0 0 1 0 .566A7.003 7.003 0 0 1 1.379 8.28ZM11 8a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
  110. clip-rule="evenodd"
  111. />
  112. </svg>
  113. <span class="ml-2 self-center"> {$i18n.t('On')} </span>
  114. {:else}
  115. <svg
  116. xmlns="http://www.w3.org/2000/svg"
  117. viewBox="0 0 16 16"
  118. fill="currentColor"
  119. class="w-4 h-4"
  120. >
  121. <path
  122. fill-rule="evenodd"
  123. d="M3.28 2.22a.75.75 0 0 0-1.06 1.06l10.5 10.5a.75.75 0 1 0 1.06-1.06l-1.322-1.323a7.012 7.012 0 0 0 2.16-3.11.87.87 0 0 0 0-.567A7.003 7.003 0 0 0 4.82 3.76l-1.54-1.54Zm3.196 3.195 1.135 1.136A1.502 1.502 0 0 1 9.45 8.389l1.136 1.135a3 3 0 0 0-4.109-4.109Z"
  124. clip-rule="evenodd"
  125. />
  126. <path
  127. d="m7.812 10.994 1.816 1.816A7.003 7.003 0 0 1 1.38 8.28a.87.87 0 0 1 0-.566 6.985 6.985 0 0 1 1.113-2.039l2.513 2.513a3 3 0 0 0 2.806 2.806Z"
  128. />
  129. </svg>
  130. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  131. {/if}
  132. </button>
  133. </div>
  134. <div class="text-xs text-left w-full font-medium mt-0.5">
  135. {$i18n.t('This setting does not sync across browsers or devices.')}
  136. </div>
  137. </div>
  138. <hr class=" dark:border-gray-700" />
  139. <div class="flex flex-col">
  140. <input id="chat-import-input" bind:files={importFiles} type="file" accept=".json" hidden />
  141. <button
  142. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  143. on:click={() => {
  144. document.getElementById('chat-import-input').click();
  145. }}
  146. >
  147. <div class=" self-center mr-3">
  148. <svg
  149. xmlns="http://www.w3.org/2000/svg"
  150. viewBox="0 0 16 16"
  151. fill="currentColor"
  152. class="w-4 h-4"
  153. >
  154. <path
  155. fill-rule="evenodd"
  156. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 9.5a.75.75 0 0 1-.75-.75V8.06l-.72.72a.75.75 0 0 1-1.06-1.06l2-2a.75.75 0 0 1 1.06 0l2 2a.75.75 0 1 1-1.06 1.06l-.72-.72v2.69a.75.75 0 0 1-.75.75Z"
  157. clip-rule="evenodd"
  158. />
  159. </svg>
  160. </div>
  161. <div class=" self-center text-sm font-medium">{$i18n.t('Import Chats')}</div>
  162. </button>
  163. <button
  164. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  165. on:click={() => {
  166. exportChats();
  167. }}
  168. >
  169. <div class=" self-center mr-3">
  170. <svg
  171. xmlns="http://www.w3.org/2000/svg"
  172. viewBox="0 0 16 16"
  173. fill="currentColor"
  174. class="w-4 h-4"
  175. >
  176. <path
  177. fill-rule="evenodd"
  178. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 3.5a.75.75 0 0 1 .75.75v2.69l.72-.72a.75.75 0 1 1 1.06 1.06l-2 2a.75.75 0 0 1-1.06 0l-2-2a.75.75 0 0 1 1.06-1.06l.72.72V6.25A.75.75 0 0 1 8 5.5Z"
  179. clip-rule="evenodd"
  180. />
  181. </svg>
  182. </div>
  183. <div class=" self-center text-sm font-medium">{$i18n.t('Export Chats')}</div>
  184. </button>
  185. </div>
  186. <hr class=" dark:border-gray-700" />
  187. {#if showDeleteConfirm}
  188. <div class="flex justify-between rounded-md items-center py-2 px-3.5 w-full transition">
  189. <div class="flex items-center space-x-3">
  190. <svg
  191. xmlns="http://www.w3.org/2000/svg"
  192. viewBox="0 0 16 16"
  193. fill="currentColor"
  194. class="w-4 h-4"
  195. >
  196. <path d="M2 3a1 1 0 0 1 1-1h10a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3Z" />
  197. <path
  198. fill-rule="evenodd"
  199. d="M13 6H3v6a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V6ZM5.72 7.47a.75.75 0 0 1 1.06 0L8 8.69l1.22-1.22a.75.75 0 1 1 1.06 1.06L9.06 9.75l1.22 1.22a.75.75 0 1 1-1.06 1.06L8 10.81l-1.22 1.22a.75.75 0 0 1-1.06-1.06l1.22-1.22-1.22-1.22a.75.75 0 0 1 0-1.06Z"
  200. clip-rule="evenodd"
  201. />
  202. </svg>
  203. <span>Are you sure?</span>
  204. </div>
  205. <div class="flex space-x-1.5 items-center">
  206. <button
  207. class="hover:text-white transition"
  208. on:click={() => {
  209. deleteChats();
  210. showDeleteConfirm = false;
  211. }}
  212. >
  213. <svg
  214. xmlns="http://www.w3.org/2000/svg"
  215. viewBox="0 0 20 20"
  216. fill="currentColor"
  217. class="w-4 h-4"
  218. >
  219. <path
  220. fill-rule="evenodd"
  221. d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z"
  222. clip-rule="evenodd"
  223. />
  224. </svg>
  225. </button>
  226. <button
  227. class="hover:text-white transition"
  228. on:click={() => {
  229. showDeleteConfirm = false;
  230. }}
  231. >
  232. <svg
  233. xmlns="http://www.w3.org/2000/svg"
  234. viewBox="0 0 20 20"
  235. fill="currentColor"
  236. class="w-4 h-4"
  237. >
  238. <path
  239. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  240. />
  241. </svg>
  242. </button>
  243. </div>
  244. </div>
  245. {:else}
  246. <button
  247. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  248. on:click={() => {
  249. showDeleteConfirm = true;
  250. }}
  251. >
  252. <div class=" self-center mr-3">
  253. <svg
  254. xmlns="http://www.w3.org/2000/svg"
  255. viewBox="0 0 16 16"
  256. fill="currentColor"
  257. class="w-4 h-4"
  258. >
  259. <path
  260. fill-rule="evenodd"
  261. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm7 7a.75.75 0 0 1-.75.75h-4.5a.75.75 0 0 1 0-1.5h4.5A.75.75 0 0 1 11 9Z"
  262. clip-rule="evenodd"
  263. />
  264. </svg>
  265. </div>
  266. <div class=" self-center text-sm font-medium">{$i18n.t('Delete Chats')}</div>
  267. </button>
  268. {/if}
  269. {#if $user?.role === 'admin'}
  270. <hr class=" dark:border-gray-700" />
  271. <button
  272. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  273. on:click={() => {
  274. exportAllUserChats();
  275. }}
  276. >
  277. <div class=" self-center mr-3">
  278. <svg
  279. xmlns="http://www.w3.org/2000/svg"
  280. viewBox="0 0 16 16"
  281. fill="currentColor"
  282. class="w-4 h-4"
  283. >
  284. <path d="M2 3a1 1 0 0 1 1-1h10a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3Z" />
  285. <path
  286. fill-rule="evenodd"
  287. d="M13 6H3v6a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V6ZM8.75 7.75a.75.75 0 0 0-1.5 0v2.69L6.03 9.22a.75.75 0 0 0-1.06 1.06l2.5 2.5a.75.75 0 0 0 1.06 0l2.5-2.5a.75.75 0 1 0-1.06-1.06l-1.22 1.22V7.75Z"
  288. clip-rule="evenodd"
  289. />
  290. </svg>
  291. </div>
  292. <div class=" self-center text-sm font-medium">
  293. {$i18n.t('Export All Chats (All Users)')}
  294. </div>
  295. </button>
  296. <hr class=" dark:border-gray-700" />
  297. <button
  298. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  299. on:click={() => {
  300. const res = resetVectorDB(localStorage.token).catch((error) => {
  301. toast.error(error);
  302. return null;
  303. });
  304. if (res) {
  305. toast.success($i18n.t('Success'));
  306. }
  307. }}
  308. >
  309. <div class=" self-center mr-3">
  310. <svg
  311. xmlns="http://www.w3.org/2000/svg"
  312. viewBox="0 0 16 16"
  313. fill="currentColor"
  314. class="w-4 h-4"
  315. >
  316. <path
  317. fill-rule="evenodd"
  318. d="M3.5 2A1.5 1.5 0 0 0 2 3.5v9A1.5 1.5 0 0 0 3.5 14h9a1.5 1.5 0 0 0 1.5-1.5v-7A1.5 1.5 0 0 0 12.5 4H9.621a1.5 1.5 0 0 1-1.06-.44L7.439 2.44A1.5 1.5 0 0 0 6.38 2H3.5Zm6.75 7.75a.75.75 0 0 0 0-1.5h-4.5a.75.75 0 0 0 0 1.5h4.5Z"
  319. clip-rule="evenodd"
  320. />
  321. </svg>
  322. </div>
  323. <div class=" self-center text-sm font-medium">{$i18n.t('Reset Vector Storage')}</div>
  324. </button>
  325. {/if}
  326. </div>
  327. </div>