@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
@@ -0,0 +1,9 @@
1
+ // Bounds of the definition format, without Joi: the interpreter and the isolate engine bundle
2
+ // read them, and neither may pull Joi in (see `engine/run-in-isolate.ts`).
3
+
4
+ export const MAX_HTTP_TIMEOUT_MS = 30_000;
5
+ export const MAX_WAIT_DURATION_MS = 30 * 24 * 60 * 60 * 1000;
6
+
7
+ /** HTTP status range a RESPOND step may send. */
8
+ export const MIN_RESPONSE_STATUS = 100;
9
+ export const MAX_RESPONSE_STATUS = 599;
@@ -1,139 +1,172 @@
1
- import Joi from 'joi';
2
- import { AccessKind } from '../access/access.types';
3
- import { CURRENT_FORMAT_VERSION, HttpMethod, StepType, StopStatus } from './definition.types';
4
-
5
- export const MAX_HTTP_TIMEOUT_MS = 30_000;
6
- export const MAX_WAIT_DURATION_MS = 30 * 24 * 60 * 60 * 1000;
7
-
8
- /** Names that would hit `Object.prototype` when used as a key of `outputs` or `vars`. */
9
- const RESERVED_NAMES = ['__proto__', 'constructor', 'prototype'];
10
-
11
- const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
12
- const HOST_PATTERN = /^(\*\.)?[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
13
-
14
- let valueSourceCount = 0;
15
-
16
- /**
17
- * Literal JSON, `{ $expression }` (that key alone), arrays and objects of value sources. Joi
18
- * links resolve by id and ids must be unique inside one schema tree, hence a fresh id per use.
19
- */
20
- const valueSource = (): Joi.AlternativesSchema => {
21
- const id = `valueSource${valueSourceCount++}`;
22
- return Joi.alternatives()
23
- .try(
24
- Joi.string().allow(''),
25
- Joi.number(),
26
- Joi.boolean(),
27
- Joi.valid(null),
28
- Joi.array().items(Joi.link(`#${id}`)),
29
- Joi.object({ $expression: Joi.string().required() }),
30
- Joi.object().pattern(Joi.string().invalid('$expression'), Joi.link(`#${id}`)),
31
- )
32
- .id(id);
33
- };
34
-
35
- const valueMap = () => Joi.object().pattern(Joi.string(), valueSource());
36
-
37
- /** A number is bounded here; anything else (template, `$expression`) is checked at run time. */
38
- const durationSource = (): Joi.AlternativesSchema =>
39
- Joi.alternatives().try(
40
- Joi.number().integer().min(0).max(MAX_WAIT_DURATION_MS),
41
- Joi.string().allow(''),
42
- Joi.object({ $expression: Joi.string().required() }),
43
- );
44
-
45
- /** A literal must be a date; a template or `$expression` is checked at run time. */
46
- const untilSource = (): Joi.AlternativesSchema =>
47
- Joi.alternatives().try(
48
- Joi.string().pattern(/\{\{[\s\S]*?\}\}/),
49
- Joi.string().isoDate(),
50
- Joi.object({ $expression: Joi.string().required() }),
51
- );
52
-
53
- const stepBase = {
54
- id: Joi.string()
55
- .min(1)
56
- .invalid(...RESERVED_NAMES)
57
- .required(),
58
- name: Joi.string().allow(''),
59
- };
60
-
61
- // Children (`then`, `else`, `steps`, `default`) are only checked to be arrays here: the
62
- // validator walks them to give each nested step its own path.
63
- export const STEP_SCHEMAS: Record<StepType, Joi.ObjectSchema> = {
64
- [StepType.CONDITION]: Joi.object({
65
- ...stepBase,
66
- type: Joi.valid(StepType.CONDITION).required(),
67
- if: valueSource().required(),
68
- then: Joi.array().required(),
69
- else: Joi.array(),
70
- }),
71
- [StepType.SWITCH]: Joi.object({
72
- ...stepBase,
73
- type: Joi.valid(StepType.SWITCH).required(),
74
- value: valueSource().required(),
75
- cases: Joi.array()
76
- .min(1)
77
- .items(Joi.object({ match: valueSource().required(), steps: Joi.array().required() }))
78
- .required(),
79
- default: Joi.array(),
80
- }),
81
- [StepType.WAIT]: Joi.object({
82
- ...stepBase,
83
- type: Joi.valid(StepType.WAIT).required(),
84
- durationMs: durationSource(),
85
- until: untilSource(),
86
- }).xor('durationMs', 'until'),
87
- [StepType.SET]: Joi.object({
88
- ...stepBase,
89
- type: Joi.valid(StepType.SET).required(),
90
- variable: Joi.string()
91
- .pattern(IDENTIFIER)
92
- .invalid(...RESERVED_NAMES)
93
- .required(),
94
- value: valueSource().required(),
95
- }),
96
- [StepType.CODE]: Joi.object({
97
- ...stepBase,
98
- type: Joi.valid(StepType.CODE).required(),
99
- source: Joi.string().min(1).required(),
100
- }),
101
- [StepType.HTTP]: Joi.object({
102
- ...stepBase,
103
- type: Joi.valid(StepType.HTTP).required(),
104
- method: Joi.valid(...Object.values(HttpMethod)).required(),
105
- url: valueSource().required(),
106
- headers: valueMap(),
107
- body: valueSource(),
108
- timeoutMs: Joi.number().integer().min(1).max(MAX_HTTP_TIMEOUT_MS),
109
- }),
110
- [StepType.HOST]: Joi.object({
111
- ...stepBase,
112
- type: Joi.valid(StepType.HOST).required(),
113
- action: Joi.string().min(1).required(),
114
- input: valueMap().required(),
115
- }),
116
- [StepType.STOP]: Joi.object({
117
- ...stepBase,
118
- type: Joi.valid(StepType.STOP).required(),
119
- status: Joi.valid(...Object.values(StopStatus)).required(),
120
- output: valueSource(),
121
- }),
122
- };
123
-
124
- export const DEFINITION_SCHEMA = Joi.object({
125
- formatVersion: Joi.valid(CURRENT_FORMAT_VERSION).required(),
126
- accesses: Joi.array().required(),
127
- steps: Joi.array().required(),
128
- });
129
-
130
- export const ACCESS_SCHEMAS: Record<AccessKind, Joi.ObjectSchema> = {
131
- [AccessKind.HTTP]: Joi.object({
132
- kind: Joi.valid(AccessKind.HTTP).required(),
133
- hosts: Joi.array().items(Joi.string().pattern(HOST_PATTERN)).min(1).required(),
134
- }),
135
- [AccessKind.HOST_ACTION]: Joi.object({
136
- kind: Joi.valid(AccessKind.HOST_ACTION).required(),
137
- action: Joi.string().min(1).required(),
138
- }),
139
- };
1
+ import Joi from 'joi';
2
+ import { AccessKind } from '../access/access.types';
3
+ import {
4
+ MAX_HTTP_TIMEOUT_MS,
5
+ MAX_RESPONSE_STATUS,
6
+ MAX_WAIT_DURATION_MS,
7
+ MIN_RESPONSE_STATUS,
8
+ } from './definition.limits';
9
+ import {
10
+ CURRENT_FORMAT_VERSION,
11
+ HttpMethod,
12
+ LogLevel,
13
+ StepType,
14
+ StopStatus,
15
+ } from './definition.types';
16
+
17
+ export { MAX_HTTP_TIMEOUT_MS, MAX_WAIT_DURATION_MS };
18
+
19
+ /** Names that would hit `Object.prototype` when used as a key of `outputs` or `vars`. */
20
+ const RESERVED_NAMES = ['__proto__', 'constructor', 'prototype'];
21
+
22
+ const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
23
+ const HOST_PATTERN = /^(\*\.)?[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
24
+
25
+ let valueSourceCount = 0;
26
+
27
+ /**
28
+ * Literal JSON, `{ $expression }` (that key alone), arrays and objects of value sources. Joi
29
+ * links resolve by id and ids must be unique inside one schema tree, hence a fresh id per use.
30
+ */
31
+ const valueSource = (): Joi.AlternativesSchema => {
32
+ const id = `valueSource${valueSourceCount++}`;
33
+ return Joi.alternatives()
34
+ .try(
35
+ Joi.string().allow(''),
36
+ Joi.number(),
37
+ Joi.boolean(),
38
+ Joi.valid(null),
39
+ Joi.array().items(Joi.link(`#${id}`)),
40
+ Joi.object({ $expression: Joi.string().required() }),
41
+ Joi.object().pattern(Joi.string().invalid('$expression'), Joi.link(`#${id}`)),
42
+ )
43
+ .id(id);
44
+ };
45
+
46
+ const valueMap = () => Joi.object().pattern(Joi.string(), valueSource());
47
+
48
+ /** A number is bounded here; anything else (template, `$expression`) is checked at run time. */
49
+ const durationSource = (): Joi.AlternativesSchema =>
50
+ Joi.alternatives().try(
51
+ Joi.number().integer().min(0).max(MAX_WAIT_DURATION_MS),
52
+ Joi.string().allow(''),
53
+ Joi.object({ $expression: Joi.string().required() }),
54
+ );
55
+
56
+ /** A literal must be a date; a template or `$expression` is checked at run time. */
57
+ const untilSource = (): Joi.AlternativesSchema =>
58
+ Joi.alternatives().try(
59
+ Joi.string().pattern(/\{\{[\s\S]*?\}\}/),
60
+ Joi.string().isoDate(),
61
+ Joi.object({ $expression: Joi.string().required() }),
62
+ );
63
+
64
+ /** A literal must be an HTTP status; a template or `$expression` is checked at run time. */
65
+ const statusSource = (): Joi.AlternativesSchema =>
66
+ Joi.alternatives().try(
67
+ Joi.number().integer().min(MIN_RESPONSE_STATUS).max(MAX_RESPONSE_STATUS),
68
+ Joi.string().pattern(/\{\{[\s\S]*?\}\}/),
69
+ Joi.object({ $expression: Joi.string().required() }),
70
+ );
71
+
72
+ const stepBase = {
73
+ id: Joi.string()
74
+ .min(1)
75
+ .invalid(...RESERVED_NAMES)
76
+ .required(),
77
+ name: Joi.string().allow(''),
78
+ };
79
+
80
+ // Children (`then`, `else`, `steps`, `default`) are only checked to be arrays here: the
81
+ // validator walks them to give each nested step its own path.
82
+ export const STEP_SCHEMAS: Record<StepType, Joi.ObjectSchema> = {
83
+ [StepType.CONDITION]: Joi.object({
84
+ ...stepBase,
85
+ type: Joi.valid(StepType.CONDITION).required(),
86
+ if: valueSource().required(),
87
+ then: Joi.array().required(),
88
+ else: Joi.array(),
89
+ }),
90
+ [StepType.SWITCH]: Joi.object({
91
+ ...stepBase,
92
+ type: Joi.valid(StepType.SWITCH).required(),
93
+ value: valueSource().required(),
94
+ cases: Joi.array()
95
+ .min(1)
96
+ .items(Joi.object({ match: valueSource().required(), steps: Joi.array().required() }))
97
+ .required(),
98
+ default: Joi.array(),
99
+ }),
100
+ [StepType.WAIT]: Joi.object({
101
+ ...stepBase,
102
+ type: Joi.valid(StepType.WAIT).required(),
103
+ durationMs: durationSource(),
104
+ until: untilSource(),
105
+ }).xor('durationMs', 'until'),
106
+ [StepType.SET]: Joi.object({
107
+ ...stepBase,
108
+ type: Joi.valid(StepType.SET).required(),
109
+ variable: Joi.string()
110
+ .pattern(IDENTIFIER)
111
+ .invalid(...RESERVED_NAMES)
112
+ .required(),
113
+ value: valueSource().required(),
114
+ }),
115
+ [StepType.CODE]: Joi.object({
116
+ ...stepBase,
117
+ type: Joi.valid(StepType.CODE).required(),
118
+ source: Joi.string().min(1).required(),
119
+ }),
120
+ [StepType.HTTP]: Joi.object({
121
+ ...stepBase,
122
+ type: Joi.valid(StepType.HTTP).required(),
123
+ method: Joi.valid(...Object.values(HttpMethod)).required(),
124
+ url: valueSource().required(),
125
+ headers: valueMap(),
126
+ body: valueSource(),
127
+ timeoutMs: Joi.number().integer().min(1).max(MAX_HTTP_TIMEOUT_MS),
128
+ }),
129
+ [StepType.HOST]: Joi.object({
130
+ ...stepBase,
131
+ type: Joi.valid(StepType.HOST).required(),
132
+ action: Joi.string().min(1).required(),
133
+ input: valueMap().required(),
134
+ }),
135
+ [StepType.LOG]: Joi.object({
136
+ ...stepBase,
137
+ type: Joi.valid(StepType.LOG).required(),
138
+ level: Joi.valid(...Object.values(LogLevel)).required(),
139
+ message: valueSource().required(),
140
+ data: valueSource(),
141
+ }),
142
+ [StepType.RESPOND]: Joi.object({
143
+ ...stepBase,
144
+ type: Joi.valid(StepType.RESPOND).required(),
145
+ status: statusSource().required(),
146
+ headers: valueMap(),
147
+ body: valueSource(),
148
+ }),
149
+ [StepType.STOP]: Joi.object({
150
+ ...stepBase,
151
+ type: Joi.valid(StepType.STOP).required(),
152
+ status: Joi.valid(...Object.values(StopStatus)).required(),
153
+ output: valueSource(),
154
+ }),
155
+ };
156
+
157
+ export const DEFINITION_SCHEMA = Joi.object({
158
+ formatVersion: Joi.valid(CURRENT_FORMAT_VERSION).required(),
159
+ accesses: Joi.array().required(),
160
+ steps: Joi.array().required(),
161
+ });
162
+
163
+ export const ACCESS_SCHEMAS: Record<AccessKind, Joi.ObjectSchema> = {
164
+ [AccessKind.HTTP]: Joi.object({
165
+ kind: Joi.valid(AccessKind.HTTP).required(),
166
+ hosts: Joi.array().items(Joi.string().pattern(HOST_PATTERN)).min(1).required(),
167
+ }),
168
+ [AccessKind.HOST_ACTION]: Joi.object({
169
+ kind: Joi.valid(AccessKind.HOST_ACTION).required(),
170
+ action: Joi.string().min(1).required(),
171
+ }),
172
+ };
@@ -1,143 +1,171 @@
1
- import type { ObjectSchema } from 'joi';
2
- import type { AccessDeclaration } from '../access/access.types';
3
-
4
- export const CURRENT_FORMAT_VERSION = 1;
5
-
6
- /**
7
- * A full JavaScript expression, evaluated by the code runner. Any other string may embed
8
- * `{{ … }}` templates; an object whose only key is `$expression` is an expression.
9
- */
10
- export interface ExpressionValue {
11
- $expression: string;
12
- }
13
-
14
- export type ValueSource =
15
- | string
16
- | number
17
- | boolean
18
- | null
19
- | ExpressionValue
20
- | ValueSource[]
21
- | { [key: string]: ValueSource };
22
-
23
- export enum StepType {
24
- CONDITION = 'CONDITION',
25
- SWITCH = 'SWITCH',
26
- WAIT = 'WAIT',
27
- SET = 'SET',
28
- CODE = 'CODE',
29
- HTTP = 'HTTP',
30
- HOST = 'HOST',
31
- STOP = 'STOP',
32
- }
33
-
34
- export enum HttpMethod {
35
- GET = 'GET',
36
- POST = 'POST',
37
- PUT = 'PUT',
38
- PATCH = 'PATCH',
39
- DELETE = 'DELETE',
40
- }
41
-
42
- export enum StopStatus {
43
- SUCCEEDED = 'SUCCEEDED',
44
- FAILED = 'FAILED',
45
- }
46
-
47
- interface StepBase {
48
- /** Stable, unique in the definition; outputs are read as `steps.<id>.output`. */
49
- id: string;
50
- name?: string;
51
- }
52
-
53
- export interface ConditionStep extends StepBase {
54
- type: StepType.CONDITION;
55
- if: ValueSource;
56
- then: Step[];
57
- else?: Step[];
58
- }
59
-
60
- export interface SwitchCase {
61
- match: ValueSource;
62
- steps: Step[];
63
- }
64
-
65
- export interface SwitchStep extends StepBase {
66
- type: StepType.SWITCH;
67
- value: ValueSource;
68
- cases: SwitchCase[];
69
- default?: Step[];
70
- }
71
-
72
- /** Exactly one of `durationMs` and `until` (an ISO 8601 date); the host schedules the wake-up. */
73
- export interface WaitStep extends StepBase {
74
- type: StepType.WAIT;
75
- durationMs?: ValueSource;
76
- until?: ValueSource;
77
- }
78
-
79
- export interface SetStep extends StepBase {
80
- type: StepType.SET;
81
- /** Read back as `vars.<variable>`. */
82
- variable: string;
83
- value: ValueSource;
84
- }
85
-
86
- /** JavaScript statements; the value of their `return` becomes the step's output. */
87
- export interface CodeStep extends StepBase {
88
- type: StepType.CODE;
89
- source: string;
90
- }
91
-
92
- export interface HttpStep extends StepBase {
93
- type: StepType.HTTP;
94
- method: HttpMethod;
95
- url: ValueSource;
96
- headers?: Record<string, ValueSource>;
97
- body?: ValueSource;
98
- timeoutMs?: number;
99
- }
100
-
101
- export interface HostStep extends StepBase {
102
- type: StepType.HOST;
103
- action: string;
104
- input: Record<string, ValueSource>;
105
- }
106
-
107
- export interface StopStep extends StepBase {
108
- type: StepType.STOP;
109
- status: StopStatus;
110
- output?: ValueSource;
111
- }
112
-
113
- export type Step =
114
- | ConditionStep
115
- | SwitchStep
116
- | WaitStep
117
- | SetStep
118
- | CodeStep
119
- | HttpStep
120
- | HostStep
121
- | StopStep;
122
-
123
- export interface RuntimeDefinition {
124
- formatVersion: typeof CURRENT_FORMAT_VERSION;
125
- accesses: AccessDeclaration[];
126
- steps: Step[];
127
- }
128
-
129
- /** One action a product offers to its definitions (e-mail for Flow, Discord for Reflex…). */
130
- export interface HostActionDefinition {
131
- name: string;
132
- /** Validates the input once every expression in it is resolved. */
133
- inputSchema: ObjectSchema;
134
- }
135
-
136
- /** Supplied by each product; the core knows no Abyss service. */
137
- export type HostActionCatalog = ReadonlyMap<string, HostActionDefinition>;
138
-
139
- export interface DefinitionError {
140
- /** JSON path of the offending node, e.g. `steps[2].then[0].action`. */
141
- path: string;
142
- message: string;
143
- }
1
+ import type { ObjectSchema } from 'joi';
2
+ import type { AccessDeclaration } from '../access/access.types';
3
+
4
+ export const CURRENT_FORMAT_VERSION = 1;
5
+
6
+ /**
7
+ * A full JavaScript expression, evaluated by the code runner. Any other string may embed
8
+ * `{{ … }}` templates; an object whose only key is `$expression` is an expression.
9
+ */
10
+ export interface ExpressionValue {
11
+ $expression: string;
12
+ }
13
+
14
+ export type ValueSource =
15
+ | string
16
+ | number
17
+ | boolean
18
+ | null
19
+ | ExpressionValue
20
+ | ValueSource[]
21
+ | { [key: string]: ValueSource };
22
+
23
+ export enum StepType {
24
+ CONDITION = 'CONDITION',
25
+ SWITCH = 'SWITCH',
26
+ WAIT = 'WAIT',
27
+ SET = 'SET',
28
+ CODE = 'CODE',
29
+ HTTP = 'HTTP',
30
+ HOST = 'HOST',
31
+ /** Writes a log line kept with the run, through the host capability `log.write`. */
32
+ LOG = 'LOG',
33
+ /** Sends the HTTP response to the caller now, through `http.respond`; the run goes on. */
34
+ RESPOND = 'RESPOND',
35
+ STOP = 'STOP',
36
+ }
37
+
38
+ export enum LogLevel {
39
+ INFO = 'info',
40
+ WARN = 'warn',
41
+ ERROR = 'error',
42
+ }
43
+
44
+ export enum HttpMethod {
45
+ GET = 'GET',
46
+ POST = 'POST',
47
+ PUT = 'PUT',
48
+ PATCH = 'PATCH',
49
+ DELETE = 'DELETE',
50
+ }
51
+
52
+ export enum StopStatus {
53
+ SUCCEEDED = 'SUCCEEDED',
54
+ FAILED = 'FAILED',
55
+ }
56
+
57
+ interface StepBase {
58
+ /** Stable, unique in the definition; outputs are read as `steps.<id>.output`. */
59
+ id: string;
60
+ name?: string;
61
+ }
62
+
63
+ export interface ConditionStep extends StepBase {
64
+ type: StepType.CONDITION;
65
+ if: ValueSource;
66
+ then: Step[];
67
+ else?: Step[];
68
+ }
69
+
70
+ export interface SwitchCase {
71
+ match: ValueSource;
72
+ steps: Step[];
73
+ }
74
+
75
+ export interface SwitchStep extends StepBase {
76
+ type: StepType.SWITCH;
77
+ value: ValueSource;
78
+ cases: SwitchCase[];
79
+ default?: Step[];
80
+ }
81
+
82
+ /** Exactly one of `durationMs` and `until` (an ISO 8601 date); the host schedules the wake-up. */
83
+ export interface WaitStep extends StepBase {
84
+ type: StepType.WAIT;
85
+ durationMs?: ValueSource;
86
+ until?: ValueSource;
87
+ }
88
+
89
+ export interface SetStep extends StepBase {
90
+ type: StepType.SET;
91
+ /** Read back as `vars.<variable>`. */
92
+ variable: string;
93
+ value: ValueSource;
94
+ }
95
+
96
+ /** JavaScript statements; the value of their `return` becomes the step's output. */
97
+ export interface CodeStep extends StepBase {
98
+ type: StepType.CODE;
99
+ source: string;
100
+ }
101
+
102
+ export interface HttpStep extends StepBase {
103
+ type: StepType.HTTP;
104
+ method: HttpMethod;
105
+ url: ValueSource;
106
+ headers?: Record<string, ValueSource>;
107
+ body?: ValueSource;
108
+ timeoutMs?: number;
109
+ }
110
+
111
+ export interface HostStep extends StepBase {
112
+ type: StepType.HOST;
113
+ action: string;
114
+ input: Record<string, ValueSource>;
115
+ }
116
+
117
+ export interface LogStep extends StepBase {
118
+ type: StepType.LOG;
119
+ level: LogLevel;
120
+ /** Resolved to a string; a non-string value is written as its JSON. */
121
+ message: ValueSource;
122
+ data?: ValueSource;
123
+ }
124
+
125
+ export interface RespondStep extends StepBase {
126
+ type: StepType.RESPOND;
127
+ /** Resolved to an integer HTTP status, 100 to 599. */
128
+ status: ValueSource;
129
+ headers?: Record<string, ValueSource>;
130
+ body?: ValueSource;
131
+ }
132
+
133
+ export interface StopStep extends StepBase {
134
+ type: StepType.STOP;
135
+ status: StopStatus;
136
+ output?: ValueSource;
137
+ }
138
+
139
+ export type Step =
140
+ | ConditionStep
141
+ | SwitchStep
142
+ | WaitStep
143
+ | SetStep
144
+ | CodeStep
145
+ | HttpStep
146
+ | HostStep
147
+ | LogStep
148
+ | RespondStep
149
+ | StopStep;
150
+
151
+ export interface RuntimeDefinition {
152
+ formatVersion: typeof CURRENT_FORMAT_VERSION;
153
+ accesses: AccessDeclaration[];
154
+ steps: Step[];
155
+ }
156
+
157
+ /** One action a product offers to its definitions (e-mail for Flow, Discord for Reflex…). */
158
+ export interface HostActionDefinition {
159
+ name: string;
160
+ /** Validates the input once every expression in it is resolved. */
161
+ inputSchema: ObjectSchema;
162
+ }
163
+
164
+ /** Supplied by each product; the core knows no Abyss service. */
165
+ export type HostActionCatalog = ReadonlyMap<string, HostActionDefinition>;
166
+
167
+ export interface DefinitionError {
168
+ /** JSON path of the offending node, e.g. `steps[2].then[0].action`. */
169
+ path: string;
170
+ message: string;
171
+ }