What: connect's result is taken from the module as JSON text (moejs AppendJSON keeps JS insertion order) instead of rt.ToGo, which made a Go map at every level. An object or array connect returns reaches the writer as a json.RawMessage, so edit.SetJSON writes it in the plugin's order; strings, numbers, booleans and null are their Go values as before (the gateway-key mark still matches a string). edit.SetYAML now takes a json.RawMessage too, as a block-style node in the text's order (it would have written the bytes as a list of ints), and the env format writes it compact as before. Why: Kilo's agent plugin (magpie-community/plugins#90) returns each model's effort variants weakest first, low, medium, high, the order Kilo lists them in. Go maps marshal sorted, so Kilo showed high, low, medium. magpie's own OpenCode wiring already keeps order (openCodeVariants). The key paths connect returns are still applied in sorted order: each is a separate edit, and the user's file around them is untouched. Siblings: rt.ToGo's other caller (middleware's string lists) writes no file; the Library's MCP block for a plugin agent is built by magpie, not returned by the plugin. Run: - TestPluggedKeepsPluginOrder (Kilo-shaped connect, a kilo.jsonc with a comment, a theme, $schema, a provider of the user's own with keys zeta, alpha and a trailing key) and TestPluggedKeepsPluginOrderYAMLAndEnv. With agentplug.go put back: FAIL, variants written high, low, medium and provider keys sorted. With yaml.go put back: FAIL, the object written as a list of byte values. Both pass with the change. - gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., the three builds: OK. - go test -tags nogui -timeout 40m ./... under a scratch HOME: all ok. (A first run with the default 10m timeout timed out in internal/gateway while four other sessions' suites ran on this box and the disk was full; the gateway package took 973s under that load and passed.) - Real use, sandbox HOME: this build, `magpie plugin add` of packages/kilo at plugins PR #90 head b67360cb7, a provider whose fake-chat has efforts stored high, low, medium; `magpie kilo magpie/fakeup/fake-chat` wrote variants low, medium, high with the user's comment, $schema, theme and own provider in place, and @kilocode/cli 7.8.8 `kilo models magpie --verbose` lists them low, medium, high. `magpie kilo default` put the file back; the only difference is Kilo's own one-time permission migration.
524 lines
14 KiB
Go
524 lines
14 KiB
Go
package edit
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/tidwall/jsonc"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Nested YAML keys are edited through yaml.v3's node tree, which keeps
|
|
// comments and key order; indentation comes out as two spaces.
|
|
|
|
// GetYAML reads a dot-separated key path to a scalar from a YAML file.
|
|
func GetYAML(path, keyPath string) (string, bool) {
|
|
root, err := loadYAML(path)
|
|
if err != nil || root == nil {
|
|
return "", false
|
|
}
|
|
n := lookupYAML(root, strings.Split(keyPath, "."))
|
|
if n == nil || n.Kind != yaml.ScalarNode {
|
|
return "", false
|
|
}
|
|
return n.Value, true
|
|
}
|
|
|
|
// GetYAMLMap reads the scalar entries of the mapping at a key path.
|
|
func GetYAMLMap(path, keyPath string) map[string]string {
|
|
root, err := loadYAML(path)
|
|
if err != nil || root == nil {
|
|
return nil
|
|
}
|
|
n := lookupYAML(root, strings.Split(keyPath, "."))
|
|
if n == nil || n.Kind != yaml.MappingNode {
|
|
return nil
|
|
}
|
|
out := map[string]string{}
|
|
for i := 0; i+1 < len(n.Content); i += 2 {
|
|
if v := n.Content[i+1]; v.Kind == yaml.ScalarNode {
|
|
out[n.Content[i].Value] = v.Value
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// GetYAMLList reads the scalar items of the list at a key path.
|
|
func GetYAMLList(path, keyPath string) []string {
|
|
root, err := loadYAML(path)
|
|
if err != nil || root == nil {
|
|
return nil
|
|
}
|
|
n := lookupYAML(root, strings.Split(keyPath, "."))
|
|
if n == nil || n.Kind != yaml.SequenceNode {
|
|
return nil
|
|
}
|
|
var out []string
|
|
for _, c := range n.Content {
|
|
if c.Kind == yaml.ScalarNode {
|
|
out = append(out, c.Value)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// GetYAMLText reads the value at a key path as the YAML it is written in —
|
|
// a list flow ("[a, b]") or block as it is — for SetYAML to put back as a
|
|
// YAMLText.
|
|
func GetYAMLText(path, keyPath string) (string, bool) {
|
|
root, err := loadYAML(path)
|
|
if err != nil || root == nil {
|
|
return "", false
|
|
}
|
|
n := lookupYAML(root, strings.Split(keyPath, "."))
|
|
if n == nil {
|
|
return "", false
|
|
}
|
|
b, err := yaml.Marshal(n)
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
return strings.TrimSuffix(string(b), "\n"), true
|
|
}
|
|
|
|
// YAMLText, as a KV's value, is set as the YAML it reads as rather than as
|
|
// a string: a list read with GetYAMLText goes back written as it was.
|
|
type YAMLText string
|
|
|
|
// EditYAMLTextStrings is EditYAMLStrings on YAML text (a YAMLText) rather
|
|
// than a file: fn's answers in place of every string in it. Invalid input
|
|
// or output returns the original text and an error.
|
|
func EditYAMLTextStrings(text YAMLText, fn func(string) string) (YAMLText, error) {
|
|
root, err := parseYAMLDocument([]byte(text))
|
|
if err != nil || root == nil {
|
|
return text, err
|
|
}
|
|
if !mapStrings(root, fn) {
|
|
return text, nil
|
|
}
|
|
b, err := yaml.Marshal(root)
|
|
if err != nil {
|
|
return text, err
|
|
}
|
|
if _, err := parseYAMLDocument(b); err != nil {
|
|
return text, err
|
|
}
|
|
return YAMLText(strings.TrimSuffix(string(b), "\n")), nil
|
|
}
|
|
|
|
// SetYAML sets key paths in a YAML file; a value may be a scalar, a map,
|
|
// a slice, a struct with yaml tags or YAMLText. Missing files and parents
|
|
// are created. Only the first mapping document can be edited, with empty or
|
|
// null trailing documents allowed; invalid input or output leaves the file
|
|
// untouched.
|
|
func SetYAML(path string, kvs ...KV) error {
|
|
root, err := loadYAMLForEdit(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if root == nil {
|
|
root = &yaml.Node{Kind: yaml.MappingNode}
|
|
}
|
|
for _, kv := range kvs {
|
|
var v yaml.Node
|
|
if t, ok := kv.Value.(YAMLText); ok {
|
|
n, err := parseYAMLDocument([]byte(t))
|
|
if err != nil {
|
|
return fmt.Errorf("%s: %s: %w", path, kv.Path, err)
|
|
}
|
|
if n == nil {
|
|
return fmt.Errorf("%s: %s: not YAML: %q", path, kv.Path, string(t))
|
|
}
|
|
v = *n
|
|
} else if r, ok := kv.Value.(json.RawMessage); ok {
|
|
n, err := yamlOfJSON(r)
|
|
if err != nil {
|
|
return fmt.Errorf("%s: %s: %w", path, kv.Path, err)
|
|
}
|
|
v = *n
|
|
} else if err := v.Encode(kv.Value); err != nil {
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
setYAML(root, strings.Split(kv.Path, "."), &v)
|
|
}
|
|
return writeYAML(path, root)
|
|
}
|
|
|
|
// yamlOfJSON is JSON text as a YAML node in block style, its object keys in
|
|
// the order the text has them, where encoding a Go map would sort them.
|
|
func yamlOfJSON(r json.RawMessage) (*yaml.Node, error) {
|
|
if !json.Valid(r) {
|
|
return nil, fmt.Errorf("not JSON: %q", string(r))
|
|
}
|
|
var doc yaml.Node
|
|
if err := yaml.Unmarshal(r, &doc); err != nil {
|
|
return nil, err
|
|
}
|
|
if doc.Kind != yaml.DocumentNode || len(doc.Content) != 1 {
|
|
return nil, fmt.Errorf("not one JSON value: %q", string(r))
|
|
}
|
|
var block func(n *yaml.Node)
|
|
block = func(n *yaml.Node) {
|
|
// a string keeps its tag, so the encoder still quotes one that
|
|
// would read as another type ("true", "1")
|
|
n.Style = 0
|
|
for _, c := range n.Content {
|
|
block(c)
|
|
}
|
|
}
|
|
n := doc.Content[0]
|
|
block(n)
|
|
return n, nil
|
|
}
|
|
|
|
// DelYAML removes key paths from a YAML file. A missing file stays missing.
|
|
func DelYAML(path string, keyPaths ...string) error {
|
|
root, err := loadYAMLForEdit(path)
|
|
if err != nil || root == nil {
|
|
return err
|
|
}
|
|
changed := false
|
|
for _, kp := range keyPaths {
|
|
parts := strings.Split(kp, ".")
|
|
parent := root
|
|
if len(parts) > 1 {
|
|
parent = lookupYAML(root, parts[:len(parts)-1])
|
|
}
|
|
if parent == nil || parent.Kind != yaml.MappingNode {
|
|
continue
|
|
}
|
|
key := parts[len(parts)-1]
|
|
for i := 0; i+1 < len(parent.Content); i += 2 {
|
|
if parent.Content[i].Value == key {
|
|
parent.Content = append(parent.Content[:i], parent.Content[i+2:]...)
|
|
changed = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if !changed {
|
|
return nil
|
|
}
|
|
return writeYAML(path, root)
|
|
}
|
|
|
|
// EditYAMLNode hands fn the node at a key path, nil when there is none, to
|
|
// change in place: a list grown by one item keeps the comments on the rest,
|
|
// where SetYAML would write the whole value anew. fn says whether it changed
|
|
// the node, and whether the key is to go instead. The file is written only
|
|
// on a change.
|
|
func EditYAMLNode(path, keyPath string, fn func(n *yaml.Node) (changed, remove bool)) error {
|
|
root, err := loadYAMLForEdit(path)
|
|
if err != nil || root == nil {
|
|
return err
|
|
}
|
|
changed, remove := fn(lookupYAML(root, strings.Split(keyPath, ".")))
|
|
switch {
|
|
case remove:
|
|
return DelYAML(path, keyPath)
|
|
case changed:
|
|
return writeYAML(path, root)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditYAMLStrings calls fn on every string under the key paths — mapping
|
|
// keys and values, sequence items, at any depth — and puts fn's answer in
|
|
// its place, comments and quoting kept. The file is written only when an
|
|
// answer differs, so an fn that answers what it is given only reads.
|
|
func EditYAMLStrings(path string, keyPaths []string, fn func(string) string) error {
|
|
root, err := loadYAMLForEdit(path)
|
|
if err != nil || root == nil {
|
|
return err
|
|
}
|
|
changed := false
|
|
for _, kp := range keyPaths {
|
|
if n := lookupYAML(root, strings.Split(kp, ".")); n != nil {
|
|
changed = mapStrings(n, fn) || changed
|
|
}
|
|
}
|
|
if !changed {
|
|
return nil
|
|
}
|
|
return writeYAML(path, root)
|
|
}
|
|
|
|
// mapStrings puts fn's answer in place of every string under n, and says
|
|
// whether any differs.
|
|
func mapStrings(n *yaml.Node, fn func(string) string) bool {
|
|
changed := false
|
|
if n.Kind == yaml.ScalarNode && n.ShortTag() == "!!str" {
|
|
if v := fn(n.Value); v != n.Value {
|
|
n.Value, changed = v, true
|
|
}
|
|
}
|
|
for _, c := range n.Content {
|
|
changed = mapStrings(c, fn) || changed
|
|
}
|
|
return changed
|
|
}
|
|
|
|
// JSONToYAML writes a JSON or JSONC file out as block-style YAML, key
|
|
// order kept, for agents that moved from one to the other.
|
|
func JSONToYAML(src, dst string) error {
|
|
raw, err := Read(src)
|
|
if err != nil || raw == nil {
|
|
return err
|
|
}
|
|
var doc yaml.Node
|
|
if err := yaml.Unmarshal(jsonc.ToJSON(raw), &doc); err != nil {
|
|
return fmt.Errorf("%s: %w", src, err)
|
|
}
|
|
if len(doc.Content) == 0 {
|
|
return nil
|
|
}
|
|
root := doc.Content[0]
|
|
if root.Kind != yaml.MappingNode {
|
|
return fmt.Errorf("%s: top level is not an object", src)
|
|
}
|
|
blockStyle(root)
|
|
return writeYAML(dst, root)
|
|
}
|
|
|
|
// loadYAML reads the first document's mapping without decoding its values.
|
|
// Unrelated duplicate keys, complex keys or invalid tags must not hide a
|
|
// readable model setting. Writers use loadYAMLForEdit to validate the whole file.
|
|
func loadYAML(path string) (*yaml.Node, error) {
|
|
raw, err := Read(path)
|
|
if err != nil || len(bytes.TrimSpace(raw)) == 0 {
|
|
return nil, err
|
|
}
|
|
var doc yaml.Node
|
|
if err := yaml.Unmarshal(raw, &doc); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
if len(doc.Content) == 0 {
|
|
return nil, nil
|
|
}
|
|
root := doc.Content[0]
|
|
if root.Kind != yaml.MappingNode {
|
|
return nil, fmt.Errorf("%s: top level is not a YAML mapping", path)
|
|
}
|
|
if doc.HeadComment != "" && root.HeadComment == "" {
|
|
root.HeadComment = doc.HeadComment
|
|
}
|
|
return root, nil
|
|
}
|
|
|
|
// loadYAMLForEdit validates input even when the requested key is unchanged
|
|
// or missing.
|
|
func loadYAMLForEdit(path string) (*yaml.Node, error) {
|
|
raw, err := Read(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
root, _, err := parseYAMLTop(raw)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
return root, nil
|
|
}
|
|
|
|
// parseYAMLDocument validates the first document and any empty/null trailing ones.
|
|
func parseYAMLDocument(raw []byte) (*yaml.Node, error) {
|
|
root, _, err := parseYAMLDocuments(raw)
|
|
return root, err
|
|
}
|
|
|
|
// Decode as well as parse: yaml.Node alone accepts duplicate mapping keys.
|
|
// Only empty or null trailing documents can be discarded safely. nextLine
|
|
// bounds the first document for line edits; it is zero when no other follows.
|
|
func parseYAMLDocuments(raw []byte) (root *yaml.Node, nextLine int, err error) {
|
|
dec := yaml.NewDecoder(bytes.NewReader(raw))
|
|
var doc yaml.Node
|
|
if err := dec.Decode(&doc); err == io.EOF {
|
|
return nil, 0, nil
|
|
} else if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
for {
|
|
var extra yaml.Node
|
|
if err := dec.Decode(&extra); err == io.EOF {
|
|
break
|
|
} else if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if nextLine == 0 {
|
|
nextLine = extra.Line
|
|
}
|
|
if len(extra.Content) == 0 {
|
|
continue
|
|
}
|
|
n := extra.Content[0]
|
|
if n.Kind != yaml.ScalarNode || n.ShortTag() != "!!null" {
|
|
return nil, 0, fmt.Errorf("expected a single YAML document")
|
|
}
|
|
var null any
|
|
if err := n.Decode(&null); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
}
|
|
if len(doc.Content) == 0 {
|
|
return nil, nextLine, nil
|
|
}
|
|
root = doc.Content[0]
|
|
var decoded any
|
|
if err := root.Decode(&decoded); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
// comments above the first key belong to the document
|
|
if doc.HeadComment != "" && root.HeadComment == "" {
|
|
root.HeadComment = doc.HeadComment
|
|
}
|
|
return root, nextLine, nil
|
|
}
|
|
|
|
func writeYAML(path string, root *yaml.Node) error {
|
|
var buf bytes.Buffer
|
|
enc := yaml.NewEncoder(&buf)
|
|
enc.SetIndent(2)
|
|
if err := enc.Encode(root); err != nil {
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
if err := enc.Close(); err != nil {
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
// Reparse the rendered text: replacing or removing a node can leave a
|
|
// dangling alias even though the original node tree was valid.
|
|
if _, err := parseYAMLDocument(buf.Bytes()); err != nil {
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
orig, _ := Read(path)
|
|
return WriteAtomic(path, keepCRLF(buf.Bytes(), orig))
|
|
}
|
|
|
|
func lookupYAML(n *yaml.Node, parts []string) *yaml.Node {
|
|
for _, p := range parts {
|
|
if n.Kind != yaml.MappingNode {
|
|
return nil
|
|
}
|
|
var next *yaml.Node
|
|
for i := 0; i+1 < len(n.Content); i += 2 {
|
|
if n.Content[i].Value == p {
|
|
next = n.Content[i+1]
|
|
break
|
|
}
|
|
}
|
|
if next == nil {
|
|
return nil
|
|
}
|
|
n = next
|
|
}
|
|
return n
|
|
}
|
|
|
|
// setYAML puts v at parts under the mapping m. An existing value is
|
|
// replaced in place, keeping the comments beside it; a scalar in the way
|
|
// of a deeper key becomes a mapping.
|
|
func setYAML(m *yaml.Node, parts []string, v *yaml.Node) {
|
|
for i, p := range parts {
|
|
last := i == len(parts)-1
|
|
var cur *yaml.Node
|
|
for j := 0; j+1 < len(m.Content); j += 2 {
|
|
if m.Content[j].Value == p {
|
|
cur = m.Content[j+1]
|
|
break
|
|
}
|
|
}
|
|
if cur == nil {
|
|
// the {} a key taken out left (Hermes's providers: {} after
|
|
// a disconnect) is filled in block style, not on one line
|
|
if len(m.Content) == 0 {
|
|
m.Style &^= yaml.FlowStyle
|
|
}
|
|
cur = &yaml.Node{Kind: yaml.MappingNode}
|
|
if last {
|
|
cur = v
|
|
}
|
|
m.Content = append(m.Content, &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: p}, cur)
|
|
m = cur
|
|
continue
|
|
}
|
|
if last {
|
|
v.HeadComment, v.LineComment, v.FootComment = cur.HeadComment, cur.LineComment, cur.FootComment
|
|
*cur = *v
|
|
return
|
|
}
|
|
if cur.Kind != yaml.MappingNode {
|
|
*cur = yaml.Node{Kind: yaml.MappingNode, HeadComment: cur.HeadComment, LineComment: cur.LineComment}
|
|
}
|
|
m = cur
|
|
}
|
|
}
|
|
|
|
func blockStyle(n *yaml.Node) {
|
|
if n.Kind == yaml.MappingNode || n.Kind == yaml.SequenceNode {
|
|
n.Style = 0
|
|
} else if n.Kind == yaml.ScalarNode {
|
|
n.Style &^= yaml.DoubleQuotedStyle | yaml.SingleQuotedStyle
|
|
}
|
|
for _, c := range n.Content {
|
|
blockStyle(c)
|
|
}
|
|
}
|
|
|
|
// BlockList is raw, a file whose one document is a top-level sequence
|
|
// written in flow style ([ {id: a}, {id: b} ], what dsh's own writers keep
|
|
// a file that starts as []), with that sequence written as a block list
|
|
// instead: one "- " item after another, each entry's mappings and lists in
|
|
// block style too, its scalars quoted as they were. The lines before the
|
|
// sequence and the comment lines after it stay as written. ok is false, and
|
|
// raw is left to the caller, for anything else: a block list, a mapping,
|
|
// several documents, or text that isn't YAML.
|
|
func BlockList(raw string) (string, bool) {
|
|
raw = strings.ReplaceAll(raw, "\r\n", "\n")
|
|
dec := yaml.NewDecoder(strings.NewReader(raw))
|
|
var doc, more yaml.Node
|
|
if dec.Decode(&doc) != nil || dec.Decode(&more) == nil {
|
|
return "", false
|
|
}
|
|
if len(doc.Content) != 1 {
|
|
return "", false
|
|
}
|
|
seq := doc.Content[0]
|
|
if seq.Kind != yaml.SequenceNode || seq.Style&yaml.FlowStyle == 0 {
|
|
return "", false
|
|
}
|
|
lines := strings.Split(strings.TrimRight(raw, "\n"), "\n")
|
|
start := min(max(seq.Line-1, 0), len(lines))
|
|
end := len(lines)
|
|
for end > start {
|
|
if t := strings.TrimSpace(lines[end-1]); t != "" && !strings.HasPrefix(t, "#") {
|
|
break
|
|
}
|
|
end--
|
|
}
|
|
out := append([]string{}, lines[:start]...)
|
|
if len(seq.Content) == 0 {
|
|
out = append(out, "[]")
|
|
} else {
|
|
seq.HeadComment, seq.LineComment, seq.FootComment = "", "", ""
|
|
unflow(seq)
|
|
var buf bytes.Buffer
|
|
enc := yaml.NewEncoder(&buf)
|
|
enc.SetIndent(2)
|
|
if enc.Encode(seq) != nil || enc.Close() != nil {
|
|
return "", false
|
|
}
|
|
out = append(out, strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")...)
|
|
}
|
|
out = append(out, lines[end:]...)
|
|
return strings.Join(out, "\n") + "\n", true
|
|
}
|
|
|
|
// unflow writes n's mappings and sequences in block style.
|
|
func unflow(n *yaml.Node) {
|
|
n.Style &^= yaml.FlowStyle
|
|
for _, c := range n.Content {
|
|
unflow(c)
|
|
}
|
|
}
|