@abyss-project/runtime 3.0.0 → 3.2.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 (41) hide show
  1. package/README.md +31 -0
  2. package/dist/discord/discord.types.d.ts +135 -0
  3. package/dist/discord/discord.types.js +59 -0
  4. package/dist/discord/discord.types.js.map +1 -0
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.js +1 -0
  7. package/dist/index.js.map +1 -1
  8. package/dist/runner-protocol/execution-protocol.types.js.map +1 -1
  9. package/package.json +1 -1
  10. package/scripts/build-engine.cjs +70 -70
  11. package/src/__tests__/engine-bundle.test.ts +121 -121
  12. package/src/cost/__tests__/execution-cost.test.ts +196 -196
  13. package/src/cost/execution-cost.ts +186 -186
  14. package/src/definition/__tests__/validate-definition.test.ts +354 -354
  15. package/src/definition/definition.limits.ts +9 -9
  16. package/src/definition/definition.schema.ts +172 -172
  17. package/src/definition/definition.types.ts +171 -171
  18. package/src/discord/discord.types.ts +181 -0
  19. package/src/engine/__tests__/run-in-isolate.test.ts +468 -468
  20. package/src/engine/engine-entry.ts +3 -3
  21. package/src/engine/engine-global.ts +2 -2
  22. package/src/engine/run-in-isolate.ts +404 -404
  23. package/src/errors/runtime-error-code.enum.ts +37 -37
  24. package/src/index.ts +31 -30
  25. package/src/interpreter/__tests__/interpreter.test.ts +854 -854
  26. package/src/interpreter/__tests__/log-respond-resume.test.ts +223 -223
  27. package/src/interpreter/interpreter.ts +461 -461
  28. package/src/interpreter/interpreter.types.ts +192 -192
  29. package/src/interpreter/test-host.ts +85 -85
  30. package/src/runner-protocol/evaluation.types.ts +21 -21
  31. package/src/runner-protocol/execution-protocol.types.ts +231 -225
  32. package/src/server/__tests__/code-runner-session.test.ts +311 -311
  33. package/src/server/code-runner-error.ts +12 -12
  34. package/src/server/code-runner-session.ts +310 -310
  35. package/src/server/index.ts +11 -11
  36. package/src/steps/__tests__/global-references.test.ts +133 -113
  37. package/src/steps/execute-step.ts +186 -186
  38. package/src/steps/global-references.ts +48 -48
  39. package/src/steps/step-access.ts +56 -56
  40. package/src/steps/step-fields.ts +87 -87
  41. package/src/steps/value-source.ts +69 -69
@@ -1,223 +1,223 @@
1
- import { CURRENT_FORMAT_VERSION, LogLevel, StepType } from '../../definition/definition.types';
2
- import type { RuntimeDefinition, Step } from '../../definition/definition.types';
3
- import { RuntimeErrorCode } from '../../errors/runtime-error-code.enum';
4
- import { EvaluationKind } from '../../runner-protocol/evaluation.types';
5
- import { InterpreterError } from '../interpreter-error';
6
- import { start, step } from '../interpreter';
7
- import { CURRENT_STATE_VERSION, EffectType, RunEventType, RunStatus } from '../interpreter.types';
8
- import type { RunOptions, RunState } from '../interpreter.types';
9
- import { runToCompletion } from '../test-host';
10
-
11
- const options = (overrides: Partial<RunOptions['budget']> = {}): RunOptions => ({
12
- catalog: new Map(),
13
- grants: [],
14
- budget: { maxSteps: 100, maxEffects: 100, maxWaitMs: 1_000_000, ...overrides },
15
- });
16
-
17
- const definitionOf = (steps: Step[]): RuntimeDefinition => ({
18
- formatVersion: CURRENT_FORMAT_VERSION,
19
- accesses: [],
20
- steps,
21
- });
22
-
23
- const thrownBy = (fn: () => unknown): unknown => {
24
- try {
25
- fn();
26
- } catch (error) {
27
- return error;
28
- }
29
- throw new Error('expected a throw');
30
- };
31
-
32
- describe('LOG step', () => {
33
- const log: Step = {
34
- id: 'log',
35
- type: StepType.LOG,
36
- level: LogLevel.WARN,
37
- message: 'order {{ input.id }}',
38
- data: { total: { $expression: 'input.total' }, fixed: 1 },
39
- };
40
-
41
- it('evaluates its message and data, then emits a LOG effect', () => {
42
- const definition = definitionOf([log]);
43
- const first = start(definition, { id: 7, total: 12 }, options());
44
- expect(first.effects).toEqual([
45
- expect.objectContaining({
46
- type: EffectType.EVALUATE,
47
- items: [
48
- { id: 'message', kind: EvaluationKind.TEMPLATE, source: 'order {{ input.id }}' },
49
- { id: 'data.total', kind: EvaluationKind.EXPRESSION, source: 'input.total' },
50
- ],
51
- }),
52
- ]);
53
- const second = step(
54
- definition,
55
- first.state,
56
- {
57
- type: RunEventType.EVALUATED,
58
- results: [
59
- { id: 'message', ok: true, value: 'order 7' },
60
- { id: 'data.total', ok: true, value: 12 },
61
- ],
62
- },
63
- options(),
64
- );
65
- expect(second.effects).toEqual([
66
- {
67
- type: EffectType.LOG,
68
- stepId: 'log',
69
- level: LogLevel.WARN,
70
- message: 'order 7',
71
- data: { total: 12, fixed: 1 },
72
- },
73
- ]);
74
- const done = step(definition, second.state, { type: RunEventType.LOG_WRITTEN }, options());
75
- expect(done.status).toBe(RunStatus.SUCCEEDED);
76
- expect(done.state.outputs.log).toEqual({ output: null });
77
- });
78
-
79
- it('writes a non-string message as its JSON and omits absent data', () => {
80
- const definition = definitionOf([
81
- { id: 'log', type: StepType.LOG, level: LogLevel.INFO, message: { $expression: 'input' } },
82
- ]);
83
- const first = start(definition, null, options());
84
- const second = step(
85
- definition,
86
- first.state,
87
- { type: RunEventType.EVALUATED, results: [{ id: 'message', ok: true, value: { a: 1 } }] },
88
- options(),
89
- );
90
- expect(second.effects).toEqual([
91
- { type: EffectType.LOG, stepId: 'log', level: LogLevel.INFO, message: '{"a":1}' },
92
- ]);
93
- });
94
-
95
- it('counts against the effect budget', () => {
96
- const definition = definitionOf([
97
- { id: 'a', type: StepType.LOG, level: LogLevel.INFO, message: 'a' },
98
- { id: 'b', type: StepType.LOG, level: LogLevel.INFO, message: 'b' },
99
- ]);
100
- const first = start(definition, null, options({ maxEffects: 1 }));
101
- const second = step(
102
- definition,
103
- first.state,
104
- { type: RunEventType.LOG_WRITTEN },
105
- options({ maxEffects: 1 }),
106
- );
107
- expect(second.status).toBe(RunStatus.FAILED);
108
- expect(second.state.outcome?.error).toEqual(
109
- expect.objectContaining({ code: RuntimeErrorCode.BUDGET_EXCEEDED, stepId: 'b' }),
110
- );
111
- });
112
- });
113
-
114
- describe('RESPOND step', () => {
115
- it('emits the response with string headers, and the run goes on after it', async () => {
116
- const definition = definitionOf([
117
- {
118
- id: 'reply',
119
- type: StepType.RESPOND,
120
- status: 201,
121
- headers: { 'X-Count': 3, 'Content-Type': 'application/json' },
122
- body: { ok: true },
123
- },
124
- { id: 'after', type: StepType.SET, variable: 'x', value: 1 },
125
- ]);
126
- const responses: unknown[] = [];
127
- const { state, effects } = await runToCompletion(definition, null, options(), {
128
- evaluate: () => [],
129
- respond: (effect) => {
130
- responses.push(effect.response);
131
- },
132
- });
133
- expect(effects.map((effect) => effect.type)).toEqual([EffectType.RESPOND]);
134
- expect(responses).toEqual([
135
- {
136
- status: 201,
137
- headers: { 'X-Count': '3', 'Content-Type': 'application/json' },
138
- body: { ok: true },
139
- },
140
- ]);
141
- expect(state.status).toBe(RunStatus.SUCCEEDED);
142
- expect(state.outputs.reply).toEqual({ output: null });
143
- expect(state.vars.x).toBe(1);
144
- });
145
-
146
- it('fails the step when the status does not resolve to an HTTP status', () => {
147
- const definition = definitionOf([
148
- { id: 'reply', type: StepType.RESPOND, status: { $expression: 'input.status' } },
149
- ]);
150
- const first = start(definition, null, options());
151
- const second = step(
152
- definition,
153
- first.state,
154
- { type: RunEventType.EVALUATED, results: [{ id: 'status', ok: true, value: 700 }] },
155
- options(),
156
- );
157
- expect(second.status).toBe(RunStatus.FAILED);
158
- expect(second.state.outcome?.error).toEqual(
159
- expect.objectContaining({ code: RuntimeErrorCode.EXPRESSION_ERROR, stepId: 'reply' }),
160
- );
161
- });
162
-
163
- it('refuses a RESPONSE_SENT event for another pending effect', () => {
164
- const definition = definitionOf([{ id: 'w', type: StepType.WAIT, durationMs: 10 }]);
165
- const first = start(definition, null, options());
166
- const error = thrownBy(() =>
167
- step(definition, first.state, { type: RunEventType.RESPONSE_SENT }, options()),
168
- );
169
- expect(error).toBeInstanceOf(InterpreterError);
170
- });
171
- });
172
-
173
- describe('resume and state version', () => {
174
- const definition = definitionOf([
175
- { id: 'approve', type: StepType.WAIT, durationMs: 500 },
176
- {
177
- id: 'after',
178
- type: StepType.SET,
179
- variable: 'v',
180
- value: { $expression: 'steps.approve.output' },
181
- },
182
- ]);
183
-
184
- it('stamps every new state with the current state version', () => {
185
- expect(CURRENT_STATE_VERSION).toBe(1);
186
- expect(start(definition, null, options()).state.stateVersion).toBe(CURRENT_STATE_VERSION);
187
- });
188
-
189
- it('gives a RESUMED output to the step that suspended', () => {
190
- const first = start(definition, null, options());
191
- const persisted = JSON.parse(JSON.stringify(first.state)) as RunState;
192
- const resumed = step(
193
- definition,
194
- persisted,
195
- { type: RunEventType.RESUMED, output: { approved: true } },
196
- options(),
197
- );
198
- expect(resumed.state.outputs.approve).toEqual({ output: { approved: true } });
199
- expect(resumed.effects).toEqual([
200
- expect.objectContaining({ type: EffectType.EVALUATE, stepId: 'after' }),
201
- ]);
202
- });
203
-
204
- it('refuses RESUMED while an evaluation is pending', () => {
205
- const evaluating = definitionOf([{ id: 'c', type: StepType.CODE, source: 'return 1;' }]);
206
- const first = start(evaluating, null, options());
207
- const error = thrownBy(() =>
208
- step(evaluating, first.state, { type: RunEventType.RESUMED, output: 1 }, options()),
209
- );
210
- expect(error).toBeInstanceOf(InterpreterError);
211
- expect((error as InterpreterError).code).toBe(RuntimeErrorCode.INVALID_REQUEST);
212
- });
213
-
214
- it('refuses a state of an unknown version', () => {
215
- const first = start(definition, null, options());
216
- const future = { ...first.state, stateVersion: CURRENT_STATE_VERSION + 1 };
217
- const error = thrownBy(() =>
218
- step(definition, future, { type: RunEventType.WAIT_ELAPSED }, options()),
219
- );
220
- expect(error).toBeInstanceOf(InterpreterError);
221
- expect((error as InterpreterError).code).toBe(RuntimeErrorCode.STATE_VERSION_UNSUPPORTED);
222
- });
223
- });
1
+ import { CURRENT_FORMAT_VERSION, LogLevel, StepType } from '../../definition/definition.types';
2
+ import type { RuntimeDefinition, Step } from '../../definition/definition.types';
3
+ import { RuntimeErrorCode } from '../../errors/runtime-error-code.enum';
4
+ import { EvaluationKind } from '../../runner-protocol/evaluation.types';
5
+ import { InterpreterError } from '../interpreter-error';
6
+ import { start, step } from '../interpreter';
7
+ import { CURRENT_STATE_VERSION, EffectType, RunEventType, RunStatus } from '../interpreter.types';
8
+ import type { RunOptions, RunState } from '../interpreter.types';
9
+ import { runToCompletion } from '../test-host';
10
+
11
+ const options = (overrides: Partial<RunOptions['budget']> = {}): RunOptions => ({
12
+ catalog: new Map(),
13
+ grants: [],
14
+ budget: { maxSteps: 100, maxEffects: 100, maxWaitMs: 1_000_000, ...overrides },
15
+ });
16
+
17
+ const definitionOf = (steps: Step[]): RuntimeDefinition => ({
18
+ formatVersion: CURRENT_FORMAT_VERSION,
19
+ accesses: [],
20
+ steps,
21
+ });
22
+
23
+ const thrownBy = (fn: () => unknown): unknown => {
24
+ try {
25
+ fn();
26
+ } catch (error) {
27
+ return error;
28
+ }
29
+ throw new Error('expected a throw');
30
+ };
31
+
32
+ describe('LOG step', () => {
33
+ const log: Step = {
34
+ id: 'log',
35
+ type: StepType.LOG,
36
+ level: LogLevel.WARN,
37
+ message: 'order {{ input.id }}',
38
+ data: { total: { $expression: 'input.total' }, fixed: 1 },
39
+ };
40
+
41
+ it('evaluates its message and data, then emits a LOG effect', () => {
42
+ const definition = definitionOf([log]);
43
+ const first = start(definition, { id: 7, total: 12 }, options());
44
+ expect(first.effects).toEqual([
45
+ expect.objectContaining({
46
+ type: EffectType.EVALUATE,
47
+ items: [
48
+ { id: 'message', kind: EvaluationKind.TEMPLATE, source: 'order {{ input.id }}' },
49
+ { id: 'data.total', kind: EvaluationKind.EXPRESSION, source: 'input.total' },
50
+ ],
51
+ }),
52
+ ]);
53
+ const second = step(
54
+ definition,
55
+ first.state,
56
+ {
57
+ type: RunEventType.EVALUATED,
58
+ results: [
59
+ { id: 'message', ok: true, value: 'order 7' },
60
+ { id: 'data.total', ok: true, value: 12 },
61
+ ],
62
+ },
63
+ options(),
64
+ );
65
+ expect(second.effects).toEqual([
66
+ {
67
+ type: EffectType.LOG,
68
+ stepId: 'log',
69
+ level: LogLevel.WARN,
70
+ message: 'order 7',
71
+ data: { total: 12, fixed: 1 },
72
+ },
73
+ ]);
74
+ const done = step(definition, second.state, { type: RunEventType.LOG_WRITTEN }, options());
75
+ expect(done.status).toBe(RunStatus.SUCCEEDED);
76
+ expect(done.state.outputs.log).toEqual({ output: null });
77
+ });
78
+
79
+ it('writes a non-string message as its JSON and omits absent data', () => {
80
+ const definition = definitionOf([
81
+ { id: 'log', type: StepType.LOG, level: LogLevel.INFO, message: { $expression: 'input' } },
82
+ ]);
83
+ const first = start(definition, null, options());
84
+ const second = step(
85
+ definition,
86
+ first.state,
87
+ { type: RunEventType.EVALUATED, results: [{ id: 'message', ok: true, value: { a: 1 } }] },
88
+ options(),
89
+ );
90
+ expect(second.effects).toEqual([
91
+ { type: EffectType.LOG, stepId: 'log', level: LogLevel.INFO, message: '{"a":1}' },
92
+ ]);
93
+ });
94
+
95
+ it('counts against the effect budget', () => {
96
+ const definition = definitionOf([
97
+ { id: 'a', type: StepType.LOG, level: LogLevel.INFO, message: 'a' },
98
+ { id: 'b', type: StepType.LOG, level: LogLevel.INFO, message: 'b' },
99
+ ]);
100
+ const first = start(definition, null, options({ maxEffects: 1 }));
101
+ const second = step(
102
+ definition,
103
+ first.state,
104
+ { type: RunEventType.LOG_WRITTEN },
105
+ options({ maxEffects: 1 }),
106
+ );
107
+ expect(second.status).toBe(RunStatus.FAILED);
108
+ expect(second.state.outcome?.error).toEqual(
109
+ expect.objectContaining({ code: RuntimeErrorCode.BUDGET_EXCEEDED, stepId: 'b' }),
110
+ );
111
+ });
112
+ });
113
+
114
+ describe('RESPOND step', () => {
115
+ it('emits the response with string headers, and the run goes on after it', async () => {
116
+ const definition = definitionOf([
117
+ {
118
+ id: 'reply',
119
+ type: StepType.RESPOND,
120
+ status: 201,
121
+ headers: { 'X-Count': 3, 'Content-Type': 'application/json' },
122
+ body: { ok: true },
123
+ },
124
+ { id: 'after', type: StepType.SET, variable: 'x', value: 1 },
125
+ ]);
126
+ const responses: unknown[] = [];
127
+ const { state, effects } = await runToCompletion(definition, null, options(), {
128
+ evaluate: () => [],
129
+ respond: (effect) => {
130
+ responses.push(effect.response);
131
+ },
132
+ });
133
+ expect(effects.map((effect) => effect.type)).toEqual([EffectType.RESPOND]);
134
+ expect(responses).toEqual([
135
+ {
136
+ status: 201,
137
+ headers: { 'X-Count': '3', 'Content-Type': 'application/json' },
138
+ body: { ok: true },
139
+ },
140
+ ]);
141
+ expect(state.status).toBe(RunStatus.SUCCEEDED);
142
+ expect(state.outputs.reply).toEqual({ output: null });
143
+ expect(state.vars.x).toBe(1);
144
+ });
145
+
146
+ it('fails the step when the status does not resolve to an HTTP status', () => {
147
+ const definition = definitionOf([
148
+ { id: 'reply', type: StepType.RESPOND, status: { $expression: 'input.status' } },
149
+ ]);
150
+ const first = start(definition, null, options());
151
+ const second = step(
152
+ definition,
153
+ first.state,
154
+ { type: RunEventType.EVALUATED, results: [{ id: 'status', ok: true, value: 700 }] },
155
+ options(),
156
+ );
157
+ expect(second.status).toBe(RunStatus.FAILED);
158
+ expect(second.state.outcome?.error).toEqual(
159
+ expect.objectContaining({ code: RuntimeErrorCode.EXPRESSION_ERROR, stepId: 'reply' }),
160
+ );
161
+ });
162
+
163
+ it('refuses a RESPONSE_SENT event for another pending effect', () => {
164
+ const definition = definitionOf([{ id: 'w', type: StepType.WAIT, durationMs: 10 }]);
165
+ const first = start(definition, null, options());
166
+ const error = thrownBy(() =>
167
+ step(definition, first.state, { type: RunEventType.RESPONSE_SENT }, options()),
168
+ );
169
+ expect(error).toBeInstanceOf(InterpreterError);
170
+ });
171
+ });
172
+
173
+ describe('resume and state version', () => {
174
+ const definition = definitionOf([
175
+ { id: 'approve', type: StepType.WAIT, durationMs: 500 },
176
+ {
177
+ id: 'after',
178
+ type: StepType.SET,
179
+ variable: 'v',
180
+ value: { $expression: 'steps.approve.output' },
181
+ },
182
+ ]);
183
+
184
+ it('stamps every new state with the current state version', () => {
185
+ expect(CURRENT_STATE_VERSION).toBe(1);
186
+ expect(start(definition, null, options()).state.stateVersion).toBe(CURRENT_STATE_VERSION);
187
+ });
188
+
189
+ it('gives a RESUMED output to the step that suspended', () => {
190
+ const first = start(definition, null, options());
191
+ const persisted = JSON.parse(JSON.stringify(first.state)) as RunState;
192
+ const resumed = step(
193
+ definition,
194
+ persisted,
195
+ { type: RunEventType.RESUMED, output: { approved: true } },
196
+ options(),
197
+ );
198
+ expect(resumed.state.outputs.approve).toEqual({ output: { approved: true } });
199
+ expect(resumed.effects).toEqual([
200
+ expect.objectContaining({ type: EffectType.EVALUATE, stepId: 'after' }),
201
+ ]);
202
+ });
203
+
204
+ it('refuses RESUMED while an evaluation is pending', () => {
205
+ const evaluating = definitionOf([{ id: 'c', type: StepType.CODE, source: 'return 1;' }]);
206
+ const first = start(evaluating, null, options());
207
+ const error = thrownBy(() =>
208
+ step(evaluating, first.state, { type: RunEventType.RESUMED, output: 1 }, options()),
209
+ );
210
+ expect(error).toBeInstanceOf(InterpreterError);
211
+ expect((error as InterpreterError).code).toBe(RuntimeErrorCode.INVALID_REQUEST);
212
+ });
213
+
214
+ it('refuses a state of an unknown version', () => {
215
+ const first = start(definition, null, options());
216
+ const future = { ...first.state, stateVersion: CURRENT_STATE_VERSION + 1 };
217
+ const error = thrownBy(() =>
218
+ step(definition, future, { type: RunEventType.WAIT_ELAPSED }, options()),
219
+ );
220
+ expect(error).toBeInstanceOf(InterpreterError);
221
+ expect((error as InterpreterError).code).toBe(RuntimeErrorCode.STATE_VERSION_UNSUPPORTED);
222
+ });
223
+ });