スライム on Discord (#help, 10-22): taste-skills took some of its skills
out, Update all reported each of them as a failure, and the only way
out was to delete them one by one.
An update or a check that reads the source whole and finds the skill's
folder (or its SKILL.md) gone now calls it removed upstream, not a
failure. GitHub tarballs and other git repositories both work this way.
- UpdateSkill returns *GoneError.
- UpdateSkills and UpdateSomeSkills list it in Result.Gone, not
Unupdated.
- A check gives status "gone".
- Each marks Skill.Gone. The mark is cleared when a check or an update
finds the skill there again.
- A fetch that failed, a skill left out of a fetch as too big, and a
folder that can't be read stay unknown, never gone (goneFrom).
Nothing is deleted by itself. RemoveGoneSkills takes the named skills
out, or every marked one when none are named, through RemoveSkills, so
the folders go to the backups. A skill found in its source again is
kept and said so.
- GUI: a "Removed upstream" tag replaces Update available and the
update icon. Update all says gone skills apart from failures. "Clean
up N" sits in the group heading, or in the bar when there is one
group, and opens a sheet naming each skill. One changed here carries
"Changed here". Strings are in en, zh, zh-TW, ja and de.
- CLI: `magpie library skill rm --gone [--yes]` asks at a terminal and
refuses without --yes otherwise. It names the ones changed here.
update and status print the hint.
- TUI: the row says "removed from <repo> · d takes it out".
- API: POST /api/library/skills/remove-gone {names}.
- Docs: the Skill updates row and Verification in
docs/subsystems/library.md.
Record for this push: 13 commits on origin/main ca4a1cde0, head tree
539c84e92. Go 1.26.8, sandbox HOME, GOMODCACHE/GOCACHE pinned. The full Go
run and vet were at tree d6393dfc8, the same Go code as this head: only
app.js comments (cut under the core script budget) and one ja string
changed after it.
- gofmt -l: nothing. go build ./...; go vet ./... (darwin), GOOS=linux
go vet -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test -timeout 45m ./..., through tee, one suite on the machine:
68 packages ok. The one red was internal/gui's
TestCoreScriptBudget: app.js 1,335,314 > 1,335,000, since main grew
app.js under the stack. The autoscroll commit's own comments were cut
(to 1,334,629) rather than the budget raised, and go test -count=1
./internal/gui is ok at this head (113s). Packages the run took from
Go's test cache had the same inputs.
- GUI: the 18 files this stack touches or names, both engines,
202 of 204 pass (tree 068acf128, this push less the last commit). The
two reds: gui-ja, red on main since c2636b7b5 and fixed by this push's
last commit (gui-ja and i18n-keys 6 of 6 there), and usage-fold's
chromium "several accounts" (#2163, red on main the same way).
- skill-gone passes, chromium and webkit, en and zh, 860 and 400px.
- internal/library, internal/cli and internal/tui ok, the Gone tests and
TestLibrarySkillRm among them.
- The rebase onto main met c2636b7b5's Verification line in library.md;
both sides' tests are kept in it.
The whole GUI suite ran at 1c1fc7183 (this stack's first 12 commits on
0ddcf28a1): 11713 of 11769 pass. Each of the 21 red files was then run
alone at the stack and at origin/main 8278fd9b0. Eight are red the same
way on main: plugin-card-update (#3079), quota-count-wrap and
quota-name-translated (#2228), s3-sync (#2448), usage-agents-hide
(#2276), usage-checkin (#3091), usage-fold's chromium "several
accounts" (#2163), usage-ledger (#3104). The other 13 pass alone on both
and were red only under that run's load (50 to 335): currency #3101,
plugin-github #3218, provider-fetch-again #3292, rest-try-now #2564,
routing-auto-reset #3293, routing-keys-fold #1612, routing-steady #1804,
routing-wb-restricted #1905, sessions-folder-delete #2218,
sessions-manage #3294, switch-only #3295, text-size #1866, usage-pack
#3296.
Broken by hand, each red, then restored:
- prepare not returning GoneError
- apply not clearing Gone
- leftOutAt ignored
- every Stat error read as absent
- the git check not saying gone
- RemoveGoneSkills taking a skill that isn't marked
Not tried:
- the real taste-skills repo over the network
- the Windows box
- a skill CC Switch installed (Origin, adopt path), which still reports
as a failure
- a GitHub check with a known commit, which keeps an existing gone
mark but only marks gone after an update
634 lines
18 KiB
Go
634 lines
18 KiB
Go
package tui
|
|
|
|
// The library page: the instructions, MCP servers and skills magpie gives
|
|
// the agents, which agents get each, what the agents have that the library
|
|
// doesn't, and RTK's hook in each — what the app's Library view does, in
|
|
// the terminal.
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"runtime"
|
|
"slices"
|
|
"strings"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/library"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// libRow is one line of the page.
|
|
type libRow struct {
|
|
kind string // instructions, mcp, skill, found-mcp, found-skill, rtk
|
|
name string
|
|
about string // what it runs, or what it is
|
|
on []string // the agents that get it (or have it, when found)
|
|
own bool // a server the agent's app puts in itself: nothing to bring in
|
|
bad int // agents it couldn't be given to
|
|
}
|
|
|
|
// libKinds are what the library can give, by row kind.
|
|
var libKinds = map[string]string{"instructions": "instructions", "mcp": "mcp", "skill": "skills"}
|
|
|
|
func (m *model) reloadLibrary() {
|
|
m.lib, m.libAgents, m.libErr = nil, nil, ""
|
|
v, err := library.Read(nil)
|
|
if err != nil {
|
|
m.libErr = err.Error()
|
|
return
|
|
}
|
|
m.libView = v
|
|
m.libAgents = v.Agents
|
|
ins := libRow{kind: "instructions", name: "shared instructions"}
|
|
if lines := strings.TrimRight(v.Instructions.Shared, "\n"); strings.TrimSpace(lines) == "" {
|
|
ins.about = "none yet · e writes them"
|
|
} else {
|
|
ins.about = fmt.Sprintf("%d line%s", strings.Count(lines, "\n")+1, plural(strings.Count(lines, "\n")+1))
|
|
}
|
|
for _, a := range v.Instructions.Agents {
|
|
if a.On {
|
|
ins.on = append(ins.on, a.Agent)
|
|
}
|
|
if a.Edited {
|
|
ins.bad++
|
|
}
|
|
}
|
|
m.lib = append(m.lib, ins)
|
|
for _, s := range v.Servers {
|
|
what := s.URL
|
|
if s.Command != "" {
|
|
what = strings.TrimSpace(s.Command + " " + strings.Join(s.Args, " "))
|
|
}
|
|
m.lib = append(m.lib, libRow{kind: "mcp", name: s.Name, about: what, on: s.Agents, bad: len(s.Problems)})
|
|
}
|
|
for _, s := range v.Skills {
|
|
r := libRow{kind: "skill", name: s.Name, about: s.Description, on: s.Agents, bad: len(s.Problems)}
|
|
if s.Missing {
|
|
r.about, r.bad = "its folder is gone", r.bad+1
|
|
} else if s.Gone != "" {
|
|
r.about, r.bad = "removed from "+s.Source+" · d takes it out", r.bad+1
|
|
}
|
|
m.lib = append(m.lib, r)
|
|
}
|
|
for _, f := range v.FoundServers {
|
|
m.lib = append(m.lib, libRow{kind: "found-mcp", name: f.Server.Name, on: f.Server.Agents, own: f.Own})
|
|
}
|
|
for _, f := range v.FoundSkills {
|
|
m.lib = append(m.lib, libRow{kind: "found-skill", name: f.Name, about: f.Description, on: f.Agents})
|
|
}
|
|
if rv := library.ReadRTK(); rv.Path != "" || len(rv.Agents) > 0 {
|
|
r := libRow{kind: "rtk", name: "RTK"}
|
|
switch {
|
|
case rv.Path == "":
|
|
r.about = "not installed · " + rv.URL
|
|
case rv.Gain != nil:
|
|
r.about = fmt.Sprintf("%s · %s tokens saved", rv.Version, fmtTokens(int(rv.Gain.Saved)))
|
|
default:
|
|
r.about = rv.Version
|
|
}
|
|
for _, a := range rv.Agents {
|
|
if a.On {
|
|
r.on = append(r.on, a.ID)
|
|
}
|
|
}
|
|
m.lib = append(m.lib, r)
|
|
}
|
|
m.lrow = clamp(m.lrow, len(m.lib))
|
|
}
|
|
|
|
// libName is an agent's name, by its id.
|
|
func (m model) libName(id string) string {
|
|
for _, a := range m.libAgents {
|
|
if a.ID == id {
|
|
return a.Name
|
|
}
|
|
}
|
|
return id
|
|
}
|
|
|
|
func (m model) updateLibrary(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|
n := len(m.lib)
|
|
key := msg.String()
|
|
if key != "d" {
|
|
m.confirm = ""
|
|
}
|
|
switch key {
|
|
case "j", "down":
|
|
if n > 0 {
|
|
m.lrow = (m.lrow + 1) % n
|
|
}
|
|
return m, nil
|
|
case "k", "up":
|
|
if n > 0 {
|
|
m.lrow = (m.lrow + n - 1) % n
|
|
}
|
|
return m, nil
|
|
case "a":
|
|
m.openLibAdd()
|
|
return m, nil
|
|
case "s":
|
|
m.flash, m.flashOK = "writing the library into the agents…", true
|
|
return m, libCmd(library.Sync, "synced")
|
|
}
|
|
if n == 0 {
|
|
return m, nil
|
|
}
|
|
r := m.lib[m.lrow]
|
|
switch key {
|
|
case "enter", " ":
|
|
switch r.kind {
|
|
case "instructions", "mcp", "skill", "rtk":
|
|
m.openLibAgents(r)
|
|
case "found-mcp", "found-skill":
|
|
m.flash, m.flashOK = "i brings "+r.name+" into the library, for magpie to give it to other agents too", true
|
|
if !r.own {
|
|
m.flash += "; d takes it out of every agent that has it"
|
|
}
|
|
}
|
|
case "e":
|
|
switch r.kind {
|
|
case "instructions":
|
|
return m, editInstructions()
|
|
case "mcp":
|
|
m.openServer(r.name)
|
|
}
|
|
case "u":
|
|
if r.kind != "skill" {
|
|
return m, nil
|
|
}
|
|
m.flash, m.flashOK = "fetching "+r.name+" from GitHub again…", true
|
|
return m, libCmd(func() (*library.Result, error) {
|
|
res, err := library.UpdateSkill(r.name, false)
|
|
var gone *library.GoneError
|
|
if errors.As(err, &gone) {
|
|
// removed from its source: kept till the user takes it out
|
|
err = fmt.Errorf("%w · d takes it out", err)
|
|
}
|
|
return res, err
|
|
}, "updated "+r.name)
|
|
case "i":
|
|
if r.own {
|
|
m.flash, m.flashOK = r.name+" is put there by the agent's app itself, each time it starts: it stays as it is", false
|
|
return m, nil
|
|
}
|
|
var do func(string) (*library.Result, error)
|
|
switch r.kind {
|
|
case "found-mcp":
|
|
do = library.ImportServer
|
|
case "found-skill":
|
|
do = library.ImportSkill
|
|
default:
|
|
return m, nil
|
|
}
|
|
return m, libCmd(func() (*library.Result, error) { return do(r.name) }, "brought "+r.name+" into the library")
|
|
case "d":
|
|
var do func(string) (*library.Result, error)
|
|
switch r.kind {
|
|
case "mcp":
|
|
do = library.RemoveServer
|
|
case "skill":
|
|
do = library.RemoveSkill
|
|
case "found-skill":
|
|
do = library.RemoveFoundSkill
|
|
case "found-mcp":
|
|
if r.own {
|
|
m.flash, m.flashOK = r.name+" is put there by the agent's app itself, each time it starts: it stays as it is", false
|
|
return m, nil
|
|
}
|
|
do = library.RemoveFoundServer
|
|
default:
|
|
return m, nil
|
|
}
|
|
if m.confirm != "library/"+r.kind+"/"+r.name {
|
|
m.confirm = "library/" + r.kind + "/" + r.name
|
|
m.flash, m.flashOK = "press d again to take "+r.name+" out of the library and every agent", false
|
|
switch r.kind {
|
|
case "found-skill":
|
|
m.flash = "press d again to take " + r.name + " out of every agent that has it, kept with the backups"
|
|
case "found-mcp":
|
|
m.flash = "press d again to take " + r.name + " out of every agent that has it, their files kept with the backups"
|
|
}
|
|
return m, nil
|
|
}
|
|
m.confirm = ""
|
|
return m, libCmd(func() (*library.Result, error) { return do(r.name) }, "removed "+r.name)
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// libCmd runs a library change, saying what it did to the agents.
|
|
func libCmd(do func() (*library.Result, error), done string) tea.Cmd {
|
|
return func() tea.Msg {
|
|
res, err := do()
|
|
if err != nil {
|
|
return flashMsg{text: err.Error()}
|
|
}
|
|
text, ok := libSaid(res, done)
|
|
return flashMsg{text: text, ok: ok}
|
|
}
|
|
}
|
|
|
|
// libSaid is a change's result in a line.
|
|
func libSaid(res *library.Result, done string) (string, bool) {
|
|
// an install of skills the library had already added none
|
|
if len(res.Installed) == 0 && len(res.Had) > 0 {
|
|
done = strings.Join(res.Had, ", ") + " in the library already"
|
|
}
|
|
for _, p := range res.Skipped {
|
|
done += " · " + strings.TrimPrefix(p.What, "skill:") + " skipped: " + p.Error
|
|
}
|
|
if len(res.Changed) > 0 {
|
|
done += " · written into " + strings.Join(res.Changed, ", ")
|
|
}
|
|
for _, p := range res.Problems {
|
|
done += " · " + p.Agent + ": " + p.Error
|
|
}
|
|
return done, len(res.Problems) == 0 && len(res.Skipped) == 0
|
|
}
|
|
|
|
// libOptions are the agents that can get a row's kind, those that get it
|
|
// marked.
|
|
func (m model) libOptions(r libRow) []agent.Option {
|
|
var out []agent.Option
|
|
add := func(id, name string) {
|
|
note := "○ off"
|
|
if slices.Contains(r.on, id) {
|
|
note = "● on"
|
|
}
|
|
out = append(out, agent.Option{Value: id, Note: note + " " + name})
|
|
}
|
|
if r.kind == "rtk" {
|
|
for _, a := range library.ReadRTK().Agents {
|
|
add(a.ID, a.Name)
|
|
}
|
|
return out
|
|
}
|
|
for _, a := range m.libAgents {
|
|
has := map[string]bool{"instructions": a.Instructions != "", "mcp": a.MCP != "", "skill": a.Skills != ""}[r.kind]
|
|
if has {
|
|
add(a.ID, a.Name)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// openLibAgents lists the agents a row can go to; enter gives it to one or
|
|
// takes it away, and the list stays open.
|
|
func (m *model) openLibAgents(r libRow) {
|
|
crumbs := []string{"library", r.name, "agents"}
|
|
m.pk = picker{
|
|
crumbs: crumbs,
|
|
input: newInput("filter agents"),
|
|
items: m.libOptions(r),
|
|
empty: "no agent here has a place for it",
|
|
toggle: func(id string) ([]agent.Option, string, bool) {
|
|
on := !slices.Contains(r.on, id)
|
|
var (
|
|
text string
|
|
ok = true
|
|
)
|
|
if r.kind == "rtk" {
|
|
v, err := library.SetRTK(id, on)
|
|
if err != nil {
|
|
return nil, err.Error(), false
|
|
}
|
|
r.on = nil
|
|
for _, a := range v.Agents {
|
|
if a.On {
|
|
r.on = append(r.on, a.ID)
|
|
}
|
|
}
|
|
text = "RTK " + map[bool]string{true: "on", false: "off"}[on] + " for " + m.libName(id)
|
|
if len(v.Restart) > 0 {
|
|
text += " · restart " + strings.Join(v.Restart, ", ") + " for it to take effect"
|
|
}
|
|
} else {
|
|
agents := slices.Clone(r.on)
|
|
if on {
|
|
agents = append(agents, id)
|
|
} else {
|
|
agents = slices.DeleteFunc(agents, func(a string) bool { return a == id })
|
|
}
|
|
var (
|
|
res *library.Result
|
|
err error
|
|
)
|
|
switch r.kind {
|
|
case "instructions":
|
|
res, err = library.SaveInstructions(library.InstructionsChange{Agents: agents})
|
|
case "mcp":
|
|
res, err = library.ServerAgents(r.name, agents)
|
|
case "skill":
|
|
res, err = library.SkillAgents(r.name, agents)
|
|
}
|
|
if err != nil {
|
|
return nil, err.Error(), false
|
|
}
|
|
r.on = agents
|
|
verb := "given to "
|
|
if !on {
|
|
verb = "taken from "
|
|
}
|
|
text, ok = libSaid(&library.Result{Problems: res.Problems}, r.name+" "+verb+m.libName(id))
|
|
}
|
|
return m.libOptions(r), text, ok
|
|
},
|
|
}
|
|
m.pk.refilter()
|
|
m.mode = modePick
|
|
m.back = modeList
|
|
}
|
|
|
|
// openLibAdd picks what to add: an MCP server, or skills.
|
|
func (m *model) openLibAdd() {
|
|
m.pk = picker{
|
|
crumbs: []string{"library", "add"},
|
|
input: newInput("filter"),
|
|
items: []agent.Option{
|
|
{Value: "mcp server", Note: "a URL, or a command it runs"},
|
|
{Value: "skills", Note: "from GitHub, GitLab or any git repository, or a folder on this machine"},
|
|
},
|
|
onPick: func(v string) tea.Cmd {
|
|
return func() tea.Msg {
|
|
if v == "skills" {
|
|
in := newInput("owner/repo, a GitHub or GitLab link, a git address (git@host:group/repo.git), or a folder's full path")
|
|
in.CharLimit = 400
|
|
return askMsg{ask{crumbs: []string{"library", "add", "skills"}, input: in,
|
|
hint: "magpie looks there for skills, then you pick one",
|
|
onEnter: probeCmd}}
|
|
}
|
|
in := newInput("<name> <url | command args…>")
|
|
in.CharLimit = 400
|
|
return askMsg{ask{crumbs: []string{"library", "add", "mcp server"}, input: in,
|
|
hint: "e.g. context7 https://mcp.context7.com/mcp · fs npx -y @modelcontextprotocol/server-filesystem ~/code\nthen ↵ on it gives it to agents",
|
|
onEnter: func(line string) tea.Cmd { return saveServer(nil, line) }}}
|
|
}
|
|
},
|
|
}
|
|
m.pk.refilter()
|
|
m.mode = modePick
|
|
m.back = modeList
|
|
}
|
|
|
|
// openServer types a library server again: its name, then its URL or
|
|
// command; what else it has (env, headers, agents) stays.
|
|
func (m *model) openServer(name string) {
|
|
var old *library.Server
|
|
for _, s := range m.libView.Servers {
|
|
if s.Name == name {
|
|
c := *s.Server
|
|
old = &c
|
|
}
|
|
}
|
|
if old == nil {
|
|
return
|
|
}
|
|
line := old.Name + " " + old.URL
|
|
if old.Command != "" {
|
|
line = strings.Join(append([]string{old.Name, old.Command}, old.Args...), " ")
|
|
}
|
|
in := newInput("<name> <url | command args…>")
|
|
in.CharLimit = 400
|
|
in.SetValue(line)
|
|
in.CursorEnd()
|
|
m.openAsk(ask{crumbs: []string{"library", name, "edit"}, input: in,
|
|
hint: "its env, headers and agents stay as they are",
|
|
onEnter: func(line string) tea.Cmd { return saveServer(old, line) }})
|
|
}
|
|
|
|
// saveServer makes a server of a typed line, in place of old when it is
|
|
// one being changed.
|
|
func saveServer(old *library.Server, line string) tea.Cmd {
|
|
return func() tea.Msg {
|
|
words := strings.Fields(line)
|
|
if len(words) < 2 {
|
|
return flashMsg{text: "a name, then a URL or a command"}
|
|
}
|
|
s, err := library.ServerOf(words[0], words[1:])
|
|
if err != nil {
|
|
return flashMsg{text: err.Error()}
|
|
}
|
|
was, done := "", "added "+s.Name+" · ↵ on it gives it to agents"
|
|
if old != nil {
|
|
was, done = old.Name, "saved "+s.Name
|
|
// and its OAuth app, which only the GUI edits
|
|
s.Agents, s.Env, s.Headers, s.OAuth = old.Agents, old.Env, old.Headers, old.OAuth
|
|
}
|
|
return libCmd(func() (*library.Result, error) { return library.SaveServer(was, s) }, done)()
|
|
}
|
|
}
|
|
|
|
// probedMsg is what was found at a source typed for skills.
|
|
type probedMsg struct{ p *library.Probe }
|
|
|
|
func probeCmd(source string) tea.Cmd {
|
|
return func() tea.Msg {
|
|
p, err := library.ProbeSkills(source)
|
|
if err != nil {
|
|
return flashMsg{text: err.Error()}
|
|
}
|
|
return probedMsg{p}
|
|
}
|
|
}
|
|
|
|
// openProbe lists the skills found at a source; enter adds one to the
|
|
// library.
|
|
func (m *model) openProbe(p *library.Probe) {
|
|
var items []agent.Option
|
|
for _, c := range p.Candidates {
|
|
note := c.Description
|
|
if c.Have {
|
|
note = "in the library already · " + note
|
|
}
|
|
v := c.Path
|
|
if v == "" {
|
|
v = "."
|
|
}
|
|
items = append(items, agent.Option{Value: v, Note: c.Name + " " + trunc(note, 80)})
|
|
}
|
|
m.pk = picker{
|
|
crumbs: []string{"library", "add", "skills", p.Source},
|
|
input: newInput("filter skills"),
|
|
items: items,
|
|
empty: "no skills there: a skill is a folder with a SKILL.md",
|
|
onPick: func(path string) tea.Cmd {
|
|
if path == "." {
|
|
path = ""
|
|
}
|
|
return libCmd(func() (*library.Result, error) {
|
|
return library.InstallSkills(p.Source, []string{path}, []string{})
|
|
}, "added the skill · ↵ on it gives it to agents")
|
|
},
|
|
}
|
|
m.pk.refilter()
|
|
m.mode = modePick
|
|
m.back = modeList
|
|
// skills too big to install on their own, not in the list (#2062)
|
|
var left []string
|
|
for _, l := range p.Left {
|
|
name := p.Source
|
|
if l.Path != "" {
|
|
name = path.Base(l.Path)
|
|
}
|
|
left = append(left, fmt.Sprintf("%s (%d MB)", name, l.Size>>20))
|
|
}
|
|
if len(left) > 0 {
|
|
m.flash = "too big to install, over 200 MB a skill: " + strings.Join(left, ", ")
|
|
}
|
|
}
|
|
|
|
// editedMsg is the shared instructions as the editor left them.
|
|
type editedMsg struct {
|
|
text string
|
|
err error
|
|
}
|
|
|
|
// editInstructions opens the shared instructions in $VISUAL or $EDITOR
|
|
// and saves what it leaves, which writes it into the agents that get it.
|
|
func editInstructions() tea.Cmd {
|
|
v, err := library.ReadInstructions()
|
|
if err != nil {
|
|
return func() tea.Msg { return flashMsg{text: err.Error()} }
|
|
}
|
|
f, err := os.CreateTemp("", "magpie-instructions-*.md")
|
|
if err != nil {
|
|
return func() tea.Msg { return flashMsg{text: err.Error()} }
|
|
}
|
|
f.WriteString(v.Shared)
|
|
f.Close()
|
|
ed := os.Getenv("VISUAL")
|
|
if ed == "" {
|
|
ed = os.Getenv("EDITOR")
|
|
}
|
|
if ed == "" {
|
|
ed = "vi"
|
|
if runtime.GOOS == "windows" {
|
|
ed = "notepad"
|
|
}
|
|
}
|
|
// the editor may come with flags: code -w
|
|
words := strings.Fields(ed)
|
|
c := proc.Command(words[0], append(words[1:], f.Name())...)
|
|
return tea.ExecProcess(c, func(err error) tea.Msg {
|
|
defer os.Remove(f.Name())
|
|
if err != nil {
|
|
return editedMsg{err: err}
|
|
}
|
|
b, err := os.ReadFile(f.Name())
|
|
if err != nil {
|
|
return editedMsg{err: err}
|
|
}
|
|
if string(b) == v.Shared {
|
|
return flashMsg{text: "the instructions are as they were", ok: true}
|
|
}
|
|
return editedMsg{text: string(b)}
|
|
})
|
|
}
|
|
|
|
func saveInstructions(text string) tea.Cmd {
|
|
return libCmd(func() (*library.Result, error) {
|
|
return library.SaveInstructions(library.InstructionsChange{Shared: &text})
|
|
}, "instructions saved")
|
|
}
|
|
|
|
func (m model) viewLibrary() string {
|
|
var b strings.Builder
|
|
b.WriteString(m.header())
|
|
b.WriteString("\n\n")
|
|
if m.libErr != "" {
|
|
b.WriteString(pad + " " + sBad.Render(m.libErr))
|
|
return b.String()
|
|
}
|
|
nameW := 0
|
|
for _, r := range m.lib {
|
|
nameW = max(nameW, len([]rune(r.name)))
|
|
}
|
|
nameW = min(nameW, 32)
|
|
heads := map[string]string{
|
|
"instructions": "instructions · what every agent reads before a conversation",
|
|
"mcp": "mcp servers · a adds one, e changes it",
|
|
"skill": "skills · a adds some from GitHub or a folder, u fetches one from GitHub again",
|
|
"found-mcp": "mcp servers in your agents, not in the library",
|
|
"found-skill": "skills in your agents, not in the library",
|
|
"rtk": "rtk · its hook in each agent, by RTK's own installer",
|
|
}
|
|
// the rows around the cursor, as many as fit
|
|
visible := max(4, m.h-6)
|
|
// where the window starts is counted in the lines it draws — counted in
|
|
// rows alone, a list of several sections drew heads the budget never
|
|
// paid for, and the row picked fell off the bottom of the page
|
|
spend := func(from int) int {
|
|
lines := 0
|
|
for i := from; i <= m.lrow && lines <= visible; i++ {
|
|
lines++
|
|
if i == from || m.lib[i].kind != m.lib[i-1].kind {
|
|
lines += 2 // the head, and the blank before it
|
|
if i == 0 {
|
|
lines-- // the list's first section has no blank above it
|
|
}
|
|
}
|
|
}
|
|
return lines
|
|
}
|
|
start := m.lrow
|
|
for start > 0 && spend(start-1) <= visible {
|
|
start--
|
|
}
|
|
last, lines := "", 0
|
|
for i := start; i < len(m.lib) && lines < visible; i++ {
|
|
r := m.lib[i]
|
|
if r.kind != last {
|
|
if last != "" || start > 0 {
|
|
b.WriteString("\n")
|
|
lines++
|
|
}
|
|
b.WriteString(pad + " " + sFaint.Render(heads[r.kind]) + "\n")
|
|
lines++
|
|
last = r.kind
|
|
}
|
|
marker, name := " ", sText.Render(padRight(trunc(r.name, nameW), nameW))
|
|
if i == m.lrow {
|
|
marker, name = sCursor.Render("▸ "), sNameOn.Render(padRight(trunc(r.name, nameW), nameW))
|
|
}
|
|
var agents []string
|
|
for _, id := range r.on {
|
|
agents = append(agents, m.libName(id))
|
|
}
|
|
on := sMuted.Render(strings.Join(agents, ", "))
|
|
switch {
|
|
case r.own:
|
|
on = sFaint.Render(strings.Join(agents, ", ") + "'s own")
|
|
case len(agents) == 0 && strings.HasPrefix(r.kind, "found-"):
|
|
case len(agents) == 0:
|
|
on = sFaint.Render("no agent")
|
|
}
|
|
line := pad + marker + name + " " + on
|
|
if r.bad > 0 {
|
|
line += " " + sBad.Render(fmt.Sprintf("! %d", r.bad))
|
|
}
|
|
if r.about != "" {
|
|
if room := m.w - lipgloss.Width(line) - 4; room > 8 {
|
|
line += " " + sFaint.Render(trunc(r.about, room))
|
|
}
|
|
}
|
|
b.WriteString(line + "\n")
|
|
lines++
|
|
}
|
|
b.WriteString("\n" + pad + " " + sFaint.Render("kept in "+tilde(m.libView.Dir)))
|
|
return b.String()
|
|
}
|
|
|
|
// trunc cuts s to n runes, with an ellipsis.
|
|
func trunc(s string, n int) string {
|
|
s = strings.Join(strings.Fields(s), " ")
|
|
if r := []rune(s); len(r) > n {
|
|
return string(r[:max(1, n-1)]) + "…"
|
|
}
|
|
return s
|
|
}
|