'use client' import { Button } from '@/components/ui/button' import { Card, CardContent } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { useState, useEffect } from 'react' import { toast } from 'sonner' import { Loader2, CheckCircle2, XCircle, Clock, Zap, Info } from 'lucide-react' interface TestResult { success: boolean provider?: string model?: string responseTime?: number tags?: Array<{ tag: string; confidence: number }> embeddingLength?: number firstValues?: number[] error?: string details?: any } export function AI_TESTER({ type }: { type: 'tags' | 'embeddings' }) { const [isLoading, setIsLoading] = useState(false) const [result, setResult] = useState(null) const [config, setConfig] = useState(null) useEffect(() => { fetchConfig() }, []) const fetchConfig = async () => { try { const response = await fetch('/api/ai/config') const data = await response.json() setConfig(data) // Set previous result if available if (data.previousTest) { setResult(data.previousTest[type] || null) } } catch (error) { console.error('Failed to fetch config:', error) } } const runTest = async () => { setIsLoading(true) const startTime = Date.now() try { const response = await fetch(`/api/ai/test-${type}`, { method: 'POST', headers: { 'Content-Type': 'application/json' } }) const endTime = Date.now() const data = await response.json() setResult({ ...data, responseTime: endTime - startTime }) if (data.success) { toast.success( `✅ ${type === 'tags' ? 'Tags' : 'Embeddings'} Test Successful!`, { description: `Provider: ${data.provider} | Time: ${endTime - startTime}ms` } ) } else { toast.error( `❌ ${type === 'tags' ? 'Tags' : 'Embeddings'} Test Failed`, { description: data.error || 'Unknown error' } ) } } catch (error: any) { const endTime = Date.now() const errorResult = { success: false, error: error.message || 'Network error', responseTime: endTime - startTime } setResult(errorResult) toast.error(`❌ Test Error: ${error.message}`) } finally { setIsLoading(false) } } const getProviderInfo = () => { if (!config) return { provider: 'Loading...', model: 'Loading...' } if (type === 'tags') { return { provider: config.AI_PROVIDER_TAGS || 'ollama', model: config.AI_MODEL_TAGS || 'granite4:latest' } } else { return { provider: config.AI_PROVIDER_EMBEDDING || 'ollama', model: config.AI_MODEL_EMBEDDING || 'embeddinggemma:latest' } } } const providerInfo = getProviderInfo() return (
{/* Provider Info */}
Provider: {providerInfo.provider.toUpperCase()}
Model: {providerInfo.model}
{/* Test Button */} {/* Results */} {result && ( {/* Status */}
{result.success ? ( <> Test Passed ) : ( <> Test Failed )}
{/* Response Time */} {result.responseTime && (
Response time: {result.responseTime}ms
)} {/* Tags Results */} {type === 'tags' && result.success && result.tags && (
Generated Tags:
{result.tags.map((tag, idx) => ( {tag.tag} ({Math.round(tag.confidence * 100)}%) ))}
)} {/* Embeddings Results */} {type === 'embeddings' && result.success && result.embeddingLength && (
Embedding Dimensions:
{result.embeddingLength}
vector dimensions
{result.firstValues && result.firstValues.length > 0 && (
First 5 values:
[{result.firstValues.slice(0, 5).map((v, i) => v.toFixed(4)).join(', ')}]
)}
)} {/* Error Details */} {!result.success && result.error && (

Error:

{result.error}

{result.details && (
Technical details
                      {JSON.stringify(result.details, null, 2)}
                    
)}
)}
)} {/* Loading State */} {isLoading && (

Testing {type === 'tags' ? 'tags generation' : 'embeddings'}...

)}
) }