Interface.svelte 10 KB

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