@pogodisco/zephyr 1.3.14 → 1.3.16
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/workflow-module.d.ts +26 -12
- package/dist/workflow-module.js +162 -11
- package/package.json +1 -1
|
@@ -1,34 +1,48 @@
|
|
|
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];
|
|
13
|
+
type ModuleDepsPublic<Use> = {
|
|
14
|
+
[K in keyof Use]: Use[K] extends Module<any, infer Ctx, infer Own, any> ? ModulePublic<Ctx, Own> : never;
|
|
15
|
+
};
|
|
16
|
+
type ModulePublic<Context extends Record<string, any>, Own extends ModuleShape> = {
|
|
17
|
+
own: Own;
|
|
18
|
+
__ctx: Context;
|
|
19
|
+
};
|
|
3
20
|
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];
|
|
7
21
|
type ModuleShape = Record<string, AnyWorkflow>;
|
|
8
22
|
type ContextFromDeps<Deps> = [keyof Deps] extends [never] ? {} : {
|
|
9
23
|
[K in keyof Deps]: Deps[K] extends Module<any, infer Ctx, any, any> ? Ctx : never;
|
|
10
24
|
}[keyof Deps];
|
|
11
|
-
type FinalContext<Reg extends ActionRegistry, Context extends Record<string, any>, Deps extends ModuleMap
|
|
12
|
-
type ModuleMap
|
|
25
|
+
type FinalContext<Reg extends ActionRegistry, Context extends Record<string, any>, Deps extends ModuleMap> = Context & ContextFromDeps<Deps>;
|
|
26
|
+
type ModuleMap = Record<string, Module<any, any, any, any>>;
|
|
13
27
|
export type WorkflowInput<W> = W extends WorkflowDef<any, infer I, any, any, any> ? I : never;
|
|
14
28
|
export type WorkflowResults<W> = W extends WorkflowDef<any, any, infer R, any, any> ? R : never;
|
|
15
29
|
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
|
|
30
|
+
type Module<Reg extends ActionRegistry, Context extends Record<string, any>, Own extends ModuleShape = {}, Deps extends ModuleMap = {}> = {
|
|
17
31
|
own: Own;
|
|
18
|
-
deps: Deps
|
|
32
|
+
deps: ModuleDepsPublic<Deps>;
|
|
19
33
|
createRuntime: (config: {
|
|
20
34
|
registry: Reg;
|
|
21
35
|
context: FinalContext<Reg, Context, Deps>;
|
|
22
36
|
}) => {
|
|
23
|
-
run: <
|
|
24
|
-
results: WorkflowResults<
|
|
25
|
-
output: WorkflowOutput<
|
|
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]>;
|
|
26
40
|
extras: Record<string, any>;
|
|
27
41
|
}>;
|
|
28
42
|
getContext: () => FinalContext<Reg, Context, Deps>;
|
|
29
43
|
};
|
|
30
44
|
};
|
|
31
|
-
export type ModuleContext<Reg extends ActionRegistry, Context extends Record<string, any>, Deps extends ModuleMap
|
|
45
|
+
export type ModuleContext<Reg extends ActionRegistry, Context extends Record<string, any>, Deps extends ModuleMap> = {
|
|
32
46
|
wf: ReturnType<typeof createWorkflow<Reg, Context>>;
|
|
33
47
|
deps: Deps;
|
|
34
48
|
context: Context;
|
|
@@ -38,7 +52,7 @@ export type ModuleContext<Reg extends ActionRegistry, Context extends Record<str
|
|
|
38
52
|
context: Context;
|
|
39
53
|
}) => T) => T;
|
|
40
54
|
};
|
|
41
|
-
export declare function createModuleFactory<Reg extends ActionRegistry, Context extends Record<string, any>>(): <Use extends ModuleMap
|
|
55
|
+
export declare function createModuleFactory<Reg extends ActionRegistry, Context extends Record<string, any>>(): <Use extends ModuleMap = {}, Own extends ModuleShape = {}>(config: {
|
|
42
56
|
use?: Use;
|
|
43
57
|
define: (ctx: ModuleContext<Reg, Context, Use>) => Own;
|
|
44
58
|
}) => Module<Reg, Context, Own, Use>;
|
package/dist/workflow-module.js
CHANGED
|
@@ -1,29 +1,180 @@
|
|
|
1
|
+
// import { ActionRegistry, Simplify, WorkflowObserver } from "./types.js";
|
|
2
|
+
// import { createWorkflow, WorkflowDef } from "./workflow-composer.js";
|
|
3
|
+
// import { executeWorkflow } from "./workflow-executor.js";
|
|
4
|
+
//
|
|
5
|
+
// type AnyWorkflow = WorkflowDef<any, any, any, any, any>;
|
|
6
|
+
//
|
|
7
|
+
// type WorkflowFromDeps<Deps extends Record<string, any>> = {
|
|
8
|
+
// [K in keyof Deps]: Deps[K] extends Module<any, any, infer Own, infer SubDeps>
|
|
9
|
+
// ? Own[keyof Own] | WorkflowFromDeps<SubDeps>
|
|
10
|
+
// : never;
|
|
11
|
+
// }[keyof Deps];
|
|
12
|
+
// type ModuleShape = Record<string, AnyWorkflow>;
|
|
13
|
+
//
|
|
14
|
+
// type ContextFromDeps<Deps> = [keyof Deps] extends [never]
|
|
15
|
+
// ? {} // 👈 THIS is the fix
|
|
16
|
+
// : {
|
|
17
|
+
// [K in keyof Deps]: Deps[K] extends Module<any, infer Ctx, any, any>
|
|
18
|
+
// ? Ctx
|
|
19
|
+
// : never;
|
|
20
|
+
// }[keyof Deps];
|
|
21
|
+
//
|
|
22
|
+
// type FinalContext<
|
|
23
|
+
// Reg extends ActionRegistry,
|
|
24
|
+
// Context extends Record<string, any>,
|
|
25
|
+
// Deps extends ModuleMap<Reg>,
|
|
26
|
+
// > = Context & ContextFromDeps<Deps>;
|
|
27
|
+
//
|
|
28
|
+
// type ModuleMap<Reg extends ActionRegistry> = Record<
|
|
29
|
+
// string,
|
|
30
|
+
// Module<Reg, any, any, any>
|
|
31
|
+
// >;
|
|
32
|
+
//
|
|
33
|
+
// // type Module<Own extends ModuleShape = {}, Deps extends ModuleMap = {}> = Own & {
|
|
34
|
+
// // deps: Deps;
|
|
35
|
+
// // };
|
|
36
|
+
// export type WorkflowInput<W> =
|
|
37
|
+
// W extends WorkflowDef<any, infer I, any, any, any> ? I : never;
|
|
38
|
+
//
|
|
39
|
+
// export type WorkflowResults<W> =
|
|
40
|
+
// W extends WorkflowDef<any, any, infer R, any, any> ? R : never;
|
|
41
|
+
//
|
|
42
|
+
// export type WorkflowOutput<W> =
|
|
43
|
+
// W extends WorkflowDef<any, any, any, any, infer O> ? O : never;
|
|
44
|
+
// type Module<
|
|
45
|
+
// Reg extends ActionRegistry,
|
|
46
|
+
// Context extends Record<string, any>,
|
|
47
|
+
// Own extends ModuleShape = {},
|
|
48
|
+
// Deps extends ModuleMap<Reg> = {},
|
|
49
|
+
// > = {
|
|
50
|
+
// own: Own;
|
|
51
|
+
// deps: Deps;
|
|
52
|
+
// createRuntime: (config: {
|
|
53
|
+
// registry: Reg;
|
|
54
|
+
// context: FinalContext<Reg, Context, Deps>;
|
|
55
|
+
// }) => {
|
|
56
|
+
// run: <W extends Own[keyof Own] | WorkflowFromDeps<Deps>>(
|
|
57
|
+
// workflow: W,
|
|
58
|
+
// input: WorkflowInput<W>,
|
|
59
|
+
// obververs?: WorkflowObserver<Reg>[],
|
|
60
|
+
// ) => Promise<{
|
|
61
|
+
// results: WorkflowResults<W>;
|
|
62
|
+
// output: WorkflowOutput<W>;
|
|
63
|
+
// extras: Record<string, any>;
|
|
64
|
+
// }>;
|
|
65
|
+
// getContext: () => FinalContext<Reg, Context, Deps>;
|
|
66
|
+
// };
|
|
67
|
+
// };
|
|
68
|
+
//
|
|
69
|
+
// export type ModuleContext<
|
|
70
|
+
// Reg extends ActionRegistry,
|
|
71
|
+
// Context extends Record<string, any>,
|
|
72
|
+
// Deps extends ModuleMap<Reg>,
|
|
73
|
+
// > = {
|
|
74
|
+
// wf: ReturnType<typeof createWorkflow<Reg, Context>>;
|
|
75
|
+
//
|
|
76
|
+
// deps: Deps;
|
|
77
|
+
// context: Context;
|
|
78
|
+
//
|
|
79
|
+
// tools: <T>(
|
|
80
|
+
// factory: (ctx: {
|
|
81
|
+
// wf: ModuleContext<Reg, Context, Deps>["wf"];
|
|
82
|
+
// deps: Deps;
|
|
83
|
+
// context: Context;
|
|
84
|
+
// }) => T,
|
|
85
|
+
// ) => T;
|
|
86
|
+
// };
|
|
87
|
+
//
|
|
88
|
+
// function createModule<
|
|
89
|
+
// Reg extends ActionRegistry,
|
|
90
|
+
// Context extends Record<string, any>,
|
|
91
|
+
// Use extends ModuleMap<Reg>,
|
|
92
|
+
// Own extends ModuleShape,
|
|
93
|
+
// >(config: {
|
|
94
|
+
// use?: Use;
|
|
95
|
+
// define: (ctx: ModuleContext<Reg, Context, Use>) => Own;
|
|
96
|
+
// }): Module<Reg, Context, Own, Use> {
|
|
97
|
+
// const wf = createWorkflow<Reg, Context>();
|
|
98
|
+
//
|
|
99
|
+
// const deps = (config.use ?? {}) as Use;
|
|
100
|
+
//
|
|
101
|
+
// const moduleCtx: ModuleContext<Reg, Context, Use> = {
|
|
102
|
+
// wf,
|
|
103
|
+
// deps,
|
|
104
|
+
// context: {} as Context,
|
|
105
|
+
//
|
|
106
|
+
// tools: (factory) =>
|
|
107
|
+
// factory({
|
|
108
|
+
// wf,
|
|
109
|
+
// deps,
|
|
110
|
+
// context: {} as Context,
|
|
111
|
+
// }),
|
|
112
|
+
// };
|
|
113
|
+
//
|
|
114
|
+
// const own = config.define(moduleCtx);
|
|
115
|
+
//
|
|
116
|
+
// return {
|
|
117
|
+
// own,
|
|
118
|
+
// deps,
|
|
119
|
+
// createRuntime({ registry, context }) {
|
|
120
|
+
// const runtimeCtx = { ...context } as FinalContext<Reg, Context, Use>;
|
|
121
|
+
// return {
|
|
122
|
+
// run: async (workflow, input, observers = []) => {
|
|
123
|
+
// return executeWorkflow(workflow, registry, input, context, observers);
|
|
124
|
+
// },
|
|
125
|
+
// getContext: () => ({ ...runtimeCtx }),
|
|
126
|
+
// };
|
|
127
|
+
// },
|
|
128
|
+
// };
|
|
129
|
+
// }
|
|
130
|
+
//
|
|
131
|
+
// export function createModuleFactory<
|
|
132
|
+
// Reg extends ActionRegistry,
|
|
133
|
+
// Context extends Record<string, any>,
|
|
134
|
+
// >() {
|
|
135
|
+
// return function <
|
|
136
|
+
// Use extends ModuleMap<Reg> = {},
|
|
137
|
+
// Own extends ModuleShape = {},
|
|
138
|
+
// >(config: {
|
|
139
|
+
// use?: Use;
|
|
140
|
+
// define: (ctx: ModuleContext<Reg, Context, Use>) => Own;
|
|
141
|
+
// }): Module<Reg, Context, Own, Use> {
|
|
142
|
+
// return createModule<Reg, Context, Use, Own>(config);
|
|
143
|
+
// };
|
|
144
|
+
// }
|
|
145
|
+
//
|
|
1
146
|
import { createWorkflow } from "./workflow-composer.js";
|
|
2
147
|
import { executeWorkflow } from "./workflow-executor.js";
|
|
148
|
+
function toPublicDeps(deps) {
|
|
149
|
+
return deps;
|
|
150
|
+
}
|
|
3
151
|
function createModule(config) {
|
|
4
|
-
const wf = createWorkflow();
|
|
5
152
|
const deps = (config.use ?? {});
|
|
6
|
-
const
|
|
7
|
-
wf,
|
|
153
|
+
const own = config.define({
|
|
154
|
+
wf: createWorkflow(),
|
|
8
155
|
deps,
|
|
9
156
|
context: {},
|
|
10
157
|
tools: (factory) => factory({
|
|
11
|
-
wf,
|
|
158
|
+
wf: createWorkflow(),
|
|
12
159
|
deps,
|
|
13
160
|
context: {},
|
|
14
161
|
}),
|
|
15
|
-
};
|
|
16
|
-
|
|
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
|
+
}
|
|
17
167
|
return {
|
|
18
168
|
own,
|
|
19
|
-
deps,
|
|
169
|
+
deps: toPublicDeps(deps),
|
|
20
170
|
createRuntime({ registry, context }) {
|
|
21
|
-
const
|
|
171
|
+
const workflowRegistry = mergeWFs();
|
|
22
172
|
return {
|
|
23
|
-
run: async (
|
|
24
|
-
|
|
173
|
+
run: async (workflowId, input, observers = []) => {
|
|
174
|
+
const wfObj = workflowRegistry[workflowId]; // <-- concrete WorkflowDef
|
|
175
|
+
return executeWorkflow(wfObj, registry, input, context, observers);
|
|
25
176
|
},
|
|
26
|
-
getContext: () => ({ ...
|
|
177
|
+
getContext: () => ({ ...context }),
|
|
27
178
|
};
|
|
28
179
|
},
|
|
29
180
|
};
|