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