Interface.svelte 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <script lang="ts">
  2. import { v4 as uuidv4 } from 'uuid';
  3. import { toast } from 'svelte-sonner';
  4. import { getBackendConfig } from '$lib/apis';
  5. import { setDefaultPromptSuggestions } from '$lib/apis/configs';
  6. import { config, models, settings, user } from '$lib/stores';
  7. import { createEventDispatcher, onMount, getContext } from 'svelte';
  8. import { banners as _banners } from '$lib/stores';
  9. import type { Banner } from '$lib/types';
  10. import { getBanners, setBanners } from '$lib/apis/configs';
  11. import Tooltip from '$lib/components/common/Tooltip.svelte';
  12. import Switch from '$lib/components/common/Switch.svelte';
  13. const dispatch = createEventDispatcher();
  14. const i18n = getContext('i18n');
  15. let taskModel = '';
  16. let taskModelExternal = '';
  17. let titleGenerationPrompt = '';
  18. let promptSuggestions = [];
  19. let banners: Banner[] = [];
  20. const updateInterfaceHandler = async () => {
  21. promptSuggestions = await setDefaultPromptSuggestions(localStorage.token, promptSuggestions);
  22. await updateBanners();
  23. await config.set(await getBackendConfig());
  24. };
  25. onMount(async () => {
  26. taskModel = $settings?.title?.model ?? '';
  27. taskModelExternal = $settings?.title?.modelExternal ?? '';
  28. titleGenerationPrompt =
  29. $settings?.title?.prompt ??
  30. `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': {{prompt}}`;
  31. promptSuggestions = $config?.default_prompt_suggestions;
  32. banners = await getBanners(localStorage.token);
  33. });
  34. const updateBanners = async () => {
  35. _banners.set(await setBanners(localStorage.token, banners));
  36. };
  37. </script>
  38. <form
  39. class="flex flex-col h-full justify-between space-y-3 text-sm"
  40. on:submit|preventDefault={() => {
  41. updateInterfaceHandler();
  42. dispatch('save');
  43. }}
  44. >
  45. <div class=" overflow-y-scroll scrollbar-hidden h-full">
  46. <div>
  47. <div class=" mb-2.5 text-sm font-medium flex">
  48. <div class=" mr-1">{$i18n.t('Set Task Model')}</div>
  49. <Tooltip
  50. content={$i18n.t(
  51. 'A task model is used when performing tasks such as generating titles for chats and web search queries'
  52. )}
  53. >
  54. <svg
  55. xmlns="http://www.w3.org/2000/svg"
  56. fill="none"
  57. viewBox="0 0 24 24"
  58. stroke-width="1.5"
  59. stroke="currentColor"
  60. class="w-5 h-5"
  61. >
  62. <path
  63. stroke-linecap="round"
  64. stroke-linejoin="round"
  65. d="m11.25 11.25.041-.02a.75.75 0 0 1 1.063.852l-.708 2.836a.75.75 0 0 0 1.063.853l.041-.021M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Zm-9-3.75h.008v.008H12V8.25Z"
  66. />
  67. </svg>
  68. </Tooltip>
  69. </div>
  70. <div class="flex w-full gap-2">
  71. <div class="flex-1">
  72. <div class=" text-xs mb-1">{$i18n.t('Local Models')}</div>
  73. <select
  74. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  75. bind:value={taskModel}
  76. placeholder={$i18n.t('Select a model')}
  77. >
  78. <option value="" selected>{$i18n.t('Current Model')}</option>
  79. {#each $models.filter((m) => m.owned_by === 'ollama') as model}
  80. <option value={model.id} class="bg-gray-100 dark:bg-gray-700">
  81. {model.name}
  82. </option>
  83. {/each}
  84. </select>
  85. </div>
  86. <div class="flex-1">
  87. <div class=" text-xs mb-1">{$i18n.t('External Models')}</div>
  88. <select
  89. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  90. bind:value={taskModelExternal}
  91. placeholder={$i18n.t('Select a model')}
  92. >
  93. <option value="" selected>{$i18n.t('Current Model')}</option>
  94. {#each $models as model}
  95. <option value={model.id} class="bg-gray-100 dark:bg-gray-700">
  96. {model.name}
  97. </option>
  98. {/each}
  99. </select>
  100. </div>
  101. </div>
  102. <div class="mt-3">
  103. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Title Generation Prompt')}</div>
  104. <textarea
  105. bind:value={titleGenerationPrompt}
  106. class="w-full rounded-lg p-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none resize-none"
  107. rows="3"
  108. />
  109. </div>
  110. </div>
  111. <hr class=" dark:border-gray-850 my-3" />
  112. <div class=" space-y-3 {banners.length > 0 ? ' mb-3' : ''}">
  113. <div class="flex w-full justify-between">
  114. <div class=" self-center text-sm font-semibold">
  115. {$i18n.t('Banners')}
  116. </div>
  117. <button
  118. class="p-1 px-3 text-xs flex rounded transition"
  119. type="button"
  120. on:click={() => {
  121. if (banners.length === 0 || banners.at(-1).content !== '') {
  122. banners = [
  123. ...banners,
  124. {
  125. id: uuidv4(),
  126. type: '',
  127. title: '',
  128. content: '',
  129. dismissible: true,
  130. timestamp: Math.floor(Date.now() / 1000)
  131. }
  132. ];
  133. }
  134. }}
  135. >
  136. <svg
  137. xmlns="http://www.w3.org/2000/svg"
  138. viewBox="0 0 20 20"
  139. fill="currentColor"
  140. class="w-4 h-4"
  141. >
  142. <path
  143. 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"
  144. />
  145. </svg>
  146. </button>
  147. </div>
  148. <div class="flex flex-col space-y-1">
  149. {#each banners as banner, bannerIdx}
  150. <div class=" flex justify-between">
  151. <div class="flex flex-row flex-1 border rounded-xl dark:border-gray-800">
  152. <select
  153. class="w-fit capitalize rounded-xl py-2 px-4 text-xs bg-transparent outline-none"
  154. bind:value={banner.type}
  155. required
  156. >
  157. {#if banner.type == ''}
  158. <option value="" selected disabled class="text-gray-900">{$i18n.t('Type')}</option
  159. >
  160. {/if}
  161. <option value="info" class="text-gray-900">{$i18n.t('Info')}</option>
  162. <option value="warning" class="text-gray-900">{$i18n.t('Warning')}</option>
  163. <option value="error" class="text-gray-900">{$i18n.t('Error')}</option>
  164. <option value="success" class="text-gray-900">{$i18n.t('Success')}</option>
  165. </select>
  166. <input
  167. class="pr-5 py-1.5 text-xs w-full bg-transparent outline-none"
  168. placeholder={$i18n.t('Content')}
  169. bind:value={banner.content}
  170. />
  171. <div class="relative top-1.5 -left-2">
  172. <Tooltip content={$i18n.t('Dismissible')} className="flex h-fit items-center">
  173. <Switch bind:state={banner.dismissible} />
  174. </Tooltip>
  175. </div>
  176. </div>
  177. <button
  178. class="px-2"
  179. type="button"
  180. on:click={() => {
  181. banners.splice(bannerIdx, 1);
  182. banners = banners;
  183. }}
  184. >
  185. <svg
  186. xmlns="http://www.w3.org/2000/svg"
  187. viewBox="0 0 20 20"
  188. fill="currentColor"
  189. class="w-4 h-4"
  190. >
  191. <path
  192. 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"
  193. />
  194. </svg>
  195. </button>
  196. </div>
  197. {/each}
  198. </div>
  199. </div>
  200. {#if $user.role === 'admin'}
  201. <div class=" space-y-3">
  202. <div class="flex w-full justify-between mb-2">
  203. <div class=" self-center text-sm font-semibold">
  204. {$i18n.t('Default Prompt Suggestions')}
  205. </div>
  206. <button
  207. class="p-1 px-3 text-xs flex rounded transition"
  208. type="button"
  209. on:click={() => {
  210. if (promptSuggestions.length === 0 || promptSuggestions.at(-1).content !== '') {
  211. promptSuggestions = [...promptSuggestions, { content: '', title: ['', ''] }];
  212. }
  213. }}
  214. >
  215. <svg
  216. xmlns="http://www.w3.org/2000/svg"
  217. viewBox="0 0 20 20"
  218. fill="currentColor"
  219. class="w-4 h-4"
  220. >
  221. <path
  222. 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"
  223. />
  224. </svg>
  225. </button>
  226. </div>
  227. <div class="grid lg:grid-cols-2 flex-col gap-1.5">
  228. {#each promptSuggestions as prompt, promptIdx}
  229. <div class=" flex dark:bg-gray-850 rounded-xl py-1.5">
  230. <div class="flex flex-col flex-1 pl-1">
  231. <div class="flex border-b dark:border-gray-800 w-full">
  232. <input
  233. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-800"
  234. placeholder={$i18n.t('Title (e.g. Tell me a fun fact)')}
  235. bind:value={prompt.title[0]}
  236. />
  237. <input
  238. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-800"
  239. placeholder={$i18n.t('Subtitle (e.g. about the Roman Empire)')}
  240. bind:value={prompt.title[1]}
  241. />
  242. </div>
  243. <input
  244. class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-800"
  245. placeholder={$i18n.t('Prompt (e.g. Tell me a fun fact about the Roman Empire)')}
  246. bind:value={prompt.content}
  247. />
  248. </div>
  249. <button
  250. class="px-3"
  251. type="button"
  252. on:click={() => {
  253. promptSuggestions.splice(promptIdx, 1);
  254. promptSuggestions = promptSuggestions;
  255. }}
  256. >
  257. <svg
  258. xmlns="http://www.w3.org/2000/svg"
  259. viewBox="0 0 20 20"
  260. fill="currentColor"
  261. class="w-4 h-4"
  262. >
  263. <path
  264. 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"
  265. />
  266. </svg>
  267. </button>
  268. </div>
  269. {/each}
  270. </div>
  271. {#if promptSuggestions.length > 0}
  272. <div class="text-xs text-left w-full mt-2">
  273. {$i18n.t('Adjusting these settings will apply changes universally to all users.')}
  274. </div>
  275. {/if}
  276. </div>
  277. {/if}
  278. </div>
  279. <div class="flex justify-end text-sm font-medium">
  280. <button
  281. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  282. type="submit"
  283. >
  284. {$i18n.t('Save')}
  285. </button>
  286. </div>
  287. </form>