"use client"; import { useMemo, useState } from "react"; import { useTenant } from "@/contexts/TenantContext"; type Message = { role: "user" | "assistant" | "system"; content: string; meta?: string; }; const API_BASE = process.env.NEXT_PUBLIC_API_URL?.replace(/\/$/, "") || "http://localhost:8000"; export function ChatPanel() { const { tenantId } = useTenant(); const [message, setMessage] = useState(""); const [isSending, setIsSending] = useState(false); const [history, setHistory] = useState([ { role: "assistant", content: "Hi there! I’m the IntegraChat orchestrator. Ask anything about your tenant data and I will route the right MCP tools.", meta: "Agent ready", }, ]); const [lastDecision, setLastDecision] = useState(null); const conversationPayload = useMemo( () => history .filter((m) => m.role !== "system") .map((m) => ({ role: m.role, content: m.content, })), [history], ); async function handleSend() { if (!message.trim() || isSending) return; const userMessage: Message = { role: "user", content: message.trim() }; const optimisticHistory = [...history, userMessage]; setHistory(optimisticHistory); setMessage(""); setIsSending(true); try { const response = await fetch(`${API_BASE}/agent/message`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ tenant_id: tenantId, message: userMessage.content, conversation_history: conversationPayload, temperature: 0, }), }); if (!response.ok) { throw new Error( `API error (${response.status}) – check backend/api/main.py`, ); } const data = await response.json(); const assistantText = data?.text ?? "Agent responded but text field was empty. Inspect FastAPI logs for clues."; setHistory((prev) => [ ...prev, { role: "assistant", content: assistantText, meta: data?.decision?.reason ?? "response", }, ]); setLastDecision( data?.decision ? `${data.decision.action} · ${data.decision.tool ?? "llm"}` : null, ); } catch (err) { console.error(err); setHistory((prev) => [ ...prev, { role: "assistant", content: err instanceof Error ? err.message : "Failed to reach the FastAPI gateway.", meta: "error", }, ]); setLastDecision("error"); } finally { setIsSending(false); } } return (

Orchestrator Console

Talk to your enterprise agent

{history.map((msg, idx) => (
{msg.role}

{msg.content}

{msg.meta && (

{msg.meta}

)}
))}