@lukstei/dsh-yaml-workflow 0.0.0-stage → 0.2.2-rc.5.4
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/LICENSE +21 -0
- package/README.md +452 -2
- package/cordis.patch.yml +4 -0
- package/dist/client.js +1746 -0
- package/dist/index.js +3495 -0
- package/examples/gate.yml +4 -0
- package/examples/parametrized-wf.yml +20 -0
- package/examples/wf.yml +3 -0
- package/package.json +114 -3
- package/src/boolean.ts +96 -0
- package/src/client/WorkflowCommandCard.tsx +217 -0
- package/src/client/WorkflowErrorCard.tsx +99 -0
- package/src/client/WorkflowMarkdown.tsx +28 -0
- package/src/client/WorkflowMarkdownResult.tsx +86 -0
- package/src/client/WorkflowPrintStep.tsx +46 -0
- package/src/client/WorkflowResumeButton.tsx +159 -0
- package/src/client/WorkflowStepRow.tsx +106 -0
- package/src/client/WorkflowStyles.tsx +10 -0
- package/src/client/actions.ts +113 -0
- package/src/client/context.ts +30 -0
- package/src/client/dsh.d.ts +33 -0
- package/src/client/icons.tsx +91 -0
- package/src/client/index.tsx +186 -0
- package/src/client/store.ts +655 -0
- package/src/client/styles.ts +340 -0
- package/src/effects.ts +696 -0
- package/src/fold.ts +201 -0
- package/src/index.ts +468 -0
- package/src/parser.ts +1146 -0
- package/src/requests.ts +85 -0
- package/src/rules.ts +647 -0
- package/src/runner.ts +1955 -0
- package/src/skill.ts +67 -0
- package/src/transitions.ts +357 -0
- package/src/types.ts +604 -0
package/src/effects.ts
ADDED
|
@@ -0,0 +1,696 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PURPOSE: Execution effect boundary and Cordis/DSH runtime implementation.
|
|
3
|
+
* ALLOWED: Async execution, agent followups, subagent activation, event appending.
|
|
4
|
+
* FORBIDDEN: Direct workflow parsing or state transitions (delegate to pure core).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
8
|
+
import { homedir } from "node:os";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import type { Context, Logger } from "@deepseek-ai/cordis";
|
|
11
|
+
import type { Agent } from "@deepseek-ai/dsh-agent";
|
|
12
|
+
import type { CommandId } from "@deepseek-ai/dsh-commands";
|
|
13
|
+
import { createUserMessage, ReasoningEffortId } from "@deepseek-ai/dsh-llm";
|
|
14
|
+
import type { SessionEventMap } from "@deepseek-ai/dsh-session";
|
|
15
|
+
import type { SessionId } from "@deepseek-ai/dsh-session/types";
|
|
16
|
+
import type {} from "@deepseek-ai/dsh-session-title";
|
|
17
|
+
import { parentAgentOptionsForDelegation } from "@deepseek-ai/dsh-subagent";
|
|
18
|
+
import {
|
|
19
|
+
type FileOutputBase,
|
|
20
|
+
type SubagentConfig,
|
|
21
|
+
type SubagentDispatchResult,
|
|
22
|
+
WORKFLOW_EVENT_NAME,
|
|
23
|
+
type WorkflowDocument,
|
|
24
|
+
type WorkflowEffect,
|
|
25
|
+
type WorkflowInvocation,
|
|
26
|
+
type WorkflowOutputPayloadData,
|
|
27
|
+
type WorkflowPayload,
|
|
28
|
+
type WorkflowState,
|
|
29
|
+
} from "./types.ts";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* PURPOSE: Abstract execution effect boundary for workflows.
|
|
33
|
+
* CONTRACT:
|
|
34
|
+
* - Subclasses define runtime-specific I/O (DSH/Cordis vs. in-memory test runner).
|
|
35
|
+
* - Stateless workflow runner delegates all side effects through this contract.
|
|
36
|
+
*/
|
|
37
|
+
export abstract class WorkflowEffectContext {
|
|
38
|
+
abstract readonly sessionId: SessionId;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Resolves a workflow target path or well-known name to an existing workflow file path.
|
|
42
|
+
* CONTRACT:
|
|
43
|
+
* - Inputs: targetPath string.
|
|
44
|
+
* - Outputs: Promise resolving to the absolute file path.
|
|
45
|
+
* - Throws: Error when workflow file cannot be resolved.
|
|
46
|
+
*/
|
|
47
|
+
abstract resolveWorkflowFile(targetPath: string): Promise<string>;
|
|
48
|
+
|
|
49
|
+
/** Reads raw workflow file content from specified target path */
|
|
50
|
+
abstract readWorkflow(targetPath: string): Promise<string>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Lists all workflow files (.yaml and .yml) in the specified scope ("local" or "common").
|
|
54
|
+
* CONTRACT:
|
|
55
|
+
* - Inputs: scope: "local" (session cwd/.agents/wf) or "common" (~/.agents/wf).
|
|
56
|
+
* - Outputs: Promise resolving to array of target identifier and raw string content.
|
|
57
|
+
* - Throws: None (missing directories return empty array).
|
|
58
|
+
*/
|
|
59
|
+
abstract listWorkflowFiles(
|
|
60
|
+
scope: "local" | "common",
|
|
61
|
+
): Promise<
|
|
62
|
+
ReadonlyArray<{ readonly target: string; readonly content: string }>
|
|
63
|
+
>;
|
|
64
|
+
|
|
65
|
+
/** Dispatches an interactive prompt to the agent and awaits completion */
|
|
66
|
+
abstract dispatchPrompt(
|
|
67
|
+
effect: Extract<WorkflowEffect, { type: "prompt" }>,
|
|
68
|
+
): Promise<void>;
|
|
69
|
+
|
|
70
|
+
/** Dispatches a subagent activation and awaits completion, returning the output text and optional structured data */
|
|
71
|
+
abstract dispatchSubagent(
|
|
72
|
+
effect: Extract<WorkflowEffect, { type: "subagent" }>,
|
|
73
|
+
signal?: AbortSignal,
|
|
74
|
+
): Promise<SubagentDispatchResult>;
|
|
75
|
+
|
|
76
|
+
/** Validates that a subagent step's provider, adapter route, and model are supported and registered */
|
|
77
|
+
abstract validateSubagentRoute(
|
|
78
|
+
config: SubagentConfig,
|
|
79
|
+
signal?: AbortSignal,
|
|
80
|
+
): Promise<
|
|
81
|
+
| { readonly valid: true }
|
|
82
|
+
| { readonly valid: false; readonly message: string }
|
|
83
|
+
>;
|
|
84
|
+
|
|
85
|
+
/** Emits informational log messages regarding workflow lifecycle */
|
|
86
|
+
abstract logInfo(message: string): void;
|
|
87
|
+
|
|
88
|
+
/** Resolves the fully-qualified target path for a file output */
|
|
89
|
+
abstract resolveOutputPath(filePath: string, base: FileOutputBase): string;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Subscribes an AbortController to agent/turn interruption events during execution.
|
|
93
|
+
* CONTRACT:
|
|
94
|
+
* - Inputs: AbortController instance.
|
|
95
|
+
* - Outputs: Cleanup function to remove event subscriptions.
|
|
96
|
+
*/
|
|
97
|
+
abstract watchAbort(controller: AbortController): () => void;
|
|
98
|
+
|
|
99
|
+
/** Checks for a non-empty file; propagates filesystem errors other than absence. */
|
|
100
|
+
abstract checkFileWritten(resolvedPath: string): Promise<boolean>;
|
|
101
|
+
|
|
102
|
+
/** Ensures the scratch directory exists, writing .gitignore if base scratch directory is created */
|
|
103
|
+
abstract ensureScratchDirectory(): Promise<string>;
|
|
104
|
+
|
|
105
|
+
/** Records a verified workflow output in session events */
|
|
106
|
+
recordOutput(
|
|
107
|
+
stepIndex: number,
|
|
108
|
+
name: string,
|
|
109
|
+
value: string,
|
|
110
|
+
outputType: "file" | "string" | "list",
|
|
111
|
+
base: FileOutputBase | undefined,
|
|
112
|
+
runId: string,
|
|
113
|
+
): void {
|
|
114
|
+
this.emit({
|
|
115
|
+
action: "output",
|
|
116
|
+
stepIndex,
|
|
117
|
+
name,
|
|
118
|
+
value,
|
|
119
|
+
outputType,
|
|
120
|
+
...(base ? { base } : {}),
|
|
121
|
+
runId,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Retrieves a previously captured output for a step from session events */
|
|
126
|
+
getCapturedOutput(stepIndex: number, name: string): string | undefined {
|
|
127
|
+
const events = this.snapshotEvents();
|
|
128
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
129
|
+
const evt = events[i];
|
|
130
|
+
if (evt.type !== "command/run") continue;
|
|
131
|
+
const data = evt.data as { name?: string; args?: string } | undefined;
|
|
132
|
+
if (data?.name !== WORKFLOW_EVENT_NAME || typeof data.args !== "string")
|
|
133
|
+
continue;
|
|
134
|
+
try {
|
|
135
|
+
const payload = JSON.parse(data.args) as WorkflowPayload;
|
|
136
|
+
if (
|
|
137
|
+
payload.action === "output" &&
|
|
138
|
+
payload.stepIndex === stepIndex &&
|
|
139
|
+
payload.name === name
|
|
140
|
+
) {
|
|
141
|
+
return payload.value;
|
|
142
|
+
}
|
|
143
|
+
} catch {
|
|
144
|
+
// ignore parse errors
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Appends a low-level session event */
|
|
151
|
+
abstract appendSessionEvent(
|
|
152
|
+
type: "command/run",
|
|
153
|
+
data: SessionEventMap["command/run"],
|
|
154
|
+
): void;
|
|
155
|
+
abstract appendSessionEvent(
|
|
156
|
+
type: "command/done",
|
|
157
|
+
data: SessionEventMap["command/done"],
|
|
158
|
+
): void;
|
|
159
|
+
abstract appendSessionEvent(
|
|
160
|
+
type: "session/title",
|
|
161
|
+
data: SessionEventMap["session/title"],
|
|
162
|
+
): void;
|
|
163
|
+
|
|
164
|
+
/** Retrieves the full chronological snapshot of session events */
|
|
165
|
+
abstract snapshotEvents(): readonly {
|
|
166
|
+
readonly type: string;
|
|
167
|
+
readonly data?: unknown;
|
|
168
|
+
}[];
|
|
169
|
+
|
|
170
|
+
/** Updates the agent status */
|
|
171
|
+
abstract setStatus(status: "running" | "idle"): void;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Purpose: Awaits until the underlying agent finishes its active turn.
|
|
175
|
+
* Contract:
|
|
176
|
+
* - Inputs: None.
|
|
177
|
+
* - Outputs: Promise that resolves when the agent is idle.
|
|
178
|
+
* - Throws: None.
|
|
179
|
+
*/
|
|
180
|
+
abstract whenIdle(): Promise<void>;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Purpose: Determines whether a session title has already been set in the event log.
|
|
184
|
+
* Contract:
|
|
185
|
+
* - Inputs: None.
|
|
186
|
+
* - Outputs: True if a session/title event exists; false otherwise.
|
|
187
|
+
*/
|
|
188
|
+
hasTitle(): boolean {
|
|
189
|
+
return this.snapshotEvents().some(
|
|
190
|
+
(event) => event.type === "session/title",
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Sets the session title */
|
|
195
|
+
setTitle(title: string): void {
|
|
196
|
+
this.appendSessionEvent("session/title", {
|
|
197
|
+
title,
|
|
198
|
+
messageSeqs: [],
|
|
199
|
+
source: { kind: "user" },
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Emits a workflow event as paired command/run and command/done records.
|
|
205
|
+
* CONTRACT:
|
|
206
|
+
* - Inputs: workflow lifecycle, request, step, print, or output payload.
|
|
207
|
+
* - Outputs: appends command/run and command/done records to session.
|
|
208
|
+
*/
|
|
209
|
+
emit(
|
|
210
|
+
event:
|
|
211
|
+
| {
|
|
212
|
+
readonly action: "request";
|
|
213
|
+
readonly rawInput: string;
|
|
214
|
+
readonly runId: string;
|
|
215
|
+
}
|
|
216
|
+
| {
|
|
217
|
+
readonly action: "request-failed";
|
|
218
|
+
readonly runId: string;
|
|
219
|
+
}
|
|
220
|
+
| {
|
|
221
|
+
readonly action: "init";
|
|
222
|
+
readonly workflow: WorkflowDocument;
|
|
223
|
+
readonly invocation: WorkflowInvocation;
|
|
224
|
+
readonly runId: string;
|
|
225
|
+
}
|
|
226
|
+
| ({
|
|
227
|
+
readonly action: "state";
|
|
228
|
+
readonly runId: string;
|
|
229
|
+
} & Partial<WorkflowState>)
|
|
230
|
+
| {
|
|
231
|
+
readonly action: "step";
|
|
232
|
+
readonly id: string;
|
|
233
|
+
readonly stepIndex: number;
|
|
234
|
+
readonly label: string;
|
|
235
|
+
readonly kind: "success" | "error";
|
|
236
|
+
readonly runId: string;
|
|
237
|
+
}
|
|
238
|
+
| {
|
|
239
|
+
readonly action: "print";
|
|
240
|
+
readonly id: string;
|
|
241
|
+
readonly text: string;
|
|
242
|
+
readonly runId: string;
|
|
243
|
+
}
|
|
244
|
+
| ({
|
|
245
|
+
readonly action: "output";
|
|
246
|
+
readonly runId: string;
|
|
247
|
+
} & Omit<WorkflowOutputPayloadData, "sessionId">),
|
|
248
|
+
): void {
|
|
249
|
+
const commandId =
|
|
250
|
+
`wf-${event.action}-${this.snapshotEvents().length}` as CommandId;
|
|
251
|
+
|
|
252
|
+
const payload: WorkflowPayload = {
|
|
253
|
+
...event,
|
|
254
|
+
sessionId: this.sessionId,
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
this.appendSessionEvent("command/run", {
|
|
258
|
+
commandId,
|
|
259
|
+
name: WORKFLOW_EVENT_NAME,
|
|
260
|
+
args: JSON.stringify(payload),
|
|
261
|
+
source: { kind: "user" },
|
|
262
|
+
});
|
|
263
|
+
this.appendSessionEvent("command/done", {
|
|
264
|
+
commandId,
|
|
265
|
+
kind: "success",
|
|
266
|
+
text: `Workflow action '${event.action}' recorded`,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export interface DshWorkflowEffectContextOptions {
|
|
272
|
+
readonly ctx: Context;
|
|
273
|
+
readonly agent: Agent;
|
|
274
|
+
readonly log?: Logger;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* PURPOSE: Production effect context binding the runner to DeepSeek Harness / Cordis runtime.
|
|
279
|
+
* CONTRACT:
|
|
280
|
+
* - Inputs: DshWorkflowEffectContextOptions containing Context, Agent, and optional Logger.
|
|
281
|
+
* - Side-effects: Dispatches to Cordis services, reads files via fs/promises, appends to Session.
|
|
282
|
+
*/
|
|
283
|
+
export class DshWorkflowEffectContext extends WorkflowEffectContext {
|
|
284
|
+
readonly sessionId: SessionId;
|
|
285
|
+
private readonly ctx: Context;
|
|
286
|
+
private readonly agent: Agent;
|
|
287
|
+
private readonly log?: Logger;
|
|
288
|
+
private activeAbortController?: AbortController;
|
|
289
|
+
|
|
290
|
+
constructor({ ctx, agent, log }: DshWorkflowEffectContextOptions) {
|
|
291
|
+
super();
|
|
292
|
+
this.ctx = ctx;
|
|
293
|
+
this.agent = agent;
|
|
294
|
+
this.sessionId = agent.id as SessionId;
|
|
295
|
+
this.log = log;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
watchAbort(controller: AbortController): () => void {
|
|
299
|
+
this.activeAbortController = controller;
|
|
300
|
+
// cast: Cordis Context type does not export host session/event interface
|
|
301
|
+
const eventDispose = (
|
|
302
|
+
this.ctx.on as unknown as (
|
|
303
|
+
event: string,
|
|
304
|
+
cb: (session: unknown, event: { type: string; data?: unknown }) => void,
|
|
305
|
+
) => () => void
|
|
306
|
+
)("session/event", (session, event) => {
|
|
307
|
+
if (session === this.agent.session && event.type === "turn/end") {
|
|
308
|
+
const reason = (event.data as { reason?: { kind?: string } })?.reason;
|
|
309
|
+
if (reason?.kind === "aborted") {
|
|
310
|
+
controller.abort({ kind: "pause" });
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
return () => {
|
|
316
|
+
this.activeAbortController = undefined;
|
|
317
|
+
eventDispose();
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
async resolveWorkflowFile(targetPath: string): Promise<string> {
|
|
322
|
+
const cwd = this.agent.session.header.cwd;
|
|
323
|
+
if (!cwd) {
|
|
324
|
+
throw new Error(
|
|
325
|
+
"Session working directory (agent.session.header.cwd) is missing",
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
const baseLocations = path.isAbsolute(targetPath)
|
|
329
|
+
? [targetPath]
|
|
330
|
+
: [
|
|
331
|
+
path.resolve(cwd, targetPath),
|
|
332
|
+
path.resolve(cwd, ".agents/wf", targetPath),
|
|
333
|
+
path.resolve(homedir(), ".agents/wf", targetPath),
|
|
334
|
+
];
|
|
335
|
+
const hasExt = targetPath.endsWith(".yaml") || targetPath.endsWith(".yml");
|
|
336
|
+
for (const base of baseLocations) {
|
|
337
|
+
const candidates = hasExt
|
|
338
|
+
? [base]
|
|
339
|
+
: [base, `${base}.yaml`, `${base}.yml`];
|
|
340
|
+
for (const candidate of candidates) {
|
|
341
|
+
try {
|
|
342
|
+
const s = await stat(candidate);
|
|
343
|
+
if (s.isFile()) {
|
|
344
|
+
return candidate;
|
|
345
|
+
}
|
|
346
|
+
} catch (err: unknown) {
|
|
347
|
+
// edge case: candidate path does not exist, continue search
|
|
348
|
+
const nodeErr = err as NodeJS.ErrnoException;
|
|
349
|
+
if (nodeErr.code !== "ENOENT" && nodeErr.code !== "ENOTDIR") {
|
|
350
|
+
throw err;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
throw new Error(
|
|
356
|
+
`Workflow file not found for target '${targetPath}'. Checked locations relative to cwd and ~/.agents/wf.`,
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
async readWorkflow(targetPath: string): Promise<string> {
|
|
361
|
+
const resolvedPath = await this.resolveWorkflowFile(targetPath);
|
|
362
|
+
return readFile(resolvedPath, "utf8");
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async listWorkflowFiles(
|
|
366
|
+
scope: "local" | "common",
|
|
367
|
+
): Promise<
|
|
368
|
+
ReadonlyArray<{ readonly target: string; readonly content: string }>
|
|
369
|
+
> {
|
|
370
|
+
const cwd = this.agent.session.header.cwd;
|
|
371
|
+
if (!cwd) {
|
|
372
|
+
throw new Error(
|
|
373
|
+
"Session working directory (agent.session.header.cwd) is missing",
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
const dirPath =
|
|
377
|
+
scope === "local"
|
|
378
|
+
? path.resolve(cwd, ".agents/wf")
|
|
379
|
+
: path.resolve(homedir(), ".agents/wf");
|
|
380
|
+
|
|
381
|
+
const files = await scanWorkflowDirectory(dirPath, dirPath);
|
|
382
|
+
return files.sort((a, b) => a.target.localeCompare(b.target));
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
async dispatchPrompt(
|
|
386
|
+
effect: Extract<WorkflowEffect, { type: "prompt" }>,
|
|
387
|
+
): Promise<void> {
|
|
388
|
+
this.agent.followup(
|
|
389
|
+
createUserMessage({
|
|
390
|
+
content: [{ type: "text", text: effect.promptText }],
|
|
391
|
+
source: {
|
|
392
|
+
kind: "workflow",
|
|
393
|
+
form: "notice",
|
|
394
|
+
summary: effect.stepLabel,
|
|
395
|
+
},
|
|
396
|
+
}),
|
|
397
|
+
);
|
|
398
|
+
await this.agent.whenIdle();
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
async dispatchSubagent(
|
|
402
|
+
effect: Extract<WorkflowEffect, { type: "subagent" }>,
|
|
403
|
+
signal?: AbortSignal,
|
|
404
|
+
): Promise<SubagentDispatchResult> {
|
|
405
|
+
const provider = effect.config.provider ?? "spawn";
|
|
406
|
+
|
|
407
|
+
const subagentSignal = signal ?? new AbortController().signal;
|
|
408
|
+
const activation = await this.ctx.subagents.startActivation({
|
|
409
|
+
provider,
|
|
410
|
+
label: effect.stepLabel,
|
|
411
|
+
delivery: "caller",
|
|
412
|
+
signal: subagentSignal,
|
|
413
|
+
parent: this.agent,
|
|
414
|
+
prompt: [{ type: "text", text: effect.promptText }],
|
|
415
|
+
...(effect.config.preset ? { agentPreset: effect.config.preset } : {}),
|
|
416
|
+
...(effect.config.llmProvider ||
|
|
417
|
+
effect.config.model ||
|
|
418
|
+
effect.config.thinking
|
|
419
|
+
? {
|
|
420
|
+
agentOptions: {
|
|
421
|
+
...(effect.config.llmProvider
|
|
422
|
+
? { provider: effect.config.llmProvider }
|
|
423
|
+
: {}),
|
|
424
|
+
...(effect.config.model ? { model: effect.config.model } : {}),
|
|
425
|
+
...(effect.config.thinking
|
|
426
|
+
? {
|
|
427
|
+
reasoningEffort: ReasoningEffortId(effect.config.thinking),
|
|
428
|
+
}
|
|
429
|
+
: {}),
|
|
430
|
+
},
|
|
431
|
+
}
|
|
432
|
+
: {}),
|
|
433
|
+
...(effect.outputSchema ? { outputSchema: effect.outputSchema } : {}),
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
const result = await activation.result;
|
|
437
|
+
if (result.stopReason === "aborted") {
|
|
438
|
+
this.activeAbortController?.abort({ kind: "pause" });
|
|
439
|
+
}
|
|
440
|
+
const outputText = result.output
|
|
441
|
+
.filter(
|
|
442
|
+
(block): block is { type: "text"; text: string } =>
|
|
443
|
+
block.type === "text" && Boolean(block.text),
|
|
444
|
+
)
|
|
445
|
+
.map((block) => block.text)
|
|
446
|
+
.join("\n")
|
|
447
|
+
.trim();
|
|
448
|
+
|
|
449
|
+
return {
|
|
450
|
+
outputText,
|
|
451
|
+
structured: result.structured,
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
async validateSubagentRoute(
|
|
456
|
+
config: SubagentConfig,
|
|
457
|
+
signal?: AbortSignal,
|
|
458
|
+
): Promise<
|
|
459
|
+
| { readonly valid: true }
|
|
460
|
+
| { readonly valid: false; readonly message: string }
|
|
461
|
+
> {
|
|
462
|
+
const provider = config.provider ?? "spawn";
|
|
463
|
+
const subagentProvider = this.ctx.subagents.getProvider(provider);
|
|
464
|
+
if (!subagentProvider) {
|
|
465
|
+
return {
|
|
466
|
+
valid: false,
|
|
467
|
+
message: `Subagent provider '${provider}' is not registered`,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
if (config.preset) {
|
|
472
|
+
if (
|
|
473
|
+
!subagentProvider.capabilities.agentPreset ||
|
|
474
|
+
subagentProvider.inheritsParentContext
|
|
475
|
+
) {
|
|
476
|
+
return {
|
|
477
|
+
valid: false,
|
|
478
|
+
message: `Subagent provider '${provider}' does not support independent agent preset selection`,
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
const presets = this.ctx.get("agentPresets");
|
|
482
|
+
if (!presets) {
|
|
483
|
+
return {
|
|
484
|
+
valid: false,
|
|
485
|
+
message: `Cannot validate agent preset '${config.preset}': 'agentPresets' service is unavailable`,
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
try {
|
|
489
|
+
await presets.readDocument(config.preset);
|
|
490
|
+
} catch (err: unknown) {
|
|
491
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
492
|
+
return {
|
|
493
|
+
valid: false,
|
|
494
|
+
message: `Agent preset '${config.preset}' not found: ${message}`,
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
if (config.llmProvider || config.model || config.thinking) {
|
|
500
|
+
if (!subagentProvider.capabilities.agentOptions) {
|
|
501
|
+
return {
|
|
502
|
+
valid: false,
|
|
503
|
+
message: `Subagent provider '${provider}' does not support custom model options (agentOptions: false)`,
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const llm = this.ctx.get("llm");
|
|
508
|
+
if (!llm) {
|
|
509
|
+
return {
|
|
510
|
+
valid: false,
|
|
511
|
+
message:
|
|
512
|
+
"Cannot validate subagent model: 'llm' service is unavailable",
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const parentOptions = parentAgentOptionsForDelegation(this.agent);
|
|
517
|
+
const effectiveProvider = config.llmProvider ?? parentOptions.provider;
|
|
518
|
+
const effectiveModel = config.model ?? parentOptions.model;
|
|
519
|
+
|
|
520
|
+
if (!effectiveProvider) {
|
|
521
|
+
return {
|
|
522
|
+
valid: false,
|
|
523
|
+
message:
|
|
524
|
+
"Cannot validate subagent model without an effective LLM provider",
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
if (!effectiveModel) {
|
|
528
|
+
return {
|
|
529
|
+
valid: false,
|
|
530
|
+
message:
|
|
531
|
+
"Cannot validate subagent model without an effective model ID",
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
const availableProviders = llm.listProviders();
|
|
536
|
+
if (!availableProviders.some((p) => p.id === effectiveProvider)) {
|
|
537
|
+
const available =
|
|
538
|
+
availableProviders.map((p) => p.id).join(", ") || "(none)";
|
|
539
|
+
return {
|
|
540
|
+
valid: false,
|
|
541
|
+
message: `LLM provider '${effectiveProvider}' is not registered; available providers: ${available}`,
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
try {
|
|
546
|
+
await llm.resolveCallConfig(
|
|
547
|
+
{
|
|
548
|
+
provider: effectiveProvider,
|
|
549
|
+
model: effectiveModel,
|
|
550
|
+
...(config.thinking
|
|
551
|
+
? { reasoningEffort: ReasoningEffortId(config.thinking) }
|
|
552
|
+
: {}),
|
|
553
|
+
},
|
|
554
|
+
signal,
|
|
555
|
+
);
|
|
556
|
+
} catch (err: unknown) {
|
|
557
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
558
|
+
return {
|
|
559
|
+
valid: false,
|
|
560
|
+
message,
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
return { valid: true };
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
logInfo(message: string): void {
|
|
569
|
+
if (this.log) {
|
|
570
|
+
this.log.info(message);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
resolveOutputPath(filePath: string, base: FileOutputBase): string {
|
|
575
|
+
const cwd = this.agent.session.header.cwd;
|
|
576
|
+
if (!cwd) {
|
|
577
|
+
throw new Error(
|
|
578
|
+
"Session working directory (agent.session.header.cwd) is missing",
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
if (base === "workspace") {
|
|
582
|
+
return path.resolve(cwd, filePath);
|
|
583
|
+
}
|
|
584
|
+
return path.resolve(cwd, ".agents/scratch", this.sessionId, filePath);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
async checkFileWritten(resolvedPath: string): Promise<boolean> {
|
|
588
|
+
try {
|
|
589
|
+
const s = await stat(resolvedPath);
|
|
590
|
+
return s.isFile() && s.size > 0;
|
|
591
|
+
} catch (err: unknown) {
|
|
592
|
+
// edge case: a missing path component or non-directory parent means no file.
|
|
593
|
+
if (
|
|
594
|
+
err instanceof Error &&
|
|
595
|
+
"code" in err &&
|
|
596
|
+
(err.code === "ENOENT" || err.code === "ENOTDIR")
|
|
597
|
+
) {
|
|
598
|
+
return false;
|
|
599
|
+
}
|
|
600
|
+
throw err;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
async ensureScratchDirectory(): Promise<string> {
|
|
605
|
+
const cwd = this.agent.session.header.cwd;
|
|
606
|
+
if (!cwd) {
|
|
607
|
+
throw new Error(
|
|
608
|
+
"Session working directory (agent.session.header.cwd) is missing",
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
const baseScratchDir = path.resolve(cwd, ".agents/scratch");
|
|
612
|
+
let baseExists = false;
|
|
613
|
+
try {
|
|
614
|
+
const s = await stat(baseScratchDir);
|
|
615
|
+
baseExists = s.isDirectory();
|
|
616
|
+
} catch (err: unknown) {
|
|
617
|
+
if (!(err instanceof Error && "code" in err && err.code === "ENOENT")) {
|
|
618
|
+
throw err;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
if (!baseExists) {
|
|
622
|
+
await mkdir(baseScratchDir, { recursive: true });
|
|
623
|
+
await writeFile(path.join(baseScratchDir, ".gitignore"), "*\n", "utf8");
|
|
624
|
+
}
|
|
625
|
+
const sessionScratchDir = path.resolve(baseScratchDir, this.sessionId);
|
|
626
|
+
await mkdir(sessionScratchDir, { recursive: true });
|
|
627
|
+
return sessionScratchDir;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
appendSessionEvent(
|
|
631
|
+
type: "command/run" | "command/done" | "session/title",
|
|
632
|
+
data:
|
|
633
|
+
| SessionEventMap["command/run"]
|
|
634
|
+
| SessionEventMap["command/done"]
|
|
635
|
+
| SessionEventMap["session/title"],
|
|
636
|
+
): void {
|
|
637
|
+
// smell: Session.append uses generic overloads that cannot be correlated without casting
|
|
638
|
+
(this.agent.session.append as (t: string, d: unknown) => void)(type, data);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
snapshotEvents(): readonly {
|
|
642
|
+
readonly type: string;
|
|
643
|
+
readonly data?: unknown;
|
|
644
|
+
}[] {
|
|
645
|
+
return this.agent.session.snapshotEvents();
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
setStatus(status: "running" | "idle"): void {
|
|
649
|
+
this.ctx.emit("agent/status", { agent: this.agent, status });
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
whenIdle(): Promise<void> {
|
|
653
|
+
return this.agent.whenIdle();
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Recursively scans directory for workflow files (.yaml and .yml).
|
|
659
|
+
* CONTRACT:
|
|
660
|
+
* - Inputs: base directory path and current directory path.
|
|
661
|
+
* - Outputs: Array of target identifier relative to base and raw file content.
|
|
662
|
+
* - Throws: Re-throws unexpected filesystem errors; returns [] on ENOENT / ENOTDIR.
|
|
663
|
+
*/
|
|
664
|
+
async function scanWorkflowDirectory(
|
|
665
|
+
baseDir: string,
|
|
666
|
+
currentDir: string,
|
|
667
|
+
): Promise<Array<{ readonly target: string; readonly content: string }>> {
|
|
668
|
+
try {
|
|
669
|
+
const entries = await readdir(currentDir, { withFileTypes: true });
|
|
670
|
+
const results: Array<{
|
|
671
|
+
readonly target: string;
|
|
672
|
+
readonly content: string;
|
|
673
|
+
}> = [];
|
|
674
|
+
for (const entry of entries) {
|
|
675
|
+
const fullPath = path.join(currentDir, entry.name);
|
|
676
|
+
if (entry.isDirectory()) {
|
|
677
|
+
const nested = await scanWorkflowDirectory(baseDir, fullPath);
|
|
678
|
+
results.push(...nested);
|
|
679
|
+
} else if (
|
|
680
|
+
entry.isFile() &&
|
|
681
|
+
(entry.name.endsWith(".yaml") || entry.name.endsWith(".yml"))
|
|
682
|
+
) {
|
|
683
|
+
const relPath = path.relative(baseDir, fullPath);
|
|
684
|
+
const content = await readFile(fullPath, "utf8");
|
|
685
|
+
results.push({ target: relPath, content });
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
return results;
|
|
689
|
+
} catch (err: unknown) {
|
|
690
|
+
const nodeErr = err as NodeJS.ErrnoException;
|
|
691
|
+
if (nodeErr.code === "ENOENT" || nodeErr.code === "ENOTDIR") {
|
|
692
|
+
return [];
|
|
693
|
+
}
|
|
694
|
+
throw err;
|
|
695
|
+
}
|
|
696
|
+
}
|