@codemation/core-nodes 0.2.0 → 0.3.0
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/CHANGELOG.md +8 -0
- package/dist/index.cjs +4 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -8
- package/dist/index.d.ts +12 -8
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/workflowAuthoring/WorkflowBranchBuilder.types.ts +10 -7
- package/src/workflowAuthoring/WorkflowChain.types.ts +30 -18
package/package.json
CHANGED
|
@@ -19,6 +19,11 @@ import { WorkflowAgentNodeFactory } from "./WorkflowAgentNodeFactory.types";
|
|
|
19
19
|
import { WorkflowDefinedNodeResolver } from "./WorkflowDefinedNodeResolver.types";
|
|
20
20
|
import { WorkflowDurationParser } from "./WorkflowDurationParser.types";
|
|
21
21
|
|
|
22
|
+
type WorkflowMapCallback<TCurrentJson, TNextJson> = (
|
|
23
|
+
item: Item<TCurrentJson>,
|
|
24
|
+
ctx: NodeExecutionContext<MapData<TCurrentJson, TNextJson>>,
|
|
25
|
+
) => TNextJson;
|
|
26
|
+
|
|
22
27
|
export class WorkflowBranchBuilder<TCurrentJson> {
|
|
23
28
|
constructor(private readonly steps: ReadonlyArray<AnyRunnableNodeConfig> = []) {}
|
|
24
29
|
|
|
@@ -28,22 +33,20 @@ export class WorkflowBranchBuilder<TCurrentJson> {
|
|
|
28
33
|
return new WorkflowBranchBuilder<RunnableNodeOutputJson<TConfig>>([...this.steps, config]);
|
|
29
34
|
}
|
|
30
35
|
|
|
31
|
-
map<TNextJson>(mapper:
|
|
36
|
+
map<TNextJson>(mapper: WorkflowMapCallback<TCurrentJson, TNextJson>): WorkflowBranchBuilder<TNextJson>;
|
|
32
37
|
map<TNextJson>(
|
|
33
38
|
name: string,
|
|
34
|
-
mapper:
|
|
39
|
+
mapper: WorkflowMapCallback<TCurrentJson, TNextJson>,
|
|
35
40
|
options?: MapDataOptions,
|
|
36
41
|
): WorkflowBranchBuilder<TNextJson>;
|
|
37
42
|
map<TNextJson>(
|
|
38
|
-
nameOrMapper: string |
|
|
39
|
-
mapperOrUndefined?:
|
|
43
|
+
nameOrMapper: string | WorkflowMapCallback<TCurrentJson, TNextJson>,
|
|
44
|
+
mapperOrUndefined?: WorkflowMapCallback<TCurrentJson, TNextJson>,
|
|
40
45
|
options?: MapDataOptions,
|
|
41
46
|
): WorkflowBranchBuilder<TNextJson> {
|
|
42
47
|
const name = typeof nameOrMapper === "string" ? nameOrMapper : "Map data";
|
|
43
48
|
const mapper = typeof nameOrMapper === "string" ? mapperOrUndefined! : nameOrMapper;
|
|
44
|
-
return this.then(
|
|
45
|
-
new MapData<TCurrentJson, TNextJson>(name, (item) => mapper(item.json as TCurrentJson), options),
|
|
46
|
-
) as WorkflowBranchBuilder<TNextJson>;
|
|
49
|
+
return this.then(new MapData<TCurrentJson, TNextJson>(name, mapper, options)) as WorkflowBranchBuilder<TNextJson>;
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
wait(duration: number | string): WorkflowBranchBuilder<TCurrentJson>;
|
|
@@ -29,6 +29,18 @@ type BranchCallback<TCurrentJson, TNextJson> = (
|
|
|
29
29
|
) => WorkflowBranchBuilder<TNextJson>;
|
|
30
30
|
type RouteBranchCallback<TCurrentJson, TNextJson> = (branch: WorkflowChain<TCurrentJson>) => WorkflowChain<TNextJson>;
|
|
31
31
|
type BranchOutputMatch<TLeft, TRight> = [TLeft] extends [TRight] ? ([TRight] extends [TLeft] ? true : false) : false;
|
|
32
|
+
type WorkflowMapCallback<TCurrentJson, TNextJson> = (
|
|
33
|
+
item: Item<TCurrentJson>,
|
|
34
|
+
ctx: NodeExecutionContext<MapData<TCurrentJson, TNextJson>>,
|
|
35
|
+
) => TNextJson;
|
|
36
|
+
type WorkflowIfPredicate<TCurrentJson> = (
|
|
37
|
+
item: Item<TCurrentJson>,
|
|
38
|
+
ctx: NodeExecutionContext<If<TCurrentJson>>,
|
|
39
|
+
) => boolean;
|
|
40
|
+
type WorkflowSwitchCaseKeyResolver<TCurrentJson> = (
|
|
41
|
+
item: Item<TCurrentJson>,
|
|
42
|
+
ctx: NodeExecutionContext<Switch<TCurrentJson>>,
|
|
43
|
+
) => string | Promise<string>;
|
|
32
44
|
|
|
33
45
|
export class WorkflowChain<TCurrentJson> {
|
|
34
46
|
constructor(private readonly chain: ChainCursor<TCurrentJson>) {}
|
|
@@ -39,22 +51,20 @@ export class WorkflowChain<TCurrentJson> {
|
|
|
39
51
|
return new WorkflowChain(this.chain.then(config));
|
|
40
52
|
}
|
|
41
53
|
|
|
42
|
-
map<TNextJson>(mapper:
|
|
54
|
+
map<TNextJson>(mapper: WorkflowMapCallback<TCurrentJson, TNextJson>): WorkflowChain<TNextJson>;
|
|
43
55
|
map<TNextJson>(
|
|
44
56
|
name: string,
|
|
45
|
-
mapper:
|
|
57
|
+
mapper: WorkflowMapCallback<TCurrentJson, TNextJson>,
|
|
46
58
|
options?: MapDataOptions,
|
|
47
59
|
): WorkflowChain<TNextJson>;
|
|
48
60
|
map<TNextJson>(
|
|
49
|
-
nameOrMapper: string |
|
|
50
|
-
mapperOrUndefined?:
|
|
61
|
+
nameOrMapper: string | WorkflowMapCallback<TCurrentJson, TNextJson>,
|
|
62
|
+
mapperOrUndefined?: WorkflowMapCallback<TCurrentJson, TNextJson>,
|
|
51
63
|
options?: MapDataOptions,
|
|
52
64
|
): WorkflowChain<TNextJson> {
|
|
53
65
|
const name = typeof nameOrMapper === "string" ? nameOrMapper : "Map data";
|
|
54
66
|
const mapper = typeof nameOrMapper === "string" ? mapperOrUndefined! : nameOrMapper;
|
|
55
|
-
return this.then(
|
|
56
|
-
new MapData<TCurrentJson, TNextJson>(name, (item) => mapper(item.json as TCurrentJson), options),
|
|
57
|
-
) as WorkflowChain<TNextJson>;
|
|
67
|
+
return this.then(new MapData<TCurrentJson, TNextJson>(name, mapper, options)) as WorkflowChain<TNextJson>;
|
|
58
68
|
}
|
|
59
69
|
|
|
60
70
|
wait(duration: number | string): WorkflowChain<TCurrentJson>;
|
|
@@ -190,7 +200,7 @@ export class WorkflowChain<TCurrentJson> {
|
|
|
190
200
|
}
|
|
191
201
|
|
|
192
202
|
if<TBranchJson>(
|
|
193
|
-
predicate:
|
|
203
|
+
predicate: WorkflowIfPredicate<TCurrentJson>,
|
|
194
204
|
branches: Readonly<{
|
|
195
205
|
true?: BranchCallback<TCurrentJson, TBranchJson>;
|
|
196
206
|
false?: BranchCallback<TCurrentJson, TBranchJson>;
|
|
@@ -198,16 +208,16 @@ export class WorkflowChain<TCurrentJson> {
|
|
|
198
208
|
): WorkflowChain<TBranchJson>;
|
|
199
209
|
if<TBranchJson>(
|
|
200
210
|
name: string,
|
|
201
|
-
predicate:
|
|
211
|
+
predicate: WorkflowIfPredicate<TCurrentJson>,
|
|
202
212
|
branches: Readonly<{
|
|
203
213
|
true?: BranchCallback<TCurrentJson, TBranchJson>;
|
|
204
214
|
false?: BranchCallback<TCurrentJson, TBranchJson>;
|
|
205
215
|
}>,
|
|
206
216
|
): WorkflowChain<TBranchJson>;
|
|
207
217
|
if<TTrueJson, TFalseJson>(
|
|
208
|
-
nameOrPredicate: string |
|
|
218
|
+
nameOrPredicate: string | WorkflowIfPredicate<TCurrentJson>,
|
|
209
219
|
predicateOrBranches:
|
|
210
|
-
|
|
|
220
|
+
| WorkflowIfPredicate<TCurrentJson>
|
|
211
221
|
| Readonly<{ true?: BranchCallback<TCurrentJson, TTrueJson>; false?: BranchCallback<TCurrentJson, TFalseJson> }>,
|
|
212
222
|
branchesOrUndefined?: Readonly<{
|
|
213
223
|
true?: BranchCallback<TCurrentJson, TTrueJson>;
|
|
@@ -216,12 +226,14 @@ export class WorkflowChain<TCurrentJson> {
|
|
|
216
226
|
): WorkflowChain<BranchOutputMatch<TTrueJson, TFalseJson> extends true ? TTrueJson : never> {
|
|
217
227
|
const name = typeof nameOrPredicate === "string" ? nameOrPredicate : "If";
|
|
218
228
|
const predicate =
|
|
219
|
-
typeof nameOrPredicate === "string"
|
|
229
|
+
typeof nameOrPredicate === "string"
|
|
230
|
+
? (predicateOrBranches as WorkflowIfPredicate<TCurrentJson>)
|
|
231
|
+
: nameOrPredicate;
|
|
220
232
|
const branches = (typeof nameOrPredicate === "string" ? branchesOrUndefined : predicateOrBranches) as Readonly<{
|
|
221
233
|
true?: BranchCallback<TCurrentJson, TTrueJson>;
|
|
222
234
|
false?: BranchCallback<TCurrentJson, TFalseJson>;
|
|
223
235
|
}>;
|
|
224
|
-
const cursor = this.chain.then(new If<TCurrentJson>(name, (item) => predicate(item
|
|
236
|
+
const cursor = this.chain.then(new If<TCurrentJson>(name, (item, _index, _items, ctx) => predicate(item, ctx)));
|
|
225
237
|
const trueSteps = branches.true?.(new WorkflowBranchBuilder<TCurrentJson>()).getSteps();
|
|
226
238
|
const falseSteps = branches.false?.(new WorkflowBranchBuilder<TCurrentJson>()).getSteps();
|
|
227
239
|
return new WorkflowChain(
|
|
@@ -258,7 +270,7 @@ export class WorkflowChain<TCurrentJson> {
|
|
|
258
270
|
cfg: Readonly<{
|
|
259
271
|
cases: readonly string[];
|
|
260
272
|
defaultCase: string;
|
|
261
|
-
resolveCaseKey:
|
|
273
|
+
resolveCaseKey: WorkflowSwitchCaseKeyResolver<TCurrentJson>;
|
|
262
274
|
branches: Readonly<Record<string, RouteBranchCallback<TCurrentJson, TBranchJson> | undefined>>;
|
|
263
275
|
}>,
|
|
264
276
|
): WorkflowChain<TBranchJson>;
|
|
@@ -267,7 +279,7 @@ export class WorkflowChain<TCurrentJson> {
|
|
|
267
279
|
cfg: Readonly<{
|
|
268
280
|
cases: readonly string[];
|
|
269
281
|
defaultCase: string;
|
|
270
|
-
resolveCaseKey:
|
|
282
|
+
resolveCaseKey: WorkflowSwitchCaseKeyResolver<TCurrentJson>;
|
|
271
283
|
branches: Readonly<Record<string, RouteBranchCallback<TCurrentJson, TBranchJson> | undefined>>;
|
|
272
284
|
}>,
|
|
273
285
|
id?: string,
|
|
@@ -278,13 +290,13 @@ export class WorkflowChain<TCurrentJson> {
|
|
|
278
290
|
| Readonly<{
|
|
279
291
|
cases: readonly string[];
|
|
280
292
|
defaultCase: string;
|
|
281
|
-
resolveCaseKey:
|
|
293
|
+
resolveCaseKey: WorkflowSwitchCaseKeyResolver<TCurrentJson>;
|
|
282
294
|
branches: Readonly<Record<string, RouteBranchCallback<TCurrentJson, TBranchJson> | undefined>>;
|
|
283
295
|
}>,
|
|
284
296
|
cfgOrUndefined?: Readonly<{
|
|
285
297
|
cases: readonly string[];
|
|
286
298
|
defaultCase: string;
|
|
287
|
-
resolveCaseKey:
|
|
299
|
+
resolveCaseKey: WorkflowSwitchCaseKeyResolver<TCurrentJson>;
|
|
288
300
|
branches: Readonly<Record<string, RouteBranchCallback<TCurrentJson, TBranchJson> | undefined>>;
|
|
289
301
|
}>,
|
|
290
302
|
id?: string,
|
|
@@ -297,7 +309,7 @@ export class WorkflowChain<TCurrentJson> {
|
|
|
297
309
|
{
|
|
298
310
|
cases: cfg.cases,
|
|
299
311
|
defaultCase: cfg.defaultCase,
|
|
300
|
-
resolveCaseKey: (item) => cfg.resolveCaseKey(item
|
|
312
|
+
resolveCaseKey: (item, _index, _items, ctx) => cfg.resolveCaseKey(item, ctx),
|
|
301
313
|
},
|
|
302
314
|
id,
|
|
303
315
|
),
|