While a WSL distro runs, the Subagents page lists its agents that take subagents here (Claude Code, Codex and the rest of SubagentAgents) with a greyed chip that says magpie gives subagents only to agents on this computer, not yet to it in WSL. With the distro stopped, they were gone from the page: subagentsView built its agents from the targets only, and an agent in a stopped distro is no target (#3896). As the commit before did for the Hooks page: subagentsView now also lists each stopped agent of those kinds that the MCP and Skills pages list from its shape (asleepView), with the reason it has while the distro runs (wslNoSubagents, which subagentSpecOf now shares), so its chip, a subagent's problem for it and the "magpie can't give subagents to ... yet" line are the same, stopped or running. Nothing of the distro is opened. One magpie never saw (unseen) isn't listed. No WSL agent can be given a subagent yet, so there is nothing to keep for Woken. No change to the page; nothing new to translate. The line in library.md that named the pages leaving a stopped distro's agents out (#4182, #4237, #4238) is gone with this. What was run (tree e03c52f0, on origin/mainec5571d6, go1.26.8, linux/amd64, Linux 5.15, umask 077): - TestWSLStoppedSubagents (new): the Subagents page's WSL agents read while a FakeWSL distro runs (Claude Code and Codex, the kinds it lists, each with the reason), then with it stopped and its folder moved away: the same two, field for field. A subagent given the stopped Claude Code has the same problem for it. Nothing was written into the distro and FakeWSLRuns didn't move. - Broken by hand, each alone: subagentsView skipping the stopped agents ("... while it is stopped: map[], while it ran: map[claude@wsl:...]", and the subagent with no problem for Claude Code); the stopped agents listed with no reason ("No:" empty against the running ones', the same problem missing). - library-wsl-asleep, its new Subagents case beside the Hooks one, Chromium and WebKit, en, zh, zh-TW, ja and de at 1100 and 390px: a subagent's chips are Claude Code and the stopped Codex, Codex's blocked, asleep, aria-disabled, with the WSL reason as its tooltip, the line names it, a click on it sends nothing. As for Hooks, the page isn't changed, so only TestWSLStoppedSubagents catches the gap. - GUI files, each alone, Chromium and WebKit, Node 22.22.1, Playwright 1.62.1, DISPLAY unset: library-wsl-asleep 44/44 each, i18n-keys 4/4, library-subagents 6/6, library-hooks 6/6, library-gone-agents 11/11: 142 of 142. - go test -tags nogui -count=1 ./internal/library ./internal/agent ./internal/cli: library and cli ok; agent red in TestInstalledVersion, TestCursorFolderFromTheEditorIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI and TestOmpDetectedWithPiDir (#3141). go test -race -run TestWSL ./internal/library: ok. - gofmt -l clean. go vet -tags nogui for linux, windows and darwin, go vet for windows, go build -tags nogui for linux and darwin and go build for windows: exit 0 each. go vet and go build without the tag on linux: exit 1 (no GTK headers here). - go test -tags nogui -count=1 -timeout 45m ./... (Bun 1.4.2; 9m49s, load 20 at its start): 78 packages ok, and 8 tests red in 3 packages, each filed: agent TestInstalledVersion, TestCursorFolderFromTheEditorIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI, TestOmpDetectedWithPiDir and TestZedAppPath (#3141); gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie (#3649); plugin TestGatewayHookMixedPiPackage ("starting plugins: context deadline exceeded", #2865, under load). The same suite on an origin/mainec5571d6worktree right after (10m01s): the same agent and gui reds, test for test, and plugin ok there. Not tried: a real distro, and the real app; FakeWSL counts the wsl.exe -d commands run in the stopped distro (none). Reviewed and run again by the maintainer at the stack's tree 45994757c (this commit and the six beside it, on origin/main90518de26), go1.26.8: - mote (macOS, arm64): gofmt -l clean; go vet and go build for darwin, GOOS=linux -tags nogui and GOOS=windows (library, plugin, gui): exit 0. go test -tags nogui -count=1 -timeout 45m ./...: every package ok but internal/gui TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and internal/proc TestNoCommandBypassesProc (#4491), both red on main fromb09349d17andc59c547c5, in files this stack doesn't touch. go test -race -run 'TestWSL|TestTagged|TestPinnedPiCache' for library and plugin: ok. - magpie-desktop (Linux, wsl-run; WebKit hangs on mote), Chromium and WebKit: every library-*, sync-*, s3-sync, github-sync and i18n-keys file, 4 at a time: 1003 of 1020. The 17 reds are in 6 files no commit here touches: i18n-keys ("Unmarked", #4524), library-chip-fan, -items, -mcp-health, -mcp-hub (#4483) and -mcp-tools. Those 6 alone, one at a time, at load ~60: 74 of 92 here, 77 of 92 at90518de26itself. Red in both: i18n-keys, -chip-fan and -items in WebKit, -mcp-tools in Chromium ja 420; -mcp-health green in both; -mcp-hub's WebKit case red here only, the case #4483 has red on main. -chip-fan and -items are #3072's and #4501's kind, WebKit under load. - Broken by hand on mote: subagentsView skipping the stopped distro's agents turns TestWSLStoppedSubagents red.
513 lines
16 KiB
Go
513 lines
16 KiB
Go
package library
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/edit"
|
|
)
|
|
|
|
// Subagents (#2283) are agents of their own the main agent hands a task
|
|
// to: a name, what it is for, its prompt, and the skills and servers it
|
|
// works with. One is kept once and written into each agent ticked for it,
|
|
// as a file of that agent's own, in its own folder and shape (each read
|
|
// from the agent's source or binary):
|
|
//
|
|
// Claude Code <claude dir>/agents/<name>.md name, description, skills, mcpServers; the prompt below
|
|
// Codex <codex home>/agents/<name>.toml name, description, developer_instructions
|
|
// OpenCode <config>/agents/<name>.md description, mode: subagent; named by its file
|
|
// Gemini CLI ~/.gemini/agents/<name>.md name, description
|
|
// Qwen Code <qwen home>/agents/<name>.md name, description
|
|
// Factory Droid ~/.factory/droids/<name>.md name, description, mcpServers
|
|
// Grok Build <grok home>/agents/<name>.md name, description
|
|
// Cursor ~/.cursor/agents/<name>.md name, description
|
|
// Copilot CLI <copilot home>/agents/<name>.agent.md name, description
|
|
//
|
|
// Only Claude Code takes a subagent's skills, and only Claude Code and
|
|
// Droid its servers by name; every other agent's subagents use whatever
|
|
// the agent has (the page says so). What magpie wrote is recorded file
|
|
// by file with its hash (Applied.Subagents): a file still as magpie wrote
|
|
// it is magpie's to rewrite or remove, one the user changed is theirs from
|
|
// then on, and a file by that name magpie didn't write is never touched.
|
|
|
|
// Subagent is one the agents ticked for it are given.
|
|
type Subagent struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Prompt string `json:"prompt"`
|
|
Skills []string `json:"skills,omitempty"`
|
|
MCP []string `json:"mcp,omitempty"`
|
|
Agents []string `json:"agents"`
|
|
}
|
|
|
|
// SubagentFile is a file magpie wrote for a subagent, and the hash of what
|
|
// it wrote there.
|
|
type SubagentFile struct {
|
|
Name string `json:"name"`
|
|
Path string `json:"path"`
|
|
Hash string `json:"hash"`
|
|
}
|
|
|
|
// subagentNameRe is a name every agent takes: Cursor's and CodeBuddy's
|
|
// are lowercase letters, digits and -, Qwen Code's 2 to 50 characters
|
|
// that neither start nor end with - (validateName in its subagents).
|
|
var subagentNameRe = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,48}[a-z0-9]$`)
|
|
|
|
// reservedSubagents are names Qwen Code keeps for itself.
|
|
var reservedSubagents = []string{"self", "system", "user", "model", "tool", "config", "default", "main"}
|
|
|
|
const (
|
|
maxSubagentDescription = 1000
|
|
maxSubagentPrompt = 100_000
|
|
)
|
|
|
|
// SubagentAgents are the agents a subagent can be given, in the order the
|
|
// page lists them.
|
|
var SubagentAgents = []string{"claude", "codex", "opencode", "gemini", "qwen", "droid", "grok", "cursor", "copilot"}
|
|
|
|
// subagentSpec is where an agent keeps its subagents and how it reads one.
|
|
type subagentSpec struct {
|
|
dir string
|
|
ext string
|
|
// skills and mcp: the agent's file names the subagent's own; one
|
|
// without uses all the agent has
|
|
skills, mcp bool
|
|
render func(s *Subagent, skills, mcp []string) []byte
|
|
}
|
|
|
|
func (sp *subagentSpec) path(name string) string { return filepath.Join(sp.dir, name+sp.ext) }
|
|
|
|
// wslNoSubagents is why an agent in WSL is given no subagents, its distro
|
|
// running or not.
|
|
const wslNoSubagents = "magpie gives no subagents to an agent in WSL yet"
|
|
|
|
func subagentSpecOf(t *Target) (*subagentSpec, string) {
|
|
a := t.Agent
|
|
if a.WSL != "" {
|
|
return nil, wslNoSubagents
|
|
}
|
|
md := func(dir string, keys func(s *Subagent, skills, mcp []string) [][2]string) *subagentSpec {
|
|
return &subagentSpec{dir: dir, ext: ".md", render: func(s *Subagent, skills, mcp []string) []byte {
|
|
return frontmatter(keys(s, skills, mcp), s.Prompt)
|
|
}}
|
|
}
|
|
plain := func(s *Subagent, _, _ []string) [][2]string {
|
|
return [][2]string{{"name", yamlStr(s.Name)}, {"description", yamlStr(s.Description)}}
|
|
}
|
|
h := home()
|
|
switch a.ID {
|
|
case "claude":
|
|
sp := md(filepath.Join(claudeDir(), "agents"), func(s *Subagent, skills, mcp []string) [][2]string {
|
|
kv := plain(s, nil, nil)
|
|
if len(skills) > 0 {
|
|
kv = append(kv, [2]string{"skills", yamlList(skills)})
|
|
}
|
|
if len(mcp) > 0 {
|
|
kv = append(kv, [2]string{"mcpServers", yamlList(mcp)})
|
|
}
|
|
return kv
|
|
})
|
|
sp.skills, sp.mcp = true, true
|
|
return sp, ""
|
|
case "codex":
|
|
return &subagentSpec{dir: filepath.Join(codexDir(), "agents"), ext: ".toml", render: codexRole}, ""
|
|
case "opencode":
|
|
// named by its file; mode subagent is for the main agent to call
|
|
return md(filepath.Join(filepath.Dir(a.Path), "agents"), func(s *Subagent, _, _ []string) [][2]string {
|
|
return [][2]string{{"description", yamlStr(s.Description)}, {"mode", "subagent"}}
|
|
}), ""
|
|
case "gemini":
|
|
return md(filepath.Join(h, ".gemini", "agents"), plain), ""
|
|
case "qwen":
|
|
return md(filepath.Join(a.Dir, "agents"), plain), ""
|
|
case "droid":
|
|
sp := md(filepath.Join(a.Dir, "droids"), func(s *Subagent, _, mcp []string) [][2]string {
|
|
kv := plain(s, nil, nil)
|
|
if len(mcp) > 0 {
|
|
kv = append(kv, [2]string{"mcpServers", yamlList(mcp)})
|
|
}
|
|
return kv
|
|
})
|
|
sp.mcp = true
|
|
return sp, ""
|
|
case "grok":
|
|
return md(filepath.Join(a.Dir, "agents"), plain), ""
|
|
case "cursor":
|
|
return md(filepath.Join(h, ".cursor", "agents"), plain), ""
|
|
case "copilot":
|
|
d := appdir.Getenv("COPILOT_HOME")
|
|
if d == "" {
|
|
d = filepath.Join(h, ".copilot")
|
|
}
|
|
sp := md(filepath.Join(d, "agents"), plain)
|
|
sp.ext = ".agent.md"
|
|
return sp, ""
|
|
}
|
|
return nil, "magpie can't give " + a.Name + " subagents: it reads none from a folder of its own"
|
|
}
|
|
|
|
// yamlStr is s as a YAML scalar: JSON's quoted string is one.
|
|
func yamlStr(s string) string {
|
|
var b bytes.Buffer
|
|
enc := json.NewEncoder(&b)
|
|
enc.SetEscapeHTML(false)
|
|
_ = enc.Encode(s)
|
|
return strings.TrimSuffix(b.String(), "\n")
|
|
}
|
|
|
|
func yamlList(xs []string) string {
|
|
q := make([]string, len(xs))
|
|
for i, x := range xs {
|
|
q[i] = yamlStr(x)
|
|
}
|
|
return "[" + strings.Join(q, ", ") + "]"
|
|
}
|
|
|
|
func frontmatter(kv [][2]string, body string) []byte {
|
|
var b strings.Builder
|
|
b.WriteString("---\n")
|
|
for _, p := range kv {
|
|
b.WriteString(p[0] + ": " + p[1] + "\n")
|
|
}
|
|
b.WriteString("---\n\n")
|
|
b.WriteString(strings.TrimRight(body, "\n") + "\n")
|
|
return []byte(b.String())
|
|
}
|
|
|
|
// codexRole is an agent role file as Codex reads one from its agents
|
|
// folder (codex-rs/agent-roles): name, description and its prompt as
|
|
// developer_instructions, each required.
|
|
func codexRole(s *Subagent, _, _ []string) []byte {
|
|
b, _ := toml.Marshal(struct {
|
|
Name string `toml:"name"`
|
|
Description string `toml:"description"`
|
|
DeveloperInstructions string `toml:"developer_instructions,multiline"`
|
|
}{s.Name, s.Description, strings.TrimRight(s.Prompt, "\n") + "\n"})
|
|
return b
|
|
}
|
|
|
|
func subagentHash(b []byte) string {
|
|
h := sha256.Sum256(b)
|
|
return hex.EncodeToString(h[:])
|
|
}
|
|
|
|
func (s *Subagent) check() error {
|
|
if !subagentNameRe.MatchString(s.Name) {
|
|
return fmt.Errorf("subagent name %q: use 2 to 50 lowercase letters, digits and -, not starting or ending with -", s.Name)
|
|
}
|
|
if slices.Contains(reservedSubagents, s.Name) {
|
|
return fmt.Errorf("%s is a name agents keep for themselves", s.Name)
|
|
}
|
|
if s.Description == "" {
|
|
return errors.New("a subagent needs a description: it is how the main agent knows when to use it")
|
|
}
|
|
if utf8.RuneCountInString(s.Description) > maxSubagentDescription {
|
|
return fmt.Errorf("a subagent's description is at most %d characters", maxSubagentDescription)
|
|
}
|
|
if strings.TrimSpace(s.Prompt) == "" {
|
|
return errors.New("a subagent needs a prompt")
|
|
}
|
|
if len(s.Prompt) > maxSubagentPrompt {
|
|
return fmt.Errorf("a subagent's prompt is at most %d bytes", maxSubagentPrompt)
|
|
}
|
|
for _, a := range s.Agents {
|
|
if base, _, _ := strings.Cut(a, "@wsl:"); !slices.Contains(SubagentAgents, base) {
|
|
return fmt.Errorf("%s can't be given subagents", a)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (l *Library) subagent(name string) *Subagent {
|
|
for _, s := range l.Subagents {
|
|
if s.Name == name {
|
|
return s
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// renderSubagent is s as an agent's file: of its skills and servers,
|
|
// those the library still has.
|
|
func (l *Library) renderSubagent(sp *subagentSpec, s *Subagent) []byte {
|
|
var skills, mcp []string
|
|
if sp.skills {
|
|
for _, n := range s.Skills {
|
|
if l.skill(n) != nil {
|
|
skills = append(skills, n)
|
|
}
|
|
}
|
|
}
|
|
if sp.mcp {
|
|
for _, n := range s.MCP {
|
|
if l.server(n) != nil {
|
|
mcp = append(mcp, n)
|
|
}
|
|
}
|
|
}
|
|
return sp.render(s, skills, mcp)
|
|
}
|
|
|
|
func (l *Library) syncSubagents(t *Target, b *backups, res *Result) {
|
|
id := t.Agent.ID
|
|
a := l.applied(id)
|
|
var want []*Subagent
|
|
for _, s := range l.Subagents {
|
|
if slices.Contains(s.Agents, id) {
|
|
want = append(want, s)
|
|
}
|
|
}
|
|
if len(a.Subagents) == 0 && len(want) == 0 {
|
|
return
|
|
}
|
|
sp, why := subagentSpecOf(t)
|
|
if sp == nil {
|
|
for _, s := range want {
|
|
res.fail(id, "subagent:"+s.Name, errors.New(why))
|
|
}
|
|
return
|
|
}
|
|
wanted := map[string]*Subagent{} // path → the subagent written there
|
|
for _, s := range want {
|
|
wanted[sp.path(s.Name)] = s
|
|
}
|
|
done := map[string]bool{}
|
|
var kept []SubagentFile
|
|
write := func(s *Subagent, path string, body []byte) bool {
|
|
if err := b.keep(id, path); err != nil {
|
|
res.fail(id, "subagent:"+s.Name, err)
|
|
return false
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
res.fail(id, "subagent:"+s.Name, err)
|
|
return false
|
|
}
|
|
if err := edit.WriteAtomic(path, body); err != nil {
|
|
res.fail(id, "subagent:"+s.Name, err)
|
|
return false
|
|
}
|
|
res.changed(id)
|
|
return true
|
|
}
|
|
for _, f := range a.Subagents {
|
|
cur, err := os.ReadFile(f.Path)
|
|
switch {
|
|
case os.IsNotExist(err):
|
|
// gone: written again below if it is still wanted
|
|
continue
|
|
case err != nil:
|
|
// can't tell whether it is still magpie's: left, and kept on
|
|
// record, untouched
|
|
res.fail(id, "subagent:"+f.Name, err)
|
|
kept = append(kept, f)
|
|
done[f.Path] = true
|
|
continue
|
|
case subagentHash(cur) != f.Hash:
|
|
// changed since magpie wrote it: the user's from now on, which
|
|
// the loop below says if the library still wants that name
|
|
continue
|
|
}
|
|
s := wanted[f.Path]
|
|
if s == nil {
|
|
if err := b.keep(id, f.Path); err != nil {
|
|
res.fail(id, "subagent:"+f.Name, err)
|
|
kept = append(kept, f)
|
|
continue
|
|
}
|
|
if err := os.Remove(f.Path); err != nil && !os.IsNotExist(err) {
|
|
res.fail(id, "subagent:"+f.Name, err)
|
|
kept = append(kept, f)
|
|
continue
|
|
}
|
|
res.changed(id)
|
|
continue
|
|
}
|
|
done[f.Path] = true
|
|
body := l.renderSubagent(sp, s)
|
|
if !bytes.Equal(cur, body) && !write(s, f.Path, body) {
|
|
kept = append(kept, f)
|
|
continue
|
|
}
|
|
kept = append(kept, SubagentFile{Name: s.Name, Path: f.Path, Hash: subagentHash(body)})
|
|
}
|
|
for _, s := range want {
|
|
path := sp.path(s.Name)
|
|
if done[path] {
|
|
continue
|
|
}
|
|
body := l.renderSubagent(sp, s)
|
|
cur, err := os.ReadFile(path)
|
|
switch {
|
|
case err == nil && bytes.Equal(cur, body):
|
|
// the same as magpie would write: taken as magpie's
|
|
case err == nil:
|
|
res.fail(id, "subagent:"+s.Name, fmt.Errorf("%s has a subagent of its own at %s, which magpie leaves as it is", t.Agent.Name, path))
|
|
continue
|
|
case !os.IsNotExist(err):
|
|
res.fail(id, "subagent:"+s.Name, err)
|
|
continue
|
|
default:
|
|
if !write(s, path, body) {
|
|
continue
|
|
}
|
|
}
|
|
kept = append(kept, SubagentFile{Name: s.Name, Path: path, Hash: subagentHash(body)})
|
|
}
|
|
slices.SortFunc(kept, func(x, y SubagentFile) int { return strings.Compare(x.Path, y.Path) })
|
|
a.Subagents = kept
|
|
}
|
|
|
|
// ---- changes ---------------------------------------------------------------
|
|
|
|
// SaveSubagent adds a subagent, or replaces the one called old (renaming
|
|
// it: the files magpie wrote by the old name go).
|
|
func SaveSubagent(old string, s Subagent) (*Result, error) {
|
|
s.Name = strings.TrimSpace(s.Name)
|
|
s.Description = strings.Join(strings.Fields(s.Description), " ")
|
|
s.Prompt = strings.TrimRight(strings.ReplaceAll(s.Prompt, "\r\n", "\n"), " \n\t")
|
|
if err := s.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
return change(func(l *Library) error {
|
|
if s.Name != old && l.subagent(s.Name) != nil {
|
|
return fmt.Errorf("the library already has a subagent called %s", s.Name)
|
|
}
|
|
if old != "" {
|
|
i := slices.IndexFunc(l.Subagents, func(x *Subagent) bool { return x.Name == old })
|
|
if i < 0 {
|
|
return fmt.Errorf("no subagent called %s", old)
|
|
}
|
|
l.Subagents = slices.Delete(l.Subagents, i, i+1)
|
|
}
|
|
s.Skills = slices.Compact(slices.Sorted(slices.Values(s.Skills)))
|
|
s.MCP = slices.Compact(slices.Sorted(slices.Values(s.MCP)))
|
|
s.Agents = orNone(slices.Compact(slices.Sorted(slices.Values(s.Agents))))
|
|
l.Subagents = append(l.Subagents, &s)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// SubagentAgentsSet sets which agents get a subagent.
|
|
func SubagentAgentsSet(name string, agents []string) (*Result, error) {
|
|
return change(func(l *Library) error {
|
|
s := l.subagent(name)
|
|
if s == nil {
|
|
return fmt.Errorf("no subagent called %s", name)
|
|
}
|
|
c := *s
|
|
c.Agents = agents
|
|
if err := c.check(); err != nil {
|
|
return err
|
|
}
|
|
s.Agents = orNone(slices.Compact(slices.Sorted(slices.Values(agents))))
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// RemoveSubagents takes the subagents named out of the library, and the
|
|
// files magpie wrote of them, still as it wrote them, out of every agent.
|
|
func RemoveSubagents(names []string) (*Result, error) {
|
|
if len(names) == 0 {
|
|
return nil, errors.New("no subagents to remove")
|
|
}
|
|
return change(func(l *Library) error {
|
|
for _, n := range names {
|
|
if l.subagent(n) == nil {
|
|
return fmt.Errorf("no subagent called %s", n)
|
|
}
|
|
}
|
|
l.Subagents = slices.DeleteFunc(l.Subagents, func(s *Subagent) bool { return slices.Contains(names, s.Name) })
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// ---- the page --------------------------------------------------------------
|
|
|
|
// SubagentView is a library subagent, and where each agent has it.
|
|
type SubagentView struct {
|
|
*Subagent
|
|
// Files are, for each agent it was written to, the file
|
|
Files map[string]string `json:"files,omitempty"`
|
|
// Problems are the agents it couldn't be given to, and why
|
|
Problems map[string]string `json:"problems,omitempty"`
|
|
}
|
|
|
|
// SubagentAgentView is an agent the Subagents tab lists.
|
|
type SubagentAgentView struct {
|
|
ID string `json:"id"`
|
|
// Dir is where its subagents are; No is why magpie can't give it any
|
|
Dir string `json:"dir,omitempty"`
|
|
No string `json:"no,omitempty"`
|
|
WSL bool `json:"wsl,omitempty"`
|
|
// Skills and MCP: its subagents take skills, servers of their own;
|
|
// one that doesn't uses all the agent has
|
|
Skills bool `json:"skills,omitempty"`
|
|
MCP bool `json:"mcp,omitempty"`
|
|
}
|
|
|
|
// subagentsView is the Subagents page for these agents, and those in
|
|
// stopped WSL distros (asleep) that the page lists by what magpie last
|
|
// knew of them (asleepView): greyed as while their distro runs, since
|
|
// magpie gives no agent in WSL subagents yet, and nothing of theirs opened
|
|
// (#3896, #4238).
|
|
func (l *Library) subagentsView(targets []*Target, asleep []*agent.Agent, of func(string) map[string]string) ([]SubagentView, []SubagentAgentView) {
|
|
agents := []SubagentAgentView{}
|
|
for _, t := range targets {
|
|
if base, _, _ := strings.Cut(t.Agent.ID, "@wsl:"); !slices.Contains(SubagentAgents, base) {
|
|
continue
|
|
}
|
|
sp, why := subagentSpecOf(t)
|
|
av := SubagentAgentView{ID: t.Agent.ID, No: why, WSL: t.Agent.WSL != ""}
|
|
if sp != nil {
|
|
av.Dir, av.Skills, av.MCP = sp.dir, sp.skills, sp.mcp
|
|
}
|
|
agents = append(agents, av)
|
|
}
|
|
for _, a := range asleep {
|
|
if base, _, _ := strings.Cut(a.ID, "@wsl:"); slices.Contains(SubagentAgents, base) && shapeOf(a) != nil {
|
|
agents = append(agents, SubagentAgentView{ID: a.ID, No: wslNoSubagents, WSL: true})
|
|
}
|
|
}
|
|
views := []SubagentView{}
|
|
for _, s := range l.Subagents {
|
|
c := *s
|
|
c.Agents = orNone(c.Agents)
|
|
sv := SubagentView{Subagent: &c, Problems: of("subagent:" + s.Name)}
|
|
for id, a := range l.Applied {
|
|
for _, f := range a.Subagents {
|
|
if f.Name == s.Name && slices.Contains(s.Agents, id) {
|
|
if sv.Files == nil {
|
|
sv.Files = map[string]string{}
|
|
}
|
|
sv.Files[id] = f.Path
|
|
}
|
|
}
|
|
}
|
|
for _, av := range agents {
|
|
if av.No != "" && slices.Contains(s.Agents, av.ID) {
|
|
if sv.Problems == nil {
|
|
sv.Problems = map[string]string{}
|
|
}
|
|
if sv.Problems[av.ID] == "" {
|
|
sv.Problems[av.ID] = av.No
|
|
}
|
|
}
|
|
}
|
|
views = append(views, sv)
|
|
}
|
|
return views, agents
|
|
}
|