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
+9 -11
View File
@@ -42,20 +42,18 @@ func main() {
// harness; the summariser falls back to the prompt's first line, so title
// generation never blocks or fails run creation.
options := server.Options{ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle, StepVerdicts: summarise.GenerateVerdictReason}
// Chat brokers OpenCode over the owned endpoint. Streaming needs a
// URL-bearing, SSE-capable backend. Detect may select "v2" (the CLI
// advertises `api`), but Open never starts a serve process for v2 and so
// returns no event-stream URL: this build enables chat only when a
// connection carries a streamable endpoint (the owned v1 serve process).
// Otherwise chat stays disabled and every chat_* request is answered with a
// protocol error naming the missing configuration rather than hanging.
if backend, err := opencode.Detect(ctx, opencode.Executable()); err != nil {
log.Printf("OpenCode chat unavailable: %v", err)
} else if conn, err := opencode.Open(ctx, opencode.Executable(), "", backend); err != nil {
// Chat brokers OpenCode over one owned endpoint. OpenChat detects the
// installed CLI and starts the single owned serve process for it: the native
// wire for v1, and the V2 API for the installed V2 CLI (which exposes the
// authenticated SSE firehose at /api/event). Only a URL-bearing,
// SSE-capable connection enables chat; otherwise chat stays disabled and
// every chat_* request is answered with a protocol error naming the missing
// configuration rather than hanging.
if conn, err := opencode.OpenChat(ctx, opencode.Executable(), ""); err != nil {
log.Printf("OpenCode chat unavailable: %v", err)
} else if conn.URL == "" {
conn.Close()
log.Printf("OpenCode chat unavailable: backend %q exposes no streamable event endpoint", backend)
log.Printf("OpenCode chat unavailable: the connection exposes no streamable event endpoint")
} else {
options.Chat = opencode.NewChat(conn)
defer conn.Close()