"use client"; import { useEffect, useState } from "react"; import { Bot, FilePen, FileText, FlaskConical, Globe, Plug, Search, SquareTerminal, Wrench, type LucideIcon } from "lucide-react"; import type { StepKind, StepView } from "@peppa/core"; import { cn } from "@/lib/utils"; const KIND: Record = { command: { icon: SquareTerminal, label: "команда" }, test: { icon: FlaskConical, label: "тест" }, edit: { icon: FilePen, label: "правка" }, read: { icon: FileText, label: "чтение" }, search: { icon: Search, label: "поиск" }, web: { icon: Globe, label: "веб" }, agent: { icon: Bot, label: "субагент" }, mcp: { icon: Plug, label: "MCP" }, other: { icon: Wrench, label: "инструмент" }, }; const KEEP = 30; function since(iso: string, now: number): string { const s = Math.max(0, Math.round((now - Date.parse(iso)) / 1000)); if (s < 60) return `${s} с`; if (s < 3600) return `${Math.floor(s / 60)} мин`; return `${Math.floor(s / 3600)} ч`; } /** * What the agent is doing now (PEP-33): its latest tool calls, newest first. Initial steps * come from the server, new ones from the live channel (PEP-37, `steps:`). */ export function AgentSteps({ sessionId, initial, live, hooks = true }: { sessionId: number; initial: StepView[]; live: boolean; hooks?: boolean }) { const [steps, setSteps] = useState(initial); const [now, setNow] = useState(() => Date.now()); useEffect(() => setSteps((cur) => merge(initial, cur)), [initial]); useEffect(() => { const t = setInterval(() => setNow(Date.now()), 10_000); if (!live || typeof EventSource === "undefined") return () => clearInterval(t); const es = new EventSource(`/api/live?scope=steps:${sessionId}`); es.addEventListener("step", (e) => { try { const { step } = JSON.parse((e as MessageEvent).data) as { step: StepView }; setSteps((cur) => merge([step], cur)); setNow(Date.now()); } catch {} }); return () => { clearInterval(t); es.close(); }; }, [sessionId, live]); if (!steps.length) return (

{hooks ? (live ? "Агент ещё ничего не сделал — шаги появятся здесь." : "Шагов не записано.") : "Этот агент не сообщает шаги (нет хуков) — смотри терминал."}

); return (
    {steps.map((s, i) => { const k = KIND[s.kind] ?? KIND.other; const Icon = k.icon; return (
  1. {s.text} {!s.ok && ошибка} {since(s.at, now)}
  2. ); })}
); } function merge(add: StepView[], cur: StepView[]): StepView[] { const seen = new Set(); return [...add, ...cur] .filter((s) => !seen.has(s.id) && (seen.add(s.id), true)) .sort((a, b) => b.id - a.id) .slice(0, KEEP); }