AddConnectionModal.svelte 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { getContext, onMount } from 'svelte';
  4. const i18n = getContext('i18n');
  5. import { settings } 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 Spinner from '$lib/components/common/Spinner.svelte';
  17. import XMark from '$lib/components/icons/XMark.svelte';
  18. export let onSubmit: Function = () => {};
  19. export let onDelete: Function = () => {};
  20. export let show = false;
  21. export let edit = false;
  22. export let ollama = false;
  23. export let direct = false;
  24. export let connection = null;
  25. let url = '';
  26. let key = '';
  27. let auth_type = 'bearer';
  28. let connectionType = 'external';
  29. let azure = false;
  30. $: azure =
  31. (url.includes('azure.') || url.includes('cognitive.microsoft.com')) && !direct ? true : false;
  32. let prefixId = '';
  33. let enable = true;
  34. let apiVersion = '';
  35. let tags = [];
  36. let modelId = '';
  37. let modelIds = [];
  38. let loading = false;
  39. const verifyOllamaHandler = async () => {
  40. // remove trailing slash from url
  41. url = url.replace(/\/$/, '');
  42. const res = await verifyOllamaConnection(localStorage.token, {
  43. url,
  44. key
  45. }).catch((error) => {
  46. toast.error(`${error}`);
  47. });
  48. if (res) {
  49. toast.success($i18n.t('Server connection verified'));
  50. }
  51. };
  52. const verifyOpenAIHandler = async () => {
  53. // remove trailing slash from url
  54. url = url.replace(/\/$/, '');
  55. const res = await verifyOpenAIConnection(
  56. localStorage.token,
  57. {
  58. url,
  59. key,
  60. auth_type,
  61. config: {
  62. azure: azure,
  63. api_version: apiVersion
  64. }
  65. },
  66. direct
  67. ).catch((error) => {
  68. toast.error(`${error}`);
  69. });
  70. if (res) {
  71. toast.success($i18n.t('Server connection verified'));
  72. }
  73. };
  74. const verifyHandler = () => {
  75. if (ollama) {
  76. verifyOllamaHandler();
  77. } else {
  78. verifyOpenAIHandler();
  79. }
  80. };
  81. const addModelHandler = () => {
  82. if (modelId) {
  83. modelIds = [...modelIds, modelId];
  84. modelId = '';
  85. }
  86. };
  87. const submitHandler = async () => {
  88. loading = true;
  89. if (!ollama && !url) {
  90. loading = false;
  91. toast.error($i18n.t('URL is required'));
  92. return;
  93. }
  94. if (azure) {
  95. if (!apiVersion) {
  96. loading = false;
  97. toast.error($i18n.t('API Version is required'));
  98. return;
  99. }
  100. if (!key) {
  101. loading = false;
  102. toast.error($i18n.t('Key is required'));
  103. return;
  104. }
  105. if (modelIds.length === 0) {
  106. loading = false;
  107. toast.error($i18n.t('Deployment names are required for Azure OpenAI'));
  108. return;
  109. }
  110. }
  111. // remove trailing slash from url
  112. url = url.replace(/\/$/, '');
  113. const connection = {
  114. url,
  115. key,
  116. auth_type,
  117. config: {
  118. enable: enable,
  119. tags: tags,
  120. prefix_id: prefixId,
  121. model_ids: modelIds,
  122. connection_type: connectionType,
  123. ...(!ollama && azure ? { azure: true, api_version: apiVersion } : {})
  124. }
  125. };
  126. await onSubmit(connection);
  127. loading = false;
  128. show = false;
  129. url = '';
  130. key = '';
  131. auth_type = 'bearer';
  132. prefixId = '';
  133. tags = [];
  134. modelIds = [];
  135. };
  136. const init = () => {
  137. if (connection) {
  138. url = connection.url;
  139. key = connection.key;
  140. auth_type = connection.auth_type ?? 'bearer';
  141. enable = connection.config?.enable ?? true;
  142. tags = connection.config?.tags ?? [];
  143. prefixId = connection.config?.prefix_id ?? '';
  144. modelIds = connection.config?.model_ids ?? [];
  145. if (ollama) {
  146. connectionType = connection.config?.connection_type ?? 'local';
  147. } else {
  148. connectionType = connection.config?.connection_type ?? 'external';
  149. azure = connection.config?.azure ?? false;
  150. apiVersion = connection.config?.api_version ?? '';
  151. }
  152. }
  153. };
  154. $: if (show) {
  155. init();
  156. }
  157. onMount(() => {
  158. init();
  159. });
  160. </script>
  161. <Modal size="sm" bind:show>
  162. <div>
  163. <div class=" flex justify-between dark:text-gray-100 px-5 pt-4 pb-1.5">
  164. <h1 class="text-lg font-medium self-center font-primary">
  165. {#if edit}
  166. {$i18n.t('Edit Connection')}
  167. {:else}
  168. {$i18n.t('Add Connection')}
  169. {/if}
  170. </h1>
  171. <button
  172. class="self-center"
  173. aria-label={$i18n.t('Close modal')}
  174. on:click={() => {
  175. show = false;
  176. }}
  177. >
  178. <XMark className={'size-5'} />
  179. </button>
  180. </div>
  181. <div class="flex flex-col md:flex-row w-full px-4 pb-4 md:space-x-4 dark:text-gray-200">
  182. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  183. <form
  184. class="flex flex-col w-full"
  185. on:submit={(e) => {
  186. e.preventDefault();
  187. submitHandler();
  188. }}
  189. >
  190. <div class="px-1">
  191. {#if !direct}
  192. <div class="flex gap-2">
  193. <div class="flex w-full justify-between items-center">
  194. <div class=" text-xs text-gray-500">{$i18n.t('Connection Type')}</div>
  195. <div class="">
  196. <button
  197. on:click={() => {
  198. connectionType = connectionType === 'local' ? 'external' : 'local';
  199. }}
  200. type="button"
  201. class=" text-xs text-gray-700 dark:text-gray-300"
  202. >
  203. {#if connectionType === 'local'}
  204. {$i18n.t('Local')}
  205. {:else}
  206. {$i18n.t('External')}
  207. {/if}
  208. </button>
  209. </div>
  210. </div>
  211. </div>
  212. {/if}
  213. <div class="flex gap-2 mt-1.5">
  214. <div class="flex flex-col w-full">
  215. <label
  216. for="url-input"
  217. class={`mb-0.5 text-xs text-gray-500
  218. ${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : ''}`}
  219. >{$i18n.t('URL')}</label
  220. >
  221. <div class="flex-1">
  222. <input
  223. id="url-input"
  224. class={`w-full text-sm bg-transparent ${($settings?.highContrastMode ?? false) ? 'placeholder:text-gray-700 dark:placeholder:text-gray-100' : 'outline-hidden placeholder:text-gray-300 dark:placeholder:text-gray-700'}`}
  225. type="text"
  226. bind:value={url}
  227. placeholder={$i18n.t('API Base URL')}
  228. autocomplete="off"
  229. required
  230. />
  231. </div>
  232. </div>
  233. <Tooltip content={$i18n.t('Verify Connection')} className="self-end -mb-1">
  234. <button
  235. class="self-center p-1 bg-transparent hover:bg-gray-100 dark:bg-gray-900 dark:hover:bg-gray-850 rounded-lg transition"
  236. on:click={() => {
  237. verifyHandler();
  238. }}
  239. type="button"
  240. aria-label={$i18n.t('Verify Connection')}
  241. >
  242. <svg
  243. xmlns="http://www.w3.org/2000/svg"
  244. viewBox="0 0 20 20"
  245. fill="currentColor"
  246. aria-hidden="true"
  247. class="w-4 h-4"
  248. >
  249. <path
  250. fill-rule="evenodd"
  251. 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"
  252. clip-rule="evenodd"
  253. />
  254. </svg>
  255. </button>
  256. </Tooltip>
  257. <div class="flex flex-col shrink-0 self-end">
  258. <label class="sr-only" for="toggle-connection"
  259. >{$i18n.t('Toggle whether current connection is active.')}</label
  260. >
  261. <Tooltip content={enable ? $i18n.t('Enabled') : $i18n.t('Disabled')}>
  262. <Switch id="toggle-connection" bind:state={enable} />
  263. </Tooltip>
  264. </div>
  265. </div>
  266. <div class="flex gap-2 mt-2">
  267. <div class="flex flex-col w-full">
  268. <label
  269. for="select-bearer-or-session"
  270. class={`text-xs ${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : 'text-gray-500'}`}
  271. >{$i18n.t('Auth')}</label
  272. >
  273. <div class="flex gap-2">
  274. <div class="flex-shrink-0 self-start">
  275. <select
  276. id="select-bearer-or-session"
  277. class={`w-full text-sm bg-transparent pr-5 ${($settings?.highContrastMode ?? false) ? 'placeholder:text-gray-700 dark:placeholder:text-gray-100' : 'outline-hidden placeholder:text-gray-300 dark:placeholder:text-gray-700'}`}
  278. bind:value={auth_type}
  279. >
  280. <option value="none">{$i18n.t('None')}</option>
  281. <option value="bearer">{$i18n.t('Bearer')}</option>
  282. {#if !ollama}
  283. <option value="session">{$i18n.t('Session')}</option>
  284. {#if !direct}
  285. <option value="oauth">{$i18n.t('OAuth')}</option>
  286. {/if}
  287. {/if}
  288. </select>
  289. </div>
  290. <div class="flex flex-1 items-center">
  291. {#if auth_type === 'bearer'}
  292. <SensitiveInput
  293. bind:value={key}
  294. placeholder={$i18n.t('API Key')}
  295. required={false}
  296. />
  297. {:else if auth_type === 'none'}
  298. <div
  299. class={`text-xs self-center translate-y-[1px] ${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : 'text-gray-500'}`}
  300. >
  301. {$i18n.t('No authentication')}
  302. </div>
  303. {:else if auth_type === 'session'}
  304. <div
  305. class={`text-xs self-center translate-y-[1px] ${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : 'text-gray-500'}`}
  306. >
  307. {$i18n.t('Forwards system user session credentials to authenticate')}
  308. </div>
  309. {:else if auth_type === 'oauth'}
  310. <div
  311. class={`text-xs self-center translate-y-[1px] ${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : 'text-gray-500'}`}
  312. >
  313. {$i18n.t('Forwards system user OAuth access token to authenticate')}
  314. </div>
  315. {/if}
  316. </div>
  317. </div>
  318. </div>
  319. </div>
  320. <div class="flex gap-2 mt-2">
  321. <div class="flex flex-col w-full">
  322. <label
  323. for="prefix-id-input"
  324. class={`mb-0.5 text-xs text-gray-500
  325. ${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : ''}`}
  326. >{$i18n.t('Prefix ID')}</label
  327. >
  328. <div class="flex-1">
  329. <Tooltip
  330. content={$i18n.t(
  331. 'Prefix ID is used to avoid conflicts with other connections by adding a prefix to the model IDs - leave empty to disable'
  332. )}
  333. >
  334. <input
  335. class={`w-full text-sm bg-transparent ${($settings?.highContrastMode ?? false) ? 'placeholder:text-gray-700 dark:placeholder:text-gray-100' : 'outline-hidden placeholder:text-gray-300 dark:placeholder:text-gray-700'}`}
  336. type="text"
  337. id="prefix-id-input"
  338. bind:value={prefixId}
  339. placeholder={$i18n.t('Prefix ID')}
  340. autocomplete="off"
  341. />
  342. </Tooltip>
  343. </div>
  344. </div>
  345. </div>
  346. {#if !ollama && !direct}
  347. <div class="flex flex-row justify-between items-center w-full mt-2">
  348. <label
  349. for="prefix-id-input"
  350. class={`mb-0.5 text-xs text-gray-500
  351. ${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : ''}`}
  352. >{$i18n.t('Provider Type')}</label
  353. >
  354. <div>
  355. <button
  356. on:click={() => {
  357. azure = !azure;
  358. }}
  359. type="button"
  360. class=" text-xs text-gray-700 dark:text-gray-300"
  361. >
  362. {azure ? $i18n.t('Azure OpenAI') : $i18n.t('OpenAI')}
  363. </button>
  364. </div>
  365. </div>
  366. {/if}
  367. {#if azure}
  368. <div class="flex gap-2 mt-2">
  369. <div class="flex flex-col w-full">
  370. <label
  371. for="api-version-input"
  372. class={`mb-0.5 text-xs text-gray-500
  373. ${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : ''}`}
  374. >{$i18n.t('API Version')}</label
  375. >
  376. <div class="flex-1">
  377. <input
  378. id="api-version-input"
  379. class={`w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 ${($settings?.highContrastMode ?? false) ? 'placeholder:text-gray-700 dark:placeholder:text-gray-100' : 'outline-hidden placeholder:text-gray-300 dark:placeholder:text-gray-700'}`}
  380. type="text"
  381. bind:value={apiVersion}
  382. placeholder={$i18n.t('API Version')}
  383. autocomplete="off"
  384. required
  385. />
  386. </div>
  387. </div>
  388. </div>
  389. {/if}
  390. <hr class=" border-gray-50 dark:border-gray-850 my-2.5 w-full" />
  391. <div class="flex flex-col w-full">
  392. <div class="mb-1 flex justify-between">
  393. <div
  394. class={`mb-0.5 text-xs text-gray-500
  395. ${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : ''}`}
  396. >
  397. {$i18n.t('Model IDs')}
  398. </div>
  399. </div>
  400. {#if modelIds.length > 0}
  401. <ul class="flex flex-col">
  402. {#each modelIds as modelId, modelIdx}
  403. <li class=" flex gap-2 w-full justify-between items-center">
  404. <div class=" text-sm flex-1 py-1 rounded-lg">
  405. {modelId}
  406. </div>
  407. <div class="shrink-0">
  408. <button
  409. aria-label={$i18n.t(`Remove {{MODELID}} from list.`, {
  410. MODELID: modelId
  411. })}
  412. type="button"
  413. on:click={() => {
  414. modelIds = modelIds.filter((_, idx) => idx !== modelIdx);
  415. }}
  416. >
  417. <Minus strokeWidth="2" className="size-3.5" />
  418. </button>
  419. </div>
  420. </li>
  421. {/each}
  422. </ul>
  423. {:else}
  424. <div
  425. class={`text-gray-500 text-xs text-center py-2 px-10
  426. ${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : ''}`}
  427. >
  428. {#if ollama}
  429. {$i18n.t('Leave empty to include all models from "{{url}}/api/tags" endpoint', {
  430. url: url
  431. })}
  432. {:else if azure}
  433. {$i18n.t('Deployment names are required for Azure OpenAI')}
  434. <!-- {$i18n.t('Leave empty to include all models from "{{url}}" endpoint', {
  435. url: `${url}/openai/deployments`
  436. })} -->
  437. {:else}
  438. {$i18n.t('Leave empty to include all models from "{{url}}/models" endpoint', {
  439. url: url
  440. })}
  441. {/if}
  442. </div>
  443. {/if}
  444. </div>
  445. <hr class=" border-gray-100 dark:border-gray-700/10 my-1.5 w-full" />
  446. <div class="flex items-center">
  447. <label class="sr-only" for="add-model-id-input">{$i18n.t('Add a model ID')}</label>
  448. <input
  449. class="w-full py-1 text-sm rounded-lg bg-transparent {modelId
  450. ? ''
  451. : 'text-gray-500'} {($settings?.highContrastMode ?? false)
  452. ? 'dark:placeholder:text-gray-100 placeholder:text-gray-700'
  453. : 'placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-hidden'}"
  454. bind:value={modelId}
  455. id="add-model-id-input"
  456. placeholder={$i18n.t('Add a model ID')}
  457. />
  458. <div>
  459. <button
  460. type="button"
  461. aria-label={$i18n.t('Add')}
  462. on:click={() => {
  463. addModelHandler();
  464. }}
  465. >
  466. <Plus className="size-3.5" strokeWidth="2" />
  467. </button>
  468. </div>
  469. </div>
  470. </div>
  471. <hr class=" border-gray-50 dark:border-gray-850 my-2.5 w-full" />
  472. <div class="flex gap-2">
  473. <div class="flex flex-col w-full">
  474. <div
  475. class={`mb-0.5 text-xs text-gray-500
  476. ${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : ''}`}
  477. >
  478. {$i18n.t('Tags')}
  479. </div>
  480. <div class="flex-1 mt-0.5">
  481. <Tags
  482. bind:tags
  483. on:add={(e) => {
  484. tags = [
  485. ...tags,
  486. {
  487. name: e.detail
  488. }
  489. ];
  490. }}
  491. on:delete={(e) => {
  492. tags = tags.filter((tag) => tag.name !== e.detail);
  493. }}
  494. />
  495. </div>
  496. </div>
  497. </div>
  498. <div class="flex justify-end pt-3 text-sm font-medium gap-1.5">
  499. {#if edit}
  500. <button
  501. 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"
  502. type="button"
  503. on:click={() => {
  504. onDelete();
  505. show = false;
  506. }}
  507. >
  508. {$i18n.t('Delete')}
  509. </button>
  510. {/if}
  511. <button
  512. 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
  513. ? ' cursor-not-allowed'
  514. : ''}"
  515. type="submit"
  516. disabled={loading}
  517. >
  518. {$i18n.t('Save')}
  519. {#if loading}
  520. <div class="ml-2 self-center">
  521. <Spinner />
  522. </div>
  523. {/if}
  524. </button>
  525. </div>
  526. </form>
  527. </div>
  528. </div>
  529. </div>
  530. </Modal>