Simplify Go installation and support standard OpenCode releases
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -17,13 +18,16 @@ import (
|
||||
const maxModelRequest = 64 << 10 // CLI request body is passed as one OS argument.
|
||||
const maxModelResponse = 1 << 20
|
||||
|
||||
// OpenCodeClient reuses OpenCode V2's service discovery and authenticated accounts.
|
||||
// OpenCodeClient reuses the selected OpenCode CLI and its authenticated accounts.
|
||||
// No API keys are copied into pipeline definitions, subprocess arguments, or logs.
|
||||
// A fresh location-scoped session avoids unrelated user conversation history and
|
||||
// is deleted after generation. Generation is text-only: no agent tools are run.
|
||||
type OpenCodeClient struct {
|
||||
Directory string // empty uses the pipeline server's working directory
|
||||
command func(context.Context, string, ...string) ([]byte, error)
|
||||
Directory string // empty uses the pipeline server's working directory
|
||||
Backend string
|
||||
Reference string
|
||||
connection *opencode.Connection
|
||||
command func(context.Context, string, ...string) ([]byte, error)
|
||||
}
|
||||
|
||||
type openCodeError struct {
|
||||
@@ -48,6 +52,20 @@ var sessionIDPattern = regexp.MustCompile(`^ses[A-Za-z0-9_-]+$`)
|
||||
var httpStatusPattern = regexp.MustCompile(`HTTP (\d{3})`)
|
||||
|
||||
func (c OpenCodeClient) call(ctx context.Context, method, path string, body any) ([]byte, error) {
|
||||
if c.connection != nil && c.connection.Backend == "v1" {
|
||||
data, err := c.connection.Call(ctx, method, path, body)
|
||||
if err != nil {
|
||||
status := 0
|
||||
if match := httpStatusPattern.FindStringSubmatch(err.Error()); len(match) > 0 {
|
||||
status, _ = strconv.Atoi(match[1])
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
return nil, &openCodeError{operation: method + " " + path, status: status, cause: err}
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
args := []string{"api", method, path}
|
||||
if body != nil {
|
||||
data, err := json.Marshal(body)
|
||||
@@ -70,7 +88,7 @@ func (c OpenCodeClient) call(ctx context.Context, method, path string, body any)
|
||||
if c.command != nil {
|
||||
return c.command(ctx, directory, args...)
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, "opencode", args...)
|
||||
cmd := exec.CommandContext(ctx, opencode.Executable(), args...)
|
||||
cmd.Dir = directory
|
||||
stdout := &boundedBuffer{limit: maxModelResponse}
|
||||
stderr := &boundedBuffer{limit: 8 << 10}
|
||||
@@ -94,6 +112,15 @@ func (c OpenCodeClient) call(ctx context.Context, method, path string, body any)
|
||||
}
|
||||
|
||||
func (c OpenCodeClient) Generate(ctx context.Context, config ModelConfig, prompt string) (text string, err error) {
|
||||
if c.command == nil {
|
||||
connection, err := opencode.Open(ctx, opencode.Executable(), c.Directory, c.Backend)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer connection.Close()
|
||||
c.connection = connection
|
||||
c.Backend = connection.Backend
|
||||
}
|
||||
directory := c.Directory
|
||||
if directory == "" {
|
||||
directory, err = os.Getwd()
|
||||
|
||||
Reference in New Issue
Block a user