@operad-io/engine 0.1.1 → 0.3.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/config.schema.json +2 -1
- package/package.json +1 -1
- package/src/config/config.ts +74 -45
- package/src/flow/types.ts +4 -0
- package/src/flow/validate.ts +5 -0
- package/src/http/index.ts +2 -0
- package/src/http/plugins.ts +68 -0
- package/src/http/server.ts +342 -48
- package/src/kernel/decide.ts +115 -30
- package/src/kernel/state.ts +9 -0
- package/src/kernel/types.ts +25 -40
- package/src/log/open-log.ts +2 -5
- package/src/log/sqlite.ts +127 -85
- package/src/log/types.ts +35 -9
- package/src/loop/code-watch.ts +108 -2
- package/src/loop/older-code.ts +3 -4
- package/src/loop/serve.ts +122 -95
- package/src/notify/notify.ts +2 -6
- package/src/platform/atomic-write.ts +163 -0
- package/src/platform/file-lock.ts +43 -21
- package/src/platform/git-program.ts +108 -0
- package/src/platform/index.ts +3 -0
- package/src/platform/kept-workers.ts +290 -0
- package/src/platform/process-group.ts +11 -3
- package/src/platform/sqlite-open.ts +20 -6
- package/src/platform/sqlite-schema.ts +30 -0
- package/src/platform/step-reap.ts +33 -18
- package/src/platform/watch.ts +1 -11
- package/src/plugin/action-schema.ts +59 -1
- package/src/plugin/choice-info.ts +133 -0
- package/src/plugin/context.ts +279 -0
- package/src/plugin/declared-flows.ts +3 -10
- package/src/plugin/errors.ts +9 -0
- package/src/plugin/freeze.ts +12 -0
- package/src/plugin/harness.ts +28 -0
- package/src/plugin/index.ts +8 -7
- package/src/plugin/install.ts +265 -17
- package/src/plugin/keys.ts +2 -12
- package/src/plugin/load.ts +47 -23
- package/src/plugin/logger.ts +14 -0
- package/src/plugin/open-store.ts +0 -6
- package/src/plugin/question-form.ts +323 -0
- package/src/plugin/registry.ts +519 -76
- package/src/plugin/secrets.ts +14 -24
- package/src/plugin/semver.ts +52 -0
- package/src/plugin/settings.ts +1 -9
- package/src/plugin/sqlite-store.ts +94 -64
- package/src/plugin/store.ts +5 -0
- package/src/plugin/types.ts +575 -21
- package/src/plugin/validate.ts +402 -26
- package/src/runner/answer.ts +26 -68
- package/src/runner/ask.ts +227 -28
- package/src/runner/cancel.ts +10 -7
- package/src/runner/grants.ts +2 -2
- package/src/runner/index.ts +1 -3
- package/src/runner/limits.ts +2 -2
- package/src/runner/runner.ts +15 -14
- package/src/runner/service.ts +52 -19
- package/src/runner/step-worker.ts +9 -0
- package/src/testing/answer.ts +69 -0
- package/src/testing/clock-manual.ts +152 -0
- package/src/testing/config.ts +16 -0
- package/src/testing/fs.ts +172 -0
- package/src/testing/home.ts +43 -0
- package/src/testing/index.ts +19 -0
- package/src/testing/types.ts +2 -0
- package/src/testing/world.ts +9 -2
- package/src/trigger/kernel.ts +13 -46
- package/src/trigger/rules.ts +22 -12
- package/src/trigger/types.ts +2 -6
- package/src/util.ts +99 -0
- package/src/views/event-line.ts +22 -0
- package/src/views/history.ts +36 -19
- package/src/views/live-logs.ts +313 -0
- package/src/views/pending.ts +23 -5
- package/src/views/presentation.ts +629 -81
- package/src/views/running.ts +5 -5
- package/src/views/settings.ts +205 -66
- package/src/views/types.ts +8 -2
- package/src/views/uploads.ts +15 -7
- package/triggers.schema.json +4 -0
package/src/plugin/errors.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { AskQuestion, ChoiceInfo } from "./types.ts";
|
|
2
|
+
|
|
1
3
|
/** A plugin cannot be loaded or used; the message names the plugin and field. */
|
|
2
4
|
export class PluginError extends Error {
|
|
3
5
|
constructor(message: string, options?: ErrorOptions) {
|
|
@@ -47,12 +49,17 @@ export class AskTimeoutError extends Error {
|
|
|
47
49
|
readonly waitId: string;
|
|
48
50
|
readonly prompt: string;
|
|
49
51
|
readonly choices: readonly string[];
|
|
52
|
+
readonly choiceInfo: Readonly<Record<string, ChoiceInfo>> | undefined;
|
|
53
|
+
/** The form the step asked, when it asked one. */
|
|
54
|
+
readonly questions: readonly AskQuestion[] | undefined;
|
|
50
55
|
readonly resume: unknown;
|
|
51
56
|
|
|
52
57
|
constructor(question: {
|
|
53
58
|
readonly waitId: string;
|
|
54
59
|
readonly prompt: string;
|
|
55
60
|
readonly choices: readonly string[];
|
|
61
|
+
readonly choiceInfo?: Readonly<Record<string, ChoiceInfo>> | undefined;
|
|
62
|
+
readonly questions?: readonly AskQuestion[] | undefined;
|
|
56
63
|
readonly resume: unknown;
|
|
57
64
|
}) {
|
|
58
65
|
super(`nobody answered "${question.prompt}" in time`);
|
|
@@ -60,6 +67,8 @@ export class AskTimeoutError extends Error {
|
|
|
60
67
|
this.waitId = question.waitId;
|
|
61
68
|
this.prompt = question.prompt;
|
|
62
69
|
this.choices = question.choices;
|
|
70
|
+
this.choiceInfo = question.choiceInfo;
|
|
71
|
+
this.questions = question.questions;
|
|
63
72
|
this.resume = question.resume;
|
|
64
73
|
}
|
|
65
74
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Freezing a plugin value so no caller can change the copy another shares. */
|
|
2
|
+
|
|
3
|
+
export function deepFreeze<T>(value: T): T {
|
|
4
|
+
if (typeof value !== "object" || value === null || Object.isFrozen(value)) {
|
|
5
|
+
return value;
|
|
6
|
+
}
|
|
7
|
+
Object.freeze(value);
|
|
8
|
+
for (const item of Object.values(value as Record<string, unknown>)) {
|
|
9
|
+
deepFreeze(item);
|
|
10
|
+
}
|
|
11
|
+
return value;
|
|
12
|
+
}
|
package/src/plugin/harness.ts
CHANGED
|
@@ -13,6 +13,8 @@ export interface AgentStepParams {
|
|
|
13
13
|
readonly prompt: string;
|
|
14
14
|
readonly cwd: string;
|
|
15
15
|
readonly args?: readonly string[] | undefined;
|
|
16
|
+
/** Skill directories the agent is given; a harness that cannot load them lists them in the prompt. */
|
|
17
|
+
readonly skills?: readonly string[] | undefined;
|
|
16
18
|
/** The wall time the agent may run, in seconds. */
|
|
17
19
|
readonly wallTimeSecs?: number | undefined;
|
|
18
20
|
/** How long the agent may go without a new output line, in seconds. */
|
|
@@ -26,11 +28,23 @@ export interface PromptFlowParams {
|
|
|
26
28
|
readonly prompt: string;
|
|
27
29
|
readonly cwd: string;
|
|
28
30
|
readonly args?: readonly string[] | undefined;
|
|
31
|
+
/** Skill directories the agent is given; a harness that cannot load them lists them in the prompt. */
|
|
32
|
+
readonly skills?: readonly string[] | undefined;
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
/** The result names a harness `agent` step declares: a subset of `EndResult`. */
|
|
32
36
|
export type AgentResultName = Extract<EndResult, "done" | "failed" | "interrupted">;
|
|
33
37
|
|
|
38
|
+
/** How long one agent step took to become ready, to its first increment and in all. */
|
|
39
|
+
export interface AgentTimings {
|
|
40
|
+
/** From the step's start to the worker's `session` line; null when there was none. */
|
|
41
|
+
readonly startToReadyMs: number | null;
|
|
42
|
+
/** From the `session` line to the first text, thinking or tool line; null when there was none. */
|
|
43
|
+
readonly readyToFirstMs: number | null;
|
|
44
|
+
/** The whole step. */
|
|
45
|
+
readonly totalMs: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
34
48
|
/** What every harness `agent` step records as its facts. */
|
|
35
49
|
export interface AgentResultData {
|
|
36
50
|
readonly exitCode: number | null;
|
|
@@ -42,6 +56,20 @@ export interface AgentResultData {
|
|
|
42
56
|
/** The path of the JSONL file the harness wrote. */
|
|
43
57
|
readonly log: string;
|
|
44
58
|
readonly durationMs: number;
|
|
59
|
+
/** The three durations of the run, when the step measured them. */
|
|
60
|
+
readonly timings?: AgentTimings | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* The context at the end of a finished reply: `used` is the last model
|
|
63
|
+
* call's input plus cache read and cache write, and `window` is the
|
|
64
|
+
* model's context window when known. A harness that cannot tell leaves it
|
|
65
|
+
* out, and an unknown window is left out.
|
|
66
|
+
*/
|
|
67
|
+
readonly context?:
|
|
68
|
+
| {
|
|
69
|
+
readonly used: number;
|
|
70
|
+
readonly window?: number | undefined;
|
|
71
|
+
}
|
|
72
|
+
| undefined;
|
|
45
73
|
/** Set on `failed`: which limit or condition ended the run. */
|
|
46
74
|
readonly reason?: string | undefined;
|
|
47
75
|
readonly message?: string | undefined;
|
package/src/plugin/index.ts
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
export { definePlugin } from "./define-plugin.ts";
|
|
2
2
|
export { AccessError, AskTimeoutError, messageOf, PluginError, StartRefusedError, StepNotLoadedError } from "./errors.ts";
|
|
3
3
|
export { defaultOperadHome } from "./home.ts";
|
|
4
|
+
export { KERNEL_VERBS, openHome, type KernelVerb, type OpenHomeOptions, type OpenedHome } from "./context.ts";
|
|
4
5
|
export {
|
|
6
|
+
addPackage,
|
|
5
7
|
ensureInstalledPackage,
|
|
6
8
|
installedPackageFile,
|
|
7
9
|
pluginProjectManifestPath,
|
|
8
10
|
pluginProjectPath,
|
|
9
11
|
PLUGIN_PROJECT_DIRECTORY,
|
|
12
|
+
removePackage,
|
|
10
13
|
runningFromWorkspace,
|
|
11
14
|
} from "./install.ts";
|
|
12
|
-
export type { EnsureInstalledOptions, InstallRun, InstallRunner } from "./install.ts";
|
|
15
|
+
export type { AddedPackage, AddPackageOptions, EnsureInstalledOptions, InstallProgress, InstallRun, InstallRunner, RemovePackageOptions } from "./install.ts";
|
|
13
16
|
export { keysPathFor, readKeysFile } from "./keys.ts";
|
|
14
17
|
export {
|
|
15
18
|
loadConfiguredPlugins,
|
|
@@ -20,7 +23,7 @@ export {
|
|
|
20
23
|
} from "./load.ts";
|
|
21
24
|
export type { LoadPluginRegistryOptions } from "./load.ts";
|
|
22
25
|
export { createPluginLogger } from "./logger.ts";
|
|
23
|
-
export { PluginRegistry, splitActionName } from "./registry.ts";
|
|
26
|
+
export { guideBody, PluginRegistry, splitActionName } from "./registry.ts";
|
|
24
27
|
export type {
|
|
25
28
|
ActionInfo,
|
|
26
29
|
KeyBinding,
|
|
@@ -29,16 +32,14 @@ export type {
|
|
|
29
32
|
} from "./registry.ts";
|
|
30
33
|
export { FileSecretStore, secretsDirectory, secretsPathFor } from "./secrets.ts";
|
|
31
34
|
export { prepareSettings } from "./settings.ts";
|
|
32
|
-
export { isRangeSyntax, satisfiesRange } from "./semver.ts";
|
|
35
|
+
export { compareVersionText, isRangeSyntax, readVersionText, satisfiesRange } from "./semver.ts";
|
|
36
|
+
export type { VersionText } from "./semver.ts";
|
|
33
37
|
export { MemoryPluginStore } from "./store.ts";
|
|
34
38
|
export type { MemoryPluginStoreOptions } from "./store.ts";
|
|
35
39
|
export { PLUGIN_STORE_SCHEMA_VERSION, SqlitePluginStore } from "./sqlite-store.ts";
|
|
36
40
|
export type { SqlitePluginStoreOptions } from "./sqlite-store.ts";
|
|
37
41
|
export { openPluginStore } from "./open-store.ts";
|
|
38
|
-
export type {
|
|
39
|
-
OpenPluginStoreOptions,
|
|
40
|
-
PluginStoreBackend,
|
|
41
|
-
} from "./open-store.ts";
|
|
42
|
+
export type { OpenPluginStoreOptions } from "./open-store.ts";
|
|
42
43
|
export {
|
|
43
44
|
ActionArgsError,
|
|
44
45
|
fileProperties,
|
package/src/plugin/install.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
13
13
|
import { basename, dirname, join } from "node:path";
|
|
14
|
-
import { isRecord } from "../util.ts";
|
|
14
|
+
import { isMissingFile, isRecord } from "../util.ts";
|
|
15
15
|
|
|
16
16
|
/** The directory under the operad home that holds the Bun project. */
|
|
17
17
|
export const PLUGIN_PROJECT_DIRECTORY = "plugins";
|
|
@@ -145,21 +145,73 @@ export interface InstallRun {
|
|
|
145
145
|
readonly stderr: string;
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
+
/** Receives one line a package command printed while it ran. */
|
|
149
|
+
export type InstallProgress = (line: string) => void;
|
|
150
|
+
|
|
148
151
|
/** Runs one command; a test replaces the real Bun process. */
|
|
149
|
-
export type InstallRunner = (
|
|
152
|
+
export type InstallRunner = (
|
|
153
|
+
cwd: string,
|
|
154
|
+
args: readonly string[],
|
|
155
|
+
onProgress?: InstallProgress,
|
|
156
|
+
) => InstallRun | Promise<InstallRun>;
|
|
157
|
+
|
|
158
|
+
/** The most characters of one progress line kept for a client. */
|
|
159
|
+
const PROGRESS_LINE_MAX = 200;
|
|
150
160
|
|
|
151
|
-
function
|
|
152
|
-
|
|
161
|
+
function reportLine(text: string, onProgress: InstallProgress | undefined): void {
|
|
162
|
+
if (onProgress === undefined) return;
|
|
163
|
+
const line = text.replace(/\x1b\[[0-9;]*m/g, "").trim();
|
|
164
|
+
if (line.length > 0) onProgress(line.slice(0, PROGRESS_LINE_MAX));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Reads a child's stream into `out`, reporting whole lines as they arrive. */
|
|
168
|
+
async function readReported(
|
|
169
|
+
stream: ReadableStream<Uint8Array> | undefined,
|
|
170
|
+
out: string[],
|
|
171
|
+
onProgress: InstallProgress | undefined,
|
|
172
|
+
): Promise<void> {
|
|
173
|
+
if (stream === undefined) return;
|
|
174
|
+
const reader = stream.getReader();
|
|
175
|
+
const decoder = new TextDecoder();
|
|
176
|
+
let pending = "";
|
|
177
|
+
for (;;) {
|
|
178
|
+
const chunk = await reader.read();
|
|
179
|
+
if (chunk.done) break;
|
|
180
|
+
const text = decoder.decode(chunk.value, { stream: true });
|
|
181
|
+
out.push(text);
|
|
182
|
+
pending += text;
|
|
183
|
+
const parts = pending.split(/[\r\n]+/);
|
|
184
|
+
pending = parts.pop() ?? "";
|
|
185
|
+
for (const part of parts) reportLine(part, onProgress);
|
|
186
|
+
}
|
|
187
|
+
const last = decoder.decode();
|
|
188
|
+
out.push(last);
|
|
189
|
+
pending += last;
|
|
190
|
+
for (const part of pending.split(/[\r\n]+/)) reportLine(part, onProgress);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async function runWithBun(
|
|
194
|
+
cwd: string,
|
|
195
|
+
args: readonly string[],
|
|
196
|
+
onProgress?: InstallProgress,
|
|
197
|
+
): Promise<InstallRun> {
|
|
198
|
+
const child = Bun.spawn({
|
|
153
199
|
cmd: [process.execPath, ...args],
|
|
154
200
|
cwd,
|
|
155
201
|
stdin: "ignore",
|
|
156
202
|
stdout: "pipe",
|
|
157
203
|
stderr: "pipe",
|
|
158
204
|
});
|
|
205
|
+
const stdout: string[] = [];
|
|
206
|
+
const stderr: string[] = [];
|
|
207
|
+
await Promise.all([
|
|
208
|
+
readReported(child.stdout, stdout, onProgress),
|
|
209
|
+
readReported(child.stderr, stderr, onProgress),
|
|
210
|
+
]);
|
|
159
211
|
return {
|
|
160
|
-
exitCode:
|
|
161
|
-
stdout:
|
|
162
|
-
stderr:
|
|
212
|
+
exitCode: await child.exited,
|
|
213
|
+
stdout: stdout.join(""),
|
|
214
|
+
stderr: stderr.join(""),
|
|
163
215
|
};
|
|
164
216
|
}
|
|
165
217
|
|
|
@@ -168,12 +220,7 @@ function readManifest(path: string): Record<string, unknown> | undefined {
|
|
|
168
220
|
try {
|
|
169
221
|
text = readFileSync(path, "utf8");
|
|
170
222
|
} catch (error) {
|
|
171
|
-
if (
|
|
172
|
-
typeof error === "object" &&
|
|
173
|
-
error !== null &&
|
|
174
|
-
"code" in error &&
|
|
175
|
-
(error as { code?: unknown }).code === "ENOENT"
|
|
176
|
-
) {
|
|
223
|
+
if (isMissingFile(error)) {
|
|
177
224
|
return undefined;
|
|
178
225
|
}
|
|
179
226
|
throw error;
|
|
@@ -196,6 +243,29 @@ function outputTail(run: InstallRun): string {
|
|
|
196
243
|
return lines.length > 2000 ? lines.slice(-2000) : lines;
|
|
197
244
|
}
|
|
198
245
|
|
|
246
|
+
/** True when the output says the package could not be downloaded. */
|
|
247
|
+
function looksLikeNetworkFailure(text: string): boolean {
|
|
248
|
+
return /ENOTFOUND|EAI_AGAIN|ECONNREFUSED|ECONNRESET|ETIMEDOUT|getaddrinfo|fetch failed|network is unreachable|could not resolve/i.test(
|
|
249
|
+
text,
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** The refusal of a failed install, with how to retry a download that did not happen. */
|
|
254
|
+
function failedInstall(
|
|
255
|
+
specifier: string,
|
|
256
|
+
args: readonly string[],
|
|
257
|
+
directory: string,
|
|
258
|
+
run: InstallRun,
|
|
259
|
+
): Error {
|
|
260
|
+
const text = `${run.stdout}\n${run.stderr}`;
|
|
261
|
+
const retry = looksLikeNetworkFailure(text)
|
|
262
|
+
? "; the package could not be downloaded, so check the network and run the command again"
|
|
263
|
+
: "; run the command again to retry";
|
|
264
|
+
return new Error(
|
|
265
|
+
`${specifier}: \`bun ${args.join(" ")}\` in ${directory} failed with exit code ${String(run.exitCode)}; ${outputTail(run)}${retry}`,
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
|
|
199
269
|
/** Options for installing one package into the home project. */
|
|
200
270
|
export interface EnsureInstalledOptions {
|
|
201
271
|
readonly home: string;
|
|
@@ -207,6 +277,8 @@ export interface EnsureInstalledOptions {
|
|
|
207
277
|
readonly from?: string | undefined;
|
|
208
278
|
/** Skip optional dependencies; the claude SDK's platform binaries are optional. */
|
|
209
279
|
readonly omitOptional?: boolean | undefined;
|
|
280
|
+
/** Receives each line the install prints while it runs. */
|
|
281
|
+
readonly onProgress?: InstallProgress | undefined;
|
|
210
282
|
/** A test replaces the command; defaults to Bun. */
|
|
211
283
|
readonly run?: InstallRunner | undefined;
|
|
212
284
|
}
|
|
@@ -254,11 +326,9 @@ export async function ensureInstalledPackage(options: EnsureInstalledOptions): P
|
|
|
254
326
|
writeFileSync(path, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
255
327
|
|
|
256
328
|
const args = ["install", ...(options.omitOptional === true ? ["--omit=optional"] : []), "--no-summary"];
|
|
257
|
-
const run = (options.run ?? runWithBun)(directory, args);
|
|
329
|
+
const run = await (options.run ?? runWithBun)(directory, args, options.onProgress);
|
|
258
330
|
if (run.exitCode !== 0) {
|
|
259
|
-
throw
|
|
260
|
-
`${specifier}: \`bun ${args.join(" ")}\` in ${directory} failed with exit code ${String(run.exitCode)}; ${outputTail(run)}`,
|
|
261
|
-
);
|
|
331
|
+
throw failedInstall(specifier, args, directory, run);
|
|
262
332
|
}
|
|
263
333
|
const installed = installedPackageFile(home, specifier);
|
|
264
334
|
if (installed === undefined) {
|
|
@@ -266,3 +336,181 @@ export async function ensureInstalledPackage(options: EnsureInstalledOptions): P
|
|
|
266
336
|
}
|
|
267
337
|
return installed;
|
|
268
338
|
}
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
/** The lifecycle scripts a package needs before it can be imported. */
|
|
342
|
+
const INSTALL_SCRIPT_NAMES = ["preinstall", "install", "postinstall"] as const;
|
|
343
|
+
|
|
344
|
+
function packageManifest(packageDirectory: string): Record<string, unknown> | undefined {
|
|
345
|
+
try {
|
|
346
|
+
return readManifest(join(packageDirectory, "package.json"));
|
|
347
|
+
} catch {
|
|
348
|
+
return undefined;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** True when the package's manifest declares a lifecycle script. */
|
|
353
|
+
function declaresInstallScript(manifest: Record<string, unknown> | undefined): boolean {
|
|
354
|
+
const scripts = manifest?.scripts;
|
|
355
|
+
if (!isRecord(scripts)) return false;
|
|
356
|
+
return INSTALL_SCRIPT_NAMES.some((name) => typeof scripts[name] === "string");
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function installScriptHint(packageDirectory: string): string {
|
|
360
|
+
return declaresInstallScript(packageManifest(packageDirectory))
|
|
361
|
+
? "; its package declares an install script, and installing runs no package scripts, so a package that needs one cannot be added"
|
|
362
|
+
: "";
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function dependenciesOf(manifest: Record<string, unknown> | undefined): Record<string, unknown> {
|
|
366
|
+
if (manifest === undefined) return {};
|
|
367
|
+
const dependencies = manifest.dependencies;
|
|
368
|
+
return isRecord(dependencies) ? dependencies : {};
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** The dependency name `bun add` wrote for a package, including a local tarball. */
|
|
372
|
+
function addedDependencyName(
|
|
373
|
+
before: Record<string, unknown>,
|
|
374
|
+
after: Record<string, unknown>,
|
|
375
|
+
wanted: string | undefined,
|
|
376
|
+
): string | undefined {
|
|
377
|
+
if (wanted !== undefined) return wanted;
|
|
378
|
+
const previous = dependenciesOf(before);
|
|
379
|
+
const next = dependenciesOf(after);
|
|
380
|
+
const changed = Object.keys(next).filter((name) => previous[name] !== next[name]);
|
|
381
|
+
if (changed.length === 1) return changed[0];
|
|
382
|
+
const named = Object.entries(next).find(
|
|
383
|
+
([, value]) => typeof value === "string" && value.includes("file:"),
|
|
384
|
+
);
|
|
385
|
+
return named?.[0];
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** Options for adding one package to the home project. */
|
|
389
|
+
export interface AddPackageOptions {
|
|
390
|
+
readonly home: string;
|
|
391
|
+
/** A package name, or a local tarball a test installs without a registry. */
|
|
392
|
+
readonly specifier: string;
|
|
393
|
+
/** An exact version; absent asks Bun for the latest. */
|
|
394
|
+
readonly version?: string | undefined;
|
|
395
|
+
/** Receives each line the install prints while it runs. */
|
|
396
|
+
readonly onProgress?: InstallProgress | undefined;
|
|
397
|
+
/** A test replaces the command; defaults to Bun. */
|
|
398
|
+
readonly run?: InstallRunner | undefined;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** Options for removing one package from the home project. */
|
|
402
|
+
export interface RemovePackageOptions {
|
|
403
|
+
readonly home: string;
|
|
404
|
+
/** The package name `addPackage` returned. */
|
|
405
|
+
readonly name: string;
|
|
406
|
+
/**
|
|
407
|
+
* The dependency spec the project had before the add; when present, put it
|
|
408
|
+
* back instead of removing the package the project already needed.
|
|
409
|
+
*/
|
|
410
|
+
readonly previous?: string | undefined;
|
|
411
|
+
/** A test replaces the command; defaults to Bun. */
|
|
412
|
+
readonly run?: InstallRunner | undefined;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** What adding a package installed. */
|
|
416
|
+
export interface AddedPackage {
|
|
417
|
+
readonly name: string;
|
|
418
|
+
readonly version: string;
|
|
419
|
+
readonly file: string;
|
|
420
|
+
/** True when the package declares a lifecycle script Bun was told not to run. */
|
|
421
|
+
readonly hasInstallScript: boolean;
|
|
422
|
+
/** The dependency spec the project named before the add, when it named one. */
|
|
423
|
+
readonly previousVersion?: string | undefined;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Installs one package into the home's Bun project with install scripts off.
|
|
428
|
+
* The package may be a name with an exact version, a name with no version
|
|
429
|
+
* (the latest), or a local tarball a test names. Unlike
|
|
430
|
+
* `ensureInstalledPackage`, this always asks Bun for the package, so a person
|
|
431
|
+
* adding it gets the version they named.
|
|
432
|
+
*/
|
|
433
|
+
export async function addPackage(options: AddPackageOptions): Promise<AddedPackage> {
|
|
434
|
+
const directory = pluginProjectPath(options.home);
|
|
435
|
+
const path = join(directory, "package.json");
|
|
436
|
+
const before = readManifest(path);
|
|
437
|
+
mkdirSync(directory, { recursive: true });
|
|
438
|
+
if (before === undefined) {
|
|
439
|
+
writeFileSync(
|
|
440
|
+
path,
|
|
441
|
+
`${JSON.stringify({ name: PROJECT_NAME, private: true, type: "module" }, null, 2)}\n`,
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
const target =
|
|
445
|
+
options.version === undefined ? options.specifier : `${options.specifier}@${options.version}`;
|
|
446
|
+
const args = ["add", target, "--exact", "--ignore-scripts", "--no-summary"];
|
|
447
|
+
const run = await (options.run ?? runWithBun)(directory, args, options.onProgress);
|
|
448
|
+
if (run.exitCode !== 0) {
|
|
449
|
+
throw failedInstall(options.specifier, args, directory, run);
|
|
450
|
+
}
|
|
451
|
+
const after = readManifest(path);
|
|
452
|
+
if (after === undefined) {
|
|
453
|
+
throw new Error(`${options.specifier}: \`bun ${args.join(" ")}\` in ${directory} wrote no package.json`);
|
|
454
|
+
}
|
|
455
|
+
const wanted =
|
|
456
|
+
options.specifier.startsWith("file:") || options.specifier.startsWith(".") || options.specifier.startsWith("/")
|
|
457
|
+
? undefined
|
|
458
|
+
: options.specifier;
|
|
459
|
+
const name = addedDependencyName(before ?? {}, after, wanted);
|
|
460
|
+
if (name === undefined) {
|
|
461
|
+
throw new Error(`${options.specifier}: was installed into ${directory}, but the project does not name it`);
|
|
462
|
+
}
|
|
463
|
+
const packageDirectory = join(directory, "node_modules", packageNameOf(name));
|
|
464
|
+
const manifest = packageManifest(packageDirectory);
|
|
465
|
+
if (manifest === undefined) {
|
|
466
|
+
throw new Error(`${options.specifier}: was installed into ${directory}, but its package.json is missing`);
|
|
467
|
+
}
|
|
468
|
+
const version = manifest.version;
|
|
469
|
+
if (typeof version !== "string" || version.length === 0) {
|
|
470
|
+
throw new Error(`${options.specifier}: its installed package has no version`);
|
|
471
|
+
}
|
|
472
|
+
const file = installedPackageFile(options.home, name);
|
|
473
|
+
if (file === undefined) {
|
|
474
|
+
throw new Error(
|
|
475
|
+
`${options.specifier}: was installed into ${directory}, but its entry does not resolve${installScriptHint(packageDirectory)}`,
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
const previous = before === undefined ? undefined : dependenciesOf(before)[name];
|
|
479
|
+
return {
|
|
480
|
+
name,
|
|
481
|
+
version,
|
|
482
|
+
file,
|
|
483
|
+
hasInstallScript: declaresInstallScript(manifest),
|
|
484
|
+
previousVersion: typeof previous === "string" ? previous : undefined,
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Takes a package out of the home's Bun project again, dependency, files and
|
|
490
|
+
* lockfile entry alike. A refusal after `addPackage` calls this so a package
|
|
491
|
+
* operad refused does not stay installed; the project already naming it means
|
|
492
|
+
* it was not this add's to take away, so `previous` puts that dependency back.
|
|
493
|
+
* A package the project does not name is left alone.
|
|
494
|
+
*/
|
|
495
|
+
export async function removePackage(options: RemovePackageOptions): Promise<void> {
|
|
496
|
+
const directory = pluginProjectPath(options.home);
|
|
497
|
+
const path = join(directory, "package.json");
|
|
498
|
+
const manifest = readManifest(path);
|
|
499
|
+
if (manifest === undefined) return;
|
|
500
|
+
let args: string[];
|
|
501
|
+
if (options.previous !== undefined) {
|
|
502
|
+
const dependencies = isRecord(manifest.dependencies) ? manifest.dependencies : {};
|
|
503
|
+
dependencies[options.name] = options.previous;
|
|
504
|
+
manifest.dependencies = dependencies;
|
|
505
|
+
writeFileSync(path, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
506
|
+
args = ["install", "--ignore-scripts", "--no-summary"];
|
|
507
|
+
} else {
|
|
508
|
+
const dependencies = manifest.dependencies;
|
|
509
|
+
if (!isRecord(dependencies) || !Object.hasOwn(dependencies, options.name)) return;
|
|
510
|
+
args = ["remove", options.name, "--ignore-scripts", "--no-summary"];
|
|
511
|
+
}
|
|
512
|
+
const run = await (options.run ?? runWithBun)(directory, args);
|
|
513
|
+
if (run.exitCode !== 0) {
|
|
514
|
+
throw failedInstall(options.name, args, directory, run);
|
|
515
|
+
}
|
|
516
|
+
}
|
package/src/plugin/keys.ts
CHANGED
|
@@ -4,17 +4,7 @@ import { readFile } from "node:fs/promises";
|
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { messageOf, PluginError } from "./errors.ts";
|
|
6
6
|
import type { KeyBindings } from "./types.ts";
|
|
7
|
-
import { isRecord } from "../util.ts";
|
|
8
|
-
|
|
9
|
-
function isMissing(error: unknown): boolean {
|
|
10
|
-
return (
|
|
11
|
-
typeof error === "object" &&
|
|
12
|
-
error !== null &&
|
|
13
|
-
"code" in error &&
|
|
14
|
-
error.code === "ENOENT"
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
|
|
7
|
+
import { isMissingFile, isRecord } from "../util.ts";
|
|
18
8
|
/** The path of the key overrides for an operad home. */
|
|
19
9
|
export function keysPathFor(home: string): string {
|
|
20
10
|
return join(home, "keys.json");
|
|
@@ -26,7 +16,7 @@ export async function readKeysFile(path: string): Promise<KeyBindings> {
|
|
|
26
16
|
try {
|
|
27
17
|
text = await readFile(path, "utf8");
|
|
28
18
|
} catch (error) {
|
|
29
|
-
if (
|
|
19
|
+
if (isMissingFile(error)) return {};
|
|
30
20
|
throw new PluginError(`key bindings "${path}": ${messageOf(error)}`, {
|
|
31
21
|
cause: error,
|
|
32
22
|
});
|
package/src/plugin/load.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { messageOf, PluginError } from "./errors.ts";
|
|
|
7
7
|
import { defaultOperadHome } from "./home.ts";
|
|
8
8
|
import { installedPackageFile, runningFromWorkspace } from "./install.ts";
|
|
9
9
|
import { keysPathFor, readKeysFile } from "./keys.ts";
|
|
10
|
+
import { definitionGuide, definitionName, isMissingFile } from "../util.ts";
|
|
10
11
|
import { PluginRegistry } from "./registry.ts";
|
|
11
12
|
import type { PluginRegistryOptions } from "./registry.ts";
|
|
12
13
|
|
|
@@ -22,6 +23,11 @@ export interface LoadPluginRegistryOptions extends PluginRegistryOptions {
|
|
|
22
23
|
* Empty by default: the engine names no plugin.
|
|
23
24
|
*/
|
|
24
25
|
readonly installOnEnable?: readonly string[] | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* The shipped plugins a person may turn on that `config.json` need not name,
|
|
28
|
+
* by name and module. Their setup guides are readable while they are off.
|
|
29
|
+
*/
|
|
30
|
+
readonly optional?: Readonly<Record<string, string>> | undefined;
|
|
25
31
|
}
|
|
26
32
|
|
|
27
33
|
let loadCount = 0;
|
|
@@ -126,27 +132,6 @@ export interface DefinitelyLoadedPlugin {
|
|
|
126
132
|
readonly definition: unknown;
|
|
127
133
|
}
|
|
128
134
|
|
|
129
|
-
function definitionName(value: unknown): string | undefined {
|
|
130
|
-
if (
|
|
131
|
-
typeof value === "object" &&
|
|
132
|
-
value !== null &&
|
|
133
|
-
"name" in value &&
|
|
134
|
-
typeof (value as { name?: unknown }).name === "string"
|
|
135
|
-
) {
|
|
136
|
-
return (value as { name: string }).name;
|
|
137
|
-
}
|
|
138
|
-
return undefined;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function isMissingFileError(error: unknown): boolean {
|
|
142
|
-
return (
|
|
143
|
-
typeof error === "object" &&
|
|
144
|
-
error !== null &&
|
|
145
|
-
"code" in error &&
|
|
146
|
-
(error as { code?: unknown }).code === "ENOENT"
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
135
|
/**
|
|
151
136
|
* False only when the config file does not exist. Any other access error means
|
|
152
137
|
* the file may exist but cannot be read, so `readConfig` gets to report it
|
|
@@ -157,7 +142,7 @@ async function configFileExists(path: string): Promise<boolean> {
|
|
|
157
142
|
await access(path);
|
|
158
143
|
return true;
|
|
159
144
|
} catch (error) {
|
|
160
|
-
return !
|
|
145
|
+
return !isMissingFile(error);
|
|
161
146
|
}
|
|
162
147
|
}
|
|
163
148
|
|
|
@@ -221,6 +206,40 @@ export async function loadConfiguredPlugins(
|
|
|
221
206
|
return { entries: config.plugins, loaded, disabled, errors };
|
|
222
207
|
}
|
|
223
208
|
|
|
209
|
+
/** The plugins that are off: switched off in the file, or offered and not in it. */
|
|
210
|
+
function offNames(entries: readonly PluginConfigEntry[], optional: Readonly<Record<string, string>>): string[] {
|
|
211
|
+
const names = entries.filter((item) => !item.enabled && item.name !== undefined).map((item) => item.name ?? "");
|
|
212
|
+
for (const name of Object.keys(optional)) {
|
|
213
|
+
if (!entries.some((item) => item.name === name) && !names.includes(name)) names.push(name);
|
|
214
|
+
}
|
|
215
|
+
return names;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Reads the guide path of a switched-off plugin from its module, once per
|
|
220
|
+
* name: a module that cannot be loaded has no guide to offer.
|
|
221
|
+
*/
|
|
222
|
+
function offGuideReader(
|
|
223
|
+
entries: readonly PluginConfigEntry[],
|
|
224
|
+
optional: Readonly<Record<string, string>>,
|
|
225
|
+
home: string,
|
|
226
|
+
): (name: string) => Promise<string | undefined> {
|
|
227
|
+
const seen = new Map<string, Promise<string | undefined>>();
|
|
228
|
+
return (name) => {
|
|
229
|
+
const known = seen.get(name);
|
|
230
|
+
if (known !== undefined) return known;
|
|
231
|
+
const module =
|
|
232
|
+
entries.find((item) => !item.enabled && item.name === name)?.module ??
|
|
233
|
+
(entries.some((item) => item.name === name) ? undefined : optional[name]);
|
|
234
|
+
const read =
|
|
235
|
+
module === undefined
|
|
236
|
+
? Promise.resolve(undefined)
|
|
237
|
+
: loadPlugin(module, home).then(definitionGuide, () => undefined);
|
|
238
|
+
seen.set(name, read);
|
|
239
|
+
return read;
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
224
243
|
/**
|
|
225
244
|
* Loads the plugins listed in `$OPERAD_HOME/config.json` (default
|
|
226
245
|
* `~/.operad/config.json`) and validates them together.
|
|
@@ -245,12 +264,17 @@ export async function loadPluginRegistry(
|
|
|
245
264
|
}
|
|
246
265
|
|
|
247
266
|
const keys = await readKeysFile(options.keysPath ?? keysPathFor(home));
|
|
248
|
-
|
|
267
|
+
const registry = new PluginRegistry(definitions, {
|
|
249
268
|
...options,
|
|
250
269
|
home,
|
|
251
270
|
keys: options.keys ?? keys,
|
|
252
271
|
sources,
|
|
253
272
|
settings: options.settings ?? settings,
|
|
254
273
|
disabled: options.disabled ?? config.disabled,
|
|
274
|
+
off: offNames(config.entries, options.optional ?? {}),
|
|
275
|
+
disabledGuide: options.disabledGuide ?? offGuideReader(config.entries, options.optional ?? {}, home),
|
|
255
276
|
});
|
|
277
|
+
// A harness reads its model list in the background; the load asks for none,
|
|
278
|
+
// so a client that asks just after it gets the list the home already keeps.
|
|
279
|
+
return registry;
|
|
256
280
|
}
|
package/src/plugin/logger.ts
CHANGED
|
@@ -10,6 +10,20 @@ function segment(value: string): string {
|
|
|
10
10
|
: cleaned;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/** The directory a plugin's runs write their step logs in: `<store>/logs/<plugin>/runs`. */
|
|
14
|
+
function runLogsDirectory(storeRoot: string, plugin: string): string {
|
|
15
|
+
return join(storeRoot, "logs", segment(plugin), "runs");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** One run's step-log directory: `<store>/logs/<plugin>/runs/<run>`. */
|
|
19
|
+
export function runLogDirectory(
|
|
20
|
+
storeRoot: string,
|
|
21
|
+
plugin: string,
|
|
22
|
+
runId: string,
|
|
23
|
+
): string {
|
|
24
|
+
return join(runLogsDirectory(storeRoot, plugin), segment(runId));
|
|
25
|
+
}
|
|
26
|
+
|
|
13
27
|
/**
|
|
14
28
|
* A logger for one step, action, command or tick. It appends to a file under
|
|
15
29
|
* `<store>/logs` and returns that path so the plugin can put it in a fact.
|
package/src/plugin/open-store.ts
CHANGED
|
@@ -5,10 +5,7 @@ import type { SubscriptionWatchOptions } from "../platform/index.ts";
|
|
|
5
5
|
import { SqlitePluginStore } from "./sqlite-store.ts";
|
|
6
6
|
import type { PluginStore } from "./types.ts";
|
|
7
7
|
|
|
8
|
-
export type PluginStoreBackend = "sqlite";
|
|
9
|
-
|
|
10
8
|
export interface OpenPluginStoreOptions extends SubscriptionWatchOptions {
|
|
11
|
-
readonly backend: PluginStoreBackend;
|
|
12
9
|
/** The home's store directory. */
|
|
13
10
|
readonly root: string;
|
|
14
11
|
readonly clock?: (() => number) | undefined;
|
|
@@ -24,9 +21,6 @@ export function openPluginStore(options: OpenPluginStoreOptions): PluginStore {
|
|
|
24
21
|
file: join(options.root, "plugins.sqlite"),
|
|
25
22
|
...(options.clock === undefined ? {} : { clock: options.clock }),
|
|
26
23
|
...(options.follow === undefined ? {} : { follow: options.follow }),
|
|
27
|
-
...(options.readonly === undefined ? {} : { readonly: options.readonly }),
|
|
28
|
-
...(options.readOnly === undefined ? {} : { readOnly: options.readOnly }),
|
|
29
|
-
...(options.oneShot === undefined ? {} : { oneShot: options.oneShot }),
|
|
30
24
|
...(options.repeat === undefined ? {} : { repeat: options.repeat }),
|
|
31
25
|
});
|
|
32
26
|
}
|