@operad-io/engine 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +11 -2
- 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 +90 -0
- package/src/index.ts +18 -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 +675 -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 +1786 -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 +1243 -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 +1221 -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 +2609 -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
|
@@ -0,0 +1,1028 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The checks a registry runs on each plugin definition before it uses one: the
|
|
3
|
+
* normalization of the fields a plugin declares, and the rules that span all
|
|
4
|
+
* plugins of a set.
|
|
5
|
+
*/
|
|
6
|
+
import { ALL_FILTER } from "./filters.ts";
|
|
7
|
+
import type { FlowDefinition, StepType } from "../flow/types.ts";
|
|
8
|
+
import type { JsonValue } from "../log/types.ts";
|
|
9
|
+
import { FlowError, validateFlow } from "../flow/validate.ts";
|
|
10
|
+
import { normalizeActionArgs, noteProperty } from "./action-schema.ts";
|
|
11
|
+
import { messageOf, PluginError } from "./errors.ts";
|
|
12
|
+
import { isRangeSyntax } from "./semver.ts";
|
|
13
|
+
import { END_RESULTS, isEndResult } from "./usage.ts";
|
|
14
|
+
import { parseTriggerRules, TriggerError } from "../trigger/rules.ts";
|
|
15
|
+
import type { TriggerRule } from "../trigger/types.ts";
|
|
16
|
+
import {
|
|
17
|
+
OPERAD_PLUGIN_VERSION,
|
|
18
|
+
type ActionArgsSchema,
|
|
19
|
+
type ActionTypeDefinition,
|
|
20
|
+
type AskRequest,
|
|
21
|
+
type CommandDefinition,
|
|
22
|
+
type HarnessEfforts,
|
|
23
|
+
type HarnessModels,
|
|
24
|
+
type KeyBindings,
|
|
25
|
+
type MaybePromise,
|
|
26
|
+
type ObjectFilterDefinition,
|
|
27
|
+
type ObjectTypeDefinition,
|
|
28
|
+
type ObjectViews,
|
|
29
|
+
type PluginBaseContext,
|
|
30
|
+
type PluginKind,
|
|
31
|
+
type PluginRequires,
|
|
32
|
+
type ProjectSettingsDeclaration,
|
|
33
|
+
type SourceContext,
|
|
34
|
+
type SourceHandle,
|
|
35
|
+
type StandingContext,
|
|
36
|
+
type StandingItem,
|
|
37
|
+
type StepLimitDefinition,
|
|
38
|
+
type StepTypeDefinition,
|
|
39
|
+
type TickContext,
|
|
40
|
+
} from "./types.ts";
|
|
41
|
+
import { isRecord } from "../util.ts";
|
|
42
|
+
|
|
43
|
+
export interface NormalizedPlugin {
|
|
44
|
+
readonly name: string;
|
|
45
|
+
readonly kind: PluginKind;
|
|
46
|
+
readonly version: string;
|
|
47
|
+
readonly objects: readonly ObjectTypeDefinition[];
|
|
48
|
+
readonly actions: readonly ActionTypeDefinition[];
|
|
49
|
+
readonly steps: readonly StepTypeDefinition[];
|
|
50
|
+
readonly flows: readonly unknown[];
|
|
51
|
+
readonly commands: readonly CommandDefinition[];
|
|
52
|
+
readonly views: Readonly<Record<string, ObjectViews>>;
|
|
53
|
+
readonly keys: KeyBindings;
|
|
54
|
+
readonly rules: readonly TriggerRule[];
|
|
55
|
+
readonly onTick: ((ctx: TickContext) => MaybePromise<void>) | undefined;
|
|
56
|
+
readonly standing:
|
|
57
|
+
| ((ctx: StandingContext) => MaybePromise<readonly StandingItem[]>)
|
|
58
|
+
| undefined;
|
|
59
|
+
readonly start:
|
|
60
|
+
| ((ctx: SourceContext) => MaybePromise<SourceHandle> | SourceHandle)
|
|
61
|
+
| undefined;
|
|
62
|
+
readonly check:
|
|
63
|
+
| ((ctx: PluginBaseContext) => MaybePromise<string | void>)
|
|
64
|
+
| undefined;
|
|
65
|
+
readonly settingsSchema: ActionArgsSchema | undefined;
|
|
66
|
+
/** The plugin's objects belong under a client's settings view. */
|
|
67
|
+
readonly objectsUnderSettings: boolean;
|
|
68
|
+
readonly projectSettings: ProjectSettingsDeclaration | undefined;
|
|
69
|
+
readonly enforces: readonly string[];
|
|
70
|
+
readonly models:
|
|
71
|
+
| HarnessModels
|
|
72
|
+
| ((settings: Readonly<Record<string, JsonValue>>) => HarnessModels)
|
|
73
|
+
| undefined;
|
|
74
|
+
readonly efforts: HarnessEfforts | undefined;
|
|
75
|
+
readonly requires: PluginRequires | undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface LoadedPlugin extends Omit<NormalizedPlugin, "flows"> {
|
|
79
|
+
readonly flows: readonly FlowDefinition[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const KINDS: readonly PluginKind[] = [
|
|
83
|
+
"domain",
|
|
84
|
+
"harness",
|
|
85
|
+
"stage",
|
|
86
|
+
"channel",
|
|
87
|
+
"source",
|
|
88
|
+
"action",
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
export function noteInfo(
|
|
92
|
+
args: ActionArgsSchema | undefined,
|
|
93
|
+
): { readonly name: string | undefined; readonly required: boolean } {
|
|
94
|
+
const note = noteProperty(args);
|
|
95
|
+
return { name: note?.name, required: note?.required === true };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Splits `<plugin>:<action>` or `<plugin>/<action>` without splitting an id. */
|
|
99
|
+
export function splitActionName(
|
|
100
|
+
name: string,
|
|
101
|
+
): { readonly plugin: string; readonly action: string } | undefined {
|
|
102
|
+
const colon = name.indexOf(":");
|
|
103
|
+
const slash = name.indexOf("/");
|
|
104
|
+
const at = colon > 0 ? colon : slash > 0 ? slash : -1;
|
|
105
|
+
if (at <= 0 || at === name.length - 1) return undefined;
|
|
106
|
+
return { plugin: name.slice(0, at), action: name.slice(at + 1) };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The step param `askGraceSecs`, when the step's params carry one. */
|
|
110
|
+
export function askGraceOf(params: unknown): unknown {
|
|
111
|
+
return isRecord(params) ? params.askGraceSecs : undefined;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The smaller of the grace seconds that are set. */
|
|
115
|
+
export function narrowerOf(...values: readonly unknown[]): number | undefined {
|
|
116
|
+
let result: number | undefined;
|
|
117
|
+
for (const value of values) {
|
|
118
|
+
if (typeof value !== "number") continue;
|
|
119
|
+
result = result === undefined ? value : Math.min(result, value);
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function parseObjectKeyMaybe(key: string): { readonly type: string; readonly id: string } {
|
|
125
|
+
const colon = key.indexOf(":");
|
|
126
|
+
if (colon <= 0 || colon === key.length - 1) {
|
|
127
|
+
return { type: "object", id: key };
|
|
128
|
+
}
|
|
129
|
+
return { type: key.slice(0, colon), id: key.slice(colon + 1) };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function isPluginKind(value: unknown): value is PluginKind {
|
|
133
|
+
return (
|
|
134
|
+
value === "domain" ||
|
|
135
|
+
value === "harness" ||
|
|
136
|
+
value === "stage" ||
|
|
137
|
+
value === "channel" ||
|
|
138
|
+
value === "source" ||
|
|
139
|
+
value === "action"
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function normalizeViews(
|
|
144
|
+
value: unknown,
|
|
145
|
+
at: string,
|
|
146
|
+
objects: readonly ObjectTypeDefinition[],
|
|
147
|
+
): Readonly<Record<string, ObjectViews>> {
|
|
148
|
+
if (value === undefined) return {};
|
|
149
|
+
if (!isRecord(value)) {
|
|
150
|
+
throw new PluginError(`${at}: must be an object`);
|
|
151
|
+
}
|
|
152
|
+
const known = new Set(objects.map((object) => object.name));
|
|
153
|
+
const views: Record<string, ObjectViews> = {};
|
|
154
|
+
for (const [type, raw] of Object.entries(value)) {
|
|
155
|
+
if (!known.has(type)) {
|
|
156
|
+
throw new PluginError(`${at}.${type}: the plugin declares no object type "${type}"`);
|
|
157
|
+
}
|
|
158
|
+
if (!isRecord(raw)) {
|
|
159
|
+
throw new PluginError(`${at}.${type}: must be an object`);
|
|
160
|
+
}
|
|
161
|
+
if (typeof raw.row !== "function") {
|
|
162
|
+
throw new PluginError(`${at}.${type}: row: must be a function`);
|
|
163
|
+
}
|
|
164
|
+
if (typeof raw.sections !== "function") {
|
|
165
|
+
throw new PluginError(`${at}.${type}: sections: must be a function`);
|
|
166
|
+
}
|
|
167
|
+
if (typeof raw.load !== "function") {
|
|
168
|
+
throw new PluginError(`${at}.${type}: load: must be a function`);
|
|
169
|
+
}
|
|
170
|
+
if (raw.showScope !== undefined && typeof raw.showScope !== "function") {
|
|
171
|
+
throw new PluginError(`${at}.${type}: showScope: must be a function`);
|
|
172
|
+
}
|
|
173
|
+
views[type] = {
|
|
174
|
+
row: raw.row as ObjectViews["row"],
|
|
175
|
+
sections: raw.sections as ObjectViews["sections"],
|
|
176
|
+
load: raw.load as ObjectViews["load"],
|
|
177
|
+
...(raw.showScope === undefined
|
|
178
|
+
? {}
|
|
179
|
+
: { showScope: raw.showScope as NonNullable<ObjectViews["showScope"]> }),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
return views;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function normalizeProjectSettings(
|
|
186
|
+
value: unknown,
|
|
187
|
+
at: string,
|
|
188
|
+
): ProjectSettingsDeclaration | undefined {
|
|
189
|
+
if (value === undefined) return undefined;
|
|
190
|
+
if (!isRecord(value)) throw new PluginError(`${at}: must be an object`);
|
|
191
|
+
if (typeof value.repositories !== "function") {
|
|
192
|
+
throw new PluginError(`${at}: repositories: must be a function of ({ store })`);
|
|
193
|
+
}
|
|
194
|
+
if (!Array.isArray(value.files)) {
|
|
195
|
+
throw new PluginError(`${at}: files: must be an array`);
|
|
196
|
+
}
|
|
197
|
+
const seen = new Set<string>();
|
|
198
|
+
const files = value.files.map((item: unknown, index: number) => {
|
|
199
|
+
const where = `${at}: files[${index}]`;
|
|
200
|
+
if (!isRecord(item)) throw new PluginError(`${where}: must be an object`);
|
|
201
|
+
const { name, path } = item;
|
|
202
|
+
if (typeof name !== "string" || !/^[a-z][a-z0-9-]*$/.test(name)) {
|
|
203
|
+
throw new PluginError(`${where}: name: must be lowercase letters, digits and -`);
|
|
204
|
+
}
|
|
205
|
+
if (seen.has(name)) throw new PluginError(`${where}: name "${name}" is declared twice`);
|
|
206
|
+
seen.add(name);
|
|
207
|
+
if (
|
|
208
|
+
typeof path !== "string" ||
|
|
209
|
+
!path.endsWith(".json") ||
|
|
210
|
+
path.startsWith("/") ||
|
|
211
|
+
path.split("/").some((part) => part === ".." || part === "" || part === ".")
|
|
212
|
+
) {
|
|
213
|
+
throw new PluginError(
|
|
214
|
+
`${where}: path: must be a relative .json path under .operad/ without ".."`,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
let schema: ActionArgsSchema | undefined;
|
|
218
|
+
try {
|
|
219
|
+
schema = normalizeActionArgs(item.schema, `${where}: schema`);
|
|
220
|
+
} catch (error) {
|
|
221
|
+
throw new PluginError(messageOf(error), { cause: error });
|
|
222
|
+
}
|
|
223
|
+
if (schema === undefined) throw new PluginError(`${where}: schema: is required`);
|
|
224
|
+
return { name, path, schema };
|
|
225
|
+
});
|
|
226
|
+
return {
|
|
227
|
+
repositories: value.repositories as ProjectSettingsDeclaration["repositories"],
|
|
228
|
+
files,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function normalizeKeys(value: unknown, at: string): KeyBindings {
|
|
233
|
+
if (value === undefined) return {};
|
|
234
|
+
if (!isRecord(value)) {
|
|
235
|
+
throw new PluginError(`${at}: must be an object`);
|
|
236
|
+
}
|
|
237
|
+
const keys: Record<string, string> = {};
|
|
238
|
+
for (const [key, target] of Object.entries(value)) {
|
|
239
|
+
if (key.trim().length === 0) {
|
|
240
|
+
throw new PluginError(`${at}: a key must be a non-empty string`);
|
|
241
|
+
}
|
|
242
|
+
if (typeof target !== "string" || target.trim().length === 0) {
|
|
243
|
+
throw new PluginError(`${at}.${key}: must name an action as "<plugin>:<action>"`);
|
|
244
|
+
}
|
|
245
|
+
keys[key] = target;
|
|
246
|
+
}
|
|
247
|
+
return keys;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** The default rules a plugin ships, checked by the same validator as the file. */
|
|
251
|
+
function normalizeRules(
|
|
252
|
+
value: unknown,
|
|
253
|
+
at: string,
|
|
254
|
+
): readonly TriggerRule[] {
|
|
255
|
+
if (value === undefined) return [];
|
|
256
|
+
try {
|
|
257
|
+
return parseTriggerRules(value, `${at}: rules`);
|
|
258
|
+
} catch (error) {
|
|
259
|
+
if (error instanceof TriggerError) {
|
|
260
|
+
throw new PluginError(error.message, { cause: error });
|
|
261
|
+
}
|
|
262
|
+
throw error;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function requireNameText(value: unknown, at: string): string {
|
|
267
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
268
|
+
throw new PluginError(`${at}: must be a non-empty string`);
|
|
269
|
+
}
|
|
270
|
+
return value;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function optionalArray(value: unknown, at: string): readonly unknown[] {
|
|
274
|
+
if (value === undefined) return [];
|
|
275
|
+
if (!Array.isArray(value)) {
|
|
276
|
+
throw new PluginError(`${at}: must be an array`);
|
|
277
|
+
}
|
|
278
|
+
return value as readonly unknown[];
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function normalizeFilters(
|
|
282
|
+
value: unknown,
|
|
283
|
+
at: string,
|
|
284
|
+
): readonly ObjectFilterDefinition[] | undefined {
|
|
285
|
+
if (value === undefined) return undefined;
|
|
286
|
+
if (!Array.isArray(value)) {
|
|
287
|
+
throw new PluginError(`${at}: must be an array of filters`);
|
|
288
|
+
}
|
|
289
|
+
const names = new Set<string>();
|
|
290
|
+
const filters: ObjectFilterDefinition[] = [];
|
|
291
|
+
let defaultName: string | undefined;
|
|
292
|
+
value.forEach((raw: unknown, index: number) => {
|
|
293
|
+
const here = `${at}[${index}]`;
|
|
294
|
+
if (!isRecord(raw)) throw new PluginError(`${here}: must be an object`);
|
|
295
|
+
const name = requireNameText(raw.name, `${here}: name`);
|
|
296
|
+
if (name === ALL_FILTER) {
|
|
297
|
+
throw new PluginError(`${here}: name: "${ALL_FILTER}" is added by the kernel`);
|
|
298
|
+
}
|
|
299
|
+
if (names.has(name)) {
|
|
300
|
+
throw new PluginError(`${here}: name: duplicate filter "${name}"`);
|
|
301
|
+
}
|
|
302
|
+
names.add(name);
|
|
303
|
+
if (!Array.isArray(raw.states) || raw.states.length === 0) {
|
|
304
|
+
throw new PluginError(`${here}: states: must be a non-empty array of state names`);
|
|
305
|
+
}
|
|
306
|
+
const states: string[] = [];
|
|
307
|
+
raw.states.forEach((state: unknown, stateIndex: number) => {
|
|
308
|
+
if (typeof state !== "string" || state.trim().length === 0) {
|
|
309
|
+
throw new PluginError(`${here}: states[${stateIndex}]: must be a non-empty string`);
|
|
310
|
+
}
|
|
311
|
+
if (!states.includes(state)) states.push(state);
|
|
312
|
+
});
|
|
313
|
+
if (raw.default !== undefined && typeof raw.default !== "boolean") {
|
|
314
|
+
throw new PluginError(`${here}: default: must be a boolean`);
|
|
315
|
+
}
|
|
316
|
+
if (raw.default === true) {
|
|
317
|
+
if (defaultName !== undefined) {
|
|
318
|
+
throw new PluginError(
|
|
319
|
+
`${here}: default: "${defaultName}" is already the default filter`,
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
defaultName = name;
|
|
323
|
+
}
|
|
324
|
+
filters.push({ name, states, ...(raw.default === true ? { default: true } : {}) });
|
|
325
|
+
});
|
|
326
|
+
return filters;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function normalizeObjects(
|
|
330
|
+
value: unknown,
|
|
331
|
+
at: string,
|
|
332
|
+
): readonly ObjectTypeDefinition[] {
|
|
333
|
+
const names = new Set<string>();
|
|
334
|
+
const objects: ObjectTypeDefinition[] = [];
|
|
335
|
+
optionalArray(value, at).forEach((entry, index) => {
|
|
336
|
+
if (!isRecord(entry)) {
|
|
337
|
+
throw new PluginError(`${at}[${index}]: must be an object`);
|
|
338
|
+
}
|
|
339
|
+
const name = requireNameText(entry.name, `${at}[${index}]: name`);
|
|
340
|
+
if (names.has(name)) {
|
|
341
|
+
throw new PluginError(`${at}: duplicate object type "${name}"`);
|
|
342
|
+
}
|
|
343
|
+
names.add(name);
|
|
344
|
+
if (entry.prepare !== undefined && typeof entry.prepare !== "function") {
|
|
345
|
+
throw new PluginError(`${at}[${index}]: prepare: must be a function`);
|
|
346
|
+
}
|
|
347
|
+
if (entry.resolve !== undefined && typeof entry.resolve !== "function") {
|
|
348
|
+
throw new PluginError(`${at}[${index}]: resolve: must be a function`);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
let fields: string[] | undefined;
|
|
352
|
+
if (entry.index !== undefined) {
|
|
353
|
+
if (!Array.isArray(entry.index)) {
|
|
354
|
+
throw new PluginError(`${at}[${index}]: index: must be an array of field names`);
|
|
355
|
+
}
|
|
356
|
+
fields = [];
|
|
357
|
+
entry.index.forEach((field: unknown, fieldIndex: number) => {
|
|
358
|
+
if (typeof field !== "string" || field.trim().length === 0) {
|
|
359
|
+
throw new PluginError(
|
|
360
|
+
`${at}[${index}]: index[${fieldIndex}]: must be a non-empty string`,
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
if (fields?.includes(field) === true) {
|
|
364
|
+
throw new PluginError(
|
|
365
|
+
`${at}[${index}]: index: duplicate field "${field}"`,
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
fields?.push(field);
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (
|
|
373
|
+
entry.answeredAutomatically !== undefined &&
|
|
374
|
+
typeof entry.answeredAutomatically !== "function"
|
|
375
|
+
) {
|
|
376
|
+
throw new PluginError(
|
|
377
|
+
`${at}[${index}]: answeredAutomatically: must be a function`,
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const filters = normalizeFilters(entry.filters, `${at}[${index}]: filters`);
|
|
382
|
+
|
|
383
|
+
objects.push({
|
|
384
|
+
name,
|
|
385
|
+
...(entry.answeredAutomatically === undefined
|
|
386
|
+
? {}
|
|
387
|
+
: {
|
|
388
|
+
answeredAutomatically: entry.answeredAutomatically as NonNullable<
|
|
389
|
+
ObjectTypeDefinition["answeredAutomatically"]
|
|
390
|
+
>,
|
|
391
|
+
}),
|
|
392
|
+
...(filters === undefined ? {} : { filters }),
|
|
393
|
+
...(entry.schema === undefined ? {} : { schema: entry.schema }),
|
|
394
|
+
...(entry.resolve === undefined
|
|
395
|
+
? {}
|
|
396
|
+
: {
|
|
397
|
+
resolve: entry.resolve as NonNullable<
|
|
398
|
+
ObjectTypeDefinition["resolve"]
|
|
399
|
+
>,
|
|
400
|
+
}),
|
|
401
|
+
...(entry.prepare === undefined
|
|
402
|
+
? {}
|
|
403
|
+
: { prepare: entry.prepare as NonNullable<ObjectTypeDefinition["prepare"]> }),
|
|
404
|
+
...(fields === undefined ? {} : { index: fields }),
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
return objects;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function normalizeEnforces(value: unknown, at: string): readonly string[] | undefined {
|
|
411
|
+
if (value === undefined) return undefined;
|
|
412
|
+
if (!Array.isArray(value)) {
|
|
413
|
+
throw new PluginError(`${at}: enforces: must be an array of profile field names`);
|
|
414
|
+
}
|
|
415
|
+
const fields: string[] = [];
|
|
416
|
+
value.forEach((field, index) => {
|
|
417
|
+
if (typeof field !== "string" || field.trim().length === 0) {
|
|
418
|
+
throw new PluginError(
|
|
419
|
+
`${at}: enforces[${index}]: must be a non-empty string`,
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
if (fields.includes(field)) {
|
|
423
|
+
throw new PluginError(`${at}: enforces: duplicate field "${field}"`);
|
|
424
|
+
}
|
|
425
|
+
fields.push(field);
|
|
426
|
+
});
|
|
427
|
+
return fields;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function normalizeRequires(value: unknown, at: string): PluginRequires | undefined {
|
|
431
|
+
if (value === undefined) return undefined;
|
|
432
|
+
if (!isRecord(value)) {
|
|
433
|
+
throw new PluginError(`${at}: requires: must be an object`);
|
|
434
|
+
}
|
|
435
|
+
let plugins: Record<string, string> | undefined;
|
|
436
|
+
if (value.plugins !== undefined) {
|
|
437
|
+
if (!isRecord(value.plugins)) {
|
|
438
|
+
throw new PluginError(`${at}: requires.plugins: must be an object`);
|
|
439
|
+
}
|
|
440
|
+
plugins = {};
|
|
441
|
+
for (const [name, range] of Object.entries(value.plugins)) {
|
|
442
|
+
if (name.trim().length === 0) {
|
|
443
|
+
throw new PluginError(`${at}: requires.plugins: a plugin name must be non-empty`);
|
|
444
|
+
}
|
|
445
|
+
if (typeof range !== "string" || !isRangeSyntax(range)) {
|
|
446
|
+
throw new PluginError(
|
|
447
|
+
`${at}: requires.plugins.${name}: must be a supported version range (^, ~, >= or exact)`,
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
plugins[name] = range;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
let api: string | undefined;
|
|
454
|
+
if (value.api !== undefined) {
|
|
455
|
+
if (typeof value.api !== "string" || !isRangeSyntax(value.api)) {
|
|
456
|
+
throw new PluginError(
|
|
457
|
+
`${at}: requires.api: must be a supported version range (^, ~, >= or exact)`,
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
api = value.api;
|
|
461
|
+
}
|
|
462
|
+
return { ...(plugins === undefined ? {} : { plugins }), ...(api === undefined ? {} : { api }) };
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** A step's `calls`: a list of distinct `plugin/step` names. */
|
|
466
|
+
function normalizeCalls(value: unknown, at: string): readonly string[] {
|
|
467
|
+
if (!Array.isArray(value)) {
|
|
468
|
+
throw new PluginError(`${at}: calls: must be an array`);
|
|
469
|
+
}
|
|
470
|
+
const seen = new Set<string>();
|
|
471
|
+
for (const [index, call] of (value as readonly unknown[]).entries()) {
|
|
472
|
+
if (typeof call !== "string" || !/^[^/\s]+\/[^/\s]+$/.test(call)) {
|
|
473
|
+
throw new PluginError(
|
|
474
|
+
`${at}: calls[${index}]: must be a step name like "plugin/step"`,
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
if (seen.has(call)) {
|
|
478
|
+
throw new PluginError(`${at}: calls[${index}]: duplicate "${call}"`);
|
|
479
|
+
}
|
|
480
|
+
seen.add(call);
|
|
481
|
+
}
|
|
482
|
+
return [...seen];
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function normalizeSteps(
|
|
486
|
+
value: unknown,
|
|
487
|
+
at: string,
|
|
488
|
+
): readonly StepTypeDefinition[] {
|
|
489
|
+
const names = new Set<string>();
|
|
490
|
+
const steps: StepTypeDefinition[] = [];
|
|
491
|
+
optionalArray(value, at).forEach((entry, index) => {
|
|
492
|
+
if (!isRecord(entry)) {
|
|
493
|
+
throw new PluginError(`${at}[${index}]: must be an object`);
|
|
494
|
+
}
|
|
495
|
+
const name = requireNameText(entry.name, `${at}[${index}]: name`);
|
|
496
|
+
if (names.has(name)) {
|
|
497
|
+
throw new PluginError(`${at}: duplicate step "${name}"`);
|
|
498
|
+
}
|
|
499
|
+
names.add(name);
|
|
500
|
+
|
|
501
|
+
const rawResults = entry.results;
|
|
502
|
+
if (!Array.isArray(rawResults)) {
|
|
503
|
+
throw new PluginError(`${at}[${index}]: results: must be an array`);
|
|
504
|
+
}
|
|
505
|
+
const results = rawResults as readonly unknown[];
|
|
506
|
+
const seen = new Set<string>();
|
|
507
|
+
const normalizedResults: string[] = [];
|
|
508
|
+
results.forEach((result, resultIndex) => {
|
|
509
|
+
if (typeof result !== "string" || result.trim().length === 0) {
|
|
510
|
+
throw new PluginError(
|
|
511
|
+
`${at}[${index}]: results[${resultIndex}]: must be a non-empty string`,
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
if (seen.has(result)) {
|
|
515
|
+
throw new PluginError(
|
|
516
|
+
`${at}: step "${name}": duplicate result "${result}"`,
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
seen.add(result);
|
|
520
|
+
normalizedResults.push(result);
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
if (typeof entry.run !== "function") {
|
|
524
|
+
throw new PluginError(`${at}[${index}]: run: must be a function`);
|
|
525
|
+
}
|
|
526
|
+
steps.push({
|
|
527
|
+
name,
|
|
528
|
+
results: normalizedResults,
|
|
529
|
+
...(entry.calls === undefined
|
|
530
|
+
? {}
|
|
531
|
+
: { calls: normalizeCalls(entry.calls, `${at}[${index}]`) }),
|
|
532
|
+
...(entry.grants === undefined
|
|
533
|
+
? {}
|
|
534
|
+
: { grants: normalizeFlag(entry.grants, "grants", `${at}[${index}]`) }),
|
|
535
|
+
...(entry.callsNamedByData === undefined
|
|
536
|
+
? {}
|
|
537
|
+
: {
|
|
538
|
+
callsNamedByData: normalizeFlag(
|
|
539
|
+
entry.callsNamedByData,
|
|
540
|
+
"callsNamedByData",
|
|
541
|
+
`${at}[${index}]`,
|
|
542
|
+
),
|
|
543
|
+
}),
|
|
544
|
+
...(entry.resumable === undefined
|
|
545
|
+
? {}
|
|
546
|
+
: { resumable: normalizeResumable(entry.resumable, `${at}[${index}]`) }),
|
|
547
|
+
...(entry.enforces === undefined
|
|
548
|
+
? {}
|
|
549
|
+
: { enforces: normalizeEnforces(entry.enforces, `${at}[${index}]`) }),
|
|
550
|
+
...(entry.timeoutSecs === undefined
|
|
551
|
+
? {}
|
|
552
|
+
: { timeoutSecs: normalizeTimeoutSecs(entry.timeoutSecs, `${at}[${index}]`) }),
|
|
553
|
+
...(entry.limit === undefined
|
|
554
|
+
? {}
|
|
555
|
+
: { limit: normalizeLimit(entry.limit, `${at}[${index}]`, name) }),
|
|
556
|
+
run: entry.run as StepTypeDefinition["run"],
|
|
557
|
+
});
|
|
558
|
+
});
|
|
559
|
+
return steps;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
function normalizeActions(
|
|
563
|
+
value: unknown,
|
|
564
|
+
at: string,
|
|
565
|
+
): readonly ActionTypeDefinition[] {
|
|
566
|
+
const names = new Set<string>();
|
|
567
|
+
const actions: ActionTypeDefinition[] = [];
|
|
568
|
+
optionalArray(value, at).forEach((entry, index) => {
|
|
569
|
+
if (!isRecord(entry)) {
|
|
570
|
+
throw new PluginError(`${at}[${index}]: must be an object`);
|
|
571
|
+
}
|
|
572
|
+
const name = requireNameText(entry.name, `${at}[${index}]: name`);
|
|
573
|
+
const on = requireNameText(entry.on, `${at}[${index}]: on`);
|
|
574
|
+
const key = `${name}\u0000${on}`;
|
|
575
|
+
if (names.has(key)) {
|
|
576
|
+
throw new PluginError(
|
|
577
|
+
`${at}: duplicate action "${name}" on object type "${on}"`,
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
names.add(key);
|
|
581
|
+
|
|
582
|
+
if (typeof entry.when !== "function") {
|
|
583
|
+
throw new PluginError(`${at}[${index}]: when: must be a function`);
|
|
584
|
+
}
|
|
585
|
+
if (typeof entry.run !== "function") {
|
|
586
|
+
throw new PluginError(`${at}[${index}]: run: must be a function`);
|
|
587
|
+
}
|
|
588
|
+
if (entry.note !== undefined) {
|
|
589
|
+
throw new PluginError(
|
|
590
|
+
`${at}[${index}]: note: is no longer a field; declare a string property with role "note" in args`,
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
let args: ActionArgsSchema | undefined;
|
|
595
|
+
try {
|
|
596
|
+
args = normalizeActionArgs(entry.args, `${at}[${index}]: args`);
|
|
597
|
+
} catch (error) {
|
|
598
|
+
throw new PluginError(messageOf(error), {
|
|
599
|
+
cause: error,
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const confirm = entry.confirm;
|
|
604
|
+
if (confirm !== undefined && typeof confirm !== "boolean") {
|
|
605
|
+
throw new PluginError(`${at}[${index}]: confirm: must be a boolean`);
|
|
606
|
+
}
|
|
607
|
+
if (
|
|
608
|
+
entry.creates !== undefined &&
|
|
609
|
+
(typeof entry.creates !== "string" || entry.creates.length === 0)
|
|
610
|
+
) {
|
|
611
|
+
throw new PluginError(`${at}[${index}]: creates: must be a non-empty string`);
|
|
612
|
+
}
|
|
613
|
+
actions.push({
|
|
614
|
+
name,
|
|
615
|
+
on,
|
|
616
|
+
...(typeof entry.creates === "string" ? { creates: entry.creates } : {}),
|
|
617
|
+
...(args === undefined ? {} : { args }),
|
|
618
|
+
...(confirm === undefined ? {} : { confirm }),
|
|
619
|
+
...(entry.ends === true ? { ends: true } : {}),
|
|
620
|
+
when: entry.when as ActionTypeDefinition["when"],
|
|
621
|
+
...(typeof entry.rank === "function"
|
|
622
|
+
? { rank: entry.rank as NonNullable<ActionTypeDefinition["rank"]> }
|
|
623
|
+
: {}),
|
|
624
|
+
...(typeof entry.argsFor === "function"
|
|
625
|
+
? { argsFor: entry.argsFor as NonNullable<ActionTypeDefinition["argsFor"]> }
|
|
626
|
+
: {}),
|
|
627
|
+
...(typeof entry.settled === "function"
|
|
628
|
+
? { settled: entry.settled as NonNullable<ActionTypeDefinition["settled"]> }
|
|
629
|
+
: {}),
|
|
630
|
+
...(typeof entry.why === "function"
|
|
631
|
+
? { why: entry.why as NonNullable<ActionTypeDefinition["why"]> }
|
|
632
|
+
: {}),
|
|
633
|
+
run: entry.run as ActionTypeDefinition["run"],
|
|
634
|
+
});
|
|
635
|
+
});
|
|
636
|
+
return actions;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
function normalizeCommands(
|
|
640
|
+
value: unknown,
|
|
641
|
+
at: string,
|
|
642
|
+
): readonly CommandDefinition[] {
|
|
643
|
+
const names = new Set<string>();
|
|
644
|
+
const commands: CommandDefinition[] = [];
|
|
645
|
+
optionalArray(value, at).forEach((entry, index) => {
|
|
646
|
+
if (!isRecord(entry)) {
|
|
647
|
+
throw new PluginError(`${at}[${index}]: must be an object`);
|
|
648
|
+
}
|
|
649
|
+
const name = requireNameText(entry.name, `${at}[${index}]: name`);
|
|
650
|
+
if (names.has(name)) {
|
|
651
|
+
throw new PluginError(`${at}: duplicate command "${name}"`);
|
|
652
|
+
}
|
|
653
|
+
names.add(name);
|
|
654
|
+
|
|
655
|
+
if (typeof entry.args !== "string") {
|
|
656
|
+
throw new PluginError(`${at}[${index}]: args: must be a string`);
|
|
657
|
+
}
|
|
658
|
+
if (typeof entry.run !== "function") {
|
|
659
|
+
throw new PluginError(`${at}[${index}]: run: must be a function`);
|
|
660
|
+
}
|
|
661
|
+
commands.push({
|
|
662
|
+
name,
|
|
663
|
+
args: entry.args,
|
|
664
|
+
run: entry.run as CommandDefinition["run"],
|
|
665
|
+
});
|
|
666
|
+
});
|
|
667
|
+
return commands;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
export function normalizePlugin(raw: unknown, index: number): NormalizedPlugin {
|
|
671
|
+
if (!isRecord(raw)) {
|
|
672
|
+
throw new PluginError(`plugin[${index}]: must be an object`);
|
|
673
|
+
}
|
|
674
|
+
const name = requireNameText(raw.name, `plugin[${index}]: name`);
|
|
675
|
+
const at = `plugin "${name}"`;
|
|
676
|
+
|
|
677
|
+
if (raw.operadPlugin !== OPERAD_PLUGIN_VERSION) {
|
|
678
|
+
throw new PluginError(
|
|
679
|
+
`${at}: operadPlugin: expected ${OPERAD_PLUGIN_VERSION}, got ${JSON.stringify(raw.operadPlugin)}`,
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
const kind = raw.kind;
|
|
684
|
+
if (!isPluginKind(kind)) {
|
|
685
|
+
throw new PluginError(
|
|
686
|
+
`${at}: kind: must be one of ${KINDS.map((item) => `"${item}"`).join(", ")}`,
|
|
687
|
+
);
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
const version = requireNameText(raw.version, `${at}: version`);
|
|
691
|
+
|
|
692
|
+
let onTick: ((ctx: TickContext) => MaybePromise<void>) | undefined;
|
|
693
|
+
if (raw.onTick !== undefined) {
|
|
694
|
+
if (typeof raw.onTick !== "function") {
|
|
695
|
+
throw new PluginError(`${at}: onTick: must be a function`);
|
|
696
|
+
}
|
|
697
|
+
onTick = raw.onTick as (ctx: TickContext) => MaybePromise<void>;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
let standing: NormalizedPlugin["standing"];
|
|
701
|
+
if (raw.standing !== undefined) {
|
|
702
|
+
if (typeof raw.standing !== "function") {
|
|
703
|
+
throw new PluginError(`${at}: standing: must be a function`);
|
|
704
|
+
}
|
|
705
|
+
standing = raw.standing as NormalizedPlugin["standing"];
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
let check:
|
|
709
|
+
| ((ctx: PluginBaseContext) => MaybePromise<string | void>)
|
|
710
|
+
| undefined;
|
|
711
|
+
if (raw.check !== undefined) {
|
|
712
|
+
if (typeof raw.check !== "function") {
|
|
713
|
+
throw new PluginError(`${at}: check: must be a function`);
|
|
714
|
+
}
|
|
715
|
+
check = raw.check as (
|
|
716
|
+
ctx: PluginBaseContext,
|
|
717
|
+
) => MaybePromise<string | void>;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
let start: NormalizedPlugin["start"];
|
|
721
|
+
if (raw.start !== undefined) {
|
|
722
|
+
if (typeof raw.start !== "function") {
|
|
723
|
+
throw new PluginError(`${at}: start: must be a function`);
|
|
724
|
+
}
|
|
725
|
+
start = raw.start as NormalizedPlugin["start"];
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
const objects = normalizeObjects(raw.objects, `${at}: objects`);
|
|
729
|
+
|
|
730
|
+
let settingsSchema: ActionArgsSchema | undefined;
|
|
731
|
+
try {
|
|
732
|
+
settingsSchema = normalizeActionArgs(raw.settings, `${at}: settings`);
|
|
733
|
+
} catch (error) {
|
|
734
|
+
throw new PluginError(messageOf(error), { cause: error });
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
if (
|
|
738
|
+
raw.objectsUnderSettings !== undefined &&
|
|
739
|
+
typeof raw.objectsUnderSettings !== "boolean"
|
|
740
|
+
) {
|
|
741
|
+
throw new PluginError(`${at}: objectsUnderSettings: must be a boolean`);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const projectSettings = normalizeProjectSettings(
|
|
745
|
+
raw.projectSettings,
|
|
746
|
+
`${at}: projectSettings`,
|
|
747
|
+
);
|
|
748
|
+
|
|
749
|
+
return {
|
|
750
|
+
name,
|
|
751
|
+
kind,
|
|
752
|
+
version,
|
|
753
|
+
objects,
|
|
754
|
+
actions: normalizeActions(raw.actions, `${at}: actions`),
|
|
755
|
+
steps: checkHarnessResults(
|
|
756
|
+
normalizeSteps(raw.steps, `${at}: steps`),
|
|
757
|
+
kind,
|
|
758
|
+
at,
|
|
759
|
+
),
|
|
760
|
+
flows: optionalArray(raw.flows, `${at}: flows`),
|
|
761
|
+
commands: normalizeCommands(raw.commands, `${at}: commands`),
|
|
762
|
+
views: normalizeViews(raw.views, `${at}: views`, objects),
|
|
763
|
+
keys: normalizeKeys(raw.keys, `${at}: keys`),
|
|
764
|
+
rules: normalizeRules(raw.rules, at),
|
|
765
|
+
onTick,
|
|
766
|
+
standing,
|
|
767
|
+
start,
|
|
768
|
+
check,
|
|
769
|
+
settingsSchema,
|
|
770
|
+
objectsUnderSettings: raw.objectsUnderSettings === true,
|
|
771
|
+
projectSettings,
|
|
772
|
+
enforces: normalizeEnforces(raw.enforces, `${at}: enforces`) ?? [],
|
|
773
|
+
models: normalizeModels(raw.models, kind, at),
|
|
774
|
+
efforts: normalizeEfforts(raw.efforts, kind, at),
|
|
775
|
+
requires: normalizeRequires(raw.requires, at),
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/** The models a harness plugin declares: a list of names and the default among them, or null. */
|
|
780
|
+
function normalizeModels(
|
|
781
|
+
value: unknown,
|
|
782
|
+
kind: string,
|
|
783
|
+
at: string,
|
|
784
|
+
): HarnessModels | ((settings: Readonly<Record<string, JsonValue>>) => HarnessModels) | undefined {
|
|
785
|
+
if (typeof value === "function") {
|
|
786
|
+
if (kind !== "harness") throw new PluginError(`${at}: models: only a harness plugin declares models`);
|
|
787
|
+
const source = value as (settings: Readonly<Record<string, JsonValue>>) => unknown;
|
|
788
|
+
return (settings) => {
|
|
789
|
+
const levels = normalizeLevels(source(settings), kind, at, "models");
|
|
790
|
+
return levels === undefined ? { list: [], default: null } : { list: levels.list, default: levels.fallback };
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
const levels = normalizeLevels(value, kind, at, "models");
|
|
794
|
+
return levels === undefined ? undefined : { list: levels.list, default: levels.fallback };
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* The efforts a harness plugin declares: a list of levels, the default among
|
|
799
|
+
* them or null, and the step argument that sets one.
|
|
800
|
+
*/
|
|
801
|
+
function normalizeEfforts(value: unknown, kind: string, at: string): HarnessEfforts | undefined {
|
|
802
|
+
const levels = normalizeLevels(value, kind, at, "efforts");
|
|
803
|
+
if (levels === undefined) return undefined;
|
|
804
|
+
const flag = value === undefined ? undefined : (value as Record<string, unknown>).flag;
|
|
805
|
+
if (flag !== undefined && (typeof flag !== "string" || flag.length === 0)) {
|
|
806
|
+
throw new PluginError(`${at}: efforts.flag: must be a non-empty string`);
|
|
807
|
+
}
|
|
808
|
+
return {
|
|
809
|
+
list: levels.list,
|
|
810
|
+
default: levels.fallback,
|
|
811
|
+
...(typeof flag === "string" ? { flag } : {}),
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
/** The list and fallback of a harness plugin's `models` or `efforts`. */
|
|
816
|
+
function normalizeLevels(
|
|
817
|
+
value: unknown,
|
|
818
|
+
kind: string,
|
|
819
|
+
at: string,
|
|
820
|
+
field: "models" | "efforts",
|
|
821
|
+
): { readonly list: readonly string[]; readonly fallback: string | null } | undefined {
|
|
822
|
+
if (value === undefined) return undefined;
|
|
823
|
+
if (kind !== "harness") throw new PluginError(`${at}: ${field}: only a harness plugin declares ${field}`);
|
|
824
|
+
if (!isRecord(value) || !Array.isArray(value.list)) throw new PluginError(`${at}: ${field}: must be { list, default }`);
|
|
825
|
+
const list = value.list.map((item, index) => {
|
|
826
|
+
if (typeof item !== "string" || item.length === 0) throw new PluginError(`${at}: ${field}.list[${index}]: must be a non-empty string`);
|
|
827
|
+
return item;
|
|
828
|
+
});
|
|
829
|
+
const fallback = value.default === undefined ? null : value.default;
|
|
830
|
+
if (fallback !== null && (typeof fallback !== "string" || !list.includes(fallback))) {
|
|
831
|
+
throw new PluginError(`${at}: ${field}.default: must be null or one of the list`);
|
|
832
|
+
}
|
|
833
|
+
return { list, fallback };
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
/** A harness step type ends only in the standard end results. */
|
|
837
|
+
function checkHarnessResults(
|
|
838
|
+
steps: readonly StepTypeDefinition[],
|
|
839
|
+
kind: string,
|
|
840
|
+
at: string,
|
|
841
|
+
): readonly StepTypeDefinition[] {
|
|
842
|
+
if (kind !== "harness") return steps;
|
|
843
|
+
for (const step of steps) {
|
|
844
|
+
for (const result of step.results) {
|
|
845
|
+
if (!isEndResult(result)) {
|
|
846
|
+
throw new PluginError(
|
|
847
|
+
`${at}: step "${step.name}": result "${result}" is not an end result; a harness step ends in ${END_RESULTS.join(", ")}`,
|
|
848
|
+
);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
return steps;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
function normalizeLimit(
|
|
856
|
+
value: unknown,
|
|
857
|
+
at: string,
|
|
858
|
+
step: string,
|
|
859
|
+
): StepLimitDefinition {
|
|
860
|
+
const where = at.replace(/: steps\[\d+\]$/, "");
|
|
861
|
+
const fail = (message: string): never => {
|
|
862
|
+
throw new PluginError(`${where}: step "${step}": limit: ${message}`);
|
|
863
|
+
};
|
|
864
|
+
if (!isRecord(value)) return fail("must be { key, concurrent }");
|
|
865
|
+
for (const field of Object.keys(value)) {
|
|
866
|
+
if (field !== "key" && field !== "concurrent") {
|
|
867
|
+
return fail(`unknown field "${field}"; expected key and concurrent`);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
if (typeof value.key !== "function") {
|
|
871
|
+
return fail("key: must be a function of (params, settings)");
|
|
872
|
+
}
|
|
873
|
+
if (typeof value.concurrent !== "function") {
|
|
874
|
+
return fail("concurrent: must be a function of (params, settings)");
|
|
875
|
+
}
|
|
876
|
+
return {
|
|
877
|
+
key: value.key as StepLimitDefinition["key"],
|
|
878
|
+
concurrent: value.concurrent as StepLimitDefinition["concurrent"],
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
function normalizeTimeoutSecs(value: unknown, at: string): number {
|
|
883
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
884
|
+
throw new PluginError(`${at}: timeoutSecs: must be a positive number of seconds`);
|
|
885
|
+
}
|
|
886
|
+
return value;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
/**
|
|
890
|
+
* What is wrong with the one-time permission a question asks for, if anything.
|
|
891
|
+
* It is found before the person is asked: a permission that could never be
|
|
892
|
+
* used, or that any answer would give, must not be offered.
|
|
893
|
+
*/
|
|
894
|
+
export function grantRequestProblem(
|
|
895
|
+
request: AskRequest,
|
|
896
|
+
grants: boolean | undefined,
|
|
897
|
+
): string | undefined {
|
|
898
|
+
const resume: unknown = request.resume;
|
|
899
|
+
if (!isRecord(resume) || resume.grant === undefined) return undefined;
|
|
900
|
+
const grant: unknown = resume.grant;
|
|
901
|
+
if (grants === false) {
|
|
902
|
+
return "resume.grant: this step cannot enforce a one-time permission, so it cannot ask for one";
|
|
903
|
+
}
|
|
904
|
+
if (
|
|
905
|
+
!isRecord(grant) ||
|
|
906
|
+
(grant.kind !== "command" && grant.kind !== "path") ||
|
|
907
|
+
typeof grant.value !== "string" ||
|
|
908
|
+
grant.value.length === 0
|
|
909
|
+
) {
|
|
910
|
+
return 'resume.grant: must be { kind: "command" | "path", value, choice?, tool? }';
|
|
911
|
+
}
|
|
912
|
+
if (grant.kind === "path" && (typeof grant.tool !== "string" || grant.tool.length === 0)) {
|
|
913
|
+
return "resume.grant.tool: a path grant names the tool the person approves (such as write)";
|
|
914
|
+
}
|
|
915
|
+
const choices = request.choices ?? [];
|
|
916
|
+
if (choices.length > 0) {
|
|
917
|
+
if (typeof grant.choice !== "string") {
|
|
918
|
+
return "resume.grant.choice: a grant needs the choice that approves it";
|
|
919
|
+
}
|
|
920
|
+
if (!choices.includes(grant.choice)) {
|
|
921
|
+
return "resume.grant.choice: must be one of the choices";
|
|
922
|
+
}
|
|
923
|
+
} else if (grant.choice !== undefined && typeof grant.choice !== "string") {
|
|
924
|
+
return "resume.grant.choice: must be a string";
|
|
925
|
+
}
|
|
926
|
+
return undefined;
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
function normalizeFlag(value: unknown, field: string, at: string): boolean {
|
|
930
|
+
if (typeof value !== "boolean") {
|
|
931
|
+
throw new PluginError(`${at}: ${field}: must be true or false`);
|
|
932
|
+
}
|
|
933
|
+
return value;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
function normalizeResumable(value: unknown, at: string): boolean {
|
|
937
|
+
return normalizeFlag(value, "resumable", at);
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
export function collectStepTypes(plugins: readonly NormalizedPlugin[]): readonly StepType[] {
|
|
941
|
+
return plugins.flatMap((plugin) =>
|
|
942
|
+
plugin.steps.map((step) => ({
|
|
943
|
+
name: `${plugin.name}/${step.name}`,
|
|
944
|
+
results: step.results,
|
|
945
|
+
...(step.resumable === undefined ? {} : { resumable: step.resumable }),
|
|
946
|
+
...(step.enforces === undefined ? {} : { enforces: step.enforces }),
|
|
947
|
+
...(step.timeoutSecs === undefined ? {} : { timeoutSecs: step.timeoutSecs }),
|
|
948
|
+
})),
|
|
949
|
+
);
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
export function validatePluginSet(plugins: readonly NormalizedPlugin[]): void {
|
|
953
|
+
const names = new Set<string>();
|
|
954
|
+
const objects = new Set<string>();
|
|
955
|
+
|
|
956
|
+
for (const plugin of plugins) {
|
|
957
|
+
if (names.has(plugin.name)) {
|
|
958
|
+
throw new PluginError(`plugin "${plugin.name}": duplicate plugin name`);
|
|
959
|
+
}
|
|
960
|
+
names.add(plugin.name);
|
|
961
|
+
|
|
962
|
+
for (const object of plugin.objects) {
|
|
963
|
+
if (objects.has(object.name)) {
|
|
964
|
+
throw new PluginError(
|
|
965
|
+
`plugin "${plugin.name}": object "${object.name}": another plugin already owns it`,
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
objects.add(object.name);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
for (const plugin of plugins) {
|
|
973
|
+
for (const action of plugin.actions) {
|
|
974
|
+
if (!objects.has(action.on)) {
|
|
975
|
+
throw new PluginError(
|
|
976
|
+
`plugin "${plugin.name}": action "${action.name}": on: unknown object type "${action.on}"`,
|
|
977
|
+
);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
export function validateFlows(
|
|
984
|
+
plugins: readonly NormalizedPlugin[],
|
|
985
|
+
stepTypes: readonly StepType[],
|
|
986
|
+
): readonly LoadedPlugin[] {
|
|
987
|
+
return plugins.map((plugin) => {
|
|
988
|
+
const flows: FlowDefinition[] = [];
|
|
989
|
+
const names = new Set<string>();
|
|
990
|
+
for (const raw of plugin.flows) {
|
|
991
|
+
let flow: FlowDefinition;
|
|
992
|
+
try {
|
|
993
|
+
flow = validateFlow(raw, stepTypes);
|
|
994
|
+
} catch (error) {
|
|
995
|
+
if (error instanceof FlowError) {
|
|
996
|
+
throw new PluginError(`plugin "${plugin.name}": ${error.message}`, {
|
|
997
|
+
cause: error,
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
throw error;
|
|
1001
|
+
}
|
|
1002
|
+
if (names.has(flow.name)) {
|
|
1003
|
+
throw new PluginError(
|
|
1004
|
+
`plugin "${plugin.name}": flows: duplicate flow "${flow.name}"`,
|
|
1005
|
+
);
|
|
1006
|
+
}
|
|
1007
|
+
names.add(flow.name);
|
|
1008
|
+
flows.push(flow);
|
|
1009
|
+
}
|
|
1010
|
+
return { ...plugin, flows };
|
|
1011
|
+
});
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
/** A plugin's default rule must start a flow some loaded plugin declares. */
|
|
1015
|
+
export function validateRuleFlows(plugins: readonly LoadedPlugin[]): void {
|
|
1016
|
+
const flowNames = new Set(
|
|
1017
|
+
plugins.flatMap((plugin) => plugin.flows.map((flow) => flow.name)),
|
|
1018
|
+
);
|
|
1019
|
+
for (const plugin of plugins) {
|
|
1020
|
+
for (const [index, rule] of plugin.rules.entries()) {
|
|
1021
|
+
if (!flowNames.has(rule.start.flow)) {
|
|
1022
|
+
throw new PluginError(
|
|
1023
|
+
`plugin "${plugin.name}": rules: rule ${index}: start.flow: unknown flow "${rule.start.flow}"`,
|
|
1024
|
+
);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
}
|