"use client"; import { Markdown } from "@copilotkit/react-ui"; import { useState } from "react"; interface InsightRecord { id: string; summary: string; status: string; notes?: string; supporting_sources: string[]; originating_questions: string[]; } interface GapRecord { id: string; description: string; severity: string; blocking: boolean; resolved: boolean; notes?: string; supporting_sources: string[]; resolved_by: string[]; } interface SearchAnswer { sub_question: string; answer: string; confidence: number; chunks_used: number; } interface ResearchContext { original_question: string; sub_questions: string[]; qa_responses: SearchAnswer[]; insights: InsightRecord[]; gaps: GapRecord[]; } interface EvaluationResult { confidence: number; reasoning: string; should_continue: boolean; gaps_identified: string[]; follow_up_questions: string[]; } interface ResearchReport { question: string; summary: string; findings: string[]; conclusions: string[]; insights_used: string[]; methodology: string; } interface ResearchState { context: ResearchContext; iterations: number; max_iterations: number; confidence_threshold: number; max_concurrency: number; last_eval: EvaluationResult | null; last_analysis: { insights_extracted: InsightRecord[]; gaps_identified: GapRecord[]; } | null; result?: ResearchReport; } interface StateDisplayProps { state: ResearchState; } export default function StateDisplay({ state }: StateDisplayProps) { const [expandedSections, setExpandedSections] = useState< Record >({ questions: true, insights: true, gaps: true, report: true, }); const [expandedQuestions, setExpandedQuestions] = useState< Record >({}); const toggleSection = (section: string) => { setExpandedSections((prev) => ({ ...prev, [section]: !prev[section], })); }; const toggleQuestion = (questionId: string) => { setExpandedQuestions((prev) => ({ ...prev, [questionId]: !prev[questionId], })); }; // Calculate research progress based on iterations const researchProgress = state.max_iterations > 0 ? (state.iterations / state.max_iterations) * 100 : 0; const confidence = state.last_eval?.confidence || 0; return (
{/* Current Status */}
Research Progress
{state.iterations === 0 ? "Ready" : state.result ? "Complete" : "Researching"}
{state.iterations > 0 && (
Iteration {state.iterations} of {state.max_iterations}
)}
{/* Research Progress Bar */} {state.iterations > 0 && (
Iterations {state.iterations}/{state.max_iterations}
)}
{/* Question */} {state.context.original_question && (
Question
{state.context.original_question}
)} {/* Confidence Meter */} {confidence > 0 && (
Confidence
0.8 ? "#48bb78" : confidence > 0.5 ? "#ed8936" : "#f56565", transition: "width 0.3s ease", }} />
0.8 ? "#48bb78" : confidence > 0.5 ? "#ed8936" : "#f56565", }} > {(confidence * 100).toFixed(0)}%
{state.last_eval?.reasoning && (
)}
)} {/* Sub-Questions and QA Responses */} {(state.context.sub_questions.length > 0 || state.context.qa_responses.length > 0) && (
{expandedSections.questions && (
{/* Show pending sub_questions */} {state.context.sub_questions.map((question, idx) => (
))} {/* Show all qa_responses (each has query + answer) */} {state.context.qa_responses.map((qaResponse, idx) => { const questionId = `q-${idx}`; return (
{/* QA Response nested inside question */} {expandedQuestions[questionId] && (
Answer
)}
); })}
)}
)} {/* Insights */} {state.context.insights.length > 0 && (
{expandedSections.insights && (
{state.context.insights.map((insight) => (
{insight.status} {insight.supporting_sources.length} sources
{insight.notes && (
)} {insight.supporting_sources.length > 0 && (
Sources: {insight.supporting_sources.map((source, srcIdx) => ( {srcIdx > 0 && ", "} {source} ))}
)}
))}
)}
)} {/* Knowledge Gaps */} {state.context.gaps.length > 0 && (
{expandedSections.gaps && (
{state.context.gaps.map((gap) => (
{gap.severity} {gap.blocking && ( Blocking )} {gap.resolved && ( Resolved )}
{gap.notes && (
)} {gap.resolved && gap.resolved_by.length > 0 && (
Resolved by: {gap.resolved_by.map((source, srcIdx) => ( {srcIdx > 0 && ", "} {source} ))}
)}
))}
)}
)} {/* Final Report */} {state.result && (
{expandedSections.report && (

{state.result.question}

Summary

Key Findings

    {state.result.findings.map((finding, idx) => (
  • ))}

Conclusions

    {state.result.conclusions.map((conclusion, idx) => (
  • ))}

Methodology

Insights Used ({state.result.insights_used.length})

{state.result.insights_used.map((insightId, idx) => { const insight = state.context.insights.find( (i) => i.id === insightId, ); return (
{insight ? (
) : (
Insight ID: {insightId}
)}
); })}
)}
)}
); }