This repository has been archived on 2026-07-17. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
codespace/internal/acp/messages.go
T
tao.chen eb2bf3e684 fix(acp): parse session/update content as ContentBlock, not string
The ACP spec defines session/update content as a ContentBlock object
({type, text}) but we decoded it as a string. This dropped every
agent_message_chunk and left prompt responses empty. E2E against the
real opencode acp binary surfaced the bug.

- internal/acp/messages.go: add ContentBlock struct; Update.Content is
  now ContentBlock.
- internal/acp/client.go: handleNotification extracts Text when
  Content.Type == "text"; other types are logged at debug and dropped.
- internal/acp/client_test.go: NDJSON framing test sends content as a
  ContentBlock object and reads .Text.
- README.md: document /acp/* routes + CODESPACE_OPENCODE_ARGS env.

Verified end-to-end: POST /acp/prompt returns text="Hi there!", 4
messages in history (1 user + 3 agent chunks), 0 invalid session/update
log entries.

Conversation: 019f357d-4236-7010-949c-e61067618d42
2026-07-06 11:52:22 +08:00

128 lines
3.7 KiB
Go

package acp
import "encoding/json"
// jsonRPCMessage is the raw JSON-RPC 2.0 envelope used over NDJSON.
type jsonRPCMessage struct {
JSONRPC string `json:"jsonrpc"`
ID *int `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Error *rpcError `json:"error,omitempty"`
}
// rpcError is a JSON-RPC 2.0 error object.
type rpcError struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
// InitializeParams is sent once to negotiate the ACP session.
type InitializeParams struct {
ProtocolVersion int `json:"protocolVersion"`
ClientCapabilities ClientCapabilities `json:"clientCapabilities"`
ClientInfo ClientInfo `json:"clientInfo"`
}
// ClientCapabilities describes tools the client exposes to the agent.
type ClientCapabilities struct {
FS ClientFSCapabilities `json:"fs"`
}
// ClientFSCapabilities declares filesystem support.
type ClientFSCapabilities struct {
ReadTextFile bool `json:"readTextFile"`
WriteTextFile bool `json:"writeTextFile"`
}
// ClientInfo identifies this client to the agent.
type ClientInfo struct {
Name string `json:"name"`
Version string `json:"version"`
}
// InitializeResult is returned by the agent for initialize.
type InitializeResult struct {
ProtocolVersion int `json:"protocolVersion"`
AgentCapabilities any `json:"agentCapabilities"`
AgentInfo any `json:"agentInfo"`
}
// SessionNewParams starts a new agent session for a workspace.
type SessionNewParams struct {
Cwd string `json:"cwd"`
MCPServers []any `json:"mcpServers"`
}
// SessionNewResult returns the session identifier.
type SessionNewResult struct {
SessionID string `json:"sessionId"`
}
// PromptMessage is a single user message part.
type PromptMessage struct {
Type string `json:"type"`
Text string `json:"text"`
}
// SessionPromptParams sends user input to the agent.
type SessionPromptParams struct {
SessionID string `json:"sessionId"`
Prompt []PromptMessage `json:"prompt"`
}
// SessionPromptResult is the final response for a prompt turn.
type SessionPromptResult struct {
StopReason string `json:"stopReason"`
}
// SessionCancelParams interrupts an in-flight turn.
type SessionCancelParams struct {
SessionID string `json:"sessionId"`
}
// SessionUpdateParams is the payload of a session/update notification.
type SessionUpdateParams struct {
SessionID string `json:"sessionId"`
Update Update `json:"update"`
}
// Update carries a single streamed update from the agent.
type Update struct {
SessionUpdate string `json:"sessionUpdate"`
MessageID string `json:"messageId,omitempty"`
Content ContentBlock `json:"content,omitempty"`
ToolCallID string `json:"toolCallId,omitempty"`
}
// ContentBlock is a content payload in session/update notifications.
type ContentBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
}
// FSReadParams is sent by the agent to read a workspace file.
type FSReadParams struct {
Path string `json:"path"`
SessionID string `json:"sessionId"`
Line *int `json:"line,omitempty"`
Limit *int `json:"limit,omitempty"`
}
// FSReadResult returns file contents to the agent.
type FSReadResult struct {
Content string `json:"content"`
}
// FSWriteParams is sent by the agent to write a workspace file.
type FSWriteParams struct {
Path string `json:"path"`
SessionID string `json:"sessionId"`
Content string `json:"content"`
}
// FSWriteResult is the empty success response for fs/write_text_file.
type FSWriteResult struct{}