Force input API keys + cache it
Browse files
app/components/chat/APIKeyManager.tsx
CHANGED
|
@@ -2,7 +2,6 @@ import React, { useState, useEffect } from 'react';
|
|
| 2 |
import { IconButton } from '~/components/ui/IconButton';
|
| 3 |
import { Switch } from '~/components/ui/Switch';
|
| 4 |
import type { ProviderInfo } from '~/types/model';
|
| 5 |
-
import Cookies from 'js-cookie';
|
| 6 |
|
| 7 |
interface APIKeyManagerProps {
|
| 8 |
provider: ProviderInfo;
|
|
@@ -12,40 +11,46 @@ interface APIKeyManagerProps {
|
|
| 12 |
labelForGetApiKey?: string;
|
| 13 |
}
|
| 14 |
|
| 15 |
-
const
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
if (storedApiKeys) {
|
| 22 |
-
parsedKeys = apiKeyMemoizeCache[storedApiKeys];
|
| 23 |
-
|
| 24 |
-
if (!parsedKeys) {
|
| 25 |
-
parsedKeys = apiKeyMemoizeCache[storedApiKeys] = JSON.parse(storedApiKeys);
|
| 26 |
-
}
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
return parsedKeys;
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
| 33 |
-
export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey, setApiKey }) => {
|
| 34 |
const [isEditing, setIsEditing] = useState(false);
|
| 35 |
-
const [tempKey, setTempKey] = useState(
|
| 36 |
const [isPromptCachingEnabled, setIsPromptCachingEnabled] = useState(() => {
|
| 37 |
-
// Read initial state from localStorage, defaulting to true
|
| 38 |
const savedState = localStorage.getItem('PROMPT_CACHING_ENABLED');
|
| 39 |
return savedState !== null ? JSON.parse(savedState) : true;
|
| 40 |
});
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
useEffect(() => {
|
| 43 |
// Update localStorage whenever the prompt caching state changes
|
| 44 |
localStorage.setItem('PROMPT_CACHING_ENABLED', JSON.stringify(isPromptCachingEnabled));
|
| 45 |
}, [isPromptCachingEnabled]);
|
| 46 |
|
| 47 |
const handleSave = () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
setApiKey(tempKey);
|
|
|
|
|
|
|
| 49 |
setIsEditing(false);
|
| 50 |
};
|
| 51 |
|
|
@@ -57,9 +62,12 @@ export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey,
|
|
| 57 |
{!isEditing && (
|
| 58 |
<div className="flex items-center">
|
| 59 |
<span className="flex-1 text-xs text-bolt-elements-textPrimary mr-2">
|
| 60 |
-
{apiKey ? '••••••••' : '
|
| 61 |
</span>
|
| 62 |
-
<IconButton onClick={() =>
|
|
|
|
|
|
|
|
|
|
| 63 |
<div className="i-ph:pencil-simple" />
|
| 64 |
</IconButton>
|
| 65 |
</div>
|
|
@@ -109,4 +117,4 @@ export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey,
|
|
| 109 |
)}
|
| 110 |
</div>
|
| 111 |
);
|
| 112 |
-
};
|
|
|
|
| 2 |
import { IconButton } from '~/components/ui/IconButton';
|
| 3 |
import { Switch } from '~/components/ui/Switch';
|
| 4 |
import type { ProviderInfo } from '~/types/model';
|
|
|
|
| 5 |
|
| 6 |
interface APIKeyManagerProps {
|
| 7 |
provider: ProviderInfo;
|
|
|
|
| 11 |
labelForGetApiKey?: string;
|
| 12 |
}
|
| 13 |
|
| 14 |
+
export const APIKeyManager: React.FC<APIKeyManagerProps> = ({
|
| 15 |
+
provider,
|
| 16 |
+
apiKey,
|
| 17 |
+
setApiKey
|
| 18 |
+
}) => {
|
| 19 |
+
// Use localStorage to store and retrieve API key for the specific provider
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
const [isEditing, setIsEditing] = useState(false);
|
| 21 |
+
const [tempKey, setTempKey] = useState('');
|
| 22 |
const [isPromptCachingEnabled, setIsPromptCachingEnabled] = useState(() => {
|
|
|
|
| 23 |
const savedState = localStorage.getItem('PROMPT_CACHING_ENABLED');
|
| 24 |
return savedState !== null ? JSON.parse(savedState) : true;
|
| 25 |
});
|
| 26 |
|
| 27 |
+
// Load stored API key for this provider on component mount
|
| 28 |
+
useEffect(() => {
|
| 29 |
+
const storedKey = localStorage.getItem(`API_KEY_${provider.name}`);
|
| 30 |
+
if (storedKey) {
|
| 31 |
+
setApiKey(storedKey);
|
| 32 |
+
}
|
| 33 |
+
}, [provider.name]);
|
| 34 |
+
|
| 35 |
useEffect(() => {
|
| 36 |
// Update localStorage whenever the prompt caching state changes
|
| 37 |
localStorage.setItem('PROMPT_CACHING_ENABLED', JSON.stringify(isPromptCachingEnabled));
|
| 38 |
}, [isPromptCachingEnabled]);
|
| 39 |
|
| 40 |
const handleSave = () => {
|
| 41 |
+
if (!tempKey.trim()) {
|
| 42 |
+
// Prevent saving empty API key
|
| 43 |
+
alert(`Please enter a valid API key for ${provider.name}`);
|
| 44 |
+
return;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// Save to localStorage
|
| 48 |
+
localStorage.setItem(`API_KEY_${provider.name}`, tempKey);
|
| 49 |
+
|
| 50 |
+
// Update the API key in parent component
|
| 51 |
setApiKey(tempKey);
|
| 52 |
+
|
| 53 |
+
// Exit editing mode
|
| 54 |
setIsEditing(false);
|
| 55 |
};
|
| 56 |
|
|
|
|
| 62 |
{!isEditing && (
|
| 63 |
<div className="flex items-center">
|
| 64 |
<span className="flex-1 text-xs text-bolt-elements-textPrimary mr-2">
|
| 65 |
+
{apiKey ? '••••••••' : 'API Key Required'}
|
| 66 |
</span>
|
| 67 |
+
<IconButton onClick={() => {
|
| 68 |
+
setTempKey(apiKey || '');
|
| 69 |
+
setIsEditing(true);
|
| 70 |
+
}} title="Edit API Key">
|
| 71 |
<div className="i-ph:pencil-simple" />
|
| 72 |
</IconButton>
|
| 73 |
</div>
|
|
|
|
| 117 |
)}
|
| 118 |
</div>
|
| 119 |
);
|
| 120 |
+
};
|