Database.svelte 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <script lang="ts">
  2. import fileSaver from 'file-saver';
  3. const { saveAs } = fileSaver;
  4. import { downloadDatabase, downloadLiteLLMConfig } from '$lib/apis/utils';
  5. import { onMount, getContext } from 'svelte';
  6. import { config, user } from '$lib/stores';
  7. import { toast } from 'svelte-sonner';
  8. import { getAllUserChats } from '$lib/apis/chats';
  9. import { exportConfig, importConfig } from '$lib/apis/configs';
  10. const i18n = getContext('i18n');
  11. export let saveHandler: Function;
  12. const exportAllUserChats = async () => {
  13. let blob = new Blob([JSON.stringify(await getAllUserChats(localStorage.token))], {
  14. type: 'application/json'
  15. });
  16. saveAs(blob, `all-chats-export-${Date.now()}.json`);
  17. };
  18. onMount(async () => {
  19. // permissions = await getUserPermissions(localStorage.token);
  20. });
  21. </script>
  22. <form
  23. class="flex flex-col h-full justify-between space-y-3 text-sm"
  24. on:submit|preventDefault={async () => {
  25. saveHandler();
  26. }}
  27. >
  28. <div class=" space-y-3 overflow-y-scroll scrollbar-hidden h-full">
  29. <div>
  30. <div class=" mb-2 text-sm font-medium">{$i18n.t('Database')}</div>
  31. <input
  32. id="config-json-input"
  33. hidden
  34. type="file"
  35. accept=".json"
  36. on:change={(e) => {
  37. const file = e.target.files[0];
  38. const reader = new FileReader();
  39. reader.onload = async (e) => {
  40. const res = await importConfig(localStorage.token, JSON.parse(e.target.result)).catch(
  41. (error) => {
  42. toast.error(`${error}`);
  43. }
  44. );
  45. if (res) {
  46. toast.success('Config imported successfully');
  47. }
  48. e.target.value = null;
  49. };
  50. reader.readAsText(file);
  51. }}
  52. />
  53. <button
  54. type="button"
  55. class=" flex rounded-md py-2 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  56. on:click={async () => {
  57. document.getElementById('config-json-input').click();
  58. }}
  59. >
  60. <div class=" self-center mr-3">
  61. <svg
  62. xmlns="http://www.w3.org/2000/svg"
  63. viewBox="0 0 16 16"
  64. fill="currentColor"
  65. class="w-4 h-4"
  66. >
  67. <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" />
  68. <path
  69. fill-rule="evenodd"
  70. 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"
  71. clip-rule="evenodd"
  72. />
  73. </svg>
  74. </div>
  75. <div class=" self-center text-sm font-medium">
  76. {$i18n.t('Import Config from JSON File')}
  77. </div>
  78. </button>
  79. <button
  80. type="button"
  81. class=" flex rounded-md py-2 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  82. on:click={async () => {
  83. const config = await exportConfig(localStorage.token);
  84. const blob = new Blob([JSON.stringify(config)], {
  85. type: 'application/json'
  86. });
  87. saveAs(blob, `config-${Date.now()}.json`);
  88. }}
  89. >
  90. <div class=" self-center mr-3">
  91. <svg
  92. xmlns="http://www.w3.org/2000/svg"
  93. viewBox="0 0 16 16"
  94. fill="currentColor"
  95. class="w-4 h-4"
  96. >
  97. <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" />
  98. <path
  99. fill-rule="evenodd"
  100. 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"
  101. clip-rule="evenodd"
  102. />
  103. </svg>
  104. </div>
  105. <div class=" self-center text-sm font-medium">
  106. {$i18n.t('Export Config to JSON File')}
  107. </div>
  108. </button>
  109. <hr class="border-gray-100 dark:border-gray-850 my-1" />
  110. {#if $config?.features.enable_admin_export ?? true}
  111. <div class=" flex w-full justify-between">
  112. <!-- <div class=" self-center text-xs font-medium">{$i18n.t('Allow Chat Deletion')}</div> -->
  113. <button
  114. class=" flex rounded-md py-1.5 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  115. type="button"
  116. on:click={() => {
  117. // exportAllUserChats();
  118. downloadDatabase(localStorage.token).catch((error) => {
  119. toast.error(`${error}`);
  120. });
  121. }}
  122. >
  123. <div class=" self-center mr-3">
  124. <svg
  125. xmlns="http://www.w3.org/2000/svg"
  126. viewBox="0 0 16 16"
  127. fill="currentColor"
  128. class="w-4 h-4"
  129. >
  130. <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" />
  131. <path
  132. fill-rule="evenodd"
  133. 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"
  134. clip-rule="evenodd"
  135. />
  136. </svg>
  137. </div>
  138. <div class=" self-center text-sm font-medium">{$i18n.t('Download Database')}</div>
  139. </button>
  140. </div>
  141. <button
  142. class=" flex rounded-md py-2 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  143. on:click={() => {
  144. exportAllUserChats();
  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 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" />
  155. <path
  156. fill-rule="evenodd"
  157. 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"
  158. clip-rule="evenodd"
  159. />
  160. </svg>
  161. </div>
  162. <div class=" self-center text-sm font-medium">
  163. {$i18n.t('Export All Chats (All Users)')}
  164. </div>
  165. </button>
  166. {/if}
  167. </div>
  168. </div>
  169. <!-- <div class="flex justify-end pt-3 text-sm font-medium">
  170. <button
  171. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  172. type="submit"
  173. >
  174. {$i18n.t('Save')}
  175. </button>
  176. </div> -->
  177. </form>