Simplify Go installation and support standard OpenCode releases
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -23,11 +25,35 @@ func (c OpenCodeClient) Interrupt(ctx context.Context, id string) error {
|
||||
if !sessionIDPattern.MatchString(id) {
|
||||
return fmt.Errorf("invalid session ID")
|
||||
}
|
||||
if c.command == nil && c.connection == nil {
|
||||
var connection *opencode.Connection
|
||||
var err error
|
||||
if c.Backend == "v1" && c.Reference != "" {
|
||||
connection, err = opencode.FromReference(c.Reference)
|
||||
} else {
|
||||
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
|
||||
}
|
||||
_, err := c.call(ctx, "post", "/api/session/"+id+"/interrupt", map[string]any{})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *AgentClient) 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
|
||||
}
|
||||
rules := c.permissionRules()
|
||||
data, err := c.call(ctx, "post", "/api/session", map[string]any{"title": "Pipeline audited agent stage", "agent": "build", "location": map[string]string{"directory": c.Directory}, "model": map[string]string{"providerID": config.Provider, "id": config.Model}, "permissions": rules})
|
||||
if err != nil {
|
||||
@@ -42,8 +68,22 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s
|
||||
return "", fmt.Errorf("invalid agent session")
|
||||
}
|
||||
c.SessionID = created.Data.ID
|
||||
if c.connection != nil && c.Backend == "v1" {
|
||||
c.Reference, err = c.connection.Reference()
|
||||
if err != nil {
|
||||
cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 15*time.Second)
|
||||
defer cancel()
|
||||
_, _ = c.call(cleanup, "delete", "/api/session/"+c.SessionID, nil)
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
if c.OnSession != nil {
|
||||
if err := c.OnSession(c.SessionID, true); err != nil {
|
||||
cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 15*time.Second)
|
||||
defer cancel()
|
||||
if _, cleanupErr := c.call(cleanup, "delete", "/api/session/"+c.SessionID, nil); cleanupErr != nil {
|
||||
return "", fmt.Errorf("agent ownership could not be recorded and session cleanup failed")
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
@@ -60,8 +100,12 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s
|
||||
if persistErr := c.OnSession(c.SessionID, false); persistErr != nil {
|
||||
err = persistErr
|
||||
text = ""
|
||||
return
|
||||
}
|
||||
}
|
||||
if c.Reference != "" {
|
||||
_ = os.Remove(c.Reference)
|
||||
}
|
||||
}()
|
||||
tools := c.Tools
|
||||
if tools == nil {
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -33,7 +33,7 @@ func ToolNames(def Definition) ([]string, bool, error) {
|
||||
return tools, true, nil
|
||||
}
|
||||
|
||||
var verificationCommands = []string{"go test ./...", "go test -count=1 ./...", "go test -race ./...", "go test -race -count=1 ./...", "go vet ./...", "go build ./cmd/server ./cmd/tui"}
|
||||
var verificationCommands = []string{"go test ./...", "go test -count=1 ./...", "go test -race ./...", "go test -race -count=1 ./...", "go vet ./...", "go build ./cmd/..."}
|
||||
|
||||
func (c *AgentClient) permissionRules() []map[string]string {
|
||||
rules := []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}}
|
||||
|
||||
Reference in New Issue
Block a user