acp-extension-claude 0.13.1

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