import React, { useState, useEffect } from 'react'; import { IconButton } from '~/components/ui/IconButton'; import { Switch } from '~/components/ui/Switch'; import type { ProviderInfo } from '~/types/model'; import Cookies from 'js-cookie'; interface APIKeyManagerProps { provider: ProviderInfo; apiKey: string; setApiKey: (key: string) => void; getApiKeyLink?: string; labelForGetApiKey?: string; } const apiKeyMemoizeCache: { [k: string]: Record } = {}; export function getApiKeysFromCookies() { const storedApiKeys = Cookies.get('apiKeys'); let parsedKeys = {}; if (storedApiKeys) { parsedKeys = apiKeyMemoizeCache[storedApiKeys]; if (!parsedKeys) { parsedKeys = apiKeyMemoizeCache[storedApiKeys] = JSON.parse(storedApiKeys); } } return parsedKeys; } // eslint-disable-next-line @typescript-eslint/naming-convention export const APIKeyManager: React.FC = ({ provider, apiKey, setApiKey }) => { const [isEditing, setIsEditing] = useState(false); const [tempKey, setTempKey] = useState(apiKey); const [isPromptCachingEnabled, setIsPromptCachingEnabled] = useState(() => { // Read initial state from localStorage, defaulting to true const savedState = localStorage.getItem('PROMPT_CACHING_ENABLED'); return savedState !== null ? JSON.parse(savedState) : true; }); useEffect(() => { // Update localStorage whenever the prompt caching state changes localStorage.setItem('PROMPT_CACHING_ENABLED', JSON.stringify(isPromptCachingEnabled)); }, [isPromptCachingEnabled]); const handleSave = () => { setApiKey(tempKey); setIsEditing(false); }; return (
{provider?.name} API Key: {!isEditing && (
{apiKey ? '••••••••' : 'Not set (will still work if set in .env file)'} setIsEditing(true)} title="Edit API Key">
)}
{isEditing ? (
setTempKey(e.target.value)} className="flex-1 px-2 py-1 text-xs lg:text-sm rounded border border-bolt-elements-borderColor bg-bolt-elements-prompt-background text-bolt-elements-textPrimary focus:outline-none focus:ring-2 focus:ring-bolt-elements-focus" />
setIsEditing(false)} title="Cancel">
) : ( <> {provider?.getApiKeyLink && ( window.open(provider?.getApiKeyLink)} title="Edit API Key"> {provider?.labelForGetApiKey || 'Get API Key'}
)} )}
{provider?.name === 'Anthropic' && (

When enabled, allows caching of prompts for 10x cheaper responses. Recommended for Claude models.

)}
); };