import { ReactNode, useState } from 'react'; import Button from '../ui/button/Button'; import { fetchAPI } from '../../services/api'; interface ValidationCardProps { title: string; description?: string; integrationId: string; icon?: ReactNode; } interface TestResult { success: boolean; message: string; model_used?: string; response?: string; tokens_used?: string; total_tokens?: number; cost?: string; full_response?: any; } /** * Validation Card Component * Two-way response validation testing for OpenAI API * Matches reference plugin implementation exactly */ export default function ValidationCard({ title, description, integrationId, icon, }: ValidationCardProps) { const [isLoading, setIsLoading] = useState(false); const [testResult, setTestResult] = useState(null); const [withResponse, setWithResponse] = useState(false); // Support OpenAI and Runware if (integrationId !== 'openai' && integrationId !== 'runware') { return null; } const testApiConnection = async (withResponseTest: boolean = false) => { setIsLoading(true); setWithResponse(withResponseTest); setTestResult(null); try { // Get saved settings to get API key and model const settingsData = await fetchAPI(`/v1/system/settings/integrations/${integrationId}/`); let apiKey = ''; let model = 'gpt-4.1'; if (settingsData.success && settingsData.data) { apiKey = settingsData.data.apiKey || ''; model = settingsData.data.model || 'gpt-4.1'; } if (!apiKey) { setTestResult({ success: false, message: 'API key not configured. Please configure your API key in settings first.', }); setIsLoading(false); return; } // Call test endpoint // For Runware, we don't need with_response or model config const requestBody: any = { apiKey: apiKey, }; if (integrationId === 'openai') { requestBody.config = { model: model, with_response: withResponseTest, }; } const data = await fetchAPI(`/v1/system/settings/integrations/${integrationId}/test/`, { method: 'POST', body: JSON.stringify(requestBody), }); if (data.success) { setTestResult({ success: true, message: data.message || 'API connection successful!', model_used: data.model_used || data.model, response: data.response, tokens_used: data.tokens_used, total_tokens: data.total_tokens, cost: data.cost, full_response: data.full_response || { image_url: data.image_url, provider: data.provider, size: data.size, }, }); } else { setTestResult({ success: false, message: data.error || data.message || 'API connection failed', }); } } catch (error: any) { setTestResult({ success: false, message: `API connection failed: ${error.message || 'Unknown error'}`, }); } finally { setIsLoading(false); } }; return (

{title}

{description && (

{description}

)}
{/* Test Buttons */}
{integrationId === 'openai' ? ( <> ) : ( // Runware: Single button for 128x128 image generation validation )}
{/* Test Results */} {testResult && (
{/* Success Message */} {testResult.success && (
{testResult.message}
)} {/* Error Message */} {!testResult.success && (
{testResult.message}
)} {/* Detailed Results Box */} {testResult.success && (
{integrationId === 'openai' && withResponse ? ( // OpenAI response test details <>
Model Used:{' '} {testResult.model_used || 'N/A'}
Expected:{' '} "OK! Ping Received"
Actual Response:{' '} "{testResult.response || 'N/A'}"
Token Limit Sent:{' '} N/A (from your settings)
Tokens Used:{' '} {testResult.tokens_used || 'N/A'} (input/output)
Total Tokens:{' '} {testResult.total_tokens || 'N/A'}
Cost:{' '} {testResult.cost || '$0.0000'}
) : integrationId === 'runware' ? ( // Runware image generation test details <>
Provider:{' '} Runware
Model:{' '} {testResult.model_used || 'runware:97@1'}
Image Size:{' '} 128 x 128 (test image)
Cost:{' '} {testResult.cost || '$0.0090'}
{testResult.full_response?.image_url && (
Test Image:{' '} View Image
)} ) : null}
)}
)}
); }