peppa/apps/web/components/AgentSteps.tsx
2026-11-15 14:47:50 +05:00

92 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"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 { useT } from "@/components/I18n";
import { m, type Translator } from "@peppa/shared/i18n";
const KIND: Record<StepKind, { icon: LucideIcon; label: string }> = {
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("инструмент") },
};
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:<sessionId>`).
*/
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());
const t = useT();
useEffect(() => setSteps((cur) => merge(initial, cur)), [initial]);
useEffect(() => {
const timer = setInterval(() => setNow(Date.now()), 10_000);
if (!live || typeof EventSource === "undefined") return () => clearInterval(timer);
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(timer);
es.close();
};
}, [sessionId, live]);
if (!steps.length)
return (
<p className="m-0 rounded-md border border-dashed px-3 py-2 text-xs text-muted-foreground">
{hooks ? (live ? t("Агент ещё ничего не сделал — шаги появятся здесь.") : t("Шагов не записано.")) : t("Этот агент не сообщает шаги (нет хуков) — смотри терминал.")}
</p>
);
return (
<ol aria-label={t("Шаги агента")} className="m-0 flex max-h-72 list-none flex-col gap-0.5 overflow-auto p-0">
{steps.map((s, i) => {
const k = KIND[s.kind] ?? KIND.other;
const Icon = k.icon;
return (
<li key={s.id} className={cn("flex items-center gap-2 rounded px-1.5 py-1 text-xs", i === 0 && live && "bg-info/10")}>
<Icon className={cn("size-3.5 shrink-0", s.ok ? "text-muted-foreground" : "text-destructive")} aria-label={t(k.label)} />
<span className={cn("min-w-0 flex-1 truncate font-mono", !s.ok && "text-destructive")} title={`${s.tool}: ${s.text}`}>
{s.text}
</span>
{!s.ok && <span className="shrink-0 text-[10px] text-destructive">{t("ошибка")}</span>}
<span className="shrink-0 font-mono text-[10px] text-muted-foreground tabular-nums">{since(s.at, now, t)}</span>
</li>
);
})}
</ol>
);
}
function merge(add: StepView[], cur: StepView[]): StepView[] {
const seen = new Set<number>();
return [...add, ...cur]
.filter((s) => !seen.has(s.id) && (seen.add(s.id), true))
.sort((a, b) => b.id - a.id)
.slice(0, KEEP);
}