import { useState, useRef } from 'react'; import { detectProvider } from '../api'; import { Key } from './icons/Key'; import { ArrowRight } from './icons/ArrowRight'; import { Button } from './ui/button'; interface ApiKeyTesterProps { onSuccess: (provider: string, model: string, apiKey: string) => void; onStartTesting?: () => void; } interface DetectionResult { provider: string; model: string; totalModels: number; } export default function ApiKeyTester({ onSuccess, onStartTesting }: ApiKeyTesterProps) { const [apiKey, setApiKey] = useState(''); const [isLoading, setIsLoading] = useState(false); const [result, setResult] = useState(null); const [error, setError] = useState(false); const inputRef = useRef(null); const testApiKey = async () => { const actualValue = inputRef.current?.value || apiKey; if (!actualValue.trim()) { return; } onStartTesting?.(); setIsLoading(true); setResult(null); setError(false); try { const response = await detectProvider({ body: { api_key: actualValue }, throwOnError: true, }); if (response.data) { const { provider_name, models } = response.data; setResult({ provider: provider_name, model: models[0], totalModels: models.length, }); setTimeout(() => { onSuccess(provider_name, models[0], actualValue); }, 1500); } } catch { setError(true); } finally { setIsLoading(false); } }; const hasInput = apiKey.trim().length > 0; const canSubmit = hasInput && !isLoading; return (
{/* Recommended pill */}
Recommended if you have API access already

Quick Setup with API Key

Auto-detect your provider
setApiKey(e.target.value)} placeholder="Enter your API key (OpenAI, Anthropic, Google, etc.)" className="flex-1 px-3 py-2 border rounded-lg bg-background-default text-text-default placeholder-text-muted focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent" disabled={isLoading} onKeyDown={(e) => { if (e.key === 'Enter' && canSubmit) { testApiKey(); } }} />
{/* Loading state */} {isLoading && (
Detecting provider and validating key...
)} {/* Success result */} {result && (
Detected {result.provider}
Model: {result.model} ({result.totalModels} models available)
)} {/* Error result */} {error && (
Provider Detection Failed
Could not detect provider from API key

Suggestions:

  • Make sure you are using a valid API key from OpenAI, Anthropic, Google, Groq, or xAI
  • Check that the key is complete and not truncated
  • Verify your API key is active and has sufficient credits
  • For local Ollama setup, use the "Other Providers" section below
)}
); }