Skip to content

Commit

Permalink
fixed API Key import
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinwloring1988 committed Dec 18, 2024
1 parent 1a0ede6 commit 7458b22
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions app/components/settings/data/DataTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,27 +174,41 @@ export default function DataTab() {
try {
const apiKeys = JSON.parse(e.target?.result as string);
let importedCount = 0;
const consolidatedKeys: Record<string, string> = {};

API_KEY_PROVIDERS.forEach(provider => {
const keyName = `${provider}_API_KEY`;
if (apiKeys[keyName]) {
Cookies.set(keyName, apiKeys[keyName]);
consolidatedKeys[provider] = apiKeys[keyName];
importedCount++;
}
});

if (importedCount > 0) {
// 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]);
importedCount++;
}
});

if (importedCount > 0) {
toast.success(`Successfully imported ${importedCount} API keys/URLs`);
} else {
toast.warn('No valid API keys found in the file');
}
} 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);
Expand Down

0 comments on commit 7458b22

Please sign in to comment.