96 lines
3.1 KiB
Go
96 lines
3.1 KiB
Go
package author
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"pipelines/internal/draft"
|
|
"regexp"
|
|
)
|
|
|
|
//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) {
|
|
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 {
|
|
return "", err
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
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)
|
|
}
|