@operad-io/engine 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/config.schema.json +188 -0
- package/keys.schema.json +12 -0
- package/package.json +22 -4
- package/src/config/config.ts +627 -0
- package/src/config/index.ts +1 -0
- package/src/flow/define-flow.ts +57 -0
- package/src/flow/index.ts +5 -0
- package/src/flow/load.ts +41 -0
- package/src/flow/types.ts +107 -0
- package/src/flow/validate.ts +296 -0
- package/src/http/address.ts +142 -0
- package/src/http/index.ts +25 -0
- package/src/http/link.ts +20 -0
- package/src/http/server.ts +920 -0
- package/src/http/token.ts +85 -0
- package/src/index.ts +16 -0
- package/src/kernel/decide.ts +689 -0
- package/src/kernel/index.ts +3 -0
- package/src/kernel/state.ts +275 -0
- package/src/kernel/types.ts +366 -0
- package/src/log/errors.ts +77 -0
- package/src/log/facts.ts +50 -0
- package/src/log/index.ts +6 -0
- package/src/log/json-value.ts +67 -0
- package/src/log/open-log.ts +17 -0
- package/src/log/schema.ts +5 -0
- package/src/log/secret.ts +30 -0
- package/src/log/sqlite.ts +1011 -0
- package/src/log/types.ts +230 -0
- package/src/loop/code-watch.ts +486 -0
- package/src/loop/index.ts +3 -0
- package/src/loop/older-code.ts +58 -0
- package/src/loop/serve.ts +483 -0
- package/src/markdown.ts +451 -0
- package/src/notify/index.ts +10 -0
- package/src/notify/notify.ts +256 -0
- package/src/platform/file-lock.ts +385 -0
- package/src/platform/index.ts +13 -0
- package/src/platform/load.ts +18 -0
- package/src/platform/process-group.ts +635 -0
- package/src/platform/process-watch.ts +152 -0
- package/src/platform/sqlite-open.ts +104 -0
- package/src/platform/step-reap.ts +668 -0
- package/src/platform/watch.ts +158 -0
- package/src/platform/worker-reports.ts +34 -0
- package/src/plugin/action-schema.ts +550 -0
- package/src/plugin/declared-flows.ts +74 -0
- package/src/plugin/define-plugin.ts +12 -0
- package/src/plugin/errors.ts +73 -0
- package/src/plugin/filters.ts +46 -0
- package/src/plugin/harness.ts +50 -0
- package/src/plugin/home.ts +13 -0
- package/src/plugin/index.ts +65 -0
- package/src/plugin/install.ts +268 -0
- package/src/plugin/keys.ts +63 -0
- package/src/plugin/load.ts +256 -0
- package/src/plugin/logger.ts +30 -0
- package/src/plugin/object-facts.ts +132 -0
- package/src/plugin/open-store.ts +32 -0
- package/src/plugin/paging.ts +133 -0
- package/src/plugin/registry.ts +1783 -0
- package/src/plugin/secrets.ts +141 -0
- package/src/plugin/semver.ts +110 -0
- package/src/plugin/settings.ts +64 -0
- package/src/plugin/sqlite-store.ts +756 -0
- package/src/plugin/store.ts +318 -0
- package/src/plugin/types.ts +1236 -0
- package/src/plugin/usage.ts +157 -0
- package/src/plugin/validate.ts +1028 -0
- package/src/runner/answer.ts +156 -0
- package/src/runner/ask.ts +483 -0
- package/src/runner/cancel.ts +51 -0
- package/src/runner/errors.ts +20 -0
- package/src/runner/grants.ts +92 -0
- package/src/runner/index.ts +33 -0
- package/src/runner/limits.ts +403 -0
- package/src/runner/lock.ts +140 -0
- package/src/runner/runner.ts +1175 -0
- package/src/runner/service.ts +184 -0
- package/src/runner/step-worker.ts +303 -0
- package/src/testing/clock.ts +67 -0
- package/src/testing/index.ts +90 -0
- package/src/testing/types.ts +116 -0
- package/src/testing/world.ts +369 -0
- package/src/trigger/cron.ts +193 -0
- package/src/trigger/index.ts +32 -0
- package/src/trigger/kernel.ts +1212 -0
- package/src/trigger/rules.ts +300 -0
- package/src/trigger/store.ts +233 -0
- package/src/trigger/types.ts +156 -0
- package/src/util.ts +50 -0
- package/src/views/history.ts +634 -0
- package/src/views/index.ts +7 -0
- package/src/views/pending.ts +209 -0
- package/src/views/presentation.ts +2551 -0
- package/src/views/running.ts +255 -0
- package/src/views/settings.ts +452 -0
- package/src/views/types.ts +72 -0
- package/src/views/uploads.ts +228 -0
- package/triggers.schema.json +48 -0
- package/README.md +0 -3
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The small JSON Schema subset an action's `args` may use. The kernel validates
|
|
3
|
+
* it when the plugin loads and validates the person's values against it.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { ActionArgSchema, ActionArgsSchema } from "./types.ts";
|
|
7
|
+
import { canonical } from "./paging.ts";
|
|
8
|
+
import { isRecord } from "../util.ts";
|
|
9
|
+
|
|
10
|
+
const KEYWORDS = new Set([
|
|
11
|
+
"type",
|
|
12
|
+
"properties",
|
|
13
|
+
"required",
|
|
14
|
+
"enum",
|
|
15
|
+
"minimum",
|
|
16
|
+
"maximum",
|
|
17
|
+
"minLength",
|
|
18
|
+
"maxLength",
|
|
19
|
+
"items",
|
|
20
|
+
"minItems",
|
|
21
|
+
"exclusiveMinimum",
|
|
22
|
+
"exclusiveMaximum",
|
|
23
|
+
"deprecated",
|
|
24
|
+
"readOnly",
|
|
25
|
+
"minProperties",
|
|
26
|
+
"pattern",
|
|
27
|
+
"const",
|
|
28
|
+
"oneOf",
|
|
29
|
+
"$ref",
|
|
30
|
+
"$defs",
|
|
31
|
+
"$schema",
|
|
32
|
+
"$id",
|
|
33
|
+
"title",
|
|
34
|
+
"additionalProperties",
|
|
35
|
+
"default",
|
|
36
|
+
"description",
|
|
37
|
+
"role",
|
|
38
|
+
"format",
|
|
39
|
+
"editable",
|
|
40
|
+
"confirm",
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
const TYPES = new Set([
|
|
44
|
+
"string",
|
|
45
|
+
"number",
|
|
46
|
+
"integer",
|
|
47
|
+
"boolean",
|
|
48
|
+
"array",
|
|
49
|
+
"object",
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
/** A person's values do not fit the action's schema. */
|
|
53
|
+
export class ActionArgsError extends Error {
|
|
54
|
+
constructor(message: string) {
|
|
55
|
+
super(message);
|
|
56
|
+
this.name = "ActionArgsError";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function typeOf(value: unknown): string {
|
|
61
|
+
if (value === null) return "null";
|
|
62
|
+
if (Array.isArray(value)) return "array";
|
|
63
|
+
if (typeof value === "number" && Number.isInteger(value)) return "integer";
|
|
64
|
+
return typeof value;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function schemaError(message: string): never {
|
|
68
|
+
throw new Error(message);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The `$defs` of the schema's root, and the references being followed. */
|
|
72
|
+
interface Context {
|
|
73
|
+
readonly defs: Record<string, unknown>;
|
|
74
|
+
readonly following: string[];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function normalizeSchema(
|
|
78
|
+
value: unknown,
|
|
79
|
+
at: string,
|
|
80
|
+
context: Context,
|
|
81
|
+
): ActionArgSchema {
|
|
82
|
+
if (!isRecord(value)) {
|
|
83
|
+
throw new Error(`${at}: must be an object`);
|
|
84
|
+
}
|
|
85
|
+
for (const key of Object.keys(value)) {
|
|
86
|
+
if (!KEYWORDS.has(key)) {
|
|
87
|
+
throw new Error(`${at}: unknown keyword "${key}"`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (value.$ref !== undefined) {
|
|
92
|
+
const ref = value.$ref;
|
|
93
|
+
const name =
|
|
94
|
+
typeof ref === "string" && ref.startsWith("#/$defs/")
|
|
95
|
+
? ref.slice("#/$defs/".length)
|
|
96
|
+
: undefined;
|
|
97
|
+
if (name === undefined || !Object.hasOwn(context.defs, name)) {
|
|
98
|
+
throw new Error(`${at}.$ref: must name an entry of the schema's $defs, as #/$defs/<name>`);
|
|
99
|
+
}
|
|
100
|
+
if (context.following.includes(name)) {
|
|
101
|
+
throw new Error(`${at}.$ref: "${name}" refers to itself`);
|
|
102
|
+
}
|
|
103
|
+
const { $ref: _ref, ...siblings } = value;
|
|
104
|
+
const target = normalizeSchema(
|
|
105
|
+
context.defs[name],
|
|
106
|
+
`${at}.$ref`,
|
|
107
|
+
{ defs: context.defs, following: [...context.following, name] },
|
|
108
|
+
);
|
|
109
|
+
const own = normalizeSchema(siblings, at, context);
|
|
110
|
+
return { ...target, ...own };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const type = value.type;
|
|
114
|
+
if (type !== undefined && (typeof type !== "string" || !TYPES.has(type))) {
|
|
115
|
+
throw new Error(`${at}.type: must be one of ${[...TYPES].join(", ")}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const description = value.description;
|
|
119
|
+
if (description !== undefined && typeof description !== "string") {
|
|
120
|
+
throw new Error(`${at}.description: must be a string`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const role = value.role;
|
|
124
|
+
if (role !== undefined && role !== "note") {
|
|
125
|
+
throw new Error(`${at}.role: must be "note"`);
|
|
126
|
+
}
|
|
127
|
+
if (role === "note" && type !== "string") {
|
|
128
|
+
throw new Error(`${at}.role: a note property must have type "string"`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const format = value.format;
|
|
132
|
+
if (format !== undefined) {
|
|
133
|
+
if (format !== "file") throw new Error(`${at}.format: must be "file"`);
|
|
134
|
+
if (type !== "string") throw new Error(`${at}.format: a file property must have type "string"`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
for (const key of ["editable", "confirm"] as const) {
|
|
138
|
+
if (value[key] !== undefined && typeof value[key] !== "boolean") {
|
|
139
|
+
throw new Error(`${at}.${key}: must be a boolean`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const enumValues = value.enum;
|
|
144
|
+
if (
|
|
145
|
+
enumValues !== undefined &&
|
|
146
|
+
(!Array.isArray(enumValues) ||
|
|
147
|
+
!enumValues.every(
|
|
148
|
+
(item) =>
|
|
149
|
+
typeof item === "string" ||
|
|
150
|
+
typeof item === "number" ||
|
|
151
|
+
typeof item === "boolean",
|
|
152
|
+
))
|
|
153
|
+
) {
|
|
154
|
+
throw new Error(`${at}.enum: must be an array of strings, numbers or booleans`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const numeric: Record<string, number | undefined> = {};
|
|
158
|
+
for (const key of [
|
|
159
|
+
"minimum",
|
|
160
|
+
"maximum",
|
|
161
|
+
"minLength",
|
|
162
|
+
"maxLength",
|
|
163
|
+
"minItems",
|
|
164
|
+
"exclusiveMinimum",
|
|
165
|
+
"exclusiveMaximum",
|
|
166
|
+
"minProperties",
|
|
167
|
+
] as const) {
|
|
168
|
+
const item = value[key];
|
|
169
|
+
if (item !== undefined && typeof item !== "number") {
|
|
170
|
+
throw new Error(`${at}.${key}: must be a number`);
|
|
171
|
+
}
|
|
172
|
+
numeric[key] = typeof item === "number" ? item : undefined;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const pattern = value.pattern;
|
|
176
|
+
if (pattern !== undefined) {
|
|
177
|
+
if (typeof pattern !== "string") throw new Error(`${at}.pattern: must be a string`);
|
|
178
|
+
try {
|
|
179
|
+
new RegExp(pattern);
|
|
180
|
+
} catch {
|
|
181
|
+
throw new Error(`${at}.pattern: is not a regular expression`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
let oneOf: ActionArgSchema[] | undefined;
|
|
186
|
+
if (value.oneOf !== undefined) {
|
|
187
|
+
if (!Array.isArray(value.oneOf) || value.oneOf.length === 0) {
|
|
188
|
+
throw new Error(`${at}.oneOf: must be a non-empty array of schemas`);
|
|
189
|
+
}
|
|
190
|
+
oneOf = value.oneOf.map((branch: unknown, index: number) =>
|
|
191
|
+
normalizeSchema(branch, `${at}.oneOf[${index}]`, context),
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const additionalRaw = value.additionalProperties;
|
|
196
|
+
const additional: false | ActionArgSchema | undefined =
|
|
197
|
+
additionalRaw === undefined || additionalRaw === false
|
|
198
|
+
? additionalRaw
|
|
199
|
+
: isRecord(additionalRaw)
|
|
200
|
+
? normalizeSchema(additionalRaw, `${at}.additionalProperties`, context)
|
|
201
|
+
: schemaError(`${at}.additionalProperties: must be false or a schema`);
|
|
202
|
+
|
|
203
|
+
let properties: Record<string, ActionArgSchema> | undefined;
|
|
204
|
+
if (value.properties !== undefined) {
|
|
205
|
+
if (!isRecord(value.properties)) {
|
|
206
|
+
throw new Error(`${at}.properties: must be an object`);
|
|
207
|
+
}
|
|
208
|
+
properties = {};
|
|
209
|
+
for (const [name, schema] of Object.entries(value.properties)) {
|
|
210
|
+
properties[name] = normalizeSchema(schema, `${at}.properties.${name}`, context);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
let required: string[] | undefined;
|
|
215
|
+
if (value.required !== undefined) {
|
|
216
|
+
if (
|
|
217
|
+
!Array.isArray(value.required) ||
|
|
218
|
+
!value.required.every((item) => typeof item === "string")
|
|
219
|
+
) {
|
|
220
|
+
throw new Error(`${at}.required: must be an array of field names`);
|
|
221
|
+
}
|
|
222
|
+
required = value.required as string[];
|
|
223
|
+
for (const name of required) {
|
|
224
|
+
if (properties === undefined || properties[name] === undefined) {
|
|
225
|
+
throw new Error(`${at}.required: "${name}" is not a property`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
let items: ActionArgSchema | undefined;
|
|
231
|
+
if (value.items !== undefined) {
|
|
232
|
+
items = normalizeSchema(value.items, `${at}.items`, context);
|
|
233
|
+
}
|
|
234
|
+
if (items !== undefined && type !== undefined && type !== "array") {
|
|
235
|
+
throw new Error(`${at}.items: only an array has items`);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
...(type === undefined ? {} : { type: type as ActionArgSchema["type"] }),
|
|
240
|
+
...(enumValues === undefined
|
|
241
|
+
? {}
|
|
242
|
+
: { enum: enumValues as (string | number | boolean)[] }),
|
|
243
|
+
...(numeric.minimum === undefined ? {} : { minimum: numeric.minimum }),
|
|
244
|
+
...(numeric.maximum === undefined ? {} : { maximum: numeric.maximum }),
|
|
245
|
+
...(numeric.minLength === undefined ? {} : { minLength: numeric.minLength }),
|
|
246
|
+
...(numeric.maxLength === undefined ? {} : { maxLength: numeric.maxLength }),
|
|
247
|
+
...(numeric.minItems === undefined ? {} : { minItems: numeric.minItems }),
|
|
248
|
+
...(numeric.exclusiveMinimum === undefined
|
|
249
|
+
? {}
|
|
250
|
+
: { exclusiveMinimum: numeric.exclusiveMinimum }),
|
|
251
|
+
...(numeric.minProperties === undefined
|
|
252
|
+
? {}
|
|
253
|
+
: { minProperties: numeric.minProperties }),
|
|
254
|
+
...(numeric.exclusiveMaximum === undefined
|
|
255
|
+
? {}
|
|
256
|
+
: { exclusiveMaximum: numeric.exclusiveMaximum }),
|
|
257
|
+
...(pattern === undefined ? {} : { pattern: pattern as string }),
|
|
258
|
+
...(Object.hasOwn(value, "const") ? { const: value.const } : {}),
|
|
259
|
+
...(oneOf === undefined ? {} : { oneOf }),
|
|
260
|
+
...(properties === undefined ? {} : { properties }),
|
|
261
|
+
...(required === undefined ? {} : { required }),
|
|
262
|
+
...(items === undefined ? {} : { items }),
|
|
263
|
+
...(additional === undefined ? {} : { additionalProperties: additional }),
|
|
264
|
+
...(value.default === undefined ? {} : { default: value.default }),
|
|
265
|
+
...(description === undefined ? {} : { description }),
|
|
266
|
+
...(role === "note" ? { role } : {}),
|
|
267
|
+
...(format === "file" ? { format } : {}),
|
|
268
|
+
...(value.editable === undefined ? {} : { editable: value.editable as boolean }),
|
|
269
|
+
...(value.confirm === undefined ? {} : { confirm: value.confirm as boolean }),
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Validates the schema a plugin declares at load; the label names the action. */
|
|
274
|
+
export function normalizeActionArgs(
|
|
275
|
+
value: unknown,
|
|
276
|
+
at: string,
|
|
277
|
+
): ActionArgsSchema | undefined {
|
|
278
|
+
if (value === undefined) return undefined;
|
|
279
|
+
let defs: Record<string, unknown> = {};
|
|
280
|
+
if (isRecord(value) && value.$defs !== undefined) {
|
|
281
|
+
if (!isRecord(value.$defs)) throw new Error(`${at}.$defs: must be an object`);
|
|
282
|
+
defs = value.$defs;
|
|
283
|
+
}
|
|
284
|
+
const normalized = normalizeSchema(value, at, { defs, following: [] });
|
|
285
|
+
if (normalized.type !== "object") {
|
|
286
|
+
throw new Error(`${at}: type: must be "object"`);
|
|
287
|
+
}
|
|
288
|
+
return normalized as ActionArgsSchema;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** The note property of a schema, when it declares one. */
|
|
292
|
+
export function noteProperty(
|
|
293
|
+
schema: ActionArgsSchema | undefined,
|
|
294
|
+
): { readonly name: string; readonly required: boolean } | undefined {
|
|
295
|
+
if (schema?.properties === undefined) return undefined;
|
|
296
|
+
for (const [name, property] of Object.entries(schema.properties)) {
|
|
297
|
+
if (property.role === "note") {
|
|
298
|
+
return {
|
|
299
|
+
name,
|
|
300
|
+
required: schema.required?.includes(name) === true,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return undefined;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* The properties of an action's arguments that take a file: a string with
|
|
309
|
+
* `format: "file"`, or a list of them. The value is the upload's reference.
|
|
310
|
+
*/
|
|
311
|
+
export function fileProperties(
|
|
312
|
+
schema: ActionArgsSchema | undefined,
|
|
313
|
+
): readonly { readonly name: string; readonly many: boolean }[] {
|
|
314
|
+
const found: { name: string; many: boolean }[] = [];
|
|
315
|
+
for (const [name, property] of Object.entries(schema?.properties ?? {})) {
|
|
316
|
+
if (property.format === "file") found.push({ name, many: false });
|
|
317
|
+
else if (property.type === "array" && property.items?.format === "file") {
|
|
318
|
+
found.push({ name, many: true });
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return found;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function fail(label: string, field: string, detail: string): never {
|
|
325
|
+
throw new ActionArgsError(`${label}: args.${field}: ${detail}`);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function fieldName(path: string, name: string): string {
|
|
329
|
+
return path.length === 0 ? name : `${path}.${name}`;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const TYPE_WORDS: Record<string, string> = {
|
|
333
|
+
string: "text",
|
|
334
|
+
number: "a number",
|
|
335
|
+
integer: "a whole number",
|
|
336
|
+
boolean: "true or false",
|
|
337
|
+
array: "a list",
|
|
338
|
+
object: "an object",
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
/** What a schema accepts, in words a person reads. */
|
|
342
|
+
function describeSchema(schema: ActionArgSchema): string {
|
|
343
|
+
if (schema.const !== undefined) return JSON.stringify(schema.const);
|
|
344
|
+
if (schema.enum !== undefined) {
|
|
345
|
+
return `one of ${schema.enum.map((item) => JSON.stringify(item)).join(", ")}`;
|
|
346
|
+
}
|
|
347
|
+
if (schema.oneOf !== undefined) {
|
|
348
|
+
return schema.oneOf.map(describeSchema).join(" or ");
|
|
349
|
+
}
|
|
350
|
+
if (schema.type === "string") {
|
|
351
|
+
const kind = schema.minLength !== undefined && schema.minLength > 0 ? "non-empty text" : "text";
|
|
352
|
+
return schema.pattern === undefined ? kind : `${kind} written as ${schema.pattern}`;
|
|
353
|
+
}
|
|
354
|
+
if (schema.type === "object") {
|
|
355
|
+
const fixed = Object.entries(schema.properties ?? {}).find(
|
|
356
|
+
([, property]) => property.const !== undefined,
|
|
357
|
+
);
|
|
358
|
+
const names = Object.keys(schema.properties ?? {});
|
|
359
|
+
const base = fixed === undefined ? "an object" : `an object with ${fixed[0]} ${JSON.stringify(fixed[1].const)}`;
|
|
360
|
+
return fixed === undefined && names.length > 0
|
|
361
|
+
? `${base} with the fields ${names.join(", ")}`
|
|
362
|
+
: base;
|
|
363
|
+
}
|
|
364
|
+
if (schema.type === "array") {
|
|
365
|
+
return schema.items === undefined ? "a list" : `a list of ${describeSchema(schema.items)}`;
|
|
366
|
+
}
|
|
367
|
+
return schema.type === undefined ? "any value" : (TYPE_WORDS[schema.type] ?? schema.type);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function fits(schema: ActionArgSchema, value: unknown): boolean {
|
|
371
|
+
try {
|
|
372
|
+
checkValue(schema, value, "", "");
|
|
373
|
+
return true;
|
|
374
|
+
} catch (error) {
|
|
375
|
+
if (error instanceof ActionArgsError) return false;
|
|
376
|
+
throw error;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function typeFits(schema: ActionArgSchema, actual: string): boolean {
|
|
381
|
+
if (schema.type === undefined) return true;
|
|
382
|
+
return schema.type === actual || (schema.type === "number" && actual === "integer");
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function checkOneOf(
|
|
386
|
+
branches: readonly ActionArgSchema[],
|
|
387
|
+
value: unknown,
|
|
388
|
+
label: string,
|
|
389
|
+
field: string,
|
|
390
|
+
): void {
|
|
391
|
+
const matching = branches.filter((branch) => fits(branch, value));
|
|
392
|
+
if (matching.length === 1) return;
|
|
393
|
+
if (matching.length > 1) {
|
|
394
|
+
fail(label, field, "fits more than one of the accepted forms");
|
|
395
|
+
}
|
|
396
|
+
// Exactly one branch is of the value's kind: its own reason is the useful one.
|
|
397
|
+
const actual = typeOf(value);
|
|
398
|
+
const sameKind = branches.filter((branch) => typeFits(branch, actual));
|
|
399
|
+
const [only] = sameKind;
|
|
400
|
+
if (sameKind.length === 1 && only !== undefined) {
|
|
401
|
+
checkValue(only, value, label, field);
|
|
402
|
+
}
|
|
403
|
+
fail(label, field, `must be ${branches.map(describeSchema).join(" or ")}`);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function checkValue(
|
|
407
|
+
schema: ActionArgSchema,
|
|
408
|
+
value: unknown,
|
|
409
|
+
label: string,
|
|
410
|
+
field: string,
|
|
411
|
+
): void {
|
|
412
|
+
if (schema.oneOf !== undefined) {
|
|
413
|
+
checkOneOf(schema.oneOf, value, label, field);
|
|
414
|
+
}
|
|
415
|
+
const actual = typeOf(value);
|
|
416
|
+
if (schema.type !== undefined) {
|
|
417
|
+
const matches =
|
|
418
|
+
schema.type === actual ||
|
|
419
|
+
(schema.type === "number" && actual === "integer");
|
|
420
|
+
if (!matches) {
|
|
421
|
+
fail(label, field, `must be a ${schema.type}, got ${actual}`);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
if (schema.const !== undefined && canonical(schema.const) !== canonical(value)) {
|
|
426
|
+
fail(label, field, `must be ${JSON.stringify(schema.const)}`);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (schema.enum !== undefined) {
|
|
430
|
+
if (!schema.enum.some((item) => canonical(item) === canonical(value))) {
|
|
431
|
+
const allowed = schema.enum
|
|
432
|
+
.map((item) => JSON.stringify(item))
|
|
433
|
+
.join(", ");
|
|
434
|
+
fail(label, field, `${JSON.stringify(value)} is not one of ${allowed}`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (typeof value === "number") {
|
|
439
|
+
if (schema.minimum !== undefined && value < schema.minimum) {
|
|
440
|
+
fail(label, field, `must be at least ${schema.minimum}`);
|
|
441
|
+
}
|
|
442
|
+
if (schema.maximum !== undefined && value > schema.maximum) {
|
|
443
|
+
fail(label, field, `must be at most ${schema.maximum}`);
|
|
444
|
+
}
|
|
445
|
+
if (schema.exclusiveMinimum !== undefined && value <= schema.exclusiveMinimum) {
|
|
446
|
+
fail(label, field, `must be more than ${schema.exclusiveMinimum}`);
|
|
447
|
+
}
|
|
448
|
+
if (schema.exclusiveMaximum !== undefined && value >= schema.exclusiveMaximum) {
|
|
449
|
+
fail(label, field, `must be less than ${schema.exclusiveMaximum}`);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if (typeof value === "string") {
|
|
454
|
+
if (schema.minLength !== undefined && value.length < schema.minLength) {
|
|
455
|
+
fail(label, field, `must be at least ${schema.minLength} characters`);
|
|
456
|
+
}
|
|
457
|
+
if (schema.maxLength !== undefined && value.length > schema.maxLength) {
|
|
458
|
+
fail(label, field, `must be at most ${schema.maxLength} characters`);
|
|
459
|
+
}
|
|
460
|
+
if (schema.pattern !== undefined && !new RegExp(schema.pattern).test(value)) {
|
|
461
|
+
fail(label, field, `must be written as ${schema.pattern}`);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if (Array.isArray(value)) {
|
|
466
|
+
if (schema.minItems !== undefined && value.length < schema.minItems) {
|
|
467
|
+
fail(label, field, `must have at least ${schema.minItems} items`);
|
|
468
|
+
}
|
|
469
|
+
if (schema.items !== undefined) {
|
|
470
|
+
value.forEach((item, index) => {
|
|
471
|
+
checkValue(schema.items as ActionArgSchema, item, label, fieldName(field, String(index)));
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (
|
|
477
|
+
isRecord(value) &&
|
|
478
|
+
schema.minProperties !== undefined &&
|
|
479
|
+
Object.keys(value).length < schema.minProperties
|
|
480
|
+
) {
|
|
481
|
+
fail(label, field, `must have at least ${schema.minProperties} fields`);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
if (
|
|
485
|
+
isRecord(value) &&
|
|
486
|
+
(schema.properties !== undefined || typeof schema.additionalProperties === "object")
|
|
487
|
+
) {
|
|
488
|
+
checkObject(schema, value, label, field);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function checkObject(
|
|
493
|
+
schema: ActionArgSchema,
|
|
494
|
+
value: Record<string, unknown>,
|
|
495
|
+
label: string,
|
|
496
|
+
field: string,
|
|
497
|
+
): void {
|
|
498
|
+
for (const name of schema.required ?? []) {
|
|
499
|
+
if (value[name] === undefined) {
|
|
500
|
+
fail(label, fieldName(field, name), "is required");
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
for (const [name, item] of Object.entries(value)) {
|
|
504
|
+
const property = schema.properties?.[name];
|
|
505
|
+
if (property === undefined) {
|
|
506
|
+
if (schema.additionalProperties === false) {
|
|
507
|
+
fail(label, fieldName(field, name), "is not a field of this action");
|
|
508
|
+
}
|
|
509
|
+
if (typeof schema.additionalProperties === "object") {
|
|
510
|
+
checkValue(schema.additionalProperties, item, label, fieldName(field, name));
|
|
511
|
+
}
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
checkValue(property, item, label, fieldName(field, name));
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Validates one value against a normalized schema. The label and field prefix
|
|
520
|
+
* the message, so a settings section names its plugin and its field.
|
|
521
|
+
*/
|
|
522
|
+
export function validateSchemaValue(
|
|
523
|
+
schema: ActionArgSchema,
|
|
524
|
+
value: unknown,
|
|
525
|
+
label: string,
|
|
526
|
+
field: string,
|
|
527
|
+
): void {
|
|
528
|
+
checkValue(schema, value, label, field);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Validates a person's values against an action's schema. The returned object
|
|
533
|
+
* is the same value; omitted optional fields stay omitted.
|
|
534
|
+
*/
|
|
535
|
+
export function validateActionArgs(
|
|
536
|
+
schema: ActionArgsSchema | undefined,
|
|
537
|
+
args: unknown,
|
|
538
|
+
label: string,
|
|
539
|
+
): Record<string, unknown> {
|
|
540
|
+
if (args === undefined) {
|
|
541
|
+
args = {};
|
|
542
|
+
}
|
|
543
|
+
if (!isRecord(args)) {
|
|
544
|
+
throw new ActionArgsError(`${label}: args: must be an object`);
|
|
545
|
+
}
|
|
546
|
+
if (schema !== undefined) {
|
|
547
|
+
checkObject(schema, args, label, "");
|
|
548
|
+
}
|
|
549
|
+
return args;
|
|
550
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flows a plugin generated while a process ran. They are kept beside the
|
|
3
|
+
* plugin store so the run's own process loads the same graph: a declaration
|
|
4
|
+
* exists only in the memory of the process that made it.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
import {
|
|
9
|
+
mkdirSync,
|
|
10
|
+
readdirSync,
|
|
11
|
+
readFileSync,
|
|
12
|
+
renameSync,
|
|
13
|
+
writeFileSync,
|
|
14
|
+
} from "node:fs";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import type { FlowDefinition } from "../flow/types.ts";
|
|
17
|
+
import { isRecord } from "../util.ts";
|
|
18
|
+
|
|
19
|
+
/** One flow a plugin declared, with the plugin that made it. */
|
|
20
|
+
export interface StoredDeclaredFlow {
|
|
21
|
+
readonly plugin: string;
|
|
22
|
+
readonly flow: FlowDefinition;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const DIRECTORY = "flows";
|
|
26
|
+
|
|
27
|
+
function directoryOf(root: string): string {
|
|
28
|
+
return join(root, DIRECTORY);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function fileOf(root: string, name: string): string {
|
|
32
|
+
const hash = createHash("sha256").update(name).digest("hex").slice(0, 32);
|
|
33
|
+
return join(directoryOf(root), `${hash}.json`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Keeps a declaration for a later process; a write lands whole or not at all. */
|
|
37
|
+
export function writeDeclaredFlow(
|
|
38
|
+
root: string,
|
|
39
|
+
plugin: string,
|
|
40
|
+
flow: FlowDefinition,
|
|
41
|
+
): void {
|
|
42
|
+
const file = fileOf(root, flow.name);
|
|
43
|
+
const directory = directoryOf(root);
|
|
44
|
+
mkdirSync(directory, { recursive: true });
|
|
45
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
46
|
+
const record: StoredDeclaredFlow = { plugin, flow };
|
|
47
|
+
writeFileSync(temporary, `${JSON.stringify(record)}\n`, "utf8");
|
|
48
|
+
renameSync(temporary, file);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Every declaration kept under the store root. A torn file says nothing. */
|
|
52
|
+
export function readDeclaredFlows(root: string): readonly StoredDeclaredFlow[] {
|
|
53
|
+
let names: string[];
|
|
54
|
+
try {
|
|
55
|
+
names = readdirSync(directoryOf(root)).filter((name) => name.endsWith(".json")).sort();
|
|
56
|
+
} catch {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
const flows: StoredDeclaredFlow[] = [];
|
|
60
|
+
for (const name of names) {
|
|
61
|
+
let value: unknown;
|
|
62
|
+
try {
|
|
63
|
+
value = JSON.parse(readFileSync(join(directoryOf(root), name), "utf8"));
|
|
64
|
+
} catch {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (!isRecord(value) || typeof value.plugin !== "string" || !isRecord(value.flow)) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (typeof value.flow.name !== "string" || value.flow.name.length === 0) continue;
|
|
71
|
+
flows.push(value as unknown as StoredDeclaredFlow);
|
|
72
|
+
}
|
|
73
|
+
return flows;
|
|
74
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { OPERAD_PLUGIN_VERSION } from "./types.ts";
|
|
2
|
+
import type { PluginShape } from "./types.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Declares a plugin. The value is plain data; the registry checks it before
|
|
6
|
+
* the plugin can be used. `operadPlugin` is the API version the kernel reads.
|
|
7
|
+
*/
|
|
8
|
+
export function definePlugin<const P extends PluginShape>(
|
|
9
|
+
plugin: P,
|
|
10
|
+
): P & { readonly operadPlugin: typeof OPERAD_PLUGIN_VERSION } {
|
|
11
|
+
return { ...plugin, operadPlugin: OPERAD_PLUGIN_VERSION };
|
|
12
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/** A plugin cannot be loaded or used; the message names the plugin and field. */
|
|
2
|
+
export class PluginError extends Error {
|
|
3
|
+
constructor(message: string, options?: ErrorOptions) {
|
|
4
|
+
super(message, options);
|
|
5
|
+
this.name = "PluginError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** A run's access profile asks for a field no step of its flow applies. */
|
|
10
|
+
export class AccessError extends PluginError {
|
|
11
|
+
readonly field: string;
|
|
12
|
+
|
|
13
|
+
constructor(field: string, message: string) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "AccessError";
|
|
16
|
+
this.field = field;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A step was called that no loaded plugin declares: its plugin is disabled or
|
|
22
|
+
* failed to load. `kind` lets a caller tell this from a step's own failure
|
|
23
|
+
* without reading the message.
|
|
24
|
+
*/
|
|
25
|
+
export class StepNotLoadedError extends PluginError {
|
|
26
|
+
readonly kind = "step_not_loaded";
|
|
27
|
+
readonly use: string;
|
|
28
|
+
|
|
29
|
+
constructor(use: string) {
|
|
30
|
+
super(`step "${use}": no loaded plugin declares it`);
|
|
31
|
+
this.name = "StepNotLoadedError";
|
|
32
|
+
this.use = use;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The message of an unknown thrown value. */
|
|
37
|
+
export function messageOf(error: unknown): string {
|
|
38
|
+
return error instanceof Error ? error.message : String(error);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A step asked a person and the grace passed with `onTimeout: "waiting"`. A step
|
|
43
|
+
* that lets it escape ends with the result `waiting`; the registry turns it into
|
|
44
|
+
* that result, carrying the question for the flow's wait.
|
|
45
|
+
*/
|
|
46
|
+
export class AskTimeoutError extends Error {
|
|
47
|
+
readonly waitId: string;
|
|
48
|
+
readonly prompt: string;
|
|
49
|
+
readonly choices: readonly string[];
|
|
50
|
+
readonly resume: unknown;
|
|
51
|
+
|
|
52
|
+
constructor(question: {
|
|
53
|
+
readonly waitId: string;
|
|
54
|
+
readonly prompt: string;
|
|
55
|
+
readonly choices: readonly string[];
|
|
56
|
+
readonly resume: unknown;
|
|
57
|
+
}) {
|
|
58
|
+
super(`nobody answered "${question.prompt}" in time`);
|
|
59
|
+
this.name = "AskTimeoutError";
|
|
60
|
+
this.waitId = question.waitId;
|
|
61
|
+
this.prompt = question.prompt;
|
|
62
|
+
this.choices = question.choices;
|
|
63
|
+
this.resume = question.resume;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A run cannot start because a step it needs is not loaded. */
|
|
68
|
+
export class StartRefusedError extends PluginError {
|
|
69
|
+
constructor(message: string) {
|
|
70
|
+
super(message);
|
|
71
|
+
this.name = "StartRefusedError";
|
|
72
|
+
}
|
|
73
|
+
}
|