"use client"; import React, { useState } from "react"; import { CopilotKit, useCoAgent, useCoAgentStateRender, useCopilotAction, } from "@copilotkit/react-core"; import { CopilotChat } from "@copilotkit/react-ui"; import "@copilotkit/react-ui/styles.css"; import StateDisplay from "./StateDisplay"; interface SourceRef { chunk_id: string; document_uri: string; document_title: string; chunk_position: number; } interface ResearchState { question: string; phase: string; status: string; plan: Array<{ id: number; question: string; status: string; search_results?: { type: string; results: Array<{ chunk: string; chunk_id: string; document_uri: string; document_title: string; chunk_position: number; full_chunk_content: string; score: number; expanded: boolean; }>; }; }>; current_question_index: number; insights: Array<{ summary: string; confidence: number; source_refs: SourceRef[]; }>; document_registry: Record< string, { title: string; chunks_referenced: string[]; } >; current_document: { uri: string; title: string; content: string; total_chunks: number; metadata?: Record; } | null; confidence: number; final_report: { title: string; summary: string; findings: string[]; conclusions: string[]; sources: string[]; citations: Array<{ document_uri: string; document_title: string; chunk_ids: string[]; }>; } | null; } function AgentContent() { // Use useCoAgent to sync state with the backend research agent const { state } = useCoAgent({ name: "research_agent", initialState: { question: "", phase: "idle", status: "", plan: [], current_question_index: 0, insights: [], document_registry: {}, current_document: null, confidence: 0.0, final_report: null, }, }); // Log state changes console.log("[FRONTEND] Current state:", state); // Human-in-the-loop: Request approval for research plan console.log("[FRONTEND] Registering approve_research_plan action"); useCopilotAction({ name: "approve_research_plan", description: "Request user approval for the research plan. Returns 'APPROVED' if approved or 'REVISE' if user wants to revise.", parameters: [], renderAndWaitForResponse: ({ respond, status }) => { console.log( "[FRONTEND ACTION] renderAndWaitForResponse called", { status } ); return (

Research Plan Approval

Please review the research plan in the right pane.

); }, }); // Render state updates from the research agent useCoAgentStateRender({ name: "research_agent", render: ({ state: newState }) => { console.log("[FRONTEND] State render update:", newState); // Show different messages based on phase let phaseMessage = ""; switch (newState.phase) { case "planning": phaseMessage = "Planning research..."; break; case "searching": phaseMessage = "Searching..."; break; case "analyzing": phaseMessage = "Extracting insights..."; break; case "evaluating": phaseMessage = `Evaluating confidence: ${(newState.confidence * 100).toFixed(0)}%`; break; case "synthesizing": phaseMessage = "Generating final report..."; break; case "done": phaseMessage = "Research complete!"; break; default: phaseMessage = newState.status || "Ready"; } return (
Research Update: {phaseMessage}
); }, }); return ( <>
{/* Chat on the left */}
{/* State display on the right */}

Research State

Live updates from the research agent

); } export default function Agent() { return ( ); }