peppa/apps/web/components/AgentSteps.tsx
Асель 4a80d9dc6b PEP-32/33/37: story page tabs, live agent step feed, SSE live updates instead of 5 s polling
- PEP-37: change hub (runner/live.ts) woken by notify() (new 'web' subscriber) and
  PRAGMA data_version; per-scope fingerprints; GET /api/live SSE (no-transform, ping 15 s);
  LiveRefresh replaces AutoRefresh with a connection indicator and polling fallback
- PEP-33: async PostToolUse/PostToolUseFailure hook -> 'step' events (last 200 per
  session), AgentSteps live feed on the story page and runner panel, tasks.md progress
  on board cards and the story header
- PEP-32: story page header + tabs Proposal/Design/Specs/Tasks/Comments/Tests/Merge
  request/Activity, safe Markdown renderer, PM comments visible in get_task, edit
  title/description/epic, accept-and-merge (acceptStory)
- OpenSpec changes archived; specs live-updates, agent-steps, story-page

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 05:59:15 +05:00

89 lines
3.7 KiB
TypeScript
Raw Permalink 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";
const KIND: Record<StepKind, { icon: LucideIcon; label: string }> = {
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:<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());
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 (
<p className="m-0 rounded-md border border-dashed px-3 py-2 text-xs text-muted-foreground">
{hooks ? (live ? "Агент ещё ничего не сделал — шаги появятся здесь." : "Шагов не записано.") : "Этот агент не сообщает шаги (нет хуков) — смотри терминал."}
</p>
);
return (
<ol aria-label="Шаги агента" 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={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">ошибка</span>}
<span className="shrink-0 font-mono text-[10px] text-muted-foreground tabular-nums">{since(s.at, now)}</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);
}