@pogodisco/zephyr 1.3.11 → 1.3.13

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/session.d.ts CHANGED
@@ -1,25 +1,20 @@
1
1
  import { ActionRegistry, WorkflowObserver } from "./types.js";
2
- import { WorkflowDef } from "./workflow-composer.js";
3
- type ModuleFlows<Reg extends ActionRegistry> = Record<string, WorkflowDef<Reg, any, any>>;
4
- export declare class WorkflowSession<Reg extends ActionRegistry, State extends Record<string, any>> {
5
- private registry;
2
+ export declare class WorkflowSession<Reg extends ActionRegistry, Ctx> {
3
+ runtime: {
4
+ run: any;
5
+ getContext: () => Ctx;
6
+ };
6
7
  private subscribers;
7
8
  private running;
8
9
  private queue;
9
- state: State;
10
10
  observers: WorkflowObserver[];
11
- private moduleFlows;
12
- /**
13
- * @param baseModule Module to extend (inherits workflows)
14
- * @param registry Action registry
15
- * @param initialContext Initial context (session state) will be mutated in workflows
16
- */
17
- constructor(baseModule: ModuleFlows<Reg>, registry: Reg, initialContext: State, observers: WorkflowObserver[]);
18
- getState(): State;
19
- subscribe(fn: (state: Partial<State>) => void): () => boolean;
11
+ constructor(runtime: {
12
+ run: any;
13
+ getContext: () => Ctx;
14
+ });
15
+ subscribe(fn: (state: Partial<Ctx>) => void): () => boolean;
20
16
  private notify;
21
17
  /** Queue a workflow execution by key and input; state/context is automatically updated */
22
- dispatch(key: keyof ModuleFlows<Reg>, input?: any): void;
18
+ dispatch(workflow: any, input: any): void;
23
19
  private processQueue;
24
20
  }
25
- export {};
package/dist/session.js CHANGED
@@ -1,56 +1,32 @@
1
- import { executeWorkflow } from "./workflow-executor.js";
2
- import { createModule } from "./workflow-module.js";
3
1
  export class WorkflowSession {
4
- /**
5
- * @param baseModule Module to extend (inherits workflows)
6
- * @param registry Action registry
7
- * @param initialContext Initial context (session state) — will be mutated in workflows
8
- */
9
- constructor(baseModule, registry, initialContext, observers) {
10
- this.registry = registry;
2
+ constructor(runtime) {
3
+ this.runtime = runtime;
11
4
  this.subscribers = new Set();
12
5
  this.running = false;
13
6
  this.queue = [];
7
+ // public state: State; // session state & workflow context are the same object
14
8
  this.observers = [];
15
- // Use the same object for session state and module context
16
- this.state = initialContext;
17
- this.observers = observers;
18
- // Per-session module: inherits workflows, shares the same state/context object
19
- this.moduleFlows = createModule({
20
- registry: this.registry,
21
- context: this.state,
22
- use: [baseModule],
23
- define: () => ({}), // no new flows needed
24
- });
25
- }
26
- getState() {
27
- return this.state;
28
9
  }
29
10
  subscribe(fn) {
30
11
  this.subscribers.add(fn);
31
- fn(this.state); // immediately notify
12
+ fn(this.runtime.getContext()); // immediately notify
32
13
  return () => this.subscribers.delete(fn);
33
14
  }
34
15
  notify() {
35
16
  for (const fn of this.subscribers)
36
- fn(this.state);
17
+ fn(this.runtime.getContext());
37
18
  }
38
19
  /** Queue a workflow execution by key and input; state/context is automatically updated */
39
- dispatch(key, input = {}) {
40
- this.queue.push({ key, input });
20
+ dispatch(workflow, input) {
21
+ this.queue.push({ workflow, input });
41
22
  if (!this.running)
42
23
  this.processQueue();
43
24
  }
44
25
  async processQueue() {
45
26
  this.running = true;
46
27
  while (this.queue.length) {
47
- const { key, input } = this.queue.shift();
48
- const workflow = this.moduleFlows[key];
49
- if (!workflow)
50
- throw new Error(`Workflow ${String(key)} not found`);
51
- // state/context already lives on the module; no need to pass a separate context
52
- await executeWorkflow(workflow, this.registry, input, this.observers);
53
- // Notify subscribers after workflow mutates state
28
+ const { workflow, input } = this.queue.shift();
29
+ await this.runtime.run(workflow, input);
54
30
  this.notify();
55
31
  }
56
32
  this.running = false;
package/dist/types.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { createWorkflow } from "./workflow-composer.js";
2
1
  export type Action<F extends (...args: any[]) => any = (...args: any[]) => any> = F;
3
2
  export type ActionRegistry = Record<string, (...args: any[]) => any>;
4
3
  export type MergeActionRegistries<A extends ActionRegistry, B extends ActionRegistry> = Simplify<Omit<A, keyof B> & B>;
@@ -27,4 +26,3 @@ export type WorkflowObserver<Reg extends ActionRegistry = any> = {
27
26
  frame: ExecutionFrame;
28
27
  }, next: () => Promise<any>): Promise<any>;
29
28
  };
30
- export type WF<Reg extends ActionRegistry, Context extends Record<string, any>> = ReturnType<typeof createWorkflow<Reg, Context>>;
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import { ActionRegistry } from "./types.js";
2
- import { WorkflowDef } from "./workflow-composer.js";
3
1
  type AnyFn = (...args: any[]) => any;
4
2
  /**
5
3
  * For generic actions - preserves the generic parameter
@@ -9,20 +7,4 @@ export declare function genericAction<F extends AnyFn>(fn: F): <T>() => ((...arg
9
7
  * For fixed actions
10
8
  */
11
9
  export declare function fixedAction<F extends AnyFn>(fn: F): () => ((...args: Parameters<F>) => ReturnType<F>);
12
- export declare function createRunner<Reg extends ActionRegistry>(registry: Reg): <W extends WorkflowDef<Reg, any, any, any, any>>(workflow: W, input: W extends WorkflowDef<Reg, infer I, any, any, any> ? I : never, observers?: any[]) => Promise<{
13
- output: W extends WorkflowDef<Reg, any, any, any, infer O> ? O : never;
14
- results: W extends WorkflowDef<Reg, any, infer R, any, any> ? R : never;
15
- extras: Record<string, any>;
16
- }>;
17
- /**
18
- * Create a typed runner for a specific module
19
- * @example
20
- * const run = createModuleRunner(modWithLoop, regOne);
21
- * const result = await run("loopWorkflow", { items: ["APPLE"] });
22
- */
23
- export declare function createModuleRunner<M extends Record<string, WorkflowDef<any, any, any, any, any>>, Reg extends ActionRegistry>(module: M, registry: Reg): <K extends keyof M>(key: K, input: M[K] extends WorkflowDef<Reg, infer I, any, any, any> ? I : never, observers?: any[]) => Promise<{
24
- output: M[K] extends WorkflowDef<Reg, any, any, any, infer O> ? O : never;
25
- results: M[K] extends WorkflowDef<Reg, any, infer R, any, any> ? R : never;
26
- extras: Record<string, any>;
27
- }>;
28
10
  export {};
package/dist/utils.js CHANGED
@@ -1,4 +1,3 @@
1
- import { executeWorkflow } from "./workflow-executor.js";
2
1
  /**
3
2
  * For generic actions - preserves the generic parameter
4
3
  */
@@ -16,20 +15,21 @@ export function fixedAction(fn) {
16
15
  return fn;
17
16
  };
18
17
  }
19
- export function createRunner(registry) {
20
- return async (workflow, input, observers) => {
21
- return executeWorkflow(workflow, registry, input, observers ?? []);
22
- };
23
- }
24
- /**
25
- * Create a typed runner for a specific module
26
- * @example
27
- * const run = createModuleRunner(modWithLoop, regOne);
28
- * const result = await run("loopWorkflow", { items: ["APPLE"] });
29
- */
30
- export function createModuleRunner(module, registry) {
31
- return async (key, input, observers) => {
32
- const workflow = module[key];
33
- return executeWorkflow(workflow, registry, input, observers ?? []);
34
- };
35
- }
18
+ // export function createRuntime<Reg extends ActionRegistry, Context>(
19
+ // registry: Reg,
20
+ // context: Context,
21
+ // ) {
22
+ // return {
23
+ // run: async <W extends WorkflowDef<Reg, any, any, any, any>>(
24
+ // workflow: W,
25
+ // input: W extends WorkflowDef<Reg, infer I, any, any, any> ? I : never,
26
+ // observers?: WorkflowObserver[],
27
+ // ): Promise<{
28
+ // output: W extends WorkflowDef<Reg, any, any, any, infer O> ? O : never;
29
+ // results: W extends WorkflowDef<Reg, any, infer R, any, any> ? R : never;
30
+ // extras: Record<string, any>;
31
+ // }> => {
32
+ // return executeWorkflow(workflow, registry, input, context, observers);
33
+ // },
34
+ // };
35
+ // }
@@ -1,4 +1,4 @@
1
- import { ActionParams, ActionRegistry, ActionReturn } from "./types.js";
1
+ import { ActionParams, ActionRegistry, ActionReturn, Simplify } from "./types.js";
2
2
  type NormalizedCall = {
3
3
  kind: "none";
4
4
  } | {
@@ -58,7 +58,6 @@ export type WorkflowDef<Reg extends ActionRegistry, Input, Results, Steps extend
58
58
  input: Input;
59
59
  results: Results;
60
60
  outputResolver?: (ctx: any) => Output;
61
- __context?: any;
62
61
  };
63
62
  type StepRuntimeCtx<I, R, C> = {
64
63
  input: I;
@@ -74,17 +73,19 @@ type StepOptions<Input, Results, Context> = {
74
73
  label?: string;
75
74
  meta?: Record<string, any>;
76
75
  };
77
- type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
78
- export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown, Context extends Record<string, any> = {}, Steps extends StepDef<Reg, any, any>[] = [], Results = {}, Output = undefined> {
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
+ type MergeBranchSteps<Branches extends readonly any[], Acc extends any[]> = Branches extends readonly [infer Head, ...infer Tail] ? MergeBranchSteps<Tail, [
78
+ ...Acc,
79
+ ...(Head extends WorkflowBuilder<any, any, any, infer S, any> ? S : [])
80
+ ]> : Acc;
81
+ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown, Context = unknown, Steps extends StepDef<Reg, any, any>[] = [], Results = {}, Output = undefined> {
79
82
  private name;
80
- private registry;
81
- private context;
82
83
  private steps;
83
84
  private frontier;
84
85
  private pendingWhen?;
85
86
  private outputResolver?;
86
87
  private clearPendingWhen;
87
- constructor(name: string, registry: Reg, context: Context);
88
+ constructor(name: string);
88
89
  step<ID extends string, ActionName extends keyof Reg & string, ResolveOut extends ResolvedStepInput = ResolvedStepInput>(id: ID, action: ActionName, resolve?: (ctx: {
89
90
  input: Input;
90
91
  results: Results;
@@ -92,30 +93,25 @@ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown
92
93
  } & CallHelpers<Reg, ActionName>) => ResolveOut, dependsOn?: string[], options?: StepOptions<Input, Results, Context>): WorkflowBuilder<Reg, Input, Context, [
93
94
  ...Steps,
94
95
  StepDef<Reg, ID, ActionName>
95
- ], Results & {
96
+ ], Simplify<Results & {
96
97
  [K in ID]: StepResultFromResolve<Reg, ActionName, ResolveOut>;
97
- }>;
98
+ }>>;
98
99
  seq<ID extends string = string, ActionName extends keyof Reg & string = any, ResolveOut extends ResolvedStepInput = ResolvedStepInput>(id: ID, action: ActionName, resolve?: (ctx: {
99
100
  input: Input;
100
101
  results: Results;
101
102
  context: Context;
102
- } & CallHelpers<Reg, ActionName>) => ResolveOut, options?: StepOptions<Input, Results, Context>): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: StepResultFromResolve<Reg, ActionName, ResolveOut>; }, undefined>;
103
- as<NewType>(): WorkflowBuilder<Reg, Input, Context, Steps, Steps extends [...infer Rest, infer Last] ? Last extends StepDef<Reg, infer ID, any> ? Omit<Results, ID> & {
103
+ } & 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>;
104
+ as<NewType>(): WorkflowBuilder<Reg, Input, Context, Steps, Steps extends [...infer Rest, infer Last] ? Last extends StepDef<Reg, infer ID, any> ? Simplify<Omit<Results, ID> & {
104
105
  [K in ID]: NewType;
105
- } : Results : Results, Output>;
106
- parallel<Branches extends WorkflowBuilder<Reg, Input, Context, any, any>[]>(...branches: {
106
+ }> : Results : Results, Output>;
107
+ parallel<Branches extends readonly WorkflowBuilder<Reg, Input, Context, any, any>[]>(...branches: {
107
108
  [K in keyof Branches]: (builder: WorkflowBuilder<Reg, Input, Context, [], Results>) => Branches[K];
108
- }): WorkflowBuilder<Reg, Input, Context, [
109
- ...Steps,
110
- ...(Branches[number] extends WorkflowBuilder<Reg, any, any, infer S, any> ? S : never)
111
- ], Results & UnionToIntersection<{
112
- [K in keyof Branches]: Branches[K] extends WorkflowBuilder<Reg, any, any, any, infer R> ? R : never;
113
- }[number]>>;
109
+ }): WorkflowBuilder<Reg, Input, Context, MergeBranchSteps<Branches, Steps>, Simplify<MergeBranchResults<Branches, Results>>>;
114
110
  join<ID extends string = string, ActionName extends keyof Reg & string = any, ResolveOut extends ResolvedStepInput = ResolvedStepInput>(id: ID, action: ActionName, resolve?: (ctx: {
115
111
  input: Input;
116
112
  results: Results;
117
113
  context: Context;
118
- } & CallHelpers<Reg, ActionName>) => ResolveOut, options?: StepOptions<Input, Results, Context>): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: StepResultFromResolve<Reg, ActionName, ResolveOut>; }, undefined>;
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>;
119
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: {
120
116
  input: Input;
121
117
  results: Results;
@@ -145,5 +141,5 @@ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown
145
141
  private validateDependencies;
146
142
  private getEndSteps;
147
143
  }
148
- export declare function createWorkflow<Reg extends ActionRegistry, Context extends Record<string, any> = {}>(registry: Reg, context?: Context): <Input = unknown>(name: string) => WorkflowBuilder<Reg, Input, Context, [], {}, undefined>;
144
+ export declare function createWorkflow<Reg extends ActionRegistry, Context extends Record<string, any> = {}>(): <Input = unknown>(name: string) => WorkflowBuilder<Reg, Input, Context, [], {}, undefined>;
149
145
  export {};
@@ -5,10 +5,8 @@ export class WorkflowBuilder {
5
5
  clearPendingWhen() {
6
6
  this.pendingWhen = undefined;
7
7
  }
8
- constructor(name, registry, context) {
8
+ constructor(name) {
9
9
  this.name = name;
10
- this.registry = registry;
11
- this.context = context;
12
10
  this.steps = [];
13
11
  this.frontier = [];
14
12
  }
@@ -37,55 +35,11 @@ export class WorkflowBuilder {
37
35
  as() {
38
36
  return this;
39
37
  }
40
- /* ------------------------------------------------ */
41
- /* Parallel branches */
42
- /* ------------------------------------------------ */
43
- // parallel<Branches extends WorkflowBuilder<Reg, Input, Context, any, any>[]>(
44
- // ...branches: {
45
- // [K in keyof Branches]: (
46
- // builder: WorkflowBuilder<Reg, Input, Context, [], Results>,
47
- // ) => Branches[K];
48
- // }
49
- // ): WorkflowBuilder<
50
- // Reg,
51
- // Input,
52
- // Context,
53
- // [
54
- // ...Steps,
55
- // ...(Branches[number] extends WorkflowBuilder<Reg, any, any, infer S, any>
56
- // ? S
57
- // : never),
58
- // ],
59
- // Results &
60
- // (Branches[number] extends WorkflowBuilder<Reg, any, any, any, infer R>
61
- // ? UnionToIntersection<R>
62
- // : {})
63
- // > {
64
- // const parentFrontier = [...this.frontier];
65
- // const branchEnds: string[] = [];
66
- //
67
- // branches.forEach((branch) => {
68
- // const b = new WorkflowBuilder<Reg, Input, Context, [], Results>(
69
- // this.name,
70
- // this.registry,
71
- // this.context,
72
- // );
73
- //
74
- // b.frontier = parentFrontier;
75
- // branch(b);
76
- // branchEnds.push(...b.frontier);
77
- // this.steps.push(...(b as any).steps);
78
- // });
79
- //
80
- // this.frontier = branchEnds;
81
- // this.clearPendingWhen();
82
- // return this as any;
83
- // }
84
38
  parallel(...branches) {
85
39
  const parentFrontier = [...this.frontier];
86
40
  const branchEnds = [];
87
41
  branches.forEach((branch) => {
88
- const b = new WorkflowBuilder(this.name, this.registry, this.context);
42
+ const b = new WorkflowBuilder(this.name);
89
43
  b.frontier = parentFrontier;
90
44
  branch(b);
91
45
  branchEnds.push(...b.frontier);
@@ -161,7 +115,6 @@ export class WorkflowBuilder {
161
115
  input: {},
162
116
  results: {},
163
117
  outputResolver: this.outputResolver,
164
- __context: this.context,
165
118
  };
166
119
  }
167
120
  validateDependencies() {
@@ -185,8 +138,16 @@ export class WorkflowBuilder {
185
138
  /* ------------------------------------------------ */
186
139
  /* WORKFLOW CREATOR */
187
140
  /* ------------------------------------------------ */
188
- export function createWorkflow(registry, context) {
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
+ export function createWorkflow() {
189
150
  return function workflow(name) {
190
- return new WorkflowBuilder(name, registry, context || {});
151
+ return new WorkflowBuilder(name);
191
152
  };
192
153
  }
@@ -1,6 +1,6 @@
1
1
  import { ActionRegistry, WorkflowObserver } from "./types.js";
2
2
  import { StepDef, WorkflowDef } from "./workflow-composer.js";
3
- export declare function executeWorkflow<Reg extends ActionRegistry, I, R, O = R, Steps extends StepDef<Reg, any, any>[] = StepDef<Reg, any, any>[]>(workflow: WorkflowDef<Reg, I, R, Steps, O>, registry: Reg, input: I, observers?: WorkflowObserver<Reg>[]): Promise<{
3
+ export declare function executeWorkflow<Reg extends ActionRegistry, I, R, O = R, Steps extends StepDef<Reg, any, any>[] = StepDef<Reg, any, any>[]>(workflow: WorkflowDef<Reg, I, R, Steps, O>, registry: Reg, input: I, context: any, observers?: WorkflowObserver<Reg>[]): Promise<{
4
4
  results: R;
5
5
  output: O;
6
6
  extras: Record<string, any>;
@@ -39,7 +39,7 @@ async function runWithRetry(actionFn, stepOptions) {
39
39
  }
40
40
  throw lastError;
41
41
  }
42
- export async function executeWorkflow(workflow, registry, input, observers = []) {
42
+ export async function executeWorkflow(workflow, registry, input, context, observers = []) {
43
43
  const results = {};
44
44
  const extras = {};
45
45
  extras.frames = {};
@@ -93,7 +93,6 @@ export async function executeWorkflow(workflow, registry, input, observers = [])
93
93
  start: Date.now(),
94
94
  };
95
95
  extras.frames[stepId] = frame;
96
- const context = workflow.__context;
97
96
  const ctx = {
98
97
  stepId,
99
98
  input,
@@ -1,10 +1,34 @@
1
- import { ActionRegistry } from "./types.js";
1
+ import { ActionRegistry, WorkflowObserver } from "./types.js";
2
2
  import { createWorkflow, WorkflowDef } from "./workflow-composer.js";
3
3
  type AnyWorkflow = WorkflowDef<any, any, any, any, any>;
4
+ type WorkflowFromDeps<Deps extends Record<string, any>> = {
5
+ [K in keyof Deps]: Deps[K] extends Module<any, any, infer Own, infer SubDeps> ? Own[keyof Own] | WorkflowFromDeps<SubDeps> : never;
6
+ }[keyof Deps];
4
7
  type ModuleShape = Record<string, AnyWorkflow>;
5
- type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
6
- type UnionToIntersectionOrEmpty<U> = [U] extends [never] ? {} : UnionToIntersection<U>;
7
- export type ModuleContext<Reg extends ActionRegistry, Context extends Record<string, any>, Deps extends ModuleShape> = {
8
+ type ContextFromDeps<Deps> = [keyof Deps] extends [never] ? {} : {
9
+ [K in keyof Deps]: Deps[K] extends Module<any, infer Ctx, any, any> ? Ctx : never;
10
+ }[keyof Deps];
11
+ type FinalContext<Reg extends ActionRegistry, Context extends Record<string, any>, Deps extends ModuleMap<Reg>> = Context & ContextFromDeps<Deps>;
12
+ type ModuleMap<Reg extends ActionRegistry> = Record<string, Module<Reg, any, any, any>>;
13
+ export type WorkflowInput<W> = W extends WorkflowDef<any, infer I, any, any, any> ? I : never;
14
+ export type WorkflowResults<W> = W extends WorkflowDef<any, any, infer R, any, any> ? R : never;
15
+ export type WorkflowOutput<W> = W extends WorkflowDef<any, any, any, any, infer O> ? O : never;
16
+ type Module<Reg extends ActionRegistry, Context extends Record<string, any>, Own extends ModuleShape = {}, Deps extends ModuleMap<Reg> = {}> = {
17
+ own: Own;
18
+ deps: Deps;
19
+ createRuntime: (config: {
20
+ registry: Reg;
21
+ context: FinalContext<Reg, Context, Deps>;
22
+ }) => {
23
+ run: <W extends Own[keyof Own] | WorkflowFromDeps<Deps>>(workflow: W, input: WorkflowInput<W>, obververs?: WorkflowObserver<Reg>[]) => Promise<{
24
+ results: WorkflowResults<W>;
25
+ output: WorkflowOutput<W>;
26
+ extras: Record<string, any>;
27
+ }>;
28
+ getContext: () => FinalContext<Reg, Context, Deps>;
29
+ };
30
+ };
31
+ export type ModuleContext<Reg extends ActionRegistry, Context extends Record<string, any>, Deps extends ModuleMap<Reg>> = {
8
32
  wf: ReturnType<typeof createWorkflow<Reg, Context>>;
9
33
  deps: Deps;
10
34
  context: Context;
@@ -14,10 +38,12 @@ export type ModuleContext<Reg extends ActionRegistry, Context extends Record<str
14
38
  context: Context;
15
39
  }) => T) => T;
16
40
  };
17
- export declare function createModule<Reg extends ActionRegistry, Context extends Record<string, any>, Use extends ModuleShape[] = [], Deps extends ModuleShape = UnionToIntersectionOrEmpty<Use[number]> & ModuleShape, Own extends ModuleShape = {}>({ registry, context, use, define, }: {
18
- registry: Reg;
19
- context: Context;
20
- use?: [...Use];
21
- define: (ctx: ModuleContext<Reg, Context, Deps>) => Own;
22
- }): Deps & Own;
41
+ export declare function createModule<Reg extends ActionRegistry, Context extends Record<string, any>, Use extends ModuleMap<Reg>, Own extends ModuleShape>(config: {
42
+ use?: Use;
43
+ define: (ctx: ModuleContext<Reg, Context, Use>) => Own;
44
+ }): Module<Reg, Context, Own, Use>;
45
+ export declare function createModuleFactory<Reg extends ActionRegistry, Context extends Record<string, any>>(): <Use extends ModuleMap<Reg> = {}, Own extends ModuleShape = {}>(config: {
46
+ use?: Use;
47
+ define: (ctx: ModuleContext<Reg, Context, Use>) => Own;
48
+ }) => Module<Reg, Context, Own, Use>;
23
49
  export {};
@@ -1,16 +1,35 @@
1
- import { createWorkflow, } from "./workflow-composer.js";
2
- export function createModule({ registry, context, use, define, }) {
3
- const wf = createWorkflow(registry, context);
4
- const deps = (use ? Object.assign({}, ...use) : {});
1
+ import { createWorkflow } from "./workflow-composer.js";
2
+ import { executeWorkflow } from "./workflow-executor.js";
3
+ export function createModule(config) {
4
+ const wf = createWorkflow();
5
+ const deps = (config.use ?? {});
5
6
  const moduleCtx = {
6
7
  wf,
7
8
  deps,
8
- context,
9
- tools: (factory) => factory({ wf, deps, context }),
9
+ context: {},
10
+ tools: (factory) => factory({
11
+ wf,
12
+ deps,
13
+ context: {},
14
+ }),
10
15
  };
11
- const own = define(moduleCtx);
16
+ const own = config.define(moduleCtx);
12
17
  return {
13
- ...deps,
14
- ...own,
18
+ own,
19
+ deps,
20
+ createRuntime({ registry, context }) {
21
+ const runtimeCtx = { ...context };
22
+ return {
23
+ run: async (workflow, input, observers = []) => {
24
+ return executeWorkflow(workflow, registry, input, context, observers);
25
+ },
26
+ getContext: () => ({ ...runtimeCtx }),
27
+ };
28
+ },
29
+ };
30
+ }
31
+ export function createModuleFactory() {
32
+ return function (config) {
33
+ return createModule(config);
15
34
  };
16
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pogodisco/zephyr",
3
- "version": "1.3.11",
3
+ "version": "1.3.13",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },