Simplify Go installation and support standard OpenCode releases

This commit is contained in:
2026-10-08 23:17:38 +01:00
parent 0f30a001c2
commit accbe63e39
101 changed files with 1331 additions and 682 deletions
+41 -1
View File
@@ -5,11 +5,13 @@ import (
"embed"
"encoding/json"
"fmt"
"git.a-wan.net/alan/pipelines/internal/draft"
"git.a-wan.net/alan/pipelines/internal/opencode"
"os"
"os/exec"
"path/filepath"
"pipelines/internal/draft"
"regexp"
"time"
)
//go:embed skill/SKILL.md skill/references/format.md
@@ -47,6 +49,14 @@ func Workspace(d draft.Draft) (string, error) {
// PrepareSession explicitly loads the skill without starting a model turn.
// The existing user's CLI handles discovery and authentication; no auth is exported.
func PrepareSession(ctx context.Context, executable, workspace, name string) (string, error) {
connection, err := opencode.Open(ctx, executable, workspace, "")
if err != nil {
return "", err
}
defer connection.Close()
if connection.Backend == "v1" {
return prepareNative(ctx, connection, workspace, name)
}
call := func(method, path string, body any) ([]byte, error) {
args := []string{"api", method, path}
if body != nil {
@@ -82,11 +92,41 @@ func PrepareSession(ctx context.Context, executable, workspace, name string) (st
return "", err
}
if err = os.WriteFile(filepath.Join(workspace, "SESSION"), []byte(id+"\n"), 0600); err != nil {
_, _ = call("delete", "/api/session/"+id, nil)
return "", err
}
return id, nil
}
func prepareNative(ctx context.Context, connection *opencode.Connection, workspace, name string) (id string, err error) {
data, err := connection.Native(ctx, "post", "/session", map[string]string{"title": "Pipeline draft: " + name})
if err != nil {
return "", err
}
var result struct {
ID string `json:"id"`
}
if json.Unmarshal(data, &result) != nil || !regexp.MustCompile(`^ses[a-zA-Z0-9_-]+$`).MatchString(result.ID) {
return "", fmt.Errorf("invalid OpenCode native session")
}
id = result.ID
defer func() {
if err != nil {
cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
defer cancel()
_, _ = connection.Native(cleanup, "delete", "/session/"+id, nil)
}
}()
skill, _ := content.ReadFile("skill/SKILL.md")
reference, _ := content.ReadFile("skill/references/format.md")
instructions := "Pipeline authoring skill (preloaded; no inference requested):\n" + string(skill) + "\nDefinition reference:\n" + string(reference)
if _, err = connection.Native(ctx, "post", "/session/"+id+"/message", map[string]any{"noReply": true, "parts": []map[string]string{{"type": "text", "text": instructions}}}); err != nil {
return "", err
}
err = os.WriteFile(filepath.Join(workspace, "SESSION"), []byte(id+"\n"), 0600)
return id, err
}
func Prompt(d draft.Draft, request, command string) string {
if request == "" {
request = "Ask me what this pipeline should do or what to change, then help me edit its draft."
+3 -3
View File
@@ -2,9 +2,9 @@ package author
import (
"context"
"git.a-wan.net/alan/pipelines/internal/draft"
"os"
"path/filepath"
"pipelines/internal/draft"
"strings"
"testing"
)
@@ -21,7 +21,7 @@ func TestSkillActivationIsExplicitAndNoResume(t *testing.T) {
}
capture := filepath.Join(t.TempDir(), "calls")
script := filepath.Join(t.TempDir(), "opencode")
os.WriteFile(script, []byte("#!/bin/sh\nprintf '%s\\n' \"$@\" >> '"+capture+"'\nif [ \"$3\" = /api/session ]; then printf '%s' '{\"data\":{\"id\":\"ses_test\"}}'; fi\n"), 0700)
os.WriteFile(script, []byte("#!/bin/sh\nif [ \"$1\" = --help ]; then printf '%s\\n' ' api commands'; exit 0; fi\nprintf '%s\\n' \"$@\" >> '"+capture+"'\nif [ \"$3\" = /api/session ]; then printf '%s' '{\"data\":{\"id\":\"ses_test\"}}'; fi\n"), 0700)
id, err := PrepareSession(context.Background(), script, workspace, d.Name)
if err != nil || id != "ses_test" {
t.Fatal(id, err)
@@ -40,7 +40,7 @@ func TestSkillFailureDoesNotLaunchAndCleansOnlyCreatedSession(t *testing.T) {
workspace := t.TempDir()
capture := filepath.Join(workspace, "calls")
script := filepath.Join(workspace, "opencode")
os.WriteFile(script, []byte("#!/bin/sh\nprintf '%s\\n' \"$@\" >> '"+capture+"'\nif [ \"$3\" = /api/session ]; then printf '%s' '{\"data\":{\"id\":\"ses_new\"}}'; exit 0; fi\nif [ \"$2\" = delete ]; then exit 0; fi\nexit 1\n"), 0700)
os.WriteFile(script, []byte("#!/bin/sh\nif [ \"$1\" = --help ]; then printf '%s\\n' ' api commands'; exit 0; fi\nprintf '%s\\n' \"$@\" >> '"+capture+"'\nif [ \"$3\" = /api/session ]; then printf '%s' '{\"data\":{\"id\":\"ses_new\"}}'; exit 0; fi\nif [ \"$2\" = delete ]; then exit 0; fi\nexit 1\n"), 0700)
if _, err := PrepareSession(context.Background(), script, workspace, "name"); err == nil {
t.Fatal("missing skill allowed")
}
+68
View File
@@ -0,0 +1,68 @@
package author
import (
"context"
"encoding/json"
"git.a-wan.net/alan/pipelines/internal/draft"
"git.a-wan.net/alan/pipelines/internal/opencode"
"os"
"strings"
"testing"
"time"
)
func TestStableAuthoringInstructionsPreloadedWithoutInference(t *testing.T) {
executable := os.Getenv("PIPELINES_TEST_OPENCODE_V1")
if executable == "" {
t.Skip("set PIPELINES_TEST_OPENCODE_V1 for no-inference authoring preload test")
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_CONFIG_HOME", home+"/config")
t.Setenv("XDG_DATA_HOME", home+"/data")
t.Setenv("XDG_CACHE_HOME", home+"/cache")
t.Setenv("XDG_STATE_HOME", home+"/state")
t.Setenv("PIPELINES_DRAFT_DIR", home+"/drafts")
d, err := draft.Create("native authoring", "draft source", "ws://localhost", true, 0)
if err != nil {
t.Fatal(err)
}
workspace, err := Workspace(d)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
id, err := PrepareSession(ctx, executable, workspace, d.Name)
if err != nil {
t.Fatal(err)
}
c, err := opencode.Open(ctx, executable, workspace, "")
if err != nil {
t.Fatal(err)
}
defer c.Close()
defer c.Native(context.Background(), "delete", "/session/"+id, nil)
data, err := c.Native(ctx, "get", "/session/"+id+"/message", nil)
if err != nil {
t.Fatal(err)
}
var messages []struct {
Info struct {
Role string `json:"role"`
} `json:"info"`
Parts []struct{ Type, Text string } `json:"parts"`
}
if json.Unmarshal(data, &messages) != nil || len(messages) != 1 || messages[0].Info.Role != "user" {
t.Fatal("preload ran inference or missing message", string(data))
}
text := messages[0].Parts[0].Text
if !strings.Contains(text, "Do not submit or run by default") && !strings.Contains(text, "Do not submit or run") {
if !strings.Contains(text, "Do not submit") {
t.Fatal("draft-only instruction not preloaded", text)
}
}
if !strings.Contains(text, "Definition reference") || !strings.Contains(text, "provider=opencode-go") {
t.Fatal("format reference not preloaded")
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
---
name: Pipeline authoring
name: pipelines
description: Create or edit validated pipeline Markdown drafts with the pipelines CLI; preserve immutable versions and require explicit submission.
---