Interface.svelte 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <script lang="ts">
  2. import { getBackendConfig } from '$lib/apis';
  3. import { setDefaultPromptSuggestions } from '$lib/apis/configs';
  4. import { config, models, user } from '$lib/stores';
  5. import { createEventDispatcher, onMount, getContext } from 'svelte';
  6. import { toast } from 'svelte-sonner';
  7. const dispatch = createEventDispatcher();
  8. const i18n = getContext('i18n');
  9. export let saveSettings: Function;
  10. // Addons
  11. let titleAutoGenerate = true;
  12. let responseAutoCopy = false;
  13. let titleAutoGenerateModel = '';
  14. let fullScreenMode = false;
  15. let titleGenerationPrompt = '';
  16. // Interface
  17. let promptSuggestions = [];
  18. let showUsername = false;
  19. const toggleFullScreenMode = async () => {
  20. fullScreenMode = !fullScreenMode;
  21. saveSettings({ fullScreenMode: fullScreenMode });
  22. };
  23. const toggleShowUsername = async () => {
  24. showUsername = !showUsername;
  25. saveSettings({ showUsername: showUsername });
  26. };
  27. const toggleTitleAutoGenerate = async () => {
  28. titleAutoGenerate = !titleAutoGenerate;
  29. saveSettings({ titleAutoGenerate: titleAutoGenerate });
  30. };
  31. const toggleResponseAutoCopy = async () => {
  32. const permission = await navigator.clipboard
  33. .readText()
  34. .then(() => {
  35. return 'granted';
  36. })
  37. .catch(() => {
  38. return '';
  39. });
  40. console.log(permission);
  41. if (permission === 'granted') {
  42. responseAutoCopy = !responseAutoCopy;
  43. saveSettings({ responseAutoCopy: responseAutoCopy });
  44. } else {
  45. toast.error(
  46. 'Clipboard write permission denied. Please check your browser settings to grant the necessary access.'
  47. );
  48. }
  49. };
  50. const updateInterfaceHandler = async () => {
  51. if ($user.role === 'admin') {
  52. promptSuggestions = await setDefaultPromptSuggestions(localStorage.token, promptSuggestions);
  53. await config.set(await getBackendConfig());
  54. }
  55. saveSettings({
  56. titleAutoGenerateModel: titleAutoGenerateModel !== '' ? titleAutoGenerateModel : undefined,
  57. titleGenerationPrompt: titleGenerationPrompt ? titleGenerationPrompt : undefined
  58. });
  59. };
  60. onMount(async () => {
  61. if ($user.role === 'admin') {
  62. promptSuggestions = $config?.default_prompt_suggestions;
  63. }
  64. let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');
  65. titleAutoGenerate = settings.titleAutoGenerate ?? true;
  66. responseAutoCopy = settings.responseAutoCopy ?? false;
  67. showUsername = settings.showUsername ?? false;
  68. fullScreenMode = settings.fullScreenMode ?? false;
  69. titleAutoGenerateModel = settings.titleAutoGenerateModel ?? '';
  70. titleGenerationPrompt =
  71. settings.titleGenerationPrompt ??
  72. $i18n.t(
  73. "Create a concise, 3-5 word phrase as a header for the following query, strictly adhering to the 3-5 word limit and avoiding the use of the word 'title':"
  74. ) + ' {{prompt}}';
  75. });
  76. </script>
  77. <form
  78. class="flex flex-col h-full justify-between space-y-3 text-sm"
  79. on:submit|preventDefault={() => {
  80. updateInterfaceHandler();
  81. dispatch('save');
  82. }}
  83. >
  84. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[22rem]">
  85. <div>
  86. <div class=" mb-1 text-sm font-medium">{$i18n.t('WebUI Add-ons')}</div>
  87. <div>
  88. <div class=" py-0.5 flex w-full justify-between">
  89. <div class=" self-center text-xs font-medium">{$i18n.t('Title Auto-Generation')}</div>
  90. <button
  91. class="p-1 px-3 text-xs flex rounded transition"
  92. on:click={() => {
  93. toggleTitleAutoGenerate();
  94. }}
  95. type="button"
  96. >
  97. {#if titleAutoGenerate === true}
  98. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  99. {:else}
  100. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  101. {/if}
  102. </button>
  103. </div>
  104. </div>
  105. <div>
  106. <div class=" py-0.5 flex w-full justify-between">
  107. <div class=" self-center text-xs font-medium">
  108. {$i18n.t('Response AutoCopy to Clipboard')}
  109. </div>
  110. <button
  111. class="p-1 px-3 text-xs flex rounded transition"
  112. on:click={() => {
  113. toggleResponseAutoCopy();
  114. }}
  115. type="button"
  116. >
  117. {#if responseAutoCopy === true}
  118. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  119. {:else}
  120. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  121. {/if}
  122. </button>
  123. </div>
  124. </div>
  125. <div>
  126. <div class=" py-0.5 flex w-full justify-between">
  127. <div class=" self-center text-xs font-medium">{$i18n.t('Full Screen Mode')}</div>
  128. <button
  129. class="p-1 px-3 text-xs flex rounded transition"
  130. on:click={() => {
  131. toggleFullScreenMode();
  132. }}
  133. type="button"
  134. >
  135. {#if fullScreenMode === true}
  136. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  137. {:else}
  138. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  139. {/if}
  140. </button>
  141. </div>
  142. </div>
  143. <div>
  144. <div class=" py-0.5 flex w-full justify-between">
  145. <div class=" self-center text-xs font-medium">
  146. {$i18n.t('Display the username instead of You in the Chat')}
  147. </div>
  148. <button
  149. class="p-1 px-3 text-xs flex rounded transition"
  150. on:click={() => {
  151. toggleShowUsername();
  152. }}
  153. type="button"
  154. >
  155. {#if showUsername === true}
  156. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  157. {:else}
  158. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  159. {/if}
  160. </button>
  161. </div>
  162. </div>
  163. </div>
  164. <hr class=" dark:border-gray-700" />
  165. <div>
  166. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Title Auto-Generation Model')}</div>
  167. <div class="flex w-full">
  168. <div class="flex-1 mr-2">
  169. <select
  170. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  171. bind:value={titleAutoGenerateModel}
  172. placeholder={$i18n.t('Select a model')}
  173. >
  174. <option value="" selected>{$i18n.t('Current Model')}</option>
  175. {#each $models as model}
  176. {#if model.size != null}
  177. <option value={model.name} class="bg-gray-100 dark:bg-gray-700">
  178. {model.name + ' (' + (model.size / 1024 ** 3).toFixed(1) + ' GB)'}
  179. </option>
  180. {/if}
  181. {/each}
  182. </select>
  183. </div>
  184. </div>
  185. <div class="mt-3 mr-2">
  186. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Title Generation Prompt')}</div>
  187. <textarea
  188. bind:value={titleGenerationPrompt}
  189. class="w-full rounded-lg p-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none resize-none"
  190. rows="3"
  191. />
  192. </div>
  193. </div>
  194. {#if $user.role === 'admin'}
  195. <hr class=" dark:border-gray-700" />
  196. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-80">
  197. <div class="flex w-full justify-between mb-2">
  198. <div class=" self-center text-sm font-semibold">
  199. {$i18n.t('Default Prompt Suggestions')}
  200. </div>
  201. <button
  202. class="p-1 px-3 text-xs flex rounded transition"
  203. type="button"
  204. on:click={() => {
  205. if (promptSuggestions.length === 0 || promptSuggestions.at(-1).content !== '') {
  206. promptSuggestions = [...promptSuggestions, { content: '', title: ['', ''] }];
  207. }
  208. }}
  209. >
  210. <svg
  211. xmlns="http://www.w3.org/2000/svg"
  212. viewBox="0 0 20 20"
  213. fill="currentColor"
  214. class="w-4 h-4"
  215. >
  216. <path
  217. d="M10.75 4.75a.75.75 0 00-1.5 0v4.5h-4.5a.75.75 0 000 1.5h4.5v4.5a.75.75 0 001.5 0v-4.5h4.5a.75.75 0 000-1.5h-4.5v-4.5z"
  218. />
  219. </svg>
  220. </button>
  221. </div>
  222. <div class="flex flex-col space-y-1">
  223. {#each promptSuggestions as prompt, promptIdx}
  224. <div class=" flex border dark:border-gray-600 rounded-lg">
  225. <div class="flex flex-col flex-1">
  226. <div class="flex border-b dark:border-gray-600 w-full">
  227. <input
  228. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  229. placeholder="Title (e.g. Tell me a fun fact)"
  230. bind:value={prompt.title[0]}
  231. />
  232. <input
  233. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  234. placeholder="Subtitle (e.g. about the Roman Empire)"
  235. bind:value={prompt.title[1]}
  236. />
  237. </div>
  238. <input
  239. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  240. placeholder="Prompt (e.g. Tell me a fun fact about the Roman Empire)"
  241. bind:value={prompt.content}
  242. />
  243. </div>
  244. <button
  245. class="px-2"
  246. type="button"
  247. on:click={() => {
  248. promptSuggestions.splice(promptIdx, 1);
  249. promptSuggestions = promptSuggestions;
  250. }}
  251. >
  252. <svg
  253. xmlns="http://www.w3.org/2000/svg"
  254. viewBox="0 0 20 20"
  255. fill="currentColor"
  256. class="w-4 h-4"
  257. >
  258. <path
  259. 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"
  260. />
  261. </svg>
  262. </button>
  263. </div>
  264. {/each}
  265. </div>
  266. {#if promptSuggestions.length > 0}
  267. <div class="text-xs text-left w-full mt-2">
  268. {$i18n.t('Adjusting these settings will apply changes universally to all users.')}
  269. </div>
  270. {/if}
  271. </div>
  272. {/if}
  273. </div>
  274. <div class="flex justify-end text-sm font-medium">
  275. <button
  276. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  277. type="submit"
  278. >
  279. {$i18n.t('Save')}
  280. </button>
  281. </div>
  282. </form>