// Package opencode adapts the two published OpenCode API families without installing a CLI. package opencode import ( "bufio" "bytes" "context" "crypto/rand" "encoding/hex" "encoding/json" "fmt" "io" "net/http" "net/url" "os" "os/exec" "path/filepath" "regexp" "strings" "syscall" "time" ) const MaxRequest = 64 << 10 const MaxResponse = 1 << 20 type Connection struct { Backend, Executable, Directory string URL string password string process *exec.Cmd done chan error models map[string]map[string]string responses map[string][]byte formatChecked bool formatSupported bool FormatProbe *FormatProbe LastHTTPStatus int LastHTTPOperation string MessageReadbackDiagnostic string } // Reference records only our ephemeral loopback service credentials, never provider auth. // The private file lets crash recovery abort the same owned service, not a new instance. func (c *Connection) Reference() (string, error) { cache, err := os.UserCacheDir() if err != nil { return "", err } dir := filepath.Join(cache, "pipelines", "private") if err = os.MkdirAll(dir, 0700); err != nil { return "", err } f, err := os.CreateTemp(dir, "opencode-connection-*.json") if err != nil { return "", err } data, _ := json.Marshal(map[string]string{"url": c.URL, "password": c.password, "directory": c.Directory}) _, writeErr := f.Write(data) closeErr := f.Close() if writeErr != nil { return "", writeErr } if closeErr != nil { return "", closeErr } return f.Name(), nil } func FromReference(path string) (*Connection, error) { info, err := os.Lstat(path) if err != nil { return nil, err } if !info.Mode().IsRegular() || info.Mode().Perm() != 0600 || info.Size() > MaxRequest { return nil, fmt.Errorf("invalid private OpenCode connection reference") } if stat, ok := info.Sys().(*syscall.Stat_t); !ok || stat.Uid != uint32(os.Getuid()) { return nil, fmt.Errorf("OpenCode reference belongs to another user") } data, err := os.ReadFile(path) if err != nil { return nil, err } var reference struct{ URL, Password, Directory string } if err = json.Unmarshal(data, &reference); err != nil { return nil, err } u, err := url.Parse(reference.URL) if err != nil || u.Scheme != "http" || u.Hostname() != "127.0.0.1" || u.Port() == "" || u.Path != "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || len(reference.Password) != 64 { return nil, fmt.Errorf("invalid owned OpenCode service endpoint") } return &Connection{Backend: "v1", URL: reference.URL, password: reference.Password, Directory: reference.Directory}, nil } func Executable() string { if path := os.Getenv("PIPELINES_OPENCODE"); path != "" { return path } return "opencode" } // Detect checks the advertised command interface, not an assumed release number. func Detect(ctx context.Context, executable string) (string, error) { cmd := CommandContext(ctx, executable, "--help") data, err := cmd.CombinedOutput() if err != nil { // Preserve the caller's cancellation/deadline identity; a parent that // exits 0 while a descendant still holds the combined-output pipe is // never parsed from its partial stdout. if ctx.Err() != nil { return "", ctx.Err() } return "", fmt.Errorf("cannot run the selected OpenCode CLI: %w", err) } if regexp.MustCompile(`(?m)^\s*(?:opencode\s+)?api\s`).Match(data) { return "v2", nil } if regexp.MustCompile(`(?m)^\s*(?:opencode\s+)?serve\s`).Match(data) { return "v1", nil } return "", fmt.Errorf("selected OpenCode CLI advertises neither api nor serve") } func Open(ctx context.Context, executable, directory, backend string) (*Connection, error) { c, err := newConnection(ctx, executable, directory, backend) if err != nil { return nil, err } // The V2 CLI talks to its own managed service, so stages reach it through the // `api` subcommand and Open returns no owned process for them. Only // OpenStream (brokered chat) opens the owned V2 endpoint. if c.Backend == "v2" { return c, nil } if err := c.startServe(ctx); err != nil { return nil, err } return c, nil } // OpenStream opens the single owned `opencode serve` endpoint for either backend // so a caller that needs a URL-bearing, SSE-capable connection (brokered chat) // gets one on the installed V2 CLI too. The V2 server exposes the authenticated // SSE event firehose at /api/event and the /api HTTP surface; the native serve // exposes /event and the translated /api surface. Stage generation keeps using // Open (the unstarted V2 connection plus its `api` subcommand), so this starts // no process for it and there is never a second transport. func OpenStream(ctx context.Context, executable, directory, backend string) (*Connection, error) { c, err := newConnection(ctx, executable, directory, backend) if err != nil { return nil, err } if err := c.startServe(ctx); err != nil { return nil, err } return c, nil } // OpenChat detects the installed backend and opens the owned, URL-bearing // connection the brokered chat adapter streams from. main and the chat live test // call this one selection function so their Detect/Open logic cannot drift. func OpenChat(ctx context.Context, executable, directory string) (*Connection, error) { if executable == "" { executable = Executable() } backend, err := Detect(ctx, executable) if err != nil { return nil, err } return OpenStream(ctx, executable, directory, backend) } // newConnection resolves the executable/directory defaults, detects the backend // when it is empty, and returns the bare, not-yet-opened connection. It is the // shared construction path for Open and OpenStream so backend validation lives // once. func newConnection(ctx context.Context, executable, directory, backend string) (*Connection, error) { if executable == "" { executable = Executable() } if directory == "" { var err error directory, err = os.Getwd() if err != nil { return nil, err } } if backend == "" { var err error backend, err = Detect(ctx, executable) if err != nil { return nil, err } } if backend != "v1" && backend != "v2" { return nil, fmt.Errorf("unknown OpenCode backend %q", backend) } return &Connection{Backend: backend, Executable: executable, Directory: directory, models: map[string]map[string]string{}, responses: map[string][]byte{}}, nil } // serverUser is the HTTP basic-auth username the owned serve process expects. // The native serve authenticates the fixed "pipelines" user; the V2 API server // authenticates the fixed "opencode" user regardless of // OPENCODE_SERVER_USERNAME, verified against the installed 2.0.20 binary. func (c *Connection) serverUser() string { if c.Backend == "v2" { return "opencode" } return "pipelines" } // startServe launches one owned `opencode serve` process on loopback, injects the // ephemeral password, discovers the URL and verifies the backend-specific health // contract. Close terminates it. It is idempotent so a connection only ever owns // one process. func (c *Connection) startServe(ctx context.Context) error { if c.process != nil { return nil } secret := make([]byte, 32) if _, err := rand.Read(secret); err != nil { return err } c.password = hex.EncodeToString(secret) c.process = exec.Command(c.Executable, "serve", "--hostname", "127.0.0.1", "--port", "0") c.process.Dir = c.Directory for _, entry := range os.Environ() { if !strings.HasPrefix(entry, "OPENCODE_SERVER_PASSWORD=") && !strings.HasPrefix(entry, "OPENCODE_SERVER_USERNAME=") { c.process.Env = append(c.process.Env, entry) } } c.process.Env = append(c.process.Env, "OPENCODE_SERVER_PASSWORD="+c.password, "OPENCODE_SERVER_USERNAME="+c.serverUser()) stdout, err := c.process.StdoutPipe() if err != nil { return err } c.process.Stderr = io.Discard if err = c.process.Start(); err != nil { return err } c.done = make(chan error, 1) go func() { c.done <- c.process.Wait() }() address := make(chan string, 1) go func() { scanner := bufio.NewScanner(stdout) pattern := regexp.MustCompile(`http://127\.0\.0\.1:[0-9]+`) sent := false for scanner.Scan() { if !sent { if found := pattern.FindString(scanner.Text()); found != "" { address <- found sent = true } } } }() startup, stop := context.WithTimeout(ctx, 20*time.Second) defer stop() select { case c.URL = <-address: case <-c.done: c.process = nil return fmt.Errorf("OpenCode serve exited before becoming ready") case <-startup.Done(): c.Close() return fmt.Errorf("OpenCode serve startup: %w", startup.Err()) } if err := c.checkHealth(startup); err != nil { c.Close() return err } return nil } // checkHealth verifies the backend-specific readiness contract after the serve // process prints its URL: the native serve answers /global/health, the V2 API // server answers /api/info. Both use the same owned credentials the rest of the // adapter uses, so a reachable but unauthorized endpoint is not mistaken for a // healthy one. func (c *Connection) checkHealth(ctx context.Context) error { if c.Backend == "v2" { info, err := c.Native(ctx, "get", "/api/info", nil) var result struct { Version string `json:"version"` URLs []string `json:"urls"` } if err != nil || json.Unmarshal(info, &result) != nil || result.Version == "" { return fmt.Errorf("OpenCode serve did not return the expected V2 info contract") } return nil } health, err := c.Native(ctx, "get", "/global/health", nil) var result struct { Healthy bool `json:"healthy"` Version string `json:"version"` } if err != nil || json.Unmarshal(health, &result) != nil || !result.Healthy || result.Version == "" { return fmt.Errorf("OpenCode serve did not return the expected native health contract") } return nil } func (c *Connection) Close() { if c.process == nil { return } _ = c.process.Process.Signal(syscall.SIGTERM) select { case <-c.done: case <-time.After(5 * time.Second): _ = c.process.Process.Kill() <-c.done } c.process = nil } func (c *Connection) Native(ctx context.Context, method, path string, body any) ([]byte, error) { return c.nativeRequest(ctx, method, path, body, c.Directory) } // nativeRequest is the shared Native request path. An empty directory omits the // ?directory= query parameter; the V2 session list then returns sessions across // every directory instead of only the connection's own. func (c *Connection) nativeRequest(ctx context.Context, method, path string, body any, directory string) ([]byte, error) { c.LastHTTPStatus = 0 c.LastHTTPOperation = strings.ToUpper(method) + " " + path u, err := url.Parse(c.URL + path) if err != nil { return nil, err } if directory != "" { query := u.Query() query.Set("directory", directory) u.RawQuery = query.Encode() } var data []byte if body != nil { data, err = json.Marshal(body) if err != nil { return nil, err } if len(data) > MaxRequest { return nil, fmt.Errorf("OpenCode request exceeds 64 KiB") } } request, err := http.NewRequestWithContext(ctx, strings.ToUpper(method), u.String(), bytes.NewReader(data)) if err != nil { return nil, err } request.Header.Set("Content-Type", "application/json") request.SetBasicAuth(c.serverUser(), c.password) response, err := http.DefaultClient.Do(request) if err != nil { if ctx.Err() != nil { return nil, ctx.Err() } return nil, fmt.Errorf("OpenCode native API request failed") } defer response.Body.Close() c.LastHTTPStatus = response.StatusCode if response.StatusCode < 200 || response.StatusCode >= 300 { // The stage adapter stores bounded, redacted failure evidence separately; // the public error string remains free of provider bodies and credentials. evidence, _ := io.ReadAll(io.LimitReader(response.Body, 16<<10)) return evidence, fmt.Errorf("OpenCode %s %s failed (HTTP %d)", method, path, response.StatusCode) } // A successful no-content operation (the permission-reply endpoint returns // 204) carries no JSON body to validate. if response.StatusCode == http.StatusNoContent { return nil, nil } if !strings.HasPrefix(response.Header.Get("Content-Type"), "application/json") { return nil, fmt.Errorf("OpenCode %s %s returned a non-JSON API response", method, path) } data, err = io.ReadAll(io.LimitReader(response.Body, MaxResponse+1)) if err != nil { return nil, err } if len(data) > MaxResponse { return nil, fmt.Errorf("OpenCode response exceeds 1 MiB") } if !json.Valid(data) { return nil, fmt.Errorf("OpenCode returned invalid JSON") } return data, nil } // EventStream opens the authenticated SSE firehose for the connection's owned // endpoint using the same URL and credentials as Native, so chat never builds a // parallel HTTP client. The path follows the backend surface: the native serve // stream is /event, the V2 HTTP API is /api/event. The caller closes the body. func (c *Connection) EventStream(ctx context.Context) (io.ReadCloser, error) { if c == nil || c.URL == "" { return nil, fmt.Errorf("OpenCode chat requires an event-stream endpoint") } path := "/api/event" if c.Backend == "v1" { path = "/event" } request, err := http.NewRequestWithContext(ctx, http.MethodGet, strings.TrimSuffix(c.URL, "/")+path, nil) if err != nil { return nil, err } request.Header.Set("Accept", "text/event-stream") request.SetBasicAuth(c.serverUser(), c.password) response, err := http.DefaultClient.Do(request) if err != nil { if ctx.Err() != nil { return nil, ctx.Err() } return nil, fmt.Errorf("OpenCode event stream request failed") } if response.StatusCode < 200 || response.StatusCode >= 300 { evidence, _ := io.ReadAll(io.LimitReader(response.Body, 16<<10)) response.Body.Close() return nil, fmt.Errorf("OpenCode event stream failed (HTTP %d): %s", response.StatusCode, strings.TrimSpace(string(evidence))) } if !strings.HasPrefix(response.Header.Get("Content-Type"), "text/event-stream") { response.Body.Close() return nil, fmt.Errorf("OpenCode event stream returned a non-SSE response") } return response.Body, nil }