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
This commit is contained in:
@@ -2,6 +2,7 @@ package acp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"os"
|
||||
@@ -351,3 +352,74 @@ func TestHistoryAppendOrCreateGroupsByMessageID(t *testing.T) {
|
||||
t.Fatalf("after clear message = %+v, want text=\"after clear\" messageId=msg-1", msgs[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientStreamEmitsChunkAndComplete(t *testing.T) {
|
||||
c := &Client{
|
||||
workspaceID: "ws",
|
||||
fs: newStubFS(),
|
||||
history: NewHistory(),
|
||||
lg: slog.Default(),
|
||||
}
|
||||
stdin := &recordingWriteCloser{}
|
||||
sub := newFakeSubscription()
|
||||
tr := newTransport(stdin, sub, slog.Default())
|
||||
tr.onNotification = c.handleNotification
|
||||
c.transport = tr
|
||||
c.ready = true
|
||||
c.sessionID = "s1"
|
||||
tr.start()
|
||||
defer tr.Close()
|
||||
|
||||
out := make(chan StreamEvent, 16)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
go func() {
|
||||
for strings.TrimSpace(stdin.String()) == "" {
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
chunk1 := `{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"agent_message_chunk","messageId":"m1","content":{"type":"text","text":"hello"}}}}`
|
||||
chunk2 := `{"jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"agent_message_chunk","messageId":"m1","content":{"type":"text","text":" world"}}}}`
|
||||
response := `{"jsonrpc":"2.0","id":1,"result":{"stopReason":"end_turn"}}`
|
||||
sub.send([]byte(chunk1 + "\n"))
|
||||
sub.send([]byte(chunk2 + "\n"))
|
||||
sub.send([]byte(response + "\n"))
|
||||
}()
|
||||
|
||||
if err := c.Stream(ctx, "hi", out); err != nil {
|
||||
t.Fatalf("Stream error: %v", err)
|
||||
}
|
||||
close(out)
|
||||
|
||||
var chunks int
|
||||
var complete *StreamEvent
|
||||
for ev := range out {
|
||||
switch ev.Type {
|
||||
case "chunk":
|
||||
chunks++
|
||||
if ev.MessageID != "m1" {
|
||||
t.Fatalf("chunk messageId = %q, want m1", ev.MessageID)
|
||||
}
|
||||
case "complete":
|
||||
if complete != nil {
|
||||
t.Fatal("received more than one complete event")
|
||||
}
|
||||
cp := ev
|
||||
complete = &cp
|
||||
case "error":
|
||||
t.Fatalf("unexpected error event: %q", ev.Error)
|
||||
default:
|
||||
t.Fatalf("unexpected event type %q", ev.Type)
|
||||
}
|
||||
}
|
||||
|
||||
if chunks != 2 {
|
||||
t.Fatalf("chunks = %d, want 2", chunks)
|
||||
}
|
||||
if complete == nil {
|
||||
t.Fatal("expected complete event")
|
||||
}
|
||||
if complete.StopReason != "end_turn" {
|
||||
t.Fatalf("stopReason = %q, want end_turn", complete.StopReason)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user