Files
magpie/internal/agent/hermes.go
T
yetone 17f790b145 agent: five package-level twins nothing calls are gone — isCodexGateway, ccProviderJSON, fxProvider, hermesProvider and writeMiniMaxEntry
Each was a one-line wrapper of its *At function for the local gateway.
The callers moved to closures of the same name over the agent's own
address (at.v1(), at.gw()), which shadow the package function, so the
wrappers were reached by nothing, tests included (deadcode -test). Their
doc comments now sit on the *At functions, and isCodexGatewayOn has its
own again: it had been left above codexKeptGateway.

go vet (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui
./internal/agent under a temp HOME pass.
2026-10-06 04:04:47 +08:00

155 lines
5.1 KiB
Go

package agent
// Hermes Agent (Nous Research) keeps its settings in $HERMES_HOME/config.yaml,
// ~/.hermes/config.yaml by default: the model under model.default, served by
// model.provider, which may name an entry of providers. magpie adds itself
// there as the provider "magpie" (the gateway, spoken to as chat completions,
// with the catalog as its models) and points model.provider at it; a model
// through magpie is "magpie/<provider>/<model>" here and model.default holds
// "<provider>/<model>". The provider and model the user had are stashed and
// put back when magpie steps out.
import (
"path/filepath"
"strings"
"github.com/yetone/magpie/internal/edit"
)
func hermes(home string) *Agent { return hermesIn(here(home)) }
// hermesIn is Hermes at a place: this machine's home, or a WSL distro's
// (see wsl.go), where HERMES_HOME isn't read and its provider names the
// gateway as the distro reaches it.
func hermesIn(at place) *Agent {
dir := at.getenv("HERMES_HOME")
if dir == "" {
dir = filepath.Join(at.home, ".hermes")
}
path := filepath.Join(dir, "config.yaml")
hermesProvider := func() hermesProviderEntry { return hermesProviderAt(at.gw()) }
key := "hermes:" + path + ":"
getKey := func(k string) string { v, _ := edit.GetYAML(path, k); return v }
onMagpie := func() bool { return getKey("model.provider") == magpieID }
// restore puts back the provider and model the user had before magpie
restore := func() error {
var del []string
var kvs []edit.KV
for _, k := range []string{"model.provider", "model.default"} {
if v := unstash(key + k); v != "" {
kvs = append(kvs, edit.KV{Path: k, Value: v})
} else {
del = append(del, k)
}
}
if err := edit.DelYAML(path, append(del, "providers."+magpieID)...); err != nil {
return err
}
if len(kvs) == 0 {
return nil
}
return edit.SetYAML(path, kvs...)
}
return &Agent{
ID: "hermes", Name: "Hermes Agent", Icon: "hermes", Aliases: []string{"hermes-agent"}, Spelled: prefixed,
UA: []string{"hermes-agent"},
Bin: "hermes", Dir: dir, Path: path,
Sync: func() error {
return syncYAML(path, "providers."+magpieID, func() any { return hermesProvider() })
},
Notice: func() string {
if Running(`(^|/)hermes( |$)`) {
return "Hermes reads its settings at start-up — restart open Hermes sessions to use this."
}
return ""
},
Check: func() string {
if !onMagpie() {
return ""
}
return wiringOff("Hermes", path, func(k string) (string, bool) { return edit.GetYAML(path, "providers."+magpieID+"."+k) },
"base_url", at.v1(), "api_key", at.gwKey())
},
Fields: []Field{{
Key: "model", Label: "model",
Get: func() string {
v := getKey("model.default")
if v == "" {
v = getKey("model.model")
}
if v != "" && onMagpie() {
return magpieID + "/" + v
}
return v
},
Set: func(v string) error {
if v == "" {
if onMagpie() {
return restore()
}
return edit.DelYAML(path, "model.default")
}
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
if !onMagpie() {
stash(map[string]string{
key + "model.provider": getKey("model.provider"),
key + "model.default": getKey("model.default"),
})
}
return edit.SetYAML(path,
edit.KV{Path: "providers." + magpieID, Value: hermesProvider()},
edit.KV{Path: "model.provider", Value: magpieID},
edit.KV{Path: "model.default", Value: ref},
)
}
if onMagpie() {
if err := restore(); err != nil {
return err
}
}
return edit.SetYAML(path, edit.KV{Path: "model.default", Value: v})
},
Options: func(cur map[string]string) []Option {
return append(ownOptions("", cur["model"]), viaMagpie("hermes", magpieID+"/")...)
},
}, {
// agent.reasoning_effort, which Hermes's /reasoning saves; none
// turns reasoning off, and unset Hermes asks for medium
Key: "effort", Label: "effort",
Get: func() string { return getKey("agent.reasoning_effort") },
Set: func(v string) error {
if v == "" {
return edit.DelYAML(path, "agent.reasoning_effort")
}
return edit.SetYAML(path, edit.KV{Path: "agent.reasoning_effort", Value: v})
},
Options: func(map[string]string) []Option {
return static("none", "minimal", "low", "medium", "high", "xhigh")
},
}},
}
}
type hermesProviderEntry struct {
Name string `yaml:"name"`
BaseURL string `yaml:"base_url"`
APIKey string `yaml:"api_key"`
APIMode string `yaml:"api_mode"`
Headers map[string]string `yaml:"extra_headers"`
Models []string `yaml:"models"`
}
// hermesProviderAt is magpie's entry under providers, for a Hermes reaching
// the gateway at gw. Hermes sends its own User-Agent only from a recent
// release on, so the header names it for the gateway's usage view.
func hermesProviderAt(gw string) hermesProviderEntry {
ms := []string{}
for _, m := range magpieModels("hermes") {
ms = append(ms, m.ID)
}
return hermesProviderEntry{
Name: magpieID, BaseURL: gw + "/v1", APIKey: keyAt(gw), APIMode: "chat_completions",
Headers: map[string]string{"User-Agent": "hermes-agent"}, Models: ms,
}
}