@mikode13/harness 2.1.0 → 2.3.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/dist/agent/domain/agent.d.ts +7 -0
- package/dist/agent/domain/agent.js.map +1 -1
- package/dist/engines/domain/model/llmAgent.d.ts +20 -3
- package/dist/engines/domain/model/llmAgent.js +156 -30
- package/dist/engines/domain/model/llmAgent.js.map +1 -1
- package/dist/llm/domain/conversation.d.ts +4 -3
- package/dist/llm/domain/conversation.js +5 -4
- package/dist/llm/domain/conversation.js.map +1 -1
- package/dist/llm/domain/llm.d.ts +5 -1
- package/dist/llm/domain/llm.js.map +1 -1
- package/dist/llm/domain/message.d.ts +30 -4
- package/dist/llm/domain/message.js.map +1 -1
- package/dist/llm/domain/tool.d.ts +10 -0
- package/dist/llm/domain/tool.js +2 -0
- package/dist/llm/domain/tool.js.map +1 -0
- package/dist/llm/infrastructure/claudeLLMClient.d.ts +5 -1
- package/dist/llm/infrastructure/claudeLLMClient.js +78 -19
- package/dist/llm/infrastructure/claudeLLMClient.js.map +1 -1
- package/dist/llm/infrastructure/openAILLMClient.d.ts +5 -1
- package/dist/llm/infrastructure/openAILLMClient.js +88 -12
- package/dist/llm/infrastructure/openAILLMClient.js.map +1 -1
- package/dist/tools/domain/tool.d.ts +9 -0
- package/dist/tools/domain/tool.js +2 -0
- package/dist/tools/domain/tool.js.map +1 -0
- package/dist/tools/domain/workspace.d.ts +48 -0
- package/dist/tools/domain/workspace.js +2 -0
- package/dist/tools/domain/workspace.js.map +1 -0
- package/dist/tools/infrastructure/boundedWorkspace.d.ts +66 -0
- package/dist/tools/infrastructure/boundedWorkspace.js +160 -0
- package/dist/tools/infrastructure/boundedWorkspace.js.map +1 -0
- package/dist/tools/infrastructure/createWorkspace.d.ts +14 -0
- package/dist/tools/infrastructure/createWorkspace.js +54 -0
- package/dist/tools/infrastructure/createWorkspace.js.map +1 -0
- package/dist/tools/infrastructure/defineTool.d.ts +15 -0
- package/dist/tools/infrastructure/defineTool.js +87 -0
- package/dist/tools/infrastructure/defineTool.js.map +1 -0
- package/dist/tools/infrastructure/gitWorkspace.d.ts +13 -0
- package/dist/tools/infrastructure/gitWorkspace.js +61 -0
- package/dist/tools/infrastructure/gitWorkspace.js.map +1 -0
- package/dist/tools/infrastructure/ripgrepWorkspace.d.ts +20 -0
- package/dist/tools/infrastructure/ripgrepWorkspace.js +85 -0
- package/dist/tools/infrastructure/ripgrepWorkspace.js.map +1 -0
- package/dist/tools/infrastructure/runProcess.d.ts +23 -0
- package/dist/tools/infrastructure/runProcess.js +91 -0
- package/dist/tools/infrastructure/runProcess.js.map +1 -0
- package/dist/tools/infrastructure/workspaceTools.d.ts +4 -0
- package/dist/tools/infrastructure/workspaceTools.js +110 -0
- package/dist/tools/infrastructure/workspaceTools.js.map +1 -0
- package/package.json +2 -1
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs';
|
|
2
|
+
import { lstat, open } from 'node:fs/promises';
|
|
3
|
+
import { isAbsolute, join, matchesGlob, relative, resolve, sep } from 'node:path';
|
|
4
|
+
import { createInterface } from 'node:readline';
|
|
5
|
+
// A NUL byte this early means a binary file, as git and ripgrep both judge it.
|
|
6
|
+
const binaryProbeBytes = 8000;
|
|
7
|
+
// What a search holds while it sorts. A minified file can put a whole bundle on one line, and
|
|
8
|
+
// a careless pattern can match every line of a repository.
|
|
9
|
+
const maxStoredMatches = 50_000;
|
|
10
|
+
const maxStoredLineLength = 1_000;
|
|
11
|
+
// Far beyond what ripgrep needs on a large repository, so only a runaway search reaches it,
|
|
12
|
+
// such as a pattern that backtracks without end in git's Perl engine.
|
|
13
|
+
const defaultTimeoutMs = 30_000;
|
|
14
|
+
function compareText(a, b) {
|
|
15
|
+
// Plain code-unit order: `localeCompare` would sort differently on every machine.
|
|
16
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Everything both implementations share: the boundary and the shape of every answer. A
|
|
20
|
+
* subclass only says which files are visible and where a pattern matches, across the whole
|
|
21
|
+
* root; the scope a model asks for is applied here, to those results, and never handed to the
|
|
22
|
+
* program underneath. ripgrep stops honouring `.gitignore` for a path it is given explicitly,
|
|
23
|
+
* so passing the model's scope down is how a secret would leak. See "`.gitignore` is the
|
|
24
|
+
* boundary of what an agent reads" in decisions.md.
|
|
25
|
+
*
|
|
26
|
+
* Every operation also stops after `timeoutMs`. A timeout is a failure the model reads and
|
|
27
|
+
* narrows its query from, not a cancellation: only the run's own signal cancels the run.
|
|
28
|
+
*/
|
|
29
|
+
export class BoundedWorkspace {
|
|
30
|
+
root;
|
|
31
|
+
timeoutMs;
|
|
32
|
+
constructor({ root, timeoutMs = defaultTimeoutMs }) {
|
|
33
|
+
this.root = root;
|
|
34
|
+
this.timeoutMs = timeoutMs;
|
|
35
|
+
}
|
|
36
|
+
listFiles(query, signal) {
|
|
37
|
+
return this.withTimeout(signal, async (bounded) => {
|
|
38
|
+
const inScope = [...new Set(await this.listCandidates(bounded))].filter(path => this.inScope(path, query));
|
|
39
|
+
const files = (await this.regularFiles(inScope)).sort(compareText);
|
|
40
|
+
return {
|
|
41
|
+
files: files.slice(0, query.limit),
|
|
42
|
+
total: files.length,
|
|
43
|
+
truncated: files.length > query.limit,
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
searchText(query, signal) {
|
|
48
|
+
return this.withTimeout(signal, async (bounded) => {
|
|
49
|
+
const listed = new Set(await this.listCandidates(bounded));
|
|
50
|
+
// The scope applies as the output arrives, so narrowing a search is what makes it
|
|
51
|
+
// lighter: matches outside it are never stored, however many the program prints.
|
|
52
|
+
const kept = [];
|
|
53
|
+
await this.findMatches(query.pattern, query.ignoreCase, bounded, match => {
|
|
54
|
+
if (!listed.has(match.path) || !this.inScope(match.path, query)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (kept.length === maxStoredMatches) {
|
|
58
|
+
throw new Error(`More than ${String(maxStoredMatches)} lines match; narrow the pattern, path or glob`);
|
|
59
|
+
}
|
|
60
|
+
kept.push({ ...match, text: match.text.slice(0, maxStoredLineLength) });
|
|
61
|
+
});
|
|
62
|
+
// Only the files that matched are checked on disk, not the whole repository.
|
|
63
|
+
const regular = new Set(await this.regularFiles([...new Set(kept.map(match => match.path))]));
|
|
64
|
+
const matches = kept
|
|
65
|
+
.filter(match => regular.has(match.path))
|
|
66
|
+
.sort((a, b) => compareText(a.path, b.path) || a.line - b.line);
|
|
67
|
+
return {
|
|
68
|
+
matches: matches.slice(0, query.limit),
|
|
69
|
+
total: matches.length,
|
|
70
|
+
truncated: matches.length > query.limit,
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
readFile(query, signal) {
|
|
75
|
+
return this.withTimeout(signal, async (bounded) => {
|
|
76
|
+
const path = this.toRelative(query.path);
|
|
77
|
+
const visible = path !== undefined &&
|
|
78
|
+
(await this.listCandidates(bounded)).includes(path) &&
|
|
79
|
+
(await this.regularFiles([path])).length === 1;
|
|
80
|
+
// One answer for a missing file and an ignored one, or the model would learn the secret exists.
|
|
81
|
+
if (!visible) {
|
|
82
|
+
throw new Error(`No such file: ${query.path}`);
|
|
83
|
+
}
|
|
84
|
+
const absolute = join(this.root, path);
|
|
85
|
+
if (await isBinary(absolute)) {
|
|
86
|
+
throw new Error(`Not a text file: ${query.path}`);
|
|
87
|
+
}
|
|
88
|
+
// Streamed, so a large file costs its length in lines, not in memory.
|
|
89
|
+
const reader = createInterface({
|
|
90
|
+
input: createReadStream(absolute, { encoding: 'utf8', signal: bounded }),
|
|
91
|
+
crlfDelay: Infinity,
|
|
92
|
+
});
|
|
93
|
+
const last = query.fromLine + query.lineCount - 1;
|
|
94
|
+
const lines = [];
|
|
95
|
+
let totalLines = 0;
|
|
96
|
+
for await (const line of reader) {
|
|
97
|
+
totalLines++;
|
|
98
|
+
if (totalLines >= query.fromLine && totalLines <= last) {
|
|
99
|
+
lines.push(line);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { lines, totalLines, truncated: totalLines > last };
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Runs an operation under the run's signal and this workspace's time limit together. The
|
|
107
|
+
* run's cancellation escapes unchanged; the time limit becomes an error the model can act on.
|
|
108
|
+
*/
|
|
109
|
+
async withTimeout(signal, operation) {
|
|
110
|
+
signal.throwIfAborted();
|
|
111
|
+
const timeout = AbortSignal.timeout(this.timeoutMs);
|
|
112
|
+
try {
|
|
113
|
+
return await operation(AbortSignal.any([signal, timeout]));
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
signal.throwIfAborted();
|
|
117
|
+
if (timeout.aborted) {
|
|
118
|
+
throw new Error(`The operation took longer than ${String(this.timeoutMs / 1000)} s; narrow the pattern, path or glob`, { cause: error });
|
|
119
|
+
}
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Regular files only: a symlink could point out of the root, and a deleted file is gone. */
|
|
124
|
+
async regularFiles(paths) {
|
|
125
|
+
const regular = await Promise.all(paths.map(path => lstat(join(this.root, path)).then(stats => stats.isFile(), () => false)));
|
|
126
|
+
return paths.filter((_, index) => regular[index]);
|
|
127
|
+
}
|
|
128
|
+
inScope(path, { path: scope, glob }) {
|
|
129
|
+
if (scope !== undefined) {
|
|
130
|
+
const folder = this.toRelative(scope);
|
|
131
|
+
if (folder === undefined) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
if (folder !== '' && path !== folder && !path.startsWith(`${folder}/`)) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return glob === undefined || matchesGlob(path, glob);
|
|
139
|
+
}
|
|
140
|
+
/** A path relative to the root with `/`, '' for the root itself, or undefined when it leaves it. */
|
|
141
|
+
toRelative(path) {
|
|
142
|
+
const relativePath = relative(this.root, resolve(this.root, path));
|
|
143
|
+
if (relativePath === '..' || relativePath.startsWith(`..${sep}`) || isAbsolute(relativePath)) {
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
return relativePath.split(sep).join('/');
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async function isBinary(path) {
|
|
150
|
+
const file = await open(path);
|
|
151
|
+
try {
|
|
152
|
+
const probe = Buffer.alloc(binaryProbeBytes);
|
|
153
|
+
const { bytesRead } = await file.read(probe, 0, probe.length, 0);
|
|
154
|
+
return probe.subarray(0, bytesRead).includes(0);
|
|
155
|
+
}
|
|
156
|
+
finally {
|
|
157
|
+
await file.close();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=boundedWorkspace.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"boundedWorkspace.js","sourceRoot":"","sources":["../../../src/tools/infrastructure/boundedWorkspace.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAClF,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAGhD,+EAA+E;AAC/E,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAE9B,8FAA8F;AAC9F,2DAA2D;AAC3D,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAChC,MAAM,mBAAmB,GAAG,KAAK,CAAC;AAElC,4FAA4F;AAC5F,sEAAsE;AACtE,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEhC,SAAS,WAAW,CAAC,CAAS,EAAE,CAAS;IACxC,kFAAkF;IAClF,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,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AC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC1C,CAAC;CACD;AAED,KAAK,UAAU,QAAQ,CAAC,IAAY;IACnC,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,CAAC;IAC9B,IAAI,CAAC;QACJ,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC;QAC7C,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;QACjE,OAAO,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IACjD,CAAC;YAAS,CAAC;QACV,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;IACpB,CAAC;AACF,CAAC","sourcesContent":["import { createReadStream } from 'node:fs';\nimport { lstat, open } from 'node:fs/promises';\nimport { isAbsolute, join, matchesGlob, relative, resolve, sep } from 'node:path';\nimport { createInterface } from 'node:readline';\nimport type { TextMatch, Workspace } from '../domain/workspace.ts';\n\n// A NUL byte this early means a binary file, as git and ripgrep both judge it.\nconst binaryProbeBytes = 8000;\n\n// What a search holds while it sorts. A minified file can put a whole bundle on one line, and\n// a careless pattern can match every line of a repository.\nconst maxStoredMatches = 50_000;\nconst maxStoredLineLength = 1_000;\n\n// Far beyond what ripgrep needs on a large repository, so only a runaway search reaches it,\n// such as a pattern that backtracks without end in git's Perl engine.\nconst defaultTimeoutMs = 30_000;\n\nfunction compareText(a: string, b: string): number {\n\t// Plain code-unit order: `localeCompare` would sort differently on every machine.\n\treturn a < b ? -1 : a > b ? 1 : 0;\n}\n\n/**\n * Everything both implementations share: the boundary and the shape of every answer. A\n * subclass only says which files are visible and where a pattern matches, across the whole\n * root; the scope a model asks for is applied here, to those results, and never handed to the\n * program underneath. ripgrep stops honouring `.gitignore` for a path it is given explicitly,\n * so passing the model's scope down is how a secret would leak. See \"`.gitignore` is the\n * boundary of what an agent reads\" in decisions.md.\n *\n * Every operation also stops after `timeoutMs`. A timeout is a failure the model reads and\n * narrows its query from, not a cancellation: only the run's own signal cancels the run.\n */\nexport abstract class BoundedWorkspace implements Workspace {\n\tprotected readonly root: string;\n\tprivate readonly timeoutMs: number;\n\n\tconstructor({ root, timeoutMs = defaultTimeoutMs }: { root: string; timeoutMs?: number }) {\n\t\tthis.root = root;\n\t\tthis.timeoutMs = timeoutMs;\n\t}\n\n\t/** Every file the program does not ignore, relative to the root, in any order. */\n\tprotected abstract listCandidates(signal: AbortSignal): Promise<string[]>;\n\n\t/**\n\t * Hands `keep` every line matching `pattern` in a file the program does not ignore, in any\n\t * order, as the program prints it. Nothing is held here: `keep` decides what to store.\n\t */\n\tprotected abstract findMatches(\n\t\tpattern: string,\n\t\tignoreCase: boolean,\n\t\tsignal: AbortSignal,\n\t\tkeep: (match: TextMatch) => void,\n\t): Promise<void>;\n\n\tlistFiles(\n\t\tquery: { path?: string; glob?: string; limit: number },\n\t\tsignal: AbortSignal,\n\t): Promise<{ files: string[]; total: number; truncated: boolean }> {\n\t\treturn this.withTimeout(signal, async bounded => {\n\t\t\tconst inScope = [...new Set(await this.listCandidates(bounded))].filter(path =>\n\t\t\t\tthis.inScope(path, query),\n\t\t\t);\n\t\t\tconst files = (await this.regularFiles(inScope)).sort(compareText);\n\n\t\t\treturn {\n\t\t\t\tfiles: files.slice(0, query.limit),\n\t\t\t\ttotal: files.length,\n\t\t\t\ttruncated: files.length > query.limit,\n\t\t\t};\n\t\t});\n\t}\n\n\tsearchText(\n\t\tquery: { pattern: string; ignoreCase: boolean; path?: string; glob?: string; limit: number },\n\t\tsignal: AbortSignal,\n\t): Promise<{ matches: TextMatch[]; total: number; truncated: boolean }> {\n\t\treturn this.withTimeout(signal, async bounded => {\n\t\t\tconst listed = new Set(await this.listCandidates(bounded));\n\t\t\t// The scope applies as the output arrives, so narrowing a search is what makes it\n\t\t\t// lighter: matches outside it are never stored, however many the program prints.\n\t\t\tconst kept: TextMatch[] = [];\n\t\t\tawait this.findMatches(query.pattern, query.ignoreCase, bounded, match => {\n\t\t\t\tif (!listed.has(match.path) || !this.inScope(match.path, query)) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (kept.length === maxStoredMatches) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`More than ${String(maxStoredMatches)} lines match; narrow the pattern, path or glob`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tkept.push({ ...match, text: match.text.slice(0, maxStoredLineLength) });\n\t\t\t});\n\n\t\t\t// Only the files that matched are checked on disk, not the whole repository.\n\t\t\tconst regular = new Set(await this.regularFiles([...new Set(kept.map(match => match.path))]));\n\t\t\tconst matches = kept\n\t\t\t\t.filter(match => regular.has(match.path))\n\t\t\t\t.sort((a, b) => compareText(a.path, b.path) || a.line - b.line);\n\n\t\t\treturn {\n\t\t\t\tmatches: matches.slice(0, query.limit),\n\t\t\t\ttotal: matches.length,\n\t\t\t\ttruncated: matches.length > query.limit,\n\t\t\t};\n\t\t});\n\t}\n\n\treadFile(\n\t\tquery: { path: string; fromLine: number; lineCount: number },\n\t\tsignal: AbortSignal,\n\t): Promise<{ lines: string[]; totalLines: number; truncated: boolean }> {\n\t\treturn this.withTimeout(signal, async bounded => {\n\t\t\tconst path = this.toRelative(query.path);\n\t\t\tconst visible =\n\t\t\t\tpath !== undefined &&\n\t\t\t\t(await this.listCandidates(bounded)).includes(path) &&\n\t\t\t\t(await this.regularFiles([path])).length === 1;\n\t\t\t// One answer for a missing file and an ignored one, or the model would learn the secret exists.\n\t\t\tif (!visible) {\n\t\t\t\tthrow new Error(`No such file: ${query.path}`);\n\t\t\t}\n\n\t\t\tconst absolute = join(this.root, path);\n\t\t\tif (await isBinary(absolute)) {\n\t\t\t\tthrow new Error(`Not a text file: ${query.path}`);\n\t\t\t}\n\n\t\t\t// Streamed, so a large file costs its length in lines, not in memory.\n\t\t\tconst reader = createInterface({\n\t\t\t\tinput: createReadStream(absolute, { encoding: 'utf8', signal: bounded }),\n\t\t\t\tcrlfDelay: Infinity,\n\t\t\t});\n\t\t\tconst last = query.fromLine + query.lineCount - 1;\n\t\t\tconst lines: string[] = [];\n\t\t\tlet totalLines = 0;\n\t\t\tfor await (const line of reader) {\n\t\t\t\ttotalLines++;\n\t\t\t\tif (totalLines >= query.fromLine && totalLines <= last) {\n\t\t\t\t\tlines.push(line);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn { lines, totalLines, truncated: totalLines > last };\n\t\t});\n\t}\n\n\t/**\n\t * Runs an operation under the run's signal and this workspace's time limit together. The\n\t * run's cancellation escapes unchanged; the time limit becomes an error the model can act on.\n\t */\n\tprivate async withTimeout<Result>(\n\t\tsignal: AbortSignal,\n\t\toperation: (bounded: AbortSignal) => Promise<Result>,\n\t): Promise<Result> {\n\t\tsignal.throwIfAborted();\n\t\tconst timeout = AbortSignal.timeout(this.timeoutMs);\n\t\ttry {\n\t\t\treturn await operation(AbortSignal.any([signal, timeout]));\n\t\t} catch (error) {\n\t\t\tsignal.throwIfAborted();\n\t\t\tif (timeout.aborted) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`The operation took longer than ${String(this.timeoutMs / 1000)} s; narrow the pattern, path or glob`,\n\t\t\t\t\t{ cause: error },\n\t\t\t\t);\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/** Regular files only: a symlink could point out of the root, and a deleted file is gone. */\n\tprivate async regularFiles(paths: string[]): Promise<string[]> {\n\t\tconst regular = await Promise.all(\n\t\t\tpaths.map(path =>\n\t\t\t\tlstat(join(this.root, path)).then(\n\t\t\t\t\tstats => stats.isFile(),\n\t\t\t\t\t() => false,\n\t\t\t\t),\n\t\t\t),\n\t\t);\n\n\t\treturn paths.filter((_, index) => regular[index]);\n\t}\n\n\tprivate inScope(path: string, { path: scope, glob }: { path?: string; glob?: string }): boolean {\n\t\tif (scope !== undefined) {\n\t\t\tconst folder = this.toRelative(scope);\n\t\t\tif (folder === undefined) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tif (folder !== '' && path !== folder && !path.startsWith(`${folder}/`)) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\treturn glob === undefined || matchesGlob(path, glob);\n\t}\n\n\t/** A path relative to the root with `/`, '' for the root itself, or undefined when it leaves it. */\n\tprivate toRelative(path: string): string | undefined {\n\t\tconst relativePath = relative(this.root, resolve(this.root, path));\n\t\tif (relativePath === '..' || relativePath.startsWith(`..${sep}`) || isAbsolute(relativePath)) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn relativePath.split(sep).join('/');\n\t}\n}\n\nasync function isBinary(path: string): Promise<boolean> {\n\tconst file = await open(path);\n\ttry {\n\t\tconst probe = Buffer.alloc(binaryProbeBytes);\n\t\tconst { bytesRead } = await file.read(probe, 0, probe.length, 0);\n\t\treturn probe.subarray(0, bytesRead).includes(0);\n\t} finally {\n\t\tawait file.close();\n\t}\n}\n"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ILogger } from '#src/shared/domain/logger';
|
|
2
|
+
import type { Workspace } from '../domain/workspace.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Picks how to read `root`: the ripgrep that ships with the package, or git when this platform
|
|
5
|
+
* has no ripgrep binary, which is logged because it is a degraded path. Plain `grep` is never a
|
|
6
|
+
* fallback: it knows nothing of `.gitignore`. See "`.gitignore` is the boundary of what an agent
|
|
7
|
+
* reads" in decisions.md.
|
|
8
|
+
*
|
|
9
|
+
* @throws {UnrecoverableError} when neither ripgrep nor a git repository is available.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createWorkspace({ root, logger, }: {
|
|
12
|
+
root: string;
|
|
13
|
+
logger: ILogger;
|
|
14
|
+
}): Promise<Workspace>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { UnrecoverableError } from '#src/agent/domain/errors';
|
|
2
|
+
import { classifyHostFailure, treatErrors } from '#src/agent/domain/providerFailure';
|
|
3
|
+
import { GitWorkspace } from "./gitWorkspace.js";
|
|
4
|
+
import { RipgrepWorkspace } from "./ripgrepWorkspace.js";
|
|
5
|
+
import { runProcess } from "./runProcess.js";
|
|
6
|
+
// Long enough for a slow disk; short enough that a hung git does not hold up building an agent.
|
|
7
|
+
const gitCheckTimeoutMs = 5_000;
|
|
8
|
+
async function loadRipgrep() {
|
|
9
|
+
try {
|
|
10
|
+
// Dynamic: the package throws on import when the platform has no binary.
|
|
11
|
+
const { rgPath } = await import('@vscode/ripgrep');
|
|
12
|
+
return rgPath;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async function isGitWorkTree(root) {
|
|
19
|
+
try {
|
|
20
|
+
const { stdout, exitCode } = await runProcess('git', ['rev-parse', '--is-inside-work-tree'], {
|
|
21
|
+
cwd: root,
|
|
22
|
+
signal: AbortSignal.timeout(gitCheckTimeoutMs),
|
|
23
|
+
});
|
|
24
|
+
return exitCode === 0 && stdout.trim() === 'true';
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
// No git at all, or one that never answered.
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Picks how to read `root`: the ripgrep that ships with the package, or git when this platform
|
|
33
|
+
* has no ripgrep binary, which is logged because it is a degraded path. Plain `grep` is never a
|
|
34
|
+
* fallback: it knows nothing of `.gitignore`. See "`.gitignore` is the boundary of what an agent
|
|
35
|
+
* reads" in decisions.md.
|
|
36
|
+
*
|
|
37
|
+
* @throws {UnrecoverableError} when neither ripgrep nor a git repository is available.
|
|
38
|
+
*/
|
|
39
|
+
export async function createWorkspace({ root, logger, }) {
|
|
40
|
+
const ripgrepPath = await loadRipgrep();
|
|
41
|
+
if (ripgrepPath !== undefined) {
|
|
42
|
+
return new RipgrepWorkspace({ root, ripgrepPath });
|
|
43
|
+
}
|
|
44
|
+
if (await isGitWorkTree(root)) {
|
|
45
|
+
treatErrors(() => {
|
|
46
|
+
logger.warn('ripgrep is not available on this platform; reading the repository through git');
|
|
47
|
+
}, classifyHostFailure, 'Workspace logger failed while choosing an implementation');
|
|
48
|
+
return new GitWorkspace({ root });
|
|
49
|
+
}
|
|
50
|
+
throw new UnrecoverableError('No program can read the workspace', {
|
|
51
|
+
cause: `ripgrep is not available on this platform, and ${root} is not a git repository.`,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=createWorkspace.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createWorkspace.js","sourceRoot":"","sources":["../../../src/tools/infrastructure/createWorkspace.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAGrF,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,gGAAgG;AAChG,MAAM,iBAAiB,GAAG,KAAK,CAAC;AAEhC,KAAK,UAAU,WAAW;IACzB,IAAI,CAAC;QACJ,yEAAyE;QACzE,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,MAAM,CAAC,iBAAiB,CAAC,CAAC;QACnD,OAAO,MAAM,CAAC;IACf,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,SAAS,CAAC;IAClB,CAAC;AACF,CAAC;AAED,KAAK,UAAU,aAAa,CAAC,IAAY;IACxC,IAAI,CAAC;QACJ,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,UAAU,CAAC,KAAK,EAAE,CAAC,WAAW,EAAE,uBAAuB,CAAC,EAAE;YAC5F,GAAG,EAAE,IAAI;YACT,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,iBAAiB,CAAC;SAC9C,CAAC,CAAC;QACH,OAAO,QAAQ,KAAK,CAAC,IAAI,MAAM,CAAC,IAAI,EAAE,KAAK,MAAM,CAAC;IACnD,CAAC;IAAC,MAAM,CAAC;QACR,6CAA6C;QAC7C,OAAO,KAAK,CAAC;IACd,CAAC;AACF,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,EACrC,IAAI,EACJ,MAAM,GAIN;IACA,MAAM,WAAW,GAAG,MAAM,WAAW,EAAE,CAAC;IACxC,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;QAC/B,OAAO,IAAI,gBAAgB,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC;IACpD,CAAC;IAED,IAAI,MAAM,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/B,WAAW,CACV,GAAG,EAAE;YACJ,MAAM,CAAC,IAAI,CACV,+EAA+E,CAC/E,CAAC;QACH,CAAC,EACD,mBAAmB,EACnB,0DAA0D,CAC1D,CAAC;QACF,OAAO,IAAI,YAAY,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;IACnC,CAAC;IAED,MAAM,IAAI,kBAAkB,CAAC,mCAAmC,EAAE;QACjE,KAAK,EAAE,kDAAkD,IAAI,2BAA2B;KACxF,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { UnrecoverableError } from '#src/agent/domain/errors';\nimport { classifyHostFailure, treatErrors } from '#src/agent/domain/providerFailure';\nimport type { ILogger } from '#src/shared/domain/logger';\nimport type { Workspace } from '../domain/workspace.ts';\nimport { GitWorkspace } from './gitWorkspace.ts';\nimport { RipgrepWorkspace } from './ripgrepWorkspace.ts';\nimport { runProcess } from './runProcess.ts';\n\n// Long enough for a slow disk; short enough that a hung git does not hold up building an agent.\nconst gitCheckTimeoutMs = 5_000;\n\nasync function loadRipgrep(): Promise<string | undefined> {\n\ttry {\n\t\t// Dynamic: the package throws on import when the platform has no binary.\n\t\tconst { rgPath } = await import('@vscode/ripgrep');\n\t\treturn rgPath;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nasync function isGitWorkTree(root: string): Promise<boolean> {\n\ttry {\n\t\tconst { stdout, exitCode } = await runProcess('git', ['rev-parse', '--is-inside-work-tree'], {\n\t\t\tcwd: root,\n\t\t\tsignal: AbortSignal.timeout(gitCheckTimeoutMs),\n\t\t});\n\t\treturn exitCode === 0 && stdout.trim() === 'true';\n\t} catch {\n\t\t// No git at all, or one that never answered.\n\t\treturn false;\n\t}\n}\n\n/**\n * Picks how to read `root`: the ripgrep that ships with the package, or git when this platform\n * has no ripgrep binary, which is logged because it is a degraded path. Plain `grep` is never a\n * fallback: it knows nothing of `.gitignore`. See \"`.gitignore` is the boundary of what an agent\n * reads\" in decisions.md.\n *\n * @throws {UnrecoverableError} when neither ripgrep nor a git repository is available.\n */\nexport async function createWorkspace({\n\troot,\n\tlogger,\n}: {\n\troot: string;\n\tlogger: ILogger;\n}): Promise<Workspace> {\n\tconst ripgrepPath = await loadRipgrep();\n\tif (ripgrepPath !== undefined) {\n\t\treturn new RipgrepWorkspace({ root, ripgrepPath });\n\t}\n\n\tif (await isGitWorkTree(root)) {\n\t\ttreatErrors(\n\t\t\t() => {\n\t\t\t\tlogger.warn(\n\t\t\t\t\t'ripgrep is not available on this platform; reading the repository through git',\n\t\t\t\t);\n\t\t\t},\n\t\t\tclassifyHostFailure,\n\t\t\t'Workspace logger failed while choosing an implementation',\n\t\t);\n\t\treturn new GitWorkspace({ root });\n\t}\n\n\tthrow new UnrecoverableError('No program can read the workspace', {\n\t\tcause: `ripgrep is not available on this platform, and ${root} is not a git repository.`,\n\t});\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { Tool } from '../domain/tool.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Builds a `Tool` from one Zod schema, which gives the model its JSON Schema, validates what the
|
|
5
|
+
* model sends, and types the input `execute` receives. Invalid input is rejected with a message
|
|
6
|
+
* naming what is wrong, which the agent hands back to the model as an error result.
|
|
7
|
+
*
|
|
8
|
+
* @throws {InvalidAgentConfigError} when the schema falls outside what strict mode supports.
|
|
9
|
+
*/
|
|
10
|
+
export declare function defineTool<Schema extends z.ZodObject>({ name, description, input, execute, }: {
|
|
11
|
+
name: string;
|
|
12
|
+
description: string;
|
|
13
|
+
input: Schema;
|
|
14
|
+
execute: (input: z.infer<Schema>, signal: AbortSignal) => Promise<string>;
|
|
15
|
+
}): Tool;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { InvalidAgentConfigError } from '#src/agent/domain/errors';
|
|
3
|
+
// Both providers take tools in strict mode, which supports only part of JSON Schema. Claude
|
|
4
|
+
// rejects these bounds; a tool that needs one checks it in its own code instead.
|
|
5
|
+
const unsupportedKeywords = [
|
|
6
|
+
'minimum',
|
|
7
|
+
'maximum',
|
|
8
|
+
'exclusiveMinimum',
|
|
9
|
+
'exclusiveMaximum',
|
|
10
|
+
'multipleOf',
|
|
11
|
+
'minLength',
|
|
12
|
+
'maxLength',
|
|
13
|
+
];
|
|
14
|
+
// Keywords whose value is one schema, or a list or map of them. Anything else, such as the
|
|
15
|
+
// keys of `properties`, is a name or a value, never a schema to check.
|
|
16
|
+
const schemaKeywords = ['items', 'not', 'additionalProperties'];
|
|
17
|
+
const schemaListKeywords = ['anyOf', 'oneOf', 'allOf', 'prefixItems'];
|
|
18
|
+
const schemaMapKeywords = ['properties', '$defs', 'definitions'];
|
|
19
|
+
function isSchema(value) {
|
|
20
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Walks the whole schema, nested objects included, and visits only schemas: a property named
|
|
24
|
+
* `maxLength` is a name, not the keyword. A schema the providers reject would fail every
|
|
25
|
+
* request with a 400, so it fails here, when the tool is defined.
|
|
26
|
+
*/
|
|
27
|
+
function assertStrict(schema, toolName) {
|
|
28
|
+
if (!isSchema(schema)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const keyword = unsupportedKeywords.find(candidate => candidate in schema);
|
|
32
|
+
if (keyword) {
|
|
33
|
+
throw new InvalidAgentConfigError(`Tool "${toolName}" uses "${keyword}", which strict mode does not support; check it in the tool's code`);
|
|
34
|
+
}
|
|
35
|
+
if (schema.type === 'object') {
|
|
36
|
+
// A record or a loose object takes keys strict mode cannot know in advance.
|
|
37
|
+
if (schema.additionalProperties !== false) {
|
|
38
|
+
throw new InvalidAgentConfigError(`Tool "${toolName}" has an object open to any key; strict mode needs every key declared`);
|
|
39
|
+
}
|
|
40
|
+
const required = (schema.required ?? []);
|
|
41
|
+
// OpenAI requires every property: an optional one is a union with `null`.
|
|
42
|
+
const optional = Object.keys(schema.properties ?? {}).filter(property => !required.includes(property));
|
|
43
|
+
if (optional.length > 0) {
|
|
44
|
+
throw new InvalidAgentConfigError(`Tool "${toolName}" has optional fields (${optional.join(', ')}); make them .nullable() instead`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
for (const key of schemaKeywords)
|
|
48
|
+
assertStrict(schema[key], toolName);
|
|
49
|
+
for (const key of schemaListKeywords) {
|
|
50
|
+
const list = schema[key];
|
|
51
|
+
if (Array.isArray(list))
|
|
52
|
+
for (const item of list)
|
|
53
|
+
assertStrict(item, toolName);
|
|
54
|
+
}
|
|
55
|
+
for (const key of schemaMapKeywords) {
|
|
56
|
+
const map = schema[key];
|
|
57
|
+
if (isSchema(map))
|
|
58
|
+
for (const item of Object.values(map))
|
|
59
|
+
assertStrict(item, toolName);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Builds a `Tool` from one Zod schema, which gives the model its JSON Schema, validates what the
|
|
64
|
+
* model sends, and types the input `execute` receives. Invalid input is rejected with a message
|
|
65
|
+
* naming what is wrong, which the agent hands back to the model as an error result.
|
|
66
|
+
*
|
|
67
|
+
* @throws {InvalidAgentConfigError} when the schema falls outside what strict mode supports.
|
|
68
|
+
*/
|
|
69
|
+
export function defineTool({ name, description, input, execute, }) {
|
|
70
|
+
const inputSchema = { ...z.toJSONSchema(input) };
|
|
71
|
+
// The dialect marker is noise to both APIs.
|
|
72
|
+
delete inputSchema.$schema;
|
|
73
|
+
assertStrict(inputSchema, name);
|
|
74
|
+
return {
|
|
75
|
+
name,
|
|
76
|
+
description,
|
|
77
|
+
inputSchema: inputSchema,
|
|
78
|
+
execute: (raw, signal) => {
|
|
79
|
+
const parsed = input.safeParse(raw);
|
|
80
|
+
if (!parsed.success) {
|
|
81
|
+
return Promise.reject(new Error(z.prettifyError(parsed.error)));
|
|
82
|
+
}
|
|
83
|
+
return execute(parsed.data, signal);
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=defineTool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineTool.js","sourceRoot":"","sources":["../../../src/tools/infrastructure/defineTool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,uBAAuB,EAAE,MAAM,0BAA0B,CAAC;AAInE,4FAA4F;AAC5F,iFAAiF;AACjF,MAAM,mBAAmB,GAAG;IAC3B,SAAS;IACT,SAAS;IACT,kBAAkB;IAClB,kBAAkB;IAClB,YAAY;IACZ,WAAW;IACX,WAAW;CACX,CAAC;AAEF,2FAA2F;AAC3F,uEAAuE;AACvE,MAAM,cAAc,GAAG,CAAC,OAAO,EAAE,KAAK,EAAE,sBAAsB,CAAC,CAAC;AAChE,MAAM,kBAAkB,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC;AACtE,MAAM,iBAAiB,GAAG,CAAC,YAAY,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC;AAEjE,SAAS,Q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AO,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACrC,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import { z } from 'zod';\nimport { InvalidAgentConfigError } from '#src/agent/domain/errors';\nimport type { JSONSchema } from '#src/llm/domain/tool';\nimport type { Tool } from '../domain/tool.ts';\n\n// Both providers take tools in strict mode, which supports only part of JSON Schema. Claude\n// rejects these bounds; a tool that needs one checks it in its own code instead.\nconst unsupportedKeywords = [\n\t'minimum',\n\t'maximum',\n\t'exclusiveMinimum',\n\t'exclusiveMaximum',\n\t'multipleOf',\n\t'minLength',\n\t'maxLength',\n];\n\n// Keywords whose value is one schema, or a list or map of them. Anything else, such as the\n// keys of `properties`, is a name or a value, never a schema to check.\nconst schemaKeywords = ['items', 'not', 'additionalProperties'];\nconst schemaListKeywords = ['anyOf', 'oneOf', 'allOf', 'prefixItems'];\nconst schemaMapKeywords = ['properties', '$defs', 'definitions'];\n\nfunction isSchema(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/**\n * Walks the whole schema, nested objects included, and visits only schemas: a property named\n * `maxLength` is a name, not the keyword. A schema the providers reject would fail every\n * request with a 400, so it fails here, when the tool is defined.\n */\nfunction assertStrict(schema: unknown, toolName: string): void {\n\tif (!isSchema(schema)) {\n\t\treturn;\n\t}\n\n\tconst keyword = unsupportedKeywords.find(candidate => candidate in schema);\n\tif (keyword) {\n\t\tthrow new InvalidAgentConfigError(\n\t\t\t`Tool \"${toolName}\" uses \"${keyword}\", which strict mode does not support; check it in the tool's code`,\n\t\t);\n\t}\n\n\tif (schema.type === 'object') {\n\t\t// A record or a loose object takes keys strict mode cannot know in advance.\n\t\tif (schema.additionalProperties !== false) {\n\t\t\tthrow new InvalidAgentConfigError(\n\t\t\t\t`Tool \"${toolName}\" has an object open to any key; strict mode needs every key declared`,\n\t\t\t);\n\t\t}\n\n\t\tconst required = (schema.required ?? []) as string[];\n\t\t// OpenAI requires every property: an optional one is a union with `null`.\n\t\tconst optional = Object.keys(schema.properties ?? {}).filter(\n\t\t\tproperty => !required.includes(property),\n\t\t);\n\t\tif (optional.length > 0) {\n\t\t\tthrow new InvalidAgentConfigError(\n\t\t\t\t`Tool \"${toolName}\" has optional fields (${optional.join(', ')}); make them .nullable() instead`,\n\t\t\t);\n\t\t}\n\t}\n\n\tfor (const key of schemaKeywords) assertStrict(schema[key], toolName);\n\tfor (const key of schemaListKeywords) {\n\t\tconst list = schema[key];\n\t\tif (Array.isArray(list)) for (const item of list) assertStrict(item, toolName);\n\t}\n\tfor (const key of schemaMapKeywords) {\n\t\tconst map = schema[key];\n\t\tif (isSchema(map)) for (const item of Object.values(map)) assertStrict(item, toolName);\n\t}\n}\n\n/**\n * Builds a `Tool` from one Zod schema, which gives the model its JSON Schema, validates what the\n * model sends, and types the input `execute` receives. Invalid input is rejected with a message\n * naming what is wrong, which the agent hands back to the model as an error result.\n *\n * @throws {InvalidAgentConfigError} when the schema falls outside what strict mode supports.\n */\nexport function defineTool<Schema extends z.ZodObject>({\n\tname,\n\tdescription,\n\tinput,\n\texecute,\n}: {\n\tname: string;\n\tdescription: string;\n\tinput: Schema;\n\texecute: (input: z.infer<Schema>, signal: AbortSignal) => Promise<string>;\n}): Tool {\n\tconst inputSchema: Record<string, unknown> = { ...z.toJSONSchema(input) };\n\t// The dialect marker is noise to both APIs.\n\tdelete inputSchema.$schema;\n\tassertStrict(inputSchema, name);\n\n\treturn {\n\t\tname,\n\t\tdescription,\n\t\tinputSchema: inputSchema as JSONSchema,\n\t\texecute: (raw, signal) => {\n\t\t\tconst parsed = input.safeParse(raw);\n\t\t\tif (!parsed.success) {\n\t\t\t\treturn Promise.reject(new Error(z.prettifyError(parsed.error)));\n\t\t\t}\n\n\t\t\treturn execute(parsed.data, signal);\n\t\t},\n\t};\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { TextMatch } from '../domain/workspace.ts';
|
|
2
|
+
import { BoundedWorkspace } from './boundedWorkspace.ts';
|
|
3
|
+
/**
|
|
4
|
+
* The fallback `Workspace`, on the git the repository already has. git applies `.gitignore`
|
|
5
|
+
* by itself, even to an explicit path, but only inside a repository: outside one, every
|
|
6
|
+
* operation fails.
|
|
7
|
+
*/
|
|
8
|
+
export declare class GitWorkspace extends BoundedWorkspace {
|
|
9
|
+
protected listCandidates(signal: AbortSignal): Promise<string[]>;
|
|
10
|
+
protected findMatches(pattern: string, ignoreCase: boolean, signal: AbortSignal, keep: (match: TextMatch) => void): Promise<void>;
|
|
11
|
+
private listFiles0;
|
|
12
|
+
private git;
|
|
13
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { BoundedWorkspace } from "./boundedWorkspace.js";
|
|
2
|
+
import { firstLine, runProcess } from "./runProcess.js";
|
|
3
|
+
/**
|
|
4
|
+
* The fallback `Workspace`, on the git the repository already has. git applies `.gitignore`
|
|
5
|
+
* by itself, even to an explicit path, but only inside a repository: outside one, every
|
|
6
|
+
* operation fails.
|
|
7
|
+
*/
|
|
8
|
+
export class GitWorkspace extends BoundedWorkspace {
|
|
9
|
+
async listCandidates(signal) {
|
|
10
|
+
// Tracked files and new ones not yet added; then the tracked ones `.gitignore` matches
|
|
11
|
+
// anyway, such as a `.env` committed before it was ignored. ripgrep hides those, and the
|
|
12
|
+
// fallback must not show what the preferred implementation hides.
|
|
13
|
+
const [listed, ignored] = await Promise.all([
|
|
14
|
+
this.listFiles0(['--cached', '--others', '--exclude-standard'], signal),
|
|
15
|
+
this.listFiles0(['--cached', '--ignored', '--exclude-standard'], signal),
|
|
16
|
+
]);
|
|
17
|
+
const hidden = new Set(ignored);
|
|
18
|
+
return listed.filter(path => !hidden.has(path));
|
|
19
|
+
}
|
|
20
|
+
async findMatches(pattern, ignoreCase, signal, keep) {
|
|
21
|
+
const { stderr, exitCode } = await this.git([
|
|
22
|
+
'grep',
|
|
23
|
+
// Untracked files too, or the agent could not search a file it has just created.
|
|
24
|
+
'--untracked',
|
|
25
|
+
// Binary files left out, as ripgrep does.
|
|
26
|
+
'-I',
|
|
27
|
+
'--line-number',
|
|
28
|
+
// path NUL line NUL text, so a colon in a path cannot split it.
|
|
29
|
+
'-z',
|
|
30
|
+
// Perl syntax, so `\d` and `\b` mean what they mean to ripgrep.
|
|
31
|
+
'-P',
|
|
32
|
+
...(ignoreCase ? ['--ignore-case'] : []),
|
|
33
|
+
// `-e`, so a pattern starting with a dash is never read as an option.
|
|
34
|
+
'-e',
|
|
35
|
+
pattern,
|
|
36
|
+
], signal, line => {
|
|
37
|
+
const [path, number, ...text] = line.split('\0');
|
|
38
|
+
if (path && number) {
|
|
39
|
+
keep({ path, line: Number(number), text: text.join('\0').replace(/\r$/, '') });
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
// 1 means no match; anything above is a failure, such as a pattern that does not compile.
|
|
43
|
+
if (exitCode > 1) {
|
|
44
|
+
throw new Error(`git could not search for the pattern: ${firstLine(stderr)}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async listFiles0(options, signal) {
|
|
48
|
+
const { stdout, stderr, exitCode } = await this.git(['ls-files', '-z', ...options], signal);
|
|
49
|
+
if (exitCode !== 0) {
|
|
50
|
+
throw new Error(`git could not list the files: ${firstLine(stderr)}`);
|
|
51
|
+
}
|
|
52
|
+
return stdout.split('\0').filter(path => path !== '');
|
|
53
|
+
}
|
|
54
|
+
git(args, signal, onLine) {
|
|
55
|
+
// A user's settings must not change the output this parses: colour codes would end up in
|
|
56
|
+
// the text, `grep.column` adds a field, and `grep.fullName` makes paths relative to the
|
|
57
|
+
// repository instead of the root.
|
|
58
|
+
return runProcess('git', ['-c', 'color.ui=never', '-c', 'grep.column=false', '-c', 'grep.fullName=false', ...args], { cwd: this.root, signal, onLine });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=gitWorkspace.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gitW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AAE,GAAG,IAAI,CAAC,EACzF,EAAE,GAAG,EAAE,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAClC,CAAC;IACH,CAAC;CACD","sourcesContent":["import type { TextMatch } from '../domain/workspace.ts';\nimport { BoundedWorkspace } from './boundedWorkspace.ts';\nimport { firstLine, runProcess } from './runProcess.ts';\n\n/**\n * The fallback `Workspace`, on the git the repository already has. git applies `.gitignore`\n * by itself, even to an explicit path, but only inside a repository: outside one, every\n * operation fails.\n */\nexport class GitWorkspace extends BoundedWorkspace {\n\tprotected async listCandidates(signal: AbortSignal): Promise<string[]> {\n\t\t// Tracked files and new ones not yet added; then the tracked ones `.gitignore` matches\n\t\t// anyway, such as a `.env` committed before it was ignored. ripgrep hides those, and the\n\t\t// fallback must not show what the preferred implementation hides.\n\t\tconst [listed, ignored] = await Promise.all([\n\t\t\tthis.listFiles0(['--cached', '--others', '--exclude-standard'], signal),\n\t\t\tthis.listFiles0(['--cached', '--ignored', '--exclude-standard'], signal),\n\t\t]);\n\t\tconst hidden = new Set(ignored);\n\n\t\treturn listed.filter(path => !hidden.has(path));\n\t}\n\n\tprotected async findMatches(\n\t\tpattern: string,\n\t\tignoreCase: boolean,\n\t\tsignal: AbortSignal,\n\t\tkeep: (match: TextMatch) => void,\n\t): Promise<void> {\n\t\tconst { stderr, exitCode } = await this.git(\n\t\t\t[\n\t\t\t\t'grep',\n\t\t\t\t// Untracked files too, or the agent could not search a file it has just created.\n\t\t\t\t'--untracked',\n\t\t\t\t// Binary files left out, as ripgrep does.\n\t\t\t\t'-I',\n\t\t\t\t'--line-number',\n\t\t\t\t// path NUL line NUL text, so a colon in a path cannot split it.\n\t\t\t\t'-z',\n\t\t\t\t// Perl syntax, so `\\d` and `\\b` mean what they mean to ripgrep.\n\t\t\t\t'-P',\n\t\t\t\t...(ignoreCase ? ['--ignore-case'] : []),\n\t\t\t\t// `-e`, so a pattern starting with a dash is never read as an option.\n\t\t\t\t'-e',\n\t\t\t\tpattern,\n\t\t\t],\n\t\t\tsignal,\n\t\t\tline => {\n\t\t\t\tconst [path, number, ...text] = line.split('\\0');\n\t\t\t\tif (path && number) {\n\t\t\t\t\tkeep({ path, line: Number(number), text: text.join('\\0').replace(/\\r$/, '') });\n\t\t\t\t}\n\t\t\t},\n\t\t);\n\t\t// 1 means no match; anything above is a failure, such as a pattern that does not compile.\n\t\tif (exitCode > 1) {\n\t\t\tthrow new Error(`git could not search for the pattern: ${firstLine(stderr)}`);\n\t\t}\n\t}\n\n\tprivate async listFiles0(options: string[], signal: AbortSignal): Promise<string[]> {\n\t\tconst { stdout, stderr, exitCode } = await this.git(['ls-files', '-z', ...options], signal);\n\t\tif (exitCode !== 0) {\n\t\t\tthrow new Error(`git could not list the files: ${firstLine(stderr)}`);\n\t\t}\n\n\t\treturn stdout.split('\\0').filter(path => path !== '');\n\t}\n\n\tprivate git(args: string[], signal: AbortSignal, onLine?: (line: string) => void) {\n\t\t// A user's settings must not change the output this parses: colour codes would end up in\n\t\t// the text, `grep.column` adds a field, and `grep.fullName` makes paths relative to the\n\t\t// repository instead of the root.\n\t\treturn runProcess(\n\t\t\t'git',\n\t\t\t['-c', 'color.ui=never', '-c', 'grep.column=false', '-c', 'grep.fullName=false', ...args],\n\t\t\t{ cwd: this.root, signal, onLine },\n\t\t);\n\t}\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { TextMatch } from '../domain/workspace.ts';
|
|
2
|
+
import { BoundedWorkspace } from './boundedWorkspace.ts';
|
|
3
|
+
/**
|
|
4
|
+
* The preferred `Workspace`, on the ripgrep binary that ships with the package. It always runs
|
|
5
|
+
* from the root with no path: ripgrep ignores nothing that it is given explicitly.
|
|
6
|
+
*
|
|
7
|
+
* It takes the binary's path instead of importing `@vscode/ripgrep`, which throws on import when
|
|
8
|
+
* the platform has no binary; `createWorkspace` loads it where that failure can be handled.
|
|
9
|
+
*/
|
|
10
|
+
export declare class RipgrepWorkspace extends BoundedWorkspace {
|
|
11
|
+
private readonly ripgrepPath;
|
|
12
|
+
constructor({ ripgrepPath, ...options }: {
|
|
13
|
+
root: string;
|
|
14
|
+
ripgrepPath: string;
|
|
15
|
+
timeoutMs?: number;
|
|
16
|
+
});
|
|
17
|
+
protected listCandidates(signal: AbortSignal): Promise<string[]>;
|
|
18
|
+
protected findMatches(pattern: string, ignoreCase: boolean, signal: AbortSignal, keep: (match: TextMatch) => void): Promise<void>;
|
|
19
|
+
private ripgrep;
|
|
20
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { BoundedWorkspace } from "./boundedWorkspace.js";
|
|
2
|
+
import { firstLine, runProcess } from "./runProcess.js";
|
|
3
|
+
// Every call sees what git would. Each flag is ours; nothing a model writes ever reaches the
|
|
4
|
+
// command line except the pattern, and that only after `-e`.
|
|
5
|
+
const sameViewAsGit = [
|
|
6
|
+
// A user's ripgrep configuration could switch the ignore rules off.
|
|
7
|
+
'--no-config',
|
|
8
|
+
// ripgrep's own `.ignore` and `.rgignore` outrank `.gitignore`, so a `!.env` line in either
|
|
9
|
+
// would show a secret git hides. Only git's ignore sources count.
|
|
10
|
+
'--no-ignore-dot',
|
|
11
|
+
// Hidden files such as `.github/` are part of the repository; ripgrep skips them by default.
|
|
12
|
+
'--hidden',
|
|
13
|
+
// `.gitignore` applies even outside a git repository.
|
|
14
|
+
'--no-require-git',
|
|
15
|
+
// `--hidden` would otherwise search git's own folder.
|
|
16
|
+
'--glob',
|
|
17
|
+
'!.git',
|
|
18
|
+
// Windows would print `\`; every path in a `Workspace` is written with `/`.
|
|
19
|
+
'--path-separator',
|
|
20
|
+
'/',
|
|
21
|
+
];
|
|
22
|
+
function decode({ text, bytes }) {
|
|
23
|
+
return text ?? Buffer.from(bytes ?? '', 'base64').toString('utf8');
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The preferred `Workspace`, on the ripgrep binary that ships with the package. It always runs
|
|
27
|
+
* from the root with no path: ripgrep ignores nothing that it is given explicitly.
|
|
28
|
+
*
|
|
29
|
+
* It takes the binary's path instead of importing `@vscode/ripgrep`, which throws on import when
|
|
30
|
+
* the platform has no binary; `createWorkspace` loads it where that failure can be handled.
|
|
31
|
+
*/
|
|
32
|
+
export class RipgrepWorkspace extends BoundedWorkspace {
|
|
33
|
+
ripgrepPath;
|
|
34
|
+
constructor({ ripgrepPath, ...options }) {
|
|
35
|
+
super(options);
|
|
36
|
+
this.ripgrepPath = ripgrepPath;
|
|
37
|
+
}
|
|
38
|
+
async listCandidates(signal) {
|
|
39
|
+
const { stdout, stderr, exitCode } = await this.ripgrep(['--files', '--null'], signal);
|
|
40
|
+
// 1 means nothing to list, which an empty repository is. 2 with a listing means some
|
|
41
|
+
// folder could not be read, which git skips with a warning too; without one, it failed.
|
|
42
|
+
if (exitCode > 1 && stdout === '') {
|
|
43
|
+
throw new Error(`ripgrep could not list the files: ${firstLine(stderr)}`);
|
|
44
|
+
}
|
|
45
|
+
return stdout
|
|
46
|
+
.split('\0')
|
|
47
|
+
.filter(path => path !== '')
|
|
48
|
+
.map(path => path.replace(/^\.\//, ''));
|
|
49
|
+
}
|
|
50
|
+
async findMatches(pattern, ignoreCase, signal, keep) {
|
|
51
|
+
// ripgrep ends every search it ran with a summary. A failure before searching, such as a
|
|
52
|
+
// pattern that does not compile, prints none. A count, not a flag: TypeScript cannot see
|
|
53
|
+
// the callback change it.
|
|
54
|
+
let summaries = 0;
|
|
55
|
+
const { stderr, exitCode } = await this.ripgrep(['--json', ...(ignoreCase ? ['--ignore-case'] : []), '-e', pattern], signal, line => {
|
|
56
|
+
if (line === '') {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const event = JSON.parse(line);
|
|
60
|
+
if (event.type === 'summary') {
|
|
61
|
+
summaries++;
|
|
62
|
+
}
|
|
63
|
+
if (event.type === 'match') {
|
|
64
|
+
keep({
|
|
65
|
+
path: decode(event.data.path).replace(/^\.\//, ''),
|
|
66
|
+
line: event.data.line_number,
|
|
67
|
+
text: decode(event.data.lines).replace(/\r?\n$/, ''),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
// 1 means no match. 2 after a search means some file could not be read, and the rest
|
|
72
|
+
// stands, as it does on git; 2 with no search is the failure.
|
|
73
|
+
if (exitCode > 1 && summaries === 0) {
|
|
74
|
+
throw new Error(`ripgrep could not search for the pattern: ${firstLine(stderr)}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
ripgrep(args, signal, onLine) {
|
|
78
|
+
return runProcess(this.ripgrepPath, [...sameViewAsGit, ...args], {
|
|
79
|
+
cwd: this.root,
|
|
80
|
+
signal,
|
|
81
|
+
onLine,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=ripgrepWorkspace.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ripgrepWorkspace.js","sourceRoot":"","sources":["../../../src/tools/infrastructure/ripgrepWorkspace.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAExD,6FAA6F;AAC7F,6DAA6D;AAC7D,MAAM,aAAa,GAAG;IACrB,oEAAoE;IACpE,aAAa;IACb,4FAA4F;IAC5F,kEAAkE;IAClE,iBAAiB;IACjB,6FAA6F;IAC7F,UAAU;IACV,sDAAsD;IACtD,kBAAkB;IAClB,sDAAsD;IACtD,QAAQ;IACR,OAAO;IACP,4EAA4E;IAC5E,kBAAkB;IAClB,GAAG;CACH,CAAC;AAaF,SAAS,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,EAAe;IAC3C,OAAO,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AACpE,CAAC;AAED;;;;;;GAMG;AACH,M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;gBACjB,OAAO;YACR,CAAC;YACD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAiB,CAAC;YAC/C,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;gBAC9B,SAAS,EAAE,CAAC;YACb,CAAC;YACD,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;gBAC5B,IAAI,CAAC;oBACJ,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;oBAClD,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,WAAW;oBAC5B,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC;iBACpD,CAAC,CAAC;YACJ,CAAC;QACF,CAAC,CACD,CAAC;QAEF,qFAAqF;QACrF,8DAA8D;QAC9D,IAAI,QAAQ,GAAG,CAAC,IAAI,SAAS,KAAK,CAAC,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CAAC,6CAA6C,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QACnF,CAAC;IACF,CAAC;IAEO,OAAO,CAAC,IAAc,EAAE,MAAmB,EAAE,MAA+B;QACnF,OAAO,UAAU,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,GAAG,aAAa,EAAE,GAAG,IAAI,CAAC,EAAE;YAChE,GAAG,EAAE,IAAI,CAAC,IAAI;YACd,MAAM;YACN,MAAM;SACN,CAAC,CAAC;IACJ,CAAC;CACD","sourcesContent":["import type { TextMatch } from '../domain/workspace.ts';\nimport { BoundedWorkspace } from './boundedWorkspace.ts';\nimport { firstLine, runProcess } from './runProcess.ts';\n\n// Every call sees what git would. Each flag is ours; nothing a model writes ever reaches the\n// command line except the pattern, and that only after `-e`.\nconst sameViewAsGit = [\n\t// A user's ripgrep configuration could switch the ignore rules off.\n\t'--no-config',\n\t// ripgrep's own `.ignore` and `.rgignore` outrank `.gitignore`, so a `!.env` line in either\n\t// would show a secret git hides. Only git's ignore sources count.\n\t'--no-ignore-dot',\n\t// Hidden files such as `.github/` are part of the repository; ripgrep skips them by default.\n\t'--hidden',\n\t// `.gitignore` applies even outside a git repository.\n\t'--no-require-git',\n\t// `--hidden` would otherwise search git's own folder.\n\t'--glob',\n\t'!.git',\n\t// Windows would print `\\`; every path in a `Workspace` is written with `/`.\n\t'--path-separator',\n\t'/',\n];\n\ninterface RipgrepText {\n\ttext?: string;\n\t// ripgrep sends what is not valid UTF-8 as base64 instead.\n\tbytes?: string;\n}\n\ninterface RipgrepEvent {\n\ttype: string;\n\tdata: { path: RipgrepText; line_number: number; lines: RipgrepText };\n}\n\nfunction decode({ text, bytes }: RipgrepText): string {\n\treturn text ?? Buffer.from(bytes ?? '', 'base64').toString('utf8');\n}\n\n/**\n * The preferred `Workspace`, on the ripgrep binary that ships with the package. It always runs\n * from the root with no path: ripgrep ignores nothing that it is given explicitly.\n *\n * It takes the binary's path instead of importing `@vscode/ripgrep`, which throws on import when\n * the platform has no binary; `createWorkspace` loads it where that failure can be handled.\n */\nexport class RipgrepWorkspace extends BoundedWorkspace {\n\tprivate readonly ripgrepPath: string;\n\n\tconstructor({\n\t\tripgrepPath,\n\t\t...options\n\t}: {\n\t\troot: string;\n\t\tripgrepPath: string;\n\t\ttimeoutMs?: number;\n\t}) {\n\t\tsuper(options);\n\t\tthis.ripgrepPath = ripgrepPath;\n\t}\n\n\tprotected async listCandidates(signal: AbortSignal): Promise<string[]> {\n\t\tconst { stdout, stderr, exitCode } = await this.ripgrep(['--files', '--null'], signal);\n\t\t// 1 means nothing to list, which an empty repository is. 2 with a listing means some\n\t\t// folder could not be read, which git skips with a warning too; without one, it failed.\n\t\tif (exitCode > 1 && stdout === '') {\n\t\t\tthrow new Error(`ripgrep could not list the files: ${firstLine(stderr)}`);\n\t\t}\n\n\t\treturn stdout\n\t\t\t.split('\\0')\n\t\t\t.filter(path => path !== '')\n\t\t\t.map(path => path.replace(/^\\.\\//, ''));\n\t}\n\n\tprotected async findMatches(\n\t\tpattern: string,\n\t\tignoreCase: boolean,\n\t\tsignal: AbortSignal,\n\t\tkeep: (match: TextMatch) => void,\n\t): Promise<void> {\n\t\t// ripgrep ends every search it ran with a summary. A failure before searching, such as a\n\t\t// pattern that does not compile, prints none. A count, not a flag: TypeScript cannot see\n\t\t// the callback change it.\n\t\tlet summaries = 0;\n\t\tconst { stderr, exitCode } = await this.ripgrep(\n\t\t\t['--json', ...(ignoreCase ? ['--ignore-case'] : []), '-e', pattern],\n\t\t\tsignal,\n\t\t\tline => {\n\t\t\t\tif (line === '') {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst event = JSON.parse(line) as RipgrepEvent;\n\t\t\t\tif (event.type === 'summary') {\n\t\t\t\t\tsummaries++;\n\t\t\t\t}\n\t\t\t\tif (event.type === 'match') {\n\t\t\t\t\tkeep({\n\t\t\t\t\t\tpath: decode(event.data.path).replace(/^\\.\\//, ''),\n\t\t\t\t\t\tline: event.data.line_number,\n\t\t\t\t\t\ttext: decode(event.data.lines).replace(/\\r?\\n$/, ''),\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t);\n\n\t\t// 1 means no match. 2 after a search means some file could not be read, and the rest\n\t\t// stands, as it does on git; 2 with no search is the failure.\n\t\tif (exitCode > 1 && summaries === 0) {\n\t\t\tthrow new Error(`ripgrep could not search for the pattern: ${firstLine(stderr)}`);\n\t\t}\n\t}\n\n\tprivate ripgrep(args: string[], signal: AbortSignal, onLine?: (line: string) => void) {\n\t\treturn runProcess(this.ripgrepPath, [...sameViewAsGit, ...args], {\n\t\t\tcwd: this.root,\n\t\t\tsignal,\n\t\t\tonLine,\n\t\t});\n\t}\n}\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface ProcessResult {
|
|
2
|
+
/** Empty when the output went to `onLine` instead. */
|
|
3
|
+
stdout: string;
|
|
4
|
+
stderr: string;
|
|
5
|
+
exitCode: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Runs a program with its arguments as an array, never through a shell, so nothing a model
|
|
9
|
+
* writes can become a command. Standard input is closed: ripgrep given no path searches its
|
|
10
|
+
* standard input whenever that is a pipe. A cancelled signal kills the process and rejects
|
|
11
|
+
* with the signal's `AbortError`.
|
|
12
|
+
*
|
|
13
|
+
* With `onLine`, each line of the output is handed over as it arrives and nothing is kept, so
|
|
14
|
+
* the caller decides what to hold; only a single line is then bound by the size limit. An
|
|
15
|
+
* error thrown by `onLine` stops the program and rejects with that error.
|
|
16
|
+
*/
|
|
17
|
+
export declare function runProcess(command: string, args: string[], { cwd, signal, onLine }: {
|
|
18
|
+
cwd: string;
|
|
19
|
+
signal: AbortSignal;
|
|
20
|
+
onLine?: (line: string) => void;
|
|
21
|
+
}): Promise<ProcessResult>;
|
|
22
|
+
/** The first line of a program's error output, which is the part a model can act on. */
|
|
23
|
+
export declare function firstLine(text: string): string;
|