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
|
@@ -1,302 +1,302 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import type { TeamRunManifest } from "../state/types.ts";
|
|
3
|
-
import { agentOutputPath, readCrewAgents } from "../runtime/crew-agent-records.ts";
|
|
4
|
-
import type { CrewTheme } from "./theme-adapter.ts";
|
|
5
|
-
import { asCrewTheme, subscribeThemeChange } from "./theme-adapter.ts";
|
|
6
|
-
import { renderDiff } from "./render-diff.ts";
|
|
7
|
-
import { highlightCode, highlightJson } from "./syntax-highlight.ts";
|
|
8
|
-
import { pad, truncate, truncateToVisualLines } from "../utils/visual.ts";
|
|
9
|
-
import { colorForStatus, iconForStatus, type RunStatus } from "./status-colors.ts";
|
|
10
|
-
|
|
11
|
-
type Component = { invalidate(): void; render(width: number): string[]; handleInput(data: string): void };
|
|
12
|
-
|
|
13
|
-
type TranscriptTheme = CrewTheme;
|
|
14
|
-
|
|
15
|
-
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
16
|
-
return value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : undefined;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function textFromContent(content: unknown): string {
|
|
20
|
-
if (typeof content === "string") return content;
|
|
21
|
-
if (!Array.isArray(content)) return "";
|
|
22
|
-
return content
|
|
23
|
-
.map((part) => {
|
|
24
|
-
const obj = asRecord(part);
|
|
25
|
-
if (!obj) return "";
|
|
26
|
-
if (typeof obj.text === "string") return obj.text;
|
|
27
|
-
if (typeof obj.content === "string") return obj.content;
|
|
28
|
-
if (typeof obj.name === "string") return `[tool:${obj.name}]`;
|
|
29
|
-
return "";
|
|
30
|
-
})
|
|
31
|
-
.filter(Boolean)
|
|
32
|
-
.join("\n");
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function isLikelyDiff(text: string): boolean {
|
|
36
|
-
const lines = text.split(/\r?\n/);
|
|
37
|
-
const matched = lines.filter((line) => /^[-+\s]\d+\s/.test(line)).length;
|
|
38
|
-
return matched >= 2 && (text.includes("-") || text.includes("+"));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function highlightCodeBlocks(input: string, theme: TranscriptTheme): string[] {
|
|
42
|
-
const codeBlockRegex = /```(\S+)?\n([\s\S]*?)```/g;
|
|
43
|
-
const lines: string[] = [];
|
|
44
|
-
let index = 0;
|
|
45
|
-
let match: RegExpExecArray | null;
|
|
46
|
-
while ((match = codeBlockRegex.exec(input)) !== null) {
|
|
47
|
-
if (match.index > index) lines.push(...input.slice(index, match.index).split(/\r?\n/));
|
|
48
|
-
const lang = match[1]?.trim();
|
|
49
|
-
const block = match[2] ?? "";
|
|
50
|
-
const highlighted = highlightCode(block, lang, theme);
|
|
51
|
-
if (highlighted) {
|
|
52
|
-
lines.push(...highlighted.split(/\r?\n/));
|
|
53
|
-
}
|
|
54
|
-
index = match.index + match[0].length;
|
|
55
|
-
}
|
|
56
|
-
if (index < input.length) lines.push(...input.slice(index).split(/\r?\n/));
|
|
57
|
-
return lines.filter((line) => line.length > 0);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function formatTranscriptEvent(event: unknown, themeLike: unknown = undefined): string[] {
|
|
61
|
-
const theme = asCrewTheme(themeLike);
|
|
62
|
-
const obj = asRecord(event);
|
|
63
|
-
if (!obj) return [String(event)];
|
|
64
|
-
const type = typeof obj.type === "string" ? obj.type : undefined;
|
|
65
|
-
const toolName = typeof obj.toolName === "string" ? obj.toolName : typeof obj.name === "string" ? obj.name : undefined;
|
|
66
|
-
const content = textFromContent(obj.content);
|
|
67
|
-
if (type && /tool/i.test(type)) {
|
|
68
|
-
const result = asRecord(obj.result);
|
|
69
|
-
const isError = obj.isError === true || result?.isError === true;
|
|
70
|
-
const isPartial = obj.isPartial === true;
|
|
71
|
-
const status: RunStatus = isError ? "failed" : isPartial ? "running" : "completed";
|
|
72
|
-
const header = theme.fg(colorForStatus(status), `${iconForStatus(status, { runningGlyph: "⋯" })} [Tool${toolName ? `: ${toolName}` : ""}] ${type}`);
|
|
73
|
-
const text = (content || (typeof obj.text === "string" ? obj.text : typeof obj.result === "string" ? obj.result : "")).trim();
|
|
74
|
-
if (!text) return [header, "(no output)"];
|
|
75
|
-
if (isLikelyDiff(text)) {
|
|
76
|
-
return [header, renderDiff(text, { theme })];
|
|
77
|
-
}
|
|
78
|
-
if (text.startsWith("{") && text.endsWith("}")) {
|
|
79
|
-
return [header, ...highlightJson(text, theme).split(/\r?\n/).filter(Boolean)];
|
|
80
|
-
}
|
|
81
|
-
if (text.includes("```") && text.includes("```")) {
|
|
82
|
-
return [header, ...highlightCodeBlocks(text, theme)];
|
|
83
|
-
}
|
|
84
|
-
return [header, ...text.split(/\r?\n/).filter(Boolean).map((line) => theme.fg("muted", line))];
|
|
85
|
-
}
|
|
86
|
-
const message = asRecord(obj.message);
|
|
87
|
-
if (message) {
|
|
88
|
-
const role = typeof message.role === "string" ? message.role : "message";
|
|
89
|
-
const text = textFromContent(message.content);
|
|
90
|
-
if (text.trim()) {
|
|
91
|
-
const label = role === "assistant" ? "Assistant" : role === "user" ? "User" : role;
|
|
92
|
-
const header = `[${label}]:`;
|
|
93
|
-
const lines = text.split(/\r?\n/);
|
|
94
|
-
if (text.includes("```") && text.includes("```")) {
|
|
95
|
-
return [theme.fg("accent", header), ...highlightCodeBlocks(text, theme)];
|
|
96
|
-
}
|
|
97
|
-
if (lines.length > 1) {
|
|
98
|
-
const block = lines
|
|
99
|
-
.map((line) => (role === "assistant" ? theme.bold(line) : line))
|
|
100
|
-
.join("\n");
|
|
101
|
-
return [theme.fg("accent", header), ...block.split(/\r?\n/).filter(Boolean)];
|
|
102
|
-
}
|
|
103
|
-
return [theme.fg("accent", header), ...lines.filter(Boolean)];
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
if (type) {
|
|
107
|
-
const text = content || (typeof obj.text === "string" ? obj.text : "");
|
|
108
|
-
return text.trim() ? [theme.fg("muted", `[${type}]: ${text.trim()}`)] : [`[${type}]`];
|
|
109
|
-
}
|
|
110
|
-
return [JSON.stringify(event)];
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function formatTranscriptText(text: string, themeLike: unknown = undefined): string[] {
|
|
114
|
-
const lines: string[] = [];
|
|
115
|
-
for (const raw of text.split(/\r?\n/).filter(Boolean)) {
|
|
116
|
-
try {
|
|
117
|
-
const parsed = JSON.parse(raw);
|
|
118
|
-
lines.push(...formatTranscriptEvent(parsed, themeLike));
|
|
119
|
-
} catch {
|
|
120
|
-
lines.push(raw);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
return lines.length ? lines : ["(no transcript content)"];
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export function readRunTranscript(manifest: TeamRunManifest, taskId?: string): { title: string; path: string; lines: string[] } {
|
|
127
|
-
const agents = readCrewAgents(manifest);
|
|
128
|
-
const agent = taskId ? agents.find((item) => item.taskId === taskId || item.id === taskId) : agents.find((item) => item.transcriptPath) ?? agents[0];
|
|
129
|
-
const selectedTaskId = agent?.taskId ?? taskId ?? "unknown";
|
|
130
|
-
const transcriptPath = agent?.transcriptPath && fs.existsSync(agent.transcriptPath) ? agent.transcriptPath : agentOutputPath(manifest, selectedTaskId);
|
|
131
|
-
const text = fs.existsSync(transcriptPath) ? fs.readFileSync(transcriptPath, "utf-8") : "";
|
|
132
|
-
return { title: `${manifest.runId}:${selectedTaskId}`, path: transcriptPath, lines: formatTranscriptText(text) };
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
interface ViewerState {
|
|
136
|
-
theme: TranscriptTheme;
|
|
137
|
-
autoScroll: boolean;
|
|
138
|
-
lastHeight: number;
|
|
139
|
-
scroll: number;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function renderViewerBase(
|
|
143
|
-
state: ViewerState,
|
|
144
|
-
width: number,
|
|
145
|
-
lines: string[],
|
|
146
|
-
title: string,
|
|
147
|
-
subtitle: string,
|
|
148
|
-
): string[] {
|
|
149
|
-
const inner = Math.max(20, width - 4);
|
|
150
|
-
const bodyText = lines.join("\n");
|
|
151
|
-
const { visualLines, skippedCount } = truncateToVisualLines(bodyText, state.lastHeight, inner);
|
|
152
|
-
const maxScroll = Math.max(0, visualLines.length - state.lastHeight);
|
|
153
|
-
if (state.autoScroll) state.scroll = maxScroll;
|
|
154
|
-
state.scroll = Math.min(state.scroll, maxScroll);
|
|
155
|
-
const visible = visualLines.slice(state.scroll, state.scroll + state.lastHeight);
|
|
156
|
-
const statusLine = `${visualLines.length} lines · ${visualLines.length ? Math.round(((state.scroll + visible.length) / visualLines.length) * 100) : 100}% · auto-scroll ${state.autoScroll ? "on" : "off"}`;
|
|
157
|
-
const fg = (color: Parameters<TranscriptTheme["fg"]>[0], text: string) => state.theme.fg(color, text);
|
|
158
|
-
const row = (text: string) => `${fg("border", "│")} ${pad(truncate(text, inner), inner)} ${fg("border", "│")}`;
|
|
159
|
-
const linesOut: string[] = [
|
|
160
|
-
fg("border", `╭${"─".repeat(inner + 2)}╮`),
|
|
161
|
-
row(`${fg("accent", title)} ${fg("dim", subtitle)}`),
|
|
162
|
-
row(fg("dim", "j/k scroll · PgUp/PgDn · g/G top/bottom · q close")),
|
|
163
|
-
fg("border", `├${"─".repeat(inner + 2)}┤`),
|
|
164
|
-
...visible.map(row),
|
|
165
|
-
fg("border", `├${"─".repeat(inner + 2)}┤`),
|
|
166
|
-
row(fg("dim", statusLine)),
|
|
167
|
-
fg("border", `╰${"─".repeat(inner + 2)}╯`),
|
|
168
|
-
];
|
|
169
|
-
if (skippedCount > 0) {
|
|
170
|
-
linesOut.splice(linesOut.length - 1, 0, row(fg("muted", `… (${skippedCount} lines truncated above`)));
|
|
171
|
-
}
|
|
172
|
-
return linesOut.map((line) => truncate(line, width));
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
export class DurableTextViewer implements Component {
|
|
177
|
-
private scroll = 0;
|
|
178
|
-
private lastHeight = 16;
|
|
179
|
-
private autoScroll = true;
|
|
180
|
-
private title: string;
|
|
181
|
-
private subtitle: string;
|
|
182
|
-
private lines: string[];
|
|
183
|
-
private theme: TranscriptTheme;
|
|
184
|
-
private done: (result: undefined) => void;
|
|
185
|
-
private readonly unsubscribeTheme: () => void;
|
|
186
|
-
|
|
187
|
-
constructor(title: string, subtitle: string, lines: string[], theme: unknown, done: (result: undefined) => void) {
|
|
188
|
-
this.title = title;
|
|
189
|
-
this.subtitle = subtitle;
|
|
190
|
-
this.lines = lines.length ? lines : ["(empty)"];
|
|
191
|
-
this.theme = asCrewTheme(theme);
|
|
192
|
-
this.done = done;
|
|
193
|
-
this.unsubscribeTheme = subscribeThemeChange(theme, () => this.invalidate());
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
invalidate(): void {}
|
|
197
|
-
|
|
198
|
-
dispose(): void {
|
|
199
|
-
this.unsubscribeTheme();
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
handleInput(data: string): void {
|
|
203
|
-
if (data === "q" || data === "\u001b") {
|
|
204
|
-
this.done(undefined);
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
const maxScroll = Math.max(0, this.lines.length - this.lastHeight);
|
|
208
|
-
if (data === "k" || data === "\u001b[A") {
|
|
209
|
-
this.scroll = Math.max(0, this.scroll - 1);
|
|
210
|
-
this.autoScroll = false;
|
|
211
|
-
} else if (data === "j" || data === "\u001b[B") {
|
|
212
|
-
this.scroll = Math.min(maxScroll, this.scroll + 1);
|
|
213
|
-
this.autoScroll = this.scroll >= maxScroll;
|
|
214
|
-
} else if (data === "\u001b[5~") {
|
|
215
|
-
this.scroll = Math.max(0, this.scroll - this.lastHeight);
|
|
216
|
-
this.autoScroll = false;
|
|
217
|
-
} else if (data === "\u001b[6~") {
|
|
218
|
-
this.scroll = Math.min(maxScroll, this.scroll + this.lastHeight);
|
|
219
|
-
this.autoScroll = this.scroll >= maxScroll;
|
|
220
|
-
} else if (data === "g" || data === "\u001b[H") {
|
|
221
|
-
this.scroll = 0;
|
|
222
|
-
this.autoScroll = false;
|
|
223
|
-
} else if (data === "G" || data === "\u001b[F") {
|
|
224
|
-
this.scroll = maxScroll;
|
|
225
|
-
this.autoScroll = true;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
render(width: number): string[] {
|
|
230
|
-
return renderViewerBase(
|
|
231
|
-
{ theme: this.theme, autoScroll: this.autoScroll, lastHeight: this.lastHeight, scroll: this.scroll },
|
|
232
|
-
width,
|
|
233
|
-
this.lines,
|
|
234
|
-
this.title,
|
|
235
|
-
this.subtitle,
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
export class DurableTranscriptViewer implements Component {
|
|
241
|
-
private scroll = 0;
|
|
242
|
-
private lastHeight = 16;
|
|
243
|
-
private autoScroll = true;
|
|
244
|
-
private manifest: TeamRunManifest;
|
|
245
|
-
private theme: TranscriptTheme;
|
|
246
|
-
private done: (result: undefined) => void;
|
|
247
|
-
private taskId?: string;
|
|
248
|
-
private readonly unsubscribeTheme: () => void;
|
|
249
|
-
|
|
250
|
-
constructor(manifest: TeamRunManifest, theme: unknown, done: (result: undefined) => void, taskId?: string) {
|
|
251
|
-
this.manifest = manifest;
|
|
252
|
-
this.theme = asCrewTheme(theme);
|
|
253
|
-
this.done = done;
|
|
254
|
-
this.taskId = taskId;
|
|
255
|
-
this.unsubscribeTheme = subscribeThemeChange(theme, () => this.invalidate());
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
invalidate(): void {}
|
|
259
|
-
|
|
260
|
-
dispose(): void {
|
|
261
|
-
this.unsubscribeTheme();
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
handleInput(data: string): void {
|
|
265
|
-
if (data === "q" || data === "\u001b") {
|
|
266
|
-
this.done(undefined);
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
const content = readRunTranscript(this.manifest, this.taskId).lines;
|
|
270
|
-
const maxScroll = Math.max(0, content.length - this.lastHeight);
|
|
271
|
-
if (data === "k" || data === "\u001b[A") {
|
|
272
|
-
this.scroll = Math.max(0, this.scroll - 1);
|
|
273
|
-
this.autoScroll = false;
|
|
274
|
-
} else if (data === "j" || data === "\u001b[B") {
|
|
275
|
-
this.scroll = Math.min(maxScroll, this.scroll + 1);
|
|
276
|
-
this.autoScroll = this.scroll >= maxScroll;
|
|
277
|
-
} else if (data === "\u001b[5~") {
|
|
278
|
-
this.scroll = Math.max(0, this.scroll - this.lastHeight);
|
|
279
|
-
this.autoScroll = false;
|
|
280
|
-
} else if (data === "\u001b[6~") {
|
|
281
|
-
this.scroll = Math.min(maxScroll, this.scroll + this.lastHeight);
|
|
282
|
-
this.autoScroll = this.scroll >= maxScroll;
|
|
283
|
-
} else if (data === "g" || data === "\u001b[H") {
|
|
284
|
-
this.scroll = 0;
|
|
285
|
-
this.autoScroll = false;
|
|
286
|
-
} else if (data === "G" || data === "\u001b[F") {
|
|
287
|
-
this.scroll = maxScroll;
|
|
288
|
-
this.autoScroll = true;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
render(width: number): string[] {
|
|
293
|
-
const data = readRunTranscript(this.manifest, this.taskId);
|
|
294
|
-
return renderViewerBase(
|
|
295
|
-
{ theme: this.theme, autoScroll: this.autoScroll, lastHeight: this.lastHeight, scroll: this.scroll },
|
|
296
|
-
width,
|
|
297
|
-
data.lines,
|
|
298
|
-
"pi-crew transcript",
|
|
299
|
-
data.title,
|
|
300
|
-
);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import type { TeamRunManifest } from "../state/types.ts";
|
|
3
|
+
import { agentOutputPath, readCrewAgents } from "../runtime/crew-agent-records.ts";
|
|
4
|
+
import type { CrewTheme } from "./theme-adapter.ts";
|
|
5
|
+
import { asCrewTheme, subscribeThemeChange } from "./theme-adapter.ts";
|
|
6
|
+
import { renderDiff } from "./render-diff.ts";
|
|
7
|
+
import { highlightCode, highlightJson } from "./syntax-highlight.ts";
|
|
8
|
+
import { pad, truncate, truncateToVisualLines } from "../utils/visual.ts";
|
|
9
|
+
import { colorForStatus, iconForStatus, type RunStatus } from "./status-colors.ts";
|
|
10
|
+
|
|
11
|
+
type Component = { invalidate(): void; render(width: number): string[]; handleInput(data: string): void };
|
|
12
|
+
|
|
13
|
+
type TranscriptTheme = CrewTheme;
|
|
14
|
+
|
|
15
|
+
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
16
|
+
return value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function textFromContent(content: unknown): string {
|
|
20
|
+
if (typeof content === "string") return content;
|
|
21
|
+
if (!Array.isArray(content)) return "";
|
|
22
|
+
return content
|
|
23
|
+
.map((part) => {
|
|
24
|
+
const obj = asRecord(part);
|
|
25
|
+
if (!obj) return "";
|
|
26
|
+
if (typeof obj.text === "string") return obj.text;
|
|
27
|
+
if (typeof obj.content === "string") return obj.content;
|
|
28
|
+
if (typeof obj.name === "string") return `[tool:${obj.name}]`;
|
|
29
|
+
return "";
|
|
30
|
+
})
|
|
31
|
+
.filter(Boolean)
|
|
32
|
+
.join("\n");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isLikelyDiff(text: string): boolean {
|
|
36
|
+
const lines = text.split(/\r?\n/);
|
|
37
|
+
const matched = lines.filter((line) => /^[-+\s]\d+\s/.test(line)).length;
|
|
38
|
+
return matched >= 2 && (text.includes("-") || text.includes("+"));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function highlightCodeBlocks(input: string, theme: TranscriptTheme): string[] {
|
|
42
|
+
const codeBlockRegex = /```(\S+)?\n([\s\S]*?)```/g;
|
|
43
|
+
const lines: string[] = [];
|
|
44
|
+
let index = 0;
|
|
45
|
+
let match: RegExpExecArray | null;
|
|
46
|
+
while ((match = codeBlockRegex.exec(input)) !== null) {
|
|
47
|
+
if (match.index > index) lines.push(...input.slice(index, match.index).split(/\r?\n/));
|
|
48
|
+
const lang = match[1]?.trim();
|
|
49
|
+
const block = match[2] ?? "";
|
|
50
|
+
const highlighted = highlightCode(block, lang, theme);
|
|
51
|
+
if (highlighted) {
|
|
52
|
+
lines.push(...highlighted.split(/\r?\n/));
|
|
53
|
+
}
|
|
54
|
+
index = match.index + match[0].length;
|
|
55
|
+
}
|
|
56
|
+
if (index < input.length) lines.push(...input.slice(index).split(/\r?\n/));
|
|
57
|
+
return lines.filter((line) => line.length > 0);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function formatTranscriptEvent(event: unknown, themeLike: unknown = undefined): string[] {
|
|
61
|
+
const theme = asCrewTheme(themeLike);
|
|
62
|
+
const obj = asRecord(event);
|
|
63
|
+
if (!obj) return [String(event)];
|
|
64
|
+
const type = typeof obj.type === "string" ? obj.type : undefined;
|
|
65
|
+
const toolName = typeof obj.toolName === "string" ? obj.toolName : typeof obj.name === "string" ? obj.name : undefined;
|
|
66
|
+
const content = textFromContent(obj.content);
|
|
67
|
+
if (type && /tool/i.test(type)) {
|
|
68
|
+
const result = asRecord(obj.result);
|
|
69
|
+
const isError = obj.isError === true || result?.isError === true;
|
|
70
|
+
const isPartial = obj.isPartial === true;
|
|
71
|
+
const status: RunStatus = isError ? "failed" : isPartial ? "running" : "completed";
|
|
72
|
+
const header = theme.fg(colorForStatus(status), `${iconForStatus(status, { runningGlyph: "⋯" })} [Tool${toolName ? `: ${toolName}` : ""}] ${type}`);
|
|
73
|
+
const text = (content || (typeof obj.text === "string" ? obj.text : typeof obj.result === "string" ? obj.result : "")).trim();
|
|
74
|
+
if (!text) return [header, "(no output)"];
|
|
75
|
+
if (isLikelyDiff(text)) {
|
|
76
|
+
return [header, renderDiff(text, { theme })];
|
|
77
|
+
}
|
|
78
|
+
if (text.startsWith("{") && text.endsWith("}")) {
|
|
79
|
+
return [header, ...highlightJson(text, theme).split(/\r?\n/).filter(Boolean)];
|
|
80
|
+
}
|
|
81
|
+
if (text.includes("```") && text.includes("```")) {
|
|
82
|
+
return [header, ...highlightCodeBlocks(text, theme)];
|
|
83
|
+
}
|
|
84
|
+
return [header, ...text.split(/\r?\n/).filter(Boolean).map((line) => theme.fg("muted", line))];
|
|
85
|
+
}
|
|
86
|
+
const message = asRecord(obj.message);
|
|
87
|
+
if (message) {
|
|
88
|
+
const role = typeof message.role === "string" ? message.role : "message";
|
|
89
|
+
const text = textFromContent(message.content);
|
|
90
|
+
if (text.trim()) {
|
|
91
|
+
const label = role === "assistant" ? "Assistant" : role === "user" ? "User" : role;
|
|
92
|
+
const header = `[${label}]:`;
|
|
93
|
+
const lines = text.split(/\r?\n/);
|
|
94
|
+
if (text.includes("```") && text.includes("```")) {
|
|
95
|
+
return [theme.fg("accent", header), ...highlightCodeBlocks(text, theme)];
|
|
96
|
+
}
|
|
97
|
+
if (lines.length > 1) {
|
|
98
|
+
const block = lines
|
|
99
|
+
.map((line) => (role === "assistant" ? theme.bold(line) : line))
|
|
100
|
+
.join("\n");
|
|
101
|
+
return [theme.fg("accent", header), ...block.split(/\r?\n/).filter(Boolean)];
|
|
102
|
+
}
|
|
103
|
+
return [theme.fg("accent", header), ...lines.filter(Boolean)];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (type) {
|
|
107
|
+
const text = content || (typeof obj.text === "string" ? obj.text : "");
|
|
108
|
+
return text.trim() ? [theme.fg("muted", `[${type}]: ${text.trim()}`)] : [`[${type}]`];
|
|
109
|
+
}
|
|
110
|
+
return [JSON.stringify(event)];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function formatTranscriptText(text: string, themeLike: unknown = undefined): string[] {
|
|
114
|
+
const lines: string[] = [];
|
|
115
|
+
for (const raw of text.split(/\r?\n/).filter(Boolean)) {
|
|
116
|
+
try {
|
|
117
|
+
const parsed = JSON.parse(raw);
|
|
118
|
+
lines.push(...formatTranscriptEvent(parsed, themeLike));
|
|
119
|
+
} catch {
|
|
120
|
+
lines.push(raw);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return lines.length ? lines : ["(no transcript content)"];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function readRunTranscript(manifest: TeamRunManifest, taskId?: string): { title: string; path: string; lines: string[] } {
|
|
127
|
+
const agents = readCrewAgents(manifest);
|
|
128
|
+
const agent = taskId ? agents.find((item) => item.taskId === taskId || item.id === taskId) : agents.find((item) => item.transcriptPath) ?? agents[0];
|
|
129
|
+
const selectedTaskId = agent?.taskId ?? taskId ?? "unknown";
|
|
130
|
+
const transcriptPath = agent?.transcriptPath && fs.existsSync(agent.transcriptPath) ? agent.transcriptPath : agentOutputPath(manifest, selectedTaskId);
|
|
131
|
+
const text = fs.existsSync(transcriptPath) ? fs.readFileSync(transcriptPath, "utf-8") : "";
|
|
132
|
+
return { title: `${manifest.runId}:${selectedTaskId}`, path: transcriptPath, lines: formatTranscriptText(text) };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
interface ViewerState {
|
|
136
|
+
theme: TranscriptTheme;
|
|
137
|
+
autoScroll: boolean;
|
|
138
|
+
lastHeight: number;
|
|
139
|
+
scroll: number;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function renderViewerBase(
|
|
143
|
+
state: ViewerState,
|
|
144
|
+
width: number,
|
|
145
|
+
lines: string[],
|
|
146
|
+
title: string,
|
|
147
|
+
subtitle: string,
|
|
148
|
+
): string[] {
|
|
149
|
+
const inner = Math.max(20, width - 4);
|
|
150
|
+
const bodyText = lines.join("\n");
|
|
151
|
+
const { visualLines, skippedCount } = truncateToVisualLines(bodyText, state.lastHeight, inner);
|
|
152
|
+
const maxScroll = Math.max(0, visualLines.length - state.lastHeight);
|
|
153
|
+
if (state.autoScroll) state.scroll = maxScroll;
|
|
154
|
+
state.scroll = Math.min(state.scroll, maxScroll);
|
|
155
|
+
const visible = visualLines.slice(state.scroll, state.scroll + state.lastHeight);
|
|
156
|
+
const statusLine = `${visualLines.length} lines · ${visualLines.length ? Math.round(((state.scroll + visible.length) / visualLines.length) * 100) : 100}% · auto-scroll ${state.autoScroll ? "on" : "off"}`;
|
|
157
|
+
const fg = (color: Parameters<TranscriptTheme["fg"]>[0], text: string) => state.theme.fg(color, text);
|
|
158
|
+
const row = (text: string) => `${fg("border", "│")} ${pad(truncate(text, inner), inner)} ${fg("border", "│")}`;
|
|
159
|
+
const linesOut: string[] = [
|
|
160
|
+
fg("border", `╭${"─".repeat(inner + 2)}╮`),
|
|
161
|
+
row(`${fg("accent", title)} ${fg("dim", subtitle)}`),
|
|
162
|
+
row(fg("dim", "j/k scroll · PgUp/PgDn · g/G top/bottom · q close")),
|
|
163
|
+
fg("border", `├${"─".repeat(inner + 2)}┤`),
|
|
164
|
+
...visible.map(row),
|
|
165
|
+
fg("border", `├${"─".repeat(inner + 2)}┤`),
|
|
166
|
+
row(fg("dim", statusLine)),
|
|
167
|
+
fg("border", `╰${"─".repeat(inner + 2)}╯`),
|
|
168
|
+
];
|
|
169
|
+
if (skippedCount > 0) {
|
|
170
|
+
linesOut.splice(linesOut.length - 1, 0, row(fg("muted", `… (${skippedCount} lines truncated above`)));
|
|
171
|
+
}
|
|
172
|
+
return linesOut.map((line) => truncate(line, width));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
export class DurableTextViewer implements Component {
|
|
177
|
+
private scroll = 0;
|
|
178
|
+
private lastHeight = 16;
|
|
179
|
+
private autoScroll = true;
|
|
180
|
+
private title: string;
|
|
181
|
+
private subtitle: string;
|
|
182
|
+
private lines: string[];
|
|
183
|
+
private theme: TranscriptTheme;
|
|
184
|
+
private done: (result: undefined) => void;
|
|
185
|
+
private readonly unsubscribeTheme: () => void;
|
|
186
|
+
|
|
187
|
+
constructor(title: string, subtitle: string, lines: string[], theme: unknown, done: (result: undefined) => void) {
|
|
188
|
+
this.title = title;
|
|
189
|
+
this.subtitle = subtitle;
|
|
190
|
+
this.lines = lines.length ? lines : ["(empty)"];
|
|
191
|
+
this.theme = asCrewTheme(theme);
|
|
192
|
+
this.done = done;
|
|
193
|
+
this.unsubscribeTheme = subscribeThemeChange(theme, () => this.invalidate());
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
invalidate(): void {}
|
|
197
|
+
|
|
198
|
+
dispose(): void {
|
|
199
|
+
this.unsubscribeTheme();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
handleInput(data: string): void {
|
|
203
|
+
if (data === "q" || data === "\u001b") {
|
|
204
|
+
this.done(undefined);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const maxScroll = Math.max(0, this.lines.length - this.lastHeight);
|
|
208
|
+
if (data === "k" || data === "\u001b[A") {
|
|
209
|
+
this.scroll = Math.max(0, this.scroll - 1);
|
|
210
|
+
this.autoScroll = false;
|
|
211
|
+
} else if (data === "j" || data === "\u001b[B") {
|
|
212
|
+
this.scroll = Math.min(maxScroll, this.scroll + 1);
|
|
213
|
+
this.autoScroll = this.scroll >= maxScroll;
|
|
214
|
+
} else if (data === "\u001b[5~") {
|
|
215
|
+
this.scroll = Math.max(0, this.scroll - this.lastHeight);
|
|
216
|
+
this.autoScroll = false;
|
|
217
|
+
} else if (data === "\u001b[6~") {
|
|
218
|
+
this.scroll = Math.min(maxScroll, this.scroll + this.lastHeight);
|
|
219
|
+
this.autoScroll = this.scroll >= maxScroll;
|
|
220
|
+
} else if (data === "g" || data === "\u001b[H") {
|
|
221
|
+
this.scroll = 0;
|
|
222
|
+
this.autoScroll = false;
|
|
223
|
+
} else if (data === "G" || data === "\u001b[F") {
|
|
224
|
+
this.scroll = maxScroll;
|
|
225
|
+
this.autoScroll = true;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
render(width: number): string[] {
|
|
230
|
+
return renderViewerBase(
|
|
231
|
+
{ theme: this.theme, autoScroll: this.autoScroll, lastHeight: this.lastHeight, scroll: this.scroll },
|
|
232
|
+
width,
|
|
233
|
+
this.lines,
|
|
234
|
+
this.title,
|
|
235
|
+
this.subtitle,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export class DurableTranscriptViewer implements Component {
|
|
241
|
+
private scroll = 0;
|
|
242
|
+
private lastHeight = 16;
|
|
243
|
+
private autoScroll = true;
|
|
244
|
+
private manifest: TeamRunManifest;
|
|
245
|
+
private theme: TranscriptTheme;
|
|
246
|
+
private done: (result: undefined) => void;
|
|
247
|
+
private taskId?: string;
|
|
248
|
+
private readonly unsubscribeTheme: () => void;
|
|
249
|
+
|
|
250
|
+
constructor(manifest: TeamRunManifest, theme: unknown, done: (result: undefined) => void, taskId?: string) {
|
|
251
|
+
this.manifest = manifest;
|
|
252
|
+
this.theme = asCrewTheme(theme);
|
|
253
|
+
this.done = done;
|
|
254
|
+
this.taskId = taskId;
|
|
255
|
+
this.unsubscribeTheme = subscribeThemeChange(theme, () => this.invalidate());
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
invalidate(): void {}
|
|
259
|
+
|
|
260
|
+
dispose(): void {
|
|
261
|
+
this.unsubscribeTheme();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
handleInput(data: string): void {
|
|
265
|
+
if (data === "q" || data === "\u001b") {
|
|
266
|
+
this.done(undefined);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
const content = readRunTranscript(this.manifest, this.taskId).lines;
|
|
270
|
+
const maxScroll = Math.max(0, content.length - this.lastHeight);
|
|
271
|
+
if (data === "k" || data === "\u001b[A") {
|
|
272
|
+
this.scroll = Math.max(0, this.scroll - 1);
|
|
273
|
+
this.autoScroll = false;
|
|
274
|
+
} else if (data === "j" || data === "\u001b[B") {
|
|
275
|
+
this.scroll = Math.min(maxScroll, this.scroll + 1);
|
|
276
|
+
this.autoScroll = this.scroll >= maxScroll;
|
|
277
|
+
} else if (data === "\u001b[5~") {
|
|
278
|
+
this.scroll = Math.max(0, this.scroll - this.lastHeight);
|
|
279
|
+
this.autoScroll = false;
|
|
280
|
+
} else if (data === "\u001b[6~") {
|
|
281
|
+
this.scroll = Math.min(maxScroll, this.scroll + this.lastHeight);
|
|
282
|
+
this.autoScroll = this.scroll >= maxScroll;
|
|
283
|
+
} else if (data === "g" || data === "\u001b[H") {
|
|
284
|
+
this.scroll = 0;
|
|
285
|
+
this.autoScroll = false;
|
|
286
|
+
} else if (data === "G" || data === "\u001b[F") {
|
|
287
|
+
this.scroll = maxScroll;
|
|
288
|
+
this.autoScroll = true;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
render(width: number): string[] {
|
|
293
|
+
const data = readRunTranscript(this.manifest, this.taskId);
|
|
294
|
+
return renderViewerBase(
|
|
295
|
+
{ theme: this.theme, autoScroll: this.autoScroll, lastHeight: this.lastHeight, scroll: this.scroll },
|
|
296
|
+
width,
|
|
297
|
+
data.lines,
|
|
298
|
+
"pi-crew transcript",
|
|
299
|
+
data.title,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
}
|