@useavalon/caelence-agent 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/README.md +201 -0
- package/bin/harness.ts +10 -0
- package/package.json +75 -0
- package/skills/copy-editor/SKILL.md +74 -0
- package/skills/copy-editor/references/anti-slop.md +62 -0
- package/skills/copy-editor/references/ui-states.md +46 -0
- package/skills/copy-editor/scripts/lint-copy.py +78 -0
- package/skills/copy-harry-dry/SKILL.md +120 -0
- package/skills/copy-rmbc/SKILL.md +97 -0
- package/skills/copywriting/SKILL.md +73 -0
- package/skills/impeccable/SKILL.md +133 -0
- package/skills/impeccable/references/visual-slop.md +36 -0
- package/src/cli/main.test.ts +36 -0
- package/src/cli/main.ts +384 -0
- package/src/cli/media-models.ts +82 -0
- package/src/cli/models.ts +43 -0
- package/src/cli/openrouter-live.test.ts +35 -0
- package/src/cli/openrouter-live.ts +72 -0
- package/src/cli/openrouter-modality.test.ts +25 -0
- package/src/cli/openrouter-modality.ts +83 -0
- package/src/cli/picker.test.ts +53 -0
- package/src/cli/picker.ts +48 -0
- package/src/cli/plain-text.ts +15 -0
- package/src/cli/slash-dispatch.test.ts +213 -0
- package/src/cli/slash-dispatch.ts +298 -0
- package/src/cli/slash.test.ts +113 -0
- package/src/cli/slash.ts +119 -0
- package/src/cli/theme.test.ts +10 -0
- package/src/cli/theme.ts +44 -0
- package/src/cli/tui-layout.test.ts +33 -0
- package/src/cli/tui-layout.ts +33 -0
- package/src/cli/tui-screen.test.ts +33 -0
- package/src/cli/tui-screen.ts +13 -0
- package/src/cli/tui.tsx +735 -0
- package/src/config.ts +86 -0
- package/src/core/approval.test.ts +60 -0
- package/src/core/approval.ts +83 -0
- package/src/core/auto-router.test.ts +31 -0
- package/src/core/auto-router.ts +66 -0
- package/src/core/errors.ts +67 -0
- package/src/core/events.ts +85 -0
- package/src/core/hooks.test.ts +66 -0
- package/src/core/hooks.ts +94 -0
- package/src/core/ids.ts +6 -0
- package/src/core/instructions.test.ts +49 -0
- package/src/core/instructions.ts +44 -0
- package/src/core/logger.ts +45 -0
- package/src/core/mcp.ts +191 -0
- package/src/core/mode.test.ts +63 -0
- package/src/core/mode.ts +60 -0
- package/src/core/run-turn.test.ts +339 -0
- package/src/core/run-turn.ts +487 -0
- package/src/core/session.test.ts +203 -0
- package/src/core/session.ts +349 -0
- package/src/core/stdio-mcp.ts +143 -0
- package/src/core/strands.ts +262 -0
- package/src/core/usage.test.ts +69 -0
- package/src/core/usage.ts +171 -0
- package/src/desktop/bridge.test.ts +331 -0
- package/src/desktop/bridge.ts +729 -0
- package/src/desktop/open-local.test.ts +40 -0
- package/src/desktop/open-local.ts +52 -0
- package/src/desktop/preview-cache.test.ts +36 -0
- package/src/desktop/preview-cache.ts +63 -0
- package/src/desktop/secrets.test.ts +44 -0
- package/src/desktop/secrets.ts +60 -0
- package/src/evals/runner.test.ts +43 -0
- package/src/evals/runner.ts +225 -0
- package/src/index.ts +132 -0
- package/src/init.test.ts +54 -0
- package/src/init.ts +117 -0
- package/src/integrations/catalog-data.json +424 -0
- package/src/integrations/catalog.ts +81 -0
- package/src/integrations/connect.ts +231 -0
- package/src/integrations/integrations.test.ts +329 -0
- package/src/integrations/marks/airtable.svg +1 -0
- package/src/integrations/marks/algolia-mcp.svg +1 -0
- package/src/integrations/marks/canva.jpg +0 -0
- package/src/integrations/marks/cloudflare-mcp.svg +1 -0
- package/src/integrations/marks/contentful-mcp.svg +1 -0
- package/src/integrations/marks/craft.png +0 -0
- package/src/integrations/marks/dinero.png +0 -0
- package/src/integrations/marks/figma.svg +1 -0
- package/src/integrations/marks/gitlab.svg +1 -0
- package/src/integrations/marks/google-calendar.svg +1 -0
- package/src/integrations/marks/google-docs.svg +1 -0
- package/src/integrations/marks/google-drive.svg +1 -0
- package/src/integrations/marks/google-gmail.svg +1 -0
- package/src/integrations/marks/google-sheets.svg +1 -0
- package/src/integrations/marks/google-slides.svg +1 -0
- package/src/integrations/marks/granola.png +0 -0
- package/src/integrations/marks/jira.svg +1 -0
- package/src/integrations/marks/linear.svg +1 -0
- package/src/integrations/marks/microsoft-365.svg +7 -0
- package/src/integrations/marks/miro.svg +1 -0
- package/src/integrations/marks/mixpanel-mcp.svg +1 -0
- package/src/integrations/marks/monday.png +0 -0
- package/src/integrations/marks/mux-mcp.png +0 -0
- package/src/integrations/marks/neon-mcp.svg +1 -0
- package/src/integrations/marks/notion.svg +1 -0
- package/src/integrations/marks/paddle-mcp.svg +1 -0
- package/src/integrations/marks/posthog-mcp.svg +1 -0
- package/src/integrations/marks/prismic-mcp.svg +1 -0
- package/src/integrations/marks/replicate-mcp.svg +1 -0
- package/src/integrations/marks/resend-mcp.svg +1 -0
- package/src/integrations/marks/sanity-mcp.svg +1 -0
- package/src/integrations/marks/sceneplane.png +0 -0
- package/src/integrations/marks/sentry-mcp.svg +1 -0
- package/src/integrations/marks/stripe-mcp.svg +1 -0
- package/src/integrations/marks/supabase-mcp.svg +1 -0
- package/src/integrations/marks/upstash-mcp.svg +1 -0
- package/src/integrations/marks/vercel.svg +1 -0
- package/src/integrations/marks/webflow.svg +1 -0
- package/src/integrations/mcp-oauth.test.ts +39 -0
- package/src/integrations/mcp-oauth.ts +330 -0
- package/src/integrations/oauth-clients.test.ts +77 -0
- package/src/integrations/oauth-clients.ts +127 -0
- package/src/integrations/oauth.ts +145 -0
- package/src/integrations/remote-mcp.ts +307 -0
- package/src/integrations/store.ts +149 -0
- package/src/media/openrouter-generate.test.ts +80 -0
- package/src/media/openrouter-generate.ts +178 -0
- package/src/media/prefs.ts +54 -0
- package/src/observability/categories.ts +41 -0
- package/src/observability/create.test.ts +56 -0
- package/src/observability/create.ts +45 -0
- package/src/observability/experiments.ts +113 -0
- package/src/observability/fake.ts +191 -0
- package/src/observability/index.ts +57 -0
- package/src/observability/judges.ts +91 -0
- package/src/observability/langfuse.ts +275 -0
- package/src/observability/noop.ts +33 -0
- package/src/observability/observability.test.ts +113 -0
- package/src/observability/prompts.ts +98 -0
- package/src/observability/regression-items.ts +158 -0
- package/src/observability/sanitize.test.ts +26 -0
- package/src/observability/sanitize.ts +57 -0
- package/src/observability/types.ts +122 -0
- package/src/observability/version.ts +41 -0
- package/src/product.ts +2 -0
- package/src/runtime.test.ts +98 -0
- package/src/runtime.ts +438 -0
- package/src/skills/author-skill.ts +49 -0
- package/src/skills/create-skill.test.ts +108 -0
- package/src/skills/create-skill.ts +70 -0
- package/src/skills/loader.test.ts +122 -0
- package/src/skills/loader.ts +162 -0
- package/src/skills/registry.test.ts +190 -0
- package/src/skills/registry.ts +402 -0
- package/src/skills/skill-command.ts +53 -0
- package/src/skills/user-dir.ts +8 -0
- package/src/tools/git.test.ts +71 -0
- package/src/tools/git.ts +126 -0
- package/src/tools/local.ts +258 -0
- package/src/tools/paths.test.ts +24 -0
- package/src/tools/paths.ts +39 -0
- package/src/tools/read-skill.ts +43 -0
- package/src/tools/subagent.test.ts +77 -0
- package/src/tools/subagent.ts +74 -0
- package/src/tools/todo.test.ts +25 -0
- package/src/tools/todo.ts +70 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { Glob } from "bun";
|
|
4
|
+
import {
|
|
5
|
+
createMcpServer,
|
|
6
|
+
errorResult,
|
|
7
|
+
jsonResult,
|
|
8
|
+
type McpServer,
|
|
9
|
+
type McpToolDefinition,
|
|
10
|
+
textResult,
|
|
11
|
+
} from "../core/mcp.ts";
|
|
12
|
+
import { PathEscapeError, resolveUnderCwd, shouldSkipDir, toProjectRel } from "./paths.ts";
|
|
13
|
+
|
|
14
|
+
const MAX_FILE_BYTES = 10 * 1024 * 1024;
|
|
15
|
+
const EXEC_TIMEOUT_MS = 120_000;
|
|
16
|
+
|
|
17
|
+
function asString(value: unknown): string {
|
|
18
|
+
return typeof value === "string" ? value : "";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function readUtf8(abs: string): Promise<string> {
|
|
22
|
+
const info = await stat(abs);
|
|
23
|
+
if (!info.isFile()) throw new Error(`Not a file: ${abs}`);
|
|
24
|
+
if (info.size > MAX_FILE_BYTES) throw new Error(`File exceeds ${MAX_FILE_BYTES} bytes`);
|
|
25
|
+
return readFile(abs, "utf8");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface LocalToolsOptions {
|
|
29
|
+
cwd: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function createLocalTools(options: LocalToolsOptions): McpServer {
|
|
33
|
+
const cwd = options.cwd;
|
|
34
|
+
|
|
35
|
+
const tools: McpToolDefinition[] = [
|
|
36
|
+
{
|
|
37
|
+
name: "read_file",
|
|
38
|
+
description:
|
|
39
|
+
"Read a UTF-8 file under the project root. Prefer this over exec for inspecting source.",
|
|
40
|
+
inputSchema: {
|
|
41
|
+
type: "object",
|
|
42
|
+
properties: {
|
|
43
|
+
path: { type: "string", description: "Project-relative file path." },
|
|
44
|
+
},
|
|
45
|
+
required: ["path"],
|
|
46
|
+
},
|
|
47
|
+
async handler(raw) {
|
|
48
|
+
try {
|
|
49
|
+
const abs = resolveUnderCwd(cwd, asString(raw.path));
|
|
50
|
+
const content = await readUtf8(abs);
|
|
51
|
+
return textResult(content);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
return errorResult(err instanceof Error ? err.message : String(err));
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: "write_file",
|
|
59
|
+
description:
|
|
60
|
+
"Create or overwrite a whole file under the project root (up to 10 MB). Prefer edit_file for a small change.",
|
|
61
|
+
inputSchema: {
|
|
62
|
+
type: "object",
|
|
63
|
+
properties: {
|
|
64
|
+
path: { type: "string", description: "Project-relative file path." },
|
|
65
|
+
content: { type: "string", description: "UTF-8 file contents." },
|
|
66
|
+
},
|
|
67
|
+
required: ["path", "content"],
|
|
68
|
+
},
|
|
69
|
+
async handler(raw) {
|
|
70
|
+
try {
|
|
71
|
+
const abs = resolveUnderCwd(cwd, asString(raw.path));
|
|
72
|
+
const content = asString(raw.content);
|
|
73
|
+
if (Buffer.byteLength(content, "utf8") > MAX_FILE_BYTES) {
|
|
74
|
+
return errorResult(`File exceeds ${MAX_FILE_BYTES} bytes`);
|
|
75
|
+
}
|
|
76
|
+
await mkdir(dirname(abs), { recursive: true });
|
|
77
|
+
await writeFile(abs, content, "utf8");
|
|
78
|
+
return jsonResult({
|
|
79
|
+
path: toProjectRel(cwd, abs),
|
|
80
|
+
bytes: Buffer.byteLength(content, "utf8"),
|
|
81
|
+
});
|
|
82
|
+
} catch (err) {
|
|
83
|
+
return errorResult(err instanceof Error ? err.message : String(err));
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
name: "edit_file",
|
|
89
|
+
description:
|
|
90
|
+
"Replace an exact snippet in an existing file. old_str must match uniquely unless replace_all is true.",
|
|
91
|
+
inputSchema: {
|
|
92
|
+
type: "object",
|
|
93
|
+
properties: {
|
|
94
|
+
path: { type: "string", description: "Project-relative path of an existing file." },
|
|
95
|
+
old_str: { type: "string", description: "Exact snippet to replace." },
|
|
96
|
+
new_str: { type: "string", description: "Replacement snippet." },
|
|
97
|
+
replace_all: {
|
|
98
|
+
type: "boolean",
|
|
99
|
+
description: "Replace every occurrence (default false).",
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
required: ["path", "old_str", "new_str"],
|
|
103
|
+
},
|
|
104
|
+
async handler(raw) {
|
|
105
|
+
try {
|
|
106
|
+
const abs = resolveUnderCwd(cwd, asString(raw.path));
|
|
107
|
+
const oldStr = asString(raw.old_str);
|
|
108
|
+
const newStr = asString(raw.new_str);
|
|
109
|
+
if (!oldStr) return errorResult("old_str is empty");
|
|
110
|
+
const original = await readUtf8(abs);
|
|
111
|
+
const replaceAll = raw.replace_all === true;
|
|
112
|
+
if (!original.includes(oldStr)) {
|
|
113
|
+
return errorResult("old_str was not found in the file");
|
|
114
|
+
}
|
|
115
|
+
if (!replaceAll) {
|
|
116
|
+
const first = original.indexOf(oldStr);
|
|
117
|
+
const second = original.indexOf(oldStr, first + oldStr.length);
|
|
118
|
+
if (second >= 0) {
|
|
119
|
+
return errorResult(
|
|
120
|
+
"old_str matches more than once; pass replace_all or a unique snippet",
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const next = replaceAll
|
|
125
|
+
? original.split(oldStr).join(newStr)
|
|
126
|
+
: original.replace(oldStr, newStr);
|
|
127
|
+
await writeFile(abs, next, "utf8");
|
|
128
|
+
return jsonResult({ path: toProjectRel(cwd, abs), replaced: replaceAll ? "all" : "one" });
|
|
129
|
+
} catch (err) {
|
|
130
|
+
return errorResult(err instanceof Error ? err.message : String(err));
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
name: "glob",
|
|
136
|
+
description: "List project-relative file paths matching a glob pattern (e.g. **/*.ts).",
|
|
137
|
+
inputSchema: {
|
|
138
|
+
type: "object",
|
|
139
|
+
properties: {
|
|
140
|
+
pattern: { type: "string", description: "Glob pattern relative to the project root." },
|
|
141
|
+
},
|
|
142
|
+
required: ["pattern"],
|
|
143
|
+
},
|
|
144
|
+
async handler(raw) {
|
|
145
|
+
try {
|
|
146
|
+
const pattern = asString(raw.pattern) || "**/*";
|
|
147
|
+
const glob = new Glob(pattern);
|
|
148
|
+
const matches: string[] = [];
|
|
149
|
+
for await (const path of glob.scan({ cwd, onlyFiles: true, dot: false })) {
|
|
150
|
+
const parts = path.split("/");
|
|
151
|
+
if (parts.some((p) => shouldSkipDir(p))) continue;
|
|
152
|
+
matches.push(path);
|
|
153
|
+
if (matches.length >= 500) break;
|
|
154
|
+
}
|
|
155
|
+
matches.sort();
|
|
156
|
+
return jsonResult({ pattern, matches, truncated: matches.length >= 500 });
|
|
157
|
+
} catch (err) {
|
|
158
|
+
return errorResult(err instanceof Error ? err.message : String(err));
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
name: "grep",
|
|
164
|
+
description: "Search file contents under the project root. Prefer this over exec for search.",
|
|
165
|
+
inputSchema: {
|
|
166
|
+
type: "object",
|
|
167
|
+
properties: {
|
|
168
|
+
pattern: { type: "string", description: "Literal substring or regex." },
|
|
169
|
+
glob: { type: "string", description: "Optional file glob (default **/*)." },
|
|
170
|
+
regex: { type: "boolean", description: "Treat pattern as a regular expression." },
|
|
171
|
+
max_matches: { type: "number", description: "Cap on matches (default 100)." },
|
|
172
|
+
},
|
|
173
|
+
required: ["pattern"],
|
|
174
|
+
},
|
|
175
|
+
async handler(raw) {
|
|
176
|
+
try {
|
|
177
|
+
const pattern = asString(raw.pattern);
|
|
178
|
+
if (!pattern) return errorResult("pattern is empty");
|
|
179
|
+
const fileGlob = asString(raw.glob) || "**/*";
|
|
180
|
+
const maxMatches = typeof raw.max_matches === "number" ? raw.max_matches : 100;
|
|
181
|
+
const useRegex = raw.regex === true;
|
|
182
|
+
const re = useRegex ? new RegExp(pattern) : null;
|
|
183
|
+
const hits: Array<{ path: string; line: number; text: string }> = [];
|
|
184
|
+
const glob = new Glob(fileGlob);
|
|
185
|
+
for await (const rel of glob.scan({ cwd, onlyFiles: true, dot: false })) {
|
|
186
|
+
const parts = rel.split("/");
|
|
187
|
+
if (parts.some((p) => shouldSkipDir(p))) continue;
|
|
188
|
+
let content: string;
|
|
189
|
+
try {
|
|
190
|
+
content = await readFile(join(cwd, rel), "utf8");
|
|
191
|
+
} catch {
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
const lines = content.split("\n");
|
|
195
|
+
for (let i = 0; i < lines.length; i++) {
|
|
196
|
+
const line = lines[i] ?? "";
|
|
197
|
+
const ok = re ? re.test(line) : line.includes(pattern);
|
|
198
|
+
if (!ok) continue;
|
|
199
|
+
hits.push({ path: rel, line: i + 1, text: line.slice(0, 240) });
|
|
200
|
+
if (hits.length >= maxMatches) {
|
|
201
|
+
return jsonResult({ pattern, hits, truncated: true });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return jsonResult({ pattern, hits, truncated: false });
|
|
206
|
+
} catch (err) {
|
|
207
|
+
return errorResult(err instanceof Error ? err.message : String(err));
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
name: "exec",
|
|
213
|
+
description:
|
|
214
|
+
"Run a shell command in the project root. Times out after 120 seconds. Do not use exec to read or search files.",
|
|
215
|
+
inputSchema: {
|
|
216
|
+
type: "object",
|
|
217
|
+
properties: {
|
|
218
|
+
command: { type: "string", description: "The shell command to run." },
|
|
219
|
+
cwd: { type: "string", description: "Optional project-relative working directory." },
|
|
220
|
+
},
|
|
221
|
+
required: ["command"],
|
|
222
|
+
},
|
|
223
|
+
async handler(raw) {
|
|
224
|
+
try {
|
|
225
|
+
const command = asString(raw.command);
|
|
226
|
+
if (!command.trim()) return errorResult("command is empty");
|
|
227
|
+
let workdir = cwd;
|
|
228
|
+
if (typeof raw.cwd === "string" && raw.cwd.trim()) {
|
|
229
|
+
workdir = resolveUnderCwd(cwd, raw.cwd);
|
|
230
|
+
}
|
|
231
|
+
const proc = Bun.spawn(["/bin/sh", "-lc", command], {
|
|
232
|
+
cwd: workdir,
|
|
233
|
+
stdout: "pipe",
|
|
234
|
+
stderr: "pipe",
|
|
235
|
+
});
|
|
236
|
+
const timer = setTimeout(() => proc.kill(), EXEC_TIMEOUT_MS);
|
|
237
|
+
const [stdout, stderr, exit] = await Promise.all([
|
|
238
|
+
new Response(proc.stdout).text(),
|
|
239
|
+
new Response(proc.stderr).text(),
|
|
240
|
+
proc.exited,
|
|
241
|
+
]);
|
|
242
|
+
clearTimeout(timer);
|
|
243
|
+
const payload = {
|
|
244
|
+
exit,
|
|
245
|
+
stdout: stdout.slice(0, 32_000),
|
|
246
|
+
stderr: stderr.slice(0, 32_000),
|
|
247
|
+
};
|
|
248
|
+
return jsonResult(payload, exit !== 0);
|
|
249
|
+
} catch (err) {
|
|
250
|
+
if (err instanceof PathEscapeError) return errorResult(err.message);
|
|
251
|
+
return errorResult(err instanceof Error ? err.message : String(err));
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
];
|
|
256
|
+
|
|
257
|
+
return createMcpServer(tools);
|
|
258
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { PathEscapeError, resolveUnderCwd } from "./paths.ts";
|
|
4
|
+
|
|
5
|
+
describe("resolveUnderCwd", () => {
|
|
6
|
+
const cwd = resolve("/tmp/project");
|
|
7
|
+
|
|
8
|
+
test("resolves a relative file", () => {
|
|
9
|
+
expect(resolveUnderCwd(cwd, "src/index.ts")).toBe(resolve(cwd, "src/index.ts"));
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("rejects absolute paths", () => {
|
|
13
|
+
expect(() => resolveUnderCwd(cwd, "/etc/passwd")).toThrow(PathEscapeError);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("rejects parent traversal", () => {
|
|
17
|
+
expect(() => resolveUnderCwd(cwd, "../secret")).toThrow(PathEscapeError);
|
|
18
|
+
expect(() => resolveUnderCwd(cwd, "foo/../../etc/passwd")).toThrow(PathEscapeError);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("allows dot segments that stay inside", () => {
|
|
22
|
+
expect(resolveUnderCwd(cwd, "src/./lib.ts")).toBe(resolve(cwd, "src/lib.ts"));
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { isAbsolute, relative, resolve, sep } from "node:path";
|
|
2
|
+
|
|
3
|
+
export class PathEscapeError extends Error {
|
|
4
|
+
constructor(rel: string) {
|
|
5
|
+
super(`Path escapes project root: ${rel}`);
|
|
6
|
+
this.name = "PathEscapeError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Resolve `rel` under `cwd`. Rejects absolute paths and `..` that leave the root. */
|
|
11
|
+
export function resolveUnderCwd(cwd: string, rel: string): string {
|
|
12
|
+
if (!rel || rel.trim() === "") throw new PathEscapeError(rel);
|
|
13
|
+
if (isAbsolute(rel)) throw new PathEscapeError(rel);
|
|
14
|
+
const root = resolve(cwd);
|
|
15
|
+
const resolved = resolve(root, rel);
|
|
16
|
+
const relToRoot = relative(root, resolved);
|
|
17
|
+
if (relToRoot.startsWith("..") || isAbsolute(relToRoot)) {
|
|
18
|
+
throw new PathEscapeError(rel);
|
|
19
|
+
}
|
|
20
|
+
return resolved;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function toProjectRel(cwd: string, abs: string): string {
|
|
24
|
+
const rel = relative(resolve(cwd), abs);
|
|
25
|
+
return rel.split(sep).join("/");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const SKIP_DIR_NAMES = new Set([
|
|
29
|
+
"node_modules",
|
|
30
|
+
".git",
|
|
31
|
+
".harness",
|
|
32
|
+
"dist",
|
|
33
|
+
".output",
|
|
34
|
+
".bun",
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
export function shouldSkipDir(name: string): boolean {
|
|
38
|
+
return SKIP_DIR_NAMES.has(name);
|
|
39
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { errorResult, type McpToolDefinition, textResult } from "../core/mcp.ts";
|
|
4
|
+
import type { Skill } from "../skills/loader.ts";
|
|
5
|
+
import { PathEscapeError, resolveUnderCwd } from "./paths.ts";
|
|
6
|
+
|
|
7
|
+
export function createReadSkillTool(skills: Skill[]): McpToolDefinition {
|
|
8
|
+
const byName = new Map(skills.map((s) => [s.name.toLowerCase(), s]));
|
|
9
|
+
return {
|
|
10
|
+
name: "read_skill",
|
|
11
|
+
description:
|
|
12
|
+
"Read a loaded skill by name (user or project). Optional `file` reads a reference next to that skill (e.g. references/anti-slop.md). Prefer this over guessing skill contents.",
|
|
13
|
+
inputSchema: {
|
|
14
|
+
type: "object",
|
|
15
|
+
properties: {
|
|
16
|
+
name: { type: "string", description: "Skill name from the catalog (e.g. copywriting)." },
|
|
17
|
+
file: {
|
|
18
|
+
type: "string",
|
|
19
|
+
description: "Optional path relative to that skill's folder.",
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
required: ["name"],
|
|
23
|
+
},
|
|
24
|
+
async handler(raw) {
|
|
25
|
+
const name = typeof raw.name === "string" ? raw.name.trim() : "";
|
|
26
|
+
if (!name) return errorResult("name is empty");
|
|
27
|
+
const skill = byName.get(name.toLowerCase());
|
|
28
|
+
if (!skill) {
|
|
29
|
+
const known = [...byName.keys()].sort().join(", ") || "(none)";
|
|
30
|
+
return errorResult(`Unknown skill ${JSON.stringify(name)}. Loaded: ${known}`);
|
|
31
|
+
}
|
|
32
|
+
const file = typeof raw.file === "string" ? raw.file.trim() : "";
|
|
33
|
+
if (!file) return textResult(`# ${skill.name}\n\n${skill.body}`);
|
|
34
|
+
try {
|
|
35
|
+
const abs = resolveUnderCwd(dirname(skill.path), file);
|
|
36
|
+
return textResult(readFileSync(abs, "utf8"));
|
|
37
|
+
} catch (err) {
|
|
38
|
+
if (err instanceof PathEscapeError) return errorResult(err.message);
|
|
39
|
+
return errorResult(err instanceof Error ? err.message : String(err));
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { DEFAULT_CONFIG } from "../config.ts";
|
|
6
|
+
import type { AgentEvent, MainModelProvider } from "../core/events.ts";
|
|
7
|
+
import type { ConstructAgent } from "../core/strands.ts";
|
|
8
|
+
import { createHarness } from "../runtime.ts";
|
|
9
|
+
import { listBundledSkills } from "../skills/loader.ts";
|
|
10
|
+
import { createReadSkillTool } from "./read-skill.ts";
|
|
11
|
+
|
|
12
|
+
const fakeProvider: MainModelProvider = {
|
|
13
|
+
kind: "fake",
|
|
14
|
+
modelId: "fake",
|
|
15
|
+
async *stream() {
|
|
16
|
+
yield { type: "done" };
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
describe("read_skill", () => {
|
|
21
|
+
test("reads a catalog skill and a reference file", async () => {
|
|
22
|
+
const skills = listBundledSkills();
|
|
23
|
+
const tool = createReadSkillTool(skills);
|
|
24
|
+
const copy = await tool.handler({ name: "copywriting" });
|
|
25
|
+
expect(copy.isError).toBeFalsy();
|
|
26
|
+
expect(copy.content[0]?.text).toContain("RMBC");
|
|
27
|
+
const slop = await tool.handler({ name: "copy-editor", file: "references/anti-slop.md" });
|
|
28
|
+
expect(slop.isError).toBeFalsy();
|
|
29
|
+
expect(slop.content[0]?.text).toContain("Never write");
|
|
30
|
+
const visual = await tool.handler({ name: "impeccable", file: "references/visual-slop.md" });
|
|
31
|
+
expect(visual.isError).toBeFalsy();
|
|
32
|
+
expect(visual.content[0]?.text).toContain("Kickers");
|
|
33
|
+
const missing = await tool.handler({ name: "not-a-skill" });
|
|
34
|
+
expect(missing.isError).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("task subagent", () => {
|
|
39
|
+
test("nested agent can write a file and cannot spawn task", async () => {
|
|
40
|
+
const cwd = await mkdtemp(join(tmpdir(), "harness-sub-"));
|
|
41
|
+
try {
|
|
42
|
+
let nestedSawTask = false;
|
|
43
|
+
const constructAgent: ConstructAgent = (input) => ({
|
|
44
|
+
async run(_message, emit) {
|
|
45
|
+
const task = input.tools.find((t) => t.name === "task");
|
|
46
|
+
if (task) {
|
|
47
|
+
await task.invoke({ prompt: "write from-sub.txt", label: "writer" });
|
|
48
|
+
emit({ kind: "text_delta", text: "parent" });
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
nestedSawTask = input.tools.some((t) => t.name === "task");
|
|
52
|
+
const write = input.tools.find((t) => t.name === "write_file");
|
|
53
|
+
if (!write) throw new Error("write_file missing");
|
|
54
|
+
await write.invoke({ path: "from-sub.txt", content: "ok" });
|
|
55
|
+
emit({ kind: "text_delta", text: "sub" });
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
const harness = await createHarness({
|
|
59
|
+
cwd,
|
|
60
|
+
config: { ...DEFAULT_CONFIG, tools: { exec: { approval: "deny" } } },
|
|
61
|
+
provider: fakeProvider,
|
|
62
|
+
constructAgent,
|
|
63
|
+
approvalPolicy: "deny",
|
|
64
|
+
});
|
|
65
|
+
const kinds: AgentEvent["kind"][] = [];
|
|
66
|
+
await harness.runTurn("delegate", (event) => {
|
|
67
|
+
kinds.push(event.kind);
|
|
68
|
+
});
|
|
69
|
+
expect(nestedSawTask).toBe(false);
|
|
70
|
+
expect(kinds).toContain("tool_call_start");
|
|
71
|
+
expect(await readFile(join(cwd, "from-sub.txt"), "utf8")).toBe("ok");
|
|
72
|
+
harness.close();
|
|
73
|
+
} finally {
|
|
74
|
+
await rm(cwd, { recursive: true, force: true });
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { AgentEventEmitter } from "../core/events.ts";
|
|
2
|
+
import { errorResult, type McpToolDefinition, textResult } from "../core/mcp.ts";
|
|
3
|
+
import { type Skill, skillBodiesForNames } from "../skills/loader.ts";
|
|
4
|
+
|
|
5
|
+
export interface TaskToolOptions {
|
|
6
|
+
skills: Skill[];
|
|
7
|
+
/** Current parent-turn emitter (nested tool cards, approval). */
|
|
8
|
+
parentEmit: () => AgentEventEmitter;
|
|
9
|
+
/** Nested turn. Must not include another `task` tool. */
|
|
10
|
+
run: (input: {
|
|
11
|
+
prompt: string;
|
|
12
|
+
label: string;
|
|
13
|
+
extraSystem: string;
|
|
14
|
+
emit: AgentEventEmitter;
|
|
15
|
+
}) => Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Spawn a one-shot nested agent. Subagents do not get this tool, so depth is 1.
|
|
20
|
+
*/
|
|
21
|
+
export function createTaskTool(options: TaskToolOptions): McpToolDefinition {
|
|
22
|
+
return {
|
|
23
|
+
name: "task",
|
|
24
|
+
description:
|
|
25
|
+
"Spawn a subagent for a focused job (research, an isolated edit, one copy-pipeline step). " +
|
|
26
|
+
"Pass a complete prompt. Optional `skills` injects those skill bodies into the subagent. " +
|
|
27
|
+
"Returns the subagent's final reply. Do not use for a one-line question.",
|
|
28
|
+
inputSchema: {
|
|
29
|
+
type: "object",
|
|
30
|
+
properties: {
|
|
31
|
+
prompt: { type: "string", description: "The full task for the subagent." },
|
|
32
|
+
label: {
|
|
33
|
+
type: "string",
|
|
34
|
+
description: "Short name shown in the transcript (e.g. copy-edit).",
|
|
35
|
+
},
|
|
36
|
+
skills: {
|
|
37
|
+
type: "array",
|
|
38
|
+
items: { type: "string" },
|
|
39
|
+
description: "Skill names to load into the subagent system prompt.",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
required: ["prompt"],
|
|
43
|
+
},
|
|
44
|
+
async handler(raw) {
|
|
45
|
+
const prompt = typeof raw.prompt === "string" ? raw.prompt.trim() : "";
|
|
46
|
+
if (!prompt) return errorResult("prompt is empty");
|
|
47
|
+
const label =
|
|
48
|
+
typeof raw.label === "string" && raw.label.trim() ? raw.label.trim() : "subagent";
|
|
49
|
+
const names = Array.isArray(raw.skills)
|
|
50
|
+
? raw.skills.filter((s): s is string => typeof s === "string")
|
|
51
|
+
: [];
|
|
52
|
+
const extraSystem = skillBodiesForNames(options.skills, names);
|
|
53
|
+
let nestedText = "";
|
|
54
|
+
let nestedError: string | undefined;
|
|
55
|
+
const parent = options.parentEmit();
|
|
56
|
+
const emit: AgentEventEmitter = (event) => {
|
|
57
|
+
if (event.kind === "text_delta") nestedText += event.text;
|
|
58
|
+
else if (event.kind === "error") {
|
|
59
|
+
nestedError = event.message;
|
|
60
|
+
parent(event);
|
|
61
|
+
} else if (event.kind !== "session_meta" && event.kind !== "completion") {
|
|
62
|
+
parent(event);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
try {
|
|
66
|
+
await options.run({ prompt, label, extraSystem, emit });
|
|
67
|
+
if (nestedError) return errorResult(nestedError);
|
|
68
|
+
return textResult(nestedText.trim() || "(subagent produced no text)");
|
|
69
|
+
} catch (err) {
|
|
70
|
+
return errorResult(err instanceof Error ? err.message : String(err));
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { createMemoryTodoStore, createTodoTool } from "./todo.ts";
|
|
3
|
+
|
|
4
|
+
describe("todo_write", () => {
|
|
5
|
+
test("replaces the list and drops invalid items", async () => {
|
|
6
|
+
const store = createMemoryTodoStore();
|
|
7
|
+
const tool = createTodoTool(store);
|
|
8
|
+
const result = await tool.handler({
|
|
9
|
+
items: [
|
|
10
|
+
{ id: "1", content: "inspect", status: "done" },
|
|
11
|
+
{ id: "2", content: "edit", status: "in_progress" },
|
|
12
|
+
{ id: "", content: "skip", status: "pending" },
|
|
13
|
+
{ id: "3", content: "later", status: "nope" },
|
|
14
|
+
],
|
|
15
|
+
});
|
|
16
|
+
expect(result.isError).toBeFalsy();
|
|
17
|
+
expect(result.structuredContent).toEqual({
|
|
18
|
+
items: [
|
|
19
|
+
{ id: "1", content: "inspect", status: "done" },
|
|
20
|
+
{ id: "2", content: "edit", status: "in_progress" },
|
|
21
|
+
],
|
|
22
|
+
});
|
|
23
|
+
expect(store.list()).toHaveLength(2);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { jsonResult, type McpToolDefinition } from "../core/mcp.ts";
|
|
2
|
+
|
|
3
|
+
export const TODO_STATUSES = ["pending", "in_progress", "done"] as const;
|
|
4
|
+
export type TodoStatus = (typeof TODO_STATUSES)[number];
|
|
5
|
+
|
|
6
|
+
export interface TodoItem {
|
|
7
|
+
id: string;
|
|
8
|
+
content: string;
|
|
9
|
+
status: TodoStatus;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface TodoStore {
|
|
13
|
+
list(): TodoItem[];
|
|
14
|
+
replace(items: TodoItem[]): TodoItem[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function createMemoryTodoStore(): TodoStore {
|
|
18
|
+
let items: TodoItem[] = [];
|
|
19
|
+
return {
|
|
20
|
+
list: () => items.map((item) => ({ ...item })),
|
|
21
|
+
replace(next) {
|
|
22
|
+
items = next.map((item) => ({ ...item }));
|
|
23
|
+
return items.map((item) => ({ ...item }));
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function asStatus(value: unknown): TodoStatus | undefined {
|
|
29
|
+
return value === "pending" || value === "in_progress" || value === "done" ? value : undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function createTodoTool(store: TodoStore): McpToolDefinition {
|
|
33
|
+
return {
|
|
34
|
+
name: "todo_write",
|
|
35
|
+
description:
|
|
36
|
+
"Replace the session todo list. Use for multi-step work. One in_progress item at a time.",
|
|
37
|
+
inputSchema: {
|
|
38
|
+
type: "object",
|
|
39
|
+
properties: {
|
|
40
|
+
items: {
|
|
41
|
+
type: "array",
|
|
42
|
+
items: {
|
|
43
|
+
type: "object",
|
|
44
|
+
properties: {
|
|
45
|
+
id: { type: "string" },
|
|
46
|
+
content: { type: "string" },
|
|
47
|
+
status: { type: "string", enum: [...TODO_STATUSES] },
|
|
48
|
+
},
|
|
49
|
+
required: ["id", "content", "status"],
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
required: ["items"],
|
|
54
|
+
},
|
|
55
|
+
async handler(raw) {
|
|
56
|
+
const rawItems = Array.isArray(raw.items) ? raw.items : [];
|
|
57
|
+
const items: TodoItem[] = [];
|
|
58
|
+
for (const entry of rawItems) {
|
|
59
|
+
if (!entry || typeof entry !== "object") continue;
|
|
60
|
+
const rec = entry as Record<string, unknown>;
|
|
61
|
+
const id = typeof rec.id === "string" ? rec.id.trim() : "";
|
|
62
|
+
const content = typeof rec.content === "string" ? rec.content.trim() : "";
|
|
63
|
+
const status = asStatus(rec.status);
|
|
64
|
+
if (!id || !content || !status) continue;
|
|
65
|
+
items.push({ id, content, status });
|
|
66
|
+
}
|
|
67
|
+
return jsonResult({ items: store.replace(items) });
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|