@abyss-project/runtime 3.7.0 → 3.8.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 (55) hide show
  1. package/.eslintrc.js +32 -32
  2. package/.prettierrc +5 -5
  3. package/README.md +88 -88
  4. package/dist/engine/engine-source.js +1 -1
  5. package/dist/engine/run-in-isolate.js +32 -2
  6. package/dist/engine/run-in-isolate.js.map +1 -1
  7. package/dist/runner-protocol/execution-protocol.types.d.ts +2 -0
  8. package/dist/runner-protocol/execution-protocol.types.js +2 -1
  9. package/dist/runner-protocol/execution-protocol.types.js.map +1 -1
  10. package/jest.config.ts +11 -11
  11. package/package.json +10 -15
  12. package/scripts/build-engine.cjs +70 -70
  13. package/src/__tests__/engine-bundle.test.ts +121 -121
  14. package/src/__tests__/surface.test.ts +12 -1
  15. package/src/access/__tests__/host-match.test.ts +24 -24
  16. package/src/access/access.types.ts +25 -25
  17. package/src/access/host-match.ts +13 -13
  18. package/src/cost/__tests__/execution-cost.test.ts +196 -196
  19. package/src/cost/execution-cost.ts +186 -186
  20. package/src/definition/__tests__/validate-definition.test.ts +354 -354
  21. package/src/definition/definition.limits.ts +9 -9
  22. package/src/definition/definition.schema.ts +172 -172
  23. package/src/definition/definition.types.ts +171 -171
  24. package/src/definition/validate-definition.ts +206 -206
  25. package/src/definition/walk-steps.ts +44 -44
  26. package/src/discord/__tests__/discord-event-input.test.ts +89 -89
  27. package/src/discord/discord.types.ts +639 -639
  28. package/src/engine/__tests__/run-in-isolate.test.ts +56 -5
  29. package/src/engine/engine-entry.ts +3 -3
  30. package/src/engine/engine-global.ts +2 -2
  31. package/src/engine/run-in-isolate.ts +47 -4
  32. package/src/errors/runtime-error-code.enum.ts +37 -37
  33. package/src/index.ts +32 -32
  34. package/src/interpreter/__tests__/interpreter.test.ts +854 -854
  35. package/src/interpreter/__tests__/log-respond-resume.test.ts +271 -271
  36. package/src/interpreter/interpreter-error.ts +12 -12
  37. package/src/interpreter/interpreter.ts +483 -483
  38. package/src/interpreter/interpreter.types.ts +204 -204
  39. package/src/interpreter/sequence-path.ts +67 -67
  40. package/src/interpreter/test-host.ts +85 -85
  41. package/src/runner-protocol/evaluation.types.ts +21 -21
  42. package/src/runner-protocol/execution-protocol.types.ts +20 -3
  43. package/src/schedule/schedule.types.ts +100 -100
  44. package/src/server/__tests__/code-runner-session.test.ts +311 -311
  45. package/src/server/code-runner-error.ts +12 -12
  46. package/src/server/code-runner-session.ts +310 -310
  47. package/src/server/index.ts +11 -11
  48. package/src/steps/__tests__/global-references.test.ts +133 -133
  49. package/src/steps/execute-step.ts +186 -186
  50. package/src/steps/global-references.ts +48 -48
  51. package/src/steps/step-access.ts +56 -56
  52. package/src/steps/step-fields.ts +87 -87
  53. package/src/steps/value-source.ts +69 -69
  54. package/tsconfig.check.json +15 -15
  55. package/tsconfig.json +35 -35
@@ -1,206 +1,206 @@
1
- import type Joi from 'joi';
2
- import { AccessKind } from '../access/access.types';
3
- import type { AccessDeclaration } from '../access/access.types';
4
- import { hostMatches } from '../access/host-match';
5
- import { ACCESS_SCHEMAS, DEFINITION_SCHEMA, STEP_SCHEMAS } from './definition.schema';
6
- import { CURRENT_FORMAT_VERSION, StepType } from './definition.types';
7
- import type { DefinitionError, HostActionCatalog, RuntimeDefinition } from './definition.types';
8
- import { walkRawSteps } from './walk-steps';
9
-
10
- export type ValidationResult =
11
- | { ok: true; definition: RuntimeDefinition }
12
- | { ok: false; errors: DefinitionError[] };
13
-
14
- const JOI_OPTIONS: Joi.ValidationOptions = { abortEarly: false, convert: false };
15
-
16
- const isRecord = (value: unknown): value is Record<string, unknown> =>
17
- typeof value === 'object' && value !== null && !Array.isArray(value);
18
-
19
- const joinPath = (base: string, segments: ReadonlyArray<string | number>): string =>
20
- segments.reduce<string>((path, segment) => {
21
- if (typeof segment === 'number') {
22
- return `${path}[${segment}]`;
23
- }
24
- return path === '' ? segment : `${path}.${segment}`;
25
- }, base);
26
-
27
- const joiErrors = (
28
- schema: Joi.Schema,
29
- value: unknown,
30
- basePath: string,
31
- errors: DefinitionError[],
32
- ): void => {
33
- const { error } = schema.validate(value, JOI_OPTIONS);
34
- error?.details.forEach((detail) =>
35
- errors.push({ path: joinPath(basePath, detail.path), message: detail.message }),
36
- );
37
- };
38
-
39
- const isAccessKind = (value: unknown): value is AccessKind =>
40
- Object.values(AccessKind).includes(value as AccessKind);
41
-
42
- const isStepType = (value: unknown): value is StepType =>
43
- Object.values(StepType).includes(value as StepType);
44
-
45
- /** Literal keys of an action's input schema, or `null` when it accepts any key. */
46
- const allowedInputKeys = (catalog: HostActionCatalog, action: string): Set<string> | null => {
47
- const description = catalog.get(action)?.inputSchema.describe() as
48
- | { keys?: Record<string, unknown>; flags?: { unknown?: boolean }; patterns?: unknown }
49
- | undefined;
50
- if (!description?.keys || description.flags?.unknown || description.patterns) {
51
- return null;
52
- }
53
- return new Set(Object.keys(description.keys));
54
- };
55
-
56
- const checkAccesses = (accesses: unknown, errors: DefinitionError[]): AccessDeclaration[] => {
57
- if (!Array.isArray(accesses)) {
58
- return [];
59
- }
60
- const valid: AccessDeclaration[] = [];
61
- accesses.forEach((access, index) => {
62
- const path = `accesses[${index}]`;
63
- const kind = isRecord(access) ? access.kind : undefined;
64
- if (!isAccessKind(kind)) {
65
- errors.push({ path: `${path}.kind`, message: '"kind" must be one of [HTTP, HOST_ACTION]' });
66
- return;
67
- }
68
- const before = errors.length;
69
- joiErrors(ACCESS_SCHEMAS[kind], access, path, errors);
70
- if (errors.length === before) {
71
- valid.push(access as AccessDeclaration);
72
- }
73
- });
74
- return valid;
75
- };
76
-
77
- interface StepChecks {
78
- catalog: HostActionCatalog;
79
- accesses: AccessDeclaration[];
80
- errors: DefinitionError[];
81
- }
82
-
83
- const checkHttpAccess = (step: Record<string, unknown>, path: string, checks: StepChecks): void => {
84
- const httpAccesses = checks.accesses.filter((access) => access.kind === AccessKind.HTTP);
85
- if (httpAccesses.length === 0) {
86
- checks.errors.push({
87
- path,
88
- message: 'an HTTP step requires an access declaration of kind HTTP',
89
- });
90
- return;
91
- }
92
- const { url } = step;
93
- if (typeof url !== 'string' || url.includes('{{')) {
94
- return;
95
- }
96
- let parsed: URL;
97
- try {
98
- parsed = new URL(url);
99
- } catch {
100
- checks.errors.push({ path: `${path}.url`, message: '"url" must be an absolute http(s) URL' });
101
- return;
102
- }
103
- if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
104
- checks.errors.push({ path: `${path}.url`, message: '"url" must be an absolute http(s) URL' });
105
- return;
106
- }
107
- const covered = httpAccesses.some(
108
- (access) =>
109
- access.kind === AccessKind.HTTP &&
110
- access.hosts.some((pattern) => hostMatches(parsed.hostname, pattern)),
111
- );
112
- if (!covered) {
113
- checks.errors.push({
114
- path,
115
- message: `host "${parsed.hostname}" is not covered by any HTTP access declaration`,
116
- });
117
- }
118
- };
119
-
120
- const checkHost = (step: Record<string, unknown>, path: string, checks: StepChecks): void => {
121
- const { action, input } = step;
122
- if (typeof action !== 'string' || action === '') {
123
- return;
124
- }
125
- if (!checks.catalog.has(action)) {
126
- checks.errors.push({
127
- path: `${path}.action`,
128
- message: `action "${action}" is not in the host catalog`,
129
- });
130
- } else if (isRecord(input)) {
131
- const allowed = allowedInputKeys(checks.catalog, action);
132
- if (allowed) {
133
- Object.keys(input)
134
- .filter((key) => !allowed.has(key))
135
- .forEach((key) =>
136
- checks.errors.push({
137
- path: `${path}.input.${key}`,
138
- message: `"${key}" is not an input of action "${action}"`,
139
- }),
140
- );
141
- }
142
- }
143
- const declared = checks.accesses.some(
144
- (access) => access.kind === AccessKind.HOST_ACTION && access.action === action,
145
- );
146
- if (!declared) {
147
- checks.errors.push({
148
- path,
149
- message: `action "${action}" requires an access declaration of kind HOST_ACTION`,
150
- });
151
- }
152
- };
153
-
154
- const checkStep = (node: unknown, path: string, ids: Set<string>, checks: StepChecks): void => {
155
- if (!isRecord(node)) {
156
- checks.errors.push({ path, message: 'a step must be an object' });
157
- return;
158
- }
159
- if (!isStepType(node.type)) {
160
- checks.errors.push({
161
- path: `${path}.type`,
162
- message: `"type" must be one of [${Object.values(StepType).join(', ')}]`,
163
- });
164
- return;
165
- }
166
- joiErrors(STEP_SCHEMAS[node.type], node, path, checks.errors);
167
- if (typeof node.id === 'string' && node.id !== '') {
168
- if (ids.has(node.id)) {
169
- checks.errors.push({ path: `${path}.id`, message: `id "${node.id}" is already used` });
170
- }
171
- ids.add(node.id);
172
- }
173
- if (node.type === StepType.HTTP) {
174
- checkHttpAccess(node, path, checks);
175
- } else if (node.type === StepType.HOST) {
176
- checkHost(node, path, checks);
177
- }
178
- };
179
-
180
- /**
181
- * Checks a raw document against the format and against the product's host catalog. Every
182
- * error is collected, each with the JSON path of its node. An unknown `formatVersion` is
183
- * reported alone: no other rule is meaningful for a format we do not know.
184
- */
185
- export const validateDefinition = (raw: unknown, catalog: HostActionCatalog): ValidationResult => {
186
- if (!isRecord(raw)) {
187
- return { ok: false, errors: [{ path: '', message: 'a definition must be an object' }] };
188
- }
189
- if (raw.formatVersion !== CURRENT_FORMAT_VERSION) {
190
- return {
191
- ok: false,
192
- errors: [
193
- { path: 'formatVersion', message: `"formatVersion" must be ${CURRENT_FORMAT_VERSION}` },
194
- ],
195
- };
196
- }
197
- const errors: DefinitionError[] = [];
198
- joiErrors(DEFINITION_SCHEMA, raw, '', errors);
199
- const accesses = checkAccesses(raw.accesses, errors);
200
- const ids = new Set<string>();
201
- const checks: StepChecks = { catalog, accesses, errors };
202
- walkRawSteps(raw.steps, (node, path) => checkStep(node, path, ids, checks));
203
- return errors.length === 0
204
- ? { ok: true, definition: raw as unknown as RuntimeDefinition }
205
- : { ok: false, errors };
206
- };
1
+ import type Joi from 'joi';
2
+ import { AccessKind } from '../access/access.types';
3
+ import type { AccessDeclaration } from '../access/access.types';
4
+ import { hostMatches } from '../access/host-match';
5
+ import { ACCESS_SCHEMAS, DEFINITION_SCHEMA, STEP_SCHEMAS } from './definition.schema';
6
+ import { CURRENT_FORMAT_VERSION, StepType } from './definition.types';
7
+ import type { DefinitionError, HostActionCatalog, RuntimeDefinition } from './definition.types';
8
+ import { walkRawSteps } from './walk-steps';
9
+
10
+ export type ValidationResult =
11
+ | { ok: true; definition: RuntimeDefinition }
12
+ | { ok: false; errors: DefinitionError[] };
13
+
14
+ const JOI_OPTIONS: Joi.ValidationOptions = { abortEarly: false, convert: false };
15
+
16
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
17
+ typeof value === 'object' && value !== null && !Array.isArray(value);
18
+
19
+ const joinPath = (base: string, segments: ReadonlyArray<string | number>): string =>
20
+ segments.reduce<string>((path, segment) => {
21
+ if (typeof segment === 'number') {
22
+ return `${path}[${segment}]`;
23
+ }
24
+ return path === '' ? segment : `${path}.${segment}`;
25
+ }, base);
26
+
27
+ const joiErrors = (
28
+ schema: Joi.Schema,
29
+ value: unknown,
30
+ basePath: string,
31
+ errors: DefinitionError[],
32
+ ): void => {
33
+ const { error } = schema.validate(value, JOI_OPTIONS);
34
+ error?.details.forEach((detail) =>
35
+ errors.push({ path: joinPath(basePath, detail.path), message: detail.message }),
36
+ );
37
+ };
38
+
39
+ const isAccessKind = (value: unknown): value is AccessKind =>
40
+ Object.values(AccessKind).includes(value as AccessKind);
41
+
42
+ const isStepType = (value: unknown): value is StepType =>
43
+ Object.values(StepType).includes(value as StepType);
44
+
45
+ /** Literal keys of an action's input schema, or `null` when it accepts any key. */
46
+ const allowedInputKeys = (catalog: HostActionCatalog, action: string): Set<string> | null => {
47
+ const description = catalog.get(action)?.inputSchema.describe() as
48
+ | { keys?: Record<string, unknown>; flags?: { unknown?: boolean }; patterns?: unknown }
49
+ | undefined;
50
+ if (!description?.keys || description.flags?.unknown || description.patterns) {
51
+ return null;
52
+ }
53
+ return new Set(Object.keys(description.keys));
54
+ };
55
+
56
+ const checkAccesses = (accesses: unknown, errors: DefinitionError[]): AccessDeclaration[] => {
57
+ if (!Array.isArray(accesses)) {
58
+ return [];
59
+ }
60
+ const valid: AccessDeclaration[] = [];
61
+ accesses.forEach((access, index) => {
62
+ const path = `accesses[${index}]`;
63
+ const kind = isRecord(access) ? access.kind : undefined;
64
+ if (!isAccessKind(kind)) {
65
+ errors.push({ path: `${path}.kind`, message: '"kind" must be one of [HTTP, HOST_ACTION]' });
66
+ return;
67
+ }
68
+ const before = errors.length;
69
+ joiErrors(ACCESS_SCHEMAS[kind], access, path, errors);
70
+ if (errors.length === before) {
71
+ valid.push(access as AccessDeclaration);
72
+ }
73
+ });
74
+ return valid;
75
+ };
76
+
77
+ interface StepChecks {
78
+ catalog: HostActionCatalog;
79
+ accesses: AccessDeclaration[];
80
+ errors: DefinitionError[];
81
+ }
82
+
83
+ const checkHttpAccess = (step: Record<string, unknown>, path: string, checks: StepChecks): void => {
84
+ const httpAccesses = checks.accesses.filter((access) => access.kind === AccessKind.HTTP);
85
+ if (httpAccesses.length === 0) {
86
+ checks.errors.push({
87
+ path,
88
+ message: 'an HTTP step requires an access declaration of kind HTTP',
89
+ });
90
+ return;
91
+ }
92
+ const { url } = step;
93
+ if (typeof url !== 'string' || url.includes('{{')) {
94
+ return;
95
+ }
96
+ let parsed: URL;
97
+ try {
98
+ parsed = new URL(url);
99
+ } catch {
100
+ checks.errors.push({ path: `${path}.url`, message: '"url" must be an absolute http(s) URL' });
101
+ return;
102
+ }
103
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
104
+ checks.errors.push({ path: `${path}.url`, message: '"url" must be an absolute http(s) URL' });
105
+ return;
106
+ }
107
+ const covered = httpAccesses.some(
108
+ (access) =>
109
+ access.kind === AccessKind.HTTP &&
110
+ access.hosts.some((pattern) => hostMatches(parsed.hostname, pattern)),
111
+ );
112
+ if (!covered) {
113
+ checks.errors.push({
114
+ path,
115
+ message: `host "${parsed.hostname}" is not covered by any HTTP access declaration`,
116
+ });
117
+ }
118
+ };
119
+
120
+ const checkHost = (step: Record<string, unknown>, path: string, checks: StepChecks): void => {
121
+ const { action, input } = step;
122
+ if (typeof action !== 'string' || action === '') {
123
+ return;
124
+ }
125
+ if (!checks.catalog.has(action)) {
126
+ checks.errors.push({
127
+ path: `${path}.action`,
128
+ message: `action "${action}" is not in the host catalog`,
129
+ });
130
+ } else if (isRecord(input)) {
131
+ const allowed = allowedInputKeys(checks.catalog, action);
132
+ if (allowed) {
133
+ Object.keys(input)
134
+ .filter((key) => !allowed.has(key))
135
+ .forEach((key) =>
136
+ checks.errors.push({
137
+ path: `${path}.input.${key}`,
138
+ message: `"${key}" is not an input of action "${action}"`,
139
+ }),
140
+ );
141
+ }
142
+ }
143
+ const declared = checks.accesses.some(
144
+ (access) => access.kind === AccessKind.HOST_ACTION && access.action === action,
145
+ );
146
+ if (!declared) {
147
+ checks.errors.push({
148
+ path,
149
+ message: `action "${action}" requires an access declaration of kind HOST_ACTION`,
150
+ });
151
+ }
152
+ };
153
+
154
+ const checkStep = (node: unknown, path: string, ids: Set<string>, checks: StepChecks): void => {
155
+ if (!isRecord(node)) {
156
+ checks.errors.push({ path, message: 'a step must be an object' });
157
+ return;
158
+ }
159
+ if (!isStepType(node.type)) {
160
+ checks.errors.push({
161
+ path: `${path}.type`,
162
+ message: `"type" must be one of [${Object.values(StepType).join(', ')}]`,
163
+ });
164
+ return;
165
+ }
166
+ joiErrors(STEP_SCHEMAS[node.type], node, path, checks.errors);
167
+ if (typeof node.id === 'string' && node.id !== '') {
168
+ if (ids.has(node.id)) {
169
+ checks.errors.push({ path: `${path}.id`, message: `id "${node.id}" is already used` });
170
+ }
171
+ ids.add(node.id);
172
+ }
173
+ if (node.type === StepType.HTTP) {
174
+ checkHttpAccess(node, path, checks);
175
+ } else if (node.type === StepType.HOST) {
176
+ checkHost(node, path, checks);
177
+ }
178
+ };
179
+
180
+ /**
181
+ * Checks a raw document against the format and against the product's host catalog. Every
182
+ * error is collected, each with the JSON path of its node. An unknown `formatVersion` is
183
+ * reported alone: no other rule is meaningful for a format we do not know.
184
+ */
185
+ export const validateDefinition = (raw: unknown, catalog: HostActionCatalog): ValidationResult => {
186
+ if (!isRecord(raw)) {
187
+ return { ok: false, errors: [{ path: '', message: 'a definition must be an object' }] };
188
+ }
189
+ if (raw.formatVersion !== CURRENT_FORMAT_VERSION) {
190
+ return {
191
+ ok: false,
192
+ errors: [
193
+ { path: 'formatVersion', message: `"formatVersion" must be ${CURRENT_FORMAT_VERSION}` },
194
+ ],
195
+ };
196
+ }
197
+ const errors: DefinitionError[] = [];
198
+ joiErrors(DEFINITION_SCHEMA, raw, '', errors);
199
+ const accesses = checkAccesses(raw.accesses, errors);
200
+ const ids = new Set<string>();
201
+ const checks: StepChecks = { catalog, accesses, errors };
202
+ walkRawSteps(raw.steps, (node, path) => checkStep(node, path, ids, checks));
203
+ return errors.length === 0
204
+ ? { ok: true, definition: raw as unknown as RuntimeDefinition }
205
+ : { ok: false, errors };
206
+ };
@@ -1,44 +1,44 @@
1
- import type { Step } from './definition.types';
2
-
3
- export type RawStepVisitor = (node: unknown, path: string) => void;
4
- export type StepVisitor = (step: Step, path: string) => void;
5
-
6
- const isRecord = (value: unknown): value is Record<string, unknown> =>
7
- typeof value === 'object' && value !== null && !Array.isArray(value);
8
-
9
- const walkList = (list: unknown, basePath: string, visit: RawStepVisitor): void => {
10
- if (!Array.isArray(list)) {
11
- return;
12
- }
13
- list.forEach((node, index) => {
14
- const path = `${basePath}[${index}]`;
15
- visit(node, path);
16
- if (!isRecord(node)) {
17
- return;
18
- }
19
- walkList(node.then, `${path}.then`, visit);
20
- walkList(node.else, `${path}.else`, visit);
21
- if (Array.isArray(node.cases)) {
22
- node.cases.forEach((branch, caseIndex) => {
23
- if (isRecord(branch)) {
24
- walkList(branch.steps, `${path}.cases[${caseIndex}].steps`, visit);
25
- }
26
- });
27
- }
28
- walkList(node.default, `${path}.default`, visit);
29
- });
30
- };
31
-
32
- /**
33
- * Same traversal as `walkSteps` over a document that is not validated yet: every node is
34
- * visited as `unknown`, and children are followed only where they are arrays.
35
- */
36
- export const walkRawSteps = (steps: unknown, visit: RawStepVisitor, basePath = 'steps'): void =>
37
- walkList(steps, basePath, visit);
38
-
39
- /**
40
- * Visits every step of the tree, depth first and in document order (a step before its
41
- * children), with its JSON path, e.g. `steps[2].cases[0].steps[1]`.
42
- */
43
- export const walkSteps = (steps: readonly Step[], visit: StepVisitor, basePath = 'steps'): void =>
44
- walkRawSteps(steps, (node, path) => visit(node as Step, path), basePath);
1
+ import type { Step } from './definition.types';
2
+
3
+ export type RawStepVisitor = (node: unknown, path: string) => void;
4
+ export type StepVisitor = (step: Step, path: string) => void;
5
+
6
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
7
+ typeof value === 'object' && value !== null && !Array.isArray(value);
8
+
9
+ const walkList = (list: unknown, basePath: string, visit: RawStepVisitor): void => {
10
+ if (!Array.isArray(list)) {
11
+ return;
12
+ }
13
+ list.forEach((node, index) => {
14
+ const path = `${basePath}[${index}]`;
15
+ visit(node, path);
16
+ if (!isRecord(node)) {
17
+ return;
18
+ }
19
+ walkList(node.then, `${path}.then`, visit);
20
+ walkList(node.else, `${path}.else`, visit);
21
+ if (Array.isArray(node.cases)) {
22
+ node.cases.forEach((branch, caseIndex) => {
23
+ if (isRecord(branch)) {
24
+ walkList(branch.steps, `${path}.cases[${caseIndex}].steps`, visit);
25
+ }
26
+ });
27
+ }
28
+ walkList(node.default, `${path}.default`, visit);
29
+ });
30
+ };
31
+
32
+ /**
33
+ * Same traversal as `walkSteps` over a document that is not validated yet: every node is
34
+ * visited as `unknown`, and children are followed only where they are arrays.
35
+ */
36
+ export const walkRawSteps = (steps: unknown, visit: RawStepVisitor, basePath = 'steps'): void =>
37
+ walkList(steps, basePath, visit);
38
+
39
+ /**
40
+ * Visits every step of the tree, depth first and in document order (a step before its
41
+ * children), with its JSON path, e.g. `steps[2].cases[0].steps[1]`.
42
+ */
43
+ export const walkSteps = (steps: readonly Step[], visit: StepVisitor, basePath = 'steps'): void =>
44
+ walkRawSteps(steps, (node, path) => visit(node as Step, path), basePath);