@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/types.ts ADDED
@@ -0,0 +1,604 @@
1
+ /**
2
+ * PURPOSE: Data contracts, Zod schemas, and protocol types for dsh-workflow-runner.
3
+ * ALLOWED: Only type definitions, Zod schemas, and type-level helpers.
4
+ * FORBIDDEN: No I/O, no filesystem, no runtime state, no network, no side effects.
5
+ */
6
+
7
+ import type { SessionId } from "@deepseek-ai/dsh-session";
8
+ import type { JsonSchemaNode, ObjectJsonSchema } from "@deepseek-ai/dsh-tools";
9
+ import { z } from "zod";
10
+
11
+ export type { JsonSchemaNode, ObjectJsonSchema };
12
+
13
+ export interface SubagentDispatchResult {
14
+ readonly outputText: string;
15
+ readonly structured?: unknown;
16
+ }
17
+
18
+ // --- DSH LLM Message Source Augmentation ---
19
+
20
+ declare module "@deepseek-ai/dsh-llm" {
21
+ interface MessageSourceMap {
22
+ workflow: {
23
+ readonly kind: "workflow";
24
+ readonly form: "notice";
25
+ readonly summary: string;
26
+ };
27
+ }
28
+ }
29
+
30
+ // --- Workflow YAML Schema (Strict Typing & Discriminated Union) ---
31
+
32
+ const FlatChoiceMappingSchema = z
33
+ .record(z.string().min(1, "Choice key cannot be empty"), z.string())
34
+ .refine((val) => Object.keys(val).length > 0, {
35
+ message: "Choice mapping cannot be empty",
36
+ });
37
+
38
+ const NestedChoiceMappingSchema = z
39
+ .record(
40
+ z.string().min(1, "Derived argument name cannot be empty"),
41
+ z
42
+ .record(z.string().min(1, "Choice key cannot be empty"), z.string())
43
+ .refine((val) => Object.keys(val).length > 0, {
44
+ message: "Choice mapping cannot be empty",
45
+ }),
46
+ )
47
+ .refine((val) => Object.keys(val).length > 0, {
48
+ message: "Choice mapping cannot be empty",
49
+ });
50
+
51
+ export const ChoiceMappingSchema = z.union([
52
+ FlatChoiceMappingSchema,
53
+ NestedChoiceMappingSchema,
54
+ ]);
55
+ export type ChoiceMapping = z.infer<typeof ChoiceMappingSchema>;
56
+
57
+ export const WorkflowArgSchema = z
58
+ .discriminatedUnion("type", [
59
+ z
60
+ .object({
61
+ type: z.literal("string"),
62
+ description: z.string().optional(),
63
+ default: z.string().optional(),
64
+ required: z.boolean().optional(),
65
+ })
66
+ .strict(),
67
+ z
68
+ .object({
69
+ type: z.literal("choice"),
70
+ description: z.string().optional(),
71
+ choices: z
72
+ .array(z.string().min(1, "Choice values cannot be empty"))
73
+ .min(1, "Choice argument must define at least one option"),
74
+ default: z.string().optional(),
75
+ required: z.boolean().optional(),
76
+ mapping: ChoiceMappingSchema.optional(),
77
+ mappings: ChoiceMappingSchema.optional(),
78
+ })
79
+ .strict(),
80
+ z
81
+ .object({
82
+ type: z.literal("bool"),
83
+ description: z.string().optional(),
84
+ default: z.union([z.boolean(), z.string()]).optional(),
85
+ required: z.boolean().optional(),
86
+ })
87
+ .strict(),
88
+ ])
89
+ .refine(
90
+ (data) => {
91
+ if (data.type === "choice" && data.default !== undefined) {
92
+ return data.choices.includes(data.default);
93
+ }
94
+ if (data.type === "bool" && data.default !== undefined) {
95
+ if (typeof data.default === "boolean") return true;
96
+ if (typeof data.default === "string") {
97
+ const lower = data.default.trim().toLowerCase();
98
+ return lower === "true" || lower === "false";
99
+ }
100
+ return false;
101
+ }
102
+ return true;
103
+ },
104
+ {
105
+ message: "Default value must be valid for the specified argument type",
106
+ path: ["default"],
107
+ },
108
+ )
109
+ .refine(
110
+ (data) => {
111
+ if (
112
+ data.type === "choice" &&
113
+ data.mapping !== undefined &&
114
+ data.mappings !== undefined
115
+ ) {
116
+ return false;
117
+ }
118
+ return true;
119
+ },
120
+ {
121
+ message: "Cannot specify both 'mapping' and 'mappings'",
122
+ path: ["mappings"],
123
+ },
124
+ );
125
+
126
+ export type WorkflowArg = z.infer<typeof WorkflowArgSchema>;
127
+
128
+ export const SubagentConfigSchema = z.object({
129
+ provider: z.string().min(1, "Provider name cannot be empty").optional(),
130
+ llmProvider: z
131
+ .string()
132
+ .min(1, "LLM provider name cannot be empty")
133
+ .optional(),
134
+ preset: z.string().min(1, "Preset name cannot be empty").optional(),
135
+ model: z.string().min(1, "Model name cannot be empty").optional(),
136
+ thinking: z.string().min(1, "Thinking level cannot be empty").optional(),
137
+ });
138
+
139
+ export type SubagentConfig = z.infer<typeof SubagentConfigSchema>;
140
+
141
+ export const FileOutputBaseSchema = z.enum(["scratch", "workspace"]);
142
+ export type FileOutputBase = z.infer<typeof FileOutputBaseSchema>;
143
+
144
+ export const StepOutputDefSchema = z
145
+ .discriminatedUnion("type", [
146
+ z.object({
147
+ type: z.literal("file"),
148
+ path: z.string().min(1, "File path cannot be empty"),
149
+ base: FileOutputBaseSchema.optional(),
150
+ }),
151
+ z.object({
152
+ type: z.literal("string"),
153
+ description: z.string().optional(),
154
+ }),
155
+ z.object({
156
+ type: z.literal("list"),
157
+ description: z.string().optional(),
158
+ min: z.number().int().nonnegative().optional(),
159
+ max: z.number().int().nonnegative().optional(),
160
+ }),
161
+ ])
162
+ .refine(
163
+ (data) => {
164
+ if (data.type === "list") {
165
+ if (
166
+ data.min !== undefined &&
167
+ data.max !== undefined &&
168
+ data.min > data.max
169
+ ) {
170
+ return false;
171
+ }
172
+ }
173
+ return true;
174
+ },
175
+ {
176
+ message: "'min' cannot be greater than 'max'",
177
+ path: ["min"],
178
+ },
179
+ );
180
+
181
+ export type StepOutputDef = z.infer<typeof StepOutputDefSchema>;
182
+
183
+ export const StepOutputsMapSchema = z.record(
184
+ z.string().min(1, "Output name cannot be empty"),
185
+ StepOutputDefSchema,
186
+ );
187
+ export type StepOutputsMap = z.infer<typeof StepOutputsMapSchema>;
188
+
189
+ // --- Workflow File YAML Schema (External Contract) ---
190
+ // Note: In YAML file format, subagents are declared via `subagent:` on a prompt step.
191
+ // The `kind: "subagent"` step variant is an internal normalized runtime type only,
192
+ // produced during preprocessing.
193
+ export const WorkflowFileStepSchema = z.union([
194
+ z.string().min(1, "Prompt cannot be empty"),
195
+ z
196
+ .looseObject({
197
+ kind: z.literal("prompt").optional(),
198
+ name: z.string().optional(),
199
+ if: z.string().min(1, "If condition cannot be empty").optional(),
200
+ prompt: z.string().min(1, "Prompt cannot be empty"),
201
+ gate: z
202
+ .union([
203
+ z.boolean(),
204
+ z.string().min(1, "Gate description cannot be empty"),
205
+ ])
206
+ .optional(),
207
+ subagent: z
208
+ .union([
209
+ z.boolean(),
210
+ z.string().trim().min(1, "Subagent descriptor cannot be empty"),
211
+ SubagentConfigSchema,
212
+ ])
213
+ .optional(),
214
+ foreach: z.string().min(1, "Foreach cannot be empty").optional(),
215
+ outputs: StepOutputsMapSchema.optional(),
216
+ output: z.string().min(1, "Output name cannot be empty").optional(),
217
+ })
218
+ .refine(
219
+ (data) => !(data.output !== undefined && data.outputs !== undefined),
220
+ { message: "Step cannot declare both 'output' and 'outputs'" },
221
+ )
222
+ .refine(
223
+ (data) =>
224
+ !(
225
+ data.output !== undefined &&
226
+ (data.subagent === undefined || data.subagent === false)
227
+ ),
228
+ { message: "'output' is only allowed on subagent steps" },
229
+ )
230
+ .refine(
231
+ (data) =>
232
+ !(
233
+ data.foreach !== undefined &&
234
+ (data.subagent === undefined || data.subagent === false)
235
+ ),
236
+ { message: "'foreach' is only allowed on subagent steps" },
237
+ )
238
+ .refine(
239
+ (data) => {
240
+ const isSubagent =
241
+ data.subagent !== undefined && data.subagent !== false;
242
+ if (!isSubagent || !data.outputs) return true;
243
+ return Object.values(data.outputs).every((def) => def.type !== "list");
244
+ },
245
+ { message: "Subagent steps cannot declare outputs of type 'list'" },
246
+ )
247
+ .refine(
248
+ (data) => {
249
+ const isForeachSubagent =
250
+ data.foreach !== undefined &&
251
+ data.subagent !== undefined &&
252
+ data.subagent !== false;
253
+ if (!isForeachSubagent || !data.outputs) return true;
254
+ return Object.values(data.outputs).every(
255
+ (def) => def.type === "string",
256
+ );
257
+ },
258
+ { message: "Foreach subagent steps only support string outputs" },
259
+ ),
260
+ z
261
+ .looseObject({
262
+ kind: z.literal("print").optional(),
263
+ name: z.string().optional(),
264
+ if: z.string().min(1, "If condition cannot be empty").optional(),
265
+ text: z.string().min(1, "Text cannot be empty"),
266
+ })
267
+ .refine((data) => !("output" in data && data.output !== undefined), {
268
+ message: "'output' is only allowed on subagent steps",
269
+ })
270
+ .refine((data) => !("foreach" in data && data.foreach !== undefined), {
271
+ message: "'foreach' is only allowed on subagent steps",
272
+ }),
273
+ z
274
+ .looseObject({
275
+ name: z.string().optional(),
276
+ if: z.string().min(1, "If condition cannot be empty").optional(),
277
+ print: z.string().min(1, "Text cannot be empty"),
278
+ })
279
+ .refine((data) => !("output" in data && data.output !== undefined), {
280
+ message: "'output' is only allowed on subagent steps",
281
+ })
282
+ .refine((data) => !("foreach" in data && data.foreach !== undefined), {
283
+ message: "'foreach' is only allowed on subagent steps",
284
+ }),
285
+ z
286
+ .looseObject({
287
+ name: z.string().optional(),
288
+ if: z.string().min(1, "If condition cannot be empty").optional(),
289
+ gate: z.union([
290
+ z.boolean(),
291
+ z.string().min(1, "Gate description cannot be empty"),
292
+ ]),
293
+ outputs: StepOutputsMapSchema.optional(),
294
+ })
295
+ .refine(
296
+ (data) =>
297
+ !("output" in data && data.output !== undefined) &&
298
+ !("prompt" in data && data.prompt !== undefined),
299
+ {
300
+ message: "'output' is only allowed on subagent steps",
301
+ },
302
+ )
303
+ .refine((data) => !("foreach" in data && data.foreach !== undefined), {
304
+ message: "'foreach' is only allowed on subagent steps",
305
+ }),
306
+ ]);
307
+
308
+ export type WorkflowFileStep = z.infer<typeof WorkflowFileStepSchema>;
309
+
310
+ export const WorkflowFileSchema = z
311
+ .object({
312
+ name: z.string().optional(),
313
+ title: z.string().optional(),
314
+ description: z.string().optional(),
315
+ args: z.record(z.string(), WorkflowArgSchema).optional(),
316
+ steps: z
317
+ .array(WorkflowFileStepSchema)
318
+ .min(1, "Workflow must contain at least one step"),
319
+ })
320
+ .strict();
321
+
322
+ export type WorkflowFile = z.infer<typeof WorkflowFileSchema>;
323
+
324
+ // --- Preprocessed Workflow Types (Internal Runtime Model) ---
325
+
326
+ export type WorkflowStepOutputDef =
327
+ | {
328
+ readonly type: "file";
329
+ readonly path: string;
330
+ readonly base: FileOutputBase;
331
+ }
332
+ | {
333
+ readonly type: "string";
334
+ readonly description?: string;
335
+ }
336
+ | {
337
+ readonly type: "list";
338
+ readonly description?: string;
339
+ readonly min?: number;
340
+ readonly max?: number;
341
+ };
342
+
343
+ export type WorkflowStepOutputsMap = Record<string, WorkflowStepOutputDef>;
344
+
345
+ export type WorkflowStep =
346
+ | {
347
+ readonly kind: "prompt";
348
+ readonly name: string;
349
+ readonly if?: string;
350
+ readonly prompt: string;
351
+ readonly gate?: string | boolean;
352
+ readonly isSoloGate?: boolean;
353
+ readonly outputs?: WorkflowStepOutputsMap;
354
+ }
355
+ | {
356
+ readonly kind: "subagent";
357
+ readonly name: string;
358
+ readonly if?: string;
359
+ readonly prompt: string;
360
+ readonly config: SubagentConfig;
361
+ readonly rawSubagent?: string;
362
+ readonly foreach?: string;
363
+ readonly gate?: string | boolean;
364
+ readonly outputs?: WorkflowStepOutputsMap;
365
+ readonly output?: string;
366
+ }
367
+ | {
368
+ readonly kind: "print";
369
+ readonly name: string;
370
+ readonly if?: string;
371
+ readonly text: string;
372
+ };
373
+
374
+ export interface WorkflowDocument {
375
+ readonly name?: string;
376
+ readonly title?: string;
377
+ readonly description?: string;
378
+ readonly args: Record<string, WorkflowArg>;
379
+ readonly steps: readonly WorkflowStep[];
380
+ }
381
+
382
+ // --- Workflow Invocation & Dynamic State ---
383
+
384
+ export interface WorkflowInvocation {
385
+ readonly targetPath: string;
386
+ readonly runId: string;
387
+ // edge case: historical initialization events did not record the original input.
388
+ readonly rawInput?: string;
389
+ readonly resolvedArgs: Record<string, string>;
390
+ }
391
+
392
+ export const WorkflowStatusSchema = z.enum([
393
+ "running",
394
+ "paused",
395
+ "aborted",
396
+ "completed",
397
+ ]);
398
+
399
+ export type WorkflowStatus = z.infer<typeof WorkflowStatusSchema>;
400
+
401
+ export const workflowRunSummarySchema = z.object({
402
+ status: z.enum(["idle", "running", "paused", "aborted", "completed"]),
403
+ runId: z.string().optional(),
404
+ stepIndex: z.number().int().nonnegative().optional(),
405
+ skillInvoked: z.boolean().default(false),
406
+ });
407
+
408
+ export type WorkflowRunSummary = z.infer<typeof workflowRunSummarySchema>;
409
+
410
+ export const WorkflowStateSchema = z.object({
411
+ stepIndex: z.number().int().nonnegative(),
412
+ status: WorkflowStatusSchema,
413
+ pauseReason: z.enum(["gate", "interrupted"]).optional(),
414
+ outputs: z
415
+ .record(z.string(), z.union([z.string(), z.array(z.string()).readonly()]))
416
+ .readonly()
417
+ .optional(),
418
+ error: z.string().optional(),
419
+ });
420
+
421
+ export type WorkflowState = z.infer<typeof WorkflowStateSchema>;
422
+
423
+ export interface WorkflowRun {
424
+ readonly workflow: WorkflowDocument;
425
+ readonly invocation: WorkflowInvocation;
426
+ readonly state: WorkflowState;
427
+ }
428
+
429
+ export type WorkflowFoldResult = WorkflowRun;
430
+
431
+ // --- Workflow Command Payloads (Strict Typing & Discriminated Union) ---
432
+
433
+ export interface WorkflowInitPayloadData {
434
+ readonly workflow: WorkflowDocument;
435
+ readonly invocation: WorkflowInvocation;
436
+ readonly sessionId: SessionId;
437
+ readonly runId: string;
438
+ }
439
+
440
+ export type WorkflowStatePayloadData = Partial<WorkflowState> & {
441
+ readonly sessionId: SessionId;
442
+ readonly runId: string;
443
+ };
444
+
445
+ export interface WorkflowStepPayloadData {
446
+ readonly id: string;
447
+ readonly stepIndex: number;
448
+ readonly label: string;
449
+ readonly kind: "success" | "error";
450
+ readonly sessionId: SessionId;
451
+ readonly runId: string;
452
+ }
453
+
454
+ export interface WorkflowPrintPayloadData {
455
+ readonly id: string;
456
+ readonly text: string;
457
+ readonly sessionId: SessionId;
458
+ readonly runId: string;
459
+ }
460
+
461
+ export interface WorkflowOutputPayloadData {
462
+ readonly stepIndex: number;
463
+ readonly name: string;
464
+ readonly value: string;
465
+ readonly outputType: "file" | "string" | "list";
466
+ readonly base?: FileOutputBase;
467
+ readonly sessionId: SessionId;
468
+ readonly runId: string;
469
+ }
470
+
471
+ export { WORKFLOW_EVENT_NAME } from "./fold.ts";
472
+
473
+ export type WorkflowPayload =
474
+ | {
475
+ readonly action: "request";
476
+ readonly rawInput: string;
477
+ readonly sessionId: SessionId;
478
+ readonly runId: string;
479
+ }
480
+ | {
481
+ readonly action: "request-failed";
482
+ readonly sessionId: SessionId;
483
+ readonly runId: string;
484
+ }
485
+ | ({ readonly action: "init" } & WorkflowInitPayloadData)
486
+ | ({ readonly action: "state" } & WorkflowStatePayloadData)
487
+ | ({ readonly action: "step" } & WorkflowStepPayloadData)
488
+ | ({ readonly action: "print" } & WorkflowPrintPayloadData)
489
+ | ({ readonly action: "output" } & WorkflowOutputPayloadData);
490
+
491
+ export interface ParsedInvocation {
492
+ readonly targetPath: string;
493
+ readonly explicitArgs: Record<string, string>;
494
+ readonly implicitInstructions: string;
495
+ }
496
+
497
+ export interface FormatStepPromptOutputRequirement {
498
+ readonly name: string;
499
+ readonly type: "file" | "string" | "list";
500
+ readonly resolvedPath?: string;
501
+ readonly description?: string;
502
+ readonly min?: number;
503
+ readonly max?: number;
504
+ }
505
+
506
+ export interface FormatStepPromptOptions {
507
+ readonly stepIndex: number;
508
+ readonly totalSteps: number;
509
+ readonly stepName: string;
510
+ readonly workflowName?: string;
511
+ readonly prompt: string;
512
+ readonly isResumedStep: boolean;
513
+ readonly instructions?: string;
514
+ readonly outputRequirements?: readonly FormatStepPromptOutputRequirement[];
515
+ }
516
+
517
+ export type WorkflowEffect =
518
+ | {
519
+ readonly type: "prompt";
520
+ readonly promptText: string;
521
+ readonly stepLabel: string;
522
+ readonly gate?: string;
523
+ readonly outputs?: WorkflowStepOutputsMap;
524
+ }
525
+ | {
526
+ readonly type: "subagent";
527
+ readonly promptText: string;
528
+ readonly stepLabel: string;
529
+ readonly config: SubagentConfig;
530
+ readonly foreach?: string;
531
+ readonly gate?: string;
532
+ readonly outputs?: WorkflowStepOutputsMap;
533
+ readonly output?: string;
534
+ readonly outputSchema?: ObjectJsonSchema;
535
+ readonly resolvedOutputPaths?: Record<string, string>;
536
+ }
537
+ | {
538
+ readonly type: "print";
539
+ readonly text: string;
540
+ readonly stepLabel: string;
541
+ }
542
+ | {
543
+ readonly type: "gate_pause";
544
+ readonly gate: string;
545
+ readonly stepLabel: string;
546
+ }
547
+ | {
548
+ readonly type: "step_pause";
549
+ readonly stepIndex: number;
550
+ readonly stepLabel: string;
551
+ }
552
+ | {
553
+ readonly type: "abort";
554
+ readonly stepIndex: number;
555
+ readonly totalSteps: number;
556
+ readonly isAfter: boolean;
557
+ }
558
+ | {
559
+ readonly type: "skip";
560
+ readonly stepIndex: number;
561
+ readonly stepLabel: string;
562
+ }
563
+ | {
564
+ readonly type: "complete";
565
+ readonly totalSteps: number;
566
+ };
567
+
568
+ export interface TransitionResult {
569
+ readonly nextState: WorkflowState;
570
+ readonly effect: WorkflowEffect;
571
+ }
572
+
573
+ export interface WorkflowParamSummary {
574
+ readonly name: string;
575
+ readonly description?: string;
576
+ readonly type: "string" | "choice" | "bool";
577
+ readonly required: boolean;
578
+ readonly default?: string;
579
+ readonly choices?: readonly string[];
580
+ }
581
+
582
+ export interface WorkflowSummary {
583
+ readonly target: string;
584
+ readonly name?: string;
585
+ readonly description?: string;
586
+ readonly params: readonly WorkflowParamSummary[];
587
+ }
588
+
589
+ export interface WorkflowGroup {
590
+ readonly title: string;
591
+ readonly workflows: readonly WorkflowSummary[];
592
+ }
593
+
594
+ declare module "@deepseek-ai/dsh-session-projection/types" {
595
+ interface SessionProjectionMap {
596
+ workflow: WorkflowRunSummary | null;
597
+ }
598
+ }
599
+
600
+ declare module "@deepseek-ai/dsh-session-projection" {
601
+ interface SessionProjectionMap {
602
+ workflow: WorkflowRunSummary | null;
603
+ }
604
+ }