@pogodisco/zephyr 1.3.15 → 1.3.17

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.
@@ -73,11 +73,11 @@ type StepOptions<Input, Results, Context> = {
73
73
  label?: string;
74
74
  meta?: Record<string, any>;
75
75
  };
76
- type MergeBranchResults<Branches extends readonly any[], Acc> = Branches extends readonly [infer Head, ...infer Tail] ? MergeBranchResults<Tail, Acc & (Head extends WorkflowBuilder<any, any, any, any, infer R> ? R : {})> : Acc;
77
76
  type MergeBranchSteps<Branches extends readonly any[], Acc extends any[]> = Branches extends readonly [infer Head, ...infer Tail] ? MergeBranchSteps<Tail, [
78
77
  ...Acc,
79
78
  ...(Head extends WorkflowBuilder<any, any, any, infer S, any> ? S : [])
80
79
  ]> : Acc;
80
+ type MergeBranchResults<Branches extends readonly WorkflowBuilder<any, any, any, any, any>[]> = Simplify<Branches extends readonly [infer Head, ...infer Tail] ? Head extends WorkflowBuilder<any, any, any, any, infer R> ? Tail extends readonly WorkflowBuilder<any, any, any, any, any>[] ? R & MergeBranchResults<Tail> : R : MergeBranchResults<Tail & WorkflowBuilder<any, any, any, any, any>[]> : {}>;
81
81
  export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown, Context = unknown, Steps extends StepDef<Reg, any, any>[] = [], Results = {}, Output = undefined> {
82
82
  private name;
83
83
  private steps;
@@ -106,12 +106,12 @@ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown
106
106
  }> : Results : Results, Output>;
107
107
  parallel<Branches extends readonly WorkflowBuilder<Reg, Input, Context, any, any>[]>(...branches: {
108
108
  [K in keyof Branches]: (builder: WorkflowBuilder<Reg, Input, Context, [], Results>) => Branches[K];
109
- }): WorkflowBuilder<Reg, Input, Context, MergeBranchSteps<Branches, Steps>, Simplify<MergeBranchResults<Branches, Results>>>;
110
- join<ID extends string = string, ActionName extends keyof Reg & string = any, ResolveOut extends ResolvedStepInput = ResolvedStepInput>(id: ID, action: ActionName, resolve?: (ctx: {
109
+ }): WorkflowBuilder<Reg, Input, Context, MergeBranchSteps<Branches, Steps>, Results & MergeBranchResults<Branches>>;
110
+ join<ID extends string = string, ActionName extends keyof Reg & string = any, ResolveOut extends ResolvedStepInput = ResolvedStepInput, BranchResults = {}>(id: ID, action: ActionName, resolve?: (ctx: {
111
111
  input: Input;
112
- results: Results;
112
+ results: Results & BranchResults;
113
113
  context: Context;
114
- } & CallHelpers<Reg, ActionName>) => ResolveOut, options?: StepOptions<Input, Results, Context>): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K_1 in ID]: StepResultFromResolve<Reg, ActionName, ResolveOut>; } extends infer T ? { [K in keyof T]: T[K]; } : never, undefined>;
114
+ } & CallHelpers<Reg, ActionName>) => ResolveOut, options?: StepOptions<Input, Results & BranchResults, Context>): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K_1 in ID]: StepResultFromResolve<Reg, ActionName, ResolveOut>; } extends infer T ? { [K in keyof T]: T[K]; } : never, undefined>;
115
115
  subflow<Prefix extends string, SubSteps extends StepDef<Reg, any, any>[], WF extends WorkflowDef<Reg, any, any, SubSteps, any>>(prefix: Prefix, workflow: WF, resolveInput: WorkflowInput<WF> extends undefined ? ((ctx: {
116
116
  input: Input;
117
117
  results: Results;
@@ -1,3 +1,508 @@
1
+ // import {
2
+ // ActionParams,
3
+ // ActionRegistry,
4
+ // ActionReturn,
5
+ // Simplify,
6
+ // } from "./types.js";
7
+ //
8
+ // /* ------------------------------------------------ */
9
+ // /* STEP INPUT NORMALIZATION TYPES */
10
+ // /* ------------------------------------------------ */
11
+ //
12
+ // type NormalizedCall =
13
+ // | { kind: "none" }
14
+ // | { kind: "positional"; args: any[] }
15
+ // | { kind: "object"; args: any };
16
+ //
17
+ // export type ResolvedStepInput =
18
+ // | NormalizedCall
19
+ // | { kind: "loop"; items: NormalizedCall[] };
20
+ //
21
+ // type CallHelpers<Reg extends ActionRegistry, ActionName extends keyof Reg> = {
22
+ // args: (...args: ActionParams<Reg, ActionName>) => {
23
+ // kind: "positional";
24
+ // args: ActionParams<Reg, ActionName>;
25
+ // };
26
+ //
27
+ // obj: ActionParams<Reg, ActionName> extends [infer A]
28
+ // ? (arg: A) => { kind: "object"; args: A }
29
+ // : never;
30
+ //
31
+ // none: () => { kind: "none" };
32
+ // loop: (
33
+ // items:
34
+ // | { kind: "positional"; args: ActionParams<Reg, ActionName> }[]
35
+ // | { kind: "object"; args: ActionParams<Reg, ActionName>[0] }[],
36
+ // ) => {
37
+ // kind: "loop";
38
+ // items: typeof items;
39
+ // };
40
+ // };
41
+ //
42
+ // /* ------------------------------------------------ */
43
+ // /* STEP RESULT TYPES */
44
+ // /* ------------------------------------------------ */
45
+ //
46
+ // type SubflowResult<SubResults, SubOutput> = SubOutput extends undefined
47
+ // ? SubResults
48
+ // : SubOutput;
49
+ //
50
+ // type WorkflowInput<T> =
51
+ // T extends WorkflowDef<any, infer I, any, any, any> ? I : never;
52
+ //
53
+ // type WorkflowResults<T> =
54
+ // T extends WorkflowDef<any, any, infer R, any, any> ? R : never;
55
+ //
56
+ // type WorkflowOutput<T> =
57
+ // T extends WorkflowDef<any, any, any, any, infer O> ? O : never;
58
+ //
59
+ // export type StepResultFromResolve<
60
+ // Reg extends ActionRegistry,
61
+ // ActionName extends keyof Reg,
62
+ // R extends ResolvedStepInput,
63
+ // > = R extends { kind: "loop" }
64
+ // ? ActionReturn<Reg, ActionName>[]
65
+ // : ActionReturn<Reg, ActionName>;
66
+ //
67
+ // /* ------------------------------------------------ */
68
+ // /* STEP DEFINITION */
69
+ // /* ------------------------------------------------ */
70
+ //
71
+ // export type StepDef<
72
+ // Reg extends ActionRegistry,
73
+ // ID extends string = string,
74
+ // ActionName extends keyof Reg = any,
75
+ // > = {
76
+ // id: ID;
77
+ // action: ActionName;
78
+ // dependsOn: string[];
79
+ // resolve: (ctx: any) => ResolvedStepInput;
80
+ // when?: (ctx: any) => boolean;
81
+ // options?: StepOptions<any, any, any>;
82
+ // };
83
+ //
84
+ // /* ------------------------------------------------ */
85
+ // /* WORKFLOW DEFINITION */
86
+ // /* ------------------------------------------------ */
87
+ //
88
+ // export type WorkflowDef<
89
+ // Reg extends ActionRegistry,
90
+ // Input,
91
+ // Results,
92
+ // Steps extends StepDef<Reg, any, any>[] = StepDef<Reg, any, any>[],
93
+ // Output = undefined,
94
+ // > = {
95
+ // name: string;
96
+ // steps: Steps;
97
+ // entrySteps: StepDef<Reg>[];
98
+ // endSteps: StepDef<Reg>[];
99
+ // input: Input;
100
+ // results: Results;
101
+ // outputResolver?: (ctx: any) => Output;
102
+ // // __context?: any;
103
+ // };
104
+ // type StepRuntimeCtx<I, R, C> = {
105
+ // input: I;
106
+ // results: R;
107
+ // context: C;
108
+ // };
109
+ // type StepOptions<Input, Results, Context> = {
110
+ // retry?: number;
111
+ // retryDelay?: number | ((attempt: number) => number);
112
+ // timeout?: number;
113
+ //
114
+ // continueOnError?: boolean;
115
+ //
116
+ // onError?: (err: unknown, ctx: StepRuntimeCtx<Input, Results, Context>) => any;
117
+ //
118
+ // // optional later:
119
+ // label?: string;
120
+ // meta?: Record<string, any>;
121
+ // };
122
+ //
123
+ // /* ------------------------------------------------ */
124
+ // /* HELPER TYPES */
125
+ // /* ------------------------------------------------ */
126
+ //
127
+ // type MergeBranchResults<
128
+ // Branches extends readonly any[],
129
+ // Acc,
130
+ // > = Branches extends readonly [infer Head, ...infer Tail]
131
+ // ? MergeBranchResults<
132
+ // Tail,
133
+ // Acc & (Head extends WorkflowBuilder<any, any, any, any, infer R> ? R : {})
134
+ // >
135
+ // : Acc;
136
+ //
137
+ // type MergeBranchSteps<
138
+ // Branches extends readonly any[],
139
+ // Acc extends any[],
140
+ // > = Branches extends readonly [infer Head, ...infer Tail]
141
+ // ? MergeBranchSteps<
142
+ // Tail,
143
+ // [
144
+ // ...Acc,
145
+ // ...(Head extends WorkflowBuilder<any, any, any, infer S, any> ? S : []),
146
+ // ]
147
+ // >
148
+ // : Acc;
149
+ //
150
+ // // type UnionToIntersection<U> = (U extends any ? (x: U) => any : never) extends (
151
+ // // x: infer I,
152
+ // // ) => any
153
+ // // ? I
154
+ // // : never;
155
+ // //
156
+ // // type ExtractBranchResults<Branches extends readonly any[]> =
157
+ // // Branches[number] extends WorkflowBuilder<any, any, any, any, infer R>
158
+ // // ? R
159
+ // // : never;
160
+ // //
161
+ // // type MergeBranchResults<Branches extends readonly any[], Acc> = Simplify<
162
+ // // Acc & Partial<ExtractBranchResults<Branches>>
163
+ // // >;
164
+ //
165
+ // /* ------------------------------------------------ */
166
+ // /* FLUENT WORKFLOW BUILDER */
167
+ // /* ------------------------------------------------ */
168
+ //
169
+ // export class WorkflowBuilder<
170
+ // Reg extends ActionRegistry,
171
+ // Input = unknown,
172
+ // Context = unknown,
173
+ // Steps extends StepDef<Reg, any, any>[] = [],
174
+ // Results = {},
175
+ // Output = undefined,
176
+ // > {
177
+ // private steps: StepDef<Reg, any, any>[] = [];
178
+ // private frontier: string[] = [];
179
+ // private pendingWhen?: (ctx: {
180
+ // input: Input;
181
+ // results: Results;
182
+ // context: Context;
183
+ // }) => boolean;
184
+ // private outputResolver?: (ctx: any) => any;
185
+ // private clearPendingWhen() {
186
+ // this.pendingWhen = undefined;
187
+ // }
188
+ //
189
+ // constructor(private name: string) {}
190
+ //
191
+ // step<
192
+ // ID extends string,
193
+ // ActionName extends keyof Reg & string,
194
+ // ResolveOut extends ResolvedStepInput = ResolvedStepInput,
195
+ // >(
196
+ // id: ID,
197
+ // action: ActionName,
198
+ // resolve?: (
199
+ // ctx: {
200
+ // input: Input;
201
+ // results: Results;
202
+ // context: Context;
203
+ // } & CallHelpers<Reg, ActionName>,
204
+ // ) => ResolveOut,
205
+ // dependsOn?: string[],
206
+ // options?: StepOptions<Input, Results, Context>,
207
+ // ): WorkflowBuilder<
208
+ // Reg,
209
+ // Input,
210
+ // Context,
211
+ // [...Steps, StepDef<Reg, ID, ActionName>],
212
+ // Simplify<
213
+ // Results & {
214
+ // [K in ID]: StepResultFromResolve<Reg, ActionName, ResolveOut>;
215
+ // }
216
+ // >
217
+ // > {
218
+ // const deps = dependsOn ?? [...this.frontier];
219
+ //
220
+ // this.steps.push({
221
+ // id,
222
+ // action,
223
+ // resolve: resolve ?? (() => ({ kind: "none" })),
224
+ // dependsOn: deps,
225
+ // when: this.pendingWhen,
226
+ // options,
227
+ // });
228
+ //
229
+ // this.frontier = [id];
230
+ //
231
+ // return this as any;
232
+ // }
233
+ //
234
+ // /* ------------------------------------------------ */
235
+ // /* Sequential shortcut */
236
+ // /* ------------------------------------------------ */
237
+ // seq<
238
+ // ID extends string = string,
239
+ // ActionName extends keyof Reg & string = any,
240
+ // ResolveOut extends ResolvedStepInput = ResolvedStepInput,
241
+ // >(
242
+ // id: ID,
243
+ // action: ActionName,
244
+ // resolve?: (
245
+ // ctx: {
246
+ // input: Input;
247
+ // results: Results;
248
+ // context: Context;
249
+ // } & CallHelpers<Reg, ActionName>,
250
+ // ) => ResolveOut,
251
+ // options?: StepOptions<Input, Results, Context>,
252
+ // ) {
253
+ // return this.step<ID, ActionName, ResolveOut>(
254
+ // id,
255
+ // action,
256
+ // resolve,
257
+ // undefined,
258
+ // options,
259
+ // );
260
+ // }
261
+ //
262
+ // /* ------------------------------------------------ */
263
+ // /* Override the result of the last step */
264
+ // /* ------------------------------------------------ */
265
+ // as<NewType>(): WorkflowBuilder<
266
+ // Reg,
267
+ // Input,
268
+ // Context,
269
+ // Steps,
270
+ // // Override the result of the last step only
271
+ // Steps extends [...infer Rest, infer Last]
272
+ // ? Last extends StepDef<Reg, infer ID, any>
273
+ // ? Simplify<Omit<Results, ID> & { [K in ID]: NewType }>
274
+ // : Results
275
+ // : Results,
276
+ // Output
277
+ // > {
278
+ // return this as any;
279
+ // }
280
+ //
281
+ // parallel<
282
+ // Branches extends readonly WorkflowBuilder<Reg, Input, Context, any, any>[],
283
+ // >(
284
+ // ...branches: {
285
+ // [K in keyof Branches]: (
286
+ // builder: WorkflowBuilder<Reg, Input, Context, [], Results>,
287
+ // ) => Branches[K];
288
+ // }
289
+ // ): WorkflowBuilder<
290
+ // Reg,
291
+ // Input,
292
+ // Context,
293
+ // MergeBranchSteps<Branches, Steps>,
294
+ // // [
295
+ // // ...Steps,
296
+ // // ...(Branches[number] extends WorkflowBuilder<Reg, any, any, infer S, any>
297
+ // // ? S
298
+ // // : never),
299
+ // // ],
300
+ // Simplify<MergeBranchResults<Branches, Results>>
301
+ // > {
302
+ // const parentFrontier = [...this.frontier];
303
+ // const branchEnds: string[] = [];
304
+ //
305
+ // branches.forEach((branch) => {
306
+ // const b = new WorkflowBuilder<Reg, Input, Context, [], Results>(
307
+ // this.name,
308
+ // );
309
+ //
310
+ // b.frontier = parentFrontier;
311
+ // branch(b);
312
+ // branchEnds.push(...b.frontier);
313
+ // this.steps.push(...(b as any).steps);
314
+ // });
315
+ //
316
+ // this.frontier = branchEnds;
317
+ // this.clearPendingWhen();
318
+ // return this as any;
319
+ // }
320
+ //
321
+ // /* ------------------------------------------------ */
322
+ // /* Join helper */
323
+ // /* ------------------------------------------------ */
324
+ // join<
325
+ // ID extends string = string,
326
+ // ActionName extends keyof Reg & string = any,
327
+ // ResolveOut extends ResolvedStepInput = ResolvedStepInput,
328
+ // >(
329
+ // id: ID,
330
+ // action: ActionName,
331
+ // resolve?: (
332
+ // ctx: {
333
+ // input: Input;
334
+ // results: Results;
335
+ // context: Context;
336
+ // } & CallHelpers<Reg, ActionName>,
337
+ // ) => ResolveOut,
338
+ // options?: StepOptions<Input, Results, Context>,
339
+ // ) {
340
+ // const result = this.step<ID, ActionName, ResolveOut>(
341
+ // id,
342
+ // action,
343
+ // resolve,
344
+ // [...this.frontier],
345
+ // options,
346
+ // );
347
+ //
348
+ // this.clearPendingWhen();
349
+ // return result;
350
+ // }
351
+ //
352
+ // /* ------------------------------------------------ */
353
+ // /* Subflow */
354
+ // /* ------------------------------------------------ */
355
+ //
356
+ // subflow<
357
+ // Prefix extends string,
358
+ // SubSteps extends StepDef<Reg, any, any>[],
359
+ // WF extends WorkflowDef<Reg, any, any, SubSteps, any>,
360
+ // >(
361
+ // prefix: Prefix,
362
+ // workflow: WF,
363
+ // resolveInput: WorkflowInput<WF> extends undefined
364
+ // ?
365
+ // | ((ctx: {
366
+ // input: Input;
367
+ // results: Results;
368
+ // context: Context;
369
+ // }) => WorkflowInput<WF>)
370
+ // | undefined
371
+ // : (ctx: {
372
+ // input: Input;
373
+ // results: Results;
374
+ // context: Context;
375
+ // }) => WorkflowInput<WF>,
376
+ // options?: StepOptions<Input, Results, Context>,
377
+ // ): WorkflowBuilder<
378
+ // Reg,
379
+ // Input,
380
+ // Context,
381
+ // [...Steps, ...WF["steps"]],
382
+ // Results & {
383
+ // [K in Prefix]: SubflowResult<WorkflowResults<WF>, WorkflowOutput<WF>>;
384
+ // }
385
+ // > {
386
+ // const idMap = new Map<string, string>();
387
+ //
388
+ // workflow.steps.forEach((step) => {
389
+ // idMap.set(step.id, `${prefix}.${step.id}`);
390
+ // });
391
+ //
392
+ // workflow.steps.forEach((step) => {
393
+ // const newStep: StepDef<Reg, any, any> = {
394
+ // ...step,
395
+ // id: idMap.get(step.id)!,
396
+ // dependsOn: step.dependsOn.map((d) => idMap.get(d)!),
397
+ // resolve: (ctx: any) => {
398
+ // const subInput = resolveInput ? resolveInput(ctx) : undefined;
399
+ // return step.resolve({
400
+ // input: subInput,
401
+ // results: ctx.results,
402
+ // context: ctx.context,
403
+ // });
404
+ // },
405
+ // };
406
+ //
407
+ // if (workflow.entrySteps.find((e) => e.id === step.id)) {
408
+ // newStep.dependsOn = [...this.frontier];
409
+ // }
410
+ //
411
+ // this.steps.push(newStep);
412
+ // });
413
+ //
414
+ // this.frontier = workflow.endSteps.map((e) => idMap.get(e.id)!);
415
+ // return this as any;
416
+ // }
417
+ //
418
+ // /* ------------------------------------------------ */
419
+ // /* Conditional */
420
+ // /* ------------------------------------------------ */
421
+ //
422
+ // when(
423
+ // predicate: (ctx: {
424
+ // input: Input;
425
+ // results: Results;
426
+ // context: Context;
427
+ // }) => boolean,
428
+ // ): WorkflowBuilder<Reg, Input, Context, Steps, Results, Output> {
429
+ // this.pendingWhen = predicate;
430
+ // return this;
431
+ // }
432
+ //
433
+ // endWhen(): this {
434
+ // this.clearPendingWhen();
435
+ //
436
+ // return this;
437
+ // }
438
+ //
439
+ // /* ------------------------------------------------ */
440
+ // /* Workflow output */
441
+ // /* ------------------------------------------------ */
442
+ // output<Output>(
443
+ // fn: (ctx: { input: Input; results: Results; context: Context }) => Output,
444
+ // ): WorkflowDef<Reg, Input, Results, Steps, Output> {
445
+ // this.outputResolver = fn;
446
+ // return this.build() as WorkflowDef<Reg, Input, Results, Steps, Output>;
447
+ // }
448
+ //
449
+ // build(): WorkflowDef<Reg, Input, Results, Steps> {
450
+ // this.validateDependencies();
451
+ //
452
+ // return {
453
+ // name: this.name,
454
+ // steps: this.steps as Steps,
455
+ // entrySteps: this.steps.filter((s) => s.dependsOn.length === 0),
456
+ // endSteps: this.getEndSteps(),
457
+ // input: {} as Input,
458
+ // results: {} as Results,
459
+ // outputResolver: this.outputResolver,
460
+ // };
461
+ // }
462
+ //
463
+ // private validateDependencies() {
464
+ // const stepIds = new Set(this.steps.map((s) => s.id));
465
+ // for (const step of this.steps) {
466
+ // for (const dep of step.dependsOn) {
467
+ // if (!stepIds.has(dep))
468
+ // throw new Error(`Step ${step.id} depends on unknown step ${dep}`);
469
+ // }
470
+ // }
471
+ // }
472
+ //
473
+ // private getEndSteps() {
474
+ // const hasDependents = new Set<string>();
475
+ // for (const step of this.steps) {
476
+ // for (const dep of step.dependsOn) hasDependents.add(dep);
477
+ // }
478
+ // return this.steps.filter((s) => !hasDependents.has(s.id));
479
+ // }
480
+ // }
481
+ //
482
+ // /* ------------------------------------------------ */
483
+ // /* WORKFLOW CREATOR */
484
+ // /* ------------------------------------------------ */
485
+ // // export function createWorkflow<
486
+ // // Reg extends ActionRegistry,
487
+ // // Context extends Record<string, any> = {},
488
+ // // >(registry: Reg, context?: Context) {
489
+ // // return function workflow<Input = unknown>(name: string) {
490
+ // // return new WorkflowBuilder<Reg, Input, Context>(name);
491
+ // // };
492
+ // // }
493
+ //
494
+ // export function createWorkflow<
495
+ // Reg extends ActionRegistry,
496
+ // Context extends Record<string, any> = {},
497
+ // >() {
498
+ // return function workflow<Input = unknown>(name: string) {
499
+ // return new WorkflowBuilder<Reg, Input, Context>(name);
500
+ // };
501
+ // }
502
+ //
503
+ //
504
+ //
505
+ //
1
506
  /* ------------------------------------------------ */
2
507
  /* FLUENT WORKFLOW BUILDER */
3
508
  /* ------------------------------------------------ */
@@ -47,6 +552,7 @@ export class WorkflowBuilder {
47
552
  });
48
553
  this.frontier = branchEnds;
49
554
  this.clearPendingWhen();
555
+ // TypeScript now knows the returned WorkflowBuilder's Results = parent & branch results
50
556
  return this;
51
557
  }
52
558
  /* ------------------------------------------------ */
@@ -138,14 +644,6 @@ export class WorkflowBuilder {
138
644
  /* ------------------------------------------------ */
139
645
  /* WORKFLOW CREATOR */
140
646
  /* ------------------------------------------------ */
141
- // export function createWorkflow<
142
- // Reg extends ActionRegistry,
143
- // Context extends Record<string, any> = {},
144
- // >(registry: Reg, context?: Context) {
145
- // return function workflow<Input = unknown>(name: string) {
146
- // return new WorkflowBuilder<Reg, Input, Context>(name);
147
- // };
148
- // }
149
647
  export function createWorkflow() {
150
648
  return function workflow(name) {
151
649
  return new WorkflowBuilder(name);
@@ -1,5 +1,15 @@
1
1
  import { ActionRegistry, WorkflowObserver } from "./types.js";
2
2
  import { createWorkflow, WorkflowDef } from "./workflow-composer.js";
3
+ type ModuleWFRegistry<Own extends ModuleShape, Deps extends ModuleMap> = OwnWFs<Own> & DepsWFs<Deps>;
4
+ type OwnWFs<Own extends ModuleShape> = {
5
+ [K in keyof Own & string]: Own[K];
6
+ };
7
+ type NamespacedDepWFs<DepName extends string, M extends Module<any, any, any, any>> = {
8
+ [K in keyof M["own"] & string as `${DepName}.${K}`]: M["own"][K];
9
+ };
10
+ type DepsWFs<Deps extends ModuleMap> = {
11
+ [DepName in keyof Deps & string]: NamespacedDepWFs<DepName, Deps[DepName]>;
12
+ }[keyof Deps & string];
3
13
  type ModuleDepsPublic<Use> = {
4
14
  [K in keyof Use]: Use[K] extends Module<any, infer Ctx, infer Own, any> ? ModulePublic<Ctx, Own> : never;
5
15
  };
@@ -17,9 +27,6 @@ type ModuleMap = Record<string, Module<any, any, any, any>>;
17
27
  export type WorkflowInput<W> = W extends WorkflowDef<any, infer I, any, any, any> ? I : never;
18
28
  export type WorkflowResults<W> = W extends WorkflowDef<any, any, infer R, any, any> ? R : never;
19
29
  export type WorkflowOutput<W> = W extends WorkflowDef<any, any, any, any, infer O> ? O : never;
20
- type WorkflowFromDeps<Deps> = {
21
- [K in keyof Deps]: Deps[K] extends Module<any, any, infer Own, any> ? Own[keyof Own] : never;
22
- }[keyof Deps];
23
30
  type Module<Reg extends ActionRegistry, Context extends Record<string, any>, Own extends ModuleShape = {}, Deps extends ModuleMap = {}> = {
24
31
  own: Own;
25
32
  deps: ModuleDepsPublic<Deps>;
@@ -27,9 +34,9 @@ type Module<Reg extends ActionRegistry, Context extends Record<string, any>, Own
27
34
  registry: Reg;
28
35
  context: FinalContext<Reg, Context, Deps>;
29
36
  }) => {
30
- run: <W extends Own[keyof Own] | WorkflowFromDeps<Deps>>(workflow: W, input: WorkflowInput<W>, observers?: WorkflowObserver<Reg>[]) => Promise<{
31
- results: WorkflowResults<W>;
32
- output: WorkflowOutput<W>;
37
+ run: <K extends keyof ModuleWFRegistry<Own, Deps>>(workflow: K, input: WorkflowInput<ModuleWFRegistry<Own, Deps>[K]>, observers?: WorkflowObserver<Reg>[]) => Promise<{
38
+ results: WorkflowResults<ModuleWFRegistry<Own, Deps>[K]>;
39
+ output: WorkflowOutput<ModuleWFRegistry<Own, Deps>[K]>;
33
40
  extras: Record<string, any>;
34
41
  }>;
35
42
  getContext: () => FinalContext<Reg, Context, Deps>;
@@ -149,29 +149,32 @@ function toPublicDeps(deps) {
149
149
  return deps;
150
150
  }
151
151
  function createModule(config) {
152
- const wf = createWorkflow();
153
152
  const deps = (config.use ?? {});
154
- const moduleCtx = {
155
- wf,
153
+ const own = config.define({
154
+ wf: createWorkflow(),
156
155
  deps,
157
156
  context: {},
158
157
  tools: (factory) => factory({
159
- wf,
158
+ wf: createWorkflow(),
160
159
  deps,
161
160
  context: {},
162
161
  }),
163
- };
164
- const own = config.define(moduleCtx);
162
+ });
163
+ function mergeWFs() {
164
+ const depWFs = Object.fromEntries(Object.entries(deps).flatMap(([depName, depModule]) => Object.entries(depModule.own).map(([k, wf]) => [`${depName}.${k}`, wf])));
165
+ return { ...own, ...depWFs };
166
+ }
165
167
  return {
166
168
  own,
167
169
  deps: toPublicDeps(deps),
168
170
  createRuntime({ registry, context }) {
169
- const runtimeCtx = { ...context };
171
+ const workflowRegistry = mergeWFs();
170
172
  return {
171
- run: async (workflow, input, observers = []) => {
172
- return executeWorkflow(workflow, registry, input, context, observers);
173
+ run: async (workflowId, input, observers = []) => {
174
+ const wfObj = workflowRegistry[workflowId]; // <-- concrete WorkflowDef
175
+ return executeWorkflow(wfObj, registry, input, context, observers);
173
176
  },
174
- getContext: () => ({ ...runtimeCtx }),
177
+ getContext: () => ({ ...context }),
175
178
  };
176
179
  },
177
180
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pogodisco/zephyr",
3
- "version": "1.3.15",
3
+ "version": "1.3.17",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },