chat: broker OpenCode v2 chat over the owned serve SSE endpoint

The installed OpenCode 2.0.20 exposes the authenticated SSE event firehose at
/api/event and the /api HTTP surface, but Detect selects backend "v2" and Open
returned no owned process/URL, so chat stayed disabled and every chat_* request
was answered with "chat is not configured on this server".

Add opencode.OpenStream/OpenChat: for either backend they start the single owned
opencode serve process, discover the loopback URL and verify the backend health
contract (/api/info for v2, /global/health for v1). main and the live test share
OpenChat so the selection cannot drift. Chat now streams real incremental
session.text.delta/session.reasoning.delta frames via the existing
Connection.EventStream + NormalizeChatEvent, with no polling and no fake
streaming.

- Authenticate the owned endpoint with the per-backend user (opencode for v2,
  pipelines for v1); the v2 server ignores OPENCODE_SERVER_USERNAME.
- Send the V2 prompt shape {"text":...}, which the v1 translation also accepts.
- Skip firehose event types the adapter does not surface before decoding their
  data, so session.instructions.updated (delta is an object) no longer fails the
  stream.

Unit tests cover the per-backend user, the prompt shape/auth, the captured real
V2 event shapes and the selection guards. An opt-in live test drives
chat_sessions/chat_messages/a completed chat_prompt/an interrupted chat_prompt
against the installed OpenCode. Update the packaged operations reference with the
realized capability, dated evidence and deployment state.

Checks before commit: the packaged authoring reference
internal/author/skill/references/operations.md is updated in this change; no new
reference file was added, so internal/author/install.go's managed inventory is
unchanged and the internal/author tests pass. No packaged template describes the
brokered chat surface, and neither README.md nor the topic docs under docs/ (nor
web/) document it, so none of them needed updating.
This commit is contained in:
2026-10-17 01:48:07 +01:00
parent 515059674d
commit 30c1ed442e
6 changed files with 586 additions and 48 deletions
+29 -1
View File
@@ -65,7 +65,10 @@ func (c *Chat) Prompt(ctx context.Context, id, text string) error {
if strings.TrimSpace(text) == "" {
return fmt.Errorf("empty chat prompt")
}
body := map[string]any{"parts": []map[string]string{{"type": "text", "text": text}}}
// The V2 prompt body is {"text": ...}; the native serve translation accepts
// the same "text" field, so one shape works on both backends without a
// second request format.
body := map[string]any{"text": text}
if _, err := c.call(ctx, "post", "/api/session/"+id+"/prompt", body); err != nil {
return err
}
@@ -312,6 +315,23 @@ func scanChatEvents(ctx context.Context, body io.Reader, sessionID string, emit
return ctx.Err()
}
// isChatEventType reports whether t is an event type NormalizeChatEvent
// surfaces. Unknown types are skipped before their data is decoded, so an
// unrelated event whose fields share a name with chatEvent but carry a different
// JSON shape cannot fail the stream.
func isChatEventType(t string) bool {
switch t {
case "session.text.delta", "session.reasoning.delta",
"session.step.started", "session.step.streamed", "session.step.ended",
"session.usage.updated",
"session.tool.input.started", "session.tool.input.ended",
"session.tool.called", "session.tool.progress",
"session.tool.success", "session.tool.failed":
return true
}
return false
}
// chatEvent is the subset of the verified firehose envelope this adapter reads:
// {"type":"session.text.delta","data":{...}}. Fields absent from a given event
// stay zero so one struct covers every type.
@@ -353,6 +373,14 @@ func NormalizeChatEvent(payload []byte, sessionID string, lastMessageID *string)
if outer.Type == "" || len(outer.Data) == 0 {
return protocol.ChatEventFrame{}, false, nil
}
// Only decode the data of event types this adapter surfaces. The firehose
// also carries unrelated session events whose fields collide with chatEvent
// but have a different JSON shape (session.instructions.updated carries
// "delta" as an object), so decoding every event would fail the stream on an
// event that is not a chat frame at all.
if !isChatEventType(outer.Type) {
return protocol.ChatEventFrame{}, false, nil
}
var data chatEvent
if err := json.Unmarshal(outer.Data, &data); err != nil {
return protocol.ChatEventFrame{}, false, fmt.Errorf("malformed OpenCode event data: %w", err)