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:
@@ -65,9 +65,14 @@ func messageText(data []byte) (string, error) {
|
||||
|
||||
// Call adapts the pipeline's existing V2 command contract to the native stable API.
|
||||
// Sessions still use the same account/database; only our loopback serve process is owned.
|
||||
// A connection whose backend already speaks the /api HTTP surface forwards the path
|
||||
// unchanged: only the v1 serve surface needs the /api -> native translation below.
|
||||
func (c *Connection) Call(ctx context.Context, method, path string, body any) ([]byte, error) {
|
||||
if c.Backend != "v1" {
|
||||
return nil, fmt.Errorf("native adapter requires the serve backend")
|
||||
if c.URL == "" {
|
||||
return nil, fmt.Errorf("native adapter requires the serve backend")
|
||||
}
|
||||
return c.Native(ctx, method, path, body)
|
||||
}
|
||||
if path == "/api/session" && method == "post" {
|
||||
data, _ := json.Marshal(body)
|
||||
@@ -152,6 +157,9 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
|
||||
Prompt string `json:"prompt"`
|
||||
Text string `json:"text"`
|
||||
Format json.RawMessage `json:"format"`
|
||||
Parts []struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"parts"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &request); err != nil {
|
||||
return nil, err
|
||||
@@ -160,6 +168,17 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
|
||||
if text == "" {
|
||||
text = request.Text
|
||||
}
|
||||
// The brokered chat adapter sends the V2 prompt shape (a parts array);
|
||||
// accept it so the native translation carries the text unchanged instead
|
||||
// of posting an empty prompt.
|
||||
if text == "" {
|
||||
for _, part := range request.Parts {
|
||||
if part.Text != "" {
|
||||
text = part.Text
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
payload := map[string]any{"model": c.models[id], "parts": []map[string]string{{"type": "text", "text": text}}}
|
||||
if len(request.Format) > 0 {
|
||||
supported, err := c.supportsFormat(ctx, request.Format)
|
||||
|
||||
Reference in New Issue
Block a user