Adds a minimal but real ACP stack for the opencode process: - pkg/config: process.args default ["acp"] (opencodeCommand still "opencode") - internal/process: NewManager(command, args) — exec.Command uses args - internal/acp (new): NDJSON transport + JSON-RPC client over the existing process stdio. Implements initialize / session/new / session/prompt / session/cancel. Serves fs/read_text_file and fs/write_text_file from the workspace's fs.FileSystem. terminal/* requests get MethodNotFound. - internal/service/acp_service: per-workspace Client + mutex; starts the process on first prompt; transparently re-init on restart. - internal/api/acp_handler: GET /acp/status, GET /acp/history, POST /acp/prompt, POST /acp/cancel. - internal/model/acp: API DTOs. - internal/acp/client_test: NDJSON split-lines, request/response correlation, notification dispatch, agent-initiated request handling (fs + terminal). Existing process WS endpoint and Shell subsystem are unchanged. Conversation: 019f354c-a51b-7ec3-83ad-c647e9b50b19
122 lines
3.5 KiB
Go
122 lines
3.5 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 string `json:"content,omitempty"`
|
|
ToolCallID string `json:"toolCallId,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{}
|