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:
2026-10-17 20:14:11 +01:00
parent 50af9dbc62
commit 1f75e4797e
20 changed files with 1027 additions and 33 deletions
+14 -2
View File
@@ -16,8 +16,12 @@ import "encoding/json"
// brokered values match the service exactly; all list fields are optional so
// older peers ignore them.
const (
ChatSessions = "chat_sessions"
ChatCreate = "chat_create"
ChatSessions = "chat_sessions"
ChatCreate = "chat_create"
// ChatAdopt records a session that already exists on the configured OpenCode
// endpoint in the operator-session registry, using the endpoint's own
// directory and title, so it appears in chat_sessions and can be continued.
ChatAdopt = "chat_adopt"
ChatPrompt = "chat_prompt"
ChatMessages = "chat_messages"
ChatInterrupt = "chat_interrupt"
@@ -122,6 +126,14 @@ type ChatCreateRequest struct {
Directory string `json:"directory,omitempty"`
}
// ChatAdoptRequest is the chat_adopt request body: the id of a session that
// already exists on the configured endpoint. The server records it in the
// operator-session registry with the endpoint's own directory and title and
// echoes the adopted session (and the registry list row) like chat_create.
type ChatAdoptRequest struct {
SessionID string `json:"session_id"`
}
// ChatQuery is the session-scoped request body for chat_messages and
// chat_interrupt, which name their target session through it; Limit caps the
// returned history (server default when zero).
+4 -1
View File
@@ -23,6 +23,7 @@ func chatSamples() []struct {
}},
{"session_busy", ChatSession{ID: "ses_2", Title: "held", Busy: true}},
{"create", ChatCreateRequest{Title: "demo", Directory: "/work"}},
{"adopt", ChatAdoptRequest{SessionID: "ses_1"}},
{"query", ChatQuery{SessionID: "ses_1", Limit: 50}},
{"prompt", ChatPromptRequest{SessionID: "ses_1", Text: "hello", Model: "m", Agent: "build", Files: []string{"file:///tmp/a.png"}}},
{"message", ChatMessage{
@@ -155,6 +156,7 @@ func TestChatEnvelopeRoundTrip(t *testing.T) {
ChatSessions: []ChatSession{session},
ChatSession: &session,
ChatCreate: &ChatCreateRequest{Title: "new"},
ChatAdopt: &ChatAdoptRequest{SessionID: "ses_1"},
ChatQuery: &ChatQuery{SessionID: "ses_1", Limit: 10},
ChatPrompt: &ChatPromptRequest{SessionID: "ses_1", Text: "hi"},
ChatMessages: []ChatMessage{{ID: "msg_1", SessionID: "ses_1", Role: ChatRoleUser, Parts: []ChatPart{{Type: ChatPartText, Text: "hi"}}}},
@@ -179,6 +181,7 @@ func TestChatEnvelopeRoundTrip(t *testing.T) {
if !reflect.DeepEqual(decoded.ChatSessions, message.ChatSessions) ||
!reflect.DeepEqual(decoded.ChatSession, message.ChatSession) ||
!reflect.DeepEqual(decoded.ChatCreate, message.ChatCreate) ||
!reflect.DeepEqual(decoded.ChatAdopt, message.ChatAdopt) ||
!reflect.DeepEqual(decoded.ChatQuery, message.ChatQuery) ||
!reflect.DeepEqual(decoded.ChatPrompt, message.ChatPrompt) ||
!reflect.DeepEqual(decoded.ChatMessages, message.ChatMessages) ||
@@ -195,7 +198,7 @@ func TestChatFieldsOmittedWhenUnused(t *testing.T) {
if err != nil {
t.Fatal(err)
}
for _, absent := range []string{"chat_sessions", "chat_session", "chat_create", "chat_query", "chat_prompt", "chat_messages", "chat_event", "chat_permission", "chat_reply", "chat_directories"} {
for _, absent := range []string{"chat_sessions", "chat_session", "chat_create", "chat_adopt", "chat_query", "chat_prompt", "chat_messages", "chat_event", "chat_permission", "chat_reply", "chat_directories"} {
if strings.Contains(string(minimal), absent) {
t.Fatalf("absent %q must be omitted: %s", absent, minimal)
}
+1
View File
@@ -159,6 +159,7 @@ type Message struct {
ChatSessions []ChatSession `json:"chat_sessions,omitempty"`
ChatSession *ChatSession `json:"chat_session,omitempty"`
ChatCreate *ChatCreateRequest `json:"chat_create,omitempty"`
ChatAdopt *ChatAdoptRequest `json:"chat_adopt,omitempty"`
ChatQuery *ChatQuery `json:"chat_query,omitempty"`
ChatPrompt *ChatPromptRequest `json:"chat_prompt,omitempty"`
ChatMessages []ChatMessage `json:"chat_messages,omitempty"`