@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,627 @@
|
|
|
1
|
+
/** The operad home config: JSON, read by the kernel, never by a plugin kind. */
|
|
2
|
+
|
|
3
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
import { defaultOperadHome } from "../plugin/home.ts";
|
|
6
|
+
import { MAX_ASK_GRACE_SECS } from "../plugin/types.ts";
|
|
7
|
+
import {
|
|
8
|
+
openLog,
|
|
9
|
+
type LogBackend,
|
|
10
|
+
type LogBackendName,
|
|
11
|
+
type LogBackendOptions,
|
|
12
|
+
} from "../log/index.ts";
|
|
13
|
+
import { SQLITE_FILE_NAME } from "../log/sqlite.ts";
|
|
14
|
+
import { isRecord } from "../util.ts";
|
|
15
|
+
|
|
16
|
+
/** The storage backend the log uses. */
|
|
17
|
+
export type StoreBackend = LogBackendName;
|
|
18
|
+
|
|
19
|
+
/** Where the log lives: the sqlite file. */
|
|
20
|
+
export interface StoreConfig {
|
|
21
|
+
readonly backend: StoreBackend;
|
|
22
|
+
readonly path: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** One plugin listed in `config.json`, with its options. */
|
|
26
|
+
export interface PluginConfigEntry {
|
|
27
|
+
/** The name the config keyed the plugin by, when it keyed one. */
|
|
28
|
+
readonly name: string | undefined;
|
|
29
|
+
/** The module name or path to import. */
|
|
30
|
+
readonly module: string;
|
|
31
|
+
readonly enabled: boolean;
|
|
32
|
+
/** The raw section handed to the plugin's settings schema. */
|
|
33
|
+
readonly settings: unknown;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The HTTP interface `serve` exposes, if it is enabled. */
|
|
37
|
+
export interface HttpConfig {
|
|
38
|
+
readonly enabled: boolean;
|
|
39
|
+
readonly port: number;
|
|
40
|
+
/** IP literals to listen on; defaults to loopback only. */
|
|
41
|
+
readonly listen: readonly string[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The HTTP defaults: on, port 7480, loopback only. */
|
|
45
|
+
export const DEFAULT_HTTP: HttpConfig = {
|
|
46
|
+
enabled: true,
|
|
47
|
+
port: 7480,
|
|
48
|
+
listen: ["127.0.0.1"],
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** What can start a notification. */
|
|
52
|
+
export type NotifyTrigger = "waiting";
|
|
53
|
+
|
|
54
|
+
/** The command `serve` runs when something waits for a person. */
|
|
55
|
+
export interface NotifyConfig {
|
|
56
|
+
/** A shell line, or null for no notifications. */
|
|
57
|
+
readonly command: string | null;
|
|
58
|
+
readonly on: readonly NotifyTrigger[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The notify defaults: no command, and the one trigger there is. */
|
|
62
|
+
export const DEFAULT_NOTIFY: NotifyConfig = {
|
|
63
|
+
command: null,
|
|
64
|
+
on: ["waiting"],
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** What the MCP channel sends a block for. */
|
|
68
|
+
export type ChannelNotifyKind = "waiting" | "ended" | "loop" | "changes";
|
|
69
|
+
|
|
70
|
+
/** The kinds `mcp.channel.notify` accepts. */
|
|
71
|
+
export const CHANNEL_NOTIFY_KINDS: readonly ChannelNotifyKind[] = [
|
|
72
|
+
"waiting",
|
|
73
|
+
"ended",
|
|
74
|
+
"loop",
|
|
75
|
+
"changes",
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
/** When the MCP channel sends a block. */
|
|
79
|
+
export interface McpChannelConfig {
|
|
80
|
+
/** `changes` sends every state change, as well as what else is listed. */
|
|
81
|
+
readonly notify: readonly ChannelNotifyKind[];
|
|
82
|
+
/** How many times in a row an object reaches one state before a loop block. */
|
|
83
|
+
readonly loopAfter: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The channel defaults: waits, ends and loops of three. */
|
|
87
|
+
export const DEFAULT_MCP_CHANNEL: McpChannelConfig = {
|
|
88
|
+
notify: ["waiting", "ended", "loop"],
|
|
89
|
+
loopAfter: 3,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/** The actions the MCP server may run for a lead. */
|
|
93
|
+
export interface McpConfig {
|
|
94
|
+
/** When the channel sends a block. */
|
|
95
|
+
readonly channel: McpChannelConfig;
|
|
96
|
+
/** Actions named `<plugin>:<action>`; every other action is the owner's. */
|
|
97
|
+
readonly actions: readonly string[];
|
|
98
|
+
/** Allowed actions that also need this extra confirmation to run. */
|
|
99
|
+
readonly confirmActions: readonly string[];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The MCP defaults: the channel defaults and no allowed action. A distribution writes the actions it allows into the config. */
|
|
103
|
+
export const DEFAULT_MCP: McpConfig = {
|
|
104
|
+
channel: DEFAULT_MCP_CHANNEL,
|
|
105
|
+
actions: [],
|
|
106
|
+
confirmActions: [],
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** How long `ctx.ask` waits for an answer unless something narrower says. */
|
|
110
|
+
export interface AskConfig {
|
|
111
|
+
readonly graceSecs: number;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The ask defaults: ten minutes. */
|
|
115
|
+
export const DEFAULT_ASK: AskConfig = { graceSecs: 600 };
|
|
116
|
+
|
|
117
|
+
/** The keys of `$OPERAD_HOME/config.json` the kernel reads. */
|
|
118
|
+
export interface OperadConfig {
|
|
119
|
+
readonly plugins: readonly PluginConfigEntry[];
|
|
120
|
+
readonly store: StoreConfig;
|
|
121
|
+
readonly http: HttpConfig;
|
|
122
|
+
readonly notify: NotifyConfig;
|
|
123
|
+
readonly mcp: McpConfig;
|
|
124
|
+
readonly ask: AskConfig;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A config file is not usable; the message names the file and the field. */
|
|
128
|
+
export class ConfigError extends Error {
|
|
129
|
+
constructor(message: string, options?: ErrorOptions) {
|
|
130
|
+
super(message, options);
|
|
131
|
+
this.name = "ConfigError";
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function configPath(home: string): string {
|
|
136
|
+
return join(home, "config.json");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The default config for a home: sqlite at `<home>/store/log.sqlite`, no plugin and no allowed action. */
|
|
140
|
+
export function defaultConfig(home: string): OperadConfig {
|
|
141
|
+
return {
|
|
142
|
+
plugins: [],
|
|
143
|
+
store: { backend: "sqlite", path: join(home, "store", SQLITE_FILE_NAME) },
|
|
144
|
+
http: DEFAULT_HTTP,
|
|
145
|
+
notify: DEFAULT_NOTIFY,
|
|
146
|
+
mcp: DEFAULT_MCP,
|
|
147
|
+
ask: DEFAULT_ASK,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The directory a backend keeps its files in. */
|
|
152
|
+
export function storeRootOf(config: OperadConfig): string {
|
|
153
|
+
return dirname(config.store.path);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function readPlugins(value: unknown, file: string): readonly PluginConfigEntry[] {
|
|
157
|
+
if (value === undefined) return [];
|
|
158
|
+
if (Array.isArray(value)) {
|
|
159
|
+
if (
|
|
160
|
+
!value.every(
|
|
161
|
+
(item): item is string => typeof item === "string" && item.trim().length > 0,
|
|
162
|
+
)
|
|
163
|
+
) {
|
|
164
|
+
throw new ConfigError(
|
|
165
|
+
`${file}: plugins: must be an object of plugin options or an array of module names`,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
return value.map((module) => ({
|
|
169
|
+
name: undefined,
|
|
170
|
+
module: module,
|
|
171
|
+
enabled: true,
|
|
172
|
+
settings: {},
|
|
173
|
+
}));
|
|
174
|
+
}
|
|
175
|
+
if (!isRecord(value)) {
|
|
176
|
+
throw new ConfigError(
|
|
177
|
+
`${file}: plugins: must be an object of plugin options or an array of module names`,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const entries: PluginConfigEntry[] = [];
|
|
182
|
+
for (const [name, raw] of Object.entries(value)) {
|
|
183
|
+
if (typeof raw === "string") {
|
|
184
|
+
if (raw.trim().length === 0) {
|
|
185
|
+
throw new ConfigError(`${file}: plugins.${name}: must be a non-empty module name or path`);
|
|
186
|
+
}
|
|
187
|
+
entries.push({ name, module: raw, enabled: true, settings: {} });
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (!isRecord(raw)) {
|
|
191
|
+
throw new ConfigError(
|
|
192
|
+
`${file}: plugins.${name}: must be a module name or an object of options`,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
const module = raw.module;
|
|
196
|
+
if (
|
|
197
|
+
module !== undefined &&
|
|
198
|
+
(typeof module !== "string" || module.trim().length === 0)
|
|
199
|
+
) {
|
|
200
|
+
throw new ConfigError(`${file}: plugins.${name}.module: must be a non-empty string`);
|
|
201
|
+
}
|
|
202
|
+
const enabled = raw.enabled;
|
|
203
|
+
if (enabled !== undefined && typeof enabled !== "boolean") {
|
|
204
|
+
throw new ConfigError(`${file}: plugins.${name}.enabled: must be a boolean`);
|
|
205
|
+
}
|
|
206
|
+
entries.push({
|
|
207
|
+
name,
|
|
208
|
+
module: module ?? name,
|
|
209
|
+
enabled: enabled !== false,
|
|
210
|
+
settings: raw.settings === undefined ? {} : raw.settings,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
return entries;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function readHttp(value: unknown, file: string): HttpConfig {
|
|
217
|
+
if (value === undefined) return DEFAULT_HTTP;
|
|
218
|
+
if (!isRecord(value)) {
|
|
219
|
+
throw new ConfigError(`${file}: http: must be an object`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const enabled = value.enabled;
|
|
223
|
+
if (enabled !== undefined && typeof enabled !== "boolean") {
|
|
224
|
+
throw new ConfigError(`${file}: http.enabled: must be a boolean`);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const port = value.port;
|
|
228
|
+
if (
|
|
229
|
+
port !== undefined &&
|
|
230
|
+
(typeof port !== "number" ||
|
|
231
|
+
!Number.isInteger(port) ||
|
|
232
|
+
port < 0 ||
|
|
233
|
+
port > 65_535)
|
|
234
|
+
) {
|
|
235
|
+
throw new ConfigError(`${file}: http.port: must be an integer from 0 to 65535`);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const listen = value.listen;
|
|
239
|
+
if (listen !== undefined) {
|
|
240
|
+
if (
|
|
241
|
+
!Array.isArray(listen) ||
|
|
242
|
+
!listen.every(
|
|
243
|
+
(entry): entry is string =>
|
|
244
|
+
typeof entry === "string" && entry.trim().length > 0,
|
|
245
|
+
)
|
|
246
|
+
) {
|
|
247
|
+
throw new ConfigError(`${file}: http.listen: must be an array of IP addresses`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return {
|
|
252
|
+
enabled: enabled === undefined ? DEFAULT_HTTP.enabled : enabled,
|
|
253
|
+
port: port === undefined ? DEFAULT_HTTP.port : port,
|
|
254
|
+
listen: listen === undefined ? DEFAULT_HTTP.listen : listen,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function readAsk(value: unknown, file: string): AskConfig {
|
|
259
|
+
if (value === undefined) return DEFAULT_ASK;
|
|
260
|
+
if (!isRecord(value)) {
|
|
261
|
+
throw new ConfigError(`${file}: ask: must be an object`);
|
|
262
|
+
}
|
|
263
|
+
const graceSecs = value.graceSecs;
|
|
264
|
+
if (
|
|
265
|
+
graceSecs !== undefined &&
|
|
266
|
+
(typeof graceSecs !== "number" ||
|
|
267
|
+
!Number.isFinite(graceSecs) ||
|
|
268
|
+
graceSecs < 0 ||
|
|
269
|
+
graceSecs > MAX_ASK_GRACE_SECS)
|
|
270
|
+
) {
|
|
271
|
+
throw new ConfigError(
|
|
272
|
+
`${file}: ask.graceSecs: must be a number of seconds from 0 to ${MAX_ASK_GRACE_SECS}`,
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
return { graceSecs: graceSecs ?? DEFAULT_ASK.graceSecs };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function readNotify(value: unknown, file: string): NotifyConfig {
|
|
279
|
+
if (value === undefined) return DEFAULT_NOTIFY;
|
|
280
|
+
if (!isRecord(value)) {
|
|
281
|
+
throw new ConfigError(`${file}: notify: must be an object`);
|
|
282
|
+
}
|
|
283
|
+
const command = value.command;
|
|
284
|
+
if (
|
|
285
|
+
command !== undefined &&
|
|
286
|
+
command !== null &&
|
|
287
|
+
(typeof command !== "string" || command.trim().length === 0)
|
|
288
|
+
) {
|
|
289
|
+
throw new ConfigError(
|
|
290
|
+
`${file}: notify.command: must be a non-empty string or null`,
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
const on = value.on;
|
|
294
|
+
if (
|
|
295
|
+
on !== undefined &&
|
|
296
|
+
(!Array.isArray(on) || !on.every((entry) => entry === "waiting"))
|
|
297
|
+
) {
|
|
298
|
+
throw new ConfigError(`${file}: notify.on: must be an array of "waiting"`);
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
command: command ?? null,
|
|
302
|
+
on: on === undefined ? DEFAULT_NOTIFY.on : [...new Set(on as NotifyTrigger[])],
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function readMcp(value: unknown, file: string): McpConfig {
|
|
307
|
+
if (value === undefined) return DEFAULT_MCP;
|
|
308
|
+
if (!isRecord(value)) {
|
|
309
|
+
throw new ConfigError(`${file}: mcp: must be an object`);
|
|
310
|
+
}
|
|
311
|
+
const readNames = (name: "actions" | "confirmActions"): readonly string[] => {
|
|
312
|
+
const raw = value[name];
|
|
313
|
+
if (raw === undefined) return DEFAULT_MCP[name];
|
|
314
|
+
if (
|
|
315
|
+
!Array.isArray(raw) ||
|
|
316
|
+
!raw.every((entry): entry is string => typeof entry === "string" && entry.trim().length > 0)
|
|
317
|
+
) {
|
|
318
|
+
throw new ConfigError(
|
|
319
|
+
`${file}: mcp.${name}: must be an array of "<plugin>:<action>" names`,
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
return [...raw];
|
|
323
|
+
};
|
|
324
|
+
return {
|
|
325
|
+
channel: readChannel(value.channel, file),
|
|
326
|
+
actions: readNames("actions"),
|
|
327
|
+
confirmActions: readNames("confirmActions"),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function readChannel(value: unknown, file: string): McpChannelConfig {
|
|
332
|
+
if (value === undefined) return DEFAULT_MCP_CHANNEL;
|
|
333
|
+
if (!isRecord(value)) {
|
|
334
|
+
throw new ConfigError(`${file}: mcp.channel: must be an object`);
|
|
335
|
+
}
|
|
336
|
+
let notify: readonly ChannelNotifyKind[] = DEFAULT_MCP_CHANNEL.notify;
|
|
337
|
+
if (value.notify !== undefined) {
|
|
338
|
+
if (!Array.isArray(value.notify)) {
|
|
339
|
+
throw new ConfigError(`${file}: mcp.channel.notify: must be an array`);
|
|
340
|
+
}
|
|
341
|
+
const kinds: ChannelNotifyKind[] = [];
|
|
342
|
+
for (const entry of value.notify) {
|
|
343
|
+
const kind = CHANNEL_NOTIFY_KINDS.find((known) => known === entry);
|
|
344
|
+
if (kind === undefined) {
|
|
345
|
+
throw new ConfigError(
|
|
346
|
+
`${file}: mcp.channel.notify: unknown entry ${JSON.stringify(entry)}; use ${CHANNEL_NOTIFY_KINDS.map((known) => `"${known}"`).join(", ")}`,
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
kinds.push(kind);
|
|
350
|
+
}
|
|
351
|
+
notify = kinds;
|
|
352
|
+
}
|
|
353
|
+
const loopAfter = value.loopAfter ?? DEFAULT_MCP_CHANNEL.loopAfter;
|
|
354
|
+
if (typeof loopAfter !== "number" || !Number.isInteger(loopAfter) || loopAfter < 2) {
|
|
355
|
+
throw new ConfigError(
|
|
356
|
+
`${file}: mcp.channel.loopAfter: must be an integer of 2 or more`,
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
return { notify, loopAfter };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function readStore(value: unknown, file: string, home: string): StoreConfig {
|
|
363
|
+
if (value === undefined) return defaultConfig(home).store;
|
|
364
|
+
if (!isRecord(value)) {
|
|
365
|
+
throw new ConfigError(`${file}: store: must be an object`);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const backend = value.backend;
|
|
369
|
+
if (backend !== undefined && backend !== "sqlite") {
|
|
370
|
+
throw new ConfigError(`${file}: store.backend: must be "sqlite"`);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const path = value.path;
|
|
374
|
+
if (
|
|
375
|
+
path !== undefined &&
|
|
376
|
+
(typeof path !== "string" || path.length === 0)
|
|
377
|
+
) {
|
|
378
|
+
throw new ConfigError(`${file}: store.path: must be a non-empty string`);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return {
|
|
382
|
+
backend: "sqlite",
|
|
383
|
+
path: path ?? join(home, "store", SQLITE_FILE_NAME),
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Reads `$OPERAD_HOME/config.json`; a missing file means the default config.
|
|
389
|
+
* Relative store paths are resolved against the home, so the current
|
|
390
|
+
* directory never changes what a command reads.
|
|
391
|
+
*/
|
|
392
|
+
export async function readConfig(
|
|
393
|
+
home = defaultOperadHome(),
|
|
394
|
+
file = configPath(home),
|
|
395
|
+
): Promise<OperadConfig> {
|
|
396
|
+
let text: string;
|
|
397
|
+
try {
|
|
398
|
+
text = await readFile(file, "utf8");
|
|
399
|
+
} catch (error) {
|
|
400
|
+
if (
|
|
401
|
+
typeof error === "object" &&
|
|
402
|
+
error !== null &&
|
|
403
|
+
"code" in error &&
|
|
404
|
+
error.code === "ENOENT"
|
|
405
|
+
) {
|
|
406
|
+
return defaultConfig(home);
|
|
407
|
+
}
|
|
408
|
+
throw new ConfigError(`${file}: ${String(error)}`, { cause: error });
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
let raw: unknown;
|
|
412
|
+
try {
|
|
413
|
+
raw = JSON.parse(text);
|
|
414
|
+
} catch (error) {
|
|
415
|
+
throw new ConfigError(`${file}: invalid JSON: ${String(error)}`, {
|
|
416
|
+
cause: error,
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
if (!isRecord(raw)) {
|
|
420
|
+
throw new ConfigError(`${file}: must be a JSON object`);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const store = readStore(raw.store, file, home);
|
|
424
|
+
const configured = store.path;
|
|
425
|
+
return {
|
|
426
|
+
plugins: readPlugins(raw.plugins, file),
|
|
427
|
+
store: {
|
|
428
|
+
backend: store.backend,
|
|
429
|
+
path: configured.startsWith("/") ? configured : resolve(home, configured),
|
|
430
|
+
},
|
|
431
|
+
http: readHttp(raw.http, file),
|
|
432
|
+
notify: readNotify(raw.notify, file),
|
|
433
|
+
mcp: readMcp(raw.mcp, file),
|
|
434
|
+
ask: readAsk(raw.ask, file),
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** The JSON form of a config, with `plugins` as an object keyed by name. */
|
|
439
|
+
export function configJson(config: OperadConfig): Record<string, unknown> {
|
|
440
|
+
const plugins: Record<string, unknown> = {};
|
|
441
|
+
for (const entry of config.plugins) {
|
|
442
|
+
if (entry.name === undefined) {
|
|
443
|
+
// A legacy array entry has no name to key it by.
|
|
444
|
+
plugins[entry.module] = { module: entry.module, enabled: entry.enabled };
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
plugins[entry.name] = {
|
|
448
|
+
module: entry.module,
|
|
449
|
+
enabled: entry.enabled,
|
|
450
|
+
...(isRecord(entry.settings) && Object.keys(entry.settings).length > 0
|
|
451
|
+
? { settings: entry.settings }
|
|
452
|
+
: {}),
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
return {
|
|
456
|
+
plugins,
|
|
457
|
+
store: config.store,
|
|
458
|
+
http: {
|
|
459
|
+
enabled: config.http.enabled,
|
|
460
|
+
port: config.http.port,
|
|
461
|
+
listen: [...config.http.listen],
|
|
462
|
+
},
|
|
463
|
+
...(config.notify.command === null
|
|
464
|
+
? {}
|
|
465
|
+
: { notify: { command: config.notify.command, on: [...config.notify.on] } }),
|
|
466
|
+
mcp: {
|
|
467
|
+
channel: {
|
|
468
|
+
notify: [...config.mcp.channel.notify],
|
|
469
|
+
loopAfter: config.mcp.channel.loopAfter,
|
|
470
|
+
},
|
|
471
|
+
actions: [...config.mcp.actions],
|
|
472
|
+
confirmActions: [...config.mcp.confirmActions],
|
|
473
|
+
},
|
|
474
|
+
...(config.ask.graceSecs === DEFAULT_ASK.graceSecs
|
|
475
|
+
? {}
|
|
476
|
+
: { ask: { graceSecs: config.ask.graceSecs } }),
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** Writes a config file, replacing it atomically enough for one command. */
|
|
481
|
+
export async function writeConfig(
|
|
482
|
+
home: string,
|
|
483
|
+
config: OperadConfig,
|
|
484
|
+
): Promise<void> {
|
|
485
|
+
await mkdir(home, { recursive: true });
|
|
486
|
+
await writeFile(
|
|
487
|
+
configPath(home),
|
|
488
|
+
`${JSON.stringify(configJson(config), null, 2)}\n`,
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* A config that does not mention `plugins` is a config someone wrote for the
|
|
494
|
+
* store only; `init` installs the given plugins into it and leaves the rest.
|
|
495
|
+
*/
|
|
496
|
+
async function addDefaultPlugins(defaults: OperadConfig, file: string): Promise<void> {
|
|
497
|
+
let raw: unknown;
|
|
498
|
+
try {
|
|
499
|
+
raw = JSON.parse(await readFile(file, "utf8"));
|
|
500
|
+
} catch {
|
|
501
|
+
return; // readConfig reports an unreadable file with its own message
|
|
502
|
+
}
|
|
503
|
+
if (!isRecord(raw) || raw.plugins !== undefined) return;
|
|
504
|
+
const plugins = configJson(defaults).plugins;
|
|
505
|
+
// Write beside the config and rename over it, so a crash or a full disk
|
|
506
|
+
// leaves the old file whole instead of a truncated one.
|
|
507
|
+
const temp = `${file}.tmp`;
|
|
508
|
+
try {
|
|
509
|
+
await writeFile(temp, `${JSON.stringify({ ...raw, plugins }, null, 2)}\n`);
|
|
510
|
+
await rename(temp, file);
|
|
511
|
+
} catch (error) {
|
|
512
|
+
await rm(temp, { force: true }).catch(() => undefined);
|
|
513
|
+
throw error;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Writes `defaults` (the distribution's config for a new home) when the home
|
|
519
|
+
* has none. An existing file is kept; only a missing `plugins` section is
|
|
520
|
+
* filled in from `defaults`. Idempotent.
|
|
521
|
+
*/
|
|
522
|
+
export async function ensureConfig(
|
|
523
|
+
home: string,
|
|
524
|
+
defaults: OperadConfig = defaultConfig(home),
|
|
525
|
+
): Promise<{
|
|
526
|
+
readonly created: boolean;
|
|
527
|
+
readonly path: string;
|
|
528
|
+
readonly config: OperadConfig;
|
|
529
|
+
}> {
|
|
530
|
+
await mkdir(home, { recursive: true });
|
|
531
|
+
const file = configPath(home);
|
|
532
|
+
const config = defaults;
|
|
533
|
+
try {
|
|
534
|
+
await writeFile(file, `${JSON.stringify(configJson(config), null, 2)}\n`, {
|
|
535
|
+
flag: "wx",
|
|
536
|
+
});
|
|
537
|
+
return { created: true, path: file, config };
|
|
538
|
+
} catch (error) {
|
|
539
|
+
if (
|
|
540
|
+
typeof error === "object" &&
|
|
541
|
+
error !== null &&
|
|
542
|
+
"code" in error &&
|
|
543
|
+
error.code === "EEXIST"
|
|
544
|
+
) {
|
|
545
|
+
await addDefaultPlugins(defaults, file);
|
|
546
|
+
return { created: false, path: file, config: await readConfig(home) };
|
|
547
|
+
}
|
|
548
|
+
throw error;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** What `setPluginEnabled` needs from the distribution. */
|
|
553
|
+
export interface PluginOffer {
|
|
554
|
+
/** Plugins the distribution ships but does not install, by name to module. */
|
|
555
|
+
readonly optional: Readonly<Record<string, string>>;
|
|
556
|
+
/** The config to start from when the home has no file. */
|
|
557
|
+
readonly defaults?: OperadConfig | undefined;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Turns one configured plugin on or off, preserving the rest of the file.
|
|
562
|
+
* A plugin in `offer.optional` that the file does not list is added.
|
|
563
|
+
* The change takes effect at the next command or `serve` reload.
|
|
564
|
+
*/
|
|
565
|
+
export async function setPluginEnabled(
|
|
566
|
+
home: string,
|
|
567
|
+
name: string,
|
|
568
|
+
enabled: boolean,
|
|
569
|
+
offer: PluginOffer = { optional: {} },
|
|
570
|
+
): Promise<void> {
|
|
571
|
+
const file = configPath(home);
|
|
572
|
+
let raw: Record<string, unknown> = {};
|
|
573
|
+
try {
|
|
574
|
+
const text = await readFile(file, "utf8");
|
|
575
|
+
const parsed: unknown = JSON.parse(text);
|
|
576
|
+
if (!isRecord(parsed)) throw new ConfigError(`${file}: must be a JSON object`);
|
|
577
|
+
raw = parsed;
|
|
578
|
+
} catch (error) {
|
|
579
|
+
if (!(
|
|
580
|
+
typeof error === "object" &&
|
|
581
|
+
error !== null &&
|
|
582
|
+
"code" in error &&
|
|
583
|
+
error.code === "ENOENT"
|
|
584
|
+
)) {
|
|
585
|
+
if (error instanceof ConfigError) throw error;
|
|
586
|
+
throw new ConfigError(`${file}: invalid JSON: ${String(error)}`, {
|
|
587
|
+
cause: error,
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
raw = configJson(offer.defaults ?? defaultConfig(home));
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const plugins = raw.plugins;
|
|
594
|
+
if (!isRecord(plugins)) {
|
|
595
|
+
throw new ConfigError(
|
|
596
|
+
`${file}: plugins: must be an object keyed by plugin name to change it`,
|
|
597
|
+
);
|
|
598
|
+
}
|
|
599
|
+
const entry = Object.hasOwn(plugins, name) ? plugins[name] : undefined;
|
|
600
|
+
if (entry === undefined) {
|
|
601
|
+
const module = Object.hasOwn(offer.optional, name) ? offer.optional[name] : undefined;
|
|
602
|
+
if (!enabled || module === undefined) {
|
|
603
|
+
throw new ConfigError(`${file}: plugins.${name}: no such plugin`);
|
|
604
|
+
}
|
|
605
|
+
plugins[name] = { module, enabled: true };
|
|
606
|
+
} else if (typeof entry === "string") {
|
|
607
|
+
plugins[name] = { module: entry, enabled };
|
|
608
|
+
} else if (isRecord(entry)) {
|
|
609
|
+
entry.enabled = enabled;
|
|
610
|
+
} else {
|
|
611
|
+
throw new ConfigError(`${file}: plugins.${name}: must be a module name or an object`);
|
|
612
|
+
}
|
|
613
|
+
await mkdir(home, { recursive: true });
|
|
614
|
+
await writeFile(file, `${JSON.stringify(raw, null, 2)}\n`);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/** Opens the log the config selects. Options pass through to the backend. */
|
|
618
|
+
export function openConfigLog(
|
|
619
|
+
config: OperadConfig,
|
|
620
|
+
options: LogBackendOptions = {},
|
|
621
|
+
): LogBackend {
|
|
622
|
+
return openLog({
|
|
623
|
+
...options,
|
|
624
|
+
backend: config.store.backend,
|
|
625
|
+
path: config.store.path,
|
|
626
|
+
});
|
|
627
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./config.ts";
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { FlowDefinition, StepShape } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
/** The keys a step's `next` map declares. */
|
|
4
|
+
type NextKeys<T> = T extends { readonly next: infer N } ? keyof N : never;
|
|
5
|
+
|
|
6
|
+
/** The extra answers a wait_person may accept from a timed-out step. */
|
|
7
|
+
type LateChoices<T> = T extends { readonly params: { readonly lateChoices?: infer L } }
|
|
8
|
+
? L extends readonly (infer K extends string)[]
|
|
9
|
+
? K
|
|
10
|
+
: never
|
|
11
|
+
: never;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The steps `defineFlow` accepts are checked here, after their literals are
|
|
15
|
+
* inferred, so a wrong result name in `next` is a compile error. A missing
|
|
16
|
+
* edge is not: runtime validation reports it with the step and field.
|
|
17
|
+
*/
|
|
18
|
+
export type CheckStep<T> = T extends { readonly id: string; readonly replay: "safe" | "unsafe" }
|
|
19
|
+
? T extends { readonly kind: "plugin"; readonly params: { readonly use: string; readonly params: unknown } }
|
|
20
|
+
? T
|
|
21
|
+
: T extends {
|
|
22
|
+
readonly kind: "wait_person";
|
|
23
|
+
readonly params: { readonly prompt: string; readonly choices: infer C extends readonly string[] };
|
|
24
|
+
}
|
|
25
|
+
? Exclude<NextKeys<T>, C[number] | LateChoices<T> | "interrupted"> extends never
|
|
26
|
+
? T
|
|
27
|
+
: { readonly error: `wait_person: not a result: ${string & Exclude<NextKeys<T>, C[number] | LateChoices<T> | "interrupted">}` }
|
|
28
|
+
: T extends {
|
|
29
|
+
readonly kind: "wait";
|
|
30
|
+
readonly params: { readonly afterSecs: number } | { readonly check: string; readonly params?: unknown };
|
|
31
|
+
}
|
|
32
|
+
? Exclude<NextKeys<T>, "ready" | "interrupted"> extends never
|
|
33
|
+
? T
|
|
34
|
+
: { readonly error: `wait: not a result: ${string & Exclude<NextKeys<T>, "ready" | "interrupted">}` }
|
|
35
|
+
: T extends {
|
|
36
|
+
readonly kind: "end";
|
|
37
|
+
readonly params: { readonly status: "done" | "failed" | "cancelled" };
|
|
38
|
+
}
|
|
39
|
+
? [NextKeys<T>] extends [never]
|
|
40
|
+
? T
|
|
41
|
+
: { readonly error: "end: takes no results" }
|
|
42
|
+
: { readonly error: "unknown step kind" }
|
|
43
|
+
: { readonly error: "invalid step" };
|
|
44
|
+
|
|
45
|
+
export type CheckSteps<S extends readonly StepShape[]> = {
|
|
46
|
+
readonly [K in keyof S]: CheckStep<S[K]>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Defines a flow. The returned value is data: an entry step id, the steps, and
|
|
51
|
+
* the edges from each step's results to the next step id (or `end`).
|
|
52
|
+
*/
|
|
53
|
+
export function defineFlow<const S extends readonly StepShape[]>(
|
|
54
|
+
flow: FlowDefinition<S> & { readonly steps: CheckSteps<S> },
|
|
55
|
+
): FlowDefinition<S> {
|
|
56
|
+
return flow;
|
|
57
|
+
}
|