@wrongstack/tools 0.1.1 → 0.1.3

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