Interface.svelte 12 KB

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