From 7458b227802095282acb1094e17a1a7ea69002b5 Mon Sep 17 00:00:00 2001 From: Dustin Loring Date: Wed, 18 Dec 2024 09:06:58 -0500 Subject: [PATCH] fixed API Key import --- app/components/settings/data/DataTab.tsx | 32 +++++++++++++++++------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/app/components/settings/data/DataTab.tsx b/app/components/settings/data/DataTab.tsx index b34d409..756abaa 100644 --- a/app/components/settings/data/DataTab.tsx +++ b/app/components/settings/data/DataTab.tsx @@ -174,27 +174,41 @@ export default function DataTab() { try { const apiKeys = JSON.parse(e.target?.result as string); let importedCount = 0; + const consolidatedKeys: Record = {}; API_KEY_PROVIDERS.forEach(provider => { const keyName = `${provider}_API_KEY`; if (apiKeys[keyName]) { - Cookies.set(keyName, apiKeys[keyName]); - importedCount++; - } - }); - - ['OPENAI_LIKE_API_BASE_URL', 'LMSTUDIO_API_BASE_URL', 'OLLAMA_API_BASE_URL', 'TOGETHER_API_BASE_URL'].forEach(baseUrl => { - if (apiKeys[baseUrl]) { - Cookies.set(baseUrl, apiKeys[baseUrl]); + consolidatedKeys[provider] = apiKeys[keyName]; importedCount++; } }); if (importedCount > 0) { - toast.success(`Successfully imported ${importedCount} API keys/URLs`); + // Store all API keys in a single cookie as JSON + Cookies.set('apiKeys', JSON.stringify(consolidatedKeys)); + + // Also set individual cookies for backward compatibility + Object.entries(consolidatedKeys).forEach(([provider, key]) => { + Cookies.set(`${provider}_API_KEY`, key); + }); + + toast.success(`Successfully imported ${importedCount} API keys/URLs. Refreshing page to apply changes...`); + // Reload the page after a short delay to allow the toast to be seen + setTimeout(() => { + window.location.reload(); + }, 1500); } else { toast.warn('No valid API keys found in the file'); } + + // Set base URLs if they exist + ['OPENAI_LIKE_API_BASE_URL', 'LMSTUDIO_API_BASE_URL', 'OLLAMA_API_BASE_URL', 'TOGETHER_API_BASE_URL'].forEach(baseUrl => { + if (apiKeys[baseUrl]) { + Cookies.set(baseUrl, apiKeys[baseUrl]); + } + }); + } catch (error) { toast.error('Failed to import API keys. Make sure the file is a valid JSON file.'); console.error('Failed to import API keys:', error);