Interface.svelte 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. titleGenerationPrompt: titleGenerationPrompt ? titleGenerationPrompt : undefined
  57. });
  58. };
  59. onMount(async () => {
  60. if ($user.role === 'admin') {
  61. promptSuggestions = $config?.default_prompt_suggestions;
  62. }
  63. let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');
  64. titleAutoGenerate = settings.titleAutoGenerate ?? true;
  65. responseAutoCopy = settings.responseAutoCopy ?? false;
  66. showUsername = settings.showUsername ?? false;
  67. fullScreenMode = settings.fullScreenMode ?? false;
  68. titleAutoGenerateModel = settings.titleAutoGenerateModel ?? '';
  69. titleGenerationPrompt =
  70. settings.titleGenerationPrompt ?? $i18n.t('Default titleGenerationPromt') + ' {{prompt}}';
  71. });
  72. </script>
  73. <form
  74. class="flex flex-col h-full justify-between space-y-3 text-sm"
  75. on:submit|preventDefault={() => {
  76. updateInterfaceHandler();
  77. dispatch('save');
  78. }}
  79. >
  80. <div class=" space-y-3 pr-1.5 overflow-y-scroll h-80">
  81. <div>
  82. <div class=" mb-1 text-sm font-medium">{$i18n.t('WebUI Add-ons')}</div>
  83. <div>
  84. <div class=" py-0.5 flex w-full justify-between">
  85. <div class=" self-center text-xs font-medium">{$i18n.t('Title Auto-Generation')}</div>
  86. <button
  87. class="p-1 px-3 text-xs flex rounded transition"
  88. on:click={() => {
  89. toggleTitleAutoGenerate();
  90. }}
  91. type="button"
  92. >
  93. {#if titleAutoGenerate === true}
  94. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  95. {:else}
  96. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  97. {/if}
  98. </button>
  99. </div>
  100. </div>
  101. <div>
  102. <div class=" py-0.5 flex w-full justify-between">
  103. <div class=" self-center text-xs font-medium">
  104. {$i18n.t('Response AutoCopy to Clipboard')}
  105. </div>
  106. <button
  107. class="p-1 px-3 text-xs flex rounded transition"
  108. on:click={() => {
  109. toggleResponseAutoCopy();
  110. }}
  111. type="button"
  112. >
  113. {#if responseAutoCopy === true}
  114. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  115. {:else}
  116. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  117. {/if}
  118. </button>
  119. </div>
  120. </div>
  121. <div>
  122. <div class=" py-0.5 flex w-full justify-between">
  123. <div class=" self-center text-xs font-medium">{$i18n.t('Full Screen Mode')}</div>
  124. <button
  125. class="p-1 px-3 text-xs flex rounded transition"
  126. on:click={() => {
  127. toggleFullScreenMode();
  128. }}
  129. type="button"
  130. >
  131. {#if fullScreenMode === true}
  132. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  133. {:else}
  134. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  135. {/if}
  136. </button>
  137. </div>
  138. </div>
  139. <div>
  140. <div class=" py-0.5 flex w-full justify-between">
  141. <div class=" self-center text-xs font-medium">
  142. {$i18n.t('Display the username instead of You in the Chat')}
  143. </div>
  144. <button
  145. class="p-1 px-3 text-xs flex rounded transition"
  146. on:click={() => {
  147. toggleShowUsername();
  148. }}
  149. type="button"
  150. >
  151. {#if showUsername === true}
  152. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  153. {:else}
  154. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  155. {/if}
  156. </button>
  157. </div>
  158. </div>
  159. </div>
  160. <hr class=" dark:border-gray-700" />
  161. <div>
  162. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Title Auto-Generation Model')}</div>
  163. <div class="flex w-full">
  164. <div class="flex-1 mr-2">
  165. <select
  166. class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  167. bind:value={titleAutoGenerateModel}
  168. placeholder={$i18n.t('Select a model')}
  169. >
  170. <option value="" selected>{$i18n.t('Current Model')}</option>
  171. {#each $models as model}
  172. {#if model.size != null}
  173. <option value={model.name} class="bg-gray-100 dark:bg-gray-700">
  174. {model.name + ' (' + (model.size / 1024 ** 3).toFixed(1) + ' GB)'}
  175. </option>
  176. {/if}
  177. {/each}
  178. </select>
  179. </div>
  180. <button
  181. class="px-3 bg-gray-200 hover:bg-gray-300 dark:bg-gray-700 dark:hover:bg-gray-800 dark:text-gray-100 rounded transition"
  182. on:click={() => {
  183. saveSettings({
  184. titleAutoGenerateModel:
  185. titleAutoGenerateModel !== '' ? titleAutoGenerateModel : undefined
  186. });
  187. }}
  188. type="button"
  189. >
  190. <svg
  191. xmlns="http://www.w3.org/2000/svg"
  192. viewBox="0 0 16 16"
  193. fill="currentColor"
  194. class="w-3.5 h-3.5"
  195. >
  196. <path
  197. fill-rule="evenodd"
  198. d="M13.836 2.477a.75.75 0 0 1 .75.75v3.182a.75.75 0 0 1-.75.75h-3.182a.75.75 0 0 1 0-1.5h1.37l-.84-.841a4.5 4.5 0 0 0-7.08.932.75.75 0 0 1-1.3-.75 6 6 0 0 1 9.44-1.242l.842.84V3.227a.75.75 0 0 1 .75-.75Zm-.911 7.5A.75.75 0 0 1 13.199 11a6 6 0 0 1-9.44 1.241l-.84-.84v1.371a.75.75 0 0 1-1.5 0V9.591a.75.75 0 0 1 .75-.75H5.35a.75.75 0 0 1 0 1.5H3.98l.841.841a4.5 4.5 0 0 0 7.08-.932.75.75 0 0 1 1.025-.273Z"
  199. clip-rule="evenodd"
  200. />
  201. </svg>
  202. </button>
  203. </div>
  204. <div class="mt-3">
  205. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Title Generation Prompt')}</div>
  206. <textarea
  207. bind:value={titleGenerationPrompt}
  208. class="w-full rounded p-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none resize-none"
  209. rows="3"
  210. />
  211. </div>
  212. </div>
  213. {#if $user.role === 'admin'}
  214. <hr class=" dark:border-gray-700" />
  215. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-80">
  216. <div class="flex w-full justify-between mb-2">
  217. <div class=" self-center text-sm font-semibold">
  218. {$i18n.t('Default Prompt Suggestions')}
  219. </div>
  220. <button
  221. class="p-1 px-3 text-xs flex rounded transition"
  222. type="button"
  223. on:click={() => {
  224. if (promptSuggestions.length === 0 || promptSuggestions.at(-1).content !== '') {
  225. promptSuggestions = [...promptSuggestions, { content: '', title: ['', ''] }];
  226. }
  227. }}
  228. >
  229. <svg
  230. xmlns="http://www.w3.org/2000/svg"
  231. viewBox="0 0 20 20"
  232. fill="currentColor"
  233. class="w-4 h-4"
  234. >
  235. <path
  236. 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"
  237. />
  238. </svg>
  239. </button>
  240. </div>
  241. <div class="flex flex-col space-y-1">
  242. {#each promptSuggestions as prompt, promptIdx}
  243. <div class=" flex border dark:border-gray-600 rounded-lg">
  244. <div class="flex flex-col flex-1">
  245. <div class="flex border-b dark:border-gray-600 w-full">
  246. <input
  247. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  248. placeholder="Title (e.g. Tell me a fun fact)"
  249. bind:value={prompt.title[0]}
  250. />
  251. <input
  252. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  253. placeholder="Subtitle (e.g. about the Roman Empire)"
  254. bind:value={prompt.title[1]}
  255. />
  256. </div>
  257. <input
  258. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  259. placeholder="Prompt (e.g. Tell me a fun fact about the Roman Empire)"
  260. bind:value={prompt.content}
  261. />
  262. </div>
  263. <button
  264. class="px-2"
  265. type="button"
  266. on:click={() => {
  267. promptSuggestions.splice(promptIdx, 1);
  268. promptSuggestions = promptSuggestions;
  269. }}
  270. >
  271. <svg
  272. xmlns="http://www.w3.org/2000/svg"
  273. viewBox="0 0 20 20"
  274. fill="currentColor"
  275. class="w-4 h-4"
  276. >
  277. <path
  278. 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"
  279. />
  280. </svg>
  281. </button>
  282. </div>
  283. {/each}
  284. </div>
  285. {#if promptSuggestions.length > 0}
  286. <div class="text-xs text-left w-full mt-2">
  287. Adjusting these settings will apply changes universally to all users.
  288. </div>
  289. {/if}
  290. </div>
  291. {/if}
  292. </div>
  293. <div class="flex justify-end pt-3 text-sm font-medium">
  294. <button
  295. class=" px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-gray-100 transition rounded"
  296. type="submit"
  297. >
  298. Save
  299. </button>
  300. </div>
  301. </form>