108c29f9eb
Checks the CLI on the server's PATH (install command when missing), signs in with the harness's own login only when needed (--login forces it), keeps the OpenCode/Pi model pickers, and verifies with the server's check. Works on native and Docker installs (Docker delegates to the container setup); --host is a hidden compatibility alias. harness list shows all four harnesses' installed/signed-in/configured/default status. ./install --opencode|--pi now point to the command; ./install ends with the next-step hint.
177 lines
4.7 KiB
Go
177 lines
4.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/author"
|
|
"git.a-wan.net/alan/pipelines/internal/harness"
|
|
)
|
|
|
|
func (a App) harness(ctx context.Context, args []string) error {
|
|
if len(args) > 0 && args[0] == "list" {
|
|
return a.harnessList(ctx, args[1:])
|
|
}
|
|
if len(args) > 0 && args[0] == "default" {
|
|
c, err := harness.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
names, _, err := harness.Choices()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
name := ""
|
|
if len(args) == 2 {
|
|
name = args[1]
|
|
} else if len(args) == 1 {
|
|
name, err = harness.Select(ctx, "Default harness", names, a.In, a.Err)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
return fmt.Errorf("usage: harness default [opencode|pi]")
|
|
}
|
|
if _, ok := c.Profiles[name]; !ok {
|
|
return fmt.Errorf("harness %q is not configured", name)
|
|
}
|
|
c.Default = name
|
|
return harness.Save(c)
|
|
}
|
|
if len(args) > 0 && args[0] == "link" {
|
|
f := flag.NewFlagSet("harness link", flag.ContinueOnError)
|
|
f.SetOutput(a.Err)
|
|
directory := f.String("docker-directory", "", "Existing Docker installation checkout")
|
|
model := f.String("model", "", "Configured interactive model")
|
|
pipelineModel := f.String("pipeline-model", "", "Configured new-pipeline model")
|
|
makeDefault := f.Bool("default", false, "Set default harness")
|
|
if err := parse(f, args[1:]); err != nil {
|
|
return err
|
|
}
|
|
if len(f.Args()) != 1 || (f.Args()[0] != "pi" && f.Args()[0] != "opencode") || !harness.ValidModel(*model) || !harness.ValidModel(*pipelineModel) {
|
|
return fmt.Errorf("harness link requires opencode|pi and exact --model/--pipeline-model")
|
|
}
|
|
absolute, err := filepath.Abs(*directory)
|
|
if err != nil || *directory == "" {
|
|
return fmt.Errorf("Docker installation directory is required")
|
|
}
|
|
if err := harness.VerifyDockerLink(absolute); err != nil {
|
|
return err
|
|
}
|
|
c, err := harness.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
name := f.Args()[0]
|
|
c.Profiles[name] = harness.Profile{Executable: name, Model: *model, PipelineModel: *pipelineModel, DockerDirectory: absolute}
|
|
if *makeDefault || c.Default == "" {
|
|
c.Default = name
|
|
}
|
|
return harness.Save(c)
|
|
}
|
|
if len(args) > 0 && args[0] == "setup" {
|
|
return a.harnessSetup(ctx, args[1:])
|
|
}
|
|
name := ""
|
|
if len(args) > 0 && (args[0] == "opencode" || args[0] == "pi") {
|
|
name = args[0]
|
|
args = args[1:]
|
|
}
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
skill, err := author.SkillDirectory()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cmd, err := harness.Command(ctx, name, cwd, strings.Join(args, " "), skill)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cmd.Stdin = a.In
|
|
cmd.Stdout = a.Out
|
|
cmd.Stderr = a.Err
|
|
return cmd.Run()
|
|
}
|
|
|
|
func harnessModels(ctx context.Context, name, executable string) ([]string, error) {
|
|
if name == "opencode" {
|
|
if _, err := exec.CommandContext(ctx, executable, "api", "get", "/api/integration").Output(); err != nil {
|
|
return nil, fmt.Errorf("OpenCode provider discovery failed: %w", err)
|
|
}
|
|
}
|
|
args := []string{"models"}
|
|
if name == "pi" {
|
|
args = []string{"--no-extensions", "--list-models"}
|
|
}
|
|
data, err := exec.CommandContext(ctx, executable, args...).Output()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s model discovery failed: %w", name, err)
|
|
}
|
|
if name == "opencode" {
|
|
for attempt := 0; len(strings.TrimSpace(string(data))) == 0 && attempt < 2; attempt++ {
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
case <-time.After(500 * time.Millisecond):
|
|
}
|
|
data, err = exec.CommandContext(ctx, executable, args...).Output()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
var models []string
|
|
seen := map[string]bool{}
|
|
for _, line := range strings.Split(string(data), "\n") {
|
|
fields := strings.Fields(line)
|
|
if len(fields) == 0 {
|
|
continue
|
|
}
|
|
value := fields[0]
|
|
if name == "pi" && len(fields) > 1 {
|
|
value = fields[0] + "/" + fields[1]
|
|
}
|
|
if harness.ValidModel(value) && !seen[value] && value != "provider/model" {
|
|
seen[value] = true
|
|
models = append(models, value)
|
|
}
|
|
}
|
|
if name == "opencode" {
|
|
data, err := exec.CommandContext(ctx, executable, "auth", "list", "--format", "json").Output()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("OpenCode connected-provider discovery failed: %w", err)
|
|
}
|
|
var accounts []struct {
|
|
ID string `json:"id"`
|
|
Connections []json.RawMessage `json:"connections"`
|
|
}
|
|
if err = json.Unmarshal(data, &accounts); err != nil {
|
|
return nil, err
|
|
}
|
|
connected := map[string]bool{}
|
|
for _, a := range accounts {
|
|
if len(a.Connections) > 0 {
|
|
connected[a.ID] = true
|
|
}
|
|
}
|
|
filtered := models[:0]
|
|
for _, m := range models {
|
|
p, _, _ := strings.Cut(m, "/")
|
|
if connected[p] {
|
|
filtered = append(filtered, m)
|
|
}
|
|
}
|
|
models = filtered
|
|
}
|
|
return models, nil
|
|
}
|