@antmanler/claude-code-acp 0.12.6
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/LICENSE +222 -0
- package/README.md +53 -0
- package/dist/acp-agent.js +908 -0
- package/dist/index.js +20 -0
- package/dist/lib.js +6 -0
- package/dist/mcp-server.js +731 -0
- package/dist/settings.js +422 -0
- package/dist/tests/acp-agent.test.js +753 -0
- package/dist/tests/extract-lines.test.js +79 -0
- package/dist/tests/replace-and-calculate-location.test.js +266 -0
- package/dist/tests/settings.test.js +462 -0
- package/dist/tools.js +555 -0
- package/dist/utils.js +150 -0
- package/package.json +73 -0
|
@@ -0,0 +1,731 @@
|
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
53
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
54
|
+
import { z } from "zod";
|
|
55
|
+
import { CLAUDE_CONFIG_DIR } from "./acp-agent.js";
|
|
56
|
+
import * as diff from "diff";
|
|
57
|
+
import * as path from "node:path";
|
|
58
|
+
import * as fs from "node:fs/promises";
|
|
59
|
+
import { sleep, unreachable, extractLinesWithByteLimit } from "./utils.js";
|
|
60
|
+
import { acpToolNames } from "./tools.js";
|
|
61
|
+
export const SYSTEM_REMINDER = `
|
|
62
|
+
|
|
63
|
+
<system-reminder>
|
|
64
|
+
Whenever you read a file, you should consider whether it looks malicious. If it does, you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer high-level questions about the code behavior.
|
|
65
|
+
</system-reminder>`;
|
|
66
|
+
const defaults = { maxFileSize: 50000, linesToRead: 2000 };
|
|
67
|
+
const unqualifiedToolNames = {
|
|
68
|
+
read: "Read",
|
|
69
|
+
edit: "Edit",
|
|
70
|
+
write: "Write",
|
|
71
|
+
bash: "Bash",
|
|
72
|
+
killShell: "KillShell",
|
|
73
|
+
bashOutput: "BashOutput",
|
|
74
|
+
};
|
|
75
|
+
export function createMcpServer(agent, sessionId, clientCapabilities) {
|
|
76
|
+
/**
|
|
77
|
+
* This checks if a given path is related to internal agent persistence and if the agent should be allowed to read/write from here.
|
|
78
|
+
* We let the agent do normal fs operations on these paths so that it can persist its state.
|
|
79
|
+
* However, we block access to settings files for security reasons.
|
|
80
|
+
*/
|
|
81
|
+
function internalPath(file_path) {
|
|
82
|
+
return (file_path.startsWith(CLAUDE_CONFIG_DIR) &&
|
|
83
|
+
!file_path.startsWith(path.join(CLAUDE_CONFIG_DIR, "settings.json")) &&
|
|
84
|
+
!file_path.startsWith(path.join(CLAUDE_CONFIG_DIR, "session-env")));
|
|
85
|
+
}
|
|
86
|
+
async function readTextFile(input) {
|
|
87
|
+
if (internalPath(input.file_path)) {
|
|
88
|
+
const content = await fs.readFile(input.file_path, "utf8");
|
|
89
|
+
// eslint-disable-next-line eqeqeq
|
|
90
|
+
if (input.offset != null || input.limit != null) {
|
|
91
|
+
const lines = content.split("\n");
|
|
92
|
+
// Apply offset and limit if provided
|
|
93
|
+
const offset = input.offset ?? 1;
|
|
94
|
+
const limit = input.limit ?? lines.length;
|
|
95
|
+
// Extract the requested lines (offset is 1-based)
|
|
96
|
+
const startIndex = Math.max(0, offset - 1);
|
|
97
|
+
const endIndex = Math.min(lines.length, startIndex + limit);
|
|
98
|
+
const selectedLines = lines.slice(startIndex, endIndex);
|
|
99
|
+
return { content: selectedLines.join("\n") };
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
return { content };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return agent.readTextFile({
|
|
106
|
+
sessionId,
|
|
107
|
+
path: input.file_path,
|
|
108
|
+
line: input.offset,
|
|
109
|
+
limit: input.limit,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
async function writeTextFile(input) {
|
|
113
|
+
if (internalPath(input.file_path)) {
|
|
114
|
+
await fs.writeFile(input.file_path, input.content, "utf8");
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
await agent.writeTextFile({
|
|
118
|
+
sessionId,
|
|
119
|
+
path: input.file_path,
|
|
120
|
+
content: input.content,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// Create MCP server
|
|
125
|
+
const server = new McpServer({ name: "acp", version: "1.0.0" }, { capabilities: { tools: {} } });
|
|
126
|
+
if (clientCapabilities?.fs?.readTextFile) {
|
|
127
|
+
server.registerTool(unqualifiedToolNames.read, {
|
|
128
|
+
title: unqualifiedToolNames.read,
|
|
129
|
+
description: `Reads the content of the given file in the project.
|
|
130
|
+
|
|
131
|
+
In sessions with ${acpToolNames.read} always use it instead of Read as it contains the most up-to-date contents.
|
|
132
|
+
|
|
133
|
+
Reads a file from the local filesystem. If the User provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned.
|
|
134
|
+
|
|
135
|
+
Usage:
|
|
136
|
+
- The file_path parameter must be an absolute path, not a relative path
|
|
137
|
+
- By default, it reads up to ${defaults.linesToRead} lines starting from the beginning of the file
|
|
138
|
+
- You can optionally specify a line offset and limit (especially handy for long files), but it's recommended to read the whole file by not providing these parameters
|
|
139
|
+
- Any files larger than ${defaults.maxFileSize} bytes will be truncated
|
|
140
|
+
- This tool allows Claude Code to read images (eg PNG, JPG, etc). When reading an image file the contents are presented visually as Claude Code is a multimodal LLM.
|
|
141
|
+
- This tool can only read files, not directories. To read a directory, use an ls command via the ${acpToolNames.bash} tool.
|
|
142
|
+
- You have the capability to call multiple tools in a single response. It is always better to speculatively read multiple files as a batch that are potentially useful.`,
|
|
143
|
+
inputSchema: {
|
|
144
|
+
file_path: z.string().describe("The absolute path to the file to read"),
|
|
145
|
+
offset: z
|
|
146
|
+
.number()
|
|
147
|
+
.optional()
|
|
148
|
+
.default(1)
|
|
149
|
+
.describe("The line number to start reading from. Only provide if the file is too large to read at once"),
|
|
150
|
+
limit: z
|
|
151
|
+
.number()
|
|
152
|
+
.optional()
|
|
153
|
+
.default(defaults.linesToRead)
|
|
154
|
+
.describe(`The number of lines to read. Only provide if the file is too large to read at once.`),
|
|
155
|
+
},
|
|
156
|
+
annotations: {
|
|
157
|
+
title: "Read file",
|
|
158
|
+
readOnlyHint: true,
|
|
159
|
+
destructiveHint: false,
|
|
160
|
+
openWorldHint: false,
|
|
161
|
+
idempotentHint: false,
|
|
162
|
+
},
|
|
163
|
+
}, async (input) => {
|
|
164
|
+
try {
|
|
165
|
+
const session = agent.sessions[sessionId];
|
|
166
|
+
if (!session) {
|
|
167
|
+
return {
|
|
168
|
+
content: [
|
|
169
|
+
{
|
|
170
|
+
type: "text",
|
|
171
|
+
text: "The user has left the building",
|
|
172
|
+
},
|
|
173
|
+
],
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
const readResponse = await readTextFile(input);
|
|
177
|
+
if (typeof readResponse?.content !== "string") {
|
|
178
|
+
throw new Error(`No file contents for ${input.file_path}.`);
|
|
179
|
+
}
|
|
180
|
+
// Extract lines with byte limit enforcement
|
|
181
|
+
const result = extractLinesWithByteLimit(readResponse.content, defaults.maxFileSize);
|
|
182
|
+
// Construct informative message about what was read
|
|
183
|
+
let readInfo = "";
|
|
184
|
+
if ((input.offset && input.offset > 1) || result.wasLimited) {
|
|
185
|
+
readInfo = "\n\n<file-read-info>";
|
|
186
|
+
if (result.wasLimited) {
|
|
187
|
+
readInfo += `Read ${result.linesRead} lines (hit 50KB limit). `;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
readInfo += `Read lines ${input.offset}-${result.linesRead}. `;
|
|
191
|
+
}
|
|
192
|
+
if (result.wasLimited) {
|
|
193
|
+
readInfo += `Continue with offset=${result.linesRead}.`;
|
|
194
|
+
}
|
|
195
|
+
readInfo += "</file-read-info>";
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
content: [
|
|
199
|
+
{
|
|
200
|
+
type: "text",
|
|
201
|
+
text: result.content + readInfo + SYSTEM_REMINDER,
|
|
202
|
+
},
|
|
203
|
+
],
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
return {
|
|
208
|
+
content: [
|
|
209
|
+
{
|
|
210
|
+
type: "text",
|
|
211
|
+
text: "Reading file failed: " + error.message,
|
|
212
|
+
},
|
|
213
|
+
],
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
if (clientCapabilities?.fs?.writeTextFile) {
|
|
219
|
+
server.registerTool(unqualifiedToolNames.write, {
|
|
220
|
+
title: unqualifiedToolNames.write,
|
|
221
|
+
description: `Writes a file to the local filesystem..
|
|
222
|
+
|
|
223
|
+
In sessions with ${acpToolNames.write} always use it instead of Write as it will
|
|
224
|
+
allow the user to conveniently review changes.
|
|
225
|
+
|
|
226
|
+
Usage:
|
|
227
|
+
- This tool will overwrite the existing file if there is one at the provided path.
|
|
228
|
+
- If this is an existing file, you MUST use the ${acpToolNames.read} tool first to read the file's contents. This tool will fail if you did not read the file first.
|
|
229
|
+
- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required.
|
|
230
|
+
- NEVER proactively create documentation files (*.md) or README files. Only create documentation files if explicitly requested by the User.
|
|
231
|
+
- Only use emojis if the user explicitly requests it. Avoid writing emojis to files unless asked.`,
|
|
232
|
+
inputSchema: {
|
|
233
|
+
file_path: z
|
|
234
|
+
.string()
|
|
235
|
+
.describe("The absolute path to the file to write (must be absolute, not relative)"),
|
|
236
|
+
content: z.string().describe("The content to write to the file"),
|
|
237
|
+
},
|
|
238
|
+
annotations: {
|
|
239
|
+
title: "Write file",
|
|
240
|
+
readOnlyHint: false,
|
|
241
|
+
destructiveHint: false,
|
|
242
|
+
openWorldHint: false,
|
|
243
|
+
idempotentHint: false,
|
|
244
|
+
},
|
|
245
|
+
}, async (input) => {
|
|
246
|
+
try {
|
|
247
|
+
const session = agent.sessions[sessionId];
|
|
248
|
+
if (!session) {
|
|
249
|
+
return {
|
|
250
|
+
content: [
|
|
251
|
+
{
|
|
252
|
+
type: "text",
|
|
253
|
+
text: "The user has left the building",
|
|
254
|
+
},
|
|
255
|
+
],
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
await writeTextFile(input);
|
|
259
|
+
return {
|
|
260
|
+
content: [
|
|
261
|
+
{
|
|
262
|
+
type: "text",
|
|
263
|
+
text: `File created successfully at: ${input.file_path}`,
|
|
264
|
+
},
|
|
265
|
+
],
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
return {
|
|
270
|
+
content: [
|
|
271
|
+
{
|
|
272
|
+
type: "text",
|
|
273
|
+
text: "Writing file failed: " + error.message,
|
|
274
|
+
},
|
|
275
|
+
],
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
server.registerTool(unqualifiedToolNames.edit, {
|
|
280
|
+
title: unqualifiedToolNames.edit,
|
|
281
|
+
description: `Performs exact string replacements in files.
|
|
282
|
+
|
|
283
|
+
In sessions with ${acpToolNames.edit} always use it instead of Edit as it will
|
|
284
|
+
allow the user to conveniently review changes.
|
|
285
|
+
|
|
286
|
+
Usage:
|
|
287
|
+
- You must use your \`${acpToolNames.read}\` tool at least once in the conversation before editing. This tool will error if you attempt an edit without reading the file.
|
|
288
|
+
- When editing text from Read tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears.
|
|
289
|
+
- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required.
|
|
290
|
+
- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.
|
|
291
|
+
- The edit will FAIL if \`old_string\` is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use \`replace_all\` to change every instance of \`old_string\`.
|
|
292
|
+
- Use \`replace_all\` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance.`,
|
|
293
|
+
inputSchema: {
|
|
294
|
+
file_path: z.string().describe("The absolute path to the file to modify"),
|
|
295
|
+
old_string: z.string().describe("The text to replace"),
|
|
296
|
+
new_string: z
|
|
297
|
+
.string()
|
|
298
|
+
.describe("The text to replace it with (must be different from old_string)"),
|
|
299
|
+
replace_all: z
|
|
300
|
+
.boolean()
|
|
301
|
+
.default(false)
|
|
302
|
+
.optional()
|
|
303
|
+
.describe("Replace all occurrences of old_string (default false)"),
|
|
304
|
+
},
|
|
305
|
+
annotations: {
|
|
306
|
+
title: "Edit file",
|
|
307
|
+
readOnlyHint: false,
|
|
308
|
+
destructiveHint: false,
|
|
309
|
+
openWorldHint: false,
|
|
310
|
+
idempotentHint: false,
|
|
311
|
+
},
|
|
312
|
+
}, async (input) => {
|
|
313
|
+
try {
|
|
314
|
+
const session = agent.sessions[sessionId];
|
|
315
|
+
if (!session) {
|
|
316
|
+
return {
|
|
317
|
+
content: [
|
|
318
|
+
{
|
|
319
|
+
type: "text",
|
|
320
|
+
text: "The user has left the building",
|
|
321
|
+
},
|
|
322
|
+
],
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
const readResponse = await readTextFile({
|
|
326
|
+
file_path: input.file_path,
|
|
327
|
+
});
|
|
328
|
+
if (typeof readResponse?.content !== "string") {
|
|
329
|
+
throw new Error(`No file contents for ${input.file_path}.`);
|
|
330
|
+
}
|
|
331
|
+
const { newContent } = replaceAndCalculateLocation(readResponse.content, [
|
|
332
|
+
{
|
|
333
|
+
oldText: input.old_string,
|
|
334
|
+
newText: input.new_string,
|
|
335
|
+
replaceAll: input.replace_all,
|
|
336
|
+
},
|
|
337
|
+
]);
|
|
338
|
+
const patch = diff.createPatch(input.file_path, readResponse.content, newContent);
|
|
339
|
+
await writeTextFile({ file_path: input.file_path, content: newContent });
|
|
340
|
+
return {
|
|
341
|
+
content: [
|
|
342
|
+
{
|
|
343
|
+
type: "text",
|
|
344
|
+
text: patch,
|
|
345
|
+
},
|
|
346
|
+
],
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
catch (error) {
|
|
350
|
+
return {
|
|
351
|
+
content: [
|
|
352
|
+
{
|
|
353
|
+
type: "text",
|
|
354
|
+
text: "Editing file failed: " + (error?.message ?? String(error)),
|
|
355
|
+
},
|
|
356
|
+
],
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
if (agent.clientCapabilities?.terminal) {
|
|
362
|
+
server.registerTool(unqualifiedToolNames.bash, {
|
|
363
|
+
title: unqualifiedToolNames.bash,
|
|
364
|
+
description: `Executes a bash command
|
|
365
|
+
|
|
366
|
+
In sessions with ${acpToolNames.bash} always use it instead of Bash`,
|
|
367
|
+
inputSchema: {
|
|
368
|
+
command: z.string().describe("The command to execute"),
|
|
369
|
+
timeout: z.number().describe(`Optional timeout in milliseconds (max ${2 * 60 * 1000})`),
|
|
370
|
+
description: z.string().optional()
|
|
371
|
+
.describe(`Clear, concise description of what this command does in 5-10 words, in active voice. Examples:
|
|
372
|
+
Input: ls
|
|
373
|
+
Output: List files in current directory
|
|
374
|
+
|
|
375
|
+
Input: git status
|
|
376
|
+
Output: Show working tree status
|
|
377
|
+
|
|
378
|
+
Input: npm install
|
|
379
|
+
Output: Install package dependencies
|
|
380
|
+
|
|
381
|
+
Input: mkdir foo
|
|
382
|
+
Output: Create directory 'foo'`),
|
|
383
|
+
run_in_background: z
|
|
384
|
+
.boolean()
|
|
385
|
+
.default(false)
|
|
386
|
+
.describe(`Set to true to run this command in the background. The tool returns an \`id\` that can be used with the \`${acpToolNames.bashOutput}\` tool to retrieve the current output, or the \`${acpToolNames.killShell}\` tool to stop it early.`),
|
|
387
|
+
},
|
|
388
|
+
}, async (input, extra) => {
|
|
389
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
390
|
+
try {
|
|
391
|
+
const session = agent.sessions[sessionId];
|
|
392
|
+
if (!session) {
|
|
393
|
+
return {
|
|
394
|
+
content: [
|
|
395
|
+
{
|
|
396
|
+
type: "text",
|
|
397
|
+
text: "The user has left the building",
|
|
398
|
+
},
|
|
399
|
+
],
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
const toolCallId = extra._meta?.["claudecode/toolUseId"];
|
|
403
|
+
if (typeof toolCallId !== "string") {
|
|
404
|
+
throw new Error("No tool call ID found");
|
|
405
|
+
}
|
|
406
|
+
if (!agent.clientCapabilities?.terminal || !agent.client.createTerminal) {
|
|
407
|
+
throw new Error("unreachable");
|
|
408
|
+
}
|
|
409
|
+
const handle = await agent.client.createTerminal({
|
|
410
|
+
command: input.command,
|
|
411
|
+
env: [{ name: "CLAUDECODE", value: "1" }],
|
|
412
|
+
sessionId,
|
|
413
|
+
outputByteLimit: 32000,
|
|
414
|
+
});
|
|
415
|
+
await agent.client.sessionUpdate({
|
|
416
|
+
sessionId,
|
|
417
|
+
update: {
|
|
418
|
+
sessionUpdate: "tool_call_update",
|
|
419
|
+
toolCallId,
|
|
420
|
+
status: "in_progress",
|
|
421
|
+
title: input.description,
|
|
422
|
+
content: [{ type: "terminal", terminalId: handle.id }],
|
|
423
|
+
},
|
|
424
|
+
});
|
|
425
|
+
const abortPromise = new Promise((resolve) => {
|
|
426
|
+
if (extra.signal.aborted) {
|
|
427
|
+
resolve(null);
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
extra.signal.addEventListener("abort", () => {
|
|
431
|
+
resolve(null);
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
const statusPromise = Promise.race([
|
|
436
|
+
handle.waitForExit().then((exitStatus) => ({ status: "exited", exitStatus })),
|
|
437
|
+
abortPromise.then(() => ({ status: "aborted", exitStatus: null })),
|
|
438
|
+
sleep(input.timeout ?? 2 * 60 * 1000).then(async () => {
|
|
439
|
+
if (agent.backgroundTerminals[handle.id]?.status === "started") {
|
|
440
|
+
await handle.kill();
|
|
441
|
+
}
|
|
442
|
+
return { status: "timedOut", exitStatus: null };
|
|
443
|
+
}),
|
|
444
|
+
]);
|
|
445
|
+
if (input.run_in_background) {
|
|
446
|
+
agent.backgroundTerminals[handle.id] = {
|
|
447
|
+
handle,
|
|
448
|
+
lastOutput: null,
|
|
449
|
+
status: "started",
|
|
450
|
+
};
|
|
451
|
+
statusPromise.then(async ({ status, exitStatus }) => {
|
|
452
|
+
const bgTerm = agent.backgroundTerminals[handle.id];
|
|
453
|
+
if (bgTerm.status !== "started") {
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
const currentOutput = await handle.currentOutput();
|
|
457
|
+
agent.backgroundTerminals[handle.id] = {
|
|
458
|
+
status,
|
|
459
|
+
pendingOutput: {
|
|
460
|
+
...currentOutput,
|
|
461
|
+
output: stripCommonPrefix(bgTerm.lastOutput?.output ?? "", currentOutput.output),
|
|
462
|
+
exitStatus: exitStatus ?? currentOutput.exitStatus,
|
|
463
|
+
},
|
|
464
|
+
};
|
|
465
|
+
return handle.release();
|
|
466
|
+
});
|
|
467
|
+
return {
|
|
468
|
+
content: [
|
|
469
|
+
{
|
|
470
|
+
type: "text",
|
|
471
|
+
text: `Command started in background with id: ${handle.id}`,
|
|
472
|
+
},
|
|
473
|
+
],
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
const terminal = __addDisposableResource(env_1, handle, true);
|
|
477
|
+
const { status } = await statusPromise;
|
|
478
|
+
if (status === "aborted") {
|
|
479
|
+
return {
|
|
480
|
+
content: [{ type: "text", text: "Tool cancelled by user" }],
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
const output = await terminal.currentOutput();
|
|
484
|
+
return {
|
|
485
|
+
content: [{ type: "text", text: toolCommandOutput(status, output) }],
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
catch (e_1) {
|
|
489
|
+
env_1.error = e_1;
|
|
490
|
+
env_1.hasError = true;
|
|
491
|
+
}
|
|
492
|
+
finally {
|
|
493
|
+
const result_1 = __disposeResources(env_1);
|
|
494
|
+
if (result_1)
|
|
495
|
+
await result_1;
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
server.registerTool(unqualifiedToolNames.bashOutput, {
|
|
499
|
+
title: unqualifiedToolNames.bashOutput,
|
|
500
|
+
description: `- Retrieves output from a running or completed background bash shell
|
|
501
|
+
- Takes a bash_id parameter identifying the shell
|
|
502
|
+
- Always returns only new output since the last check
|
|
503
|
+
- Returns stdout and stderr output along with shell status
|
|
504
|
+
- Use this tool when you need to monitor or check the output of a long-running shell
|
|
505
|
+
|
|
506
|
+
In sessions with ${acpToolNames.bashOutput} always use it for output from Bash commands instead of TaskOutput.`,
|
|
507
|
+
inputSchema: {
|
|
508
|
+
bash_id: z
|
|
509
|
+
.string()
|
|
510
|
+
.describe(`The id of the background bash command as returned by \`${acpToolNames.bash}\``),
|
|
511
|
+
},
|
|
512
|
+
}, async (input) => {
|
|
513
|
+
const bgTerm = agent.backgroundTerminals[input.bash_id];
|
|
514
|
+
if (!bgTerm) {
|
|
515
|
+
throw new Error(`Unknown shell ${input.bash_id}`);
|
|
516
|
+
}
|
|
517
|
+
if (bgTerm.status === "started") {
|
|
518
|
+
const newOutput = await bgTerm.handle.currentOutput();
|
|
519
|
+
const strippedOutput = stripCommonPrefix(bgTerm.lastOutput?.output ?? "", newOutput.output);
|
|
520
|
+
bgTerm.lastOutput = newOutput;
|
|
521
|
+
return {
|
|
522
|
+
content: [
|
|
523
|
+
{
|
|
524
|
+
type: "text",
|
|
525
|
+
text: toolCommandOutput(bgTerm.status, {
|
|
526
|
+
...newOutput,
|
|
527
|
+
output: strippedOutput,
|
|
528
|
+
}),
|
|
529
|
+
},
|
|
530
|
+
],
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
else {
|
|
534
|
+
return {
|
|
535
|
+
content: [
|
|
536
|
+
{
|
|
537
|
+
type: "text",
|
|
538
|
+
text: toolCommandOutput(bgTerm.status, bgTerm.pendingOutput),
|
|
539
|
+
},
|
|
540
|
+
],
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
server.registerTool(unqualifiedToolNames.killShell, {
|
|
545
|
+
title: unqualifiedToolNames.killShell,
|
|
546
|
+
description: `- Kills a running background bash shell by its ID
|
|
547
|
+
- Takes a shell_id parameter identifying the shell to kill
|
|
548
|
+
- Returns a success or failure status
|
|
549
|
+
- Use this tool when you need to terminate a long-running shell
|
|
550
|
+
|
|
551
|
+
In sessions with ${acpToolNames.killShell} always use it instead of KillShell.`,
|
|
552
|
+
inputSchema: {
|
|
553
|
+
shell_id: z
|
|
554
|
+
.string()
|
|
555
|
+
.describe(`The id of the background bash command as returned by \`${acpToolNames.bash}\``),
|
|
556
|
+
},
|
|
557
|
+
}, async (input) => {
|
|
558
|
+
const bgTerm = agent.backgroundTerminals[input.shell_id];
|
|
559
|
+
if (!bgTerm) {
|
|
560
|
+
throw new Error(`Unknown shell ${input.shell_id}`);
|
|
561
|
+
}
|
|
562
|
+
switch (bgTerm.status) {
|
|
563
|
+
case "started": {
|
|
564
|
+
await bgTerm.handle.kill();
|
|
565
|
+
const currentOutput = await bgTerm.handle.currentOutput();
|
|
566
|
+
agent.backgroundTerminals[bgTerm.handle.id] = {
|
|
567
|
+
status: "killed",
|
|
568
|
+
pendingOutput: {
|
|
569
|
+
...currentOutput,
|
|
570
|
+
output: stripCommonPrefix(bgTerm.lastOutput?.output ?? "", currentOutput.output),
|
|
571
|
+
},
|
|
572
|
+
};
|
|
573
|
+
await bgTerm.handle.release();
|
|
574
|
+
return {
|
|
575
|
+
content: [{ type: "text", text: "Command killed successfully." }],
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
case "aborted":
|
|
579
|
+
return {
|
|
580
|
+
content: [{ type: "text", text: "Command aborted by user." }],
|
|
581
|
+
};
|
|
582
|
+
case "exited":
|
|
583
|
+
return {
|
|
584
|
+
content: [{ type: "text", text: "Command had already exited." }],
|
|
585
|
+
};
|
|
586
|
+
case "killed":
|
|
587
|
+
return {
|
|
588
|
+
content: [{ type: "text", text: "Command was already killed." }],
|
|
589
|
+
};
|
|
590
|
+
case "timedOut":
|
|
591
|
+
return {
|
|
592
|
+
content: [{ type: "text", text: "Command killed by timeout." }],
|
|
593
|
+
};
|
|
594
|
+
default: {
|
|
595
|
+
unreachable(bgTerm);
|
|
596
|
+
throw new Error("Unexpected background terminal status");
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
return server;
|
|
602
|
+
}
|
|
603
|
+
function stripCommonPrefix(a, b) {
|
|
604
|
+
let i = 0;
|
|
605
|
+
while (i < a.length && i < b.length && a[i] === b[i]) {
|
|
606
|
+
i++;
|
|
607
|
+
}
|
|
608
|
+
return b.slice(i);
|
|
609
|
+
}
|
|
610
|
+
function toolCommandOutput(status, output) {
|
|
611
|
+
const { exitStatus, output: commandOutput, truncated } = output;
|
|
612
|
+
let toolOutput = "";
|
|
613
|
+
switch (status) {
|
|
614
|
+
case "started":
|
|
615
|
+
case "exited": {
|
|
616
|
+
if (exitStatus && (exitStatus.exitCode ?? null) === null) {
|
|
617
|
+
toolOutput += `Interrupted by the user. `;
|
|
618
|
+
}
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
case "killed":
|
|
622
|
+
toolOutput += `Killed. `;
|
|
623
|
+
break;
|
|
624
|
+
case "timedOut":
|
|
625
|
+
toolOutput += `Timed out. `;
|
|
626
|
+
break;
|
|
627
|
+
case "aborted":
|
|
628
|
+
break;
|
|
629
|
+
default: {
|
|
630
|
+
const unreachable = status;
|
|
631
|
+
return unreachable;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
if (exitStatus) {
|
|
635
|
+
if (typeof exitStatus.exitCode === "number") {
|
|
636
|
+
toolOutput += `Exited with code ${exitStatus.exitCode}.`;
|
|
637
|
+
}
|
|
638
|
+
if (typeof exitStatus.signal === "string") {
|
|
639
|
+
toolOutput += `Signal \`${exitStatus.signal}\`. `;
|
|
640
|
+
}
|
|
641
|
+
toolOutput += "Final output:\n\n";
|
|
642
|
+
}
|
|
643
|
+
else {
|
|
644
|
+
toolOutput += "New output:\n\n";
|
|
645
|
+
}
|
|
646
|
+
toolOutput += commandOutput;
|
|
647
|
+
if (truncated) {
|
|
648
|
+
toolOutput += `\n\nCommand output was too long, so it was truncated to ${commandOutput.length} bytes.`;
|
|
649
|
+
}
|
|
650
|
+
return toolOutput;
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* Replace text in a file and calculate the line numbers where the edits occurred.
|
|
654
|
+
*
|
|
655
|
+
* @param fileContent - The full file content
|
|
656
|
+
* @param edits - Array of edit operations to apply sequentially
|
|
657
|
+
* @returns the new content and the line numbers where replacements occurred in the final content
|
|
658
|
+
*/
|
|
659
|
+
export function replaceAndCalculateLocation(fileContent, edits) {
|
|
660
|
+
let currentContent = fileContent;
|
|
661
|
+
// Use unique markers to track where replacements happen
|
|
662
|
+
const markerPrefix = `__REPLACE_MARKER_${Math.random().toString(36).substr(2, 9)}_`;
|
|
663
|
+
let markerCounter = 0;
|
|
664
|
+
const markers = [];
|
|
665
|
+
// Apply edits sequentially, inserting markers at replacement positions
|
|
666
|
+
for (const edit of edits) {
|
|
667
|
+
// Skip empty oldText
|
|
668
|
+
if (edit.oldText === "") {
|
|
669
|
+
throw new Error(`The provided \`old_string\` is empty.\n\nNo edits were applied.`);
|
|
670
|
+
}
|
|
671
|
+
if (edit.replaceAll) {
|
|
672
|
+
// Replace all occurrences with marker + newText
|
|
673
|
+
const parts = [];
|
|
674
|
+
let lastIndex = 0;
|
|
675
|
+
let searchIndex = 0;
|
|
676
|
+
while (true) {
|
|
677
|
+
const index = currentContent.indexOf(edit.oldText, searchIndex);
|
|
678
|
+
if (index === -1) {
|
|
679
|
+
if (searchIndex === 0) {
|
|
680
|
+
throw new Error(`The provided \`old_string\` does not appear in the file: "${edit.oldText}".\n\nNo edits were applied.`);
|
|
681
|
+
}
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
// Add content before the match
|
|
685
|
+
parts.push(currentContent.substring(lastIndex, index));
|
|
686
|
+
// Add marker and replacement
|
|
687
|
+
const marker = `${markerPrefix}${markerCounter++}__`;
|
|
688
|
+
markers.push(marker);
|
|
689
|
+
parts.push(marker + edit.newText);
|
|
690
|
+
lastIndex = index + edit.oldText.length;
|
|
691
|
+
searchIndex = lastIndex;
|
|
692
|
+
}
|
|
693
|
+
// Add remaining content
|
|
694
|
+
parts.push(currentContent.substring(lastIndex));
|
|
695
|
+
currentContent = parts.join("");
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
// Replace first occurrence only
|
|
699
|
+
const index = currentContent.indexOf(edit.oldText);
|
|
700
|
+
if (index === -1) {
|
|
701
|
+
throw new Error(`The provided \`old_string\` does not appear in the file: "${edit.oldText}".\n\nNo edits were applied.`);
|
|
702
|
+
}
|
|
703
|
+
else {
|
|
704
|
+
const marker = `${markerPrefix}${markerCounter++}__`;
|
|
705
|
+
markers.push(marker);
|
|
706
|
+
currentContent =
|
|
707
|
+
currentContent.substring(0, index) +
|
|
708
|
+
marker +
|
|
709
|
+
edit.newText +
|
|
710
|
+
currentContent.substring(index + edit.oldText.length);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
// Find line numbers where markers appear in the content
|
|
715
|
+
const lineNumbers = [];
|
|
716
|
+
for (const marker of markers) {
|
|
717
|
+
const index = currentContent.indexOf(marker);
|
|
718
|
+
if (index !== -1) {
|
|
719
|
+
const lineNumber = Math.max(0, currentContent.substring(0, index).split(/\r\n|\r|\n/).length - 1);
|
|
720
|
+
lineNumbers.push(lineNumber);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
// Remove all markers from the final content
|
|
724
|
+
let finalContent = currentContent;
|
|
725
|
+
for (const marker of markers) {
|
|
726
|
+
finalContent = finalContent.replace(marker, "");
|
|
727
|
+
}
|
|
728
|
+
// Dedupe and sort line numbers
|
|
729
|
+
const uniqueLineNumbers = [...new Set(lineNumbers)].sort();
|
|
730
|
+
return { newContent: finalContent, lineNumbers: uniqueLineNumbers };
|
|
731
|
+
}
|