Stream brokered OpenCode chat replies over /connect

Add the server-side half of the brokered chat path (route B): the
opencode.Chat adapter forwards chat_prompt to POST
/api/session/{id}/prompt through the owned Connection.Call translation
and derives chat_event frames from the authenticated /api/event SSE
firehose (text and reasoning deltas, tool parts with their status
transitions, step and usage). Every pushed frame carries a stable
session and message id, with a last-message-id fallback so step/tool
events that omit the assistant message id stay attributable.

The /connect handler keeps per-session retained state (chatHub) capped at
1 MiB with least-recently-updated eviction at 128 turns, so a
reconnecting client re-syncs through chat_messages instead of replaying
deltas the OpenCode per-session log never resends. Prompt dispatch
subscribes before posting and buffers pre-ack frames to preserve order,
and cancels the stream on disconnect so no goroutine leaks. chat_sessions
and chat_create are declared but not yet brokered: they answer with an
explicit protocol error rather than the generic Pong.

Reuse the existing transport, validation and normalization; no second
OpenCode transport or credential path is introduced.

Tests: protocol golden round-trip, adapter normalization + v1/v2 Call
translation, and handler ack/stream/validation/resume/disconnect, plus a
real-adapter no-tool evidence harness.
This commit is contained in:
2026-10-16 04:09:14 +01:00
parent 9fa09e6ce8
commit b8b545accd
9 changed files with 1786 additions and 10 deletions
+37
View File
@@ -277,3 +277,40 @@ func (c *Connection) Native(ctx context.Context, method, path string, body any)
}
return data, nil
}
// EventStream opens the authenticated SSE firehose for the connection's owned
// endpoint using the same URL and credentials as Native, so chat never builds a
// parallel HTTP client. The path follows the backend surface: the native serve
// stream is /event, the V2 HTTP API is /api/event. The caller closes the body.
func (c *Connection) EventStream(ctx context.Context) (io.ReadCloser, error) {
if c == nil || c.URL == "" {
return nil, fmt.Errorf("OpenCode chat requires an event-stream endpoint")
}
path := "/api/event"
if c.Backend == "v1" {
path = "/event"
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, strings.TrimSuffix(c.URL, "/")+path, nil)
if err != nil {
return nil, err
}
request.Header.Set("Accept", "text/event-stream")
request.SetBasicAuth("pipelines", c.password)
response, err := http.DefaultClient.Do(request)
if err != nil {
if ctx.Err() != nil {
return nil, ctx.Err()
}
return nil, fmt.Errorf("OpenCode event stream request failed")
}
if response.StatusCode < 200 || response.StatusCode >= 300 {
evidence, _ := io.ReadAll(io.LimitReader(response.Body, 16<<10))
response.Body.Close()
return nil, fmt.Errorf("OpenCode event stream failed (HTTP %d): %s", response.StatusCode, strings.TrimSpace(string(evidence)))
}
if !strings.HasPrefix(response.Header.Get("Content-Type"), "text/event-stream") {
response.Body.Close()
return nil, fmt.Errorf("OpenCode event stream returned a non-SSE response")
}
return response.Body, nil
}