Files
magpie/internal/agent/hermes.go
T

151 lines
4.8 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 (
"os"
"path/filepath"
"strings"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
)
func hermes(home string) *Agent {
dir := os.Getenv("HERMES_HOME")
if dir == "" {
dir = filepath.Join(home, ".hermes")
}
path := filepath.Join(dir, "config.yaml")
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"},
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", gatewayV1(), "api_key", gateway.Token)
},
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"`
}
// hermesProvider is magpie's entry under providers. Hermes sends its own
// User-Agent only from a recent release on, so the header names it for the
// gateway's usage view.
func hermesProvider() hermesProviderEntry {
ms := []string{}
for _, m := range magpieModels("hermes") {
ms = append(ms, m.ID)
}
return hermesProviderEntry{
Name: magpieID, BaseURL: gatewayV1(), APIKey: gateway.Token, APIMode: "chat_completions",
Headers: map[string]string{"User-Agent": "hermes-agent"}, Models: ms,
}
}