Interface.svelte 12 KB

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