Broker chat from an operator's own OpenCode endpoint and adopt existing sessions
Add an opt-in way to broker chat from an operator-supplied OpenCode service instead of the instance the server spawns for itself, and a way to adopt a session that already exists on that endpoint so it appears in chat_sessions and can be continued from the app. - Endpoint selection: the `-opencode-endpoint` server flag (defaulting to `PIPELINES_OPENCODE_ENDPOINT`) opts in; precedence is explicit flag > explicit environment > the endpoint recorded on a previous run, with the spawned instance as the default. opencode.OpenEndpoint validates the reference once (http(s), host, no userinfo/query/fragment/path), detects the backend from its own /api/info (v2) or /global/health (v1) contract, and requires a streamable text/event-stream event path. It owns no process (Close is a no-op) and never calls startServe; every failure names the endpoint and disables chat rather than silently falling back. An authenticated endpoint takes an optional password only through the private file PIPELINES_OPENCODE_ENDPOINT_PASSWORD_FILE, with PIPELINES_OPENCODE_ENDPOINT_USERNAME overriding the detected default. - Persistence: the validated choice is recorded in an additive single-row server_settings table so a restart with no flag/environment reuses it; an unreachable recorded endpoint disables chat with a clear message and no fallback. - chat_adopt: a new protocol request (ChatAdoptRequest) with handleChatAdopt in internal/server/chat.go. It refuses a nil broker/registry/payload and a blank or foreign-shaped id before any endpoint call, reads the endpoint via the shared Chat.Session (which reuses Sessions/decodeChatSessions), records through the existing RecordOperatorSession with the endpoint's directory and title verbatim, and echoes the adopted session plus its registry row like chat_create. A missing id is refused naming the id and records nothing; an endpoint record with an empty directory is refused rather than substituting the server's working directory or home. - CLI: `pipelines chat-adopt <session-id> [--json]` (usage, notes, help inventory, dispatch) with docs/cli.md updated under Chat sessions. - Autostart preserves the endpoint variables (and the password-file reference, never an inline secret) via serviceEnvironmentKeys. Checks before committing: - Package location: the change is user-facing chat behaviour, so the packaged authoring skill reference internal/author/skill/references/operations.md is updated in the same change (Brokered OpenCode chat section) with a dated verification-evidence entry and the not-deployed state. No new reference file is added, so internal/author/install.go's managed inventory is unchanged. - Packaged templates: this repository packages no template set for this behaviour and none references the chat endpoint, so no template needs updating. - Verification: `go build ./...`, `go vet` on the touched packages, `gofmt -l` clean and `go test ./...` all pass. The unit tests cover the endpoint validation/precedence, the adopt happy path and refusals, the recorded endpoint round-trip, the protocol round-trip and the CLI dispatch. The live scratch-server check against the operator's real endpoint was not run in this change. Nothing unrelated is staged; docs left stale (README and other shared documents) are not edited here and are for a single folding writer.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// openCodeEndpointSetting is the single server_settings key that remembers the
|
||||
// operator's chosen OpenCode endpoint across restarts.
|
||||
const openCodeEndpointSetting = "opencode_endpoint"
|
||||
|
||||
// initServerSettings creates the additive single-row settings table that records
|
||||
// operator choices that must survive a restart (currently the OpenCode
|
||||
// endpoint). It is its own table rather than a column elsewhere so no existing
|
||||
// schema or ownership table is touched.
|
||||
func (s *Store) initServerSettings() error {
|
||||
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS server_settings (
|
||||
key TEXT PRIMARY KEY, value TEXT NOT NULL);`)
|
||||
return err
|
||||
}
|
||||
|
||||
// RecordOpenCodeEndpoint records the operator's chosen OpenCode endpoint in one
|
||||
// bounded upsert, so a restart with no flag or environment keeps brokering from
|
||||
// the same place. It is only called after OpenEndpoint validated the endpoint.
|
||||
func (s *Store) RecordOpenCodeEndpoint(ctx context.Context, url string) error {
|
||||
_, err := s.db.ExecContext(ctx, `INSERT INTO server_settings(key,value) VALUES(?,?)
|
||||
ON CONFLICT(key) DO UPDATE SET value=excluded.value`, openCodeEndpointSetting, url)
|
||||
return err
|
||||
}
|
||||
|
||||
// OpenCodeEndpoint returns the recorded OpenCode endpoint, or an empty string
|
||||
// when none was ever recorded.
|
||||
func (s *Store) OpenCodeEndpoint(ctx context.Context) (string, error) {
|
||||
var value string
|
||||
err := s.db.QueryRowContext(ctx, `SELECT value FROM server_settings WHERE key=?`, openCodeEndpointSetting).Scan(&value)
|
||||
if err == sql.ErrNoRows {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestOpenCodeEndpointRoundTrip pins that the operator's chosen endpoint
|
||||
// survives a store Close/Open (a restart), and that an unset value reads empty.
|
||||
func TestOpenCodeEndpointRoundTrip(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "settings.db")
|
||||
store, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := store.OpenCodeEndpoint(ctx); err != nil || got != "" {
|
||||
t.Fatalf("unset endpoint = %q,%v want empty", got, err)
|
||||
}
|
||||
if err := store.RecordOpenCodeEndpoint(ctx, "http://127.0.0.1:4096"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reopened, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reopened.Close()
|
||||
if got, err := reopened.OpenCodeEndpoint(ctx); err != nil || got != "http://127.0.0.1:4096" {
|
||||
t.Fatalf("recorded endpoint = %q,%v want the same endpoint after reopen", got, err)
|
||||
}
|
||||
// A later choice replaces the row rather than appending a second one.
|
||||
if err := reopened.RecordOpenCodeEndpoint(ctx, "https://opencode.example"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := reopened.OpenCodeEndpoint(ctx); err != nil || got != "https://opencode.example" {
|
||||
t.Fatalf("updated endpoint = %q,%v want the new endpoint", got, err)
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,10 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := s.initServerSettings(); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user