136 lines
4.7 KiB
Go
136 lines
4.7 KiB
Go
package author
|
|
|
|
import (
|
|
"context"
|
|
"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"
|
|
"regexp"
|
|
"time"
|
|
)
|
|
|
|
//go:embed skill/SKILL.md skill/references/format.md
|
|
var content embed.FS
|
|
|
|
// Workspace isolates authoring instructions without changing shared OpenCode config.
|
|
func Workspace(d draft.Draft) (string, error) {
|
|
dir := d.Path + ".author"
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
return "", err
|
|
}
|
|
for _, file := range []string{"SKILL.md", "references/format.md"} {
|
|
data, err := content.ReadFile("skill/" + file)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
path := filepath.Join(dir, ".opencode", "skills", "pipelines", file)
|
|
if err = os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
|
return "", err
|
|
}
|
|
if err = os.WriteFile(path, data, 0600); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
data, err := json.MarshalIndent(d, "", " ")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if err = os.WriteFile(filepath.Join(dir, "DRAFT.json"), data, 0600); err != nil {
|
|
return "", err
|
|
}
|
|
return dir, nil
|
|
}
|
|
|
|
// 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 {
|
|
data, err := json.Marshal(body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
args = append(args, "--data", string(data))
|
|
}
|
|
cmd := exec.CommandContext(ctx, executable, args...)
|
|
cmd.Dir = workspace
|
|
data, err := cmd.Output()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("OpenCode %s %s failed; draft retained: %w", method, path, err)
|
|
}
|
|
return data, nil
|
|
}
|
|
data, err := call("post", "/api/session", map[string]any{"title": "Pipeline draft: " + name, "location": map[string]string{"directory": workspace}})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
var result struct {
|
|
Data struct {
|
|
ID string `json:"id"`
|
|
} `json:"data"`
|
|
}
|
|
if err = json.Unmarshal(data, &result); err != nil || !regexp.MustCompile(`^ses[a-zA-Z0-9_-]+$`).MatchString(result.Data.ID) {
|
|
return "", fmt.Errorf("OpenCode returned an invalid session ID; draft retained")
|
|
}
|
|
id := result.Data.ID
|
|
if _, err = call("post", "/api/experimental/session/"+id+"/skill", map[string]any{"id": "pipelines", "resume": false}); err != nil {
|
|
_, _ = call("delete", "/api/session/"+id, nil)
|
|
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."
|
|
}
|
|
return fmt.Sprintf("@pipelines\nLoad the pipelines skill before editing. Read DRAFT.json. Edit the draft at %q for pipeline %q.\nUse this installation's CLI: %s\nLeave changes as a draft; do not submit or run unless I explicitly request it. Validate using that CLI.\nUser request:\n%s", d.Path, d.Name, command, request)
|
|
}
|