Files
magpie/internal/agent/goose.go
T
yetone 94db53a847 goose: the model is read and written in the layout Goose's config is in, active_provider and the providers map as well as GOOSE_PROVIDER/GOOSE_MODEL (a follow-up to #987: the reporter's Goose 1.53 has active_provider: magpie and its model in providers.magpie.model) — Goose 1.53 reads active_provider before GOOSE_PROVIDER and the active entry's model before GOOSE_MODEL (get_active_provider/get_active_model, crates/goose/src/config/providers.rs at v1.53.0), and on every write migrates the flat keys into the map and, once a map is there, deletes them (migrate_provider_config/cleanup_legacy_provider_keys, config/migrations.rs; load_write_config, config/base.rs). magpie read and wrote only the flat keys: on a migrated config it showed no model, its Check and the restart notice never saw magpie in use, and a model picked in magpie went beside a map goose read first, so goose kept its own and deleted magpie's at its next write. Now a config with a providers map or active_provider is written as goose's set_active_provider writes it (active_provider, providers.<id>: enabled, model, configured), other providers' entries and the user's other keys and comments kept, stale flat keys removed; an old config keeps the flat keys, which an older goose reads and a newer migrates. Taking Goose off magpie clears active_provider and the flat keys as goose's clear_active_provider does, and drops magpie's own entry with its custom provider. TestGooseModelInEitherLayout (the reporter's layout with another provider; switching to a native model, to magpie's and back off; a stale flat key beside the map; the old layout left old) fails on the old code ("model = \"\", want magpie/zhipu/glm-5.3") and passes; go test ./internal/agent/ ./internal/edit/, -race -count=5 on the Goose/pick/effort tests, go vet (darwin, windows) and darwin/linux(nogui)/windows builds pass. Not tried with a real Goose Desktop.
2026-10-06 14:53:46 +08:00

281 lines
9.8 KiB
Go

package agent
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"gopkg.in/yaml.v3"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
)
// Goose (Block's goose, its CLI and desktop app alike) reaches magpie as a
// custom provider: a JSON file of its own in custom_providers beside
// config.yaml, named by the provider's id, which goose loads at start-up
// (crates/goose/src/config/declarative_providers.rs). Its schema is
// DeclarativeProviderConfig (crates/goose-providers/src/declarative.rs), and
// each of its models a ModelInfo (crates/goose-provider-types/src/base.rs):
//
// - context_limit is the window goose's ContextLimitResolver takes for the
// model before anything else but GOOSE_CONTEXT_LIMIT
// (crates/goose-provider-types/src/context_limit.rs). Without it goose
// guesses from its own registry by the model's name, or 128K.
// - reasoning is what the desktop app's model picker asks before it shows
// its Thinking Level (fetchModelReasoning, ui/desktop/src/components/
// settings/models/modelInterface.ts); a model declared without it is
// taken for one that doesn't think.
//
// dynamic_models false keeps goose to this list rather than the gateway's
// /v1/models (every model, none of their windows), and
// skip_canonical_filtering keeps magpie's order and models goose's registry
// doesn't know.
// gooseProviderID is magpie's file in Goose's custom_providers and the
// provider GOOSE_PROVIDER names.
const gooseProviderID = magpieID
// gooseThinks matches the model names goose sends its thinking level for on
// an OpenAI-compatible provider: OpenAI's reasoning models, as goose's
// is_openai_responses_model tells them (crates/goose-provider-types/src/
// formats/openai.rs). For any other model goose keeps the level to itself —
// thinking_effort is one of its own request params, never sent — so a
// level offered for it would change nothing; it is declared without
// reasoning rather than shown a picker that does nothing.
var gooseThinks = regexp.MustCompile(`(?i)(?:^|[-/])(?:o\d+(?:$|-)|gpt-(?:5|6)(?:$|[-.]))`)
// gooseKeys are the secrets goose reads for the native providers whose
// models magpie can list, and which goose takes from the environment before
// its keyring (Config::get_secret, crates/goose/src/config/base.rs; the keys
// from crates/goose/src/providers/<name>_def.rs).
var gooseKeys = map[string]string{
"anthropic": "ANTHROPIC_API_KEY",
"openai": "OPENAI_API_KEY",
"google": "GOOGLE_API_KEY",
"openrouter": "OPENROUTER_API_KEY",
}
// gooseConfigured is the providers other than magpie that the Goose whose
// config.yaml is at cfg has set up: the ones its providers map marks
// configured or that it uses now (set_active_provider, crates/goose/src/
// config/providers.rs), the ones a legacy <name>_configured marker names
// (migrate_provider_config, config/migrations.rs), and a provider whose key
// is in the environment. A key in goose's keyring can't be seen from here;
// such a provider still counts once goose has used it, as each provider it
// is set up with gets an entry.
func gooseConfigured(cfg string) []string {
set := map[string]bool{}
if b, err := os.ReadFile(cfg); err == nil {
var c map[string]any
if yaml.Unmarshal(b, &c) == nil {
if ps, ok := c["providers"].(map[string]any); ok {
for name, v := range ps {
if e, ok := v.(map[string]any); ok && e["configured"] == true {
set[name] = true
}
}
}
for k, v := range c {
if name, ok := strings.CutSuffix(k, "_configured"); ok && v == true {
set[name] = true
}
}
for _, k := range []string{"active_provider", "GOOSE_PROVIDER"} {
if p, ok := c[k].(string); ok && p != "" {
set[p] = true
}
}
}
}
for p, k := range gooseKeys {
if os.Getenv(k) != "" {
set[p] = true
}
}
delete(set, gooseProviderID)
out := make([]string, 0, len(set))
for p := range set {
out = append(out, p)
}
sort.Strings(out)
return out
}
// gooseProviderPath is magpie's custom provider file for the Goose whose
// config.yaml is at cfg.
func gooseProviderPath(cfg string) string {
return filepath.Join(filepath.Dir(cfg), "custom_providers", gooseProviderID+".json")
}
// gooseProviderJSON is magpie as a Goose custom provider, its models the
// catalog as Goose is shown it.
func gooseProviderJSON() map[string]any {
models := []map[string]any{}
for _, m := range magpieModels("goose") {
e := map[string]any{"name": m.ID, "reasoning": gooseThinks.MatchString(m.ID)}
if m.Context > 0 {
e["context_limit"] = m.Context
}
models = append(models, e)
}
p := map[string]any{
"name": gooseProviderID,
"engine": "openai",
"display_name": "magpie",
"description": "Every model magpie has, through its local gateway",
"api_key_env": "",
"base_url": gatewayV1(),
"headers": map[string]any{"Authorization": "Bearer " + gateway.Token},
"requires_auth": false,
"skip_canonical_filtering": true,
"models": models,
}
// goose refuses a provider whose static list is empty
if len(models) > 0 {
p["dynamic_models"] = false
}
return p
}
// writeGooseProvider writes magpie's custom provider file for Goose.
func writeGooseProvider(path string) error {
b, err := json.MarshalIndent(gooseProviderJSON(), "", " ")
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
return edit.WriteAtomic(path, append(b, '\n'))
}
// syncGooseProvider rewrites magpie's custom provider file for Goose with
// the catalog as it is now, where magpie wrote one and it differs.
func syncGooseProvider(path string) error {
raw, err := edit.Read(path)
if err != nil || raw == nil {
return nil
}
if sameJSON(string(raw), gooseProviderJSON()) {
return nil
}
return writeGooseProvider(path)
}
// removeGooseProvider takes magpie's custom provider file away once Goose's
// model is no longer one of magpie's.
func removeGooseProvider(path string) error {
if err := edit.Remove(path); err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
return nil
}
// Goose keeps the provider and model it uses in one of two layouts of its
// config.yaml (crates/goose/src/config/providers.rs, migrations.rs). The
// old one is two flat keys, GOOSE_PROVIDER and GOOSE_MODEL. The new one,
// which goose migrates a config to on its next write, is active_provider
// and an entry in the providers map holding that provider's model:
//
// active_provider: magpie
// providers:
// magpie:
// enabled: true
// model: zhipu/glm-5.3
// configured: true
//
// goose reads active_provider before GOOSE_PROVIDER and the entry's model
// before GOOSE_MODEL (get_active_provider, get_active_model), and once a
// providers map is there its migration deletes the flat keys on the next
// write (cleanup_legacy_provider_keys). So a model set with the flat keys
// alone on a migrated config is never used, and lost.
// gooseNewLayout reports whether the Goose config at cfg is in the new
// layout: it has a providers map or an active_provider.
func gooseNewLayout(cfg string) bool {
b, err := edit.Read(cfg)
if err != nil || b == nil {
return false
}
var c map[string]any
if yaml.Unmarshal(b, &c) != nil {
return false
}
_, providers := c["providers"]
_, active := c["active_provider"]
return providers || active
}
// gooseActive is the provider and model the Goose config at cfg uses, read
// as goose reads them (the environment aside).
func gooseActive(cfg string) (provider, model string) {
provider, _ = edit.GetYAMLTop(cfg, "active_provider")
if provider == "" {
provider, _ = edit.GetYAMLTop(cfg, "GOOSE_PROVIDER")
}
if provider != "" && !strings.Contains(provider, ".") {
model, _ = edit.GetYAML(cfg, "providers."+provider+".model")
}
if model == "" {
model, _ = edit.GetYAMLTop(cfg, "GOOSE_MODEL")
}
return provider, model
}
// gooseModel is the Goose config at cfg's model as provider/model.
func gooseModel(cfg string) string {
p, m := gooseActive(cfg)
switch {
case m == "":
return ""
case p == "":
return m
}
return p + "/" + m
}
// setGooseModel makes v, a provider/model, the model the Goose config at
// cfg uses, in the layout the config is in: on a new one as goose's own
// set_active_provider writes it, other providers' entries kept; on an old
// one the flat keys, which an old goose reads and a new one migrates.
func setGooseModel(cfg, v string) error {
p, m, ok := strings.Cut(v, "/")
if !ok || p == "" || m == "" {
return fmt.Errorf("expected provider/model, got %q", v)
}
if !gooseNewLayout(cfg) {
return edit.SetYAMLTop(cfg, edit.KV{Path: "GOOSE_PROVIDER", Value: p}, edit.KV{Path: "GOOSE_MODEL", Value: m})
}
if strings.Contains(p, ".") {
return fmt.Errorf("goose provider %q: a dot can't be written into its providers map", p)
}
if err := edit.SetYAML(cfg,
edit.KV{Path: "active_provider", Value: p},
edit.KV{Path: "providers." + p + ".enabled", Value: true},
edit.KV{Path: "providers." + p + ".model", Value: m},
edit.KV{Path: "providers." + p + ".configured", Value: true},
); err != nil {
return err
}
// flat keys left beside the map would be read by no goose that reads
// the map, and deleted by the next one that writes
return edit.DelYAMLTop(cfg, "GOOSE_PROVIDER", "GOOSE_MODEL")
}
// clearGooseModel takes the Goose config at cfg off any model, as goose's
// clear_active_provider does, and drops magpie's own providers entry, as
// magpie's custom provider goes with it; other providers' entries are kept.
func clearGooseModel(cfg string) error {
if err := edit.DelYAMLTop(cfg, "active_provider", "GOOSE_PROVIDER", "GOOSE_MODEL"); err != nil {
return err
}
return edit.DelYAML(cfg, "providers."+gooseProviderID)
}