@pogodisco/zephyr 1.3.4 → 1.3.5
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/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/observer.d.ts +4 -0
- package/dist/observer.js +69 -0
- package/dist/session.d.ts +3 -3
- package/dist/session.js +4 -4
- package/dist/types.d.ts +1 -1
- package/dist/types.js +0 -46
- package/dist/workflow-composer.d.ts +37 -23
- package/dist/workflow-composer.js +44 -566
- package/dist/workflow-executor.d.ts +3 -3
- package/dist/workflow-executor.js +463 -21
- package/dist/workflow-module.d.ts +16 -7
- package/dist/workflow-module.js +12 -38
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ActionRegistry, WorkflowObserver } from "./types.js";
|
|
2
|
+
export declare function composeObserver<Reg extends ActionRegistry>(middleware: WorkflowObserver<Reg>[], ctx: Parameters<WorkflowObserver<Reg>>[0], core: () => Promise<any>): () => Promise<any>;
|
|
3
|
+
export declare function useLog(): WorkflowObserver;
|
|
4
|
+
export declare function useMetrics(): WorkflowObserver;
|
package/dist/observer.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { eventStream } from "./event-stream.js";
|
|
2
|
+
export function composeObserver(middleware, ctx, core) {
|
|
3
|
+
let index = -1;
|
|
4
|
+
async function dispatch(i) {
|
|
5
|
+
if (i <= index)
|
|
6
|
+
throw new Error("next() called multiple times");
|
|
7
|
+
index = i;
|
|
8
|
+
const fn = middleware[i];
|
|
9
|
+
if (!fn)
|
|
10
|
+
return core();
|
|
11
|
+
return fn(ctx, () => dispatch(i + 1));
|
|
12
|
+
}
|
|
13
|
+
return () => dispatch(0);
|
|
14
|
+
}
|
|
15
|
+
export function useLog() {
|
|
16
|
+
return async ({ frame, stepId }, next) => {
|
|
17
|
+
eventStream.emit({
|
|
18
|
+
type: "node_start",
|
|
19
|
+
node: stepId,
|
|
20
|
+
timestamp: frame.start,
|
|
21
|
+
input: frame.input,
|
|
22
|
+
});
|
|
23
|
+
try {
|
|
24
|
+
const res = await next();
|
|
25
|
+
eventStream.emit({
|
|
26
|
+
type: "node_success",
|
|
27
|
+
node: stepId,
|
|
28
|
+
output: frame.output,
|
|
29
|
+
duration: frame.end - frame.start,
|
|
30
|
+
attempts: frame.attempts,
|
|
31
|
+
timestamp: Date.now(),
|
|
32
|
+
});
|
|
33
|
+
return res;
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
eventStream.emit({
|
|
37
|
+
type: "node_fail",
|
|
38
|
+
node: stepId,
|
|
39
|
+
error: frame.error,
|
|
40
|
+
timestamp: Date.now(),
|
|
41
|
+
attempts: frame.attempts,
|
|
42
|
+
});
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export function useMetrics() {
|
|
48
|
+
return async ({ stepId, extras }, next) => {
|
|
49
|
+
if (!extras?.metrics) {
|
|
50
|
+
extras.metrics = {};
|
|
51
|
+
}
|
|
52
|
+
const start = Date.now();
|
|
53
|
+
try {
|
|
54
|
+
const res = await next();
|
|
55
|
+
extras.metrics[stepId] = {
|
|
56
|
+
status: "success",
|
|
57
|
+
duration: Date.now() - start,
|
|
58
|
+
};
|
|
59
|
+
return res;
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
extras.metrics[stepId] = {
|
|
63
|
+
status: "fail",
|
|
64
|
+
duration: Date.now() - start,
|
|
65
|
+
};
|
|
66
|
+
throw err;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
package/dist/session.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ActionRegistry,
|
|
1
|
+
import { ActionRegistry, WorkflowObserver } from "./types.js";
|
|
2
2
|
import { WorkflowDef } from "./workflow-composer.js";
|
|
3
3
|
type ModuleFlows<Reg extends ActionRegistry> = Record<string, WorkflowDef<Reg, any, any>>;
|
|
4
4
|
export declare class WorkflowSession<Reg extends ActionRegistry, State extends Record<string, any>> {
|
|
@@ -7,14 +7,14 @@ export declare class WorkflowSession<Reg extends ActionRegistry, State extends R
|
|
|
7
7
|
private running;
|
|
8
8
|
private queue;
|
|
9
9
|
state: State;
|
|
10
|
-
|
|
10
|
+
observers: WorkflowObserver[];
|
|
11
11
|
private moduleFlows;
|
|
12
12
|
/**
|
|
13
13
|
* @param baseModule Module to extend (inherits workflows)
|
|
14
14
|
* @param registry Action registry
|
|
15
15
|
* @param initialContext Initial context (session state) — will be mutated in workflows
|
|
16
16
|
*/
|
|
17
|
-
constructor(baseModule: ModuleFlows<Reg>, registry: Reg, initialContext: State,
|
|
17
|
+
constructor(baseModule: ModuleFlows<Reg>, registry: Reg, initialContext: State, observers: WorkflowObserver[]);
|
|
18
18
|
getState(): State;
|
|
19
19
|
subscribe(fn: (state: Partial<State>) => void): () => boolean;
|
|
20
20
|
private notify;
|
package/dist/session.js
CHANGED
|
@@ -6,15 +6,15 @@ export class WorkflowSession {
|
|
|
6
6
|
* @param registry Action registry
|
|
7
7
|
* @param initialContext Initial context (session state) — will be mutated in workflows
|
|
8
8
|
*/
|
|
9
|
-
constructor(baseModule, registry, initialContext,
|
|
9
|
+
constructor(baseModule, registry, initialContext, observers) {
|
|
10
10
|
this.registry = registry;
|
|
11
11
|
this.subscribers = new Set();
|
|
12
12
|
this.running = false;
|
|
13
13
|
this.queue = [];
|
|
14
|
-
this.
|
|
14
|
+
this.observers = [];
|
|
15
15
|
// Use the same object for session state and module context
|
|
16
16
|
this.state = initialContext;
|
|
17
|
-
this.
|
|
17
|
+
this.observers = observers;
|
|
18
18
|
// Per-session module: inherits workflows, shares the same state/context object
|
|
19
19
|
this.moduleFlows = createModule({
|
|
20
20
|
registry: this.registry,
|
|
@@ -49,7 +49,7 @@ export class WorkflowSession {
|
|
|
49
49
|
if (!workflow)
|
|
50
50
|
throw new Error(`Workflow ${String(key)} not found`);
|
|
51
51
|
// state/context already lives on the module; no need to pass a separate context
|
|
52
|
-
await executeWorkflow(workflow, this.registry, input, this.
|
|
52
|
+
await executeWorkflow(workflow, this.registry, input, this.observers);
|
|
53
53
|
// Notify subscribers after workflow mutates state
|
|
54
54
|
this.notify();
|
|
55
55
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export type ExecutionFrame = {
|
|
|
14
14
|
};
|
|
15
15
|
export type ActionParams<Reg, K extends keyof Reg> = Reg[K] extends (...args: infer P) => any ? P : never;
|
|
16
16
|
export type ActionReturn<Reg, K extends keyof Reg> = Reg[K] extends (...args: any[]) => infer R ? Awaited<R> : never;
|
|
17
|
-
export type
|
|
17
|
+
export type WorkflowObserver<Reg extends ActionRegistry = any> = {
|
|
18
18
|
(ctx: {
|
|
19
19
|
stepId: string;
|
|
20
20
|
input: any;
|
package/dist/types.js
CHANGED
|
@@ -1,47 +1 @@
|
|
|
1
|
-
// import { createWorkflow } from "./workflow-composer.js";
|
|
2
|
-
//
|
|
3
|
-
// export type Action<I = any, O = any> = (args: I) => Promise<O>;
|
|
4
|
-
//
|
|
5
|
-
// export interface ActionRegistry {
|
|
6
|
-
// [key: string]: Action;
|
|
7
|
-
// }
|
|
8
|
-
//
|
|
9
|
-
// export type MergeActionRegistries<
|
|
10
|
-
// A extends ActionRegistry,
|
|
11
|
-
// B extends ActionRegistry,
|
|
12
|
-
// > = Omit<A, keyof B> & B;
|
|
13
|
-
//
|
|
14
|
-
// export type ExecutionFrame = {
|
|
15
|
-
// stepId: string;
|
|
16
|
-
// attempts: number;
|
|
17
|
-
// start: number;
|
|
18
|
-
// end?: number;
|
|
19
|
-
// input?: any;
|
|
20
|
-
// output?: any;
|
|
21
|
-
// error?: any;
|
|
22
|
-
// };
|
|
23
|
-
//
|
|
24
|
-
// export type WorkflowMiddleware<Reg extends ActionRegistry = any> = {
|
|
25
|
-
// (
|
|
26
|
-
// ctx: {
|
|
27
|
-
// stepId: string;
|
|
28
|
-
// input: any;
|
|
29
|
-
// results: Record<string, any>;
|
|
30
|
-
// registry: Reg;
|
|
31
|
-
// extras: Record<string, any>;
|
|
32
|
-
// frame: ExecutionFrame;
|
|
33
|
-
// },
|
|
34
|
-
// next: () => Promise<any>,
|
|
35
|
-
// ): Promise<any>;
|
|
36
|
-
// };
|
|
37
|
-
//
|
|
38
|
-
// export type WF<
|
|
39
|
-
// Reg extends ActionRegistry,
|
|
40
|
-
// Context extends Record<string, any>,
|
|
41
|
-
// > = ReturnType<typeof createWorkflow<Reg, Context>>;
|
|
42
|
-
// // TODO: this needs enforcing
|
|
43
|
-
// type Observer = (
|
|
44
|
-
// frame: Readonly<ExecutionFrame>,
|
|
45
|
-
// extras: Record<string, any>,
|
|
46
|
-
// ) => void | Promise<void>;
|
|
47
1
|
export {};
|
|
@@ -1,12 +1,26 @@
|
|
|
1
|
-
import { ActionRegistry,
|
|
1
|
+
import { ActionRegistry, ActionReturn } from "./types.js";
|
|
2
|
+
export type NormalizedCall = {
|
|
3
|
+
kind: "none";
|
|
4
|
+
} | {
|
|
5
|
+
kind: "positional";
|
|
6
|
+
args: any[];
|
|
7
|
+
} | {
|
|
8
|
+
kind: "object";
|
|
9
|
+
args: any;
|
|
10
|
+
};
|
|
11
|
+
export type ResolvedStepInput = NormalizedCall | {
|
|
12
|
+
kind: "loop";
|
|
13
|
+
items: NormalizedCall[];
|
|
14
|
+
};
|
|
2
15
|
type SubflowResult<SubResults, SubOutput> = SubOutput extends undefined ? SubResults : SubOutput;
|
|
3
|
-
type StepResult<Reg extends ActionRegistry, ActionName extends keyof Reg> = ActionReturn<Reg, ActionName>;
|
|
16
|
+
export type StepResult<Reg extends ActionRegistry, ActionName extends keyof Reg, Loop extends boolean | undefined = undefined> = Loop extends true ? ActionReturn<Reg, ActionName>[] : ActionReturn<Reg, ActionName>;
|
|
4
17
|
export type StepDef<Reg extends ActionRegistry, ID extends string = string, ActionName extends keyof Reg = any> = {
|
|
5
18
|
id: ID;
|
|
6
19
|
action: ActionName;
|
|
7
20
|
dependsOn: string[];
|
|
8
|
-
resolve: (ctx: any) =>
|
|
21
|
+
resolve: (ctx: any) => ResolvedStepInput;
|
|
9
22
|
when?: (ctx: any) => boolean;
|
|
23
|
+
loop?: boolean;
|
|
10
24
|
};
|
|
11
25
|
export type WorkflowDef<Reg extends ActionRegistry, Input, Results, Steps extends StepDef<Reg, any, any>[] = StepDef<Reg, any, any>[], Output = undefined> = {
|
|
12
26
|
name: string;
|
|
@@ -27,49 +41,45 @@ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown
|
|
|
27
41
|
private frontier;
|
|
28
42
|
private outputResolver?;
|
|
29
43
|
constructor(name: string, registry: Reg, context: Context);
|
|
30
|
-
step<ID extends string, ActionName extends keyof Reg & string>(id: ID, action: ActionName, resolve?:
|
|
44
|
+
step<ID extends string, ActionName extends keyof Reg & string, Loop extends boolean | undefined = undefined>(id: ID, action: ActionName, resolve?: (ctx: {
|
|
31
45
|
input: Input;
|
|
32
46
|
results: Results;
|
|
33
47
|
context: Context;
|
|
34
|
-
}) =>
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
context: Context;
|
|
38
|
-
}) => ActionParams<Reg, ActionName>, dependsOn?: string[]): WorkflowBuilder<Reg, Input, Context, [
|
|
48
|
+
}) => ResolvedStepInput, dependsOn?: string[], options?: {
|
|
49
|
+
loop?: Loop;
|
|
50
|
+
}): WorkflowBuilder<Reg, Input, Context, [
|
|
39
51
|
...Steps,
|
|
40
52
|
StepDef<Reg, ID, ActionName>
|
|
41
53
|
], Results & {
|
|
42
|
-
[K in ID]: StepResult<Reg, ActionName>;
|
|
54
|
+
[K in ID]: StepResult<Reg, ActionName, Loop>;
|
|
43
55
|
}>;
|
|
44
|
-
seq<ID extends string, ActionName extends keyof Reg & string>(id: ID, action: ActionName, resolve?:
|
|
56
|
+
seq<ID extends string, ActionName extends keyof Reg & string>(id: ID, action: ActionName, resolve?: (ctx: {
|
|
45
57
|
input: Input;
|
|
46
58
|
results: Results;
|
|
47
59
|
context: Context;
|
|
48
|
-
}) =>
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
context: Context;
|
|
52
|
-
}) => ActionParams<Reg, ActionName>): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: ActionReturn<Reg, ActionName>; }, undefined>;
|
|
60
|
+
}) => ResolvedStepInput, options?: {
|
|
61
|
+
loop?: boolean;
|
|
62
|
+
}): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: ActionReturn<Reg, ActionName> | ActionReturn<Reg, ActionName>[]; }, undefined>;
|
|
53
63
|
parallel<Branches extends WorkflowBuilder<Reg, Input, Context, any, any>[]>(...branches: {
|
|
54
64
|
[K in keyof Branches]: (builder: WorkflowBuilder<Reg, Input, Context, [], Results>) => Branches[K];
|
|
55
65
|
}): WorkflowBuilder<Reg, Input, Context, [
|
|
56
66
|
...Steps,
|
|
57
67
|
...(Branches[number] extends WorkflowBuilder<Reg, any, any, infer S, any> ? S : never)
|
|
58
68
|
], Results & (Branches[number] extends WorkflowBuilder<Reg, any, any, any, infer R> ? UnionToIntersection<R> : {})>;
|
|
59
|
-
join<ID extends string, ActionName extends keyof Reg & string>(id: ID, action: ActionName, resolve?:
|
|
60
|
-
input: Input;
|
|
61
|
-
results: Results;
|
|
62
|
-
context: Context;
|
|
63
|
-
}) => undefined : (ctx: {
|
|
69
|
+
join<ID extends string, ActionName extends keyof Reg & string>(id: ID, action: ActionName, resolve?: (ctx: {
|
|
64
70
|
input: Input;
|
|
65
71
|
results: Results;
|
|
66
72
|
context: Context;
|
|
67
|
-
}) =>
|
|
73
|
+
}) => ResolvedStepInput, options?: {
|
|
74
|
+
loop?: boolean;
|
|
75
|
+
}): WorkflowBuilder<Reg, Input, Context, [...Steps, StepDef<Reg, ID, ActionName>], Results & { [K in ID]: ActionReturn<Reg, ActionName> | ActionReturn<Reg, ActionName>[]; }, undefined>;
|
|
68
76
|
subflow<Prefix extends string, SubInput, SubResults, SubSteps extends StepDef<Reg, any, any>[], SubOutput>(prefix: Prefix, workflow: WorkflowDef<Reg, SubInput, SubResults, SubSteps, SubOutput>, resolveInput?: (ctx: {
|
|
69
77
|
input: Input;
|
|
70
78
|
results: Results;
|
|
71
79
|
context: Context;
|
|
72
|
-
}) =>
|
|
80
|
+
}) => SubInput, options?: {
|
|
81
|
+
loop?: boolean;
|
|
82
|
+
}): WorkflowBuilder<Reg, Input, Context, [
|
|
73
83
|
...Steps,
|
|
74
84
|
...SubSteps
|
|
75
85
|
], Results & {
|
|
@@ -88,6 +98,10 @@ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown
|
|
|
88
98
|
build(): WorkflowDef<Reg, Input, Results, Steps>;
|
|
89
99
|
private validateDependencies;
|
|
90
100
|
private getEndSteps;
|
|
101
|
+
static args: (...args: any[]) => NormalizedCall;
|
|
102
|
+
static obj: (args: any) => NormalizedCall;
|
|
103
|
+
static none: () => NormalizedCall;
|
|
104
|
+
static loop: (items: NormalizedCall[]) => ResolvedStepInput;
|
|
91
105
|
}
|
|
92
106
|
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>;
|
|
93
107
|
export {};
|