@abyss-project/runtime 3.8.0 → 3.10.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/.eslintrc.js +32 -32
- package/dist/cost/execution-cost.d.ts +6 -0
- package/dist/cost/execution-cost.js +27 -12
- package/dist/cost/execution-cost.js.map +1 -1
- package/dist/discord/discord.types.d.ts +44 -2
- package/dist/discord/discord.types.js +12 -1
- package/dist/discord/discord.types.js.map +1 -1
- package/dist/engine/engine-source.js +1 -1
- package/dist/runner-protocol/execution-protocol.types.d.ts +3 -0
- package/dist/runner-protocol/execution-protocol.types.js +5 -1
- package/dist/runner-protocol/execution-protocol.types.js.map +1 -1
- package/jest.config.ts +11 -11
- package/package.json +1 -1
- package/scripts/build-engine.cjs +70 -70
- package/src/__tests__/engine-bundle.test.ts +121 -121
- package/src/access/__tests__/host-match.test.ts +24 -24
- package/src/access/access.types.ts +25 -25
- package/src/access/host-match.ts +13 -13
- package/src/cost/__tests__/execution-cost.test.ts +249 -196
- package/src/cost/execution-cost.ts +203 -186
- package/src/definition/__tests__/validate-definition.test.ts +354 -354
- package/src/definition/definition.limits.ts +9 -9
- package/src/definition/definition.schema.ts +172 -172
- package/src/definition/definition.types.ts +171 -171
- package/src/definition/validate-definition.ts +206 -206
- package/src/definition/walk-steps.ts +44 -44
- package/src/discord/__tests__/discord-event-input.test.ts +90 -89
- package/src/discord/discord.types.ts +56 -1
- package/src/engine/engine-entry.ts +3 -3
- package/src/engine/engine-global.ts +2 -2
- package/src/errors/runtime-error-code.enum.ts +37 -37
- package/src/index.ts +32 -32
- package/src/interpreter/__tests__/interpreter.test.ts +854 -854
- package/src/interpreter/__tests__/log-respond-resume.test.ts +271 -271
- package/src/interpreter/interpreter-error.ts +12 -12
- package/src/interpreter/interpreter.ts +483 -483
- package/src/interpreter/interpreter.types.ts +204 -204
- package/src/interpreter/sequence-path.ts +67 -67
- package/src/interpreter/test-host.ts +85 -85
- package/src/runner-protocol/__tests__/file-payload.test.ts +26 -0
- package/src/runner-protocol/evaluation.types.ts +21 -21
- package/src/runner-protocol/execution-protocol.types.ts +20 -0
- package/src/schedule/schedule.types.ts +100 -100
- package/src/server/__tests__/code-runner-session.test.ts +311 -311
- package/src/server/code-runner-error.ts +12 -12
- package/src/server/code-runner-session.ts +310 -310
- package/src/server/index.ts +11 -11
- package/src/steps/__tests__/global-references.test.ts +133 -133
- package/src/steps/execute-step.ts +186 -186
- package/src/steps/global-references.ts +48 -48
- package/src/steps/step-access.ts +56 -56
- package/src/steps/step-fields.ts +87 -87
- package/src/steps/value-source.ts +69 -69
- package/tsconfig.check.json +15 -15
- package/tsconfig.json +35 -35
|
@@ -1,204 +1,204 @@
|
|
|
1
|
-
import type { AccessGrant } from '../access/access.types';
|
|
2
|
-
import type {
|
|
3
|
-
HostActionCatalog,
|
|
4
|
-
HttpMethod,
|
|
5
|
-
LogLevel,
|
|
6
|
-
StopStatus,
|
|
7
|
-
} from '../definition/definition.types';
|
|
8
|
-
import type { RuntimeErrorDetail } from '../errors/runtime-error-code.enum';
|
|
9
|
-
import type { EvaluationItem, EvaluationResult } from '../runner-protocol/evaluation.types';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Version of the `RunState` layout. A state persisted by a host (Flow, during a suspension) is
|
|
13
|
-
* resumed only by a core that reads its version; any change to the layout bumps it.
|
|
14
|
-
*/
|
|
15
|
-
export const CURRENT_STATE_VERSION = 1;
|
|
16
|
-
|
|
17
|
-
export enum RunStatus {
|
|
18
|
-
/** Waiting for the result of the effect it just emitted. */
|
|
19
|
-
WAITING = 'WAITING',
|
|
20
|
-
SUCCEEDED = 'SUCCEEDED',
|
|
21
|
-
FAILED = 'FAILED',
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface RunBudget {
|
|
25
|
-
maxSteps: number;
|
|
26
|
-
maxEffects: number;
|
|
27
|
-
/**
|
|
28
|
-
* When given, LOG effects do not count in `maxEffects`: they are counted apart, and a LOG step
|
|
29
|
-
* past this number is answered without emitting its effect. Absent, LOG counts in `maxEffects`.
|
|
30
|
-
*/
|
|
31
|
-
maxLogLines?: number;
|
|
32
|
-
/** Longest `durationMs` a WAIT may ask for; the host applies the same bound to `until`. */
|
|
33
|
-
maxWaitMs: number;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface RunOptions {
|
|
37
|
-
catalog: HostActionCatalog;
|
|
38
|
-
/** What this run is actually allowed to do; compared to the definition's declarations. */
|
|
39
|
-
grants: AccessGrant[];
|
|
40
|
-
budget: RunBudget;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** Data an evaluation sees: `input`, `steps.<id>.output`, `vars`. `fn.*` is added by the runner. */
|
|
44
|
-
export interface EvaluationContext {
|
|
45
|
-
input: unknown;
|
|
46
|
-
steps: Record<string, { output: unknown }>;
|
|
47
|
-
vars: Record<string, unknown>;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export enum EffectType {
|
|
51
|
-
EVALUATE = 'EVALUATE',
|
|
52
|
-
HTTP = 'HTTP',
|
|
53
|
-
HOST = 'HOST',
|
|
54
|
-
WAIT = 'WAIT',
|
|
55
|
-
LOG = 'LOG',
|
|
56
|
-
RESPOND = 'RESPOND',
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** Every expression and template of one step, sent to the code runner as one lot. */
|
|
60
|
-
export interface EvaluateEffect {
|
|
61
|
-
type: EffectType.EVALUATE;
|
|
62
|
-
stepId: string;
|
|
63
|
-
items: EvaluationItem[];
|
|
64
|
-
context: EvaluationContext;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export interface HttpRequest {
|
|
68
|
-
method: HttpMethod;
|
|
69
|
-
url: string;
|
|
70
|
-
headers: Record<string, string>;
|
|
71
|
-
body?: unknown;
|
|
72
|
-
timeoutMs?: number;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface HttpEffect {
|
|
76
|
-
type: EffectType.HTTP;
|
|
77
|
-
stepId: string;
|
|
78
|
-
request: HttpRequest;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export interface HostEffect {
|
|
82
|
-
type: EffectType.HOST;
|
|
83
|
-
stepId: string;
|
|
84
|
-
action: string;
|
|
85
|
-
input: Record<string, unknown>;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* The interpreter has no clock: the host turns either field into a wake-up. It bounds
|
|
90
|
-
* `durationMs` against `RunBudget.maxWaitMs` itself, but cannot bound `until`: the host must
|
|
91
|
-
* refuse an `until` further from now than the same `maxWaitMs`.
|
|
92
|
-
*/
|
|
93
|
-
export interface WaitEffect {
|
|
94
|
-
type: EffectType.WAIT;
|
|
95
|
-
stepId: string;
|
|
96
|
-
durationMs?: number;
|
|
97
|
-
/** ISO 8601. */
|
|
98
|
-
until?: string;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export interface LogEffect {
|
|
102
|
-
type: EffectType.LOG;
|
|
103
|
-
stepId: string;
|
|
104
|
-
level: LogLevel;
|
|
105
|
-
message: string;
|
|
106
|
-
data?: unknown;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/** The HTTP response a RESPOND step sends to the caller of the run. */
|
|
110
|
-
export interface HttpResponseToSend {
|
|
111
|
-
status: number;
|
|
112
|
-
headers: Record<string, string>;
|
|
113
|
-
body?: unknown;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export interface RespondEffect {
|
|
117
|
-
type: EffectType.RESPOND;
|
|
118
|
-
stepId: string;
|
|
119
|
-
response: HttpResponseToSend;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export type Effect =
|
|
123
|
-
| EvaluateEffect
|
|
124
|
-
| HttpEffect
|
|
125
|
-
| HostEffect
|
|
126
|
-
| WaitEffect
|
|
127
|
-
| LogEffect
|
|
128
|
-
| RespondEffect;
|
|
129
|
-
|
|
130
|
-
export enum RunEventType {
|
|
131
|
-
EVALUATED = 'EVALUATED',
|
|
132
|
-
HTTP_COMPLETED = 'HTTP_COMPLETED',
|
|
133
|
-
HOST_COMPLETED = 'HOST_COMPLETED',
|
|
134
|
-
WAIT_ELAPSED = 'WAIT_ELAPSED',
|
|
135
|
-
LOG_WRITTEN = 'LOG_WRITTEN',
|
|
136
|
-
RESPONSE_SENT = 'RESPONSE_SENT',
|
|
137
|
-
/**
|
|
138
|
-
* Answers the pending effect of a resumed run (any effect but EVALUATE): `output` becomes the
|
|
139
|
-
* output of the step that suspended.
|
|
140
|
-
*/
|
|
141
|
-
RESUMED = 'RESUMED',
|
|
142
|
-
EFFECT_FAILED = 'EFFECT_FAILED',
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export interface HttpResponse {
|
|
146
|
-
status: number;
|
|
147
|
-
headers: Record<string, string>;
|
|
148
|
-
body: unknown;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export type RunEvent =
|
|
152
|
-
| { type: RunEventType.EVALUATED; results: EvaluationResult[] }
|
|
153
|
-
| { type: RunEventType.HTTP_COMPLETED; response: HttpResponse }
|
|
154
|
-
| { type: RunEventType.HOST_COMPLETED; output: unknown }
|
|
155
|
-
| { type: RunEventType.WAIT_ELAPSED }
|
|
156
|
-
| { type: RunEventType.LOG_WRITTEN }
|
|
157
|
-
| { type: RunEventType.RESPONSE_SENT }
|
|
158
|
-
| { type: RunEventType.RESUMED; output: unknown }
|
|
159
|
-
| { type: RunEventType.EFFECT_FAILED; error: RuntimeErrorDetail };
|
|
160
|
-
|
|
161
|
-
/** Position in the step tree: indexes from the root sequence down to the current step. */
|
|
162
|
-
export interface Frame {
|
|
163
|
-
/** Path of the sequence, e.g. `steps`, `steps[2].then`, `steps[1].cases[0].steps`. */
|
|
164
|
-
sequencePath: string;
|
|
165
|
-
index: number;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Plain JSON: a host can persist it during a long wait (Flow) or keep it in memory (Reflex).
|
|
170
|
-
* Only the interpreter reads or writes its fields.
|
|
171
|
-
*/
|
|
172
|
-
export interface RunState {
|
|
173
|
-
/** Layout of this object; see `CURRENT_STATE_VERSION`. */
|
|
174
|
-
stateVersion: number;
|
|
175
|
-
formatVersion: number;
|
|
176
|
-
status: RunStatus;
|
|
177
|
-
input: unknown;
|
|
178
|
-
stack: Frame[];
|
|
179
|
-
outputs: Record<string, { output: unknown }>;
|
|
180
|
-
vars: Record<string, unknown>;
|
|
181
|
-
/** The effect the run waits for, if any. */
|
|
182
|
-
pending: Effect | null;
|
|
183
|
-
counters: {
|
|
184
|
-
steps: number;
|
|
185
|
-
effects: number;
|
|
186
|
-
/** LOG effects emitted, counted only when the budget has `maxLogLines`. */
|
|
187
|
-
logLines?: number;
|
|
188
|
-
/** LOG steps answered without emitting because `maxLogLines` was reached. */
|
|
189
|
-
droppedLogLines?: number;
|
|
190
|
-
};
|
|
191
|
-
/** Set once the run is SUCCEEDED (stop output) or FAILED. */
|
|
192
|
-
outcome?: {
|
|
193
|
-
status: StopStatus;
|
|
194
|
-
output?: unknown;
|
|
195
|
-
error?: RuntimeErrorDetail & { stepId?: string };
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export interface StepResult {
|
|
200
|
-
state: RunState;
|
|
201
|
-
/** Zero or one effect: the interpreter waits for one result at a time. */
|
|
202
|
-
effects: Effect[];
|
|
203
|
-
status: RunStatus;
|
|
204
|
-
}
|
|
1
|
+
import type { AccessGrant } from '../access/access.types';
|
|
2
|
+
import type {
|
|
3
|
+
HostActionCatalog,
|
|
4
|
+
HttpMethod,
|
|
5
|
+
LogLevel,
|
|
6
|
+
StopStatus,
|
|
7
|
+
} from '../definition/definition.types';
|
|
8
|
+
import type { RuntimeErrorDetail } from '../errors/runtime-error-code.enum';
|
|
9
|
+
import type { EvaluationItem, EvaluationResult } from '../runner-protocol/evaluation.types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Version of the `RunState` layout. A state persisted by a host (Flow, during a suspension) is
|
|
13
|
+
* resumed only by a core that reads its version; any change to the layout bumps it.
|
|
14
|
+
*/
|
|
15
|
+
export const CURRENT_STATE_VERSION = 1;
|
|
16
|
+
|
|
17
|
+
export enum RunStatus {
|
|
18
|
+
/** Waiting for the result of the effect it just emitted. */
|
|
19
|
+
WAITING = 'WAITING',
|
|
20
|
+
SUCCEEDED = 'SUCCEEDED',
|
|
21
|
+
FAILED = 'FAILED',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface RunBudget {
|
|
25
|
+
maxSteps: number;
|
|
26
|
+
maxEffects: number;
|
|
27
|
+
/**
|
|
28
|
+
* When given, LOG effects do not count in `maxEffects`: they are counted apart, and a LOG step
|
|
29
|
+
* past this number is answered without emitting its effect. Absent, LOG counts in `maxEffects`.
|
|
30
|
+
*/
|
|
31
|
+
maxLogLines?: number;
|
|
32
|
+
/** Longest `durationMs` a WAIT may ask for; the host applies the same bound to `until`. */
|
|
33
|
+
maxWaitMs: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface RunOptions {
|
|
37
|
+
catalog: HostActionCatalog;
|
|
38
|
+
/** What this run is actually allowed to do; compared to the definition's declarations. */
|
|
39
|
+
grants: AccessGrant[];
|
|
40
|
+
budget: RunBudget;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Data an evaluation sees: `input`, `steps.<id>.output`, `vars`. `fn.*` is added by the runner. */
|
|
44
|
+
export interface EvaluationContext {
|
|
45
|
+
input: unknown;
|
|
46
|
+
steps: Record<string, { output: unknown }>;
|
|
47
|
+
vars: Record<string, unknown>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export enum EffectType {
|
|
51
|
+
EVALUATE = 'EVALUATE',
|
|
52
|
+
HTTP = 'HTTP',
|
|
53
|
+
HOST = 'HOST',
|
|
54
|
+
WAIT = 'WAIT',
|
|
55
|
+
LOG = 'LOG',
|
|
56
|
+
RESPOND = 'RESPOND',
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Every expression and template of one step, sent to the code runner as one lot. */
|
|
60
|
+
export interface EvaluateEffect {
|
|
61
|
+
type: EffectType.EVALUATE;
|
|
62
|
+
stepId: string;
|
|
63
|
+
items: EvaluationItem[];
|
|
64
|
+
context: EvaluationContext;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface HttpRequest {
|
|
68
|
+
method: HttpMethod;
|
|
69
|
+
url: string;
|
|
70
|
+
headers: Record<string, string>;
|
|
71
|
+
body?: unknown;
|
|
72
|
+
timeoutMs?: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface HttpEffect {
|
|
76
|
+
type: EffectType.HTTP;
|
|
77
|
+
stepId: string;
|
|
78
|
+
request: HttpRequest;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface HostEffect {
|
|
82
|
+
type: EffectType.HOST;
|
|
83
|
+
stepId: string;
|
|
84
|
+
action: string;
|
|
85
|
+
input: Record<string, unknown>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The interpreter has no clock: the host turns either field into a wake-up. It bounds
|
|
90
|
+
* `durationMs` against `RunBudget.maxWaitMs` itself, but cannot bound `until`: the host must
|
|
91
|
+
* refuse an `until` further from now than the same `maxWaitMs`.
|
|
92
|
+
*/
|
|
93
|
+
export interface WaitEffect {
|
|
94
|
+
type: EffectType.WAIT;
|
|
95
|
+
stepId: string;
|
|
96
|
+
durationMs?: number;
|
|
97
|
+
/** ISO 8601. */
|
|
98
|
+
until?: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface LogEffect {
|
|
102
|
+
type: EffectType.LOG;
|
|
103
|
+
stepId: string;
|
|
104
|
+
level: LogLevel;
|
|
105
|
+
message: string;
|
|
106
|
+
data?: unknown;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The HTTP response a RESPOND step sends to the caller of the run. */
|
|
110
|
+
export interface HttpResponseToSend {
|
|
111
|
+
status: number;
|
|
112
|
+
headers: Record<string, string>;
|
|
113
|
+
body?: unknown;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface RespondEffect {
|
|
117
|
+
type: EffectType.RESPOND;
|
|
118
|
+
stepId: string;
|
|
119
|
+
response: HttpResponseToSend;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export type Effect =
|
|
123
|
+
| EvaluateEffect
|
|
124
|
+
| HttpEffect
|
|
125
|
+
| HostEffect
|
|
126
|
+
| WaitEffect
|
|
127
|
+
| LogEffect
|
|
128
|
+
| RespondEffect;
|
|
129
|
+
|
|
130
|
+
export enum RunEventType {
|
|
131
|
+
EVALUATED = 'EVALUATED',
|
|
132
|
+
HTTP_COMPLETED = 'HTTP_COMPLETED',
|
|
133
|
+
HOST_COMPLETED = 'HOST_COMPLETED',
|
|
134
|
+
WAIT_ELAPSED = 'WAIT_ELAPSED',
|
|
135
|
+
LOG_WRITTEN = 'LOG_WRITTEN',
|
|
136
|
+
RESPONSE_SENT = 'RESPONSE_SENT',
|
|
137
|
+
/**
|
|
138
|
+
* Answers the pending effect of a resumed run (any effect but EVALUATE): `output` becomes the
|
|
139
|
+
* output of the step that suspended.
|
|
140
|
+
*/
|
|
141
|
+
RESUMED = 'RESUMED',
|
|
142
|
+
EFFECT_FAILED = 'EFFECT_FAILED',
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface HttpResponse {
|
|
146
|
+
status: number;
|
|
147
|
+
headers: Record<string, string>;
|
|
148
|
+
body: unknown;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export type RunEvent =
|
|
152
|
+
| { type: RunEventType.EVALUATED; results: EvaluationResult[] }
|
|
153
|
+
| { type: RunEventType.HTTP_COMPLETED; response: HttpResponse }
|
|
154
|
+
| { type: RunEventType.HOST_COMPLETED; output: unknown }
|
|
155
|
+
| { type: RunEventType.WAIT_ELAPSED }
|
|
156
|
+
| { type: RunEventType.LOG_WRITTEN }
|
|
157
|
+
| { type: RunEventType.RESPONSE_SENT }
|
|
158
|
+
| { type: RunEventType.RESUMED; output: unknown }
|
|
159
|
+
| { type: RunEventType.EFFECT_FAILED; error: RuntimeErrorDetail };
|
|
160
|
+
|
|
161
|
+
/** Position in the step tree: indexes from the root sequence down to the current step. */
|
|
162
|
+
export interface Frame {
|
|
163
|
+
/** Path of the sequence, e.g. `steps`, `steps[2].then`, `steps[1].cases[0].steps`. */
|
|
164
|
+
sequencePath: string;
|
|
165
|
+
index: number;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Plain JSON: a host can persist it during a long wait (Flow) or keep it in memory (Reflex).
|
|
170
|
+
* Only the interpreter reads or writes its fields.
|
|
171
|
+
*/
|
|
172
|
+
export interface RunState {
|
|
173
|
+
/** Layout of this object; see `CURRENT_STATE_VERSION`. */
|
|
174
|
+
stateVersion: number;
|
|
175
|
+
formatVersion: number;
|
|
176
|
+
status: RunStatus;
|
|
177
|
+
input: unknown;
|
|
178
|
+
stack: Frame[];
|
|
179
|
+
outputs: Record<string, { output: unknown }>;
|
|
180
|
+
vars: Record<string, unknown>;
|
|
181
|
+
/** The effect the run waits for, if any. */
|
|
182
|
+
pending: Effect | null;
|
|
183
|
+
counters: {
|
|
184
|
+
steps: number;
|
|
185
|
+
effects: number;
|
|
186
|
+
/** LOG effects emitted, counted only when the budget has `maxLogLines`. */
|
|
187
|
+
logLines?: number;
|
|
188
|
+
/** LOG steps answered without emitting because `maxLogLines` was reached. */
|
|
189
|
+
droppedLogLines?: number;
|
|
190
|
+
};
|
|
191
|
+
/** Set once the run is SUCCEEDED (stop output) or FAILED. */
|
|
192
|
+
outcome?: {
|
|
193
|
+
status: StopStatus;
|
|
194
|
+
output?: unknown;
|
|
195
|
+
error?: RuntimeErrorDetail & { stepId?: string };
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export interface StepResult {
|
|
200
|
+
state: RunState;
|
|
201
|
+
/** Zero or one effect: the interpreter waits for one result at a time. */
|
|
202
|
+
effects: Effect[];
|
|
203
|
+
status: RunStatus;
|
|
204
|
+
}
|
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
import { RuntimeErrorCode } from '../errors/runtime-error-code.enum';
|
|
2
|
-
import { StepType } from '../definition/definition.types';
|
|
3
|
-
import type { RuntimeDefinition, Step } from '../definition/definition.types';
|
|
4
|
-
import { InterpreterError } from './interpreter-error';
|
|
5
|
-
import type { Frame } from './interpreter.types';
|
|
6
|
-
|
|
7
|
-
const ROOT = 'steps';
|
|
8
|
-
const SEGMENT = /\[(\d+)\]\.(?:(then|else|default)|cases\[(\d+)\]\.steps)/y;
|
|
9
|
-
|
|
10
|
-
const corrupted = (path: string): InterpreterError =>
|
|
11
|
-
new InterpreterError(
|
|
12
|
-
RuntimeErrorCode.INVALID_REQUEST,
|
|
13
|
-
`Run state points outside the definition: ${path}`,
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
const branchOf = (
|
|
17
|
-
step: Step,
|
|
18
|
-
name: string | undefined,
|
|
19
|
-
caseIndex: string | undefined,
|
|
20
|
-
): Step[] | undefined => {
|
|
21
|
-
if (step.type === StepType.CONDITION) {
|
|
22
|
-
return name === 'then' ? step.then : name === 'else' ? step.else : undefined;
|
|
23
|
-
}
|
|
24
|
-
if (step.type === StepType.SWITCH) {
|
|
25
|
-
return name === 'default'
|
|
26
|
-
? step.default
|
|
27
|
-
: caseIndex === undefined
|
|
28
|
-
? undefined
|
|
29
|
-
: step.cases[Number(caseIndex)]?.steps;
|
|
30
|
-
}
|
|
31
|
-
return undefined;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/** The sequence a frame iterates: the root one, or a branch reached through the path. */
|
|
35
|
-
export const resolveSequence = (definition: RuntimeDefinition, path: string): Step[] => {
|
|
36
|
-
if (!path.startsWith(ROOT)) {
|
|
37
|
-
throw corrupted(path);
|
|
38
|
-
}
|
|
39
|
-
let sequence = definition.steps;
|
|
40
|
-
let position = ROOT.length;
|
|
41
|
-
while (position < path.length) {
|
|
42
|
-
SEGMENT.lastIndex = position;
|
|
43
|
-
const match = SEGMENT.exec(path);
|
|
44
|
-
const next = match ? branchOf(sequence[Number(match[1])], match[2], match[3]) : undefined;
|
|
45
|
-
if (!match || !next) {
|
|
46
|
-
throw corrupted(path);
|
|
47
|
-
}
|
|
48
|
-
sequence = next;
|
|
49
|
-
position = SEGMENT.lastIndex;
|
|
50
|
-
}
|
|
51
|
-
return sequence;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/** The step a frame points at. */
|
|
55
|
-
export const resolveStep = (definition: RuntimeDefinition, frame: Frame): Step => {
|
|
56
|
-
const step = resolveSequence(definition, frame.sequencePath)[frame.index];
|
|
57
|
-
if (!step) {
|
|
58
|
-
throw corrupted(frame.sequencePath);
|
|
59
|
-
}
|
|
60
|
-
return step;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
/** Path of a branch of the step the frame points at. */
|
|
64
|
-
export const branchPath = (frame: Frame, branch: string): string =>
|
|
65
|
-
`${frame.sequencePath}[${frame.index}].${branch}`;
|
|
66
|
-
|
|
67
|
-
export const ROOT_SEQUENCE_PATH = ROOT;
|
|
1
|
+
import { RuntimeErrorCode } from '../errors/runtime-error-code.enum';
|
|
2
|
+
import { StepType } from '../definition/definition.types';
|
|
3
|
+
import type { RuntimeDefinition, Step } from '../definition/definition.types';
|
|
4
|
+
import { InterpreterError } from './interpreter-error';
|
|
5
|
+
import type { Frame } from './interpreter.types';
|
|
6
|
+
|
|
7
|
+
const ROOT = 'steps';
|
|
8
|
+
const SEGMENT = /\[(\d+)\]\.(?:(then|else|default)|cases\[(\d+)\]\.steps)/y;
|
|
9
|
+
|
|
10
|
+
const corrupted = (path: string): InterpreterError =>
|
|
11
|
+
new InterpreterError(
|
|
12
|
+
RuntimeErrorCode.INVALID_REQUEST,
|
|
13
|
+
`Run state points outside the definition: ${path}`,
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
const branchOf = (
|
|
17
|
+
step: Step,
|
|
18
|
+
name: string | undefined,
|
|
19
|
+
caseIndex: string | undefined,
|
|
20
|
+
): Step[] | undefined => {
|
|
21
|
+
if (step.type === StepType.CONDITION) {
|
|
22
|
+
return name === 'then' ? step.then : name === 'else' ? step.else : undefined;
|
|
23
|
+
}
|
|
24
|
+
if (step.type === StepType.SWITCH) {
|
|
25
|
+
return name === 'default'
|
|
26
|
+
? step.default
|
|
27
|
+
: caseIndex === undefined
|
|
28
|
+
? undefined
|
|
29
|
+
: step.cases[Number(caseIndex)]?.steps;
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** The sequence a frame iterates: the root one, or a branch reached through the path. */
|
|
35
|
+
export const resolveSequence = (definition: RuntimeDefinition, path: string): Step[] => {
|
|
36
|
+
if (!path.startsWith(ROOT)) {
|
|
37
|
+
throw corrupted(path);
|
|
38
|
+
}
|
|
39
|
+
let sequence = definition.steps;
|
|
40
|
+
let position = ROOT.length;
|
|
41
|
+
while (position < path.length) {
|
|
42
|
+
SEGMENT.lastIndex = position;
|
|
43
|
+
const match = SEGMENT.exec(path);
|
|
44
|
+
const next = match ? branchOf(sequence[Number(match[1])], match[2], match[3]) : undefined;
|
|
45
|
+
if (!match || !next) {
|
|
46
|
+
throw corrupted(path);
|
|
47
|
+
}
|
|
48
|
+
sequence = next;
|
|
49
|
+
position = SEGMENT.lastIndex;
|
|
50
|
+
}
|
|
51
|
+
return sequence;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** The step a frame points at. */
|
|
55
|
+
export const resolveStep = (definition: RuntimeDefinition, frame: Frame): Step => {
|
|
56
|
+
const step = resolveSequence(definition, frame.sequencePath)[frame.index];
|
|
57
|
+
if (!step) {
|
|
58
|
+
throw corrupted(frame.sequencePath);
|
|
59
|
+
}
|
|
60
|
+
return step;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** Path of a branch of the step the frame points at. */
|
|
64
|
+
export const branchPath = (frame: Frame, branch: string): string =>
|
|
65
|
+
`${frame.sequencePath}[${frame.index}].${branch}`;
|
|
66
|
+
|
|
67
|
+
export const ROOT_SEQUENCE_PATH = ROOT;
|