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 031451fe3e feat(acp): streaming WebSocket route for prompt chunks
Adds GET /api/workspaces/:id/acp/stream. Client opens a WebSocket,
sends {"type":"prompt","content":"..."}, and receives a stream of
{"type":"chunk","messageId","text"} events followed by exactly
one {"type":"complete","stopReason"} or {"type":"error","error"}.
Closing the WS early triggers session/cancel.

- internal/acp/messages.go: StreamEvent wire shape.
- internal/acp/client.go:
  - streamChs []chan StreamEvent set; AddStream / RemoveStream.
  - sendStream non-blocking fanout.
  - Client.Stream(ctx, content, out) registers out, sends prompt,
    emits complete/error after the prompt response, unregisters.
  - handleNotification fans chunk events to all stream consumers.
  - notifyWG ensures chunk ordering vs the terminal event.
- internal/acp/service.go: Service.Stream(workspaceID, content, out)
  mirrors Prompt (per-workspace lock, 5-min timeout, EnsureReady).
- internal/service/acp_service.go: thin AcpService.Stream wrapper
  that maps acp.StreamEvent -> model.AcpStreamEvent.
- internal/model/acp.go: AcpStreamRequest, AcpStreamEvent DTOs.
- internal/api/acp_handler.go: stream WS handler (upgrade, read
  prompt, run Stream in a goroutine, write events, ping/pong, Cancel
  on client close).
- internal/api/router.go: register the new route.
- internal/acp/transport.go: dispatch notifications synchronously
  (vs. goroutine per notification) so chunks preserve order before
  the session/prompt response.
- internal/acp/client_test.go: TestClientStreamEmitsChunkAndComplete
  with a fake transport that drives a known sequence.
- internal/api/acp_handler_test.go: TestAcpStreamHandlerRoutes
  smoke test using a fake opencode acp script.

Existing POST /api/workspaces/:id/acp/prompt is unchanged.

E2E: prompt 'say hi in exactly 3 words' -> 3 chunk events
('Hi',' there','!') + 1 complete {stopReason: 'end_turn'}.

Conversation: 019f3680-200f-79b0-860b-43302e60d0ea
2026-07-06 16:43:17 +08:00

139 lines
4.1 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"`
}
// StreamEvent is the wire shape of the streaming protocol. The Type field
// is one of "chunk", "complete", "error"; the other fields are populated
// based on Type.
type StreamEvent struct {
Type string `json:"type"`
MessageID string `json:"messageId,omitempty"`
Text string `json:"text,omitempty"`
StopReason string `json:"stopReason,omitempty"`
Error string `json:"error,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{}