mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
A target declared with `agent` and `config_dir` is another config directory of a built-in Agent, such as a second Claude account. Its skills and agents paths are derived from the Agent's defaults at load time, so the config only records the directory. The same target name works for MCP (written in the Agent's format into the account's own file), for plugins (the Agent CLI runs with CLAUDE_CONFIG_DIR, CODEX_HOME or PI_CODING_AGENT_DIR) and for `mcp import --from`. Removing such a target warns when MCP config still names it instead of failing later at sync. The dashboard gains an "another account" mode in the add-target dialog and lists accounts next to the Agents on the MCP and plugin pages. Refs #289
158 lines
3.9 KiB
Go
158 lines
3.9 KiB
Go
package plugin
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
type document struct {
|
|
raw []byte
|
|
node yaml.Node
|
|
packages map[string]Package
|
|
}
|
|
|
|
func (s *Service) load() (*document, error) {
|
|
raw, err := os.ReadFile(s.ConfigPath)
|
|
if err != nil && !os.IsNotExist(err) {
|
|
return nil, err
|
|
}
|
|
return decodeDocument(raw, s.accountAgents())
|
|
}
|
|
|
|
// Validate checks plugin declarations without reading or writing native state. accounts
|
|
// maps each target that is another config directory of an Agent to that Agent, so a
|
|
// binding is validated by the Agent that would install it.
|
|
func Validate(raw []byte, accounts map[string]string) error {
|
|
_, err := decodeDocument(raw, accounts)
|
|
return err
|
|
}
|
|
|
|
// decodeDocument reads the plugin section. accounts maps each account to its Agent, or to
|
|
// "" where only the names are known.
|
|
func decodeDocument(raw []byte, accounts map[string]string) (*document, error) {
|
|
d := &document{raw: raw, packages: map[string]Package{}}
|
|
content := raw
|
|
if len(content) == 0 {
|
|
content = []byte("{}\n")
|
|
}
|
|
decoder := yaml.NewDecoder(bytes.NewReader(content))
|
|
if err := decoder.Decode(&d.node); err != nil {
|
|
return nil, err
|
|
}
|
|
if decoder.Decode(new(yaml.Node)) != io.EOF {
|
|
return nil, fmt.Errorf("configuration must contain one YAML document")
|
|
}
|
|
var check map[string]any
|
|
if err := d.node.Decode(&check); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(d.node.Content) != 1 || d.node.Content[0].Kind != yaml.MappingNode {
|
|
return nil, fmt.Errorf("configuration must be a YAML mapping")
|
|
}
|
|
root := d.node.Content[0]
|
|
for i := 0; i < len(root.Content); i += 2 {
|
|
if root.Content[i].Value == "plugins" {
|
|
var body struct {
|
|
Packages map[string]Package `yaml:"packages"`
|
|
}
|
|
data, err := yaml.Marshal(root.Content[i+1])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
dec := yaml.NewDecoder(bytes.NewReader(data))
|
|
dec.KnownFields(true)
|
|
if err := dec.Decode(&body); err != nil {
|
|
return nil, fmt.Errorf("invalid plugins configuration: %w", err)
|
|
}
|
|
if body.Packages != nil {
|
|
d.packages = body.Packages
|
|
}
|
|
}
|
|
}
|
|
for name, p := range d.packages {
|
|
if !namePattern.MatchString(name) {
|
|
return nil, fmt.Errorf("invalid plugin package name %q", name)
|
|
}
|
|
for target, b := range p.Bindings {
|
|
agent, account := accounts[target]
|
|
if !account && !slices.Contains(Targets, target) {
|
|
return nil, fmt.Errorf("invalid plugin binding for %s", name)
|
|
}
|
|
if !account {
|
|
agent = target
|
|
}
|
|
if !validTargetID(agent, b.ID) {
|
|
return nil, fmt.Errorf("invalid plugin binding for %s", name)
|
|
}
|
|
}
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
func (s *Service) save(d *document) error {
|
|
current, err := os.ReadFile(s.ConfigPath)
|
|
if err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
if !bytes.Equal(current, d.raw) {
|
|
return fmt.Errorf("configuration changed; preview again")
|
|
}
|
|
var n yaml.Node
|
|
if err := n.Encode(struct {
|
|
Packages map[string]Package `yaml:"packages"`
|
|
}{d.packages}); err != nil {
|
|
return err
|
|
}
|
|
root := d.node.Content[0]
|
|
found := false
|
|
for i := 0; i < len(root.Content); i += 2 {
|
|
if root.Content[i].Value == "plugins" {
|
|
root.Content[i+1] = &n
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
root.Content = append(root.Content, &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: "plugins"}, &n)
|
|
}
|
|
data, err := utils.MarshalYAML(&d.node)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(s.ConfigPath), 0755); err != nil {
|
|
return err
|
|
}
|
|
f, err := os.CreateTemp(filepath.Dir(s.ConfigPath), ".plugins-*")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(f.Name())
|
|
mode := os.FileMode(0600)
|
|
if info, err := os.Stat(s.ConfigPath); err == nil {
|
|
mode = info.Mode().Perm()
|
|
}
|
|
if err = f.Chmod(mode); err == nil {
|
|
_, err = f.Write(data)
|
|
}
|
|
if err == nil {
|
|
err = f.Sync()
|
|
}
|
|
closeErr := f.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if closeErr != nil {
|
|
return closeErr
|
|
}
|
|
if err = os.Rename(f.Name(), s.ConfigPath); err == nil {
|
|
d.raw = data
|
|
}
|
|
return err
|
|
}
|