@parcha/agentrun-serve 0.0.0-stage → 0.1.0-beta.30
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/CHANGELOG.md +748 -0
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +2190 -2
- package/dist/adapters.d.ts +234 -0
- package/dist/adapters.js +585 -0
- package/dist/agents.d.ts +130 -0
- package/dist/agents.js +308 -0
- package/dist/app.d.ts +7 -0
- package/dist/app.js +384 -0
- package/dist/background.d.ts +28 -0
- package/dist/background.js +376 -0
- package/dist/bound-files.d.ts +4 -0
- package/dist/bound-files.js +164 -0
- package/dist/box/record.d.ts +6 -0
- package/dist/box/record.js +41 -0
- package/dist/box/sweep.d.ts +53 -0
- package/dist/box/sweep.js +193 -0
- package/dist/box-egress.d.ts +98 -0
- package/dist/box-egress.js +259 -0
- package/dist/box-image.d.ts +98 -0
- package/dist/box-image.js +285 -0
- package/dist/box-relay-vm.d.ts +80 -0
- package/dist/box-relay-vm.js +389 -0
- package/dist/box-relay.d.ts +45 -0
- package/dist/box-relay.js +227 -0
- package/dist/boxes.d.ts +72 -0
- package/dist/boxes.js +189 -0
- package/dist/browser-cost.d.ts +41 -0
- package/dist/browser-cost.js +158 -0
- package/dist/cap.d.ts +332 -0
- package/dist/cap.js +992 -0
- package/dist/catalog.d.ts +20 -0
- package/dist/catalog.js +53 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +167 -0
- package/dist/coding-tools.d.ts +35 -0
- package/dist/coding-tools.js +87 -0
- package/dist/console/THIRD-PARTY-NOTICES.txt +1721 -0
- package/dist/console/assets/index-34jfpIbQ.js +481 -0
- package/dist/console/assets/index-DUHbWcIP.css +1 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-400-normal-BEIGL1Tu.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-400-normal-CIEtRijs.woff +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-600-normal-BB5xRk30.woff +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-600-normal-EVf6-Yzo.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-ext-400-normal-BHyURHCj.woff +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-ext-400-normal-C_uLvvQ5.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-ext-600-normal-Dj6eZy5f.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-cyrillic-ext-600-normal-DpeVePZf.woff +0 -0
- package/dist/console/assets/jetbrains-mono-greek-400-normal-C190GLew.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-greek-400-normal-CNojz0t3.woff +0 -0
- package/dist/console/assets/jetbrains-mono-greek-600-normal-3KAm9r92.woff +0 -0
- package/dist/console/assets/jetbrains-mono-greek-600-normal-H7WoG9Et.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-400-normal-B11XCQ5g.woff +0 -0
- package/dist/console/assets/jetbrains-mono-latin-400-normal-V6pRDFza.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-600-normal-C8RAYTDA.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-600-normal-DXv3JcnD.woff +0 -0
- package/dist/console/assets/jetbrains-mono-latin-ext-400-normal-Bc8Ftmh3.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-ext-400-normal-DHMOLxAi.woff +0 -0
- package/dist/console/assets/jetbrains-mono-latin-ext-600-normal-BfB_LPfz.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-latin-ext-600-normal-DiQlUoE6.woff +0 -0
- package/dist/console/assets/jetbrains-mono-vietnamese-400-normal-BeU3T8wf.woff +0 -0
- package/dist/console/assets/jetbrains-mono-vietnamese-400-normal-ByoDsISC.woff2 +0 -0
- package/dist/console/assets/jetbrains-mono-vietnamese-600-normal-CSpzN4cI.woff +0 -0
- package/dist/console/assets/jetbrains-mono-vietnamese-600-normal-Dtizs43-.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-400-normal-Cph9ettZ.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-400-normal-DfGZKnlb.woff +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-600-normal-BGBUNdzT.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-600-normal-COje25QW.woff +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-ext-400-normal-4l4lq8zr.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-ext-400-normal-BGzXWJeR.woff +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-ext-600-normal-8_E36CQj.woff2 +0 -0
- package/dist/console/assets/schibsted-grotesk-latin-ext-600-normal-C27an7Zc.woff +0 -0
- package/dist/console/index.html +15 -0
- package/dist/content-filter.d.ts +7 -0
- package/dist/content-filter.js +23 -0
- package/dist/daytona-api.d.ts +21 -0
- package/dist/daytona-api.js +101 -0
- package/dist/decision-rows.d.ts +19 -0
- package/dist/decision-rows.js +123 -0
- package/dist/decisions.d.ts +94 -0
- package/dist/decisions.js +184 -0
- package/dist/effort-guidance.d.ts +5 -0
- package/dist/effort-guidance.js +14 -0
- package/dist/environment.d.ts +5 -0
- package/dist/environment.js +11 -0
- package/dist/errors.d.ts +74 -0
- package/dist/errors.js +69 -0
- package/dist/exa-keyless.d.ts +35 -0
- package/dist/exa-keyless.js +202 -0
- package/dist/fetch-page.d.ts +114 -0
- package/dist/fetch-page.js +182 -0
- package/dist/files.d.ts +65 -0
- package/dist/files.js +158 -0
- package/dist/grade.d.ts +93 -0
- package/dist/grade.js +328 -0
- package/dist/graph-served.d.ts +23 -0
- package/dist/graph-served.js +75 -0
- package/dist/graph-view.d.ts +102 -0
- package/dist/graph-view.js +219 -0
- package/dist/harness-app.d.ts +40 -0
- package/dist/harness-app.js +708 -0
- package/dist/harness-opencode/adapter.d.ts +45 -0
- package/dist/harness-opencode/adapter.js +272 -0
- package/dist/harness-opencode/binary.d.ts +18 -0
- package/dist/harness-opencode/binary.js +87 -0
- package/dist/harness-opencode/config.d.ts +24 -0
- package/dist/harness-opencode/config.js +61 -0
- package/dist/harness-opencode/harness.d.ts +77 -0
- package/dist/harness-opencode/harness.js +620 -0
- package/dist/harness-opencode/host/mcp.d.ts +40 -0
- package/dist/harness-opencode/host/mcp.js +208 -0
- package/dist/harness-opencode/host/policy.d.ts +71 -0
- package/dist/harness-opencode/host/policy.js +419 -0
- package/dist/harness-opencode/host/server.d.ts +55 -0
- package/dist/harness-opencode/host/server.js +165 -0
- package/dist/harness-opencode/index.d.ts +9 -0
- package/dist/harness-opencode/index.js +6 -0
- package/dist/harness-opencode/verify.d.ts +32 -0
- package/dist/harness-opencode/verify.js +75 -0
- package/dist/harness-opencode/versions.d.ts +33 -0
- package/dist/harness-opencode/versions.js +64 -0
- package/dist/harness.d.ts +56 -0
- package/dist/harness.js +77 -0
- package/dist/home-path.d.ts +27 -0
- package/dist/home-path.js +78 -0
- package/dist/host-tools.d.ts +30 -0
- package/dist/host-tools.js +154 -0
- package/dist/host.d.ts +39 -0
- package/dist/host.js +85 -0
- package/dist/hosted-search.d.ts +27 -0
- package/dist/hosted-search.js +133 -0
- package/dist/import-sources.d.ts +104 -0
- package/dist/import-sources.js +629 -0
- package/dist/import-summary.d.ts +25 -0
- package/dist/import-summary.js +75 -0
- package/dist/import-worker.d.ts +26 -0
- package/dist/import-worker.js +41 -0
- package/dist/import.d.ts +145 -0
- package/dist/import.js +1222 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/key-check.d.ts +37 -0
- package/dist/key-check.js +112 -0
- package/dist/key-refusal.d.ts +51 -0
- package/dist/key-refusal.js +191 -0
- package/dist/learn/check.d.ts +108 -0
- package/dist/learn/check.js +277 -0
- package/dist/learn/grade.d.ts +80 -0
- package/dist/learn/grade.js +445 -0
- package/dist/learn/index.d.ts +7 -0
- package/dist/learn/index.js +11 -0
- package/dist/learn/invisible.d.ts +5 -0
- package/dist/learn/invisible.js +42 -0
- package/dist/learn/notes.d.ts +59 -0
- package/dist/learn/notes.js +147 -0
- package/dist/learn/reflect.d.ts +49 -0
- package/dist/learn/reflect.js +213 -0
- package/dist/learn/secrets.d.ts +18 -0
- package/dist/learn/secrets.js +131 -0
- package/dist/learn/skills.d.ts +97 -0
- package/dist/learn/skills.js +271 -0
- package/dist/learned-workflow.d.ts +59 -0
- package/dist/learned-workflow.js +191 -0
- package/dist/learning-routes.d.ts +196 -0
- package/dist/learning-routes.js +609 -0
- package/dist/learning-run.d.ts +306 -0
- package/dist/learning-run.js +819 -0
- package/dist/learning-words.d.ts +76 -0
- package/dist/learning-words.js +174 -0
- package/dist/learning.d.ts +418 -0
- package/dist/learning.js +946 -0
- package/dist/llm.d.ts +40 -0
- package/dist/llm.js +37 -0
- package/dist/llms.txt +37 -0
- package/dist/mcp-servers.d.ts +118 -0
- package/dist/mcp-servers.js +450 -0
- package/dist/mcp.d.ts +51 -0
- package/dist/mcp.js +392 -0
- package/dist/model.d.ts +186 -0
- package/dist/model.js +485 -0
- package/dist/monid.d.ts +74 -0
- package/dist/monid.js +277 -0
- package/dist/openapi/agentrun-v2.openapi.json +4806 -0
- package/dist/openapi.d.ts +10 -0
- package/dist/openapi.js +1727 -0
- package/dist/owner.d.ts +55 -0
- package/dist/owner.js +222 -0
- package/dist/packs.d.ts +71 -0
- package/dist/packs.js +180 -0
- package/dist/page.d.ts +32 -0
- package/dist/page.js +133 -0
- package/dist/paid-calls.d.ts +50 -0
- package/dist/paid-calls.js +110 -0
- package/dist/pause.d.ts +70 -0
- package/dist/pause.js +97 -0
- package/dist/pdf-pages.d.ts +44 -0
- package/dist/pdf-pages.js +147 -0
- package/dist/place.d.ts +69 -0
- package/dist/place.js +137 -0
- package/dist/print.d.ts +20 -0
- package/dist/print.js +29 -0
- package/dist/propose.d.ts +40 -0
- package/dist/propose.js +112 -0
- package/dist/quiet-sqlite-warning.d.ts +1 -0
- package/dist/quiet-sqlite-warning.js +14 -0
- package/dist/request-start.d.ts +22 -0
- package/dist/request-start.js +350 -0
- package/dist/result.d.ts +3 -0
- package/dist/result.js +5 -0
- package/dist/rows.d.ts +46 -0
- package/dist/rows.js +121 -0
- package/dist/run-box.d.ts +186 -0
- package/dist/run-box.js +590 -0
- package/dist/run-manifest.d.ts +27 -0
- package/dist/run-manifest.js +60 -0
- package/dist/runnable.d.ts +83 -0
- package/dist/runnable.js +170 -0
- package/dist/runs.d.ts +1062 -0
- package/dist/runs.js +3020 -0
- package/dist/secret-cut.d.ts +27 -0
- package/dist/secret-cut.js +109 -0
- package/dist/secrets.d.ts +62 -0
- package/dist/secrets.js +158 -0
- package/dist/server-lock.d.ts +44 -0
- package/dist/server-lock.js +140 -0
- package/dist/server.d.ts +722 -0
- package/dist/server.js +2118 -0
- package/dist/sessions.d.ts +4 -0
- package/dist/sessions.js +105 -0
- package/dist/sign-ins.d.ts +99 -0
- package/dist/sign-ins.js +288 -0
- package/dist/skills.d.ts +43 -0
- package/dist/skills.js +96 -0
- package/dist/stats.d.ts +106 -0
- package/dist/stats.js +103 -0
- package/dist/tool-gate.d.ts +266 -0
- package/dist/tool-gate.js +554 -0
- package/dist/tool-keys.d.ts +187 -0
- package/dist/tool-keys.js +315 -0
- package/dist/tool-plane.d.ts +228 -0
- package/dist/tool-plane.js +400 -0
- package/dist/tools.d.ts +202 -0
- package/dist/tools.js +372 -0
- package/dist/trigger-secrets.d.ts +32 -0
- package/dist/trigger-secrets.js +143 -0
- package/dist/triggers.d.ts +439 -0
- package/dist/triggers.js +863 -0
- package/dist/turn-end.d.ts +24 -0
- package/dist/turn-end.js +15 -0
- package/dist/turns.d.ts +34 -0
- package/dist/turns.js +202 -0
- package/dist/uploads.d.ts +104 -0
- package/dist/uploads.js +474 -0
- package/dist/usage-lines.d.ts +26 -0
- package/dist/usage-lines.js +28 -0
- package/dist/usage.d.ts +29 -0
- package/dist/usage.js +58 -0
- package/dist/validate.d.ts +57 -0
- package/dist/validate.js +184 -0
- package/dist/web-fetch.d.ts +64 -0
- package/dist/web-fetch.js +262 -0
- package/dist/web-search.d.ts +54 -0
- package/dist/web-search.js +136 -0
- package/dist/webhooks.d.ts +189 -0
- package/dist/webhooks.js +503 -0
- package/dist/workspace-paths.d.ts +33 -0
- package/dist/workspace-paths.js +104 -0
- package/dist/workspace.d.ts +88 -0
- package/dist/workspace.js +369 -0
- package/launcher/agentrun-serve +109 -0
- package/package.json +100 -4
- package/skills/agentrun-serve/SKILL.md +128 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { type KeyCheck } from "./key-check.js";
|
|
2
|
+
export declare const TOOL_KEYS_FILE = "tool-keys.json";
|
|
3
|
+
export declare const SEAL_KEY_ENV = "AGENTRUN_SEAL_KEY";
|
|
4
|
+
/** Each key a person may save, fixed here: its kind, the fields it is entered as (BROWSERBASE_PROJECT_ID is no secret,
|
|
5
|
+
* and is kept the same way), and what it may unlock: tools by name, and `decisions.<id>`, a workflow's decision
|
|
6
|
+
* provider. GET lists only those this server binds (unlockedBy), so a name here the server does not bind is never
|
|
7
|
+
* promised, and one shows by itself once the server binds it. A `tool` key's fields reach only the runs that name a
|
|
8
|
+
* tool it unlocks (runs.ts' secretsFor); a key of any other kind (a model's) reaches every run. */
|
|
9
|
+
export declare const TOOL_KEYS: readonly [{
|
|
10
|
+
readonly id: "parallel";
|
|
11
|
+
readonly kind: "tool";
|
|
12
|
+
readonly label: "Parallel";
|
|
13
|
+
readonly fields: readonly ["PARALLEL_API_KEY"];
|
|
14
|
+
readonly unlocks: readonly ["web_fetch", "web_search", "decisions.parallel"];
|
|
15
|
+
}, {
|
|
16
|
+
readonly id: "monid";
|
|
17
|
+
readonly kind: "tool";
|
|
18
|
+
readonly label: "Monid";
|
|
19
|
+
readonly fields: readonly ["MONID_API_KEY"];
|
|
20
|
+
readonly unlocks: readonly ["monid.discover", "monid.inspect", "monid.run"];
|
|
21
|
+
}, {
|
|
22
|
+
readonly id: "browserbase";
|
|
23
|
+
readonly kind: "tool";
|
|
24
|
+
readonly label: "Browserbase";
|
|
25
|
+
readonly fields: readonly ["BROWSERBASE_API_KEY", "BROWSERBASE_PROJECT_ID"];
|
|
26
|
+
readonly unlocks: readonly ["browser"];
|
|
27
|
+
}, {
|
|
28
|
+
readonly id: "kernel";
|
|
29
|
+
readonly kind: "tool";
|
|
30
|
+
readonly label: "Kernel";
|
|
31
|
+
readonly fields: readonly ["KERNEL_API_KEY"];
|
|
32
|
+
readonly unlocks: readonly ["browser"];
|
|
33
|
+
}, {
|
|
34
|
+
readonly id: "typesafe";
|
|
35
|
+
readonly kind: "tool";
|
|
36
|
+
readonly label: "TypeSafe (Jev)";
|
|
37
|
+
readonly fields: readonly ["TYPESAFE_API_KEY"];
|
|
38
|
+
readonly unlocks: readonly ["decisions.jev"];
|
|
39
|
+
}, ...{
|
|
40
|
+
id: "anthropic" | "openai" | "openrouter";
|
|
41
|
+
kind: "model";
|
|
42
|
+
label: "Anthropic" | "OpenAI" | "OpenRouter";
|
|
43
|
+
fields: readonly ["ANTHROPIC_API_KEY" | "OPENAI_API_KEY" | "OPENROUTER_API_KEY"];
|
|
44
|
+
unlocks: readonly [];
|
|
45
|
+
harnesses: readonly ["pi" | "claude-agent" | "codex" | "opencode", "claude-agent"] | readonly ["pi" | "claude-agent" | "codex" | "opencode", "codex", "opencode"] | readonly ["pi" | "claude-agent" | "codex" | "opencode", "claude-agent", "codex", "opencode"];
|
|
46
|
+
}[]];
|
|
47
|
+
/** The fields of the `tool` rows: keys a tool or a decision provider reads, and no harness (the harnesses read their
|
|
48
|
+
* packages, their pins and their models' keys). A row of another kind (a model's) is outside them. */
|
|
49
|
+
export declare const TOOL_FIELDS: readonly string[];
|
|
50
|
+
/** What `candidates` unlock here, as capabilities show it: the tools of `tools` (capabilities' `tools`, available or
|
|
51
|
+
* not) and the providers of `providers` (its `workflows.decisions.providers`) named among them, and the harnesses
|
|
52
|
+
* that run those tools. A name neither holds is left out; a key that unlocks none of its names lists nothing. */
|
|
53
|
+
export declare function unlockedBy(candidates: readonly string[], tools: readonly {
|
|
54
|
+
name: string;
|
|
55
|
+
harnesses: readonly string[];
|
|
56
|
+
}[], providers: readonly {
|
|
57
|
+
id: string;
|
|
58
|
+
}[]): {
|
|
59
|
+
unlocks: string[];
|
|
60
|
+
harnesses: ("pi" | "claude-agent" | "codex" | "opencode")[];
|
|
61
|
+
};
|
|
62
|
+
/** What a row unlocks here: a `tool` row's, its tools and providers as capabilities bind them (unlockedBy); any other
|
|
63
|
+
* kind's (a model's), no tool, on those that run on it (its `harnesses`, when it names them) that this server runs
|
|
64
|
+
* (`offers`, harness.ts' harnessOffers) or would once its key is set: one unavailable only for want of a model key
|
|
65
|
+
* among whose `keys` is the row's (its first field). So OpenRouter's row names all three before any key is saved; a
|
|
66
|
+
* harness not installed, off its pins or too big for this host is left out. */
|
|
67
|
+
export declare function rowUnlocks(row: {
|
|
68
|
+
kind: string;
|
|
69
|
+
unlocks: readonly string[];
|
|
70
|
+
harnesses?: readonly string[];
|
|
71
|
+
fields?: readonly string[];
|
|
72
|
+
}, tools: Parameters<typeof unlockedBy>[1], providers: Parameters<typeof unlockedBy>[2], offers: readonly {
|
|
73
|
+
id: string;
|
|
74
|
+
available: boolean;
|
|
75
|
+
keys?: readonly string[];
|
|
76
|
+
}[]): {
|
|
77
|
+
unlocks: string[];
|
|
78
|
+
harnesses: ("pi" | "claude-agent" | "codex" | "opencode")[];
|
|
79
|
+
};
|
|
80
|
+
/** The key that unlocks the tool `name` (capabilities' `tools[].key`, so a page links "needs the X key" to it), if any. */
|
|
81
|
+
export declare const keyOf: (name: string) => string | undefined;
|
|
82
|
+
/** Why a stored value does not open, in the order a re-entry names one: no seal key now, another than it was sealed
|
|
83
|
+
* under, or the file (or its entry) damaged. */
|
|
84
|
+
export declare const UNREADABLE: readonly ["seal_key_missing", "seal_key_changed", "file_unreadable"];
|
|
85
|
+
/** The check of a key the server was started with (ToolKeys.checkServer): a KeyCheck, or refused by its provider, why in
|
|
86
|
+
* plain words. Held in memory only, for this start. */
|
|
87
|
+
export type StartCheck = KeyCheck | {
|
|
88
|
+
state: "refused";
|
|
89
|
+
reason: string;
|
|
90
|
+
at: string;
|
|
91
|
+
};
|
|
92
|
+
/** The seal key `text` gives: 32 bytes in base64 (or base64url), else none. */
|
|
93
|
+
export declare function sealKeyOf(text: string | undefined): Buffer | undefined;
|
|
94
|
+
export declare class ToolKeys {
|
|
95
|
+
private readonly env;
|
|
96
|
+
private readonly say;
|
|
97
|
+
readonly file: string;
|
|
98
|
+
private readonly key;
|
|
99
|
+
private readonly sealCheck;
|
|
100
|
+
private said;
|
|
101
|
+
/** The checks of the keys the server was started with, by key id, each once it answered (checkServer). */
|
|
102
|
+
private readonly started;
|
|
103
|
+
/** `env` is what the server is started with (withSecrets' `"own"`, never a saved key, or each would read as the
|
|
104
|
+
* server's and could not be deleted): a field it sets is the server's, and wins. `say` takes a log line's fields. */
|
|
105
|
+
constructor(data: string, env: NodeJS.ProcessEnv, say: (fields: Record<string, string>) => void);
|
|
106
|
+
/** The file's entries, none with no file; "unreadable" when it does not read as this format. A file from before checks
|
|
107
|
+
* has none. */
|
|
108
|
+
private kept;
|
|
109
|
+
private open;
|
|
110
|
+
/** The stored value of the field `name`, opened now (secrets.ts reads a saved key by name through this); undefined
|
|
111
|
+
* with none, or one that does not open, which view() asks for again: never a clear fallback. */
|
|
112
|
+
value(name: string): string | undefined;
|
|
113
|
+
/** Every stored value that opens, by field name. */
|
|
114
|
+
values(): Record<string, string>;
|
|
115
|
+
/** GET: each key's fields, whether set and where from (never a value), what it unlocks of what capabilities shows
|
|
116
|
+
* here (a model key: the harnesses `offers` says this server runs or would on the key), its check when its key (its first field) is
|
|
117
|
+
* stored here (null otherwise: the server's own is not checked here), and what to enter again, with why. */
|
|
118
|
+
view(offers?: Parameters<typeof rowUnlocks>[3]): {
|
|
119
|
+
keys: {
|
|
120
|
+
fields: ({
|
|
121
|
+
name: "KERNEL_API_KEY" | "BROWSERBASE_API_KEY" | "BROWSERBASE_PROJECT_ID" | "TYPESAFE_API_KEY" | "PARALLEL_API_KEY" | "ANTHROPIC_API_KEY" | "OPENAI_API_KEY" | "OPENROUTER_API_KEY" | "MONID_API_KEY";
|
|
122
|
+
set: boolean;
|
|
123
|
+
source: "server";
|
|
124
|
+
} | {
|
|
125
|
+
name: "KERNEL_API_KEY" | "BROWSERBASE_API_KEY" | "BROWSERBASE_PROJECT_ID" | "TYPESAFE_API_KEY" | "PARALLEL_API_KEY" | "ANTHROPIC_API_KEY" | "OPENAI_API_KEY" | "OPENROUTER_API_KEY" | "MONID_API_KEY";
|
|
126
|
+
set: boolean;
|
|
127
|
+
source: "stored";
|
|
128
|
+
} | {
|
|
129
|
+
stored_unreadable?: true | undefined;
|
|
130
|
+
name: "KERNEL_API_KEY" | "BROWSERBASE_API_KEY" | "BROWSERBASE_PROJECT_ID" | "TYPESAFE_API_KEY" | "PARALLEL_API_KEY" | "ANTHROPIC_API_KEY" | "OPENAI_API_KEY" | "OPENROUTER_API_KEY" | "MONID_API_KEY";
|
|
131
|
+
set: boolean;
|
|
132
|
+
source: null;
|
|
133
|
+
})[];
|
|
134
|
+
check: {
|
|
135
|
+
state: "accepted";
|
|
136
|
+
at: string;
|
|
137
|
+
} | {
|
|
138
|
+
state: "not_checked";
|
|
139
|
+
reason: string;
|
|
140
|
+
at?: string;
|
|
141
|
+
} | {
|
|
142
|
+
state: "refused";
|
|
143
|
+
reason: string;
|
|
144
|
+
at: string;
|
|
145
|
+
} | null;
|
|
146
|
+
unlocks: string[];
|
|
147
|
+
harnesses: ("pi" | "claude-agent" | "codex" | "opencode")[];
|
|
148
|
+
id: "browserbase" | "kernel" | "anthropic" | "openai" | "typesafe" | "openrouter" | "parallel" | "monid";
|
|
149
|
+
kind: "model" | "tool";
|
|
150
|
+
label: "Anthropic" | "OpenAI" | "OpenRouter" | "Parallel" | "Monid" | "Browserbase" | "Kernel" | "TypeSafe (Jev)";
|
|
151
|
+
}[];
|
|
152
|
+
reenter: {
|
|
153
|
+
reason: "seal_key_missing" | "seal_key_changed" | "file_unreadable";
|
|
154
|
+
fields: string[];
|
|
155
|
+
} | null;
|
|
156
|
+
};
|
|
157
|
+
/** Each tool key the server is started with (a `tool` row whose key, its first field, the server sets), checked against
|
|
158
|
+
* its provider once, as a saved one is (key-check.ts), through the fetch `fetchFor` gives for the row (null: not
|
|
159
|
+
* checked, its check stays null); all at once, none holding up another or the start. `answered` is called as each
|
|
160
|
+
* answers, unless `stop` (the server closing) came first. A fetch that cannot be made (a fake that does not load)
|
|
161
|
+
* reads as not checked, saying so; this never rejects. A model key is not checked here: a run on a refused one fails
|
|
162
|
+
* at its first request, saying so. */
|
|
163
|
+
checkServer(fetchFor: (id: string) => Promise<typeof fetch | null>, stop: AbortSignal, answered: () => void): Promise<void>;
|
|
164
|
+
/** The variables of the keys the server was started with that their provider refused at the start, each with why
|
|
165
|
+
* (capabilities' tools that need one read unavailable: tools.ts toolsView). */
|
|
166
|
+
refused(): Record<string, string>;
|
|
167
|
+
/** A PUT's check, before save(): the key it sends (its row's first field; Browserbase's project id is no key) checked
|
|
168
|
+
* against its provider (key-check.ts), the fetch `fetchFor` gives for the row (undefined: none, so not checked), its
|
|
169
|
+
* wait cut by `stop`. The body is held to what save() takes first, so a save that would be refused sends nothing. A
|
|
170
|
+
* key the provider refuses is 422 in its words, and nothing is kept; undefined when the body sends no key: the row's
|
|
171
|
+
* check stays as it was. */
|
|
172
|
+
check(id: string, body: Record<string, unknown>, fetchFor: (id: string) => Promise<typeof fetch | undefined>, stop?: AbortSignal): Promise<KeyCheck | undefined>;
|
|
173
|
+
/** PUT: each of `body.values` sealed under the seal key, and `check` (check()'s) kept for the key when given; the key's
|
|
174
|
+
* other fields stay as they are. A key saved with no check (a caller other than the route) drops the one it had. */
|
|
175
|
+
save(id: string, body: Record<string, unknown>, check?: KeyCheck): void;
|
|
176
|
+
/** What a PUT may save: the key's row and the body's values, or 422 (the body), 409 (a field the server's, or no seal
|
|
177
|
+
* key). No message holds a value, or any part of one. */
|
|
178
|
+
private admit;
|
|
179
|
+
/** DELETE: the key's stored fields gone, and its check. Refused only when the server sets every field of it: none is
|
|
180
|
+
* the page's. */
|
|
181
|
+
remove(id: string): void;
|
|
182
|
+
private row;
|
|
183
|
+
/** 409 when this server's own environment (its launcher) sets one of `names`: that one wins, and is changed where the
|
|
184
|
+
* server is started. */
|
|
185
|
+
private notServers;
|
|
186
|
+
private write;
|
|
187
|
+
}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
// The keys a person saves for this server's tools, by name, through /v2/settings/tool-keys. DIR/tool-keys.json (0600,
|
|
2
|
+
// written whole and synced, then renamed) holds them only as AES-256-GCM ciphertext (pi-durable-agent's `seal`) under the seal
|
|
3
|
+
// key the launcher hands the server (AGENTRUN_SEAL_KEY), which is never in the data directory: a copy of the directory
|
|
4
|
+
// holds ciphertext alone. Each value is bound to its field's name and this format, so one moved to another name does
|
|
5
|
+
// not open, and carries a check of the key it was sealed under (an HMAC of a fixed label, neither the key nor a hash of
|
|
6
|
+
// it), so a value sealed under another key reads as that, told apart from a damaged one. Only this process opens a
|
|
7
|
+
// value, when it is asked for. One that does not open is never a crash and never a clear fallback: it reads as not
|
|
8
|
+
// set, to be entered again, and the log says why once, naming the file, never a value. The server reads a saved value by
|
|
9
|
+
// name as it reads a launcher's secret (secrets.ts' `secret`), the launcher's winning: a run gets it over its pipe (a
|
|
10
|
+
// decision key on its decision descriptor) at its launch, and capabilities and checks see it once it is saved. A key is
|
|
11
|
+
// checked against its provider before it is kept (key-check.ts): one the provider refuses is never kept, and the file
|
|
12
|
+
// keeps, beside the sealed values, whether each key was accepted or kept not checked, and why (never any of a value).
|
|
13
|
+
import { createHmac } from "node:crypto";
|
|
14
|
+
import { readFileSync } from "node:fs";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import { seal, unseal } from "@parcha/pi-durable-agent";
|
|
17
|
+
import { decisionsOf } from "./decisions.js";
|
|
18
|
+
import { ApiError, invalid } from "./errors.js";
|
|
19
|
+
import { writeDurableSync } from "./files.js";
|
|
20
|
+
import { HARNESSES } from "./harness.js";
|
|
21
|
+
import { checkKey } from "./key-check.js";
|
|
22
|
+
import { toolsView } from "./tools.js";
|
|
23
|
+
import { MODEL_KEYS } from "./model.js";
|
|
24
|
+
import { isObject, unknownFields } from "./validate.js";
|
|
25
|
+
export const TOOL_KEYS_FILE = "tool-keys.json";
|
|
26
|
+
export const SEAL_KEY_ENV = "AGENTRUN_SEAL_KEY";
|
|
27
|
+
const FORMAT = "tool-keys/1";
|
|
28
|
+
const MAX_VALUE_BYTES = 1024;
|
|
29
|
+
const CONTROL = /[\u0000-\u001f\u007f-\u009f]/;
|
|
30
|
+
/** Each key a person may save, fixed here: its kind, the fields it is entered as (BROWSERBASE_PROJECT_ID is no secret,
|
|
31
|
+
* and is kept the same way), and what it may unlock: tools by name, and `decisions.<id>`, a workflow's decision
|
|
32
|
+
* provider. GET lists only those this server binds (unlockedBy), so a name here the server does not bind is never
|
|
33
|
+
* promised, and one shows by itself once the server binds it. A `tool` key's fields reach only the runs that name a
|
|
34
|
+
* tool it unlocks (runs.ts' secretsFor); a key of any other kind (a model's) reaches every run. */
|
|
35
|
+
export const TOOL_KEYS = [
|
|
36
|
+
{ id: "parallel", kind: "tool", label: "Parallel", fields: ["PARALLEL_API_KEY"], unlocks: ["web_fetch", "web_search", "decisions.parallel"] },
|
|
37
|
+
{ id: "monid", kind: "tool", label: "Monid", fields: ["MONID_API_KEY"], unlocks: ["monid.discover", "monid.inspect", "monid.run"] },
|
|
38
|
+
{ id: "browserbase", kind: "tool", label: "Browserbase", fields: ["BROWSERBASE_API_KEY", "BROWSERBASE_PROJECT_ID"], unlocks: ["browser"] },
|
|
39
|
+
// The browser's other vendor (tool-plane.ts BROWSER_KEYS): a run's browser is the one its plane picks.
|
|
40
|
+
{ id: "kernel", kind: "tool", label: "Kernel", fields: ["KERNEL_API_KEY"], unlocks: ["browser"] },
|
|
41
|
+
// Jev's decision provider (decisions.ts' `jev`), on TypeSafe's key: a workflow's judges, as Parallel's key unlocks its.
|
|
42
|
+
{ id: "typesafe", kind: "tool", label: "TypeSafe (Jev)", fields: ["TYPESAFE_API_KEY"], unlocks: ["decisions.jev"] },
|
|
43
|
+
// A model provider's key (model.ts' MODEL_KEYS, which give each its default model): every run gets it, as the
|
|
44
|
+
// launcher's model keys reach every run. Its harnesses are pi and those that talk to the provider's API (MODEL_KEYS).
|
|
45
|
+
...MODEL_KEYS.map(({ id, label, field, harnesses }) => ({ id, kind: "model", label, fields: [field], unlocks: [], harnesses })),
|
|
46
|
+
];
|
|
47
|
+
const FIELDS = TOOL_KEYS.flatMap((row) => row.fields);
|
|
48
|
+
/** The fields of the `tool` rows: keys a tool or a decision provider reads, and no harness (the harnesses read their
|
|
49
|
+
* packages, their pins and their models' keys). A row of another kind (a model's) is outside them. */
|
|
50
|
+
export const TOOL_FIELDS = TOOL_KEYS.filter((row) => row.kind === "tool").flatMap((row) => row.fields);
|
|
51
|
+
/** What `candidates` unlock here, as capabilities show it: the tools of `tools` (capabilities' `tools`, available or
|
|
52
|
+
* not) and the providers of `providers` (its `workflows.decisions.providers`) named among them, and the harnesses
|
|
53
|
+
* that run those tools. A name neither holds is left out; a key that unlocks none of its names lists nothing. */
|
|
54
|
+
export function unlockedBy(candidates, tools, providers) {
|
|
55
|
+
const bound = new Set([...tools.map((tool) => tool.name), ...providers.map((provider) => `decisions.${provider.id}`)]);
|
|
56
|
+
const unlocks = candidates.filter((name) => bound.has(name));
|
|
57
|
+
const run = new Set(tools.filter((tool) => unlocks.includes(tool.name)).flatMap((tool) => tool.harnesses));
|
|
58
|
+
return { unlocks, harnesses: HARNESSES.filter((harness) => run.has(harness)) };
|
|
59
|
+
}
|
|
60
|
+
/** What a row unlocks here: a `tool` row's, its tools and providers as capabilities bind them (unlockedBy); any other
|
|
61
|
+
* kind's (a model's), no tool, on those that run on it (its `harnesses`, when it names them) that this server runs
|
|
62
|
+
* (`offers`, harness.ts' harnessOffers) or would once its key is set: one unavailable only for want of a model key
|
|
63
|
+
* among whose `keys` is the row's (its first field). So OpenRouter's row names all three before any key is saved; a
|
|
64
|
+
* harness not installed, off its pins or too big for this host is left out. */
|
|
65
|
+
export function rowUnlocks(row, tools, providers, offers) {
|
|
66
|
+
if (row.kind === "tool")
|
|
67
|
+
return unlockedBy(row.unlocks, tools, providers);
|
|
68
|
+
const key = row.fields?.[0];
|
|
69
|
+
const runs = (harness) => offers.some((offer) => offer.id === harness && (offer.available || (key !== undefined && offer.keys?.includes(key) === true)));
|
|
70
|
+
return { unlocks: [], harnesses: HARNESSES.filter((harness) => (row.harnesses ?? HARNESSES).includes(harness) && runs(harness)) };
|
|
71
|
+
}
|
|
72
|
+
/** The key that unlocks the tool `name` (capabilities' `tools[].key`, so a page links "needs the X key" to it), if any. */
|
|
73
|
+
export const keyOf = (name) => TOOL_KEYS.find((row) => row.unlocks.includes(name))?.id;
|
|
74
|
+
/** Why a stored value does not open, in the order a re-entry names one: no seal key now, another than it was sealed
|
|
75
|
+
* under, or the file (or its entry) damaged. */
|
|
76
|
+
export const UNREADABLE = ["seal_key_missing", "seal_key_changed", "file_unreadable"];
|
|
77
|
+
/** The check a key kept before this server checked keys reads as: none was made. */
|
|
78
|
+
const UNCHECKED = { state: "not_checked", reason: "Saved before this server checked keys; save it again to check it" };
|
|
79
|
+
/** A check as the file keeps it, else undefined. */
|
|
80
|
+
const keptCheck = (entry) => !isObject(entry) ? undefined
|
|
81
|
+
: entry.state === "accepted" && typeof entry.at === "string" ? { state: "accepted", at: entry.at }
|
|
82
|
+
: entry.state === "not_checked" && typeof entry.reason === "string" ? { state: "not_checked", reason: entry.reason, ...(typeof entry.at === "string" ? { at: entry.at } : {}) }
|
|
83
|
+
: undefined;
|
|
84
|
+
/** The seal key `text` gives: 32 bytes in base64 (or base64url), else none. */
|
|
85
|
+
export function sealKeyOf(text) {
|
|
86
|
+
const trimmed = text?.trim() ?? "";
|
|
87
|
+
return /^[A-Za-z0-9+/_-]{43}=?$/.test(trimmed) ? Buffer.from(trimmed, "base64") : undefined;
|
|
88
|
+
}
|
|
89
|
+
const aad = (name) => `${FORMAT}|${name}`;
|
|
90
|
+
export class ToolKeys {
|
|
91
|
+
env;
|
|
92
|
+
say;
|
|
93
|
+
file;
|
|
94
|
+
key;
|
|
95
|
+
sealCheck;
|
|
96
|
+
said = "";
|
|
97
|
+
/** The checks of the keys the server was started with, by key id, each once it answered (checkServer). */
|
|
98
|
+
started = {};
|
|
99
|
+
/** `env` is what the server is started with (withSecrets' `"own"`, never a saved key, or each would read as the
|
|
100
|
+
* server's and could not be deleted): a field it sets is the server's, and wins. `say` takes a log line's fields. */
|
|
101
|
+
constructor(data, env, say) {
|
|
102
|
+
this.env = env;
|
|
103
|
+
this.say = say;
|
|
104
|
+
this.file = join(data, TOOL_KEYS_FILE);
|
|
105
|
+
this.key = sealKeyOf(env[SEAL_KEY_ENV]);
|
|
106
|
+
this.sealCheck = this.key && createHmac("sha256", this.key).update(`agentrun-serve ${FORMAT} key check`).digest("base64url");
|
|
107
|
+
if (env[SEAL_KEY_ENV] !== undefined && !this.key)
|
|
108
|
+
say({ condition: "seal_key_invalid", name: SEAL_KEY_ENV });
|
|
109
|
+
}
|
|
110
|
+
/** The file's entries, none with no file; "unreadable" when it does not read as this format. A file from before checks
|
|
111
|
+
* has none. */
|
|
112
|
+
kept() {
|
|
113
|
+
try {
|
|
114
|
+
const doc = JSON.parse(readFileSync(this.file, "utf8"));
|
|
115
|
+
if (!isObject(doc) || doc.v !== 1 || !isObject(doc.fields))
|
|
116
|
+
return "unreadable";
|
|
117
|
+
const checks = Object.entries(isObject(doc.checks) ? doc.checks : {}).flatMap(([id, entry]) => {
|
|
118
|
+
const check = keptCheck(entry);
|
|
119
|
+
return check ? [[id, check]] : [];
|
|
120
|
+
});
|
|
121
|
+
return { fields: { ...doc.fields }, checks: Object.fromEntries(checks) };
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
return error.code === "ENOENT" ? { fields: {}, checks: {} } : "unreadable";
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
open(name, entry) {
|
|
128
|
+
if (!this.key)
|
|
129
|
+
return { unreadable: "seal_key_missing" };
|
|
130
|
+
if (!isObject(entry) || typeof entry.sealed !== "string" || typeof entry.check !== "string")
|
|
131
|
+
return { unreadable: "file_unreadable" };
|
|
132
|
+
if (entry.check !== this.sealCheck)
|
|
133
|
+
return { unreadable: "seal_key_changed" };
|
|
134
|
+
try {
|
|
135
|
+
return { value: unseal(this.key, Buffer.from(entry.sealed, "base64"), aad(name)).toString("utf8") };
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
return { unreadable: "file_unreadable" };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/** The stored value of the field `name`, opened now (secrets.ts reads a saved key by name through this); undefined
|
|
142
|
+
* with none, or one that does not open, which view() asks for again: never a clear fallback. */
|
|
143
|
+
value(name) {
|
|
144
|
+
if (!FIELDS.includes(name))
|
|
145
|
+
return undefined;
|
|
146
|
+
const kept = this.kept();
|
|
147
|
+
const opened = kept !== "unreadable" && kept.fields[name] !== undefined ? this.open(name, kept.fields[name]) : undefined;
|
|
148
|
+
return opened && "value" in opened ? opened.value : undefined;
|
|
149
|
+
}
|
|
150
|
+
/** Every stored value that opens, by field name. */
|
|
151
|
+
values() {
|
|
152
|
+
return Object.fromEntries(FIELDS.flatMap((name) => {
|
|
153
|
+
const value = this.value(name);
|
|
154
|
+
return value === undefined ? [] : [[name, value]];
|
|
155
|
+
}));
|
|
156
|
+
}
|
|
157
|
+
/** GET: each key's fields, whether set and where from (never a value), what it unlocks of what capabilities shows
|
|
158
|
+
* here (a model key: the harnesses `offers` says this server runs or would on the key), its check when its key (its first field) is
|
|
159
|
+
* stored here (null otherwise: the server's own is not checked here), and what to enter again, with why. */
|
|
160
|
+
view(offers = []) {
|
|
161
|
+
const kept = this.kept();
|
|
162
|
+
const again = [];
|
|
163
|
+
const tools = toolsView(this.env);
|
|
164
|
+
const providers = decisionsOf(this.env);
|
|
165
|
+
const keys = TOOL_KEYS.map((row) => {
|
|
166
|
+
const fields = row.fields.map((name) => {
|
|
167
|
+
if (this.env[name])
|
|
168
|
+
return { name, set: true, source: "server" };
|
|
169
|
+
const opened = kept !== "unreadable" && kept.fields[name] !== undefined ? this.open(name, kept.fields[name]) : undefined;
|
|
170
|
+
if (opened && "value" in opened)
|
|
171
|
+
return { name, set: true, source: "stored" };
|
|
172
|
+
if (opened)
|
|
173
|
+
again.push({ name, why: opened.unreadable });
|
|
174
|
+
return { name, set: false, source: null, ...(opened ? { stored_unreadable: true } : {}) };
|
|
175
|
+
});
|
|
176
|
+
const check = fields[0].source === "stored" && kept !== "unreadable" ? (kept.checks[row.id] ?? UNCHECKED) : fields[0].source === "server" ? (this.started[row.id] ?? null) : null;
|
|
177
|
+
return { id: row.id, kind: row.kind, label: row.label, ...rowUnlocks(row, tools, providers, offers), fields, check };
|
|
178
|
+
});
|
|
179
|
+
// A file that does not read as a whole names no field: which it held is unknown.
|
|
180
|
+
const reason = kept === "unreadable" ? "file_unreadable" : UNREADABLE.find((why) => again.some((field) => field.why === why));
|
|
181
|
+
const reenter = reason ? { reason, fields: again.map((field) => field.name) } : null;
|
|
182
|
+
const said = JSON.stringify(reenter);
|
|
183
|
+
if (reenter && said !== this.said)
|
|
184
|
+
this.say({ condition: reenter.reason, file: this.file, fields: reenter.fields.join(",") || "unknown" });
|
|
185
|
+
this.said = said;
|
|
186
|
+
return { keys, reenter };
|
|
187
|
+
}
|
|
188
|
+
/** Each tool key the server is started with (a `tool` row whose key, its first field, the server sets), checked against
|
|
189
|
+
* its provider once, as a saved one is (key-check.ts), through the fetch `fetchFor` gives for the row (null: not
|
|
190
|
+
* checked, its check stays null); all at once, none holding up another or the start. `answered` is called as each
|
|
191
|
+
* answers, unless `stop` (the server closing) came first. A fetch that cannot be made (a fake that does not load)
|
|
192
|
+
* reads as not checked, saying so; this never rejects. A model key is not checked here: a run on a refused one fails
|
|
193
|
+
* at its first request, saying so. */
|
|
194
|
+
async checkServer(fetchFor, stop, answered) {
|
|
195
|
+
await Promise.all(TOOL_KEYS.filter((row) => row.kind === "tool" && this.env[row.fields[0]]).map(async (row) => {
|
|
196
|
+
const name = row.fields[0];
|
|
197
|
+
let fetcher;
|
|
198
|
+
try {
|
|
199
|
+
fetcher = await fetchFor(row.id);
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
if (stop.aborted)
|
|
203
|
+
return;
|
|
204
|
+
const why = error?.code ?? error?.name ?? "an error";
|
|
205
|
+
this.started[row.id] = { state: "not_checked", reason: `${row.label}'s key could not be checked: its check did not start (${why})`, at: new Date().toISOString() };
|
|
206
|
+
answered();
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (!fetcher)
|
|
210
|
+
return;
|
|
211
|
+
const outcome = await checkKey(row, this.env[name], this.env, fetcher, undefined, stop, name).catch(() => undefined);
|
|
212
|
+
if (!outcome || stop.aborted)
|
|
213
|
+
return;
|
|
214
|
+
this.started[row.id] = outcome.state === "refused" ? { state: "refused", reason: `${outcome.message} when this server started: set the right one where AgentRun starts, then restart it`, at: new Date().toISOString() } : outcome;
|
|
215
|
+
answered();
|
|
216
|
+
}));
|
|
217
|
+
}
|
|
218
|
+
/** The variables of the keys the server was started with that their provider refused at the start, each with why
|
|
219
|
+
* (capabilities' tools that need one read unavailable: tools.ts toolsView). */
|
|
220
|
+
refused() {
|
|
221
|
+
return Object.fromEntries(TOOL_KEYS.flatMap((row) => {
|
|
222
|
+
const check = this.started[row.id];
|
|
223
|
+
return check?.state === "refused" ? [[row.fields[0], check.reason]] : [];
|
|
224
|
+
}));
|
|
225
|
+
}
|
|
226
|
+
/** A PUT's check, before save(): the key it sends (its row's first field; Browserbase's project id is no key) checked
|
|
227
|
+
* against its provider (key-check.ts), the fetch `fetchFor` gives for the row (undefined: none, so not checked), its
|
|
228
|
+
* wait cut by `stop`. The body is held to what save() takes first, so a save that would be refused sends nothing. A
|
|
229
|
+
* key the provider refuses is 422 in its words, and nothing is kept; undefined when the body sends no key: the row's
|
|
230
|
+
* check stays as it was. */
|
|
231
|
+
async check(id, body, fetchFor, stop) {
|
|
232
|
+
const { row, values } = this.admit(id, body);
|
|
233
|
+
const name = row.fields[0];
|
|
234
|
+
if (values[name] === undefined)
|
|
235
|
+
return undefined;
|
|
236
|
+
const outcome = await checkKey(row, values[name], this.env, await fetchFor(row.id), undefined, stop);
|
|
237
|
+
if (outcome.state === "refused")
|
|
238
|
+
throw invalid([{ field: `values.${name}`, reason: "refused_by_provider", message: outcome.message }]);
|
|
239
|
+
return outcome;
|
|
240
|
+
}
|
|
241
|
+
/** PUT: each of `body.values` sealed under the seal key, and `check` (check()'s) kept for the key when given; the key's
|
|
242
|
+
* other fields stay as they are. A key saved with no check (a caller other than the route) drops the one it had. */
|
|
243
|
+
save(id, body, check) {
|
|
244
|
+
const { row, values } = this.admit(id, body);
|
|
245
|
+
const kept = this.kept();
|
|
246
|
+
// A file that does not read is replaced: what it held opens nowhere.
|
|
247
|
+
const { fields, checks } = kept === "unreadable" ? { fields: {}, checks: {} } : kept;
|
|
248
|
+
for (const [name, value] of Object.entries(values))
|
|
249
|
+
fields[name] = { sealed: seal(this.key, Buffer.from(value, "utf8"), aad(name)).toString("base64"), check: this.sealCheck };
|
|
250
|
+
if (values[row.fields[0]] !== undefined)
|
|
251
|
+
delete checks[row.id];
|
|
252
|
+
if (check)
|
|
253
|
+
checks[row.id] = check;
|
|
254
|
+
this.write(fields, checks);
|
|
255
|
+
}
|
|
256
|
+
/** What a PUT may save: the key's row and the body's values, or 422 (the body), 409 (a field the server's, or no seal
|
|
257
|
+
* key). No message holds a value, or any part of one. */
|
|
258
|
+
admit(id, body) {
|
|
259
|
+
const row = this.row(id);
|
|
260
|
+
const issues = unknownFields(body, ["values"]);
|
|
261
|
+
const values = isObject(body.values) ? body.values : {};
|
|
262
|
+
if (!Object.keys(values).length)
|
|
263
|
+
issues.push({ field: "values", reason: body.values === undefined ? "missing" : "wrong_type", message: `values holds one or more of ${row.fields.join(", ")}, by name` });
|
|
264
|
+
for (const [name, value] of Object.entries(values)) {
|
|
265
|
+
const field = `values.${name}`;
|
|
266
|
+
if (!row.fields.includes(name))
|
|
267
|
+
issues.push({ field, reason: "not_allowed", message: `The key ${row.id} has the fields ${row.fields.join(", ")}.` });
|
|
268
|
+
else if (typeof value !== "string" || value === "")
|
|
269
|
+
issues.push({ field, reason: "wrong_type", message: `${name} is text, not empty` });
|
|
270
|
+
else if (CONTROL.test(value))
|
|
271
|
+
issues.push({ field, reason: "invalid", message: `${name} holds a control character` });
|
|
272
|
+
else if (Buffer.byteLength(value) > MAX_VALUE_BYTES)
|
|
273
|
+
issues.push({ field, reason: "too_long", message: `${name} is at most ${MAX_VALUE_BYTES} bytes` });
|
|
274
|
+
}
|
|
275
|
+
if (issues.length)
|
|
276
|
+
throw invalid(issues);
|
|
277
|
+
this.notServers(Object.keys(values));
|
|
278
|
+
if (!this.key)
|
|
279
|
+
throw new ApiError("no_sealing_key", `This server was handed no seal key (${SEAL_KEY_ENV}), so it keeps no tool key: it never keeps one in the clear.`);
|
|
280
|
+
return { row, values };
|
|
281
|
+
}
|
|
282
|
+
/** DELETE: the key's stored fields gone, and its check. Refused only when the server sets every field of it: none is
|
|
283
|
+
* the page's. */
|
|
284
|
+
remove(id) {
|
|
285
|
+
const row = this.row(id);
|
|
286
|
+
if (row.fields.every((name) => this.env[name]))
|
|
287
|
+
this.notServers(row.fields);
|
|
288
|
+
const kept = this.kept();
|
|
289
|
+
if (kept !== "unreadable" && !row.fields.some((name) => name in kept.fields))
|
|
290
|
+
return;
|
|
291
|
+
const { fields, checks } = kept === "unreadable" ? { fields: {}, checks: {} } : kept;
|
|
292
|
+
for (const name of row.fields)
|
|
293
|
+
delete fields[name];
|
|
294
|
+
delete checks[row.id];
|
|
295
|
+
this.write(fields, checks);
|
|
296
|
+
}
|
|
297
|
+
row(id) {
|
|
298
|
+
const row = TOOL_KEYS.find((r) => r.id === id);
|
|
299
|
+
if (!row)
|
|
300
|
+
throw invalid([{ field: "key_id", reason: "not_an_option", message: `The tool keys are ${TOOL_KEYS.map((r) => r.id).join(", ")}.` }]);
|
|
301
|
+
return row;
|
|
302
|
+
}
|
|
303
|
+
/** 409 when this server's own environment (its launcher) sets one of `names`: that one wins, and is changed where the
|
|
304
|
+
* server is started. */
|
|
305
|
+
notServers(names) {
|
|
306
|
+
const held = names.filter((name) => this.env[name]);
|
|
307
|
+
if (held.length)
|
|
308
|
+
throw new ApiError("key_set_by_server", `This server is started with ${held.join(" and ")}: change it where the server is started.`, { details: { fields: held } });
|
|
309
|
+
}
|
|
310
|
+
write(fields, checks) {
|
|
311
|
+
// On disk, the file and then its directory, before the 204: a crash after it neither loses a saved key nor brings
|
|
312
|
+
// back a forgotten one.
|
|
313
|
+
writeDurableSync(this.file, `${JSON.stringify({ v: 1, fields, checks }, null, 2)}\n`);
|
|
314
|
+
}
|
|
315
|
+
}
|