wave-agent-sdk 0.17.1 → 0.17.2
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/builtin/skills/deep-research/SKILL.md +90 -0
- package/builtin/skills/settings/ENV.md +6 -3
- package/dist/agent.d.ts +28 -1
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +128 -34
- package/dist/constants/goalPrompts.d.ts +2 -0
- package/dist/constants/goalPrompts.d.ts.map +1 -0
- package/dist/constants/goalPrompts.js +10 -0
- package/dist/constants/tools.d.ts +1 -0
- package/dist/constants/tools.d.ts.map +1 -1
- package/dist/constants/tools.js +1 -0
- package/dist/managers/aiManager.d.ts +7 -0
- package/dist/managers/aiManager.d.ts.map +1 -1
- package/dist/managers/aiManager.js +77 -41
- package/dist/managers/backgroundTaskManager.d.ts.map +1 -1
- package/dist/managers/backgroundTaskManager.js +10 -2
- package/dist/managers/goalManager.d.ts +43 -0
- package/dist/managers/goalManager.d.ts.map +1 -0
- package/dist/managers/goalManager.js +177 -0
- package/dist/managers/messageManager.d.ts +2 -2
- package/dist/managers/messageManager.d.ts.map +1 -1
- package/dist/managers/messageQueue.d.ts +10 -0
- package/dist/managers/messageQueue.d.ts.map +1 -1
- package/dist/managers/messageQueue.js +53 -1
- package/dist/managers/pluginManager.d.ts.map +1 -1
- package/dist/managers/pluginManager.js +7 -1
- package/dist/managers/skillManager.d.ts +2 -0
- package/dist/managers/skillManager.d.ts.map +1 -1
- package/dist/managers/skillManager.js +19 -9
- package/dist/managers/slashCommandManager.d.ts +6 -0
- package/dist/managers/slashCommandManager.d.ts.map +1 -1
- package/dist/managers/slashCommandManager.js +105 -0
- package/dist/managers/toolManager.d.ts.map +1 -1
- package/dist/managers/toolManager.js +5 -0
- package/dist/managers/workflowManager.d.ts +65 -0
- package/dist/managers/workflowManager.d.ts.map +1 -0
- package/dist/managers/workflowManager.js +380 -0
- package/dist/prompts/index.d.ts +2 -1
- package/dist/prompts/index.d.ts.map +1 -1
- package/dist/prompts/index.js +3 -3
- package/dist/services/aiService.d.ts +23 -0
- package/dist/services/aiService.d.ts.map +1 -1
- package/dist/services/aiService.js +102 -9
- package/dist/services/configurationService.d.ts +1 -1
- package/dist/services/configurationService.d.ts.map +1 -1
- package/dist/services/configurationService.js +3 -16
- package/dist/services/hook.d.ts.map +1 -1
- package/dist/services/hook.js +4 -0
- package/dist/services/session.d.ts +9 -1
- package/dist/services/session.d.ts.map +1 -1
- package/dist/services/session.js +28 -1
- package/dist/tools/bashTool.d.ts.map +1 -1
- package/dist/tools/bashTool.js +49 -7
- package/dist/tools/readTool.d.ts.map +1 -1
- package/dist/tools/readTool.js +1 -1
- package/dist/tools/taskManagementTools.d.ts.map +1 -1
- package/dist/tools/taskManagementTools.js +103 -157
- package/dist/tools/types.d.ts +2 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/webFetchTool.d.ts.map +1 -1
- package/dist/tools/webFetchTool.js +0 -9
- package/dist/tools/workflowTool.d.ts +11 -0
- package/dist/tools/workflowTool.d.ts.map +1 -0
- package/dist/tools/workflowTool.js +190 -0
- package/dist/types/agent.d.ts +2 -0
- package/dist/types/agent.d.ts.map +1 -1
- package/dist/types/commands.d.ts +4 -0
- package/dist/types/commands.d.ts.map +1 -1
- package/dist/types/config.d.ts +2 -2
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/core.d.ts +1 -1
- package/dist/types/core.d.ts.map +1 -1
- package/dist/types/hooks.d.ts +2 -0
- package/dist/types/hooks.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -0
- package/dist/types/messaging.d.ts +2 -2
- package/dist/types/messaging.d.ts.map +1 -1
- package/dist/types/processes.d.ts +6 -2
- package/dist/types/processes.d.ts.map +1 -1
- package/dist/types/workflow.d.ts +2 -0
- package/dist/types/workflow.d.ts.map +1 -0
- package/dist/types/workflow.js +1 -0
- package/dist/utils/cacheControlUtils.d.ts +13 -8
- package/dist/utils/cacheControlUtils.d.ts.map +1 -1
- package/dist/utils/cacheControlUtils.js +73 -102
- package/dist/utils/containerSetup.d.ts.map +1 -1
- package/dist/utils/containerSetup.js +7 -0
- package/dist/utils/markdownParser.d.ts.map +1 -1
- package/dist/utils/markdownParser.js +21 -6
- package/dist/utils/messageOperations.d.ts +2 -2
- package/dist/utils/messageOperations.d.ts.map +1 -1
- package/dist/utils/notificationXml.d.ts.map +1 -1
- package/dist/workflow/budgetTracker.d.ts +12 -0
- package/dist/workflow/budgetTracker.d.ts.map +1 -0
- package/dist/workflow/budgetTracker.js +30 -0
- package/dist/workflow/concurrencyLimiter.d.ts +14 -0
- package/dist/workflow/concurrencyLimiter.d.ts.map +1 -0
- package/dist/workflow/concurrencyLimiter.js +39 -0
- package/dist/workflow/journal.d.ts +19 -0
- package/dist/workflow/journal.d.ts.map +1 -0
- package/dist/workflow/journal.js +74 -0
- package/dist/workflow/progressReporter.d.ts +21 -0
- package/dist/workflow/progressReporter.d.ts.map +1 -0
- package/dist/workflow/progressReporter.js +118 -0
- package/dist/workflow/runState.d.ts +16 -0
- package/dist/workflow/runState.d.ts.map +1 -0
- package/dist/workflow/runState.js +57 -0
- package/dist/workflow/scriptRuntime.d.ts +35 -0
- package/dist/workflow/scriptRuntime.d.ts.map +1 -0
- package/dist/workflow/scriptRuntime.js +196 -0
- package/dist/workflow/structuredOutput.d.ts +27 -0
- package/dist/workflow/structuredOutput.d.ts.map +1 -0
- package/dist/workflow/structuredOutput.js +106 -0
- package/dist/workflow/types.d.ts +81 -0
- package/dist/workflow/types.d.ts.map +1 -0
- package/dist/workflow/types.js +1 -0
- package/dist/workflow/workflowApis.d.ts +46 -0
- package/dist/workflow/workflowApis.d.ts.map +1 -0
- package/dist/workflow/workflowApis.js +280 -0
- package/package.json +1 -1
- package/src/agent.ts +144 -34
- package/src/constants/goalPrompts.ts +10 -0
- package/src/constants/tools.ts +1 -0
- package/src/managers/aiManager.ts +91 -47
- package/src/managers/backgroundTaskManager.ts +16 -4
- package/src/managers/goalManager.ts +232 -0
- package/src/managers/messageManager.ts +2 -2
- package/src/managers/messageQueue.ts +59 -1
- package/src/managers/pluginManager.ts +8 -1
- package/src/managers/skillManager.ts +20 -9
- package/src/managers/slashCommandManager.ts +119 -0
- package/src/managers/toolManager.ts +7 -0
- package/src/managers/workflowManager.ts +491 -0
- package/src/prompts/index.ts +4 -2
- package/src/services/aiService.ts +166 -12
- package/src/services/configurationService.ts +2 -22
- package/src/services/hook.ts +5 -0
- package/src/services/session.ts +42 -2
- package/src/tools/bashTool.ts +64 -9
- package/src/tools/readTool.ts +1 -2
- package/src/tools/taskManagementTools.ts +146 -195
- package/src/tools/types.ts +2 -0
- package/src/tools/webFetchTool.ts +0 -12
- package/src/tools/workflowTool.ts +205 -0
- package/src/types/agent.ts +6 -0
- package/src/types/commands.ts +4 -0
- package/src/types/config.ts +2 -2
- package/src/types/core.ts +3 -3
- package/src/types/hooks.ts +2 -0
- package/src/types/index.ts +1 -0
- package/src/types/messaging.ts +2 -2
- package/src/types/processes.ts +10 -2
- package/src/types/workflow.ts +5 -0
- package/src/utils/cacheControlUtils.ts +106 -131
- package/src/utils/containerSetup.ts +9 -0
- package/src/utils/markdownParser.ts +26 -8
- package/src/utils/messageOperations.ts +2 -2
- package/src/utils/notificationXml.ts +6 -1
- package/src/workflow/budgetTracker.ts +34 -0
- package/src/workflow/concurrencyLimiter.ts +47 -0
- package/src/workflow/journal.ts +95 -0
- package/src/workflow/progressReporter.ts +141 -0
- package/src/workflow/runState.ts +65 -0
- package/src/workflow/scriptRuntime.ts +274 -0
- package/src/workflow/structuredOutput.ts +123 -0
- package/src/workflow/types.ts +95 -0
- package/src/workflow/workflowApis.ts +412 -0
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
import type { SubagentManager } from "../managers/subagentManager.js";
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import { ConcurrencyLimiter } from "./concurrencyLimiter.js";
|
|
5
|
+
import { BudgetTracker } from "./budgetTracker.js";
|
|
6
|
+
import { ProgressReporter } from "./progressReporter.js";
|
|
7
|
+
import { Journal } from "./journal.js";
|
|
8
|
+
import type { BudgetInfo, AgentMeta, WorkflowProgressEvent } from "./types.js";
|
|
9
|
+
import {
|
|
10
|
+
createStructuredOutputPrompt,
|
|
11
|
+
createStructuredOutputTool,
|
|
12
|
+
extractStructuredResult,
|
|
13
|
+
} from "./structuredOutput.js";
|
|
14
|
+
import { AGENT_TOOL_NAME, WORKFLOW_TOOL_NAME } from "../constants/tools.js";
|
|
15
|
+
import { logger } from "../utils/globalLogger.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Attempt to parse args if it was passed as a JSON string.
|
|
19
|
+
* LLMs may serialize array/object args as a string (e.g. "[3,8,15,42,7]")
|
|
20
|
+
* which would cause `for...of` to iterate character-by-character.
|
|
21
|
+
*/
|
|
22
|
+
function parseArgsIfNeeded(args: unknown): unknown {
|
|
23
|
+
if (typeof args === "string") {
|
|
24
|
+
try {
|
|
25
|
+
const parsed = JSON.parse(args);
|
|
26
|
+
// Only accept parsed arrays/objects, not primitives like "42" → 42
|
|
27
|
+
if (typeof parsed === "object" && parsed !== null) {
|
|
28
|
+
return parsed;
|
|
29
|
+
}
|
|
30
|
+
} catch {
|
|
31
|
+
// Not valid JSON — use as-is (string)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return args;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const MAX_TOTAL_AGENTS = 1000;
|
|
38
|
+
const MAX_ITEMS_PER_CALL = 4096;
|
|
39
|
+
|
|
40
|
+
interface WorkflowApiContext {
|
|
41
|
+
subagentManager: SubagentManager;
|
|
42
|
+
concurrencyLimiter: ConcurrencyLimiter;
|
|
43
|
+
budgetTracker: BudgetTracker;
|
|
44
|
+
progressReporter: ProgressReporter;
|
|
45
|
+
journal: Journal;
|
|
46
|
+
abortSignal: AbortSignal;
|
|
47
|
+
args: unknown;
|
|
48
|
+
onLog?: (message: string) => void;
|
|
49
|
+
/** When resuming, offset the agent counter by this many existing entries */
|
|
50
|
+
initialAgentCount?: number;
|
|
51
|
+
/** Session directory for computing transcript paths */
|
|
52
|
+
sessionDir: string;
|
|
53
|
+
/** Per-run directory for writing agent metadata sidecars */
|
|
54
|
+
runDir: string;
|
|
55
|
+
/** Per-agent abort controllers for kill/skip support */
|
|
56
|
+
agentControllers: Map<number, AbortController>;
|
|
57
|
+
/** Optional callback for progress events */
|
|
58
|
+
onProgress?: (event: WorkflowProgressEvent) => void;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface AgentOpts {
|
|
62
|
+
label?: string;
|
|
63
|
+
phase?: string;
|
|
64
|
+
schema?: object;
|
|
65
|
+
model?: string;
|
|
66
|
+
isolation?: string;
|
|
67
|
+
agentType?: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface WorkflowApis {
|
|
71
|
+
agent: (prompt: string, opts?: AgentOpts) => Promise<unknown>;
|
|
72
|
+
parallel: (thunks: Array<() => Promise<unknown>>) => Promise<unknown[]>;
|
|
73
|
+
pipeline: (
|
|
74
|
+
items: unknown[],
|
|
75
|
+
...stages: Array<
|
|
76
|
+
(prev: unknown, item: unknown, index: number) => Promise<unknown>
|
|
77
|
+
>
|
|
78
|
+
) => Promise<unknown[]>;
|
|
79
|
+
phase: (title: string) => void;
|
|
80
|
+
log: (message: string) => void;
|
|
81
|
+
args: unknown;
|
|
82
|
+
budget: BudgetInfo;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function createWorkflowApis(ctx: WorkflowApiContext): WorkflowApis {
|
|
86
|
+
let agentCounter = ctx.initialAgentCount ?? 0;
|
|
87
|
+
|
|
88
|
+
const agent = async (prompt: string, opts?: AgentOpts): Promise<unknown> => {
|
|
89
|
+
const index = agentCounter++;
|
|
90
|
+
|
|
91
|
+
// Check agent limit
|
|
92
|
+
if (index >= MAX_TOTAL_AGENTS) {
|
|
93
|
+
throw new Error(
|
|
94
|
+
`Workflow exceeded maximum agent count of ${MAX_TOTAL_AGENTS}`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Check abort
|
|
99
|
+
if (ctx.abortSignal.aborted) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Check budget
|
|
104
|
+
if (ctx.budgetTracker.isExceeded()) {
|
|
105
|
+
throw new Error("Workflow token budget exceeded");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Check journal for cached result (resume)
|
|
109
|
+
const cached = ctx.journal.getCachedResult(index);
|
|
110
|
+
if (cached !== undefined) {
|
|
111
|
+
logger.debug(`[Workflow] agent(${index}): using cached result`);
|
|
112
|
+
return cached;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Acquire concurrency slot
|
|
116
|
+
await ctx.concurrencyLimiter.acquire();
|
|
117
|
+
|
|
118
|
+
// Create per-agent abort controller linked to the run's signal
|
|
119
|
+
const agentController = new AbortController();
|
|
120
|
+
ctx.agentControllers.set(index, agentController);
|
|
121
|
+
// If the run is already aborted, abort this agent immediately
|
|
122
|
+
if (ctx.abortSignal.aborted) {
|
|
123
|
+
agentController.abort();
|
|
124
|
+
}
|
|
125
|
+
// Propagate run abort to agent abort
|
|
126
|
+
const onRunAbort = () => agentController.abort();
|
|
127
|
+
ctx.abortSignal.addEventListener("abort", onRunAbort);
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
// Resolve subagent type
|
|
131
|
+
const subagentType = opts?.agentType || "general-purpose";
|
|
132
|
+
let configuration = await ctx.subagentManager.findSubagent(subagentType);
|
|
133
|
+
|
|
134
|
+
if (!configuration) {
|
|
135
|
+
logger.warn(
|
|
136
|
+
`[Workflow] agent(${index}): subagent type "${subagentType}" not found, falling back to general-purpose`,
|
|
137
|
+
);
|
|
138
|
+
configuration =
|
|
139
|
+
await ctx.subagentManager.findSubagent("general-purpose");
|
|
140
|
+
if (!configuration) {
|
|
141
|
+
throw new Error(`No subagent type available for agent call`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Build the effective prompt
|
|
146
|
+
let effectivePrompt = prompt;
|
|
147
|
+
if (opts?.schema) {
|
|
148
|
+
effectivePrompt += createStructuredOutputPrompt(opts.schema);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Set phase if specified
|
|
152
|
+
if (opts?.phase) {
|
|
153
|
+
ctx.progressReporter.setPhase(opts.phase);
|
|
154
|
+
}
|
|
155
|
+
ctx.progressReporter.agentStarted();
|
|
156
|
+
|
|
157
|
+
// Create subagent instance
|
|
158
|
+
const instance = await ctx.subagentManager.createInstance(configuration, {
|
|
159
|
+
description: opts?.label || `workflow-agent-${index}`,
|
|
160
|
+
prompt: effectivePrompt,
|
|
161
|
+
subagent_type: subagentType,
|
|
162
|
+
model: opts?.model,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// Capture subagent linkage info
|
|
166
|
+
const subagentId = instance.subagentId;
|
|
167
|
+
const transcriptPath = instance.messageManager.getTranscriptPath();
|
|
168
|
+
|
|
169
|
+
// If schema provided, register StructuredOutput tool on the subagent's tool manager
|
|
170
|
+
// and force the model to call it via tool_choice
|
|
171
|
+
if (opts?.schema) {
|
|
172
|
+
const structuredTool = createStructuredOutputTool(opts.schema);
|
|
173
|
+
instance.toolManager.register(structuredTool);
|
|
174
|
+
instance.aiManager.toolChoiceOverride = {
|
|
175
|
+
type: "function",
|
|
176
|
+
function: { name: "StructuredOutput" },
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Deny Agent and Workflow tools in workflow subagents
|
|
181
|
+
// (prevent infinite recursion)
|
|
182
|
+
instance.permissionManager.addTemporaryRules([
|
|
183
|
+
`${AGENT_TOOL_NAME}:deny`,
|
|
184
|
+
`${WORKFLOW_TOOL_NAME}:deny`,
|
|
185
|
+
]);
|
|
186
|
+
|
|
187
|
+
// Execute agent
|
|
188
|
+
const result = await ctx.subagentManager.executeAgent(
|
|
189
|
+
instance,
|
|
190
|
+
effectivePrompt,
|
|
191
|
+
agentController.signal,
|
|
192
|
+
false,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
// Track token usage — sum from assistant messages' usage field
|
|
196
|
+
// (getUsages() is empty because onUsageAdded targets the parent agent)
|
|
197
|
+
const messages = instance.messageManager.getMessages();
|
|
198
|
+
const tokens = messages.reduce((sum, msg) => {
|
|
199
|
+
if (msg.role !== "assistant" || !msg.usage) return sum;
|
|
200
|
+
const u = msg.usage;
|
|
201
|
+
return (
|
|
202
|
+
sum +
|
|
203
|
+
(u.total_tokens || 0) +
|
|
204
|
+
(u.cache_read_input_tokens || 0) +
|
|
205
|
+
(u.cache_creation_input_tokens || 0)
|
|
206
|
+
);
|
|
207
|
+
}, 0);
|
|
208
|
+
ctx.budgetTracker.addUsage(tokens);
|
|
209
|
+
ctx.progressReporter.agentCompleted(tokens);
|
|
210
|
+
|
|
211
|
+
// Extract structured result if schema was provided
|
|
212
|
+
let finalResult: unknown;
|
|
213
|
+
if (opts?.schema) {
|
|
214
|
+
const messages = instance.messageManager.getMessages();
|
|
215
|
+
// Build the format extractStructuredResult expects:
|
|
216
|
+
// Look for StructuredOutput tool calls in ToolBlocks
|
|
217
|
+
const structuredToolBlocks = messages
|
|
218
|
+
.filter((m) => m.role === "assistant")
|
|
219
|
+
.flatMap((m) => m.blocks)
|
|
220
|
+
.filter(
|
|
221
|
+
(b): b is import("../types/messaging.js").ToolBlock =>
|
|
222
|
+
b.type === "tool" &&
|
|
223
|
+
b.name === "StructuredOutput" &&
|
|
224
|
+
b.stage === "end",
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
if (structuredToolBlocks.length > 0) {
|
|
228
|
+
// Parse the StructuredOutput tool call parameters
|
|
229
|
+
const lastBlock =
|
|
230
|
+
structuredToolBlocks[structuredToolBlocks.length - 1];
|
|
231
|
+
try {
|
|
232
|
+
const parsed = JSON.parse(lastBlock.parameters || "{}");
|
|
233
|
+
finalResult = parsed;
|
|
234
|
+
} catch {
|
|
235
|
+
// Parameters parse failed, try result
|
|
236
|
+
try {
|
|
237
|
+
const parsed = JSON.parse(lastBlock.result || "null");
|
|
238
|
+
finalResult = parsed;
|
|
239
|
+
} catch {
|
|
240
|
+
finalResult = result;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
} else {
|
|
244
|
+
// Fallback: try extractStructuredResult with message content
|
|
245
|
+
finalResult = extractStructuredResult(
|
|
246
|
+
messages.map((m) => {
|
|
247
|
+
const textBlock = m.blocks.find(
|
|
248
|
+
(b): b is import("../types/messaging.js").TextBlock =>
|
|
249
|
+
b.type === "text",
|
|
250
|
+
);
|
|
251
|
+
return {
|
|
252
|
+
role: m.role,
|
|
253
|
+
content: textBlock?.content,
|
|
254
|
+
tool_calls: (
|
|
255
|
+
m as unknown as {
|
|
256
|
+
tool_calls?: Array<{
|
|
257
|
+
function: { name: string; arguments: string };
|
|
258
|
+
}>;
|
|
259
|
+
}
|
|
260
|
+
).tool_calls,
|
|
261
|
+
};
|
|
262
|
+
}),
|
|
263
|
+
opts.schema,
|
|
264
|
+
);
|
|
265
|
+
if (finalResult === null) {
|
|
266
|
+
// Schema enforcement failed — return the raw text
|
|
267
|
+
finalResult = result;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
} else {
|
|
271
|
+
finalResult = result;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Append to journal
|
|
275
|
+
ctx.journal.append({
|
|
276
|
+
agentIndex: index,
|
|
277
|
+
prompt,
|
|
278
|
+
opts: { ...opts } as Record<string, unknown>,
|
|
279
|
+
result: finalResult,
|
|
280
|
+
tokens,
|
|
281
|
+
subagentId,
|
|
282
|
+
transcriptPath,
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// Write agent metadata sidecar
|
|
286
|
+
try {
|
|
287
|
+
const agentMeta: AgentMeta = {
|
|
288
|
+
agentType: subagentType,
|
|
289
|
+
subagentId,
|
|
290
|
+
transcriptPath,
|
|
291
|
+
label: opts?.label,
|
|
292
|
+
phase: opts?.phase,
|
|
293
|
+
};
|
|
294
|
+
const metaPath = path.join(ctx.runDir, "agents", `${index}.meta.json`);
|
|
295
|
+
await fs.promises.writeFile(
|
|
296
|
+
metaPath,
|
|
297
|
+
JSON.stringify(agentMeta, null, 2),
|
|
298
|
+
"utf-8",
|
|
299
|
+
);
|
|
300
|
+
} catch (metaErr) {
|
|
301
|
+
logger.warn(
|
|
302
|
+
`[Workflow] Failed to write agent meta for ${index}: ${metaErr instanceof Error ? metaErr.message : String(metaErr)}`,
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Cleanup
|
|
307
|
+
ctx.subagentManager.cleanupInstance(instance.subagentId);
|
|
308
|
+
|
|
309
|
+
return finalResult;
|
|
310
|
+
} catch (error) {
|
|
311
|
+
// Agent errors are logged but don't crash the workflow
|
|
312
|
+
// Return null so the caller can filter with .filter(Boolean)
|
|
313
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
314
|
+
logger.warn(`[Workflow] agent(${index}) failed: ${errorMsg}`);
|
|
315
|
+
|
|
316
|
+
// Write agent_failed entry to journal for skip/retry tracking
|
|
317
|
+
ctx.journal.append({
|
|
318
|
+
type: "agent_failed",
|
|
319
|
+
agentIndex: index,
|
|
320
|
+
error: errorMsg,
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// Emit progress event for agent failure
|
|
324
|
+
ctx.progressReporter.agentFailed(index);
|
|
325
|
+
|
|
326
|
+
return null;
|
|
327
|
+
} finally {
|
|
328
|
+
ctx.agentControllers.delete(index);
|
|
329
|
+
ctx.abortSignal.removeEventListener("abort", onRunAbort);
|
|
330
|
+
ctx.concurrencyLimiter.release();
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const parallel = async (
|
|
335
|
+
thunks: Array<() => Promise<unknown>>,
|
|
336
|
+
): Promise<unknown[]> => {
|
|
337
|
+
if (thunks.length > MAX_ITEMS_PER_CALL) {
|
|
338
|
+
throw new Error(
|
|
339
|
+
`parallel() accepts at most ${MAX_ITEMS_PER_CALL} items, got ${thunks.length}`,
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Note: thunks typically call agent() which acquires its own slot,
|
|
344
|
+
// so parallel does NOT acquire a slot per thunk to avoid deadlock.
|
|
345
|
+
const results = await Promise.allSettled(
|
|
346
|
+
thunks.map(async (thunk) => {
|
|
347
|
+
try {
|
|
348
|
+
return await thunk();
|
|
349
|
+
} catch {
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
}),
|
|
353
|
+
);
|
|
354
|
+
|
|
355
|
+
// Convert rejected promises to null
|
|
356
|
+
return results.map((r) => (r.status === "fulfilled" ? r.value : null));
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
const pipeline = async (
|
|
360
|
+
items: unknown[],
|
|
361
|
+
...stages: Array<
|
|
362
|
+
(prev: unknown, item: unknown, index: number) => Promise<unknown>
|
|
363
|
+
>
|
|
364
|
+
): Promise<unknown[]> => {
|
|
365
|
+
if (items.length > MAX_ITEMS_PER_CALL) {
|
|
366
|
+
throw new Error(
|
|
367
|
+
`pipeline() accepts at most ${MAX_ITEMS_PER_CALL} items, got ${items.length}`,
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Run each item through all stages, items are independent.
|
|
372
|
+
// Note: agent() inside stages acquires its own concurrency slot,
|
|
373
|
+
// so pipeline does NOT acquire a slot per item to avoid deadlock.
|
|
374
|
+
const results = await Promise.allSettled(
|
|
375
|
+
items.map(async (item, index) => {
|
|
376
|
+
try {
|
|
377
|
+
let result: unknown = undefined;
|
|
378
|
+
for (const stage of stages) {
|
|
379
|
+
result = await stage(result, item, index);
|
|
380
|
+
}
|
|
381
|
+
return result;
|
|
382
|
+
} catch (error) {
|
|
383
|
+
logger.warn(
|
|
384
|
+
`[Workflow] pipeline item ${index} failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
385
|
+
);
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
}),
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
return results.map((r) => (r.status === "fulfilled" ? r.value : null));
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
const phase = (title: string): void => {
|
|
395
|
+
ctx.progressReporter.setPhase(title);
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
const log = (message: string): void => {
|
|
399
|
+
ctx.journal.appendLog(message);
|
|
400
|
+
ctx.onLog?.(message);
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
return {
|
|
404
|
+
agent,
|
|
405
|
+
parallel,
|
|
406
|
+
pipeline,
|
|
407
|
+
phase,
|
|
408
|
+
log,
|
|
409
|
+
args: parseArgsIfNeeded(ctx.args),
|
|
410
|
+
budget: ctx.budgetTracker.toBudgetInfo(),
|
|
411
|
+
};
|
|
412
|
+
}
|