peppa/apps/web/components/RunnerConsole.tsx
Асель d3a54fc845 PEP-38: runners console — filter counters, search, limit resume time, details panel
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 16:40:27 +05:00

147 lines
7.2 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.

// Runners console (PEP-38): list rows with status and limit info, and the details panel of the chosen runner.
import Link from "next/link";
import { SquareTerminal, X } from "lucide-react";
import type { SessionView } from "@peppa/core";
import { MAX_LIMIT_RESUMES_PER_DAY } from "@peppa/runner";
import { PanelSection } from "@/components/ActivityAside";
import { AttachCommand, QuestionCard, SessionActions } from "@/components/cards";
import { ago, Key, StatusBadge } from "@/components/common";
import { LocalTime, ScrollIntoViewOnNarrow } from "@/components/LocalTime";
import type { InboxItem } from "@/lib/queries";
import { SESSION_STATUS, TONE_BG, TONE_TEXT } from "@/lib/status";
import { cn } from "@/lib/utils";
/** «остановлена · продолжит в 03:10» for a runner that hit the usage limit (PEP-56). */
export function LimitNote({ s }: { s: SessionView }) {
if (s.status !== "rate_limited") return null;
if (!s.resumeAt) return <>остановлена · лимит</>;
if (s.resumeAt.getTime() <= Date.now()) return <>остановлена · продолжит сейчас</>;
return (
<>
остановлена · продолжит в <LocalTime iso={s.resumeAt.toISOString()} />
</>
);
}
/** `selected`: "wide" — picked by default, highlighted only where the side panel is visible. */
export function RunnerRow({ s, href, selected, needsPm }: { s: SessionView; href: string; selected: boolean | "wide"; needsPm: boolean }) {
const limited = s.status === "rate_limited";
const st = SESSION_STATUS[s.status] ?? { label: s.status, tone: "muted" as const };
return (
<li>
<Link
href={href}
scroll={false}
aria-current={selected === true ? "true" : undefined}
className={cn(
"group grid grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-x-3 gap-y-0.5 rounded-md border border-transparent px-3 py-2.5 text-sm text-foreground transition-colors hover:bg-accent hover:no-underline",
needsPm && "bg-primary/[0.07] shadow-[inset_2px_0_0_var(--primary)]",
selected === true && "border-border bg-accent",
selected === "wide" && "xl:border-border xl:bg-accent",
)}
>
<span className={cn("row-span-2 size-2 self-start mt-1.5 rounded-full", TONE_BG[st.tone], (s.status === "running" || s.status === "starting") && "animate-pulse")} />
<span className="flex min-w-0 items-baseline gap-2">
<span className="shrink-0 font-mono text-xs text-primary">{s.taskKey ?? s.projectKey}</span>
<span className="truncate">{s.taskTitle ?? "без задачи"}</span>
</span>
<span className={cn("text-right font-mono text-xs whitespace-nowrap", TONE_TEXT[st.tone])}>{limited ? (
<>
<span className="sm:hidden">{st.label}</span>
<span className="max-sm:hidden">
<LimitNote s={s} />
</span>
</>
) : (
st.label
)}
</span>
<span className="truncate font-mono text-[11px] text-muted-foreground">
{s.projectKey} · {s.provider} · #{s.id} · {s.tmuxName}
</span>
<span className="text-right font-mono text-[11px] whitespace-nowrap text-muted-foreground">{ago(s.lastActivityAt)}</span>
{limited && (
<span className="col-span-2 col-start-2 font-mono text-[11px] text-warn sm:hidden">
<LimitNote s={s} />
</span>
)}
</Link>
</li>
);
}
export function RunnerDetails({ s, questions, events, back, closeHref, chosen }: { s: SessionView; questions: InboxItem[]; events: Array<{ id: number; type: string; payload: unknown; createdAt: Date }>; back: string; closeHref: string; chosen: boolean }) {
return (
<div className="flex flex-col gap-5">
{/* Picked from the list: on narrow screens the panel is under it, scroll there. */}
{chosen && <ScrollIntoViewOnNarrow id={s.id} />}
<header className="flex flex-col gap-2">
<div className="flex items-center gap-2">
<StatusBadge status={s.status} kind="session" />
<span className="font-mono text-xs text-muted-foreground">#{s.id}</span>
<Link href={closeHref} scroll={false} aria-label="Закрыть" title="Закрыть" className="ml-auto rounded-md p-1 text-muted-foreground hover:bg-accent hover:text-foreground">
<X className="size-4" />
</Link>
</div>
<h2 className="m-0 text-base leading-snug font-semibold">
{s.taskKey && <Key href={`/t/${s.taskKey}`} className="mr-1.5">{s.taskKey}</Key>}
{s.taskTitle ?? "без задачи"}
</h2>
{s.status === "rate_limited" && (
<p className="m-0 rounded-md border border-warn/40 bg-warn/10 px-3 py-2 text-sm">
<LimitNote s={s} />
<span className="block text-xs text-muted-foreground">
авто-продолжений сегодня: {s.limitResumes} из {MAX_LIMIT_RESUMES_PER_DAY}
</span>
</p>
)}
</header>
<dl className="m-0 grid grid-cols-[max-content_minmax(0,1fr)] gap-x-3 gap-y-1.5 text-sm">
<dt className="text-muted-foreground">Проект</dt>
<dd className="m-0">
<Key href={`/p/${s.projectKey}`}>{s.projectKey}</Key>
</dd>
<dt className="text-muted-foreground">Агент</dt>
<dd className="m-0">{s.provider === "codex" ? "Codex" : "Claude Code"} · поколение {s.generation}</dd>
<dt className="text-muted-foreground">Запущен</dt>
<dd className="m-0">{ago(s.startedAt)}</dd>
<dt className="text-muted-foreground">Активность</dt>
<dd className="m-0">{ago(s.lastActivityAt)}</dd>
</dl>
<AttachCommand name={s.tmuxName} />
<SessionActions s={s} back={back} />
{questions.length > 0 && (
<PanelSection title="Ждёт ответа" count={questions.length}>
{questions.map((q) => (
<QuestionCard key={q.id} q={q} taskKey={s.taskKey} taskStatus={q.taskStatus} back={back} compact />
))}
</PanelSection>
)}
<PanelSection title="Терминал">
<div className="flex min-h-40 flex-col items-center justify-center gap-2 rounded-lg border border-dashed bg-background/60 p-4 text-center font-mono text-xs text-muted-foreground">
<SquareTerminal className="size-6" />
Живой терминал раннера появится здесь (PEP-9).
<span>Пока — команда tmux attach выше, на машине Пеппы.</span>
</div>
</PanelSection>
{events.length > 0 && (
<PanelSection title="События">
<ol className="m-0 flex list-none flex-col gap-1.5 border-l p-0 pl-3">
{events.map((e) => (
<li key={e.id} className="text-xs [overflow-wrap:anywhere]">
<span className="font-mono text-muted-foreground">{ago(e.createdAt)}</span> <span className="font-mono text-primary">{e.type}</span>{" "}
{e.type === "progress" ? (e.payload as { text: string }).text : ""}
</li>
))}
</ol>
</PanelSection>
)}
</div>
);
}