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:
@@ -12,6 +12,7 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
"git.a-wan.net/alan/pipelines/internal/paths"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/server"
|
||||
@@ -36,7 +37,26 @@ func main() {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
artifactsRoot := filepath.Join(filepath.Dir(*db), "artifacts")
|
||||
srv := &http.Server{Addr: *addr, Handler: server.HandlerWithOptions(ctx, server.Options{ArtifactsRoot: artifactsRoot}, store), ReadHeaderTimeout: 5 * time.Second}
|
||||
options := server.Options{ArtifactsRoot: artifactsRoot}
|
||||
// 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 {
|
||||
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)
|
||||
} else {
|
||||
options.Chat = opencode.NewChat(conn)
|
||||
defer conn.Close()
|
||||
}
|
||||
srv := &http.Server{Addr: *addr, Handler: server.HandlerWithOptions(ctx, options, store), ReadHeaderTimeout: 5 * time.Second}
|
||||
defer server.WaitRuns(srv.Handler)
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
|
||||
Reference in New Issue
Block a user