Files
magpie/internal/agent/goose.go
T
yetone 2eb9266395 goose: runs on magpie as a custom provider that hands Goose each model's window and says which models Goose can send a thinking level (wani on Discord: goose gui不支持设置推理等级,而且上下文也不对)
magpie never wired Goose to its gateway: its model field offered only Goose's own providers, so a user had to add magpie by hand as a custom provider. Goose then treats every declared model as one that doesn't think (ModelInfo.reasoning defaults to false). With no context_limit, it guesses each window from its own registry by the model's name, or falls back to 128K. Goose's model field now offers magpie's models. Picking one writes custom_providers/magpie.json next to Goose's config.yaml (~/.config/goose, or %APPDATA%\Block\goose\config on Windows), then sets GOOSE_PROVIDER=magpie and GOOSE_MODEL. The file holds only keys Goose reads, from DeclarativeProviderConfig (crates/goose-providers/src/declarative.rs) and ModelInfo (crates/goose-provider-types/src/base.rs):
- engine openai, base_url set to the gateway's /v1, Authorization: Bearer magpie as a header, api_key_env "" and requires_auth false;
- dynamic_models false, so Goose uses this list rather than /v1/models, which has no windows;
- skip_canonical_filtering, so models Goose's registry doesn't know stay in the list;
- for each model, its context_limit (ContextLimitResolver takes it before anything but GOOSE_CONTEXT_LIMIT) and reasoning.

reasoning is true only for the models Goose actually sends a level to on an OpenAI-compatible provider. Those are OpenAI's reasoning models, as is_openai_responses_model in crates/goose-provider-types/src/formats/openai.rs tells them (gpt-5/6, o-series). For every other model (Claude, GLM, DeepSeek, Gemini…) Goose keeps thinking_effort to itself and never sends it, so a Thinking Level shown for them would do nothing. Sync rewrites the file as the catalog changes. Check reports a base_url or header someone else rewrote. Picking no model removes the file. The Notice asks for a restart, because Goose loads custom_providers at start-up. GOOSE_CONTEXT_LIMIT is left alone: it is one global override, and it would beat every per-model window.

What Goose itself can't do:
- On an OpenAI-compatible provider, Goose sends no effort for a model that isn't OpenAI's.
- The in-session thinking menu follows Goose's name heuristics and its registry, not the provider's reasoning.
- Before a session starts, the desktop app's context ring prefers Goose's registry to the declared window.

TestGooseRunsOnMagpie covers the options, the file's keys, the windows, reasoning for gpt-5.5 but not glm-4.6, Check, Sync and removal; the old code fails it. go vet, the builds (darwin, GOOS=linux -tags nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass, except TestProvidersAnswerWhileListsComeIn and TestSavedButSignedOutClaudeServed: these timing tests pass when run alone. Not tried against a real Goose.
2026-10-03 19:18:46 +08:00

118 lines
4.3 KiB
Go

package agent
import (
"encoding/json"
"errors"
"io/fs"
"os"
"path/filepath"
"regexp"
"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)(?:$|[-.]))`)
// 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
}