b8b545accd
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.
74 lines
2.5 KiB
Go
74 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"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"
|
|
)
|
|
|
|
func main() {
|
|
addr := flag.String("addr", "127.0.0.1:8080", "HTTP/WebSocket listen address")
|
|
defaultDB, err := paths.Database()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
db := flag.String("db", defaultDB, "SQLite database path (honors XDG_DATA_HOME)")
|
|
flag.Parse()
|
|
if err := os.MkdirAll(filepath.Dir(*db), 0700); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
store, err := pipeline.Open(*db)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
artifactsRoot := filepath.Join(filepath.Dir(*db), "artifacts")
|
|
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()
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := srv.Shutdown(shutdownCtx); err != nil {
|
|
log.Printf("shutdown: %v", err)
|
|
}
|
|
}()
|
|
log.Printf("listening on %s (WebSocket endpoint: /connect)", *addr)
|
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
log.Fatal(err)
|
|
}
|
|
}
|