@dexto/tools-filesystem 1.16.5 → 1.17.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/blocked-path-check.cjs +62 -0
- package/dist/blocked-path-check.d.ts +11 -0
- package/dist/blocked-path-check.d.ts.map +1 -0
- package/dist/blocked-path-check.js +27 -0
- package/dist/blocked-path-check.test.cjs +117 -0
- package/dist/blocked-path-check.test.d.ts +2 -0
- package/dist/blocked-path-check.test.d.ts.map +1 -0
- package/dist/blocked-path-check.test.js +116 -0
- package/dist/directory-approval.integration.test.cjs +12 -11
- package/dist/directory-approval.integration.test.js +12 -11
- package/dist/edit-file-tool.cjs +48 -27
- package/dist/edit-file-tool.d.ts +2 -1
- package/dist/edit-file-tool.d.ts.map +1 -1
- package/dist/edit-file-tool.js +48 -27
- package/dist/edit-file-tool.test.cjs +161 -30
- package/dist/edit-file-tool.test.js +161 -30
- package/dist/file-size.cjs +47 -0
- package/dist/file-size.d.ts +12 -0
- package/dist/file-size.d.ts.map +1 -0
- package/dist/file-size.js +22 -0
- package/dist/glob-files-tool.cjs +9 -4
- package/dist/glob-files-tool.d.ts +2 -1
- package/dist/glob-files-tool.d.ts.map +1 -1
- package/dist/glob-files-tool.js +9 -4
- package/dist/glob-files-tool.test.cjs +72 -4
- package/dist/glob-files-tool.test.js +72 -4
- package/dist/grep-content-tool.cjs +4 -1
- package/dist/grep-content-tool.d.ts +2 -1
- package/dist/grep-content-tool.d.ts.map +1 -1
- package/dist/grep-content-tool.js +4 -1
- package/dist/grep-content-tool.test.cjs +5 -4
- package/dist/grep-content-tool.test.js +5 -4
- package/dist/index.d.cts +12 -5
- package/dist/path-validator.cjs +26 -1
- package/dist/path-validator.d.ts.map +1 -1
- package/dist/path-validator.js +26 -1
- package/dist/path-validator.test.cjs +75 -0
- package/dist/path-validator.test.js +75 -0
- package/dist/read-file-tool.cjs +14 -2
- package/dist/read-file-tool.d.ts +2 -1
- package/dist/read-file-tool.d.ts.map +1 -1
- package/dist/read-file-tool.js +14 -2
- package/dist/read-file-tool.test.cjs +54 -5
- package/dist/read-file-tool.test.js +54 -5
- package/dist/test-helpers.cjs +28 -0
- package/dist/test-helpers.d.ts +4 -0
- package/dist/test-helpers.d.ts.map +1 -0
- package/dist/test-helpers.js +4 -0
- package/dist/tool-factory-config.d.ts +2 -2
- package/dist/tool-factory.cjs +9 -5
- package/dist/tool-factory.d.ts.map +1 -1
- package/dist/tool-factory.js +9 -5
- package/dist/workspace-paths.cjs +5 -1
- package/dist/workspace-paths.d.ts.map +1 -1
- package/dist/workspace-paths.js +5 -1
- package/dist/write-file-tool.cjs +8 -1
- package/dist/write-file-tool.d.ts +2 -1
- package/dist/write-file-tool.d.ts.map +1 -1
- package/dist/write-file-tool.js +8 -1
- package/dist/write-file-tool.test.cjs +18 -17
- package/dist/write-file-tool.test.js +18 -17
- package/package.json +4 -3
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
2
|
+
import { allowAllPaths } from "./test-helpers.js";
|
|
2
3
|
import * as path from "node:path";
|
|
3
4
|
import * as fs from "node:fs/promises";
|
|
4
5
|
import * as os from "node:os";
|
|
6
|
+
import { createBlockedPathCheck } from "./blocked-path-check.js";
|
|
5
7
|
import { createEditFileTool } from "./edit-file-tool.js";
|
|
8
|
+
import { FileSystemErrorCode } from "./error-codes.js";
|
|
6
9
|
import { FileSystemService } from "./filesystem-service.js";
|
|
7
10
|
import { ToolErrorCode } from "@dexto/core";
|
|
8
11
|
import { DextoRuntimeError } from "@dexto/core";
|
|
@@ -102,7 +105,7 @@ describe("edit_file tool", () => {
|
|
|
102
105
|
const getFileSystemService = vi.fn(async () => {
|
|
103
106
|
throw new Error("edit_file execute must not use FileSystemService");
|
|
104
107
|
});
|
|
105
|
-
const tool = createEditFileTool(getFileSystemService);
|
|
108
|
+
const tool = createEditFileTool(getFileSystemService, allowAllPaths, 10 * 1024 * 1024);
|
|
106
109
|
const parsedInput = tool.inputSchema.parse({
|
|
107
110
|
file_path: "workspace.txt",
|
|
108
111
|
old_string: "world",
|
|
@@ -140,7 +143,7 @@ describe("edit_file tool", () => {
|
|
|
140
143
|
}
|
|
141
144
|
}))
|
|
142
145
|
};
|
|
143
|
-
const tool = createEditFileTool(vi.fn());
|
|
146
|
+
const tool = createEditFileTool(vi.fn(), allowAllPaths, 10 * 1024 * 1024);
|
|
144
147
|
await tool.execute(
|
|
145
148
|
tool.inputSchema.parse({
|
|
146
149
|
file_path: path.join(tempDir, "workspace.txt"),
|
|
@@ -178,7 +181,7 @@ describe("edit_file tool", () => {
|
|
|
178
181
|
}
|
|
179
182
|
}))
|
|
180
183
|
};
|
|
181
|
-
const tool = createEditFileTool(vi.fn());
|
|
184
|
+
const tool = createEditFileTool(vi.fn(), allowAllPaths, 10 * 1024 * 1024);
|
|
182
185
|
await expect(
|
|
183
186
|
tool.execute(
|
|
184
187
|
tool.inputSchema.parse({
|
|
@@ -198,7 +201,11 @@ describe("edit_file tool", () => {
|
|
|
198
201
|
expect(writeFile).not.toHaveBeenCalled();
|
|
199
202
|
});
|
|
200
203
|
it("should describe edit calls with an Edit header", () => {
|
|
201
|
-
const tool = createEditFileTool(
|
|
204
|
+
const tool = createEditFileTool(
|
|
205
|
+
async () => fileSystemService,
|
|
206
|
+
allowAllPaths,
|
|
207
|
+
10 * 1024 * 1024
|
|
208
|
+
);
|
|
202
209
|
const header = tool.presentation?.describeHeader?.(
|
|
203
210
|
tool.inputSchema.parse({
|
|
204
211
|
file_path: "snake.py",
|
|
@@ -213,8 +220,12 @@ describe("edit_file tool", () => {
|
|
|
213
220
|
});
|
|
214
221
|
});
|
|
215
222
|
describe("File Modification Detection", () => {
|
|
216
|
-
it("
|
|
217
|
-
const tool = createEditFileTool(
|
|
223
|
+
it("refuses to preview a file outside the workspace", async () => {
|
|
224
|
+
const tool = createEditFileTool(
|
|
225
|
+
async () => fileSystemService,
|
|
226
|
+
allowAllPaths,
|
|
227
|
+
10 * 1024 * 1024
|
|
228
|
+
);
|
|
218
229
|
const previewFn = tool.presentation?.preview;
|
|
219
230
|
expect(previewFn).toBeDefined();
|
|
220
231
|
const rawExternalDir = await fs.mkdtemp(
|
|
@@ -224,30 +235,130 @@ describe("edit_file tool", () => {
|
|
|
224
235
|
const externalFile = path.join(externalDir, "external.txt");
|
|
225
236
|
try {
|
|
226
237
|
await fs.writeFile(externalFile, "hello world");
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
);
|
|
237
|
-
expect(preview).toBeDefined();
|
|
238
|
-
expect(preview?.type).toBe("diff");
|
|
239
|
-
if (preview?.type === "diff") {
|
|
240
|
-
expect(preview.title).toBe("Update file");
|
|
241
|
-
expect(preview.filename).toBe(externalFile);
|
|
242
|
-
} else {
|
|
243
|
-
expect.fail("Expected diff preview");
|
|
244
|
-
}
|
|
238
|
+
await expect(
|
|
239
|
+
previewFn(
|
|
240
|
+
tool.inputSchema.parse({
|
|
241
|
+
file_path: externalFile,
|
|
242
|
+
old_string: "world",
|
|
243
|
+
new_string: "universe"
|
|
244
|
+
}),
|
|
245
|
+
createToolContext(mockLogger, { toolCallId: "preview-outside-roots" })
|
|
246
|
+
)
|
|
247
|
+
).rejects.toMatchObject({ code: ToolErrorCode.VALIDATION_FAILED });
|
|
245
248
|
} finally {
|
|
246
249
|
await fs.rm(externalDir, { recursive: true, force: true });
|
|
247
250
|
}
|
|
248
251
|
});
|
|
252
|
+
it("refuses to preview a blocked file", async () => {
|
|
253
|
+
const blockedCheck = createBlockedPathCheck({
|
|
254
|
+
allowedPaths: ["."],
|
|
255
|
+
blockedPaths: [".env"],
|
|
256
|
+
blockedExtensions: [],
|
|
257
|
+
maxFileSize: 1024,
|
|
258
|
+
enableBackups: false,
|
|
259
|
+
backupRetentionDays: 7
|
|
260
|
+
});
|
|
261
|
+
await fs.mkdir(path.join(tempDir, "sub"));
|
|
262
|
+
await fs.writeFile(path.join(tempDir, "sub", ".env"), "SECRET=1");
|
|
263
|
+
const tool = createEditFileTool(vi.fn(), blockedCheck, 1024);
|
|
264
|
+
await expect(
|
|
265
|
+
tool.presentation.preview(
|
|
266
|
+
tool.inputSchema.parse({
|
|
267
|
+
file_path: "sub/.env",
|
|
268
|
+
old_string: "SECRET=1",
|
|
269
|
+
new_string: "SECRET=2"
|
|
270
|
+
}),
|
|
271
|
+
createToolContext(mockLogger)
|
|
272
|
+
)
|
|
273
|
+
).rejects.toMatchObject({ code: ToolErrorCode.VALIDATION_FAILED });
|
|
274
|
+
});
|
|
275
|
+
it("refuses to preview or edit a file larger than maxFileSize", async () => {
|
|
276
|
+
await fs.writeFile(path.join(tempDir, "big.txt"), "x".repeat(11));
|
|
277
|
+
const tool = createEditFileTool(vi.fn(), allowAllPaths, 10);
|
|
278
|
+
const input = tool.inputSchema.parse({
|
|
279
|
+
file_path: "big.txt",
|
|
280
|
+
old_string: "x",
|
|
281
|
+
new_string: "y",
|
|
282
|
+
replace_all: true
|
|
283
|
+
});
|
|
284
|
+
await expect(
|
|
285
|
+
tool.presentation.preview(input, createToolContext(mockLogger))
|
|
286
|
+
).rejects.toMatchObject({ code: ToolErrorCode.VALIDATION_FAILED });
|
|
287
|
+
await expect(tool.execute(input, createToolContext(mockLogger))).rejects.toMatchObject({
|
|
288
|
+
code: FileSystemErrorCode.FILE_TOO_LARGE
|
|
289
|
+
});
|
|
290
|
+
await expect(fs.readFile(path.join(tempDir, "big.txt"), "utf-8")).resolves.toBe(
|
|
291
|
+
"x".repeat(11)
|
|
292
|
+
);
|
|
293
|
+
});
|
|
294
|
+
it("checks the size before reading when previewing and editing", async () => {
|
|
295
|
+
const readText = vi.fn(async () => "x".repeat(11));
|
|
296
|
+
const readFileSize = vi.fn(async () => 11);
|
|
297
|
+
const writeFile = vi.fn(async () => void 0);
|
|
298
|
+
const services = {
|
|
299
|
+
...createWorkspaceServices(tempDir),
|
|
300
|
+
workspaceManager: {
|
|
301
|
+
open: async () => ({
|
|
302
|
+
context: { id: "w", path: tempDir, createdAt: 1, lastActiveAt: 1 },
|
|
303
|
+
capabilities: ["files"],
|
|
304
|
+
files: {
|
|
305
|
+
readFile: readText,
|
|
306
|
+
readText,
|
|
307
|
+
readFileSize,
|
|
308
|
+
glob: vi.fn(async () => []),
|
|
309
|
+
writeFile,
|
|
310
|
+
listFiles: vi.fn(async () => [])
|
|
311
|
+
}
|
|
312
|
+
})
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
const tool = createEditFileTool(vi.fn(), allowAllPaths, 10);
|
|
316
|
+
const input = tool.inputSchema.parse({
|
|
317
|
+
file_path: "big.txt",
|
|
318
|
+
old_string: "x",
|
|
319
|
+
new_string: "y",
|
|
320
|
+
replace_all: true
|
|
321
|
+
});
|
|
322
|
+
await expect(
|
|
323
|
+
tool.presentation.preview(input, createToolContext(mockLogger, { services }))
|
|
324
|
+
).rejects.toMatchObject({ code: ToolErrorCode.VALIDATION_FAILED });
|
|
325
|
+
await expect(
|
|
326
|
+
tool.execute(input, createToolContext(mockLogger, { services }))
|
|
327
|
+
).rejects.toMatchObject({ code: FileSystemErrorCode.FILE_TOO_LARGE });
|
|
328
|
+
expect(readFileSize).toHaveBeenCalledTimes(2);
|
|
329
|
+
expect(readText).not.toHaveBeenCalled();
|
|
330
|
+
expect(writeFile).not.toHaveBeenCalled();
|
|
331
|
+
});
|
|
332
|
+
it.each([
|
|
333
|
+
["$&", "cost $& now"],
|
|
334
|
+
["$1", "cost $1 now"],
|
|
335
|
+
["$$", "cost $$ now"],
|
|
336
|
+
["$'", "cost $' now"],
|
|
337
|
+
["$`", "cost $` now"]
|
|
338
|
+
])("writes %s from new_string as literal text", async (token, expected) => {
|
|
339
|
+
await fs.writeFile(path.join(tempDir, "price.txt"), "cost PRICE now");
|
|
340
|
+
const tool = createEditFileTool(vi.fn(), allowAllPaths, 10 * 1024 * 1024);
|
|
341
|
+
const input = tool.inputSchema.parse({
|
|
342
|
+
file_path: "price.txt",
|
|
343
|
+
old_string: "PRICE",
|
|
344
|
+
new_string: token
|
|
345
|
+
});
|
|
346
|
+
const preview = await tool.presentation.preview(input, createToolContext(mockLogger));
|
|
347
|
+
expect(preview).toMatchObject({ type: "diff" });
|
|
348
|
+
if (preview?.type === "diff") {
|
|
349
|
+
expect(preview.unified).toContain(`+${expected}`);
|
|
350
|
+
}
|
|
351
|
+
await tool.execute(input, createToolContext(mockLogger));
|
|
352
|
+
await expect(fs.readFile(path.join(tempDir, "price.txt"), "utf-8")).resolves.toBe(
|
|
353
|
+
expected
|
|
354
|
+
);
|
|
355
|
+
});
|
|
249
356
|
it("should succeed when file is not modified between preview and execute", async () => {
|
|
250
|
-
const tool = createEditFileTool(
|
|
357
|
+
const tool = createEditFileTool(
|
|
358
|
+
async () => fileSystemService,
|
|
359
|
+
allowAllPaths,
|
|
360
|
+
10 * 1024 * 1024
|
|
361
|
+
);
|
|
251
362
|
const previewFn = tool.presentation?.preview;
|
|
252
363
|
expect(previewFn).toBeDefined();
|
|
253
364
|
const testFile = path.join(tempDir, "test.txt");
|
|
@@ -274,7 +385,11 @@ describe("edit_file tool", () => {
|
|
|
274
385
|
expect(content).toBe("hello universe");
|
|
275
386
|
});
|
|
276
387
|
it("should fail when file is modified between preview and execute", async () => {
|
|
277
|
-
const tool = createEditFileTool(
|
|
388
|
+
const tool = createEditFileTool(
|
|
389
|
+
async () => fileSystemService,
|
|
390
|
+
allowAllPaths,
|
|
391
|
+
10 * 1024 * 1024
|
|
392
|
+
);
|
|
278
393
|
const previewFn = tool.presentation?.preview;
|
|
279
394
|
expect(previewFn).toBeDefined();
|
|
280
395
|
const testFile = path.join(tempDir, "test.txt");
|
|
@@ -305,7 +420,11 @@ describe("edit_file tool", () => {
|
|
|
305
420
|
expect(content).toBe("hello world - user added this");
|
|
306
421
|
});
|
|
307
422
|
it("should detect file modification with correct error code", async () => {
|
|
308
|
-
const tool = createEditFileTool(
|
|
423
|
+
const tool = createEditFileTool(
|
|
424
|
+
async () => fileSystemService,
|
|
425
|
+
allowAllPaths,
|
|
426
|
+
10 * 1024 * 1024
|
|
427
|
+
);
|
|
309
428
|
const previewFn = tool.presentation?.preview;
|
|
310
429
|
expect(previewFn).toBeDefined();
|
|
311
430
|
const testFile = path.join(tempDir, "test.txt");
|
|
@@ -334,7 +453,11 @@ describe("edit_file tool", () => {
|
|
|
334
453
|
}
|
|
335
454
|
});
|
|
336
455
|
it("should work without toolCallId (no modification check)", async () => {
|
|
337
|
-
const tool = createEditFileTool(
|
|
456
|
+
const tool = createEditFileTool(
|
|
457
|
+
async () => fileSystemService,
|
|
458
|
+
allowAllPaths,
|
|
459
|
+
10 * 1024 * 1024
|
|
460
|
+
);
|
|
338
461
|
const previewFn = tool.presentation?.preview;
|
|
339
462
|
expect(previewFn).toBeDefined();
|
|
340
463
|
const testFile = path.join(tempDir, "test.txt");
|
|
@@ -357,7 +480,11 @@ describe("edit_file tool", () => {
|
|
|
357
480
|
}
|
|
358
481
|
});
|
|
359
482
|
it("should clean up hash cache after successful execution", async () => {
|
|
360
|
-
const tool = createEditFileTool(
|
|
483
|
+
const tool = createEditFileTool(
|
|
484
|
+
async () => fileSystemService,
|
|
485
|
+
allowAllPaths,
|
|
486
|
+
10 * 1024 * 1024
|
|
487
|
+
);
|
|
361
488
|
const previewFn = tool.presentation?.preview;
|
|
362
489
|
expect(previewFn).toBeDefined();
|
|
363
490
|
const testFile = path.join(tempDir, "test.txt");
|
|
@@ -387,7 +514,11 @@ describe("edit_file tool", () => {
|
|
|
387
514
|
expect(content).toBe("hello galaxy");
|
|
388
515
|
});
|
|
389
516
|
it("should clean up hash cache after failed execution", async () => {
|
|
390
|
-
const tool = createEditFileTool(
|
|
517
|
+
const tool = createEditFileTool(
|
|
518
|
+
async () => fileSystemService,
|
|
519
|
+
allowAllPaths,
|
|
520
|
+
10 * 1024 * 1024
|
|
521
|
+
);
|
|
391
522
|
const previewFn = tool.presentation?.preview;
|
|
392
523
|
expect(previewFn).toBeDefined();
|
|
393
524
|
const testFile = path.join(tempDir, "test.txt");
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var file_size_exports = {};
|
|
20
|
+
__export(file_size_exports, {
|
|
21
|
+
assertWithinMaxFileSize: () => assertWithinMaxFileSize,
|
|
22
|
+
readTextWithinMaxFileSize: () => readTextWithinMaxFileSize
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(file_size_exports);
|
|
25
|
+
var import_errors = require("./errors.js");
|
|
26
|
+
function assertWithinMaxFileSize(workspacePath, content, maxFileSize) {
|
|
27
|
+
const size = Buffer.byteLength(content, "utf-8");
|
|
28
|
+
if (size > maxFileSize) {
|
|
29
|
+
throw import_errors.FileSystemError.fileTooLarge(workspacePath, size, maxFileSize);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function readTextWithinMaxFileSize(files, workspacePath, maxFileSize) {
|
|
33
|
+
if (files.readFileSize !== void 0) {
|
|
34
|
+
const size = await files.readFileSize(workspacePath);
|
|
35
|
+
if (size > maxFileSize) {
|
|
36
|
+
throw import_errors.FileSystemError.fileTooLarge(workspacePath, size, maxFileSize);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const content = await files.readText(workspacePath);
|
|
40
|
+
assertWithinMaxFileSize(workspacePath, content, maxFileSize);
|
|
41
|
+
return content;
|
|
42
|
+
}
|
|
43
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
44
|
+
0 && (module.exports = {
|
|
45
|
+
assertWithinMaxFileSize,
|
|
46
|
+
readTextWithinMaxFileSize
|
|
47
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { WorkspaceFiles } from '@dexto/core/workspace';
|
|
2
|
+
/**
|
|
3
|
+
* Refuses text that is larger than the configured `maxFileSize`.
|
|
4
|
+
*/
|
|
5
|
+
export declare function assertWithinMaxFileSize(workspacePath: string, content: string, maxFileSize: number): void;
|
|
6
|
+
/**
|
|
7
|
+
* Reads a workspace text file, refusing one larger than `maxFileSize`. When the handle can report
|
|
8
|
+
* a size, that is checked first so an oversized file is never read. The size of the text is
|
|
9
|
+
* checked as well, for handles that cannot report one and in case the file changed in between.
|
|
10
|
+
*/
|
|
11
|
+
export declare function readTextWithinMaxFileSize(files: WorkspaceFiles, workspacePath: string, maxFileSize: number): Promise<string>;
|
|
12
|
+
//# sourceMappingURL=file-size.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-size.d.ts","sourceRoot":"","sources":["../src/file-size.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAG5D;;GAEG;AACH,wBAAgB,uBAAuB,CACnC,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM,EACf,WAAW,EAAE,MAAM,GACpB,IAAI,CAKN;AAED;;;;GAIG;AACH,wBAAsB,yBAAyB,CAC3C,KAAK,EAAE,cAAc,EACrB,aAAa,EAAE,MAAM,EACrB,WAAW,EAAE,MAAM,GACpB,OAAO,CAAC,MAAM,CAAC,CAUjB"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { FileSystemError } from "./errors.js";
|
|
2
|
+
function assertWithinMaxFileSize(workspacePath, content, maxFileSize) {
|
|
3
|
+
const size = Buffer.byteLength(content, "utf-8");
|
|
4
|
+
if (size > maxFileSize) {
|
|
5
|
+
throw FileSystemError.fileTooLarge(workspacePath, size, maxFileSize);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
async function readTextWithinMaxFileSize(files, workspacePath, maxFileSize) {
|
|
9
|
+
if (files.readFileSize !== void 0) {
|
|
10
|
+
const size = await files.readFileSize(workspacePath);
|
|
11
|
+
if (size > maxFileSize) {
|
|
12
|
+
throw FileSystemError.fileTooLarge(workspacePath, size, maxFileSize);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const content = await files.readText(workspacePath);
|
|
16
|
+
assertWithinMaxFileSize(workspacePath, content, maxFileSize);
|
|
17
|
+
return content;
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
assertWithinMaxFileSize,
|
|
21
|
+
readTextWithinMaxFileSize
|
|
22
|
+
};
|
package/dist/glob-files-tool.cjs
CHANGED
|
@@ -31,7 +31,7 @@ const GlobFilesInputSchema = import_zod.z.object({
|
|
|
31
31
|
path: import_zod.z.string().optional().describe("Base directory to search from (defaults to working directory)"),
|
|
32
32
|
max_results: import_zod.z.number().int().positive().optional().default(1e3).describe("Maximum number of results to return (default: 1000)")
|
|
33
33
|
}).strict();
|
|
34
|
-
function createGlobFilesTool(getFileSystemService) {
|
|
34
|
+
function createGlobFilesTool(getFileSystemService, blockedPathCheck) {
|
|
35
35
|
return (0, import_tools.defineTool)({
|
|
36
36
|
id: "glob_files",
|
|
37
37
|
aliases: ["glob"],
|
|
@@ -67,9 +67,14 @@ function createGlobFilesTool(getFileSystemService) {
|
|
|
67
67
|
}
|
|
68
68
|
const handle = await context.services.workspaceManager.open({ intent: "read" });
|
|
69
69
|
const workspacePattern = joinWorkspacePattern(handle.context.path, searchPath, pattern);
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
|
|
70
|
+
const allowedMatches = [];
|
|
71
|
+
for (const match of await handle.files.glob(workspacePattern)) {
|
|
72
|
+
if (allowedMatches.length > max_results) break;
|
|
73
|
+
const blocked = await blockedPathCheck(handle.context.path, match, context.logger);
|
|
74
|
+
if (blocked === null) allowedMatches.push(match);
|
|
75
|
+
}
|
|
76
|
+
const limitedMatches = allowedMatches.slice(0, max_results);
|
|
77
|
+
const truncated = allowedMatches.length > max_results;
|
|
73
78
|
const _display = {
|
|
74
79
|
type: "search",
|
|
75
80
|
pattern,
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { z } from 'zod';
|
|
7
7
|
import type { Tool } from '@dexto/core/tools';
|
|
8
|
+
import type { BlockedPathCheck } from './blocked-path-check.js';
|
|
8
9
|
import type { FileSystemServiceGetter } from './file-tool-types.js';
|
|
9
10
|
declare const GlobFilesInputSchema: z.ZodObject<{
|
|
10
11
|
pattern: z.ZodString;
|
|
@@ -14,6 +15,6 @@ declare const GlobFilesInputSchema: z.ZodObject<{
|
|
|
14
15
|
/**
|
|
15
16
|
* Create the glob_files internal tool with directory approval support
|
|
16
17
|
*/
|
|
17
|
-
export declare function createGlobFilesTool(getFileSystemService: FileSystemServiceGetter): Tool<typeof GlobFilesInputSchema>;
|
|
18
|
+
export declare function createGlobFilesTool(getFileSystemService: FileSystemServiceGetter, blockedPathCheck: BlockedPathCheck): Tool<typeof GlobFilesInputSchema>;
|
|
18
19
|
export {};
|
|
19
20
|
//# sourceMappingURL=glob-files-tool.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"glob-files-tool.d.ts","sourceRoot":"","sources":["../src/glob-files-tool.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAOxB,OAAO,KAAK,EAAqB,IAAI,EAAwB,MAAM,mBAAmB,CAAC;AACvF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAKpE,QAAA,MAAM,oBAAoB;;;;kBAiBb,CAAC;AAEd;;GAEG;AACH,wBAAgB,mBAAmB,CAC/B,oBAAoB,EAAE,uBAAuB,
|
|
1
|
+
{"version":3,"file":"glob-files-tool.d.ts","sourceRoot":"","sources":["../src/glob-files-tool.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAOxB,OAAO,KAAK,EAAqB,IAAI,EAAwB,MAAM,mBAAmB,CAAC;AACvF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAChE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAKpE,QAAA,MAAM,oBAAoB;;;;kBAiBb,CAAC;AAEd;;GAEG;AACH,wBAAgB,mBAAmB,CAC/B,oBAAoB,EAAE,uBAAuB,EAC7C,gBAAgB,EAAE,gBAAgB,GACnC,IAAI,CAAC,OAAO,oBAAoB,CAAC,CAyEnC"}
|
package/dist/glob-files-tool.js
CHANGED
|
@@ -13,7 +13,7 @@ const GlobFilesInputSchema = z.object({
|
|
|
13
13
|
path: z.string().optional().describe("Base directory to search from (defaults to working directory)"),
|
|
14
14
|
max_results: z.number().int().positive().optional().default(1e3).describe("Maximum number of results to return (default: 1000)")
|
|
15
15
|
}).strict();
|
|
16
|
-
function createGlobFilesTool(getFileSystemService) {
|
|
16
|
+
function createGlobFilesTool(getFileSystemService, blockedPathCheck) {
|
|
17
17
|
return defineTool({
|
|
18
18
|
id: "glob_files",
|
|
19
19
|
aliases: ["glob"],
|
|
@@ -49,9 +49,14 @@ function createGlobFilesTool(getFileSystemService) {
|
|
|
49
49
|
}
|
|
50
50
|
const handle = await context.services.workspaceManager.open({ intent: "read" });
|
|
51
51
|
const workspacePattern = joinWorkspacePattern(handle.context.path, searchPath, pattern);
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
|
|
52
|
+
const allowedMatches = [];
|
|
53
|
+
for (const match of await handle.files.glob(workspacePattern)) {
|
|
54
|
+
if (allowedMatches.length > max_results) break;
|
|
55
|
+
const blocked = await blockedPathCheck(handle.context.path, match, context.logger);
|
|
56
|
+
if (blocked === null) allowedMatches.push(match);
|
|
57
|
+
}
|
|
58
|
+
const limitedMatches = allowedMatches.slice(0, max_results);
|
|
59
|
+
const truncated = allowedMatches.length > max_results;
|
|
55
60
|
const _display = {
|
|
56
61
|
type: "search",
|
|
57
62
|
pattern,
|
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var import_vitest = require("vitest");
|
|
3
3
|
var import_core = require("@dexto/core");
|
|
4
|
+
var import_blocked_path_check = require("./blocked-path-check.js");
|
|
4
5
|
var import_glob_files_tool = require("./glob-files-tool.js");
|
|
6
|
+
var import_test_helpers = require("./test-helpers.js");
|
|
7
|
+
const blockedPathCheck = (0, import_blocked_path_check.createBlockedPathCheck)({
|
|
8
|
+
allowedPaths: ["."],
|
|
9
|
+
blockedPaths: [".git", ".env"],
|
|
10
|
+
blockedExtensions: [],
|
|
11
|
+
maxFileSize: 1024,
|
|
12
|
+
enableBackups: false,
|
|
13
|
+
backupRetentionDays: 7
|
|
14
|
+
});
|
|
5
15
|
const createMockLogger = () => {
|
|
6
16
|
const logger = {
|
|
7
17
|
debug: import_vitest.vi.fn(),
|
|
@@ -59,7 +69,7 @@ function createToolContext(logger, glob) {
|
|
|
59
69
|
throw new Error("glob_files execute must not use FileSystemService");
|
|
60
70
|
});
|
|
61
71
|
const glob = import_vitest.vi.fn(async () => ["src/a.ts", "src/b.ts", "src/c.ts"]);
|
|
62
|
-
const tool = (0, import_glob_files_tool.createGlobFilesTool)(getFileSystemService);
|
|
72
|
+
const tool = (0, import_glob_files_tool.createGlobFilesTool)(getFileSystemService, import_test_helpers.allowAllPaths);
|
|
63
73
|
const parsedInput = tool.inputSchema.parse({
|
|
64
74
|
pattern: "**/*.ts",
|
|
65
75
|
path: "src",
|
|
@@ -77,7 +87,7 @@ function createToolContext(logger, glob) {
|
|
|
77
87
|
(0, import_vitest.it)("normalizes workspace-contained absolute search paths before globbing", async () => {
|
|
78
88
|
const mockLogger = createMockLogger();
|
|
79
89
|
const glob = import_vitest.vi.fn(async () => ["src/a.ts"]);
|
|
80
|
-
const tool = (0, import_glob_files_tool.createGlobFilesTool)(import_vitest.vi.fn());
|
|
90
|
+
const tool = (0, import_glob_files_tool.createGlobFilesTool)(import_vitest.vi.fn(), import_test_helpers.allowAllPaths);
|
|
81
91
|
await tool.execute(
|
|
82
92
|
tool.inputSchema.parse({
|
|
83
93
|
pattern: "**/*.ts",
|
|
@@ -90,7 +100,7 @@ function createToolContext(logger, glob) {
|
|
|
90
100
|
(0, import_vitest.it)("rejects external absolute search paths before file provider calls", async () => {
|
|
91
101
|
const mockLogger = createMockLogger();
|
|
92
102
|
const glob = import_vitest.vi.fn(async () => ["src/a.ts"]);
|
|
93
|
-
const tool = (0, import_glob_files_tool.createGlobFilesTool)(import_vitest.vi.fn());
|
|
103
|
+
const tool = (0, import_glob_files_tool.createGlobFilesTool)(import_vitest.vi.fn(), import_test_helpers.allowAllPaths);
|
|
94
104
|
await (0, import_vitest.expect)(
|
|
95
105
|
tool.execute(
|
|
96
106
|
tool.inputSchema.parse({
|
|
@@ -105,7 +115,7 @@ function createToolContext(logger, glob) {
|
|
|
105
115
|
(0, import_vitest.it)("rejects absolute and escaping glob patterns before file provider calls", async () => {
|
|
106
116
|
const mockLogger = createMockLogger();
|
|
107
117
|
const glob = import_vitest.vi.fn(async () => ["src/a.ts"]);
|
|
108
|
-
const tool = (0, import_glob_files_tool.createGlobFilesTool)(import_vitest.vi.fn());
|
|
118
|
+
const tool = (0, import_glob_files_tool.createGlobFilesTool)(import_vitest.vi.fn(), import_test_helpers.allowAllPaths);
|
|
109
119
|
await (0, import_vitest.expect)(
|
|
110
120
|
tool.execute(
|
|
111
121
|
tool.inputSchema.parse({
|
|
@@ -124,4 +134,62 @@ function createToolContext(logger, glob) {
|
|
|
124
134
|
).rejects.toMatchObject({ code: import_core.ToolErrorCode.VALIDATION_FAILED });
|
|
125
135
|
(0, import_vitest.expect)(glob).not.toHaveBeenCalled();
|
|
126
136
|
});
|
|
137
|
+
import_vitest.it.each([
|
|
138
|
+
"{..,src}/**/*.ts",
|
|
139
|
+
"{.,.}{.,.}/secret.txt",
|
|
140
|
+
"{/outside,src}/**/*.ts",
|
|
141
|
+
"src/{a,../b}"
|
|
142
|
+
])("rejects brace pattern %s that expands to an escaping glob", async (pattern) => {
|
|
143
|
+
const glob = import_vitest.vi.fn(async () => ["src/a.ts"]);
|
|
144
|
+
const tool = (0, import_glob_files_tool.createGlobFilesTool)(import_vitest.vi.fn(), import_test_helpers.allowAllPaths);
|
|
145
|
+
await (0, import_vitest.expect)(
|
|
146
|
+
tool.execute(
|
|
147
|
+
tool.inputSchema.parse({ pattern }),
|
|
148
|
+
createToolContext(createMockLogger(), glob)
|
|
149
|
+
)
|
|
150
|
+
).rejects.toMatchObject({ code: import_core.ToolErrorCode.VALIDATION_FAILED });
|
|
151
|
+
(0, import_vitest.expect)(glob).not.toHaveBeenCalled();
|
|
152
|
+
});
|
|
153
|
+
(0, import_vitest.it)("still accepts brace patterns that stay inside the workspace", async () => {
|
|
154
|
+
const glob = import_vitest.vi.fn(async () => ["src/a.ts", "src/b.tsx"]);
|
|
155
|
+
const tool = (0, import_glob_files_tool.createGlobFilesTool)(import_vitest.vi.fn(), import_test_helpers.allowAllPaths);
|
|
156
|
+
await tool.execute(
|
|
157
|
+
tool.inputSchema.parse({ pattern: "src/**/*.{ts,tsx}" }),
|
|
158
|
+
createToolContext(createMockLogger(), glob)
|
|
159
|
+
);
|
|
160
|
+
(0, import_vitest.expect)(glob).toHaveBeenCalledWith("src/**/*.{ts,tsx}");
|
|
161
|
+
});
|
|
162
|
+
(0, import_vitest.it)("leaves blocked files out of the results", async () => {
|
|
163
|
+
const glob = import_vitest.vi.fn(async () => [".env", "sub/.env", "sub/.git/config", "src/a.ts"]);
|
|
164
|
+
const tool = (0, import_glob_files_tool.createGlobFilesTool)(import_vitest.vi.fn(), blockedPathCheck);
|
|
165
|
+
const result = await tool.execute(
|
|
166
|
+
tool.inputSchema.parse({ pattern: "**/*" }),
|
|
167
|
+
createToolContext(createMockLogger(), glob)
|
|
168
|
+
);
|
|
169
|
+
(0, import_vitest.expect)(result.files).toEqual([{ path: "src/a.ts" }]);
|
|
170
|
+
(0, import_vitest.expect)(result.total_found).toBe(1);
|
|
171
|
+
});
|
|
172
|
+
(0, import_vitest.it)("checks only the entries it returns, stopping at the result cap", async () => {
|
|
173
|
+
const files = [".env", ...Array.from({ length: 50 }, (_, index) => `src/f${index}.ts`)];
|
|
174
|
+
const glob = import_vitest.vi.fn(async () => files);
|
|
175
|
+
const check = import_vitest.vi.fn(blockedPathCheck);
|
|
176
|
+
const tool = (0, import_glob_files_tool.createGlobFilesTool)(import_vitest.vi.fn(), check);
|
|
177
|
+
const result = await tool.execute(
|
|
178
|
+
tool.inputSchema.parse({ pattern: "**/*", max_results: 2 }),
|
|
179
|
+
createToolContext(createMockLogger(), glob)
|
|
180
|
+
);
|
|
181
|
+
(0, import_vitest.expect)(result.files).toEqual([{ path: "src/f0.ts" }, { path: "src/f1.ts" }]);
|
|
182
|
+
(0, import_vitest.expect)(result.truncated).toBe(true);
|
|
183
|
+
(0, import_vitest.expect)(check).toHaveBeenCalledTimes(4);
|
|
184
|
+
});
|
|
185
|
+
(0, import_vitest.it)("does not report truncation when exactly max_results files are left", async () => {
|
|
186
|
+
const glob = import_vitest.vi.fn(async () => [".env", "src/a.ts", "src/b.ts"]);
|
|
187
|
+
const tool = (0, import_glob_files_tool.createGlobFilesTool)(import_vitest.vi.fn(), blockedPathCheck);
|
|
188
|
+
const result = await tool.execute(
|
|
189
|
+
tool.inputSchema.parse({ pattern: "**/*", max_results: 2 }),
|
|
190
|
+
createToolContext(createMockLogger(), glob)
|
|
191
|
+
);
|
|
192
|
+
(0, import_vitest.expect)(result.files).toHaveLength(2);
|
|
193
|
+
(0, import_vitest.expect)(result.truncated).toBe(false);
|
|
194
|
+
});
|
|
127
195
|
});
|