From 982b1fb7e205d145e940aaae5aca87b4811c9f6b Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 12 Feb 2025 00:08:51 -0800 Subject: [PATCH] wip: direct connections --- .../chat/Settings/Connections.svelte | 168 +++++++++++------- .../Settings/Connections/Connection.svelte | 23 --- 2 files changed, 108 insertions(+), 83 deletions(-) diff --git a/src/lib/components/chat/Settings/Connections.svelte b/src/lib/components/chat/Settings/Connections.svelte index 3700f90e4..4c0d54595 100644 --- a/src/lib/components/chat/Settings/Connections.svelte +++ b/src/lib/components/chat/Settings/Connections.svelte @@ -6,7 +6,7 @@ const dispatch = createEventDispatcher(); const i18n = getContext('i18n'); - import { models, user } from '$lib/stores'; + import { models, settings, user } from '$lib/stores'; import Switch from '$lib/components/common/Switch.svelte'; import Spinner from '$lib/components/common/Spinner.svelte'; @@ -16,84 +16,132 @@ import AddConnectionModal from '$lib/components/AddConnectionModal.svelte'; - const getModels = async () => { - const models = await _getModels(localStorage.token); - return models; - }; + export let saveSettings: Function; let config = null; let showConnectionModal = false; - onMount(async () => {}); + const addConnectionHandler = async (connection) => { + config.OPENAI_API_BASE_URLS.push(connection.url); + config.OPENAI_API_KEYS.push(connection.key); + config.OPENAI_API_CONFIGS[config.OPENAI_API_BASE_URLS.length - 1] = connection.config; - const addConnectionHandler = async (connection) => {}; + await updateHandler(); + }; - const submitHandler = async () => {}; - const updateHandler = async () => {}; + const updateHandler = async () => { + // Remove trailing slashes + config.OPENAI_API_BASE_URLS = config.OPENAI_API_BASE_URLS.map((url) => url.replace(/\/$/, '')); + + // Check if API KEYS length is same than API URLS length + if (config.OPENAI_API_KEYS.length !== config.OPENAI_API_BASE_URLS.length) { + // if there are more keys than urls, remove the extra keys + if (config.OPENAI_API_KEYS.length > config.OPENAI_API_BASE_URLS.length) { + config.OPENAI_API_KEYS = config.OPENAI_API_KEYS.slice( + 0, + config.OPENAI_API_BASE_URLS.length + ); + } + + // if there are more urls than keys, add empty keys + if (config.OPENAI_API_KEYS.length < config.OPENAI_API_BASE_URLS.length) { + const diff = config.OPENAI_API_BASE_URLS.length - config.OPENAI_API_KEYS.length; + for (let i = 0; i < diff; i++) { + config.OPENAI_API_KEYS.push(''); + } + } + } + + await saveSettings({ + directConnections: config + }); + }; + + const submitHandler = async () => { + await updateHandler(); + + await saveSettings({ + directConnections: config + }); + }; + + onMount(async () => { + config = $settings?.directConnections ?? { + OPENAI_API_BASE_URLS: [], + OPENAI_API_KEYS: [], + OPENAI_API_CONFIGS: {} + }; + });
-
-
-
-
-
{$i18n.t('Manage Direct Connections')}
+ {#if config !== null} +
+
+
+
+
{$i18n.t('Manage Direct Connections')}
- - - + + + +
+ +
+ {#each config?.OPENAI_API_BASE_URLS ?? [] as url, idx} + { + updateHandler(); + }} + onDelete={() => { + config.OPENAI_API_BASE_URLS = config.OPENAI_API_BASE_URLS.filter( + (url, urlIdx) => idx !== urlIdx + ); + config.OPENAI_API_KEYS = config.OPENAI_API_KEYS.filter( + (key, keyIdx) => idx !== keyIdx + ); + + let newConfig = {}; + config.OPENAI_API_BASE_URLS.forEach((url, newIdx) => { + newConfig[newIdx] = + config.OPENAI_API_CONFIGS[newIdx < idx ? newIdx : newIdx + 1]; + }); + config.OPENAI_API_CONFIGS = newConfig; + }} + /> + {/each} +
-
- {#each config?.OPENAI_API_BASE_URLS ?? [] as url, idx} - { - updateHandler(); - }} - onDelete={() => { - config.OPENAI_API_BASE_URLS = config.OPENAI_API_BASE_URLS.filter( - (url, urlIdx) => idx !== urlIdx - ); - config.OPENAI_API_KEYS = config.OPENAI_API_KEYS.filter( - (key, keyIdx) => idx !== keyIdx - ); - - let newConfig = {}; - config.OPENAI_API_BASE_URLS.forEach((url, newIdx) => { - newConfig[newIdx] = - config.OPENAI_API_CONFIGS[newIdx < idx ? newIdx : newIdx + 1]; - }); - config.OPENAI_API_CONFIGS = newConfig; - }} - /> - {/each} -
-
- -
- -
-
- {$i18n.t('Connect to your own OpenAI compatible API endpoints.')} +
+
+ {$i18n.t('Connect to your own OpenAI compatible API endpoints.')} +
-
+ {:else} +
+
+ +
+
+ {/if}
diff --git a/src/lib/components/chat/Settings/Connections/Connection.svelte b/src/lib/components/chat/Settings/Connections/Connection.svelte index 6c8475b4b..ef605876e 100644 --- a/src/lib/components/chat/Settings/Connections/Connection.svelte +++ b/src/lib/components/chat/Settings/Connections/Connection.svelte @@ -57,29 +57,6 @@ bind:value={url} autocomplete="off" /> - - {#if pipeline} -
- - - - - - - -
- {/if}