@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,908 @@
|
|
|
1
|
+
import { AgentSideConnection, ndJsonStream, RequestError, } from "@agentclientprotocol/sdk";
|
|
2
|
+
import { SettingsManager } from "./settings.js";
|
|
3
|
+
import { query, } from "@anthropic-ai/claude-agent-sdk";
|
|
4
|
+
import * as fs from "node:fs";
|
|
5
|
+
import * as path from "node:path";
|
|
6
|
+
import * as os from "node:os";
|
|
7
|
+
import { nodeToWebReadable, nodeToWebWritable, Pushable, unreachable } from "./utils.js";
|
|
8
|
+
import { createMcpServer } from "./mcp-server.js";
|
|
9
|
+
import { EDIT_TOOL_NAMES, acpToolNames } from "./tools.js";
|
|
10
|
+
import { toolInfoFromToolUse, planEntries, toolUpdateFromToolResult, registerHookCallback, createPostToolUseHook, createPreToolUseHook, } from "./tools.js";
|
|
11
|
+
import packageJson from "../package.json" with { type: "json" };
|
|
12
|
+
import { randomUUID } from "node:crypto";
|
|
13
|
+
export const CLAUDE_CONFIG_DIR = process.env.CLAUDE ?? path.join(os.homedir(), ".claude");
|
|
14
|
+
// Bypass Permissions doesn't work if we are a root/sudo user
|
|
15
|
+
const IS_ROOT = (process.geteuid?.() ?? process.getuid?.()) === 0;
|
|
16
|
+
// Implement the ACP Agent interface
|
|
17
|
+
export class ClaudeAcpAgent {
|
|
18
|
+
constructor(client, logger) {
|
|
19
|
+
this.backgroundTerminals = {};
|
|
20
|
+
this.sessions = {};
|
|
21
|
+
this.client = client;
|
|
22
|
+
this.toolUseCache = {};
|
|
23
|
+
this.logger = logger ?? console;
|
|
24
|
+
}
|
|
25
|
+
async initialize(request) {
|
|
26
|
+
this.clientCapabilities = request.clientCapabilities;
|
|
27
|
+
// Default authMethod
|
|
28
|
+
const authMethod = {
|
|
29
|
+
description: "Run `claude /login` in the terminal",
|
|
30
|
+
name: "Log in with Claude Code",
|
|
31
|
+
id: "claude-login",
|
|
32
|
+
};
|
|
33
|
+
// If client supports terminal-auth capability, use that instead.
|
|
34
|
+
// if (request.clientCapabilities?._meta?.["terminal-auth"] === true) {
|
|
35
|
+
// const cliPath = fileURLToPath(import.meta.resolve("@anthropic-ai/claude-agent-sdk/cli.js"));
|
|
36
|
+
// authMethod._meta = {
|
|
37
|
+
// "terminal-auth": {
|
|
38
|
+
// command: "node",
|
|
39
|
+
// args: [cliPath, "/login"],
|
|
40
|
+
// label: "Claude Code Login",
|
|
41
|
+
// },
|
|
42
|
+
// };
|
|
43
|
+
// }
|
|
44
|
+
return {
|
|
45
|
+
protocolVersion: 1,
|
|
46
|
+
agentCapabilities: {
|
|
47
|
+
promptCapabilities: {
|
|
48
|
+
image: true,
|
|
49
|
+
embeddedContext: true,
|
|
50
|
+
},
|
|
51
|
+
mcpCapabilities: {
|
|
52
|
+
http: true,
|
|
53
|
+
sse: true,
|
|
54
|
+
},
|
|
55
|
+
sessionCapabilities: {
|
|
56
|
+
fork: {},
|
|
57
|
+
resume: {},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
agentInfo: {
|
|
61
|
+
name: packageJson.name,
|
|
62
|
+
title: "Claude Code",
|
|
63
|
+
version: packageJson.version,
|
|
64
|
+
},
|
|
65
|
+
authMethods: [authMethod],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
async newSession(params) {
|
|
69
|
+
if (fs.existsSync(path.resolve(os.homedir(), ".claude.json.backup")) &&
|
|
70
|
+
!fs.existsSync(path.resolve(os.homedir(), ".claude.json"))) {
|
|
71
|
+
throw RequestError.authRequired();
|
|
72
|
+
}
|
|
73
|
+
return await this.createSession(params, {
|
|
74
|
+
// Revisit these meta values once we support resume
|
|
75
|
+
resume: params._meta?.claudeCode?.options?.resume,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async unstable_forkSession(params) {
|
|
79
|
+
return await this.createSession({
|
|
80
|
+
cwd: params.cwd,
|
|
81
|
+
mcpServers: params.mcpServers ?? [],
|
|
82
|
+
_meta: params._meta,
|
|
83
|
+
}, {
|
|
84
|
+
resume: params.sessionId,
|
|
85
|
+
forkSession: true,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
async unstable_resumeSession(params) {
|
|
89
|
+
const response = await this.createSession({
|
|
90
|
+
cwd: params.cwd,
|
|
91
|
+
mcpServers: params.mcpServers ?? [],
|
|
92
|
+
_meta: params._meta,
|
|
93
|
+
}, {
|
|
94
|
+
resume: params.sessionId,
|
|
95
|
+
});
|
|
96
|
+
return response;
|
|
97
|
+
}
|
|
98
|
+
async authenticate(_params) {
|
|
99
|
+
throw new Error("Method not implemented.");
|
|
100
|
+
}
|
|
101
|
+
async prompt(params) {
|
|
102
|
+
if (!this.sessions[params.sessionId]) {
|
|
103
|
+
throw new Error("Session not found");
|
|
104
|
+
}
|
|
105
|
+
this.sessions[params.sessionId].cancelled = false;
|
|
106
|
+
const { query, input } = this.sessions[params.sessionId];
|
|
107
|
+
input.push(promptToClaude(params));
|
|
108
|
+
while (true) {
|
|
109
|
+
const { value: message, done } = await query.next();
|
|
110
|
+
if (done || !message) {
|
|
111
|
+
if (this.sessions[params.sessionId].cancelled) {
|
|
112
|
+
return { stopReason: "cancelled" };
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
switch (message.type) {
|
|
117
|
+
case "system":
|
|
118
|
+
switch (message.subtype) {
|
|
119
|
+
case "init":
|
|
120
|
+
break;
|
|
121
|
+
case "compact_boundary":
|
|
122
|
+
case "hook_response":
|
|
123
|
+
case "status":
|
|
124
|
+
// Todo: process via status api: https://docs.claude.com/en/docs/claude-code/hooks#hook-output
|
|
125
|
+
break;
|
|
126
|
+
default:
|
|
127
|
+
unreachable(message, this.logger);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
131
|
+
case "result": {
|
|
132
|
+
if (this.sessions[params.sessionId].cancelled) {
|
|
133
|
+
return { stopReason: "cancelled" };
|
|
134
|
+
}
|
|
135
|
+
switch (message.subtype) {
|
|
136
|
+
case "success": {
|
|
137
|
+
if (message.result.includes("Please run /login")) {
|
|
138
|
+
throw RequestError.authRequired();
|
|
139
|
+
}
|
|
140
|
+
if (message.is_error) {
|
|
141
|
+
throw RequestError.internalError(undefined, message.result);
|
|
142
|
+
}
|
|
143
|
+
return { stopReason: "end_turn" };
|
|
144
|
+
}
|
|
145
|
+
case "error_during_execution":
|
|
146
|
+
if (message.is_error) {
|
|
147
|
+
throw RequestError.internalError(undefined, message.errors.join(", ") || message.subtype);
|
|
148
|
+
}
|
|
149
|
+
return { stopReason: "end_turn" };
|
|
150
|
+
case "error_max_budget_usd":
|
|
151
|
+
case "error_max_turns":
|
|
152
|
+
case "error_max_structured_output_retries":
|
|
153
|
+
if (message.is_error) {
|
|
154
|
+
throw RequestError.internalError(undefined, message.errors.join(", ") || message.subtype);
|
|
155
|
+
}
|
|
156
|
+
return { stopReason: "max_turn_requests" };
|
|
157
|
+
default:
|
|
158
|
+
unreachable(message, this.logger);
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
case "stream_event": {
|
|
164
|
+
for (const notification of streamEventToAcpNotifications(message, params.sessionId, this.toolUseCache, this.client, this.logger)) {
|
|
165
|
+
await this.client.sessionUpdate(notification);
|
|
166
|
+
}
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
case "user":
|
|
170
|
+
case "assistant": {
|
|
171
|
+
if (this.sessions[params.sessionId].cancelled) {
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
// Slash commands like /compact can generate invalid output... doesn't match
|
|
175
|
+
// their own docs: https://docs.anthropic.com/en/docs/claude-code/sdk/sdk-slash-commands#%2Fcompact-compact-conversation-history
|
|
176
|
+
if (typeof message.message.content === "string" &&
|
|
177
|
+
message.message.content.includes("<local-command-stdout>")) {
|
|
178
|
+
this.logger.log(message.message.content);
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
if (typeof message.message.content === "string" &&
|
|
182
|
+
message.message.content.includes("<local-command-stderr>")) {
|
|
183
|
+
this.logger.error(message.message.content);
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
// Skip these user messages for now, since they seem to just be messages we don't want in the feed
|
|
187
|
+
if (message.type === "user" &&
|
|
188
|
+
(typeof message.message.content === "string" ||
|
|
189
|
+
(Array.isArray(message.message.content) &&
|
|
190
|
+
message.message.content.length === 1 &&
|
|
191
|
+
message.message.content[0].type === "text"))) {
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
if (message.type === "assistant" &&
|
|
195
|
+
message.message.model === "<synthetic>" &&
|
|
196
|
+
Array.isArray(message.message.content) &&
|
|
197
|
+
message.message.content.length === 1 &&
|
|
198
|
+
message.message.content[0].type === "text" &&
|
|
199
|
+
message.message.content[0].text.includes("Please run /login")) {
|
|
200
|
+
throw RequestError.authRequired();
|
|
201
|
+
}
|
|
202
|
+
const content = message.type === "assistant"
|
|
203
|
+
? // Handled by stream events above
|
|
204
|
+
message.message.content.filter((item) => !["text", "thinking"].includes(item.type))
|
|
205
|
+
: message.message.content;
|
|
206
|
+
for (const notification of toAcpNotifications(content, message.message.role, params.sessionId, this.toolUseCache, this.client, this.logger)) {
|
|
207
|
+
await this.client.sessionUpdate(notification);
|
|
208
|
+
}
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
case "tool_progress":
|
|
212
|
+
break;
|
|
213
|
+
case "auth_status":
|
|
214
|
+
break;
|
|
215
|
+
default:
|
|
216
|
+
unreachable(message);
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
throw new Error("Session did not end in result");
|
|
221
|
+
}
|
|
222
|
+
async cancel(params) {
|
|
223
|
+
if (!this.sessions[params.sessionId]) {
|
|
224
|
+
throw new Error("Session not found");
|
|
225
|
+
}
|
|
226
|
+
this.sessions[params.sessionId].cancelled = true;
|
|
227
|
+
await this.sessions[params.sessionId].query.interrupt();
|
|
228
|
+
}
|
|
229
|
+
async unstable_setSessionModel(params) {
|
|
230
|
+
if (!this.sessions[params.sessionId]) {
|
|
231
|
+
throw new Error("Session not found");
|
|
232
|
+
}
|
|
233
|
+
await this.sessions[params.sessionId].query.setModel(params.modelId);
|
|
234
|
+
}
|
|
235
|
+
async setSessionMode(params) {
|
|
236
|
+
if (!this.sessions[params.sessionId]) {
|
|
237
|
+
throw new Error("Session not found");
|
|
238
|
+
}
|
|
239
|
+
switch (params.modeId) {
|
|
240
|
+
case "default":
|
|
241
|
+
case "acceptEdits":
|
|
242
|
+
case "bypassPermissions":
|
|
243
|
+
case "dontAsk":
|
|
244
|
+
case "plan":
|
|
245
|
+
this.sessions[params.sessionId].permissionMode = params.modeId;
|
|
246
|
+
try {
|
|
247
|
+
await this.sessions[params.sessionId].query.setPermissionMode(params.modeId);
|
|
248
|
+
}
|
|
249
|
+
catch (error) {
|
|
250
|
+
const errorMessage = error instanceof Error && error.message ? error.message : "Invalid Mode";
|
|
251
|
+
throw new Error(errorMessage);
|
|
252
|
+
}
|
|
253
|
+
return {};
|
|
254
|
+
default:
|
|
255
|
+
throw new Error("Invalid Mode");
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async readTextFile(params) {
|
|
259
|
+
const response = await this.client.readTextFile(params);
|
|
260
|
+
return response;
|
|
261
|
+
}
|
|
262
|
+
async writeTextFile(params) {
|
|
263
|
+
const response = await this.client.writeTextFile(params);
|
|
264
|
+
return response;
|
|
265
|
+
}
|
|
266
|
+
canUseTool(sessionId) {
|
|
267
|
+
return async (toolName, toolInput, { signal, suggestions, toolUseID }) => {
|
|
268
|
+
const session = this.sessions[sessionId];
|
|
269
|
+
if (!session) {
|
|
270
|
+
return {
|
|
271
|
+
behavior: "deny",
|
|
272
|
+
message: "Session not found",
|
|
273
|
+
interrupt: true,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
if (toolName === "ExitPlanMode") {
|
|
277
|
+
const response = await this.client.requestPermission({
|
|
278
|
+
options: [
|
|
279
|
+
{
|
|
280
|
+
kind: "allow_always",
|
|
281
|
+
name: "Yes, and auto-accept edits",
|
|
282
|
+
optionId: "acceptEdits",
|
|
283
|
+
},
|
|
284
|
+
{ kind: "allow_once", name: "Yes, and manually approve edits", optionId: "default" },
|
|
285
|
+
{ kind: "reject_once", name: "No, keep planning", optionId: "plan" },
|
|
286
|
+
],
|
|
287
|
+
sessionId,
|
|
288
|
+
toolCall: {
|
|
289
|
+
toolCallId: toolUseID,
|
|
290
|
+
rawInput: toolInput,
|
|
291
|
+
title: toolInfoFromToolUse({ name: toolName, input: toolInput }).title,
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
if (signal.aborted || response.outcome?.outcome === "cancelled") {
|
|
295
|
+
throw new Error("Tool use aborted");
|
|
296
|
+
}
|
|
297
|
+
if (response.outcome?.outcome === "selected" &&
|
|
298
|
+
(response.outcome.optionId === "default" || response.outcome.optionId === "acceptEdits")) {
|
|
299
|
+
session.permissionMode = response.outcome.optionId;
|
|
300
|
+
await this.client.sessionUpdate({
|
|
301
|
+
sessionId,
|
|
302
|
+
update: {
|
|
303
|
+
sessionUpdate: "current_mode_update",
|
|
304
|
+
currentModeId: response.outcome.optionId,
|
|
305
|
+
},
|
|
306
|
+
});
|
|
307
|
+
return {
|
|
308
|
+
behavior: "allow",
|
|
309
|
+
updatedInput: toolInput,
|
|
310
|
+
updatedPermissions: suggestions ?? [
|
|
311
|
+
{ type: "setMode", mode: response.outcome.optionId, destination: "session" },
|
|
312
|
+
],
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
return {
|
|
317
|
+
behavior: "deny",
|
|
318
|
+
message: "User rejected request to exit plan mode.",
|
|
319
|
+
interrupt: true,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (session.permissionMode === "bypassPermissions" ||
|
|
324
|
+
(session.permissionMode === "acceptEdits" && EDIT_TOOL_NAMES.includes(toolName))) {
|
|
325
|
+
return {
|
|
326
|
+
behavior: "allow",
|
|
327
|
+
updatedInput: toolInput,
|
|
328
|
+
updatedPermissions: suggestions ?? [
|
|
329
|
+
{ type: "addRules", rules: [{ toolName }], behavior: "allow", destination: "session" },
|
|
330
|
+
],
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
const response = await this.client.requestPermission({
|
|
334
|
+
options: [
|
|
335
|
+
{
|
|
336
|
+
kind: "allow_always",
|
|
337
|
+
name: "Always Allow",
|
|
338
|
+
optionId: "allow_always",
|
|
339
|
+
},
|
|
340
|
+
{ kind: "allow_once", name: "Allow", optionId: "allow" },
|
|
341
|
+
{ kind: "reject_once", name: "Reject", optionId: "reject" },
|
|
342
|
+
],
|
|
343
|
+
sessionId,
|
|
344
|
+
toolCall: {
|
|
345
|
+
toolCallId: toolUseID,
|
|
346
|
+
rawInput: toolInput,
|
|
347
|
+
title: toolInfoFromToolUse({ name: toolName, input: toolInput }).title,
|
|
348
|
+
},
|
|
349
|
+
});
|
|
350
|
+
if (signal.aborted || response.outcome?.outcome === "cancelled") {
|
|
351
|
+
throw new Error("Tool use aborted");
|
|
352
|
+
}
|
|
353
|
+
if (response.outcome?.outcome === "selected" &&
|
|
354
|
+
(response.outcome.optionId === "allow" || response.outcome.optionId === "allow_always")) {
|
|
355
|
+
// If Claude Code has suggestions, it will update their settings already
|
|
356
|
+
if (response.outcome.optionId === "allow_always") {
|
|
357
|
+
return {
|
|
358
|
+
behavior: "allow",
|
|
359
|
+
updatedInput: toolInput,
|
|
360
|
+
updatedPermissions: suggestions ?? [
|
|
361
|
+
{
|
|
362
|
+
type: "addRules",
|
|
363
|
+
rules: [{ toolName }],
|
|
364
|
+
behavior: "allow",
|
|
365
|
+
destination: "session",
|
|
366
|
+
},
|
|
367
|
+
],
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
return {
|
|
371
|
+
behavior: "allow",
|
|
372
|
+
updatedInput: toolInput,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
return {
|
|
377
|
+
behavior: "deny",
|
|
378
|
+
message: "User refused permission to run tool",
|
|
379
|
+
interrupt: true,
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
async createSession(params, creationOpts = {}) {
|
|
385
|
+
// We want to create a new session id unless it is resume,
|
|
386
|
+
// but not resume + forkSession.
|
|
387
|
+
let sessionId;
|
|
388
|
+
if (creationOpts.forkSession) {
|
|
389
|
+
sessionId = randomUUID();
|
|
390
|
+
}
|
|
391
|
+
else if (creationOpts.resume) {
|
|
392
|
+
sessionId = creationOpts.resume;
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
sessionId = randomUUID();
|
|
396
|
+
}
|
|
397
|
+
const input = new Pushable();
|
|
398
|
+
const settingsManager = new SettingsManager(params.cwd, {
|
|
399
|
+
logger: this.logger,
|
|
400
|
+
});
|
|
401
|
+
await settingsManager.initialize();
|
|
402
|
+
const mcpServers = {};
|
|
403
|
+
if (Array.isArray(params.mcpServers)) {
|
|
404
|
+
for (const server of params.mcpServers) {
|
|
405
|
+
if ("type" in server) {
|
|
406
|
+
mcpServers[server.name] = {
|
|
407
|
+
type: server.type,
|
|
408
|
+
url: server.url,
|
|
409
|
+
headers: server.headers
|
|
410
|
+
? Object.fromEntries(server.headers.map((e) => [e.name, e.value]))
|
|
411
|
+
: undefined,
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
mcpServers[server.name] = {
|
|
416
|
+
type: "stdio",
|
|
417
|
+
command: server.command,
|
|
418
|
+
args: server.args,
|
|
419
|
+
env: server.env
|
|
420
|
+
? Object.fromEntries(server.env.map((e) => [e.name, e.value]))
|
|
421
|
+
: undefined,
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
// Only add the acp MCP server if built-in tools are not disabled
|
|
427
|
+
if (!params._meta?.disableBuiltInTools) {
|
|
428
|
+
const server = createMcpServer(this, sessionId, this.clientCapabilities);
|
|
429
|
+
mcpServers["acp"] = {
|
|
430
|
+
type: "sdk",
|
|
431
|
+
name: "acp",
|
|
432
|
+
instance: server,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
let systemPrompt = { type: "preset", preset: "claude_code" };
|
|
436
|
+
if (params._meta?.systemPrompt) {
|
|
437
|
+
const customPrompt = params._meta.systemPrompt;
|
|
438
|
+
if (typeof customPrompt === "string") {
|
|
439
|
+
systemPrompt = customPrompt;
|
|
440
|
+
}
|
|
441
|
+
else if (typeof customPrompt === "object" &&
|
|
442
|
+
"append" in customPrompt &&
|
|
443
|
+
typeof customPrompt.append === "string") {
|
|
444
|
+
systemPrompt.append = customPrompt.append;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
const permissionMode = "default";
|
|
448
|
+
// Extract options from _meta if provided
|
|
449
|
+
const userProvidedOptions = params._meta?.claudeCode?.options;
|
|
450
|
+
const extraArgs = { ...userProvidedOptions?.extraArgs };
|
|
451
|
+
if (creationOpts?.resume === undefined || creationOpts?.forkSession) {
|
|
452
|
+
// Set our own session id if not resuming an existing session.
|
|
453
|
+
extraArgs["session-id"] = sessionId;
|
|
454
|
+
}
|
|
455
|
+
const options = {
|
|
456
|
+
systemPrompt,
|
|
457
|
+
settingSources: ["user", "project", "local"],
|
|
458
|
+
stderr: (err) => this.logger.error(err),
|
|
459
|
+
...userProvidedOptions,
|
|
460
|
+
// Override certain fields that must be controlled by ACP
|
|
461
|
+
cwd: params.cwd,
|
|
462
|
+
includePartialMessages: true,
|
|
463
|
+
mcpServers: { ...(userProvidedOptions?.mcpServers || {}), ...mcpServers },
|
|
464
|
+
extraArgs,
|
|
465
|
+
// If we want bypassPermissions to be an option, we have to allow it here.
|
|
466
|
+
// But it doesn't work in root mode, so we only activate it if it will work.
|
|
467
|
+
allowDangerouslySkipPermissions: !IS_ROOT,
|
|
468
|
+
permissionMode,
|
|
469
|
+
canUseTool: this.canUseTool(sessionId),
|
|
470
|
+
// note: although not documented by the types, passing an absolute path
|
|
471
|
+
// here works to find zed's managed node version.
|
|
472
|
+
executable: process.execPath,
|
|
473
|
+
...(process.env.CLAUDE_CODE_EXECUTABLE && {
|
|
474
|
+
pathToClaudeCodeExecutable: process.env.CLAUDE_CODE_EXECUTABLE,
|
|
475
|
+
}),
|
|
476
|
+
hooks: {
|
|
477
|
+
...userProvidedOptions?.hooks,
|
|
478
|
+
PreToolUse: [
|
|
479
|
+
...(userProvidedOptions?.hooks?.PreToolUse || []),
|
|
480
|
+
{
|
|
481
|
+
hooks: [createPreToolUseHook(settingsManager, this.logger)],
|
|
482
|
+
},
|
|
483
|
+
],
|
|
484
|
+
PostToolUse: [
|
|
485
|
+
...(userProvidedOptions?.hooks?.PostToolUse || []),
|
|
486
|
+
{
|
|
487
|
+
hooks: [createPostToolUseHook(this.logger)],
|
|
488
|
+
},
|
|
489
|
+
],
|
|
490
|
+
},
|
|
491
|
+
...creationOpts,
|
|
492
|
+
};
|
|
493
|
+
const allowedTools = [];
|
|
494
|
+
const disallowedTools = [];
|
|
495
|
+
// Check if built-in tools should be disabled
|
|
496
|
+
const disableBuiltInTools = params._meta?.disableBuiltInTools === true;
|
|
497
|
+
if (!disableBuiltInTools) {
|
|
498
|
+
if (this.clientCapabilities?.fs?.readTextFile) {
|
|
499
|
+
allowedTools.push(acpToolNames.read);
|
|
500
|
+
disallowedTools.push("Read");
|
|
501
|
+
}
|
|
502
|
+
if (this.clientCapabilities?.fs?.writeTextFile) {
|
|
503
|
+
disallowedTools.push("Write", "Edit");
|
|
504
|
+
}
|
|
505
|
+
if (this.clientCapabilities?.terminal) {
|
|
506
|
+
allowedTools.push(acpToolNames.bashOutput, acpToolNames.killShell);
|
|
507
|
+
disallowedTools.push("Bash", "BashOutput", "KillShell");
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
// When built-in tools are disabled, explicitly disallow all of them
|
|
512
|
+
disallowedTools.push(acpToolNames.read, acpToolNames.write, acpToolNames.edit, acpToolNames.bash, acpToolNames.bashOutput, acpToolNames.killShell, "Read", "Write", "Edit", "Bash", "BashOutput", "KillShell", "Glob", "Grep", "Task", "TodoWrite", "ExitPlanMode", "WebSearch", "WebFetch", "AskUserQuestion", "SlashCommand", "Skill", "NotebookEdit");
|
|
513
|
+
}
|
|
514
|
+
if (allowedTools.length > 0) {
|
|
515
|
+
options.allowedTools = allowedTools;
|
|
516
|
+
}
|
|
517
|
+
if (disallowedTools.length > 0) {
|
|
518
|
+
options.disallowedTools = disallowedTools;
|
|
519
|
+
}
|
|
520
|
+
// Handle abort controller from meta options
|
|
521
|
+
const abortController = userProvidedOptions?.abortController;
|
|
522
|
+
if (abortController?.signal.aborted) {
|
|
523
|
+
throw new Error("Cancelled");
|
|
524
|
+
}
|
|
525
|
+
const q = query({
|
|
526
|
+
prompt: input,
|
|
527
|
+
options,
|
|
528
|
+
});
|
|
529
|
+
this.sessions[sessionId] = {
|
|
530
|
+
query: q,
|
|
531
|
+
input: input,
|
|
532
|
+
cancelled: false,
|
|
533
|
+
permissionMode,
|
|
534
|
+
settingsManager,
|
|
535
|
+
};
|
|
536
|
+
const availableCommands = await getAvailableSlashCommands(q);
|
|
537
|
+
const models = await getAvailableModels(q);
|
|
538
|
+
// Needs to happen after we return the session
|
|
539
|
+
setTimeout(() => {
|
|
540
|
+
this.client.sessionUpdate({
|
|
541
|
+
sessionId,
|
|
542
|
+
update: {
|
|
543
|
+
sessionUpdate: "available_commands_update",
|
|
544
|
+
availableCommands,
|
|
545
|
+
},
|
|
546
|
+
});
|
|
547
|
+
}, 0);
|
|
548
|
+
const availableModes = [
|
|
549
|
+
{
|
|
550
|
+
id: "default",
|
|
551
|
+
name: "Default",
|
|
552
|
+
description: "Standard behavior, prompts for dangerous operations",
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
id: "acceptEdits",
|
|
556
|
+
name: "Accept Edits",
|
|
557
|
+
description: "Auto-accept file edit operations",
|
|
558
|
+
},
|
|
559
|
+
{
|
|
560
|
+
id: "plan",
|
|
561
|
+
name: "Plan Mode",
|
|
562
|
+
description: "Planning mode, no actual tool execution",
|
|
563
|
+
},
|
|
564
|
+
{
|
|
565
|
+
id: "dontAsk",
|
|
566
|
+
name: "Don't Ask",
|
|
567
|
+
description: "Don't prompt for permissions, deny if not pre-approved",
|
|
568
|
+
},
|
|
569
|
+
];
|
|
570
|
+
// Only works in non-root mode
|
|
571
|
+
if (!IS_ROOT) {
|
|
572
|
+
availableModes.push({
|
|
573
|
+
id: "bypassPermissions",
|
|
574
|
+
name: "Bypass Permissions",
|
|
575
|
+
description: "Bypass all permission checks",
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
return {
|
|
579
|
+
sessionId,
|
|
580
|
+
models,
|
|
581
|
+
modes: {
|
|
582
|
+
currentModeId: permissionMode,
|
|
583
|
+
availableModes,
|
|
584
|
+
},
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
async function getAvailableModels(query) {
|
|
589
|
+
const models = await query.supportedModels();
|
|
590
|
+
// Query doesn't give us access to the currently selected model, so we just choose the first model in the list.
|
|
591
|
+
const currentModel = models[0];
|
|
592
|
+
await query.setModel(currentModel.value);
|
|
593
|
+
const availableModels = models.map((model) => ({
|
|
594
|
+
modelId: model.value,
|
|
595
|
+
name: model.displayName,
|
|
596
|
+
description: model.description,
|
|
597
|
+
}));
|
|
598
|
+
return {
|
|
599
|
+
availableModels,
|
|
600
|
+
currentModelId: currentModel.value,
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
async function getAvailableSlashCommands(query) {
|
|
604
|
+
const UNSUPPORTED_COMMANDS = [
|
|
605
|
+
"context",
|
|
606
|
+
"cost",
|
|
607
|
+
"login",
|
|
608
|
+
"logout",
|
|
609
|
+
"output-style:new",
|
|
610
|
+
"release-notes",
|
|
611
|
+
"todos",
|
|
612
|
+
];
|
|
613
|
+
const commands = await query.supportedCommands();
|
|
614
|
+
return commands
|
|
615
|
+
.map((command) => {
|
|
616
|
+
const input = command.argumentHint ? { hint: command.argumentHint } : null;
|
|
617
|
+
let name = command.name;
|
|
618
|
+
if (command.name.endsWith(" (MCP)")) {
|
|
619
|
+
name = `mcp:${name.replace(" (MCP)", "")}`;
|
|
620
|
+
}
|
|
621
|
+
return {
|
|
622
|
+
name,
|
|
623
|
+
description: command.description || "",
|
|
624
|
+
input,
|
|
625
|
+
};
|
|
626
|
+
})
|
|
627
|
+
.filter((command) => !UNSUPPORTED_COMMANDS.includes(command.name));
|
|
628
|
+
}
|
|
629
|
+
function formatUriAsLink(uri) {
|
|
630
|
+
try {
|
|
631
|
+
if (uri.startsWith("file://")) {
|
|
632
|
+
const path = uri.slice(7); // Remove "file://"
|
|
633
|
+
const name = path.split("/").pop() || path;
|
|
634
|
+
return `[@${name}](${uri})`;
|
|
635
|
+
}
|
|
636
|
+
else if (uri.startsWith("zed://")) {
|
|
637
|
+
const parts = uri.split("/");
|
|
638
|
+
const name = parts[parts.length - 1] || uri;
|
|
639
|
+
return `[@${name}](${uri})`;
|
|
640
|
+
}
|
|
641
|
+
return uri;
|
|
642
|
+
}
|
|
643
|
+
catch {
|
|
644
|
+
return uri;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
export function promptToClaude(prompt) {
|
|
648
|
+
const content = [];
|
|
649
|
+
const context = [];
|
|
650
|
+
for (const chunk of prompt.prompt) {
|
|
651
|
+
switch (chunk.type) {
|
|
652
|
+
case "text": {
|
|
653
|
+
let text = chunk.text;
|
|
654
|
+
// change /mcp:server:command args -> /server:command (MCP) args
|
|
655
|
+
const mcpMatch = text.match(/^\/mcp:([^:\s]+):(\S+)(\s+.*)?$/);
|
|
656
|
+
if (mcpMatch) {
|
|
657
|
+
const [, server, command, args] = mcpMatch;
|
|
658
|
+
text = `/${server}:${command} (MCP)${args || ""}`;
|
|
659
|
+
}
|
|
660
|
+
content.push({ type: "text", text });
|
|
661
|
+
break;
|
|
662
|
+
}
|
|
663
|
+
case "resource_link": {
|
|
664
|
+
const formattedUri = formatUriAsLink(chunk.uri);
|
|
665
|
+
content.push({
|
|
666
|
+
type: "text",
|
|
667
|
+
text: formattedUri,
|
|
668
|
+
});
|
|
669
|
+
break;
|
|
670
|
+
}
|
|
671
|
+
case "resource": {
|
|
672
|
+
if ("text" in chunk.resource) {
|
|
673
|
+
const formattedUri = formatUriAsLink(chunk.resource.uri);
|
|
674
|
+
content.push({
|
|
675
|
+
type: "text",
|
|
676
|
+
text: formattedUri,
|
|
677
|
+
});
|
|
678
|
+
context.push({
|
|
679
|
+
type: "text",
|
|
680
|
+
text: `\n<context ref="${chunk.resource.uri}">\n${chunk.resource.text}\n</context>`,
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
// Ignore blob resources (unsupported)
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
case "image":
|
|
687
|
+
if (chunk.data) {
|
|
688
|
+
content.push({
|
|
689
|
+
type: "image",
|
|
690
|
+
source: {
|
|
691
|
+
type: "base64",
|
|
692
|
+
data: chunk.data,
|
|
693
|
+
media_type: chunk.mimeType,
|
|
694
|
+
},
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
else if (chunk.uri && chunk.uri.startsWith("http")) {
|
|
698
|
+
content.push({
|
|
699
|
+
type: "image",
|
|
700
|
+
source: {
|
|
701
|
+
type: "url",
|
|
702
|
+
url: chunk.uri,
|
|
703
|
+
},
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
break;
|
|
707
|
+
// Ignore audio and other unsupported types
|
|
708
|
+
default:
|
|
709
|
+
break;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
content.push(...context);
|
|
713
|
+
return {
|
|
714
|
+
type: "user",
|
|
715
|
+
message: {
|
|
716
|
+
role: "user",
|
|
717
|
+
content: content,
|
|
718
|
+
},
|
|
719
|
+
session_id: prompt.sessionId,
|
|
720
|
+
parent_tool_use_id: null,
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Convert an SDKAssistantMessage (Claude) to a SessionNotification (ACP).
|
|
725
|
+
* Only handles text, image, and thinking chunks for now.
|
|
726
|
+
*/
|
|
727
|
+
export function toAcpNotifications(content, role, sessionId, toolUseCache, client, logger) {
|
|
728
|
+
if (typeof content === "string") {
|
|
729
|
+
return [
|
|
730
|
+
{
|
|
731
|
+
sessionId,
|
|
732
|
+
update: {
|
|
733
|
+
sessionUpdate: role === "assistant" ? "agent_message_chunk" : "user_message_chunk",
|
|
734
|
+
content: {
|
|
735
|
+
type: "text",
|
|
736
|
+
text: content,
|
|
737
|
+
},
|
|
738
|
+
},
|
|
739
|
+
},
|
|
740
|
+
];
|
|
741
|
+
}
|
|
742
|
+
const output = [];
|
|
743
|
+
// Only handle the first chunk for streaming; extend as needed for batching
|
|
744
|
+
for (const chunk of content) {
|
|
745
|
+
let update = null;
|
|
746
|
+
switch (chunk.type) {
|
|
747
|
+
case "text":
|
|
748
|
+
case "text_delta":
|
|
749
|
+
update = {
|
|
750
|
+
sessionUpdate: role === "assistant" ? "agent_message_chunk" : "user_message_chunk",
|
|
751
|
+
content: {
|
|
752
|
+
type: "text",
|
|
753
|
+
text: chunk.text,
|
|
754
|
+
},
|
|
755
|
+
};
|
|
756
|
+
break;
|
|
757
|
+
case "image":
|
|
758
|
+
update = {
|
|
759
|
+
sessionUpdate: role === "assistant" ? "agent_message_chunk" : "user_message_chunk",
|
|
760
|
+
content: {
|
|
761
|
+
type: "image",
|
|
762
|
+
data: chunk.source.type === "base64" ? chunk.source.data : "",
|
|
763
|
+
mimeType: chunk.source.type === "base64" ? chunk.source.media_type : "",
|
|
764
|
+
uri: chunk.source.type === "url" ? chunk.source.url : undefined,
|
|
765
|
+
},
|
|
766
|
+
};
|
|
767
|
+
break;
|
|
768
|
+
case "thinking":
|
|
769
|
+
case "thinking_delta":
|
|
770
|
+
update = {
|
|
771
|
+
sessionUpdate: "agent_thought_chunk",
|
|
772
|
+
content: {
|
|
773
|
+
type: "text",
|
|
774
|
+
text: chunk.thinking,
|
|
775
|
+
},
|
|
776
|
+
};
|
|
777
|
+
break;
|
|
778
|
+
case "tool_use":
|
|
779
|
+
case "server_tool_use":
|
|
780
|
+
case "mcp_tool_use": {
|
|
781
|
+
toolUseCache[chunk.id] = chunk;
|
|
782
|
+
if (chunk.name === "TodoWrite") {
|
|
783
|
+
// @ts-expect-error - sometimes input is empty object
|
|
784
|
+
if (Array.isArray(chunk.input.todos)) {
|
|
785
|
+
update = {
|
|
786
|
+
sessionUpdate: "plan",
|
|
787
|
+
entries: planEntries(chunk.input),
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
else {
|
|
792
|
+
// Register hook callback to receive the structured output from the hook
|
|
793
|
+
registerHookCallback(chunk.id, {
|
|
794
|
+
onPostToolUseHook: async (toolUseId, toolInput, toolResponse) => {
|
|
795
|
+
const toolUse = toolUseCache[toolUseId];
|
|
796
|
+
if (toolUse) {
|
|
797
|
+
const update = {
|
|
798
|
+
_meta: {
|
|
799
|
+
claudeCode: {
|
|
800
|
+
toolResponse,
|
|
801
|
+
toolName: toolUse.name,
|
|
802
|
+
},
|
|
803
|
+
},
|
|
804
|
+
toolCallId: toolUseId,
|
|
805
|
+
sessionUpdate: "tool_call_update",
|
|
806
|
+
};
|
|
807
|
+
await client.sessionUpdate({
|
|
808
|
+
sessionId,
|
|
809
|
+
update,
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
else {
|
|
813
|
+
logger.error(`[claude-code-acp] Got a tool response for tool use that wasn't tracked: ${toolUseId}`);
|
|
814
|
+
}
|
|
815
|
+
},
|
|
816
|
+
});
|
|
817
|
+
let rawInput;
|
|
818
|
+
try {
|
|
819
|
+
rawInput = JSON.parse(JSON.stringify(chunk.input));
|
|
820
|
+
}
|
|
821
|
+
catch {
|
|
822
|
+
// ignore if we can't turn it to JSON
|
|
823
|
+
}
|
|
824
|
+
update = {
|
|
825
|
+
_meta: {
|
|
826
|
+
claudeCode: {
|
|
827
|
+
toolName: chunk.name,
|
|
828
|
+
},
|
|
829
|
+
},
|
|
830
|
+
toolCallId: chunk.id,
|
|
831
|
+
sessionUpdate: "tool_call",
|
|
832
|
+
rawInput,
|
|
833
|
+
status: "pending",
|
|
834
|
+
...toolInfoFromToolUse(chunk),
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
break;
|
|
838
|
+
}
|
|
839
|
+
case "tool_result":
|
|
840
|
+
case "tool_search_tool_result":
|
|
841
|
+
case "web_fetch_tool_result":
|
|
842
|
+
case "web_search_tool_result":
|
|
843
|
+
case "code_execution_tool_result":
|
|
844
|
+
case "bash_code_execution_tool_result":
|
|
845
|
+
case "text_editor_code_execution_tool_result":
|
|
846
|
+
case "mcp_tool_result": {
|
|
847
|
+
const toolUse = toolUseCache[chunk.tool_use_id];
|
|
848
|
+
if (!toolUse) {
|
|
849
|
+
logger.error(`[claude-code-acp] Got a tool result for tool use that wasn't tracked: ${chunk.tool_use_id}`);
|
|
850
|
+
break;
|
|
851
|
+
}
|
|
852
|
+
if (toolUse.name !== "TodoWrite") {
|
|
853
|
+
update = {
|
|
854
|
+
_meta: {
|
|
855
|
+
claudeCode: {
|
|
856
|
+
toolName: toolUse.name,
|
|
857
|
+
},
|
|
858
|
+
},
|
|
859
|
+
toolCallId: chunk.tool_use_id,
|
|
860
|
+
sessionUpdate: "tool_call_update",
|
|
861
|
+
status: "is_error" in chunk && chunk.is_error ? "failed" : "completed",
|
|
862
|
+
...toolUpdateFromToolResult(chunk, toolUseCache[chunk.tool_use_id]),
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
break;
|
|
866
|
+
}
|
|
867
|
+
case "document":
|
|
868
|
+
case "search_result":
|
|
869
|
+
case "redacted_thinking":
|
|
870
|
+
case "input_json_delta":
|
|
871
|
+
case "citations_delta":
|
|
872
|
+
case "signature_delta":
|
|
873
|
+
case "container_upload":
|
|
874
|
+
break;
|
|
875
|
+
default:
|
|
876
|
+
unreachable(chunk, logger);
|
|
877
|
+
break;
|
|
878
|
+
}
|
|
879
|
+
if (update) {
|
|
880
|
+
output.push({ sessionId, update });
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
return output;
|
|
884
|
+
}
|
|
885
|
+
export function streamEventToAcpNotifications(message, sessionId, toolUseCache, client, logger) {
|
|
886
|
+
const event = message.event;
|
|
887
|
+
switch (event.type) {
|
|
888
|
+
case "content_block_start":
|
|
889
|
+
return toAcpNotifications([event.content_block], "assistant", sessionId, toolUseCache, client, logger);
|
|
890
|
+
case "content_block_delta":
|
|
891
|
+
return toAcpNotifications([event.delta], "assistant", sessionId, toolUseCache, client, logger);
|
|
892
|
+
// No content
|
|
893
|
+
case "message_start":
|
|
894
|
+
case "message_delta":
|
|
895
|
+
case "message_stop":
|
|
896
|
+
case "content_block_stop":
|
|
897
|
+
return [];
|
|
898
|
+
default:
|
|
899
|
+
unreachable(event, logger);
|
|
900
|
+
return [];
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
export function runAcp() {
|
|
904
|
+
const input = nodeToWebWritable(process.stdout);
|
|
905
|
+
const output = nodeToWebReadable(process.stdin);
|
|
906
|
+
const stream = ndJsonStream(input, output);
|
|
907
|
+
new AgentSideConnection((client) => new ClaudeAcpAgent(client), stream);
|
|
908
|
+
}
|