Interface.svelte 8.8 KB

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