Interface.svelte 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 ??
  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">{$i18n.t('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">{$i18n.t('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">{$i18n.t('On')}</span>
  96. {:else}
  97. <span class="ml-2 self-center">{$i18n.t('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">{$i18n.t('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">{$i18n.t('On')}</span>
  114. {:else}
  115. <span class="ml-2 self-center">{$i18n.t('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">{$i18n.t('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">{$i18n.t('On')}</span>
  132. {:else}
  133. <span class="ml-2 self-center">{$i18n.t('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">{$i18n.t('On')}</span>
  152. {:else}
  153. <span class="ml-2 self-center">{$i18n.t('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">{$i18n.t('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 py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  166. bind:value={titleAutoGenerateModel}
  167. placeholder={$i18n.t('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. <button
  180. 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"
  181. on:click={() => {
  182. saveSettings({
  183. titleAutoGenerateModel:
  184. titleAutoGenerateModel !== '' ? titleAutoGenerateModel : undefined
  185. });
  186. }}
  187. type="button"
  188. >
  189. <svg
  190. xmlns="http://www.w3.org/2000/svg"
  191. viewBox="0 0 16 16"
  192. fill="currentColor"
  193. class="w-3.5 h-3.5"
  194. >
  195. <path
  196. fill-rule="evenodd"
  197. 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"
  198. clip-rule="evenodd"
  199. />
  200. </svg>
  201. </button>
  202. </div>
  203. <div class="mt-3">
  204. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Title Generation Prompt')}</div>
  205. <textarea
  206. bind:value={titleGenerationPrompt}
  207. class="w-full rounded p-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none resize-none"
  208. rows="3"
  209. />
  210. </div>
  211. </div>
  212. {#if $user.role === 'admin'}
  213. <hr class=" dark:border-gray-700" />
  214. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-80">
  215. <div class="flex w-full justify-between mb-2">
  216. <div class=" self-center text-sm font-semibold">{$i18n.t('Default Prompt Suggestions')}</div>
  217. <button
  218. class="p-1 px-3 text-xs flex rounded transition"
  219. type="button"
  220. on:click={() => {
  221. if (promptSuggestions.length === 0 || promptSuggestions.at(-1).content !== '') {
  222. promptSuggestions = [...promptSuggestions, { content: '', title: ['', ''] }];
  223. }
  224. }}
  225. >
  226. <svg
  227. xmlns="http://www.w3.org/2000/svg"
  228. viewBox="0 0 20 20"
  229. fill="currentColor"
  230. class="w-4 h-4"
  231. >
  232. <path
  233. 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"
  234. />
  235. </svg>
  236. </button>
  237. </div>
  238. <div class="flex flex-col space-y-1">
  239. {#each promptSuggestions as prompt, promptIdx}
  240. <div class=" flex border dark:border-gray-600 rounded-lg">
  241. <div class="flex flex-col flex-1">
  242. <div class="flex border-b dark:border-gray-600 w-full">
  243. <input
  244. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  245. placeholder="Title (e.g. Tell me a fun fact)"
  246. bind:value={prompt.title[0]}
  247. />
  248. <input
  249. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  250. placeholder="Subtitle (e.g. about the Roman Empire)"
  251. bind:value={prompt.title[1]}
  252. />
  253. </div>
  254. <input
  255. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  256. placeholder="Prompt (e.g. Tell me a fun fact about the Roman Empire)"
  257. bind:value={prompt.content}
  258. />
  259. </div>
  260. <button
  261. class="px-2"
  262. type="button"
  263. on:click={() => {
  264. promptSuggestions.splice(promptIdx, 1);
  265. promptSuggestions = promptSuggestions;
  266. }}
  267. >
  268. <svg
  269. xmlns="http://www.w3.org/2000/svg"
  270. viewBox="0 0 20 20"
  271. fill="currentColor"
  272. class="w-4 h-4"
  273. >
  274. <path
  275. 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"
  276. />
  277. </svg>
  278. </button>
  279. </div>
  280. {/each}
  281. </div>
  282. {#if promptSuggestions.length > 0}
  283. <div class="text-xs text-left w-full mt-2">
  284. Adjusting these settings will apply changes universally to all users.
  285. </div>
  286. {/if}
  287. </div>
  288. {/if}
  289. </div>
  290. <div class="flex justify-end pt-3 text-sm font-medium">
  291. <button
  292. class=" px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-gray-100 transition rounded"
  293. type="submit"
  294. >
  295. Save
  296. </button>
  297. </div>
  298. </form>