feat(pi): render subagent monitor views
This commit is contained in:
@@ -1,29 +1,80 @@
|
||||
import { isTerminalState } from "./status.ts";
|
||||
import type { SubagentStatus } from "./types.ts";
|
||||
import type { SubagentState, SubagentStatus } from "./types.ts";
|
||||
|
||||
const COMPACT_GROUPS: Array<{ label: string; states: SubagentState[] }> = [
|
||||
{ label: "queued", states: ["queued"] },
|
||||
{ label: "running", states: ["starting", "running"] },
|
||||
{ label: "settling", states: ["settling"] },
|
||||
{ label: "completed", states: ["completed"] },
|
||||
{ label: "failed", states: ["failed"] },
|
||||
{ label: "timed out", states: ["timed_out"] },
|
||||
{ label: "cancelled", states: ["cancelled"] },
|
||||
{ label: "orphaned", states: ["orphaned"] },
|
||||
];
|
||||
|
||||
const STATE_PRESENTATION: Record<SubagentState, { icon: string; label: string }> = {
|
||||
queued: { icon: "…", label: "queued" },
|
||||
starting: { icon: "◌", label: "starting" },
|
||||
running: { icon: "▶", label: "running" },
|
||||
settling: { icon: "◒", label: "settling" },
|
||||
completed: { icon: "✓", label: "completed" },
|
||||
failed: { icon: "✗", label: "failed" },
|
||||
cancelled: { icon: "■", label: "cancelled" },
|
||||
timed_out: { icon: "⏱", label: "timed out" },
|
||||
orphaned: { icon: "?", label: "orphaned" },
|
||||
};
|
||||
|
||||
export function renderSummary(statuses: SubagentStatus[]): string[] {
|
||||
const running = statuses.filter((status) => ["starting", "running", "settling"].includes(status.state)).length;
|
||||
const queued = statuses.filter((status) => status.state === "queued").length;
|
||||
const terminal = statuses.filter((status) => isTerminalState(status.state)).length;
|
||||
if (running === 0 && queued === 0 && terminal === 0) return [];
|
||||
return [`subagents: ${running} running · ${queued} queued · ${terminal} terminal`];
|
||||
const groups = COMPACT_GROUPS.map((group) => ({
|
||||
label: group.label,
|
||||
count: statuses.filter((status) => group.states.includes(status.state)).length,
|
||||
})).filter((group) => group.count > 0);
|
||||
|
||||
if (groups.length === 0) return [];
|
||||
return [`subagents: ${groups.map((group) => `${group.label} ${group.count}`).join(" · ")}`];
|
||||
}
|
||||
|
||||
export function renderInspector(statuses: SubagentStatus[]): string[] {
|
||||
const lines = renderSummary(statuses);
|
||||
for (const status of statuses) {
|
||||
lines.push(
|
||||
`${status.id} ${status.label} ${status.context} ${status.state} ${Math.round(status.elapsedMs / 1000)}s ${status.model ?? "inherit"} ${status.tools} ${status.lastEvent ?? "none"} result:${status.resultAvailable ? "yes" : "no"}`,
|
||||
);
|
||||
}
|
||||
return lines;
|
||||
return statuses.map((status) => renderStatusRow(status));
|
||||
}
|
||||
|
||||
export function widget(statuses: SubagentStatus[], expanded: boolean) {
|
||||
return () => ({
|
||||
invalidate() {},
|
||||
render(width: number) {
|
||||
return (expanded ? renderInspector(statuses) : renderSummary(statuses)).map((line) => (line.length > width ? line.slice(0, Math.max(0, width - 1)) : line));
|
||||
return (expanded ? renderInspector(statuses) : renderSummary(statuses)).map((line) => truncateLine(line, width));
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function renderStatusRow(status: SubagentStatus): string {
|
||||
const presentation = STATE_PRESENTATION[status.state];
|
||||
const marker = statusMarker(status);
|
||||
return `${presentation.icon} ${presentation.label.padEnd(9)} ${formatDuration(status.elapsedMs)} ${status.label}${marker ? ` ${marker}` : ""}`;
|
||||
}
|
||||
|
||||
function statusMarker(status: SubagentStatus): string | undefined {
|
||||
if (status.error) return `error: ${status.error}`;
|
||||
if (status.resultAvailable) return "result: available";
|
||||
if (status.lastEvent) return `last: ${status.lastEvent}`;
|
||||
if (status.state === "queued") return "waiting";
|
||||
if (status.state === "settling") return "settling";
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function formatDuration(elapsedMs: number): string {
|
||||
const totalSeconds = Math.max(0, Math.round(elapsedMs / 1000));
|
||||
const hours = Math.floor(totalSeconds / 3600);
|
||||
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||
const seconds = totalSeconds % 60;
|
||||
|
||||
if (hours > 0) return `${hours}h${String(minutes).padStart(2, "0")}m${String(seconds).padStart(2, "0")}s`;
|
||||
if (minutes > 0) return `${minutes}m${String(seconds).padStart(2, "0")}s`;
|
||||
return `${seconds}s`;
|
||||
}
|
||||
|
||||
function truncateLine(line: string, width: number): string {
|
||||
if (width <= 0) return "";
|
||||
if (line.length <= width) return line;
|
||||
if (width === 1) return "…";
|
||||
return `${line.slice(0, width - 1)}…`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user