@aexol/spectral 0.9.158 → 0.9.160

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 (45) hide show
  1. package/dist/agent/index.d.ts.map +1 -1
  2. package/dist/agent/index.js +29 -5
  3. package/dist/extensions/desktop-screenshot/index.d.ts.map +1 -1
  4. package/dist/extensions/desktop-screenshot/index.js +46 -39
  5. package/dist/index.d.ts +2 -2
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/memory/tool-output-compressor.d.ts +8 -2
  9. package/dist/memory/tool-output-compressor.d.ts.map +1 -1
  10. package/dist/memory/tool-output-compressor.js +8 -83
  11. package/dist/sdk/coding-agent/core/compaction/policy.js +6 -6
  12. package/dist/sdk/coding-agent/core/extensions/index.d.ts +2 -2
  13. package/dist/sdk/coding-agent/core/extensions/index.d.ts.map +1 -1
  14. package/dist/sdk/coding-agent/core/extensions/index.js +1 -1
  15. package/dist/sdk/coding-agent/core/extensions/types.d.ts +3 -44
  16. package/dist/sdk/coding-agent/core/extensions/types.d.ts.map +1 -1
  17. package/dist/sdk/coding-agent/core/extensions/types.js +0 -12
  18. package/dist/sdk/coding-agent/core/package-manager.d.ts +0 -15
  19. package/dist/sdk/coding-agent/core/package-manager.d.ts.map +1 -1
  20. package/dist/sdk/coding-agent/core/package-manager.js +0 -80
  21. package/dist/sdk/coding-agent/core/sdk.d.ts +2 -2
  22. package/dist/sdk/coding-agent/core/sdk.d.ts.map +1 -1
  23. package/dist/sdk/coding-agent/core/sdk.js +3 -3
  24. package/dist/sdk/coding-agent/core/tools/bash-blocklist.js +1 -1
  25. package/dist/sdk/coding-agent/core/tools/index.d.ts +1 -13
  26. package/dist/sdk/coding-agent/core/tools/index.d.ts.map +1 -1
  27. package/dist/sdk/coding-agent/core/tools/index.js +0 -44
  28. package/dist/sdk/coding-agent/index.d.ts +4 -4
  29. package/dist/sdk/coding-agent/index.d.ts.map +1 -1
  30. package/dist/sdk/coding-agent/index.js +3 -3
  31. package/dist/server/agent-bridge.d.ts.map +1 -1
  32. package/dist/server/agent-bridge.js +1 -0
  33. package/package.json +1 -1
  34. package/dist/sdk/coding-agent/core/tools/edit.d.ts +0 -45
  35. package/dist/sdk/coding-agent/core/tools/edit.d.ts.map +0 -1
  36. package/dist/sdk/coding-agent/core/tools/edit.js +0 -216
  37. package/dist/sdk/coding-agent/core/tools/grep.d.ts +0 -37
  38. package/dist/sdk/coding-agent/core/tools/grep.d.ts.map +0 -1
  39. package/dist/sdk/coding-agent/core/tools/grep.js +0 -288
  40. package/dist/sdk/coding-agent/core/tools/ls.d.ts +0 -37
  41. package/dist/sdk/coding-agent/core/tools/ls.d.ts.map +0 -1
  42. package/dist/sdk/coding-agent/core/tools/ls.js +0 -156
  43. package/dist/sdk/coding-agent/core/tools/write.d.ts +0 -23
  44. package/dist/sdk/coding-agent/core/tools/write.d.ts.map +0 -1
  45. package/dist/sdk/coding-agent/core/tools/write.js +0 -134
@@ -1,288 +0,0 @@
1
- import { createInterface } from "node:readline";
2
- import { spawn } from "child_process";
3
- import { readFileSync, statSync } from "fs";
4
- import path from "path";
5
- import { Type } from "typebox";
6
- import { ensureTool } from "../../utils/tools-manager.js";
7
- import { resolveToCwd } from "./path-utils.js";
8
- import { getTextOutput, shortenPath, str } from "./render-utils.js";
9
- import { wrapToolDefinition } from "./tool-definition-wrapper.js";
10
- import { DEFAULT_MAX_BYTES, formatSize, GREP_MAX_LINE_LENGTH, truncateHead, truncateLine, } from "./truncate.js";
11
- const grepSchema = Type.Object({
12
- pattern: Type.String({ description: "Search pattern (regex or literal string)" }),
13
- path: Type.Optional(Type.String({ description: "Directory or file to search (default: current directory)" })),
14
- glob: Type.Optional(Type.String({ description: "Filter files by glob pattern, e.g. '*.ts' or '**/*.spec.ts'" })),
15
- ignoreCase: Type.Optional(Type.Boolean({ description: "Case-insensitive search (default: false)" })),
16
- literal: Type.Optional(Type.Boolean({ description: "Treat pattern as literal string instead of regex (default: false)" })),
17
- context: Type.Optional(Type.Number({ description: "Number of lines to show before and after each match (default: 0)" })),
18
- limit: Type.Optional(Type.Number({ description: "Maximum number of matches to return (default: 100)" })),
19
- });
20
- const DEFAULT_LIMIT = 100;
21
- const defaultGrepOperations = {
22
- isDirectory: (p) => statSync(p).isDirectory(),
23
- readFile: (p) => readFileSync(p, "utf-8"),
24
- };
25
- function formatGrepCall(args, _theme) {
26
- const pattern = str(args?.pattern);
27
- const rawPath = str(args?.path);
28
- const path = rawPath !== null ? shortenPath(rawPath || ".") : null;
29
- const glob = str(args?.glob);
30
- const limit = args?.limit;
31
- const invalidArg = "[invalid]";
32
- let text = `grep ${pattern === null ? invalidArg : `/${pattern || ""}/`} in ${path === null ? invalidArg : path}`;
33
- if (glob)
34
- text += ` (${glob})`;
35
- if (limit !== undefined)
36
- text += ` limit ${limit}`;
37
- return text;
38
- }
39
- function formatGrepResult(result, options, _theme, showImages) {
40
- const output = getTextOutput(result, showImages).trim();
41
- let text = "";
42
- if (output) {
43
- const lines = output.split("\n");
44
- const maxLines = options.expanded ? lines.length : 15;
45
- const displayLines = lines.slice(0, maxLines);
46
- const remaining = lines.length - maxLines;
47
- text += `\n${displayLines.join("\n")}`;
48
- if (remaining > 0) {
49
- text += `\n... (${remaining} more lines)`;
50
- }
51
- }
52
- const matchLimit = result.details?.matchLimitReached;
53
- const truncation = result.details?.truncation;
54
- const linesTruncated = result.details?.linesTruncated;
55
- if (matchLimit || truncation?.truncated || linesTruncated) {
56
- const warnings = [];
57
- if (matchLimit)
58
- warnings.push(`${matchLimit} matches limit`);
59
- if (truncation?.truncated)
60
- warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);
61
- if (linesTruncated)
62
- warnings.push("some lines truncated");
63
- text += `\n[Truncated: ${warnings.join(", ")}]`;
64
- }
65
- return text;
66
- }
67
- export function createGrepToolDefinition(cwd, options) {
68
- const customOps = options?.operations;
69
- return {
70
- name: "grep",
71
- label: "grep",
72
- description: `Search file contents for a pattern. Returns matching lines with file paths and line numbers. Respects .gitignore. Output is truncated to ${DEFAULT_LIMIT} matches or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Long lines are truncated to ${GREP_MAX_LINE_LENGTH} chars.`,
73
- promptSnippet: "Search file contents for patterns (respects .gitignore)",
74
- parameters: grepSchema,
75
- async execute(_toolCallId, { pattern, path: searchDir, glob, ignoreCase, literal, context, limit, }, signal, _onUpdate, _ctx) {
76
- return new Promise((resolve, reject) => {
77
- if (signal?.aborted) {
78
- reject(new Error("Operation aborted"));
79
- return;
80
- }
81
- let settled = false;
82
- const settle = (fn) => {
83
- if (!settled) {
84
- settled = true;
85
- fn();
86
- }
87
- };
88
- (async () => {
89
- try {
90
- const rgPath = await ensureTool("rg", true);
91
- if (!rgPath) {
92
- settle(() => reject(new Error("ripgrep (rg) is not available and could not be downloaded")));
93
- return;
94
- }
95
- const searchPath = resolveToCwd(searchDir || ".", cwd);
96
- const ops = customOps ?? defaultGrepOperations;
97
- let isDirectory;
98
- try {
99
- isDirectory = await ops.isDirectory(searchPath);
100
- }
101
- catch {
102
- settle(() => reject(new Error(`Path not found: ${searchPath}`)));
103
- return;
104
- }
105
- const contextValue = context && context > 0 ? context : 0;
106
- const effectiveLimit = Math.max(1, limit ?? DEFAULT_LIMIT);
107
- const formatPath = (filePath) => {
108
- if (isDirectory) {
109
- const relative = path.relative(searchPath, filePath);
110
- if (relative && !relative.startsWith("..")) {
111
- return relative.replace(/\\/g, "/");
112
- }
113
- }
114
- return path.basename(filePath);
115
- };
116
- const fileCache = new Map();
117
- const getFileLines = async (filePath) => {
118
- let lines = fileCache.get(filePath);
119
- if (!lines) {
120
- try {
121
- const content = await ops.readFile(filePath);
122
- lines = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
123
- }
124
- catch {
125
- lines = [];
126
- }
127
- fileCache.set(filePath, lines);
128
- }
129
- return lines;
130
- };
131
- const args = ["--json", "--line-number", "--color=never", "--hidden"];
132
- if (ignoreCase)
133
- args.push("--ignore-case");
134
- if (literal)
135
- args.push("--fixed-strings");
136
- if (glob)
137
- args.push("--glob", glob);
138
- args.push("--", pattern, searchPath);
139
- const child = spawn(rgPath, args, { stdio: ["ignore", "pipe", "pipe"] });
140
- const rl = createInterface({ input: child.stdout });
141
- let stderr = "";
142
- let matchCount = 0;
143
- let matchLimitReached = false;
144
- let linesTruncated = false;
145
- let aborted = false;
146
- let killedDueToLimit = false;
147
- const outputLines = [];
148
- const cleanup = () => {
149
- rl.close();
150
- signal?.removeEventListener("abort", onAbort);
151
- };
152
- const stopChild = (dueToLimit = false) => {
153
- if (!child.killed) {
154
- killedDueToLimit = dueToLimit;
155
- child.kill();
156
- }
157
- };
158
- const onAbort = () => {
159
- aborted = true;
160
- stopChild();
161
- };
162
- signal?.addEventListener("abort", onAbort, { once: true });
163
- child.stderr?.on("data", (chunk) => {
164
- stderr += chunk.toString();
165
- });
166
- const formatBlock = async (filePath, lineNumber) => {
167
- const relativePath = formatPath(filePath);
168
- const lines = await getFileLines(filePath);
169
- if (!lines.length)
170
- return [`${relativePath}:${lineNumber}: (unable to read file)`];
171
- const block = [];
172
- const start = contextValue > 0 ? Math.max(1, lineNumber - contextValue) : lineNumber;
173
- const end = contextValue > 0 ? Math.min(lines.length, lineNumber + contextValue) : lineNumber;
174
- for (let current = start; current <= end; current++) {
175
- const lineText = lines[current - 1] ?? "";
176
- const sanitized = lineText.replace(/\r/g, "");
177
- const isMatchLine = current === lineNumber;
178
- // Truncate long lines so grep output stays compact.
179
- const { text: truncatedText, wasTruncated } = truncateLine(sanitized);
180
- if (wasTruncated)
181
- linesTruncated = true;
182
- if (isMatchLine)
183
- block.push(`${relativePath}:${current}: ${truncatedText}`);
184
- else
185
- block.push(`${relativePath}-${current}- ${truncatedText}`);
186
- }
187
- return block;
188
- };
189
- // Collect matches during streaming, then format them after rg exits.
190
- const matches = [];
191
- rl.on("line", (line) => {
192
- if (!line.trim() || matchCount >= effectiveLimit)
193
- return;
194
- let event;
195
- try {
196
- event = JSON.parse(line);
197
- }
198
- catch {
199
- return;
200
- }
201
- if (event.type === "match") {
202
- matchCount++;
203
- const filePath = event.data?.path?.text;
204
- const lineNumber = event.data?.line_number;
205
- const lineText = event.data?.lines?.text;
206
- if (filePath && typeof lineNumber === "number")
207
- matches.push({ filePath, lineNumber, lineText });
208
- if (matchCount >= effectiveLimit) {
209
- matchLimitReached = true;
210
- stopChild(true);
211
- }
212
- }
213
- });
214
- child.on("error", (error) => {
215
- cleanup();
216
- settle(() => reject(new Error(`Failed to run ripgrep: ${error.message}`)));
217
- });
218
- child.on("close", async (code) => {
219
- cleanup();
220
- if (aborted) {
221
- settle(() => reject(new Error("Operation aborted")));
222
- return;
223
- }
224
- if (!killedDueToLimit && code !== 0 && code !== 1) {
225
- const errorMsg = stderr.trim() || `ripgrep exited with code ${code}`;
226
- settle(() => reject(new Error(errorMsg)));
227
- return;
228
- }
229
- if (matchCount === 0) {
230
- settle(() => resolve({ content: [{ type: "text", text: "No matches found" }], details: undefined }));
231
- return;
232
- }
233
- // Format matches after streaming finishes so custom readFile() backends can be async.
234
- for (const match of matches) {
235
- if (contextValue === 0 && match.lineText !== undefined) {
236
- const relativePath = formatPath(match.filePath);
237
- const sanitized = match.lineText
238
- .replace(/\r\n/g, "\n")
239
- .replace(/\r/g, "")
240
- .replace(/\n$/, "");
241
- const { text: truncatedText, wasTruncated } = truncateLine(sanitized);
242
- if (wasTruncated)
243
- linesTruncated = true;
244
- outputLines.push(`${relativePath}:${match.lineNumber}: ${truncatedText}`);
245
- }
246
- else {
247
- const block = await formatBlock(match.filePath, match.lineNumber);
248
- outputLines.push(...block);
249
- }
250
- }
251
- const rawOutput = outputLines.join("\n");
252
- // Apply byte truncation. There is no line limit here because the match limit already capped rows.
253
- const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });
254
- let output = truncation.content;
255
- const details = {};
256
- // Build actionable notices for truncation and match limits.
257
- const notices = [];
258
- if (matchLimitReached) {
259
- notices.push(`${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`);
260
- details.matchLimitReached = effectiveLimit;
261
- }
262
- if (truncation.truncated) {
263
- notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
264
- details.truncation = truncation;
265
- }
266
- if (linesTruncated) {
267
- notices.push(`Some lines truncated to ${GREP_MAX_LINE_LENGTH} chars. Use read tool to see full lines`);
268
- details.linesTruncated = true;
269
- }
270
- if (notices.length > 0)
271
- output += `\n\n[${notices.join(". ")}]`;
272
- settle(() => resolve({
273
- content: [{ type: "text", text: output }],
274
- details: Object.keys(details).length > 0 ? details : undefined,
275
- }));
276
- });
277
- }
278
- catch (err) {
279
- settle(() => reject(err));
280
- }
281
- })();
282
- });
283
- },
284
- };
285
- }
286
- export function createGrepTool(cwd, options) {
287
- return wrapToolDefinition(createGrepToolDefinition(cwd, options));
288
- }
@@ -1,37 +0,0 @@
1
- import type { AgentTool } from "../../../agent-core/index.js";
2
- import { type Static, Type } from "typebox";
3
- import type { ToolDefinition } from "../extensions/types.js";
4
- import { type TruncationResult } from "./truncate.js";
5
- declare const lsSchema: Type.TObject<{
6
- path: Type.TOptional<Type.TString>;
7
- limit: Type.TOptional<Type.TNumber>;
8
- }>;
9
- export type LsToolInput = Static<typeof lsSchema>;
10
- export interface LsToolDetails {
11
- truncation?: TruncationResult;
12
- entryLimitReached?: number;
13
- }
14
- /**
15
- * Pluggable operations for the ls tool.
16
- * Override these to delegate directory listing to remote systems (for example SSH).
17
- */
18
- export interface LsOperations {
19
- /** Check if path exists */
20
- exists: (absolutePath: string) => Promise<boolean> | boolean;
21
- /** Get file or directory stats. Throws if not found. */
22
- stat: (absolutePath: string) => Promise<{
23
- isDirectory: () => boolean;
24
- }> | {
25
- isDirectory: () => boolean;
26
- };
27
- /** Read directory entries */
28
- readdir: (absolutePath: string) => Promise<string[]> | string[];
29
- }
30
- export interface LsToolOptions {
31
- /** Custom operations for directory listing. Default: local filesystem */
32
- operations?: LsOperations;
33
- }
34
- export declare function createLsToolDefinition(cwd: string, options?: LsToolOptions): ToolDefinition<typeof lsSchema, LsToolDetails | undefined>;
35
- export declare function createLsTool(cwd: string, options?: LsToolOptions): AgentTool<typeof lsSchema>;
36
- export {};
37
- //# sourceMappingURL=ls.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ls.d.ts","sourceRoot":"","sources":["../../../../../src/sdk/coding-agent/core/tools/ls.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAG9D,OAAO,EAAE,KAAK,MAAM,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAI7D,OAAO,EAAiC,KAAK,gBAAgB,EAAgB,MAAM,eAAe,CAAC;AAEnG,QAAA,MAAM,QAAQ;;;EAGZ,CAAC;AAEH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,QAAQ,CAAC,CAAC;AAIlD,MAAM,WAAW,aAAa;IAC7B,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAC9B,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC5B,2BAA2B;IAC3B,MAAM,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;IAC7D,wDAAwD;IACxD,IAAI,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,OAAO,CAAC;QAAE,WAAW,EAAE,MAAM,OAAO,CAAA;KAAE,CAAC,GAAG;QAAE,WAAW,EAAE,MAAM,OAAO,CAAA;KAAE,CAAC;IACzG,6BAA6B;IAC7B,OAAO,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC;CAChE;AAQD,MAAM,WAAW,aAAa;IAC7B,yEAAyE;IACzE,UAAU,CAAC,EAAE,YAAY,CAAC;CAC1B;AAkDD,wBAAgB,sBAAsB,CACrC,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,aAAa,GACrB,cAAc,CAAC,OAAO,QAAQ,EAAE,aAAa,GAAG,SAAS,CAAC,CAiH5D;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC,OAAO,QAAQ,CAAC,CAE7F"}
@@ -1,156 +0,0 @@
1
- import { existsSync, readdirSync, statSync } from "fs";
2
- import nodePath from "path";
3
- import { Type } from "typebox";
4
- import { resolveToCwd } from "./path-utils.js";
5
- import { getTextOutput, shortenPath, str } from "./render-utils.js";
6
- import { wrapToolDefinition } from "./tool-definition-wrapper.js";
7
- import { DEFAULT_MAX_BYTES, formatSize, truncateHead } from "./truncate.js";
8
- const lsSchema = Type.Object({
9
- path: Type.Optional(Type.String({ description: "Directory to list (default: current directory)" })),
10
- limit: Type.Optional(Type.Number({ description: "Maximum number of entries to return (default: 500)" })),
11
- });
12
- const DEFAULT_LIMIT = 500;
13
- const defaultLsOperations = {
14
- exists: existsSync,
15
- stat: statSync,
16
- readdir: readdirSync,
17
- };
18
- function formatLsCall(args, _theme) {
19
- const rawPath = str(args?.path);
20
- const path = rawPath !== null ? shortenPath(rawPath || ".") : null;
21
- const limit = args?.limit;
22
- const invalidArg = "[invalid]";
23
- let text = `ls ${path === null ? invalidArg : path}`;
24
- if (limit !== undefined) {
25
- text += ` (limit ${limit})`;
26
- }
27
- return text;
28
- }
29
- function formatLsResult(result, options, _theme, showImages) {
30
- const output = getTextOutput(result, showImages).trim();
31
- let text = "";
32
- if (output) {
33
- const lines = output.split("\n");
34
- const maxLines = options.expanded ? lines.length : 20;
35
- const displayLines = lines.slice(0, maxLines);
36
- const remaining = lines.length - maxLines;
37
- text += `\n${displayLines.join("\n")}`;
38
- if (remaining > 0) {
39
- text += `\n... (${remaining} more lines)`;
40
- }
41
- }
42
- const entryLimit = result.details?.entryLimitReached;
43
- const truncation = result.details?.truncation;
44
- if (entryLimit || truncation?.truncated) {
45
- const warnings = [];
46
- if (entryLimit)
47
- warnings.push(`${entryLimit} entries limit`);
48
- if (truncation?.truncated)
49
- warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);
50
- text += `\n[Truncated: ${warnings.join(", ")}]`;
51
- }
52
- return text;
53
- }
54
- export function createLsToolDefinition(cwd, options) {
55
- const ops = options?.operations ?? defaultLsOperations;
56
- return {
57
- name: "ls",
58
- label: "ls",
59
- description: `List directory contents. Returns entries sorted alphabetically, with '/' suffix for directories. Includes dotfiles. Output is truncated to ${DEFAULT_LIMIT} entries or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first).`,
60
- promptSnippet: "List directory contents",
61
- parameters: lsSchema,
62
- async execute(_toolCallId, { path, limit }, signal, _onUpdate, _ctx) {
63
- return new Promise((resolve, reject) => {
64
- if (signal?.aborted) {
65
- reject(new Error("Operation aborted"));
66
- return;
67
- }
68
- const onAbort = () => reject(new Error("Operation aborted"));
69
- signal?.addEventListener("abort", onAbort, { once: true });
70
- (async () => {
71
- try {
72
- const dirPath = resolveToCwd(path || ".", cwd);
73
- const effectiveLimit = limit ?? DEFAULT_LIMIT;
74
- // Check if path exists.
75
- if (!(await ops.exists(dirPath))) {
76
- reject(new Error(`Path not found: ${dirPath}`));
77
- return;
78
- }
79
- // Check if path is a directory.
80
- const stat = await ops.stat(dirPath);
81
- if (!stat.isDirectory()) {
82
- reject(new Error(`Not a directory: ${dirPath}`));
83
- return;
84
- }
85
- // Read directory entries.
86
- let entries;
87
- try {
88
- entries = await ops.readdir(dirPath);
89
- }
90
- catch (e) {
91
- reject(new Error(`Cannot read directory: ${e.message}`));
92
- return;
93
- }
94
- // Sort alphabetically, case-insensitive.
95
- entries.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
96
- // Format entries with directory indicators.
97
- const results = [];
98
- let entryLimitReached = false;
99
- for (const entry of entries) {
100
- if (results.length >= effectiveLimit) {
101
- entryLimitReached = true;
102
- break;
103
- }
104
- const fullPath = nodePath.join(dirPath, entry);
105
- let suffix = "";
106
- try {
107
- const entryStat = await ops.stat(fullPath);
108
- if (entryStat.isDirectory())
109
- suffix = "/";
110
- }
111
- catch {
112
- // Skip entries we cannot stat.
113
- continue;
114
- }
115
- results.push(entry + suffix);
116
- }
117
- signal?.removeEventListener("abort", onAbort);
118
- if (results.length === 0) {
119
- resolve({ content: [{ type: "text", text: "(empty directory)" }], details: undefined });
120
- return;
121
- }
122
- const rawOutput = results.join("\n");
123
- // Apply byte truncation. There is no separate line limit because entry count is already capped.
124
- const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });
125
- let output = truncation.content;
126
- const details = {};
127
- // Build actionable notices for truncation and entry limits.
128
- const notices = [];
129
- if (entryLimitReached) {
130
- notices.push(`${effectiveLimit} entries limit reached. Use limit=${effectiveLimit * 2} for more`);
131
- details.entryLimitReached = effectiveLimit;
132
- }
133
- if (truncation.truncated) {
134
- notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
135
- details.truncation = truncation;
136
- }
137
- if (notices.length > 0) {
138
- output += `\n\n[${notices.join(". ")}]`;
139
- }
140
- resolve({
141
- content: [{ type: "text", text: output }],
142
- details: Object.keys(details).length > 0 ? details : undefined,
143
- });
144
- }
145
- catch (e) {
146
- signal?.removeEventListener("abort", onAbort);
147
- reject(e);
148
- }
149
- })();
150
- });
151
- },
152
- };
153
- }
154
- export function createLsTool(cwd, options) {
155
- return wrapToolDefinition(createLsToolDefinition(cwd, options));
156
- }
@@ -1,23 +0,0 @@
1
- import type { AgentTool } from "../../../agent-core/index.js";
2
- import { type Static, Type } from "typebox";
3
- import type { ToolDefinition } from "../extensions/types.js";
4
- declare const writeSchema: Type.TObject<{
5
- path: Type.TString;
6
- content: Type.TString;
7
- }>;
8
- export type WriteToolInput = Static<typeof writeSchema>;
9
- export interface WriteOperations {
10
- /**
11
- * Optional atomic write override (temp file + rename). When omitted,
12
- * the default {@link atomicWriteFile} helper is used.
13
- */
14
- atomicWrite?: (absolutePath: string, content: string) => Promise<void>;
15
- mkdir: (dir: string) => Promise<void>;
16
- }
17
- export interface WriteToolOptions {
18
- operations?: WriteOperations;
19
- }
20
- export declare function createWriteToolDefinition(cwd: string, options?: WriteToolOptions): ToolDefinition<typeof writeSchema, undefined>;
21
- export declare function createWriteTool(cwd: string, options?: WriteToolOptions): AgentTool<typeof writeSchema>;
22
- export {};
23
- //# sourceMappingURL=write.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"write.d.ts","sourceRoot":"","sources":["../../../../../src/sdk/coding-agent/core/tools/write.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAG9D,OAAO,EAAE,KAAK,MAAM,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAE5C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAe7D,QAAA,MAAM,WAAW;;;EAKf,CAAC;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,OAAO,WAAW,CAAC,CAAC;AACxD,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,WAAW,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACvE,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACvC;AAID,MAAM,WAAW,gBAAgB;IAC/B,UAAU,CAAC,EAAE,eAAe,CAAC;CAC9B;AA+DD,wBAAgB,yBAAyB,CACvC,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,gBAAgB,GACzB,cAAc,CAAC,OAAO,WAAW,EAAE,SAAS,CAAC,CA+E/C;AACD,wBAAgB,eAAe,CAC7B,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,gBAAgB,GACzB,SAAS,CAAC,OAAO,WAAW,CAAC,CAE/B"}
@@ -1,134 +0,0 @@
1
- import { mkdir as fsMkdir } from "fs/promises";
2
- import { dirname } from "path";
3
- import { Type } from "typebox";
4
- import { getLanguageFromPath, highlightCode } from "../theme.js";
5
- import { atomicWriteFile, defaultAtomicWriteOperations, } from "./atomic-write.js";
6
- import { withFileMutationQueue } from "./file-mutation-queue.js";
7
- import { resolveToCwd } from "./path-utils.js";
8
- import { normalizeDisplayText, replaceTabs, shortenPath, str, } from "./render-utils.js";
9
- import { wrapToolDefinition } from "./tool-definition-wrapper.js";
10
- const writeSchema = Type.Object({
11
- path: Type.String({
12
- description: "Path to the file to write (relative or absolute)",
13
- }),
14
- content: Type.String({ description: "Content to write to the file" }),
15
- });
16
- const defaultWriteOperations = {
17
- mkdir: (dir) => fsMkdir(dir, { recursive: true }).then(() => { }),
18
- };
19
- function trimTrailingEmptyLines(lines) {
20
- let end = lines.length;
21
- while (end > 0 && lines[end - 1] === "") {
22
- end--;
23
- }
24
- return lines.slice(0, end);
25
- }
26
- function formatWriteCall(args, options, _theme) {
27
- const rawPath = str(args?.file_path ?? args?.path);
28
- const fileContent = str(args?.content);
29
- const path = rawPath !== null ? shortenPath(rawPath) : null;
30
- const pathDisplay = path === null ? "[invalid]" : path || "...";
31
- let text = `write ${pathDisplay}`;
32
- if (fileContent === null) {
33
- text += `\n\n[invalid content arg - expected string]`;
34
- }
35
- else if (fileContent) {
36
- const lines = trimTrailingEmptyLines(normalizeDisplayText(fileContent).split("\n"));
37
- const totalLines = lines.length;
38
- const maxLines = options.expanded ? totalLines : 10;
39
- const preview = lines.slice(0, maxLines);
40
- const lang = rawPath ? getLanguageFromPath(rawPath) : undefined;
41
- const displayLines = lang
42
- ? highlightCode(replaceTabs(preview.join("\n")), lang)
43
- : preview;
44
- const remaining = totalLines - maxLines;
45
- text += `\n\n${displayLines.join("\n")}`;
46
- if (remaining > 0) {
47
- text += `\n... (${remaining} more lines, ${totalLines} total)`;
48
- }
49
- }
50
- return text;
51
- }
52
- function formatWriteResult(result, _theme) {
53
- if (!result.isError) {
54
- return undefined;
55
- }
56
- const output = result.content
57
- .filter((c) => c.type === "text")
58
- .map((c) => c.text || "")
59
- .join("\n");
60
- if (!output) {
61
- return undefined;
62
- }
63
- return `\n${output}`;
64
- }
65
- export function createWriteToolDefinition(cwd, options) {
66
- const ops = options?.operations ?? defaultWriteOperations;
67
- const atomicOps = defaultAtomicWriteOperations;
68
- return {
69
- name: "write",
70
- label: "write",
71
- description: "Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.",
72
- promptSnippet: "Create or overwrite files",
73
- promptGuidelines: ["Use write only for new files or complete rewrites."],
74
- parameters: writeSchema,
75
- async execute(_toolCallId, { path, content }, signal, _onUpdate, _ctx) {
76
- const absolutePath = resolveToCwd(path, cwd);
77
- const dir = dirname(absolutePath);
78
- return withFileMutationQueue(absolutePath, () => new Promise((resolve, reject) => {
79
- // Abort BEFORE the write begins -> reject immediately.
80
- if (signal?.aborted) {
81
- reject(new Error("Operation aborted"));
82
- return;
83
- }
84
- let aborted = false;
85
- let writeStarted = false;
86
- const onAbort = () => {
87
- aborted = true;
88
- // W2: only reject when aborted BEFORE the write begins. An abort
89
- // that fires during/after the write must not swallow the
90
- // successful resolve once the write completes.
91
- if (!writeStarted) {
92
- reject(new Error("Operation aborted"));
93
- }
94
- };
95
- signal?.addEventListener("abort", onAbort, { once: true });
96
- (async () => {
97
- try {
98
- await ops.mkdir(dir);
99
- // Reject if aborted before the write begins.
100
- if (aborted)
101
- return;
102
- writeStarted = true;
103
- // W1 + W5: atomic write (temp + rename). On failure the
104
- // temp is cleaned up and the target is left untouched.
105
- const writeFn = ops.atomicWrite ??
106
- ((p, c) => atomicWriteFile(p, c, "utf-8", atomicOps));
107
- await writeFn(absolutePath, content);
108
- // W2: once the write has completed successfully, resolve
109
- // with the success content even if the abort signal
110
- // fired mid-write. Do NOT swallow the resolve.
111
- signal?.removeEventListener("abort", onAbort);
112
- resolve({
113
- content: [
114
- {
115
- type: "text",
116
- text: `Successfully wrote ${Buffer.byteLength(content, "utf-8")} bytes to ${path}`,
117
- },
118
- ],
119
- details: undefined,
120
- });
121
- }
122
- catch (error) {
123
- signal?.removeEventListener("abort", onAbort);
124
- if (!aborted)
125
- reject(error);
126
- }
127
- })();
128
- }));
129
- },
130
- };
131
- }
132
- export function createWriteTool(cwd, options) {
133
- return wrapToolDefinition(createWriteToolDefinition(cwd, options));
134
- }