From de3b68740aa9d8c26286454bc760f8fd58724384 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 18 Jan 2026 08:02:42 +0000 Subject: [PATCH] feat(acp): add experimental ACP support Co-authored-by: Jonathan Taylor --- CHANGELOG.md | 1 + docs.acp.md | 194 ++++++++++ docs/cli/acp.md | 143 +++++++ docs/cli/index.md | 7 + docs/debugging.md | 1 + package.json | 5 +- pnpm-lock.yaml | 12 + src/acp/commands.ts | 40 ++ src/acp/event-mapper.test.ts | 34 ++ src/acp/event-mapper.ts | 73 ++++ src/acp/index.ts | 4 + src/acp/meta.ts | 35 ++ src/acp/server.ts | 149 ++++++++ src/acp/session-mapper.test.ts | 57 +++ src/acp/session-mapper.ts | 95 +++++ src/acp/session.test.ts | 26 ++ src/acp/session.ts | 93 +++++ src/acp/translator.ts | 433 ++++++++++++++++++++++ src/acp/types.ts | 30 ++ src/cli/acp-cli.ts | 43 +++ src/cli/program/register.subclis.ts | 2 + src/gateway/server-bridge-methods-chat.ts | 3 + src/gateway/server-methods/chat.ts | 3 + src/routing/session-key.ts | 39 +- src/sessions/session-key-utils.ts | 35 ++ 25 files changed, 1528 insertions(+), 29 deletions(-) create mode 100644 docs.acp.md create mode 100644 docs/cli/acp.md create mode 100644 src/acp/commands.ts create mode 100644 src/acp/event-mapper.test.ts create mode 100644 src/acp/event-mapper.ts create mode 100644 src/acp/index.ts create mode 100644 src/acp/meta.ts create mode 100644 src/acp/server.ts create mode 100644 src/acp/session-mapper.test.ts create mode 100644 src/acp/session-mapper.ts create mode 100644 src/acp/session.test.ts create mode 100644 src/acp/session.ts create mode 100644 src/acp/translator.ts create mode 100644 src/acp/types.ts create mode 100644 src/cli/acp-cli.ts create mode 100644 src/sessions/session-key-utils.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index ff991ce05..949a6333b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ Docs: https://docs.clawd.bot - Sessions: add daily reset policy with per-type overrides and idle windows (default 4am local), preserving legacy idle-only configs. (#1146) — thanks @austinm911. - Docs: refresh exec/elevated/exec-approvals docs for the new flow. https://docs.clawd.bot/tools/exec-approvals - Docs: add node host CLI + update exec approvals/bridge protocol docs. https://docs.clawd.bot/cli/node +- ACP: add experimental ACP support for IDE integrations (`clawdbot acp`). Thanks @visionik. ### Fixes - Exec approvals: enforce allowlist when ask is off; prefer raw command for node approvals/events. diff --git a/docs.acp.md b/docs.acp.md new file mode 100644 index 000000000..ca664134e --- /dev/null +++ b/docs.acp.md @@ -0,0 +1,194 @@ +# Clawdbot ACP Bridge + +This document describes how the Clawdbot ACP (Agent Client Protocol) bridge works, +how it maps ACP sessions to Gateway sessions, and how IDEs should invoke it. + +## Overview + +`clawdbot acp` exposes an ACP agent over stdio and forwards prompts to a running +Clawdbot Gateway over WebSocket. It keeps ACP session ids mapped to Gateway +session keys so IDEs can reconnect to the same agent transcript or reset it on +request. + +Key goals: + +- Minimal ACP surface area (stdio, NDJSON). +- Stable session mapping across reconnects. +- Works with existing Gateway session store (list/resolve/reset). +- Safe defaults (isolated ACP session keys by default). + +## How can I use this + +Use ACP when an IDE or tooling speaks Agent Client Protocol and you want it to +drive a Clawdbot Gateway session. + +Quick steps: + +1. Run a Gateway (local or remote). +2. Configure the Gateway target (`gateway.remote.url` + auth) or pass flags. +3. Point the IDE to run `clawdbot acp` over stdio. + +Example config: + +```bash +clawdbot config set gateway.remote.url wss://gateway-host:18789 +clawdbot config set gateway.remote.token +``` + +Example run: + +```bash +clawdbot acp --url wss://gateway-host:18789 --token +``` + +## Selecting agents + +ACP does not pick agents directly. It routes by the Gateway session key. + +Use agent-scoped session keys to target a specific agent: + +```bash +clawdbot acp --session agent:main:main +clawdbot acp --session agent:design:main +clawdbot acp --session agent:qa:bug-123 +``` + +Each ACP session maps to a single Gateway session key. One agent can have many +sessions; ACP defaults to an isolated `acp:` session unless you override +the key or label. + +## Zed editor setup + +Add a custom ACP agent in `~/.config/zed/settings.json`: + +```json +{ + "agent_servers": { + "Clawdbot ACP": { + "type": "custom", + "command": "clawdbot", + "args": ["acp"], + "env": {} + } + } +} +``` + +To target a specific Gateway or agent: + +```json +{ + "agent_servers": { + "Clawdbot ACP": { + "type": "custom", + "command": "clawdbot", + "args": [ + "acp", + "--url", "wss://gateway-host:18789", + "--token", "", + "--session", "agent:design:main" + ], + "env": {} + } + } +} +``` + +In Zed, open the Agent panel and select “Clawdbot ACP” to start a thread. + +## Execution Model + +- ACP client spawns `clawdbot acp` and speaks ACP messages over stdio. +- The bridge connects to the Gateway using existing auth config (or CLI flags). +- ACP `prompt` translates to Gateway `chat.send`. +- Gateway streaming events are translated back into ACP streaming events. +- ACP `cancel` maps to Gateway `chat.abort` for the active run. + +## Session Mapping + +By default each ACP session is mapped to a dedicated Gateway session key: + +- `acp:` unless overridden. + +You can override or reuse sessions in two ways: + +1) CLI defaults + +```bash +clawdbot acp --session agent:main:main +clawdbot acp --session-label "support inbox" +clawdbot acp --reset-session +``` + +2) ACP metadata per session + +```json +{ + "_meta": { + "sessionKey": "agent:main:main", + "sessionLabel": "support inbox", + "resetSession": true, + "requireExisting": false + } +} +``` + +Rules: + +- `sessionKey`: direct Gateway session key. +- `sessionLabel`: resolve an existing session by label. +- `resetSession`: mint a new transcript for the key before first use. +- `requireExisting`: fail if the key/label does not exist. + +### Session Listing + +ACP `listSessions` maps to Gateway `sessions.list` and returns a filtered +summary suitable for IDE session pickers. `_meta.limit` can cap the number of +sessions returned. + +## Prompt Translation + +ACP prompt inputs are converted into a Gateway `chat.send`: + +- `text` and `resource` blocks become prompt text. +- `resource_link` with image mime types become attachments. +- The working directory can be prefixed into the prompt (default on, can be + disabled with `--no-prefix-cwd`). + +Gateway streaming events are translated into ACP `message` and `tool_call` +updates. Terminal Gateway states map to ACP `done` with stop reasons: + +- `complete` -> `stop` +- `aborted` -> `cancel` +- `error` -> `error` + +## Auth + Gateway Discovery + +`clawdbot acp` resolves the Gateway URL and auth from CLI flags or config: + +- `--url` / `--token` / `--password` take precedence. +- Otherwise use configured `gateway.remote.*` settings. + +## Operational Notes + +- ACP sessions are stored in memory for the bridge process lifetime. +- Gateway session state is persisted by the Gateway itself. +- `--verbose` logs ACP/Gateway bridge events to stderr (never stdout). +- ACP runs can be canceled and the active run id is tracked per session. + +## Compatibility + +- ACP bridge uses `@agentclientprotocol/sdk` (currently 0.13.x). +- Works with ACP clients that implement `initialize`, `newSession`, + `loadSession`, `prompt`, `cancel`, and `listSessions`. + +## Testing + +- Unit: `src/acp/session.test.ts` covers run id lifecycle. +- Full gate: `pnpm lint && pnpm build && pnpm test && pnpm docs:build`. + +## Related Docs + +- CLI usage: `docs/cli/acp.md` +- Session model: `docs/concepts/session.md` +- Session management internals: `docs/reference/session-management-compaction.md` diff --git a/docs/cli/acp.md b/docs/cli/acp.md new file mode 100644 index 000000000..fae4a6390 --- /dev/null +++ b/docs/cli/acp.md @@ -0,0 +1,143 @@ +--- +summary: "Run the ACP bridge for IDE integrations" +read_when: + - Setting up ACP-based IDE integrations + - Debugging ACP session routing to the Gateway +--- + +# acp + +Run the ACP (Agent Client Protocol) bridge that talks to a Clawdbot Gateway. + +This command speaks ACP over stdio for IDEs and forwards prompts to the Gateway +over WebSocket. It keeps ACP sessions mapped to Gateway session keys. + +## Usage + +```bash +clawdbot acp + +# Remote Gateway +clawdbot acp --url wss://gateway-host:18789 --token + +# Attach to an existing session key +clawdbot acp --session agent:main:main + +# Attach by label (must already exist) +clawdbot acp --session-label "support inbox" + +# Reset the session key before the first prompt +clawdbot acp --session agent:main:main --reset-session +``` + +## How to use this + +Use ACP when an IDE (or other client) speaks Agent Client Protocol and you want +it to drive a Clawdbot Gateway session. + +1. Ensure the Gateway is running (local or remote). +2. Configure the Gateway target (config or flags). +3. Point your IDE to run `clawdbot acp` over stdio. + +Example config (persisted): + +```bash +clawdbot config set gateway.remote.url wss://gateway-host:18789 +clawdbot config set gateway.remote.token +``` + +Example direct run (no config write): + +```bash +clawdbot acp --url wss://gateway-host:18789 --token +``` + +## Selecting agents + +ACP does not pick agents directly. It routes by the Gateway session key. + +Use agent-scoped session keys to target a specific agent: + +```bash +clawdbot acp --session agent:main:main +clawdbot acp --session agent:design:main +clawdbot acp --session agent:qa:bug-123 +``` + +Each ACP session maps to a single Gateway session key. One agent can have many +sessions; ACP defaults to an isolated `acp:` session unless you override +the key or label. + +## Zed editor setup + +Add a custom ACP agent in `~/.config/zed/settings.json` (or use Zed’s Settings UI): + +```json +{ + "agent_servers": { + "Clawdbot ACP": { + "type": "custom", + "command": "clawdbot", + "args": ["acp"], + "env": {} + } + } +} +``` + +To target a specific Gateway or agent: + +```json +{ + "agent_servers": { + "Clawdbot ACP": { + "type": "custom", + "command": "clawdbot", + "args": [ + "acp", + "--url", "wss://gateway-host:18789", + "--token", "", + "--session", "agent:design:main" + ], + "env": {} + } + } +} +``` + +In Zed, open the Agent panel and select “Clawdbot ACP” to start a thread. + +## Session mapping + +By default, ACP sessions get an isolated Gateway session key with an `acp:` prefix. +To reuse a known session, pass a session key or label: + +- `--session `: use a specific Gateway session key. +- `--session-label