@soft-boy/agentschema 0.3.1 → 0.3.3
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/README.md +30 -18
- package/lib/check.js +365 -0
- package/lib/files.js +30 -0
- package/lib/index.js +18 -0
- package/{src → lib}/instance.js +10 -21
- package/{src → lib}/markdown.js +13 -11
- package/lib/scaffold.js +49 -0
- package/lib/skills.js +58 -0
- package/lib/stream.js +50 -0
- package/{src → lib}/tools.js +80 -57
- package/{src → lib}/vaults.js +27 -33
- package/package.json +7 -1
- package/src/chat.js +3 -50
- package/src/check.js +13 -362
- package/src/cred-menu.js +1 -1
- package/src/cred.js +20 -11
- package/src/custom-tools.js +17 -0
- package/src/deploy.js +63 -25
- package/src/folder.js +52 -0
- package/src/init.js +4 -47
- package/src/lock.js +6 -0
- package/src/node.js +9 -0
- package/src/oauth.js +4 -4
- package/src/session.js +34 -10
- package/src/tools-menu.js +4 -4
- package/src/skills.js +0 -79
package/src/check.js
CHANGED
|
@@ -1,363 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
[/\bghp_[A-Za-z0-9]{20,}/, "a GitHub token"],
|
|
15
|
-
[/\bgithub_pat_[A-Za-z0-9_]{20,}/, "a GitHub token"],
|
|
16
|
-
[/\bxox[bpas]-[A-Za-z0-9-]{10,}/, "a Slack token"],
|
|
17
|
-
[/\bAKIA[0-9A-Z]{16}\b/, "an AWS access key"],
|
|
18
|
-
];
|
|
19
|
-
|
|
20
|
-
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
21
|
-
|
|
22
|
-
// Validates an instance folder without calling the API.
|
|
23
|
-
// Returns { errors, warnings, instance }; instance is null when the files can't be read.
|
|
24
|
-
export function check(dir) {
|
|
25
|
-
const { agent, env, errors } = readInstance(dir);
|
|
26
|
-
const { tools, errors: toolErrors } = readTools(dir);
|
|
27
|
-
const { vaults, errors: vaultErrors } = readVaults(dir);
|
|
28
|
-
const { skills, errors: skillErrors } = readSkills(dir);
|
|
29
|
-
errors.push(...toolErrors, ...vaultErrors, ...skillErrors);
|
|
30
|
-
const warnings = [];
|
|
31
|
-
|
|
32
|
-
if (agent) checkAgent(agent, errors, warnings);
|
|
33
|
-
if (env) checkEnv(env, errors, warnings);
|
|
34
|
-
checkTools(tools, errors);
|
|
35
|
-
if (agent) checkMcpServers(agent, tools, errors);
|
|
36
|
-
if (agent && env) checkMcpNetworking(agent, env, warnings);
|
|
37
|
-
for (const skill of skills) checkSkill(skill, errors);
|
|
38
|
-
const declaredSkills = Array.isArray(agent?.data.skills) ? agent.data.skills.length : 0;
|
|
39
|
-
if (declaredSkills + skills.length > 500) errors.push(`${declaredSkills + skills.length} skills; the limit is 500`);
|
|
40
|
-
for (const vault of vaults) checkVault(vault, errors, warnings);
|
|
41
|
-
checkEnvVarCredentials(vaults, env, warnings);
|
|
42
|
-
|
|
43
|
-
const vaultFiles = vaults.flatMap((v) => [...(v.file ? [[`${v.path}/${VAULT_FILE}`, v.file]] : []), ...v.credentials.map((c) => [c.file, c])]);
|
|
44
|
-
const skillTexts = skills.flatMap((s) => s.files.map((f) => [`${s.path}/${f.rel}`, { raw: f.bytes.toString("utf8") }]));
|
|
45
|
-
for (const [file, parsed] of [[AGENT_FILE, agent], [ENV_FILE, env], ...tools.map((t) => [t.file, t]), ...vaultFiles, ...skillTexts]) {
|
|
46
|
-
if (!parsed) continue;
|
|
47
|
-
for (const [pattern, what] of SECRET_PATTERNS) {
|
|
48
|
-
if (pattern.test(parsed.raw)) errors.push(`${file}: looks like it contains ${what}; keep secrets out of these files`);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return { errors, warnings, instance: agent && env ? { agent, env, tools, vaults, skills } : null };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function checkAgent({ data, body }, errors, warnings) {
|
|
56
|
-
const err = (msg) => errors.push(`${AGENT_FILE}: ${msg}`);
|
|
57
|
-
|
|
58
|
-
if (typeof data.name !== "string" || data.name.trim() === "") err("`name` is required");
|
|
59
|
-
else if (data.name.length > 256) err("`name` must be at most 256 characters");
|
|
60
|
-
|
|
61
|
-
if (data.model === undefined) err("`model` is required");
|
|
62
|
-
else if (isObject(data.model) ? typeof data.model.id !== "string" : typeof data.model !== "string") {
|
|
63
|
-
err("`model` must be a model ID, or a mapping with an `id`");
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if ("system" in data) err("`system` is not allowed in frontmatter; the Markdown body is the system prompt");
|
|
67
|
-
if (body.length > 100_000) err(`system prompt is ${body.length} characters; the limit is 100,000`);
|
|
68
|
-
|
|
69
|
-
if (data.description !== undefined && data.description !== null) {
|
|
70
|
-
if (typeof data.description !== "string") err("`description` must be a string");
|
|
71
|
-
else if (data.description.length > 2048) err("`description` must be at most 2048 characters");
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if ("tools" in data) err(`\`tools\` is not allowed in frontmatter; define each tool as a file in ${TOOLS_DIR}/`);
|
|
75
|
-
|
|
76
|
-
for (const [field, max] of [["mcp_servers", 20], ["skills", 500]]) {
|
|
77
|
-
if (data[field] === undefined) continue;
|
|
78
|
-
if (!Array.isArray(data[field])) err(`\`${field}\` must be a list`);
|
|
79
|
-
else if (data[field].length > max) err(`\`${field}\` has ${data[field].length} entries; the limit is ${max}`);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (Array.isArray(data.skills)) {
|
|
83
|
-
for (const [index, skill] of data.skills.entries()) checkSkillRef(skill, (msg) => err(`\`skills[${index}]\`: ${msg}`));
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
checkMetadata(data.metadata, 16, err);
|
|
87
|
-
|
|
88
|
-
for (const key of Object.keys(data)) {
|
|
89
|
-
if (!AGENT_FIELDS.has(key) && key !== "system" && key !== "tools") {
|
|
90
|
-
warnings.push(`${AGENT_FILE}: unknown field \`${key}\` will be sent to the API as written`);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function checkEnv({ data }, errors, warnings) {
|
|
96
|
-
const err = (msg) => errors.push(`${ENV_FILE}: ${msg}`);
|
|
97
|
-
|
|
98
|
-
if (typeof data.name !== "string" || data.name.trim() === "") err("`name` is required");
|
|
99
|
-
|
|
100
|
-
if (data.config !== undefined) {
|
|
101
|
-
const { config } = data;
|
|
102
|
-
if (!isObject(config)) err("`config` must be a mapping");
|
|
103
|
-
else {
|
|
104
|
-
if (!["cloud", "self_hosted"].includes(config.type)) err("`config.type` must be `cloud` or `self_hosted`");
|
|
105
|
-
const net = config.networking;
|
|
106
|
-
if (net !== undefined && !(isObject(net) && ["limited", "unrestricted"].includes(net.type))) {
|
|
107
|
-
err("`config.networking.type` must be `limited` or `unrestricted`");
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (data.description !== undefined && data.description !== null && typeof data.description !== "string") {
|
|
113
|
-
err("`description` must be a string");
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
checkMetadata(data.metadata, Infinity, err);
|
|
117
|
-
|
|
118
|
-
for (const key of Object.keys(data)) {
|
|
119
|
-
if (!ENV_FIELDS.has(key)) warnings.push(`${ENV_FILE}: unknown field \`${key}\` will be sent to the API as written`);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// An entry in agent.md's `skills` list, in the API's shape.
|
|
124
|
-
function checkSkillRef(skill, err) {
|
|
125
|
-
if (typeof skill === "string") {
|
|
126
|
-
return err(`write this as a mapping with \`type\` and \`skill_id\`; a skill in ${SKILLS_DIR}/ is attached without being listed`);
|
|
127
|
-
}
|
|
128
|
-
if (!isObject(skill)) return err("must be a mapping with `type` and `skill_id`");
|
|
129
|
-
if (!["anthropic", "custom"].includes(skill.type)) err("`type` must be `anthropic` or `custom`");
|
|
130
|
-
if (typeof skill.skill_id !== "string" || skill.skill_id === "") err("`skill_id` is required");
|
|
131
|
-
else if (skill.type === "custom" && !skill.skill_id.startsWith("skill_")) err("a custom skill's `skill_id` starts with skill_");
|
|
132
|
-
if (skill.version !== undefined && skill.version !== null && typeof skill.version !== "string") {
|
|
133
|
-
err("`version` must be a string (quote numbers)");
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function checkSkill(skill, errors) {
|
|
138
|
-
const err = (msg) => errors.push(`${skill.file}: ${msg}`);
|
|
139
|
-
const { name, description } = skill.data;
|
|
140
|
-
if (typeof name !== "string" || name === "") err("`name` is required");
|
|
141
|
-
else {
|
|
142
|
-
if (name !== skill.name) err(`\`name\` must match the folder's name (${skill.name})`);
|
|
143
|
-
if (!/^[a-z0-9-]{1,64}$/.test(name)) err("`name` may only use lowercase letters, digits and hyphens, up to 64 characters");
|
|
144
|
-
if (/anthropic|claude/.test(name)) err("`name` can't contain the reserved words `anthropic` or `claude`");
|
|
145
|
-
}
|
|
146
|
-
if (typeof description !== "string" || description.trim() === "") err("`description` is required");
|
|
147
|
-
else {
|
|
148
|
-
if (description.length > 1024) err(`\`description\` is ${description.length} characters; the limit is 1,024`);
|
|
149
|
-
if (/<[^>]+>/.test(description)) err("`description` can't contain XML tags");
|
|
150
|
-
}
|
|
151
|
-
const size = skill.files.reduce((total, f) => total + f.bytes.length, 0);
|
|
152
|
-
if (size > MAX_UPLOAD_BYTES) errors.push(`${skill.path}: its files add up to ${Math.ceil(size / 1024 / 1024)} MB; the limit is 30 MB`);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const mcpServers = (agent) => (Array.isArray(agent.data.mcp_servers) ? agent.data.mcp_servers.filter(isObject) : []);
|
|
156
|
-
|
|
157
|
-
// Each server agent.md declares needs a toolset file, and each toolset file a declared server;
|
|
158
|
-
// the API rejects an agent otherwise.
|
|
159
|
-
function checkMcpServers(agent, tools, errors) {
|
|
160
|
-
const err = (msg) => errors.push(`${AGENT_FILE}: ${msg}`);
|
|
161
|
-
const names = new Set();
|
|
162
|
-
for (const server of mcpServers(agent)) {
|
|
163
|
-
const label = typeof server.name === "string" && server.name ? `\`${server.name}\`` : "an entry";
|
|
164
|
-
if (server.type !== "url") err(`\`mcp_servers\`: ${label} must have \`type: url\``);
|
|
165
|
-
if (typeof server.name !== "string" || server.name === "" || server.name.length > 255) {
|
|
166
|
-
err("`mcp_servers`: each server needs a `name` of 1 to 255 characters");
|
|
167
|
-
} else if (names.has(server.name)) {
|
|
168
|
-
err(`\`mcp_servers\`: \`${server.name}\` is declared twice`);
|
|
169
|
-
}
|
|
170
|
-
if (typeof server.url !== "string" || !/^https?:\/\/\S+$/.test(server.url) || server.url.length > 2048) {
|
|
171
|
-
err(`\`mcp_servers\`: ${label} needs a \`url\`, an http(s) URL of at most 2048 characters`);
|
|
172
|
-
}
|
|
173
|
-
names.add(server.name);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const toolsets = tools.filter((t) => t.data.type === MCP_TOOLSET);
|
|
177
|
-
const covered = new Set(toolsets.map((t) => t.data.mcp_server_name));
|
|
178
|
-
for (const name of names) {
|
|
179
|
-
if (typeof name === "string" && name && !covered.has(name)) {
|
|
180
|
-
err(`\`mcp_servers\`: \`${name}\` has no toolset; add ${TOOLS_DIR}/${name}.md with \`type: mcp_toolset\`, or the API rejects the agent`);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
for (const toolset of toolsets) {
|
|
184
|
-
const name = toolset.data.mcp_server_name;
|
|
185
|
-
if (typeof name === "string" && name && !names.has(name)) {
|
|
186
|
-
errors.push(`${toolset.file}: \`${name}\` isn't in ${AGENT_FILE}'s \`mcp_servers\``);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// A limited network blocks MCP servers that aren't allowed, and the API then refuses to start sessions.
|
|
192
|
-
function checkMcpNetworking(agent, env, warnings) {
|
|
193
|
-
const net = env.data.config?.networking;
|
|
194
|
-
if (net?.type !== "limited" || net.allow_mcp_servers === true) return;
|
|
195
|
-
const allowed = net.allowed_hosts ?? [];
|
|
196
|
-
for (const server of mcpServers(agent)) {
|
|
197
|
-
let host;
|
|
198
|
-
try {
|
|
199
|
-
host = new URL(server.url).host;
|
|
200
|
-
} catch {
|
|
201
|
-
continue;
|
|
202
|
-
}
|
|
203
|
-
if (!allowed.includes(host)) {
|
|
204
|
-
warnings.push(
|
|
205
|
-
`${ENV_FILE}: its limited networking doesn't allow MCP server \`${server.name}\` (${host}), so sessions won't start; ` +
|
|
206
|
-
`set config.networking.allow_mcp_servers to true, or add ${host} to allowed_hosts`,
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
function checkVault(vault, errors, warnings) {
|
|
213
|
-
if (vault.name.length > 255) errors.push(`${vault.path}: a vault's name (its folder name) must be at most 255 characters`);
|
|
214
|
-
|
|
215
|
-
if (vault.file) {
|
|
216
|
-
const file = `${vault.path}/${VAULT_FILE}`;
|
|
217
|
-
const err = (msg) => errors.push(`${file}: ${msg}`);
|
|
218
|
-
const { data } = vault.file;
|
|
219
|
-
if ("display_name" in data) err("`display_name` is not allowed; the folder name is the vault's name");
|
|
220
|
-
checkMetadata(data.metadata, 16, err);
|
|
221
|
-
for (const key of Object.keys(data)) {
|
|
222
|
-
if (key !== "metadata" && key !== "display_name") warnings.push(`${file}: unknown field \`${key}\` will be sent to the API as written`);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
if (vault.credentials.length > 20) errors.push(`${vault.path}: ${vault.credentials.length} credentials; a vault holds at most 20`);
|
|
227
|
-
const urls = new Map();
|
|
228
|
-
const secretNames = new Map();
|
|
229
|
-
for (const cred of vault.credentials) {
|
|
230
|
-
checkCredential(cred, errors, warnings);
|
|
231
|
-
const url = cred.data.auth?.mcp_server_url;
|
|
232
|
-
if (typeof url === "string") {
|
|
233
|
-
if (urls.has(url)) errors.push(`${cred.file}: ${urls.get(url)} is already a credential for ${url}; a vault holds one per server`);
|
|
234
|
-
else urls.set(url, cred.file);
|
|
235
|
-
}
|
|
236
|
-
const secretName = cred.data.auth?.secret_name;
|
|
237
|
-
if (typeof secretName === "string") {
|
|
238
|
-
if (secretNames.has(secretName)) errors.push(`${cred.file}: ${secretNames.get(secretName)} already sets ${secretName}; a vault holds one per variable`);
|
|
239
|
-
else secretNames.set(secretName, cred.file);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// What an environment variable credential depends on outside its own file.
|
|
245
|
-
function checkEnvVarCredentials(vaults, env, warnings) {
|
|
246
|
-
const creds = vaults.flatMap((v) => v.credentials).filter((c) => c.data.auth?.type === ENV_VAR);
|
|
247
|
-
const net = env?.data.config?.networking;
|
|
248
|
-
const seen = new Map();
|
|
249
|
-
for (const cred of creds) {
|
|
250
|
-
const warn = (msg) => warnings.push(`${cred.file}: ${msg}`);
|
|
251
|
-
const { secret_name: name, networking } = cred.data.auth;
|
|
252
|
-
|
|
253
|
-
// Two vaults can both set a variable, but the docs don't say which one wins.
|
|
254
|
-
if (typeof name === "string") {
|
|
255
|
-
if (seen.has(name) && seen.get(name).vault !== cred.vault) warn(`${seen.get(name).file} also sets ${name}, and the API doesn't document which vault wins`);
|
|
256
|
-
else seen.set(name, cred);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if (env?.data.config?.type === "self_hosted") warn("environment variable credentials aren't supported on self-hosted environments yet");
|
|
260
|
-
|
|
261
|
-
// allowed_hosts only decides where the secret is substituted; the environment decides what's reachable.
|
|
262
|
-
if (net?.type === "limited" && networking?.type === "limited" && Array.isArray(networking.allowed_hosts)) {
|
|
263
|
-
for (const host of networking.allowed_hosts) {
|
|
264
|
-
if (!(net.allowed_hosts ?? []).includes(host)) {
|
|
265
|
-
warn(`${ENV_FILE}'s limited networking doesn't allow ${host}, so requests to it will be blocked; add it to allowed_hosts in ${ENV_FILE}`);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function checkCredential(cred, errors, warnings) {
|
|
273
|
-
const err = (msg) => errors.push(`${cred.file}: ${msg}`);
|
|
274
|
-
const warn = (msg) => warnings.push(`${cred.file}: ${msg}`);
|
|
275
|
-
const { data } = cred;
|
|
276
|
-
if (cred.name.length > 255) err("a credential's name (its file name) must be at most 255 characters");
|
|
277
|
-
if ("display_name" in data) err("`display_name` is not allowed; the file name is the credential's name");
|
|
278
|
-
checkMetadata(data.metadata, 16, err);
|
|
279
|
-
for (const key of Object.keys(data)) {
|
|
280
|
-
if (!["auth", "metadata", "display_name"].includes(key)) warn(`unknown field \`${key}\` will be sent to the API as written`);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
const { auth } = data;
|
|
284
|
-
if (!isObject(auth)) return err("`auth` is required");
|
|
285
|
-
if (!CREDENTIAL_TYPES.includes(auth.type)) return err(`\`auth.type\` must be ${CREDENTIAL_TYPES.map((t) => `\`${t}\``).join(" or ")}; other credential types aren't supported yet`);
|
|
286
|
-
|
|
287
|
-
for (const path of SECRET_FIELDS[auth.type]) {
|
|
288
|
-
if (get(auth, path) !== undefined) {
|
|
289
|
-
err(`\`auth.${path.join(".")}\` doesn't go in the file; \`agentschema cred deploy\` asks for it and sends it straight to the vault`);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
if (auth.type === ENV_VAR) return checkEnvVarAuth(auth, err, warn);
|
|
294
|
-
|
|
295
|
-
const url = (value, field) => {
|
|
296
|
-
if (typeof value !== "string" || value === "") return err(`\`${field}\` is required`);
|
|
297
|
-
if (!/^https?:\/\/\S+$/.test(value)) err(`\`${field}\` must be an http(s) URL`);
|
|
298
|
-
};
|
|
299
|
-
url(auth.mcp_server_url, "auth.mcp_server_url");
|
|
300
|
-
for (const key of Object.keys(auth)) {
|
|
301
|
-
if (!["type", "mcp_server_url", "refresh", "access_token", "expires_at"].includes(key)) {
|
|
302
|
-
warn(`unknown field \`auth.${key}\` will be sent to the API as written`);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
if (auth.refresh === undefined || auth.refresh === null) return;
|
|
307
|
-
const { refresh } = auth;
|
|
308
|
-
if (!isObject(refresh)) return err("`auth.refresh` must be a mapping");
|
|
309
|
-
url(refresh.token_endpoint, "auth.refresh.token_endpoint");
|
|
310
|
-
if (typeof refresh.client_id !== "string" || refresh.client_id === "") err("`auth.refresh.client_id` is required, as a string (quote it if it looks like a number)");
|
|
311
|
-
for (const field of ["scope", "resource"]) {
|
|
312
|
-
if (refresh[field] !== undefined && typeof refresh[field] !== "string") err(`\`auth.refresh.${field}\` must be a string`);
|
|
313
|
-
}
|
|
314
|
-
if (!isObject(refresh.token_endpoint_auth) || !TOKEN_ENDPOINT_AUTH.includes(refresh.token_endpoint_auth.type)) {
|
|
315
|
-
err(`\`auth.refresh.token_endpoint_auth.type\` must be ${TOKEN_ENDPOINT_AUTH.map((t) => `\`${t}\``).join(", ")}`);
|
|
316
|
-
}
|
|
317
|
-
for (const key of Object.keys(refresh)) {
|
|
318
|
-
if (!["token_endpoint", "client_id", "scope", "resource", "token_endpoint_auth", "refresh_token"].includes(key)) {
|
|
319
|
-
warn(`unknown field \`auth.refresh.${key}\` will be sent to the API as written`);
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
function checkEnvVarAuth(auth, err, warn) {
|
|
325
|
-
if (typeof auth.secret_name !== "string" || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(auth.secret_name)) {
|
|
326
|
-
err("`auth.secret_name` is required, and must be a variable name like STRIPE_API_KEY");
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
const { networking: net, injection_location: where } = auth;
|
|
330
|
-
if (!isObject(net) || !["limited", "unrestricted"].includes(net.type)) {
|
|
331
|
-
err("`auth.networking.type` is required, and must be `limited` or `unrestricted`");
|
|
332
|
-
} else if (net.type === "unrestricted") {
|
|
333
|
-
warn("`auth.networking` is `unrestricted`, so the secret is substituted on requests to any host; a prompt-injected agent could send it anywhere");
|
|
334
|
-
} else if (!Array.isArray(net.allowed_hosts) || net.allowed_hosts.length === 0 || net.allowed_hosts.some((h) => typeof h !== "string" || h === "")) {
|
|
335
|
-
err("`auth.networking.allowed_hosts` must list the hosts the secret may be sent to");
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
if (where !== undefined && where !== null) {
|
|
339
|
-
if (!isObject(where)) err("`auth.injection_location` must be a mapping");
|
|
340
|
-
else {
|
|
341
|
-
for (const field of ["header", "body"]) {
|
|
342
|
-
if (where[field] !== undefined && typeof where[field] !== "boolean") err(`\`auth.injection_location.${field}\` must be true or false`);
|
|
343
|
-
}
|
|
344
|
-
if (where.header !== true && where.body !== true) err("`auth.injection_location` must enable `header` or `body`; a field you leave out is false");
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
for (const key of Object.keys(auth)) {
|
|
349
|
-
if (!["type", "secret_name", "secret_value", "networking", "injection_location"].includes(key)) {
|
|
350
|
-
warn(`unknown field \`auth.${key}\` will be sent to the API as written`);
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
function checkMetadata(metadata, maxKeys, err) {
|
|
356
|
-
if (metadata === undefined) return;
|
|
357
|
-
if (!isObject(metadata)) return err("`metadata` must be a mapping");
|
|
358
|
-
const keys = Object.keys(metadata);
|
|
359
|
-
if (keys.length > maxKeys) err(`\`metadata\` has ${keys.length} keys; the limit is ${maxKeys}`);
|
|
360
|
-
for (const key of keys) {
|
|
361
|
-
if (typeof metadata[key] !== "string") err(`\`metadata.${key}\` must be a string (quote numbers and booleans)`);
|
|
362
|
-
}
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { checkFiles } from "../lib/check.js";
|
|
3
|
+
import { folderProblem, readFolder } from "./folder.js";
|
|
4
|
+
|
|
5
|
+
// Validates an agent folder without calling the API. See checkFiles for what it returns.
|
|
6
|
+
export function check(dir, options) {
|
|
7
|
+
const problem = folderProblem(dir);
|
|
8
|
+
if (problem) return { errors: [problem], warnings: [], instance: null };
|
|
9
|
+
|
|
10
|
+
const result = checkFiles(readFolder(dir), options);
|
|
11
|
+
// Where each tool's code lives, so its require() calls can resolve from there.
|
|
12
|
+
for (const tool of result.instance?.tools ?? []) tool.path = resolve(dir, tool.file);
|
|
13
|
+
return result;
|
|
363
14
|
}
|
package/src/cred-menu.js
CHANGED
|
@@ -5,7 +5,7 @@ import { check } from "./check.js";
|
|
|
5
5
|
import { credLogin, credPaste, locate } from "./cred.js";
|
|
6
6
|
import { DeployError } from "./deploy.js";
|
|
7
7
|
import { discover } from "./oauth.js";
|
|
8
|
-
import { ENV_VAR, TOKEN_ENDPOINT_AUTH, VAULTS_DIR, VAULT_FILE } from "
|
|
8
|
+
import { ENV_VAR, TOKEN_ENDPOINT_AUTH, VAULTS_DIR, VAULT_FILE } from "../lib/vaults.js";
|
|
9
9
|
|
|
10
10
|
const SIGN_IN = "Sign in with the browser";
|
|
11
11
|
const PASTE = "Paste the tokens";
|
package/src/cred.js
CHANGED
|
@@ -3,12 +3,12 @@ import Anthropic from "@anthropic-ai/sdk";
|
|
|
3
3
|
import { check } from "./check.js";
|
|
4
4
|
import { DeployError, call, openLock, retrieve } from "./deploy.js";
|
|
5
5
|
import { LOCK_FILE, hashBody, writeLock } from "./lock.js";
|
|
6
|
-
import { updateFrontmatter } from "./
|
|
6
|
+
import { updateFrontmatter } from "./folder.js";
|
|
7
7
|
import { authorizationRequest, discover, exchangeCode, registerClient, startCallbackServer } from "./oauth.js";
|
|
8
8
|
import {
|
|
9
9
|
ENV_VAR, VAULTS_DIR, VAULT_FILE, changedImmutableFields, credentialCreateBody, credentialKey, credentialSettings,
|
|
10
10
|
secretsNeeded, vaultBody, vaultKey,
|
|
11
|
-
} from "
|
|
11
|
+
} from "../lib/vaults.js";
|
|
12
12
|
|
|
13
13
|
// Finds the agent folder and the credential a path like my-agent/vaults/Ops/slack.md names.
|
|
14
14
|
export function locate(path) {
|
|
@@ -51,7 +51,7 @@ export async function credPaste(path, { client, ask, log = () => {} }) {
|
|
|
51
51
|
};
|
|
52
52
|
if (cred.data.auth.type === ENV_VAR) {
|
|
53
53
|
const secretValue = await required(`Value of ${cred.data.auth.secret_name}: `);
|
|
54
|
-
return createCredential({
|
|
54
|
+
return createCredential({ lock, vault, cred, secrets: { secretValue }, client, saveLock: (next) => writeLock(dir, next), log });
|
|
55
55
|
}
|
|
56
56
|
const secrets = { accessToken: await required("Access token: ") };
|
|
57
57
|
const expiresAt = (await ask("Access token expires at (ISO 8601, e.g. 2026-12-31T23:59:59Z; blank if unknown): ", { secret: false })).trim();
|
|
@@ -62,19 +62,20 @@ export async function credPaste(path, { client, ask, log = () => {} }) {
|
|
|
62
62
|
if (needs.refreshToken) secrets.refreshToken = await required("Refresh token: ");
|
|
63
63
|
if (needs.clientSecret) secrets.clientSecret = await required("Client secret: ");
|
|
64
64
|
|
|
65
|
-
return createCredential({
|
|
65
|
+
return createCredential({ lock, vault, cred, secrets, client, saveLock: (next) => writeLock(dir, next), log });
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
// Creates a credential with its secrets, creating its vault first if needed, records it in the
|
|
69
|
-
// lock, and asks the API to try it.
|
|
70
|
-
|
|
69
|
+
// lock, and asks the API to try it. `secrets` are what credentialCreateBody takes. Works on a lock
|
|
70
|
+
// object, so a host can keep it wherever it likes, and `saveLock` may be async.
|
|
71
|
+
export async function createCredential({ lock, vault, cred, secrets, client, saveLock = () => {}, log = () => {} }) {
|
|
71
72
|
let vaultId = lock.resources[vaultKey(vault)]?.id;
|
|
72
73
|
if (!vaultId) {
|
|
73
74
|
const body = vaultBody(vault);
|
|
74
75
|
const created = await call({ file: vault.path, kind: "vault" }, () => client.beta.vaults.create(body));
|
|
75
76
|
vaultId = created.id;
|
|
76
77
|
lock.resources[vaultKey(vault)] = { kind: "vault", id: vaultId, hash: hashBody(body) };
|
|
77
|
-
|
|
78
|
+
await saveLock(lock);
|
|
78
79
|
log(`created vault ${vaultId}`);
|
|
79
80
|
}
|
|
80
81
|
|
|
@@ -82,7 +83,7 @@ async function createCredential({ dir, lock, vault, cred }, secrets, { client, l
|
|
|
82
83
|
client.beta.vaults.credentials.create(vaultId, credentialCreateBody(cred, secrets)),
|
|
83
84
|
);
|
|
84
85
|
lock.resources[credentialKey(cred)] = { kind: "credential", id: created.id, vault_id: vaultId, hash: hashBody(credentialSettings(cred)) };
|
|
85
|
-
|
|
86
|
+
await saveLock(lock);
|
|
86
87
|
log(`created credential ${created.id}`);
|
|
87
88
|
|
|
88
89
|
if (cred.data.auth.type === ENV_VAR) return created;
|
|
@@ -172,7 +173,7 @@ export async function credLogin(path, { client, ask, openBrowser, port = 0, fetc
|
|
|
172
173
|
if (existing && existing.vault_id === lock.resources[vaultKey(vault)]?.id) {
|
|
173
174
|
return replaceTokens({ dir, lock, cred, entry: existing }, secrets, { client, log });
|
|
174
175
|
}
|
|
175
|
-
return createCredential({
|
|
176
|
+
return createCredential({ lock, vault, cred, secrets, client, saveLock: (next) => writeLock(dir, next), log });
|
|
176
177
|
} finally {
|
|
177
178
|
await callback.close();
|
|
178
179
|
}
|
|
@@ -230,6 +231,15 @@ export async function credDelete(path, { client, confirm, log = () => {} }) {
|
|
|
230
231
|
log("Cancelled.");
|
|
231
232
|
return;
|
|
232
233
|
}
|
|
234
|
+
await deleteCredential({ lock, cred, client, saveLock: (next) => writeLock(dir, next) });
|
|
235
|
+
log(`deleted credential ${entry.id}`);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Permanently deletes a credential, if it has one, and removes it from the lock. One that's already
|
|
239
|
+
// gone counts as deleted. Works on a lock object, like createCredential.
|
|
240
|
+
export async function deleteCredential({ lock, cred, client, saveLock = () => {} }) {
|
|
241
|
+
const entry = lock.resources[credentialKey(cred)];
|
|
242
|
+
if (!entry) return;
|
|
233
243
|
try {
|
|
234
244
|
await client.beta.vaults.credentials.delete(entry.id, { vault_id: entry.vault_id });
|
|
235
245
|
} catch (err) {
|
|
@@ -238,6 +248,5 @@ export async function credDelete(path, { client, confirm, log = () => {} }) {
|
|
|
238
248
|
}
|
|
239
249
|
}
|
|
240
250
|
delete lock.resources[credentialKey(cred)];
|
|
241
|
-
|
|
242
|
-
log(`deleted credential ${entry.id}`);
|
|
251
|
+
await saveLock(lock);
|
|
243
252
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { runInThisContext } from "node:vm";
|
|
4
|
+
import { wrapToolCode } from "../lib/tools.js";
|
|
5
|
+
|
|
6
|
+
// Loads a custom tool's code as a CommonJS module whose module.exports is the function.
|
|
7
|
+
// require() resolves relative to the tool's file, so the agent folder's node_modules work.
|
|
8
|
+
// The code runs with this process's full access, so only use this on your own machine.
|
|
9
|
+
export function loadTool(tool) {
|
|
10
|
+
const module = { exports: {} };
|
|
11
|
+
const fn = runInThisContext(wrapToolCode(tool.code.source), { filename: tool.path });
|
|
12
|
+
fn(module.exports, createRequire(tool.path), module, tool.path, resolve(tool.path, ".."));
|
|
13
|
+
if (typeof module.exports !== "function") {
|
|
14
|
+
throw new Error(`${tool.file}: module.exports must be a function`);
|
|
15
|
+
}
|
|
16
|
+
return module.exports;
|
|
17
|
+
}
|