AddServerModal.svelte 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { getContext, onMount } from 'svelte';
  4. const i18n = getContext('i18n');
  5. import { models } from '$lib/stores';
  6. import { verifyOpenAIConnection } from '$lib/apis/openai';
  7. import { verifyOllamaConnection } from '$lib/apis/ollama';
  8. import Modal from '$lib/components/common/Modal.svelte';
  9. import Plus from '$lib/components/icons/Plus.svelte';
  10. import Minus from '$lib/components/icons/Minus.svelte';
  11. import PencilSolid from '$lib/components/icons/PencilSolid.svelte';
  12. import SensitiveInput from '$lib/components/common/SensitiveInput.svelte';
  13. import Tooltip from '$lib/components/common/Tooltip.svelte';
  14. import Switch from '$lib/components/common/Switch.svelte';
  15. import Tags from './common/Tags.svelte';
  16. import { getToolServerData } from '$lib/apis';
  17. import { verifyToolServerConnection } from '$lib/apis/configs';
  18. import AccessControl from './workspace/common/AccessControl.svelte';
  19. export let onSubmit: Function = () => {};
  20. export let onDelete: Function = () => {};
  21. export let show = false;
  22. export let edit = false;
  23. export let direct = false;
  24. export let connection = null;
  25. let url = '';
  26. let path = 'openapi.json';
  27. let auth_type = 'bearer';
  28. let key = '';
  29. let accessControl = {};
  30. let enable = true;
  31. let loading = false;
  32. const verifyHandler = async () => {
  33. if (url === '') {
  34. toast.error($i18n.t('Please enter a valid URL'));
  35. return;
  36. }
  37. if (path === '') {
  38. toast.error($i18n.t('Please enter a valid path'));
  39. return;
  40. }
  41. if (direct) {
  42. const res = await getToolServerData(
  43. auth_type === 'bearer' ? key : localStorage.token,
  44. `${url}/${path}`
  45. ).catch((err) => {
  46. toast.error($i18n.t('Connection failed'));
  47. });
  48. if (res) {
  49. toast.success($i18n.t('Connection successful'));
  50. console.debug('Connection successful', res);
  51. }
  52. } else {
  53. const res = await verifyToolServerConnection(localStorage.token, {
  54. url,
  55. path,
  56. auth_type,
  57. key,
  58. config: {
  59. enable: enable,
  60. access_control: accessControl
  61. }
  62. }).catch((err) => {
  63. toast.error($i18n.t('Connection failed'));
  64. });
  65. if (res) {
  66. toast.success($i18n.t('Connection successful'));
  67. console.debug('Connection successful', res);
  68. }
  69. }
  70. };
  71. const submitHandler = async () => {
  72. loading = true;
  73. // remove trailing slash from url
  74. url = url.replace(/\/$/, '');
  75. const connection = {
  76. url,
  77. path,
  78. auth_type,
  79. key,
  80. config: {
  81. enable: enable,
  82. access_control: accessControl
  83. }
  84. };
  85. await onSubmit(connection);
  86. loading = false;
  87. show = false;
  88. url = '';
  89. path = 'openapi.json';
  90. key = '';
  91. auth_type = 'bearer';
  92. enable = true;
  93. accessControl = null;
  94. };
  95. const init = () => {
  96. if (connection) {
  97. url = connection.url;
  98. path = connection?.path ?? 'openapi.json';
  99. auth_type = connection?.auth_type ?? 'bearer';
  100. key = connection?.key ?? '';
  101. enable = connection.config?.enable ?? true;
  102. accessControl = connection.config?.access_control ?? null;
  103. }
  104. };
  105. $: if (show) {
  106. init();
  107. }
  108. onMount(() => {
  109. init();
  110. });
  111. </script>
  112. <Modal size="sm" bind:show>
  113. <div>
  114. <div class=" flex justify-between dark:text-gray-100 px-5 pt-4 pb-2">
  115. <div class=" text-lg font-medium self-center font-primary">
  116. {#if edit}
  117. {$i18n.t('Edit Connection')}
  118. {:else}
  119. {$i18n.t('Add Connection')}
  120. {/if}
  121. </div>
  122. <button
  123. class="self-center"
  124. on:click={() => {
  125. show = false;
  126. }}
  127. >
  128. <svg
  129. xmlns="http://www.w3.org/2000/svg"
  130. viewBox="0 0 20 20"
  131. fill="currentColor"
  132. class="w-5 h-5"
  133. >
  134. <path
  135. 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"
  136. />
  137. </svg>
  138. </button>
  139. </div>
  140. <div class="flex flex-col md:flex-row w-full px-4 pb-4 md:space-x-4 dark:text-gray-200">
  141. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  142. <form
  143. class="flex flex-col w-full"
  144. on:submit={(e) => {
  145. e.preventDefault();
  146. submitHandler();
  147. }}
  148. >
  149. <div class="px-1">
  150. <div class="flex gap-2">
  151. <div class="flex flex-col w-full">
  152. <div class="flex justify-between mb-0.5">
  153. <div class=" text-xs text-gray-500">{$i18n.t('URL')}</div>
  154. </div>
  155. <div class="flex flex-1 items-center">
  156. <input
  157. class="w-full flex-1 text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-hidden"
  158. type="text"
  159. bind:value={url}
  160. placeholder={$i18n.t('API Base URL')}
  161. autocomplete="off"
  162. required
  163. />
  164. <Tooltip
  165. content={$i18n.t('Verify Connection')}
  166. className="shrink-0 flex items-center mr-1"
  167. >
  168. <button
  169. class="self-center p-1 bg-transparent hover:bg-gray-100 dark:bg-gray-900 dark:hover:bg-gray-850 rounded-lg transition"
  170. on:click={() => {
  171. verifyHandler();
  172. }}
  173. type="button"
  174. >
  175. <svg
  176. xmlns="http://www.w3.org/2000/svg"
  177. viewBox="0 0 20 20"
  178. fill="currentColor"
  179. class="w-4 h-4"
  180. >
  181. <path
  182. fill-rule="evenodd"
  183. d="M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H3.989a.75.75 0 00-.75.75v4.242a.75.75 0 001.5 0v-2.43l.31.31a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm1.23-3.723a.75.75 0 00.219-.53V2.929a.75.75 0 00-1.5 0V5.36l-.31-.31A7 7 0 003.239 8.188a.75.75 0 101.448.389A5.5 5.5 0 0113.89 6.11l.311.31h-2.432a.75.75 0 000 1.5h4.243a.75.75 0 00.53-.219z"
  184. clip-rule="evenodd"
  185. />
  186. </svg>
  187. </button>
  188. </Tooltip>
  189. <Tooltip content={enable ? $i18n.t('Enabled') : $i18n.t('Disabled')}>
  190. <Switch bind:state={enable} />
  191. </Tooltip>
  192. </div>
  193. <div class="flex-1 flex items-center">
  194. <div class="text-sm">/</div>
  195. <input
  196. class="w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-hidden"
  197. type="text"
  198. bind:value={path}
  199. placeholder={$i18n.t('openapi.json Path')}
  200. autocomplete="off"
  201. required
  202. />
  203. </div>
  204. </div>
  205. </div>
  206. <div class="text-xs text-gray-500 mt-1">
  207. {$i18n.t(`WebUI will make requests to "{{url}}"`, {
  208. url: `${url}/${path}`
  209. })}
  210. </div>
  211. <div class="flex gap-2 mt-2">
  212. <div class="flex flex-col w-full">
  213. <div class=" text-xs text-gray-500">{$i18n.t('Auth')}</div>
  214. <div class="flex gap-2">
  215. <div class="flex-shrink-0 self-start">
  216. <select
  217. class="w-full text-sm bg-transparent dark:bg-gray-900 placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-hidden pr-5"
  218. bind:value={auth_type}
  219. >
  220. <option value="bearer">Bearer</option>
  221. <option value="session">Session</option>
  222. </select>
  223. </div>
  224. <div class="flex flex-1 items-center">
  225. {#if auth_type === 'bearer'}
  226. <SensitiveInput
  227. className="w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-hidden"
  228. bind:value={key}
  229. placeholder={$i18n.t('API Key')}
  230. required={false}
  231. />
  232. {:else if auth_type === 'session'}
  233. <div class="text-xs text-gray-500 self-center translate-y-[1px]">
  234. {$i18n.t('Forwards system user session credentials to authenticate')}
  235. </div>
  236. {/if}
  237. </div>
  238. </div>
  239. </div>
  240. </div>
  241. {#if !direct}
  242. <hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" />
  243. <div class="my-2 -mx-2">
  244. <div class="px-3 py-2 bg-gray-50 dark:bg-gray-950 rounded-lg">
  245. <AccessControl bind:accessControl />
  246. </div>
  247. </div>
  248. {/if}
  249. </div>
  250. <div class="flex justify-end pt-3 text-sm font-medium gap-1.5">
  251. {#if edit}
  252. <button
  253. class="px-3.5 py-1.5 text-sm font-medium dark:bg-black dark:hover:bg-gray-900 dark:text-white bg-white text-black hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center"
  254. type="button"
  255. on:click={() => {
  256. onDelete();
  257. show = false;
  258. }}
  259. >
  260. {$i18n.t('Delete')}
  261. </button>
  262. {/if}
  263. <button
  264. class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center {loading
  265. ? ' cursor-not-allowed'
  266. : ''}"
  267. type="submit"
  268. disabled={loading}
  269. >
  270. {$i18n.t('Save')}
  271. {#if loading}
  272. <div class="ml-2 self-center">
  273. <svg
  274. class=" w-4 h-4"
  275. viewBox="0 0 24 24"
  276. fill="currentColor"
  277. xmlns="http://www.w3.org/2000/svg"
  278. ><style>
  279. .spinner_ajPY {
  280. transform-origin: center;
  281. animation: spinner_AtaB 0.75s infinite linear;
  282. }
  283. @keyframes spinner_AtaB {
  284. 100% {
  285. transform: rotate(360deg);
  286. }
  287. }
  288. </style><path
  289. d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
  290. opacity=".25"
  291. /><path
  292. d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
  293. class="spinner_ajPY"
  294. /></svg
  295. >
  296. </div>
  297. {/if}
  298. </button>
  299. </div>
  300. </form>
  301. </div>
  302. </div>
  303. </div>
  304. </Modal>