import React from 'react'; import { Cpu, Key, Shield, Save, Loader2, ExternalLink, AlertCircle, ChevronDown, Info, MousePointer2, Zap, CheckCircle2 } from 'lucide-react'; import { GhostButton, ModernCardHeader, SettingInput } from "../ui/settings-ui"; import { Badge } from "../ui/badge"; import { Switch } from "../ui/switch"; import { cn } from "@/lib/utils"; import { motion } from 'framer-motion'; import type { WawpDashboardData } from '../../types'; interface AIProvider { id: string; name: string; description: string; logo: string; models: string[]; link: string; } export default function AISettings({ data, onBack }: { data: WawpDashboardData; onBack: () => void }) { const [settings, setSettings] = React.useState>({ freeModeEnabled: true }); const [isSaving, setIsSaving] = React.useState(false); const [isLoading, setIsLoading] = React.useState(true); const [status, setStatus] = React.useState<'idle' | 'success' | 'error'>('idle'); const [isTesting, setIsTesting] = React.useState>({}); const [testResults, setTestResults] = React.useState>({}); const assetsUrl = `${data.global.pluginUrl}assets/icons/ai/`; const PROVIDERS: AIProvider[] = [ { id: 'openai', name: 'OpenAI', description: 'Leader in Agentic AI and self-managing workflows with GPT-5.5.', logo: `${assetsUrl}openai.svg`, models: ['gpt-5.5-preview', 'gpt-5.4-turbo', 'gpt-4o', 'o1-preview'], link: 'https://platform.openai.com/' }, { id: 'anthropic', name: 'Anthropic', description: 'Expert coding with Claude 4.7 and advanced Effort Control logic.', logo: `${assetsUrl}anthropic.svg`, models: ['claude-4-7-opus', 'claude-4-6-sonnet', 'claude-3-5-sonnet-latest'], link: 'https://console.anthropic.com/' }, { id: 'google', name: 'Google DeepMind', description: 'Massive 2M context and Personal Intelligence with Gemini 3.5.', logo: `${assetsUrl}google.svg`, models: ['gemini-3-5-flash', 'gemini-3-1-pro', 'gemini-1.5-pro'], link: 'https://aistudio.google.com/' }, { id: 'groq', name: 'Groq (LPU Speed)', description: 'The world\'s fastest inference for Llama 4 and DeepSeek V4.', logo: `${assetsUrl}groq.svg`, models: ['llama-4-70b-fast', 'deepseek-v4-instant', 'llama-3.1-70b-versatile'], link: 'https://console.groq.com/' }, { id: 'deepseek', name: 'DeepSeek', description: 'Open-weight V4 Pro with Extended Thinking Mode at record low cost.', logo: `${assetsUrl}deepseek.svg`, models: ['deepseek-v4-pro', 'deepseek-v4-flash', 'deepseek-chat'], link: 'https://platform.deepseek.com/' } ]; const RECOMMENDED_MODELS = [ { provider: 'openai', model: 'gpt-5.5-preview', label: 'Agentic Standard', color: 'blue' }, { provider: 'groq', model: 'llama-4-70b-fast', label: 'LPU Lightning Speed', color: 'emerald' }, { provider: 'anthropic', model: 'claude-4-7-opus', label: 'High Effort Reasoning', color: 'orange' }, ]; React.useEffect(() => { const fetchSettings = async () => { try { const response = await fetch(`${data.global.restUrl}/ai-assistant/settings`, { headers: { 'X-WP-Nonce': data.global.wpRestNonce } }); const res = await response.json(); if (res.success && res.data) { setSettings(prev => ({ ...prev, ...res.data, freeModeEnabled: res.data.freeModeEnabled !== undefined ? Boolean(res.data.freeModeEnabled) : true })); } } catch (err) { console.error("Failed to fetch AI settings", err); } finally { setIsLoading(false); } }; fetchSettings(); }, [data.global.restUrl, data.global.wpRestNonce]); const handleSave = async (overrideSettings?: Record) => { const dataToSave = overrideSettings || settings; setIsSaving(true); try { const response = await fetch(`${data.global.restUrl}/ai-assistant/settings`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': data.global.wpRestNonce }, body: JSON.stringify(dataToSave) }); const res = await response.json(); if (res.success) { setStatus('success'); setTimeout(() => setStatus('idle'), 3000); } else { throw new Error(res.message); } } catch (e: unknown) { console.error("Failed to save AI settings", e); setStatus('error'); } finally { setIsSaving(false); } }; const quickSetup = (providerId: string, modelName: string) => { const next = { ...settings, [`${providerId}_enabled`]: true, [`${providerId}_model`]: modelName }; setSettings(next); handleSave(next); }; const handleTestConnection = async (providerId: string) => { setIsTesting(prev => ({ ...prev, [providerId]: true })); setTestResults(prev => ({ ...prev, [providerId]: null })); const kVal = settings[`${providerId}_key`]; const mVal = settings[`${providerId}_model`] || PROVIDERS.find(p => p.id === providerId)?.models[0]; try { const response = await fetch(`${data.global.restUrl}/ai-assistant/chat-proxy`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': data.global.wpRestNonce }, body: JSON.stringify({ provider: providerId, key: typeof kVal === 'string' ? kVal : '', model: typeof mVal === 'string' ? mVal : '', messages: [{ role: 'user', content: 'Ping' }] }) }); const res = await response.json(); if (res.success) { setTestResults(prev => ({ ...prev, [providerId]: { success: true, message: 'Connection successful!' } })); } else { setTestResults(prev => ({ ...prev, [providerId]: { success: false, message: res.message || 'Connection failed' } })); } } catch (e: unknown) { const message = e instanceof Error ? e.message : 'Connection failed'; setTestResults(prev => ({ ...prev, [providerId]: { success: false, message } })); } finally { setIsTesting(prev => ({ ...prev, [providerId]: false })); } }; const isUsingAnyPaid = PROVIDERS.some(p => settings[`${p.id}_enabled`] && settings[`${p.id}_key`]); if (isLoading) { return (

Initializing AI Center...

); } return (
Exit to Chat } />
{/* Free Mode Active Alert */} {!isUsingAnyPaid && (
Legacy Free Mode Active LIMITED
} description="You are currently using shared resources. For GPT-5.5 performance and extended reasoning, connect a premium provider." icon={() => (
)} className="!bg-transparent !border-none !py-5" rightAction={
FREE MODE { const next = { ...settings, freeModeEnabled: val }; setSettings(next); handleSave(next); }} />
} />
{RECOMMENDED_MODELS.map(m => ( ))}
)}

AI INFRASTRUCTURE

{PROVIDERS.length} ELITE PROVIDERS
{PROVIDERS.map(provider => (
(
{provider.name}
)} className="!bg-transparent !border-slate-50/50 !py-5" rightAction={
{ setSettings({ ...settings, [`${provider.id}_enabled`]: val }); }} />
} /> {settings[`${provider.id}_enabled`] && ( {(() => { const kVal = settings[`${provider.id}_key`]; return ( { setSettings({ ...settings, [`${provider.id}_key`]: val }); }} className="!bg-transparent" /> ); })()}
{(() => { const mVal = settings[`${provider.id}_model`]; return ( ); })()}
Test connection with the provided credentials. {testResults[provider.id] && ( {testResults[provider.id]?.success ? : } {testResults[provider.id]?.message} )}
)}
))}
{/* Security Protocol Section */}
(
)} className="!bg-transparent !border-none !py-5" />
{status === 'success' && (
Engine Synced )} {status === 'error' && ( Sync Failure )}
Cancel
); }