@pi-agents/orchid 0.1.0-beta.0
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 +41 -0
- package/LICENSE +21 -0
- package/README.md +246 -0
- package/agents/AGENTS-MANIFEST.md +42 -0
- package/agents/brain.md +42 -0
- package/agents/context-builder.md +46 -0
- package/agents/delegate.md +12 -0
- package/agents/dev-1.md +42 -0
- package/agents/oracle.md +73 -0
- package/agents/planner.md +55 -0
- package/agents/researcher.md +52 -0
- package/agents/reviewer.md +79 -0
- package/agents/scout.md +50 -0
- package/agents/tester.md +45 -0
- package/agents/worker.md +55 -0
- package/extensions/ralph.ts +1 -0
- package/extensions/reviewer-extension.ts +125 -0
- package/extensions/task-orchestrator.ts +28 -0
- package/package.json +63 -0
- package/prompts/gather-context-and-clarify.md +13 -0
- package/prompts/parallel-cleanup.md +59 -0
- package/prompts/parallel-context-build.md +53 -0
- package/prompts/parallel-handoff-plan.md +59 -0
- package/prompts/parallel-research.md +50 -0
- package/prompts/parallel-review.md +54 -0
- package/prompts/review-loop.md +41 -0
- package/skills/orchid/SKILL.md +214 -0
- package/skills/orchid/orchid-cleanup/SKILL.md +122 -0
- package/skills/orchid/orchid-converge/SKILL.md +124 -0
- package/skills/orchid/orchid-decompose/SKILL.md +201 -0
- package/skills/orchid/orchid-doctor/SKILL.md +162 -0
- package/skills/orchid/orchid-investigate/SKILL.md +102 -0
- package/skills/orchid/orchid-launch/SKILL.md +147 -0
- package/skills/ralph/SKILL.md +73 -0
- package/skills/subagents/pi-subagents/SKILL.md +813 -0
- package/src/index.ts +7 -0
- package/src/orchestrator/abort.ts +534 -0
- package/src/orchestrator/agent-bridge-extension.ts +1020 -0
- package/src/orchestrator/agent-host.ts +954 -0
- package/src/orchestrator/cleanup.ts +776 -0
- package/src/orchestrator/config-loader.ts +1412 -0
- package/src/orchestrator/config-schema.ts +690 -0
- package/src/orchestrator/config.ts +81 -0
- package/src/orchestrator/context-window.ts +66 -0
- package/src/orchestrator/diagnostic-reports.ts +475 -0
- package/src/orchestrator/diagnostics.ts +394 -0
- package/src/orchestrator/discovery.ts +1833 -0
- package/src/orchestrator/engine-worker.ts +415 -0
- package/src/orchestrator/engine.ts +5940 -0
- package/src/orchestrator/execution.ts +3104 -0
- package/src/orchestrator/extension.ts +5934 -0
- package/src/orchestrator/formatting.ts +785 -0
- package/src/orchestrator/git.ts +88 -0
- package/src/orchestrator/index.ts +28 -0
- package/src/orchestrator/lane-runner.ts +1787 -0
- package/src/orchestrator/mailbox.ts +780 -0
- package/src/orchestrator/merge.ts +3414 -0
- package/src/orchestrator/messages.ts +1062 -0
- package/src/orchestrator/migrations.ts +278 -0
- package/src/orchestrator/naming.ts +117 -0
- package/src/orchestrator/path-resolver.ts +275 -0
- package/src/orchestrator/persistence.ts +2625 -0
- package/src/orchestrator/process-registry.ts +452 -0
- package/src/orchestrator/quality-gate.ts +1085 -0
- package/src/orchestrator/resume.ts +3488 -0
- package/src/orchestrator/sessions.ts +57 -0
- package/src/orchestrator/settings-loader.ts +136 -0
- package/src/orchestrator/settings-tui.ts +2208 -0
- package/src/orchestrator/sidecar-telemetry.ts +267 -0
- package/src/orchestrator/supervisor.ts +4548 -0
- package/src/orchestrator/task-executor-core.ts +675 -0
- package/src/orchestrator/tmux-compat.ts +37 -0
- package/src/orchestrator/tool-allowlist-constants.ts +37 -0
- package/src/orchestrator/types.ts +4465 -0
- package/src/orchestrator/verification.ts +547 -0
- package/src/orchestrator/waves.ts +1564 -0
- package/src/orchestrator/workspace.ts +707 -0
- package/src/orchestrator/worktree.ts +2725 -0
- package/src/ralph/index.ts +825 -0
- package/src/subagents/agents/agent-management.ts +648 -0
- package/src/subagents/agents/agent-scope.ts +6 -0
- package/src/subagents/agents/agent-selection.ts +23 -0
- package/src/subagents/agents/agent-serializer.ts +86 -0
- package/src/subagents/agents/agents.ts +832 -0
- package/src/subagents/agents/chain-serializer.ts +137 -0
- package/src/subagents/agents/frontmatter.ts +29 -0
- package/src/subagents/agents/identity.ts +30 -0
- package/src/subagents/agents/skills.ts +632 -0
- package/src/subagents/extension/config.ts +16 -0
- package/src/subagents/extension/control-notices.ts +92 -0
- package/src/subagents/extension/doctor.ts +199 -0
- package/src/subagents/extension/fanout-child.ts +170 -0
- package/src/subagents/extension/index.ts +573 -0
- package/src/subagents/extension/schemas.ts +168 -0
- package/src/subagents/intercom/intercom-bridge.ts +379 -0
- package/src/subagents/intercom/result-intercom.ts +377 -0
- package/src/subagents/runs/background/async-execution.ts +712 -0
- package/src/subagents/runs/background/async-job-tracker.ts +310 -0
- package/src/subagents/runs/background/async-resume.ts +345 -0
- package/src/subagents/runs/background/async-status.ts +325 -0
- package/src/subagents/runs/background/completion-dedupe.ts +63 -0
- package/src/subagents/runs/background/notify.ts +108 -0
- package/src/subagents/runs/background/parallel-groups.ts +45 -0
- package/src/subagents/runs/background/result-watcher.ts +307 -0
- package/src/subagents/runs/background/run-id-resolver.ts +83 -0
- package/src/subagents/runs/background/run-status.ts +269 -0
- package/src/subagents/runs/background/stale-run-reconciler.ts +336 -0
- package/src/subagents/runs/background/subagent-runner.ts +1808 -0
- package/src/subagents/runs/background/top-level-async.ts +13 -0
- package/src/subagents/runs/foreground/chain-clarify.ts +1333 -0
- package/src/subagents/runs/foreground/chain-execution.ts +938 -0
- package/src/subagents/runs/foreground/execution.ts +918 -0
- package/src/subagents/runs/foreground/subagent-executor.ts +2527 -0
- package/src/subagents/runs/shared/completion-guard.ts +147 -0
- package/src/subagents/runs/shared/long-running-guard.ts +175 -0
- package/src/subagents/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
- package/src/subagents/runs/shared/model-fallback.ts +103 -0
- package/src/subagents/runs/shared/nested-events.ts +819 -0
- package/src/subagents/runs/shared/nested-path.ts +52 -0
- package/src/subagents/runs/shared/nested-render.ts +115 -0
- package/src/subagents/runs/shared/parallel-utils.ts +109 -0
- package/src/subagents/runs/shared/pi-args.ts +220 -0
- package/src/subagents/runs/shared/pi-spawn.ts +115 -0
- package/src/subagents/runs/shared/run-history.ts +60 -0
- package/src/subagents/runs/shared/single-output.ts +164 -0
- package/src/subagents/runs/shared/subagent-control.ts +226 -0
- package/src/subagents/runs/shared/subagent-prompt-runtime.ts +170 -0
- package/src/subagents/runs/shared/worktree.ts +577 -0
- package/src/subagents/shared/artifacts.ts +98 -0
- package/src/subagents/shared/atomic-json.ts +16 -0
- package/src/subagents/shared/file-coalescer.ts +40 -0
- package/src/subagents/shared/fork-context.ts +76 -0
- package/src/subagents/shared/formatters.ts +133 -0
- package/src/subagents/shared/jsonl-writer.ts +81 -0
- package/src/subagents/shared/model-info.ts +78 -0
- package/src/subagents/shared/post-exit-stdio-guard.ts +85 -0
- package/src/subagents/shared/session-identity.ts +10 -0
- package/src/subagents/shared/session-tokens.ts +44 -0
- package/src/subagents/shared/settings.ts +397 -0
- package/src/subagents/shared/status-format.ts +49 -0
- package/src/subagents/shared/types.ts +822 -0
- package/src/subagents/shared/utils.ts +450 -0
- package/src/subagents/slash/prompt-template-bridge.ts +397 -0
- package/src/subagents/slash/slash-bridge.ts +174 -0
- package/src/subagents/slash/slash-commands.ts +528 -0
- package/src/subagents/slash/slash-live-state.ts +292 -0
- package/src/subagents/tui/render-helpers.ts +80 -0
- package/src/subagents/tui/render.ts +1358 -0
- package/templates/agents/local/supervisor.md +33 -0
- package/templates/agents/local/task-merger.md +27 -0
- package/templates/agents/local/task-reviewer.md +30 -0
- package/templates/agents/local/task-worker.md +34 -0
- package/templates/agents/supervisor-routing.md +92 -0
- package/templates/agents/supervisor.md +229 -0
- package/templates/agents/task-merger.md +214 -0
- package/templates/agents/task-reviewer.md +260 -0
- package/templates/agents/task-worker-segment.md +44 -0
- package/templates/agents/task-worker.md +557 -0
- package/templates/tasks/CONTEXT.md +30 -0
- package/templates/tasks/EXAMPLE-001-hello-world/PROMPT.md +98 -0
- package/templates/tasks/EXAMPLE-001-hello-world/STATUS.md +73 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/PROMPT.md +97 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/STATUS.md +73 -0
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* General utility functions for the subagent extension
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as fs from "node:fs";
|
|
6
|
+
import * as os from "node:os";
|
|
7
|
+
import * as path from "node:path";
|
|
8
|
+
import type { Message } from "@earendil-works/pi-ai";
|
|
9
|
+
import { formatToolCall } from "./formatters.ts";
|
|
10
|
+
import type { AgentProgress, AsyncStatus, Details, DisplayItem, ErrorInfo, SingleResult, ToolCallSummary } from "./types.ts";
|
|
11
|
+
|
|
12
|
+
// ============================================================================
|
|
13
|
+
// File System Utilities
|
|
14
|
+
// ============================================================================
|
|
15
|
+
|
|
16
|
+
export function getAgentDir(): string {
|
|
17
|
+
const configured = process.env.PI_CODING_AGENT_DIR;
|
|
18
|
+
if (configured === "~") return os.homedir();
|
|
19
|
+
if (configured?.startsWith("~/")) return path.join(os.homedir(), configured.slice(2));
|
|
20
|
+
return configured || path.join(os.homedir(), ".pi", "agent");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const statusCache = new Map<string, { mtime: number; status: AsyncStatus }>();
|
|
24
|
+
|
|
25
|
+
function getErrorMessage(error: unknown): string {
|
|
26
|
+
return error instanceof Error ? error.message : String(error);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function resolveChildCwd(baseCwd: string, childCwd: string | undefined): string {
|
|
30
|
+
if (!childCwd) return baseCwd;
|
|
31
|
+
return path.isAbsolute(childCwd) ? childCwd : path.resolve(baseCwd, childCwd);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isNotFoundError(error: unknown): boolean {
|
|
35
|
+
return typeof error === "object"
|
|
36
|
+
&& error !== null
|
|
37
|
+
&& "code" in error
|
|
38
|
+
&& (error as NodeJS.ErrnoException).code === "ENOENT";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Read async job status from disk (with mtime-based caching)
|
|
43
|
+
*/
|
|
44
|
+
export function readStatus(asyncDir: string): AsyncStatus | null {
|
|
45
|
+
const statusPath = path.join(asyncDir, "status.json");
|
|
46
|
+
|
|
47
|
+
let stat: fs.Stats;
|
|
48
|
+
try {
|
|
49
|
+
stat = fs.statSync(statusPath);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (isNotFoundError(error)) return null;
|
|
52
|
+
throw new Error(`Failed to inspect async status file '${statusPath}': ${getErrorMessage(error)}`, {
|
|
53
|
+
cause: error instanceof Error ? error : undefined,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const cached = statusCache.get(statusPath);
|
|
58
|
+
if (cached && cached.mtime === stat.mtimeMs) {
|
|
59
|
+
return cached.status;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let content: string;
|
|
63
|
+
try {
|
|
64
|
+
content = fs.readFileSync(statusPath, "utf-8");
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (isNotFoundError(error)) return null;
|
|
67
|
+
throw new Error(`Failed to read async status file '${statusPath}': ${getErrorMessage(error)}`, {
|
|
68
|
+
cause: error instanceof Error ? error : undefined,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let status: AsyncStatus;
|
|
73
|
+
try {
|
|
74
|
+
status = JSON.parse(content) as AsyncStatus;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
throw new Error(`Failed to parse async status file '${statusPath}': ${getErrorMessage(error)}`, {
|
|
77
|
+
cause: error instanceof Error ? error : undefined,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
statusCache.set(statusPath, { mtime: stat.mtimeMs, status });
|
|
82
|
+
if (statusCache.size > 50) {
|
|
83
|
+
const firstKey = statusCache.keys().next().value;
|
|
84
|
+
if (firstKey) statusCache.delete(firstKey);
|
|
85
|
+
}
|
|
86
|
+
return status;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const outputTailCache = new Map<string, { mtime: number; size: number; lines: string[] }>();
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get the last N lines from an output file (with mtime/size-based caching)
|
|
93
|
+
*/
|
|
94
|
+
function getOutputTail(outputFile: string | undefined, maxLines: number = 3): string[] {
|
|
95
|
+
if (!outputFile) return [];
|
|
96
|
+
let fd: number | null = null;
|
|
97
|
+
try {
|
|
98
|
+
const stat = fs.statSync(outputFile);
|
|
99
|
+
if (stat.size === 0) return [];
|
|
100
|
+
|
|
101
|
+
const cached = outputTailCache.get(outputFile);
|
|
102
|
+
if (cached && cached.mtime === stat.mtimeMs && cached.size === stat.size) {
|
|
103
|
+
return cached.lines;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const tailBytes = 4096;
|
|
107
|
+
const start = Math.max(0, stat.size - tailBytes);
|
|
108
|
+
fd = fs.openSync(outputFile, "r");
|
|
109
|
+
const buffer = Buffer.alloc(Math.min(tailBytes, stat.size));
|
|
110
|
+
fs.readSync(fd, buffer, 0, buffer.length, start);
|
|
111
|
+
const content = buffer.toString("utf-8");
|
|
112
|
+
const allLines = content.split("\n").filter((l) => l.trim());
|
|
113
|
+
const lines = allLines.slice(-maxLines).map((l) => l.slice(0, 120) + (l.length > 120 ? "..." : ""));
|
|
114
|
+
|
|
115
|
+
outputTailCache.set(outputFile, { mtime: stat.mtimeMs, size: stat.size, lines });
|
|
116
|
+
if (outputTailCache.size > 20) {
|
|
117
|
+
const firstKey = outputTailCache.keys().next().value;
|
|
118
|
+
if (firstKey) outputTailCache.delete(firstKey);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return lines;
|
|
122
|
+
} catch {
|
|
123
|
+
// Output tails are UI-only hints; unreadable or missing files should render as no tail.
|
|
124
|
+
return [];
|
|
125
|
+
} finally {
|
|
126
|
+
if (fd !== null) {
|
|
127
|
+
try {
|
|
128
|
+
fs.closeSync(fd);
|
|
129
|
+
} catch {
|
|
130
|
+
// Closing the best-effort tail file handle should not surface over the main status view.
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Get human-readable last activity time for a file
|
|
138
|
+
*/
|
|
139
|
+
export function getLastActivity(outputFile: string | undefined): string {
|
|
140
|
+
if (!outputFile) return "";
|
|
141
|
+
try {
|
|
142
|
+
const stat = fs.statSync(outputFile);
|
|
143
|
+
const ago = Date.now() - stat.mtimeMs;
|
|
144
|
+
if (ago < 1000) return "active now";
|
|
145
|
+
if (ago < 60000) return `active ${Math.floor(ago / 1000)}s ago`;
|
|
146
|
+
return `active ${Math.floor(ago / 60000)}m ago`;
|
|
147
|
+
} catch {
|
|
148
|
+
// Last-activity text is best effort; missing files should simply omit the hint.
|
|
149
|
+
return "";
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Find the latest session file in a directory
|
|
155
|
+
*/
|
|
156
|
+
export function findLatestSessionFile(sessionDir: string): string | null {
|
|
157
|
+
if (!fs.existsSync(sessionDir)) return null;
|
|
158
|
+
const files = fs.readdirSync(sessionDir)
|
|
159
|
+
.filter((f) => f.endsWith(".jsonl"))
|
|
160
|
+
.map((f) => {
|
|
161
|
+
const filePath = path.join(sessionDir, f);
|
|
162
|
+
return {
|
|
163
|
+
path: filePath,
|
|
164
|
+
mtime: fs.statSync(filePath).mtimeMs,
|
|
165
|
+
};
|
|
166
|
+
})
|
|
167
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
168
|
+
return files.length > 0 ? files[0].path : null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Write a prompt to a temporary file
|
|
173
|
+
*/
|
|
174
|
+
function writePrompt(agent: string, prompt: string): { dir: string; path: string } {
|
|
175
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-subagent-"));
|
|
176
|
+
const p = path.join(dir, `${agent.replace(/[^\w.-]/g, "_")}.md`);
|
|
177
|
+
fs.writeFileSync(p, prompt, { mode: 0o600 });
|
|
178
|
+
return { dir, path: p };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ============================================================================
|
|
182
|
+
// Message Parsing Utilities
|
|
183
|
+
// ============================================================================
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Get the final text output from a list of messages
|
|
187
|
+
*/
|
|
188
|
+
export function getFinalOutput(messages: Message[]): string {
|
|
189
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
190
|
+
const msg = messages[i];
|
|
191
|
+
if (msg.role === "assistant") {
|
|
192
|
+
const hasAssistantError = ("errorMessage" in msg && typeof msg.errorMessage === "string" && msg.errorMessage.length > 0)
|
|
193
|
+
|| ("stopReason" in msg && msg.stopReason === "error");
|
|
194
|
+
if (hasAssistantError) continue;
|
|
195
|
+
for (const part of msg.content) {
|
|
196
|
+
if (part.type === "text" && part.text.trim().length > 0) return part.text;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return "";
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function getSingleResultOutput(result: Pick<SingleResult, "finalOutput" | "messages">): string {
|
|
204
|
+
return result.finalOutput ?? getFinalOutput(result.messages ?? []);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Extract display items (text and tool calls) from messages
|
|
209
|
+
*/
|
|
210
|
+
export function getDisplayItems(messages: Message[] | undefined): DisplayItem[] {
|
|
211
|
+
if (!messages || messages.length === 0) return [];
|
|
212
|
+
const items: DisplayItem[] = [];
|
|
213
|
+
for (const msg of messages) {
|
|
214
|
+
if (msg.role === "assistant") {
|
|
215
|
+
for (const part of msg.content) {
|
|
216
|
+
if (part.type === "text") items.push({ type: "text", text: part.text });
|
|
217
|
+
else if (part.type === "toolCall") items.push({ type: "tool", name: part.name, args: part.arguments });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return items;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function compactCompletedProgress(progress: AgentProgress): AgentProgress {
|
|
225
|
+
if (progress.status === "running") return progress;
|
|
226
|
+
return {
|
|
227
|
+
index: progress.index,
|
|
228
|
+
agent: progress.agent,
|
|
229
|
+
status: progress.status,
|
|
230
|
+
activityState: progress.activityState,
|
|
231
|
+
task: progress.task,
|
|
232
|
+
skills: progress.skills,
|
|
233
|
+
toolCount: progress.toolCount,
|
|
234
|
+
tokens: progress.tokens,
|
|
235
|
+
durationMs: progress.durationMs,
|
|
236
|
+
error: progress.error,
|
|
237
|
+
failedTool: progress.failedTool,
|
|
238
|
+
recentTools: [],
|
|
239
|
+
recentOutput: [],
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function extractToolCallSummaries(messages: Message[] | undefined): ToolCallSummary[] {
|
|
244
|
+
if (!messages?.length) return [];
|
|
245
|
+
const summaries: ToolCallSummary[] = [];
|
|
246
|
+
for (const msg of messages) {
|
|
247
|
+
if (msg.role !== "assistant") continue;
|
|
248
|
+
for (const part of msg.content) {
|
|
249
|
+
if (part.type !== "toolCall") continue;
|
|
250
|
+
const args = typeof part.arguments === "object" && part.arguments !== null && !Array.isArray(part.arguments)
|
|
251
|
+
? part.arguments
|
|
252
|
+
: {};
|
|
253
|
+
summaries.push({
|
|
254
|
+
text: formatToolCall(part.name, args),
|
|
255
|
+
expandedText: formatToolCall(part.name, args, true),
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return summaries;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export function compactForegroundResult(result: SingleResult): SingleResult {
|
|
263
|
+
if (result.progress?.status === "running") return result;
|
|
264
|
+
const toolCalls = result.toolCalls?.length ? result.toolCalls : extractToolCallSummaries(result.messages);
|
|
265
|
+
return {
|
|
266
|
+
...result,
|
|
267
|
+
messages: undefined,
|
|
268
|
+
progress: undefined,
|
|
269
|
+
toolCalls: toolCalls.length ? toolCalls : undefined,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function compactForegroundDetails(details: Details): Details {
|
|
274
|
+
return {
|
|
275
|
+
...details,
|
|
276
|
+
results: details.results.map(compactForegroundResult),
|
|
277
|
+
progress: details.progress
|
|
278
|
+
? details.progress.map(compactCompletedProgress)
|
|
279
|
+
: undefined,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Detect errors in subagent execution from messages (only errors with no subsequent success)
|
|
285
|
+
*/
|
|
286
|
+
export function detectSubagentError(messages: Message[]): ErrorInfo {
|
|
287
|
+
let lastAssistantTextIndex = -1;
|
|
288
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
289
|
+
const msg = messages[i];
|
|
290
|
+
if (msg.role === "assistant") {
|
|
291
|
+
const hasText = Array.isArray(msg.content) && msg.content.some(
|
|
292
|
+
(c) => c.type === "text" && "text" in c && typeof c.text === "string" && c.text.trim().length > 0,
|
|
293
|
+
);
|
|
294
|
+
if (hasText) {
|
|
295
|
+
lastAssistantTextIndex = i;
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const scanStart = lastAssistantTextIndex >= 0 ? lastAssistantTextIndex + 1 : 0;
|
|
302
|
+
|
|
303
|
+
for (let i = messages.length - 1; i >= scanStart; i--) {
|
|
304
|
+
const msg = messages[i];
|
|
305
|
+
if (msg.role !== "toolResult") continue;
|
|
306
|
+
const toolName = "toolName" in msg && typeof msg.toolName === "string" ? msg.toolName : undefined;
|
|
307
|
+
const isError = "isError" in msg && msg.isError === true;
|
|
308
|
+
|
|
309
|
+
if (isError) {
|
|
310
|
+
const text = msg.content.find((c) => c.type === "text");
|
|
311
|
+
const details = text && "text" in text ? text.text : undefined;
|
|
312
|
+
const exitMatch = details?.match(/exit(?:ed)?\s*(?:with\s*)?(?:code|status)?\s*[:\s]?\s*(\d+)/i);
|
|
313
|
+
return {
|
|
314
|
+
hasError: true,
|
|
315
|
+
exitCode: exitMatch ? parseInt(exitMatch[1], 10) : 1,
|
|
316
|
+
errorType: toolName || "tool",
|
|
317
|
+
details: details?.slice(0, 200),
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (toolName !== "bash") continue;
|
|
322
|
+
|
|
323
|
+
const text = msg.content.find((c) => c.type === "text");
|
|
324
|
+
if (!text || !("text" in text)) continue;
|
|
325
|
+
const output = text.text;
|
|
326
|
+
|
|
327
|
+
const exitMatch = output.match(/exit(?:ed)?\s*(?:with\s*)?(?:code|status)?\s*[:\s]?\s*(\d+)/i);
|
|
328
|
+
if (exitMatch) {
|
|
329
|
+
const code = parseInt(exitMatch[1], 10);
|
|
330
|
+
if (code !== 0) {
|
|
331
|
+
return { hasError: true, exitCode: code, errorType: "bash", details: output.slice(0, 200) };
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// NOTE: These patterns can match legitimate output (grep results, logs,
|
|
336
|
+
// testing). With the assistant-message check above, most false positives
|
|
337
|
+
// are mitigated since the agent will have responded after routine errors.
|
|
338
|
+
const fatalPatterns = [
|
|
339
|
+
/command not found/i,
|
|
340
|
+
/permission denied/i,
|
|
341
|
+
/no such file or directory/i,
|
|
342
|
+
/segmentation fault/i,
|
|
343
|
+
/killed|terminated/i,
|
|
344
|
+
/out of memory/i,
|
|
345
|
+
/connection refused/i,
|
|
346
|
+
/timeout/i,
|
|
347
|
+
];
|
|
348
|
+
for (const pattern of fatalPatterns) {
|
|
349
|
+
if (pattern.test(output)) {
|
|
350
|
+
return { hasError: true, exitCode: 1, errorType: "bash", details: output.slice(0, 200) };
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return { hasError: false };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Extract a preview of tool arguments for display
|
|
360
|
+
*/
|
|
361
|
+
export function extractToolArgsPreview(args: Record<string, unknown>): string {
|
|
362
|
+
const truncatePreview = (value: string, maxLength: number): string =>
|
|
363
|
+
value.length > maxLength ? `${value.slice(0, maxLength - 3)}...` : value;
|
|
364
|
+
|
|
365
|
+
const stringifyPreviewValue = (value: unknown): string | undefined => {
|
|
366
|
+
if (typeof value === "string" && value.trim().length > 0) return value;
|
|
367
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
368
|
+
return undefined;
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
const previewArray = (value: unknown): string | undefined => {
|
|
372
|
+
if (!Array.isArray(value) || value.length === 0) return undefined;
|
|
373
|
+
const first = stringifyPreviewValue(value[0]);
|
|
374
|
+
if (!first) return undefined;
|
|
375
|
+
const suffix = value.length > 1 ? ` (+${value.length - 1} more)` : "";
|
|
376
|
+
return `${first}${suffix}`;
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
// Handle MCP tool calls - show server/tool info
|
|
380
|
+
if (args.tool && typeof args.tool === "string") {
|
|
381
|
+
const server = args.server && typeof args.server === "string" ? `${args.server}/` : "";
|
|
382
|
+
const toolArgs = args.args && typeof args.args === "string" ? ` ${args.args.slice(0, 40)}` : "";
|
|
383
|
+
return `${server}${args.tool}${toolArgs}`;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const queriesPreview = previewArray(args.queries);
|
|
387
|
+
if (queriesPreview) return truncatePreview(queriesPreview, 60);
|
|
388
|
+
if (typeof args.query === "string" && args.query.trim().length > 0) return truncatePreview(args.query, 60);
|
|
389
|
+
if (typeof args.workflow === "string" && args.workflow.trim().length > 0) return `workflow=${truncatePreview(args.workflow, 48)}`;
|
|
390
|
+
|
|
391
|
+
if (typeof args.url === "string" && args.url.trim().length > 0) return truncatePreview(args.url, 60);
|
|
392
|
+
const urlsPreview = previewArray(args.urls);
|
|
393
|
+
if (urlsPreview) return truncatePreview(urlsPreview, 60);
|
|
394
|
+
if (typeof args.prompt === "string" && args.prompt.trim().length > 0) return truncatePreview(args.prompt, 60);
|
|
395
|
+
|
|
396
|
+
const previewKeys = ["command", "path", "file_path", "pattern", "query", "url", "task", "describe", "search"];
|
|
397
|
+
for (const key of previewKeys) {
|
|
398
|
+
if (args[key] && typeof args[key] === "string") {
|
|
399
|
+
const value = args[key] as string;
|
|
400
|
+
return truncatePreview(value, 60);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Fallback: show first string value found
|
|
405
|
+
for (const [key, value] of Object.entries(args)) {
|
|
406
|
+
const arrayPreview = previewArray(value);
|
|
407
|
+
if (arrayPreview) return `${key}=${truncatePreview(arrayPreview, 50)}`;
|
|
408
|
+
if (typeof value === "string" && value.length > 0) {
|
|
409
|
+
const preview = truncatePreview(value, 50);
|
|
410
|
+
return `${key}=${preview}`;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return "";
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Extract text content from various message content formats
|
|
418
|
+
*/
|
|
419
|
+
export function extractTextFromContent(content: unknown): string {
|
|
420
|
+
if (!content) return "";
|
|
421
|
+
// Handle string content directly
|
|
422
|
+
if (typeof content === "string") return content;
|
|
423
|
+
// Handle array content
|
|
424
|
+
if (!Array.isArray(content)) return "";
|
|
425
|
+
const texts: string[] = [];
|
|
426
|
+
for (const part of content) {
|
|
427
|
+
if (part && typeof part === "object") {
|
|
428
|
+
// Handle { type: "text", text: "..." }
|
|
429
|
+
if ("type" in part && part.type === "text" && "text" in part) {
|
|
430
|
+
texts.push(String(part.text));
|
|
431
|
+
}
|
|
432
|
+
// Handle { type: "tool_result", content: "..." }
|
|
433
|
+
else if ("type" in part && part.type === "tool_result" && "content" in part) {
|
|
434
|
+
const inner = extractTextFromContent(part.content);
|
|
435
|
+
if (inner) texts.push(inner);
|
|
436
|
+
}
|
|
437
|
+
// Handle { text: "..." } without type
|
|
438
|
+
else if ("text" in part) {
|
|
439
|
+
texts.push(String(part.text));
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return texts.join("\n");
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// ============================================================================
|
|
447
|
+
// Concurrency Utilities
|
|
448
|
+
// ============================================================================
|
|
449
|
+
|
|
450
|
+
export { mapConcurrent } from "../runs/shared/parallel-utils.ts";
|