@lukstei/dsh-yaml-workflow 0.0.0-stage → 0.2.2-rc.5.3

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/src/skill.ts ADDED
@@ -0,0 +1,67 @@
1
+ export const WORKFLOW_SKILL_NAME = "yaml-workflow";
2
+ export const WORKFLOW_SKILL_DESCRIPTION =
3
+ "Author, validate, and execute declarative multi-step YAML prompt workflows in DSH.";
4
+
5
+ export const WORKFLOW_SKILL_CONTENT = `
6
+ # YAML Workflow Skill
7
+
8
+ Author, validate, and execute multi-step prompt workflows defined in YAML files.
9
+ Workflows provide sequential agent orchestration, typed argument interpolation, human review gates, zero-token status printing, and delegated subagent execution.
10
+
11
+ ## Commands
12
+
13
+ - \`/wf <workflow.yaml> [key=value ...] [instructions]\`: Execute a workflow.
14
+ - \`/wf-resume [instructions]\`: Resume an interrupted workflow or continue from a human review gate.
15
+ - \`/wf-abort\`: Discard an active or paused workflow.
16
+ - \`/wf-list\`: Discover and list workflows in project (\`.agents/wf\`) and user (\`~/.agents/wf\`) directories.
17
+ - \`/wf-validate <workflow.yaml> [key=value ...]\`: Validate syntax and argument coverage without running.
18
+
19
+ ## Agent Tools
20
+
21
+ - \`wf_validate\`: Validate a workflow file programmatically without executing it:
22
+ \`\`\`json
23
+ { "target": "deploy.yaml", "args": "env=production version=1.0.0" }
24
+ \`\`\`
25
+ - \`save_output\`: Save a declared output value during a workflow prompt step:
26
+ - For string output: \`{ "name": "tag", "value": "v1.2.0" }\`
27
+ - For list output: \`{ "name": "items", "value": "[\\"item1\\", \\"item2\\"]" }\`
28
+
29
+ ## Workflow File Schema
30
+
31
+ \`\`\`yaml
32
+ name: "Deploy Service"
33
+ description: "Build and deploy service to target environment"
34
+ args:
35
+ env:
36
+ type: string
37
+ description: "Target environment (staging | production)"
38
+ required: true
39
+ dryRun:
40
+ type: bool
41
+ default: false
42
+ steps:
43
+ - kind: prompt
44
+ name: "Prepare"
45
+ prompt: "Prepare deployment artifacts for {{args.env}}."
46
+ outputs:
47
+ release_tag:
48
+ type: string
49
+ description: "Computed release tag"
50
+
51
+ - kind: print
52
+ name: "Status"
53
+ text: "Artifact ready: {{outputs.release_tag}}"
54
+
55
+ - name: "Approval"
56
+ gate: "Approve deployment to {{args.env}} with tag {{outputs.release_tag}}?"
57
+
58
+ - kind: prompt
59
+ name: "Deploy"
60
+ prompt: "Execute deployment for {{outputs.release_tag}}."
61
+ \`\`\`
62
+
63
+ ## Best Practices
64
+
65
+ 1. **Always Validate First**: Before running or scheduling a workflow, test it with the \`wf_validate\` tool to catch schema errors, missing arguments, or invalid template references.
66
+ 2. **Output Recording**: Prompt steps with declared outputs require calling \`save_output\` for each output before completing the turn.
67
+ `.trim();
@@ -0,0 +1,357 @@
1
+ /**
2
+ * PURPOSE: Pure state machine transition functions for workflow execution.
3
+ * ALLOWED: Pure computation, state progression, and declarative side-effect generation.
4
+ * FORBIDDEN: No I/O, no filesystem, no timer/date manipulation, no agent side effects.
5
+ */
6
+
7
+ import {
8
+ buildSubagentOutputSchema,
9
+ evaluateStepCondition,
10
+ formatStepPrompt,
11
+ formatSubagentPrompt,
12
+ interpolateSubagentConfig,
13
+ interpolateTemplate,
14
+ normalizeSubagentConfig,
15
+ parseSubagentShorthand,
16
+ resolveGate,
17
+ } from "./parser.ts";
18
+ import type {
19
+ TransitionResult,
20
+ WorkflowDocument,
21
+ WorkflowInvocation,
22
+ WorkflowState,
23
+ } from "./types.ts";
24
+
25
+ export {
26
+ foldWorkflowEvent,
27
+ foldWorkflowState,
28
+ initialWorkflowRunSummary,
29
+ toWorkflowRunSummaryRecord,
30
+ } from "./fold.ts";
31
+
32
+ function assertNever(value: never, label: string): never {
33
+ throw new TypeError(`Unhandled ${label}: ${JSON.stringify(value)}`);
34
+ }
35
+
36
+ export interface TransitionParams {
37
+ readonly doc: WorkflowDocument;
38
+ readonly invocation: WorkflowInvocation;
39
+ readonly state: WorkflowState;
40
+ readonly instructions?: string;
41
+ readonly outputs?: Record<string, string | readonly string[]>;
42
+ readonly resolvedOutputPaths?: Record<string, string>;
43
+ }
44
+
45
+ /**
46
+ * Computes the next state and declarative side effect for the workflow.
47
+ * CONTRACT:
48
+ * - Input: TransitionParams containing doc, invocation, state, and optional instructions.
49
+ * - Output: TransitionResult containing immutable nextState and declarative effect.
50
+ * - Invariant: Pure and deterministic. Does not mutate the input state.
51
+ */
52
+ export function computeStepTransition({
53
+ doc,
54
+ invocation,
55
+ state,
56
+ instructions,
57
+ outputs,
58
+ resolvedOutputPaths = {},
59
+ }: TransitionParams): TransitionResult {
60
+ if (state.stepIndex >= doc.steps.length) {
61
+ return {
62
+ nextState: {
63
+ ...state,
64
+ status: "completed",
65
+ },
66
+ effect: {
67
+ type: "complete",
68
+ totalSteps: doc.steps.length,
69
+ },
70
+ };
71
+ }
72
+
73
+ const effectiveOutputs = outputs ?? state.outputs;
74
+ const hasOutputs =
75
+ effectiveOutputs !== undefined && Object.keys(effectiveOutputs).length > 0;
76
+ const step = doc.steps[state.stepIndex];
77
+ const resolvedArgs = invocation.resolvedArgs;
78
+ const workflowName = doc.name
79
+ ? interpolateTemplate(doc.name, resolvedArgs)
80
+ : undefined;
81
+ const stepName = interpolateTemplate(
82
+ step.name,
83
+ resolvedArgs,
84
+ effectiveOutputs,
85
+ );
86
+ const stepLabel = `Step ${state.stepIndex + 1} of ${doc.steps.length}: ${stepName}`;
87
+
88
+ if (
89
+ step.if &&
90
+ !evaluateStepCondition(step.if, resolvedArgs, effectiveOutputs)
91
+ ) {
92
+ return {
93
+ nextState: {
94
+ ...state,
95
+ stepIndex: state.stepIndex + 1,
96
+ ...(hasOutputs ? { outputs: effectiveOutputs } : {}),
97
+ },
98
+ effect: {
99
+ type: "skip",
100
+ stepIndex: state.stepIndex,
101
+ stepLabel,
102
+ },
103
+ };
104
+ }
105
+
106
+ const isResumedStep =
107
+ state.status === "aborted" ||
108
+ (state.status === "paused" && state.pauseReason === "interrupted");
109
+
110
+ switch (step.kind) {
111
+ case "prompt": {
112
+ const interpolatedPrompt = interpolateTemplate(
113
+ step.prompt,
114
+ resolvedArgs,
115
+ effectiveOutputs,
116
+ );
117
+ const interpolatedGate = resolveGate(
118
+ step.gate,
119
+ resolvedArgs,
120
+ effectiveOutputs,
121
+ stepName,
122
+ );
123
+
124
+ if (step.isSoloGate && interpolatedGate === undefined) {
125
+ return {
126
+ nextState: {
127
+ ...state,
128
+ stepIndex: state.stepIndex + 1,
129
+ status: "running",
130
+ ...(hasOutputs ? { outputs: effectiveOutputs } : {}),
131
+ },
132
+ effect: {
133
+ type: "skip",
134
+ stepIndex: state.stepIndex,
135
+ stepLabel,
136
+ },
137
+ };
138
+ }
139
+
140
+ const outputRequirements = step.outputs
141
+ ? Object.entries(step.outputs).map(([name, def]) => ({
142
+ name,
143
+ type: def.type,
144
+ ...(def.type === "file" && resolvedOutputPaths?.[name]
145
+ ? { resolvedPath: resolvedOutputPaths[name] }
146
+ : {}),
147
+ ...(def.type !== "file" && def.description
148
+ ? { description: def.description }
149
+ : {}),
150
+ ...(def.type === "list" && def.min !== undefined
151
+ ? { min: def.min }
152
+ : {}),
153
+ ...(def.type === "list" && def.max !== undefined
154
+ ? { max: def.max }
155
+ : {}),
156
+ }))
157
+ : undefined;
158
+ return {
159
+ nextState: {
160
+ stepIndex: state.stepIndex + 1,
161
+ status: "running",
162
+ ...(hasOutputs ? { outputs: effectiveOutputs } : {}),
163
+ },
164
+ effect: {
165
+ type: "prompt",
166
+ promptText: formatStepPrompt({
167
+ stepIndex: state.stepIndex,
168
+ totalSteps: doc.steps.length,
169
+ stepName,
170
+ workflowName,
171
+ prompt: interpolatedPrompt,
172
+ isResumedStep,
173
+ instructions,
174
+ outputRequirements,
175
+ }),
176
+ stepLabel,
177
+ gate: interpolatedGate,
178
+ ...(step.outputs ? { outputs: step.outputs } : {}),
179
+ },
180
+ };
181
+ }
182
+ case "subagent": {
183
+ const interpolatedPrompt = interpolateTemplate(
184
+ step.prompt,
185
+ resolvedArgs,
186
+ effectiveOutputs,
187
+ );
188
+ const interpolatedGate = resolveGate(
189
+ step.gate,
190
+ resolvedArgs,
191
+ effectiveOutputs,
192
+ stepName,
193
+ );
194
+
195
+ const hasStepOutputs =
196
+ step.outputs !== undefined && Object.keys(step.outputs).length > 0;
197
+
198
+ const outputRequirements = hasStepOutputs
199
+ ? Object.entries(step.outputs).map(([name, def]) => ({
200
+ name,
201
+ type: def.type,
202
+ ...(def.type === "file" && resolvedOutputPaths?.[name]
203
+ ? { resolvedPath: resolvedOutputPaths[name] }
204
+ : {}),
205
+ ...(def.type === "string" && def.description
206
+ ? { description: def.description }
207
+ : {}),
208
+ }))
209
+ : undefined;
210
+
211
+ const outputSchema = hasStepOutputs
212
+ ? buildSubagentOutputSchema(step.outputs, resolvedOutputPaths)
213
+ : undefined;
214
+
215
+ const promptText = formatSubagentPrompt({
216
+ prompt: interpolatedPrompt,
217
+ outputRequirements,
218
+ });
219
+
220
+ const baseConfig = (() => {
221
+ if (step.rawSubagent) {
222
+ const interpolatedShorthand = interpolateTemplate(
223
+ step.rawSubagent,
224
+ resolvedArgs,
225
+ effectiveOutputs,
226
+ );
227
+ const parsed = parseSubagentShorthand(interpolatedShorthand);
228
+ return normalizeSubagentConfig(parsed);
229
+ }
230
+ return step.config;
231
+ })();
232
+
233
+ const interpolatedConfig = interpolateSubagentConfig(
234
+ baseConfig,
235
+ resolvedArgs,
236
+ effectiveOutputs,
237
+ );
238
+
239
+ return {
240
+ nextState: {
241
+ stepIndex: state.stepIndex + 1,
242
+ status: "running",
243
+ ...(hasOutputs ? { outputs: effectiveOutputs } : {}),
244
+ },
245
+ effect: {
246
+ type: "subagent",
247
+ promptText,
248
+ stepLabel,
249
+ config: interpolatedConfig,
250
+ ...(step.foreach ? { foreach: step.foreach } : {}),
251
+ gate: interpolatedGate,
252
+ ...(step.outputs ? { outputs: step.outputs } : {}),
253
+ ...(step.output ? { output: step.output } : {}),
254
+ ...(outputSchema ? { outputSchema } : {}),
255
+ ...(hasStepOutputs && resolvedOutputPaths
256
+ ? { resolvedOutputPaths }
257
+ : {}),
258
+ },
259
+ };
260
+ }
261
+ case "print": {
262
+ const interpolatedText = interpolateTemplate(
263
+ step.text,
264
+ resolvedArgs,
265
+ effectiveOutputs,
266
+ );
267
+ return {
268
+ nextState: {
269
+ stepIndex: state.stepIndex + 1,
270
+ status: "running",
271
+ ...(hasOutputs ? { outputs: effectiveOutputs } : {}),
272
+ },
273
+ effect: {
274
+ type: "print",
275
+ text: interpolatedText,
276
+ stepLabel,
277
+ },
278
+ };
279
+ }
280
+ default:
281
+ return assertNever(step, "workflow step");
282
+ }
283
+ }
284
+
285
+ /**
286
+ * Computes a state transition into the paused state at a gate checkpoint.
287
+ * CONTRACT:
288
+ * - Input: state, gate criteria description, stepLabel.
289
+ * - Output: TransitionResult with status 'paused' and a 'gate_pause' effect.
290
+ */
291
+ export function applyGatePause(
292
+ state: WorkflowState,
293
+ gate: string,
294
+ stepLabel: string,
295
+ ): TransitionResult {
296
+ return {
297
+ nextState: {
298
+ ...state,
299
+ status: "paused",
300
+ pauseReason: "gate",
301
+ },
302
+ effect: {
303
+ type: "gate_pause",
304
+ gate,
305
+ stepLabel,
306
+ },
307
+ };
308
+ }
309
+
310
+ /**
311
+ * Computes a state transition into the paused state when a step is interrupted.
312
+ * CONTRACT:
313
+ * - Input: state, stepLabel.
314
+ * - Output: TransitionResult with status 'paused', pauseReason 'interrupted', and a 'step_pause' effect.
315
+ */
316
+ export function applyStepPause(
317
+ state: WorkflowState,
318
+ stepLabel: string,
319
+ ): TransitionResult {
320
+ return {
321
+ nextState: {
322
+ ...state,
323
+ status: "paused",
324
+ pauseReason: "interrupted",
325
+ },
326
+ effect: {
327
+ type: "step_pause",
328
+ stepIndex: state.stepIndex,
329
+ stepLabel,
330
+ },
331
+ };
332
+ }
333
+
334
+ /**
335
+ * Computes a state transition into the aborted state upon cancellation.
336
+ * CONTRACT:
337
+ * - Input: state, totalSteps, isAfter flag.
338
+ * - Output: TransitionResult with status 'aborted' and an 'abort' effect.
339
+ */
340
+ export function applyAbort(
341
+ state: WorkflowState,
342
+ totalSteps: number,
343
+ isAfter: boolean,
344
+ ): TransitionResult {
345
+ return {
346
+ nextState: {
347
+ ...state,
348
+ status: "aborted",
349
+ },
350
+ effect: {
351
+ type: "abort",
352
+ stepIndex: state.stepIndex,
353
+ totalSteps,
354
+ isAfter,
355
+ },
356
+ };
357
+ }