@telorun/run 0.1.1
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 +9 -0
- package/LICENSE +17 -0
- package/dist/sequence.d.ts +66 -0
- package/dist/sequence.js +217 -0
- package/package.json +23 -0
- package/src/sequence.ts +318 -0
- package/tsconfig.json +17 -0
- package/tsconfig.lib.json +9 -0
- package/tsconfig.spec.json +10 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# SUSTAINABLE USE LICENSE (Fair-code)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DiglyAI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to use, copy, modify, and distribute the Software for any purpose—including commercial purposes—subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
1. ANTI-COMPETITION RESTRICTION: The Software may not be provided to third parties as a managed service, commercial SaaS (Software-as-a-Service), PaaS (Platform-as-a-Service), BaaS (Backend-as-a-Service), or similar offering where the primary value provided to the user is the functionality of the Software itself, without a separate commercial license from the copyright holder.
|
|
8
|
+
|
|
9
|
+
2. PERMITTED COMMERCIAL USE: You are free to use the Software to build, host, and monetize your own commercial applications, products, and services, provided such use does not violate Clause 1.
|
|
10
|
+
|
|
11
|
+
3. ATTRIBUTION: This copyright notice and license must be included in all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
4. CONTRIBUTIONS: Contributions to the Software are welcome and encouraged. By contributing, you agree that your contributions may be incorporated into the Software and distributed under this license.
|
|
14
|
+
|
|
15
|
+
5. DISCLAIMER: The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the Software or the use or other dealings in the Software.
|
|
16
|
+
|
|
17
|
+
For commercial licensing, managed hosting exemptions, or enterprise inquiries, please contact DiglyAI.
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Invocable, KindRef, ResourceContext, ScopeHandle } from "@telorun/sdk";
|
|
2
|
+
interface RetryPolicy {
|
|
3
|
+
attempts?: number;
|
|
4
|
+
delay?: string;
|
|
5
|
+
}
|
|
6
|
+
interface InvokeStep {
|
|
7
|
+
name: string;
|
|
8
|
+
when?: string;
|
|
9
|
+
invoke: KindRef<Invocable>;
|
|
10
|
+
inputs?: Record<string, unknown>;
|
|
11
|
+
retry?: RetryPolicy;
|
|
12
|
+
}
|
|
13
|
+
interface IfStep {
|
|
14
|
+
name: string;
|
|
15
|
+
if: string;
|
|
16
|
+
then: Step[];
|
|
17
|
+
else?: Step[];
|
|
18
|
+
}
|
|
19
|
+
interface WhileStep {
|
|
20
|
+
name: string;
|
|
21
|
+
while: string;
|
|
22
|
+
do: Step[];
|
|
23
|
+
}
|
|
24
|
+
interface SwitchStep {
|
|
25
|
+
name: string;
|
|
26
|
+
switch: string;
|
|
27
|
+
cases: Record<string, Step[]>;
|
|
28
|
+
default?: Step[];
|
|
29
|
+
}
|
|
30
|
+
interface TryStep {
|
|
31
|
+
name: string;
|
|
32
|
+
when?: string;
|
|
33
|
+
try: Step[];
|
|
34
|
+
catch?: Step[];
|
|
35
|
+
finally?: Step[];
|
|
36
|
+
}
|
|
37
|
+
type Step = InvokeStep | IfStep | WhileStep | SwitchStep | TryStep;
|
|
38
|
+
interface RunSequenceManifest {
|
|
39
|
+
metadata: Record<string, string | number | boolean>;
|
|
40
|
+
with?: ScopeHandle;
|
|
41
|
+
targets?: string[];
|
|
42
|
+
inputs?: Record<string, Record<string, unknown>>;
|
|
43
|
+
outputs?: Record<string, unknown>;
|
|
44
|
+
steps: Step[];
|
|
45
|
+
}
|
|
46
|
+
declare class RunSequence {
|
|
47
|
+
private readonly ctx;
|
|
48
|
+
readonly resource: RunSequenceManifest;
|
|
49
|
+
constructor(ctx: ResourceContext, resource: RunSequenceManifest);
|
|
50
|
+
init(): Promise<void>;
|
|
51
|
+
private resolveInvokes;
|
|
52
|
+
run(): Promise<void>;
|
|
53
|
+
invoke(inputs: Record<string, unknown>): Promise<unknown>;
|
|
54
|
+
private runScopeTargets;
|
|
55
|
+
teardown(): Promise<void>;
|
|
56
|
+
private executeSteps;
|
|
57
|
+
private executeStep;
|
|
58
|
+
private executeInvokeStep;
|
|
59
|
+
private executeIfStep;
|
|
60
|
+
private executeWhileStep;
|
|
61
|
+
private executeSwitchStep;
|
|
62
|
+
private executeTryStep;
|
|
63
|
+
}
|
|
64
|
+
export declare function register(): void;
|
|
65
|
+
export declare function create(resource: RunSequenceManifest, ctx: ResourceContext): Promise<RunSequence>;
|
|
66
|
+
export {};
|
package/dist/sequence.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
function isInvokeStep(step) {
|
|
2
|
+
return "invoke" in step;
|
|
3
|
+
}
|
|
4
|
+
function isIfStep(step) {
|
|
5
|
+
return "if" in step;
|
|
6
|
+
}
|
|
7
|
+
function isWhileStep(step) {
|
|
8
|
+
return "while" in step;
|
|
9
|
+
}
|
|
10
|
+
function isSwitchStep(step) {
|
|
11
|
+
return "switch" in step;
|
|
12
|
+
}
|
|
13
|
+
function isTryStep(step) {
|
|
14
|
+
return "try" in step;
|
|
15
|
+
}
|
|
16
|
+
class RunSequence {
|
|
17
|
+
ctx;
|
|
18
|
+
resource;
|
|
19
|
+
constructor(ctx, resource) {
|
|
20
|
+
this.ctx = ctx;
|
|
21
|
+
this.resource = resource;
|
|
22
|
+
}
|
|
23
|
+
async init() {
|
|
24
|
+
this.resolveInvokes(this.resource.steps);
|
|
25
|
+
}
|
|
26
|
+
resolveInvokes(stepList) {
|
|
27
|
+
for (const step of stepList) {
|
|
28
|
+
if (isInvokeStep(step)) {
|
|
29
|
+
const raw = step.invoke;
|
|
30
|
+
if (!raw || typeof raw.invoke !== "function") {
|
|
31
|
+
step.invoke = this.ctx.resolveChildren(raw, step.name);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (isIfStep(step)) {
|
|
35
|
+
this.resolveInvokes(step.then);
|
|
36
|
+
if (step.else)
|
|
37
|
+
this.resolveInvokes(step.else);
|
|
38
|
+
}
|
|
39
|
+
if (isWhileStep(step))
|
|
40
|
+
this.resolveInvokes(step.do);
|
|
41
|
+
if (isSwitchStep(step)) {
|
|
42
|
+
for (const branch of Object.values(step.cases))
|
|
43
|
+
this.resolveInvokes(branch);
|
|
44
|
+
if (step.default)
|
|
45
|
+
this.resolveInvokes(step.default);
|
|
46
|
+
}
|
|
47
|
+
if (isTryStep(step)) {
|
|
48
|
+
this.resolveInvokes(step.try);
|
|
49
|
+
if (step.catch)
|
|
50
|
+
this.resolveInvokes(step.catch);
|
|
51
|
+
if (step.finally)
|
|
52
|
+
this.resolveInvokes(step.finally);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async run() {
|
|
57
|
+
if (this.resource.with) {
|
|
58
|
+
await this.resource.with.run(async (scope) => {
|
|
59
|
+
await this.runScopeTargets(scope);
|
|
60
|
+
await this.executeSteps(this.resource.steps, {}, scope, {});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
await this.executeSteps(this.resource.steps, {}, undefined, {});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async invoke(inputs) {
|
|
68
|
+
const steps = {};
|
|
69
|
+
const extraCtx = inputs ?? {};
|
|
70
|
+
if (this.resource.with) {
|
|
71
|
+
await this.resource.with.run(async (scope) => {
|
|
72
|
+
await this.runScopeTargets(scope);
|
|
73
|
+
await this.executeSteps(this.resource.steps, steps, scope, extraCtx);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
await this.executeSteps(this.resource.steps, steps, undefined, extraCtx);
|
|
78
|
+
}
|
|
79
|
+
if (this.resource.outputs) {
|
|
80
|
+
return this.ctx.expandValue(this.resource.outputs, { steps, ...extraCtx });
|
|
81
|
+
}
|
|
82
|
+
return steps;
|
|
83
|
+
}
|
|
84
|
+
async runScopeTargets(scope) {
|
|
85
|
+
if (!this.resource.targets?.length)
|
|
86
|
+
return;
|
|
87
|
+
await Promise.all(this.resource.targets.map((name) => {
|
|
88
|
+
const instance = scope.getInstance(name);
|
|
89
|
+
if (typeof instance.run !== "function") {
|
|
90
|
+
throw new Error(`Scope target '${name}' does not have a run() method`);
|
|
91
|
+
}
|
|
92
|
+
return instance.run();
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
async teardown() { }
|
|
96
|
+
async executeSteps(stepList, steps, scope, extraCtx) {
|
|
97
|
+
for (const step of stepList) {
|
|
98
|
+
await this.executeStep(step, steps, scope, extraCtx);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async executeStep(step, steps, scope, extraCtx) {
|
|
102
|
+
if (isInvokeStep(step))
|
|
103
|
+
await this.executeInvokeStep(step, steps, scope, extraCtx);
|
|
104
|
+
else if (isIfStep(step))
|
|
105
|
+
await this.executeIfStep(step, steps, scope, extraCtx);
|
|
106
|
+
else if (isWhileStep(step))
|
|
107
|
+
await this.executeWhileStep(step, steps, scope, extraCtx);
|
|
108
|
+
else if (isSwitchStep(step))
|
|
109
|
+
await this.executeSwitchStep(step, steps, scope, extraCtx);
|
|
110
|
+
else if (isTryStep(step))
|
|
111
|
+
await this.executeTryStep(step, steps, scope, extraCtx);
|
|
112
|
+
else
|
|
113
|
+
throw new Error(`Step "${step.name}" has no recognized type key`);
|
|
114
|
+
}
|
|
115
|
+
async executeInvokeStep(step, steps, scope, extraCtx) {
|
|
116
|
+
const cel = { steps, ...extraCtx };
|
|
117
|
+
if (step.when !== undefined && !this.ctx.expandValue(step.when, cel))
|
|
118
|
+
return;
|
|
119
|
+
const inputs = this.ctx.expandValue(step.inputs ?? {}, cel);
|
|
120
|
+
const raw = step.invoke;
|
|
121
|
+
let result;
|
|
122
|
+
if (raw && typeof raw.invoke === "function") {
|
|
123
|
+
result = await raw.invoke(inputs);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
const ref = raw;
|
|
127
|
+
if (scope) {
|
|
128
|
+
result = await scope.getInstance(ref.name).invoke(inputs);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
result = await this.ctx.invoke(ref.kind, ref.name, inputs, { retry: step.retry });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
steps[step.name] = { result };
|
|
135
|
+
}
|
|
136
|
+
async executeIfStep(step, steps, scope, extraCtx) {
|
|
137
|
+
if (this.ctx.expandValue(step.if, { steps, ...extraCtx })) {
|
|
138
|
+
await this.executeSteps(step.then, steps, scope, extraCtx);
|
|
139
|
+
}
|
|
140
|
+
else if (step.else) {
|
|
141
|
+
await this.executeSteps(step.else, steps, scope, extraCtx);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async executeWhileStep(step, steps, scope, extraCtx) {
|
|
145
|
+
while (this.ctx.expandValue(step.while, { steps, ...extraCtx })) {
|
|
146
|
+
await this.executeSteps(step.do, steps, scope, extraCtx);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async executeSwitchStep(step, steps, scope, extraCtx) {
|
|
150
|
+
const key = String(this.ctx.expandValue(step.switch, { steps, ...extraCtx }));
|
|
151
|
+
if (Object.prototype.hasOwnProperty.call(step.cases, key)) {
|
|
152
|
+
await this.executeSteps(step.cases[key], steps, scope, extraCtx);
|
|
153
|
+
}
|
|
154
|
+
else if (step.default) {
|
|
155
|
+
await this.executeSteps(step.default, steps, scope, extraCtx);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
throw new Error(`Switch step "${step.name}": no matching case for "${key}" and no default`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
async executeTryStep(step, steps, scope, extraCtx) {
|
|
162
|
+
if (step.when !== undefined && !this.ctx.expandValue(step.when, { steps, ...extraCtx }))
|
|
163
|
+
return;
|
|
164
|
+
let tryFailed = false;
|
|
165
|
+
let tryError;
|
|
166
|
+
try {
|
|
167
|
+
await this.executeSteps(step.try, steps, scope, extraCtx);
|
|
168
|
+
}
|
|
169
|
+
catch (err) {
|
|
170
|
+
tryFailed = true;
|
|
171
|
+
tryError = err;
|
|
172
|
+
}
|
|
173
|
+
if (tryFailed) {
|
|
174
|
+
if (step.catch) {
|
|
175
|
+
const seqErr = toSequenceError(tryError, step.name);
|
|
176
|
+
try {
|
|
177
|
+
await this.executeSteps(step.catch, steps, scope, { ...extraCtx, error: seqErr });
|
|
178
|
+
}
|
|
179
|
+
catch (catchErr) {
|
|
180
|
+
if (step.finally) {
|
|
181
|
+
await this.executeSteps(step.finally, steps, scope, {
|
|
182
|
+
...extraCtx,
|
|
183
|
+
error: toSequenceError(catchErr, step.name),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
throw catchErr;
|
|
187
|
+
}
|
|
188
|
+
if (step.finally) {
|
|
189
|
+
await this.executeSteps(step.finally, steps, scope, { ...extraCtx, error: null });
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
if (step.finally) {
|
|
194
|
+
await this.executeSteps(step.finally, steps, scope, {
|
|
195
|
+
...extraCtx,
|
|
196
|
+
error: toSequenceError(tryError, step.name),
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
throw tryError;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
else if (step.finally) {
|
|
203
|
+
await this.executeSteps(step.finally, steps, scope, { ...extraCtx, error: null });
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function toSequenceError(err, stepName) {
|
|
208
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
209
|
+
const code = err instanceof Error && err.code != null
|
|
210
|
+
? err.code
|
|
211
|
+
: null;
|
|
212
|
+
return { message, code, step: stepName };
|
|
213
|
+
}
|
|
214
|
+
export function register() { }
|
|
215
|
+
export async function create(resource, ctx) {
|
|
216
|
+
return new RunSequence(ctx, resource);
|
|
217
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@telorun/run",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./src/sequence.ts",
|
|
6
|
+
"module": "./src/sequence.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
"./sequence": {
|
|
9
|
+
"bun": "./src/sequence.ts",
|
|
10
|
+
"import": "./dist/sequence.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@telorun/sdk": "0.2.6"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/node": "^20.0.0",
|
|
18
|
+
"typescript": "^5.0.0"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc -p tsconfig.lib.json"
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/sequence.ts
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import type { Invocable, KindRef, ResourceContext, ScopeContext, ScopeHandle } from "@telorun/sdk";
|
|
2
|
+
|
|
3
|
+
interface RetryPolicy {
|
|
4
|
+
attempts?: number;
|
|
5
|
+
delay?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface InvokeStep {
|
|
9
|
+
name: string;
|
|
10
|
+
when?: string;
|
|
11
|
+
invoke: KindRef<Invocable>;
|
|
12
|
+
inputs?: Record<string, unknown>;
|
|
13
|
+
retry?: RetryPolicy;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface IfStep {
|
|
17
|
+
name: string;
|
|
18
|
+
if: string;
|
|
19
|
+
then: Step[];
|
|
20
|
+
else?: Step[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface WhileStep {
|
|
24
|
+
name: string;
|
|
25
|
+
while: string;
|
|
26
|
+
do: Step[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface SwitchStep {
|
|
30
|
+
name: string;
|
|
31
|
+
switch: string;
|
|
32
|
+
cases: Record<string, Step[]>;
|
|
33
|
+
default?: Step[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface TryStep {
|
|
37
|
+
name: string;
|
|
38
|
+
when?: string;
|
|
39
|
+
try: Step[];
|
|
40
|
+
catch?: Step[];
|
|
41
|
+
finally?: Step[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type Step = InvokeStep | IfStep | WhileStep | SwitchStep | TryStep;
|
|
45
|
+
|
|
46
|
+
interface RunSequenceManifest {
|
|
47
|
+
metadata: Record<string, string | number | boolean>;
|
|
48
|
+
with?: ScopeHandle;
|
|
49
|
+
targets?: string[];
|
|
50
|
+
inputs?: Record<string, Record<string, unknown>>;
|
|
51
|
+
outputs?: Record<string, unknown>;
|
|
52
|
+
steps: Step[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface SequenceError {
|
|
56
|
+
message: string;
|
|
57
|
+
code: string | null;
|
|
58
|
+
step: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function isInvokeStep(step: Step): step is InvokeStep {
|
|
62
|
+
return "invoke" in step;
|
|
63
|
+
}
|
|
64
|
+
function isIfStep(step: Step): step is IfStep {
|
|
65
|
+
return "if" in step;
|
|
66
|
+
}
|
|
67
|
+
function isWhileStep(step: Step): step is WhileStep {
|
|
68
|
+
return "while" in step;
|
|
69
|
+
}
|
|
70
|
+
function isSwitchStep(step: Step): step is SwitchStep {
|
|
71
|
+
return "switch" in step;
|
|
72
|
+
}
|
|
73
|
+
function isTryStep(step: Step): step is TryStep {
|
|
74
|
+
return "try" in step;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
class RunSequence {
|
|
78
|
+
constructor(
|
|
79
|
+
private readonly ctx: ResourceContext,
|
|
80
|
+
public readonly resource: RunSequenceManifest,
|
|
81
|
+
) {}
|
|
82
|
+
|
|
83
|
+
async init(): Promise<void> {
|
|
84
|
+
this.resolveInvokes(this.resource.steps);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private resolveInvokes(stepList: Step[]): void {
|
|
88
|
+
for (const step of stepList) {
|
|
89
|
+
if (isInvokeStep(step)) {
|
|
90
|
+
const raw = step.invoke as unknown;
|
|
91
|
+
if (!raw || typeof (raw as Invocable).invoke !== "function") {
|
|
92
|
+
(step as InvokeStep).invoke = this.ctx.resolveChildren(
|
|
93
|
+
raw as any,
|
|
94
|
+
step.name,
|
|
95
|
+
) as KindRef<Invocable>;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (isIfStep(step)) {
|
|
99
|
+
this.resolveInvokes(step.then);
|
|
100
|
+
if (step.else) this.resolveInvokes(step.else);
|
|
101
|
+
}
|
|
102
|
+
if (isWhileStep(step)) this.resolveInvokes(step.do);
|
|
103
|
+
if (isSwitchStep(step)) {
|
|
104
|
+
for (const branch of Object.values(step.cases)) this.resolveInvokes(branch);
|
|
105
|
+
if (step.default) this.resolveInvokes(step.default);
|
|
106
|
+
}
|
|
107
|
+
if (isTryStep(step)) {
|
|
108
|
+
this.resolveInvokes(step.try);
|
|
109
|
+
if (step.catch) this.resolveInvokes(step.catch);
|
|
110
|
+
if (step.finally) this.resolveInvokes(step.finally);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async run(): Promise<void> {
|
|
116
|
+
if (this.resource.with) {
|
|
117
|
+
await this.resource.with.run(async (scope) => {
|
|
118
|
+
await this.runScopeTargets(scope);
|
|
119
|
+
await this.executeSteps(this.resource.steps, {}, scope, {});
|
|
120
|
+
});
|
|
121
|
+
} else {
|
|
122
|
+
await this.executeSteps(this.resource.steps, {}, undefined, {});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async invoke(inputs: Record<string, unknown>): Promise<unknown> {
|
|
127
|
+
const steps: Record<string, unknown> = {};
|
|
128
|
+
const extraCtx = inputs ?? {};
|
|
129
|
+
|
|
130
|
+
if (this.resource.with) {
|
|
131
|
+
await this.resource.with.run(async (scope) => {
|
|
132
|
+
await this.runScopeTargets(scope);
|
|
133
|
+
await this.executeSteps(this.resource.steps, steps, scope, extraCtx);
|
|
134
|
+
});
|
|
135
|
+
} else {
|
|
136
|
+
await this.executeSteps(this.resource.steps, steps, undefined, extraCtx);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (this.resource.outputs) {
|
|
140
|
+
return this.ctx.expandValue(this.resource.outputs, { steps, ...extraCtx });
|
|
141
|
+
}
|
|
142
|
+
return steps;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private async runScopeTargets(scope: ScopeContext): Promise<void> {
|
|
146
|
+
if (!this.resource.targets?.length) return;
|
|
147
|
+
await Promise.all(
|
|
148
|
+
this.resource.targets.map((name) => {
|
|
149
|
+
const instance = scope.getInstance(name);
|
|
150
|
+
if (typeof instance.run !== "function") {
|
|
151
|
+
throw new Error(`Scope target '${name}' does not have a run() method`);
|
|
152
|
+
}
|
|
153
|
+
return instance.run();
|
|
154
|
+
}),
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async teardown(): Promise<void> {}
|
|
159
|
+
|
|
160
|
+
private async executeSteps(
|
|
161
|
+
stepList: Step[],
|
|
162
|
+
steps: Record<string, unknown>,
|
|
163
|
+
scope: ScopeContext | undefined,
|
|
164
|
+
extraCtx: Record<string, unknown>,
|
|
165
|
+
): Promise<void> {
|
|
166
|
+
for (const step of stepList) {
|
|
167
|
+
await this.executeStep(step, steps, scope, extraCtx);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private async executeStep(
|
|
172
|
+
step: Step,
|
|
173
|
+
steps: Record<string, unknown>,
|
|
174
|
+
scope: ScopeContext | undefined,
|
|
175
|
+
extraCtx: Record<string, unknown>,
|
|
176
|
+
): Promise<void> {
|
|
177
|
+
if (isInvokeStep(step)) await this.executeInvokeStep(step, steps, scope, extraCtx);
|
|
178
|
+
else if (isIfStep(step)) await this.executeIfStep(step, steps, scope, extraCtx);
|
|
179
|
+
else if (isWhileStep(step)) await this.executeWhileStep(step, steps, scope, extraCtx);
|
|
180
|
+
else if (isSwitchStep(step)) await this.executeSwitchStep(step, steps, scope, extraCtx);
|
|
181
|
+
else if (isTryStep(step)) await this.executeTryStep(step, steps, scope, extraCtx);
|
|
182
|
+
else throw new Error(`Step "${(step as Step).name}" has no recognized type key`);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private async executeInvokeStep(
|
|
186
|
+
step: InvokeStep,
|
|
187
|
+
steps: Record<string, unknown>,
|
|
188
|
+
scope: ScopeContext | undefined,
|
|
189
|
+
extraCtx: Record<string, unknown>,
|
|
190
|
+
): Promise<void> {
|
|
191
|
+
const cel = { steps, ...extraCtx };
|
|
192
|
+
if (step.when !== undefined && !this.ctx.expandValue(step.when, cel)) return;
|
|
193
|
+
|
|
194
|
+
const inputs = this.ctx.expandValue(step.inputs ?? {}, cel) as Record<string, unknown>;
|
|
195
|
+
const raw = step.invoke as unknown;
|
|
196
|
+
let result: unknown;
|
|
197
|
+
|
|
198
|
+
if (raw && typeof (raw as Invocable).invoke === "function") {
|
|
199
|
+
result = await (raw as Invocable).invoke(inputs);
|
|
200
|
+
} else {
|
|
201
|
+
const ref = raw as KindRef<Invocable>;
|
|
202
|
+
if (scope) {
|
|
203
|
+
result = await (scope.getInstance(ref.name) as unknown as Invocable).invoke(inputs);
|
|
204
|
+
} else {
|
|
205
|
+
result = await this.ctx.invoke(ref.kind, ref.name, inputs, { retry: step.retry });
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
steps[step.name] = { result };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private async executeIfStep(
|
|
213
|
+
step: IfStep,
|
|
214
|
+
steps: Record<string, unknown>,
|
|
215
|
+
scope: ScopeContext | undefined,
|
|
216
|
+
extraCtx: Record<string, unknown>,
|
|
217
|
+
): Promise<void> {
|
|
218
|
+
if (this.ctx.expandValue(step.if, { steps, ...extraCtx })) {
|
|
219
|
+
await this.executeSteps(step.then, steps, scope, extraCtx);
|
|
220
|
+
} else if (step.else) {
|
|
221
|
+
await this.executeSteps(step.else, steps, scope, extraCtx);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private async executeWhileStep(
|
|
226
|
+
step: WhileStep,
|
|
227
|
+
steps: Record<string, unknown>,
|
|
228
|
+
scope: ScopeContext | undefined,
|
|
229
|
+
extraCtx: Record<string, unknown>,
|
|
230
|
+
): Promise<void> {
|
|
231
|
+
while (this.ctx.expandValue(step.while, { steps, ...extraCtx })) {
|
|
232
|
+
await this.executeSteps(step.do, steps, scope, extraCtx);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private async executeSwitchStep(
|
|
237
|
+
step: SwitchStep,
|
|
238
|
+
steps: Record<string, unknown>,
|
|
239
|
+
scope: ScopeContext | undefined,
|
|
240
|
+
extraCtx: Record<string, unknown>,
|
|
241
|
+
): Promise<void> {
|
|
242
|
+
const key = String(this.ctx.expandValue(step.switch, { steps, ...extraCtx }));
|
|
243
|
+
if (Object.prototype.hasOwnProperty.call(step.cases, key)) {
|
|
244
|
+
await this.executeSteps(step.cases[key], steps, scope, extraCtx);
|
|
245
|
+
} else if (step.default) {
|
|
246
|
+
await this.executeSteps(step.default, steps, scope, extraCtx);
|
|
247
|
+
} else {
|
|
248
|
+
throw new Error(`Switch step "${step.name}": no matching case for "${key}" and no default`);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private async executeTryStep(
|
|
253
|
+
step: TryStep,
|
|
254
|
+
steps: Record<string, unknown>,
|
|
255
|
+
scope: ScopeContext | undefined,
|
|
256
|
+
extraCtx: Record<string, unknown>,
|
|
257
|
+
): Promise<void> {
|
|
258
|
+
if (step.when !== undefined && !this.ctx.expandValue(step.when, { steps, ...extraCtx })) return;
|
|
259
|
+
|
|
260
|
+
let tryFailed = false;
|
|
261
|
+
let tryError: unknown;
|
|
262
|
+
|
|
263
|
+
try {
|
|
264
|
+
await this.executeSteps(step.try, steps, scope, extraCtx);
|
|
265
|
+
} catch (err) {
|
|
266
|
+
tryFailed = true;
|
|
267
|
+
tryError = err;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (tryFailed) {
|
|
271
|
+
if (step.catch) {
|
|
272
|
+
const seqErr = toSequenceError(tryError, step.name);
|
|
273
|
+
try {
|
|
274
|
+
await this.executeSteps(step.catch, steps, scope, { ...extraCtx, error: seqErr });
|
|
275
|
+
} catch (catchErr) {
|
|
276
|
+
if (step.finally) {
|
|
277
|
+
await this.executeSteps(step.finally, steps, scope, {
|
|
278
|
+
...extraCtx,
|
|
279
|
+
error: toSequenceError(catchErr, step.name),
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
throw catchErr;
|
|
283
|
+
}
|
|
284
|
+
if (step.finally) {
|
|
285
|
+
await this.executeSteps(step.finally, steps, scope, { ...extraCtx, error: null });
|
|
286
|
+
}
|
|
287
|
+
} else {
|
|
288
|
+
if (step.finally) {
|
|
289
|
+
await this.executeSteps(step.finally, steps, scope, {
|
|
290
|
+
...extraCtx,
|
|
291
|
+
error: toSequenceError(tryError, step.name),
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
throw tryError;
|
|
295
|
+
}
|
|
296
|
+
} else if (step.finally) {
|
|
297
|
+
await this.executeSteps(step.finally, steps, scope, { ...extraCtx, error: null });
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function toSequenceError(err: unknown, stepName: string): SequenceError {
|
|
303
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
304
|
+
const code =
|
|
305
|
+
err instanceof Error && (err as Error & { code?: string }).code != null
|
|
306
|
+
? (err as Error & { code?: string }).code!
|
|
307
|
+
: null;
|
|
308
|
+
return { message, code, step: stepName };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function register(): void {}
|
|
312
|
+
|
|
313
|
+
export async function create(
|
|
314
|
+
resource: RunSequenceManifest,
|
|
315
|
+
ctx: ResourceContext,
|
|
316
|
+
): Promise<RunSequence> {
|
|
317
|
+
return new RunSequence(ctx, resource);
|
|
318
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../tsconfig.base.json",
|
|
3
|
+
"files": [],
|
|
4
|
+
"include": ["src/**/*.ts", "test/**/*.ts"],
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"strict": true,
|
|
7
|
+
"esModuleInterop": true
|
|
8
|
+
},
|
|
9
|
+
"references": [
|
|
10
|
+
{
|
|
11
|
+
"path": "tsconfig.lib.json"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"path": "tsconfig.spec.json"
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
}
|