@pogodisco/zephyr 1.0.0 → 1.2.0

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.
@@ -17,17 +17,19 @@ export type WorkflowDef<Reg extends ActionRegistry, Input, Results, Steps extend
17
17
  outputResolver?: (ctx: any) => Output;
18
18
  };
19
19
  type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
20
- export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown, Steps extends StepDef<Reg, any, any>[] = [], Results = {}> {
20
+ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown, Context extends Record<string, any> = {}, Steps extends StepDef<Reg, any, any>[] = [], Results = {}> {
21
21
  private name;
22
22
  private registry;
23
+ private context;
23
24
  private steps;
24
25
  private frontier;
25
26
  private outputResolver?;
26
- constructor(name: string, registry: Reg);
27
+ constructor(name: string, registry: Reg, context: Context);
27
28
  step<ID extends string, ActionName extends keyof Reg & string>(id: ID, action: ActionName, resolve: (ctx: {
28
29
  input: Input;
29
30
  results: Results;
30
- }) => Parameters<Reg[ActionName]>[0], dependsOn?: string[]): WorkflowBuilder<Reg, Input, [
31
+ context: Context;
32
+ }) => Parameters<Reg[ActionName]>[0], dependsOn?: string[]): WorkflowBuilder<Reg, Input, Context, [
31
33
  ...Steps,
32
34
  StepDef<Reg, ID, ActionName>
33
35
  ], Results & {
@@ -36,21 +38,23 @@ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown
36
38
  seq<ID extends string, ActionName extends keyof Reg & string>(id: ID, action: ActionName, resolve: (ctx: {
37
39
  input: Input;
38
40
  results: Results;
39
- }) => Parameters<Reg[ActionName]>[0]): WorkflowBuilder<Reg, Input, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: Awaited<ReturnType<Reg[ActionName]>>; }>;
40
- parallel<Branches extends WorkflowBuilder<Reg, Input, any, any>[]>(...branches: {
41
- [K in keyof Branches]: (builder: WorkflowBuilder<Reg, Input, [], Results>) => Branches[K];
42
- }): WorkflowBuilder<Reg, Input, [
41
+ context: Context;
42
+ }) => Parameters<Reg[ActionName]>[0]): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: Awaited<ReturnType<Reg[ActionName]>>; }>;
43
+ parallel<Branches extends WorkflowBuilder<Reg, Input, Context, any, any>[]>(...branches: {
44
+ [K in keyof Branches]: (builder: WorkflowBuilder<Reg, Input, Context, [], Results>) => Branches[K];
45
+ }): WorkflowBuilder<Reg, Input, Context, [
43
46
  ...Steps,
44
- ...(Branches[number] extends WorkflowBuilder<Reg, any, infer S, any> ? S : never)
45
- ], Results & (Branches[number] extends WorkflowBuilder<Reg, any, any, infer R> ? UnionToIntersection<R> : {})>;
47
+ ...(Branches[number] extends WorkflowBuilder<Reg, any, any, infer S, any> ? S : never)
48
+ ], Results & (Branches[number] extends WorkflowBuilder<Reg, any, any, any, infer R> ? UnionToIntersection<R> : {})>;
46
49
  join<ID extends string, ActionName extends keyof Reg & string>(id: ID, action: ActionName, resolve: (ctx: {
47
50
  input: Input;
48
51
  results: Results;
49
- }) => Parameters<Reg[ActionName]>[0]): WorkflowBuilder<Reg, Input, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: Awaited<ReturnType<Reg[ActionName]>>; }>;
52
+ }) => Parameters<Reg[ActionName]>[0]): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: Awaited<ReturnType<Reg[ActionName]>>; }>;
50
53
  subflow<Prefix extends string, SubInput, SubResults, SubSteps extends StepDef<Reg, any, any>[]>(prefix: Prefix, workflow: WorkflowDef<Reg, SubInput, SubResults, SubSteps>, resolveInput: (ctx: {
51
54
  input: Input;
52
55
  results: Results;
53
- }) => SubInput): WorkflowBuilder<Reg, Input, [
56
+ context: Context;
57
+ }) => SubInput): WorkflowBuilder<Reg, Input, Context, [
54
58
  ...Steps,
55
59
  ...SubSteps
56
60
  ], Results & {
@@ -59,10 +63,11 @@ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown
59
63
  output<Output>(fn: (ctx: {
60
64
  input: Input;
61
65
  results: Results;
66
+ context: Context;
62
67
  }) => Output): WorkflowDef<Reg, Input, Results, Steps, Output>;
63
68
  build(): WorkflowDef<Reg, Input, Results, Steps>;
64
69
  private validateDependencies;
65
70
  private getEndSteps;
66
71
  }
67
- export declare function createWorkflow<Reg extends ActionRegistry>(registry: Reg): <Input = unknown>(name: string) => WorkflowBuilder<Reg, Input, [], {}>;
72
+ export declare function createWorkflow<Reg extends ActionRegistry, Context extends Record<string, any> = {}>(registry: Reg, context?: Context): <Input = unknown>(name: string) => WorkflowBuilder<Reg, Input, Context, [], {}>;
68
73
  export {};
@@ -276,10 +276,279 @@
276
276
  // };
277
277
  // }
278
278
  //
279
+ //////////////////////////////////////////////////////////////
280
+ // import { ActionRegistry } from "./types.js";
281
+ //
282
+ // type StepResult<
283
+ // Reg extends ActionRegistry,
284
+ // ActionName extends keyof Reg,
285
+ // > = Awaited<ReturnType<Reg[ActionName]>>;
286
+ //
287
+ // export type StepDef<
288
+ // Reg extends ActionRegistry,
289
+ // ID extends string = string,
290
+ // ActionName extends keyof Reg = any,
291
+ // > = {
292
+ // id: ID;
293
+ // action: ActionName;
294
+ // dependsOn: string[];
295
+ // resolve: (ctx: any) => Parameters<Reg[ActionName]>[0];
296
+ // when?: (ctx: any) => boolean;
297
+ // };
298
+ //
299
+ // export type WorkflowDef<
300
+ // Reg extends ActionRegistry,
301
+ // Input,
302
+ // Results,
303
+ // Steps extends StepDef<Reg, any, any>[] = StepDef<Reg, any, any>[],
304
+ // Output = undefined,
305
+ // > = {
306
+ // name: string;
307
+ // steps: Steps;
308
+ // entrySteps: StepDef<Reg>[];
309
+ // endSteps: StepDef<Reg>[];
310
+ // input: Input;
311
+ // results: Results;
312
+ // outputResolver?: (ctx: any) => Output;
313
+ // };
314
+ //
315
+ // type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
316
+ // k: infer I,
317
+ // ) => void
318
+ // ? I
319
+ // : never;
320
+ //
321
+ // export class WorkflowBuilder<
322
+ // Reg extends ActionRegistry,
323
+ // Input = unknown,
324
+ // Steps extends StepDef<Reg, any, any>[] = [],
325
+ // Results = {},
326
+ // > {
327
+ // private steps: StepDef<Reg, any, any>[] = [];
328
+ // private frontier: string[] = [];
329
+ //
330
+ // private outputResolver?: (ctx: any) => any;
331
+ // constructor(
332
+ // private name: string,
333
+ // private registry: Reg,
334
+ // ) {}
335
+ //
336
+ // /* ------------------------------------------------ */
337
+ // /* Base Step */
338
+ // /* ------------------------------------------------ */
339
+ //
340
+ // step<ID extends string, ActionName extends keyof Reg & string>(
341
+ // id: ID,
342
+ // action: ActionName,
343
+ // resolve: (ctx: {
344
+ // input: Input;
345
+ // results: Results;
346
+ // }) => Parameters<Reg[ActionName]>[0],
347
+ // dependsOn?: string[],
348
+ // ): WorkflowBuilder<
349
+ // Reg,
350
+ // Input,
351
+ // [...Steps, StepDef<Reg, ID, ActionName>],
352
+ // Results & { [K in ID]: StepResult<Reg, ActionName> }
353
+ // > {
354
+ // const deps = dependsOn ?? [...this.frontier];
355
+ //
356
+ // this.steps.push({
357
+ // id,
358
+ // action,
359
+ // resolve,
360
+ // dependsOn: deps,
361
+ // });
362
+ //
363
+ // this.frontier = [id];
364
+ //
365
+ // return this as any;
366
+ // }
367
+ //
368
+ // /* ------------------------------------------------ */
369
+ // /* Sequential shortcut */
370
+ // /* ------------------------------------------------ */
371
+ //
372
+ // seq<ID extends string, ActionName extends keyof Reg & string>(
373
+ // id: ID,
374
+ // action: ActionName,
375
+ // resolve: (ctx: {
376
+ // input: Input;
377
+ // results: Results;
378
+ // }) => Parameters<Reg[ActionName]>[0],
379
+ // ) {
380
+ // return this.step(id, action, resolve);
381
+ // }
382
+ //
383
+ // /* ------------------------------------------------ */
384
+ // /* Parallel branches */
385
+ // parallel<Branches extends WorkflowBuilder<Reg, Input, any, any>[]>(
386
+ // ...branches: {
387
+ // [K in keyof Branches]: (
388
+ // builder: WorkflowBuilder<Reg, Input, [], Results>,
389
+ // ) => Branches[K];
390
+ // }
391
+ // ): WorkflowBuilder<
392
+ // Reg,
393
+ // Input,
394
+ // [
395
+ // ...Steps,
396
+ // ...(Branches[number] extends WorkflowBuilder<Reg, any, infer S, any>
397
+ // ? S
398
+ // : never),
399
+ // ],
400
+ // Results &
401
+ // (Branches[number] extends WorkflowBuilder<Reg, any, any, infer R>
402
+ // ? UnionToIntersection<R>
403
+ // : {})
404
+ // > {
405
+ // const parentFrontier = [...this.frontier];
406
+ // const branchEnds: string[] = [];
407
+ //
408
+ // branches.forEach((branch) => {
409
+ // const b = new WorkflowBuilder<Reg, Input, [], Results>(
410
+ // this.name,
411
+ // this.registry,
412
+ // );
413
+ //
414
+ // b.frontier = parentFrontier;
415
+ //
416
+ // branch(b);
417
+ //
418
+ // branchEnds.push(...b.frontier);
419
+ //
420
+ // this.steps.push(...(b as any).steps);
421
+ // });
422
+ //
423
+ // this.frontier = branchEnds;
424
+ //
425
+ // return this as any;
426
+ // }
427
+ //
428
+ // /* ------------------------------------------------ */
429
+ // /* Join helper */
430
+ // /* ------------------------------------------------ */
431
+ //
432
+ // join<ID extends string, ActionName extends keyof Reg & string>(
433
+ // id: ID,
434
+ // action: ActionName,
435
+ // resolve: (ctx: {
436
+ // input: Input;
437
+ // results: Results;
438
+ // }) => Parameters<Reg[ActionName]>[0],
439
+ // ) {
440
+ // return this.step(id, action, resolve, [...this.frontier]);
441
+ // }
442
+ //
443
+ // /* ------------------------------------------------ */
444
+ // /* Subflow */
445
+ // /* ------------------------------------------------ */
446
+ //
447
+ // subflow<
448
+ // Prefix extends string,
449
+ // SubInput,
450
+ // SubResults,
451
+ // SubSteps extends StepDef<Reg, any, any>[],
452
+ // >(
453
+ // prefix: Prefix,
454
+ // workflow: WorkflowDef<Reg, SubInput, SubResults, SubSteps>,
455
+ // resolveInput: (ctx: { input: Input; results: Results }) => SubInput,
456
+ // ): WorkflowBuilder<
457
+ // Reg,
458
+ // Input,
459
+ // [...Steps, ...SubSteps],
460
+ // Results & { [K in Prefix]: SubResults }
461
+ // > {
462
+ // const idMap = new Map<string, string>();
463
+ //
464
+ // workflow.steps.forEach((step) => {
465
+ // idMap.set(step.id, `${prefix}.${step.id}`);
466
+ // });
467
+ //
468
+ // workflow.steps.forEach((step) => {
469
+ // const newStep = {
470
+ // ...step,
471
+ //
472
+ // id: idMap.get(step.id)!,
473
+ //
474
+ // dependsOn: step.dependsOn.map((d) => idMap.get(d)!),
475
+ //
476
+ // resolve: (ctx: any) => {
477
+ // const subInput = resolveInput(ctx);
478
+ //
479
+ // return step.resolve({
480
+ // input: subInput,
481
+ // results: ctx.results,
482
+ // });
483
+ // },
484
+ // };
485
+ //
486
+ // if (workflow.entrySteps.find((e) => e.id === step.id)) {
487
+ // newStep.dependsOn = [...this.frontier];
488
+ // }
489
+ //
490
+ // this.steps.push(newStep);
491
+ // });
492
+ //
493
+ // this.frontier = workflow.endSteps.map((e) => idMap.get(e.id)!);
494
+ //
495
+ // return this as any;
496
+ // }
497
+ //
498
+ // output<Output>(
499
+ // fn: (ctx: { input: Input; results: Results }) => Output,
500
+ // ): WorkflowDef<Reg, Input, Results, Steps, Output> {
501
+ // this.outputResolver = fn;
502
+ // return this.build() as WorkflowDef<Reg, Input, Results, Steps, Output>;
503
+ // }
504
+ // /* ------------------------------------------------ */
505
+ // /* Build */
506
+ // /* ------------------------------------------------ */
507
+ // build(): WorkflowDef<Reg, Input, Results, Steps> {
508
+ // this.validateDependencies();
509
+ //
510
+ // return {
511
+ // name: this.name,
512
+ // steps: this.steps as Steps,
513
+ // entrySteps: this.steps.filter((s) => s.dependsOn.length === 0),
514
+ // endSteps: this.getEndSteps(),
515
+ // input: {} as Input,
516
+ // results: {} as Results,
517
+ // outputResolver: this.outputResolver,
518
+ // };
519
+ // }
520
+ //
521
+ // /* ------------------------------------------------ */
522
+ //
523
+ // private validateDependencies() {
524
+ // const stepIds = new Set(this.steps.map((s) => s.id));
525
+ //
526
+ // for (const step of this.steps) {
527
+ // for (const dep of step.dependsOn) {
528
+ // if (!stepIds.has(dep)) {
529
+ // throw new Error(`Step ${step.id} depends on unknown step ${dep}`);
530
+ // }
531
+ // }
532
+ // }
533
+ // }
534
+ //
535
+ // private getEndSteps() {
536
+ // const hasDependents = new Set<string>();
537
+ //
538
+ // for (const step of this.steps) {
539
+ // for (const dep of step.dependsOn) {
540
+ // hasDependents.add(dep);
541
+ // }
542
+ // }
543
+ //
544
+ // return this.steps.filter((s) => !hasDependents.has(s.id));
545
+ // }
546
+ // }
279
547
  export class WorkflowBuilder {
280
- constructor(name, registry) {
548
+ constructor(name, registry, context) {
281
549
  this.name = name;
282
550
  this.registry = registry;
551
+ this.context = context;
283
552
  this.steps = [];
284
553
  this.frontier = [];
285
554
  }
@@ -309,7 +578,7 @@ export class WorkflowBuilder {
309
578
  const parentFrontier = [...this.frontier];
310
579
  const branchEnds = [];
311
580
  branches.forEach((branch) => {
312
- const b = new WorkflowBuilder(this.name, this.registry);
581
+ const b = new WorkflowBuilder(this.name, this.registry, this.context);
313
582
  b.frontier = parentFrontier;
314
583
  branch(b);
315
584
  branchEnds.push(...b.frontier);
@@ -342,6 +611,7 @@ export class WorkflowBuilder {
342
611
  return step.resolve({
343
612
  input: subInput,
344
613
  results: ctx.results,
614
+ context: ctx.context,
345
615
  });
346
616
  },
347
617
  };
@@ -393,13 +663,8 @@ export class WorkflowBuilder {
393
663
  return this.steps.filter((s) => !hasDependents.has(s.id));
394
664
  }
395
665
  }
396
- // export function createWorkflow<Reg extends ActionRegistry, Input = unknown>(
397
- // registry: Reg,
398
- // ) {
399
- // return (name: string) => new WorkflowBuilder<Reg, Input>(name, registry);
400
- // }
401
- export function createWorkflow(registry) {
666
+ export function createWorkflow(registry, context) {
402
667
  return function workflow(name) {
403
- return new WorkflowBuilder(name, registry);
668
+ return new WorkflowBuilder(name, registry, context || {});
404
669
  };
405
670
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pogodisco/zephyr",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },