Chats.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 } from 'svelte';
  15. import { goto } from '$app/navigation';
  16. import { toast } from 'svelte-sonner';
  17. export let saveSettings: Function;
  18. // Chats
  19. let saveChatHistory = true;
  20. let importFiles;
  21. let showDeleteConfirm = false;
  22. let chatImportInputElement: HTMLInputElement;
  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">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"> 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">Off</span>
  131. {/if}
  132. </button>
  133. </div>
  134. <div class="text-xs text-left w-full font-medium mt-0.5">
  135. 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
  141. id="chat-import-input"
  142. bind:this={chatImportInputElement}
  143. bind:files={importFiles}
  144. type="file"
  145. accept=".json"
  146. hidden
  147. />
  148. <button
  149. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  150. on:click={() => {
  151. chatImportInputElement.click();
  152. }}
  153. >
  154. <div class=" self-center mr-3">
  155. <svg
  156. xmlns="http://www.w3.org/2000/svg"
  157. viewBox="0 0 16 16"
  158. fill="currentColor"
  159. class="w-4 h-4"
  160. >
  161. <path
  162. fill-rule="evenodd"
  163. 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"
  164. clip-rule="evenodd"
  165. />
  166. </svg>
  167. </div>
  168. <div class=" self-center text-sm font-medium">Import Chats</div>
  169. </button>
  170. <button
  171. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  172. on:click={() => {
  173. exportChats();
  174. }}
  175. >
  176. <div class=" self-center mr-3">
  177. <svg
  178. xmlns="http://www.w3.org/2000/svg"
  179. viewBox="0 0 16 16"
  180. fill="currentColor"
  181. class="w-4 h-4"
  182. >
  183. <path
  184. fill-rule="evenodd"
  185. 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"
  186. clip-rule="evenodd"
  187. />
  188. </svg>
  189. </div>
  190. <div class=" self-center text-sm font-medium">Export Chats</div>
  191. </button>
  192. </div>
  193. <hr class=" dark:border-gray-700" />
  194. {#if showDeleteConfirm}
  195. <div class="flex justify-between rounded-md items-center py-2 px-3.5 w-full transition">
  196. <div class="flex items-center space-x-3">
  197. <svg
  198. xmlns="http://www.w3.org/2000/svg"
  199. viewBox="0 0 16 16"
  200. fill="currentColor"
  201. class="w-4 h-4"
  202. >
  203. <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" />
  204. <path
  205. fill-rule="evenodd"
  206. 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"
  207. clip-rule="evenodd"
  208. />
  209. </svg>
  210. <span>Are you sure?</span>
  211. </div>
  212. <div class="flex space-x-1.5 items-center">
  213. <button
  214. class="hover:text-white transition"
  215. on:click={() => {
  216. deleteChats();
  217. showDeleteConfirm = false;
  218. }}
  219. >
  220. <svg
  221. xmlns="http://www.w3.org/2000/svg"
  222. viewBox="0 0 20 20"
  223. fill="currentColor"
  224. class="w-4 h-4"
  225. >
  226. <path
  227. fill-rule="evenodd"
  228. 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"
  229. clip-rule="evenodd"
  230. />
  231. </svg>
  232. </button>
  233. <button
  234. class="hover:text-white transition"
  235. on:click={() => {
  236. showDeleteConfirm = false;
  237. }}
  238. >
  239. <svg
  240. xmlns="http://www.w3.org/2000/svg"
  241. viewBox="0 0 20 20"
  242. fill="currentColor"
  243. class="w-4 h-4"
  244. >
  245. <path
  246. 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"
  247. />
  248. </svg>
  249. </button>
  250. </div>
  251. </div>
  252. {:else}
  253. <button
  254. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  255. on:click={() => {
  256. showDeleteConfirm = true;
  257. }}
  258. >
  259. <div class=" self-center mr-3">
  260. <svg
  261. xmlns="http://www.w3.org/2000/svg"
  262. viewBox="0 0 16 16"
  263. fill="currentColor"
  264. class="w-4 h-4"
  265. >
  266. <path
  267. fill-rule="evenodd"
  268. 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"
  269. clip-rule="evenodd"
  270. />
  271. </svg>
  272. </div>
  273. <div class=" self-center text-sm font-medium">Delete Chats</div>
  274. </button>
  275. {/if}
  276. {#if $user?.role === 'admin'}
  277. <hr class=" dark:border-gray-700" />
  278. <button
  279. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  280. on:click={() => {
  281. exportAllUserChats();
  282. }}
  283. >
  284. <div class=" self-center mr-3">
  285. <svg
  286. xmlns="http://www.w3.org/2000/svg"
  287. viewBox="0 0 16 16"
  288. fill="currentColor"
  289. class="w-4 h-4"
  290. >
  291. <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" />
  292. <path
  293. fill-rule="evenodd"
  294. 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"
  295. clip-rule="evenodd"
  296. />
  297. </svg>
  298. </div>
  299. <div class=" self-center text-sm font-medium">Export All Chats (All Users)</div>
  300. </button>
  301. <hr class=" dark:border-gray-700" />
  302. <button
  303. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  304. on:click={() => {
  305. const res = resetVectorDB(localStorage.token).catch((error) => {
  306. toast.error(error);
  307. return null;
  308. });
  309. if (res) {
  310. toast.success('Success');
  311. }
  312. }}
  313. >
  314. <div class=" self-center mr-3">
  315. <svg
  316. xmlns="http://www.w3.org/2000/svg"
  317. viewBox="0 0 16 16"
  318. fill="currentColor"
  319. class="w-4 h-4"
  320. >
  321. <path
  322. fill-rule="evenodd"
  323. 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"
  324. clip-rule="evenodd"
  325. />
  326. </svg>
  327. </div>
  328. <div class=" self-center text-sm font-medium">Reset Vector Storage</div>
  329. </button>
  330. {/if}
  331. </div>
  332. </div>