1f75e4797e
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.
180 lines
8.1 KiB
Go
180 lines
8.1 KiB
Go
package protocol
|
|
|
|
import "git.a-wan.net/alan/pipelines/internal/buildinfo"
|
|
|
|
const (
|
|
Ping = "ping"
|
|
Pong = "pong"
|
|
Error = "error"
|
|
Submit = "submit"
|
|
Submitted = "submitted"
|
|
List = "list_pipelines"
|
|
PipelineList = "pipelines"
|
|
GetPipeline = "get_pipeline"
|
|
Pipeline = "pipeline"
|
|
ListTemplates = "list_templates"
|
|
Templates = "templates"
|
|
Validate = "validate"
|
|
Validated = "validated"
|
|
)
|
|
|
|
type Stage struct {
|
|
Limits *StageLimits `json:"limits,omitempty"`
|
|
Memory map[string]string `json:"memory,omitempty"`
|
|
Notify *NotificationPolicy `json:"notify,omitempty"`
|
|
Key string `json:"key"`
|
|
Type string `json:"type,omitempty"`
|
|
Output string `json:"output"`
|
|
Prompt string `json:"prompt"`
|
|
Configs map[string]string `json:"configs"`
|
|
Edges map[string]string `json:"edges"`
|
|
FallbackError bool `json:"fallback_error"`
|
|
// Additional type-specific Markdown blocks; legacy model fields remain above.
|
|
Blocks map[string]string `json:"blocks,omitempty"`
|
|
}
|
|
|
|
// Limits are enforced by the runner and never included in the model prompt.
|
|
type StageLimits struct {
|
|
MaxVisits int `json:"max_visits"`
|
|
On []string `json:"on,omitempty"`
|
|
}
|
|
|
|
type NotificationPolicy struct {
|
|
On []string `json:"on"`
|
|
Destination string `json:"destination"`
|
|
Artifacts []string `json:"artifacts,omitempty"`
|
|
}
|
|
|
|
// Public phone setup only. Never include publishing endpoints or credentials.
|
|
type NotificationEndpoint struct {
|
|
Destination string `json:"destination"`
|
|
ServerURL string `json:"server_url,omitempty"`
|
|
Topic string `json:"topic"`
|
|
}
|
|
|
|
type Template struct {
|
|
Key string `json:"key"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Source string `json:"source"`
|
|
Scheduled bool `json:"scheduled"`
|
|
}
|
|
|
|
type Graph struct {
|
|
Stages []Stage `json:"stages"`
|
|
Settings *Settings `json:"settings,omitempty"`
|
|
}
|
|
|
|
type Settings struct {
|
|
Prompt string `json:"prompt"`
|
|
Schedules []Schedule `json:"schedules"`
|
|
}
|
|
|
|
type Schedule struct {
|
|
Days []string `json:"days,omitempty"`
|
|
Times []string `json:"times,omitempty"`
|
|
Every string `json:"every,omitempty"`
|
|
Timezone string `json:"timezone,omitempty"`
|
|
}
|
|
|
|
type VersionSummary struct {
|
|
Version int `json:"version"`
|
|
Stages int `json:"stages"`
|
|
CreatedAt string `json:"created_at"`
|
|
}
|
|
|
|
type PipelineDetail struct {
|
|
PipelineSummary
|
|
LatestVersion int `json:"latest_version"`
|
|
VersionCreatedAt string `json:"version_created_at"`
|
|
ContentHash string `json:"content_hash"`
|
|
Source string `json:"source"`
|
|
Graph Graph `json:"graph"`
|
|
Versions []VersionSummary `json:"versions"`
|
|
}
|
|
|
|
type PipelineSummary struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Version int `json:"version"`
|
|
Stages int `json:"stages"`
|
|
CreatedAt string `json:"created_at"`
|
|
UpdatedAt string `json:"updated_at"`
|
|
}
|
|
|
|
// Message is the JSON envelope shared by the server and client. It also brokers
|
|
// omni search: a client sends Type "search" with an ID, Query text, an optional
|
|
// Kind type filter and an optional Limit, and the server replies with Type
|
|
// "search_results", the echoed ID and a ranked SearchResponse. Both search fields
|
|
// are optional so older peers ignore them. See search.go for the per-type payloads
|
|
// and the ranking/kind/bound vocabulary.
|
|
type Message struct {
|
|
DefaultHarness string `json:"default_harness,omitempty"`
|
|
Build *buildinfo.Info `json:"build,omitempty"`
|
|
Protocol int `json:"protocol,omitempty"`
|
|
DefaultModel string `json:"default_model,omitempty"`
|
|
Evidence []SessionEvidence `json:"evidence,omitempty"`
|
|
Usage *UsageStats `json:"usage,omitempty"`
|
|
Status *SystemStatus `json:"status,omitempty"`
|
|
Graph *Graph `json:"graph,omitempty"`
|
|
ExpectedVersion int `json:"expected_version,omitempty"`
|
|
Templates []Template `json:"templates,omitempty"`
|
|
CreateOnly bool `json:"create_only,omitempty"`
|
|
NotificationEndpoints []NotificationEndpoint `json:"notification_endpoints,omitempty"`
|
|
Type string `json:"type"`
|
|
ID string `json:"id,omitempty"`
|
|
Time string `json:"time,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
Source string `json:"source,omitempty"`
|
|
Title string `json:"title,omitempty"`
|
|
Version int `json:"version,omitempty"`
|
|
Pipelines []PipelineSummary `json:"pipelines,omitempty"`
|
|
PipelineID int64 `json:"pipeline_id,omitempty"`
|
|
Detail *PipelineDetail `json:"detail,omitempty"`
|
|
RunID int64 `json:"run_id,omitempty"`
|
|
RunDetail *RunDetail `json:"run_detail,omitempty"`
|
|
Runs []RunSummary `json:"runs,omitempty"`
|
|
Stage string `json:"stage,omitempty"`
|
|
Option string `json:"option,omitempty"`
|
|
Feedback string `json:"feedback,omitempty"`
|
|
Input map[string]any `json:"input,omitempty"`
|
|
AllowTools bool `json:"allow_tools,omitempty"`
|
|
WritePaths []string `json:"write_paths,omitempty"`
|
|
RequestKey string `json:"request_key,omitempty"`
|
|
Workspace string `json:"workspace,omitempty"`
|
|
// Model and StageModels are the launch-time model overrides: Model applies to
|
|
// every model stage of the run and StageModels overrides individual stages by
|
|
// key (per-stage wins over whole-run). Both are optional so older peers ignore
|
|
// them; the same fields are echoed on the run record.
|
|
Model string `json:"model,omitempty"`
|
|
StageModels map[string]string `json:"stage_models,omitempty"`
|
|
// Search is the broad, read-only search traffic over pipelines, versions,
|
|
// runs, steps, field text and artifacts. See search.go for the per-type
|
|
// payloads and the ranking/kind/bound vocabulary.
|
|
Search *SearchRequest `json:"search,omitempty"`
|
|
SearchResults *SearchResponse `json:"search_results,omitempty"`
|
|
// Chat is the brokered OpenCode chat traffic (route B). See chat.go for the
|
|
// per-type payload structs and the wire shapes they mirror.
|
|
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"`
|
|
ChatEvent *ChatEventFrame `json:"chat_event,omitempty"`
|
|
ChatPermission *ChatPermissionFrame `json:"chat_permission,omitempty"`
|
|
ChatReply *ChatReplyRequest `json:"chat_reply,omitempty"`
|
|
// ChatDirectories carries the operator's directory choices on a
|
|
// chat_directories response (home first, then recent operator-session
|
|
// directories, deduplicated and bounded). Additive/omitempty so older peers
|
|
// ignore it.
|
|
ChatDirectories []string `json:"chat_directories,omitempty"`
|
|
// RunEvent is the server-pushed run state delta (see runevents.go) and
|
|
// RunIDs the optional subscribe scope, so a client asks for the runs it
|
|
// displays. Both are optional so older peers ignore them.
|
|
RunEvent *RunEventFrame `json:"run_event,omitempty"`
|
|
RunIDs []int64 `json:"run_ids,omitempty"`
|
|
}
|