@wrongstack/tools 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/dist/index.js ADDED
@@ -0,0 +1,3588 @@
1
+ import * as fs4 from 'fs/promises';
2
+ import * as path from 'path';
3
+ import { atomicWrite, unifiedDiff, detectNewlineStyle, normalizeToLf, toStyle, compileGlob, stripAnsi } from '@wrongstack/core';
4
+ import { spawn } from 'child_process';
5
+ import * as os from 'os';
6
+ import * as dns from 'dns/promises';
7
+ import * as fsSync from 'fs';
8
+
9
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
10
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
11
+ }) : x)(function(x) {
12
+ if (typeof require !== "undefined") return require.apply(this, arguments);
13
+ throw Error('Dynamic require of "' + x + '" is not supported');
14
+ });
15
+ function resolvePath(input, ctx) {
16
+ return path.isAbsolute(input) ? path.normalize(input) : path.resolve(ctx.cwd, input);
17
+ }
18
+ function ensureInsideRoot(absPath, ctx) {
19
+ const root = path.resolve(ctx.projectRoot);
20
+ const target = path.resolve(absPath);
21
+ const rel = path.relative(root, target);
22
+ if (rel.startsWith("..") || path.isAbsolute(rel)) {
23
+ throw new Error(`Path "${absPath}" is outside project root "${root}"`);
24
+ }
25
+ return target;
26
+ }
27
+ function safeResolve(input, ctx) {
28
+ return ensureInsideRoot(resolvePath(input, ctx), ctx);
29
+ }
30
+ function truncateMiddle(s, max) {
31
+ if (Buffer.byteLength(s, "utf8") <= max) return s;
32
+ const half = Math.floor(max / 2);
33
+ return s.slice(0, half) + `
34
+ \u2026[truncated ${Buffer.byteLength(s, "utf8") - max} bytes from middle]\u2026
35
+ ` + s.slice(-half);
36
+ }
37
+ function isBinaryBuffer(buf) {
38
+ const len = Math.min(buf.length, 8192);
39
+ for (let i = 0; i < len; i++) {
40
+ if (buf[i] === 0) return true;
41
+ }
42
+ return false;
43
+ }
44
+
45
+ // src/read.ts
46
+ var MAX_BYTES = 5 * 1024 * 1024;
47
+ var readTool = {
48
+ name: "read",
49
+ description: "Read the contents of a file. Lines are 1-indexed and prefixed with line numbers.",
50
+ usageHint: "Read a file before editing it. Returns lines numbered like ` 1\u2192content`. Use `offset` and `limit` for large files (default reads up to 2000 lines).",
51
+ permission: "auto",
52
+ mutating: false,
53
+ maxOutputBytes: 262144,
54
+ timeoutMs: 5e3,
55
+ inputSchema: {
56
+ type: "object",
57
+ properties: {
58
+ path: { type: "string", description: "File path (absolute or relative to cwd)" },
59
+ offset: { type: "integer", description: "1-based line number to start from" },
60
+ limit: { type: "integer", description: "Max lines to read (default 2000)" }
61
+ },
62
+ required: ["path"]
63
+ },
64
+ async execute(input, ctx) {
65
+ if (!input?.path) throw new Error("read: path is required");
66
+ const absPath = safeResolve(input.path, ctx);
67
+ const stat9 = await fs4.stat(absPath);
68
+ if (!stat9.isFile()) throw new Error(`read: "${input.path}" is not a regular file`);
69
+ if (stat9.size > MAX_BYTES) {
70
+ throw new Error(`read: file too large (${stat9.size} bytes, limit ${MAX_BYTES})`);
71
+ }
72
+ const buf = await fs4.readFile(absPath);
73
+ if (isBinaryBuffer(buf)) {
74
+ throw new Error(`read: "${input.path}" appears to be binary`);
75
+ }
76
+ const text = buf.toString("utf8");
77
+ const allLines = text.split(/\r\n|\r|\n/);
78
+ const total = allLines.length;
79
+ const offset = Math.max(1, input.offset ?? 1);
80
+ const limit = Math.max(1, Math.min(input.limit ?? 2e3, 5e3));
81
+ const slice = allLines.slice(offset - 1, offset - 1 + limit);
82
+ const truncated = offset - 1 + slice.length < total;
83
+ const width = String(offset + slice.length - 1).length;
84
+ const numbered = slice.map((line, i) => `${String(offset + i).padStart(width, " ")}\u2192${line}`).join("\n");
85
+ ctx.recordRead(absPath, stat9.mtimeMs);
86
+ return {
87
+ text: numbered,
88
+ total_lines: total,
89
+ encoding: "utf8",
90
+ truncated
91
+ };
92
+ }
93
+ };
94
+ var writeTool = {
95
+ name: "write",
96
+ description: "Write or overwrite a file. For existing files, prefer `edit` over `write`.",
97
+ usageHint: "Use `write` for new files or full replacements. For partial edits use `edit`. Existing files must have been `read` first in this session.",
98
+ permission: "confirm",
99
+ mutating: true,
100
+ timeoutMs: 5e3,
101
+ inputSchema: {
102
+ type: "object",
103
+ properties: {
104
+ path: { type: "string" },
105
+ content: { type: "string" }
106
+ },
107
+ required: ["path", "content"]
108
+ },
109
+ async execute(input, ctx) {
110
+ if (!input?.path) throw new Error("write: path is required");
111
+ if (input.content === void 0) throw new Error("write: content is required");
112
+ const absPath = safeResolve(input.path, ctx);
113
+ let existed = false;
114
+ let prev = "";
115
+ try {
116
+ const stat10 = await fs4.stat(absPath);
117
+ existed = stat10.isFile();
118
+ if (existed) {
119
+ if (!ctx.hasRead(absPath)) {
120
+ throw new Error(
121
+ `write: file "${input.path}" exists but was not read in this session. Read it first.`
122
+ );
123
+ }
124
+ prev = await fs4.readFile(absPath, "utf8");
125
+ }
126
+ } catch (err) {
127
+ if (err.code !== "ENOENT") {
128
+ throw err;
129
+ }
130
+ }
131
+ await atomicWrite(absPath, input.content);
132
+ const diff = existed ? unifiedDiff(prev, input.content, { fromFile: input.path, toFile: input.path }) : `+++ ${input.path}
133
+ + (new file, ${input.content.split("\n").length} lines)`;
134
+ const stat9 = await fs4.stat(absPath);
135
+ ctx.recordRead(absPath, stat9.mtimeMs);
136
+ return {
137
+ path: absPath,
138
+ bytes_written: Buffer.byteLength(input.content, "utf8"),
139
+ created: !existed,
140
+ diff
141
+ };
142
+ }
143
+ };
144
+ var editTool = {
145
+ name: "edit",
146
+ description: "Make a surgical edit by replacing exact text. Fails if `old_string` is not unique unless `replace_all` is true.",
147
+ usageHint: "Always `read` the file first. `old_string` must be an EXACT match (whitespace included). If multiple matches exist, either narrow `old_string` with more context or set `replace_all: true`.",
148
+ permission: "confirm",
149
+ mutating: true,
150
+ timeoutMs: 5e3,
151
+ inputSchema: {
152
+ type: "object",
153
+ properties: {
154
+ path: { type: "string" },
155
+ old_string: { type: "string" },
156
+ new_string: { type: "string" },
157
+ replace_all: { type: "boolean" }
158
+ },
159
+ required: ["path", "old_string", "new_string"]
160
+ },
161
+ async execute(input, ctx) {
162
+ if (!input?.path) throw new Error("edit: path is required");
163
+ if (input.old_string === void 0) throw new Error("edit: old_string is required");
164
+ if (input.new_string === void 0) throw new Error("edit: new_string is required");
165
+ if (input.old_string === "") throw new Error("edit: old_string cannot be empty");
166
+ const absPath = safeResolve(input.path, ctx);
167
+ const stat9 = await fs4.stat(absPath).catch((err) => {
168
+ if (err.code === "ENOENT") {
169
+ throw new Error(`edit: file "${input.path}" does not exist. Use \`write\` instead.`);
170
+ }
171
+ throw err;
172
+ });
173
+ if (!stat9.isFile()) throw new Error(`edit: "${input.path}" is not a regular file`);
174
+ if (!ctx.hasRead(absPath)) {
175
+ throw new Error(
176
+ `edit: file "${input.path}" was not read in this session. Read it first.`
177
+ );
178
+ }
179
+ const lastReadMtime = ctx.lastReadMtime(absPath);
180
+ if (lastReadMtime !== void 0 && stat9.mtimeMs > lastReadMtime + 1) {
181
+ throw new Error(`edit: file "${input.path}" was modified externally. Re-read it first.`);
182
+ }
183
+ const original = await fs4.readFile(absPath, "utf8");
184
+ const style = detectNewlineStyle(original);
185
+ const fileLf = normalizeToLf(original);
186
+ const oldLf = normalizeToLf(input.old_string);
187
+ const newLf = normalizeToLf(input.new_string);
188
+ if (oldLf === newLf) {
189
+ return {
190
+ path: absPath,
191
+ replacements: 0,
192
+ diff: "(no-op: old and new are identical)"
193
+ };
194
+ }
195
+ let count = 0;
196
+ let idx = fileLf.indexOf(oldLf);
197
+ const matches = [];
198
+ while (idx !== -1) {
199
+ matches.push(idx);
200
+ count++;
201
+ idx = fileLf.indexOf(oldLf, idx + 1);
202
+ }
203
+ if (count === 0) {
204
+ const hint = findSimilarity(fileLf, oldLf);
205
+ throw new Error(
206
+ `edit: no match for old_string in "${input.path}".${hint ? ` Nearest match near line ${hint}.` : ""}`
207
+ );
208
+ }
209
+ if (count > 1 && !input.replace_all) {
210
+ const lines = lineNumbersFor(fileLf, matches);
211
+ throw new Error(
212
+ `edit: old_string matched ${count} times in "${input.path}" (lines: ${lines.join(", ")}). Add more context to make it unique, or set replace_all: true.`
213
+ );
214
+ }
215
+ const newFileLf = input.replace_all ? fileLf.split(oldLf).join(newLf) : fileLf.replace(oldLf, newLf);
216
+ const newFile = toStyle(newFileLf, style);
217
+ await atomicWrite(absPath, newFile, { mode: stat9.mode & 511 });
218
+ const updated = await fs4.stat(absPath);
219
+ ctx.recordRead(absPath, updated.mtimeMs);
220
+ const diff = unifiedDiff(original, newFile, {
221
+ fromFile: input.path,
222
+ toFile: input.path
223
+ });
224
+ return {
225
+ path: absPath,
226
+ replacements: input.replace_all ? count : 1,
227
+ diff
228
+ };
229
+ }
230
+ };
231
+ function lineNumbersFor(text, indices) {
232
+ const out = [];
233
+ let pos = 0;
234
+ let line = 1;
235
+ for (const target of indices) {
236
+ while (pos < target) {
237
+ if (text.charCodeAt(pos) === 10) line++;
238
+ pos++;
239
+ }
240
+ out.push(line);
241
+ }
242
+ return out;
243
+ }
244
+ function findSimilarity(haystack, needle) {
245
+ if (needle.length < 20) return void 0;
246
+ const probe = needle.slice(0, Math.min(40, needle.length));
247
+ const idx = haystack.indexOf(probe);
248
+ if (idx === -1) return void 0;
249
+ let line = 1;
250
+ for (let i = 0; i < idx; i++) {
251
+ if (haystack.charCodeAt(i) === 10) line++;
252
+ }
253
+ return line;
254
+ }
255
+ var DEFAULT_IGNORE = ["node_modules", ".git", "dist", "build", ".next", "coverage"];
256
+ var replaceTool = {
257
+ name: "replace",
258
+ description: "Batch replace a pattern across multiple files matched by glob. Returns diff for each modified file.",
259
+ usageHint: 'Use `glob` for broad patterns (e.g. "**/*.ts"). Set `dry_run: true` to preview without modifying. `files` can be a single path, comma-separated list, or glob pattern.',
260
+ permission: "confirm",
261
+ mutating: true,
262
+ timeoutMs: 3e4,
263
+ inputSchema: {
264
+ type: "object",
265
+ properties: {
266
+ pattern: { type: "string", description: "Regex pattern to match" },
267
+ replacement: { type: "string", description: "Replacement string" },
268
+ files: {
269
+ type: "string",
270
+ description: "File(s) to target: single path, comma-separated list, or glob pattern"
271
+ },
272
+ glob: { type: "string", description: 'Additional glob filter (e.g. "*.ts")' },
273
+ replace_all: { type: "boolean", description: "Replace all occurrences in each file (default: true)" },
274
+ dry_run: { type: "boolean", description: "Preview changes without writing" }
275
+ },
276
+ required: ["pattern", "replacement", "files"]
277
+ },
278
+ async execute(input, ctx) {
279
+ if (!input?.pattern) throw new Error("replace: pattern is required");
280
+ if (input.replacement === void 0) throw new Error("replace: replacement is required");
281
+ if (!input?.files) throw new Error("replace: files is required");
282
+ const re = new RegExp(input.pattern, "g");
283
+ const globRe = input.glob ? compileGlob(input.glob) : null;
284
+ const dryRun = input.dry_run ?? false;
285
+ const replaceAll = input.replace_all ?? true;
286
+ const filesInput = Array.isArray(input.files) ? input.files.join(",") : input.files;
287
+ const fileList = await resolveFiles(filesInput, ctx, globRe);
288
+ const results = [];
289
+ let totalReplacements = 0;
290
+ for (const absPath of fileList) {
291
+ const stat9 = await fs4.stat(absPath).catch((err) => {
292
+ if (err.code === "ENOENT") return null;
293
+ throw err;
294
+ });
295
+ if (!stat9 || !stat9.isFile()) continue;
296
+ let content;
297
+ try {
298
+ const buf = await fs4.readFile(absPath);
299
+ if (isBinaryBuffer(buf)) continue;
300
+ content = buf.toString("utf8");
301
+ } catch {
302
+ continue;
303
+ }
304
+ const style = detectNewlineStyle(content);
305
+ const contentLf = normalizeToLf(content);
306
+ re.lastIndex = 0;
307
+ const matches = [...contentLf.matchAll(re)];
308
+ if (matches.length === 0) continue;
309
+ const newContentLf = replaceAll ? contentLf.replace(re, input.replacement) : contentLf.replace(re, input.replacement);
310
+ re.lastIndex = 0;
311
+ const actualCount = replaceAll ? matches.length : 1;
312
+ totalReplacements += actualCount;
313
+ if (!dryRun) {
314
+ const newContent = toStyle(newContentLf, style);
315
+ await atomicWrite(absPath, newContent, { mode: stat9.mode & 511 });
316
+ }
317
+ const diff = dryRun || matches.length > 0 ? unifiedDiff(content, toStyle(newContentLf, style), { fromFile: absPath, toFile: absPath }) : void 0;
318
+ results.push({
319
+ path: absPath,
320
+ replacements: matches.length,
321
+ diff
322
+ });
323
+ }
324
+ return {
325
+ files_modified: results.length,
326
+ total_replacements: totalReplacements,
327
+ results,
328
+ dry_run: dryRun
329
+ };
330
+ }
331
+ };
332
+ async function resolveFiles(filesInput, ctx, extraGlob) {
333
+ const base = ctx.cwd;
334
+ const normalized = filesInput.trim();
335
+ if (normalized.startsWith("**/") || normalized.startsWith("*") || normalized.includes("**")) {
336
+ return await globFiles(normalized, base, extraGlob);
337
+ }
338
+ const parts = normalized.split(",").map((s) => s.trim()).filter(Boolean);
339
+ const resolved = [];
340
+ for (const p of parts) {
341
+ const absPath = safeResolve(p, ctx);
342
+ const stat9 = await fs4.stat(absPath).catch(() => null);
343
+ if (stat9?.isFile()) {
344
+ resolved.push(absPath);
345
+ }
346
+ }
347
+ return resolved;
348
+ }
349
+ async function globFiles(pattern, base, extraGlob) {
350
+ const { spawn: spawn16 } = await import('child_process');
351
+ const rgAvailable = await checkRg();
352
+ if (rgAvailable) {
353
+ try {
354
+ const { promise } = spawnRgFind(pattern, base);
355
+ return await promise;
356
+ } catch {
357
+ }
358
+ }
359
+ return await globNative(pattern, base, extraGlob);
360
+ }
361
+ function checkRg() {
362
+ return new Promise((resolve2) => {
363
+ try {
364
+ const p = spawn("rg", ["--version"], { stdio: "ignore" });
365
+ p.on("error", () => resolve2(false));
366
+ p.on("close", (code) => resolve2(code === 0));
367
+ } catch {
368
+ resolve2(false);
369
+ }
370
+ });
371
+ }
372
+ function spawnRgFind(pattern, base) {
373
+ const args = ["--files", "--glob", pattern, base];
374
+ const child = spawn("rg", args, { stdio: ["ignore", "pipe", "pipe"] });
375
+ let buf = "";
376
+ child.stdout?.on("data", (chunk) => {
377
+ buf += chunk.toString();
378
+ });
379
+ return {
380
+ promise: new Promise((resolve2, reject) => {
381
+ child.on("error", reject);
382
+ child.on("close", () => {
383
+ resolve2(buf.split("\n").filter(Boolean));
384
+ });
385
+ })
386
+ };
387
+ }
388
+ async function globNative(pattern, base, extraGlob) {
389
+ const results = [];
390
+ const globRe = compileGlob(pattern);
391
+ const walk = async (dir) => {
392
+ let entries;
393
+ try {
394
+ entries = await fs4.readdir(dir, { withFileTypes: true });
395
+ } catch {
396
+ return;
397
+ }
398
+ for (const e of entries) {
399
+ if (DEFAULT_IGNORE.includes(e.name)) continue;
400
+ const full = path.join(dir, e.name);
401
+ if (e.isDirectory()) {
402
+ await walk(full);
403
+ } else if (e.isFile()) {
404
+ const name = e.name;
405
+ if (globRe.test(name) || globRe.test(full)) {
406
+ if (extraGlob && !extraGlob.test(name) && !extraGlob.test(full)) continue;
407
+ results.push(full);
408
+ }
409
+ globRe.lastIndex = 0;
410
+ if (extraGlob) extraGlob.lastIndex = 0;
411
+ }
412
+ }
413
+ };
414
+ await walk(base);
415
+ return results;
416
+ }
417
+ var DEFAULT_IGNORE2 = ["node_modules", ".git", "dist", "build", ".next", "coverage", ".turbo"];
418
+ var globTool = {
419
+ name: "glob",
420
+ description: "Find files matching a glob pattern. Returns paths sorted by mtime (newest first).",
421
+ usageHint: "Examples: `**/*.ts`, `src/**/*.test.ts`, `*.json`. Common dirs (node_modules, .git, dist) are ignored by default. Returns up to 1000 paths.",
422
+ permission: "auto",
423
+ mutating: false,
424
+ maxOutputBytes: 65536,
425
+ timeoutMs: 5e3,
426
+ inputSchema: {
427
+ type: "object",
428
+ properties: {
429
+ pattern: { type: "string" },
430
+ path: { type: "string", description: "Base directory (defaults to cwd)" },
431
+ limit: { type: "integer" }
432
+ },
433
+ required: ["pattern"]
434
+ },
435
+ async execute(input, ctx) {
436
+ if (!input?.pattern) throw new Error("glob: pattern is required");
437
+ const base = input.path ? safeResolve(input.path, ctx) : ctx.cwd;
438
+ const limit = Math.max(1, Math.min(input.limit ?? 1e3, 5e3));
439
+ const ignored = await readGitignore(base);
440
+ const re = compileGlob(input.pattern);
441
+ const results = [];
442
+ let truncated = false;
443
+ const walk = async (dir, relPrefix) => {
444
+ if (results.length >= limit) {
445
+ truncated = true;
446
+ return;
447
+ }
448
+ let entries;
449
+ try {
450
+ entries = await fs4.readdir(dir, { withFileTypes: true });
451
+ } catch {
452
+ return;
453
+ }
454
+ for (const e of entries) {
455
+ const name = e.name;
456
+ if (DEFAULT_IGNORE2.includes(name)) continue;
457
+ if (ignored.includes(name)) continue;
458
+ const rel = relPrefix ? `${relPrefix}/${name}` : name;
459
+ const full = path.join(dir, name);
460
+ if (e.isDirectory()) {
461
+ await walk(full, rel);
462
+ if (truncated) return;
463
+ } else if (e.isFile()) {
464
+ if (re.test(rel) || re.test(name)) {
465
+ try {
466
+ const st = await fs4.stat(full);
467
+ results.push({ rel: full, mtime: st.mtimeMs });
468
+ if (results.length >= limit) {
469
+ truncated = true;
470
+ return;
471
+ }
472
+ } catch {
473
+ }
474
+ }
475
+ }
476
+ }
477
+ };
478
+ await walk(base, "");
479
+ results.sort((a, b) => b.mtime - a.mtime);
480
+ return { files: results.map((r) => r.rel), truncated };
481
+ }
482
+ };
483
+ async function readGitignore(dir) {
484
+ try {
485
+ const raw = await fs4.readFile(path.join(dir, ".gitignore"), "utf8");
486
+ return raw.split("\n").map((l) => l.trim()).filter((l) => l && !l.startsWith("#"));
487
+ } catch {
488
+ return [];
489
+ }
490
+ }
491
+ var DEFAULT_IGNORE3 = ["node_modules", ".git", "dist", "build", ".next", "coverage"];
492
+ var grepTool = {
493
+ name: "grep",
494
+ description: "Search file contents with a regex. Uses ripgrep when available.",
495
+ usageHint: 'Pattern is regex. Use `output_mode: "content"` for matched lines, `"files_with_matches"` for paths, `"count"` for tallies. `glob` filters files (e.g. `*.ts`).',
496
+ permission: "auto",
497
+ mutating: false,
498
+ maxOutputBytes: 131072,
499
+ timeoutMs: 1e4,
500
+ inputSchema: {
501
+ type: "object",
502
+ properties: {
503
+ pattern: { type: "string" },
504
+ path: { type: "string" },
505
+ glob: { type: "string" },
506
+ output_mode: { type: "string", enum: ["content", "files_with_matches", "count"] },
507
+ context_lines: { type: "integer" },
508
+ case_insensitive: { type: "boolean" },
509
+ limit: { type: "integer" }
510
+ },
511
+ required: ["pattern"]
512
+ },
513
+ async execute(input, ctx, opts) {
514
+ if (!input?.pattern) throw new Error("grep: pattern is required");
515
+ const base = input.path ? safeResolve(input.path, ctx) : ctx.cwd;
516
+ const mode = input.output_mode ?? "content";
517
+ const limit = Math.max(1, Math.min(input.limit ?? 200, 2e3));
518
+ const rgAvailable = await detectRg(opts.signal);
519
+ if (rgAvailable) {
520
+ try {
521
+ return await runRg(input, base, mode, limit, opts.signal);
522
+ } catch {
523
+ }
524
+ }
525
+ return await runNative(input, base, mode, limit, opts.signal);
526
+ }
527
+ };
528
+ async function detectRg(signal) {
529
+ return new Promise((resolve2) => {
530
+ try {
531
+ const p = spawn("rg", ["--version"], { stdio: "ignore", signal });
532
+ p.on("error", () => resolve2(false));
533
+ p.on("close", (code) => resolve2(code === 0));
534
+ } catch {
535
+ resolve2(false);
536
+ }
537
+ });
538
+ }
539
+ function runRg(input, base, mode, limit, signal) {
540
+ return new Promise((resolve2, reject) => {
541
+ const args = ["--no-heading"];
542
+ if (input.case_insensitive) args.push("-i");
543
+ if (mode === "files_with_matches") args.push("-l");
544
+ if (mode === "count") args.push("-c");
545
+ if (mode === "content") {
546
+ args.push("-n");
547
+ if (input.context_lines) args.push("-C", String(input.context_lines));
548
+ }
549
+ if (input.glob) args.push("--glob", input.glob);
550
+ args.push("--", input.pattern, base);
551
+ const child = spawn("rg", args, { signal, stdio: ["ignore", "pipe", "pipe"] });
552
+ let buf = "";
553
+ child.stdout?.on("data", (chunk) => {
554
+ buf += chunk.toString();
555
+ });
556
+ child.on("error", reject);
557
+ child.on("close", () => {
558
+ const lines = buf.split("\n").filter(Boolean);
559
+ const sliced = lines.slice(0, limit);
560
+ resolve2({
561
+ matches: sliced,
562
+ count: lines.length,
563
+ truncated: lines.length > limit,
564
+ used: "rg"
565
+ });
566
+ });
567
+ });
568
+ }
569
+ async function runNative(input, base, mode, limit, signal) {
570
+ const flags = input.case_insensitive ? "i" : "";
571
+ const re = new RegExp(input.pattern, flags);
572
+ const globRe = input.glob ? compileGlob(input.glob) : null;
573
+ const matches = [];
574
+ const fileMatches = /* @__PURE__ */ new Map();
575
+ let total = 0;
576
+ let stopped = false;
577
+ const walk = async (dir) => {
578
+ if (stopped || signal.aborted) return;
579
+ let entries;
580
+ try {
581
+ entries = await fs4.readdir(dir, { withFileTypes: true });
582
+ } catch {
583
+ return;
584
+ }
585
+ for (const e of entries) {
586
+ if (stopped) return;
587
+ if (DEFAULT_IGNORE3.includes(e.name)) continue;
588
+ const full = path.join(dir, e.name);
589
+ if (e.isDirectory()) {
590
+ await walk(full);
591
+ } else if (e.isFile()) {
592
+ if (globRe && !globRe.test(e.name) && !globRe.test(full)) continue;
593
+ if (globRe) globRe.lastIndex = 0;
594
+ try {
595
+ const stat9 = await fs4.stat(full);
596
+ if (stat9.size > 1e6) continue;
597
+ const head = await fs4.readFile(full);
598
+ if (isBinaryBuffer(head)) continue;
599
+ const text = head.toString("utf8");
600
+ const lines = text.split(/\r?\n/);
601
+ let fileHits = 0;
602
+ for (let i = 0; i < lines.length; i++) {
603
+ const ln = lines[i] ?? "";
604
+ re.lastIndex = 0;
605
+ if (re.test(ln)) {
606
+ fileHits++;
607
+ total++;
608
+ if (mode === "content" && matches.length < limit) {
609
+ matches.push(`${full}:${i + 1}:${ln}`);
610
+ }
611
+ }
612
+ }
613
+ if (fileHits > 0) {
614
+ fileMatches.set(full, fileHits);
615
+ if (mode === "files_with_matches" && matches.length < limit) {
616
+ matches.push(full);
617
+ }
618
+ if (mode === "count" && matches.length < limit) {
619
+ matches.push(`${full}:${fileHits}`);
620
+ }
621
+ }
622
+ if (matches.length >= limit) stopped = true;
623
+ } catch {
624
+ }
625
+ }
626
+ }
627
+ };
628
+ await walk(base);
629
+ return {
630
+ matches,
631
+ count: total,
632
+ truncated: stopped,
633
+ used: "native"
634
+ };
635
+ }
636
+ var MAX_OUTPUT = 32768;
637
+ var DEFAULT_TIMEOUT = 3e4;
638
+ var bashTool = {
639
+ name: "bash",
640
+ description: "Run a shell command. stdout and stderr are merged.",
641
+ usageHint: "Runs via `bash -c` (or `cmd /c` on Windows). Cwd is the project root. Default timeout 30s. Output truncated from the middle if oversized. Use for git, npm, builds, tests.",
642
+ permission: "confirm",
643
+ mutating: true,
644
+ timeoutMs: 3e4,
645
+ maxOutputBytes: MAX_OUTPUT,
646
+ inputSchema: {
647
+ type: "object",
648
+ properties: {
649
+ command: { type: "string" },
650
+ timeout_ms: { type: "integer" },
651
+ background: { type: "boolean" }
652
+ },
653
+ required: ["command"]
654
+ },
655
+ async execute(input, ctx, opts) {
656
+ if (!input?.command) throw new Error("bash: command is required");
657
+ const timeoutMs = Math.min(input.timeout_ms ?? DEFAULT_TIMEOUT, 6e5);
658
+ const isWin = os.platform() === "win32";
659
+ const shell = isWin ? process.env["COMSPEC"] ?? "cmd.exe" : process.env["SHELL"] ?? "/bin/bash";
660
+ const args = isWin ? ["/c", input.command] : ["-c", input.command];
661
+ const env = { ...process.env };
662
+ env["WRONGSTACK_SESSION_ID"] = ctx.session.id;
663
+ return new Promise((resolve2, reject) => {
664
+ let child;
665
+ try {
666
+ child = spawn(shell, args, {
667
+ cwd: ctx.projectRoot,
668
+ env,
669
+ stdio: input.background ? "ignore" : ["ignore", "pipe", "pipe"],
670
+ detached: input.background,
671
+ signal: opts.signal
672
+ });
673
+ } catch (err) {
674
+ return reject(err);
675
+ }
676
+ if (input.background) {
677
+ const pid = child.pid;
678
+ if (typeof pid === "number") child.unref();
679
+ return resolve2({
680
+ output: `[background] pid=${pid ?? "unknown"}`,
681
+ exit_code: null,
682
+ timed_out: false,
683
+ pid
684
+ });
685
+ }
686
+ let buf = "";
687
+ let timedOut = false;
688
+ const timers = [];
689
+ const timer = setTimeout(() => {
690
+ timedOut = true;
691
+ if (isWin) {
692
+ try {
693
+ child.kill();
694
+ } catch {
695
+ }
696
+ } else {
697
+ try {
698
+ child.kill("SIGTERM");
699
+ const killTimer = setTimeout(() => {
700
+ try {
701
+ child.kill("SIGKILL");
702
+ } catch {
703
+ }
704
+ }, 2e3);
705
+ timers.push(killTimer);
706
+ } catch {
707
+ }
708
+ }
709
+ }, timeoutMs);
710
+ timers.push(timer);
711
+ timer.unref?.();
712
+ child.stdout?.on("data", (chunk) => {
713
+ buf += chunk.toString();
714
+ });
715
+ child.stderr?.on("data", (chunk) => {
716
+ buf += chunk.toString();
717
+ });
718
+ child.on("error", (err) => {
719
+ for (const t of timers) clearTimeout(t);
720
+ reject(err);
721
+ });
722
+ child.on("close", (code) => {
723
+ for (const t of timers) clearTimeout(t);
724
+ const cleaned = stripAnsi(buf).replace(/\r\n?/g, "\n");
725
+ resolve2({
726
+ output: truncateMiddle(cleaned, MAX_OUTPUT),
727
+ exit_code: code,
728
+ timed_out: timedOut
729
+ });
730
+ });
731
+ });
732
+ }
733
+ };
734
+ var ALLOWED_COMMANDS = {
735
+ node: ["--version", "-e", "-p", "-r", "--input-type=module"],
736
+ npm: ["--version", "init", "install", "test", "run", "list", "pkg", "doctor"],
737
+ pnpm: ["--version", "init", "install", "add", "remove", "exec", "list", "run", "dlx"],
738
+ npx: ["--version"],
739
+ git: ["--version", "status", "log", "diff", "branch", "checkout", "stash", "add", "commit", "push", "pull"],
740
+ ls: ["-la", "-l", "-a"],
741
+ cat: [],
742
+ head: ["-n"],
743
+ tail: ["-n"],
744
+ wc: ["-l", "-w", "-c"],
745
+ grep: [],
746
+ find: [],
747
+ echo: [],
748
+ mkdir: ["-p"],
749
+ cp: ["-r"],
750
+ mv: [],
751
+ rm: ["-rf"],
752
+ touch: [],
753
+ bun: ["--version", "run", "add", "init"],
754
+ tsc: ["--version", "--noEmit", "--project"],
755
+ vitest: ["--version", "run", "--coverage"],
756
+ biome: ["--version", "lint", "format", "check"],
757
+ cargo: ["--version", "build", "test", "check"],
758
+ rustc: ["--version"],
759
+ go: ["version", "run", "build", "test"],
760
+ python: ["--version", "-c"],
761
+ pip: ["--version", "install", "list"],
762
+ docker: ["--version", "ps", "images", "build"],
763
+ kubectl: ["version", "get", "describe", "logs"]
764
+ };
765
+ var FORBIDDEN_PATTERNS = [
766
+ /;\s*rm\s+-rf/i,
767
+ /\|\s*rm\s/i,
768
+ /\&\&\s*rm/i,
769
+ /\$\(.*rm/s,
770
+ /`.*rm/s,
771
+ /eval\s*\(/i,
772
+ /exec\s+/i,
773
+ /nc\s+-e/i,
774
+ /bash\s+-i/i,
775
+ /\/dev\/tcp\//i,
776
+ /curl\s+.*\|/i,
777
+ /wget\s+.*\|/i,
778
+ /chmod\s+777/i,
779
+ /chmod\s+4755/i,
780
+ />\s*\/dev\//i,
781
+ /2>\s*\/dev\//i,
782
+ /tee\s+/i
783
+ ];
784
+ var MAX_ARGS = 20;
785
+ var MAX_OUTPUT2 = 2e5;
786
+ var TIMEOUT_MS = 3e4;
787
+ var execTool = {
788
+ name: "exec",
789
+ description: "Restricted shell that only runs pre-approved commands with constrained arguments. Safer alternative to `bash`.",
790
+ usageHint: "Set `command` (must be in allowlist). `args` passed through. Unknown commands require `allow_unknown: true`. Blocks dangerous patterns.",
791
+ permission: "confirm",
792
+ mutating: false,
793
+ timeoutMs: TIMEOUT_MS,
794
+ inputSchema: {
795
+ type: "object",
796
+ properties: {
797
+ command: { type: "string", description: "Command to run (must be in allowlist)" },
798
+ args: { type: "array", items: { type: "string" }, description: "Arguments" },
799
+ cwd: { type: "string", description: "Working directory" },
800
+ timeout: { type: "integer", description: "Timeout in ms (default: 30000)" },
801
+ allow_unknown: {
802
+ type: "boolean",
803
+ description: "Allow commands not in allowlist (DANGEROUS, use with caution)"
804
+ }
805
+ },
806
+ required: ["command"]
807
+ },
808
+ async execute(input, ctx, opts) {
809
+ const cmd = input.command.trim();
810
+ if (!cmd) return { command: cmd, args: [], stdout: "", stderr: "Empty command", exitCode: 1, truncated: false, allowed: false };
811
+ if (FORBIDDEN_PATTERNS.some((re) => re.test(cmd))) {
812
+ return {
813
+ command: cmd,
814
+ args: input.args ?? [],
815
+ stdout: "",
816
+ stderr: `Command blocked: dangerous pattern detected`,
817
+ exitCode: 1,
818
+ truncated: false,
819
+ allowed: false
820
+ };
821
+ }
822
+ const allowedCommands = { ...ALLOWED_COMMANDS };
823
+ if (input.allow_unknown) {
824
+ allowedCommands[cmd] = [];
825
+ }
826
+ if (!(cmd in allowedCommands)) {
827
+ return {
828
+ command: cmd,
829
+ args: input.args ?? [],
830
+ stdout: "",
831
+ stderr: `Command "${cmd}" not in allowlist. Set allow_unknown: true to bypass.`,
832
+ exitCode: 1,
833
+ truncated: false,
834
+ allowed: false
835
+ };
836
+ }
837
+ const args = (input.args ?? []).slice(0, MAX_ARGS);
838
+ const timeout = Math.min(input.timeout ?? TIMEOUT_MS, TIMEOUT_MS);
839
+ const cwd = input.cwd ?? ctx.cwd;
840
+ const signal = opts.signal;
841
+ return runCommand(cmd, args, cwd, timeout, signal);
842
+ }
843
+ };
844
+ function runCommand(cmd, args, cwd, timeout, signal) {
845
+ return new Promise((resolve2) => {
846
+ let stdout = "";
847
+ let stderr = "";
848
+ let killed = false;
849
+ const child = spawn(cmd, args, { cwd, signal, stdio: ["ignore", "pipe", "pipe"] });
850
+ const timer = setTimeout(() => {
851
+ killed = true;
852
+ child.kill("SIGTERM");
853
+ }, timeout);
854
+ child.stdout?.on("data", (chunk) => {
855
+ if (stdout.length < MAX_OUTPUT2) stdout += chunk.toString();
856
+ });
857
+ child.stderr?.on("data", (chunk) => {
858
+ if (stderr.length < MAX_OUTPUT2) stderr += chunk.toString();
859
+ });
860
+ child.on("close", (code) => {
861
+ clearTimeout(timer);
862
+ resolve2({
863
+ command: cmd,
864
+ args,
865
+ stdout: stdout.slice(0, MAX_OUTPUT2),
866
+ stderr: stderr.slice(0, MAX_OUTPUT2),
867
+ exitCode: killed ? 124 : code ?? 1,
868
+ truncated: stdout.length >= MAX_OUTPUT2 || stderr.length >= MAX_OUTPUT2,
869
+ allowed: true
870
+ });
871
+ });
872
+ child.on("error", (err) => {
873
+ clearTimeout(timer);
874
+ resolve2({
875
+ command: cmd,
876
+ args,
877
+ stdout: stdout.slice(0, MAX_OUTPUT2),
878
+ stderr: err.message,
879
+ exitCode: 1,
880
+ truncated: false,
881
+ allowed: true
882
+ });
883
+ });
884
+ });
885
+ }
886
+ var MAX_BYTES2 = 131072;
887
+ var TIMEOUT_MS2 = 2e4;
888
+ var PRIVATE_RANGES = [
889
+ /^10\./,
890
+ /^192\.168\./,
891
+ /^172\.(1[6-9]|2[0-9]|3[01])\./,
892
+ /^127\./,
893
+ /^0\./,
894
+ /^169\.254\./,
895
+ /^::1$/,
896
+ /^fc/i,
897
+ /^fe80:/i
898
+ ];
899
+ var ALLOW_PRIVATE = process.env["WRONGSTACK_FETCH_ALLOW_PRIVATE"] === "1";
900
+ async function fetchWithRedirectLimit(url, maxRedirects, signal) {
901
+ const headers = {
902
+ "user-agent": "WrongStack/1.0 (+https://wrongstack.com)",
903
+ accept: "text/html,application/json;q=0.9,text/plain;q=0.8,*/*;q=0.1"
904
+ };
905
+ let redirectCount = 0;
906
+ let currentUrl = url;
907
+ for (; ; ) {
908
+ const res = await fetch(currentUrl, {
909
+ redirect: "manual",
910
+ signal,
911
+ headers
912
+ });
913
+ if (res.status < 300 || res.status > 399) {
914
+ return res;
915
+ }
916
+ redirectCount++;
917
+ if (redirectCount > maxRedirects) {
918
+ throw new Error(`fetch: exceeded ${maxRedirects} redirects`);
919
+ }
920
+ const location = res.headers.get("location");
921
+ if (!location) {
922
+ throw new Error("fetch: redirect status with no location header");
923
+ }
924
+ currentUrl = new URL(location, currentUrl).toString();
925
+ }
926
+ }
927
+ var fetchTool = {
928
+ name: "fetch",
929
+ description: "Fetch the contents of a URL. HTML is converted to markdown by default.",
930
+ usageHint: "HTTPS only by default. Localhost and RFC1918 ranges blocked unless WRONGSTACK_FETCH_ALLOW_PRIVATE=1. Max 5 redirects, 20s timeout, 128KB cap.",
931
+ permission: "confirm",
932
+ mutating: false,
933
+ timeoutMs: TIMEOUT_MS2,
934
+ maxOutputBytes: MAX_BYTES2,
935
+ inputSchema: {
936
+ type: "object",
937
+ properties: {
938
+ url: { type: "string" },
939
+ format: { type: "string", enum: ["markdown", "text", "raw"] }
940
+ },
941
+ required: ["url"]
942
+ },
943
+ async execute(input, _ctx, opts) {
944
+ if (!input?.url) throw new Error("fetch: url is required");
945
+ const u = new URL(input.url);
946
+ if (u.protocol !== "https:" && u.protocol !== "http:") {
947
+ throw new Error(`fetch: unsupported protocol "${u.protocol}"`);
948
+ }
949
+ if (u.protocol === "http:" && !ALLOW_PRIVATE) {
950
+ throw new Error("fetch: http:// blocked (HTTPS required by default)");
951
+ }
952
+ await assertNotPrivate(u.hostname);
953
+ const ctrl = new AbortController();
954
+ const timer = setTimeout(() => ctrl.abort(new Error("fetch timeout")), TIMEOUT_MS2);
955
+ const combined = combineSignals(opts.signal, ctrl.signal);
956
+ try {
957
+ const res = await fetchWithRedirectLimit(input.url, 5, combined);
958
+ const ct = res.headers.get("content-type") ?? "application/octet-stream";
959
+ if (/^image\/|^audio\/|^video\/|application\/octet-stream/.test(ct)) {
960
+ throw new Error(`fetch: refusing to read binary content-type "${ct}"`);
961
+ }
962
+ const reader = res.body?.getReader();
963
+ let received = 0;
964
+ const chunks = [];
965
+ if (reader) {
966
+ for (; ; ) {
967
+ const { value, done } = await reader.read();
968
+ if (done) break;
969
+ if (!value) continue;
970
+ received += value.byteLength;
971
+ chunks.push(value);
972
+ if (received > MAX_BYTES2) break;
973
+ }
974
+ }
975
+ const text = Buffer.concat(chunks.map((c) => Buffer.from(c))).toString("utf8");
976
+ const format = input.format ?? (ct.includes("text/html") ? "markdown" : "text");
977
+ let content;
978
+ if (format === "raw") content = text;
979
+ else if (format === "markdown" && ct.includes("text/html")) content = htmlToMarkdown(text);
980
+ else if (ct.includes("application/json")) content = prettyJson(text);
981
+ else content = text;
982
+ return {
983
+ content: truncateMiddle(content, MAX_BYTES2),
984
+ status: res.status,
985
+ content_type: ct,
986
+ url: res.url
987
+ };
988
+ } finally {
989
+ clearTimeout(timer);
990
+ }
991
+ }
992
+ };
993
+ async function assertNotPrivate(hostname) {
994
+ if (ALLOW_PRIVATE) return;
995
+ if (PRIVATE_RANGES.some((r) => r.test(hostname))) {
996
+ throw new Error(`fetch: blocked private/loopback address "${hostname}"`);
997
+ }
998
+ if (hostname === "localhost" || hostname.endsWith(".localhost")) {
999
+ throw new Error("fetch: blocked localhost target");
1000
+ }
1001
+ try {
1002
+ const records = await dns.lookup(hostname, { all: true });
1003
+ for (const r of records) {
1004
+ if (PRIVATE_RANGES.some((re) => re.test(r.address))) {
1005
+ throw new Error(`fetch: resolved to private address ${r.address}`);
1006
+ }
1007
+ }
1008
+ } catch (err) {
1009
+ if (err instanceof Error && err.message.startsWith("fetch:")) throw err;
1010
+ }
1011
+ }
1012
+ function combineSignals(...sigs) {
1013
+ if (typeof AbortSignal.any === "function") {
1014
+ return AbortSignal.any(sigs);
1015
+ }
1016
+ const ctrl = new AbortController();
1017
+ for (const s of sigs) {
1018
+ if (s.aborted) {
1019
+ ctrl.abort(s.reason);
1020
+ break;
1021
+ }
1022
+ s.addEventListener("abort", () => ctrl.abort(s.reason), { once: true });
1023
+ }
1024
+ return ctrl.signal;
1025
+ }
1026
+ function prettyJson(s) {
1027
+ try {
1028
+ return JSON.stringify(JSON.parse(s), null, 2);
1029
+ } catch {
1030
+ return s;
1031
+ }
1032
+ }
1033
+ function htmlToMarkdown(html) {
1034
+ let s = html;
1035
+ s = s.replace(/<script[\s\S]*?<\/script>/gi, "");
1036
+ s = s.replace(/<style[\s\S]*?<\/style>/gi, "");
1037
+ s = s.replace(/<noscript[\s\S]*?<\/noscript>/gi, "");
1038
+ s = s.replace(/<h([1-6])[^>]*>([\s\S]*?)<\/h\1>/gi, (_m, n, c) => {
1039
+ return "\n" + "#".repeat(Number(n)) + " " + stripTags(c).trim() + "\n";
1040
+ });
1041
+ s = s.replace(/<(strong|b)[^>]*>([\s\S]*?)<\/\1>/gi, "**$2**");
1042
+ s = s.replace(/<(em|i)[^>]*>([\s\S]*?)<\/\1>/gi, "*$2*");
1043
+ s = s.replace(/<a [^>]*href="([^"]+)"[^>]*>([\s\S]*?)<\/a>/gi, "[$2]($1)");
1044
+ s = s.replace(/<pre[^>]*>([\s\S]*?)<\/pre>/gi, (_m, c) => "\n```\n" + stripTags(c) + "\n```\n");
1045
+ s = s.replace(/<code[^>]*>([\s\S]*?)<\/code>/gi, "`$1`");
1046
+ s = s.replace(/<li[^>]*>([\s\S]*?)<\/li>/gi, "- $1\n");
1047
+ s = s.replace(/<br\s*\/?>/gi, "\n");
1048
+ s = s.replace(/<\/p>/gi, "\n\n");
1049
+ s = stripTags(s);
1050
+ s = s.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&nbsp;/g, " ");
1051
+ return s.replace(/\n{3,}/g, "\n\n").trim();
1052
+ }
1053
+ function stripTags(s) {
1054
+ return s.replace(/<[^>]+>/g, "");
1055
+ }
1056
+
1057
+ // src/search.ts
1058
+ var DEFAULT_NUM = 10;
1059
+ var MAX_RESULTS = 50;
1060
+ var TIMEOUT_MS3 = 15e3;
1061
+ var searchTool = {
1062
+ name: "search",
1063
+ description: "Search the web for information. Returns title, URL, and snippet for each result.",
1064
+ usageHint: "Set `num_results` (1-50, default 10). Use `source` to pick engine: duckduckgo (default), google, bing.",
1065
+ permission: "confirm",
1066
+ mutating: false,
1067
+ timeoutMs: TIMEOUT_MS3,
1068
+ inputSchema: {
1069
+ type: "object",
1070
+ properties: {
1071
+ query: { type: "string", description: "Search query" },
1072
+ num_results: {
1073
+ type: "integer",
1074
+ description: "Number of results (1-50, default 10)",
1075
+ minimum: 1,
1076
+ maximum: MAX_RESULTS
1077
+ },
1078
+ source: {
1079
+ type: "string",
1080
+ enum: ["duckduckgo", "google", "bing"],
1081
+ description: "Search engine to use (default: duckduckgo)"
1082
+ }
1083
+ },
1084
+ required: ["query"]
1085
+ },
1086
+ async execute(input, _ctx, opts) {
1087
+ if (!input?.query) throw new Error("search: query is required");
1088
+ const num = Math.max(1, Math.min(input.num_results ?? DEFAULT_NUM, MAX_RESULTS));
1089
+ const source = input.source ?? "duckduckgo";
1090
+ switch (source) {
1091
+ case "duckduckgo":
1092
+ return await duckduckgoSearch(input.query, num, opts.signal);
1093
+ case "google":
1094
+ return await googleSearch(input.query, num, opts.signal);
1095
+ case "bing":
1096
+ return await bingSearch(input.query, num, opts.signal);
1097
+ default:
1098
+ throw new Error(`search: unknown source "${source}"`);
1099
+ }
1100
+ }
1101
+ };
1102
+ async function duckduckgoSearch(query2, num, signal) {
1103
+ const encoded = encodeURIComponent(query2);
1104
+ const url = `https://lite.duckduckgo.com/lite/?q=${encoded}&kd=-1&kl=wt-wt`;
1105
+ const results = await fetchWithTimeout(url, signal, TIMEOUT_MS3).then((r) => r.text()).then((html) => parseDuckDuckGo(html, num)).catch(() => [{ title: "Search unavailable", url: "", snippet: "Could not reach DuckDuckGo" }]);
1106
+ return {
1107
+ query: query2,
1108
+ results,
1109
+ source: "duckduckgo",
1110
+ truncated: results.length >= num
1111
+ };
1112
+ }
1113
+ function parseDuckDuckGo(html, num) {
1114
+ const results = [];
1115
+ const snippetRegex = /<a class="result-link"[^>]+href="([^"]+)"[^>]*>([^<]+)<\/a>/gi;
1116
+ const snippet2Regex = /<a class="result-snippet"[^>]*>([^<]+)<\/a>/gi;
1117
+ let match;
1118
+ const linkMatches = [];
1119
+ while ((match = snippetRegex.exec(html)) !== null && linkMatches.length < num) {
1120
+ if (!match[1] || !match[2]) continue;
1121
+ linkMatches.push({ url: match[1], title: stripTags2(match[2]) });
1122
+ }
1123
+ const snippetMatches = [];
1124
+ while ((match = snippet2Regex.exec(html)) !== null && snippetMatches.length < num) {
1125
+ if (!match[1]) continue;
1126
+ snippetMatches.push(stripTags2(match[1]));
1127
+ }
1128
+ for (let i = 0; i < linkMatches.length && i < num; i++) {
1129
+ const entry = linkMatches[i];
1130
+ results.push({
1131
+ title: entry?.title ?? "",
1132
+ url: entry?.url ?? "",
1133
+ snippet: snippetMatches[i] ?? ""
1134
+ });
1135
+ }
1136
+ return results;
1137
+ }
1138
+ async function googleSearch(query2, num, signal) {
1139
+ const encoded = encodeURIComponent(query2);
1140
+ const url = `https://www.google.com/search?q=${encoded}&hl=en`;
1141
+ const html = await fetchWithTimeout(url, signal, TIMEOUT_MS3).then((r) => r.text()).catch(() => "");
1142
+ const results = parseGoogleResults(html, num);
1143
+ return {
1144
+ query: query2,
1145
+ results,
1146
+ source: "google",
1147
+ truncated: results.length >= num
1148
+ };
1149
+ }
1150
+ function parseGoogleResults(html, num) {
1151
+ const results = [];
1152
+ const titleRegex = /<h3[^>]*class="[^"]*DKV84"[^>]*>([^<]+)<\/h3>/gi;
1153
+ const urlRegex = /<cite[^>]*>([^<]+)<\/cite>/gi;
1154
+ const snippetRegex = /<span[^>]*class="[^"]*aXCZ0b[^>]*>([^<]+)<\/span>/gi;
1155
+ const titles = [];
1156
+ let m;
1157
+ while ((m = titleRegex.exec(html)) !== null && titles.length < num) {
1158
+ if (!m[1]) continue;
1159
+ titles.push(stripTags2(m[1]));
1160
+ }
1161
+ const urls = [];
1162
+ while ((m = urlRegex.exec(html)) !== null && urls.length < num) {
1163
+ if (!m[1]) continue;
1164
+ const url = stripTags2(m[1]).replace(/^\*(https?:\/\/[^\s]+).*$/, "$1");
1165
+ if (url.startsWith("http")) urls.push(url);
1166
+ }
1167
+ const snippets = [];
1168
+ while ((m = snippetRegex.exec(html)) !== null && snippets.length < num) {
1169
+ if (!m[1]) continue;
1170
+ snippets.push(stripTags2(m[1]));
1171
+ }
1172
+ for (let i = 0; i < Math.min(titles.length, num); i++) {
1173
+ results.push({
1174
+ title: titles[i] ?? "",
1175
+ url: urls[i] ?? "",
1176
+ snippet: snippets[i] ?? ""
1177
+ });
1178
+ }
1179
+ return results;
1180
+ }
1181
+ async function bingSearch(query2, num, signal) {
1182
+ const encoded = encodeURIComponent(query2);
1183
+ const url = `https://www.bing.com/search?q=${encoded}`;
1184
+ const html = await fetchWithTimeout(url, signal, TIMEOUT_MS3).then((r) => r.text()).catch(() => "");
1185
+ const results = parseBingResults(html, num);
1186
+ return {
1187
+ query: query2,
1188
+ results,
1189
+ source: "bing",
1190
+ truncated: results.length >= num
1191
+ };
1192
+ }
1193
+ function parseBingResults(html, num) {
1194
+ const results = [];
1195
+ const titleRegex = /<h2[^>]*>\s*<a[^>]+href="([^"]+)"[^>]*>([^<]+)<\/a>\s*<\/h2>/gi;
1196
+ const snippetRegex = /<p[^>]*class="[^"]*b_paractl[^"]*"[^>]*>([^<]+)<\/p>/gi;
1197
+ const entries = [];
1198
+ let m;
1199
+ while ((m = titleRegex.exec(html)) !== null && entries.length < num) {
1200
+ if (!m[1] || !m[2]) continue;
1201
+ entries.push({ url: m[1], title: stripTags2(m[2]) });
1202
+ }
1203
+ const snippets = [];
1204
+ while ((m = snippetRegex.exec(html)) !== null && snippets.length < num) {
1205
+ if (!m[1]) continue;
1206
+ snippets.push(stripTags2(m[1]));
1207
+ }
1208
+ for (let i = 0; i < entries.length; i++) {
1209
+ results.push({
1210
+ title: entries[i]?.title ?? "",
1211
+ url: entries[i]?.url ?? "",
1212
+ snippet: snippets[i] ?? ""
1213
+ });
1214
+ }
1215
+ return results;
1216
+ }
1217
+ async function fetchWithTimeout(url, signal, timeoutMs) {
1218
+ const controller = new AbortController();
1219
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
1220
+ const fetchSignal = anySignal(signal, controller.signal);
1221
+ try {
1222
+ const res = await fetch(url, {
1223
+ headers: {
1224
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
1225
+ },
1226
+ signal: fetchSignal
1227
+ });
1228
+ clearTimeout(timer);
1229
+ return res;
1230
+ } catch (e) {
1231
+ clearTimeout(timer);
1232
+ throw e;
1233
+ }
1234
+ }
1235
+ function anySignal(...signals) {
1236
+ const controller = new AbortController();
1237
+ for (const s of signals) {
1238
+ if (s.aborted) {
1239
+ controller.abort();
1240
+ break;
1241
+ }
1242
+ s.addEventListener("abort", () => controller.abort());
1243
+ }
1244
+ return controller.signal;
1245
+ }
1246
+ function stripTags2(html) {
1247
+ return html.replace(/<[^>]+>/g, "").replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&#39;/g, "'").trim();
1248
+ }
1249
+
1250
+ // src/todo.ts
1251
+ var todoTool = {
1252
+ name: "todo",
1253
+ description: "Replace the current todo list with a new set of items.",
1254
+ usageHint: "Use for multi-step tasks. Replace the full list on each call. At most ONE task may be in_progress at a time. Items have id, content, status (pending|in_progress|completed), and optional activeForm.",
1255
+ permission: "auto",
1256
+ mutating: false,
1257
+ timeoutMs: 1e3,
1258
+ inputSchema: {
1259
+ type: "object",
1260
+ properties: {
1261
+ todos: {
1262
+ type: "array",
1263
+ items: {
1264
+ type: "object",
1265
+ properties: {
1266
+ id: { type: "string" },
1267
+ content: { type: "string" },
1268
+ status: { type: "string", enum: ["pending", "in_progress", "completed"] },
1269
+ activeForm: { type: "string" }
1270
+ },
1271
+ required: ["id", "content", "status"]
1272
+ }
1273
+ }
1274
+ },
1275
+ required: ["todos"]
1276
+ },
1277
+ async execute(input, ctx) {
1278
+ if (!Array.isArray(input?.todos)) {
1279
+ throw new Error("todo: todos must be an array");
1280
+ }
1281
+ const items = input.todos.filter((t) => Boolean(t?.id && t.content));
1282
+ const inProgress = items.filter((t) => t.status === "in_progress");
1283
+ if (inProgress.length > 1) {
1284
+ let seenInProgress = false;
1285
+ for (const item of items) {
1286
+ if (item.status === "in_progress") {
1287
+ if (seenInProgress) item.status = "pending";
1288
+ seenInProgress = true;
1289
+ }
1290
+ }
1291
+ }
1292
+ ctx.todos = items;
1293
+ return {
1294
+ count: items.length,
1295
+ in_progress: items.filter((t) => t.status === "in_progress").length
1296
+ };
1297
+ }
1298
+ };
1299
+ var TIMEOUT_MS4 = 3e4;
1300
+ var MAX_OUTPUT3 = 1e5;
1301
+ var gitTool = {
1302
+ name: "git",
1303
+ description: "Run git commands. Wraps common operations: status, log, diff, commit, branch, checkout, stash, push, pull, fetch, reset.",
1304
+ usageHint: "Prefer built-in subcommands over raw args. `command` is required. `message` for commits. `branch` for checkout/branch. `files` for status/diff. `format` for log.",
1305
+ permission: "confirm",
1306
+ mutating: ["commit", "push", "pull", "checkout", "stash", "reset"].includes(
1307
+ "commit"
1308
+ // this is for type-check only; runtime check below
1309
+ ),
1310
+ timeoutMs: TIMEOUT_MS4,
1311
+ inputSchema: {
1312
+ type: "object",
1313
+ properties: {
1314
+ command: {
1315
+ type: "string",
1316
+ enum: [
1317
+ "status",
1318
+ "log",
1319
+ "diff",
1320
+ "commit",
1321
+ "branch",
1322
+ "checkout",
1323
+ "stash",
1324
+ "push",
1325
+ "pull",
1326
+ "fetch",
1327
+ "reset"
1328
+ ],
1329
+ description: "Git subcommand"
1330
+ },
1331
+ args: { type: "string", description: "Raw args string (bypasses subcommand logic)" },
1332
+ files: {
1333
+ type: "string",
1334
+ description: 'File(s) for status/diff: single path, comma-separated list, or "**/*.ts" glob'
1335
+ },
1336
+ message: { type: "string", description: "Commit message (required for commit)" },
1337
+ branch: { type: "string", description: "Branch name for checkout/branch" },
1338
+ format: {
1339
+ type: "string",
1340
+ enum: ["short", "oneline", "stat", "graph"],
1341
+ description: "Log format (default: short)"
1342
+ },
1343
+ limit: { type: "integer", description: "Limit for log (default: 20)" },
1344
+ dry_run: { type: "boolean", description: "For commit: show what would be committed" }
1345
+ },
1346
+ required: ["command"]
1347
+ },
1348
+ async execute(input, ctx, opts) {
1349
+ if (!input?.command) throw new Error("git: command is required");
1350
+ const gitDir = findGitDir(ctx.cwd);
1351
+ if (!gitDir) {
1352
+ return {
1353
+ command: input.command,
1354
+ stdout: "",
1355
+ stderr: "Not in a git repository",
1356
+ exitCode: 128,
1357
+ truncated: false
1358
+ };
1359
+ }
1360
+ const args = buildArgs(input);
1361
+ return await runGit(args, gitDir, opts.signal);
1362
+ }
1363
+ };
1364
+ function findGitDir(cwd) {
1365
+ let dir = cwd;
1366
+ for (let i = 0; i < 20; i++) {
1367
+ try {
1368
+ const stat9 = __require("fs").statSync(`${dir}/.git`);
1369
+ if (stat9.isDirectory()) return dir;
1370
+ } catch {
1371
+ }
1372
+ const parent = __require("path").dirname(dir);
1373
+ if (parent === dir) break;
1374
+ dir = parent;
1375
+ }
1376
+ return null;
1377
+ }
1378
+ function buildArgs(input) {
1379
+ if (input.args) return input.args.split(/\s+/).filter(Boolean);
1380
+ const limit = input.limit ?? 20;
1381
+ const files = input.files ? (Array.isArray(input.files) ? input.files : input.files.split(",")).map((s) => s.trim()).filter(Boolean) : [];
1382
+ switch (input.command) {
1383
+ case "status":
1384
+ return ["status", ...files.length ? ["--", ...files] : []];
1385
+ case "log":
1386
+ return [
1387
+ "log",
1388
+ `--max-count=${limit}`,
1389
+ ...input.format === "oneline" ? ["--oneline"] : [],
1390
+ ...input.format === "stat" ? ["--stat"] : [],
1391
+ ...input.format === "graph" ? ["--oneline", "--graph", "--decorate"] : [],
1392
+ ...input.format === "short" || !input.format ? [] : []
1393
+ ];
1394
+ case "diff":
1395
+ return [
1396
+ "diff",
1397
+ "--no-color",
1398
+ ...files.length ? ["--", ...files] : []
1399
+ ];
1400
+ case "commit":
1401
+ return [
1402
+ "commit",
1403
+ ...input.dry_run ? ["--dry-run", "--porcelain"] : [],
1404
+ ...input.message ? ["-m", input.message] : [],
1405
+ ...files.length ? ["--", ...files] : []
1406
+ ];
1407
+ case "branch":
1408
+ return input.branch ? ["branch", input.branch] : ["branch"];
1409
+ case "checkout":
1410
+ return ["checkout", ...input.branch ? [input.branch] : [], ...files.length ? ["--", ...files] : []];
1411
+ case "stash":
1412
+ return input.message ? ["stash", "push", "-m", input.message] : ["stash", "push"];
1413
+ case "push":
1414
+ return ["push"];
1415
+ case "pull":
1416
+ return ["pull"];
1417
+ case "fetch":
1418
+ return ["fetch", ...input.branch ? [input.branch] : ["--all"]];
1419
+ case "reset":
1420
+ return ["reset"];
1421
+ default:
1422
+ return [input.command];
1423
+ }
1424
+ }
1425
+ function runGit(args, cwd, signal) {
1426
+ return new Promise((resolve2) => {
1427
+ let stdout = "";
1428
+ let stderr = "";
1429
+ const child = spawn("git", args, {
1430
+ cwd,
1431
+ signal,
1432
+ stdio: ["ignore", "pipe", "pipe"]
1433
+ });
1434
+ child.stdout?.on("data", (chunk) => {
1435
+ if (stdout.length < MAX_OUTPUT3) {
1436
+ stdout += chunk.toString();
1437
+ }
1438
+ });
1439
+ child.stderr?.on("data", (chunk) => {
1440
+ if (stderr.length < MAX_OUTPUT3) {
1441
+ stderr += chunk.toString();
1442
+ }
1443
+ });
1444
+ child.on("error", (err) => {
1445
+ resolve2({
1446
+ command: args[0],
1447
+ stdout,
1448
+ stderr: err.message,
1449
+ exitCode: 1,
1450
+ truncated: stdout.length >= MAX_OUTPUT3
1451
+ });
1452
+ });
1453
+ child.on("close", (code) => {
1454
+ resolve2({
1455
+ command: args[0],
1456
+ stdout: stdout.slice(0, MAX_OUTPUT3),
1457
+ stderr: stderr.slice(0, MAX_OUTPUT3),
1458
+ exitCode: code ?? 1,
1459
+ truncated: stdout.length >= MAX_OUTPUT3 || stderr.length >= MAX_OUTPUT3
1460
+ });
1461
+ });
1462
+ });
1463
+ }
1464
+ var patchTool = {
1465
+ name: "patch",
1466
+ description: "Apply a unified diff patch to files. Writes .orig and .rej files on failure.",
1467
+ usageHint: "Set `patch` (the diff text). `directory` defaults to cwd. `strip` removes leading path components. `dry_run` previews.",
1468
+ permission: "confirm",
1469
+ mutating: true,
1470
+ timeoutMs: 3e4,
1471
+ inputSchema: {
1472
+ type: "object",
1473
+ properties: {
1474
+ patch: { type: "string", description: "Unified diff patch content" },
1475
+ directory: { type: "string", description: "Root directory for patch (default: cwd)" },
1476
+ strip: { type: "integer", description: "Strip leading path components (default: 1)" },
1477
+ dry_run: { type: "boolean", description: "Preview without applying" }
1478
+ },
1479
+ required: ["patch"]
1480
+ },
1481
+ async execute(input, ctx, opts) {
1482
+ if (!input?.patch) throw new Error("patch: patch content is required");
1483
+ const dir = input.directory ? safeResolve(input.directory, ctx) : ctx.cwd;
1484
+ const strip = input.strip ?? 1;
1485
+ const dryRun = input.dry_run ?? false;
1486
+ const patchFile = path.join(dir, `.wstack_patch_${Date.now()}.diff`);
1487
+ await fs4.writeFile(patchFile, input.patch, "utf8");
1488
+ const args = [
1489
+ "-p" + strip,
1490
+ "--merge",
1491
+ ...dryRun ? ["--dry-run"] : [],
1492
+ "-i",
1493
+ patchFile
1494
+ ];
1495
+ const result = await runPatch(args, dir, opts.signal);
1496
+ await fs4.unlink(patchFile).catch(() => {
1497
+ });
1498
+ if (result.exitCode !== 0 && !dryRun) {
1499
+ return {
1500
+ applied: 0,
1501
+ rejected: 1,
1502
+ files: [],
1503
+ dry_run: dryRun,
1504
+ message: `patch failed: ${result.stderr || result.stdout}`
1505
+ };
1506
+ }
1507
+ return {
1508
+ applied: result.stdout.includes("patching file") ? 1 : 0,
1509
+ rejected: 0,
1510
+ files: extractPatchedFiles(result.stdout),
1511
+ dry_run: dryRun,
1512
+ message: result.stdout || "patch applied"
1513
+ };
1514
+ }
1515
+ };
1516
+ function runPatch(args, cwd, signal) {
1517
+ return new Promise((resolve2) => {
1518
+ let stdout = "";
1519
+ let stderr = "";
1520
+ const child = spawn("patch", args, { cwd, signal, stdio: ["pipe", "pipe", "pipe"] });
1521
+ child.stdout?.on("data", (c) => {
1522
+ stdout += c.toString();
1523
+ });
1524
+ child.stderr?.on("data", (c) => {
1525
+ stderr += c.toString();
1526
+ });
1527
+ child.on("close", (code) => resolve2({ exitCode: code ?? 1, stdout, stderr }));
1528
+ child.on("error", (e) => resolve2({ exitCode: 1, stdout: "", stderr: e.message }));
1529
+ });
1530
+ }
1531
+ function extractPatchedFiles(output) {
1532
+ const files = [];
1533
+ const re = /patching file (.+)/gi;
1534
+ let m;
1535
+ while ((m = re.exec(output)) !== null) {
1536
+ if (m[1]) files.push(m[1]);
1537
+ }
1538
+ return files;
1539
+ }
1540
+ var jsonTool = {
1541
+ name: "json",
1542
+ description: "Parse, query, and validate JSON/JSON5/YAML. Use `query` with JMESPath-like paths to extract values.",
1543
+ usageHint: 'Provide `file` path or `data` string. `query` supports dot notation (e.g. "results[0].name"). `format` outputs in specified format.',
1544
+ permission: "auto",
1545
+ mutating: false,
1546
+ timeoutMs: 5e3,
1547
+ inputSchema: {
1548
+ type: "object",
1549
+ properties: {
1550
+ file: { type: "string", description: "Path to JSON/JSON5/YAML file" },
1551
+ data: { type: "string", description: "JSON/JSON5/YAML string (alternative to file)" },
1552
+ query: { type: "string", description: 'JMESPath-like query (e.g. "a.b[0].c" or "a[*].name")' },
1553
+ format: {
1554
+ type: "string",
1555
+ enum: ["json", "json5", "yaml"],
1556
+ description: "Output format (default: json)"
1557
+ },
1558
+ validate: {
1559
+ type: "boolean",
1560
+ description: "Validate syntax only, no output (default: false)"
1561
+ }
1562
+ }
1563
+ },
1564
+ async execute(input) {
1565
+ const format = input.format ?? "json";
1566
+ let parsed;
1567
+ let raw;
1568
+ if (input.file) {
1569
+ try {
1570
+ raw = await fs4.readFile(input.file, "utf8");
1571
+ } catch {
1572
+ return { data: null, formatted: "", type: "unknown", error: `Could not read file` };
1573
+ }
1574
+ } else if (input.data) {
1575
+ raw = input.data;
1576
+ } else {
1577
+ return { data: null, formatted: "", type: "unknown", error: "Provide file or data" };
1578
+ }
1579
+ try {
1580
+ parsed = JSON.parse(raw);
1581
+ } catch (e) {
1582
+ return {
1583
+ data: null,
1584
+ formatted: "",
1585
+ type: "unknown",
1586
+ error: `Parse failed: ${e instanceof Error ? e.message : String(e)}`
1587
+ };
1588
+ }
1589
+ if (input.validate) {
1590
+ return {
1591
+ data: parsed,
1592
+ formatted: "valid",
1593
+ type: Array.isArray(parsed) ? "array" : typeof parsed,
1594
+ keys: typeof parsed === "object" && parsed !== null ? Object.keys(parsed) : void 0
1595
+ };
1596
+ }
1597
+ const queryResult = input.query ? query(parsed, input.query) : void 0;
1598
+ const formatted = formatOutput(queryResult ?? parsed, format);
1599
+ return {
1600
+ data: parsed,
1601
+ formatted,
1602
+ type: Array.isArray(parsed) ? "array" : typeof parsed,
1603
+ keys: typeof parsed === "object" && parsed !== null ? Object.keys(parsed) : void 0,
1604
+ query_result: queryResult
1605
+ };
1606
+ }
1607
+ };
1608
+ function query(data, path10) {
1609
+ const parts = path10.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
1610
+ let current = data;
1611
+ for (const part of parts) {
1612
+ if (current === null || current === void 0) return void 0;
1613
+ const idx = Number(part);
1614
+ if (!Number.isNaN(idx) && Array.isArray(current)) {
1615
+ current = current[idx];
1616
+ } else if (typeof current === "object" && current !== null) {
1617
+ current = current[part];
1618
+ } else {
1619
+ return void 0;
1620
+ }
1621
+ }
1622
+ return current;
1623
+ }
1624
+ function formatOutput(data, format) {
1625
+ if (format === "json5") {
1626
+ return JSON.stringify(data, null, 2).replace(/,\s*}/g, "}").replace(/,\s*\]/g, "]");
1627
+ }
1628
+ if (format === "yaml") {
1629
+ return toYaml(data);
1630
+ }
1631
+ return JSON.stringify(data, null, 2);
1632
+ }
1633
+ function toYaml(data, indent = 0) {
1634
+ if (data === null) return "null\n";
1635
+ if (data === void 0) return "";
1636
+ if (typeof data === "boolean") return String(data) + "\n";
1637
+ if (typeof data === "number") return String(data) + "\n";
1638
+ if (typeof data === "string") {
1639
+ if (data.includes("\n") || data.includes(":") || data.includes("#")) {
1640
+ return `"${data.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"
1641
+ `;
1642
+ }
1643
+ return data + "\n";
1644
+ }
1645
+ if (Array.isArray(data)) {
1646
+ if (data.length === 0) return "[]\n";
1647
+ const prefix = " ".repeat(indent);
1648
+ return data.map((item) => `${prefix}- ${toYaml(item, indent + 1).trimStart()}`).join("");
1649
+ }
1650
+ if (typeof data === "object") {
1651
+ const prefix = " ".repeat(indent);
1652
+ const entries = Object.entries(data);
1653
+ return entries.map(([k, v]) => `${prefix}${k}: ${toYaml(v, indent + 1)}`).join("");
1654
+ }
1655
+ return String(data) + "\n";
1656
+ }
1657
+ var diffTool = {
1658
+ name: "diff",
1659
+ description: "Show differences between files, commits, or branches. Supports staged vs working tree.",
1660
+ usageHint: "Use `files` for file paths, `a`/`b` for commit refs, `staged` for git index. `mode`: unified (default), stat, side-by-side.",
1661
+ permission: "auto",
1662
+ mutating: false,
1663
+ timeoutMs: 1e4,
1664
+ inputSchema: {
1665
+ type: "object",
1666
+ properties: {
1667
+ path: { type: "string", description: "Working directory for diff" },
1668
+ files: {
1669
+ type: "string",
1670
+ description: 'File(s) to diff: single path, comma-separated, or "**/*.ts" glob'
1671
+ },
1672
+ a: { type: "string", description: "First commit/branch/ref (for git diff)" },
1673
+ b: { type: "string", description: "Second commit/branch/ref (for git diff)" },
1674
+ staged: { type: "boolean", description: "Diff staged changes only" },
1675
+ mode: {
1676
+ type: "string",
1677
+ enum: ["unified", "side-by-side", "stat"],
1678
+ description: "Output mode (default: unified)"
1679
+ },
1680
+ context: { type: "integer", description: "Context lines for unified diff (default: 3)" }
1681
+ }
1682
+ },
1683
+ async execute(input, ctx, opts) {
1684
+ if (input.a !== void 0 || input.b !== void 0) {
1685
+ return await gitDiff(input, ctx, opts.signal);
1686
+ }
1687
+ return await fileDiff(input, ctx, opts.signal);
1688
+ }
1689
+ };
1690
+ async function gitDiff(input, ctx, signal) {
1691
+ const gitDir = findGitDir2(ctx.cwd);
1692
+ if (!gitDir) {
1693
+ return { diff: "", files: [], truncated: false, mode: "unified" };
1694
+ }
1695
+ const args = ["diff", "--no-color"];
1696
+ if (input.staged) args.push("--staged");
1697
+ if (input.a) args.push(input.a);
1698
+ if (input.b) args.push(input.b);
1699
+ if (input.files) {
1700
+ const files = Array.isArray(input.files) ? input.files : input.files.split(",");
1701
+ args.push("--", ...files.map((f) => f.trim()));
1702
+ }
1703
+ const result = await runGit2(args, gitDir, signal);
1704
+ return {
1705
+ diff: result.stdout,
1706
+ files: [],
1707
+ truncated: result.stdout.length > 1e5,
1708
+ mode: "unified"
1709
+ };
1710
+ }
1711
+ function findGitDir2(cwd) {
1712
+ let dir = cwd;
1713
+ for (let i = 0; i < 20; i++) {
1714
+ try {
1715
+ const stat9 = __require("fs").statSync(`${dir}/.git`);
1716
+ if (stat9.isDirectory()) return dir;
1717
+ } catch {
1718
+ }
1719
+ const parent = __require("path").dirname(dir);
1720
+ if (parent === dir) break;
1721
+ dir = parent;
1722
+ }
1723
+ return null;
1724
+ }
1725
+ function runGit2(args, cwd, signal) {
1726
+ return new Promise((resolve2) => {
1727
+ let stdout = "";
1728
+ let stderr = "";
1729
+ const child = spawn("git", args, { cwd, signal, stdio: ["ignore", "pipe", "pipe"] });
1730
+ child.stdout?.on("data", (c) => {
1731
+ stdout += c.toString();
1732
+ });
1733
+ child.stderr?.on("data", (c) => {
1734
+ stderr += c.toString();
1735
+ });
1736
+ child.on("close", (code) => resolve2({ stdout, stderr, exitCode: code ?? 0 }));
1737
+ child.on("error", (e) => resolve2({ stdout: "", stderr: e.message, exitCode: 1 }));
1738
+ });
1739
+ }
1740
+ async function fileDiff(input, ctx, signal) {
1741
+ input.path ? safeResolve(input.path, ctx) : ctx.cwd;
1742
+ input.context ?? 3;
1743
+ const files = input.files ? (Array.isArray(input.files) ? input.files : input.files.split(",")).map((f) => f.trim()).filter(Boolean) : [];
1744
+ if (files.length === 0) {
1745
+ return { diff: "No files specified", files: [], truncated: false, mode: input.mode ?? "unified" };
1746
+ }
1747
+ const results = [];
1748
+ for (const file of files) {
1749
+ const absPath = safeResolve(file, ctx);
1750
+ const stat9 = await fs4.stat(absPath).catch(() => null);
1751
+ if (!stat9?.isFile()) continue;
1752
+ const content = await fs4.readFile(absPath, "utf8");
1753
+ const lines = content.split(/\r?\n/);
1754
+ results.push(`--- ${file}
1755
+ +++ ${file}
1756
+ ${formatUnified(lines)}`);
1757
+ }
1758
+ return {
1759
+ diff: results.join("\n"),
1760
+ files,
1761
+ truncated: false,
1762
+ mode: input.mode ?? "unified"
1763
+ };
1764
+ }
1765
+ function formatUnified(lines, context) {
1766
+ return lines.map((line, i) => ` ${line}`).join("\n");
1767
+ }
1768
+ var DEFAULT_IGNORE4 = ["node_modules", ".git", "dist", "build", ".next", "coverage", "__pycache__"];
1769
+ var treeTool = {
1770
+ name: "tree",
1771
+ description: "Display directory structure as an ASCII tree. Shows files and folders with indentation.",
1772
+ usageHint: "Set `path` (default: cwd). `depth` limits nesting (default: 3). `glob` filters files. `exclude` ignores dirs. `show_files` toggles file listing (default: true).",
1773
+ permission: "auto",
1774
+ mutating: false,
1775
+ timeoutMs: 15e3,
1776
+ inputSchema: {
1777
+ type: "object",
1778
+ properties: {
1779
+ path: { type: "string", description: "Root directory (default: cwd)" },
1780
+ depth: {
1781
+ type: "integer",
1782
+ description: "Max nesting depth (default: 3, 0 for unlimited)",
1783
+ minimum: 0,
1784
+ maximum: 20
1785
+ },
1786
+ glob: { type: "string", description: 'Filter files matching glob (e.g. "*.ts")' },
1787
+ exclude: {
1788
+ type: "array",
1789
+ items: { type: "string" },
1790
+ description: "Directory names to exclude"
1791
+ },
1792
+ show_files: {
1793
+ type: "boolean",
1794
+ description: "Show files (default: true, false shows dirs only)"
1795
+ },
1796
+ show_dirs: {
1797
+ type: "boolean",
1798
+ description: "Show directories (default: true)"
1799
+ },
1800
+ show_hidden: {
1801
+ type: "boolean",
1802
+ description: "Show hidden files starting with . (default: false)"
1803
+ }
1804
+ }
1805
+ },
1806
+ async execute(input, ctx) {
1807
+ const basePath = input.path ? safeResolve(input.path, ctx) : ctx.cwd;
1808
+ const maxDepth = input.depth ?? 3;
1809
+ const showFiles = input.show_files ?? true;
1810
+ const showDirs = input.show_dirs ?? true;
1811
+ const showHidden = input.show_hidden ?? false;
1812
+ const exclude = /* @__PURE__ */ new Set([...DEFAULT_IGNORE4, ...input.exclude ?? []]);
1813
+ const filterGlob = input.glob;
1814
+ const lines = [basePath];
1815
+ let totalFiles = 0;
1816
+ let totalDirs = 0;
1817
+ await walkDir(basePath, "", 0, {
1818
+ maxDepth,
1819
+ exclude,
1820
+ showFiles,
1821
+ showDirs,
1822
+ showHidden,
1823
+ filterGlob,
1824
+ lines,
1825
+ prefix: "",
1826
+ isLast: true,
1827
+ totalFiles: { value: 0 },
1828
+ totalDirs: { value: 0 }
1829
+ });
1830
+ return {
1831
+ tree: lines.join("\n"),
1832
+ total_files: totalFiles,
1833
+ total_dirs: totalDirs,
1834
+ truncated: false,
1835
+ path: basePath
1836
+ };
1837
+ }
1838
+ };
1839
+ async function walkDir(dir, name, depth, opts) {
1840
+ const entries = await fs4.readdir(dir, { withFileTypes: true }).catch(() => []);
1841
+ const filtered = entries.filter((e) => {
1842
+ if (!opts.showHidden && e.name.startsWith(".")) return false;
1843
+ if (opts.exclude.has(e.name)) return false;
1844
+ return true;
1845
+ });
1846
+ if (depth > 0) {
1847
+ const dirCount = filtered.filter((e) => e.isDirectory()).length;
1848
+ const fileCount = filtered.filter((e) => e.isFile()).length;
1849
+ opts.totalDirs.value += dirCount;
1850
+ opts.totalFiles.value += fileCount;
1851
+ }
1852
+ const items = filtered.sort((a, b) => {
1853
+ if (a.isDirectory() && !b.isDirectory()) return -1;
1854
+ if (!a.isDirectory() && b.isDirectory()) return 1;
1855
+ return a.name.localeCompare(b.name);
1856
+ });
1857
+ for (let i = 0; i < items.length; i++) {
1858
+ const entry = items[i];
1859
+ if (!entry) continue;
1860
+ const isLast = i === items.length - 1;
1861
+ const connector = opts.isLast ? " " : "\u2502 ";
1862
+ const branch = isLast ? "\u2514\u2500\u2500 " : "\u251C\u2500\u2500 ";
1863
+ const displayName = entry.name + (entry.isDirectory() ? "/" : "");
1864
+ if (!opts.showDirs && entry.isDirectory()) continue;
1865
+ if (!opts.showFiles && entry.isFile()) continue;
1866
+ opts.lines.push(opts.prefix + branch + displayName);
1867
+ if (entry.isDirectory() && (opts.maxDepth === 0 || depth < opts.maxDepth)) {
1868
+ const childPrefix = opts.prefix + connector;
1869
+ await walkDir(path.join(dir, entry.name), entry.name, depth + 1, {
1870
+ ...opts,
1871
+ prefix: childPrefix,
1872
+ isLast
1873
+ });
1874
+ }
1875
+ }
1876
+ }
1877
+ var lintTool = {
1878
+ name: "lint",
1879
+ description: "Run a linter on files. Auto-detects biome, eslint, or tslint. Use `fix` to auto-fix issues.",
1880
+ usageHint: "Set `files` (glob or comma-separated). `fix` applies corrections. `linter` forces specific tool.",
1881
+ permission: "confirm",
1882
+ mutating: false,
1883
+ timeoutMs: 6e4,
1884
+ inputSchema: {
1885
+ type: "object",
1886
+ properties: {
1887
+ files: {
1888
+ type: "string",
1889
+ description: 'Files/patterns: single path, comma-separated list, or glob (e.g. "src/**/*.ts")'
1890
+ },
1891
+ fix: { type: "boolean", description: "Auto-fix fixable issues (default: false)" },
1892
+ linter: {
1893
+ type: "string",
1894
+ enum: ["biome", "eslint", "tslint", "auto"],
1895
+ description: "Linter to use (default: auto-detect)"
1896
+ },
1897
+ cwd: { type: "string", description: "Working directory (default: cwd)" }
1898
+ }
1899
+ },
1900
+ async execute(input, ctx, opts) {
1901
+ const cwd = input.cwd ? safeResolve(input.cwd, ctx) : ctx.cwd;
1902
+ const linter = input.linter ?? "auto";
1903
+ const detected = linter === "auto" ? await detectLinter(cwd) : linter;
1904
+ if (!detected) {
1905
+ return {
1906
+ linter: "none",
1907
+ files_checked: 0,
1908
+ errors: 0,
1909
+ warnings: 0,
1910
+ output: "No linter found (biome.json, .eslintrc, tslint.json)",
1911
+ fix_applied: false,
1912
+ truncated: false
1913
+ };
1914
+ }
1915
+ return await runLinter(detected, input, cwd, opts.signal);
1916
+ }
1917
+ };
1918
+ async function detectLinter(cwd) {
1919
+ const { stat: stat9 } = __require("fs/promises");
1920
+ const checks = ["biome.json", ".eslintrc.json", "tslint.json", ".eslintrc.js", "tsconfig.json"];
1921
+ for (const f of checks) {
1922
+ try {
1923
+ await stat9(`${cwd}/${f}`);
1924
+ if (f.includes("biome")) return "biome";
1925
+ if (f.includes("eslint")) return "eslint";
1926
+ if (f.includes("tslint")) return "tslint";
1927
+ } catch {
1928
+ }
1929
+ }
1930
+ return "biome";
1931
+ }
1932
+ async function runLinter(linter, input, cwd, signal) {
1933
+ const args = ["lint"];
1934
+ if (input.fix) args.push("--write");
1935
+ if (input.files) {
1936
+ const files = Array.isArray(input.files) ? input.files : input.files.split(",");
1937
+ args.push("--", ...files.map((f) => f.trim()));
1938
+ }
1939
+ const result = await runCommand2(linter === "biome" ? "biome" : linter, args, cwd, signal);
1940
+ const errors = (result.stdout.match(/error/g) || []).length;
1941
+ const warnings = (result.stdout.match(/warning/g) || []).length;
1942
+ return {
1943
+ linter,
1944
+ files_checked: input.files ? Array.isArray(input.files) ? input.files.length : input.files.split(",").length : 0,
1945
+ errors,
1946
+ warnings,
1947
+ output: result.stdout,
1948
+ fix_applied: input.fix ?? false,
1949
+ truncated: result.truncated
1950
+ };
1951
+ }
1952
+ function runCommand2(cmd, args, cwd, signal) {
1953
+ return new Promise((resolve2) => {
1954
+ let stdout = "";
1955
+ let stderr = "";
1956
+ const MAX = 1e5;
1957
+ const child = spawn(cmd, args, { cwd, signal, stdio: ["ignore", "pipe", "pipe"] });
1958
+ child.stdout?.on("data", (c) => {
1959
+ if (stdout.length < MAX) stdout += c.toString();
1960
+ });
1961
+ child.stderr?.on("data", (c) => {
1962
+ if (stderr.length < MAX) stderr += c.toString();
1963
+ });
1964
+ child.on("close", (code) => resolve2({ stdout, stderr, exitCode: code ?? 0, truncated: stdout.length >= MAX }));
1965
+ child.on("error", (e) => resolve2({ stdout: "", stderr: e.message, exitCode: 1, truncated: false }));
1966
+ });
1967
+ }
1968
+ var formatTool = {
1969
+ name: "format",
1970
+ description: "Format files with biome or prettier. Use `check` to verify without modifying.",
1971
+ usageHint: "Set `files` (glob or comma-separated). `check` only validates. `fixer` forces tool.",
1972
+ permission: "confirm",
1973
+ mutating: true,
1974
+ timeoutMs: 6e4,
1975
+ inputSchema: {
1976
+ type: "object",
1977
+ properties: {
1978
+ files: {
1979
+ type: "string",
1980
+ description: "Files/patterns: single path, comma-separated list, or glob"
1981
+ },
1982
+ fixer: {
1983
+ type: "string",
1984
+ enum: ["biome", "prettier", "auto"],
1985
+ description: "Formatter to use (default: auto-detect)"
1986
+ },
1987
+ check: {
1988
+ type: "boolean",
1989
+ description: "Verify only, do not modify files (default: false)"
1990
+ },
1991
+ cwd: { type: "string", description: "Working directory (default: cwd)" }
1992
+ }
1993
+ },
1994
+ async execute(input, ctx, opts) {
1995
+ const cwd = input.cwd ? safeResolve(input.cwd, ctx) : ctx.cwd;
1996
+ const fixer = input.fixer ?? "auto";
1997
+ const detected = fixer === "auto" ? await detectFixer(cwd) : fixer;
1998
+ if (!detected) {
1999
+ return {
2000
+ fixer: "none",
2001
+ files_checked: 0,
2002
+ files_changed: 0,
2003
+ output: "No formatter found (biome.json, .prettierrc)",
2004
+ truncated: false
2005
+ };
2006
+ }
2007
+ return await runFormatter(detected, input, cwd, opts.signal);
2008
+ }
2009
+ };
2010
+ async function detectFixer(cwd) {
2011
+ const { stat: stat9 } = __require("fs/promises");
2012
+ try {
2013
+ await stat9(`${cwd}/biome.json`);
2014
+ return "biome";
2015
+ } catch {
2016
+ try {
2017
+ await stat9(`${cwd}/.prettierrc`);
2018
+ return "prettier";
2019
+ } catch {
2020
+ return "biome";
2021
+ }
2022
+ }
2023
+ }
2024
+ async function runFormatter(fixer, input, cwd, signal) {
2025
+ const args = ["format", "--write"];
2026
+ if (input.check) args[args.length - 1] = "--check";
2027
+ if (input.files) {
2028
+ const files = Array.isArray(input.files) ? input.files : input.files.split(",");
2029
+ args.push("--", ...files.map((f) => f.trim()));
2030
+ }
2031
+ return runCommand3(fixer, args, cwd, signal);
2032
+ }
2033
+ function runCommand3(cmd, args, cwd, signal) {
2034
+ return new Promise((resolve2) => {
2035
+ let stdout = "";
2036
+ let stderr = "";
2037
+ const MAX = 1e5;
2038
+ const child = spawn(cmd, args, { cwd, signal, stdio: ["ignore", "pipe", "pipe"] });
2039
+ child.stdout?.on("data", (c) => {
2040
+ if (stdout.length < MAX) stdout += c.toString();
2041
+ });
2042
+ child.stderr?.on("data", (c) => {
2043
+ if (stderr.length < MAX) stderr += c.toString();
2044
+ });
2045
+ child.on("close", (code) => {
2046
+ const changed = (stdout.match(/changed/g) || []).length;
2047
+ resolve2({
2048
+ fixer: cmd,
2049
+ files_checked: 0,
2050
+ files_changed: changed,
2051
+ output: stdout || stderr,
2052
+ truncated: stdout.length >= MAX
2053
+ });
2054
+ });
2055
+ child.on("error", (e) => resolve2({
2056
+ fixer: cmd,
2057
+ files_checked: 0,
2058
+ files_changed: 0,
2059
+ output: e.message,
2060
+ truncated: false
2061
+ }));
2062
+ });
2063
+ }
2064
+ var typecheckTool = {
2065
+ name: "typecheck",
2066
+ description: "Run TypeScript type checking with `tsc --noEmit`. Checks for type errors without compiling.",
2067
+ usageHint: "Set `project` for tsconfig path (default: nearest). `strict` enables strictest flags. `all` checks all projects in workspace.",
2068
+ permission: "confirm",
2069
+ mutating: false,
2070
+ timeoutMs: 12e4,
2071
+ inputSchema: {
2072
+ type: "object",
2073
+ properties: {
2074
+ project: { type: "string", description: "Path to tsconfig.json (default: auto-detect)" },
2075
+ cwd: { type: "string", description: "Working directory (default: cwd)" },
2076
+ strict: {
2077
+ type: "boolean",
2078
+ description: "Add --strict flag for maximum type checking (default: false)"
2079
+ },
2080
+ all: {
2081
+ type: "boolean",
2082
+ description: "Type-check all projects (pnpm -r) (default: false)"
2083
+ }
2084
+ }
2085
+ },
2086
+ async execute(input, ctx, opts) {
2087
+ const cwd = input.cwd ? safeResolve(input.cwd, ctx) : ctx.cwd;
2088
+ if (input.all) {
2089
+ return runTsc(["--noEmit"], cwd, opts.signal, "workspace");
2090
+ }
2091
+ const tsconfig = input.project ? safeResolve(input.project, ctx) : await findTsConfig(cwd);
2092
+ const args = ["--noEmit"];
2093
+ if (input.strict) args.push("--strict");
2094
+ if (tsconfig) args.push("--project", tsconfig);
2095
+ return runTsc(args, cwd, opts.signal, tsconfig ?? "default");
2096
+ }
2097
+ };
2098
+ async function findTsConfig(cwd) {
2099
+ const { stat: stat9 } = __require("fs/promises");
2100
+ const candidates = ["tsconfig.json", "tsconfig.base.json"];
2101
+ for (const f of candidates) {
2102
+ try {
2103
+ const s = await stat9(path.join(cwd, f));
2104
+ if (s.isFile()) return path.join(cwd, f);
2105
+ } catch {
2106
+ }
2107
+ }
2108
+ return null;
2109
+ }
2110
+ function runTsc(args, cwd, signal, project) {
2111
+ return new Promise((resolve2) => {
2112
+ let stdout = "";
2113
+ let stderr = "";
2114
+ const MAX = 2e5;
2115
+ const child = spawn("npx", ["tsc", ...args], { cwd, signal, stdio: ["ignore", "pipe", "pipe"] });
2116
+ child.stdout?.on("data", (c) => {
2117
+ if (stdout.length < MAX) stdout += c.toString();
2118
+ });
2119
+ child.stderr?.on("data", (c) => {
2120
+ if (stderr.length < MAX) stderr += c.toString();
2121
+ });
2122
+ child.on("close", (code) => {
2123
+ const errors = (stdout.match(/error TS/g) || []).length;
2124
+ resolve2({
2125
+ project,
2126
+ exit_code: code ?? 0,
2127
+ errors,
2128
+ warnings: (stdout.match(/warning/g) || []).length,
2129
+ output: stdout || stderr,
2130
+ truncated: stdout.length >= MAX
2131
+ });
2132
+ });
2133
+ child.on("error", (e) => resolve2({
2134
+ project,
2135
+ exit_code: 1,
2136
+ errors: 0,
2137
+ warnings: 0,
2138
+ output: e.message,
2139
+ truncated: false
2140
+ }));
2141
+ });
2142
+ }
2143
+ var testTool = {
2144
+ name: "test",
2145
+ description: "Run tests with vitest, jest, or mocha. Returns pass/fail counts and output.",
2146
+ usageHint: "Set `files` for specific tests. `watch` enables watch mode. `coverage` generates coverage report. `grep` filters by name.",
2147
+ permission: "confirm",
2148
+ mutating: false,
2149
+ timeoutMs: 12e4,
2150
+ inputSchema: {
2151
+ type: "object",
2152
+ properties: {
2153
+ files: {
2154
+ type: "string",
2155
+ description: 'Test files: single path, comma-separated list, or glob (e.g. "**/*.test.ts")'
2156
+ },
2157
+ runner: {
2158
+ type: "string",
2159
+ enum: ["vitest", "jest", "mocha", "auto"],
2160
+ description: "Test runner (default: auto-detect)"
2161
+ },
2162
+ watch: {
2163
+ type: "boolean",
2164
+ description: "Run in watch mode (default: false)"
2165
+ },
2166
+ coverage: {
2167
+ type: "boolean",
2168
+ description: "Generate coverage report (default: false)"
2169
+ },
2170
+ cwd: { type: "string", description: "Working directory (default: cwd)" },
2171
+ grep: {
2172
+ type: "string",
2173
+ description: "Filter tests by name pattern (default: none)"
2174
+ },
2175
+ timeout: {
2176
+ type: "integer",
2177
+ description: "Test timeout in ms (default: 30000)"
2178
+ }
2179
+ }
2180
+ },
2181
+ async execute(input, ctx, opts) {
2182
+ const cwd = input.cwd ? safeResolve(input.cwd, ctx) : ctx.cwd;
2183
+ const runner = input.runner ?? "auto";
2184
+ const detected = runner === "auto" ? await detectRunner(cwd) : runner;
2185
+ if (!detected) {
2186
+ return {
2187
+ runner: "none",
2188
+ exit_code: 1,
2189
+ tests_run: 0,
2190
+ passed: 0,
2191
+ failed: 0,
2192
+ duration_ms: 0,
2193
+ output: "No test runner found (vitest.config.ts, jest.config.js, .mocharc.json)",
2194
+ truncated: false
2195
+ };
2196
+ }
2197
+ return await runTests(detected, input, cwd, opts.signal);
2198
+ }
2199
+ };
2200
+ async function detectRunner(cwd) {
2201
+ const { stat: stat9 } = __require("fs/promises");
2202
+ const candidates = ["vitest.config.ts", "jest.config.js", ".mocharc.json"];
2203
+ for (const f of candidates) {
2204
+ try {
2205
+ await stat9(path.join(cwd, f));
2206
+ if (f.includes("vitest")) return "vitest";
2207
+ if (f.includes("jest")) return "jest";
2208
+ if (f.includes("mocha")) return "mocha";
2209
+ } catch {
2210
+ }
2211
+ }
2212
+ return "vitest";
2213
+ }
2214
+ async function runTests(runner, input, cwd, signal) {
2215
+ const start = Date.now();
2216
+ const args = buildArgs2(runner, input);
2217
+ const result = await runCommand4(runner, args, cwd, signal);
2218
+ const duration = Date.now() - start;
2219
+ return parseResult(runner, result, duration);
2220
+ }
2221
+ function buildArgs2(runner, input) {
2222
+ const args = [];
2223
+ const timeout = input.timeout ?? 3e4;
2224
+ switch (runner) {
2225
+ case "vitest":
2226
+ args.push("run", "--reporter=verbose");
2227
+ if (input.watch) {
2228
+ args[1] = "";
2229
+ args.push("watch");
2230
+ }
2231
+ if (input.coverage) args.push("--coverage");
2232
+ if (input.grep) args.push("--testNamePattern", input.grep);
2233
+ args.push("--testTimeout", String(timeout));
2234
+ break;
2235
+ case "jest":
2236
+ args.push("--verbose");
2237
+ if (input.watch) args.push("--watch");
2238
+ if (input.coverage) args.push("--coverage");
2239
+ if (input.grep) args.push("--testPathPattern", input.grep);
2240
+ args.push("--testTimeout", String(timeout));
2241
+ break;
2242
+ case "mocha":
2243
+ args.push("--reporter", "spec");
2244
+ if (input.grep) args.push("--grep", input.grep);
2245
+ args.push("--timeout", String(timeout));
2246
+ break;
2247
+ }
2248
+ if (input.files) {
2249
+ const files = Array.isArray(input.files) ? input.files : input.files.split(",");
2250
+ args.push("--", ...files.map((f) => f.trim()));
2251
+ }
2252
+ return args;
2253
+ }
2254
+ function runCommand4(cmd, args, cwd, signal) {
2255
+ return new Promise((resolve2) => {
2256
+ let stdout = "";
2257
+ let stderr = "";
2258
+ const MAX = 2e5;
2259
+ const child = spawn(cmd, args, { cwd, signal, stdio: ["ignore", "pipe", "pipe"] });
2260
+ child.stdout?.on("data", (c) => {
2261
+ if (stdout.length < MAX) stdout += c.toString();
2262
+ });
2263
+ child.stderr?.on("data", (c) => {
2264
+ if (stderr.length < MAX) stderr += c.toString();
2265
+ });
2266
+ child.on("close", (code) => resolve2({ stdout, stderr, exitCode: code ?? 0, truncated: stdout.length >= MAX }));
2267
+ child.on("error", (e) => resolve2({ stdout: "", stderr: e.message, exitCode: 1, truncated: false }));
2268
+ });
2269
+ }
2270
+ function parseResult(runner, result, duration) {
2271
+ const out = result.stdout + result.stderr;
2272
+ let tests_run = 0;
2273
+ let passed = 0;
2274
+ let failed = 0;
2275
+ if (runner === "vitest") {
2276
+ const passedMatch = out.match(/(\d+) passed/);
2277
+ const failedMatch = out.match(/(\d+) failed/);
2278
+ if (passedMatch?.[1]) passed = parseInt(passedMatch[1], 10);
2279
+ if (failedMatch?.[1]) failed = parseInt(failedMatch[1], 10);
2280
+ tests_run = passed + failed;
2281
+ } else if (runner === "jest") {
2282
+ const suitesMatch = out.match(/Test Suites:\s+(\d+)\s+total/);
2283
+ const passedMatch = out.match(/Tests:\s+(\d+)\s+passed/);
2284
+ const failedMatch = out.match(/Tests:\s+(\d+)\s+failed/);
2285
+ tests_run = parseInt(suitesMatch?.[1] ?? "0", 10);
2286
+ passed = parseInt(passedMatch?.[1] ?? "0", 10);
2287
+ failed = parseInt(failedMatch?.[1] ?? "0", 10);
2288
+ }
2289
+ return {
2290
+ runner,
2291
+ exit_code: result.exitCode,
2292
+ tests_run,
2293
+ passed,
2294
+ failed,
2295
+ duration_ms: duration,
2296
+ output: result.stdout,
2297
+ truncated: result.truncated
2298
+ };
2299
+ }
2300
+ var installTool = {
2301
+ name: "install",
2302
+ description: "Install npm packages. Detects pnpm/npm/yarn and uses the right package manager.",
2303
+ usageHint: "Set `packages` to install. `save` as dependency type. `global` for global install. `dry_run` to preview.",
2304
+ permission: "confirm",
2305
+ mutating: true,
2306
+ timeoutMs: 12e4,
2307
+ inputSchema: {
2308
+ type: "object",
2309
+ properties: {
2310
+ packages: {
2311
+ type: "string",
2312
+ description: "Package(s) to install: single name, comma-separated list, or empty for all deps"
2313
+ },
2314
+ save: {
2315
+ type: "string",
2316
+ enum: ["dependency", "dev", "optional"],
2317
+ description: "Save as regular, dev, or optional dependency"
2318
+ },
2319
+ cwd: { type: "string", description: "Working directory (default: cwd)" },
2320
+ dry_run: { type: "boolean", description: "Preview install without modifying (default: false)" },
2321
+ global: { type: "boolean", description: "Install globally (default: false)" }
2322
+ }
2323
+ },
2324
+ async execute(input, ctx, opts) {
2325
+ const cwd = input.cwd ? safeResolve(input.cwd, ctx) : ctx.cwd;
2326
+ const pkgManager = await detectPackageManager(cwd);
2327
+ const save = input.save === "dev" ? "-D" : input.save === "optional" ? "-O" : "";
2328
+ const globalFlag = input.global ? ["-g"] : [];
2329
+ const args = [];
2330
+ if (input.dry_run) args.push("--dry-run");
2331
+ if (pkgManager === "pnpm") {
2332
+ if (save) args.push(save);
2333
+ args.push("add", ...globalFlag);
2334
+ } else if (pkgManager === "yarn") {
2335
+ args.push("add", ...globalFlag);
2336
+ } else {
2337
+ args.push("install", ...globalFlag);
2338
+ }
2339
+ if (input.packages) {
2340
+ const pkgs = Array.isArray(input.packages) ? input.packages : input.packages.split(",");
2341
+ args.push(...pkgs.map((p) => p.trim()));
2342
+ }
2343
+ return runInstall(pkgManager, args, cwd, opts.signal, input.packages ? (Array.isArray(input.packages) ? input.packages : input.packages.split(",")).map((p) => p.trim()) : []);
2344
+ }
2345
+ };
2346
+ async function detectPackageManager(cwd) {
2347
+ const { stat: stat9 } = __require("fs/promises");
2348
+ try {
2349
+ await stat9(`${cwd}/pnpm-lock.yaml`);
2350
+ return "pnpm";
2351
+ } catch {
2352
+ try {
2353
+ await stat9(`${cwd}/yarn.lock`);
2354
+ return "yarn";
2355
+ } catch {
2356
+ return "npm";
2357
+ }
2358
+ }
2359
+ }
2360
+ function runInstall(manager, args, cwd, signal, packages) {
2361
+ return new Promise((resolve2) => {
2362
+ let stdout = "";
2363
+ let stderr = "";
2364
+ const MAX = 1e5;
2365
+ const child = spawn(manager, args, { cwd, signal, stdio: ["ignore", "pipe", "pipe"] });
2366
+ child.stdout?.on("data", (c) => {
2367
+ if (stdout.length < MAX) stdout += c.toString();
2368
+ });
2369
+ child.stderr?.on("data", (c) => {
2370
+ if (stderr.length < MAX) stderr += c.toString();
2371
+ });
2372
+ child.on("close", (code) => resolve2({
2373
+ packages,
2374
+ exit_code: code ?? 0,
2375
+ output: stdout || stderr,
2376
+ dry_run: args.includes("--dry-run"),
2377
+ truncated: stdout.length >= MAX
2378
+ }));
2379
+ child.on("error", (e) => resolve2({
2380
+ packages,
2381
+ exit_code: 1,
2382
+ output: e.message,
2383
+ dry_run: false,
2384
+ truncated: false
2385
+ }));
2386
+ });
2387
+ }
2388
+ var auditTool = {
2389
+ name: "audit",
2390
+ description: "Run npm/pnpm security audit. Returns vulnerabilities sorted by severity.",
2391
+ usageHint: "Set `level` to filter minimum severity. `fix` attempts auto-fix. `packages` checks specific packages.",
2392
+ permission: "confirm",
2393
+ mutating: false,
2394
+ timeoutMs: 6e4,
2395
+ inputSchema: {
2396
+ type: "object",
2397
+ properties: {
2398
+ cwd: { type: "string", description: "Working directory (default: cwd)" },
2399
+ level: {
2400
+ type: "string",
2401
+ enum: ["low", "moderate", "high", "critical"],
2402
+ description: "Minimum severity level to report"
2403
+ },
2404
+ fix: {
2405
+ type: "boolean",
2406
+ description: "Attempt to fix vulnerabilities (default: false)"
2407
+ },
2408
+ packages: {
2409
+ type: "string",
2410
+ description: "Specific package(s) to audit (comma-separated)"
2411
+ }
2412
+ }
2413
+ },
2414
+ async execute(input, ctx, opts) {
2415
+ const cwd = input.cwd ? safeResolve(input.cwd, ctx) : ctx.cwd;
2416
+ const manager = await detectManager(cwd);
2417
+ const args = ["audit", "--json"];
2418
+ if (input.fix) args.push("--fix");
2419
+ if (input.packages) {
2420
+ const pkgs = Array.isArray(input.packages) ? input.packages : input.packages.split(",");
2421
+ args.push(...pkgs.map((p) => p.trim()));
2422
+ }
2423
+ return runAudit(manager, args, cwd, opts.signal);
2424
+ }
2425
+ };
2426
+ async function detectManager(cwd) {
2427
+ const { stat: stat9 } = __require("fs/promises");
2428
+ try {
2429
+ await stat9(`${cwd}/pnpm-lock.yaml`);
2430
+ return "pnpm";
2431
+ } catch {
2432
+ }
2433
+ try {
2434
+ await stat9(`${cwd}/yarn.lock`);
2435
+ return "yarn";
2436
+ } catch {
2437
+ }
2438
+ return "npm";
2439
+ }
2440
+ function runAudit(manager, args, cwd, signal) {
2441
+ return new Promise((resolve2) => {
2442
+ let stdout = "";
2443
+ let stderr = "";
2444
+ const MAX = 1e5;
2445
+ const child = spawn(manager, args, { cwd, signal, stdio: ["ignore", "pipe", "pipe"] });
2446
+ child.stdout?.on("data", (c) => {
2447
+ if (stdout.length < MAX) stdout += c.toString();
2448
+ });
2449
+ child.stderr?.on("data", (c) => {
2450
+ if (stderr.length < MAX) stderr += c.toString();
2451
+ });
2452
+ child.on("close", (code) => {
2453
+ const result = parseAuditOutput(stdout, code ?? 0);
2454
+ resolve2(result);
2455
+ });
2456
+ child.on("error", (e) => resolve2({
2457
+ exit_code: 1,
2458
+ vulnerabilities: [],
2459
+ total: 0,
2460
+ summary: e.message,
2461
+ output: e.message,
2462
+ truncated: false
2463
+ }));
2464
+ });
2465
+ }
2466
+ function parseAuditOutput(json, exitCode) {
2467
+ if (!json) {
2468
+ return {
2469
+ exit_code: exitCode,
2470
+ vulnerabilities: [],
2471
+ total: 0,
2472
+ summary: exitCode === 0 ? "No vulnerabilities found" : "Audit failed",
2473
+ output: "",
2474
+ truncated: false
2475
+ };
2476
+ }
2477
+ try {
2478
+ const data = JSON.parse(json);
2479
+ const advisories = [];
2480
+ const ads = data.advisories ?? {};
2481
+ for (const id of Object.keys(ads)) {
2482
+ const adv = ads[id];
2483
+ advisories.push({
2484
+ severity: adv.severity ?? "unknown",
2485
+ package: adv.module_name ?? id,
2486
+ title: adv.title ?? "Unknown vulnerability",
2487
+ url: adv.url ?? ""
2488
+ });
2489
+ }
2490
+ const total = advisories.length;
2491
+ const summary = total === 0 ? "No vulnerabilities found" : `Found ${total} vulnerabilities: ${advisories.filter((a) => a.severity === "critical").length} critical, ${advisories.filter((a) => a.severity === "high").length} high`;
2492
+ return {
2493
+ exit_code: exitCode,
2494
+ vulnerabilities: advisories,
2495
+ total,
2496
+ summary,
2497
+ output: json,
2498
+ truncated: json.length >= 1e5
2499
+ };
2500
+ } catch {
2501
+ return {
2502
+ exit_code: exitCode,
2503
+ vulnerabilities: [],
2504
+ total: 0,
2505
+ summary: "Could not parse audit output",
2506
+ output: json,
2507
+ truncated: false
2508
+ };
2509
+ }
2510
+ }
2511
+ var outdatedTool = {
2512
+ name: "outdated",
2513
+ description: "Check for outdated npm packages. Shows current, wanted, and latest versions.",
2514
+ usageHint: "Set `check` to filter specific packages. `format` as list or table. `include_deprecated` shows deprecated packages.",
2515
+ permission: "auto",
2516
+ mutating: false,
2517
+ timeoutMs: 6e4,
2518
+ inputSchema: {
2519
+ type: "object",
2520
+ properties: {
2521
+ cwd: { type: "string", description: "Working directory (default: cwd)" },
2522
+ format: {
2523
+ type: "string",
2524
+ enum: ["list", "table"],
2525
+ description: "Output format (default: list)"
2526
+ },
2527
+ include_deprecated: {
2528
+ type: "boolean",
2529
+ description: "Include deprecated packages (default: false)"
2530
+ },
2531
+ check: {
2532
+ type: "string",
2533
+ description: "Specific package(s) to check (comma-separated)"
2534
+ }
2535
+ }
2536
+ },
2537
+ async execute(input, ctx, opts) {
2538
+ const cwd = input.cwd ? safeResolve(input.cwd, ctx) : ctx.cwd;
2539
+ const manager = await detectManager2(cwd);
2540
+ const args = ["outdated", "--json"];
2541
+ if (input.format === "table") args.push("--table");
2542
+ if (input.include_deprecated) args.push("--include", "deprecated");
2543
+ return runOutdated(manager, args, cwd, opts.signal);
2544
+ }
2545
+ };
2546
+ async function detectManager2(cwd) {
2547
+ const { stat: stat9 } = __require("fs/promises");
2548
+ try {
2549
+ await stat9(`${cwd}/pnpm-lock.yaml`);
2550
+ return "pnpm";
2551
+ } catch {
2552
+ }
2553
+ try {
2554
+ await stat9(`${cwd}/yarn.lock`);
2555
+ return "yarn";
2556
+ } catch {
2557
+ }
2558
+ return "npm";
2559
+ }
2560
+ function runOutdated(manager, args, cwd, signal) {
2561
+ return new Promise((resolve2) => {
2562
+ let stdout = "";
2563
+ let stderr = "";
2564
+ const MAX = 1e5;
2565
+ const child = spawn(manager, args, { cwd, signal, stdio: ["ignore", "pipe", "pipe"] });
2566
+ child.stdout?.on("data", (c) => {
2567
+ if (stdout.length < MAX) stdout += c.toString();
2568
+ });
2569
+ child.stderr?.on("data", (c) => {
2570
+ if (stderr.length < MAX) stderr += c.toString();
2571
+ });
2572
+ child.on("close", (code) => {
2573
+ const result = parseOutdatedOutput(stdout, code ?? 0);
2574
+ resolve2(result);
2575
+ });
2576
+ child.on("error", (e) => resolve2({
2577
+ exit_code: 1,
2578
+ packages: [],
2579
+ total: 0,
2580
+ output: e.message,
2581
+ truncated: false
2582
+ }));
2583
+ });
2584
+ }
2585
+ function parseOutdatedOutput(json, exitCode) {
2586
+ const packages = [];
2587
+ if (!json) {
2588
+ return {
2589
+ exit_code: exitCode,
2590
+ packages: [],
2591
+ total: 0,
2592
+ output: exitCode === 0 ? "All packages up to date" : "Could not check outdated packages",
2593
+ truncated: false
2594
+ };
2595
+ }
2596
+ try {
2597
+ const data = JSON.parse(json);
2598
+ for (const name of Object.keys(data)) {
2599
+ const info = data[name];
2600
+ packages.push({
2601
+ name,
2602
+ current: info.current ?? "unknown",
2603
+ latest: info.latest ?? "unknown",
2604
+ wanted: info.wanted ?? "unknown",
2605
+ type: info.type ?? "unknown",
2606
+ location: info.location ?? name
2607
+ });
2608
+ }
2609
+ } catch {
2610
+ }
2611
+ return {
2612
+ exit_code: exitCode,
2613
+ packages,
2614
+ total: packages.length,
2615
+ output: json,
2616
+ truncated: json.length >= 1e5
2617
+ };
2618
+ }
2619
+ var logsTool = {
2620
+ name: "logs",
2621
+ description: "Stream or fetch logs from a service or file. Supports Docker, systemd, or plain log files.",
2622
+ usageHint: "Set `service` for Docker/systemd, `path` for file. `lines` limits output. `stream` for tail -f behavior. `filter` regex filters lines.",
2623
+ permission: "confirm",
2624
+ mutating: false,
2625
+ timeoutMs: 3e4,
2626
+ inputSchema: {
2627
+ type: "object",
2628
+ properties: {
2629
+ service: {
2630
+ type: "string",
2631
+ description: "Service name for Docker or systemd journal"
2632
+ },
2633
+ path: {
2634
+ type: "string",
2635
+ description: "Path to log file (alternative to service)"
2636
+ },
2637
+ lines: {
2638
+ type: "integer",
2639
+ description: "Number of log lines to fetch (default: 100, 0 for all)",
2640
+ minimum: 0,
2641
+ maximum: 1e4
2642
+ },
2643
+ stream: {
2644
+ type: "boolean",
2645
+ description: "Stream logs continuously (like tail -f) (default: false)"
2646
+ },
2647
+ filter: {
2648
+ type: "string",
2649
+ description: "Regex pattern to filter log lines"
2650
+ },
2651
+ since: {
2652
+ type: "string",
2653
+ enum: ["1h", "6h", "24h", "all"],
2654
+ description: "Only show logs since duration"
2655
+ },
2656
+ cwd: { type: "string", description: "Working directory (default: cwd)" }
2657
+ }
2658
+ },
2659
+ async execute(input, ctx, opts) {
2660
+ const cwd = input.cwd ? safeResolve(input.cwd, ctx) : ctx.cwd;
2661
+ const lines = input.lines ?? 100;
2662
+ const filterRe = input.filter ? new RegExp(input.filter, "i") : null;
2663
+ if (input.service) {
2664
+ return await dockerLogs(input.service, lines, filterRe, cwd, opts.signal);
2665
+ }
2666
+ if (input.path) {
2667
+ return await fileLogs(safeResolve(input.path, ctx), lines, filterRe, input.stream ?? false);
2668
+ }
2669
+ return {
2670
+ source: "none",
2671
+ entries: [],
2672
+ total: 0,
2673
+ truncated: false,
2674
+ stream_mode: false
2675
+ };
2676
+ }
2677
+ };
2678
+ async function dockerLogs(service, lines, filterRe, cwd, signal, since) {
2679
+ const args = ["logs"];
2680
+ if (lines > 0) args.push("--tail", String(lines));
2681
+ args.push("--timestamps", service);
2682
+ return new Promise((resolve2) => {
2683
+ let stdout = "";
2684
+ let stderr = "";
2685
+ const MAX = 2e5;
2686
+ const child = spawn("docker", args, { cwd, signal, stdio: ["ignore", "pipe", "pipe"] });
2687
+ child.stdout?.on("data", (c) => {
2688
+ if (stdout.length < MAX) stdout += c.toString();
2689
+ });
2690
+ child.stderr?.on("data", (c) => {
2691
+ if (stderr.length < MAX) stderr += c.toString();
2692
+ });
2693
+ child.on("close", (code) => {
2694
+ const output = stdout + stderr;
2695
+ const entries = parseLogLines(output, filterRe);
2696
+ resolve2({
2697
+ source: `docker:${service}`,
2698
+ entries,
2699
+ total: entries.length,
2700
+ truncated: output.length >= MAX,
2701
+ stream_mode: false
2702
+ });
2703
+ });
2704
+ child.on("error", (e) => resolve2({
2705
+ source: `docker:${service}`,
2706
+ entries: [],
2707
+ total: 0,
2708
+ truncated: false,
2709
+ stream_mode: false
2710
+ }));
2711
+ });
2712
+ }
2713
+ async function fileLogs(path10, lines, filterRe, stream) {
2714
+ const { createInterface } = await import('readline');
2715
+ const { createReadStream } = await import('fs');
2716
+ const entries = [];
2717
+ const allLines = [];
2718
+ const rl = createInterface({
2719
+ input: createReadStream(path10),
2720
+ crlfDelay: Infinity
2721
+ });
2722
+ for await (const line of rl) {
2723
+ if (filterRe && !filterRe.test(line)) continue;
2724
+ allLines.push(line);
2725
+ }
2726
+ const sliced = lines > 0 ? allLines.slice(-lines) : allLines;
2727
+ for (const line of sliced) {
2728
+ const parsed = parseLine(line);
2729
+ if (parsed) entries.push(parsed);
2730
+ }
2731
+ return {
2732
+ source: path10,
2733
+ entries,
2734
+ total: entries.length,
2735
+ truncated: allLines.length > lines && lines > 0,
2736
+ stream_mode: stream
2737
+ };
2738
+ }
2739
+ function parseLogLines(output, filterRe) {
2740
+ const lines = output.split("\n").filter(Boolean);
2741
+ const entries = [];
2742
+ for (const line of lines) {
2743
+ if (filterRe && !filterRe.test(line)) continue;
2744
+ const parsed = parseLine(line);
2745
+ if (parsed) entries.push(parsed);
2746
+ }
2747
+ return entries;
2748
+ }
2749
+ function parseLine(line) {
2750
+ const tsRe = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z?)\s+(?:\[?(\w+)\]?)\s*(.*)/;
2751
+ const match = tsRe.exec(line);
2752
+ if (match) {
2753
+ return {
2754
+ timestamp: match[1] ?? "",
2755
+ level: match[2]?.toLowerCase() ?? "info",
2756
+ message: match[3] ?? ""
2757
+ };
2758
+ }
2759
+ const levelRe = /(?:ERROR|WARN|INFO|DEBUG|TRACE)\s+(.*)/i;
2760
+ const levelMatch = levelRe.exec(line);
2761
+ if (levelMatch) {
2762
+ return {
2763
+ timestamp: "",
2764
+ level: levelMatch[1]?.toLowerCase() ?? "info",
2765
+ message: levelMatch[2] ?? line
2766
+ };
2767
+ }
2768
+ return {
2769
+ timestamp: "",
2770
+ level: "info",
2771
+ message: line
2772
+ };
2773
+ }
2774
+ var documentTool = {
2775
+ name: "document",
2776
+ description: "Generate or update documentation comments for functions, classes, and types. Supports JSDoc, TSDoc, and block comments.",
2777
+ usageHint: "Set `target` for what to document. `files` for paths. `style` for comment format. `overwrite` replaces existing docs.",
2778
+ permission: "confirm",
2779
+ mutating: true,
2780
+ timeoutMs: 3e4,
2781
+ inputSchema: {
2782
+ type: "object",
2783
+ properties: {
2784
+ target: {
2785
+ type: "string",
2786
+ enum: ["file", "function", "class", "type", "all"],
2787
+ description: "What to document"
2788
+ },
2789
+ path: {
2790
+ type: "string",
2791
+ description: "Specific file path to document"
2792
+ },
2793
+ files: {
2794
+ type: "string",
2795
+ description: "File(s) to process: single path, comma-separated list, or glob"
2796
+ },
2797
+ style: {
2798
+ type: "string",
2799
+ enum: ["jsdoc", "tsdoc", "block"],
2800
+ description: "Documentation style (default: jsdoc)"
2801
+ },
2802
+ overwrite: {
2803
+ type: "boolean",
2804
+ description: "Overwrite existing docstrings (default: false)"
2805
+ },
2806
+ cwd: { type: "string", description: "Working directory (default: cwd)" }
2807
+ }
2808
+ },
2809
+ async execute(input, ctx) {
2810
+ const cwd = input.cwd ? safeResolve(input.cwd, ctx) : ctx.cwd;
2811
+ const style = input.style ?? "jsdoc";
2812
+ const results = [];
2813
+ let filesProcessed = 0;
2814
+ let itemsDocumented = 0;
2815
+ const fileList = input.files ? await resolveFiles2(Array.isArray(input.files) ? input.files.join(",") : input.files, cwd) : input.path ? [safeResolve(input.path, ctx)] : [];
2816
+ for (const absPath of fileList) {
2817
+ try {
2818
+ const content = await fs4.readFile(absPath, "utf8");
2819
+ filesProcessed++;
2820
+ const processed = processFile(content, absPath, style, input.overwrite ?? false, input.target ?? "all");
2821
+ results.push(...processed);
2822
+ itemsDocumented += processed.filter((r) => r.status === "documented").length;
2823
+ } catch (e) {
2824
+ results.push({
2825
+ path: absPath,
2826
+ name: absPath.split("/").pop() ?? absPath,
2827
+ signature: "",
2828
+ docstring: "",
2829
+ status: "error",
2830
+ error: e instanceof Error ? e.message : String(e)
2831
+ });
2832
+ }
2833
+ }
2834
+ return {
2835
+ files_processed: filesProcessed,
2836
+ items_documented: itemsDocumented,
2837
+ results,
2838
+ style
2839
+ };
2840
+ }
2841
+ };
2842
+ async function resolveFiles2(filesInput, cwd) {
2843
+ const files = Array.isArray(filesInput) ? filesInput : filesInput.split(",");
2844
+ const resolved = [];
2845
+ for (const f of files) {
2846
+ const absPath = f.trim().startsWith("/") ? f.trim() : `${cwd}/${f.trim()}`;
2847
+ try {
2848
+ const stat9 = await fs4.stat(absPath);
2849
+ if (stat9.isFile()) resolved.push(absPath);
2850
+ } catch {
2851
+ }
2852
+ }
2853
+ return resolved;
2854
+ }
2855
+ function processFile(content, absPath, style, overwrite, target) {
2856
+ const results = [];
2857
+ content.split("\n");
2858
+ const functionRegex = /(?:async\s+)?function\s+(\w+)\s*\(([^)]*)\)/g;
2859
+ const arrowRegex = /(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s+)?\(([^)]*)\)\s*=>/g;
2860
+ const classRegex = /class\s+(\w+)/g;
2861
+ const typeRegex = /(?:type|interface)\s+(\w+)\s*[=<]/g;
2862
+ let match;
2863
+ const allMatches = [];
2864
+ if (target === "all" || target === "function") {
2865
+ while ((match = functionRegex.exec(content)) !== null) {
2866
+ if (!match[1]) continue;
2867
+ allMatches.push({ name: match[1], sig: match[2] ?? "", type: "function", line: content.slice(0, match.index).split("\n").length });
2868
+ }
2869
+ while ((match = arrowRegex.exec(content)) !== null) {
2870
+ if (!match[1]) continue;
2871
+ allMatches.push({ name: match[1], sig: match[2] ?? "", type: "arrow", line: content.slice(0, match.index).split("\n").length });
2872
+ }
2873
+ }
2874
+ if (target === "all" || target === "class") {
2875
+ functionRegex.lastIndex = 0;
2876
+ while ((match = classRegex.exec(content)) !== null) {
2877
+ if (!match[1]) continue;
2878
+ allMatches.push({ name: match[1], sig: "", type: "class", line: content.slice(0, match.index).split("\n").length });
2879
+ }
2880
+ }
2881
+ if (target === "all" || target === "type") {
2882
+ functionRegex.lastIndex = 0;
2883
+ while ((match = typeRegex.exec(content)) !== null) {
2884
+ if (!match[1]) continue;
2885
+ allMatches.push({ name: match[1], sig: match[0] ?? "", type: "type", line: content.slice(0, match.index).split("\n").length });
2886
+ }
2887
+ }
2888
+ for (const m of allMatches) {
2889
+ results.push({
2890
+ path: absPath,
2891
+ name: m.name,
2892
+ signature: m.sig,
2893
+ docstring: `/** ${m.name} - documented at line ${m.line} */`,
2894
+ status: "skipped"
2895
+ });
2896
+ }
2897
+ return results;
2898
+ }
2899
+ var BUILT_IN_TEMPLATES = {
2900
+ "npm-package": {
2901
+ description: "Basic npm package with ESM",
2902
+ files: {
2903
+ "package.json": JSON.stringify({
2904
+ name: "{{name}}",
2905
+ version: "0.0.1",
2906
+ type: "module",
2907
+ main: "./dist/index.js",
2908
+ scripts: { build: "tsc", test: "vitest run" },
2909
+ devDependencies: { typescript: "^5.0.0" }
2910
+ }, null, 2),
2911
+ "tsconfig.json": JSON.stringify({
2912
+ compilerOptions: { target: "ES2022", module: "ESNext", strict: true },
2913
+ include: ["src"]
2914
+ }, null, 2),
2915
+ "src/index.ts": `export function hello() {
2916
+ return 'Hello from {{name}}';
2917
+ }
2918
+ `,
2919
+ "src/index.test.ts": `import { hello } from './index';
2920
+ import { describe, it, expect } from 'vitest';
2921
+
2922
+ describe('hello', () => {
2923
+ it('returns greeting', () => {
2924
+ expect(hello()).toBe('Hello from {{name}}');
2925
+ });
2926
+ });
2927
+ `
2928
+ }
2929
+ },
2930
+ "cli-tool": {
2931
+ description: "CLI tool with argparse",
2932
+ files: {
2933
+ "package.json": JSON.stringify({
2934
+ name: "{{name}}",
2935
+ version: "0.0.1",
2936
+ type: "module",
2937
+ bin: { "{{name}}": "./src/index.js" },
2938
+ scripts: { build: "tsc", start: "node dist/index.js" }
2939
+ }, null, 2),
2940
+ "src/index.ts": `#!/usr/bin/env node
2941
+
2942
+ async function main() {
2943
+ console.log('Hello from {{name}}');
2944
+ }
2945
+
2946
+ main();
2947
+ `
2948
+ }
2949
+ },
2950
+ "react-component": {
2951
+ description: "React component with TypeScript",
2952
+ files: {
2953
+ "{{name}}.tsx": `interface {{Name}}Props {
2954
+ className?: string;
2955
+ }
2956
+
2957
+ export function {{Name}}({ className }: {{Name}}Props) {
2958
+ return (
2959
+ <div className={className}>
2960
+ {{Name}} Component
2961
+ </div>
2962
+ );
2963
+ }
2964
+ `,
2965
+ "{{name}}.test.tsx": `import { render, screen } from '@testing-library/react';
2966
+ import { {{Name}} } from './{{Name}}';
2967
+
2968
+ describe('{{Name}}', () => {
2969
+ it('renders', () => {
2970
+ render(<{{Name}} />);
2971
+ expect(screen.getByText('{{Name}} Component')).toBeInTheDocument();
2972
+ });
2973
+ });
2974
+ `
2975
+ }
2976
+ }
2977
+ };
2978
+ var scaffoldTool = {
2979
+ name: "scaffold",
2980
+ description: "Generate boilerplate code from built-in templates or paths. Creates package.json, source files, tests.",
2981
+ usageHint: "Set `template` (npm-package, cli-tool, react-component) and `name`. `vars` for template variables. `dry_run` preview.",
2982
+ permission: "confirm",
2983
+ mutating: true,
2984
+ timeoutMs: 3e4,
2985
+ inputSchema: {
2986
+ type: "object",
2987
+ properties: {
2988
+ template: {
2989
+ type: "string",
2990
+ description: "Template name (npm-package, cli-tool, react-component) or path to template directory"
2991
+ },
2992
+ name: {
2993
+ type: "string",
2994
+ description: "Project/component name (used in generated files)"
2995
+ },
2996
+ cwd: { type: "string", description: "Working directory (default: cwd)" },
2997
+ vars: {
2998
+ type: "object",
2999
+ additionalProperties: { type: "string" },
3000
+ description: "Template variables for custom templates"
3001
+ },
3002
+ dry_run: {
3003
+ type: "boolean",
3004
+ description: "Preview generated files without creating (default: false)"
3005
+ }
3006
+ },
3007
+ required: ["template", "name"]
3008
+ },
3009
+ async execute(input, ctx) {
3010
+ const cwd = input.cwd ? safeResolve(input.cwd, ctx) : ctx.cwd;
3011
+ const name = input.name;
3012
+ const vars = { name, ...input.vars };
3013
+ const builtIn = BUILT_IN_TEMPLATES[input.template];
3014
+ if (builtIn) {
3015
+ return handleBuiltIn(name, builtIn.files, cwd, input.dry_run ?? false, vars);
3016
+ }
3017
+ return {
3018
+ template: input.template,
3019
+ name,
3020
+ files_created: 0,
3021
+ files: [],
3022
+ dry_run: input.dry_run ?? false,
3023
+ output: `Template "${input.template}" not found. Available: ${Object.keys(BUILT_IN_TEMPLATES).join(", ")}`
3024
+ };
3025
+ }
3026
+ };
3027
+ function handleBuiltIn(name, templateFiles, cwd, dryRun, vars) {
3028
+ const files = [];
3029
+ let filesCreated = 0;
3030
+ for (const [filePath, content] of Object.entries(templateFiles)) {
3031
+ const resolvedPath = substituteVars(filePath, name, vars);
3032
+ const fullPath = path.join(cwd, resolvedPath);
3033
+ if (!dryRun) {
3034
+ fsSync.mkdirSync(path.dirname(fullPath), { recursive: true });
3035
+ fsSync.writeFileSync(fullPath, substituteVars(content, name, vars), "utf8");
3036
+ }
3037
+ files.push(resolvedPath);
3038
+ filesCreated++;
3039
+ }
3040
+ return {
3041
+ template: "built-in",
3042
+ name,
3043
+ files_created: filesCreated,
3044
+ files,
3045
+ dry_run: dryRun,
3046
+ output: dryRun ? `Would create ${filesCreated} files: ${files.join(", ")}` : `Created ${filesCreated} files: ${files.join(", ")}`
3047
+ };
3048
+ }
3049
+ function substituteVars(content, name, vars) {
3050
+ let result = content;
3051
+ result = result.replace(/\{\{name\}\}/g, name.toLowerCase().replace(/\s+/g, "-"));
3052
+ result = result.replace(/\{\{Name\}\}/g, name.replace(/(?:^|[-_\s]+)([a-z])/g, (_, c) => c.toUpperCase()));
3053
+ for (const [k, v] of Object.entries(vars)) {
3054
+ result = result.replace(new RegExp(`\\{\\{${k}\\}\\}`, "g"), v);
3055
+ }
3056
+ return result;
3057
+ }
3058
+
3059
+ // src/tool-search.ts
3060
+ var toolSearchTool = {
3061
+ name: "tool_search",
3062
+ description: "Search available tools by name, description, tags, permission level, or mutating flag.",
3063
+ usageHint: "Set `query` for keyword search. `tags` to filter by category. `permission` to filter by required permission. `mutating` to filter by mutating flag.",
3064
+ permission: "auto",
3065
+ mutating: false,
3066
+ timeoutMs: 1e3,
3067
+ inputSchema: {
3068
+ type: "object",
3069
+ properties: {
3070
+ query: {
3071
+ type: "string",
3072
+ description: "Search query for tool name or description"
3073
+ },
3074
+ tags: {
3075
+ type: "array",
3076
+ items: { type: "string" },
3077
+ description: 'Filter by tags (e.g. "filesystem", "network", "dev")'
3078
+ },
3079
+ permission: {
3080
+ type: "string",
3081
+ enum: ["auto", "confirm", "deny"],
3082
+ description: "Filter by required permission level"
3083
+ },
3084
+ mutating: {
3085
+ type: "boolean",
3086
+ description: "Filter by mutating flag (true=filters that modify, false=read-only)"
3087
+ },
3088
+ limit: {
3089
+ type: "integer",
3090
+ description: "Maximum results to return (default: 20)",
3091
+ minimum: 1,
3092
+ maximum: 100
3093
+ }
3094
+ }
3095
+ },
3096
+ async execute(input, ctx) {
3097
+ const limit = Math.min(input.limit ?? 20, 100);
3098
+ const tools = ctx.tools;
3099
+ const query2 = input.query?.toLowerCase() ?? "";
3100
+ let filtered = tools.filter((t) => {
3101
+ if (query2 && !t.name.toLowerCase().includes(query2) && !t.description.toLowerCase().includes(query2)) {
3102
+ return false;
3103
+ }
3104
+ if (input.permission && t.permission !== input.permission) {
3105
+ return false;
3106
+ }
3107
+ if (typeof input.mutating === "boolean" && t.mutating !== input.mutating) {
3108
+ return false;
3109
+ }
3110
+ return true;
3111
+ });
3112
+ const results = filtered.slice(0, limit).map((t) => ({
3113
+ name: t.name,
3114
+ description: t.description,
3115
+ permission: t.permission,
3116
+ mutating: t.mutating
3117
+ }));
3118
+ return {
3119
+ tools: results,
3120
+ total: filtered.length,
3121
+ truncated: filtered.length > limit
3122
+ };
3123
+ }
3124
+ };
3125
+
3126
+ // src/tool-use.ts
3127
+ var toolUseTool = {
3128
+ name: "tool_use",
3129
+ description: "Execute a specific tool by name with given input. Useful when the agent knows exactly which tool to call.",
3130
+ usageHint: "Set `tool` with exact tool name and `input` with the tool parameters. Returns result or error.",
3131
+ permission: "confirm",
3132
+ mutating: false,
3133
+ timeoutMs: 6e4,
3134
+ inputSchema: {
3135
+ type: "object",
3136
+ properties: {
3137
+ tool: {
3138
+ type: "string",
3139
+ description: "Exact name of the tool to execute"
3140
+ },
3141
+ input: {
3142
+ type: "object",
3143
+ description: "Input parameters for the tool"
3144
+ }
3145
+ },
3146
+ required: ["tool"]
3147
+ },
3148
+ async execute(input, ctx, opts) {
3149
+ const start = Date.now();
3150
+ if (!input?.tool) {
3151
+ return {
3152
+ tool: "unknown",
3153
+ success: false,
3154
+ error: "tool_use: tool name is required",
3155
+ executionMs: 0
3156
+ };
3157
+ }
3158
+ const tool = ctx.tools.find((t) => t.name === input.tool);
3159
+ if (!tool) {
3160
+ return {
3161
+ tool: input.tool,
3162
+ success: false,
3163
+ error: `tool_use: tool "${input.tool}" not found`,
3164
+ executionMs: Date.now() - start
3165
+ };
3166
+ }
3167
+ if (tool.permission === "deny") {
3168
+ return {
3169
+ tool: input.tool,
3170
+ success: false,
3171
+ error: `tool_use: tool "${input.tool}" is denied by policy`,
3172
+ executionMs: Date.now() - start
3173
+ };
3174
+ }
3175
+ if (tool.permission === "confirm" && input.input) {
3176
+ return {
3177
+ tool: input.tool,
3178
+ success: false,
3179
+ error: `tool_use: tool "${input.tool}" requires confirmation`,
3180
+ executionMs: Date.now() - start
3181
+ };
3182
+ }
3183
+ try {
3184
+ const result = await tool.execute(input.input, ctx, opts);
3185
+ return {
3186
+ tool: input.tool,
3187
+ success: true,
3188
+ result,
3189
+ executionMs: Date.now() - start
3190
+ };
3191
+ } catch (e) {
3192
+ return {
3193
+ tool: input.tool,
3194
+ success: false,
3195
+ error: e instanceof Error ? e.message : String(e),
3196
+ executionMs: Date.now() - start
3197
+ };
3198
+ }
3199
+ }
3200
+ };
3201
+
3202
+ // src/batch-tool-use.ts
3203
+ var batchToolUseTool = {
3204
+ name: "batch_tool_use",
3205
+ description: "Execute multiple tool calls in sequence or parallel. Returns all results.",
3206
+ usageHint: "Set `calls` array with tool names and inputs. `stop_on_error` halts on first failure. `parallel` runs concurrently (default: true).",
3207
+ permission: "confirm",
3208
+ mutating: false,
3209
+ timeoutMs: 12e4,
3210
+ inputSchema: {
3211
+ type: "object",
3212
+ properties: {
3213
+ calls: {
3214
+ type: "array",
3215
+ items: {
3216
+ type: "object",
3217
+ properties: {
3218
+ tool: { type: "string" },
3219
+ input: { type: "object" }
3220
+ },
3221
+ required: ["tool"]
3222
+ },
3223
+ description: "Array of tool calls to execute"
3224
+ },
3225
+ stop_on_error: {
3226
+ type: "boolean",
3227
+ description: "Stop execution on first error (default: false)"
3228
+ },
3229
+ parallel: {
3230
+ type: "boolean",
3231
+ description: "Execute calls in parallel (default: true)"
3232
+ }
3233
+ },
3234
+ required: ["calls"]
3235
+ },
3236
+ async execute(input, ctx, opts) {
3237
+ if (!input?.calls || input.calls.length === 0) {
3238
+ return {
3239
+ results: [],
3240
+ total: 0,
3241
+ succeeded: 0,
3242
+ failed: 0,
3243
+ stop_on_error: false
3244
+ };
3245
+ }
3246
+ const results = [];
3247
+ let succeeded = 0;
3248
+ let failed = 0;
3249
+ if (input.parallel !== false) {
3250
+ const promises = input.calls.map(async (call) => executeSingle(call, ctx, opts));
3251
+ const allResults = await Promise.all(promises);
3252
+ results.push(...allResults);
3253
+ succeeded = allResults.filter((r) => r.success).length;
3254
+ failed = allResults.filter((r) => !r.success).length;
3255
+ } else {
3256
+ for (const call of input.calls) {
3257
+ const result = await executeSingle(call, ctx, opts);
3258
+ results.push(result);
3259
+ if (result.success) {
3260
+ succeeded++;
3261
+ } else {
3262
+ failed++;
3263
+ if (input.stop_on_error) break;
3264
+ }
3265
+ }
3266
+ }
3267
+ return {
3268
+ results,
3269
+ total: input.calls.length,
3270
+ succeeded,
3271
+ failed,
3272
+ stop_on_error: input.stop_on_error ?? false
3273
+ };
3274
+ }
3275
+ };
3276
+ async function executeSingle(call, ctx, opts) {
3277
+ const start = Date.now();
3278
+ const tool = ctx.tools.find((t) => t.name === call.tool);
3279
+ if (!tool) {
3280
+ return {
3281
+ tool: call.tool,
3282
+ success: false,
3283
+ error: `tool "${call.tool}" not found`,
3284
+ executionMs: Date.now() - start
3285
+ };
3286
+ }
3287
+ try {
3288
+ const result = await tool.execute(call.input, ctx, opts);
3289
+ return {
3290
+ tool: call.tool,
3291
+ success: true,
3292
+ result,
3293
+ executionMs: Date.now() - start
3294
+ };
3295
+ } catch (e) {
3296
+ return {
3297
+ tool: call.tool,
3298
+ success: false,
3299
+ error: e instanceof Error ? e.message : String(e),
3300
+ executionMs: Date.now() - start
3301
+ };
3302
+ }
3303
+ }
3304
+
3305
+ // src/tool-help.ts
3306
+ var toolHelpTool = {
3307
+ name: "tool_help",
3308
+ description: "Get help and usage information for a specific tool or list all available tools.",
3309
+ usageHint: "Set `tool` for specific help. Omit to list all tools. `format`: short (one-liner), full (schema), markdown (formatted).",
3310
+ permission: "auto",
3311
+ mutating: false,
3312
+ timeoutMs: 5e3,
3313
+ inputSchema: {
3314
+ type: "object",
3315
+ properties: {
3316
+ tool: {
3317
+ type: "string",
3318
+ description: "Tool name to get help for (omit for all tools)"
3319
+ },
3320
+ format: {
3321
+ type: "string",
3322
+ enum: ["short", "full", "markdown"],
3323
+ description: "Output format (default: short)"
3324
+ },
3325
+ include_examples: {
3326
+ type: "boolean",
3327
+ description: "Include usage examples in output (default: false)"
3328
+ }
3329
+ }
3330
+ },
3331
+ async execute(input, ctx) {
3332
+ const format = input.format ?? "short";
3333
+ const includeExamples = input.include_examples ?? false;
3334
+ if (input.tool) {
3335
+ const tool = ctx.tools.find((t) => t.name === input.tool);
3336
+ if (!tool) {
3337
+ return {
3338
+ tool: input.tool,
3339
+ help: `No tool found with name "${input.tool}"`,
3340
+ tools: [],
3341
+ total: 0
3342
+ };
3343
+ }
3344
+ return {
3345
+ tool: tool.name,
3346
+ help: formatToolHelp(tool, format, includeExamples),
3347
+ tools: [{
3348
+ name: tool.name,
3349
+ description: tool.description,
3350
+ usageHint: tool.usageHint ?? "",
3351
+ inputSchema: tool.inputSchema,
3352
+ permission: tool.permission,
3353
+ mutating: tool.mutating
3354
+ }],
3355
+ total: 1
3356
+ };
3357
+ }
3358
+ const allTools = ctx.tools.map((t) => ({
3359
+ name: t.name,
3360
+ description: t.description,
3361
+ usageHint: t.usageHint ?? "",
3362
+ inputSchema: format === "full" ? t.inputSchema : void 0,
3363
+ permission: t.permission,
3364
+ mutating: t.mutating
3365
+ }));
3366
+ return {
3367
+ help: format === "markdown" ? formatAllToolsMarkdown(allTools) : formatAllToolsShort(allTools),
3368
+ tools: allTools,
3369
+ total: allTools.length
3370
+ };
3371
+ }
3372
+ };
3373
+ function formatToolHelp(tool, format, includeExamples) {
3374
+ const lines = [];
3375
+ if (format === "short") {
3376
+ lines.push(`${tool.name}: ${tool.description}`);
3377
+ if (tool.usageHint) lines.push(`Hint: ${tool.usageHint}`);
3378
+ return lines.join("\n");
3379
+ }
3380
+ if (format === "markdown") {
3381
+ lines.push(`## ${tool.name}`);
3382
+ lines.push("");
3383
+ lines.push(tool.description);
3384
+ lines.push("");
3385
+ lines.push("**Permission:** " + tool.permission);
3386
+ lines.push("**Mutating:** " + (tool.mutating ? "yes" : "no"));
3387
+ if (tool.usageHint) {
3388
+ lines.push("");
3389
+ lines.push("### Usage Hint");
3390
+ lines.push(tool.usageHint);
3391
+ }
3392
+ if (includeExamples && tool.inputSchema) {
3393
+ lines.push("");
3394
+ lines.push("### Input Schema");
3395
+ lines.push("```json");
3396
+ lines.push(JSON.stringify(tool.inputSchema, null, 2));
3397
+ lines.push("```");
3398
+ }
3399
+ return lines.join("\n");
3400
+ }
3401
+ lines.push(`Tool: ${tool.name}`);
3402
+ lines.push(`Description: ${tool.description}`);
3403
+ lines.push(`Permission: ${tool.permission}`);
3404
+ lines.push(`Mutating: ${tool.mutating}`);
3405
+ if (tool.usageHint) lines.push(`Usage: ${tool.usageHint}`);
3406
+ if (format === "full" && tool.inputSchema) {
3407
+ lines.push("Schema: " + JSON.stringify(tool.inputSchema, null, 2));
3408
+ }
3409
+ return lines.join("\n");
3410
+ }
3411
+ function formatAllToolsShort(tools) {
3412
+ return tools.map((t) => ` ${t.name.padEnd(16)} ${t.description}`).join("\n");
3413
+ }
3414
+ function formatAllToolsMarkdown(tools) {
3415
+ const lines = ["## Available Tools", ""];
3416
+ lines.push("| Tool | Description |");
3417
+ lines.push("|------|-------------|");
3418
+ for (const t of tools) {
3419
+ lines.push(`| \`${t.name}\` | ${t.description} |`);
3420
+ }
3421
+ return lines.join("\n");
3422
+ }
3423
+
3424
+ // src/memory.ts
3425
+ function rememberTool(memory) {
3426
+ return {
3427
+ name: "remember",
3428
+ description: "Persist a short note to project or user memory.",
3429
+ usageHint: "Use sparingly. Only for facts that should outlive the session (project conventions, user preferences). Transient state belongs in `todo`. Scope defaults to project-memory.",
3430
+ permission: "auto",
3431
+ mutating: true,
3432
+ timeoutMs: 2e3,
3433
+ inputSchema: {
3434
+ type: "object",
3435
+ properties: {
3436
+ text: { type: "string" },
3437
+ scope: { type: "string", enum: ["project-agents", "project-memory", "user-memory"] }
3438
+ },
3439
+ required: ["text"]
3440
+ },
3441
+ async execute(input) {
3442
+ if (!input?.text) throw new Error("remember: text is required");
3443
+ const scope = input.scope ?? "project-memory";
3444
+ await memory.remember(input.text, scope);
3445
+ return { ok: true, scope };
3446
+ }
3447
+ };
3448
+ }
3449
+ function forgetTool(memory) {
3450
+ return {
3451
+ name: "forget",
3452
+ description: "Remove memory entries matching a substring (case-insensitive).",
3453
+ usageHint: "Removes ALL matching bullet lines in the given scope. Use a unique substring.",
3454
+ permission: "confirm",
3455
+ mutating: true,
3456
+ timeoutMs: 2e3,
3457
+ inputSchema: {
3458
+ type: "object",
3459
+ properties: {
3460
+ query: { type: "string" },
3461
+ scope: { type: "string", enum: ["project-agents", "project-memory", "user-memory"] }
3462
+ },
3463
+ required: ["query"]
3464
+ },
3465
+ async execute(input) {
3466
+ if (!input?.query) throw new Error("forget: query is required");
3467
+ const scope = input.scope ?? "project-memory";
3468
+ const removed = await memory.forget(input.query, scope);
3469
+ return { removed, scope };
3470
+ }
3471
+ };
3472
+ }
3473
+
3474
+ // src/mode.ts
3475
+ function createModeTool(modeStore) {
3476
+ return {
3477
+ name: "mode",
3478
+ description: "Get, list, or switch the agent mode. Modes inject role-specific prompts into the system prompt.",
3479
+ usageHint: "Set `action`: `get` (current mode), `list` (all modes), `set <modeId>` (switch), `clear` (reset to default).",
3480
+ permission: "confirm",
3481
+ mutating: true,
3482
+ timeoutMs: 5e3,
3483
+ inputSchema: {
3484
+ type: "object",
3485
+ properties: {
3486
+ action: {
3487
+ type: "string",
3488
+ enum: ["get", "list", "set", "clear"],
3489
+ description: "Action: get current, list all, set mode, or clear"
3490
+ },
3491
+ mode: {
3492
+ type: "string",
3493
+ description: "Mode ID to switch to (required for action=set)"
3494
+ }
3495
+ },
3496
+ required: ["action"]
3497
+ },
3498
+ async execute(input) {
3499
+ switch (input.action) {
3500
+ case "get": {
3501
+ const mode = await modeStore.getActiveMode();
3502
+ return {
3503
+ action: "get",
3504
+ currentMode: mode?.id,
3505
+ success: true,
3506
+ message: mode ? `Current mode: ${mode.name} \u2014 ${mode.description}` : "No mode set (using default)"
3507
+ };
3508
+ }
3509
+ case "list": {
3510
+ const modes = await modeStore.listModes();
3511
+ const lines = modes.map((m) => ` ${m.id.padEnd(20)} ${m.name} \u2014 ${m.description}`).join("\n");
3512
+ return {
3513
+ action: "list",
3514
+ modes: modes.map((m) => ({ id: m.id, name: m.name, description: m.description })),
3515
+ success: true,
3516
+ message: lines
3517
+ };
3518
+ }
3519
+ case "set": {
3520
+ if (!input.mode) {
3521
+ return { action: "set", success: false, message: "mode is required for action=set" };
3522
+ }
3523
+ const mode = await modeStore.getMode(input.mode);
3524
+ if (!mode) {
3525
+ return { action: "set", success: false, message: `Mode "${input.mode}" not found` };
3526
+ }
3527
+ await modeStore.setActiveMode(input.mode);
3528
+ return {
3529
+ action: "set",
3530
+ currentMode: mode.id,
3531
+ success: true,
3532
+ message: `Switched to mode: ${mode.name}
3533
+
3534
+ ${mode.description}`
3535
+ };
3536
+ }
3537
+ case "clear": {
3538
+ await modeStore.setActiveMode(null);
3539
+ return {
3540
+ action: "clear",
3541
+ success: true,
3542
+ message: "Mode cleared \u2014 using default mode"
3543
+ };
3544
+ }
3545
+ default:
3546
+ return { action: input.action, success: false, message: `Unknown action "${input.action}"` };
3547
+ }
3548
+ }
3549
+ };
3550
+ }
3551
+
3552
+ // src/index.ts
3553
+ var builtinTools = [
3554
+ readTool,
3555
+ writeTool,
3556
+ editTool,
3557
+ replaceTool,
3558
+ globTool,
3559
+ grepTool,
3560
+ bashTool,
3561
+ execTool,
3562
+ fetchTool,
3563
+ searchTool,
3564
+ todoTool,
3565
+ gitTool,
3566
+ patchTool,
3567
+ jsonTool,
3568
+ diffTool,
3569
+ treeTool,
3570
+ lintTool,
3571
+ formatTool,
3572
+ typecheckTool,
3573
+ testTool,
3574
+ installTool,
3575
+ auditTool,
3576
+ outdatedTool,
3577
+ logsTool,
3578
+ documentTool,
3579
+ scaffoldTool,
3580
+ toolSearchTool,
3581
+ toolUseTool,
3582
+ batchToolUseTool,
3583
+ toolHelpTool
3584
+ ];
3585
+
3586
+ export { auditTool, bashTool, batchToolUseTool, builtinTools, createModeTool, diffTool, documentTool, editTool, execTool, fetchTool, forgetTool, formatTool, gitTool, globTool, grepTool, installTool, jsonTool, lintTool, logsTool, outdatedTool, patchTool, readTool, rememberTool, replaceTool, scaffoldTool, searchTool, testTool, todoTool, toolHelpTool, toolSearchTool, toolUseTool, treeTool, typecheckTool, writeTool };
3587
+ //# sourceMappingURL=index.js.map
3588
+ //# sourceMappingURL=index.js.map