import { useEffect, useRef, useState } from "react"; import { useApp } from "../state/store"; import { routeAgentInput, agentToolList, type ToolResult } from "../lib/agentTools"; import { Bot, Branch } from "../components/icons"; interface Turn { id: string; role: "user" | "agent"; text: string; ok?: boolean; } const SUGGESTIONS = [ "Open mission control", "List processes", "Run procurement to pay for store-204", "Tell Rohan that the laptop quote is approved", "Create a new process", "Open the IT hub", ]; export default function Agent() { const userEmail = useApp((s) => s.userEmail); const setScene = useApp((s) => s.setScene as (s: string) => void); const [turns, setTurns] = useState([ { id: "welcome", role: "agent", ok: true, text: `Hi ${userEmail.split("@")[0]}. I'm the FlowMaster Command Assistant. I understand a fixed set of commands and run them against EA2 — navigate, list processes, start a process, message a teammate, remember a note, recall notes. Memory is text-only with keyword recall; an LLM backend is a separate component, off by default.`, }, ]); const [draft, setDraft] = useState(""); const [working, setWorking] = useState(false); const scrollRef = useRef(null); useEffect(() => { setTimeout(() => scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight, behavior: "smooth" }), 50); }, [turns]); const submit = async (text: string) => { const t = text.trim(); if (!t || working) return; const userTurn: Turn = { id: `u-${Date.now()}`, role: "user", text: t }; setTurns((prev) => [...prev, userTurn]); setDraft(""); setWorking(true); try { const res: ToolResult = await routeAgentInput(t, { navigate: (s) => setScene(s), userEmail, }); setTurns((prev) => [...prev, { id: `a-${Date.now()}`, role: "agent", text: res.display, ok: res.ok }]); } catch (e: any) { setTurns((prev) => [...prev, { id: `a-${Date.now()}`, role: "agent", text: `Error: ${e.message}`, ok: false }]); } finally { setWorking(false); } }; return (
{turns.map((t) => (
{t.role === "user" ? userEmail.split("@")[0] : "Assistant"}
{t.text}
))} {working &&
Working…
}
{turns.length <= 1 && (
{SUGGESTIONS.map((s) => ( ))}
)}