"use client"; import React, { useEffect } from "react"; import { useState, useRef, useMemo } from "react"; import DiagnosticsView from "@/components/diagnostics-view"; import ReasoningWorkspace, { LoadingOverlay, ContinueLaterBanner } from "@/components/reasoning-workspace"; import { mockFetch, AVAILABLE_SCENARIOS } from "@/lib/mocks/confidence-engine/mock-client"; import { deriveFindingsFromContributions, normalizeFindings } from "@/lib/graph/finding-helpers"; /* Compile-time env resolution — NEXT_PUBLIC_ vars are injected by Next.js at build */ const MOCK_ENABLED = process.env.NEXT_PUBLIC_CONFIDENCE_ENGINE_MOCKS === "true"; /* ── inject runtime globals for the mock client to read ──── */ function useMockGlobals() { useEffect(() => { if (MOCK_ENABLED) { var w = window; w.__MOCK_ENABLED = true; w.__MOCK_DELAY = process.env.NEXT_PUBLIC_CONFIDENCE_MOCK_DELAY || "normal"; w.__MOCK_SCENARIO = process.env.NEXT_PUBLIC_CONFIDENCE_ENGINE_MOCK_SCENARIO || ""; } }, []); } const MAX_LENGTH = 10000; export async function submitScenarioForStartCase(fetchImpl, scenario) { return fetchImpl("/api/cases/start", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ scenario }), }); } export async function submitAnswerForUpdateCase( fetchImpl, { situationGraph, previousQuestion, answer, findings }, ) { if (!answer?.trim()) { return { ok: false, skipped: true, data: { success: false, stage: "request_validation", error: "Please enter an answer before updating.", }, }; } const body = { situationGraph, previousQuestion, answer }; if (findings && findings.length > 0) { body.findings = findings; } const response = await fetchImpl("/api/cases/update", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); return { ok: response.ok, skipped: false, data: await response.json(), }; } function normaliseStartResult(data) { return { ...data, selectedQuestion: typeof data?.selectedQuestion === "string" ? data.selectedQuestion : data?.selectedQuestion?.question ?? null, newlySurfacedNodeIds: data?.newlySurfacedNodeIds ?? [], }; } function normaliseUpdateSelectedQuestion(selectedQuestion) { if (!selectedQuestion) return null; if (typeof selectedQuestion === "string") return selectedQuestion; return selectedQuestion.question ?? null; } export function ScenarioResultPanels({ status, result }) { if (!result) return null; const hasGraph = Boolean(result.situationGraph); const hasQuestion = Boolean(result.selectedQuestion?.question); const hasDiagnostics = Boolean(result.diagnostics); return ( <> {status === "error" && (
{result.error && (
Error: {result.error}
)} {!hasGraph && !hasQuestion && (
Validation failed — no structured graph output was produced.
)}
)} {hasDiagnostics && } ); } // ── Message pools ─────────────────────────────────────────── const INITIAL_MESSAGES = [ { min: 0, text: "Reading your situation" }, { min: 10, text: "Building a structured understanding" }, { min: 25, text: "Identifying what is known and still unclear" }, { min: 45, text: "Selecting the next useful question" }, ]; const UPDATE_MESSAGES = [ { min: 0, text: "Considering your answer" }, { min: 10, text: "Updating the situation" }, { min: 25, text: "Checking what changed" }, { min: 45, text: "Choosing the next question" }, ]; function useLoadingStatus(messages, isLoading) { const [elapsed, setElapsed] = useState(0); const startRef = useRef(null); useEffect(() => { if (isLoading) { startRef.current = Date.now(); const iv = setInterval(() => { setElapsed(Math.floor((Date.now() - startRef.current) / 1000)); }, 1000); return () => clearInterval(iv); } else { setElapsed(0); startRef.current = null; } }, [isLoading]); const currentMessage = useMemo(() => { if (!messages || messages.length === 0) return ""; let msg = messages[0].text; for (const m of messages) { if (elapsed >= m.min) msg = m.text; } return msg; }, [messages, elapsed]); return { elapsed, currentMessage }; } export function UpdateErrorPanel({ updateError }) { if (!updateError) return null; const errors = [ ...(updateError.errors || []), ...(updateError.validationErrors || []), ...(updateError.graphValidationErrors || []), ...(updateError.proposalErrors || []), ...(updateError.providerErrors || []), ]; return (
Update error: {updateError.error}
{errors.length > 0 && (
Update details ({errors.length})
    {errors.map((item, index) => (
  • {typeof item === "string" ? item : item?.message || JSON.stringify(item)}
  • ))}
)}
); } export { INITIAL_MESSAGES, UPDATE_MESSAGES, useLoadingStatus }; // ── Session key ──────────────────────────────────────────────── const SESSION_KEY = "confidence-engine-session"; function getSession() { if (typeof sessionStorage === "undefined") return null; try { const raw = sessionStorage.getItem(SESSION_KEY); return raw ? JSON.parse(raw) : null; } catch (_) { return null; } } function saveSession(state) { if (typeof sessionStorage === "undefined") return; try { sessionStorage.setItem(SESSION_KEY, JSON.stringify(state)); } catch (_) {} } function clearSession() { if (typeof sessionStorage === "undefined") return; try { sessionStorage.removeItem(SESSION_KEY); } catch (_) {} } /** * Derives whether the current component state represents a valid investigation * context sufficient to render a workspace surface. * * Valid only when: * - result carries a situationGraph (renderable graph), OR * - status is "success" AND there is a non-empty scenario * (from session restoration with real data). * * This predicate is the single source of truth for all render-gate decisions. * showExperimentView, fixture availability, or sessionStorage keys alone are * NOT sufficient to constitute valid context. */ export function hasValidInvestigationContext(result, status, scenario) { return Boolean(result?.situationGraph) || (status === "success" && Boolean(scenario?.trim())); } /** * Derives the primary surface that must render for the given state tuple. * Enforces exactly-one-primary-surface invariant: no zero, no two. */ export function derivePrimarySurface(result, status, _showExperimentView, scenario, activeBranchId) { if (status === "loading") return "LOADING"; if (status === "error") return "ERROR_SURFACE"; const valid = hasValidInvestigationContext(result, status, scenario); if (valid) return "NORMAL_WORKSPACE"; return "SCENARIO_ENTRY"; } export default function ScenarioForm() { const [scenario, setScenario] = useState(""); const [status, setStatus] = useState("idle"); // idle | loading | error | success const [result, setResult] = useState(null); const [answer, setAnswer] = useState(""); const [updateStatus, setUpdateStatus] = useState("idle"); // idle | loading | error | success const [updateError, setUpdateError] = useState(null); const [updateResult, setUpdateResult] = useState(null); const [lastSubmittedAnswer, setLastSubmittedAnswer] = useState(""); const [currentUnderstanding, setCurrentUnderstanding] = useState(null); const [mockScenario, setMockScenario] = useState(""); const [hideFacilitatorOnLanding, setHideFacilitatorOnLanding] = useState(false); /* ── RTO.31: focused contributions ownership ─────────────── */ const [focusedContributions, setFocusedContributions] = useState([]); /* ── v2 findings from focused contributions ─────────────── */ const [findings, setFindings] = useState([]); function appendFinding(finding) { setFindings((prev) => { return [...prev, finding]; }); } function updateFindingDisposition(findingId, newDisposition) { setFindings((prev) => prev.map((f) => (f.id === findingId ? { ...f, userDisposition: newDisposition } : f)), ); } function updateFindingProposition(findingId, newProposition) { setFindings((prev) => prev.map((f) => f.id === findingId ? { ...f, proposition: newProposition, userDisposition: null } : f, ), ); } function appendFocusedContribution(contribution) { // Derive a single stored contribution object and use it for BOTH // contribution storage AND Finding derivation so the same identity // appears in focusedContributions[] and Finding.contributionId. setFocusedContributions((prev) => { const seq = prev.length + 1; const storedContribution = { ...contribution, sequence: seq, id: `contrib-${String(seq).padStart(4, "0")}` }; // Derive Findings from the exact stored Contribution (not a separate approximation) setFindings((prevFindings) => { const newFindings = deriveFindingsFromContributions([storedContribution]).findings; return normalizeFindings([...prevFindings, ...newFindings]); }); return [...prev, storedContribution]; }); } const textareaRef = useRef(null); /* ── Valid investigation predicate ─────────────────────── */ // Delegated to the exported utility below. const validCtx = hasValidInvestigationContext(result, status, scenario); /* Restore persisted session on mount (Phase 3) ─────────── */ useEffect(() => { if (typeof window === "undefined") return; const saved = getSession(); if (!saved) return; const hasGraph = Boolean(saved.situationGraph); setScenario(saved.scenario || ""); setResult(hasGraph ? { ...saved, situationGraph: saved.situationGraph } : null); setCurrentUnderstanding(saved.summary || null); setFocusedContributions(saved.focusedContributions || []); setFindings(saved.findings || []); // Partial sessions (present but no graph) must NOT suppress the // scenario-entry form. Only promote to success when there is actual // investigation data to render. if (hasGraph) { setStatus("success"); } }, []); /* Restore facilitator dismiss preference (Experiment 05) ─── */ useEffect(() => { if (typeof window === "undefined") return; try { const pref = sessionStorage.getItem("ce-facilitator-dismissed"); setHideFacilitatorOnLanding(pref === "true"); } catch (_) {} }, []); /* Inject mock globals so the interceptor can read them at runtime */ useMockGlobals(); function handleScenarioSelect(key) { setMockScenario(key); if (typeof window !== "undefined") { window.__MOCK_SCENARIO = key; } // Auto-fill central statement for quick start var found = AVAILABLE_SCENARIOS.find(function(s) { return s.key === key; }); if (found && found.centralStatement) { setScenario(found.centralStatement); } } function handleScenarioFill(key) { handleScenarioSelect(key); setStatus("loading"); setResult(null); setAnswer(""); setUpdateStatus("idle"); setUpdateResult(null); setLastSubmittedAnswer(""); setCurrentUnderstanding(null); setUpdateError(null); // Simulate a click on the analyse button after auto-filling setTimeout(function() { var btn = document.querySelector('button[type="submit"]'); if (btn && !btn.disabled) btn.click(); }, 50); } const { elapsed: startElapsed, currentMessage: startMsg } = useLoadingStatus( INITIAL_MESSAGES, status === "loading" ); const { elapsed: updateElapsed, currentMessage: updateMsg } = useLoadingStatus( UPDATE_MESSAGES, updateStatus === "loading" ); const handleSubmit = async (e) => { e.preventDefault(); setStatus("loading"); setResult(null); setAnswer(""); setUpdateStatus("idle"); setUpdateResult(null); setLastSubmittedAnswer(""); setUpdateError(null); setCurrentUnderstanding(null); // Force a DOM flush so loading state renders before awaiting (prevents instant mocks from swallowing it) await new Promise(r => requestAnimationFrame(() => setTimeout(r, 50))); try { const res = await submitScenarioForStartCase(MOCK_ENABLED ? mockFetch : fetch, scenario); const data = await res.json(); if (res.ok && data.success) { setStatus("success"); setCurrentUnderstanding(data.summary ?? null); const normalised = normaliseStartResult(data); setResult(normalised); saveSession({ scenario, situationGraph: normalised.situationGraph, selectedQuestion: normalised.selectedQuestion, summary: data.summary ?? null, updatedAt: new Date().toISOString(), focusedContributions, findings: [] }); } else { setStatus("error"); setCurrentUnderstanding(data.summary ?? null); setResult(normaliseStartResult(data)); } } catch (err) { setStatus("error"); setResult({ error: err.message || "Network request failed" }); } }; const handleUpdate = async (e) => { e.preventDefault(); // Guard empty answer before showing loading state if (!answer?.trim()) { setUpdateStatus("error"); setUpdateError({ error: "Please enter an answer before updating." }); return; } setUpdateStatus("loading"); setUpdateError(null); setLastSubmittedAnswer(answer.trim()); // Force a DOM flush so loading state renders before awaiting (prevents instant mocks from swallowing it) await new Promise(r => requestAnimationFrame(() => setTimeout(r, 50))); const submission = await submitAnswerForUpdateCase(MOCK_ENABLED ? mockFetch : fetch, { situationGraph: result?.situationGraph, previousQuestion: result?.selectedQuestion, answer, findings, }); if (submission.skipped) { setUpdateStatus("error"); setUpdateError(submission.data); return; } try { const outcome = submission.data; if (submission.ok && outcome.success) { // Merge server-returned findings with local state let newFindings = [...findings]; if (outcome.appendedFindings && Array.isArray(outcome.appendedFindings)) { newFindings = [...newFindings, ...outcome.appendedFindings]; } setUpdateStatus("success"); setCurrentUnderstanding( outcome.summary ? outcome.summary : currentUnderstanding, ); setUpdateResult({ ...outcome, previousSituationGraph: result?.situationGraph ?? null, }); setResult((current) => ({ ...current, situationGraph: outcome.updatedSituationGraph, selectedQuestion: normaliseUpdateSelectedQuestion( outcome.selectedQuestion, ), newlySurfacedNodeIds: (outcome.proposal?.addedNodes || []) .filter((node) => node.kind === "unknown") .map((node) => node.id), diagnostics: outcome.diagnostics, })); setAnswer(""); // Persist after successful update turn — include findings saveSession({ scenario, situationGraph: outcome.updatedSituationGraph, selectedQuestion: normaliseUpdateSelectedQuestion(outcome.selectedQuestion), summary: outcome.summary ?? currentUnderstanding, updatedAt: new Date().toISOString(), focusedContributions, findings: newFindings }); } else { setUpdateStatus("error"); setUpdateError(outcome); } } catch (err) { setUpdateStatus("error"); setUpdateError({ error: err.message || "Network request failed" }); } }; return (
{/* ── Idle form for scenario input ─ */} {!result?.situationGraph && status === "idle" && (
{/* Two-column landing workspace */}
{/* Left panel — Facilitator (1/3 on desktop) */} {!hideFacilitatorOnLanding && (

Before we begin

The Confidence Engine helps build confidence by understanding situations before deciding what to do.

You do not need to know exactly what the problem is.

Simply describe what you have observed. We will work through it together, one question at a time.

{ if (e.target.checked) { setHideFacilitatorOnLanding(true); try { sessionStorage.setItem("ce-facilitator-dismissed", "true"); } catch (_) {} } }} className="h-4 w-4 rounded border-gray-300 text-blue-600 focus:ring-blue-500" />
)} {/* Right panel — Workspace (2/3 on desktop) */}

What's the situation