@pogodisco/zephyr 1.3.16 → 1.3.18
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-composer.d.ts +8 -7
- package/dist/workflow-composer.js +506 -11
- package/package.json +1 -1
|
@@ -73,11 +73,12 @@ type StepOptions<Input, Results, Context> = {
|
|
|
73
73
|
label?: string;
|
|
74
74
|
meta?: Record<string, any>;
|
|
75
75
|
};
|
|
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
76
|
type MergeBranchSteps<Branches extends readonly any[], Acc extends any[]> = Branches extends readonly [infer Head, ...infer Tail] ? MergeBranchSteps<Tail, [
|
|
78
77
|
...Acc,
|
|
79
78
|
...(Head extends WorkflowBuilder<any, any, any, infer S, any> ? S : [])
|
|
80
79
|
]> : Acc;
|
|
80
|
+
type MergeBranchResults<Branches extends readonly WorkflowBuilder<any, any, any, any, any>[]> = Simplify<Branches extends readonly [infer Head, ...infer Tail] ? Head extends WorkflowBuilder<any, any, any, any, infer R> ? Tail extends readonly WorkflowBuilder<any, any, any, any, any>[] ? R & MergeBranchResults<Tail> : R : MergeBranchResults<Tail & WorkflowBuilder<any, any, any, any, any>[]> : {}>;
|
|
81
|
+
type WorkflowBuilderStatic<Reg extends ActionRegistry, Input, Context, Steps extends StepDef<Reg, any, any>[] = [], Results = {}, Output = undefined> = WorkflowBuilder<Reg, Input, Context, Steps, Results, Output>;
|
|
81
82
|
export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown, Context = unknown, Steps extends StepDef<Reg, any, any>[] = [], Results = {}, Output = undefined> {
|
|
82
83
|
private name;
|
|
83
84
|
private steps;
|
|
@@ -104,14 +105,14 @@ export declare class WorkflowBuilder<Reg extends ActionRegistry, Input = unknown
|
|
|
104
105
|
as<NewType>(): WorkflowBuilder<Reg, Input, Context, Steps, Steps extends [...infer Rest, infer Last] ? Last extends StepDef<Reg, infer ID, any> ? Simplify<Omit<Results, ID> & {
|
|
105
106
|
[K in ID]: NewType;
|
|
106
107
|
}> : Results : Results, Output>;
|
|
107
|
-
parallel<Branches extends readonly
|
|
108
|
-
[K in keyof Branches]: (builder:
|
|
109
|
-
}): WorkflowBuilder<Reg, Input, Context, MergeBranchSteps<Branches, Steps>,
|
|
110
|
-
join<ID extends string = string, ActionName extends keyof Reg & string = any, ResolveOut extends ResolvedStepInput = ResolvedStepInput>(id: ID, action: ActionName, resolve?: (ctx: {
|
|
108
|
+
parallel<Branches extends readonly WorkflowBuilderStatic<Reg, Input, Context, any, any>[]>(...branches: {
|
|
109
|
+
[K in keyof Branches]: (builder: WorkflowBuilderStatic<Reg, Input, Context, [], Results>) => Branches[K];
|
|
110
|
+
}): WorkflowBuilder<Reg, Input, Context, MergeBranchSteps<Branches, Steps>, Results & MergeBranchResults<Branches>>;
|
|
111
|
+
join<ID extends string = string, ActionName extends keyof Reg & string = any, ResolveOut extends ResolvedStepInput = ResolvedStepInput, BranchResults = {}>(id: ID, action: ActionName, resolve?: (ctx: {
|
|
111
112
|
input: Input;
|
|
112
|
-
results: Results;
|
|
113
|
+
results: Results & BranchResults;
|
|
113
114
|
context: Context;
|
|
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>;
|
|
115
|
+
} & CallHelpers<Reg, ActionName>) => ResolveOut, options?: StepOptions<Input, Results & BranchResults, 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>;
|
|
115
116
|
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: {
|
|
116
117
|
input: Input;
|
|
117
118
|
results: Results;
|
|
@@ -1,6 +1,508 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// import {
|
|
2
|
+
// ActionParams,
|
|
3
|
+
// ActionRegistry,
|
|
4
|
+
// ActionReturn,
|
|
5
|
+
// Simplify,
|
|
6
|
+
// } from "./types.js";
|
|
7
|
+
//
|
|
8
|
+
// /* ------------------------------------------------ */
|
|
9
|
+
// /* STEP INPUT NORMALIZATION TYPES */
|
|
10
|
+
// /* ------------------------------------------------ */
|
|
11
|
+
//
|
|
12
|
+
// type NormalizedCall =
|
|
13
|
+
// | { kind: "none" }
|
|
14
|
+
// | { kind: "positional"; args: any[] }
|
|
15
|
+
// | { kind: "object"; args: any };
|
|
16
|
+
//
|
|
17
|
+
// export type ResolvedStepInput =
|
|
18
|
+
// | NormalizedCall
|
|
19
|
+
// | { kind: "loop"; items: NormalizedCall[] };
|
|
20
|
+
//
|
|
21
|
+
// type CallHelpers<Reg extends ActionRegistry, ActionName extends keyof Reg> = {
|
|
22
|
+
// args: (...args: ActionParams<Reg, ActionName>) => {
|
|
23
|
+
// kind: "positional";
|
|
24
|
+
// args: ActionParams<Reg, ActionName>;
|
|
25
|
+
// };
|
|
26
|
+
//
|
|
27
|
+
// obj: ActionParams<Reg, ActionName> extends [infer A]
|
|
28
|
+
// ? (arg: A) => { kind: "object"; args: A }
|
|
29
|
+
// : never;
|
|
30
|
+
//
|
|
31
|
+
// none: () => { kind: "none" };
|
|
32
|
+
// loop: (
|
|
33
|
+
// items:
|
|
34
|
+
// | { kind: "positional"; args: ActionParams<Reg, ActionName> }[]
|
|
35
|
+
// | { kind: "object"; args: ActionParams<Reg, ActionName>[0] }[],
|
|
36
|
+
// ) => {
|
|
37
|
+
// kind: "loop";
|
|
38
|
+
// items: typeof items;
|
|
39
|
+
// };
|
|
40
|
+
// };
|
|
41
|
+
//
|
|
42
|
+
// /* ------------------------------------------------ */
|
|
43
|
+
// /* STEP RESULT TYPES */
|
|
44
|
+
// /* ------------------------------------------------ */
|
|
45
|
+
//
|
|
46
|
+
// type SubflowResult<SubResults, SubOutput> = SubOutput extends undefined
|
|
47
|
+
// ? SubResults
|
|
48
|
+
// : SubOutput;
|
|
49
|
+
//
|
|
50
|
+
// type WorkflowInput<T> =
|
|
51
|
+
// T extends WorkflowDef<any, infer I, any, any, any> ? I : never;
|
|
52
|
+
//
|
|
53
|
+
// type WorkflowResults<T> =
|
|
54
|
+
// T extends WorkflowDef<any, any, infer R, any, any> ? R : never;
|
|
55
|
+
//
|
|
56
|
+
// type WorkflowOutput<T> =
|
|
57
|
+
// T extends WorkflowDef<any, any, any, any, infer O> ? O : never;
|
|
58
|
+
//
|
|
59
|
+
// export type StepResultFromResolve<
|
|
60
|
+
// Reg extends ActionRegistry,
|
|
61
|
+
// ActionName extends keyof Reg,
|
|
62
|
+
// R extends ResolvedStepInput,
|
|
63
|
+
// > = R extends { kind: "loop" }
|
|
64
|
+
// ? ActionReturn<Reg, ActionName>[]
|
|
65
|
+
// : ActionReturn<Reg, ActionName>;
|
|
66
|
+
//
|
|
67
|
+
// /* ------------------------------------------------ */
|
|
68
|
+
// /* STEP DEFINITION */
|
|
69
|
+
// /* ------------------------------------------------ */
|
|
70
|
+
//
|
|
71
|
+
// export type StepDef<
|
|
72
|
+
// Reg extends ActionRegistry,
|
|
73
|
+
// ID extends string = string,
|
|
74
|
+
// ActionName extends keyof Reg = any,
|
|
75
|
+
// > = {
|
|
76
|
+
// id: ID;
|
|
77
|
+
// action: ActionName;
|
|
78
|
+
// dependsOn: string[];
|
|
79
|
+
// resolve: (ctx: any) => ResolvedStepInput;
|
|
80
|
+
// when?: (ctx: any) => boolean;
|
|
81
|
+
// options?: StepOptions<any, any, any>;
|
|
82
|
+
// };
|
|
83
|
+
//
|
|
84
|
+
// /* ------------------------------------------------ */
|
|
85
|
+
// /* WORKFLOW DEFINITION */
|
|
86
|
+
// /* ------------------------------------------------ */
|
|
87
|
+
//
|
|
88
|
+
// export type WorkflowDef<
|
|
89
|
+
// Reg extends ActionRegistry,
|
|
90
|
+
// Input,
|
|
91
|
+
// Results,
|
|
92
|
+
// Steps extends StepDef<Reg, any, any>[] = StepDef<Reg, any, any>[],
|
|
93
|
+
// Output = undefined,
|
|
94
|
+
// > = {
|
|
95
|
+
// name: string;
|
|
96
|
+
// steps: Steps;
|
|
97
|
+
// entrySteps: StepDef<Reg>[];
|
|
98
|
+
// endSteps: StepDef<Reg>[];
|
|
99
|
+
// input: Input;
|
|
100
|
+
// results: Results;
|
|
101
|
+
// outputResolver?: (ctx: any) => Output;
|
|
102
|
+
// // __context?: any;
|
|
103
|
+
// };
|
|
104
|
+
// type StepRuntimeCtx<I, R, C> = {
|
|
105
|
+
// input: I;
|
|
106
|
+
// results: R;
|
|
107
|
+
// context: C;
|
|
108
|
+
// };
|
|
109
|
+
// type StepOptions<Input, Results, Context> = {
|
|
110
|
+
// retry?: number;
|
|
111
|
+
// retryDelay?: number | ((attempt: number) => number);
|
|
112
|
+
// timeout?: number;
|
|
113
|
+
//
|
|
114
|
+
// continueOnError?: boolean;
|
|
115
|
+
//
|
|
116
|
+
// onError?: (err: unknown, ctx: StepRuntimeCtx<Input, Results, Context>) => any;
|
|
117
|
+
//
|
|
118
|
+
// // optional later:
|
|
119
|
+
// label?: string;
|
|
120
|
+
// meta?: Record<string, any>;
|
|
121
|
+
// };
|
|
122
|
+
//
|
|
123
|
+
// /* ------------------------------------------------ */
|
|
124
|
+
// /* HELPER TYPES */
|
|
125
|
+
// /* ------------------------------------------------ */
|
|
126
|
+
//
|
|
127
|
+
// type MergeBranchResults<
|
|
128
|
+
// Branches extends readonly any[],
|
|
129
|
+
// Acc,
|
|
130
|
+
// > = Branches extends readonly [infer Head, ...infer Tail]
|
|
131
|
+
// ? MergeBranchResults<
|
|
132
|
+
// Tail,
|
|
133
|
+
// Acc & (Head extends WorkflowBuilder<any, any, any, any, infer R> ? R : {})
|
|
134
|
+
// >
|
|
135
|
+
// : Acc;
|
|
136
|
+
//
|
|
137
|
+
// type MergeBranchSteps<
|
|
138
|
+
// Branches extends readonly any[],
|
|
139
|
+
// Acc extends any[],
|
|
140
|
+
// > = Branches extends readonly [infer Head, ...infer Tail]
|
|
141
|
+
// ? MergeBranchSteps<
|
|
142
|
+
// Tail,
|
|
143
|
+
// [
|
|
144
|
+
// ...Acc,
|
|
145
|
+
// ...(Head extends WorkflowBuilder<any, any, any, infer S, any> ? S : []),
|
|
146
|
+
// ]
|
|
147
|
+
// >
|
|
148
|
+
// : Acc;
|
|
149
|
+
//
|
|
150
|
+
// // type UnionToIntersection<U> = (U extends any ? (x: U) => any : never) extends (
|
|
151
|
+
// // x: infer I,
|
|
152
|
+
// // ) => any
|
|
153
|
+
// // ? I
|
|
154
|
+
// // : never;
|
|
155
|
+
// //
|
|
156
|
+
// // type ExtractBranchResults<Branches extends readonly any[]> =
|
|
157
|
+
// // Branches[number] extends WorkflowBuilder<any, any, any, any, infer R>
|
|
158
|
+
// // ? R
|
|
159
|
+
// // : never;
|
|
160
|
+
// //
|
|
161
|
+
// // type MergeBranchResults<Branches extends readonly any[], Acc> = Simplify<
|
|
162
|
+
// // Acc & Partial<ExtractBranchResults<Branches>>
|
|
163
|
+
// // >;
|
|
164
|
+
//
|
|
165
|
+
// /* ------------------------------------------------ */
|
|
166
|
+
// /* FLUENT WORKFLOW BUILDER */
|
|
167
|
+
// /* ------------------------------------------------ */
|
|
168
|
+
//
|
|
169
|
+
// export class WorkflowBuilder<
|
|
170
|
+
// Reg extends ActionRegistry,
|
|
171
|
+
// Input = unknown,
|
|
172
|
+
// Context = unknown,
|
|
173
|
+
// Steps extends StepDef<Reg, any, any>[] = [],
|
|
174
|
+
// Results = {},
|
|
175
|
+
// Output = undefined,
|
|
176
|
+
// > {
|
|
177
|
+
// private steps: StepDef<Reg, any, any>[] = [];
|
|
178
|
+
// private frontier: string[] = [];
|
|
179
|
+
// private pendingWhen?: (ctx: {
|
|
180
|
+
// input: Input;
|
|
181
|
+
// results: Results;
|
|
182
|
+
// context: Context;
|
|
183
|
+
// }) => boolean;
|
|
184
|
+
// private outputResolver?: (ctx: any) => any;
|
|
185
|
+
// private clearPendingWhen() {
|
|
186
|
+
// this.pendingWhen = undefined;
|
|
187
|
+
// }
|
|
188
|
+
//
|
|
189
|
+
// constructor(private name: string) {}
|
|
190
|
+
//
|
|
191
|
+
// step<
|
|
192
|
+
// ID extends string,
|
|
193
|
+
// ActionName extends keyof Reg & string,
|
|
194
|
+
// ResolveOut extends ResolvedStepInput = ResolvedStepInput,
|
|
195
|
+
// >(
|
|
196
|
+
// id: ID,
|
|
197
|
+
// action: ActionName,
|
|
198
|
+
// resolve?: (
|
|
199
|
+
// ctx: {
|
|
200
|
+
// input: Input;
|
|
201
|
+
// results: Results;
|
|
202
|
+
// context: Context;
|
|
203
|
+
// } & CallHelpers<Reg, ActionName>,
|
|
204
|
+
// ) => ResolveOut,
|
|
205
|
+
// dependsOn?: string[],
|
|
206
|
+
// options?: StepOptions<Input, Results, Context>,
|
|
207
|
+
// ): WorkflowBuilder<
|
|
208
|
+
// Reg,
|
|
209
|
+
// Input,
|
|
210
|
+
// Context,
|
|
211
|
+
// [...Steps, StepDef<Reg, ID, ActionName>],
|
|
212
|
+
// Simplify<
|
|
213
|
+
// Results & {
|
|
214
|
+
// [K in ID]: StepResultFromResolve<Reg, ActionName, ResolveOut>;
|
|
215
|
+
// }
|
|
216
|
+
// >
|
|
217
|
+
// > {
|
|
218
|
+
// const deps = dependsOn ?? [...this.frontier];
|
|
219
|
+
//
|
|
220
|
+
// this.steps.push({
|
|
221
|
+
// id,
|
|
222
|
+
// action,
|
|
223
|
+
// resolve: resolve ?? (() => ({ kind: "none" })),
|
|
224
|
+
// dependsOn: deps,
|
|
225
|
+
// when: this.pendingWhen,
|
|
226
|
+
// options,
|
|
227
|
+
// });
|
|
228
|
+
//
|
|
229
|
+
// this.frontier = [id];
|
|
230
|
+
//
|
|
231
|
+
// return this as any;
|
|
232
|
+
// }
|
|
233
|
+
//
|
|
234
|
+
// /* ------------------------------------------------ */
|
|
235
|
+
// /* Sequential shortcut */
|
|
236
|
+
// /* ------------------------------------------------ */
|
|
237
|
+
// seq<
|
|
238
|
+
// ID extends string = string,
|
|
239
|
+
// ActionName extends keyof Reg & string = any,
|
|
240
|
+
// ResolveOut extends ResolvedStepInput = ResolvedStepInput,
|
|
241
|
+
// >(
|
|
242
|
+
// id: ID,
|
|
243
|
+
// action: ActionName,
|
|
244
|
+
// resolve?: (
|
|
245
|
+
// ctx: {
|
|
246
|
+
// input: Input;
|
|
247
|
+
// results: Results;
|
|
248
|
+
// context: Context;
|
|
249
|
+
// } & CallHelpers<Reg, ActionName>,
|
|
250
|
+
// ) => ResolveOut,
|
|
251
|
+
// options?: StepOptions<Input, Results, Context>,
|
|
252
|
+
// ) {
|
|
253
|
+
// return this.step<ID, ActionName, ResolveOut>(
|
|
254
|
+
// id,
|
|
255
|
+
// action,
|
|
256
|
+
// resolve,
|
|
257
|
+
// undefined,
|
|
258
|
+
// options,
|
|
259
|
+
// );
|
|
260
|
+
// }
|
|
261
|
+
//
|
|
262
|
+
// /* ------------------------------------------------ */
|
|
263
|
+
// /* Override the result of the last step */
|
|
264
|
+
// /* ------------------------------------------------ */
|
|
265
|
+
// as<NewType>(): WorkflowBuilder<
|
|
266
|
+
// Reg,
|
|
267
|
+
// Input,
|
|
268
|
+
// Context,
|
|
269
|
+
// Steps,
|
|
270
|
+
// // Override the result of the last step only
|
|
271
|
+
// Steps extends [...infer Rest, infer Last]
|
|
272
|
+
// ? Last extends StepDef<Reg, infer ID, any>
|
|
273
|
+
// ? Simplify<Omit<Results, ID> & { [K in ID]: NewType }>
|
|
274
|
+
// : Results
|
|
275
|
+
// : Results,
|
|
276
|
+
// Output
|
|
277
|
+
// > {
|
|
278
|
+
// return this as any;
|
|
279
|
+
// }
|
|
280
|
+
//
|
|
281
|
+
// parallel<
|
|
282
|
+
// Branches extends readonly WorkflowBuilder<Reg, Input, Context, any, any>[],
|
|
283
|
+
// >(
|
|
284
|
+
// ...branches: {
|
|
285
|
+
// [K in keyof Branches]: (
|
|
286
|
+
// builder: WorkflowBuilder<Reg, Input, Context, [], Results>,
|
|
287
|
+
// ) => Branches[K];
|
|
288
|
+
// }
|
|
289
|
+
// ): WorkflowBuilder<
|
|
290
|
+
// Reg,
|
|
291
|
+
// Input,
|
|
292
|
+
// Context,
|
|
293
|
+
// MergeBranchSteps<Branches, Steps>,
|
|
294
|
+
// // [
|
|
295
|
+
// // ...Steps,
|
|
296
|
+
// // ...(Branches[number] extends WorkflowBuilder<Reg, any, any, infer S, any>
|
|
297
|
+
// // ? S
|
|
298
|
+
// // : never),
|
|
299
|
+
// // ],
|
|
300
|
+
// Simplify<MergeBranchResults<Branches, Results>>
|
|
301
|
+
// > {
|
|
302
|
+
// const parentFrontier = [...this.frontier];
|
|
303
|
+
// const branchEnds: string[] = [];
|
|
304
|
+
//
|
|
305
|
+
// branches.forEach((branch) => {
|
|
306
|
+
// const b = new WorkflowBuilder<Reg, Input, Context, [], Results>(
|
|
307
|
+
// this.name,
|
|
308
|
+
// );
|
|
309
|
+
//
|
|
310
|
+
// b.frontier = parentFrontier;
|
|
311
|
+
// branch(b);
|
|
312
|
+
// branchEnds.push(...b.frontier);
|
|
313
|
+
// this.steps.push(...(b as any).steps);
|
|
314
|
+
// });
|
|
315
|
+
//
|
|
316
|
+
// this.frontier = branchEnds;
|
|
317
|
+
// this.clearPendingWhen();
|
|
318
|
+
// return this as any;
|
|
319
|
+
// }
|
|
320
|
+
//
|
|
321
|
+
// /* ------------------------------------------------ */
|
|
322
|
+
// /* Join helper */
|
|
323
|
+
// /* ------------------------------------------------ */
|
|
324
|
+
// join<
|
|
325
|
+
// ID extends string = string,
|
|
326
|
+
// ActionName extends keyof Reg & string = any,
|
|
327
|
+
// ResolveOut extends ResolvedStepInput = ResolvedStepInput,
|
|
328
|
+
// >(
|
|
329
|
+
// id: ID,
|
|
330
|
+
// action: ActionName,
|
|
331
|
+
// resolve?: (
|
|
332
|
+
// ctx: {
|
|
333
|
+
// input: Input;
|
|
334
|
+
// results: Results;
|
|
335
|
+
// context: Context;
|
|
336
|
+
// } & CallHelpers<Reg, ActionName>,
|
|
337
|
+
// ) => ResolveOut,
|
|
338
|
+
// options?: StepOptions<Input, Results, Context>,
|
|
339
|
+
// ) {
|
|
340
|
+
// const result = this.step<ID, ActionName, ResolveOut>(
|
|
341
|
+
// id,
|
|
342
|
+
// action,
|
|
343
|
+
// resolve,
|
|
344
|
+
// [...this.frontier],
|
|
345
|
+
// options,
|
|
346
|
+
// );
|
|
347
|
+
//
|
|
348
|
+
// this.clearPendingWhen();
|
|
349
|
+
// return result;
|
|
350
|
+
// }
|
|
351
|
+
//
|
|
352
|
+
// /* ------------------------------------------------ */
|
|
353
|
+
// /* Subflow */
|
|
354
|
+
// /* ------------------------------------------------ */
|
|
355
|
+
//
|
|
356
|
+
// subflow<
|
|
357
|
+
// Prefix extends string,
|
|
358
|
+
// SubSteps extends StepDef<Reg, any, any>[],
|
|
359
|
+
// WF extends WorkflowDef<Reg, any, any, SubSteps, any>,
|
|
360
|
+
// >(
|
|
361
|
+
// prefix: Prefix,
|
|
362
|
+
// workflow: WF,
|
|
363
|
+
// resolveInput: WorkflowInput<WF> extends undefined
|
|
364
|
+
// ?
|
|
365
|
+
// | ((ctx: {
|
|
366
|
+
// input: Input;
|
|
367
|
+
// results: Results;
|
|
368
|
+
// context: Context;
|
|
369
|
+
// }) => WorkflowInput<WF>)
|
|
370
|
+
// | undefined
|
|
371
|
+
// : (ctx: {
|
|
372
|
+
// input: Input;
|
|
373
|
+
// results: Results;
|
|
374
|
+
// context: Context;
|
|
375
|
+
// }) => WorkflowInput<WF>,
|
|
376
|
+
// options?: StepOptions<Input, Results, Context>,
|
|
377
|
+
// ): WorkflowBuilder<
|
|
378
|
+
// Reg,
|
|
379
|
+
// Input,
|
|
380
|
+
// Context,
|
|
381
|
+
// [...Steps, ...WF["steps"]],
|
|
382
|
+
// Results & {
|
|
383
|
+
// [K in Prefix]: SubflowResult<WorkflowResults<WF>, WorkflowOutput<WF>>;
|
|
384
|
+
// }
|
|
385
|
+
// > {
|
|
386
|
+
// const idMap = new Map<string, string>();
|
|
387
|
+
//
|
|
388
|
+
// workflow.steps.forEach((step) => {
|
|
389
|
+
// idMap.set(step.id, `${prefix}.${step.id}`);
|
|
390
|
+
// });
|
|
391
|
+
//
|
|
392
|
+
// workflow.steps.forEach((step) => {
|
|
393
|
+
// const newStep: StepDef<Reg, any, any> = {
|
|
394
|
+
// ...step,
|
|
395
|
+
// id: idMap.get(step.id)!,
|
|
396
|
+
// dependsOn: step.dependsOn.map((d) => idMap.get(d)!),
|
|
397
|
+
// resolve: (ctx: any) => {
|
|
398
|
+
// const subInput = resolveInput ? resolveInput(ctx) : undefined;
|
|
399
|
+
// return step.resolve({
|
|
400
|
+
// input: subInput,
|
|
401
|
+
// results: ctx.results,
|
|
402
|
+
// context: ctx.context,
|
|
403
|
+
// });
|
|
404
|
+
// },
|
|
405
|
+
// };
|
|
406
|
+
//
|
|
407
|
+
// if (workflow.entrySteps.find((e) => e.id === step.id)) {
|
|
408
|
+
// newStep.dependsOn = [...this.frontier];
|
|
409
|
+
// }
|
|
410
|
+
//
|
|
411
|
+
// this.steps.push(newStep);
|
|
412
|
+
// });
|
|
413
|
+
//
|
|
414
|
+
// this.frontier = workflow.endSteps.map((e) => idMap.get(e.id)!);
|
|
415
|
+
// return this as any;
|
|
416
|
+
// }
|
|
417
|
+
//
|
|
418
|
+
// /* ------------------------------------------------ */
|
|
419
|
+
// /* Conditional */
|
|
420
|
+
// /* ------------------------------------------------ */
|
|
421
|
+
//
|
|
422
|
+
// when(
|
|
423
|
+
// predicate: (ctx: {
|
|
424
|
+
// input: Input;
|
|
425
|
+
// results: Results;
|
|
426
|
+
// context: Context;
|
|
427
|
+
// }) => boolean,
|
|
428
|
+
// ): WorkflowBuilder<Reg, Input, Context, Steps, Results, Output> {
|
|
429
|
+
// this.pendingWhen = predicate;
|
|
430
|
+
// return this;
|
|
431
|
+
// }
|
|
432
|
+
//
|
|
433
|
+
// endWhen(): this {
|
|
434
|
+
// this.clearPendingWhen();
|
|
435
|
+
//
|
|
436
|
+
// return this;
|
|
437
|
+
// }
|
|
438
|
+
//
|
|
439
|
+
// /* ------------------------------------------------ */
|
|
440
|
+
// /* Workflow output */
|
|
441
|
+
// /* ------------------------------------------------ */
|
|
442
|
+
// output<Output>(
|
|
443
|
+
// fn: (ctx: { input: Input; results: Results; context: Context }) => Output,
|
|
444
|
+
// ): WorkflowDef<Reg, Input, Results, Steps, Output> {
|
|
445
|
+
// this.outputResolver = fn;
|
|
446
|
+
// return this.build() as WorkflowDef<Reg, Input, Results, Steps, Output>;
|
|
447
|
+
// }
|
|
448
|
+
//
|
|
449
|
+
// build(): WorkflowDef<Reg, Input, Results, Steps> {
|
|
450
|
+
// this.validateDependencies();
|
|
451
|
+
//
|
|
452
|
+
// return {
|
|
453
|
+
// name: this.name,
|
|
454
|
+
// steps: this.steps as Steps,
|
|
455
|
+
// entrySteps: this.steps.filter((s) => s.dependsOn.length === 0),
|
|
456
|
+
// endSteps: this.getEndSteps(),
|
|
457
|
+
// input: {} as Input,
|
|
458
|
+
// results: {} as Results,
|
|
459
|
+
// outputResolver: this.outputResolver,
|
|
460
|
+
// };
|
|
461
|
+
// }
|
|
462
|
+
//
|
|
463
|
+
// private validateDependencies() {
|
|
464
|
+
// const stepIds = new Set(this.steps.map((s) => s.id));
|
|
465
|
+
// for (const step of this.steps) {
|
|
466
|
+
// for (const dep of step.dependsOn) {
|
|
467
|
+
// if (!stepIds.has(dep))
|
|
468
|
+
// throw new Error(`Step ${step.id} depends on unknown step ${dep}`);
|
|
469
|
+
// }
|
|
470
|
+
// }
|
|
471
|
+
// }
|
|
472
|
+
//
|
|
473
|
+
// private getEndSteps() {
|
|
474
|
+
// const hasDependents = new Set<string>();
|
|
475
|
+
// for (const step of this.steps) {
|
|
476
|
+
// for (const dep of step.dependsOn) hasDependents.add(dep);
|
|
477
|
+
// }
|
|
478
|
+
// return this.steps.filter((s) => !hasDependents.has(s.id));
|
|
479
|
+
// }
|
|
480
|
+
// }
|
|
481
|
+
//
|
|
482
|
+
// /* ------------------------------------------------ */
|
|
483
|
+
// /* WORKFLOW CREATOR */
|
|
484
|
+
// /* ------------------------------------------------ */
|
|
485
|
+
// // export function createWorkflow<
|
|
486
|
+
// // Reg extends ActionRegistry,
|
|
487
|
+
// // Context extends Record<string, any> = {},
|
|
488
|
+
// // >(registry: Reg, context?: Context) {
|
|
489
|
+
// // return function workflow<Input = unknown>(name: string) {
|
|
490
|
+
// // return new WorkflowBuilder<Reg, Input, Context>(name);
|
|
491
|
+
// // };
|
|
492
|
+
// // }
|
|
493
|
+
//
|
|
494
|
+
// export function createWorkflow<
|
|
495
|
+
// Reg extends ActionRegistry,
|
|
496
|
+
// Context extends Record<string, any> = {},
|
|
497
|
+
// >() {
|
|
498
|
+
// return function workflow<Input = unknown>(name: string) {
|
|
499
|
+
// return new WorkflowBuilder<Reg, Input, Context>(name);
|
|
500
|
+
// };
|
|
501
|
+
// }
|
|
502
|
+
//
|
|
503
|
+
//
|
|
504
|
+
//
|
|
505
|
+
//
|
|
4
506
|
export class WorkflowBuilder {
|
|
5
507
|
clearPendingWhen() {
|
|
6
508
|
this.pendingWhen = undefined;
|
|
@@ -47,6 +549,7 @@ export class WorkflowBuilder {
|
|
|
47
549
|
});
|
|
48
550
|
this.frontier = branchEnds;
|
|
49
551
|
this.clearPendingWhen();
|
|
552
|
+
// TypeScript now knows the returned WorkflowBuilder's Results = parent & branch results
|
|
50
553
|
return this;
|
|
51
554
|
}
|
|
52
555
|
/* ------------------------------------------------ */
|
|
@@ -138,14 +641,6 @@ export class WorkflowBuilder {
|
|
|
138
641
|
/* ------------------------------------------------ */
|
|
139
642
|
/* WORKFLOW CREATOR */
|
|
140
643
|
/* ------------------------------------------------ */
|
|
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
644
|
export function createWorkflow() {
|
|
150
645
|
return function workflow(name) {
|
|
151
646
|
return new WorkflowBuilder(name);
|