"use client"; /** * JobStatusPoller — client island for /jobs/[id]. * * Polls /api/jobs/[id] every 3s while job.status is non-terminal. Renders: * - Phase tracker bar (queued → claimed → running → complete | failed) * - Hypothesis cards (prior/posterior bars + Tetlock confidence_band) * - Evidence cards (grade A/B/C + verbatim excerpt + bbox preview) * - Error panel when status='failed' */ import { useEffect, useState } from "react"; import Link from "next/link"; interface JobPayloadOutput { evidence_id?: string; hypothesis_id?: string; case_file?: string; chunk_id?: string; error?: string; skipped?: boolean; reason?: string; kind?: string; } interface InitialJob { job_id: string; kind: string; payload: Record | null; status: string; worker_id: string | null; started_at: string | null; finished_at: string | null; outputs: JobPayloadOutput[]; error: string | null; created_at: string; } interface EvidenceItem { evidence_id: string; grade: string | null; source_page_id: string; doc_id: string | null; page: number | null; chunk_id: string | null; verbatim_excerpt: string | null; custody_steps: Array> | null; bbox: { x: number; y: number; w: number; h: number } | null; confidence_band: string | null; } interface HypothesisItem { hypothesis_id: string; question: string | null; question_pt_br?: string | null; position: string | null; position_pt_br?: string | null; argument_for: string | null; argument_for_pt_br?: string | null; argument_against: string | null; argument_against_pt_br?: string | null; prior: number | string | null; posterior: number | string | null; confidence_band: string | null; status: string | null; } interface ContradictionPositionItem { doc_id: string; chunk_id: string; page: number; statement: string; statement_pt_br?: string | null; stance?: string | null; } interface ContradictionItem { contradiction_id: string; topic: string; topic_pt_br?: string | null; chunks: ContradictionPositionItem[]; resolution_status: string | null; notes: string | null; notes_pt_br?: string | null; detected_by: string | null; } interface WitnessCorrItem { chunk_pk?: number; doc_id: string; chunk_id: string; page: number; supports: boolean; } interface WitnessItem { witness_id: string; canonical_name: string | null; entity_id: string | null; credibility: string | null; access_to_event: string | null; access_to_event_pt_br?: string | null; bias_notes: string | null; bias_notes_pt_br?: string | null; corroboration_refs: WitnessCorrItem[]; verdict: string | null; verdict_pt_br?: string | null; } interface CaseReportOutput { kind: "case_report"; case_file?: string; slug?: string; skipped?: boolean; reason?: string; } interface GapItem { gap_id: string; description: string; description_pt_br?: string | null; scope: { kind?: string; title?: string; title_pt_br?: string; doc_id?: string; chunk_id?: string; page?: number; dominant_model?: string; dominant_model_pt_br?: string; why_surprising?: string; why_surprising_pt_br?: string; what_it_implies?: string; what_it_implies_pt_br?: string; } | null; suggested_next_move: string | null; suggested_next_move_pt_br?: string | null; status: string; created_by: string; } interface FetchedJob extends InitialJob { evidence: EvidenceItem[]; hypotheses: HypothesisItem[]; contradictions: ContradictionItem[]; witnesses: WitnessItem[]; gaps: GapItem[]; duration_ms: number | null; } const BAND_COLOR: Record = { high: "text-[#06d6a0] border-[#06d6a0]", medium: "text-[#3fde6a] border-[#3fde6a]", low: "text-[#ffa500] border-[#ffa500]", speculation: "text-[#ff6ec7] border-[#ff6ec7]", }; const GRADE_COLOR: Record = { A: "text-[#06d6a0] border-[#06d6a0]", B: "text-[#3fde6a] border-[#3fde6a]", C: "text-[#ffa500] border-[#ffa500]", }; const STATUS_LABEL: Record = { queued: "Aguardando worker", running: "Investigação em curso", complete: "Concluído", failed: "Falhou", }; const PHASES = ["queued", "running", "complete"] as const; function isTerminal(status: string): boolean { return status === "complete" || status === "failed"; } function asNumber(n: number | string | null): number | null { if (n === null || n === undefined) return null; const v = typeof n === "string" ? parseFloat(n) : n; return Number.isFinite(v) ? v : null; } function formatDuration(ms: number | null): string { if (!ms || ms < 0) return "—"; if (ms < 1000) return `${ms} ms`; if (ms < 60_000) return `${(ms / 1000).toFixed(1)} s`; return `${Math.floor(ms / 60_000)} min ${Math.floor((ms % 60_000) / 1000)} s`; } export function JobStatusPoller(props: { jobId: string; initialJob: InitialJob }) { const [job, setJob] = useState({ ...props.initialJob, evidence: [], hypotheses: [], contradictions: [], witnesses: [], gaps: [], duration_ms: null, }); const [error, setError] = useState(null); useEffect(() => { let cancelled = false; let timer: ReturnType | null = null; async function tick() { try { const r = await fetch(`/api/jobs/${props.jobId}`, { cache: "no-store" }); if (!r.ok) { if (!cancelled) setError(`HTTP ${r.status}`); } else { const data = (await r.json()) as FetchedJob; if (!cancelled) { setJob(data); setError(null); } } } catch (e) { if (!cancelled) setError((e as Error).message); } if (!cancelled) { const next = isTerminal(job.status) ? null : setTimeout(tick, 3000); timer = next; } } if (!isTerminal(job.status)) { timer = setTimeout(tick, 1500); } else { // Even when terminal, do a single hydrate to fetch evidence/hypotheses tick(); } return () => { cancelled = true; if (timer) clearTimeout(timer); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [props.jobId, job.status]); const currentPhaseIdx = (() => { if (job.status === "complete") return 2; if (job.status === "failed") return -1; if (job.status === "running") return 1; return 0; })(); return (
{/* Phase tracker */}
Status
{STATUS_LABEL[job.status] ?? job.status} {job.duration_ms !== null && ( · {formatDuration(job.duration_ms)} )}
{PHASES.map((phase, i) => { const done = i <= currentPhaseIdx && job.status !== "failed"; const active = i === currentPhaseIdx && !isTerminal(job.status); return (
{i < PHASES.length - 1 && }
); })}
{PHASES.map((p) => {p})}
{job.worker_id && (
worker: {job.worker_id}
)} {error && (
polling error: {error}
)}
{/* Failure panel */} {job.status === "failed" && (
Job failed
{job.error || "(no error message)"}
          
{job.outputs.length > 0 && (
{job.outputs.length} partial output(s)
{JSON.stringify(job.outputs, null, 2)}
              
)}
)} {/* Hypothesis cards */} {job.hypotheses.length > 0 && (
Hipóteses rivais ({job.hypotheses.length})
{job.hypotheses.map((h) => )}
)} {/* Case-report link card */} {job.outputs.filter((o): o is CaseReportOutput => (o as JobPayloadOutput).kind === "case_report" && typeof (o as CaseReportOutput).slug === "string" ).map((o) => (
Case report ready
/c/{o.slug}
Open the Watson narrative →
))} {/* Outlier / gap cards */} {job.gaps.length > 0 && (
Outliers ({job.gaps.length})
{job.gaps.map((g) => )}
)} {/* Witness cards */} {job.witnesses.length > 0 && (
Análise de testemunha ({job.witnesses.length})
{job.witnesses.map((w) => )}
)} {/* Contradiction cards */} {job.contradictions.length > 0 && (
Contradições detectadas ({job.contradictions.length})
{job.contradictions.map((c) => )}
)} {/* Evidence cards */} {job.evidence.length > 0 && (
Cadeia de evidência ({job.evidence.length})
{job.evidence.map((e) => )}
)} {/* Empty / in-flight state */} {!isTerminal(job.status) && job.hypotheses.length === 0 && job.evidence.length === 0 && job.contradictions.length === 0 && job.witnesses.length === 0 && job.gaps.length === 0 && (
🔎 Os detetives estão lendo o corpus…
Holmes constrói hipóteses rivais com priors + posteriors em ~60 s.
Dupin localiza pares de chunks em tensão irreconciliável em ~60 s.
Locard documenta evidências verbatim com cadeia de custódia em ~30 s por chunk.
)} {/* Outputs raw */} {job.outputs.length > 0 && job.status === "complete" && (
Raw audit outputs ({job.outputs.length})
{JSON.stringify(job.outputs, null, 2)}
          
)}
); } function HypothesisCard({ h }: { h: HypothesisItem }) { const prior = asNumber(h.prior); const posterior = asNumber(h.posterior); const delta = prior !== null && posterior !== null ? posterior - prior : null; const bandTone = (h.confidence_band && BAND_COLOR[h.confidence_band]) || "text-[#9aa6b8] border-[#9aa6b8]"; return (
{h.hypothesis_id}
{h.confidence_band && ( {h.confidence_band} )}
{h.position_pt_br || h.position}
{h.position_pt_br && h.position && h.position_pt_br !== h.position && (
{h.position}
)} {(prior !== null || posterior !== null) && (
)} {delta !== null && (
Δ {delta >= 0 ? "+" : ""}{delta.toFixed(3)} ·{" "} {delta > 0.05 ? evidência reforçou : delta < -0.05 ? evidência reduziu : evidência ambígua}
)} {(h.argument_for_pt_br || h.argument_for) && (
Argumento a favor (PT-BR)
{h.argument_for && h.argument_for_pt_br && (
Argument for (EN)
)}
)} {(h.argument_against_pt_br || h.argument_against) && (
Argumento contra (PT-BR)
{h.argument_against && h.argument_against_pt_br && (
Argument against (EN)
)}
)}
); } function EvidenceCard({ e }: { e: EvidenceItem }) { const gradeTone = (e.grade && GRADE_COLOR[e.grade]) || "text-[#9aa6b8] border-[#9aa6b8]"; const bandTone = (e.confidence_band && BAND_COLOR[e.confidence_band]) || "text-[#9aa6b8] border-[#9aa6b8]"; const stepsCount = Array.isArray(e.custody_steps) ? e.custody_steps.length : 0; const cropUrl = e.doc_id && e.page && e.bbox && e.bbox.w > 0 && e.bbox.h > 0 ? `/api/crop?doc=${encodeURIComponent(e.doc_id)}&page=${e.page}&x=${e.bbox.x}&y=${e.bbox.y}&w=${e.bbox.w}&h=${e.bbox.h}&w_px=480` : null; return (
{e.evidence_id}
{e.grade && ( Grade {e.grade} )} {e.confidence_band && ( {e.confidence_band} )}
{e.verbatim_excerpt && (
“{e.verbatim_excerpt}”
)} {cropUrl && ( // eslint-disable-next-line @next/next/no-img-element {`${e.doc_id} )}
{e.doc_id && e.page && ( {e.doc_id}/p{String(e.page).padStart(3, "0")}{e.chunk_id ? `#${e.chunk_id}` : ""} )} · {stepsCount} custody step{stepsCount === 1 ? "" : "s"}
{stepsCount > 0 && (
Cadeia de custódia
    {(e.custody_steps as Array>).map((s, i) => (
  1. {String(s.actor ?? "?")} {s.action ? ` — ${String(s.action)}` : ""} {s.timestamp ? ` (${String(s.timestamp)})` : ""}
  2. ))}
)}
); } function GapCard({ g }: { g: GapItem }) { const s = g.scope ?? {}; const isOutlier = s.kind === "outlier"; const pageStr = s.page ? String(s.page).padStart(3, "0") : null; return (
{g.gap_id}{isOutlier && " · outlier"}
{g.status}
{s.title_pt_br || s.title || g.description_pt_br || g.description}
{s.doc_id && s.chunk_id && pageStr && (
Source:{" "} {s.doc_id}/p{pageStr}#{s.chunk_id}
)} {(s.dominant_model_pt_br || s.dominant_model) && (
Modelo dominante
{s.dominant_model_pt_br || s.dominant_model}
)} {(s.why_surprising_pt_br || s.why_surprising) && (
Por que é surpreendente
{s.why_surprising_pt_br || s.why_surprising}
)} {(s.what_it_implies_pt_br || s.what_it_implies) && (
O que implica
{s.what_it_implies_pt_br || s.what_it_implies}
)} {(g.suggested_next_move_pt_br || g.suggested_next_move) && (
→ {g.suggested_next_move_pt_br || g.suggested_next_move}
)}
); } function WitnessCard({ w }: { w: WitnessItem }) { const credTone = w.credibility === "high" ? "text-[#06d6a0] border-[#06d6a0]" : w.credibility === "medium" ? "text-[#3fde6a] border-[#3fde6a]" : w.credibility === "low" ? "text-[#ffa500] border-[#ffa500]" : w.credibility === "speculation" ? "text-[#ff6ec7] border-[#ff6ec7]" : "text-[#9aa6b8] border-[#9aa6b8]"; return (
{w.witness_id}
{w.entity_id ? ( {w.canonical_name ?? w.entity_id} ) : (w.canonical_name ?? "—")}
{w.credibility && ( {w.credibility} )}
{(w.verdict_pt_br || w.verdict) && ( <>
{w.verdict_pt_br || w.verdict}
{w.verdict_pt_br && w.verdict && w.verdict_pt_br !== w.verdict && (
{w.verdict}
)} )}
{(w.access_to_event_pt_br || w.access_to_event) && (
Acesso ao evento (PT-BR)
{w.access_to_event_pt_br || w.access_to_event}
)} {(w.bias_notes_pt_br || w.bias_notes) && (
Notas de viés (PT-BR)
{w.bias_notes_pt_br || w.bias_notes}
)}
{w.corroboration_refs && w.corroboration_refs.length > 0 && (
Corroboration chain ({w.corroboration_refs.length})
    {w.corroboration_refs.map((r, i) => { const pageStr = String(r.page).padStart(3, "0"); return (
  • {r.doc_id}/p{pageStr}#{r.chunk_id} {" "} ({r.supports ? "supports" : "refutes"})
  • ); })}
)}
); } function ContradictionCard({ c }: { c: ContradictionItem }) { const statusTone = c.resolution_status === "resolved" ? "text-[#06d6a0] border-[#06d6a0]" : c.resolution_status === "irreconcilable" ? "text-[#ff3344] border-[#ff3344]" : "text-[#ff8a4d] border-[#ff8a4d]"; return (
{c.contradiction_id}
{c.resolution_status && ( {c.resolution_status} )}
{c.topic_pt_br || c.topic}
{c.topic_pt_br && c.topic_pt_br !== c.topic && (
{c.topic}
)}
{c.chunks.map((p, i) => { const pageStr = String(p.page).padStart(3, "0"); const stmt = p.statement_pt_br || p.statement; return (
Position {i + 1}{p.stance ? ` — ${p.stance}` : ""}
“{stmt}”
{p.statement_pt_br && p.statement_pt_br !== p.statement && p.statement && (
“{p.statement}”
)} {p.doc_id}/p{pageStr}#{p.chunk_id}
); })}
{(c.notes_pt_br || c.notes) && (
Notas
{c.notes_pt_br || c.notes}
)}
); } function ProbabilityBar({ label, value, color }: { label: string; value: number | null; color: string }) { const pct = value !== null ? Math.round(value * 100) : 0; return (
{label} {value !== null ? value.toFixed(3) : "—"}
); } // Linkify [[doc-id/pNNN#cNNNN]] wiki-links in argument prose into tags. function ArgumentBody({ text }: { text: string }) { const parts: Array<{ kind: "text" | "link"; raw: string; href?: string; label?: string }> = []; const re = /\[\[([^\]|]+?)(?:\|([^\]]+))?\]\]/g; let lastIdx = 0; let m: RegExpExecArray | null; while ((m = re.exec(text)) !== null) { if (m.index > lastIdx) parts.push({ kind: "text", raw: text.slice(lastIdx, m.index) }); const target = m[1]; const label = m[2] ?? m[1]; let href: string | undefined; // doc-id/pNNN#cNNNN const chunkMatch = target.match(/^([a-z0-9][a-z0-9-]*)\/p(\d{3})#(c\d{4})$/); const pageMatch = target.match(/^([a-z0-9][a-z0-9-]*)\/p(\d{3})$/); if (chunkMatch) href = `/d/${chunkMatch[1]}/p${chunkMatch[2]}#${chunkMatch[3]}`; else if (pageMatch) href = `/d/${pageMatch[1]}/p${pageMatch[2]}`; parts.push({ kind: "link", raw: m[0], href, label }); lastIdx = m.index + m[0].length; } if (lastIdx < text.length) parts.push({ kind: "text", raw: text.slice(lastIdx) }); return (
{parts.map((p, i) => p.kind === "text" ? {p.raw} : p.href ? {p.label} : {p.label} )}
); }