Skip to content

Latest commit

 

History

History
119 lines (96 loc) · 6.68 KB

chat.md

File metadata and controls

119 lines (96 loc) · 6.68 KB

Chat over ACP

← back to the index

The app's chat drives all three AI tools — Claude Code, Codex and OpenCode — through the same protocol: ACP, the Agent Client Protocol, JSON-RPC 2.0 over an agent's stdin and stdout, one message per line. The client side lives in internal/acp; the chat manager built on top of it is internal/chat.

flowchart LR
    Chat["internal/chat\n(chat manager)"]
    Conn["acp.Conn\ninternal/acp/conn.go"]
    subgraph tool["Inside the agent's container"]
        direction TB
        ClaudeAdapter["claude-agent-acp\n(npm package)"] --> Claude["Claude Code"]
        CodexAdapter["codex-acp\n(npm package)"] --> Codex["Codex"]
        OpenCodeBin["opencode acp"] --> OpenCode["OpenCode"]
    end
    Chat --> Conn
    Conn -- "JSON-RPC 2.0, stdio" --> ClaudeAdapter
    Conn -- "JSON-RPC 2.0, stdio" --> CodexAdapter
    Conn -- "JSON-RPC 2.0, stdio" --> OpenCodeBin
Loading

ACP client

internal/acp/conn.go defines Conn, which manages one bidirectional connection to an agent process: Call() sends a request and waits for its response, and a Handler interface (Notify()/Request()) receives what the agent sends back unprompted — streamed message chunks, tool-call updates, permission requests, usage updates. internal/acp/types.go defines the protocol's own types: InitializeRequest/Response, NewSessionRequest, SessionResponse, ConfigOption (the settings — model, effort, permissions — a client can show the user), McpServer (the MCP servers a session should run, stdio-based) and the usage types Cost and RateLimit that feed the token ledger.

One adapter per tool

ChatAdapters, in internal/agent/chat.go, maps each AI tool to the binary that speaks ACP for it:

var ChatAdapters = map[string]ChatAdapter{
    "claude":   {Tool: "Claude Code", Command: "claude-agent-acp", Package: image.Pin("npm:@agentclientprotocol/claude-agent-acp")},
    "codex":    {Tool: "Codex", Command: "codex-acp", Package: image.Pin("npm:@agentclientprotocol/codex-acp")},
    "opencode": {Tool: "OpenCode", Command: "opencode", Args: []string{"acp"}, Package: image.Pin("opencode")},
}

The versions are the ones the base image pins, in internal/image/tools.txt.

OpenCode has two releases and an installation chooses between them (state.SettingOpenCodeVersion, internal/opencode/version.go). OpenCode 1 is the opencode the tools pin. OpenCode 2 is not part of the image: it is installed on demand, beside it, as opencode2, because both releases carry a command called opencode; openCodeAdapter swaps the command and drops the mise package for 2, which runs by name. The ACP side is the same acp subcommand in both.

Claude Code and Codex don't speak ACP themselves, so AgentBox runs a separate adapter package in front of each (claude-agent-acp, codex-acp) that wraps the tool's own protocol in ACP. OpenCode speaks ACP natively through its own acp subcommand, so there's no wrapper — the adapter is just opencode acp. All three are installed with mise install and launched with mise exec inside the agent's container, except OpenCode 2, which each agent installs from its npm package at its first OpenCode 2 start (ensureOpenCode2InAgent) and links into its ~/.local/bin.

The chat manager

internal/chat/chat.go holds one conversation per agent (including the lead), each guarded by its own mutex. A ChatSession (internal/api) carries the session's state, which AI tool it is, its ConfigOptions, and available slash commands; a ChatItem represents one piece of the transcript — a message, a tool call, a thought, or a subagent card — each tagged with a kind, its turn, and (for something nested under a subagent) a parent id. Streamed text is batched before it reaches listeners (a flushDelay, around 50ms) rather than flushed on every token.

A subagent spawned mid-turn (Claude Code's Task tool, for example) is announced by the adapter with a subagent_spawned ACP notification and represented as its own ChatItem of kind "subagent"; whatever it says nests underneath by Parent, so the transcript reads as a tree rather than a flat log (internal/chat/subagents.go).

Background work

Claude Code can leave work running after its turn ends: a command run in the background, a Monitor, a subagent. While any runs the agent shows as Awaiting, and the session wakes by itself when one finishes. The end of the chat lists each one as a chip with its status (running, done, failed, timed out, stopped). Click a chip to see its command, when it started and ended, its exit code, and its output, which follows the command live and loads earlier output on request; a subagent's chip goes to its card in the conversation instead. Ended work stays listed until the session's AI tool restarts.

What is shown is what the tool's adapter reports (internal/chat/background.go):

Claude Code Codex OpenCode, Cursor, Gemini CLI, Copilot CLI
Background commands status, exit code, output read from the command's output file status, and the output its tool call kept not reported
Monitors status and summary, no output n/a n/a
Subagents that outlive their turn status, with a jump to the card status, with a jump to the card not reported

The same list is backgroundTasks on GET …/chat, and GET …/chat/background/{task} returns one task with the end of its output (?before=<offset> for the piece before).

The token ledger

internal/chat/tokens.go's book() runs once a turn ends: it reads the turn's per-model token counts from the adapter's acp.PromptResponse.ByModel, takes the turn's cost from a running total the adapter's usage_update notifications have been adding to since the last booking (reset on /clear), and writes one row per model via tokenRows() straight to state.AddTokenRows — off the chat's own lock, in a background goroutine, so booking never blocks the conversation. See The token ledger and Claude accounts for the table shape and how it's read back.

Compaction

A Claude Code chat compacts once its session nears the installation's compact window — 500,000 tokens by default (DefaultClaudeCompactWindow, internal/state/settings.go), because every model call resends the whole conversation. A project's rollover_threshold (state.go, default 80%) sets how full a session gets, measured against context_tokens on the ledger, before AgentBox rolls it over into a fresh session rather than letting the tool's own compaction kick in.