Interface.svelte 11 KB

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