Interface.svelte 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 } from 'svelte';
  6. import toast from 'svelte-french-toast';
  7. const dispatch = createEventDispatcher();
  8. export let saveSettings: Function;
  9. // Addons
  10. let titleAutoGenerate = true;
  11. let speechAutoSend = false;
  12. let responseAutoCopy = false;
  13. let titleAutoGenerateModel = '';
  14. // Interface
  15. let promptSuggestions = [];
  16. const toggleSpeechAutoSend = async () => {
  17. speechAutoSend = !speechAutoSend;
  18. saveSettings({ speechAutoSend: speechAutoSend });
  19. };
  20. const toggleTitleAutoGenerate = async () => {
  21. titleAutoGenerate = !titleAutoGenerate;
  22. saveSettings({ titleAutoGenerate: titleAutoGenerate });
  23. };
  24. const toggleResponseAutoCopy = async () => {
  25. const permission = await navigator.clipboard
  26. .readText()
  27. .then(() => {
  28. return 'granted';
  29. })
  30. .catch(() => {
  31. return '';
  32. });
  33. console.log(permission);
  34. if (permission === 'granted') {
  35. responseAutoCopy = !responseAutoCopy;
  36. saveSettings({ responseAutoCopy: responseAutoCopy });
  37. } else {
  38. toast.error(
  39. 'Clipboard write permission denied. Please check your browser settings to grant the necessary access.'
  40. );
  41. }
  42. };
  43. const updateInterfaceHandler = async () => {
  44. promptSuggestions = await setDefaultPromptSuggestions(localStorage.token, promptSuggestions);
  45. await config.set(await getBackendConfig());
  46. };
  47. onMount(async () => {
  48. if ($user.role === 'admin') {
  49. promptSuggestions = $config?.default_prompt_suggestions;
  50. }
  51. let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');
  52. titleAutoGenerate = settings.titleAutoGenerate ?? true;
  53. speechAutoSend = settings.speechAutoSend ?? false;
  54. responseAutoCopy = settings.responseAutoCopy ?? false;
  55. titleAutoGenerateModel = settings.titleAutoGenerateModel ?? '';
  56. });
  57. </script>
  58. <form
  59. class="flex flex-col h-full justify-between space-y-3 text-sm"
  60. on:submit|preventDefault={() => {
  61. updateInterfaceHandler();
  62. dispatch('save');
  63. }}
  64. >
  65. <div class=" space-y-3 pr-1.5 overflow-y-scroll h-80">
  66. <div>
  67. <div class=" mb-1 text-sm font-medium">WebUI Add-ons</div>
  68. <div>
  69. <div class=" py-0.5 flex w-full justify-between">
  70. <div class=" self-center text-xs font-medium">Title Auto-Generation</div>
  71. <button
  72. class="p-1 px-3 text-xs flex rounded transition"
  73. on:click={() => {
  74. toggleTitleAutoGenerate();
  75. }}
  76. type="button"
  77. >
  78. {#if titleAutoGenerate === true}
  79. <span class="ml-2 self-center">On</span>
  80. {:else}
  81. <span class="ml-2 self-center">Off</span>
  82. {/if}
  83. </button>
  84. </div>
  85. </div>
  86. <div>
  87. <div class=" py-0.5 flex w-full justify-between">
  88. <div class=" self-center text-xs font-medium">Voice Input Auto-Send</div>
  89. <button
  90. class="p-1 px-3 text-xs flex rounded transition"
  91. on:click={() => {
  92. toggleSpeechAutoSend();
  93. }}
  94. type="button"
  95. >
  96. {#if speechAutoSend === true}
  97. <span class="ml-2 self-center">On</span>
  98. {:else}
  99. <span class="ml-2 self-center">Off</span>
  100. {/if}
  101. </button>
  102. </div>
  103. </div>
  104. <div>
  105. <div class=" py-0.5 flex w-full justify-between">
  106. <div class=" self-center text-xs font-medium">Response AutoCopy to Clipboard</div>
  107. <button
  108. class="p-1 px-3 text-xs flex rounded transition"
  109. on:click={() => {
  110. toggleResponseAutoCopy();
  111. }}
  112. type="button"
  113. >
  114. {#if responseAutoCopy === true}
  115. <span class="ml-2 self-center">On</span>
  116. {:else}
  117. <span class="ml-2 self-center">Off</span>
  118. {/if}
  119. </button>
  120. </div>
  121. </div>
  122. </div>
  123. <hr class=" dark:border-gray-700" />
  124. <div>
  125. <div class=" mb-2.5 text-sm font-medium">Set Title Auto-Generation Model</div>
  126. <div class="flex w-full">
  127. <div class="flex-1 mr-2">
  128. <select
  129. class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  130. bind:value={titleAutoGenerateModel}
  131. placeholder="Select a model"
  132. >
  133. <option value="" selected>Current Model</option>
  134. {#each $models.filter((m) => m.size != null) as model}
  135. <option value={model.name} class="bg-gray-100 dark:bg-gray-700"
  136. >{model.name + ' (' + (model.size / 1024 ** 3).toFixed(1) + ' GB)'}</option
  137. >
  138. {/each}
  139. </select>
  140. </div>
  141. <button
  142. 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"
  143. on:click={() => {
  144. saveSettings({
  145. titleAutoGenerateModel:
  146. titleAutoGenerateModel !== '' ? titleAutoGenerateModel : undefined
  147. });
  148. }}
  149. type="button"
  150. >
  151. <svg
  152. xmlns="http://www.w3.org/2000/svg"
  153. viewBox="0 0 16 16"
  154. fill="currentColor"
  155. class="w-3.5 h-3.5"
  156. >
  157. <path
  158. fill-rule="evenodd"
  159. 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"
  160. clip-rule="evenodd"
  161. />
  162. </svg>
  163. </button>
  164. </div>
  165. </div>
  166. {#if $user.role === 'admin'}
  167. <hr class=" dark:border-gray-700" />
  168. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-80">
  169. <div class="flex w-full justify-between mb-2">
  170. <div class=" self-center text-sm font-semibold">Default Prompt Suggestions</div>
  171. <button
  172. class="p-1 px-3 text-xs flex rounded transition"
  173. type="button"
  174. on:click={() => {
  175. if (promptSuggestions.length === 0 || promptSuggestions.at(-1).content !== '') {
  176. promptSuggestions = [...promptSuggestions, { content: '', title: ['', ''] }];
  177. }
  178. }}
  179. >
  180. <svg
  181. xmlns="http://www.w3.org/2000/svg"
  182. viewBox="0 0 20 20"
  183. fill="currentColor"
  184. class="w-4 h-4"
  185. >
  186. <path
  187. 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"
  188. />
  189. </svg>
  190. </button>
  191. </div>
  192. <div class="flex flex-col space-y-1">
  193. {#each promptSuggestions as prompt, promptIdx}
  194. <div class=" flex border dark:border-gray-600 rounded-lg">
  195. <div class="flex flex-col flex-1">
  196. <div class="flex border-b dark:border-gray-600 w-full">
  197. <input
  198. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  199. placeholder="Title (e.g. Tell me a fun fact)"
  200. bind:value={prompt.title[0]}
  201. />
  202. <input
  203. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  204. placeholder="Subtitle (e.g. about the Roman Empire)"
  205. bind:value={prompt.title[1]}
  206. />
  207. </div>
  208. <input
  209. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
  210. placeholder="Prompt (e.g. Tell me a fun fact about the Roman Empire)"
  211. bind:value={prompt.content}
  212. />
  213. </div>
  214. <button
  215. class="px-2"
  216. type="button"
  217. on:click={() => {
  218. promptSuggestions.splice(promptIdx, 1);
  219. promptSuggestions = promptSuggestions;
  220. }}
  221. >
  222. <svg
  223. xmlns="http://www.w3.org/2000/svg"
  224. viewBox="0 0 20 20"
  225. fill="currentColor"
  226. class="w-4 h-4"
  227. >
  228. <path
  229. 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"
  230. />
  231. </svg>
  232. </button>
  233. </div>
  234. {/each}
  235. </div>
  236. {#if promptSuggestions.length > 0}
  237. <div class="text-xs text-left w-full mt-2">
  238. Adjusting these settings will apply changes universally to all users.
  239. </div>
  240. {/if}
  241. </div>
  242. {/if}
  243. </div>
  244. <div class="flex justify-end pt-3 text-sm font-medium">
  245. <button
  246. class=" px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-gray-100 transition rounded"
  247. type="submit"
  248. >
  249. Save
  250. </button>
  251. </div>
  252. </form>