@qlairoslabs/acp-client 0.3.1 → 0.3.3

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/README.md CHANGED
@@ -97,12 +97,23 @@ await agent.prompt("Find and fix the failing test.", {
97
97
  onChunk: (text) => process.stdout.write(text), // response text as it streams
98
98
  onActivity: (event) => render(event), // thinking / tool calls / plan / …
99
99
  onUpdate: (raw) => log(raw), // every raw session/update, unmapped
100
- onPermissionRequest: (req) => ({ outcome: "selected", optionId: req.options[0].optionId }),
100
+ onPermissionRequest: (req) => { // blocking — the agent waits
101
+ const allow = req.options.find((o) => o.kind === "allow_once");
102
+ return allow ? { outcome: "selected", optionId: allow.optionId } : { outcome: "cancelled" };
103
+ },
101
104
  });
102
105
  ```
103
106
 
107
+ Always pick a permission option by **`kind`**, never by position or name. Option ids are
108
+ the agent's own strings, the order is the agent's choice — Claude lists its reject option
109
+ first — and `name` is prose meant for a human. The four kinds are `allow_once`,
110
+ `allow_always`, `reject_once`, `reject_always`; treat the `_always` pair as policy, since
111
+ they persist into the agent's settings and outlive the session.
112
+
104
113
  If you omit `onPermissionRequest`, the client falls back to the controller's
105
- `defaultPermission` (`"approve"` / `"cancel"`).
114
+ `defaultPermission` (`"approve"` / `"cancel"`). `"approve"` selects `allow_once`, or
115
+ `allow_always` if that is the only grant offered, and cancels if it finds neither — it
116
+ never guesses.
106
117
 
107
118
  Every session update except the response text reaches `onActivity`, discriminated by
108
119
  `kind`: `thinking`, `user_message`, `tool`, `tool_update`, `plan`, `plan_update`,
@@ -252,9 +263,14 @@ agents route their shell commands through `terminal/create` and read/write files
252
263
  event, so `content` entries of type `terminal` come with `output`, `truncated` and
253
264
  `exitStatus` filled in.
254
265
 
255
- Commands run as child processes of your app, with your environment. To opt out — letting
256
- the agent fall back to its own implementation — turn the capability off, which also stops
257
- us advertising it:
266
+ Commands run as child processes of your app, with your environment. When the agent sends
267
+ `args`, they are spawned exactly as given; when it sends none, `command` is treated as a
268
+ shell line and run through `bash` (or `/bin/sh`), because that is how agents use it —
269
+ `claude-code-acp` puts its whole Bash command line, redirections and all, in `command`.
270
+ Set `ACP_TERMINAL_SHELL` to choose a different shell.
271
+
272
+ To opt out — letting the agent fall back to its own implementation — turn the capability
273
+ off, which also stops us advertising it:
258
274
 
259
275
  ```ts
260
276
  new AcpClient({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qlairoslabs/acp-client",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "Embeddable Agent Client Protocol (ACP) client library with degraded PTY fallback.",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
package/src/activity.ts CHANGED
@@ -347,6 +347,15 @@ export class ActivityAggregator {
347
347
  event: Extract<ActivityEvent, { kind: "tool" }>,
348
348
  emitted: AggregatedRecord[],
349
349
  ): ApplyResult {
350
+ // A RE-announcement is an update in disguise. Claude announces every call twice — once
351
+ // with `rawInput: {}`, then again with the real arguments, both as `tool_call` rather
352
+ // than `tool_call_update`. Taking the second as a fresh announce would replace the
353
+ // stored call (losing whatever the re-announce omits) and spend a second `announced`
354
+ // line where every other agent spends `input`.
355
+ if (this.calls.has(event.toolCallId)) {
356
+ return this.foldToolUpdate({ ...event, kind: "tool_update" }, emitted);
357
+ }
358
+
350
359
  const call: ToolActivity = { ...event };
351
360
  this.calls.set(call.toolCallId, call);
352
361
  if (hasInput(call.rawInput)) this.inputSeen.add(call.toolCallId);
package/src/adapters.ts CHANGED
@@ -19,11 +19,20 @@ export interface Adapter {
19
19
  * Convenient presets. NOT exhaustive and NOT authoritative — a caller can always pass
20
20
  * a raw `{ command: [...] }`. These only encode how to start the binary; they say
21
21
  * nothing about what the agent supports.
22
+ *
23
+ * A wrong command here surfaces as a connect timeout, which reads like a library fault, so
24
+ * the marked entries are the ones actually spawned and verified. `claude` was
25
+ * `claude-agent-acp` until it wasn't: the ACP bridge for Claude Code ships as
26
+ * `@zed-industries/claude-code-acp`, and the `claude` CLI itself speaks no ACP. Check
27
+ * `which <command>` before assuming the library is at fault.
22
28
  */
23
29
  export const ADAPTERS = {
24
- kimi: { command: ["kimi", "acp"], ptyCommand: ["kimi"], displayName: "Kimi" },
30
+ kimi: { command: ["kimi", "acp"], ptyCommand: ["kimi"], displayName: "Kimi" }, // verified
31
+ // verified — @zed-industries/claude-code-acp. NOT `claude-agent-acp`, and not the `claude` CLI.
25
32
  claude: { command: ["claude-code-acp"], ptyCommand: ["claude"], displayName: "Claude" },
33
+ // verified — @agentclientprotocol/codex-acp. The `codex` CLI has no `acp` subcommand.
26
34
  codex: { command: ["codex-acp"], ptyCommand: ["codex"], displayName: "Codex" },
35
+ // Below: from each project's docs, not spawned here. Correct at time of writing, unverified.
27
36
  gemini: { command: ["gemini", "--acp", "--stdio"], ptyCommand: ["gemini"], displayName: "Gemini" },
28
37
  copilot: { command: ["copilot", "--acp", "--stdio"], ptyCommand: ["copilot"], displayName: "Copilot" },
29
38
  opencode: { command: ["opencode", "acp"], ptyCommand: ["opencode"], displayName: "OpenCode" },
@@ -453,6 +453,15 @@ export class AgentSession implements AgentClient, Bridgeable {
453
453
  get configOptions(): ConfigOptions {
454
454
  return this.controller.configOptions;
455
455
  }
456
+ /**
457
+ * Set one of the agent's own session options — the `configOptions` it advertised on
458
+ * connect. This is where per-agent knobs live that are NOT launch flags and NOT
459
+ * environment: Codex exposes `reasoning_effort` (low…ultra), `model`, and `mode` this
460
+ * way, so it is the only route to them once the process is running.
461
+ */
462
+ setConfig(configId: string, value: unknown): Promise<void> {
463
+ return this.controller.setConfig(configId, value);
464
+ }
456
465
  get agentCommands(): AgentCommand[] {
457
466
  return this.controller.agentCommands;
458
467
  }
package/src/client.ts CHANGED
@@ -673,11 +673,20 @@ export class AcpClient implements AgentClient {
673
673
  }
674
674
 
675
675
  private defaultOutcome(request: PermissionRequest): PermissionOutcome {
676
- if ((this.config.defaultPermission ?? "cancel") === "approve") {
677
- const first = request.options[0];
678
- if (first) return { outcome: "selected", optionId: first.optionId };
679
- }
680
- return { outcome: "cancelled" };
676
+ if ((this.config.defaultPermission ?? "cancel") !== "approve") return { outcome: "cancelled" };
677
+
678
+ // By KIND, never by position. Agents order their options differently and Claude leads
679
+ // with reject, so taking `options[0]` auto-REFUSED every call it meant to approve.
680
+ //
681
+ // `allow_once` first is deliberate: `allow_always` writes a rule into the agent's own
682
+ // settings that outlives this session, and an auto-approver must not quietly make
683
+ // policy on the user's behalf. It is the fallback, never the preference.
684
+ const allow =
685
+ request.options.find((o) => o.kind === "allow_once") ??
686
+ request.options.find((o) => o.kind === "allow_always");
687
+
688
+ // Nothing recognizable to approve: refuse rather than guess. Guessing is the bug above.
689
+ return allow ? { outcome: "selected", optionId: allow.optionId } : { outcome: "cancelled" };
681
690
  }
682
691
  }
683
692
 
package/src/index.ts CHANGED
@@ -129,6 +129,7 @@ export type {
129
129
  PlanEntry,
130
130
  PermissionRequest,
131
131
  PermissionOutcome,
132
+ PermissionOptionKind,
132
133
  Usage,
133
134
  Attachment,
134
135
  SessionListEntry,
package/src/terminals.ts CHANGED
@@ -68,6 +68,39 @@ interface Entry {
68
68
  */
69
69
  const DRAIN_GRACE_MS = 250;
70
70
 
71
+ /**
72
+ * The shell used when the agent hands us a command LINE.
73
+ *
74
+ * NOT `$SHELL`: that is the user's INTERACTIVE shell, which may be fish or nushell, and
75
+ * agents generate bash/POSIX syntax — `printf x >&2` is a syntax error in fish. Prefer
76
+ * bash, because agents write bashisms (`[[ ]]`, arrays) and `/bin/sh` is dash on Debian
77
+ * and Ubuntu, where those fail. Fall back to `/bin/sh`, which always exists.
78
+ */
79
+ const SHELL = process.env.ACP_TERMINAL_SHELL || Bun.which("bash") || "/bin/sh";
80
+
81
+ /**
82
+ * Turn a `terminal/create` request into an argv.
83
+ *
84
+ * The spec is ambiguous — "The command to execute" plus an optional "Array of command
85
+ * arguments" — and agents read it both ways. `claude-code-acp` puts its ENTIRE Bash command
86
+ * line in `command` and sends no `args` at all, so spawning `command` as an executable path
87
+ * failed every shell tool call it made: `Executable not found in $PATH: "printf 'ran' >
88
+ * shell.txt"`. Redirections, pipes and `&&` were unreachable, and so was a plain command
89
+ * that merely carried its own arguments.
90
+ *
91
+ * So: with `args`, the agent has already done the splitting and its arguments are final —
92
+ * spawn directly, because re-splitting would turn one argument containing a space into two.
93
+ * Without them, `command` is a line for a shell. The two readings never overlap, and for a
94
+ * bare program name they agree (`sh -c "ls"` and spawning `ls` do the same thing).
95
+ *
96
+ * The cost: a command line is subject to word splitting, so an agent sending an unquoted
97
+ * executable path containing spaces and no `args` would break. That shape is much rarer
98
+ * than the one this fixes, and quoting it here would corrupt every genuine command line.
99
+ */
100
+ function toArgv(params: CreateTerminalParams): string[] {
101
+ return params.args?.length ? [params.command, ...params.args] : [SHELL, "-c", params.command];
102
+ }
103
+
71
104
  export class TerminalRegistry {
72
105
  private readonly terminals = new Map<string, Entry>();
73
106
  private seq = 0;
@@ -86,7 +119,7 @@ export class TerminalRegistry {
86
119
  };
87
120
  for (const v of params.env ?? []) env[v.name] = v.value;
88
121
 
89
- const proc = Bun.spawn([params.command, ...(params.args ?? [])], {
122
+ const proc = Bun.spawn(toArgv(params), {
90
123
  cwd: params.cwd ?? this.options.cwd,
91
124
  env,
92
125
  stdin: "ignore",
package/src/types.ts CHANGED
@@ -168,11 +168,27 @@ export type PermissionOutcome =
168
168
  | { outcome: "selected"; optionId: string }
169
169
  | { outcome: "cancelled" };
170
170
 
171
+ /**
172
+ * What an option DOES, independent of what it is called or where it sits in the list.
173
+ * The only safe thing to branch on: `name` is prose and the order is the agent's whim
174
+ * (Claude puts its reject option first). `allow_always` and `reject_always` persist into
175
+ * the agent's own settings and outlive the session — treat them as policy, not answers.
176
+ *
177
+ * The open arm keeps a kind we don't know yet from failing to typecheck; branch with a
178
+ * fallback rather than assuming the four.
179
+ */
180
+ export type PermissionOptionKind =
181
+ | "allow_once"
182
+ | "allow_always"
183
+ | "reject_once"
184
+ | "reject_always"
185
+ | (string & {});
186
+
171
187
  /** A permission request surfaced to the caller. The agent BLOCKS until this resolves. */
172
188
  export interface PermissionRequest {
173
189
  toolCallId: string;
174
190
  title?: string;
175
- options: Array<{ optionId: string; name: string; kind: string }>;
191
+ options: Array<{ optionId: string; name: string; kind: PermissionOptionKind }>;
176
192
  /** The raw SDK request, for callers that need fields we haven't surfaced. */
177
193
  raw: unknown;
178
194
  }