"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"; import { useLive } from "@/lib/live"; import { useT } from "@/components/I18n"; import { m, type Translator } from "@peppa/shared/i18n"; const KIND: Record = { command: { icon: SquareTerminal, label: m("команда") }, test: { icon: FlaskConical, label: m("тест") }, edit: { icon: FilePen, label: m("правка") }, read: { icon: FileText, label: m("чтение") }, search: { icon: Search, label: m("поиск") }, web: { icon: Globe, label: m("веб") }, agent: { icon: Bot, label: m("субагент") }, mcp: { icon: Plug, label: "MCP" }, other: { icon: Wrench, label: m("инструмент") }, }; // PEP-154: a step for the «Продакт» view — what the agent does, in words: no command, file name or path. const STEP_PLAIN: Record = { command: m("Выполняет действие в проекте"), test: m("Запускает проверки"), edit: m("Вносит изменения"), read: m("Изучает проект"), search: m("Ищет нужное в проекте"), web: m("Смотрит в интернете"), agent: m("Поручил часть работы помощнику"), mcp: m("Сверяется с задачей"), other: m("Работает над задачей"), }; const KEEP = 30; function since(iso: string, now: number, t: Translator): string { const s = Math.max(0, Math.round((now - Date.parse(iso)) / 1000)); if (s < 60) return t("{n} с", { n: s }); if (s < 3600) return t("{n} мин", { n: Math.floor(s / 60) }); return t("{n} ч", { n: 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, plain = false }: { sessionId: number; initial: StepView[]; live: boolean; hooks?: boolean; /** PEP-154: «Продакт» — what the agent does in words, no commands, file names or paths. */ plain?: boolean }) { const [steps, setSteps] = useState(initial); const [now, setNow] = useState(() => Date.now()); const t = useT(); useEffect(() => setSteps((cur) => merge(initial, cur)), [initial]); // The «N с назад» clock only (no data is fetched on it). useEffect(() => { const timer = setInterval(() => setNow(Date.now()), 10_000); return () => clearInterval(timer); }, []); // PEP-225: new steps over the tab's live channel; a hidden tab drops them and catches up on return. useLive( live ? `steps:${sessionId}` : null, (e) => { if (e.type !== "step") return; setSteps((cur) => merge([e.step as unknown as StepView], cur)); setNow(Date.now()); }, { heavy: true }, ); if (!steps.length) return (

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

); return (
    {steps.map((s, i) => { const k = KIND[s.kind] ?? KIND.other; const Icon = k.icon; return (
  1. {plain ? ( {t(STEP_PLAIN[s.kind] ?? STEP_PLAIN.other)} ) : ( {s.text} )} {!s.ok && {t("ошибка")}} {since(s.at, now, t)}
  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); }