import React, { useState, useRef, useEffect } from "react"; import { Loader2, GitBranch, Cpu, Network } from "lucide-react"; import { buildContextForQuestion, synthesizeAnswer } from "@/lib/krowCommandEngine"; import { explainRouting } from "@/lib/engines/queryRouter"; import { getEngine } from "@/lib/engines/engineRegistry"; import CausalChainView from "@/components/krowcommand/CausalChainView"; export default function CommandAnswer({ data, question, onAskAnother }) { const [answer, setAnswer] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [routing, setRouting] = useState(null); const askedRef = useRef(null); useEffect(() => { if (!data || !question || askedRef.current === question) return; askedRef.current = question; // Show routing immediately (before LLM call) for transparency setRouting(explainRouting(question)); const run = async () => { setLoading(true); setError(null); try { const context = buildContextForQuestion(question, data); const result = await synthesizeAnswer(question, data, context); setAnswer(result); } catch (e) { setError(e.message || "Failed to generate answer"); } finally { setLoading(false); } }; run(); }, [data, question]); const primaryEngine = routing?.primaryEngine || getEngine("operational_intelligence"); return (
{/* Header — shows which engine is handling this query */}

KROW Command — Causal Reasoning

{question}

{/* Engine attribution bar — shows which engine(s) are reasoning */} {routing && (
{primaryEngine.number}. {primaryEngine.shortName} {routing.secondaryEngines?.map(e => ( + {e.shortName} ))}
{routing.explanation}
)}
{loading && (

{primaryEngine.name} is reasoning…

Building the Digital Twin and tracing the causal chain to root cause(s).

)} {error && (

Unable to generate reasoning

{error}

)} {!loading && !error && answer && ( <> {/* Engine footer — shows attribution and twin snapshot */} {answer._engineName && (
Reasoned by {answer._engineName} {answer._twinHealth && ( Digital Twin health: {answer._twinHealth.overall}/100 ({answer._twinHealth.status}) )} {answer.confidence_score != null && ( Confidence: {answer.confidence_score}% )}
)} )}
); }