@soft-boy/agentschema 0.3.1 → 0.3.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 +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 +2 -2
- 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 +7 -0
- package/src/session.js +34 -10
- package/src/tools-menu.js +4 -4
- package/src/skills.js +0 -79
package/lib/skills.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { bytesOf, isIgnoredPath, pathsUnder, textOf } from "./files.js";
|
|
2
|
+
import { FileError, parseMarkdown } from "./markdown.js";
|
|
3
|
+
|
|
4
|
+
export const SKILLS_DIR = "skills";
|
|
5
|
+
export const SKILL_FILE = "SKILL.md";
|
|
6
|
+
export const MAX_UPLOAD_BYTES = 30 * 1024 * 1024;
|
|
7
|
+
|
|
8
|
+
// Reads skills/: each folder is a skill, with a SKILL.md at its top and any other files it needs,
|
|
9
|
+
// at any depth. Hidden files and node_modules are left out. Skills are sorted by name. No skills/
|
|
10
|
+
// folder means no skills.
|
|
11
|
+
// Returns { skills, errors }; a skill that can't be read has an entry in errors.
|
|
12
|
+
export function readSkills(files) {
|
|
13
|
+
if (SKILLS_DIR in files) return { skills: [], errors: [`${SKILLS_DIR}: not a directory`] };
|
|
14
|
+
|
|
15
|
+
const folders = new Map();
|
|
16
|
+
const errors = [];
|
|
17
|
+
for (const file of pathsUnder(files, SKILLS_DIR)) {
|
|
18
|
+
const [, name, ...rest] = file.split("/");
|
|
19
|
+
if (rest.length === 0) {
|
|
20
|
+
if (name.endsWith(".md")) errors.push(`${file}: each skill is a folder, like ${SKILLS_DIR}/my-skill/${SKILL_FILE}`);
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (!folders.has(name)) folders.set(name, []);
|
|
24
|
+
folders.get(name).push({ rel: rest.join("/"), bytes: bytesOf(files[file]) });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const skills = [];
|
|
28
|
+
for (const [name, skillFiles] of folders) {
|
|
29
|
+
const path = `${SKILLS_DIR}/${name}`;
|
|
30
|
+
// Exact names only, so a skill behaves the same on a case-sensitive file system.
|
|
31
|
+
const main = skillFiles.find((f) => f.rel === SKILL_FILE);
|
|
32
|
+
if (!main) {
|
|
33
|
+
const wrongCase = skillFiles.find((f) => f.rel.toLowerCase() === SKILL_FILE.toLowerCase());
|
|
34
|
+
errors.push(`${path}: missing ${SKILL_FILE}${wrongCase ? ` (found ${wrongCase.rel}; the name must be uppercase)` : ""}`);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const parsed = parseMarkdown(textOf(main.bytes), `${path}/${SKILL_FILE}`);
|
|
39
|
+
skills.push({ name, path, file: `${path}/${SKILL_FILE}`, files: skillFiles, ...parsed });
|
|
40
|
+
} catch (err) {
|
|
41
|
+
if (!(err instanceof FileError)) throw err;
|
|
42
|
+
errors.push(err.message);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { skills, errors };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// The lock file key for a skill.
|
|
49
|
+
export const skillKey = (skill) => `./${skill.path}`;
|
|
50
|
+
|
|
51
|
+
// The agent's references to the skills deployed from this folder, pinned to their versions, so
|
|
52
|
+
// that a session runs exactly what deploy last sent.
|
|
53
|
+
export function skillRefs(skills = [], lock) {
|
|
54
|
+
return skills
|
|
55
|
+
.map((skill) => lock?.resources?.[skillKey(skill)])
|
|
56
|
+
.filter(Boolean)
|
|
57
|
+
.map((entry) => ({ type: "custom", skill_id: entry.id, version: entry.version }));
|
|
58
|
+
}
|
package/lib/stream.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// The text of a complete agent.message event.
|
|
2
|
+
export const agentMessageText = (event) => event.content.filter((b) => b.type === "text").map((b) => b.text).join("");
|
|
3
|
+
|
|
4
|
+
// Passes the agent's text to `write` as it streams in, chunk by chunk. Deltas are best effort (the
|
|
5
|
+
// server may stop sending them under load), so the complete agent.message that follows is the
|
|
6
|
+
// record: whatever part of it the deltas didn't cover is written then.
|
|
7
|
+
export function textStream(write) {
|
|
8
|
+
let preview = null; // { id, text } for the agent.message being previewed
|
|
9
|
+
const close = () => {
|
|
10
|
+
if (preview?.text) write("\n");
|
|
11
|
+
preview = null;
|
|
12
|
+
};
|
|
13
|
+
return {
|
|
14
|
+
// Returns true when the event was written here, so it shouldn't be shown again.
|
|
15
|
+
handle(event) {
|
|
16
|
+
switch (event.type) {
|
|
17
|
+
case "event_start":
|
|
18
|
+
close();
|
|
19
|
+
if (event.event?.type === "agent.message") preview = { id: event.event.id, text: "" };
|
|
20
|
+
return true;
|
|
21
|
+
case "event_delta": {
|
|
22
|
+
const content = event.delta?.content;
|
|
23
|
+
if (preview?.id !== event.event_id || content?.type !== "text" || !content.text) return true;
|
|
24
|
+
preview.text += content.text;
|
|
25
|
+
write(content.text);
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
case "agent.message": {
|
|
29
|
+
if (preview?.id !== event.id) {
|
|
30
|
+
close();
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
const full = agentMessageText(event);
|
|
34
|
+
const shown = preview.text;
|
|
35
|
+
preview = null;
|
|
36
|
+
if (full.startsWith(shown)) {
|
|
37
|
+
if (full.length > 0) write(`${full.slice(shown.length)}\n`);
|
|
38
|
+
} else {
|
|
39
|
+
write(`\n${full}\n`);
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
default:
|
|
44
|
+
// A model request that errors or is interrupted ends without its agent.message.
|
|
45
|
+
close();
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
package/{src → lib}/tools.js
RENAMED
|
@@ -1,45 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createRequire } from "node:module";
|
|
3
|
-
import { basename, join, resolve } from "node:path";
|
|
4
|
-
import { Script, runInThisContext } from "node:vm";
|
|
1
|
+
import { pathsUnder, textOf } from "./files.js";
|
|
5
2
|
import { FileError, parseMarkdown } from "./markdown.js";
|
|
6
3
|
|
|
7
4
|
export const TOOLS_DIR = "tools";
|
|
8
5
|
export const TOOLSET = "agent_toolset_20260401";
|
|
9
6
|
export const TOOLSET_FILE = `${TOOLSET}.md`;
|
|
10
7
|
export const MCP_TOOLSET = "mcp_toolset";
|
|
8
|
+
export const AGENTBOOK = "agentbook";
|
|
11
9
|
export const PREBUILT_TOOLS = ["bash", "edit", "glob", "grep", "read", "web_fetch", "web_search", "write"];
|
|
12
10
|
export const POLICIES = ["always_allow", "always_ask", "auto"];
|
|
11
|
+
// agentbook tools reach the API as custom tools, which have no permission policy: agentbook
|
|
12
|
+
// enforces these itself, and has nothing like the API's per-call `auto` judgement.
|
|
13
|
+
const AGENTBOOK_POLICIES = ["always_allow", "always_ask"];
|
|
13
14
|
const TYPES = ["string", "number", "integer", "boolean", "object", "array"];
|
|
14
15
|
|
|
15
16
|
// Reads every .md file under tools/, in any subfolder, sorted by name. Folders are only for
|
|
16
17
|
// organizing: a custom tool's name is its file name, and its path isn't compiled into anything.
|
|
17
18
|
// The pre-built toolset is one file (by convention tools/agent_toolset_20260401.md) that lists its tools.
|
|
18
|
-
//
|
|
19
|
+
// No tools/ folder means no tools.
|
|
19
20
|
// Returns { tools, errors }; a tool file that can't be parsed has an entry in errors.
|
|
20
|
-
export function readTools(
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
if (!statSync(folder).isDirectory()) return { tools: [], errors: [`${TOOLS_DIR}: not a directory`] };
|
|
24
|
-
} catch {
|
|
25
|
-
return { tools: [], errors: [] };
|
|
26
|
-
}
|
|
21
|
+
export function readTools(files) {
|
|
22
|
+
if (TOOLS_DIR in files) return { tools: [], errors: [`${TOOLS_DIR}: not a directory`] };
|
|
27
23
|
|
|
28
24
|
const tools = [];
|
|
29
25
|
const errors = [];
|
|
30
|
-
for (const file of
|
|
26
|
+
for (const file of pathsUnder(files, TOOLS_DIR).filter((path) => path.endsWith(".md"))) {
|
|
31
27
|
try {
|
|
32
|
-
const parsed = parseMarkdown(
|
|
33
|
-
const name =
|
|
34
|
-
tools.push({ file,
|
|
28
|
+
const parsed = parseMarkdown(textOf(files[file]), file);
|
|
29
|
+
const name = file.slice(file.lastIndexOf("/") + 1, -".md".length);
|
|
30
|
+
tools.push({ file, name, ...parsed, ...parseSections(parsed.body, file) });
|
|
35
31
|
} catch (err) {
|
|
36
32
|
if (!(err instanceof FileError)) throw err;
|
|
37
33
|
errors.push(err.message);
|
|
38
34
|
}
|
|
39
35
|
}
|
|
40
36
|
|
|
41
|
-
//
|
|
42
|
-
for (const [type, what] of [["custom", "tool"], [MCP_TOOLSET, "MCP server"]]) {
|
|
37
|
+
// Names must be unique across folders, since the folders are dropped.
|
|
38
|
+
for (const [type, what] of [["custom", "tool"], [MCP_TOOLSET, "MCP server"], [AGENTBOOK, "agentbook toolset"]]) {
|
|
43
39
|
for (const [name, same] of groupBy(tools.filter((t) => t.data.type === type), (t) => t.name)) {
|
|
44
40
|
if (same.length > 1) errors.push(`${TOOLS_DIR}: more than one ${what} is named \`${name}\` (${same.map((t) => t.file).join(", ")})`);
|
|
45
41
|
}
|
|
@@ -51,26 +47,14 @@ export function readTools(dir) {
|
|
|
51
47
|
return { tools: tools.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)), errors };
|
|
52
48
|
}
|
|
53
49
|
|
|
54
|
-
// Paths, relative to the instance folder, of the .md files under `folder`; hidden entries are skipped.
|
|
55
|
-
function markdownFiles(folder, relative) {
|
|
56
|
-
const files = [];
|
|
57
|
-
for (const entry of readdirSync(folder, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : 1))) {
|
|
58
|
-
if (entry.name.startsWith(".")) continue;
|
|
59
|
-
const rel = `${relative}/${entry.name}`;
|
|
60
|
-
if (entry.isDirectory()) files.push(...markdownFiles(join(folder, entry.name), rel));
|
|
61
|
-
else if (entry.name.endsWith(".md")) files.push(rel);
|
|
62
|
-
}
|
|
63
|
-
return files;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
50
|
function groupBy(items, key) {
|
|
67
51
|
const groups = new Map();
|
|
68
52
|
for (const item of items) groups.set(key(item), [...(groups.get(key(item)) ?? []), item]);
|
|
69
53
|
return groups;
|
|
70
54
|
}
|
|
71
55
|
|
|
72
|
-
// Pulls the `## Input Schema` table, the `## Code` block and the `## Tools` table (for
|
|
73
|
-
//
|
|
56
|
+
// Pulls the `## Input Schema` table, the `## Code` block and the `## Tools` table (for a toolset)
|
|
57
|
+
// out of a tool file's body.
|
|
74
58
|
function parseSections(body, file) {
|
|
75
59
|
const sections = {};
|
|
76
60
|
let current = null;
|
|
@@ -126,10 +110,21 @@ function parseCode(lines, file) {
|
|
|
126
110
|
}
|
|
127
111
|
|
|
128
112
|
// Errors and warnings for the tool files, in check's format.
|
|
129
|
-
|
|
113
|
+
// `options.customTools: false` refuses custom tools, for hosts that won't run user code.
|
|
114
|
+
// `options.toolsets` is the catalog behind `type: agentbook` files; see agentbookTools.
|
|
115
|
+
export function checkTools(tools, errors, warnings = [], options = {}) {
|
|
130
116
|
const customs = tools.filter((t) => t.data.type === "custom");
|
|
131
117
|
if (customs.length + (customs.length < tools.length ? 1 : 0) > 256) errors.push(`${TOOLS_DIR}: more than 256 tools`);
|
|
132
118
|
|
|
119
|
+
// agentbook tools and custom tools all reach the API as custom tools, in one namespace.
|
|
120
|
+
if (options.toolsets) {
|
|
121
|
+
const names = new Set(customs.map((t) => t.name));
|
|
122
|
+
for (const { file, name } of agentbookTools(tools, options.toolsets)) {
|
|
123
|
+
if (names.has(name)) errors.push(`${file}: another tool is already named \`${name}\``);
|
|
124
|
+
names.add(name);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
133
128
|
for (const tool of tools) {
|
|
134
129
|
const err = (msg) => errors.push(`${tool.file}: ${msg}`);
|
|
135
130
|
const { type } = tool.data;
|
|
@@ -144,10 +139,14 @@ export function checkTools(tools, errors) {
|
|
|
144
139
|
checkMcpToolset(tool, err);
|
|
145
140
|
extra(["type", "mcp_server_name", "default_config", "url"]);
|
|
146
141
|
} else if (type === "custom") {
|
|
147
|
-
|
|
142
|
+
if (options.customTools === false) err("custom tools aren't available here, because they'd run your code on a shared server");
|
|
143
|
+
else checkCustom(tool, err);
|
|
148
144
|
extra(["type", "name", "description"]);
|
|
145
|
+
} else if (type === AGENTBOOK) {
|
|
146
|
+
checkAgentbook(tool, err, (msg) => warnings.push(`${tool.file}: ${msg}`), options);
|
|
147
|
+
extra(["type", "toolset"]);
|
|
149
148
|
} else {
|
|
150
|
-
err(`\`type\` must be \`${TOOLSET}\` (the pre-built tools), \`${MCP_TOOLSET}\` (an MCP server) or
|
|
149
|
+
err(`\`type\` must be \`${TOOLSET}\` (the pre-built tools), \`${MCP_TOOLSET}\` (an MCP server), \`custom\` or \`${AGENTBOOK}\``);
|
|
151
150
|
}
|
|
152
151
|
}
|
|
153
152
|
}
|
|
@@ -177,26 +176,40 @@ function checkMcpToolset(tool, err) {
|
|
|
177
176
|
checkRows(tool.rows ?? [], err, { known: null });
|
|
178
177
|
}
|
|
179
178
|
|
|
179
|
+
// A toolset from agentbook's catalog. `## Tools` turns its tools on one by one, so a tool without
|
|
180
|
+
// a row is off: a new tool in the catalog never reaches a deployed agent by itself.
|
|
181
|
+
function checkAgentbook(tool, err, warn, { toolsets }) {
|
|
182
|
+
const { toolset } = tool.data;
|
|
183
|
+
if (typeof toolset !== "string" || toolset === "") return err("`toolset` is required");
|
|
184
|
+
if (toolset !== tool.name) err(`\`toolset\` must match the file name (${tool.name})`);
|
|
185
|
+
if (!tool.rows) err("a `## Tools` table listing the tools to enable is required");
|
|
186
|
+
|
|
187
|
+
if (!toolsets) warn("this toolset runs on agentbook, so `agentschema deploy` can't deploy it");
|
|
188
|
+
else if (!Object.hasOwn(toolsets, toolset)) err(`\`${toolset}\` isn't an agentbook toolset; use ${Object.keys(toolsets).join(", ")}`);
|
|
189
|
+
const known = toolsets && Object.hasOwn(toolsets, toolset) ? toolsets[toolset].tools.map((t) => t.name) : null;
|
|
190
|
+
checkRows(tool.rows ?? [], err, { known, what: `a tool in \`${toolset}\``, policies: AGENTBOOK_POLICIES });
|
|
191
|
+
}
|
|
192
|
+
|
|
180
193
|
function checkToolset(tool, err) {
|
|
181
194
|
if (!tool.rows) {
|
|
182
195
|
// 0.2.0 had one file per pre-built tool, with no table.
|
|
183
196
|
const hint = PREBUILT_TOOLS.includes(tool.name) ? `; since 0.2.1 all pre-built tools go in one file, ${TOOLS_DIR}/${TOOLSET_FILE}` : "";
|
|
184
197
|
return err(`a \`## Tools\` table listing the pre-built tools to enable is required${hint}`);
|
|
185
198
|
}
|
|
186
|
-
checkRows(tool.rows, err, { known: PREBUILT_TOOLS });
|
|
199
|
+
checkRows(tool.rows, err, { known: PREBUILT_TOOLS, what: "a pre-built tool" });
|
|
187
200
|
}
|
|
188
201
|
|
|
189
202
|
// The rows of a `## Tools` table. `known` is the list of valid tool names, when there is one.
|
|
190
|
-
function checkRows(rows, err, { known }) {
|
|
203
|
+
function checkRows(rows, err, { known, what, policies = POLICIES }) {
|
|
191
204
|
const seen = new Set();
|
|
192
205
|
for (const row of rows) {
|
|
193
206
|
if (!row.name) err("a `## Tools` row has no name");
|
|
194
|
-
else if (known && !known.includes(row.name)) err(`\`${row.name}\` is not
|
|
207
|
+
else if (known && !known.includes(row.name)) err(`\`${row.name}\` is not ${what}; use ${known.join(", ")}`);
|
|
195
208
|
else if (seen.has(row.name)) err(`\`${row.name}\` is listed twice`);
|
|
196
209
|
seen.add(row.name);
|
|
197
210
|
if (!["true", "false"].includes(row.enabled.toLowerCase())) err(`\`${row.name}\`: Enabled must be true or false`);
|
|
198
|
-
if (row.permission && !
|
|
199
|
-
err(`\`${row.name}\` has permission \`${row.permission}\`; use ${
|
|
211
|
+
if (row.permission && !policies.includes(row.permission)) {
|
|
212
|
+
err(`\`${row.name}\` has permission \`${row.permission}\`; use ${policies.join(", ")}, or leave it blank`);
|
|
200
213
|
}
|
|
201
214
|
}
|
|
202
215
|
}
|
|
@@ -223,18 +236,36 @@ function checkCustom(tool, err) {
|
|
|
223
236
|
if (!tool.code) return err("a `## Code` section with a JavaScript code block is required");
|
|
224
237
|
if (!["javascript", "js"].includes(tool.code.lang)) err("the code must be JavaScript (```javascript)");
|
|
225
238
|
else {
|
|
239
|
+
// Parses the code without running it: the function this builds is never called.
|
|
226
240
|
try {
|
|
227
|
-
new
|
|
241
|
+
new Function(wrapToolCode(tool.code.source));
|
|
228
242
|
} catch (e) {
|
|
229
|
-
|
|
243
|
+
// A page's content security policy can forbid this, which says nothing about the code.
|
|
244
|
+
if (e instanceof SyntaxError) err(`the code doesn't parse: ${e.message}`);
|
|
230
245
|
}
|
|
231
246
|
}
|
|
232
247
|
}
|
|
233
248
|
|
|
249
|
+
// The agentbook tools a folder turns on, as { file, toolset, name, description, input_schema,
|
|
250
|
+
// permission }. `toolsets` is the catalog, { [toolset]: { tools: [{ name, description, input_schema }] } }.
|
|
251
|
+
// A blank permission means `always_ask`, the safe choice for a tool with no policy of its own.
|
|
252
|
+
export function agentbookTools(tools, toolsets = {}) {
|
|
253
|
+
return byName(tools.filter((t) => t.data.type === AGENTBOOK)).flatMap((tool) => {
|
|
254
|
+
const { toolset } = tool.data;
|
|
255
|
+
if (!Object.hasOwn(toolsets, toolset)) return [];
|
|
256
|
+
return toolsets[toolset].tools.flatMap((definition) => {
|
|
257
|
+
const row = tool.rows?.find((r) => r.name === definition.name);
|
|
258
|
+
if (row?.enabled.toLowerCase() !== "true") return [];
|
|
259
|
+
return [{ ...definition, file: tool.file, toolset, permission: row.permission || "always_ask" }];
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
234
264
|
// The tools list sent to the API, each kind sorted by name: the pre-built toolset with a config
|
|
235
265
|
// 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
|
|
237
|
-
|
|
266
|
+
// each MCP server, then the custom tools, and last agentbook's tools, which reach the API as
|
|
267
|
+
// custom tools. Only their name, description and input schema are sent.
|
|
268
|
+
export function compileTools(tools, { toolsets } = {}) {
|
|
238
269
|
const compiled = [];
|
|
239
270
|
const toolset = tools.find((t) => t.data.type === TOOLSET);
|
|
240
271
|
if (toolset) compiled.push({ type: TOOLSET, configs: configs(toolset.rows) });
|
|
@@ -247,6 +278,9 @@ export function compileTools(tools) {
|
|
|
247
278
|
for (const tool of tools.filter((t) => t.data.type === "custom").sort((a, b) => (a.name < b.name ? -1 : 1))) {
|
|
248
279
|
compiled.push({ type: "custom", name: tool.name, description: tool.data.description, input_schema: inputSchema(tool.params ?? []) });
|
|
249
280
|
}
|
|
281
|
+
for (const { name, description, input_schema } of agentbookTools(tools, toolsets)) {
|
|
282
|
+
compiled.push({ type: "custom", name, description, input_schema });
|
|
283
|
+
}
|
|
250
284
|
return compiled;
|
|
251
285
|
}
|
|
252
286
|
|
|
@@ -273,16 +307,5 @@ function inputSchema(params) {
|
|
|
273
307
|
return required.length > 0 ? { type: "object", properties, required } : { type: "object", properties };
|
|
274
308
|
}
|
|
275
309
|
|
|
276
|
-
//
|
|
277
|
-
|
|
278
|
-
export function loadTool(tool) {
|
|
279
|
-
const module = { exports: {} };
|
|
280
|
-
const fn = runInThisContext(wrap(tool.code.source), { filename: tool.path });
|
|
281
|
-
fn(module.exports, createRequire(tool.path), module, tool.path, resolve(tool.path, ".."));
|
|
282
|
-
if (typeof module.exports !== "function") {
|
|
283
|
-
throw new Error(`${tool.file}: module.exports must be a function`);
|
|
284
|
-
}
|
|
285
|
-
return module.exports;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const wrap = (source) => `(function (exports, require, module, __filename, __dirname) {${source}\n})`;
|
|
310
|
+
// A custom tool's code wrapped as a CommonJS module.
|
|
311
|
+
export const wrapToolCode = (source) => `(function (exports, require, module, __filename, __dirname) {${source}\n})`;
|
package/{src → lib}/vaults.js
RENAMED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { basename, join } from "node:path";
|
|
1
|
+
import { comparePaths, isIgnoredPath, textOf } from "./files.js";
|
|
3
2
|
import { FileError, parseMarkdown } from "./markdown.js";
|
|
4
3
|
|
|
5
4
|
export const VAULTS_DIR = "vaults";
|
|
@@ -38,21 +37,16 @@ export const IMMUTABLE_FIELDS = {
|
|
|
38
37
|
|
|
39
38
|
// Reads vaults/: each folder is a vault named after the folder, holding an optional vault.md
|
|
40
39
|
// and one .md file per credential, named after the file. Vaults are sorted by name, which is
|
|
41
|
-
// the order `run` attaches them in.
|
|
40
|
+
// the order `run` attaches them in. No vaults/ folder means no vaults.
|
|
42
41
|
// Returns { vaults, errors }; a file that can't be parsed has an entry in errors.
|
|
43
|
-
export function readVaults(
|
|
44
|
-
|
|
45
|
-
try {
|
|
46
|
-
if (!statSync(folder).isDirectory()) return { vaults: [], errors: [`${VAULTS_DIR}: not a directory`] };
|
|
47
|
-
} catch {
|
|
48
|
-
return { vaults: [], errors: [] };
|
|
49
|
-
}
|
|
42
|
+
export function readVaults(files) {
|
|
43
|
+
if (VAULTS_DIR in files) return { vaults: [], errors: [`${VAULTS_DIR}: not a directory`] };
|
|
50
44
|
|
|
51
|
-
const vaults =
|
|
45
|
+
const vaults = new Map();
|
|
52
46
|
const errors = [];
|
|
53
47
|
const parse = (file) => {
|
|
54
48
|
try {
|
|
55
|
-
return parseMarkdown(
|
|
49
|
+
return parseMarkdown(textOf(files[file]), file);
|
|
56
50
|
} catch (err) {
|
|
57
51
|
if (!(err instanceof FileError)) throw err;
|
|
58
52
|
errors.push(err.message);
|
|
@@ -60,34 +54,34 @@ export function readVaults(dir) {
|
|
|
60
54
|
}
|
|
61
55
|
};
|
|
62
56
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
57
|
+
const reportedFolders = new Set();
|
|
58
|
+
const paths = Object.keys(files)
|
|
59
|
+
.filter((path) => path.startsWith(`${VAULTS_DIR}/`) && !isIgnoredPath(path))
|
|
60
|
+
.sort(comparePaths);
|
|
61
|
+
for (const file of paths) {
|
|
62
|
+
const [, vaultName, itemName, ...rest] = file.split("/");
|
|
63
|
+
if (itemName === undefined) {
|
|
64
|
+
if (vaultName.endsWith(".md")) errors.push(`${file}: each vault is a folder; put credential files inside one`);
|
|
68
65
|
continue;
|
|
69
66
|
}
|
|
70
67
|
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
68
|
+
const path = `${VAULTS_DIR}/${vaultName}`;
|
|
69
|
+
if (!vaults.has(vaultName)) vaults.set(vaultName, { name: vaultName, path, file: null, credentials: [] });
|
|
70
|
+
const vault = vaults.get(vaultName);
|
|
71
|
+
if (rest.length > 0) {
|
|
72
|
+
const folder = `${path}/${itemName}`;
|
|
73
|
+
if (!reportedFolders.has(folder)) errors.push(`${folder}: a vault holds credential files, not folders`);
|
|
74
|
+
reportedFolders.add(folder);
|
|
75
|
+
} else if (itemName === VAULT_FILE) {
|
|
76
|
+
vault.file = parse(file);
|
|
77
|
+
} else if (itemName.endsWith(".md")) {
|
|
78
|
+
const parsed = parse(file);
|
|
79
|
+
if (parsed) vault.credentials.push({ file, name: itemName.slice(0, -".md".length), vault: vaultName, ...parsed });
|
|
83
80
|
}
|
|
84
|
-
vaults.push(vault);
|
|
85
81
|
}
|
|
86
|
-
return { vaults, errors };
|
|
82
|
+
return { vaults: [...vaults.values()], errors };
|
|
87
83
|
}
|
|
88
84
|
|
|
89
|
-
const sorted = (entries) => entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
90
|
-
|
|
91
85
|
// The lock file keys for a vault and a credential.
|
|
92
86
|
export const vaultKey = (vault) => `./${vault.path}`;
|
|
93
87
|
export const credentialKey = (cred) => `./${cred.file}`;
|
package/package.json
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soft-boy/agentschema",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Markdown based AI agent schema",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"agentschema": "bin/agentschema.js",
|
|
8
8
|
"ags": "bin/agentschema.js"
|
|
9
9
|
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": "./lib/index.js",
|
|
12
|
+
"./node": "./src/node.js",
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
10
15
|
"files": [
|
|
11
16
|
"bin",
|
|
17
|
+
"lib",
|
|
12
18
|
"src"
|
|
13
19
|
],
|
|
14
20
|
"engines": {
|
package/src/chat.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createInterface } from "node:readline";
|
|
2
|
+
import { agentMessageText, textStream } from "../lib/stream.js";
|
|
3
|
+
import { loadTool } from "./custom-tools.js";
|
|
2
4
|
import { Conversation } from "./session.js";
|
|
3
|
-
import { loadTool } from "./tools.js";
|
|
4
5
|
|
|
5
6
|
const color = (code) => (text) => (process.stdout.isTTY ? `\x1b[${code}m${text}\x1b[0m` : text);
|
|
6
7
|
const dim = color(2);
|
|
@@ -11,7 +12,7 @@ const bold = color(1);
|
|
|
11
12
|
export function formatEvent(event) {
|
|
12
13
|
switch (event.type) {
|
|
13
14
|
case "agent.message":
|
|
14
|
-
return event
|
|
15
|
+
return agentMessageText(event);
|
|
15
16
|
case "agent.tool_use":
|
|
16
17
|
case "agent.mcp_tool_use": {
|
|
17
18
|
const name = event.type === "agent.mcp_tool_use" ? `${event.mcp_server_name}.${event.name}` : event.name;
|
|
@@ -54,54 +55,6 @@ function firstLine(content = []) {
|
|
|
54
55
|
|
|
55
56
|
const truncate = (s, n = 100) => (s.length > n ? `${s.slice(0, n - 1)}…` : s);
|
|
56
57
|
|
|
57
|
-
// Prints the agent's text as it streams in, chunk by chunk. Deltas are best effort (the server
|
|
58
|
-
// may stop sending them under load), so the complete agent.message that follows is the record:
|
|
59
|
-
// whatever part of it the deltas didn't cover is printed then.
|
|
60
|
-
export function textStream(write) {
|
|
61
|
-
let preview = null; // { id, text } for the agent.message being previewed
|
|
62
|
-
const close = () => {
|
|
63
|
-
if (preview?.text) write("\n");
|
|
64
|
-
preview = null;
|
|
65
|
-
};
|
|
66
|
-
return {
|
|
67
|
-
// Returns true when the event was printed here, so it shouldn't be printed again.
|
|
68
|
-
handle(event) {
|
|
69
|
-
switch (event.type) {
|
|
70
|
-
case "event_start":
|
|
71
|
-
close();
|
|
72
|
-
if (event.event?.type === "agent.message") preview = { id: event.event.id, text: "" };
|
|
73
|
-
return true;
|
|
74
|
-
case "event_delta": {
|
|
75
|
-
const content = event.delta?.content;
|
|
76
|
-
if (preview?.id !== event.event_id || content?.type !== "text" || !content.text) return true;
|
|
77
|
-
preview.text += content.text;
|
|
78
|
-
write(content.text);
|
|
79
|
-
return true;
|
|
80
|
-
}
|
|
81
|
-
case "agent.message": {
|
|
82
|
-
if (preview?.id !== event.id) {
|
|
83
|
-
close();
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
const full = formatEvent(event);
|
|
87
|
-
const shown = preview.text;
|
|
88
|
-
preview = null;
|
|
89
|
-
if (full.startsWith(shown)) {
|
|
90
|
-
if (full.length > 0) write(`${full.slice(shown.length)}\n`);
|
|
91
|
-
} else {
|
|
92
|
-
write(`\n${full}\n`);
|
|
93
|
-
}
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
default:
|
|
97
|
-
// A model request that errors or is interrupted ends without its agent.message.
|
|
98
|
-
close();
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
},
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
58
|
// Runs a custom tool's code with the call's input. A thrown error is returned as an error
|
|
106
59
|
// result, so the agent sees what went wrong.
|
|
107
60
|
export async function runTool(tool, input) {
|