wave-agent-sdk 0.0.1

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 (170) hide show
  1. package/README.md +32 -0
  2. package/dist/agent.d.ts +96 -0
  3. package/dist/agent.d.ts.map +1 -0
  4. package/dist/agent.js +286 -0
  5. package/dist/hooks/executor.d.ts +56 -0
  6. package/dist/hooks/executor.d.ts.map +1 -0
  7. package/dist/hooks/executor.js +312 -0
  8. package/dist/hooks/index.d.ts +17 -0
  9. package/dist/hooks/index.d.ts.map +1 -0
  10. package/dist/hooks/index.js +14 -0
  11. package/dist/hooks/manager.d.ts +90 -0
  12. package/dist/hooks/manager.d.ts.map +1 -0
  13. package/dist/hooks/manager.js +395 -0
  14. package/dist/hooks/matcher.d.ts +49 -0
  15. package/dist/hooks/matcher.d.ts.map +1 -0
  16. package/dist/hooks/matcher.js +147 -0
  17. package/dist/hooks/settings.d.ts +46 -0
  18. package/dist/hooks/settings.d.ts.map +1 -0
  19. package/dist/hooks/settings.js +100 -0
  20. package/dist/hooks/types.d.ts +80 -0
  21. package/dist/hooks/types.d.ts.map +1 -0
  22. package/dist/hooks/types.js +59 -0
  23. package/dist/index.d.ts +16 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +20 -0
  26. package/dist/managers/aiManager.d.ts +61 -0
  27. package/dist/managers/aiManager.d.ts.map +1 -0
  28. package/dist/managers/aiManager.js +415 -0
  29. package/dist/managers/backgroundBashManager.d.ts +27 -0
  30. package/dist/managers/backgroundBashManager.d.ts.map +1 -0
  31. package/dist/managers/backgroundBashManager.js +166 -0
  32. package/dist/managers/bashManager.d.ts +20 -0
  33. package/dist/managers/bashManager.d.ts.map +1 -0
  34. package/dist/managers/bashManager.js +66 -0
  35. package/dist/managers/mcpManager.d.ts +63 -0
  36. package/dist/managers/mcpManager.d.ts.map +1 -0
  37. package/dist/managers/mcpManager.js +378 -0
  38. package/dist/managers/messageManager.d.ts +85 -0
  39. package/dist/managers/messageManager.d.ts.map +1 -0
  40. package/dist/managers/messageManager.js +265 -0
  41. package/dist/managers/skillManager.d.ts +59 -0
  42. package/dist/managers/skillManager.d.ts.map +1 -0
  43. package/dist/managers/skillManager.js +317 -0
  44. package/dist/managers/slashCommandManager.d.ts +77 -0
  45. package/dist/managers/slashCommandManager.d.ts.map +1 -0
  46. package/dist/managers/slashCommandManager.js +208 -0
  47. package/dist/managers/toolManager.d.ts +23 -0
  48. package/dist/managers/toolManager.d.ts.map +1 -0
  49. package/dist/managers/toolManager.js +79 -0
  50. package/dist/services/aiService.d.ts +28 -0
  51. package/dist/services/aiService.d.ts.map +1 -0
  52. package/dist/services/aiService.js +180 -0
  53. package/dist/services/memory.d.ts +8 -0
  54. package/dist/services/memory.d.ts.map +1 -0
  55. package/dist/services/memory.js +128 -0
  56. package/dist/services/session.d.ts +54 -0
  57. package/dist/services/session.d.ts.map +1 -0
  58. package/dist/services/session.js +196 -0
  59. package/dist/tools/bashTool.d.ts +14 -0
  60. package/dist/tools/bashTool.d.ts.map +1 -0
  61. package/dist/tools/bashTool.js +351 -0
  62. package/dist/tools/deleteFileTool.d.ts +6 -0
  63. package/dist/tools/deleteFileTool.d.ts.map +1 -0
  64. package/dist/tools/deleteFileTool.js +67 -0
  65. package/dist/tools/editTool.d.ts +6 -0
  66. package/dist/tools/editTool.d.ts.map +1 -0
  67. package/dist/tools/editTool.js +168 -0
  68. package/dist/tools/globTool.d.ts +6 -0
  69. package/dist/tools/globTool.d.ts.map +1 -0
  70. package/dist/tools/globTool.js +113 -0
  71. package/dist/tools/grepTool.d.ts +6 -0
  72. package/dist/tools/grepTool.d.ts.map +1 -0
  73. package/dist/tools/grepTool.js +268 -0
  74. package/dist/tools/lsTool.d.ts +6 -0
  75. package/dist/tools/lsTool.d.ts.map +1 -0
  76. package/dist/tools/lsTool.js +160 -0
  77. package/dist/tools/multiEditTool.d.ts +6 -0
  78. package/dist/tools/multiEditTool.d.ts.map +1 -0
  79. package/dist/tools/multiEditTool.js +222 -0
  80. package/dist/tools/readTool.d.ts +6 -0
  81. package/dist/tools/readTool.d.ts.map +1 -0
  82. package/dist/tools/readTool.js +136 -0
  83. package/dist/tools/types.d.ts +35 -0
  84. package/dist/tools/types.d.ts.map +1 -0
  85. package/dist/tools/types.js +4 -0
  86. package/dist/tools/writeTool.d.ts +6 -0
  87. package/dist/tools/writeTool.d.ts.map +1 -0
  88. package/dist/tools/writeTool.js +138 -0
  89. package/dist/types.d.ts +212 -0
  90. package/dist/types.d.ts.map +1 -0
  91. package/dist/types.js +13 -0
  92. package/dist/utils/bashHistory.d.ts +46 -0
  93. package/dist/utils/bashHistory.d.ts.map +1 -0
  94. package/dist/utils/bashHistory.js +236 -0
  95. package/dist/utils/commandArgumentParser.d.ts +34 -0
  96. package/dist/utils/commandArgumentParser.d.ts.map +1 -0
  97. package/dist/utils/commandArgumentParser.js +123 -0
  98. package/dist/utils/constants.d.ts +27 -0
  99. package/dist/utils/constants.d.ts.map +1 -0
  100. package/dist/utils/constants.js +28 -0
  101. package/dist/utils/convertMessagesForAPI.d.ts +9 -0
  102. package/dist/utils/convertMessagesForAPI.d.ts.map +1 -0
  103. package/dist/utils/convertMessagesForAPI.js +189 -0
  104. package/dist/utils/customCommands.d.ts +14 -0
  105. package/dist/utils/customCommands.d.ts.map +1 -0
  106. package/dist/utils/customCommands.js +71 -0
  107. package/dist/utils/fileFilter.d.ts +26 -0
  108. package/dist/utils/fileFilter.d.ts.map +1 -0
  109. package/dist/utils/fileFilter.js +177 -0
  110. package/dist/utils/markdownParser.d.ts +27 -0
  111. package/dist/utils/markdownParser.d.ts.map +1 -0
  112. package/dist/utils/markdownParser.js +109 -0
  113. package/dist/utils/mcpUtils.d.ts +24 -0
  114. package/dist/utils/mcpUtils.d.ts.map +1 -0
  115. package/dist/utils/mcpUtils.js +51 -0
  116. package/dist/utils/messageOperations.d.ts +118 -0
  117. package/dist/utils/messageOperations.d.ts.map +1 -0
  118. package/dist/utils/messageOperations.js +334 -0
  119. package/dist/utils/path.d.ts +25 -0
  120. package/dist/utils/path.d.ts.map +1 -0
  121. package/dist/utils/path.js +109 -0
  122. package/dist/utils/skillParser.d.ts +18 -0
  123. package/dist/utils/skillParser.d.ts.map +1 -0
  124. package/dist/utils/skillParser.js +147 -0
  125. package/dist/utils/stringUtils.d.ts +13 -0
  126. package/dist/utils/stringUtils.d.ts.map +1 -0
  127. package/dist/utils/stringUtils.js +44 -0
  128. package/package.json +51 -0
  129. package/src/agent.ts +405 -0
  130. package/src/hooks/executor.ts +440 -0
  131. package/src/hooks/index.ts +52 -0
  132. package/src/hooks/manager.ts +618 -0
  133. package/src/hooks/matcher.ts +187 -0
  134. package/src/hooks/settings.ts +129 -0
  135. package/src/hooks/types.ts +169 -0
  136. package/src/index.ts +24 -0
  137. package/src/managers/aiManager.ts +573 -0
  138. package/src/managers/backgroundBashManager.ts +203 -0
  139. package/src/managers/bashManager.ts +97 -0
  140. package/src/managers/mcpManager.ts +493 -0
  141. package/src/managers/messageManager.ts +415 -0
  142. package/src/managers/skillManager.ts +404 -0
  143. package/src/managers/slashCommandManager.ts +293 -0
  144. package/src/managers/toolManager.ts +106 -0
  145. package/src/services/aiService.ts +252 -0
  146. package/src/services/memory.ts +149 -0
  147. package/src/services/session.ts +265 -0
  148. package/src/tools/bashTool.ts +402 -0
  149. package/src/tools/deleteFileTool.ts +81 -0
  150. package/src/tools/editTool.ts +192 -0
  151. package/src/tools/globTool.ts +135 -0
  152. package/src/tools/grepTool.ts +326 -0
  153. package/src/tools/lsTool.ts +187 -0
  154. package/src/tools/multiEditTool.ts +268 -0
  155. package/src/tools/readTool.ts +165 -0
  156. package/src/tools/types.ts +47 -0
  157. package/src/tools/writeTool.ts +163 -0
  158. package/src/types.ts +260 -0
  159. package/src/utils/bashHistory.ts +303 -0
  160. package/src/utils/commandArgumentParser.ts +153 -0
  161. package/src/utils/constants.ts +37 -0
  162. package/src/utils/convertMessagesForAPI.ts +236 -0
  163. package/src/utils/customCommands.ts +85 -0
  164. package/src/utils/fileFilter.ts +202 -0
  165. package/src/utils/markdownParser.ts +156 -0
  166. package/src/utils/mcpUtils.ts +81 -0
  167. package/src/utils/messageOperations.ts +506 -0
  168. package/src/utils/path.ts +118 -0
  169. package/src/utils/skillParser.ts +188 -0
  170. package/src/utils/stringUtils.ts +50 -0
@@ -0,0 +1,326 @@
1
+ import type { ToolPlugin, ToolResult, ToolContext } from "./types.js";
2
+ import { spawn } from "child_process";
3
+ import { getGlobIgnorePatterns } from "../utils/fileFilter.js";
4
+ import { rgPath } from "@vscode/ripgrep";
5
+ import { getDisplayPath } from "../utils/path.js";
6
+
7
+ /**
8
+ * Grep tool plugin - powerful search tool based on ripgrep
9
+ */
10
+ export const grepTool: ToolPlugin = {
11
+ name: "Grep",
12
+ config: {
13
+ type: "function",
14
+ function: {
15
+ name: "Grep",
16
+ description:
17
+ 'A powerful search tool built on ripgrep\n\n Usage:\n - ALWAYS use Grep for search tasks. NEVER invoke `grep` or `rg` as a Bash command. The Grep tool has been optimized for correct permissions and access.\n - Supports full regex syntax (e.g., "log.*Error", "function\\s+\\w+")\n - Filter files with glob parameter (e.g., "*.js", "**/*.tsx") or type parameter (e.g., "js", "py", "rust")\n - Output modes: "content" shows matching lines, "files_with_matches" shows only file paths (default), "count" shows match counts\n - Use Task tool for open-ended searches requiring multiple rounds\n - Pattern syntax: Uses ripgrep (not grep) - literal braces need escaping (use `interface\\{\\}` to find `interface{}` in Go code)\n - Multiline matching: By default patterns match within single lines only. For cross-line patterns like `struct \\{[\\s\\S]*?field`, use `multiline: true`',
18
+ parameters: {
19
+ type: "object",
20
+ properties: {
21
+ pattern: {
22
+ type: "string",
23
+ description:
24
+ "The regular expression pattern to search for in file contents",
25
+ },
26
+ path: {
27
+ type: "string",
28
+ description:
29
+ "File or directory to search in (rg PATH). Defaults to current working directory.",
30
+ },
31
+ glob: {
32
+ type: "string",
33
+ description:
34
+ 'Glob pattern to filter files (e.g. "*.js", "*.{ts,tsx}") - maps to rg --glob',
35
+ },
36
+ output_mode: {
37
+ type: "string",
38
+ enum: ["content", "files_with_matches", "count"],
39
+ description:
40
+ 'Output mode: "content" shows matching lines (supports -A/-B/-C context, -n line numbers, head_limit), "files_with_matches" shows file paths (supports head_limit), "count" shows match counts (supports head_limit). Defaults to "files_with_matches".',
41
+ },
42
+ "-B": {
43
+ type: "number",
44
+ description:
45
+ 'Number of lines to show before each match (rg -B). Requires output_mode: "content", ignored otherwise.',
46
+ },
47
+ "-A": {
48
+ type: "number",
49
+ description:
50
+ 'Number of lines to show after each match (rg -A). Requires output_mode: "content", ignored otherwise.',
51
+ },
52
+ "-C": {
53
+ type: "number",
54
+ description:
55
+ 'Number of lines to show before and after each match (rg -C). Requires output_mode: "content", ignored otherwise.',
56
+ },
57
+ "-n": {
58
+ type: "boolean",
59
+ description:
60
+ 'Show line numbers in output (rg -n). Requires output_mode: "content", ignored otherwise.',
61
+ },
62
+ "-i": {
63
+ type: "boolean",
64
+ description: "Case insensitive search (rg -i)",
65
+ },
66
+ type: {
67
+ type: "string",
68
+ description:
69
+ "File type to search (rg --type). Common types: js, py, rust, go, java, etc. More efficient than include for standard file types.",
70
+ },
71
+ head_limit: {
72
+ type: "number",
73
+ description:
74
+ 'Limit output to first N lines/entries, equivalent to "| head -N". Works across all output modes: content (limits output lines), files_with_matches (limits file paths), count (limits count entries). When unspecified, shows all results from ripgrep.',
75
+ },
76
+ multiline: {
77
+ type: "boolean",
78
+ description:
79
+ "Enable multiline mode where . matches newlines and patterns can span lines (rg -U --multiline-dotall). Default: false.",
80
+ },
81
+ },
82
+ required: ["pattern"],
83
+ },
84
+ },
85
+ },
86
+ execute: async (
87
+ args: Record<string, unknown>,
88
+ context: ToolContext,
89
+ ): Promise<ToolResult> => {
90
+ const pattern = args.pattern as string;
91
+ const searchPath = args.path as string;
92
+ const globPattern = args.glob as string;
93
+ const outputMode = (args.output_mode as string) || "files_with_matches";
94
+ const contextBefore = args["-B"] as number;
95
+ const contextAfter = args["-A"] as number;
96
+ const contextAround = args["-C"] as number;
97
+ const showLineNumbers = args["-n"] as boolean;
98
+ const caseInsensitive = args["-i"] as boolean;
99
+ const fileType = args.type as string;
100
+ const headLimit = args.head_limit as number;
101
+ const multiline = args.multiline as boolean;
102
+
103
+ if (!pattern || typeof pattern !== "string") {
104
+ return {
105
+ success: false,
106
+ content: "",
107
+ error: "pattern parameter is required and must be a string",
108
+ };
109
+ }
110
+
111
+ if (!rgPath) {
112
+ return {
113
+ success: false,
114
+ content: "",
115
+ error:
116
+ "ripgrep is not available. Please install @vscode/ripgrep package.",
117
+ };
118
+ }
119
+
120
+ try {
121
+ const workdir = context.workdir;
122
+ const rgArgs: string[] = ["--color=never"];
123
+
124
+ // Set output mode
125
+ if (outputMode === "files_with_matches") {
126
+ rgArgs.push("-l");
127
+ } else if (outputMode === "count") {
128
+ rgArgs.push("-c");
129
+ }
130
+ // content mode is default, no special parameters needed
131
+
132
+ // Add line numbers (only effective in content mode)
133
+ if (showLineNumbers && outputMode === "content") {
134
+ rgArgs.push("-n");
135
+ }
136
+
137
+ // Add file names (in content mode)
138
+ if (outputMode === "content") {
139
+ rgArgs.push("-H");
140
+ }
141
+
142
+ // Case insensitive
143
+ if (caseInsensitive) {
144
+ rgArgs.push("-i");
145
+ }
146
+
147
+ // Multiline mode
148
+ if (multiline) {
149
+ rgArgs.push("-U", "--multiline-dotall");
150
+ }
151
+
152
+ // Context lines (only effective in content mode)
153
+ if (outputMode === "content") {
154
+ if (contextAround) {
155
+ rgArgs.push("-C", contextAround.toString());
156
+ } else {
157
+ if (contextBefore) {
158
+ rgArgs.push("-B", contextBefore.toString());
159
+ }
160
+ if (contextAfter) {
161
+ rgArgs.push("-A", contextAfter.toString());
162
+ }
163
+ }
164
+ }
165
+
166
+ // File type filtering
167
+ if (fileType) {
168
+ rgArgs.push("--type", fileType);
169
+ }
170
+
171
+ // Glob pattern filtering
172
+ if (globPattern) {
173
+ rgArgs.push("--glob", globPattern);
174
+ }
175
+
176
+ // Get common ignore rules
177
+ const ignorePatterns = getGlobIgnorePatterns(workdir);
178
+ for (const exclude of ignorePatterns) {
179
+ rgArgs.push("--glob", `!${exclude}`);
180
+ }
181
+
182
+ // Add search pattern - use -e parameter to avoid patterns starting with - being mistaken as command line options
183
+ rgArgs.push("-e", pattern);
184
+
185
+ // Add search path
186
+ if (searchPath) {
187
+ rgArgs.push(searchPath);
188
+ } else {
189
+ rgArgs.push(".");
190
+ }
191
+
192
+ const result = await executeCommand(rgPath, rgArgs, workdir);
193
+
194
+ if (result.error && result.exitCode !== 1) {
195
+ // rg returns 1 for no matches, not an error
196
+ return {
197
+ success: false,
198
+ content: "",
199
+ error: `ripgrep failed: ${result.stderr}`,
200
+ };
201
+ }
202
+
203
+ const output = result.stdout.trim();
204
+ if (!output) {
205
+ return {
206
+ success: true,
207
+ content: "No matches found",
208
+ shortResult: "No matches found",
209
+ };
210
+ }
211
+
212
+ // Apply head_limit
213
+ let finalOutput = output;
214
+ let lines = output.split("\n");
215
+
216
+ if (headLimit && headLimit > 0 && lines.length > headLimit) {
217
+ lines = lines.slice(0, headLimit);
218
+ finalOutput = lines.join("\n");
219
+ }
220
+
221
+ // Generate short result
222
+ let shortResult: string;
223
+ const totalLines = output.split("\n").length;
224
+
225
+ if (outputMode === "files_with_matches") {
226
+ shortResult = `Found ${totalLines} file${totalLines === 1 ? "" : "s"}`;
227
+ } else if (outputMode === "count") {
228
+ shortResult = `Match counts for ${totalLines} file${totalLines === 1 ? "" : "s"}`;
229
+ } else {
230
+ shortResult = `Found ${totalLines} matching line${totalLines === 1 ? "" : "s"}`;
231
+ }
232
+
233
+ if (headLimit && totalLines > headLimit) {
234
+ shortResult += ` (showing first ${headLimit})`;
235
+ }
236
+
237
+ return {
238
+ success: true,
239
+ content: finalOutput,
240
+ shortResult,
241
+ };
242
+ } catch (error) {
243
+ return {
244
+ success: false,
245
+ content: "",
246
+ error: `Search failed: ${error instanceof Error ? error.message : String(error)}`,
247
+ };
248
+ }
249
+ },
250
+ formatCompactParams: (
251
+ params: Record<string, unknown>,
252
+ context: ToolContext,
253
+ ) => {
254
+ const pattern = params.pattern as string;
255
+ const outputMode = params.output_mode as string;
256
+ const fileType = params.type as string;
257
+ const path = params.path as string;
258
+
259
+ let result = pattern || "";
260
+
261
+ if (fileType) {
262
+ result += ` ${fileType}`;
263
+ }
264
+
265
+ if (path) {
266
+ const displayPath = getDisplayPath(path, context.workdir);
267
+ result += ` in ${displayPath}`;
268
+ }
269
+
270
+ if (outputMode && outputMode !== "files_with_matches") {
271
+ result += ` [${outputMode}]`;
272
+ }
273
+
274
+ return result;
275
+ },
276
+ };
277
+
278
+ /**
279
+ * Execute command and return result
280
+ */
281
+ function executeCommand(
282
+ command: string,
283
+ args: string[],
284
+ cwd: string,
285
+ ): Promise<{
286
+ stdout: string;
287
+ stderr: string;
288
+ exitCode: number | null;
289
+ error: boolean;
290
+ }> {
291
+ return new Promise((resolve) => {
292
+ const child = spawn(command, args, {
293
+ cwd,
294
+ stdio: ["ignore", "pipe", "pipe"],
295
+ });
296
+
297
+ let stdout = "";
298
+ let stderr = "";
299
+
300
+ child.stdout?.on("data", (data) => {
301
+ stdout += data.toString();
302
+ });
303
+
304
+ child.stderr?.on("data", (data) => {
305
+ stderr += data.toString();
306
+ });
307
+
308
+ child.on("close", (code) => {
309
+ resolve({
310
+ stdout,
311
+ stderr,
312
+ exitCode: code,
313
+ error: code !== 0 && code !== 1, // rg returns 1 for no matches, not an error
314
+ });
315
+ });
316
+
317
+ child.on("error", (err) => {
318
+ resolve({
319
+ stdout,
320
+ stderr: err.message,
321
+ exitCode: null,
322
+ error: true,
323
+ });
324
+ });
325
+ });
326
+ }
@@ -0,0 +1,187 @@
1
+ import * as fs from "fs";
2
+ import * as path from "path";
3
+ import { minimatch } from "minimatch";
4
+ import type { ToolPlugin, ToolResult, ToolContext } from "./types.js";
5
+ import { isBinary, getDisplayPath } from "@/utils/path.js";
6
+
7
+ /**
8
+ * LS Tool Plugin - List files and directories
9
+ */
10
+ export const lsTool: ToolPlugin = {
11
+ name: "LS",
12
+ config: {
13
+ type: "function",
14
+ function: {
15
+ name: "LS",
16
+ description:
17
+ "Lists files and directories in a given path. The path parameter must be an absolute path, not a relative path. You can optionally provide an array of glob patterns to ignore with the ignore parameter. You should generally prefer the Glob and Grep tools, if you know which directories to search.",
18
+ parameters: {
19
+ type: "object",
20
+ properties: {
21
+ path: {
22
+ type: "string",
23
+ description:
24
+ "The absolute path to the directory to list (must be absolute, not relative)",
25
+ },
26
+ ignore: {
27
+ type: "array",
28
+ items: {
29
+ type: "string",
30
+ },
31
+ description: "List of glob patterns to ignore",
32
+ },
33
+ },
34
+ required: ["path"],
35
+ },
36
+ },
37
+ },
38
+ execute: async (
39
+ args: Record<string, unknown>,
40
+ context: ToolContext,
41
+ ): Promise<ToolResult> => {
42
+ const targetPath = args.path as string;
43
+ const ignorePatterns = args.ignore as string[];
44
+
45
+ // Note: context.workdir is not used as this tool requires absolute paths
46
+ void context.workdir;
47
+
48
+ if (!targetPath || typeof targetPath !== "string") {
49
+ return {
50
+ success: false,
51
+ content: "",
52
+ error: "path parameter is required and must be a string",
53
+ };
54
+ }
55
+
56
+ // Validate that the path is absolute
57
+ if (!path.isAbsolute(targetPath)) {
58
+ return {
59
+ success: false,
60
+ content: "",
61
+ error: "Path must be an absolute path, not a relative path",
62
+ };
63
+ }
64
+
65
+ try {
66
+ // Check if path exists and is a directory
67
+ const stats = await fs.promises.stat(targetPath);
68
+ if (!stats.isDirectory()) {
69
+ return {
70
+ success: false,
71
+ content: "",
72
+ error: `Path ${targetPath} is not a directory`,
73
+ };
74
+ }
75
+
76
+ // Read directory contents
77
+ const entries = await fs.promises.readdir(targetPath, {
78
+ withFileTypes: true,
79
+ });
80
+
81
+ // Process directory items
82
+ const items: { name: string; type: string; size?: number }[] = [];
83
+
84
+ for (const entry of entries) {
85
+ const entryPath = path.join(targetPath, entry.name);
86
+
87
+ // Check if it should be ignored
88
+ if (ignorePatterns && Array.isArray(ignorePatterns)) {
89
+ const shouldIgnore = ignorePatterns.some(
90
+ (pattern) =>
91
+ minimatch(entry.name, pattern) || minimatch(entryPath, pattern),
92
+ );
93
+ if (shouldIgnore) {
94
+ continue;
95
+ }
96
+ }
97
+
98
+ if (entry.isDirectory()) {
99
+ items.push({
100
+ name: entry.name,
101
+ type: "directory",
102
+ });
103
+ } else if (entry.isFile()) {
104
+ try {
105
+ const fileStats = await fs.promises.stat(entryPath);
106
+ items.push({
107
+ name: entry.name,
108
+ type: "file",
109
+ size: fileStats.size,
110
+ });
111
+ } catch {
112
+ // If file stats cannot be obtained, still add the file but do not display size
113
+ items.push({
114
+ name: entry.name,
115
+ type: "file",
116
+ });
117
+ }
118
+ } else if (entry.isSymbolicLink()) {
119
+ items.push({
120
+ name: entry.name,
121
+ type: "symlink",
122
+ });
123
+ }
124
+ }
125
+
126
+ // Sort: directories first, then files, both alphabetically
127
+ items.sort((a, b) => {
128
+ if (a.type === "directory" && b.type !== "directory") return -1;
129
+ if (a.type !== "directory" && b.type === "directory") return 1;
130
+ return a.name.localeCompare(b.name);
131
+ });
132
+
133
+ let content = `Directory: ${targetPath}\n`;
134
+ content += `Total items: ${items.length}\n\n`;
135
+
136
+ for (const item of items) {
137
+ let typeIndicator: string;
138
+ switch (item.type) {
139
+ case "directory":
140
+ typeIndicator = "📁";
141
+ break;
142
+ case "symlink":
143
+ typeIndicator = "🔗";
144
+ break;
145
+ default:
146
+ typeIndicator = "📄";
147
+ }
148
+
149
+ const sizeInfo = item.size !== undefined ? ` (${item.size} bytes)` : "";
150
+ const binaryInfo =
151
+ item.type === "file" && isBinary(item.name) ? " [binary]" : "";
152
+ content += `${typeIndicator} ${item.name}${sizeInfo}${binaryInfo}\n`;
153
+ }
154
+
155
+ return {
156
+ success: true,
157
+ content: content.trim(),
158
+ shortResult: `${items.length} items (${items.filter((i) => i.type === "directory").length} dirs, ${items.filter((i) => i.type === "file").length} files)`,
159
+ };
160
+ } catch (error) {
161
+ return {
162
+ success: false,
163
+ content: "",
164
+ error: error instanceof Error ? error.message : String(error),
165
+ };
166
+ }
167
+ },
168
+ formatCompactParams: (
169
+ params: Record<string, unknown>,
170
+ context: ToolContext,
171
+ ) => {
172
+ const targetPath = params.path as string;
173
+ const ignorePatterns = params.ignore as string[];
174
+
175
+ let result = getDisplayPath(targetPath || "", context.workdir);
176
+
177
+ if (
178
+ ignorePatterns &&
179
+ Array.isArray(ignorePatterns) &&
180
+ ignorePatterns.length > 0
181
+ ) {
182
+ result += ` ignore: ${ignorePatterns.join(", ")}`;
183
+ }
184
+
185
+ return result;
186
+ },
187
+ };