@abyss-project/runtime 1.0.0 → 2.0.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.
Files changed (104) hide show
  1. package/README.md +6 -3
  2. package/dist/cost/execution-cost.d.ts +45 -0
  3. package/dist/cost/execution-cost.js +114 -0
  4. package/dist/cost/execution-cost.js.map +1 -0
  5. package/dist/definition/definition.limits.d.ts +4 -0
  6. package/dist/definition/definition.limits.js +8 -0
  7. package/dist/definition/definition.limits.js.map +1 -0
  8. package/dist/definition/definition.schema.d.ts +2 -2
  9. package/dist/definition/definition.schema.js +20 -4
  10. package/dist/definition/definition.schema.js.map +1 -1
  11. package/dist/definition/definition.types.d.ts +20 -1
  12. package/dist/definition/definition.types.js +9 -1
  13. package/dist/definition/definition.types.js.map +1 -1
  14. package/dist/engine/engine-entry.d.ts +1 -0
  15. package/dist/engine/engine-entry.js +6 -0
  16. package/dist/engine/engine-entry.js.map +1 -0
  17. package/dist/engine/engine-global.d.ts +1 -0
  18. package/dist/engine/engine-global.js +5 -0
  19. package/dist/engine/engine-global.js.map +1 -0
  20. package/dist/engine/engine-source.d.ts +2 -0
  21. package/dist/engine/engine-source.js +3 -0
  22. package/dist/engine/run-in-isolate.d.ts +34 -0
  23. package/dist/engine/run-in-isolate.js +267 -0
  24. package/dist/engine/run-in-isolate.js.map +1 -0
  25. package/dist/errors/runtime-error-code.enum.d.ts +2 -2
  26. package/dist/errors/runtime-error-code.enum.js +1 -1
  27. package/dist/errors/runtime-error-code.enum.js.map +1 -1
  28. package/dist/index.d.ts +8 -2
  29. package/dist/index.js +13 -4
  30. package/dist/index.js.map +1 -1
  31. package/dist/interpreter/interpreter.d.ts +27 -1
  32. package/dist/interpreter/interpreter.js +114 -42
  33. package/dist/interpreter/interpreter.js.map +1 -1
  34. package/dist/interpreter/interpreter.types.d.ts +35 -4
  35. package/dist/interpreter/interpreter.types.js +7 -1
  36. package/dist/interpreter/interpreter.types.js.map +1 -1
  37. package/dist/interpreter/test-host.d.ts +4 -2
  38. package/dist/interpreter/test-host.js +6 -0
  39. package/dist/interpreter/test-host.js.map +1 -1
  40. package/dist/runner-protocol/evaluation.types.d.ts +20 -0
  41. package/dist/runner-protocol/evaluation.types.js +10 -0
  42. package/dist/runner-protocol/evaluation.types.js.map +1 -0
  43. package/dist/runner-protocol/execution-protocol.types.d.ts +132 -0
  44. package/dist/runner-protocol/execution-protocol.types.js +48 -0
  45. package/dist/runner-protocol/execution-protocol.types.js.map +1 -0
  46. package/dist/server/code-runner-session.d.ts +28 -0
  47. package/dist/server/code-runner-session.js +208 -0
  48. package/dist/server/code-runner-session.js.map +1 -0
  49. package/dist/server/index.d.ts +2 -2
  50. package/dist/server/index.js +5 -3
  51. package/dist/server/index.js.map +1 -1
  52. package/dist/steps/execute-step.d.ts +12 -2
  53. package/dist/steps/execute-step.js +37 -0
  54. package/dist/steps/execute-step.js.map +1 -1
  55. package/dist/steps/global-references.d.ts +2 -0
  56. package/dist/steps/global-references.js +28 -0
  57. package/dist/steps/global-references.js.map +1 -0
  58. package/dist/steps/step-fields.d.ts +1 -1
  59. package/dist/steps/step-fields.js +10 -2
  60. package/dist/steps/step-fields.js.map +1 -1
  61. package/dist/steps/value-source.d.ts +1 -1
  62. package/dist/steps/value-source.js +3 -3
  63. package/dist/steps/value-source.js.map +1 -1
  64. package/package.json +10 -2
  65. package/scripts/build-engine.cjs +70 -0
  66. package/src/__tests__/engine-bundle.test.ts +121 -0
  67. package/src/cost/__tests__/execution-cost.test.ts +200 -0
  68. package/src/cost/execution-cost.ts +192 -0
  69. package/src/definition/__tests__/validate-definition.test.ts +354 -303
  70. package/src/definition/definition.limits.ts +9 -0
  71. package/src/definition/definition.schema.ts +172 -139
  72. package/src/definition/definition.types.ts +171 -143
  73. package/src/engine/__tests__/run-in-isolate.test.ts +468 -0
  74. package/src/engine/engine-entry.ts +3 -0
  75. package/src/engine/engine-global.ts +2 -0
  76. package/src/engine/run-in-isolate.ts +404 -0
  77. package/src/errors/runtime-error-code.enum.ts +37 -37
  78. package/src/index.ts +30 -18
  79. package/src/interpreter/__tests__/interpreter.test.ts +854 -854
  80. package/src/interpreter/__tests__/log-respond-resume.test.ts +223 -0
  81. package/src/interpreter/interpreter.ts +461 -342
  82. package/src/interpreter/interpreter.types.ts +192 -140
  83. package/src/interpreter/test-host.ts +85 -74
  84. package/src/runner-protocol/evaluation.types.ts +21 -0
  85. package/src/runner-protocol/execution-protocol.types.ts +221 -0
  86. package/src/server/__tests__/code-runner-session.test.ts +311 -0
  87. package/src/server/code-runner-error.ts +12 -12
  88. package/src/server/code-runner-session.ts +310 -0
  89. package/src/server/index.ts +11 -4
  90. package/src/steps/__tests__/global-references.test.ts +113 -0
  91. package/src/steps/execute-step.ts +186 -132
  92. package/src/steps/global-references.ts +48 -0
  93. package/src/steps/step-access.ts +56 -56
  94. package/src/steps/step-fields.ts +87 -79
  95. package/src/steps/value-source.ts +69 -69
  96. package/dist/runner-protocol/runner-protocol.types.d.ts +0 -92
  97. package/dist/runner-protocol/runner-protocol.types.js +0 -26
  98. package/dist/runner-protocol/runner-protocol.types.js.map +0 -1
  99. package/dist/server/code-runner-client.d.ts +0 -13
  100. package/dist/server/code-runner-client.js +0 -155
  101. package/dist/server/code-runner-client.js.map +0 -1
  102. package/src/runner-protocol/runner-protocol.types.ts +0 -117
  103. package/src/server/__tests__/code-runner-client.test.ts +0 -262
  104. package/src/server/code-runner-client.ts +0 -201
@@ -1,140 +1,192 @@
1
- import type { AccessGrant } from '../access/access.types';
2
- import type { HostActionCatalog, HttpMethod, StopStatus } from '../definition/definition.types';
3
- import type { RuntimeErrorDetail } from '../errors/runtime-error-code.enum';
4
- import type { EvaluationItem, EvaluationResult } from '../runner-protocol/runner-protocol.types';
5
-
6
- export enum RunStatus {
7
- /** Waiting for the result of the effect it just emitted. */
8
- WAITING = 'WAITING',
9
- SUCCEEDED = 'SUCCEEDED',
10
- FAILED = 'FAILED',
11
- }
12
-
13
- export interface RunBudget {
14
- maxSteps: number;
15
- maxEffects: number;
16
- /** Longest `durationMs` a WAIT may ask for; the host applies the same bound to `until`. */
17
- maxWaitMs: number;
18
- }
19
-
20
- export interface RunOptions {
21
- catalog: HostActionCatalog;
22
- /** What this run is actually allowed to do; compared to the definition's declarations. */
23
- grants: AccessGrant[];
24
- budget: RunBudget;
25
- }
26
-
27
- /** Data an evaluation sees: `input`, `steps.<id>.output`, `vars`. `fn.*` is added by the runner. */
28
- export interface EvaluationContext {
29
- input: unknown;
30
- steps: Record<string, { output: unknown }>;
31
- vars: Record<string, unknown>;
32
- }
33
-
34
- export enum EffectType {
35
- EVALUATE = 'EVALUATE',
36
- HTTP = 'HTTP',
37
- HOST = 'HOST',
38
- WAIT = 'WAIT',
39
- }
40
-
41
- /** Every expression and template of one step, sent to the code runner as one lot. */
42
- export interface EvaluateEffect {
43
- type: EffectType.EVALUATE;
44
- stepId: string;
45
- items: EvaluationItem[];
46
- context: EvaluationContext;
47
- }
48
-
49
- export interface HttpRequest {
50
- method: HttpMethod;
51
- url: string;
52
- headers: Record<string, string>;
53
- body?: unknown;
54
- timeoutMs?: number;
55
- }
56
-
57
- export interface HttpEffect {
58
- type: EffectType.HTTP;
59
- stepId: string;
60
- request: HttpRequest;
61
- }
62
-
63
- export interface HostEffect {
64
- type: EffectType.HOST;
65
- stepId: string;
66
- action: string;
67
- input: Record<string, unknown>;
68
- }
69
-
70
- /**
71
- * The interpreter has no clock: the host turns either field into a wake-up. It bounds
72
- * `durationMs` against `RunBudget.maxWaitMs` itself, but cannot bound `until`: the host must
73
- * refuse an `until` further from now than the same `maxWaitMs`.
74
- */
75
- export interface WaitEffect {
76
- type: EffectType.WAIT;
77
- stepId: string;
78
- durationMs?: number;
79
- /** ISO 8601. */
80
- until?: string;
81
- }
82
-
83
- export type Effect = EvaluateEffect | HttpEffect | HostEffect | WaitEffect;
84
-
85
- export enum RunEventType {
86
- EVALUATED = 'EVALUATED',
87
- HTTP_COMPLETED = 'HTTP_COMPLETED',
88
- HOST_COMPLETED = 'HOST_COMPLETED',
89
- WAIT_ELAPSED = 'WAIT_ELAPSED',
90
- EFFECT_FAILED = 'EFFECT_FAILED',
91
- }
92
-
93
- export interface HttpResponse {
94
- status: number;
95
- headers: Record<string, string>;
96
- body: unknown;
97
- }
98
-
99
- export type RunEvent =
100
- | { type: RunEventType.EVALUATED; results: EvaluationResult[] }
101
- | { type: RunEventType.HTTP_COMPLETED; response: HttpResponse }
102
- | { type: RunEventType.HOST_COMPLETED; output: unknown }
103
- | { type: RunEventType.WAIT_ELAPSED }
104
- | { type: RunEventType.EFFECT_FAILED; error: RuntimeErrorDetail };
105
-
106
- /** Position in the step tree: indexes from the root sequence down to the current step. */
107
- export interface Frame {
108
- /** Path of the sequence, e.g. `steps`, `steps[2].then`, `steps[1].cases[0].steps`. */
109
- sequencePath: string;
110
- index: number;
111
- }
112
-
113
- /**
114
- * Plain JSON: a host can persist it during a long wait (Flow) or keep it in memory (Reflex).
115
- * Only the interpreter reads or writes its fields.
116
- */
117
- export interface RunState {
118
- formatVersion: number;
119
- status: RunStatus;
120
- input: unknown;
121
- stack: Frame[];
122
- outputs: Record<string, { output: unknown }>;
123
- vars: Record<string, unknown>;
124
- /** The effect the run waits for, if any. */
125
- pending: Effect | null;
126
- counters: { steps: number; effects: number };
127
- /** Set once the run is SUCCEEDED (stop output) or FAILED. */
128
- outcome?: {
129
- status: StopStatus;
130
- output?: unknown;
131
- error?: RuntimeErrorDetail & { stepId?: string };
132
- };
133
- }
134
-
135
- export interface StepResult {
136
- state: RunState;
137
- /** Zero or one effect: the interpreter waits for one result at a time. */
138
- effects: Effect[];
139
- status: RunStatus;
140
- }
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
+ /** Longest `durationMs` a WAIT may ask for; the host applies the same bound to `until`. */
28
+ maxWaitMs: number;
29
+ }
30
+
31
+ export interface RunOptions {
32
+ catalog: HostActionCatalog;
33
+ /** What this run is actually allowed to do; compared to the definition's declarations. */
34
+ grants: AccessGrant[];
35
+ budget: RunBudget;
36
+ }
37
+
38
+ /** Data an evaluation sees: `input`, `steps.<id>.output`, `vars`. `fn.*` is added by the runner. */
39
+ export interface EvaluationContext {
40
+ input: unknown;
41
+ steps: Record<string, { output: unknown }>;
42
+ vars: Record<string, unknown>;
43
+ }
44
+
45
+ export enum EffectType {
46
+ EVALUATE = 'EVALUATE',
47
+ HTTP = 'HTTP',
48
+ HOST = 'HOST',
49
+ WAIT = 'WAIT',
50
+ LOG = 'LOG',
51
+ RESPOND = 'RESPOND',
52
+ }
53
+
54
+ /** Every expression and template of one step, sent to the code runner as one lot. */
55
+ export interface EvaluateEffect {
56
+ type: EffectType.EVALUATE;
57
+ stepId: string;
58
+ items: EvaluationItem[];
59
+ context: EvaluationContext;
60
+ }
61
+
62
+ export interface HttpRequest {
63
+ method: HttpMethod;
64
+ url: string;
65
+ headers: Record<string, string>;
66
+ body?: unknown;
67
+ timeoutMs?: number;
68
+ }
69
+
70
+ export interface HttpEffect {
71
+ type: EffectType.HTTP;
72
+ stepId: string;
73
+ request: HttpRequest;
74
+ }
75
+
76
+ export interface HostEffect {
77
+ type: EffectType.HOST;
78
+ stepId: string;
79
+ action: string;
80
+ input: Record<string, unknown>;
81
+ }
82
+
83
+ /**
84
+ * The interpreter has no clock: the host turns either field into a wake-up. It bounds
85
+ * `durationMs` against `RunBudget.maxWaitMs` itself, but cannot bound `until`: the host must
86
+ * refuse an `until` further from now than the same `maxWaitMs`.
87
+ */
88
+ export interface WaitEffect {
89
+ type: EffectType.WAIT;
90
+ stepId: string;
91
+ durationMs?: number;
92
+ /** ISO 8601. */
93
+ until?: string;
94
+ }
95
+
96
+ export interface LogEffect {
97
+ type: EffectType.LOG;
98
+ stepId: string;
99
+ level: LogLevel;
100
+ message: string;
101
+ data?: unknown;
102
+ }
103
+
104
+ /** The HTTP response a RESPOND step sends to the caller of the run. */
105
+ export interface HttpResponseToSend {
106
+ status: number;
107
+ headers: Record<string, string>;
108
+ body?: unknown;
109
+ }
110
+
111
+ export interface RespondEffect {
112
+ type: EffectType.RESPOND;
113
+ stepId: string;
114
+ response: HttpResponseToSend;
115
+ }
116
+
117
+ export type Effect =
118
+ | EvaluateEffect
119
+ | HttpEffect
120
+ | HostEffect
121
+ | WaitEffect
122
+ | LogEffect
123
+ | RespondEffect;
124
+
125
+ export enum RunEventType {
126
+ EVALUATED = 'EVALUATED',
127
+ HTTP_COMPLETED = 'HTTP_COMPLETED',
128
+ HOST_COMPLETED = 'HOST_COMPLETED',
129
+ WAIT_ELAPSED = 'WAIT_ELAPSED',
130
+ LOG_WRITTEN = 'LOG_WRITTEN',
131
+ RESPONSE_SENT = 'RESPONSE_SENT',
132
+ /**
133
+ * Answers the pending effect of a resumed run (any effect but EVALUATE): `output` becomes the
134
+ * output of the step that suspended.
135
+ */
136
+ RESUMED = 'RESUMED',
137
+ EFFECT_FAILED = 'EFFECT_FAILED',
138
+ }
139
+
140
+ export interface HttpResponse {
141
+ status: number;
142
+ headers: Record<string, string>;
143
+ body: unknown;
144
+ }
145
+
146
+ export type RunEvent =
147
+ | { type: RunEventType.EVALUATED; results: EvaluationResult[] }
148
+ | { type: RunEventType.HTTP_COMPLETED; response: HttpResponse }
149
+ | { type: RunEventType.HOST_COMPLETED; output: unknown }
150
+ | { type: RunEventType.WAIT_ELAPSED }
151
+ | { type: RunEventType.LOG_WRITTEN }
152
+ | { type: RunEventType.RESPONSE_SENT }
153
+ | { type: RunEventType.RESUMED; output: unknown }
154
+ | { type: RunEventType.EFFECT_FAILED; error: RuntimeErrorDetail };
155
+
156
+ /** Position in the step tree: indexes from the root sequence down to the current step. */
157
+ export interface Frame {
158
+ /** Path of the sequence, e.g. `steps`, `steps[2].then`, `steps[1].cases[0].steps`. */
159
+ sequencePath: string;
160
+ index: number;
161
+ }
162
+
163
+ /**
164
+ * Plain JSON: a host can persist it during a long wait (Flow) or keep it in memory (Reflex).
165
+ * Only the interpreter reads or writes its fields.
166
+ */
167
+ export interface RunState {
168
+ /** Layout of this object; see `CURRENT_STATE_VERSION`. */
169
+ stateVersion: number;
170
+ formatVersion: number;
171
+ status: RunStatus;
172
+ input: unknown;
173
+ stack: Frame[];
174
+ outputs: Record<string, { output: unknown }>;
175
+ vars: Record<string, unknown>;
176
+ /** The effect the run waits for, if any. */
177
+ pending: Effect | null;
178
+ counters: { steps: number; effects: number };
179
+ /** Set once the run is SUCCEEDED (stop output) or FAILED. */
180
+ outcome?: {
181
+ status: StopStatus;
182
+ output?: unknown;
183
+ error?: RuntimeErrorDetail & { stepId?: string };
184
+ };
185
+ }
186
+
187
+ export interface StepResult {
188
+ state: RunState;
189
+ /** Zero or one effect: the interpreter waits for one result at a time. */
190
+ effects: Effect[];
191
+ status: RunStatus;
192
+ }
@@ -1,74 +1,85 @@
1
- import type { RuntimeDefinition } from '../definition/definition.types';
2
- import { start, step } from './interpreter';
3
- import { EffectType, RunEventType } from './interpreter.types';
4
- import type {
5
- Effect,
6
- EvaluateEffect,
7
- HostEffect,
8
- HttpEffect,
9
- HttpResponse,
10
- RunEvent,
11
- RunOptions,
12
- RunState,
13
- StepResult,
14
- WaitEffect,
15
- } from './interpreter.types';
16
- import type { EvaluationResult } from '../runner-protocol/runner-protocol.types';
17
-
18
- type MaybePromise<T> = T | Promise<T>;
19
-
20
- /**
21
- * How a simulated host answers each effect. `evaluate` is mandatory: the package evaluates no
22
- * JavaScript. The other handlers default to an empty answer; `wait` always elapses at once.
23
- */
24
- export interface TestHostHandlers {
25
- evaluate: (effect: EvaluateEffect) => MaybePromise<EvaluationResult[]>;
26
- http?: (effect: HttpEffect) => MaybePromise<HttpResponse>;
27
- host?: (effect: HostEffect) => MaybePromise<unknown>;
28
- wait?: (effect: WaitEffect) => MaybePromise<void>;
29
- }
30
-
31
- export interface TestRunResult {
32
- state: RunState;
33
- /** Every effect the interpreter emitted, in order. */
34
- effects: Effect[];
35
- }
36
-
37
- const DEFAULT_HTTP_RESPONSE: HttpResponse = { status: 200, headers: {}, body: null };
38
-
39
- const answer = async (effect: Effect, handlers: TestHostHandlers): Promise<RunEvent> => {
40
- switch (effect.type) {
41
- case EffectType.EVALUATE:
42
- return { type: RunEventType.EVALUATED, results: await handlers.evaluate(effect) };
43
- case EffectType.HTTP:
44
- return {
45
- type: RunEventType.HTTP_COMPLETED,
46
- response: handlers.http ? await handlers.http(effect) : DEFAULT_HTTP_RESPONSE,
47
- };
48
- case EffectType.HOST:
49
- return {
50
- type: RunEventType.HOST_COMPLETED,
51
- output: handlers.host ? await handlers.host(effect) : null,
52
- };
53
- case EffectType.WAIT:
54
- await handlers.wait?.(effect);
55
- return { type: RunEventType.WAIT_ELAPSED };
56
- }
57
- };
58
-
59
- /** Runs a definition to its end, answering every effect with `handlers` and executing nothing. */
60
- export const runToCompletion = async (
61
- definition: RuntimeDefinition,
62
- input: unknown,
63
- options: RunOptions,
64
- handlers: TestHostHandlers,
65
- ): Promise<TestRunResult> => {
66
- const effects: Effect[] = [];
67
- let result: StepResult = start(definition, input, options);
68
- while (result.effects.length > 0) {
69
- const [effect] = result.effects;
70
- effects.push(effect);
71
- result = step(definition, result.state, await answer(effect, handlers), options);
72
- }
73
- return { state: result.state, effects };
74
- };
1
+ import type { RuntimeDefinition } from '../definition/definition.types';
2
+ import { start, step } from './interpreter';
3
+ import { EffectType, RunEventType } from './interpreter.types';
4
+ import type {
5
+ Effect,
6
+ EvaluateEffect,
7
+ HostEffect,
8
+ HttpEffect,
9
+ HttpResponse,
10
+ LogEffect,
11
+ RespondEffect,
12
+ RunEvent,
13
+ RunOptions,
14
+ RunState,
15
+ StepResult,
16
+ WaitEffect,
17
+ } from './interpreter.types';
18
+ import type { EvaluationResult } from '../runner-protocol/evaluation.types';
19
+
20
+ type MaybePromise<T> = T | Promise<T>;
21
+
22
+ /**
23
+ * How a simulated host answers each effect. `evaluate` is mandatory: the package evaluates no
24
+ * JavaScript. The other handlers default to an empty answer; `wait` always elapses at once,
25
+ * `log` and `respond` default to accepting the line or the response.
26
+ */
27
+ export interface TestHostHandlers {
28
+ evaluate: (effect: EvaluateEffect) => MaybePromise<EvaluationResult[]>;
29
+ http?: (effect: HttpEffect) => MaybePromise<HttpResponse>;
30
+ host?: (effect: HostEffect) => MaybePromise<unknown>;
31
+ wait?: (effect: WaitEffect) => MaybePromise<void>;
32
+ log?: (effect: LogEffect) => MaybePromise<void>;
33
+ respond?: (effect: RespondEffect) => MaybePromise<void>;
34
+ }
35
+
36
+ export interface TestRunResult {
37
+ state: RunState;
38
+ /** Every effect the interpreter emitted, in order. */
39
+ effects: Effect[];
40
+ }
41
+
42
+ const DEFAULT_HTTP_RESPONSE: HttpResponse = { status: 200, headers: {}, body: null };
43
+
44
+ const answer = async (effect: Effect, handlers: TestHostHandlers): Promise<RunEvent> => {
45
+ switch (effect.type) {
46
+ case EffectType.EVALUATE:
47
+ return { type: RunEventType.EVALUATED, results: await handlers.evaluate(effect) };
48
+ case EffectType.HTTP:
49
+ return {
50
+ type: RunEventType.HTTP_COMPLETED,
51
+ response: handlers.http ? await handlers.http(effect) : DEFAULT_HTTP_RESPONSE,
52
+ };
53
+ case EffectType.HOST:
54
+ return {
55
+ type: RunEventType.HOST_COMPLETED,
56
+ output: handlers.host ? await handlers.host(effect) : null,
57
+ };
58
+ case EffectType.WAIT:
59
+ await handlers.wait?.(effect);
60
+ return { type: RunEventType.WAIT_ELAPSED };
61
+ case EffectType.LOG:
62
+ await handlers.log?.(effect);
63
+ return { type: RunEventType.LOG_WRITTEN };
64
+ case EffectType.RESPOND:
65
+ await handlers.respond?.(effect);
66
+ return { type: RunEventType.RESPONSE_SENT };
67
+ }
68
+ };
69
+
70
+ /** Runs a definition to its end, answering every effect with `handlers` and executing nothing. */
71
+ export const runToCompletion = async (
72
+ definition: RuntimeDefinition,
73
+ input: unknown,
74
+ options: RunOptions,
75
+ handlers: TestHostHandlers,
76
+ ): Promise<TestRunResult> => {
77
+ const effects: Effect[] = [];
78
+ let result: StepResult = start(definition, input, options);
79
+ while (result.effects.length > 0) {
80
+ const [effect] = result.effects;
81
+ effects.push(effect);
82
+ result = step(definition, result.state, await answer(effect, handlers), options);
83
+ }
84
+ return { state: result.state, effects };
85
+ };
@@ -0,0 +1,21 @@
1
+ import type { RuntimeErrorDetail } from '../errors/runtime-error-code.enum';
2
+
3
+ export enum EvaluationKind {
4
+ /** A full JavaScript expression. */
5
+ EXPRESSION = 'EXPRESSION',
6
+ /** A string with `{{ … }}` placeholders; a lone placeholder keeps its value's type. */
7
+ TEMPLATE = 'TEMPLATE',
8
+ /** JavaScript statements; the value of their `return` is the result. */
9
+ CODE = 'CODE',
10
+ }
11
+
12
+ export interface EvaluationItem {
13
+ /** Unique within the step. */
14
+ id: string;
15
+ kind: EvaluationKind;
16
+ source: string;
17
+ }
18
+
19
+ export type EvaluationResult =
20
+ | { id: string; ok: true; value: unknown }
21
+ | { id: string; ok: false; error: RuntimeErrorDetail };