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
55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
package model
|
|
|
|
import "time"
|
|
|
|
// AcpStatusResponse is the API response for ACP status.
|
|
type AcpStatusResponse struct {
|
|
WorkspaceID string `json:"workspaceId"`
|
|
Ready bool `json:"ready"`
|
|
SessionID string `json:"sessionId,omitempty"`
|
|
Running bool `json:"running"`
|
|
PID int `json:"pid,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// AcpMessage is a single role/text/time entry in the ACP history.
|
|
type AcpMessage struct {
|
|
Role string `json:"role"`
|
|
Text string `json:"text"`
|
|
Time time.Time `json:"time"`
|
|
MessageID string `json:"messageId,omitempty"`
|
|
}
|
|
|
|
// AcpHistoryResponse is the API response for ACP history.
|
|
type AcpHistoryResponse struct {
|
|
SessionID string `json:"sessionId"`
|
|
Messages []AcpMessage `json:"messages"`
|
|
}
|
|
|
|
// AcpPromptRequest is the body for POST /acp/prompt.
|
|
type AcpPromptRequest struct {
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
// AcpPromptResponse is the API response for a prompt turn.
|
|
type AcpPromptResponse struct {
|
|
SessionID string `json:"sessionId"`
|
|
StopReason string `json:"stopReason"`
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
// AcpStreamRequest is the first client message on the ACP streaming WebSocket.
|
|
type AcpStreamRequest struct {
|
|
Type string `json:"type"`
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
// AcpStreamEvent is a server-to-client streaming event.
|
|
type AcpStreamEvent 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"`
|
|
}
|