diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index bc027f00b..0a1925da2 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -72,6 +72,7 @@ something else shows up as drift. - The config magpie connects is the profile `hermes` starts in: `HERMES_HOME` when it names a profile, else the one `hermes profile use` made sticky in `/active_profile` (a live profile folder; `hermes model` writes the same one), else `HERMES_HOME` or the default folder (`hermesActive`; `_apply_profile_override` in Hermes's `hermes_cli/main.py`). The root's config is then one more profile. A bot on the model the connected config is on through magpie follows too, besides one on the recorded model (`hermes model` there before the bot was made). - Disconnect moves a profile on magpie onto its own model from when magpie connected it, else the connected config's own model (with that provider's `providers` entry when the profile lacks it, as Hermes's `launch_model_seed` does); with neither, its model goes. What it was on is kept (`hermes::off`), and connecting again puts it back when nothing has changed it, and gives a profile with no model at all magpie's (`hermesBack`). The recorded model stays through Disconnect. Before, Off emptied every such profile and `hermes` there failed at start after Off and On (tjhhhh on Discord, `TestHermesOffAndOnKeepsEveryBotsModel`, `TestHermesStickyProfile`). - With more than one profile, each profile other than the connected one has a field of its own, `profile:` (`default` for the root), drawn as a line of the opened row's Other profiles with `hermes -p ` beside it, never as a square of the row's (`hermesProfileFields`; `hermesBot` in app.js; dongshanfengdongshizhcn on Discord). A magpie model puts magpie's provider into that profile and stashes its own provider and model first; a model of its own is written as `model.default` and gives back the stashed provider. Default puts back the profile's model from before magpie, or keeps the one it has: Disconnect sends the same empty value. A bot picked there is recorded (`hermes::picked`) and doesn't follow the connected config's next model, and a sync keeps magpie's provider current in a bot on magpie while the connected config isn't (`TestHermesProfileModels`, `hermes-profiles.test.cjs`). + - Each profile other than the connected one is also an agent row of its own, `hermes#` (`hermesProfileAgents`, named `Hermes · `; `default` for the root when a named profile is connected), the way omp's are `omp#`: its one field, `model`, is the same pick as the connected row's `profile:`, and the Library gives it skills and MCP servers apart from the other profiles, since Hermes reads `skills/` and `mcp_servers` from each profile's own home (`targetOf` in `internal/library/targets.go`; Norethcy on X). None is listed while Hermes itself is gone (`CLIMissing`). - Hermes's `api_key` may be a reference, `${VAR}` or `${env:VAR}`, which Hermes expands from the `.env` beside the config (its own or a profile's) and then the environment. `Check` and a sync read the key it names (`hermesKey`): one that holds (`keyHolds`) stays and isn't drift, and so does one naming a variable magpie can't see (only a shell profile sets it), which is unknown. One naming a key the gateway doesn't take is drift, and a sync keeps the reference and writes magpie's key into that `.env` under the variable, made `0600` when there is none (`hermesKeptKey`; PaulT on Discord, `TestHermesWithAKeyInItsEnvFile`). A value with more than the one reference in it is replaced by magpie's key, as before. - omp's `models.yml` keeps `modelOverrides`, `discovery`, `disableStrictTools`, headers and compat flags (`ompKept`). Its `auth` and `apiKey` are magpie's, or gone. - Kimi Code's `[providers.magpie]` has magpie's three keys set in place, so `custom_headers`, `env` and `default_model` stay (`kimiProvider`). diff --git a/internal/agent/agents.go b/internal/agent/agents.go index ff211762f..525c5f16f 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -359,7 +359,7 @@ func onMachineAll(home, cfg string) []*Agent { aionui(), cherryStudio(home), lody(home), - }, append(ompProfiles(home), qwenpawAgents(home)...)...) + }, append(append(ompProfiles(home), qwenpawAgents(home)...), hermesProfileAgents(home)...)...) } // ---- accessors ------------------------------------------------------------- diff --git a/internal/agent/hermes.go b/internal/agent/hermes.go index e110fdd1d..0f0a0496a 100644 --- a/internal/agent/hermes.go +++ b/internal/agent/hermes.go @@ -332,74 +332,124 @@ const hermesProfileKey = "profile:" // (hermesFollow). Unset puts back the model it had before magpie, and a // profile nobody picks for here keeps what it has. func hermesProfileFields(at place, dir string) []Field { - root := filepath.Clean(hermesRoot(at)) var out []Field for _, p := range hermesOthers(at, dir) { - name := "default" - if d := filepath.Clean(filepath.Dir(p)); d != root { - name = filepath.Base(d) - } - k := "hermes:" + p + ":" - out = append(out, Field{ - Key: hermesProfileKey + name, Label: name, Quiet: true, - Get: func() string { - m := hermesModelOf(p) - if m.Default != "" && hermesOurs(m.Provider) { - return magpieID + "/" + m.Default + name := hermesProfileName(at, p) + out = append(out, hermesProfileField(at, p, hermesProfileKey+name, name)) + } + return out +} + +// hermesProfileName is the name `hermes -p ` gives the profile whose +// config is at p: "default" for the root's own. +func hermesProfileName(at place, p string) string { + if d := filepath.Clean(filepath.Dir(p)); d != filepath.Clean(hermesRoot(at)) { + return filepath.Base(d) + } + return "default" +} + +// hermesProfileField is the model field of the profile whose config is at +// p, under key. +func hermesProfileField(at place, p, key, label string) Field { + k := "hermes:" + p + ":" + return Field{ + Key: key, Label: label, Quiet: true, + Get: func() string { + m := hermesModelOf(p) + if m.Default != "" && hermesOurs(m.Provider) { + return magpieID + "/" + m.Default + } + return m.Default + }, + Set: func(v string) error { + cur := hermesModelOf(p) + if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) { + if !hermesOurs(cur.Provider) { + stash(map[string]string{k + "model.provider": cur.Provider, k + "model.default": cur.Default}) } - return m.Default - }, - Set: func(v string) error { - cur := hermesModelOf(p) - if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) { - if !hermesOurs(cur.Provider) { - stash(map[string]string{k + "model.provider": cur.Provider, k + "model.default": cur.Default}) + if err := hermesPut(at, p, true); err != nil { + return err + } + if err := edit.SetYAML(p, + edit.KV{Path: "model.provider", Value: hermesRoute(p, ref)}, + edit.KV{Path: "model.default", Value: ref}, + ); err != nil { + return err + } + stash(map[string]string{k + "picked": ref}) + return nil + } + forget(k + "picked") + if hermesOurs(cur.Provider) { + // back to the provider and model it had before magpie + var del []string + var kvs []edit.KV + for _, f := range []string{"model.provider", "model.default"} { + if was := unstash(k + f); was != "" { + kvs = append(kvs, edit.KV{Path: f, Value: was}) + } else { + del = append(del, f) } - if err := hermesPut(at, p, true); err != nil { + } + if len(del) > 0 { + if err := edit.DelYAML(p, del...); err != nil { return err } - if err := edit.SetYAML(p, - edit.KV{Path: "model.provider", Value: hermesRoute(p, ref)}, - edit.KV{Path: "model.default", Value: ref}, - ); err != nil { + } + if len(kvs) > 0 { + if err := edit.SetYAML(p, kvs...); err != nil { return err } - stash(map[string]string{k + "picked": ref}) - return nil } - forget(k + "picked") - if hermesOurs(cur.Provider) { - // back to the provider and model it had before magpie - var del []string - var kvs []edit.KV - for _, f := range []string{"model.provider", "model.default"} { - if was := unstash(k + f); was != "" { - kvs = append(kvs, edit.KV{Path: f, Value: was}) - } else { - del = append(del, f) - } - } - if len(del) > 0 { - if err := edit.DelYAML(p, del...); err != nil { - return err - } - } - if len(kvs) > 0 { - if err := edit.SetYAML(p, kvs...); err != nil { - return err - } - } + } + // unset: the profile keeps its own model, which Hermes + // needs to start there + if v == "" { + return nil + } + return edit.SetYAML(p, edit.KV{Path: "model.default", Value: v}) + }, + Options: func(cur map[string]string) []Option { + return withVia(ownOptions("", cur[key]), "hermes", magpieID+"/") + }, + } +} + +// hermesProfileAgents are Hermes's other profiles as agents of their own, +// hermes# (the way omp's are omp#): each has its own +// config.yaml with its model and its mcp_servers and its own skills folder, +// none of which the connected profile's reach, so the Library gives its +// skills and servers to each profile apart (Norethcy on X: only model +// picks reached the other profiles). The row is the profile's model +// picker; the connected profile keeps the default row. None while Hermes +// itself isn't here: a leftover folder is no agent. +func hermesProfileAgents(home string) []*Agent { + at := here(home) + main := hermesIn(at) + if !main.Detected() || main.CLIMissing() { + return nil + } + var out []*Agent + for _, p := range hermesOthers(at, main.Dir) { + name := hermesProfileName(at, p) + dir := filepath.Dir(p) + out = append(out, &Agent{ + ID: "hermes#" + name, Name: "Hermes · " + name, Icon: "hermes", + Spelled: prefixed, NoNames: true, + Dir: dir, Path: p, + Fields: []Field{func() Field { + f := hermesProfileField(at, p, "model", "model") + f.Quiet = false + return f + }()}, + Notice: func() string { + if Running(`(^|/)hermes( |$)`) { + return noticeHermes.String() } - // unset: the profile keeps its own model, which Hermes - // needs to start there - if v == "" { - return nil - } - return edit.SetYAML(p, edit.KV{Path: "model.default", Value: v}) - }, - Options: func(cur map[string]string) []Option { - return withVia(ownOptions("", cur[hermesProfileKey+name]), "hermes", magpieID+"/") + return "" }, + detect: func() bool { return isDir(dir) }, }) } return out diff --git a/internal/agent/hermes_test.go b/internal/agent/hermes_test.go index ac729bfbd..7b2ce5ba0 100644 --- a/internal/agent/hermes_test.go +++ b/internal/agent/hermes_test.go @@ -1005,3 +1005,56 @@ func TestHermesProfileModels(t *testing.T) { t.Fatalf("default profile:\n%s", b) } } + +// Norethcy on X: Hermes's other profiles are agents of their own +// (hermes#), so the Library can give each its skills and servers. The +// row is the profile's model picker, and none is listed with Hermes gone. +func TestHermesProfilesAreAgents(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("HERMES_HOME", "") + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + root := appdir.HermesHomeIn(home) + write := func(rel, s string) string { + p := filepath.Join(root, rel) + os.MkdirAll(filepath.Dir(p), 0o755) + os.WriteFile(p, []byte(s), 0o644) + return p + } + write("config.yaml", "model:\n default: anthropic/claude-opus-5\n provider: openrouter\n") + write("hermes-agent/run_agent.py", "") + coder := write("profiles/coder/config.yaml", "model:\n default: kimi-k3\n provider: moonshot\n") + write("profiles/old/config.yaml", "model:\n default: x\n") + write("profiles/.deleted/old", "") + ids := func() (out []string) { + for _, a := range onMachine(home, filepath.Join(home, ".config")) { + if strings.HasPrefix(a.ID, "hermes") { + out = append(out, a.ID) + } + } + return + } + if got := ids(); !slices.Equal(got, []string{"hermes", "hermes#coder"}) { + t.Fatalf("agents %v", got) + } + // Hermes uninstalled, its folder left behind: no profile is an agent + os.RemoveAll(filepath.Join(root, "hermes-agent")) + if got := ids(); len(got) != 1 { + t.Fatalf("with Hermes gone: %v", got) + } + write("hermes-agent/run_agent.py", "") + var row *Agent + for _, a := range onMachine(home, filepath.Join(home, ".config")) { + if a.ID == "hermes#coder" { + row = a + } + } + if row.Name != "Hermes · coder" || row.Dir != filepath.Dir(coder) || row.Path != coder || !row.Detected() { + t.Fatalf("row: %+v", row) + } + if f := row.Field("model"); f == nil || f.Get() != "kimi-k3" { + t.Fatalf("model field: %+v", f) + } +} diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 15e59c0f2..0c69aec59 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -1454,7 +1454,7 @@ function connectLine(a, kind) { const at = PICKS_IN[kindOf(a)]; if (menuFromList(a)) say(connectSaid(a)); else if (kindOf(a) === "claude") say(t("Connected · switch the opus · sonnet · haiku tiers in /model")); - else if (NO_PICKER.has(kindOf(a))) say(t("Connected · starts on the model picked here")); else if (at && a.models) say(t("Connected · {n} models in {agent}'s {cmd}", { n: a.models.shown, agent: a.name, cmd: at })); + else if (NO_PICKER.has(kindOf(a).split("#")[0])) say(t("Connected · starts on the model picked here")); else if (at && a.models) say(t("Connected · {n} models in {agent}'s {cmd}", { n: a.models.shown, agent: a.name, cmd: at })); else say(connectSaid(a)); return line; } diff --git a/internal/gui/tests/hermes-profile-row.test.cjs b/internal/gui/tests/hermes-profile-row.test.cjs new file mode 100644 index 000000000..26e459d58 --- /dev/null +++ b/internal/gui/tests/hermes-profile-row.test.cjs @@ -0,0 +1,85 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// Hermes's other profiles are agents of their own (hermes#, Norethcy on +// X) so the Library can give each its skills and MCP servers. The row is +// named for the profile, says it starts on the model picked here as the +// Hermes row does, and the picker posts the encoded id (a raw # would be +// the URL's fragment). In every language, in both engines. No backend: the +// API is faked here. +const assert = require("node:assert/strict"); +const fs = require("node:fs/promises"); +const path = require("node:path"); +const { test } = require("node:test"); +const { chromium, webkit } = require("playwright"); + +const assets = path.resolve(__dirname, "../assets"); +const models = [{ value: "magpie/v/glm", label: "GLM", ref: "v/glm" }, { value: "magpie/v/flash", label: "Flash", ref: "v/flash" }]; +const hermes = (id, bots) => ({ + id, name: "Hermes", path: "/test/.hermes/config.yaml", icon: "hermes", wired: true, + fields: [ + { key: "model", label: "model", value: "magpie/v/glm", options: models }, + ...bots.map((b) => ({ key: "profile:" + b, label: b, value: b === "coder" ? "kimi-k3.5" : "magpie/v/glm", quiet: true, options: [{ value: "kimi-k3.5" }, ...models] })), + ], +}); +const fresh = () => ({ + agents: [ + { id: "hermes", name: "Hermes Agent", path: "/test/.hermes/config.yaml", icon: "hermes", wired: true, fields: [{ key: "model", label: "model", value: "magpie/v/glm", options: models }] }, + { id: "hermes#coder", name: "Hermes · coder", path: "/test/.hermes/profiles/coder/config.yaml", icon: "hermes", wired: true, fields: [{ key: "model", label: "model", value: "magpie/v/glm", options: models }] }, + ], + profiles: [], +}); + +function server(lang, sets) { + let cur = fresh(); + return async (route) => { + const req = route.request(), url = new URL(req.url()); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` }); + if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return route.fulfill({ json: { ...cur, settings: { lang, theme: "light" } } }); + if (url.pathname === "/api/set") { + const body = req.postDataJSON(); + sets.push(body); + cur = JSON.parse(JSON.stringify(cur)); + cur.agents.find((a) => a.id === body.agent).fields.find((f) => f.key === body.field).value = body.value; + return route.fulfill({ json: { ...cur, settings: { lang, theme: "light" } } }); + } + if (url.pathname === "/api/usage/quotas") return route.fulfill({ json: [] }); + if (url.pathname === "/api/groups") return route.fulfill({ json: { groups: [] } }); + if (url.pathname === "/api/agents/cli") return route.fulfill({ json: { agents: {}, pending: false } }); + if (url.pathname === "/api/providers") return route.fulfill({ json: { providers: [], gateway: { running: true } } }); + if (url.pathname === "/api/plugins") return route.fulfill({ json: { plugins: [] } }); + if (url.pathname.startsWith("/api/")) return route.fulfill({ json: {} }); + const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname); + const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)]; + await route.fulfill({ body: await fs.readFile(file), contentType }); + }; +} + +const W = { + en: "Connected · starts on the model picked here", + zh: "已接入 · 用这里选的模型启动", + "zh-TW": "已接入 · 用這裡選的模型啟動", + ja: "接続済み · ここで選んだモデルで起動", + de: "Verbunden · startet mit dem hier gewählten Modell", +}; + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of Object.keys(W)) { + test(`${engine} ${lang}: a Hermes profile is a row of its own`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + const page = await browser.newPage({ viewport: { width: 560, height: 800 } }); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = [], sets = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", server(lang, sets)); + t.after(() => browser.close()); + await page.goto("http://magpie.test/"); + const row = '.row.agent[data-id="hermes#coder"]'; + await page.locator(row).waitFor(); + assert.ok((await page.locator(row).innerText()).includes("Hermes · coder")); + assert.ok((await page.locator(row).innerText()).includes(W[lang]), await page.locator(row).innerText()); + assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), "the Agents view overflows"); + assert.deepEqual(errors, []); + }); + } +} diff --git a/internal/library/hermes_mcp_test.go b/internal/library/hermes_mcp_test.go index 34dc82e27..52886fe17 100644 --- a/internal/library/hermes_mcp_test.go +++ b/internal/library/hermes_mcp_test.go @@ -1,6 +1,7 @@ package library import ( + "os" "path/filepath" "strings" "testing" @@ -150,3 +151,50 @@ func TestHermesMCPFound(t *testing.T) { t.Errorf("remote: %+v", s) } } + +// Norethcy on X: Hermes's profiles got a model each but no skills or MCP +// servers, which reached the connected profile alone. Each other profile is +// an agent of its own (hermes#) with its own config.yaml and skills +// folder (hermes_cli/profiles.py): a server or skill given to one lands in +// that profile and in no other. +func TestHermesProfilesGetTheirOwnLibrary(t *testing.T) { + h := sandbox(t) + t.Setenv("HERMES_HOME", "") + root := appdir.HermesHomeIn(h) + main := filepath.Join(root, "config.yaml") + coder := filepath.Join(root, "profiles", "coder", "config.yaml") + write(t, main, hermesConfig) + write(t, coder, "model:\n default: kimi-k3\n provider: moonshot\n") + holdCLI(t, h, "hermes") + + tg := targetByID("hermes#coder") + if tg == nil || tg.MCP == nil || tg.MCP.Path != coder || tg.Skills != filepath.Join(root, "profiles", "coder", "skills") { + t.Fatalf("coder's target: %+v", tg) + } + if m := targetByID("hermes"); m == nil || m.MCP.Path != main || m.Skills != filepath.Join(root, "skills") { + t.Fatalf("the default profile's target: %+v", m) + } + + ok(t)(SaveServer("", Server{Name: "fs", Transport: "stdio", Command: "npx", Args: []string{"-y", "@mcp/fs"}, Agents: []string{"hermes#coder"}})) + if !strings.Contains(read(t, coder), "fs:") || !strings.Contains(read(t, coder), "provider: moonshot") { + t.Errorf("coder has no fs, or lost its own settings:\n%s", read(t, coder)) + } + if strings.Contains(read(t, main), "fs:") { + t.Errorf("the server reached the default profile:\n%s", read(t, main)) + } + + src := filepath.Join(h, "src/skills") + skill(t, filepath.Join(src, "pdf"), "pdf", "Read PDFs") + ok(t)(InstallSkills(src, []string{"pdf"}, []string{"hermes#coder"})) + if !ours(filepath.Join(root, "profiles", "coder", "skills", "pdf"), "pdf") { + t.Error("coder's skills folder has no pdf") + } + if _, err := os.Lstat(filepath.Join(root, "skills", "pdf")); !os.IsNotExist(err) { + t.Error("the skill reached the default profile") + } + + ok(t)(RemoveServer("fs")) + if strings.Contains(read(t, coder), "fs:") { + t.Errorf("fs stays in coder:\n%s", read(t, coder)) + } +} diff --git a/internal/library/mcp_tools_off.go b/internal/library/mcp_tools_off.go index ed45135d3..eaaf2066b 100644 --- a/internal/library/mcp_tools_off.go +++ b/internal/library/mcp_tools_off.go @@ -50,6 +50,8 @@ func toolsOffOf(t *Target) toolsOffHow { return toolsNone } id, _, _ := strings.Cut(t.Agent.ID, "@") + // another Hermes profile (hermes#coder) reads tools.exclude as the first + id, _, _ = strings.Cut(id, "#") switch { case id == "claude" && t.MCP.Format == fmtClaude && t.Settings != "": return toolsClaudeDeny diff --git a/internal/library/targets.go b/internal/library/targets.go index 570818c69..384830175 100644 --- a/internal/library/targets.go +++ b/internal/library/targets.go @@ -146,6 +146,14 @@ func targetOf(a *agent.Agent) *Target { } h := home() t := &Target{Agent: a} + if strings.HasPrefix(a.ID, "hermes#") { + // another Hermes profile (agent.hermesProfileAgents) is a Hermes + // home of its own: its config.yaml holds its mcp_servers, and its + // skills folder is beside it + t.MCP = &mcpFile{Path: a.Path, Format: fmtHermes} + t.Skills = filepath.Join(a.Dir, "skills") + return t + } if n := a.SharedName(); n != a.Name { // the files are another app's too (the Cursor editor's ~/.cursor): // named for both, not for the CLI alone