pi-crew 0.1.30 → 0.1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +80 -27
- package/agents/analyst.md +11 -11
- package/agents/critic.md +11 -11
- package/agents/executor.md +11 -11
- package/agents/explorer.md +11 -11
- package/agents/planner.md +11 -11
- package/agents/reviewer.md +11 -11
- package/agents/security-reviewer.md +11 -11
- package/agents/test-engineer.md +11 -11
- package/agents/verifier.md +11 -11
- package/agents/writer.md +11 -11
- package/docs/architecture.md +173 -164
- package/docs/refactor-tasks-phase3.md +394 -394
- package/docs/refactor-tasks-phase4.md +564 -564
- package/docs/refactor-tasks-phase5.md +402 -402
- package/docs/refactor-tasks-phase6.md +662 -662
- package/docs/research-extension-examples.md +297 -0
- package/docs/research-extension-system.md +324 -0
- package/docs/research-optimization-plan.md +548 -0
- package/docs/research-pi-coding-agent.md +357 -0
- package/docs/resource-formats.md +4 -3
- package/docs/runtime-flow.md +148 -148
- package/docs/source-runtime-refactor-map.md +83 -83
- package/docs/usage.md +3 -3
- package/index.ts +6 -6
- package/package.json +1 -1
- package/schema.json +53 -1
- package/skills/git-master/SKILL.md +24 -19
- package/skills/read-only-explorer/SKILL.md +26 -21
- package/skills/safe-bash/SKILL.md +21 -16
- package/skills/task-packet/SKILL.md +28 -23
- package/skills/verify-evidence/SKILL.md +27 -22
- package/src/agents/agent-serializer.ts +34 -34
- package/src/agents/discover-agents.ts +102 -102
- package/src/config/config.ts +14 -1
- package/src/config/defaults.ts +3 -2
- package/src/extension/cross-extension-rpc.ts +82 -82
- package/src/extension/import-index.ts +4 -3
- package/src/extension/management.ts +2 -2
- package/src/extension/project-init.ts +9 -7
- package/src/extension/register.ts +63 -0
- package/src/extension/registration/artifact-cleanup.ts +15 -14
- package/src/extension/registration/commands.ts +208 -208
- package/src/extension/registration/compaction-guard.ts +125 -0
- package/src/extension/registration/subagent-tools.ts +27 -8
- package/src/extension/registration/team-tool.ts +61 -44
- package/src/extension/result-watcher.ts +98 -98
- package/src/extension/run-import.ts +4 -4
- package/src/extension/run-index.ts +14 -14
- package/src/extension/run-maintenance.ts +24 -24
- package/src/extension/team-tool/api.ts +3 -0
- package/src/extension/team-tool/cancel.ts +31 -31
- package/src/extension/team-tool/doctor.ts +179 -178
- package/src/extension/team-tool/inspect.ts +41 -41
- package/src/extension/team-tool/lifecycle-actions.ts +79 -79
- package/src/extension/team-tool/plan.ts +19 -19
- package/src/extension/team-tool/status.ts +73 -73
- package/src/prompt/prompt-runtime.ts +68 -68
- package/src/runtime/agent-control.ts +64 -64
- package/src/runtime/agent-memory.ts +72 -72
- package/src/runtime/agent-observability.ts +113 -113
- package/src/runtime/async-marker.ts +26 -26
- package/src/runtime/background-runner.ts +53 -53
- package/src/runtime/crew-agent-runtime.ts +58 -58
- package/src/runtime/direct-run.ts +35 -35
- package/src/runtime/foreground-control.ts +82 -82
- package/src/runtime/green-contract.ts +46 -46
- package/src/runtime/group-join.ts +88 -88
- package/src/runtime/live-agent-control.ts +78 -78
- package/src/runtime/live-agent-manager.ts +85 -85
- package/src/runtime/live-control-realtime.ts +36 -36
- package/src/runtime/live-session-runtime.ts +299 -299
- package/src/runtime/manifest-cache.ts +212 -214
- package/src/runtime/model-fallback.ts +261 -261
- package/src/runtime/parallel-research.ts +44 -44
- package/src/runtime/parallel-utils.ts +99 -99
- package/src/runtime/pi-json-output.ts +111 -111
- package/src/runtime/pi-spawn.ts +96 -96
- package/src/runtime/policy-engine.ts +78 -78
- package/src/runtime/post-exit-stdio-guard.ts +86 -86
- package/src/runtime/process-status.ts +56 -56
- package/src/runtime/progress-event-coalescer.ts +43 -43
- package/src/runtime/recovery-recipes.ts +74 -74
- package/src/runtime/role-permission.ts +39 -39
- package/src/runtime/session-usage.ts +79 -79
- package/src/runtime/sidechain-output.ts +28 -28
- package/src/runtime/subagent-manager.ts +29 -2
- package/src/runtime/task-display.ts +38 -38
- package/src/runtime/task-output-context.ts +106 -106
- package/src/runtime/task-packet.ts +84 -84
- package/src/runtime/task-runner/live-executor.ts +98 -98
- package/src/runtime/task-runner/progress.ts +111 -111
- package/src/runtime/task-runner/prompt-builder.ts +72 -72
- package/src/runtime/task-runner/result-utils.ts +14 -14
- package/src/runtime/task-runner/state-helpers.ts +22 -22
- package/src/runtime/worker-heartbeat.ts +21 -21
- package/src/runtime/worker-startup.ts +57 -57
- package/src/schema/config-schema.ts +12 -0
- package/src/schema/team-tool-schema.ts +100 -100
- package/src/state/artifact-store.ts +108 -108
- package/src/state/contracts.ts +105 -105
- package/src/state/jsonl-writer.ts +77 -77
- package/src/state/state-store.ts +8 -9
- package/src/state/task-claims.ts +42 -42
- package/src/state/types.ts +180 -180
- package/src/state/usage.ts +29 -29
- package/src/subagents/async-entry.ts +1 -1
- package/src/subagents/index.ts +3 -3
- package/src/subagents/live/control.ts +1 -1
- package/src/subagents/live/manager.ts +1 -1
- package/src/subagents/live/realtime.ts +1 -1
- package/src/subagents/live/session-runtime.ts +1 -1
- package/src/subagents/manager.ts +1 -1
- package/src/subagents/spawn.ts +1 -1
- package/src/teams/discover-teams.ts +2 -2
- package/src/teams/team-serializer.ts +36 -36
- package/src/types/diff.d.ts +18 -0
- package/src/ui/crew-footer.ts +101 -101
- package/src/ui/crew-select-list.ts +111 -111
- package/src/ui/crew-widget.ts +285 -285
- package/src/ui/dynamic-border.ts +25 -25
- package/src/ui/layout-primitives.ts +106 -106
- package/src/ui/loaders.ts +158 -158
- package/src/ui/mascot.ts +441 -441
- package/src/ui/powerbar-publisher.ts +94 -94
- package/src/ui/render-diff.ts +119 -119
- package/src/ui/run-dashboard.ts +372 -372
- package/src/ui/status-colors.ts +54 -54
- package/src/ui/syntax-highlight.ts +116 -116
- package/src/ui/transcript-viewer.ts +302 -302
- package/src/utils/completion-dedupe.ts +63 -63
- package/src/utils/file-coalescer.ts +84 -84
- package/src/utils/frontmatter.ts +36 -36
- package/src/utils/fs-watch.ts +31 -31
- package/src/utils/git.ts +262 -262
- package/src/utils/ids.ts +12 -12
- package/src/utils/names.ts +26 -26
- package/src/utils/paths.ts +34 -7
- package/src/utils/sleep.ts +32 -32
- package/src/utils/timings.ts +31 -31
- package/src/utils/visual.ts +159 -159
- package/src/workflows/discover-workflows.ts +2 -2
- package/src/workflows/validate-workflow.ts +40 -40
- package/src/worktree/branch-freshness.ts +45 -45
- package/src/worktree/cleanup.ts +71 -69
- package/src/worktree/worktree-manager.ts +3 -1
- package/teams/default.team.md +12 -12
- package/teams/fast-fix.team.md +11 -11
- package/teams/implementation.team.md +18 -18
- package/teams/parallel-research.team.md +14 -14
- package/teams/research.team.md +11 -11
- package/teams/review.team.md +12 -12
- package/workflows/default.workflow.md +29 -29
- package/workflows/fast-fix.workflow.md +22 -22
- package/workflows/implementation.workflow.md +38 -38
- package/workflows/parallel-research.workflow.md +46 -46
- package/workflows/research.workflow.md +22 -22
- package/workflows/review.workflow.md +30 -30
package/src/ui/run-dashboard.ts
CHANGED
|
@@ -1,372 +1,372 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import type { TeamRunManifest, TeamTaskState, UsageState } from "../state/types.ts";
|
|
3
|
-
import { readCrewAgents } from "../runtime/crew-agent-records.ts";
|
|
4
|
-
import type { CrewAgentRecord } from "../runtime/crew-agent-runtime.ts";
|
|
5
|
-
import { isDisplayActiveRun, isLikelyOrphanedActiveRun } from "../runtime/process-status.ts";
|
|
6
|
-
import { readJsonFileCoalesced } from "../utils/file-coalescer.ts";
|
|
7
|
-
import type { CrewTheme } from "./theme-adapter.ts";
|
|
8
|
-
import { asCrewTheme, subscribeThemeChange } from "./theme-adapter.ts";
|
|
9
|
-
import { applyStatusColor, iconForStatus, type RunStatus } from "./status-colors.ts";
|
|
10
|
-
import { pad, truncate } from "../utils/visual.ts";
|
|
11
|
-
import { Box, Text } from "./layout-primitives.ts";
|
|
12
|
-
import { DynamicCrewBorder } from "./dynamic-border.ts";
|
|
13
|
-
import { CrewFooter } from "./crew-footer.ts";
|
|
14
|
-
import { aggregateUsage } from "../state/usage.ts";
|
|
15
|
-
|
|
16
|
-
interface DashboardComponent {
|
|
17
|
-
invalidate(): void;
|
|
18
|
-
render(width: number): string[];
|
|
19
|
-
handleInput(data: string): void;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface RunDashboardOptions {
|
|
23
|
-
placement?: "center" | "right";
|
|
24
|
-
showModel?: boolean;
|
|
25
|
-
showTokens?: boolean;
|
|
26
|
-
showTools?: boolean;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export type RunDashboardAction = "status" | "summary" | "artifacts" | "api" | "events" | "agents" | "agent-events" | "agent-output" | "agent-transcript" | "reload";
|
|
30
|
-
export interface RunDashboardSelection {
|
|
31
|
-
runId: string;
|
|
32
|
-
action: RunDashboardAction;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const TASK_READ_TTL_MS = 200;
|
|
36
|
-
|
|
37
|
-
function formatAge(iso: string | undefined): string | undefined {
|
|
38
|
-
if (!iso) return undefined;
|
|
39
|
-
const ms = Math.max(0, Date.now() - new Date(iso).getTime());
|
|
40
|
-
if (!Number.isFinite(ms)) return undefined;
|
|
41
|
-
if (ms < 1000) return "now";
|
|
42
|
-
if (ms < 60_000) return `${Math.floor(ms / 1000)}s`;
|
|
43
|
-
if (ms < 3_600_000) return `${Math.floor(ms / 60_000)}m`;
|
|
44
|
-
return `${Math.floor(ms / 3_600_000)}h`;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function renderLines(lines: string[], width: number): string[] {
|
|
48
|
-
const box = new Box(0, 0);
|
|
49
|
-
for (const line of lines) {
|
|
50
|
-
box.addChild(new Text(line));
|
|
51
|
-
}
|
|
52
|
-
return box.render(width);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function readProgressPreview(run: TeamRunManifest, maxLines = 5): string[] {
|
|
56
|
-
const progress = [...run.artifacts].reverse().find((artifact) => artifact.kind === "progress");
|
|
57
|
-
if (!progress || !fs.existsSync(progress.path)) return ["Progress: (none)"];
|
|
58
|
-
try {
|
|
59
|
-
return ["Progress:", ...fs.readFileSync(progress.path, "utf-8").split(/\r?\n/).filter(Boolean).slice(0, maxLines)];
|
|
60
|
-
} catch (error) {
|
|
61
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
62
|
-
return [`Progress: failed to read (${message})`];
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function formatTokens(usage: UsageState | undefined): string | undefined {
|
|
67
|
-
if (!usage) return undefined;
|
|
68
|
-
const total = (usage.input ?? 0) + (usage.output ?? 0) + (usage.cacheRead ?? 0) + (usage.cacheWrite ?? 0);
|
|
69
|
-
if (!total) return undefined;
|
|
70
|
-
const compact = total >= 1000 ? `${(total / 1000).toFixed(total >= 10_000 ? 0 : 1)}k` : `${total}`;
|
|
71
|
-
const parts = [`tok=${compact}`];
|
|
72
|
-
if (usage.input) parts.push(`in=${usage.input}`);
|
|
73
|
-
if (usage.output) parts.push(`out=${usage.output}`);
|
|
74
|
-
if (usage.cacheRead) parts.push(`cache=${usage.cacheRead}`);
|
|
75
|
-
if (usage.cost) parts.push(`$${usage.cost.toFixed(4)}`);
|
|
76
|
-
return parts.join("/");
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function readRunTasks(run: TeamRunManifest): TeamTaskState[] {
|
|
80
|
-
const parse = () => {
|
|
81
|
-
if (!fs.existsSync(run.tasksPath)) return [];
|
|
82
|
-
const parsed = JSON.parse(fs.readFileSync(run.tasksPath, "utf-8"));
|
|
83
|
-
return Array.isArray(parsed) ? (parsed as TeamTaskState[]) : [];
|
|
84
|
-
};
|
|
85
|
-
try {
|
|
86
|
-
return readJsonFileCoalesced(run.tasksPath, TASK_READ_TTL_MS, parse);
|
|
87
|
-
} catch {
|
|
88
|
-
return [];
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function taskForAgent(tasks: TeamTaskState[], agent: CrewAgentRecord): TeamTaskState | undefined {
|
|
93
|
-
return tasks.find((task) => task.id === agent.taskId);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function modelForTask(task: TeamTaskState | undefined): string | undefined {
|
|
97
|
-
const attempts = task?.modelAttempts;
|
|
98
|
-
if (!attempts?.length) return undefined;
|
|
99
|
-
return attempts.find((attempt) => attempt.success)?.model ?? attempts.at(-1)?.model;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function modelForAgent(agent: CrewAgentRecord, task: TeamTaskState | undefined): string | undefined {
|
|
103
|
-
return modelForTask(task) ?? agent.model;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function usageForAgent(agent: CrewAgentRecord, task: TeamTaskState | undefined): UsageState | undefined {
|
|
107
|
-
return task?.usage ?? agent.usage;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function agentPreviewLine(agent: CrewAgentRecord, task: TeamTaskState | undefined, options: RunDashboardOptions): string {
|
|
111
|
-
const stats = [
|
|
112
|
-
agent.progress?.activityState,
|
|
113
|
-
options.showModel !== false && modelForAgent(agent, task) ? `model=${modelForAgent(agent, task)}` : undefined,
|
|
114
|
-
options.showTokens !== false
|
|
115
|
-
? formatTokens(usageForAgent(agent, task)) ?? (agent.progress?.tokens !== undefined ? `tok=${agent.progress.tokens}` : undefined)
|
|
116
|
-
: undefined,
|
|
117
|
-
options.showTools !== false && agent.progress?.currentTool ? `tool=${agent.progress.currentTool}` : undefined,
|
|
118
|
-
options.showTools !== false && agent.toolUses !== undefined ? `${agent.toolUses} tools` : undefined,
|
|
119
|
-
agent.progress?.turns !== undefined ? `${agent.progress.turns} turns` : undefined,
|
|
120
|
-
agent.progress?.failedTool ? `failedTool=${agent.progress.failedTool}` : undefined,
|
|
121
|
-
agent.startedAt ? `age=${formatAge(agent.completedAt ?? agent.startedAt)}` : undefined,
|
|
122
|
-
].filter((part): part is string => Boolean(part));
|
|
123
|
-
const recent = agent.progress?.recentOutput?.at(-1);
|
|
124
|
-
return `Agent: ${iconForStatus(agent.status)} ${agent.taskId} ${agent.role}->${agent.agent}${stats.length ? ` · ${stats.join(" · ")}` : ""}${recent ? ` ⎿ ${recent}` : ""}`;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function readAgentPreview(run: TeamRunManifest, maxLines = 5, options: RunDashboardOptions = {}): string[] {
|
|
128
|
-
try {
|
|
129
|
-
const agents = readCrewAgents(run);
|
|
130
|
-
const tasks = readRunTasks(run);
|
|
131
|
-
if (!agents.length) return ["Agents: (none)"];
|
|
132
|
-
const totals = tasks.reduce((acc, task) => {
|
|
133
|
-
acc.input += task.usage?.input ?? 0;
|
|
134
|
-
acc.output += task.usage?.output ?? 0;
|
|
135
|
-
acc.cacheRead += task.usage?.cacheRead ?? 0;
|
|
136
|
-
acc.cacheWrite += task.usage?.cacheWrite ?? 0;
|
|
137
|
-
acc.cost += task.usage?.cost ?? 0;
|
|
138
|
-
return acc;
|
|
139
|
-
}, { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0 } as { input: number; output: number; cacheRead: number; cacheWrite: number; cost: number });
|
|
140
|
-
const header = formatTokens(totals) ? `Agents: ${formatTokens(totals)}` : "Agents:";
|
|
141
|
-
return [
|
|
142
|
-
header,
|
|
143
|
-
...agents
|
|
144
|
-
.slice(0, maxLines)
|
|
145
|
-
.map((agent) => agentPreviewLine(agent, taskForAgent(tasks, agent), options)),
|
|
146
|
-
...(agents.length > maxLines ? [`Agents: +${agents.length - maxLines} more`] : []),
|
|
147
|
-
];
|
|
148
|
-
} catch (error) {
|
|
149
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
150
|
-
return [`Agents: failed to read (${message})`];
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function agentsFor(run: TeamRunManifest): CrewAgentRecord[] {
|
|
155
|
-
try {
|
|
156
|
-
return readCrewAgents(run);
|
|
157
|
-
} catch {
|
|
158
|
-
return [];
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function runLabel(run: TeamRunManifest, selected: boolean): string {
|
|
163
|
-
const agents = agentsFor(run);
|
|
164
|
-
const stale = isLikelyOrphanedActiveRun(run, agents);
|
|
165
|
-
const running = agents.find((agent) => agent.status === "running");
|
|
166
|
-
const queued = agents.find((agent) => agent.status === "queued");
|
|
167
|
-
const step = stale ? "orphaned queued run" : running ? `step ${running.taskId}` : queued ? `queued ${queued.taskId}` : `agents ${agents.length}`;
|
|
168
|
-
const status: RunStatus = stale ? "stale" : (run.status as RunStatus);
|
|
169
|
-
const marker = selected ? "›" : " ";
|
|
170
|
-
return `${marker} ${iconForStatus(status)} ${run.runId.slice(-8)} ${status} | ${run.team}/${run.workflow ?? "none"} | ${step} | ${run.goal}`;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function groupedRuns(runs: TeamRunManifest[]): Array<{ label: string; run?: TeamRunManifest }> {
|
|
174
|
-
const active = runs.filter((run) => isDisplayActiveRun(run, agentsFor(run)));
|
|
175
|
-
const recent = runs.filter((run) => !isDisplayActiveRun(run, agentsFor(run)));
|
|
176
|
-
const rows: Array<{ label: string; run?: TeamRunManifest }> = [];
|
|
177
|
-
if (active.length) rows.push({ label: "Active" }, ...active.map((run) => ({ label: run.runId, run })));
|
|
178
|
-
if (recent.length) rows.push({ label: "Recent" }, ...recent.map((run) => ({ label: run.runId, run })));
|
|
179
|
-
return rows;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function selectedRunFromGrouped(runs: TeamRunManifest[], selected: number): TeamRunManifest | undefined {
|
|
183
|
-
return groupedRuns(runs).filter((row) => row.run)[selected]?.run;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function countByStatus(runs: TeamRunManifest[]): string {
|
|
187
|
-
const counts = new Map<RunStatus, number>();
|
|
188
|
-
for (const run of runs) {
|
|
189
|
-
const status: RunStatus = isLikelyOrphanedActiveRun(run, agentsFor(run)) ? "stale" : (run.status as RunStatus);
|
|
190
|
-
counts.set(status, (counts.get(status) ?? 0) + 1);
|
|
191
|
-
}
|
|
192
|
-
return [...counts.entries()].map(([status, count]) => `${status}=${count}`).join(", ") || "none";
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
export class RunDashboard implements DashboardComponent {
|
|
196
|
-
private selected = 0;
|
|
197
|
-
private showFullProgress = false;
|
|
198
|
-
private readonly runs: TeamRunManifest[];
|
|
199
|
-
private readonly done: (selection: RunDashboardSelection | undefined) => void;
|
|
200
|
-
private readonly theme: CrewTheme;
|
|
201
|
-
private readonly options: RunDashboardOptions;
|
|
202
|
-
private cachedWidth = 0;
|
|
203
|
-
private cachedVersion = "";
|
|
204
|
-
private cachedLines: string[] = [];
|
|
205
|
-
private readonly unsubscribeTheme: () => void;
|
|
206
|
-
|
|
207
|
-
constructor(
|
|
208
|
-
runs: TeamRunManifest[],
|
|
209
|
-
done: (selection: RunDashboardSelection | undefined) => void,
|
|
210
|
-
theme: unknown = {},
|
|
211
|
-
options: RunDashboardOptions = {},
|
|
212
|
-
) {
|
|
213
|
-
this.runs = runs;
|
|
214
|
-
this.done = done;
|
|
215
|
-
this.theme = asCrewTheme(theme);
|
|
216
|
-
this.options = options;
|
|
217
|
-
this.unsubscribeTheme = subscribeThemeChange(theme, () => this.invalidate());
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
private buildSignature(): string {
|
|
221
|
-
const statuses = this.runs.map((run) => {
|
|
222
|
-
const stale = isLikelyOrphanedActiveRun(run, agentsFor(run));
|
|
223
|
-
const status: RunStatus = stale ? "stale" : (run.status as RunStatus);
|
|
224
|
-
return `${run.runId}:${run.status}:${run.updatedAt}:${status}`;
|
|
225
|
-
}).join("|");
|
|
226
|
-
return `${this.selected}:${this.showFullProgress ? 1 : 0}:${statuses}`;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
invalidate(): void {
|
|
230
|
-
this.cachedVersion = "";
|
|
231
|
-
this.cachedLines = [];
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
dispose(): void {
|
|
235
|
-
this.unsubscribeTheme();
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
render(width: number): string[] {
|
|
239
|
-
const signature = this.buildSignature();
|
|
240
|
-
if (signature !== this.cachedVersion || this.cachedWidth !== width) {
|
|
241
|
-
const innerWidth = Math.max(20, width - 4);
|
|
242
|
-
const borderWidth = Math.min(innerWidth, Math.max(0, width - 2));
|
|
243
|
-
const fg = (color: Parameters<CrewTheme["fg"]>[0], text: string) => this.theme.fg(color, text);
|
|
244
|
-
const borderFill = (count: number) => new DynamicCrewBorder(this.theme).render(count)[0];
|
|
245
|
-
const border = (left: string, right: string) => `${fg("border", left)}${borderFill(borderWidth)}${fg("border", right)}`;
|
|
246
|
-
|
|
247
|
-
const lines = [
|
|
248
|
-
border("╭", "╮"),
|
|
249
|
-
`│ ${pad(truncate(`${fg("accent", "▐")} ${this.theme.bold(this.options.placement === "right" ? "pi-crew right sidebar (anchored top-right)" : "pi-crew dashboard")}`, innerWidth - 1), innerWidth - 1)}│`,
|
|
250
|
-
`│ ${pad(truncate(`Runs: ${this.runs.length} • ${countByStatus(this.runs)}`, innerWidth - 1), innerWidth - 1)}│`,
|
|
251
|
-
`│ ${pad(truncate(`↑/↓/j/k select • r reload • p progress • s/u/a/i actions • d agents • e/v/o viewers • q close`, innerWidth - 1), innerWidth - 1)}│`,
|
|
252
|
-
border("├", "┤"),
|
|
253
|
-
];
|
|
254
|
-
if (this.runs.length === 0) {
|
|
255
|
-
lines.push(`│ ${pad(truncate("No runs found.", innerWidth - 1), innerWidth - 1)}│`);
|
|
256
|
-
} else {
|
|
257
|
-
const rows = groupedRuns(this.runs).slice(0, 16);
|
|
258
|
-
const selectableRuns = rows.filter((row) => row.run);
|
|
259
|
-
for (const row of rows) {
|
|
260
|
-
if (!row.run) {
|
|
261
|
-
lines.push(`│ ${pad(truncate(fg("accent", row.label), innerWidth - 1), innerWidth - 1)}│`);
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
const index = selectableRuns.findIndex((candidate) => candidate.run?.runId === row.run?.runId);
|
|
265
|
-
const rowStatus = isLikelyOrphanedActiveRun(row.run, agentsFor(row.run)) ? "stale" : (row.run.status as RunStatus);
|
|
266
|
-
const label = runLabel(row.run, index === this.selected);
|
|
267
|
-
lines.push(`│ ${pad(applyStatusColor(this.theme, rowStatus, label), innerWidth - 1)}│`);
|
|
268
|
-
}
|
|
269
|
-
const selectedRun = selectedRunFromGrouped(this.runs, this.selected);
|
|
270
|
-
if (selectedRun) {
|
|
271
|
-
lines.push(border("├", "┤"));
|
|
272
|
-
const details = [
|
|
273
|
-
`Selected: ${selectedRun.runId}`,
|
|
274
|
-
`Status: ${isLikelyOrphanedActiveRun(selectedRun, agentsFor(selectedRun)) ? "stale" : selectedRun.status} | Team: ${selectedRun.team} | Workflow: ${selectedRun.workflow ?? "none"}`,
|
|
275
|
-
`Created: ${selectedRun.createdAt}`,
|
|
276
|
-
`Updated: ${selectedRun.updatedAt}`,
|
|
277
|
-
`Artifacts: ${selectedRun.artifacts.length} | Workspace: ${selectedRun.workspaceMode}`,
|
|
278
|
-
selectedRun.async ? `Async: pid=${selectedRun.async.pid ?? "unknown"} log=${selectedRun.async.logPath}` : "Async: no",
|
|
279
|
-
`Goal: ${selectedRun.goal}`,
|
|
280
|
-
];
|
|
281
|
-
for (const detail of [
|
|
282
|
-
...details,
|
|
283
|
-
...readAgentPreview(selectedRun, this.showFullProgress ? 20 : 8, this.options),
|
|
284
|
-
...readProgressPreview(selectedRun, this.showFullProgress ? 20 : 5),
|
|
285
|
-
]) {
|
|
286
|
-
lines.push(`│ ${pad(truncate(detail, innerWidth - 1), innerWidth - 1)}│`);
|
|
287
|
-
}
|
|
288
|
-
const selectedTasks = readRunTasks(selectedRun);
|
|
289
|
-
const footer = new CrewFooter({
|
|
290
|
-
pwd: selectedRun.cwd,
|
|
291
|
-
runId: selectedRun.runId,
|
|
292
|
-
status: isLikelyOrphanedActiveRun(selectedRun, agentsFor(selectedRun)) ? "stale" : selectedRun.status,
|
|
293
|
-
usage: aggregateUsage(selectedTasks),
|
|
294
|
-
badges: [`team ${selectedRun.team}`, `workflow ${selectedRun.workflow ?? "none"}`, `${selectedRun.artifacts.length} artifacts`, selectedRun.workspaceMode],
|
|
295
|
-
}, this.theme);
|
|
296
|
-
lines.push(border("├", "┤"));
|
|
297
|
-
for (const footerLine of footer.render(innerWidth - 1)) {
|
|
298
|
-
lines.push(`│ ${pad(truncate(footerLine, innerWidth - 1), innerWidth - 1)}│`);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
lines.push(border("╰", "╯"));
|
|
303
|
-
this.cachedLines = renderLines(lines.map((line) => truncate(line, width)), width);
|
|
304
|
-
this.cachedVersion = signature;
|
|
305
|
-
this.cachedWidth = width;
|
|
306
|
-
}
|
|
307
|
-
return this.cachedLines;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
handleInput(data: string): void {
|
|
311
|
-
if (data === "q" || data === "\u001b") {
|
|
312
|
-
this.done(undefined);
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
if (data === "\r" || data === "\n" || data === "s") {
|
|
316
|
-
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
317
|
-
this.done(runId ? { runId, action: "status" } : undefined);
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
|
-
if (data === "u") {
|
|
321
|
-
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
322
|
-
this.done(runId ? { runId, action: "summary" } : undefined);
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
|
-
if (data === "a") {
|
|
326
|
-
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
327
|
-
this.done(runId ? { runId, action: "artifacts" } : undefined);
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
if (data === "i") {
|
|
331
|
-
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
332
|
-
this.done(runId ? { runId, action: "api" } : undefined);
|
|
333
|
-
return;
|
|
334
|
-
}
|
|
335
|
-
if (data === "d") {
|
|
336
|
-
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
337
|
-
this.done(runId ? { runId, action: "agents" } : undefined);
|
|
338
|
-
return;
|
|
339
|
-
}
|
|
340
|
-
if (data === "e") {
|
|
341
|
-
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
342
|
-
this.done(runId ? { runId, action: "agent-events" } : undefined);
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
if (data === "o") {
|
|
346
|
-
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
347
|
-
this.done(runId ? { runId, action: "agent-output" } : undefined);
|
|
348
|
-
return;
|
|
349
|
-
}
|
|
350
|
-
if (data === "v") {
|
|
351
|
-
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
352
|
-
this.done(runId ? { runId, action: "agent-transcript" } : undefined);
|
|
353
|
-
return;
|
|
354
|
-
}
|
|
355
|
-
if (data === "r") {
|
|
356
|
-
this.done({ runId: selectedRunFromGrouped(this.runs, this.selected)?.runId ?? "", action: "reload" });
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
if (data === "p") {
|
|
360
|
-
this.showFullProgress = !this.showFullProgress;
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
if (data === "k" || data === "\u001b[A") {
|
|
364
|
-
this.selected = Math.max(0, this.selected - 1);
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
if (data === "j" || data === "\u001b[B") {
|
|
368
|
-
const selectableCount = groupedRuns(this.runs).filter((row) => row.run).length;
|
|
369
|
-
this.selected = Math.min(Math.max(0, selectableCount - 1), this.selected + 1);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
}
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import type { TeamRunManifest, TeamTaskState, UsageState } from "../state/types.ts";
|
|
3
|
+
import { readCrewAgents } from "../runtime/crew-agent-records.ts";
|
|
4
|
+
import type { CrewAgentRecord } from "../runtime/crew-agent-runtime.ts";
|
|
5
|
+
import { isDisplayActiveRun, isLikelyOrphanedActiveRun } from "../runtime/process-status.ts";
|
|
6
|
+
import { readJsonFileCoalesced } from "../utils/file-coalescer.ts";
|
|
7
|
+
import type { CrewTheme } from "./theme-adapter.ts";
|
|
8
|
+
import { asCrewTheme, subscribeThemeChange } from "./theme-adapter.ts";
|
|
9
|
+
import { applyStatusColor, iconForStatus, type RunStatus } from "./status-colors.ts";
|
|
10
|
+
import { pad, truncate } from "../utils/visual.ts";
|
|
11
|
+
import { Box, Text } from "./layout-primitives.ts";
|
|
12
|
+
import { DynamicCrewBorder } from "./dynamic-border.ts";
|
|
13
|
+
import { CrewFooter } from "./crew-footer.ts";
|
|
14
|
+
import { aggregateUsage } from "../state/usage.ts";
|
|
15
|
+
|
|
16
|
+
interface DashboardComponent {
|
|
17
|
+
invalidate(): void;
|
|
18
|
+
render(width: number): string[];
|
|
19
|
+
handleInput(data: string): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RunDashboardOptions {
|
|
23
|
+
placement?: "center" | "right";
|
|
24
|
+
showModel?: boolean;
|
|
25
|
+
showTokens?: boolean;
|
|
26
|
+
showTools?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type RunDashboardAction = "status" | "summary" | "artifacts" | "api" | "events" | "agents" | "agent-events" | "agent-output" | "agent-transcript" | "reload";
|
|
30
|
+
export interface RunDashboardSelection {
|
|
31
|
+
runId: string;
|
|
32
|
+
action: RunDashboardAction;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const TASK_READ_TTL_MS = 200;
|
|
36
|
+
|
|
37
|
+
function formatAge(iso: string | undefined): string | undefined {
|
|
38
|
+
if (!iso) return undefined;
|
|
39
|
+
const ms = Math.max(0, Date.now() - new Date(iso).getTime());
|
|
40
|
+
if (!Number.isFinite(ms)) return undefined;
|
|
41
|
+
if (ms < 1000) return "now";
|
|
42
|
+
if (ms < 60_000) return `${Math.floor(ms / 1000)}s`;
|
|
43
|
+
if (ms < 3_600_000) return `${Math.floor(ms / 60_000)}m`;
|
|
44
|
+
return `${Math.floor(ms / 3_600_000)}h`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function renderLines(lines: string[], width: number): string[] {
|
|
48
|
+
const box = new Box(0, 0);
|
|
49
|
+
for (const line of lines) {
|
|
50
|
+
box.addChild(new Text(line));
|
|
51
|
+
}
|
|
52
|
+
return box.render(width);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function readProgressPreview(run: TeamRunManifest, maxLines = 5): string[] {
|
|
56
|
+
const progress = [...run.artifacts].reverse().find((artifact) => artifact.kind === "progress");
|
|
57
|
+
if (!progress || !fs.existsSync(progress.path)) return ["Progress: (none)"];
|
|
58
|
+
try {
|
|
59
|
+
return ["Progress:", ...fs.readFileSync(progress.path, "utf-8").split(/\r?\n/).filter(Boolean).slice(0, maxLines)];
|
|
60
|
+
} catch (error) {
|
|
61
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
62
|
+
return [`Progress: failed to read (${message})`];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function formatTokens(usage: UsageState | undefined): string | undefined {
|
|
67
|
+
if (!usage) return undefined;
|
|
68
|
+
const total = (usage.input ?? 0) + (usage.output ?? 0) + (usage.cacheRead ?? 0) + (usage.cacheWrite ?? 0);
|
|
69
|
+
if (!total) return undefined;
|
|
70
|
+
const compact = total >= 1000 ? `${(total / 1000).toFixed(total >= 10_000 ? 0 : 1)}k` : `${total}`;
|
|
71
|
+
const parts = [`tok=${compact}`];
|
|
72
|
+
if (usage.input) parts.push(`in=${usage.input}`);
|
|
73
|
+
if (usage.output) parts.push(`out=${usage.output}`);
|
|
74
|
+
if (usage.cacheRead) parts.push(`cache=${usage.cacheRead}`);
|
|
75
|
+
if (usage.cost) parts.push(`$${usage.cost.toFixed(4)}`);
|
|
76
|
+
return parts.join("/");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function readRunTasks(run: TeamRunManifest): TeamTaskState[] {
|
|
80
|
+
const parse = () => {
|
|
81
|
+
if (!fs.existsSync(run.tasksPath)) return [];
|
|
82
|
+
const parsed = JSON.parse(fs.readFileSync(run.tasksPath, "utf-8"));
|
|
83
|
+
return Array.isArray(parsed) ? (parsed as TeamTaskState[]) : [];
|
|
84
|
+
};
|
|
85
|
+
try {
|
|
86
|
+
return readJsonFileCoalesced(run.tasksPath, TASK_READ_TTL_MS, parse);
|
|
87
|
+
} catch {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function taskForAgent(tasks: TeamTaskState[], agent: CrewAgentRecord): TeamTaskState | undefined {
|
|
93
|
+
return tasks.find((task) => task.id === agent.taskId);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function modelForTask(task: TeamTaskState | undefined): string | undefined {
|
|
97
|
+
const attempts = task?.modelAttempts;
|
|
98
|
+
if (!attempts?.length) return undefined;
|
|
99
|
+
return attempts.find((attempt) => attempt.success)?.model ?? attempts.at(-1)?.model;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function modelForAgent(agent: CrewAgentRecord, task: TeamTaskState | undefined): string | undefined {
|
|
103
|
+
return modelForTask(task) ?? agent.model;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function usageForAgent(agent: CrewAgentRecord, task: TeamTaskState | undefined): UsageState | undefined {
|
|
107
|
+
return task?.usage ?? agent.usage;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function agentPreviewLine(agent: CrewAgentRecord, task: TeamTaskState | undefined, options: RunDashboardOptions): string {
|
|
111
|
+
const stats = [
|
|
112
|
+
agent.progress?.activityState,
|
|
113
|
+
options.showModel !== false && modelForAgent(agent, task) ? `model=${modelForAgent(agent, task)}` : undefined,
|
|
114
|
+
options.showTokens !== false
|
|
115
|
+
? formatTokens(usageForAgent(agent, task)) ?? (agent.progress?.tokens !== undefined ? `tok=${agent.progress.tokens}` : undefined)
|
|
116
|
+
: undefined,
|
|
117
|
+
options.showTools !== false && agent.progress?.currentTool ? `tool=${agent.progress.currentTool}` : undefined,
|
|
118
|
+
options.showTools !== false && agent.toolUses !== undefined ? `${agent.toolUses} tools` : undefined,
|
|
119
|
+
agent.progress?.turns !== undefined ? `${agent.progress.turns} turns` : undefined,
|
|
120
|
+
agent.progress?.failedTool ? `failedTool=${agent.progress.failedTool}` : undefined,
|
|
121
|
+
agent.startedAt ? `age=${formatAge(agent.completedAt ?? agent.startedAt)}` : undefined,
|
|
122
|
+
].filter((part): part is string => Boolean(part));
|
|
123
|
+
const recent = agent.progress?.recentOutput?.at(-1);
|
|
124
|
+
return `Agent: ${iconForStatus(agent.status)} ${agent.taskId} ${agent.role}->${agent.agent}${stats.length ? ` · ${stats.join(" · ")}` : ""}${recent ? ` ⎿ ${recent}` : ""}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function readAgentPreview(run: TeamRunManifest, maxLines = 5, options: RunDashboardOptions = {}): string[] {
|
|
128
|
+
try {
|
|
129
|
+
const agents = readCrewAgents(run);
|
|
130
|
+
const tasks = readRunTasks(run);
|
|
131
|
+
if (!agents.length) return ["Agents: (none)"];
|
|
132
|
+
const totals = tasks.reduce((acc, task) => {
|
|
133
|
+
acc.input += task.usage?.input ?? 0;
|
|
134
|
+
acc.output += task.usage?.output ?? 0;
|
|
135
|
+
acc.cacheRead += task.usage?.cacheRead ?? 0;
|
|
136
|
+
acc.cacheWrite += task.usage?.cacheWrite ?? 0;
|
|
137
|
+
acc.cost += task.usage?.cost ?? 0;
|
|
138
|
+
return acc;
|
|
139
|
+
}, { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0 } as { input: number; output: number; cacheRead: number; cacheWrite: number; cost: number });
|
|
140
|
+
const header = formatTokens(totals) ? `Agents: ${formatTokens(totals)}` : "Agents:";
|
|
141
|
+
return [
|
|
142
|
+
header,
|
|
143
|
+
...agents
|
|
144
|
+
.slice(0, maxLines)
|
|
145
|
+
.map((agent) => agentPreviewLine(agent, taskForAgent(tasks, agent), options)),
|
|
146
|
+
...(agents.length > maxLines ? [`Agents: +${agents.length - maxLines} more`] : []),
|
|
147
|
+
];
|
|
148
|
+
} catch (error) {
|
|
149
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
150
|
+
return [`Agents: failed to read (${message})`];
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function agentsFor(run: TeamRunManifest): CrewAgentRecord[] {
|
|
155
|
+
try {
|
|
156
|
+
return readCrewAgents(run);
|
|
157
|
+
} catch {
|
|
158
|
+
return [];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function runLabel(run: TeamRunManifest, selected: boolean): string {
|
|
163
|
+
const agents = agentsFor(run);
|
|
164
|
+
const stale = isLikelyOrphanedActiveRun(run, agents);
|
|
165
|
+
const running = agents.find((agent) => agent.status === "running");
|
|
166
|
+
const queued = agents.find((agent) => agent.status === "queued");
|
|
167
|
+
const step = stale ? "orphaned queued run" : running ? `step ${running.taskId}` : queued ? `queued ${queued.taskId}` : `agents ${agents.length}`;
|
|
168
|
+
const status: RunStatus = stale ? "stale" : (run.status as RunStatus);
|
|
169
|
+
const marker = selected ? "›" : " ";
|
|
170
|
+
return `${marker} ${iconForStatus(status)} ${run.runId.slice(-8)} ${status} | ${run.team}/${run.workflow ?? "none"} | ${step} | ${run.goal}`;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function groupedRuns(runs: TeamRunManifest[]): Array<{ label: string; run?: TeamRunManifest }> {
|
|
174
|
+
const active = runs.filter((run) => isDisplayActiveRun(run, agentsFor(run)));
|
|
175
|
+
const recent = runs.filter((run) => !isDisplayActiveRun(run, agentsFor(run)));
|
|
176
|
+
const rows: Array<{ label: string; run?: TeamRunManifest }> = [];
|
|
177
|
+
if (active.length) rows.push({ label: "Active" }, ...active.map((run) => ({ label: run.runId, run })));
|
|
178
|
+
if (recent.length) rows.push({ label: "Recent" }, ...recent.map((run) => ({ label: run.runId, run })));
|
|
179
|
+
return rows;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function selectedRunFromGrouped(runs: TeamRunManifest[], selected: number): TeamRunManifest | undefined {
|
|
183
|
+
return groupedRuns(runs).filter((row) => row.run)[selected]?.run;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function countByStatus(runs: TeamRunManifest[]): string {
|
|
187
|
+
const counts = new Map<RunStatus, number>();
|
|
188
|
+
for (const run of runs) {
|
|
189
|
+
const status: RunStatus = isLikelyOrphanedActiveRun(run, agentsFor(run)) ? "stale" : (run.status as RunStatus);
|
|
190
|
+
counts.set(status, (counts.get(status) ?? 0) + 1);
|
|
191
|
+
}
|
|
192
|
+
return [...counts.entries()].map(([status, count]) => `${status}=${count}`).join(", ") || "none";
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export class RunDashboard implements DashboardComponent {
|
|
196
|
+
private selected = 0;
|
|
197
|
+
private showFullProgress = false;
|
|
198
|
+
private readonly runs: TeamRunManifest[];
|
|
199
|
+
private readonly done: (selection: RunDashboardSelection | undefined) => void;
|
|
200
|
+
private readonly theme: CrewTheme;
|
|
201
|
+
private readonly options: RunDashboardOptions;
|
|
202
|
+
private cachedWidth = 0;
|
|
203
|
+
private cachedVersion = "";
|
|
204
|
+
private cachedLines: string[] = [];
|
|
205
|
+
private readonly unsubscribeTheme: () => void;
|
|
206
|
+
|
|
207
|
+
constructor(
|
|
208
|
+
runs: TeamRunManifest[],
|
|
209
|
+
done: (selection: RunDashboardSelection | undefined) => void,
|
|
210
|
+
theme: unknown = {},
|
|
211
|
+
options: RunDashboardOptions = {},
|
|
212
|
+
) {
|
|
213
|
+
this.runs = runs;
|
|
214
|
+
this.done = done;
|
|
215
|
+
this.theme = asCrewTheme(theme);
|
|
216
|
+
this.options = options;
|
|
217
|
+
this.unsubscribeTheme = subscribeThemeChange(theme, () => this.invalidate());
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private buildSignature(): string {
|
|
221
|
+
const statuses = this.runs.map((run) => {
|
|
222
|
+
const stale = isLikelyOrphanedActiveRun(run, agentsFor(run));
|
|
223
|
+
const status: RunStatus = stale ? "stale" : (run.status as RunStatus);
|
|
224
|
+
return `${run.runId}:${run.status}:${run.updatedAt}:${status}`;
|
|
225
|
+
}).join("|");
|
|
226
|
+
return `${this.selected}:${this.showFullProgress ? 1 : 0}:${statuses}`;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
invalidate(): void {
|
|
230
|
+
this.cachedVersion = "";
|
|
231
|
+
this.cachedLines = [];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
dispose(): void {
|
|
235
|
+
this.unsubscribeTheme();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
render(width: number): string[] {
|
|
239
|
+
const signature = this.buildSignature();
|
|
240
|
+
if (signature !== this.cachedVersion || this.cachedWidth !== width) {
|
|
241
|
+
const innerWidth = Math.max(20, width - 4);
|
|
242
|
+
const borderWidth = Math.min(innerWidth, Math.max(0, width - 2));
|
|
243
|
+
const fg = (color: Parameters<CrewTheme["fg"]>[0], text: string) => this.theme.fg(color, text);
|
|
244
|
+
const borderFill = (count: number) => new DynamicCrewBorder(this.theme).render(count)[0];
|
|
245
|
+
const border = (left: string, right: string) => `${fg("border", left)}${borderFill(borderWidth)}${fg("border", right)}`;
|
|
246
|
+
|
|
247
|
+
const lines = [
|
|
248
|
+
border("╭", "╮"),
|
|
249
|
+
`│ ${pad(truncate(`${fg("accent", "▐")} ${this.theme.bold(this.options.placement === "right" ? "pi-crew right sidebar (anchored top-right)" : "pi-crew dashboard")}`, innerWidth - 1), innerWidth - 1)}│`,
|
|
250
|
+
`│ ${pad(truncate(`Runs: ${this.runs.length} • ${countByStatus(this.runs)}`, innerWidth - 1), innerWidth - 1)}│`,
|
|
251
|
+
`│ ${pad(truncate(`↑/↓/j/k select • r reload • p progress • s/u/a/i actions • d agents • e/v/o viewers • q close`, innerWidth - 1), innerWidth - 1)}│`,
|
|
252
|
+
border("├", "┤"),
|
|
253
|
+
];
|
|
254
|
+
if (this.runs.length === 0) {
|
|
255
|
+
lines.push(`│ ${pad(truncate("No runs found.", innerWidth - 1), innerWidth - 1)}│`);
|
|
256
|
+
} else {
|
|
257
|
+
const rows = groupedRuns(this.runs).slice(0, 16);
|
|
258
|
+
const selectableRuns = rows.filter((row) => row.run);
|
|
259
|
+
for (const row of rows) {
|
|
260
|
+
if (!row.run) {
|
|
261
|
+
lines.push(`│ ${pad(truncate(fg("accent", row.label), innerWidth - 1), innerWidth - 1)}│`);
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
const index = selectableRuns.findIndex((candidate) => candidate.run?.runId === row.run?.runId);
|
|
265
|
+
const rowStatus = isLikelyOrphanedActiveRun(row.run, agentsFor(row.run)) ? "stale" : (row.run.status as RunStatus);
|
|
266
|
+
const label = runLabel(row.run, index === this.selected);
|
|
267
|
+
lines.push(`│ ${pad(applyStatusColor(this.theme, rowStatus, label), innerWidth - 1)}│`);
|
|
268
|
+
}
|
|
269
|
+
const selectedRun = selectedRunFromGrouped(this.runs, this.selected);
|
|
270
|
+
if (selectedRun) {
|
|
271
|
+
lines.push(border("├", "┤"));
|
|
272
|
+
const details = [
|
|
273
|
+
`Selected: ${selectedRun.runId}`,
|
|
274
|
+
`Status: ${isLikelyOrphanedActiveRun(selectedRun, agentsFor(selectedRun)) ? "stale" : selectedRun.status} | Team: ${selectedRun.team} | Workflow: ${selectedRun.workflow ?? "none"}`,
|
|
275
|
+
`Created: ${selectedRun.createdAt}`,
|
|
276
|
+
`Updated: ${selectedRun.updatedAt}`,
|
|
277
|
+
`Artifacts: ${selectedRun.artifacts.length} | Workspace: ${selectedRun.workspaceMode}`,
|
|
278
|
+
selectedRun.async ? `Async: pid=${selectedRun.async.pid ?? "unknown"} log=${selectedRun.async.logPath}` : "Async: no",
|
|
279
|
+
`Goal: ${selectedRun.goal}`,
|
|
280
|
+
];
|
|
281
|
+
for (const detail of [
|
|
282
|
+
...details,
|
|
283
|
+
...readAgentPreview(selectedRun, this.showFullProgress ? 20 : 8, this.options),
|
|
284
|
+
...readProgressPreview(selectedRun, this.showFullProgress ? 20 : 5),
|
|
285
|
+
]) {
|
|
286
|
+
lines.push(`│ ${pad(truncate(detail, innerWidth - 1), innerWidth - 1)}│`);
|
|
287
|
+
}
|
|
288
|
+
const selectedTasks = readRunTasks(selectedRun);
|
|
289
|
+
const footer = new CrewFooter({
|
|
290
|
+
pwd: selectedRun.cwd,
|
|
291
|
+
runId: selectedRun.runId,
|
|
292
|
+
status: isLikelyOrphanedActiveRun(selectedRun, agentsFor(selectedRun)) ? "stale" : selectedRun.status,
|
|
293
|
+
usage: aggregateUsage(selectedTasks),
|
|
294
|
+
badges: [`team ${selectedRun.team}`, `workflow ${selectedRun.workflow ?? "none"}`, `${selectedRun.artifacts.length} artifacts`, selectedRun.workspaceMode],
|
|
295
|
+
}, this.theme);
|
|
296
|
+
lines.push(border("├", "┤"));
|
|
297
|
+
for (const footerLine of footer.render(innerWidth - 1)) {
|
|
298
|
+
lines.push(`│ ${pad(truncate(footerLine, innerWidth - 1), innerWidth - 1)}│`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
lines.push(border("╰", "╯"));
|
|
303
|
+
this.cachedLines = renderLines(lines.map((line) => truncate(line, width)), width);
|
|
304
|
+
this.cachedVersion = signature;
|
|
305
|
+
this.cachedWidth = width;
|
|
306
|
+
}
|
|
307
|
+
return this.cachedLines;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
handleInput(data: string): void {
|
|
311
|
+
if (data === "q" || data === "\u001b") {
|
|
312
|
+
this.done(undefined);
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
if (data === "\r" || data === "\n" || data === "s") {
|
|
316
|
+
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
317
|
+
this.done(runId ? { runId, action: "status" } : undefined);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
if (data === "u") {
|
|
321
|
+
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
322
|
+
this.done(runId ? { runId, action: "summary" } : undefined);
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
if (data === "a") {
|
|
326
|
+
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
327
|
+
this.done(runId ? { runId, action: "artifacts" } : undefined);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
if (data === "i") {
|
|
331
|
+
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
332
|
+
this.done(runId ? { runId, action: "api" } : undefined);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
if (data === "d") {
|
|
336
|
+
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
337
|
+
this.done(runId ? { runId, action: "agents" } : undefined);
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (data === "e") {
|
|
341
|
+
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
342
|
+
this.done(runId ? { runId, action: "agent-events" } : undefined);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
if (data === "o") {
|
|
346
|
+
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
347
|
+
this.done(runId ? { runId, action: "agent-output" } : undefined);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (data === "v") {
|
|
351
|
+
const runId = selectedRunFromGrouped(this.runs, this.selected)?.runId;
|
|
352
|
+
this.done(runId ? { runId, action: "agent-transcript" } : undefined);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (data === "r") {
|
|
356
|
+
this.done({ runId: selectedRunFromGrouped(this.runs, this.selected)?.runId ?? "", action: "reload" });
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (data === "p") {
|
|
360
|
+
this.showFullProgress = !this.showFullProgress;
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
if (data === "k" || data === "\u001b[A") {
|
|
364
|
+
this.selected = Math.max(0, this.selected - 1);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (data === "j" || data === "\u001b[B") {
|
|
368
|
+
const selectableCount = groupedRuns(this.runs).filter((row) => row.run).length;
|
|
369
|
+
this.selected = Math.min(Math.max(0, selectableCount - 1), this.selected + 1);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|