- runner/terminal.ts: TerminalFeed polls the pane (capture-pane -e, one loop per runner shared by viewers), sends changed lines, scrolled-off lines and the cursor; terminalInput (send-keys -H / paste-buffer -p) with access, liveness, size and rate checks, resets auto_nudges, logs terminal_input; terminalSse with 15 s pings for Cloudflare - web: GET/POST /api/runners/[id]/terminal, RunnerTerminal (xterm.js) in the runner panel and on the story page; help - testkit: terminal.test.ts — real tmux into a headless xterm, history, input, access, limits, SSE - OpenSpec change archived; new spec runner-terminal Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
153 lines
7.5 KiB
TypeScript
153 lines
7.5 KiB
TypeScript
// 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 { FINISHED_SESSION_STATUSES, PROVIDER_NAMES, 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 { RunnerTerminal } from "@/components/RunnerTerminal";
|
||
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.subscriptionName ? ` · ${s.subscriptionName}` : ""} · #{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">{PROVIDER_NAMES[s.provider]} · поколение {s.generation}</dd>
|
||
<dt className="text-muted-foreground">Подписка</dt>
|
||
<dd className="m-0">{s.subscriptionName ?? (s.subscriptionId ? `#${s.subscriptionId}` : "вход машины")}</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="Терминал">
|
||
{FINISHED_SESSION_STATUSES.includes(s.status) ? (
|
||
<div className="flex min-h-24 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" />
|
||
Сессия завершена — терминала больше нет.
|
||
</div>
|
||
) : (
|
||
<RunnerTerminal key={s.id} sessionId={s.id} live />
|
||
)}
|
||
</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>
|
||
);
|
||
}
|