@pogodisco/zephyr 1.3.2 → 1.3.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/dist/types.d.ts CHANGED
@@ -1,8 +1,6 @@
1
1
  import { createWorkflow } from "./workflow-composer.js";
2
2
  export type Action<F extends (...args: any[]) => any = (...args: any[]) => any> = F;
3
- export interface ActionRegistry {
4
- [key: string]: Action;
5
- }
3
+ export type ActionRegistry = Record<string, (...args: any[]) => any>;
6
4
  export type MergeActionRegistries<A extends ActionRegistry, B extends ActionRegistry> = Omit<A, keyof B> & B;
7
5
  export type ExecutionFrame = {
8
6
  stepId: string;
@@ -12,9 +10,10 @@ export type ExecutionFrame = {
12
10
  input?: any;
13
11
  output?: any;
14
12
  error?: any;
13
+ skipped?: boolean;
15
14
  };
16
- export type ActionParams<Reg extends ActionRegistry, K extends keyof Reg> = Parameters<Reg[K]>;
17
- export type ActionReturn<Reg extends ActionRegistry, K extends keyof Reg> = Awaited<ReturnType<Reg[K]>>;
15
+ export type ActionParams<Reg, K extends keyof Reg> = Reg[K] extends (...args: infer P) => any ? P : never;
16
+ export type ActionReturn<Reg, K extends keyof Reg> = Reg[K] extends (...args: any[]) => infer R ? Awaited<R> : never;
18
17
  export type WorkflowMiddleware<Reg extends ActionRegistry = any> = {
19
18
  (ctx: {
20
19
  stepId: string;
@@ -49,7 +49,7 @@ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown
49
49
  input: Input;
50
50
  results: Results;
51
51
  context: Context;
52
- }) => ActionParams<Reg, ActionName>): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: Awaited<ReturnType<Reg[ActionName]>>; }, undefined>;
52
+ }) => ActionParams<Reg, ActionName>): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: ActionReturn<Reg, ActionName>; }, undefined>;
53
53
  parallel<Branches extends WorkflowBuilder<Reg, Input, Context, any, any>[]>(...branches: {
54
54
  [K in keyof Branches]: (builder: WorkflowBuilder<Reg, Input, Context, [], Results>) => Branches[K];
55
55
  }): WorkflowBuilder<Reg, Input, Context, [
@@ -59,10 +59,12 @@ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown
59
59
  join<ID extends string, ActionName extends keyof Reg & string>(id: ID, action: ActionName, resolve?: ActionParams<Reg, ActionName> extends [] ? (ctx?: {
60
60
  input: Input;
61
61
  results: Results;
62
+ context: Context;
62
63
  }) => undefined : (ctx: {
63
64
  input: Input;
64
65
  results: Results;
65
- }) => ActionParams<Reg, ActionName>): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: Awaited<ReturnType<Reg[ActionName]>>; }, undefined>;
66
+ context: Context;
67
+ }) => ActionParams<Reg, ActionName>): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: ActionReturn<Reg, ActionName>; }, undefined>;
66
68
  subflow<Prefix extends string, SubInput, SubResults, SubSteps extends StepDef<Reg, any, any>[], SubOutput>(prefix: Prefix, workflow: WorkflowDef<Reg, SubInput, SubResults, SubSteps, SubOutput>, resolveInput?: (ctx: {
67
69
  input: Input;
68
70
  results: Results;
@@ -73,6 +75,11 @@ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown
73
75
  ], Results & {
74
76
  [K in Prefix]: SubflowResult<SubResults, SubOutput>;
75
77
  }>;
78
+ when(predicate: (ctx: {
79
+ input: Input;
80
+ results: Results;
81
+ context: Context;
82
+ }) => boolean): this;
76
83
  output<Output>(fn: (ctx: {
77
84
  input: Input;
78
85
  results: Results;
@@ -625,6 +625,14 @@ export class WorkflowBuilder {
625
625
  this.frontier = workflow.endSteps.map((e) => idMap.get(e.id));
626
626
  return this;
627
627
  }
628
+ when(predicate) {
629
+ const lastStep = this.steps[this.steps.length - 1];
630
+ if (!lastStep) {
631
+ throw new Error("when() must follow a step");
632
+ }
633
+ lastStep.when = predicate;
634
+ return this;
635
+ }
628
636
  output(fn) {
629
637
  this.outputResolver = fn;
630
638
  return this.build();
@@ -243,8 +243,17 @@ export async function executeWorkflow(workflow, registry, input, middleware = []
243
243
  };
244
244
  const core = async () => {
245
245
  frame.attempts++;
246
+ const stepCtx = { input, results, context };
247
+ if (step.when && !step.when(stepCtx)) {
248
+ frame.output = undefined;
249
+ frame.end = Date.now();
250
+ frame.skipped = true;
251
+ results[step.id] = undefined;
252
+ return undefined;
253
+ }
246
254
  // 👇 Get the resolved arguments (should be a tuple or undefined)
247
- const resolvedArgs = step.resolve?.({ input, results, context });
255
+ // const resolvedArgs = step.resolve?.({ input, results, context });
256
+ const resolvedArgs = step.resolve?.(stepCtx);
248
257
  frame.input = resolvedArgs;
249
258
  try {
250
259
  const action = registry[step.action];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pogodisco/zephyr",
3
- "version": "1.3.2",
3
+ "version": "1.3.4",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },