AddConnectionModal.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. export let onSubmit: Function = () => {};
  17. export let onDelete: Function = () => {};
  18. export let show = false;
  19. export let edit = false;
  20. export let ollama = false;
  21. export let direct = false;
  22. export let connection = null;
  23. let url = '';
  24. let key = '';
  25. let prefixId = '';
  26. let enable = true;
  27. let tags = [];
  28. let modelId = '';
  29. let modelIds = [];
  30. let loading = false;
  31. const verifyOllamaHandler = async () => {
  32. const res = await verifyOllamaConnection(localStorage.token, url, key).catch((error) => {
  33. toast.error(`${error}`);
  34. });
  35. if (res) {
  36. toast.success($i18n.t('Server connection verified'));
  37. }
  38. };
  39. const verifyOpenAIHandler = async () => {
  40. const res = await verifyOpenAIConnection(localStorage.token, url, key, direct).catch(
  41. (error) => {
  42. toast.error(`${error}`);
  43. }
  44. );
  45. if (res) {
  46. toast.success($i18n.t('Server connection verified'));
  47. }
  48. };
  49. const verifyHandler = () => {
  50. if (ollama) {
  51. verifyOllamaHandler();
  52. } else {
  53. verifyOpenAIHandler();
  54. }
  55. };
  56. const addModelHandler = () => {
  57. if (modelId) {
  58. modelIds = [...modelIds, modelId];
  59. modelId = '';
  60. }
  61. };
  62. const submitHandler = async () => {
  63. loading = true;
  64. if (!ollama && !url) {
  65. loading = false;
  66. toast.error('URL is required');
  67. return;
  68. }
  69. // remove trailing slash from url
  70. url = url.replace(/\/$/, '');
  71. const connection = {
  72. url,
  73. key,
  74. config: {
  75. enable: enable,
  76. tags: tags,
  77. prefix_id: prefixId,
  78. model_ids: modelIds
  79. }
  80. };
  81. await onSubmit(connection);
  82. loading = false;
  83. show = false;
  84. url = '';
  85. key = '';
  86. prefixId = '';
  87. tags = [];
  88. modelIds = [];
  89. };
  90. const init = () => {
  91. if (connection) {
  92. url = connection.url;
  93. key = connection.key;
  94. enable = connection.config?.enable ?? true;
  95. tags = connection.config?.tags ?? [];
  96. prefixId = connection.config?.prefix_id ?? '';
  97. modelIds = connection.config?.model_ids ?? [];
  98. }
  99. };
  100. $: if (show) {
  101. init();
  102. }
  103. onMount(() => {
  104. init();
  105. });
  106. </script>
  107. <Modal size="sm" bind:show>
  108. <div>
  109. <div class=" flex justify-between dark:text-gray-100 px-5 pt-4 pb-2">
  110. <div class=" text-lg font-medium self-center font-primary">
  111. {#if edit}
  112. {$i18n.t('Edit Connection')}
  113. {:else}
  114. {$i18n.t('Add Connection')}
  115. {/if}
  116. </div>
  117. <button
  118. class="self-center"
  119. on:click={() => {
  120. show = false;
  121. }}
  122. >
  123. <svg
  124. xmlns="http://www.w3.org/2000/svg"
  125. viewBox="0 0 20 20"
  126. fill="currentColor"
  127. class="w-5 h-5"
  128. >
  129. <path
  130. 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"
  131. />
  132. </svg>
  133. </button>
  134. </div>
  135. <div class="flex flex-col md:flex-row w-full px-4 pb-4 md:space-x-4 dark:text-gray-200">
  136. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  137. <form
  138. class="flex flex-col w-full"
  139. on:submit={(e) => {
  140. e.preventDefault();
  141. submitHandler();
  142. }}
  143. >
  144. <div class="px-1">
  145. <div class="flex gap-2">
  146. <div class="flex flex-col w-full">
  147. <div class=" mb-0.5 text-xs text-gray-500">{$i18n.t('URL')}</div>
  148. <div class="flex-1">
  149. <input
  150. class="w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-hidden"
  151. type="text"
  152. bind:value={url}
  153. placeholder={$i18n.t('API Base URL')}
  154. autocomplete="off"
  155. required
  156. />
  157. </div>
  158. </div>
  159. <Tooltip content={$i18n.t('Verify Connection')} className="self-end -mb-1">
  160. <button
  161. class="self-center p-1 bg-transparent hover:bg-gray-100 dark:bg-gray-900 dark:hover:bg-gray-850 rounded-lg transition"
  162. on:click={() => {
  163. verifyHandler();
  164. }}
  165. type="button"
  166. >
  167. <svg
  168. xmlns="http://www.w3.org/2000/svg"
  169. viewBox="0 0 20 20"
  170. fill="currentColor"
  171. class="w-4 h-4"
  172. >
  173. <path
  174. fill-rule="evenodd"
  175. 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"
  176. clip-rule="evenodd"
  177. />
  178. </svg>
  179. </button>
  180. </Tooltip>
  181. <div class="flex flex-col shrink-0 self-end">
  182. <Tooltip content={enable ? $i18n.t('Enabled') : $i18n.t('Disabled')}>
  183. <Switch bind:state={enable} />
  184. </Tooltip>
  185. </div>
  186. </div>
  187. <div class="flex gap-2 mt-2">
  188. <div class="flex flex-col w-full">
  189. <div class=" mb-0.5 text-xs text-gray-500">{$i18n.t('Key')}</div>
  190. <div class="flex-1">
  191. <SensitiveInput
  192. className="w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-hidden"
  193. bind:value={key}
  194. placeholder={$i18n.t('API Key')}
  195. required={false}
  196. />
  197. </div>
  198. </div>
  199. <div class="flex flex-col w-full">
  200. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Prefix ID')}</div>
  201. <div class="flex-1">
  202. <Tooltip
  203. content={$i18n.t(
  204. 'Prefix ID is used to avoid conflicts with other connections by adding a prefix to the model IDs - leave empty to disable'
  205. )}
  206. >
  207. <input
  208. class="w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-hidden"
  209. type="text"
  210. bind:value={prefixId}
  211. placeholder={$i18n.t('Prefix ID')}
  212. autocomplete="off"
  213. />
  214. </Tooltip>
  215. </div>
  216. </div>
  217. </div>
  218. <div class="flex gap-2 mt-2">
  219. <div class="flex flex-col w-full">
  220. <div class=" mb-1.5 text-xs text-gray-500">{$i18n.t('Tags')}</div>
  221. <div class="flex-1">
  222. <Tags
  223. bind:tags
  224. on:add={(e) => {
  225. tags = [
  226. ...tags,
  227. {
  228. name: e.detail
  229. }
  230. ];
  231. }}
  232. on:delete={(e) => {
  233. tags = tags.filter((tag) => tag.name !== e.detail);
  234. }}
  235. />
  236. </div>
  237. </div>
  238. </div>
  239. <hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" />
  240. <div class="flex flex-col w-full">
  241. <div class="mb-1 flex justify-between">
  242. <div class="text-xs text-gray-500">{$i18n.t('Model IDs')}</div>
  243. </div>
  244. {#if modelIds.length > 0}
  245. <div class="flex flex-col">
  246. {#each modelIds as modelId, modelIdx}
  247. <div class=" flex gap-2 w-full justify-between items-center">
  248. <div class=" text-sm flex-1 py-1 rounded-lg">
  249. {modelId}
  250. </div>
  251. <div class="shrink-0">
  252. <button
  253. type="button"
  254. on:click={() => {
  255. modelIds = modelIds.filter((_, idx) => idx !== modelIdx);
  256. }}
  257. >
  258. <Minus strokeWidth="2" className="size-3.5" />
  259. </button>
  260. </div>
  261. </div>
  262. {/each}
  263. </div>
  264. {:else}
  265. <div class="text-gray-500 text-xs text-center py-2 px-10">
  266. {#if ollama}
  267. {$i18n.t('Leave empty to include all models from "{{url}}/api/tags" endpoint', {
  268. url: url
  269. })}
  270. {:else}
  271. {$i18n.t('Leave empty to include all models from "{{url}}/models" endpoint', {
  272. url: url
  273. })}
  274. {/if}
  275. </div>
  276. {/if}
  277. </div>
  278. <hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" />
  279. <div class="flex items-center">
  280. <input
  281. class="w-full py-1 text-sm rounded-lg bg-transparent {modelId
  282. ? ''
  283. : 'text-gray-500'} placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-hidden"
  284. bind:value={modelId}
  285. placeholder={$i18n.t('Add a model ID')}
  286. />
  287. <div>
  288. <button
  289. type="button"
  290. on:click={() => {
  291. addModelHandler();
  292. }}
  293. >
  294. <Plus className="size-3.5" strokeWidth="2" />
  295. </button>
  296. </div>
  297. </div>
  298. </div>
  299. <div class="flex justify-end pt-3 text-sm font-medium gap-1.5">
  300. {#if edit}
  301. <button
  302. 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"
  303. type="button"
  304. on:click={() => {
  305. onDelete();
  306. show = false;
  307. }}
  308. >
  309. {$i18n.t('Delete')}
  310. </button>
  311. {/if}
  312. <button
  313. 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
  314. ? ' cursor-not-allowed'
  315. : ''}"
  316. type="submit"
  317. disabled={loading}
  318. >
  319. {$i18n.t('Save')}
  320. {#if loading}
  321. <div class="ml-2 self-center">
  322. <svg
  323. class=" w-4 h-4"
  324. viewBox="0 0 24 24"
  325. fill="currentColor"
  326. xmlns="http://www.w3.org/2000/svg"
  327. ><style>
  328. .spinner_ajPY {
  329. transform-origin: center;
  330. animation: spinner_AtaB 0.75s infinite linear;
  331. }
  332. @keyframes spinner_AtaB {
  333. 100% {
  334. transform: rotate(360deg);
  335. }
  336. }
  337. </style><path
  338. 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"
  339. opacity=".25"
  340. /><path
  341. 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"
  342. class="spinner_ajPY"
  343. /></svg
  344. >
  345. </div>
  346. {/if}
  347. </button>
  348. </div>
  349. </form>
  350. </div>
  351. </div>
  352. </div>
  353. </Modal>