@wheatley-ai/cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/bin/wheatley.js +2 -0
- package/dist/approvals/gate.js +278 -0
- package/dist/approvals/module.js +21 -0
- package/dist/approvals/policy.js +30 -0
- package/dist/approvals/preview.js +17 -0
- package/dist/approvals/prompt.js +98 -0
- package/dist/approvals/rpc.js +16 -0
- package/dist/approvals/store.js +159 -0
- package/dist/cli/ask.js +94 -0
- package/dist/cli/chat.js +178 -0
- package/dist/cli/client.js +104 -0
- package/dist/cli/daemon.js +35 -0
- package/dist/cli/init.js +72 -0
- package/dist/cli/io.js +45 -0
- package/dist/cli/main.js +206 -0
- package/dist/cli/session.js +86 -0
- package/dist/cli/status.js +46 -0
- package/dist/contracts/approval-gate.js +1 -0
- package/dist/contracts/config-store.js +1 -0
- package/dist/contracts/delivery-outbox.js +1 -0
- package/dist/contracts/errors.js +28 -0
- package/dist/contracts/extension-catalog.js +1 -0
- package/dist/contracts/maintenance-runner.js +1 -0
- package/dist/contracts/memory-store.js +1 -0
- package/dist/contracts/module.js +7 -0
- package/dist/contracts/tool-names.js +43 -0
- package/dist/contracts/trigger-runtime.js +1 -0
- package/dist/contracts/webhook-ingress.js +2 -0
- package/dist/core/agent/agent-store.js +300 -0
- package/dist/core/agent/agent.js +1033 -0
- package/dist/core/agent/gated-models.js +89 -0
- package/dist/core/agent/pi-adapter.js +401 -0
- package/dist/core/agent/policy-gate.js +44 -0
- package/dist/core/builtin-catalog.js +20 -0
- package/dist/core/concurrency-gate.js +87 -0
- package/dist/core/config/atomic-file.js +72 -0
- package/dist/core/config/config-store.js +138 -0
- package/dist/core/config/pending-restart.js +53 -0
- package/dist/core/config/secret-references.js +69 -0
- package/dist/core/config/secret-store.js +147 -0
- package/dist/core/config/validate.js +55 -0
- package/dist/core/config/yaml-patch.js +57 -0
- package/dist/core/daemon-socket.js +96 -0
- package/dist/core/daemon.js +97 -0
- package/dist/core/deployment.js +379 -0
- package/dist/core/error-message.js +4 -0
- package/dist/core/events.js +25 -0
- package/dist/core/logger.js +81 -0
- package/dist/core/models.js +27 -0
- package/dist/core/module.js +42 -0
- package/dist/core/rpc/agent-handlers.js +82 -0
- package/dist/core/rpc/deployment-handlers.js +63 -0
- package/dist/core/rpc/router.js +96 -0
- package/dist/core/server/connection.js +170 -0
- package/dist/core/server/control-server.js +152 -0
- package/dist/core/server/static-files.js +79 -0
- package/dist/core/supervisor.js +215 -0
- package/dist/core/tool-routing.js +41 -0
- package/dist/dashboard/assets/index-C9Rul5vE.css +2 -0
- package/dist/dashboard/assets/index-DXzGGAxQ.js +11 -0
- package/dist/dashboard/assets/sheet-presentation-DMljN0LW.js +1 -0
- package/dist/dashboard/index.html +18 -0
- package/dist/db/database.js +16 -0
- package/dist/db/migrations.js +184 -0
- package/dist/frontends/discord/approvals.js +199 -0
- package/dist/frontends/discord/attachments.js +115 -0
- package/dist/frontends/discord/binding.js +489 -0
- package/dist/frontends/discord/config.js +107 -0
- package/dist/frontends/discord/discord-runtime.js +52 -0
- package/dist/frontends/discord/errors.js +61 -0
- package/dist/frontends/discord/live-runs.js +189 -0
- package/dist/frontends/discord/module.js +39 -0
- package/dist/frontends/discord/recent-ids.js +20 -0
- package/dist/frontends/slack/approval-blocks.js +98 -0
- package/dist/frontends/slack/attachments.js +102 -0
- package/dist/frontends/slack/background-tasks.js +20 -0
- package/dist/frontends/slack/binding.js +405 -0
- package/dist/frontends/slack/bounded.js +9 -0
- package/dist/frontends/slack/chat-id.js +17 -0
- package/dist/frontends/slack/config.js +80 -0
- package/dist/frontends/slack/delivery-errors.js +81 -0
- package/dist/frontends/slack/inbound.js +76 -0
- package/dist/frontends/slack/live-progress.js +190 -0
- package/dist/frontends/slack/manifest.js +66 -0
- package/dist/frontends/slack/outbound.js +160 -0
- package/dist/frontends/slack/schemas.js +99 -0
- package/dist/frontends/slack/signature.js +30 -0
- package/dist/frontends/slack/slack-client.js +60 -0
- package/dist/frontends/slack/slack-frontend.js +38 -0
- package/dist/frontends/slack/state-store.js +112 -0
- package/dist/frontends/telegram/approvals.js +134 -0
- package/dist/frontends/telegram/attachments.js +126 -0
- package/dist/frontends/telegram/binding.js +144 -0
- package/dist/frontends/telegram/chat-key.js +43 -0
- package/dist/frontends/telegram/client.js +88 -0
- package/dist/frontends/telegram/config.js +93 -0
- package/dist/frontends/telegram/delivery.js +215 -0
- package/dist/frontends/telegram/frontend.js +42 -0
- package/dist/frontends/telegram/grammy-client.js +122 -0
- package/dist/frontends/telegram/http-status.js +6 -0
- package/dist/frontends/telegram/inbound.js +149 -0
- package/dist/frontends/telegram/live.js +182 -0
- package/dist/frontends/telegram/markdown.js +103 -0
- package/dist/frontends/telegram/recent-set.js +18 -0
- package/dist/frontends/telegram/serial-queue.js +23 -0
- package/dist/index.js +7 -0
- package/dist/mcp/content.js +61 -0
- package/dist/mcp/mcp-module.js +35 -0
- package/dist/mcp/mcp-tool-provider.js +93 -0
- package/dist/mcp/server-connection.js +297 -0
- package/dist/memory/consolidation.js +260 -0
- package/dist/memory/format.js +5 -0
- package/dist/memory/migrations.js +76 -0
- package/dist/memory/module.js +69 -0
- package/dist/memory/names.js +20 -0
- package/dist/memory/rows.js +22 -0
- package/dist/memory/rpc.js +49 -0
- package/dist/memory/snapshots.js +12 -0
- package/dist/memory/store.js +281 -0
- package/dist/memory/tools.js +158 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/config.d.ts +262 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/config.js +120 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/domain.d.ts +512 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/domain.js +441 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/duration.d.ts +8 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/duration.js +28 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/events.d.ts +234 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/events.js +124 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/index.d.ts +11 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/index.js +11 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/json-rpc.d.ts +81 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/json-rpc.js +157 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/json.d.ts +14 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/json.js +23 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/limits.d.ts +17 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/limits.js +17 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/methods.d.ts +1107 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/methods.js +201 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/rpc-error.d.ts +45 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/rpc-error.js +44 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/validation.d.ts +20 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/validation.js +11 -0
- package/dist/node_modules/@wheatley-ai/protocol/package.json +14 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/chunk.d.ts +11 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/chunk.js +162 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/commands.d.ts +48 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/commands.js +39 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/delivery-error.d.ts +29 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/delivery-error.js +27 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/frontend.d.ts +236 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/frontend.js +2 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/index.d.ts +10 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/index.js +7 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/json.d.ts +9 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/json.js +2 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/logger.d.ts +18 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/logger.js +2 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/plugin.d.ts +48 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/plugin.js +5 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/schema.d.ts +69 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/schema.js +3 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/tool.d.ts +69 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/tool.js +3 -0
- package/dist/node_modules/@wheatley-ai/sdk/package.json +11 -0
- package/dist/ops/backup.js +185 -0
- package/dist/ops/cli.js +194 -0
- package/dist/ops/command-runner.js +34 -0
- package/dist/ops/dashboard-assets.js +37 -0
- package/dist/ops/migrate.js +63 -0
- package/dist/ops/module.js +47 -0
- package/dist/ops/paths.js +55 -0
- package/dist/ops/service.js +376 -0
- package/dist/ops/update.js +145 -0
- package/dist/plugins/contributions.js +89 -0
- package/dist/plugins/guards.js +111 -0
- package/dist/plugins/load.js +76 -0
- package/dist/plugins/module.js +31 -0
- package/dist/plugins/provider.js +349 -0
- package/dist/plugins/schema.js +69 -0
- package/dist/skills/disclosure.js +142 -0
- package/dist/skills/discovery.js +120 -0
- package/dist/skills/fs-errors.js +8 -0
- package/dist/skills/read-skill-tool.js +3 -0
- package/dist/skills/skill-file.js +90 -0
- package/dist/skills/skills.js +140 -0
- package/dist/tools/builtin-tools.js +61 -0
- package/dist/tools/input.js +45 -0
- package/dist/tools/paths.js +6 -0
- package/dist/tools/read-file.js +65 -0
- package/dist/tools/shell.js +156 -0
- package/dist/tools/write-file.js +58 -0
- package/dist/triggers/delivery-codec.js +99 -0
- package/dist/triggers/delivery-outbox.js +313 -0
- package/dist/triggers/due-timer.js +14 -0
- package/dist/triggers/errors.js +2 -0
- package/dist/triggers/migrations.js +91 -0
- package/dist/triggers/module.js +106 -0
- package/dist/triggers/rpc.js +35 -0
- package/dist/triggers/schedule.js +52 -0
- package/dist/triggers/sql-row.js +59 -0
- package/dist/triggers/trigger-runtime.js +445 -0
- package/dist/triggers/trigger-tools.js +210 -0
- package/dist/triggers/webhook-ingress.js +212 -0
- package/dist/wiring/agent-services.js +151 -0
- package/dist/wiring/catalog.js +81 -0
- package/dist/wiring/daemon.js +83 -0
- package/dist/wiring/frontend-host.js +268 -0
- package/package.json +50 -3
- package/README.md +0 -3
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { readdir, readFile, realpath, stat } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { readSkillFile } from "./discovery.js";
|
|
4
|
+
import { isNotFound } from "./fs-errors.js";
|
|
5
|
+
import { READ_SKILL_TOOL } from "./read-skill-tool.js";
|
|
6
|
+
import { SKILL_FILE_NAME } from "./skill-file.js";
|
|
7
|
+
/** The maximum size of a bundled file that Tier 3 returns. */
|
|
8
|
+
export const BUNDLED_FILE_MAX_BYTES = 256 * 1024;
|
|
9
|
+
/** The maximum number of bundled files that Tier 2 lists. */
|
|
10
|
+
export const BUNDLED_FILE_LIST_MAX = 200;
|
|
11
|
+
/** The maximum folder depth below the Skill folder that Tier 2 lists. */
|
|
12
|
+
const BUNDLED_FILE_MAX_DEPTH = 8;
|
|
13
|
+
const NUL_BYTE = 0;
|
|
14
|
+
const PARENT_SEGMENT = /^\.\.(?:[/\\]|$)/u;
|
|
15
|
+
const failure = (reason) => ({ ok: false, reason });
|
|
16
|
+
const walk = async (root, dir, depth, listing) => {
|
|
17
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
18
|
+
const visible = entries
|
|
19
|
+
.filter((entry) => !entry.name.startsWith("."))
|
|
20
|
+
.toSorted((a, b) => (a.name < b.name ? -1 : 1));
|
|
21
|
+
for (const entry of visible) {
|
|
22
|
+
if (listing.files.length >= BUNDLED_FILE_LIST_MAX) {
|
|
23
|
+
listing.truncated = true;
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const entryPath = path.join(dir, entry.name);
|
|
27
|
+
const relativePath = path
|
|
28
|
+
.relative(root, entryPath)
|
|
29
|
+
.split(path.sep)
|
|
30
|
+
.join("/");
|
|
31
|
+
if (entry.isDirectory() && depth < BUNDLED_FILE_MAX_DEPTH) {
|
|
32
|
+
// oxlint-disable-next-line no-await-in-loop -- The walk stops at BUNDLED_FILE_LIST_MAX files, so it reads folders in order.
|
|
33
|
+
await walk(root, entryPath, depth + 1, listing);
|
|
34
|
+
}
|
|
35
|
+
else if ((entry.isFile() || entry.isSymbolicLink()) &&
|
|
36
|
+
relativePath !== SKILL_FILE_NAME) {
|
|
37
|
+
listing.files.push(relativePath);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
/** Lists the bundled files of a Skill folder as `/`-separated relative paths, without `SKILL.md` and hidden files. */
|
|
42
|
+
const listBundledFiles = async (dir) => {
|
|
43
|
+
const listing = { files: [], truncated: false };
|
|
44
|
+
await walk(dir, dir, 0, listing);
|
|
45
|
+
return listing;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Tier 2: reads the full `SKILL.md` of `skill` from disk again, so an edit since discovery shows. Returns the body, the
|
|
49
|
+
* `allowed-tools` metadata with a note that the Permission Policy decides every Tool call, and the bundled files that
|
|
50
|
+
* Tier 3 can read.
|
|
51
|
+
*
|
|
52
|
+
* Returns `ok: false` if the file is now missing or invalid. Rejects only for unexpected I/O errors.
|
|
53
|
+
*/
|
|
54
|
+
export const discloseSkill = async (skill) => {
|
|
55
|
+
const { name } = skill.info;
|
|
56
|
+
let read;
|
|
57
|
+
try {
|
|
58
|
+
read = await readSkillFile(skill.dir, path.basename(skill.dir));
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (isNotFound(error)) {
|
|
62
|
+
return failure(`The Skill ${name} no longer has a ${SKILL_FILE_NAME}`);
|
|
63
|
+
}
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
if (!read.ok) {
|
|
67
|
+
return failure(`The ${SKILL_FILE_NAME} of the Skill ${name} ${read.reason}`);
|
|
68
|
+
}
|
|
69
|
+
const { files, truncated } = await listBundledFiles(skill.dir);
|
|
70
|
+
const sections = [`# Skill: ${name}`, read.file.body];
|
|
71
|
+
if (read.file.allowedTools.length > 0) {
|
|
72
|
+
sections.push(`The allowed-tools metadata of this Skill (${read.file.allowedTools.join(", ")}) grants nothing. The Permission Policy decides every Tool call.`);
|
|
73
|
+
}
|
|
74
|
+
if (files.length > 0) {
|
|
75
|
+
const more = truncated
|
|
76
|
+
? `\n(The list stops after ${BUNDLED_FILE_LIST_MAX} files.)`
|
|
77
|
+
: "";
|
|
78
|
+
sections.push(`Bundled files. To read one, call ${READ_SKILL_TOOL} with name "${name}" and its path:\n${files.map((file) => `- ${file}`).join("\n")}${more}`);
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
ok: true,
|
|
82
|
+
text: sections.filter((part) => part !== "").join("\n\n"),
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
/** Returns the reason that `filePath` is not a valid relative path inside a Skill folder, or `undefined`. */
|
|
86
|
+
const pathProblem = (filePath) => {
|
|
87
|
+
const normalized = path.normalize(filePath);
|
|
88
|
+
const isOutside = path.isAbsolute(filePath) ||
|
|
89
|
+
filePath.includes("\0") ||
|
|
90
|
+
normalized === "." ||
|
|
91
|
+
PARENT_SEGMENT.test(normalized);
|
|
92
|
+
return isOutside
|
|
93
|
+
? `The path ${filePath} is not a relative path inside the Skill folder`
|
|
94
|
+
: undefined;
|
|
95
|
+
};
|
|
96
|
+
const resolveInside = async (skillDir, filePath) => {
|
|
97
|
+
const realDir = await realpath(skillDir);
|
|
98
|
+
const realTarget = await realpath(path.join(skillDir, path.normalize(filePath)));
|
|
99
|
+
return realTarget.startsWith(`${realDir}${path.sep}`)
|
|
100
|
+
? realTarget
|
|
101
|
+
: undefined;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Tier 3: reads one bundled text file of `skill`. `filePath` is relative to the Skill folder, for example
|
|
105
|
+
* `references/forms.md`.
|
|
106
|
+
*
|
|
107
|
+
* Invariant: the file must resolve inside the Skill folder after it follows symbolic links, so `..` segments and links
|
|
108
|
+
* cannot read other files. Returns `ok: false` for such a path, a missing file, a folder, a file larger than
|
|
109
|
+
* `BUNDLED_FILE_MAX_BYTES`, or a binary file. Rejects only for unexpected I/O errors.
|
|
110
|
+
*/
|
|
111
|
+
export const discloseBundledFile = async (skill, filePath) => {
|
|
112
|
+
const { name } = skill.info;
|
|
113
|
+
const problem = pathProblem(filePath);
|
|
114
|
+
if (problem !== undefined) {
|
|
115
|
+
return failure(problem);
|
|
116
|
+
}
|
|
117
|
+
let target;
|
|
118
|
+
try {
|
|
119
|
+
target = await resolveInside(skill.dir, filePath);
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
if (isNotFound(error)) {
|
|
123
|
+
return failure(`The Skill ${name} has no file ${filePath}`);
|
|
124
|
+
}
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
if (target === undefined) {
|
|
128
|
+
return failure(`The path ${filePath} leaves the folder of the Skill ${name}`);
|
|
129
|
+
}
|
|
130
|
+
const info = await stat(target);
|
|
131
|
+
if (!info.isFile()) {
|
|
132
|
+
return failure(`${filePath} in the Skill ${name} is not a file`);
|
|
133
|
+
}
|
|
134
|
+
if (info.size > BUNDLED_FILE_MAX_BYTES) {
|
|
135
|
+
return failure(`${filePath} in the Skill ${name} is larger than ${BUNDLED_FILE_MAX_BYTES} bytes`);
|
|
136
|
+
}
|
|
137
|
+
const bytes = await readFile(target);
|
|
138
|
+
if (bytes.includes(NUL_BYTE)) {
|
|
139
|
+
return failure(`${filePath} in the Skill ${name} is a binary file`);
|
|
140
|
+
}
|
|
141
|
+
return { ok: true, text: bytes.toString("utf-8") };
|
|
142
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { isNotFolder, isNotFound, messageOf } from "./fs-errors.js";
|
|
4
|
+
import { parseSkillFile, SKILL_FILE_NAME } from "./skill-file.js";
|
|
5
|
+
/** The maximum size of a `SKILL.md` file. A larger file is skipped with a warning. */
|
|
6
|
+
export const SKILL_FILE_MAX_BYTES = 256 * 1024;
|
|
7
|
+
/**
|
|
8
|
+
* Reads and parses the `SKILL.md` file of the Skill folder `dir`, whose base name is `folderName`.
|
|
9
|
+
*
|
|
10
|
+
* Returns `ok: false` with a reason that completes the sentence "SKILL.md ..." for a file that is too large or
|
|
11
|
+
* invalid. Rejects for I/O errors, for example a missing file.
|
|
12
|
+
*/
|
|
13
|
+
export const readSkillFile = async (dir, folderName) => {
|
|
14
|
+
const filePath = path.join(dir, SKILL_FILE_NAME);
|
|
15
|
+
const { size } = await stat(filePath);
|
|
16
|
+
if (size > SKILL_FILE_MAX_BYTES) {
|
|
17
|
+
return {
|
|
18
|
+
ok: false,
|
|
19
|
+
reason: `is larger than ${SKILL_FILE_MAX_BYTES} bytes`,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const text = await readFile(filePath, "utf-8");
|
|
23
|
+
const parsed = parseSkillFile(text, folderName);
|
|
24
|
+
return parsed.ok ? { file: parsed.skill, ok: true } : parsed;
|
|
25
|
+
};
|
|
26
|
+
/** Returns true if `dir` is a folder that contains a `SKILL.md` entry, after it follows symbolic links. */
|
|
27
|
+
const hasSkillFile = async (dir) => {
|
|
28
|
+
try {
|
|
29
|
+
await stat(path.join(dir, SKILL_FILE_NAME));
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (isNotFound(error) || isNotFolder(error)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const loadSkillFolder = async (root, folderName) => {
|
|
40
|
+
const dir = path.join(root.dir, folderName);
|
|
41
|
+
const filePath = path.join(dir, SKILL_FILE_NAME);
|
|
42
|
+
try {
|
|
43
|
+
if (!(await hasSkillFile(dir))) {
|
|
44
|
+
return { kind: "none" };
|
|
45
|
+
}
|
|
46
|
+
const read = await readSkillFile(dir, folderName);
|
|
47
|
+
if (!read.ok) {
|
|
48
|
+
return { kind: "warning", warning: `${filePath} ${read.reason}` };
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
kind: "skill",
|
|
52
|
+
skill: {
|
|
53
|
+
dir,
|
|
54
|
+
file: read.file,
|
|
55
|
+
info: {
|
|
56
|
+
description: read.file.description,
|
|
57
|
+
name: read.file.name,
|
|
58
|
+
origin: root.origin,
|
|
59
|
+
path: filePath,
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
return {
|
|
66
|
+
kind: "warning",
|
|
67
|
+
warning: `${filePath} cannot be read: ${messageOf(error)}`,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const scanRoot = async (root) => {
|
|
72
|
+
let entries = [];
|
|
73
|
+
try {
|
|
74
|
+
entries = await readdir(root.dir);
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
return isNotFound(error)
|
|
78
|
+
? []
|
|
79
|
+
: [
|
|
80
|
+
{
|
|
81
|
+
kind: "warning",
|
|
82
|
+
warning: `${root.dir} cannot be read: ${messageOf(error)}`,
|
|
83
|
+
},
|
|
84
|
+
];
|
|
85
|
+
}
|
|
86
|
+
const folderNames = entries
|
|
87
|
+
.filter((entry) => !entry.startsWith("."))
|
|
88
|
+
.toSorted();
|
|
89
|
+
return await Promise.all(folderNames.map(async (folderName) => await loadSkillFolder(root, folderName)));
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Discovers the Skills in `roots`. Each Skill is a folder with a `SKILL.md` file directly inside a root.
|
|
93
|
+
*
|
|
94
|
+
* Ordering and precedence: a later root overrides an earlier root for a Skill with the same name. Callers pass the
|
|
95
|
+
* shared root first and the Agent root second, so an Agent Skill replaces a shared Skill. An invalid Skill does not
|
|
96
|
+
* override a valid one. A missing root is not a problem. A folder without `SKILL.md`, a hidden folder, and a plain file
|
|
97
|
+
* are not Skills and produce no warning. An invalid or unreadable `SKILL.md` produces a warning, and the Skill does not
|
|
98
|
+
* load. Skills named in `disabled` do not load. Warnings follow root order, then folder name order.
|
|
99
|
+
*
|
|
100
|
+
* Errors: rejects with the abort reason if `signal` aborts. Never rejects for one broken Skill.
|
|
101
|
+
*/
|
|
102
|
+
export const discoverSkills = async (roots, disabled, signal) => {
|
|
103
|
+
signal.throwIfAborted();
|
|
104
|
+
const scans = await Promise.all(roots.map(async (root) => await scanRoot(root)));
|
|
105
|
+
signal.throwIfAborted();
|
|
106
|
+
const byName = new Map();
|
|
107
|
+
const warnings = [];
|
|
108
|
+
for (const load of scans.flat()) {
|
|
109
|
+
if (load.kind === "skill") {
|
|
110
|
+
byName.set(load.skill.info.name, load.skill);
|
|
111
|
+
}
|
|
112
|
+
else if (load.kind === "warning") {
|
|
113
|
+
warnings.push(load.warning);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const skills = [...byName.values()]
|
|
117
|
+
.filter((skill) => !disabled.has(skill.info.name))
|
|
118
|
+
.toSorted((a, b) => (a.info.name < b.info.name ? -1 : 1));
|
|
119
|
+
return { skills, warnings };
|
|
120
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Returns true if `cause` is a Node.js file system error, which carries an errno `code` such as `ENOENT`. */
|
|
2
|
+
const isErrnoException = (cause) => cause instanceof Error && "code" in cause;
|
|
3
|
+
/** Returns true if `cause` reports a missing file or folder. */
|
|
4
|
+
export const isNotFound = (cause) => isErrnoException(cause) && cause.code === "ENOENT";
|
|
5
|
+
/** Returns true if `cause` reports a path whose parent is a file, for example `notes.txt/SKILL.md`. */
|
|
6
|
+
export const isNotFolder = (cause) => isErrnoException(cause) && cause.code === "ENOTDIR";
|
|
7
|
+
/** The message of a thrown value, for a warning or a Tool error result. */
|
|
8
|
+
export const messageOf = (cause) => cause instanceof Error ? cause.message : String(cause);
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Type } from "typebox";
|
|
2
|
+
import { Value } from "typebox/value";
|
|
3
|
+
import { parseDocument } from "yaml";
|
|
4
|
+
/** The file that defines a Skill inside its folder. */
|
|
5
|
+
export const SKILL_FILE_NAME = "SKILL.md";
|
|
6
|
+
/** The maximum length of a Skill name (Agent Skills specification). */
|
|
7
|
+
export const SKILL_NAME_MAX_LENGTH = 64;
|
|
8
|
+
/** The maximum length of a Skill description (Agent Skills specification). */
|
|
9
|
+
export const SKILL_DESCRIPTION_MAX_LENGTH = 1024;
|
|
10
|
+
/** A `---` line, the YAML frontmatter, and a closing `---` line at the start of the file. */
|
|
11
|
+
const FRONTMATTER = /^---[ \t]*\r?\n(?:(?<yaml>[\s\S]*?)\r?\n)?---[ \t]*(?:\r?\n|$)/u;
|
|
12
|
+
const BYTE_ORDER_MARK = "";
|
|
13
|
+
const WHITESPACE_RUN = /\s+/gu;
|
|
14
|
+
/**
|
|
15
|
+
* The frontmatter keys that Wheatley reads. Other keys, for example `license` and `metadata`, are allowed and ignored.
|
|
16
|
+
* The name rule is from the Agent Skills specification: lowercase letters, digits, and single hyphens between them.
|
|
17
|
+
*/
|
|
18
|
+
const FrontmatterSchema = Type.Object({
|
|
19
|
+
"allowed-tools": Type.Optional(Type.Union([Type.String(), Type.Array(Type.String()), Type.Null()])),
|
|
20
|
+
description: Type.String({
|
|
21
|
+
maxLength: SKILL_DESCRIPTION_MAX_LENGTH,
|
|
22
|
+
minLength: 1,
|
|
23
|
+
}),
|
|
24
|
+
name: Type.String({
|
|
25
|
+
maxLength: SKILL_NAME_MAX_LENGTH,
|
|
26
|
+
pattern: "^[a-z0-9]+(-[a-z0-9]+)*$",
|
|
27
|
+
}),
|
|
28
|
+
});
|
|
29
|
+
const invalid = (reason) => ({ ok: false, reason });
|
|
30
|
+
const decodeFrontmatter = (source) => {
|
|
31
|
+
const document = parseDocument(source);
|
|
32
|
+
const [yamlError] = document.errors;
|
|
33
|
+
if (yamlError !== undefined) {
|
|
34
|
+
return {
|
|
35
|
+
ok: false,
|
|
36
|
+
reason: `has invalid YAML frontmatter: ${yamlError.message}`,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
const data = document.toJS() ?? {};
|
|
40
|
+
if (Value.Check(FrontmatterSchema, data)) {
|
|
41
|
+
return { frontmatter: data, ok: true };
|
|
42
|
+
}
|
|
43
|
+
const issues = Value.Errors(FrontmatterSchema, data).map((issue) => `${issue.instancePath || "/"} ${issue.message}`);
|
|
44
|
+
return { ok: false, reason: `has invalid frontmatter: ${issues.join("; ")}` };
|
|
45
|
+
};
|
|
46
|
+
const allowedToolsOf = (frontmatter) => {
|
|
47
|
+
const value = frontmatter["allowed-tools"] ?? [];
|
|
48
|
+
return Array.isArray(value)
|
|
49
|
+
? value
|
|
50
|
+
: value.split(WHITESPACE_RUN).filter((tool) => tool !== "");
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Parses the text of a `SKILL.md` file in the folder `folderName`.
|
|
54
|
+
*
|
|
55
|
+
* The file must start with YAML frontmatter between two `---` lines. The frontmatter must have a `name` that matches
|
|
56
|
+
* the folder name and the Agent Skills name rules, and a non-blank `description` of at most 1024 characters.
|
|
57
|
+
* `allowed-tools` is optional: a space-separated string or a list of strings.
|
|
58
|
+
*
|
|
59
|
+
* Returns `ok: false` with a reason that completes the sentence "SKILL.md ...". Never throws.
|
|
60
|
+
*/
|
|
61
|
+
export const parseSkillFile = (text, folderName) => {
|
|
62
|
+
const content = text.startsWith(BYTE_ORDER_MARK) ? text.slice(1) : text;
|
|
63
|
+
const match = FRONTMATTER.exec(content);
|
|
64
|
+
if (match === null) {
|
|
65
|
+
return invalid("has no YAML frontmatter between --- lines");
|
|
66
|
+
}
|
|
67
|
+
const decoded = decodeFrontmatter(match.groups?.yaml ?? "");
|
|
68
|
+
if (!decoded.ok) {
|
|
69
|
+
return invalid(decoded.reason);
|
|
70
|
+
}
|
|
71
|
+
const { frontmatter } = decoded;
|
|
72
|
+
if (frontmatter.name !== folderName) {
|
|
73
|
+
return invalid(`has the name "${frontmatter.name}", which differs from its folder name "${folderName}"`);
|
|
74
|
+
}
|
|
75
|
+
const description = frontmatter.description
|
|
76
|
+
.replaceAll(WHITESPACE_RUN, " ")
|
|
77
|
+
.trim();
|
|
78
|
+
if (description === "") {
|
|
79
|
+
return invalid("has a blank description");
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
ok: true,
|
|
83
|
+
skill: {
|
|
84
|
+
allowedTools: allowedToolsOf(frontmatter),
|
|
85
|
+
body: content.slice(match[0].length).trim(),
|
|
86
|
+
description,
|
|
87
|
+
name: frontmatter.name,
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { decode } from "@wheatley-ai/protocol";
|
|
3
|
+
import { Type } from "typebox";
|
|
4
|
+
import { discloseBundledFile, discloseSkill } from "./disclosure.js";
|
|
5
|
+
import { discoverSkills } from "./discovery.js";
|
|
6
|
+
import { messageOf } from "./fs-errors.js";
|
|
7
|
+
import { READ_SKILL_TOOL } from "./read-skill-tool.js";
|
|
8
|
+
export { READ_SKILL_TOOL } from "./read-skill-tool.js";
|
|
9
|
+
const NAME_DESCRIPTION = "The Skill name from the catalog.";
|
|
10
|
+
const PATH_DESCRIPTION = "Optional. A bundled file of the Skill, relative to its folder, for example references/forms.md.";
|
|
11
|
+
/** Validates read_skill input. It matches `readSkillInputSchema`. */
|
|
12
|
+
const ReadSkillInputSchema = Type.Object({
|
|
13
|
+
name: Type.String({ minLength: 1 }),
|
|
14
|
+
path: Type.Optional(Type.String({ minLength: 1 })),
|
|
15
|
+
}, { additionalProperties: false });
|
|
16
|
+
/** The read_skill input schema as plain JSON Schema for the model provider. */
|
|
17
|
+
const readSkillInputSchema = {
|
|
18
|
+
additionalProperties: false,
|
|
19
|
+
properties: {
|
|
20
|
+
name: { description: NAME_DESCRIPTION, minLength: 1, type: "string" },
|
|
21
|
+
path: { description: PATH_DESCRIPTION, minLength: 1, type: "string" },
|
|
22
|
+
},
|
|
23
|
+
required: ["name"],
|
|
24
|
+
type: "object",
|
|
25
|
+
};
|
|
26
|
+
const readSkillDescriptor = {
|
|
27
|
+
defaultAction: "allow",
|
|
28
|
+
description: "Loads a Skill from the Skill catalog. Pass name to read the full instructions of the Skill. Pass name and path to read one bundled file of the Skill.",
|
|
29
|
+
inputSchema: readSkillInputSchema,
|
|
30
|
+
name: READ_SKILL_TOOL,
|
|
31
|
+
source: "builtin",
|
|
32
|
+
};
|
|
33
|
+
const errorResult = (text) => ({
|
|
34
|
+
content: [{ text, type: "text" }],
|
|
35
|
+
isError: true,
|
|
36
|
+
});
|
|
37
|
+
const textResult = (text) => ({
|
|
38
|
+
content: [{ text, type: "text" }],
|
|
39
|
+
});
|
|
40
|
+
const skillRoots = (input) => [
|
|
41
|
+
{ dir: path.join(input.homeDir, "skills"), origin: "shared" },
|
|
42
|
+
{ dir: path.join(input.agentDir, "skills"), origin: "agent" },
|
|
43
|
+
];
|
|
44
|
+
const disabledSkills = (config) => new Set(config.skills?.disabled);
|
|
45
|
+
const toolResult = (disclosure) => disclosure.ok ? textResult(disclosure.text) : errorResult(disclosure.reason);
|
|
46
|
+
/**
|
|
47
|
+
* Creates the Skills provider of one Agent (ADR 0006). It discovers Skills once, when it is created, from
|
|
48
|
+
* `<homeDir>/skills/` (`origin: "shared"`) and `<agentDir>/skills/` (`origin: "agent"`).
|
|
49
|
+
*
|
|
50
|
+
* Invariants:
|
|
51
|
+
* - An Agent Skill overrides a shared Skill with the same name. Skills in `config.skills.disabled` do not load.
|
|
52
|
+
* - An invalid `SKILL.md` does not load and appears in `warnings`.
|
|
53
|
+
* - Progressive disclosure: Tier 1 is `catalogPrompt`. Tier 2 (`read_skill` with `name`) and Tier 3 (`read_skill` with
|
|
54
|
+
* `name` and `path`) read from disk on each call.
|
|
55
|
+
* - The provider offers only `read_skill`, and only if a Skill loaded. The `allowed-tools` metadata of a Skill changes
|
|
56
|
+
* no Tool and no Permission Policy action. The Approval Gate checks every Tool call, `read_skill` included.
|
|
57
|
+
*
|
|
58
|
+
* Errors: rejects with the abort reason if `input.signal` aborts during discovery. Never rejects for a broken Skill.
|
|
59
|
+
*/
|
|
60
|
+
export const createSkillProvider = async (input) => {
|
|
61
|
+
const discovery = await discoverSkills(skillRoots(input), disabledSkills(input.config), input.signal);
|
|
62
|
+
const byName = new Map(discovery.skills.map((skill) => [skill.info.name, skill]));
|
|
63
|
+
const infos = discovery.skills.map((skill) => skill.info);
|
|
64
|
+
const catalog = discovery.skills
|
|
65
|
+
.map((skill) => `- ${skill.info.name}: ${skill.info.description}`)
|
|
66
|
+
.join("\n");
|
|
67
|
+
const tools = byName.size > 0 ? [readSkillDescriptor] : [];
|
|
68
|
+
for (const warning of discovery.warnings) {
|
|
69
|
+
input.log.warn("Skipped a Skill", { warning });
|
|
70
|
+
}
|
|
71
|
+
const readSkill = async (parsed, ctx) => {
|
|
72
|
+
const skill = byName.get(parsed.name);
|
|
73
|
+
if (skill === undefined) {
|
|
74
|
+
return errorResult(`Unknown Skill: ${parsed.name}`);
|
|
75
|
+
}
|
|
76
|
+
const disclosure = parsed.path === undefined
|
|
77
|
+
? await discloseSkill(skill)
|
|
78
|
+
: await discloseBundledFile(skill, parsed.path);
|
|
79
|
+
if (ctx.signal.aborted) {
|
|
80
|
+
return errorResult(`${READ_SKILL_TOOL} was aborted`);
|
|
81
|
+
}
|
|
82
|
+
return toolResult(disclosure);
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
catalogPrompt: () => catalog,
|
|
86
|
+
close: async () => {
|
|
87
|
+
// The provider holds no open resources.
|
|
88
|
+
},
|
|
89
|
+
execute: async (name, toolInput, ctx) => {
|
|
90
|
+
if (name !== READ_SKILL_TOOL || byName.size === 0) {
|
|
91
|
+
return errorResult(`Unknown Tool: ${name}`);
|
|
92
|
+
}
|
|
93
|
+
if (ctx.signal.aborted) {
|
|
94
|
+
return errorResult(`${READ_SKILL_TOOL} was aborted`);
|
|
95
|
+
}
|
|
96
|
+
const decoded = decode(ReadSkillInputSchema, toolInput);
|
|
97
|
+
if (!decoded.ok) {
|
|
98
|
+
const issues = decoded.issues.map((issue) => `${issue.path} ${issue.message}`);
|
|
99
|
+
return errorResult(`Invalid input for ${READ_SKILL_TOOL}: ${issues.join("; ")}`);
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
return await readSkill(decoded.value, ctx);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
return errorResult(messageOf(error));
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
health: () => [],
|
|
109
|
+
skills: () => infos,
|
|
110
|
+
tools: () => tools,
|
|
111
|
+
warnings: () => discovery.warnings,
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
/** The SQLite migrations of the Skills module. Skills live on disk, so the module has no tables. */
|
|
115
|
+
export const skillsMigrations = [];
|
|
116
|
+
/**
|
|
117
|
+
* The JSON-RPC handlers of the Skills module. `extensions.list` reports Skills through
|
|
118
|
+
* `AgentExtensions.skills`, so the module handles no method itself.
|
|
119
|
+
*/
|
|
120
|
+
export const skillsRpcHandlers = {};
|
|
121
|
+
/**
|
|
122
|
+
* The Skills module for Wave 2 wiring. Its API is the Agent's `SkillProvider`. `create` discovers the Skills, so the
|
|
123
|
+
* module needs no `start`. `stop` closes the provider.
|
|
124
|
+
*/
|
|
125
|
+
export const skillsModule = {
|
|
126
|
+
create: async (ctx, deps) => {
|
|
127
|
+
const provider = await createSkillProvider({
|
|
128
|
+
agentDir: ctx.agentDir,
|
|
129
|
+
config: deps.config,
|
|
130
|
+
homeDir: deps.homeDir,
|
|
131
|
+
log: ctx.log,
|
|
132
|
+
// AgentModuleContext has no abort signal, so discovery from this path cannot be cancelled.
|
|
133
|
+
signal: new AbortController().signal,
|
|
134
|
+
});
|
|
135
|
+
return { api: provider, rpc: skillsRpcHandlers, stop: provider.close };
|
|
136
|
+
},
|
|
137
|
+
migrations: skillsMigrations,
|
|
138
|
+
name: "skills",
|
|
139
|
+
scope: "agent",
|
|
140
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { canonicalToolName } from "../contracts/tool-names.js";
|
|
2
|
+
import { errorMessage } from "../core/error-message.js";
|
|
3
|
+
import { readFileTool } from "./read-file.js";
|
|
4
|
+
import { shellTool } from "./shell.js";
|
|
5
|
+
import { writeFileTool } from "./write-file.js";
|
|
6
|
+
/**
|
|
7
|
+
* The built-in Tools and the action of each when no Permission Policy rule matches. Reading is safe. Writing files and
|
|
8
|
+
* running commands change the host, so they ask first (ADR 0011).
|
|
9
|
+
*/
|
|
10
|
+
export const BUILTIN_TOOLS = [
|
|
11
|
+
{ defaultAction: "allow", tool: readFileTool },
|
|
12
|
+
{ defaultAction: "ask", tool: writeFileTool },
|
|
13
|
+
{ defaultAction: "ask", tool: shellTool },
|
|
14
|
+
];
|
|
15
|
+
/** Returns the default action of a built-in Tool, or `undefined` for any other Tool. */
|
|
16
|
+
export const builtinDefaultAction = (toolName) => BUILTIN_TOOLS.find(({ tool }) => tool.name === toolName)?.defaultAction;
|
|
17
|
+
const errorResult = (message) => ({
|
|
18
|
+
content: [{ text: message, type: "text" }],
|
|
19
|
+
isError: true,
|
|
20
|
+
});
|
|
21
|
+
const plainSchema = (tool) => "~standard" in tool.inputSchema ? { type: "object" } : tool.inputSchema;
|
|
22
|
+
/**
|
|
23
|
+
* Creates the `ToolProvider` of the built-in Tools `read_file`, `write_file`, and `shell`. Each Agent gets its own
|
|
24
|
+
* instance. The provider holds no resources, so `close` only blocks later calls.
|
|
25
|
+
*/
|
|
26
|
+
export const createBuiltinToolProvider = () => {
|
|
27
|
+
const tools = new Map(BUILTIN_TOOLS.map((entry) => [
|
|
28
|
+
canonicalToolName({ kind: "builtin" }, entry.tool.name),
|
|
29
|
+
entry,
|
|
30
|
+
]));
|
|
31
|
+
let closed = false;
|
|
32
|
+
return {
|
|
33
|
+
close: async () => {
|
|
34
|
+
closed = true;
|
|
35
|
+
await Promise.resolve();
|
|
36
|
+
},
|
|
37
|
+
execute: async (name, input, ctx) => {
|
|
38
|
+
const entry = tools.get(name);
|
|
39
|
+
if (entry === undefined || closed) {
|
|
40
|
+
return errorResult(`Unknown Tool: ${name}`);
|
|
41
|
+
}
|
|
42
|
+
if (ctx.signal.aborted) {
|
|
43
|
+
return errorResult(`${name} was stopped before it started`);
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
return await entry.tool.execute(input, ctx);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
return errorResult(errorMessage(error));
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
health: () => [],
|
|
53
|
+
tools: () => [...tools.entries()].map(([name, entry]) => ({
|
|
54
|
+
defaultAction: entry.defaultAction,
|
|
55
|
+
description: entry.tool.description,
|
|
56
|
+
inputSchema: plainSchema(entry.tool),
|
|
57
|
+
name,
|
|
58
|
+
source: "builtin",
|
|
59
|
+
})),
|
|
60
|
+
};
|
|
61
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2
|
+
const isString = (value) => typeof value === "string";
|
|
3
|
+
const isNonNegativeInteger = (value) => typeof value === "number" && Number.isInteger(value) && value >= 0;
|
|
4
|
+
const fieldMatches = (rule, value) => {
|
|
5
|
+
if (rule === "string") {
|
|
6
|
+
return isString(value);
|
|
7
|
+
}
|
|
8
|
+
if (value === undefined) {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
return rule === "optional-string"
|
|
12
|
+
? isString(value)
|
|
13
|
+
: isNonNegativeInteger(value);
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Checks that `input` is an object with exactly the fields of `rules`. Returns the object, or a message that names the
|
|
17
|
+
* first field that breaks its rule.
|
|
18
|
+
*/
|
|
19
|
+
export const checkInput = (input, rules) => {
|
|
20
|
+
if (!isObject(input)) {
|
|
21
|
+
return { message: "The input must be a JSON object", ok: false };
|
|
22
|
+
}
|
|
23
|
+
for (const key of Object.keys(input)) {
|
|
24
|
+
if (!Object.hasOwn(rules, key)) {
|
|
25
|
+
return { message: `Unknown input field: ${key}`, ok: false };
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
for (const [key, rule] of Object.entries(rules)) {
|
|
29
|
+
if (!fieldMatches(rule, input[key])) {
|
|
30
|
+
const expected = rule === "optional-integer" ? "a non-negative integer" : "a string";
|
|
31
|
+
return { message: `The field ${key} must be ${expected}`, ok: false };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { ok: true, value: input };
|
|
35
|
+
};
|
|
36
|
+
/** Reads a string field that `checkInput` verified. */
|
|
37
|
+
export const stringField = (input, key) => {
|
|
38
|
+
const value = input[key];
|
|
39
|
+
return isString(value) ? value : undefined;
|
|
40
|
+
};
|
|
41
|
+
/** Reads an integer field that `checkInput` verified. */
|
|
42
|
+
export const integerField = (input, key) => {
|
|
43
|
+
const value = input[key];
|
|
44
|
+
return isNonNegativeInteger(value) ? value : undefined;
|
|
45
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
/**
|
|
3
|
+
* Resolves a Tool path against the Agent's workspace. A relative path resolves inside `workspaceDir`, and an absolute
|
|
4
|
+
* path stays as it is. The Permission Policy, not the path, limits what a Tool may touch (ADR 0011).
|
|
5
|
+
*/
|
|
6
|
+
export const resolveToolPath = (workspaceDir, target) => path.resolve(workspaceDir, target);
|