// 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 RunnerTaskView, type SessionView, type StepView } from "@peppa/core";
import { RunnerStories } from "@/components/Runners";
import { MAX_LIMIT_RESUMES_PER_DAY } from "@peppa/runner";
import { PanelSection } from "@/components/ActivityAside";
import { AgentSteps } from "@/components/AgentSteps";
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 { SessionUsageLine } from "@/components/Accounting";
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 (
<>
остановлена · продолжит в
>
);
}
interface SwitchPayload {
fromName?: string | null;
toName?: string | null;
reason?: "limit" | "threshold";
carried?: "resume" | "fresh";
}
/** «Claude A → Claude B · лимит · разговор продолжен» for a subscription_switched event (PEP-39). */
export function switchText(payload: unknown): string {
const p = (payload ?? {}) as SwitchPayload;
return `${p.fromName ?? "?"} → ${p.toName ?? "?"} · ${p.reason === "threshold" ? "порог использования" : "лимит"} · ${p.carried === "fresh" ? "запуск заново" : "разговор продолжен"}`;
}
/** One line of a session's event log: type + the human part for known types. */
export function eventLine(e: { type: string; payload: unknown }): string {
if (e.type === "progress") return (e.payload as { text: string }).text;
if (e.type === "subscription_switched") return switchText(e.payload);
if (e.type === "task_updated") return JSON.stringify((e.payload as { to: unknown }).to);
if (e.type === "pm_comment") return (e.payload as { text: string }).text;
if (e.type === "question_answered") return `ответ на #${(e.payload as { questionId?: number }).questionId}: ${(e.payload as { answer?: string }).answer ?? ""}`;
if (e.type === "budget_paused") return `бюджет исчерпан: ${(e.payload as { summary?: string }).summary ?? ""}`;
return "";
}
/** `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 (
{s.taskKey ?? s.projectKey}
{s.taskTitle ?? s.name ?? "без стори"}
{limited ? (
<>
{st.label}
>
) : (
st.label
)}
{s.name && s.taskTitle ? `${s.name} · ` : ""}{s.projectKey} · {s.provider}{s.subscriptionName ? ` · ${s.subscriptionName}` : ""}{s.subscriptionSwitches > 0 ? ` (↻${s.subscriptionSwitches})` : ""} · #{s.id} · {s.tmuxName}
{ago(s.lastActivityAt)}
{limited && (
)}
);
}
export function RunnerDetails({ s, questions, events, steps, stories, back, closeHref, chosen }: { s: SessionView; questions: InboxItem[]; events: Array<{ id: number; type: string; payload: unknown; createdAt: Date }>; steps: StepView[]; stories?: { history: RunnerTaskView[]; queue: RunnerTaskView[] }; back: string; closeHref: string; chosen: boolean }) {
const finished = FINISHED_SESSION_STATUSES.includes(s.status);
return (
{/* Picked from the list: on narrow screens the panel is under it, scroll there. */}
{chosen &&
}
#{s.id}
{s.taskKey && {s.taskKey}}
{s.taskTitle ?? s.name ?? "без стори"}
{s.name && s.taskTitle && {s.name}
}
{/* PEP-5: tokens, cost, context of this runner. */}
{s.status === "rate_limited" && (
авто-продолжений сегодня: {s.limitResumes} из {MAX_LIMIT_RESUMES_PER_DAY}
{s.rotateReason === "limit" && переключается на свободную подписку пула…}
)}
{s.rotateReason === "threshold" && (
Подписка подходит к лимиту — после этого хода переключится на свободную.
)}
- Проект
-
{s.projectKey}
- Агент
- {PROVIDER_NAMES[s.provider]} · поколение {s.generation}
- Подписка
-
{s.subscriptionName ?? (s.subscriptionId ? `#${s.subscriptionId}` : "вход машины")}
{s.subscriptionSwitches > 0 && · переключений {s.subscriptionSwitches}}
- Запущен
- {ago(s.startedAt)}
- Активность
- {ago(s.lastActivityAt)}
{stories &&
}
{questions.length > 0 && (
{questions.map((q) => (
))}
)}
{finished ? (
Сессия завершена — терминала больше нет.
) : (
)}
{events.some((e) => e.type === "subscription_switched") && (
{events
.filter((e) => e.type === "subscription_switched")
.map((e) => (
-
{ago(e.createdAt)} {switchText(e.payload)}
))}
)}
{events.length > 0 && (
{events.map((e) => (
-
{ago(e.createdAt)} {e.type}{" "}
{eventLine(e)}
))}
)}
);
}