Files
magpie/internal/edit/toml.go
T
yetone dfc2d7fa93 library: MCP servers, skills and instructions are kept once in magpie and given to any agent from one place, with a market to find and add them (#29). Each agent reads MCP servers and skills from files of its own and in a format of its own, so one server was set up again in Claude Code, Codex, Gemini CLI, OpenCode, Pi, Goose, Cursor, Copilot CLI and Crush. The Library view keeps each once: a server is added (stdio, http or sse, its env and headers) and switched on per agent with its icon, and magpie writes it into each agent's config in that agent's format, leaving what the agent has of its own as it is; a server or skill an agent already has is shown, to bring into the library. A skill is a folder with a SKILL.md, kept in the library from GitHub (owner/repo, or a link to a folder in one; updated from it later) or linked from a folder on this machine, and linked into each agent's skills folder. The instructions every agent reads (CLAUDE.md, AGENTS.md, GEMINI.md…) can be kept as one too. The market offers checked MCP servers first, then the official MCP Registry's, filled in from what the registry says each needs (keys asked for in a form, never guessed), and skills.sh's most installed skills with what each says it is for; each shows its real icon (the vendor's own, else its GitHub owner's), fetched through magpie only for the icons the market or the library gave out, and an added server or skill keeps its icon in the library. What magpie changes in an agent's files is backed up first
2026-09-25 14:11:17 +08:00

240 lines
5.8 KiB
Go

package edit
import (
"regexp"
"strconv"
"strings"
)
// Whole TOML tables are treated as units: magpie owns the tables it writes
// (for example a model provider) and replaces them verbatim, while every
// other line of the file stays untouched.
var tomlHeader = regexp.MustCompile(`^\s*\[\s*([^\]]+?)\s*\]`)
// TOMLTables lists the table headers of a TOML file in order.
func TOMLTables(path string) []string {
raw, err := Read(path)
if err != nil || raw == nil {
return nil
}
var out []string
for _, line := range splitLines(string(raw)) {
if m := tomlHeader.FindStringSubmatch(line); m != nil && !strings.HasPrefix(m[1], "[") {
out = append(out, m[1])
}
}
return out
}
// GetTOMLTable returns the scalar keys of one table.
func GetTOMLTable(path, name string) map[string]string {
raw, err := Read(path)
if err != nil || raw == nil {
return nil
}
lines := splitLines(string(raw))
from, to := tableSpan(lines, name)
if from < 0 {
return nil
}
out := map[string]string{}
for _, line := range lines[from+1 : to] {
if m := tomlKV.FindStringSubmatch(line); m != nil {
out[strings.Trim(m[1], `"`)] = tomlValue(m[2])
}
}
return out
}
// SetTOMLTable replaces table `name` with the given keys, or appends it.
func SetTOMLTable(path, name string, kvs ...KV) error {
raw, err := Read(path)
if err != nil {
return err
}
block := []string{"[" + name + "]"}
for _, kv := range kvs {
block = append(block, kv.Path+" = "+tomlLiteral(kv.Value))
}
lines := splitLines(string(raw))
from, to := tableSpan(lines, name)
var out []string
if from < 0 {
out = trimBlank(lines)
if len(out) > 0 {
out = append(out, "")
}
out = append(out, block...)
out = append(out, "")
} else {
out = append(out, lines[:from]...)
out = append(out, block...)
if to < len(lines) && strings.TrimSpace(lines[to]) != "" {
out = append(out, "")
}
out = append(out, lines[to:]...)
}
return WriteAtomic(path, []byte(joinLines(out)))
}
// SetTOMLKey sets one key of table `name` and leaves the table's other
// lines as they are. The key goes after the table's last key, and the table
// is appended when there is none.
func SetTOMLKey(path, name, key string, value any) error {
raw, err := Read(path)
if err != nil {
return err
}
line := key + " = " + tomlLiteral(value)
lines := splitLines(string(raw))
from, to := tableSpan(lines, name)
if from < 0 {
out := trimBlank(lines)
if len(out) > 0 {
out = append(out, "")
}
out = append(out, "["+name+"]", line, "")
return WriteAtomic(path, []byte(joinLines(out)))
}
at := from + 1
for i := from + 1; i < to; i++ {
m := tomlKV.FindStringSubmatch(lines[i])
if m == nil {
continue
}
if strings.Trim(m[1], `"`) == key {
lines[i] = line
return WriteAtomic(path, []byte(joinLines(lines)))
}
at = i + 1
}
out := append(append(append([]string{}, lines[:at]...), line), lines[at:]...)
return WriteAtomic(path, []byte(joinLines(out)))
}
// DelTOMLKey removes one key of table `name`, and the table with it when
// nothing else is left in it.
func DelTOMLKey(path, name, key string) error {
raw, err := Read(path)
if err != nil || raw == nil {
return err
}
lines := splitLines(string(raw))
from, to := tableSpan(lines, name)
if from < 0 {
return nil
}
var out []string
rest := false
for i, line := range lines {
if i > from && i < to {
if m := tomlKV.FindStringSubmatch(line); m != nil && strings.Trim(m[1], `"`) == key {
continue
}
if strings.TrimSpace(line) != "" {
rest = true
}
}
out = append(out, line)
}
if err := WriteAtomic(path, []byte(joinLines(out))); err != nil || rest {
return err
}
return DelTOMLTable(path, name)
}
// DelTOMLTable removes table `name` (header and body) if present.
func DelTOMLTable(path, name string) error {
raw, err := Read(path)
if err != nil || raw == nil {
return err
}
lines := splitLines(string(raw))
from, to := tableSpan(lines, name)
if from < 0 {
return nil
}
// Take the blank lines before the header with it so no gap is left.
for from > 0 && strings.TrimSpace(lines[from-1]) == "" {
from--
}
out := append([]string{}, lines[:from]...)
if to < len(lines) && len(out) > 0 && strings.TrimSpace(lines[to]) != "" {
out = append(out, "")
}
out = append(out, lines[to:]...)
return WriteAtomic(path, []byte(joinLines(out)))
}
// DelTOMLTop removes top-level (pre-table) keys.
func DelTOMLTop(path string, keys ...string) error {
raw, err := Read(path)
if err != nil || raw == nil {
return err
}
drop := map[string]bool{}
for _, k := range keys {
drop[k] = true
}
var out []string
header := true
for _, line := range splitLines(string(raw)) {
if header && tomlTable.MatchString(line) {
header = false
}
if header {
if m := tomlKV.FindStringSubmatch(line); m != nil && drop[strings.Trim(m[1], `"`)] {
continue
}
}
out = append(out, line)
}
return WriteAtomic(path, []byte(joinLines(out)))
}
// tableSpan returns the line range [from, to) of a table: its header line
// through the line before the next header. from is -1 when absent.
func tableSpan(lines []string, name string) (int, int) {
from := -1
for i, line := range lines {
m := tomlHeader.FindStringSubmatch(line)
if m == nil || strings.HasPrefix(m[1], "[") {
continue
}
if from >= 0 {
return from, i
}
if m[1] == name {
from = i
}
}
if from < 0 {
return -1, -1
}
return from, len(lines)
}
func trimBlank(lines []string) []string {
for len(lines) > 0 && strings.TrimSpace(lines[len(lines)-1]) == "" {
lines = lines[:len(lines)-1]
}
return lines
}
// Raw is a TOML value written as it is: an array or an inline table the
// caller has already spelled out.
type Raw string
func tomlLiteral(v any) string {
switch x := v.(type) {
case Raw:
return string(x)
case bool:
return strconv.FormatBool(x)
case int:
return strconv.Itoa(x)
}
return strconv.Quote(toString(v))
}