@d0nj/dsh-native-tools 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +109 -2
- package/cordis.patch.yml +33 -0
- package/dist/format.d.ts +36 -0
- package/dist/format.js +66 -0
- package/dist/grammars.d.ts +33 -0
- package/dist/grammars.js +124 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.js +156 -0
- package/dist/native.d.ts +165 -0
- package/dist/native.js +60 -0
- package/dist/tools/ast.d.ts +51 -0
- package/dist/tools/ast.js +283 -0
- package/dist/tools/edit.d.ts +36 -0
- package/dist/tools/edit.js +127 -0
- package/dist/tools/glob.d.ts +15 -0
- package/dist/tools/glob.js +69 -0
- package/dist/tools/grep.d.ts +20 -0
- package/dist/tools/grep.js +105 -0
- package/dist/tools/read.d.ts +88 -0
- package/dist/tools/read.js +247 -0
- package/dist/tools/write.d.ts +25 -0
- package/dist/tools/write.js +86 -0
- package/grammars.json +357 -0
- package/icon.svg +5 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/native/dsh-natives.darwin-arm64.5d4543fa2c77.node +0 -0
- package/native/dsh-natives.linux-x64.5035be1b33a4.node +0 -0
- package/native/dsh-natives.win32-x64.fa28ab97c937.node +0 -0
- package/package.json +76 -4
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { defineTool, ToolArgsError } from '@deepseek-ai/dsh-tools';
|
|
3
|
+
import { capMeta, groupMatches } from '../format.js';
|
|
4
|
+
import { cancellable, loadNative } from '../native.js';
|
|
5
|
+
export const AST_MATCH_LIMIT = 50;
|
|
6
|
+
export const AST_MAX_FILES = 200;
|
|
7
|
+
export const AST_MAX_REPLACEMENTS = 1000;
|
|
8
|
+
export const AST_META_MAX_BYTES = 65536;
|
|
9
|
+
const AST_MATCHES_SPILL = 'The complete result could not be saved; narrow pat, path, or include to see more.';
|
|
10
|
+
export function astMatchesToLines(matches) {
|
|
11
|
+
const rows = matches.map((match) => ({
|
|
12
|
+
path: match.path,
|
|
13
|
+
lineNumber: match.startLine,
|
|
14
|
+
line: match.meta.length === 0 ? match.text : `${match.text} (${match.meta.map((entry) => `${entry.name}=${entry.text}`).join(', ')})`,
|
|
15
|
+
}));
|
|
16
|
+
return groupMatches(rows);
|
|
17
|
+
}
|
|
18
|
+
export function renderAstMatches(value) {
|
|
19
|
+
if (value.total === 0) {
|
|
20
|
+
const hint = value.unsupported.length === 0 ? '' : ` (${value.unsupported.length} path(s) with no supported grammar)`;
|
|
21
|
+
return `No matches found${hint}`;
|
|
22
|
+
}
|
|
23
|
+
const files = groupMatches(value.matches.map((match) => ({ path: match.path, lineNumber: match.lineNumber, line: match.line })));
|
|
24
|
+
const header = value.matches.length < value.total ? `Found ${value.matches.length} of ${value.total} matches` : `Found ${value.total} ${value.total === 1 ? 'match' : 'matches'}`;
|
|
25
|
+
const body = files
|
|
26
|
+
.map((file) => `${file.path}\n${file.matches.map((match) => `Line ${match.lineNumber}: ${match.line}`).join('\n')}`)
|
|
27
|
+
.join('\n\n');
|
|
28
|
+
const recovery = value.matches.length < value.total ? `\n\n(${AST_MATCHES_SPILL})` : '';
|
|
29
|
+
const warnings = value.warnings.length === 0 ? '' : `\n\nWarnings:\n${value.warnings.map((warning) => `- ${warning}`).join('\n')}`;
|
|
30
|
+
return `${header}\n\n${body}${recovery}${warnings}`;
|
|
31
|
+
}
|
|
32
|
+
export function previewToken(entries) {
|
|
33
|
+
const hash = createHash('sha256');
|
|
34
|
+
for (const entry of [...entries].sort((left, right) => left.path.localeCompare(right.path))) {
|
|
35
|
+
hash.update(entry.path);
|
|
36
|
+
hash.update('\u0000');
|
|
37
|
+
hash.update(createHash('sha256').update(entry.before).digest('hex'));
|
|
38
|
+
}
|
|
39
|
+
return hash.digest('hex').slice(0, 16);
|
|
40
|
+
}
|
|
41
|
+
export function renderAstEdit(value) {
|
|
42
|
+
const files = value.files.map((file) => `- ${file.path} (${file.replacements})`).join('\n');
|
|
43
|
+
if (!value.applied) {
|
|
44
|
+
return `Staged ${value.total} replacement(s) in ${value.files.length} file(s). Nothing was written.\n${files}\n\nApply with dry_run: false and preview: "${value.preview}".`;
|
|
45
|
+
}
|
|
46
|
+
return `Applied ${value.total} replacement(s) in ${value.files.length} file(s).\n${files}`;
|
|
47
|
+
}
|
|
48
|
+
export function astEditMeta(value) {
|
|
49
|
+
return capMeta({
|
|
50
|
+
shape: 'paths',
|
|
51
|
+
paths: value.files.map((file) => file.path),
|
|
52
|
+
truncated: false,
|
|
53
|
+
total: value.files.length,
|
|
54
|
+
}, AST_META_MAX_BYTES);
|
|
55
|
+
}
|
|
56
|
+
export function createAstGrepTool(deps) {
|
|
57
|
+
return defineTool({
|
|
58
|
+
name: 'ast_grep',
|
|
59
|
+
description: 'Search code by syntax tree instead of text. Patterns use ast-grep syntax: $A captures one node, $$$ARGS captures many, $_ matches without capturing. Faster and more precise than grep for structural questions ("every call to X with two arguments").',
|
|
60
|
+
deferLoading: true,
|
|
61
|
+
parameters: {
|
|
62
|
+
pat: {
|
|
63
|
+
type: 'string',
|
|
64
|
+
required: true,
|
|
65
|
+
description: 'ast-grep pattern, e.g. "console.log($A)" or "fn $NAME($$$ARGS)".',
|
|
66
|
+
},
|
|
67
|
+
path: { type: 'string', description: 'File or directory. Defaults to the session workspace.' },
|
|
68
|
+
lang: { type: 'string', description: 'Language id, overriding extension inference (typescript, tsx, javascript, rust, python, bash, go, json, yaml, markdown).' },
|
|
69
|
+
include: { type: 'string', description: 'One glob filter for candidate files, e.g. "*.ts".' },
|
|
70
|
+
skip: { type: 'number', description: 'Matches to skip before returning the page. Defaults to 0.' },
|
|
71
|
+
},
|
|
72
|
+
output: {
|
|
73
|
+
schema: {
|
|
74
|
+
type: 'object',
|
|
75
|
+
additionalProperties: false,
|
|
76
|
+
properties: {
|
|
77
|
+
matches: {
|
|
78
|
+
type: 'array',
|
|
79
|
+
required: true,
|
|
80
|
+
items: {
|
|
81
|
+
type: 'object',
|
|
82
|
+
additionalProperties: false,
|
|
83
|
+
properties: {
|
|
84
|
+
path: { type: 'string', required: true },
|
|
85
|
+
lineNumber: { type: 'number', required: true },
|
|
86
|
+
line: { type: 'string', required: true },
|
|
87
|
+
meta: { type: 'array', required: true, items: { type: 'string' } },
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
total: { type: 'number', required: true },
|
|
92
|
+
filesSearched: { type: 'number', required: true },
|
|
93
|
+
unsupported: { type: 'array', required: true, items: { type: 'string' } },
|
|
94
|
+
warnings: { type: 'array', required: true, items: { type: 'string' } },
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
render: (_args, value) => [{ type: 'text', text: renderAstMatches(value) }],
|
|
98
|
+
presentationMeta: (_args, value) => {
|
|
99
|
+
const files = astMatchesToLines(value.matches.map((match) => ({
|
|
100
|
+
path: match.path,
|
|
101
|
+
text: match.line,
|
|
102
|
+
startLine: match.lineNumber,
|
|
103
|
+
startColumn: 0,
|
|
104
|
+
endLine: match.lineNumber,
|
|
105
|
+
endColumn: 0,
|
|
106
|
+
spanLines: 1,
|
|
107
|
+
byteStart: 0,
|
|
108
|
+
byteEnd: 0,
|
|
109
|
+
meta: [],
|
|
110
|
+
})));
|
|
111
|
+
return capMeta({ shape: 'matches', files, truncated: value.matches.length < value.total, total: value.total }, AST_META_MAX_BYTES);
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
timeoutMs: deps.timeoutMs,
|
|
115
|
+
isConcurrencySafe: () => true,
|
|
116
|
+
presentCall: (args) => ({
|
|
117
|
+
card: 'generic',
|
|
118
|
+
title: `ast_grep ${args.pat}${args.path === undefined ? '' : ` in ${args.path}`}`,
|
|
119
|
+
kind: 'search',
|
|
120
|
+
rawInput: args.pat,
|
|
121
|
+
}),
|
|
122
|
+
presentResult: (_args, result) => {
|
|
123
|
+
if (result.isError)
|
|
124
|
+
return undefined;
|
|
125
|
+
const meta = result.meta;
|
|
126
|
+
if (meta === undefined || meta.shape !== 'matches')
|
|
127
|
+
return undefined;
|
|
128
|
+
return { card: 'search', shape: 'matches', files: meta.files, truncated: meta.truncated, total: meta.total };
|
|
129
|
+
},
|
|
130
|
+
async execute(args, exec) {
|
|
131
|
+
const pattern = args.pat;
|
|
132
|
+
if (pattern.trim().length === 0)
|
|
133
|
+
throw new ToolArgsError(['pat must be a non-empty string']);
|
|
134
|
+
const skip = args.skip ?? 0;
|
|
135
|
+
if (!Number.isInteger(skip) || skip < 0)
|
|
136
|
+
throw new ToolArgsError(['skip must be a non-negative integer']);
|
|
137
|
+
const cwd = await deps.workdir(exec.signal);
|
|
138
|
+
const response = await cancellable(exec.signal, (cancel) => loadNative().astGrep({
|
|
139
|
+
pattern,
|
|
140
|
+
cwd,
|
|
141
|
+
limit: AST_MATCH_LIMIT,
|
|
142
|
+
skip,
|
|
143
|
+
maxFiles: AST_MAX_FILES,
|
|
144
|
+
cancel,
|
|
145
|
+
...(args.path === undefined ? {} : { path: args.path }),
|
|
146
|
+
...(args.lang === undefined ? {} : { lang: args.lang }),
|
|
147
|
+
...(args.include === undefined ? {} : { include: args.include }),
|
|
148
|
+
}));
|
|
149
|
+
return {
|
|
150
|
+
matches: response.matches.map((match) => ({
|
|
151
|
+
path: match.path,
|
|
152
|
+
lineNumber: match.startLine,
|
|
153
|
+
line: match.spanLines > 1 ? `${match.text} (${match.spanLines} lines)` : match.text,
|
|
154
|
+
meta: match.meta.map((entry) => `${entry.name}=${entry.text}`),
|
|
155
|
+
})),
|
|
156
|
+
total: response.total,
|
|
157
|
+
filesSearched: response.filesSearched,
|
|
158
|
+
unsupported: response.unsupported,
|
|
159
|
+
warnings: response.warnings.map((warning) => `${warning.path}: ${warning.message}`),
|
|
160
|
+
};
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
export function createAstEditTool(deps) {
|
|
165
|
+
return defineTool({
|
|
166
|
+
name: 'ast_edit',
|
|
167
|
+
description: 'Rewrite code structurally. Give an ast-grep pattern and its replacement ($A and $$$ARGS are reused from the pattern). It defaults to a dry run that writes nothing and returns a preview token; call again with dry_run: false and that token to apply, which refuses if any file changed in between.',
|
|
168
|
+
deferLoading: true,
|
|
169
|
+
parameters: {
|
|
170
|
+
pat: { type: 'string', required: true, description: 'ast-grep pattern to find, e.g. "logger.warn($A)".' },
|
|
171
|
+
out: { type: 'string', required: true, description: 'Replacement template reusing the pattern captures, e.g. "logger.info($A)".' },
|
|
172
|
+
path: { type: 'string', description: 'File or directory. Defaults to the session workspace.' },
|
|
173
|
+
lang: { type: 'string', description: 'Language id, overriding extension inference.' },
|
|
174
|
+
include: { type: 'string', description: 'One glob filter for candidate files.' },
|
|
175
|
+
dry_run: { type: 'boolean', description: 'Defaults to true. Set false with the preview token to apply.' },
|
|
176
|
+
preview: { type: 'string', description: 'Token returned by the dry run. Required when dry_run is false.' },
|
|
177
|
+
},
|
|
178
|
+
output: {
|
|
179
|
+
schema: {
|
|
180
|
+
type: 'object',
|
|
181
|
+
additionalProperties: false,
|
|
182
|
+
properties: {
|
|
183
|
+
preview: { type: 'string', required: true },
|
|
184
|
+
applied: { type: 'boolean', required: true },
|
|
185
|
+
total: { type: 'number', required: true },
|
|
186
|
+
files: {
|
|
187
|
+
type: 'array',
|
|
188
|
+
required: true,
|
|
189
|
+
items: {
|
|
190
|
+
type: 'object',
|
|
191
|
+
additionalProperties: false,
|
|
192
|
+
properties: {
|
|
193
|
+
path: { type: 'string', required: true },
|
|
194
|
+
replacements: { type: 'number', required: true },
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
render: (_args, value) => [{ type: 'text', text: renderAstEdit(value) }],
|
|
201
|
+
presentationMeta: (_args, value) => astEditMeta(value),
|
|
202
|
+
},
|
|
203
|
+
timeoutMs: deps.timeoutMs,
|
|
204
|
+
presentCall: (args) => ({
|
|
205
|
+
card: 'diff',
|
|
206
|
+
title: `ast_edit ${args.pat} -> ${args.out}`,
|
|
207
|
+
diffs: [{ path: args.path ?? '.', oldText: args.pat, newText: args.out }],
|
|
208
|
+
locations: [{ path: args.path ?? '.' }],
|
|
209
|
+
}),
|
|
210
|
+
presentResult: (_args, result) => {
|
|
211
|
+
if (result.isError)
|
|
212
|
+
return undefined;
|
|
213
|
+
const meta = result.meta;
|
|
214
|
+
if (meta === undefined || meta.shape !== 'paths')
|
|
215
|
+
return undefined;
|
|
216
|
+
return {
|
|
217
|
+
card: 'diff',
|
|
218
|
+
title: 'ast_edit',
|
|
219
|
+
diffs: meta.paths.map((path) => ({ path, oldText: null, newText: '' })),
|
|
220
|
+
};
|
|
221
|
+
},
|
|
222
|
+
async execute(args, exec) {
|
|
223
|
+
const pattern = args.pat;
|
|
224
|
+
const rewrite = args.out;
|
|
225
|
+
if (pattern.trim().length === 0)
|
|
226
|
+
throw new ToolArgsError(['pat must be a non-empty string']);
|
|
227
|
+
if (rewrite.trim().length === 0)
|
|
228
|
+
throw new ToolArgsError(['out must be a non-empty string']);
|
|
229
|
+
const applying = args.dry_run === false;
|
|
230
|
+
if (applying && (args.preview === undefined || args.preview.trim().length === 0)) {
|
|
231
|
+
throw new ToolArgsError(['preview is required when dry_run is false; run the dry run first']);
|
|
232
|
+
}
|
|
233
|
+
const cwd = await deps.workdir(exec.signal);
|
|
234
|
+
const native = loadNative();
|
|
235
|
+
const found = await cancellable(exec.signal, (cancel) => native.astGrep({
|
|
236
|
+
pattern,
|
|
237
|
+
cwd,
|
|
238
|
+
limit: 1,
|
|
239
|
+
skip: 0,
|
|
240
|
+
maxFiles: AST_MAX_FILES,
|
|
241
|
+
cancel,
|
|
242
|
+
...(args.path === undefined ? {} : { path: args.path }),
|
|
243
|
+
...(args.lang === undefined ? {} : { lang: args.lang }),
|
|
244
|
+
...(args.include === undefined ? {} : { include: args.include }),
|
|
245
|
+
}));
|
|
246
|
+
if (found.total === 0) {
|
|
247
|
+
return { preview: '', applied: false, files: [], total: 0 };
|
|
248
|
+
}
|
|
249
|
+
const candidates = [...new Set(found.matches.map((match) => match.path))];
|
|
250
|
+
const staged = [];
|
|
251
|
+
for (const path of candidates) {
|
|
252
|
+
const target = await deps.fs.resolve(path, { cwd, signal: exec.signal });
|
|
253
|
+
const before = await deps.fs.readText(target, exec.signal);
|
|
254
|
+
const rewritten = await cancellable(exec.signal, (cancel) => native.astRewrite({
|
|
255
|
+
text: before,
|
|
256
|
+
pattern,
|
|
257
|
+
rewrite,
|
|
258
|
+
path,
|
|
259
|
+
maxReplacements: AST_MAX_REPLACEMENTS,
|
|
260
|
+
cancel,
|
|
261
|
+
...(args.lang === undefined ? {} : { lang: args.lang }),
|
|
262
|
+
}));
|
|
263
|
+
if (rewritten.replacements > 0)
|
|
264
|
+
staged.push({ target, before, after: rewritten.content, replacements: rewritten.replacements });
|
|
265
|
+
}
|
|
266
|
+
const token = previewToken(staged.map((entry) => ({ path: entry.target.displayPath, before: entry.before })));
|
|
267
|
+
const files = staged.map((entry) => ({ path: entry.target.displayPath, replacements: entry.replacements }));
|
|
268
|
+
const total = staged.reduce((sum, entry) => sum + entry.replacements, 0);
|
|
269
|
+
if (!applying)
|
|
270
|
+
return { preview: token, applied: false, files, total };
|
|
271
|
+
if (args.preview !== token) {
|
|
272
|
+
throw new ToolArgsError([`preview ${args.preview} no longer matches the tree (${token}); run the dry run again`]);
|
|
273
|
+
}
|
|
274
|
+
for (const entry of staged) {
|
|
275
|
+
const intent = await deps.ctx.waterfall('fs/write-intent', entry.target, exec, () => undefined);
|
|
276
|
+
const written = await deps.fs.writeText(entry.target, entry.after, intent, exec.signal, deps.policy?.(exec));
|
|
277
|
+
deps.ctx.emit('fs/observed', entry.target, { kind: 'present', version: written.version }, exec);
|
|
278
|
+
}
|
|
279
|
+
return { preview: token, applied: true, files, total };
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
//# sourceMappingURL=ast.js.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import { type FileSystem } from '@deepseek-ai/dsh-fs';
|
|
3
|
+
import { type FileDiff, type ToolDefinition } from '@deepseek-ai/dsh-tools';
|
|
4
|
+
import type { SandboxExecutionPolicy } from '@deepseek-ai/dsh-sandbox';
|
|
5
|
+
export declare const DIFF_CONTEXT = 3;
|
|
6
|
+
export interface EditToolDeps {
|
|
7
|
+
readonly ctx: Context;
|
|
8
|
+
readonly fs: FileSystem;
|
|
9
|
+
readonly workdir: (signal: AbortSignal) => Promise<string>;
|
|
10
|
+
readonly policy?: ((exec: {
|
|
11
|
+
readonly agent?: unknown;
|
|
12
|
+
}) => SandboxExecutionPolicy | undefined) | undefined;
|
|
13
|
+
readonly timeoutMs: number;
|
|
14
|
+
}
|
|
15
|
+
export interface EditValue {
|
|
16
|
+
path: string;
|
|
17
|
+
before: string;
|
|
18
|
+
after: string;
|
|
19
|
+
}
|
|
20
|
+
export interface EditInput {
|
|
21
|
+
filePath: string;
|
|
22
|
+
oldString: string;
|
|
23
|
+
newString: string;
|
|
24
|
+
replaceAll: boolean;
|
|
25
|
+
}
|
|
26
|
+
export declare function parseEditArgs(args: {
|
|
27
|
+
file_path: string;
|
|
28
|
+
old_string: string;
|
|
29
|
+
new_string: string;
|
|
30
|
+
replace_all?: boolean;
|
|
31
|
+
}): EditInput;
|
|
32
|
+
export declare function formatEditOutput(displayPath: string, replaceAll: boolean): string;
|
|
33
|
+
export declare function appliedDiff(path: string, before: string, after: string): FileDiff[];
|
|
34
|
+
export declare function diffsFromMeta(meta: unknown): FileDiff[] | undefined;
|
|
35
|
+
export declare function createEditTool(deps: EditToolDeps): ToolDefinition;
|
|
36
|
+
//# sourceMappingURL=edit.d.ts.map
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { FsError } from '@deepseek-ai/dsh-fs';
|
|
2
|
+
import { defineTool, ToolArgsError, } from '@deepseek-ai/dsh-tools';
|
|
3
|
+
import { cancellable, loadNative } from '../native.js';
|
|
4
|
+
export const DIFF_CONTEXT = 3;
|
|
5
|
+
export function parseEditArgs(args) {
|
|
6
|
+
if (args.file_path.trim().length === 0)
|
|
7
|
+
throw new ToolArgsError(['file_path must be a non-empty string']);
|
|
8
|
+
if (args.old_string.length === 0)
|
|
9
|
+
throw new ToolArgsError(['old_string must be a non-empty string']);
|
|
10
|
+
if (args.old_string === args.new_string)
|
|
11
|
+
throw new ToolArgsError(['old_string and new_string must differ']);
|
|
12
|
+
return {
|
|
13
|
+
filePath: args.file_path,
|
|
14
|
+
oldString: args.old_string,
|
|
15
|
+
newString: args.new_string,
|
|
16
|
+
replaceAll: args.replace_all ?? false,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export function formatEditOutput(displayPath, replaceAll) {
|
|
20
|
+
return replaceAll
|
|
21
|
+
? `The file ${displayPath} has been updated. All occurrences were successfully replaced.`
|
|
22
|
+
: `The file ${displayPath} has been updated successfully.`;
|
|
23
|
+
}
|
|
24
|
+
export function appliedDiff(path, before, after) {
|
|
25
|
+
if (before === after)
|
|
26
|
+
return [];
|
|
27
|
+
return loadNative()
|
|
28
|
+
.diffHunks(before, after, DIFF_CONTEXT)
|
|
29
|
+
.map((hunk) => ({ path, oldText: hunk.oldText, newText: hunk.newText }));
|
|
30
|
+
}
|
|
31
|
+
function isFileDiff(value) {
|
|
32
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
33
|
+
return false;
|
|
34
|
+
const { path, oldText, newText } = value;
|
|
35
|
+
return typeof path === 'string'
|
|
36
|
+
&& (oldText === null || typeof oldText === 'string')
|
|
37
|
+
&& typeof newText === 'string';
|
|
38
|
+
}
|
|
39
|
+
export function diffsFromMeta(meta) {
|
|
40
|
+
if (typeof meta !== 'object' || meta === null || Array.isArray(meta))
|
|
41
|
+
return undefined;
|
|
42
|
+
const diffs = meta.diffs;
|
|
43
|
+
if (!Array.isArray(diffs) || diffs.length === 0 || !diffs.every(isFileDiff))
|
|
44
|
+
return undefined;
|
|
45
|
+
return diffs;
|
|
46
|
+
}
|
|
47
|
+
export function createEditTool(deps) {
|
|
48
|
+
return defineTool({
|
|
49
|
+
name: 'edit',
|
|
50
|
+
description: 'Edit an existing UTF-8 text file by replacing literal text.',
|
|
51
|
+
parameters: {
|
|
52
|
+
file_path: {
|
|
53
|
+
type: 'string',
|
|
54
|
+
required: true,
|
|
55
|
+
description: 'Path to edit, resolved by the filesystem backend. '
|
|
56
|
+
+ 'Provide `file_path` before `old_string` and `new_string` in the arguments.',
|
|
57
|
+
},
|
|
58
|
+
old_string: { type: 'string', required: true, description: 'Literal text to replace.' },
|
|
59
|
+
new_string: {
|
|
60
|
+
type: 'string',
|
|
61
|
+
required: true,
|
|
62
|
+
description: 'Literal replacement text. Use an empty string to delete the match.',
|
|
63
|
+
},
|
|
64
|
+
replace_all: {
|
|
65
|
+
type: 'boolean',
|
|
66
|
+
description: 'Defaults to false; when false, old_string must appear exactly once.',
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
output: {
|
|
70
|
+
schema: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
additionalProperties: false,
|
|
73
|
+
properties: {
|
|
74
|
+
path: { type: 'string', required: true },
|
|
75
|
+
before: { type: 'string', required: true },
|
|
76
|
+
after: { type: 'string', required: true },
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
render: (args, value) => [{ type: 'text', text: formatEditOutput(value.path, args.replace_all ?? false) }],
|
|
80
|
+
presentationMeta: (_args, value) => ({
|
|
81
|
+
path: value.path,
|
|
82
|
+
diffs: appliedDiff(value.path, value.before, value.after)
|
|
83
|
+
.map(({ path, oldText, newText }) => ({ path, oldText, newText })),
|
|
84
|
+
}),
|
|
85
|
+
},
|
|
86
|
+
timeoutMs: deps.timeoutMs,
|
|
87
|
+
presentCall: (args) => ({
|
|
88
|
+
card: 'diff',
|
|
89
|
+
title: `Edit ${args.file_path}`,
|
|
90
|
+
diffs: [{ path: args.file_path, oldText: args.old_string || null, newText: args.new_string }],
|
|
91
|
+
locations: [{ path: args.file_path }],
|
|
92
|
+
}),
|
|
93
|
+
presentResult(args, result) {
|
|
94
|
+
if (result.isError)
|
|
95
|
+
return undefined;
|
|
96
|
+
const diffs = diffsFromMeta(result.meta);
|
|
97
|
+
if (diffs === undefined)
|
|
98
|
+
return undefined;
|
|
99
|
+
return { card: 'diff', title: `Edit ${args.file_path}`, diffs };
|
|
100
|
+
},
|
|
101
|
+
async execute(args, exec) {
|
|
102
|
+
const input = parseEditArgs(args);
|
|
103
|
+
const cwd = await deps.workdir(exec.signal);
|
|
104
|
+
const target = await deps.fs.resolve(input.filePath, { cwd, signal: exec.signal });
|
|
105
|
+
const intent = await deps.ctx.waterfall('fs/edit-intent', target, exec, () => undefined);
|
|
106
|
+
const stored = await deps.fs.readText(target, exec.signal);
|
|
107
|
+
const outcome = await cancellable(exec.signal, (cancel) => loadNative().editContent({
|
|
108
|
+
text: stored,
|
|
109
|
+
oldString: input.oldString,
|
|
110
|
+
newString: input.newString,
|
|
111
|
+
replaceAll: input.replaceAll,
|
|
112
|
+
cancel,
|
|
113
|
+
}));
|
|
114
|
+
if (outcome.outcome === 'notFound') {
|
|
115
|
+
throw new FsError(`old_string was not found in "${target.displayPath}"`, 'FS_EDIT_NOT_FOUND');
|
|
116
|
+
}
|
|
117
|
+
if (outcome.outcome === 'ambiguous') {
|
|
118
|
+
throw new FsError(`old_string matched ${outcome.replacements} times in "${target.displayPath}"; `
|
|
119
|
+
+ 'provide a more specific old_string or set replace_all to true', 'FS_AMBIGUOUS_EDIT');
|
|
120
|
+
}
|
|
121
|
+
const written = await deps.fs.writeText(target, outcome.content, intent === undefined ? undefined : { kind: 'replaceIfVersion', version: intent.version }, exec.signal, deps.policy?.(exec));
|
|
122
|
+
deps.ctx.emit('fs/observed', target, { kind: 'present', version: written.version }, exec);
|
|
123
|
+
return { path: deps.fs.processPath(target), before: written.before ?? stored, after: written.after };
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=edit.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ToolDefinition } from '@deepseek-ai/dsh-tools';
|
|
2
|
+
export declare const GLOB_MAX_RESULTS = 100;
|
|
3
|
+
export declare const META_MAX_BYTES = 65536;
|
|
4
|
+
export interface GlobDeps {
|
|
5
|
+
readonly workdir: (signal: AbortSignal) => Promise<string>;
|
|
6
|
+
readonly maxResults: number;
|
|
7
|
+
readonly timeoutMs: number;
|
|
8
|
+
}
|
|
9
|
+
export interface GlobValue {
|
|
10
|
+
root: string;
|
|
11
|
+
paths: string[];
|
|
12
|
+
total: number;
|
|
13
|
+
}
|
|
14
|
+
export declare function createGlobTool(deps: GlobDeps): ToolDefinition;
|
|
15
|
+
//# sourceMappingURL=glob.d.ts.map
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { defineTool, ToolArgsError } from '@deepseek-ai/dsh-tools';
|
|
2
|
+
import { capMeta, formatGlob, GLOB_SPILL_FALLBACK, pathsMeta } from '../format.js';
|
|
3
|
+
import { cancellable, loadNative } from '../native.js';
|
|
4
|
+
export const GLOB_MAX_RESULTS = 100;
|
|
5
|
+
export const META_MAX_BYTES = 65536;
|
|
6
|
+
export function createGlobTool(deps) {
|
|
7
|
+
return defineTool({
|
|
8
|
+
name: 'glob',
|
|
9
|
+
description: `Find files, not directories, whose paths match a glob pattern, including hidden and ignored files. Returns up to ${deps.maxResults} paths in modification-time order; a larger result keeps the first paths.`,
|
|
10
|
+
parameters: {
|
|
11
|
+
pattern: {
|
|
12
|
+
type: 'string',
|
|
13
|
+
required: true,
|
|
14
|
+
description: 'Glob pattern to match file paths against (e.g. "**/*.ts", "src/**/*.test.js"). A pattern with no "/" matches the basename at any depth, so "*" and "*.ts" both search the whole tree; include a separator to anchor the depth.',
|
|
15
|
+
},
|
|
16
|
+
path: {
|
|
17
|
+
type: 'string',
|
|
18
|
+
description: 'Directory to search in. Defaults to the session workspace; a relative path resolves against it.',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
output: {
|
|
22
|
+
schema: {
|
|
23
|
+
type: 'object',
|
|
24
|
+
additionalProperties: false,
|
|
25
|
+
properties: {
|
|
26
|
+
root: { type: 'string', required: true },
|
|
27
|
+
paths: { type: 'array', required: true, items: { type: 'string' } },
|
|
28
|
+
total: { type: 'number', required: true },
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
render: (_args, value) => [{ type: 'text', text: formatGlob(value.paths, value.total, GLOB_SPILL_FALLBACK) }],
|
|
32
|
+
presentationMeta: (_args, value) => capMeta(pathsMeta(value.paths, value.total), META_MAX_BYTES),
|
|
33
|
+
},
|
|
34
|
+
timeoutMs: deps.timeoutMs,
|
|
35
|
+
isConcurrencySafe: () => true,
|
|
36
|
+
presentCall: (args) => ({
|
|
37
|
+
card: 'generic',
|
|
38
|
+
title: `Glob ${args.pattern}${args.path !== undefined ? ` in ${args.path}` : ''}`,
|
|
39
|
+
kind: 'search',
|
|
40
|
+
rawInput: args.pattern,
|
|
41
|
+
}),
|
|
42
|
+
presentResult: (_args, result) => {
|
|
43
|
+
if (result.isError)
|
|
44
|
+
return undefined;
|
|
45
|
+
const meta = result.meta;
|
|
46
|
+
if (meta === undefined || meta.shape !== 'paths')
|
|
47
|
+
return undefined;
|
|
48
|
+
return { card: 'search', shape: 'paths', paths: meta.paths, truncated: meta.truncated, total: meta.total };
|
|
49
|
+
},
|
|
50
|
+
async execute(args, exec) {
|
|
51
|
+
const pattern = args.pattern;
|
|
52
|
+
if (pattern.trim().length === 0)
|
|
53
|
+
throw new ToolArgsError(['pattern must be a non-empty string']);
|
|
54
|
+
const given = args.path;
|
|
55
|
+
if (given !== undefined && given.trim().length === 0)
|
|
56
|
+
throw new ToolArgsError(['path must be a non-empty string when given']);
|
|
57
|
+
const cwd = await deps.workdir(exec.signal);
|
|
58
|
+
const response = await cancellable(exec.signal, (cancel) => loadNative().glob({
|
|
59
|
+
pattern,
|
|
60
|
+
cwd,
|
|
61
|
+
maxResults: deps.maxResults,
|
|
62
|
+
cancel,
|
|
63
|
+
...(given === undefined ? {} : { path: given }),
|
|
64
|
+
}));
|
|
65
|
+
return { root: response.root, paths: response.paths, total: response.total };
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=glob.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type ToolDefinition } from '@deepseek-ai/dsh-tools';
|
|
2
|
+
import { type GrepMatch } from '../format.js';
|
|
3
|
+
import { type GrepResponse } from '../native.js';
|
|
4
|
+
export declare const GREP_MAX_MATCHES = 250;
|
|
5
|
+
export declare const GREP_MAX_LINE_BYTES = 2000;
|
|
6
|
+
export declare const META_MAX_BYTES = 65536;
|
|
7
|
+
export interface GrepDeps {
|
|
8
|
+
readonly workdir: (signal: AbortSignal) => Promise<string>;
|
|
9
|
+
readonly maxMatches: number;
|
|
10
|
+
readonly maxLineBytes: number;
|
|
11
|
+
readonly timeoutMs: number;
|
|
12
|
+
}
|
|
13
|
+
export interface GrepValue {
|
|
14
|
+
matches: GrepMatch[];
|
|
15
|
+
total: number;
|
|
16
|
+
}
|
|
17
|
+
export declare function validateInclude(include: string | undefined): string | undefined;
|
|
18
|
+
export declare function grepValuesToMatches(response: GrepResponse): GrepMatch[];
|
|
19
|
+
export declare function createGrepTool(deps: GrepDeps): ToolDefinition;
|
|
20
|
+
//# sourceMappingURL=grep.d.ts.map
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { defineTool, ToolArgsError } from '@deepseek-ai/dsh-tools';
|
|
2
|
+
import { formatGrep, matchesMeta, SPILL_FALLBACK, capMeta } from '../format.js';
|
|
3
|
+
import { cancellable, loadNative } from '../native.js';
|
|
4
|
+
export const GREP_MAX_MATCHES = 250;
|
|
5
|
+
export const GREP_MAX_LINE_BYTES = 2000;
|
|
6
|
+
export const META_MAX_BYTES = 65536;
|
|
7
|
+
const INCLUDE_NEGATED = 'include must be a positive glob filter; negated patterns ("!…") are not supported';
|
|
8
|
+
const INCLUDE_LIST = 'include must be one glob, not a comma-separated list (use {a,b} alternation instead)';
|
|
9
|
+
export function validateInclude(include) {
|
|
10
|
+
if (include === undefined || include.trim().length === 0)
|
|
11
|
+
return undefined;
|
|
12
|
+
if (include.startsWith('!'))
|
|
13
|
+
throw new ToolArgsError([INCLUDE_NEGATED]);
|
|
14
|
+
let depth = 0;
|
|
15
|
+
for (const character of include) {
|
|
16
|
+
if (character === '{')
|
|
17
|
+
depth += 1;
|
|
18
|
+
if (character === '}')
|
|
19
|
+
depth = Math.max(0, depth - 1);
|
|
20
|
+
if (character === ',' && depth === 0)
|
|
21
|
+
throw new ToolArgsError([INCLUDE_LIST]);
|
|
22
|
+
}
|
|
23
|
+
return include;
|
|
24
|
+
}
|
|
25
|
+
export function grepValuesToMatches(response) {
|
|
26
|
+
return response.hits.map((hit) => ({ path: hit.path, lineNumber: hit.lineNumber, line: hit.line }));
|
|
27
|
+
}
|
|
28
|
+
export function createGrepTool(deps) {
|
|
29
|
+
return defineTool({
|
|
30
|
+
name: 'grep',
|
|
31
|
+
description: `Search file contents with a ripgrep regular expression. Returns matching lines with line numbers, grouped by file. Returns up to ${deps.maxMatches} matches.`,
|
|
32
|
+
parameters: {
|
|
33
|
+
pattern: { type: 'string', required: true, description: 'Regular expression to search for (ripgrep syntax).' },
|
|
34
|
+
path: {
|
|
35
|
+
type: 'string',
|
|
36
|
+
description: 'File or directory to search. Defaults to the session workspace; a relative path resolves against it.',
|
|
37
|
+
},
|
|
38
|
+
include: {
|
|
39
|
+
type: 'string',
|
|
40
|
+
description: 'One glob filter for which files to search (e.g. "*.ts", "*.{js,jsx}"). Not a list; negation is not supported.',
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
output: {
|
|
44
|
+
schema: {
|
|
45
|
+
type: 'object',
|
|
46
|
+
additionalProperties: false,
|
|
47
|
+
properties: {
|
|
48
|
+
matches: {
|
|
49
|
+
type: 'array',
|
|
50
|
+
required: true,
|
|
51
|
+
items: {
|
|
52
|
+
type: 'object',
|
|
53
|
+
additionalProperties: false,
|
|
54
|
+
properties: {
|
|
55
|
+
path: { type: 'string', required: true },
|
|
56
|
+
lineNumber: { type: 'number', required: true },
|
|
57
|
+
line: { type: 'string', required: true },
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
total: { type: 'number', required: true },
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
render: (_args, value) => [{ type: 'text', text: formatGrep(value.matches, value.total, SPILL_FALLBACK) }],
|
|
65
|
+
presentationMeta: (_args, value) => capMeta(matchesMeta(value.matches, value.total), META_MAX_BYTES),
|
|
66
|
+
},
|
|
67
|
+
timeoutMs: deps.timeoutMs,
|
|
68
|
+
isConcurrencySafe: () => true,
|
|
69
|
+
presentCall: (args) => ({
|
|
70
|
+
card: 'generic',
|
|
71
|
+
title: `Grep ${args.pattern}${args.path !== undefined ? ` in ${args.path}` : ''}${args.include !== undefined && args.include !== '' ? ` (${args.include})` : ''}`,
|
|
72
|
+
kind: 'search',
|
|
73
|
+
rawInput: args.pattern,
|
|
74
|
+
}),
|
|
75
|
+
presentResult: (_args, result) => {
|
|
76
|
+
if (result.isError)
|
|
77
|
+
return undefined;
|
|
78
|
+
const meta = result.meta;
|
|
79
|
+
if (meta === undefined || meta.shape !== 'matches')
|
|
80
|
+
return undefined;
|
|
81
|
+
return { card: 'search', shape: 'matches', files: meta.files, truncated: meta.truncated, total: meta.total };
|
|
82
|
+
},
|
|
83
|
+
async execute(args, exec) {
|
|
84
|
+
const pattern = args.pattern;
|
|
85
|
+
if (pattern.trim().length === 0)
|
|
86
|
+
throw new ToolArgsError(['pattern must be a non-empty string']);
|
|
87
|
+
const given = args.path;
|
|
88
|
+
if (given !== undefined && given.trim().length === 0)
|
|
89
|
+
throw new ToolArgsError(['path must be a non-empty string when given']);
|
|
90
|
+
const include = validateInclude(args.include);
|
|
91
|
+
const cwd = await deps.workdir(exec.signal);
|
|
92
|
+
const response = await cancellable(exec.signal, (cancel) => loadNative().grep({
|
|
93
|
+
pattern,
|
|
94
|
+
cwd,
|
|
95
|
+
maxMatches: deps.maxMatches,
|
|
96
|
+
maxLineBytes: deps.maxLineBytes,
|
|
97
|
+
cancel,
|
|
98
|
+
...(given === undefined ? {} : { path: given }),
|
|
99
|
+
...(include === undefined ? {} : { include }),
|
|
100
|
+
}));
|
|
101
|
+
return { matches: grepValuesToMatches(response), total: response.total };
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=grep.js.map
|