@soft-boy/agentschema 0.2.0 → 0.2.2
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 +172 -41
- package/bin/agentschema.js +131 -15
- package/package.json +3 -2
- package/src/chat.js +50 -0
- package/src/check.js +212 -4
- package/src/cred-menu.js +197 -0
- package/src/cred.js +243 -0
- package/src/deploy.js +123 -14
- package/src/init.js +15 -106
- package/src/markdown.js +10 -0
- package/src/mcp.js +57 -0
- package/src/oauth.js +192 -0
- package/src/prompts.js +159 -0
- package/src/session.js +30 -3
- package/src/tools-menu.js +171 -0
- package/src/tools.js +105 -27
- package/src/vaults.js +148 -0
package/src/tools.js
CHANGED
|
@@ -5,13 +5,16 @@ import { Script, runInThisContext } from "node:vm";
|
|
|
5
5
|
import { FileError, parseMarkdown } from "./markdown.js";
|
|
6
6
|
|
|
7
7
|
export const TOOLS_DIR = "tools";
|
|
8
|
-
export const BUILT_IN_DIR = "built-in";
|
|
9
8
|
export const TOOLSET = "agent_toolset_20260401";
|
|
9
|
+
export const TOOLSET_FILE = `${TOOLSET}.md`;
|
|
10
|
+
export const MCP_TOOLSET = "mcp_toolset";
|
|
10
11
|
export const PREBUILT_TOOLS = ["bash", "edit", "glob", "grep", "read", "web_fetch", "web_search", "write"];
|
|
12
|
+
export const POLICIES = ["always_allow", "always_ask", "auto"];
|
|
11
13
|
const TYPES = ["string", "number", "integer", "boolean", "object", "array"];
|
|
12
14
|
|
|
13
15
|
// Reads every .md file under tools/, in any subfolder, sorted by name. Folders are only for
|
|
14
|
-
// organizing: a tool's name is its file name, and its path isn't compiled into anything.
|
|
16
|
+
// organizing: a custom tool's name is its file name, and its path isn't compiled into anything.
|
|
17
|
+
// The pre-built toolset is one file (by convention tools/agent_toolset_20260401.md) that lists its tools.
|
|
15
18
|
// A missing tools/ folder means no tools.
|
|
16
19
|
// Returns { tools, errors }; a tool file that can't be parsed has an entry in errors.
|
|
17
20
|
export function readTools(dir) {
|
|
@@ -35,9 +38,15 @@ export function readTools(dir) {
|
|
|
35
38
|
}
|
|
36
39
|
}
|
|
37
40
|
|
|
38
|
-
//
|
|
39
|
-
for (const [
|
|
40
|
-
|
|
41
|
+
// Custom tool names must be unique across folders, since the folders are dropped.
|
|
42
|
+
for (const [type, what] of [["custom", "tool"], [MCP_TOOLSET, "MCP server"]]) {
|
|
43
|
+
for (const [name, same] of groupBy(tools.filter((t) => t.data.type === type), (t) => t.name)) {
|
|
44
|
+
if (same.length > 1) errors.push(`${TOOLS_DIR}: more than one ${what} is named \`${name}\` (${same.map((t) => t.file).join(", ")})`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const toolsets = tools.filter((t) => t.data.type === TOOLSET);
|
|
48
|
+
if (toolsets.length > 1) {
|
|
49
|
+
errors.push(`${TOOLS_DIR}: more than one \`${TOOLSET}\` file (${toolsets.map((t) => t.file).join(", ")}); list every pre-built tool in one file`);
|
|
41
50
|
}
|
|
42
51
|
return { tools: tools.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)), errors };
|
|
43
52
|
}
|
|
@@ -60,7 +69,8 @@ function groupBy(items, key) {
|
|
|
60
69
|
return groups;
|
|
61
70
|
}
|
|
62
71
|
|
|
63
|
-
// Pulls the `## Input Schema` table
|
|
72
|
+
// Pulls the `## Input Schema` table, the `## Code` block and the `## Tools` table (for the
|
|
73
|
+
// pre-built toolset) out of a tool file's body.
|
|
64
74
|
function parseSections(body, file) {
|
|
65
75
|
const sections = {};
|
|
66
76
|
let current = null;
|
|
@@ -77,22 +87,29 @@ function parseSections(body, file) {
|
|
|
77
87
|
}
|
|
78
88
|
}
|
|
79
89
|
|
|
80
|
-
const result = { params: null, code: null };
|
|
81
|
-
if (sections["input schema"])
|
|
90
|
+
const result = { params: null, code: null, rows: null };
|
|
91
|
+
if (sections["input schema"]) {
|
|
92
|
+
result.params = parseTable(sections["input schema"], `${file}: the Input Schema table's columns must be Name | Type | Description | Required`,
|
|
93
|
+
["name", "type", "description", "required"]);
|
|
94
|
+
}
|
|
82
95
|
if (sections.code) result.code = parseCode(sections.code, file);
|
|
96
|
+
if (sections.tools) {
|
|
97
|
+
result.rows = parseTable(sections.tools, `${file}: the Tools table's columns must be Name | Enabled, or Name | Enabled | Permission`, ["name", "enabled"], ["permission"]);
|
|
98
|
+
}
|
|
83
99
|
return result;
|
|
84
100
|
}
|
|
85
101
|
|
|
86
|
-
//
|
|
87
|
-
|
|
102
|
+
// The rows of a Markdown table as objects keyed by column name. The header must start with
|
|
103
|
+
// the `required` columns, and may go on with the `optional` ones, in order.
|
|
104
|
+
function parseTable(lines, message, required, optional = []) {
|
|
88
105
|
const rows = lines.filter((l) => l.trim().startsWith("|")).map(splitRow);
|
|
89
106
|
if (rows.length === 0) return [];
|
|
90
107
|
const header = rows[0].map((c) => c.toLowerCase());
|
|
91
|
-
const
|
|
92
|
-
if (
|
|
93
|
-
throw new FileError(
|
|
108
|
+
const allowed = [...required, ...optional];
|
|
109
|
+
if (header.length < required.length || header.length > allowed.length || header.some((c, i) => c !== allowed[i])) {
|
|
110
|
+
throw new FileError(message);
|
|
94
111
|
}
|
|
95
|
-
return rows.slice(2).map((
|
|
112
|
+
return rows.slice(2).map((cells) => Object.fromEntries(header.map((c, i) => [c, cells[i] ?? ""])));
|
|
96
113
|
}
|
|
97
114
|
|
|
98
115
|
function splitRow(line) {
|
|
@@ -121,15 +138,65 @@ export function checkTools(tools, errors) {
|
|
|
121
138
|
};
|
|
122
139
|
|
|
123
140
|
if (type === TOOLSET) {
|
|
124
|
-
|
|
125
|
-
err(`not a pre-built tool; name the file after one of: ${PREBUILT_TOOLS.join(", ")}`);
|
|
126
|
-
}
|
|
141
|
+
checkToolset(tool, err);
|
|
127
142
|
extra(["type"]);
|
|
143
|
+
} else if (type === MCP_TOOLSET) {
|
|
144
|
+
checkMcpToolset(tool, err);
|
|
145
|
+
extra(["type", "mcp_server_name", "default_config", "url"]);
|
|
128
146
|
} else if (type === "custom") {
|
|
129
147
|
checkCustom(tool, err);
|
|
130
148
|
extra(["type", "name", "description"]);
|
|
131
149
|
} else {
|
|
132
|
-
err(`\`type\` must be \`${TOOLSET}\` (
|
|
150
|
+
err(`\`type\` must be \`${TOOLSET}\` (the pre-built tools), \`${MCP_TOOLSET}\` (an MCP server) or \`custom\``);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// The tools of an MCP server that agent.md declares: `mcp_server_name` is the server's name, and the
|
|
156
|
+
// optional `## Tools` table configures tools by the names the server reports.
|
|
157
|
+
function checkMcpToolset(tool, err) {
|
|
158
|
+
const { mcp_server_name: server, default_config: defaults } = tool.data;
|
|
159
|
+
if ("url" in tool.data) err("`url` belongs in agent.md's `mcp_servers`; refer to the server here with `mcp_server_name`");
|
|
160
|
+
if (typeof server !== "string" || server === "") err("`mcp_server_name` is required");
|
|
161
|
+
else if (server !== tool.name) err(`\`mcp_server_name\` must match the file name (${tool.name})`);
|
|
162
|
+
if (tool.name.length > 255) err("an MCP server's name (its file name) must be at most 255 characters");
|
|
163
|
+
|
|
164
|
+
if (defaults !== undefined && defaults !== null) {
|
|
165
|
+
if (typeof defaults !== "object" || Array.isArray(defaults)) err("`default_config` must be a mapping");
|
|
166
|
+
else {
|
|
167
|
+
if (defaults.enabled !== undefined && typeof defaults.enabled !== "boolean") err("`default_config.enabled` must be true or false");
|
|
168
|
+
const policy = defaults.permission_policy;
|
|
169
|
+
if (policy !== undefined && !POLICIES.includes(policy?.type)) {
|
|
170
|
+
err(`\`default_config.permission_policy.type\` must be ${POLICIES.join(", ")}`);
|
|
171
|
+
}
|
|
172
|
+
for (const key of Object.keys(defaults)) {
|
|
173
|
+
if (!["enabled", "permission_policy"].includes(key)) err(`unknown field \`default_config.${key}\``);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
checkRows(tool.rows ?? [], err, { known: null });
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function checkToolset(tool, err) {
|
|
181
|
+
if (!tool.rows) {
|
|
182
|
+
// 0.2.0 had one file per pre-built tool, with no table.
|
|
183
|
+
const hint = PREBUILT_TOOLS.includes(tool.name) ? `; since 0.2.1 all pre-built tools go in one file, ${TOOLS_DIR}/${TOOLSET_FILE}` : "";
|
|
184
|
+
return err(`a \`## Tools\` table listing the pre-built tools to enable is required${hint}`);
|
|
185
|
+
}
|
|
186
|
+
checkRows(tool.rows, err, { known: PREBUILT_TOOLS });
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// The rows of a `## Tools` table. `known` is the list of valid tool names, when there is one.
|
|
190
|
+
function checkRows(rows, err, { known }) {
|
|
191
|
+
const seen = new Set();
|
|
192
|
+
for (const row of rows) {
|
|
193
|
+
if (!row.name) err("a `## Tools` row has no name");
|
|
194
|
+
else if (known && !known.includes(row.name)) err(`\`${row.name}\` is not a pre-built tool; use ${known.join(", ")}`);
|
|
195
|
+
else if (seen.has(row.name)) err(`\`${row.name}\` is listed twice`);
|
|
196
|
+
seen.add(row.name);
|
|
197
|
+
if (!["true", "false"].includes(row.enabled.toLowerCase())) err(`\`${row.name}\`: Enabled must be true or false`);
|
|
198
|
+
if (row.permission && !POLICIES.includes(row.permission)) {
|
|
199
|
+
err(`\`${row.name}\` has permission \`${row.permission}\`; use ${POLICIES.join(", ")}, or leave it blank`);
|
|
133
200
|
}
|
|
134
201
|
}
|
|
135
202
|
}
|
|
@@ -164,17 +231,18 @@ function checkCustom(tool, err) {
|
|
|
164
231
|
}
|
|
165
232
|
}
|
|
166
233
|
|
|
167
|
-
// The tools list sent to the API
|
|
168
|
-
//
|
|
234
|
+
// The tools list sent to the API, each kind sorted by name: the pre-built toolset with a config
|
|
235
|
+
// for each row of its table (tools it doesn't list keep the API's defaults), then a toolset for
|
|
236
|
+
// each MCP server, then the custom tools.
|
|
169
237
|
export function compileTools(tools) {
|
|
170
238
|
const compiled = [];
|
|
171
|
-
const
|
|
172
|
-
if (
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
239
|
+
const toolset = tools.find((t) => t.data.type === TOOLSET);
|
|
240
|
+
if (toolset) compiled.push({ type: TOOLSET, configs: configs(toolset.rows) });
|
|
241
|
+
for (const tool of byName(tools.filter((t) => t.data.type === MCP_TOOLSET))) {
|
|
242
|
+
const entry = { type: MCP_TOOLSET, mcp_server_name: tool.name };
|
|
243
|
+
if (tool.data.default_config) entry.default_config = tool.data.default_config;
|
|
244
|
+
if (tool.rows?.length) entry.configs = configs(tool.rows);
|
|
245
|
+
compiled.push(entry);
|
|
178
246
|
}
|
|
179
247
|
for (const tool of tools.filter((t) => t.data.type === "custom").sort((a, b) => (a.name < b.name ? -1 : 1))) {
|
|
180
248
|
compiled.push({ type: "custom", name: tool.name, description: tool.data.description, input_schema: inputSchema(tool.params ?? []) });
|
|
@@ -182,6 +250,16 @@ export function compileTools(tools) {
|
|
|
182
250
|
return compiled;
|
|
183
251
|
}
|
|
184
252
|
|
|
253
|
+
const byName = (tools) => [...tools].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
254
|
+
|
|
255
|
+
function configs(rows = []) {
|
|
256
|
+
return [...rows].sort((a, b) => (a.name < b.name ? -1 : 1)).map(({ name, enabled, permission }) => {
|
|
257
|
+
const config = { name, enabled: enabled.toLowerCase() === "true" };
|
|
258
|
+
if (permission) config.permission_policy = { type: permission };
|
|
259
|
+
return config;
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
185
263
|
function inputSchema(params) {
|
|
186
264
|
const properties = {};
|
|
187
265
|
for (const p of params) {
|
package/src/vaults.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
|
+
import { FileError, parseMarkdown } from "./markdown.js";
|
|
4
|
+
|
|
5
|
+
export const VAULTS_DIR = "vaults";
|
|
6
|
+
export const VAULT_FILE = "vault.md";
|
|
7
|
+
export const MCP_OAUTH = "mcp_oauth";
|
|
8
|
+
export const ENV_VAR = "environment_variable";
|
|
9
|
+
export const CREDENTIAL_TYPES = [MCP_OAUTH, ENV_VAR];
|
|
10
|
+
export const TOKEN_ENDPOINT_AUTH = ["none", "client_secret_basic", "client_secret_post"];
|
|
11
|
+
|
|
12
|
+
// Fields that hold secrets or short-lived values. They never go in a file: `cred deploy`
|
|
13
|
+
// asks for them and sends them straight to the vault.
|
|
14
|
+
export const SECRET_FIELDS = {
|
|
15
|
+
[MCP_OAUTH]: [
|
|
16
|
+
["access_token"],
|
|
17
|
+
["expires_at"],
|
|
18
|
+
["refresh", "refresh_token"],
|
|
19
|
+
["refresh", "token_endpoint_auth", "client_secret"],
|
|
20
|
+
],
|
|
21
|
+
[ENV_VAR]: [["secret_value"]],
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Fields the API won't change on an existing credential. Changing one in a file is an error;
|
|
25
|
+
// delete the credential and create it again.
|
|
26
|
+
export const IMMUTABLE_FIELDS = {
|
|
27
|
+
[MCP_OAUTH]: [
|
|
28
|
+
["type"],
|
|
29
|
+
["mcp_server_url"],
|
|
30
|
+
["refresh"], // adding or removing the refresh block
|
|
31
|
+
["refresh", "token_endpoint"],
|
|
32
|
+
["refresh", "client_id"],
|
|
33
|
+
["refresh", "resource"],
|
|
34
|
+
["refresh", "token_endpoint_auth", "type"],
|
|
35
|
+
],
|
|
36
|
+
[ENV_VAR]: [["type"], ["secret_name"]],
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Reads vaults/: each folder is a vault named after the folder, holding an optional vault.md
|
|
40
|
+
// and one .md file per credential, named after the file. Vaults are sorted by name, which is
|
|
41
|
+
// the order `run` attaches them in. A missing vaults/ folder means no vaults.
|
|
42
|
+
// Returns { vaults, errors }; a file that can't be parsed has an entry in errors.
|
|
43
|
+
export function readVaults(dir) {
|
|
44
|
+
const folder = join(dir, VAULTS_DIR);
|
|
45
|
+
try {
|
|
46
|
+
if (!statSync(folder).isDirectory()) return { vaults: [], errors: [`${VAULTS_DIR}: not a directory`] };
|
|
47
|
+
} catch {
|
|
48
|
+
return { vaults: [], errors: [] };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const vaults = [];
|
|
52
|
+
const errors = [];
|
|
53
|
+
const parse = (file) => {
|
|
54
|
+
try {
|
|
55
|
+
return parseMarkdown(readFileSync(join(dir, file), "utf8"), file);
|
|
56
|
+
} catch (err) {
|
|
57
|
+
if (!(err instanceof FileError)) throw err;
|
|
58
|
+
errors.push(err.message);
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
for (const entry of sorted(readdirSync(folder, { withFileTypes: true }))) {
|
|
64
|
+
if (entry.name.startsWith(".")) continue;
|
|
65
|
+
const path = `${VAULTS_DIR}/${entry.name}`;
|
|
66
|
+
if (!entry.isDirectory()) {
|
|
67
|
+
if (entry.name.endsWith(".md")) errors.push(`${path}: each vault is a folder; put credential files inside one`);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const vault = { name: entry.name, path, file: null, credentials: [] };
|
|
72
|
+
for (const item of sorted(readdirSync(join(folder, entry.name), { withFileTypes: true }))) {
|
|
73
|
+
if (item.name.startsWith(".")) continue;
|
|
74
|
+
const file = `${path}/${item.name}`;
|
|
75
|
+
if (item.isDirectory()) {
|
|
76
|
+
errors.push(`${file}: a vault holds credential files, not folders`);
|
|
77
|
+
} else if (item.name === VAULT_FILE) {
|
|
78
|
+
vault.file = parse(file);
|
|
79
|
+
} else if (item.name.endsWith(".md")) {
|
|
80
|
+
const parsed = parse(file);
|
|
81
|
+
if (parsed) vault.credentials.push({ file, name: basename(item.name, ".md"), vault: entry.name, ...parsed });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
vaults.push(vault);
|
|
85
|
+
}
|
|
86
|
+
return { vaults, errors };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const sorted = (entries) => entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
90
|
+
|
|
91
|
+
// The lock file keys for a vault and a credential.
|
|
92
|
+
export const vaultKey = (vault) => `./${vault.path}`;
|
|
93
|
+
export const credentialKey = (cred) => `./${cred.file}`;
|
|
94
|
+
|
|
95
|
+
// The body sent to POST /v1/vaults: the folder name and vault.md's metadata.
|
|
96
|
+
export function vaultBody(vault) {
|
|
97
|
+
const body = { display_name: vault.name };
|
|
98
|
+
if (vault.file?.data.metadata !== undefined) body.metadata = vault.file.data.metadata;
|
|
99
|
+
return body;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// A credential's settings from its file: everything but the secrets. This is what's hashed
|
|
103
|
+
// into the lock, and what deploy keeps in sync.
|
|
104
|
+
export function credentialSettings(cred) {
|
|
105
|
+
const body = { display_name: cred.name, auth: structuredClone(cred.data.auth) };
|
|
106
|
+
if (cred.data.metadata !== undefined) body.metadata = cred.data.metadata;
|
|
107
|
+
return body;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// The body sent to create a credential: its settings plus the secrets `cred deploy` asked for.
|
|
111
|
+
export function credentialCreateBody(cred, { accessToken, expiresAt, refreshToken, clientSecret, secretValue }) {
|
|
112
|
+
const body = credentialSettings(cred);
|
|
113
|
+
if (body.auth.type === ENV_VAR) {
|
|
114
|
+
body.auth.secret_value = secretValue;
|
|
115
|
+
return body;
|
|
116
|
+
}
|
|
117
|
+
body.auth.access_token = accessToken;
|
|
118
|
+
if (expiresAt) body.auth.expires_at = expiresAt;
|
|
119
|
+
if (body.auth.refresh) {
|
|
120
|
+
body.auth.refresh.refresh_token = refreshToken;
|
|
121
|
+
if (body.auth.refresh.token_endpoint_auth?.type !== "none") body.auth.refresh.token_endpoint_auth.client_secret = clientSecret;
|
|
122
|
+
}
|
|
123
|
+
return body;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Which secrets a credential file needs when it's created.
|
|
127
|
+
export function secretsNeeded(cred) {
|
|
128
|
+
const refresh = cred.data.auth?.refresh;
|
|
129
|
+
return {
|
|
130
|
+
refreshToken: Boolean(refresh),
|
|
131
|
+
clientSecret: Boolean(refresh) && refresh.token_endpoint_auth?.type !== "none",
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// The immutable fields whose value in the file differs from the deployed credential's.
|
|
136
|
+
export function changedImmutableFields(cred, remoteAuth) {
|
|
137
|
+
const local = cred.data.auth ?? {};
|
|
138
|
+
const changed = [];
|
|
139
|
+
for (const path of IMMUTABLE_FIELDS[local.type] ?? []) {
|
|
140
|
+
const a = get(local, path);
|
|
141
|
+
const b = get(remoteAuth ?? {}, path);
|
|
142
|
+
const differs = path.length === 1 && path[0] === "refresh" ? Boolean(a) !== Boolean(b) : (a ?? null) !== (b ?? null);
|
|
143
|
+
if (differs) changed.push(path.join("."));
|
|
144
|
+
}
|
|
145
|
+
return changed;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export const get = (obj, path) => path.reduce((o, k) => (o && typeof o === "object" ? o[k] : undefined), obj);
|