AddConnectionModal.svelte 10 KB

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