import React, { useState, useRef, useEffect } from 'react'; import { Zap, ArrowRightLeft, UserPlus, Search, MoreHorizontal, Clock, CheckCircle2, XCircle, Copy, Terminal, ExternalLink, Code, X, Loader2, Cpu, ShieldCheck, Key, Plus, MessageSquare, ArrowRight, RefreshCw, Eye, Trash2, Lock, Globe } from 'lucide-react'; import { AccountCollectionFlow, PaymentFlow } from '../types/index.ts'; import { callGemini } from '../services/geminiService.ts'; const BRIDGE_URL = "https://admin08077-ai-banking.static.hf.space"; interface ApiKey { id: string; key: string; name: string; scope: string; created_at: string; status: 'ACTIVE' | 'REVOKED'; } const INITIAL_ACCOUNT_FLOWS: AccountCollectionFlow[] = [ { id: 'ACF-1102', object: 'account_collection_flow', live_mode: true, created_at: '2024-03-20T10:00:00Z', updated_at: '2024-03-31T14:00:00Z', client_token: 'acf_tok_882910398291', status: 'pending', counterparty_id: 'CP-8801', external_account_id: null, payment_types: ['ach', 'wire'] } ]; const INITIAL_PAYMENT_FLOWS: PaymentFlow[] = [ { id: 'PF-4421', object: 'payment_flow', live_mode: true, created_at: '2024-03-25T12:00:00Z', updated_at: '2024-03-25T12:30:00Z', client_token: 'pf_tok_4429188', status: 'pending', amount: 500000, currency: 'USD', direction: 'debit', counterparty_id: 'CP-8801', receiving_account_id: null, originating_account_id: 'IA-4401' } ]; const Flows: React.FC = () => { const [activeTab, setActiveTab] = useState<'account' | 'payment' | 'keys'>('account'); const [accountFlows, setAccountFlows] = useState(INITIAL_ACCOUNT_FLOWS); const [paymentFlows, setPaymentFlows] = useState(INITIAL_PAYMENT_FLOWS); const [apiKeys, setApiKeys] = useState([]); const [isSdkModalOpen, setIsSdkModalOpen] = useState(false); const [isInitModalOpen, setIsInitModalOpen] = useState(false); const [selectedFlow, setSelectedFlow] = useState(null); // AI SDK Architect State const [sdkChat, setSdkChat] = useState<{ role: 'ai' | 'user', text: string }[]>([ { role: 'ai', text: `LQI Neural Architect active. I see your bridge is live at ${BRIDGE_URL}. I can generate the Node.js or Python code required to securely trigger RTP payments through your bridge. What environment are you connecting from?` } ]); const [sdkInput, setSdkInput] = useState(''); const [isAiThinking, setIsAiThinking] = useState(false); const [finalCode, setFinalCode] = useState(null); // Init Form State const [initType, setInitType] = useState<'account' | 'payment'>('account'); const [initLoading, setInitLoading] = useState(false); const [initForm, setInitForm] = useState({ counterpartyId: 'CP-8801', amount: '0', currency: 'USD' }); // API Key State const [newKeyName, setNewKeyName] = useState(''); const [generatingKey, setGeneratingKey] = useState(false); const copyToClipboard = (text: string) => { navigator.clipboard.writeText(text); }; const handleInitializeFlow = async () => { setInitLoading(true); await new Promise(r => setTimeout(r, 1500)); const id = initType === 'account' ? `ACF-${Math.floor(1000 + Math.random() * 9000)}` : `PF-${Math.floor(1000 + Math.random() * 9000)}`; const newFlow = { id, object: initType === 'account' ? 'account_collection_flow' : 'payment_flow', live_mode: true, created_at: new Date().toISOString(), updated_at: new Date().toISOString(), client_token: `${initType === 'account' ? 'acf' : 'pf'}_tok_${Math.random().toString(36).substring(7)}`, status: 'pending' as const, counterparty_id: initForm.counterpartyId, ...(initType === 'payment' ? { amount: parseInt(initForm.amount) * 100, currency: initForm.currency, direction: 'debit', receiving_account_id: null, originating_account_id: 'IA-4401' } : { external_account_id: null, payment_types: ['ach', 'wire'] }) }; if (initType === 'account') { setAccountFlows([newFlow as any, ...accountFlows]); } else { setPaymentFlows([newFlow as any, ...paymentFlows]); } setInitLoading(false); setIsInitModalOpen(false); }; const handleSdkChat = async () => { if (!sdkInput.trim() || isAiThinking) return; const userMsg = sdkInput; setSdkChat(prev => [...prev, { role: 'user', text: userMsg }]); setSdkInput(''); setIsAiThinking(true); try { const prompt = ` You are the Lumina Quantum Neural Architect. Context: The user has a Node.js bridge hosted at ${BRIDGE_URL}. The bridge endpoint is /webhooks/trigger-payment. It expects HMAC-SHA256 signatures and a body with: amount, currency, originating_account_id, receiving_account_id, reference_number. Chat History: ${JSON.stringify(sdkChat)} User Instruction: ${userMsg} Task: 1. If you need more info (e.g. language or auth secret), ask a short, sharp technical question. 2. If you have enough info, generate a full implementation snippet to talk to that bridge. Return JSON format: { "reply": "A brief technical explanation of what you are building", "code": "The full code block or null if still asking questions" } `; const response = await callGemini( 'gemini-3-flash-preview', prompt, { responseMimeType: "application/json" } ); // Fix: Directly access the .text property from the response object as per guidelines const data = JSON.parse(response.text || '{}'); setSdkChat(prev => [...prev, { role: 'ai', text: data.reply }]); if (data.code) setFinalCode(data.code); } catch (err) { setSdkChat(prev => [...prev, { role: 'ai', text: "Handshake sync failed. Please re-specify parameters." }]); } finally { setIsAiThinking(false); } }; const generateApiKey = async () => { if (!newKeyName) return; setGeneratingKey(true); await new Promise(r => setTimeout(r, 1000)); const key: ApiKey = { id: `key_${Math.random().toString(36).substring(7)}`, key: `lq_live_${Math.random().toString(36).substring(2, 15)}`, name: newKeyName, scope: 'registry.read write.payments', created_at: new Date().toISOString(), status: 'ACTIVE' }; setApiKeys([key, ...apiKeys]); setNewKeyName(''); setGeneratingKey(false); }; const revokeKey = (id: string) => { setApiKeys(apiKeys.map(k => k.id === id ? { ...k, status: 'REVOKED' } : k)); }; return (

Onboarding Flows

Institutional M2M Integration Protocols

{activeTab === 'keys' ? (

Issue Secure Access Token

setNewKeyName(e.target.value)} placeholder="External App Name" className="flex-1 bg-black border border-zinc-800 rounded-xl px-6 py-4 text-white text-xs outline-none focus:border-emerald-500/50" />
{apiKeys.map(k => (

{k.name}

{k.key}

{k.status === 'ACTIVE' && }
{k.status}
))}
) : (
{(activeTab === 'account' ? accountFlows : paymentFlows).map((flow: any) => (
{activeTab === 'account' ? : }

{flow.id}

CP_REF: {flow.counterparty_id}

))}
)}
{/* SDK Architect Modal */} {isSdkModalOpen && (

SDK Architect

Targeting: {BRIDGE_URL}

{sdkChat.map((m, i) => (
{m.text}
))} {isAiThinking && }
setSdkInput(e.target.value)} onKeyDown={e => e.key === 'Enter' && handleSdkChat()} placeholder="e.g. Write a Node.js client to trigger a payment of $500..." className="flex-1 bg-black border border-zinc-800 rounded-2xl px-6 py-4 text-white text-xs outline-none focus:border-blue-500 transition-all font-bold" />
Implementation Script
{finalCode && }
{finalCode ?
{finalCode}
: (

Architect Idle

)}
)} {/* Init Modal */} {isInitModalOpen && (

Initialize Flow

setInitForm({...initForm, counterpartyId: e.target.value})} className="w-full bg-black border border-zinc-800 rounded-xl p-4 text-white text-xs outline-none" />
{initType === 'payment' && (
setInitForm({...initForm, amount: e.target.value})} className="w-full bg-black border border-zinc-800 rounded-xl p-4 text-white text-xs outline-none" />
)}
)} {/* Detail Modal */} {selectedFlow && (
{selectedFlow.object === 'account_collection_flow' ? : }

{selectedFlow.id}

LIVE_NODE

Registry Entity

{selectedFlow.counterparty_id}

Sync Integrity

Node Synchronized

Node Handshake Token

{selectedFlow.client_token}
)}
); }; const ThinkingState = () => (
Calculating Neural Implementation...
); const StatusBadge = ({ status }: { status: string }) => { const styles: Record = { pending: 'bg-amber-500/10 text-amber-500 border border-amber-500/20', completed: 'bg-emerald-500/10 text-emerald-500 border border-emerald-500/20', cancelled: 'bg-rose-500/10 text-rose-500 border border-rose-500/20', }; return (
{status}
); }; export default Flows;