import { useState } from 'react' import { Brain, RefreshCw, Clock, ChevronDown, ChevronUp, AlertTriangle, Target, TrendingUp, TrendingDown, Shield, Zap, BookOpen, CheckCircle, XCircle, HelpCircle, BarChart2, Activity, } from 'lucide-react' import { useKnowledgeState, useKnowledgeHistory, useKnowledgeStateVersion, useKnowledgeEntries, useSynthesizeKnowledge, usePatchKbEntryStatus, } from '../hooks/useApi' // ─── Status badge ───────────────────────────────────────────────────────────── function StatusBadge({ status }: { status: string }) { const map: Record = { active: { icon: , color: 'text-emerald-400 bg-emerald-400/10 border-emerald-400/20', label: 'Validé' }, tentative: { icon: , color: 'text-amber-400 bg-amber-400/10 border-amber-400/20', label: 'Tentative' }, invalidated: { icon: , color: 'text-rose-400 bg-rose-400/10 border-rose-400/20', label: 'Invalidé' }, } const s = map[status] || map['tentative'] return ( {s.icon}{s.label} ) } // ─── Confidence bar ─────────────────────────────────────────────────────────── function ConfidenceBar({ value }: { value: number }) { const color = value >= 70 ? 'bg-emerald-500' : value >= 40 ? 'bg-amber-500' : 'bg-rose-500' return (
{value}%
) } // ─── KB Entry card ──────────────────────────────────────────────────────────── function KbEntry({ entry, onStatusChange }: { entry: any; onStatusChange: (id: number, s: string) => void }) { const [open, setOpen] = useState(false) return (
{entry.title}
{open && (

{entry.content}

{entry.tags && (
{entry.tags.split(',').filter(Boolean).map((t: string) => ( {t.trim()} ))}
)}
{['active', 'tentative', 'invalidated'].map(s => ( ))}

Vu le {entry.first_seen_at?.slice(0, 10)} · Confirmé {entry.confirmation_count}×

)}
) } // ─── Category section ───────────────────────────────────────────────────────── const CAT_META: Record = { régimes: { icon: , color: 'text-blue-400', label: 'Régimes macro' }, patterns: { icon: , color: 'text-purple-400', label: 'Patterns' }, erreurs: { icon: , color: 'text-rose-400', label: 'Erreurs récurrentes' }, corrélations: { icon: , color: 'text-emerald-400', label: 'Corrélations' }, général: { icon: , color: 'text-slate-400', label: 'Général' }, } function CategorySection({ category, entries, onStatusChange, }: { category: string; entries: any[]; onStatusChange: (id: number, s: string) => void }) { const [open, setOpen] = useState(true) const meta = CAT_META[category] || CAT_META['général'] const active = entries.filter(e => e.status !== 'invalidated').length return (
{open && (
{entries.map(e => ( ))}
)}
) } // ─── Synthesis insight list ─────────────────────────────────────────────────── function InsightList({ title, icon, items, color }: { title: string; icon: React.ReactNode; items: string[]; color: string }) { if (!items?.length) return null return (
{icon}{title}
    {items.map((item, i) => (
  • {typeof item === 'string' ? item : (item as any).mistake || JSON.stringify(item)}
  • ))}
) } // ─── Macro regime card ──────────────────────────────────────────────────────── function RegimeCard({ r }: { r: any }) { const conf = r.confidence ?? 50 const color = conf >= 70 ? 'border-emerald-500/30 bg-emerald-500/5' : conf >= 40 ? 'border-amber-500/30 bg-amber-500/5' : 'border-slate-700/50 bg-slate-800/30' return (
{r.regime} {conf}% conf · {r.trade_count ?? '?'} trades

{r.observation}

) } // ─── Main page ──────────────────────────────────────────────────────────────── export default function SuperContexte() { const [selectedHistoryId, setSelectedHistoryId] = useState(null) const [showHistory, setShowHistory] = useState(false) const { data: stateData, isLoading: stateLoading } = useKnowledgeState() const { data: histData } = useKnowledgeHistory() const { data: entriesData } = useKnowledgeEntries() const { data: versionData } = useKnowledgeStateVersion(selectedHistoryId) const synthesize = useSynthesizeKnowledge() const patchStatus = usePatchKbEntryStatus() const currentState = selectedHistoryId ? versionData?.state : stateData?.state const synthesis = currentState?.synthesis || {} const narrative = currentState?.narrative || '' const history = histData?.history || [] const byCategory = entriesData?.by_category || {} const totalEntries = entriesData?.total || 0 const handleSynthesize = () => { synthesize.mutate(undefined, { onSuccess: () => setSelectedHistoryId(null), }) } const handleStatusChange = (id: number, status: string) => { patchStatus.mutate({ id, status }) } return (
{/* Header */}

Super Contexte

Base de raisonnement évolutive — cerveau du système

{currentState && (
v{currentState.version} · {currentState.created_at?.slice(0, 16)}
{currentState.reports_used} rapports · {currentState.trades_analyzed} trades analysés
)}
{/* History sidebar */} {showHistory && (

Versions

{history.map((h: any) => ( ))}
)} {/* Main content */}
{stateLoading && (
Chargement…
)} {!currentState && !stateLoading && (

Aucune synthèse disponible

Lance la première synthèse GPT-4o pour initialiser la base de raisonnement. Le système analysera tous les rapports et trades disponibles.

)} {synthesize.isSuccess && (
Synthèse complète. {(synthesize.data as any)?.kb_entries_added ?? 0} entrées KB ajoutées ·{' '} {(synthesize.data as any)?.sources?.reports ?? 0} rapports ·{' '} {(synthesize.data as any)?.sources?.trades ?? 0} trades analysés.
)} {currentState && ( <> {/* Narrative */}
Narrative de raisonnement {selectedHistoryId && ( v{currentState.version} (archivé) )}

{narrative}

{/* Synthesis grid */}
{/* Régimes */} {synthesis.regime_insights?.length > 0 && (
Régimes macro identifiés
{synthesis.regime_insights.map((r: any, i: number) => ( ))}
)} {/* Patterns */} {synthesis.pattern_insights?.length > 0 && (
Patterns documentés
{synthesis.pattern_insights.map((p: any, i: number) => (
{p.pattern} conf {p.confidence}% · win {p.win_rate_pct}%

{p.observation}

))}
)} {/* Macro correlations */} {synthesis.macro_correlations?.length > 0 && (
Corrélations macro/géo
{synthesis.macro_correlations.map((c: any, i: number) => (
{c.trigger} {c.reliability}

{c.market_reaction}

))}
)} {/* Risk params + insights */}
} items={synthesis.strategic_priorities || []} color="text-violet-400" /> } items={synthesis.strengths || []} color="text-emerald-400" /> } items={synthesis.blind_spots || []} color="text-amber-400" /> } items={(synthesis.recurring_mistakes || []).map((m: any) => typeof m === 'string' ? m : `${m.mistake} → ${m.mitigation}` )} color="text-rose-400" /> {synthesis.risk_parameters && ( <> } items={synthesis.risk_parameters.prefer_when || []} color="text-blue-400" /> } items={synthesis.risk_parameters.avoid_when || []} color="text-rose-400" /> )}
)} {/* Knowledge Base entries */} {totalEntries > 0 && (

Base de connaissances ({totalEntries} entrées)

{Object.entries(byCategory).map(([cat, entries]: [string, any]) => ( ))}
)}
) }