Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

model: magpie model spec sets a model's window, output, vision and levels at once

yetone4a3e07f1cf7b
diff --git a/docs/reference.md b/docs/reference.md
index 5a8bd2cb4..60cd9894e 100644
--- a/docs/reference.md
+++ b/docs/reference.md
@@ -1234,6 +1234,39 @@ at start-up. A session already running therefore keeps the window it began with,
while the gateway's own `/models` and every request from then on are right at
once.
+#### All of a model at once: `magpie model spec`
+
+`magpie model spec` sets a model's window, reply limit, vision and reasoning
+levels in one command, and says where each value magpie goes by came from:
+
+```sh
+magpie model spec kenari/glm-5:free # what it has, and whether each is yours
+magpie model spec kenari/glm-5:free --context 128k --output 16k --vision on --reasoning low-high
+magpie model spec kenari/glm-5:free --reasoning low,medium,high # a list instead of a range
+magpie model spec kenari/glm-5:free --reasoning off # send it no reasoning field
+magpie model spec kenari/glm-5:free --vision auto # take one value of yours off
+magpie model spec "kenari/*" --context 200000 --output 32k # every model of the provider without its own
+magpie model spec kenari/glm-5:free --reset # take every value of yours off
+magpie model specs # the models you gave a spec
+```
+
+It writes the same stores `magpie model context`, `output`, `images` and
+`efforts` do — the window in providers.json, the rest in settings.json
+(`modelOutputs`, `modelImages`, `modelEfforts`, `modelReasonless`) — so the
+order above holds for each part, and a catalog refresh keeps them. A model
+nothing knew the levels of can be given them (`--reasoning low-high`); one
+that has its own can only be narrowed to some of them. A bad number, an
+unknown flag, a level that isn't one, or a range that runs backwards is
+refused, and nothing of the command is written.
+
+What you set wins everywhere magpie reads a model: the app's badges and
+level chips, the gateway's `/v1/models` (`context_window`,
+`max_output_tokens`, `reasoning_levels`), the `max_tokens` a request is sent
+with, the level a request is sent at (an ask above the levels you kept goes
+at the nearest one you kept), whether images reach it or are described
+first, which model magpie picks to describe images, and the model lists
+magpie writes into the agents.
+
### The name a vendor knows a model by
A relay often serves a model under an id of its own — a prefix it namespaces
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index 0b2aa29b8..5ff13c5ab 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -180,6 +180,7 @@ recorded as a prediction failure (`noPrediction`).
- A request to an Anthropic endpoint that turns thinking off is sent as the model takes it (`thinkingOffAsTaken`, in `forwardOnce`, so relayed requests, count_tokens, magpie's own thinking-off and retries all get it; the model is read as the vendor names it, from the body, or from the path for Bedrock and Vertex). Claude Sonnet 5.5 refuses `thinking: {type: "disabled"}` and is sent `{type: "between_tools"}` alone, at effort `high` at most. Opus 5.5, Fable 5 and Mythos 5 refuse it at every effort and are sent no thinking, at effort `low` unless one was asked, with `classifierRoom` added to a reply limit below it, as Claude Code does. Opus 5 and Haiku 5.5 take it only up to `high`, so `xhigh` and `max` are asked as `high`. `between_tools` sent to any other Claude goes as `disabled`, or as none where that is refused too. Other vendors' models, older Claudes and later lines than these go as they came (#1454). A Sonnet 5.5 by a name these rules can't read, answering 400 that asks for `between_tools` (`asksBetweenTools`), is asked again with it, once; a refusal of `disabled` at any effort (`thinkingOffRefused`) is asked again with thinking left to the model, as `alwaysThinks` is. So is a relay that reads `disabled` as reasoning effort `none` and turns that level away naming the ones it takes (Sub2API's gpt-6.1-sol, "does not support reasoning effort \"none\"; use low, medium, high, xhigh or max", #1465); `refusesThinkingOff` is the one list of these refusals for both routes. A relayed request refused so marks the model (`markUnfit(offRefused)`), and its later requests go with thinking left to the model at once until magpie restarts, as auto mode's classifier asks on every action.
- A Chat or Responses client's reasoning turned off (`none`, `ThinkOff`), translated to an Anthropic endpoint (`buildAnthropic`), goes as `thinking: {type: "disabled"}`; it went as thinking at a 4096-token budget before (#1636). A model that refuses it (`refusesThinkingOff`: `alwaysThinks`, `mandatoryReasoning`, `thinkingOffRefused`, or `none` turned away naming the levels taken) is asked again at its least (`thinkingBackOn`), and `markUnfit(offRefused)` sends it so at once until magpie restarts. A translated request's level can differ from the one asked: `none` and `minimal` go as `low` where the model's levels don't say it stops (`effortOf`). The route keeps the level asked; the try, the usage log and Routing's "went" say the one built (`noteWireEffort`, `wireEffort` in [`wire_effort.go`](../../internal/gateway/wire_effort.go)). Gemini's builders say theirs their own way and are logged as asked.
- A model the user ticked no levels of (`ModelReasonless` in [settings](../../internal/settings/settings.go), `SetModelReasonless`; the provider editor's Names & levels, or `magpie model efforts <provider/model> off`) is sent no reasoning field on any route, whatever the agent asks: the candidate loop strips the body with `withoutAnyReasoning` ([`quiet.go`](../../internal/gateway/quiet.go): Chat's `reasoning_effort`, `reasoning`, `thinking`, `enable_thinking` and `include`'s `reasoning.encrypted_content`, Responses' `reasoning`, Anthropic's `thinking` of any type and `output_config.effort`, Gemini's `thinkingConfig`), the Anthropic relay doesn't add `thinking:{type:disabled}` for it, and `forwardTranslated` clears `Effort`, `ThinkOff` and `Thinking` for its other callers. It offers no levels (`Efforts`, `entryFor`), is `Quiet`, and as a routing-group member doesn't take the group's levels away. The learned refusals above cover only reasoning *off* and Gemini levels; a vendor that answers a plain `reasoning_effort` with 422 (yuexian7 on Discord) is not learned, so this is the user's switch. `TestReasonlessModelSentNoReasoning`, `TestReasonlessAnthropicRelay`, `TestGroupReasonlessMemberKeepsLevels`, `model-reasonless.test.cjs`.
+- The levels the user kept of a model (`ModelEfforts`, `SetModelEfforts`; `magpie model efforts`, `magpie model spec --reasoning`, Dani Ariyanto on Discord) are the levels a request is fitted to, not only the ones offered: `provider.Efforts` returns the kept ones, so `fitEffort` sends an ask above them at the highest kept and one below at the lowest. Before, #92 narrowed only the lists handed out and an agent asking xhigh still got xhigh. A model the lists know no levels of can be given them; it then counts as reasoning (`entryFor`). `AutoVision` honours the user's `ModelImages` over the model's own (`seesAt`). `TestModelSpecReachesTheGateway`, `TestKeptLevelsBoundTheAsk`, `TestAutoVisionTakesTheUsersVision`.
- A request to an Anthropic endpoint with `temperature`, `top_p` or `top_k` reaches a model that refuses them without them (`samplingAsTaken`, beside `thinkingOffAsTaken` in `forwardOnce`): Opus 4.7, 4.8, 5 and 5.5, Fable 5, Mythos 5, Sonnet 5 and 5.5, and Haiku 5.5, whose 400 is "`temperature` is deprecated for this model" (Claude Code's auto mode classifier and magpie's own classifiers ask at temperature 0). Older Claudes, Haiku 4.5 among them, other vendors' models and later lines go as they came (#1454). A Claude by a name these rules can't read, answering that a sampling parameter is deprecated (`samplingDeprecated`), is asked again without them, once — a Chat or Responses client's request translated to Messages (`forwardTranslated`) as much as a relayed one.
- Claude Code's auto mode asks the API to review each action ([`automode.go`](../../internal/gateway/automode.go), Anthropic's [auto-mode-classifier-billing](https://code.claude.com/docs/en/auto-mode-classifier-billing)): the `dangerous-tool-use-…` beta, a `safeguards` field, and `safeguard_results` in the reply (a stream's in its `message_delta`). Only Anthropic runs that classifier, so they reach only a route it answers on (`AutoModeReviewed`): the Claude subscription (the bridge hands them to Claude Code's own binary), a Claude model under any vendor's spelling on a provider with a key (Anthropic's API, a relay, Bedrock, Vertex), and anything on `api.anthropic.com`. Another subscription's or a plugin's Claude model (Copilot, Kiro, Cursor…) is that vendor's service and isn't reviewed by its name: Copilot turns `safeguards` away (#1756), the others translate the request, so neither the beta nor `safeguards` is sent there (`TestAutoModeReviewedRoutes`, `TestAutoModeReviewLeftOffOtherSubscriptions`). There the beta, `safeguards` and the results pass as sent, a whole reply's results moved into a stream's `message_delta` (`anthropicWholeEvents`), tool-use ids unchanged. To any other model they are taken off (`serverReview`; a translation never carries them), and magpie never answers a result on Anthropic's behalf. Claude Code then classifies locally and names the gateway in a notice; the wiring turns the review off in its settings while no model it can reach through magpie is reviewed (`claudeReviewEnv`, [agent wiring](agent-wiring.md)), which is the page's remedy for a gateway that can't run the checks (1EFLOPS on X: Codex's model behind magpie). Reviewed there means `AutoModeReviewable`: a reviewed model on a route that reaches the Messages API, since a Claude model asked on a relay's Chat or Responses URL is translated and never carries the review (nexnull on Discord, `TestAutoModeReviewableAsksOnMessages`).
- Claude Code's billing line (`x-anthropic-billing-header: cc_version=…; cc_entrypoint=…; cch=…;`, its own first system block, `cch` changing every turn) goes on only to a Claude model asked on Anthropic's Messages API (`keepsBillingLine` in [`billing_line.go`](../../internal/gateway/billing_line.go): Anthropic's, a relay's, Bedrock's), as Claude Code sent it. Anywhere else it is left out, the rest of the system as sent (`withoutBillingLine`): a vendor's model relayed on Messages (`passthrough`), every translation to Chat, Responses or Gemini (`forwardTranslated`, Claude models too). Kept, a vendor read it as the prompt's first words and its prefix cache missed from byte 0 every turn (#1600, justrun1983: 4% read from DeepSeek's cache behind Command Code's plan). The Claude subscription's run sends its own (`withoutBillingHeader`), another magpie decides for the provider it picks, and Code Assist (Antigravity, Gemini CLI) is sent it as before, which `antigravityTurnsAway` reads (#666). Once Antigravity has turned such a request away, for `agForeseeing` (6 hours) after, a candidate loop with someone left after an Antigravity account skips it for a request that carries the line, without asking it (`antigravityForeseen`, #2156: each Claude Desktop turn waited about a second on the same refusal before the next member). The skip goes in `Fallback` with the hint, nothing rests, and the agent is told the refusal if nobody after it answers but with a quota. The last one left is still asked, and an old refusal foresees nothing, so a block Google lifts is noticed (`TestAntigravityTurnedAwayIsForeseen`).
diff --git a/internal/cli/help_cli.go b/internal/cli/help_cli.go
index 4222383d4..20a88cc33 100644
--- a/internal/cli/help_cli.go
+++ b/internal/cli/help_cli.go
@@ -37,6 +37,9 @@ func helpFor(args []string) string {
case "-h", "--help", "help":
return usage
case "model":
+ if len(args) > 1 && (args[1] == "spec" || args[1] == "specs") {
+ return specUsage
+ }
return modelUsage
case "group", "groups":
if len(args) > 1 && (args[1] == "rule" || args[1] == "rules") {
diff --git a/internal/cli/model_cli.go b/internal/cli/model_cli.go
index aaee5f08a..68522f4ff 100644
--- a/internal/cli/model_cli.go
+++ b/internal/cli/model_cli.go
@@ -44,6 +44,12 @@ const modelUsage = `usage:
magpie model output <provider/model> the most a reply of it may hold, and what you said
magpie model output <provider/model> <n> say the most, as 128000 or 128k; '<provider>/*' is every model
magpie model output <provider/model> --reset take your limit off this model
+ magpie model spec <provider/model> its context window, max output, vision and reasoning levels
+ magpie model spec <provider/model> [--context <n>] [--output <n>] [--vision on|off] [--reasoning <levels>]
+ say any of them at once, e.g. --reasoning low-high; auto as a
+ value goes back to the vendor's list (magpie model spec --help)
+ magpie model spec <provider/model> --reset take every value you set off this model
+ magpie model specs the models you gave a spec
magpie model wire <provider/model> the name the vendor is asked for, and the one you gave
magpie model wire <provider/model> <name> ask for the model by this name, for a relay that serves it
magpie model wire '<provider>/*' <name> ask for every model of that provider by this name; a * in
@@ -127,6 +133,10 @@ func modelCmd(args []string) error {
return modelOutput(args[1:])
case "wire", "upstream":
return modelWire(args[1:])
+ case "spec", "specs-of":
+ return modelSpec(args[1:])
+ case "specs":
+ return modelSpecs()
case "alias":
return modelUsageAlias(args[1:])
case "aliases":
@@ -223,9 +233,12 @@ func modelEfforts(args []string) error {
return err
}
id := p.ID + "/" + model
- all := p.Efforts(model)
+ all := p.Known(model) // the model's own, the kept ones bold below
rest := args[1:]
- given := len(p.Known(model)) == 0 // levels aren't known: those it has are the user's
+ given := len(all) == 0 // levels aren't known: those it has are the user's
+ if given {
+ all = p.Efforts(model)
+ }
if len(rest) == 1 && slices.Contains([]string{"off", "--off"}, rest[0]) {
// no reasoning field at all, for a vendor that turns one away
// though its list says the model reasons (yuexian7 on Discord)
diff --git a/internal/cli/model_spec_cli.go b/internal/cli/model_spec_cli.go
new file mode 100644
index 000000000..826460cf5
--- /dev/null
+++ b/internal/cli/model_spec_cli.go
@@ -0,0 +1,201 @@
+package cli
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// magpie model spec: a model's context window, reply limit, vision and
+// reasoning levels at once, over what the vendor's list and models.dev say
+// (Dani Ariyanto on Discord: a custom provider's models had none known).
+
+const specUsage = `usage:
+ magpie model spec <provider/model> its context window, max output, vision and reasoning levels,
+ and whether each is yours or the vendor's list's / models.dev's
+ magpie model spec <provider/model> [--context <n>] [--output <n>] [--vision on|off] [--reasoning <levels>]
+ say any of them: --context 200000 or 1m, --output 128k,
+ --reasoning low-high (a range), low,medium,high (a list) or off
+ (send it no reasoning field); auto as any value takes yours off
+ that one and goes back to the vendor's list and models.dev
+ magpie model spec '<provider>/*' --context <n> --output <n>
+ a window and max output for every model of that provider that
+ has none of its own; vision and reasoning are set per model
+ magpie model spec <provider/model> --reset take every value you set off this model
+ magpie model specs the models you gave a spec
+
+ What you set wins over the vendor's list and models.dev everywhere magpie reads them: the app's
+ badges, the gateway's model list and routing, max_tokens, image handling, and the model lists
+ magpie writes into the agents. A model you set nothing on is as it was. A catalog refresh keeps them.
+
+ e.g. magpie model spec kenari/glm-5:free --context 128k --output 16k --vision off --reasoning low-high`
+
+func modelSpec(args []string) error {
+ if len(args) == 0 {
+ return fmt.Errorf("%s", specUsage)
+ }
+ if isHelp(args[0]) {
+ fmt.Println(specUsage)
+ return nil
+ }
+ ref, rest := args[0], args[1:]
+ if strings.HasPrefix(ref, "-") {
+ return fmt.Errorf("name the model first, as provider/model, not %q\n\n%s", ref, specUsage)
+ }
+ if isReset(rest) {
+ gone, err := provider.ResetModelSpec(ref)
+ if err != nil {
+ return err
+ }
+ if len(gone) == 0 {
+ return fmt.Errorf("%s has no spec of yours to reset — magpie model spec %s says what it has now", ref, typedRef(ref))
+ }
+ fmt.Println(green.Render("✓"), ref, muted.Render("has no "+strings.Join(gone, ", ")+" of yours · it goes by the vendor's list and models.dev"))
+ return nil
+ }
+ var c provider.SpecChange
+ for i := 0; i < len(rest); i++ {
+ flag, val, inline := strings.Cut(rest[i], "=")
+ if isHelp(flag) {
+ fmt.Println(specUsage)
+ return nil
+ }
+ if !strings.HasPrefix(flag, "--") {
+ return fmt.Errorf("%q is not one of --context, --output, --vision, --reasoning or --reset\n\n%s", rest[i], specUsage)
+ }
+ if !inline {
+ if i+1 >= len(rest) || strings.HasPrefix(rest[i+1], "-") {
+ if flag == "--reset" || flag == "-r" {
+ return fmt.Errorf("--reset takes every value off and goes alone; to take one part off, give it auto (--vision auto)")
+ }
+ return fmt.Errorf("%s needs a value\n\n%s", flag, specUsage)
+ }
+ i++
+ val = rest[i]
+ }
+ switch flag {
+ case "--context", "--ctx", "--window":
+ n, err := specTokens("a context window", val)
+ if err != nil {
+ return err
+ }
+ c.Context = &n
+ case "--output", "--max-output":
+ n, err := specTokens("a max output", val)
+ if err != nil {
+ return err
+ }
+ c.Output = &n
+ case "--vision", "--images", "--multimodal":
+ if _, err := provider.ParseVision(val); err != nil {
+ return err
+ }
+ c.Vision = &val
+ case "--reasoning", "--efforts", "--effort", "--levels":
+ c.Reasoning = &val
+ default:
+ return fmt.Errorf("unknown flag %s: it takes --context, --output, --vision, --reasoning or --reset\n\n%s", flag, specUsage)
+ }
+ }
+ if c.Empty() {
+ return showSpec(ref)
+ }
+ if err := provider.SetModelSpec(ref, c); err != nil {
+ return err
+ }
+ fmt.Println(green.Render("✓"), muted.Render("saved"))
+ return showSpec(ref)
+}
+
+func isHelp(a string) bool { return a == "--help" || a == "-h" || a == "help" }
+
+// specTokens is a typed number of tokens for a spec: 0 for auto. A 0 typed
+// as a number is refused, as no window at all is not what anyone means.
+func specTokens(what, v string) (int, error) {
+ if strings.EqualFold(strings.TrimSpace(v), provider.Auto) {
+ return 0, nil
+ }
+ n, err := parseTokens(v)
+ if err != nil {
+ return 0, fmt.Errorf("%s is a number of tokens, like 200000, 128k or 1m, or auto, not %q", what, v)
+ }
+ if n == 0 {
+ return 0, fmt.Errorf("%s of 0 tokens is none at all — auto takes yours off and goes back to the vendor's list", what)
+ }
+ return n, nil
+}
+
+// showSpec prints a model's spec as magpie goes by it, and where each part
+// came from.
+func showSpec(ref string) error {
+ p, model, err := modelRef(ref)
+ if err != nil {
+ return err
+ }
+ id := p.ID + "/" + model
+ if model == "*" {
+ fmt.Println(bold.Render(id), muted.Render("· every model of "+p.ID+" that has none of its own"))
+ out := provider.OutputsOf(p.ID)["*"]
+ fmt.Println(" " + pad("context", 11) + bold.Render(tokenCount(p.Contexts["*"])))
+ fmt.Println(" " + pad("max output", 11) + bold.Render(tokenCount(out)))
+ return nil
+ }
+ sp, ok := provider.SpecOf(id)
+ if !ok {
+ fmt.Println(muted.Render(id), faint.Render("· is not a model this provider serves"))
+ return nil
+ }
+ from := func(part provider.SpecPart, flag string) string {
+ switch part.From {
+ case "model":
+ return "yours · --" + flag + " auto goes back to the vendor's list"
+ case "provider":
+ return "yours for every model of " + p.ID
+ }
+ if part.Value == "unknown" {
+ return "neither the vendor's list nor models.dev says · --" + flag + " sets it"
+ }
+ return "the vendor's list / models.dev"
+ }
+ fmt.Println(bold.Render(id))
+ for _, l := range []struct {
+ name, flag string
+ part provider.SpecPart
+ }{
+ {"context", "context", sp.Context},
+ {"max output", "output", sp.Output},
+ {"vision", "vision", sp.Vision},
+ {"reasoning", "reasoning", sp.Reasoning},
+ } {
+ fmt.Println(" "+pad(l.name, 11)+bold.Render(pad(l.part.Value, 18)), faint.Render(from(l.part, l.flag)))
+ }
+ return nil
+}
+
+// modelSpecs lists the models the user gave a spec.
+func modelSpecs() error {
+ rows := provider.ModelSpecs()
+ if len(rows) == 0 {
+ fmt.Println(muted.Render("no model has a spec of yours yet · magpie model spec <provider/model> --context <n> …"))
+ return nil
+ }
+ w := 0
+ for _, r := range rows {
+ w = max(w, len(r.Key))
+ }
+ for _, r := range rows {
+ var parts []string
+ for _, p := range [][2]string{{"context", r.Context}, {"output", r.Output}, {"vision", r.Vision}, {"reasoning", r.Reasoning}} {
+ if p[1] != "" {
+ parts = append(parts, p[0]+" "+p[1])
+ }
+ }
+ line := " " + pad(r.Key, w) + " " + strings.Join(parts, " · ")
+ if r.Gone {
+ line += faint.Render(" (its provider is gone)")
+ }
+ fmt.Println(line)
+ }
+ return nil
+}
diff --git a/internal/cli/model_spec_cli_test.go b/internal/cli/model_spec_cli_test.go
new file mode 100644
index 000000000..6d527cca3
--- /dev/null
+++ b/internal/cli/model_spec_cli_test.go
@@ -0,0 +1,97 @@
+package cli
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/settings"
+)
+
+// magpie model spec (Dani Ariyanto on Discord): a model nothing knows is
+// shown unknown on each part, takes a whole spec in one command, shows it
+// as the user's, is listed by magpie model specs, and --reset puts it back.
+// A bad number, level, vision answer or flag is refused with the usage
+// and writes nothing; --help anywhere prints the usage and writes nothing.
+func TestModelSpecCmd(t *testing.T) {
+ groupsHome(t)
+ files := func() string {
+ var out string
+ for _, f := range []string{"settings.json", "providers.json"} {
+ b, _ := os.ReadFile(filepath.Join(settings.Dir(), f))
+ out += string(b)
+ }
+ return out
+ }
+ out, err := runOut(t, "model", "spec", "a/only-a")
+ if err != nil || strings.Count(out, "neither the vendor's list nor models.dev says") != 4 {
+ t.Fatalf("unknown spec: %q %v", out, err)
+ }
+ if out, err = runOut(t, "model", "specs"); err != nil || !strings.Contains(out, "no model has a spec of yours yet") {
+ t.Fatalf("empty list: %q %v", out, err)
+ }
+ if out, err = runOut(t, "model", "spec", "a/only-a", "--context", "128k", "--output=16k", "--vision", "on", "--reasoning", "low-high"); err != nil {
+ t.Fatalf("set: %q %v", out, err)
+ }
+ for _, want := range []string{"saved", "128k", "16k", "on", "low, medium, high"} {
+ if !strings.Contains(out, want) && !strings.Contains(out, strings.ReplaceAll(want, ", ", ",")) {
+ t.Errorf("set shows no %q: %q", want, out)
+ }
+ }
+ if strings.Count(out, "yours · --") != 4 {
+ t.Errorf("each part not said to be the user's: %q", out)
+ }
+ if out, err = runOut(t, "model", "specs"); err != nil || !strings.Contains(out, "a/only-a") || !strings.Contains(out, "vision on") || !strings.Contains(out, "reasoning low,medium,high") {
+ t.Fatalf("list: %q %v", out, err)
+ }
+
+ before := files()
+ for _, bad := range [][]string{
+ {"model", "spec", "a/only-a", "--context", "abc"},
+ {"model", "spec", "a/only-a", "--context", "0"},
+ {"model", "spec", "a/only-a", "--output", "-5"},
+ {"model", "spec", "a/only-a", "--reasoning", "turbo"},
+ {"model", "spec", "a/only-a", "--reasoning", "high-low"},
+ {"model", "spec", "a/only-a", "--context", "1m", "--reasoning", "bogus"},
+ {"model", "spec", "a/only-a", "--vision", "maybe"},
+ {"model", "spec", "a/only-a", "--frobnicate", "1"},
+ {"model", "spec", "a/only-a", "--context"},
+ {"model", "spec", "a/only-a", "128k"},
+ {"model", "spec", "a/only-a", "--reset", "--context", "1m"},
+ {"model", "spec", "a/nope", "--context", "1m"},
+ {"model", "spec", "a/*", "--vision", "on"},
+ {"model", "spec", "*/only-a", "--context", "1m"},
+ {"model", "spec", "--context", "1m"},
+ } {
+ if out, err := runOut(t, bad...); err == nil {
+ t.Errorf("took %v: %q", bad, out)
+ }
+ if files() != before {
+ t.Fatalf("a refused %v wrote", bad)
+ }
+ }
+ for _, help := range [][]string{
+ {"model", "spec", "--help"},
+ {"model", "spec", "a/only-a", "--help"},
+ {"model", "spec", "a/only-a", "--context", "--help"},
+ {"model", "specs", "--help"},
+ } {
+ if out, err := runOut(t, help...); err != nil || !strings.Contains(out, "say any of them: --context 200000 or 1m") {
+ t.Errorf("%v: %q %v", help, out, err)
+ }
+ if files() != before {
+ t.Fatalf("%v wrote", help)
+ }
+ }
+
+ if out, err = runOut(t, "model", "spec", "a/only-a", "--reset"); err != nil || !strings.Contains(out, "context, output, vision, reasoning") {
+ t.Fatalf("reset: %q %v", out, err)
+ }
+ if out, err = runOut(t, "model", "specs"); err != nil || !strings.Contains(out, "no model has a spec of yours yet") {
+ t.Fatalf("list after reset: %q %v", out, err)
+ }
+ if _, err = runOut(t, "model", "spec", "a/only-a", "--reset"); err == nil || !strings.Contains(err.Error(), "no spec of yours to reset") {
+ t.Fatalf("second reset: %v", err)
+ }
+}
diff --git a/internal/gateway/modelspec_test.go b/internal/gateway/modelspec_test.go
new file mode 100644
index 000000000..272b3c5cf
--- /dev/null
+++ b/internal/gateway/modelspec_test.go
@@ -0,0 +1,195 @@
+package gateway
+
+import (
+ "encoding/json"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "slices"
+ "strings"
+ "sync"
+ "testing"
+
+ "github.com/yetone/magpie/internal/catalog"
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// specUp is a Chat vendor that keeps each body it was sent.
+type specUp struct {
+ mu sync.Mutex
+ sent []map[string]any
+}
+
+func (u *specUp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ b, _ := io.ReadAll(r.Body)
+ var v map[string]any
+ json.Unmarshal(b, &v)
+ u.mu.Lock()
+ u.sent = append(u.sent, v)
+ u.mu.Unlock()
+ w.Header().Set("Content-Type", "application/json")
+ io.WriteString(w, `{"id":"c1","object":"chat.completion","model":"glm-5:free","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1,"total_tokens":4}}`)
+}
+
+func (u *specUp) last(t *testing.T) map[string]any {
+ t.Helper()
+ u.mu.Lock()
+ defer u.mu.Unlock()
+ if len(u.sent) == 0 {
+ t.Fatal("the vendor was asked nothing")
+ }
+ return u.sent[len(u.sent)-1]
+}
+
+// Dani Ariyanto on Discord: a relay's models came with nothing known —
+// no window, no reply limit, no levels. What magpie model spec sets is
+// what the gateway lists and asks by: /v1/models has the window, the
+// reply limit and the levels, a request asking more than the reply limit
+// is lowered to it, and one asking a level the model wasn't given is
+// asked the nearest it was. Reset, the request goes as the agent asked.
+func TestModelSpecReachesTheGateway(t *testing.T) {
+ fresh(t)
+ up := &specUp{}
+ vendor := httptest.NewServer(up)
+ t.Cleanup(vendor.Close)
+ if err := provider.Save(provider.Provider{ID: "kenari", Name: "Kenari", Key: "k", Chat: vendor.URL + "/v1", Models: []string{"glm-5:free"}}); err != nil {
+ t.Fatal(err)
+ }
+ n := func(v int) *int { return &v }
+ s := func(v string) *string { return &v }
+ if err := provider.SetModelSpec("kenari/glm-5:free", provider.SpecChange{Context: n(131072), Output: n(16384), Vision: s("off"), Reasoning: s("low-high")}); err != nil {
+ t.Fatal(err)
+ }
+ gw := httptest.NewServer(New().Handler())
+ t.Cleanup(gw.Close)
+
+ res, err := http.Get(gw.URL + "/v1/models")
+ if err != nil {
+ t.Fatal(err)
+ }
+ var list struct {
+ Data []struct {
+ ID string `json:"id"`
+ Context int `json:"context_window"`
+ Output int `json:"max_output_tokens"`
+ Levels []string `json:"reasoning_levels"`
+ Reasons bool `json:"reasoning"`
+ } `json:"data"`
+ }
+ json.NewDecoder(res.Body).Decode(&list)
+ res.Body.Close()
+ i := slices.IndexFunc(list.Data, func(m struct {
+ ID string `json:"id"`
+ Context int `json:"context_window"`
+ Output int `json:"max_output_tokens"`
+ Levels []string `json:"reasoning_levels"`
+ Reasons bool `json:"reasoning"`
+ }) bool {
+ return m.ID == "kenari/glm-5:free"
+ })
+ if i < 0 {
+ t.Fatalf("kenari/glm-5:free not in /v1/models: %+v", list.Data)
+ }
+ if m := list.Data[i]; m.Context != 131072 || m.Output != 16384 || !slices.Equal(m.Levels, []string{"low", "medium", "high"}) || !m.Reasons {
+ t.Fatalf("/v1/models: %+v", m)
+ }
+
+ ask := func() map[string]any {
+ t.Helper()
+ res, err := http.Post(gw.URL+"/v1/chat/completions", "application/json",
+ strings.NewReader(`{"model":"kenari/glm-5:free","max_tokens":100000,"reasoning_effort":"xhigh","messages":[{"role":"user","content":"hi"}]}`))
+ if err != nil {
+ t.Fatal(err)
+ }
+ b, _ := io.ReadAll(res.Body)
+ res.Body.Close()
+ if res.StatusCode != 200 {
+ t.Fatalf("%d %s", res.StatusCode, b)
+ }
+ return up.last(t)
+ }
+ got := ask()
+ if got["max_tokens"] != float64(16384) || got["reasoning_effort"] != "high" {
+ t.Fatalf("with the spec the vendor was asked max_tokens %v, reasoning_effort %v", got["max_tokens"], got["reasoning_effort"])
+ }
+
+ if _, err := provider.ResetModelSpec("kenari/glm-5:free"); err != nil {
+ t.Fatal(err)
+ }
+ got = ask()
+ if got["max_tokens"] != float64(100000) || got["reasoning_effort"] != "xhigh" {
+ t.Fatalf("reset, the vendor was asked max_tokens %v, reasoning_effort %v", got["max_tokens"], got["reasoning_effort"])
+ }
+}
+
+// A model whose levels the vendor's list names, kept to some of them by
+// the user, is asked the nearest of those kept, not a level left out: the
+// user's range bounds the ask, as it bounds what agents are offered.
+func TestKeptLevelsBoundTheAsk(t *testing.T) {
+ fresh(t)
+ os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
+ data := `{"kenari":{"models":{"sol":{"id":"sol","name":"Sol","reasoning_options":[{"type":"effort","values":["low","medium","high","xhigh","max"]}]}}}}`
+ if err := os.WriteFile(catalog.CachePath(), []byte(data), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ catalog.Reset()
+ t.Cleanup(catalog.Reset)
+ up := &specUp{}
+ vendor := httptest.NewServer(up)
+ t.Cleanup(vendor.Close)
+ if err := provider.Save(provider.Provider{ID: "kenari", Name: "Kenari", Catalog: "kenari", Key: "k", Chat: vendor.URL + "/v1", Models: []string{"sol"}}); err != nil {
+ t.Fatal(err)
+ }
+ gw := httptest.NewServer(New().Handler())
+ t.Cleanup(gw.Close)
+ ask := func(effort string) any {
+ t.Helper()
+ res, err := http.Post(gw.URL+"/v1/chat/completions", "application/json",
+ strings.NewReader(`{"model":"kenari/sol","reasoning_effort":"`+effort+`","messages":[{"role":"user","content":"hi"}]}`))
+ if err != nil {
+ t.Fatal(err)
+ }
+ io.Copy(io.Discard, res.Body)
+ res.Body.Close()
+ return up.last(t)["reasoning_effort"]
+ }
+ if got := ask("xhigh"); got != "xhigh" {
+ t.Fatalf("with every level offered, xhigh went as %v", got)
+ }
+ r := "low-high"
+ if err := provider.SetModelSpec("kenari/sol", provider.SpecChange{Reasoning: &r}); err != nil {
+ t.Fatal(err)
+ }
+ if got := ask("xhigh"); got != "high" {
+ t.Fatalf("kept to low-high, xhigh went as %v", got)
+ }
+ if got := ask("low"); got != "low" {
+ t.Fatalf("kept to low-high, low went as %v", got)
+ }
+}
+
+// AutoVision, which picks the model that describes an image for one that
+// can't see, takes the user's word on which see (magpie model spec
+// --vision): one marked text-only is passed over, one marked seeing is
+// picked.
+func TestAutoVisionTakesTheUsersVision(t *testing.T) {
+ routerUp(t, "text", "anthropic/claude-haiku-5.5")
+ if m := AutoVision(); m != "router/anthropic/claude-haiku-5.5" {
+ t.Fatalf("AutoVision = %q", m)
+ }
+ off, on := "off", "on"
+ if err := provider.SetModelSpec("router/anthropic/claude-haiku-5.5", provider.SpecChange{Vision: &off}); err != nil {
+ t.Fatal(err)
+ }
+ if m := AutoVision(); m != "" {
+ t.Fatalf("AutoVision = %q, a model the user said is text only", m)
+ }
+ if err := provider.SetModelSpec("router/text", provider.SpecChange{Vision: &on}); err != nil {
+ t.Fatal(err)
+ }
+ if m := AutoVision(); m != "router/text" {
+ t.Fatalf("AutoVision = %q, not the model the user said sees", m)
+ }
+}
diff --git a/internal/gateway/vision.go b/internal/gateway/vision.go
index 7a2859cde..5c50ebd24 100644
--- a/internal/gateway/vision.go
+++ b/internal/gateway/vision.go
@@ -228,7 +228,7 @@ func autoVision() (string, bool) {
if bestReady && t > bestTier {
continue
}
- m, cost, ready := smallModelWhere(p, "sees", sees)
+ m, cost, ready := smallModelWhere(p, "sees", seesAt(p.ID))
if m == "" {
continue
}
@@ -279,6 +279,20 @@ func sees(m catalog.Model) bool {
return m.Images && (m.ImageInput == nil || *m.ImageInput)
}
+// seesAt is sees for provider pid's models, the user's answer first
+// (provider.SetModelImage, magpie model spec --vision): a model they
+// marked text-only is never picked to describe images, and one they
+// marked as seeing is. smallKept is stamped by settings.json too, so a
+// change is read on the next pick.
+func seesAt(pid string) func(catalog.Model) bool {
+ return func(m catalog.Model) bool {
+ if v, ok := provider.ImageOverride(pid, m.ID); ok {
+ return v
+ }
+ return sees(m)
+ }
+}
+
// sight is a description, once it is done.
type sight struct {
done chan struct{}
diff --git a/internal/gui/assets/plugins/gateway/client.js b/internal/gui/assets/plugins/gateway/client.js
index 748fb9a1a..532dafbb2 100644
--- a/internal/gui/assets/plugins/gateway/client.js
+++ b/internal/gui/assets/plugins/gateway/client.js
@@ -353,8 +353,9 @@ function gatewayModels() {
// all say works but a request to it turns away (the same off gate the
// backend's providerEntries and the fallback picker already honour).
// the levels agents are offered: those kept, else all it has; one whose
- // levels aren't known (given) offers only those it was given
- const offered = (m) => m.kept?.length || m.given ? m.kept || [] : m.efforts || [];
+ // levels aren't known (given) offers only those it was given; one sent
+ // no reasoning field (reasonless) offers none
+ const offered = (m) => m.reasonless ? [] : m.kept?.length || m.given ? m.kept || [] : m.efforts || [];
for (const p of providers.providers) { if (p.off) continue; for (const m of p.models) if (m.on) out.push({ id: `${p.id}/${m.id}`, name: m.name, provider: p, context: m.context, output: m.output, efforts: offered(m), images: !!m.images }); }
return out;
}
diff --git a/internal/gui/modelspec_test.go b/internal/gui/modelspec_test.go
new file mode 100644
index 000000000..fc0d2a3ff
--- /dev/null
+++ b/internal/gui/modelspec_test.go
@@ -0,0 +1,57 @@
+package gui
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "slices"
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// What magpie model spec sets (Dani Ariyanto on Discord) is what the
+// providers' state behind the app's model badges says: the window, the
+// reply limit, images and the levels given a model nothing knew; a model
+// sent no reasoning field is said to be so, with no level kept.
+func TestModelSpecInProvidersState(t *testing.T) {
+ home := t.TempDir()
+ t.Setenv("HOME", home)
+ t.Setenv("USERPROFILE", home)
+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+ t.Setenv("XDG_CACHE_HOME", t.TempDir())
+ if err := provider.Save(provider.Provider{ID: "kenari", Name: "Kenari", Chat: "https://example.invalid/v1", Key: "k", Models: []string{"glm-5:free", "qwen-x:free"}}); err != nil {
+ t.Fatal(err)
+ }
+ n := func(v int) *int { return &v }
+ s := func(v string) *string { return &v }
+ if err := provider.SetModelSpec("kenari/glm-5:free", provider.SpecChange{Context: n(131072), Output: n(16384), Vision: s("on"), Reasoning: s("low-high")}); err != nil {
+ t.Fatal(err)
+ }
+ if err := provider.SetModelSpec("kenari/qwen-x:free", provider.SpecChange{Reasoning: s("off")}); err != nil {
+ t.Fatal(err)
+ }
+ mux := http.NewServeMux()
+ providerRoutes(mux, nil)
+ w := httptest.NewRecorder()
+ mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/providers", nil))
+ var state providersJSON
+ if err := json.Unmarshal(w.Body.Bytes(), &state); err != nil {
+ t.Fatal(err)
+ }
+ models := map[string]modelJSON{}
+ for _, p := range state.Providers {
+ if p.ID == "kenari" {
+ for _, m := range p.Models {
+ models[m.ID] = m
+ }
+ }
+ }
+ m := models["glm-5:free"]
+ if m.Context != 131072 || m.Output != 16384 || !m.Images || !m.ImageSet || !m.Given || !slices.Equal(m.Kept, []string{"low", "medium", "high"}) {
+ t.Fatalf("glm-5:free: %+v", m)
+ }
+ if q := models["qwen-x:free"]; !q.Reasonless || len(q.Kept) != 0 || q.Images || q.Context != 0 {
+ t.Fatalf("qwen-x:free: %+v", q)
+ }
+}
diff --git a/internal/gui/tests/gateway-model-reasonless.test.cjs b/internal/gui/tests/gateway-model-reasonless.test.cjs
new file mode 100644
index 000000000..9327135bd
--- /dev/null
+++ b/internal/gui/tests/gateway-model-reasonless.test.cjs
@@ -0,0 +1,76 @@
+// Run with Node's test runner and Playwright on the module path; see README.md.
+// Dani Ariyanto on Discord (magpie model spec --reasoning off): a model the
+// user said takes no reasoning field is offered no levels by the gateway,
+// so the Gateway page's row says none, though its list has some. The
+// backend sends such a model reasonless with no kept levels; the row went
+// back to all the list's levels. Every language, at a wide and a narrow
+// window.
+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 providers = {
+ providers: [
+ { id: "kenari", name: "Kenari", icon: "generic", agents: [], models: [
+ { id: "glm-5", name: "glm-5", on: true, efforts: ["low", "medium", "high"], reasonless: true, images: false, context: 128000 },
+ { id: "qwen-x", name: "qwen-x", on: true, efforts: ["low", "medium", "high"], images: false, context: 64000 },
+ ] },
+ ],
+ gateway: { running: true, window: true, mine: true, url: "http://127.0.0.1:3999", models: 2, calls: [], groups: [] },
+};
+
+function serve(lang) {
+ const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
+ return async (route) => {
+ const url = new URL(route.request().url());
+ const json = (data) => route.fulfill({ json: data });
+ if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:${JSON.stringify(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 json(state);
+ if (url.pathname === "/api/providers") return json(providers);
+ if (url.pathname === "/api/groups") return json({ groups: [] });
+ if (url.pathname === "/api/gateway/trace") {
+ if (url.searchParams.get("wait")) await new Promise((r) => setTimeout(r, 20e3));
+ return json({ mine: true, now: new Date().toISOString(), seq: 1, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] });
+ }
+ if (url.pathname.startsWith("/api/")) return 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 rowOf = (page, id) => page.locator("#gwModels .row.model").filter({ has: page.locator(".name", { hasText: new RegExp("^" + id.replace(/[./]/g, "\\$&") + "$") }) });
+
+for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
+ for (const width of [1100, 420]) {
+ for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
+ test(`${engine} ${width} ${lang}: a model sent no reasoning field shows no levels on the Gateway page`, async (t) => {
+ const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: process.env.PLAYWRIGHT_CHANNEL || "chromium" }));
+ const page = await (await browser.newContext({ viewport: { width, height: 800 }, reducedMotion: "reduce" })).newPage();
+ t.after(async () => {
+ if (process.env.ARTIFACT_DIR) {
+ await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
+ await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${width}-${lang}-gw-reasonless.png`) });
+ }
+ await browser.close();
+ });
+ page.setDefaultTimeout(5000);
+ const errors = [];
+ page.on("pageerror", (e) => errors.push(e.message));
+ await page.route("**/*", serve(lang));
+ await page.goto("http://magpie.test/?view=gateway");
+ await page.locator("#gwModels .row.model").first().waitFor();
+ const chips = async (id) => (await rowOf(page, id).locator(".minfo .badge").allTextContents()).map((s) => s.trim());
+ const levels = await chips("kenari/qwen-x");
+ assert.equal(levels.length, 2, "a model with its levels says them: " + levels);
+ assert.equal(levels[1], "64K");
+ assert.deepEqual(await chips("kenari/glm-5"), ["128K"], "no levels for a model sent no reasoning field");
+ assert.deepEqual(errors, []);
+ });
+ }
+ }
+}
diff --git a/internal/provider/modelprefs.go b/internal/provider/modelprefs.go
index ea04f6482..1f2e67b81 100644
--- a/internal/provider/modelprefs.go
+++ b/internal/provider/modelprefs.go
@@ -247,26 +247,9 @@ func setModelEfforts(ref string, efforts []string) (bool, error) {
if err != nil {
return false, err
}
- all := p.Known(model)
- var keep []string
- for _, e := range efforts {
- e = strings.ToLower(strings.TrimSpace(e))
- if e == "" {
- continue
- }
- if len(all) == 0 && !slices.Contains(Levels, e) {
- return false, fmt.Errorf("%q is not a reasoning level (they are %s)", e, strings.Join(Levels, ", "))
- }
- if len(all) > 0 && !slices.Contains(all, e) {
- return false, fmt.Errorf("%s/%s has no reasoning level %q (it has %s)", p.ID, model, e, strings.Join(all, ", "))
- }
- if !slices.Contains(keep, e) {
- keep = append(keep, e)
- }
- }
- keep = effortsKept(all, keep) // in the vendor's order
- if len(keep) == len(all) {
- keep = nil
+ keep, err := keptEfforts(p, model, efforts)
+ if err != nil {
+ return false, err
}
s := settings.Load()
key := p.ID + "/" + model
@@ -291,6 +274,35 @@ func setModelEfforts(ref string, efforts []string) (bool, error) {
return true, nil
}
+// keptEfforts is the levels a user keeps of p's model as ModelEfforts
+// holds them: those given, checked against the model's own (or Levels, for
+// a model with none known), in the vendor's order, and none when they are
+// all of them.
+func keptEfforts(p *Provider, model string, efforts []string) ([]string, error) {
+ all := p.Known(model)
+ var keep []string
+ for _, e := range efforts {
+ e = strings.ToLower(strings.TrimSpace(e))
+ if e == "" {
+ continue
+ }
+ if len(all) == 0 && !slices.Contains(Levels, e) {
+ return nil, fmt.Errorf("%q is not a reasoning level (they are %s)", e, strings.Join(Levels, ", "))
+ }
+ if len(all) > 0 && !slices.Contains(all, e) {
+ return nil, fmt.Errorf("%s/%s has no reasoning level %q (it has %s)", p.ID, model, e, strings.Join(all, ", "))
+ }
+ if !slices.Contains(keep, e) {
+ keep = append(keep, e)
+ }
+ }
+ keep = effortsKept(all, keep) // in the vendor's order
+ if len(keep) == len(all) {
+ keep = nil
+ }
+ return keep, nil
+}
+
// SetModelReasonless says whether a provider's model, spelt
// "provider/model", takes no reasoning ask, whatever its list says: on,
// it offers no levels (Efforts, the agents' lists, a group's) and the
diff --git a/internal/provider/modelprefs_test.go b/internal/provider/modelprefs_test.go
index 29825f539..abcb51915 100644
--- a/internal/provider/modelprefs_test.go
+++ b/internal/provider/modelprefs_test.go
@@ -173,8 +173,10 @@ func TestModelEfforts(t *testing.T) {
if got := entry(t, "auto/sol").Efforts; !slices.Equal(got, []string{"low", "high"}) {
t.Fatal(got)
}
- // the vendor is still asked at any level it has
- if got := (Provider{ID: "a", Catalog: "a"}).Efforts("sol"); len(got) != 4 {
+ // the gateway fits a request to the levels kept, not all the vendor
+ // has: what the user kept wins in routing too (magpie model spec
+ // --reasoning, Dani Ariyanto on Discord)
+ if got := (Provider{ID: "a", Catalog: "a"}).Efforts("sol"); !slices.Equal(got, []string{"low", "high"}) {
t.Fatal(got)
}
if err := SetModelEfforts("a/sol", []string{"ultra"}); err == nil {
diff --git a/internal/provider/models.go b/internal/provider/models.go
index 2f757ca18..5f36de381 100644
--- a/internal/provider/models.go
+++ b/internal/provider/models.go
@@ -1594,15 +1594,15 @@ func (p Provider) RejectsTemperature(model string) bool {
}
// Efforts are the reasoning levels the model takes, when known: its own,
-// or those the user gave it when it has none known.
+// only those of them the user kept (magpie model efforts, magpie model
+// spec --reasoning), or those the user gave it when it has none known.
+// The gateway fits a request to these, so an agent asking a level the
+// user left out is asked the nearest one kept.
func (p Provider) Efforts(model string) []string {
if Reasonless(p.ID, model) {
return nil
}
- if all := p.Known(model); len(all) > 0 {
- return all
- }
- return effortsKept(nil, settings.Shared().ModelEfforts[p.ID+"/"+model])
+ return effortsKept(p.Known(model), settings.Shared().ModelEfforts[p.ID+"/"+model])
}
// Thinks reports whether the model reasons: it has levels, its list says
@@ -2488,6 +2488,9 @@ func entryFor(p Provider, m catalog.Model, s settings.Settings) Entry {
// providers serving it say (#402)
e.Reasoning = m.Reasoning || len(e.Efforts) > 0 || catalog.Thinks(m.ID)
e.Efforts = effortsKept(e.Efforts, s.ModelEfforts[e.ID])
+ // levels the user gave a model with none known say it thinks too, so
+ // an agent told its levels isn't told it doesn't reason
+ e.Reasoning = e.Reasoning || len(e.Efforts) > 0
// Codex's Ultra on a model OpenAI offers it on, served by another
// vendor (Copilot's gpt-6.1-sol, #656): a ChatGPT account's list says it
// already. Its subagents take their tasks as text from a magpie-served
diff --git a/internal/provider/modelspec.go b/internal/provider/modelspec.go
new file mode 100644
index 000000000..efff5cbfa
--- /dev/null
+++ b/internal/provider/modelspec.go
@@ -0,0 +1,437 @@
+package provider
+
+import (
+ "errors"
+ "fmt"
+ "maps"
+ "slices"
+ "strings"
+
+ "github.com/yetone/magpie/internal/settings"
+)
+
+// A model's specs — its context window, its reply limit, whether it takes
+// images and which reasoning levels it has — come from the vendor's own
+// list, else models.dev. A custom provider's model neither lists has none
+// of them known, and the GUI shows it with no badges, the gateway routes
+// it with no window and agents are told nothing of it (Dani Ariyanto on
+// Discord: kenari/*:free, experiential-labs/deepseek-v4.1-flash-uncensored).
+// `magpie model spec` says them all at once, over every one of those
+// sources. Each part is kept where the setting of its own already keeps
+// it, so there is one value per part, which every reader already goes by
+// (entryFor and the helpers it uses): the window in the provider's own
+// Contexts (SetContext, a routing input), the reply limit in settings'
+// ModelOutputs, vision in ModelImages and the levels in ModelEfforts and
+// ModelReasonless. None of them is touched by a catalog sync.
+
+// SpecChange is what `magpie model spec` changes of one model. Each part
+// left nil stays as it is.
+type SpecChange struct {
+ // Context and Output are numbers of tokens; 0 takes the user's away.
+ Context, Output *int
+ // Vision is "on", "off", or "auto" for the vendor's list's answer.
+ Vision *string
+ // Reasoning is the levels the model offers: a range ("low-high"), a
+ // list ("low,medium,high"), "off" for no reasoning ask at all, or
+ // "auto" for the levels its list gives.
+ Reasoning *string
+}
+
+// Empty is whether c changes nothing.
+func (c SpecChange) Empty() bool {
+ return c.Context == nil && c.Output == nil && c.Vision == nil && c.Reasoning == nil
+}
+
+// Auto is the word that takes one part of a spec back to the vendor's list
+// and models.dev.
+const Auto = "auto"
+
+// ParseVision is a typed vision answer: true or false, nil for auto.
+func ParseVision(v string) (*bool, error) {
+ switch strings.ToLower(strings.TrimSpace(v)) {
+ case "on", "yes", "true", "1":
+ return new(true), nil
+ case "off", "no", "false", "0":
+ return new(false), nil
+ case Auto, "default", "reset":
+ return nil, nil
+ }
+ return nil, fmt.Errorf("vision is on, off or auto, not %q", v)
+}
+
+// ParseLevels is a typed reasoning answer for p's model: the levels to keep
+// in their order, and off for no reasoning ask. "auto" is no levels and not
+// off. Levels are the model's own when its list gives them, else any of
+// Levels; a range runs from one to the other in that order, both
+// included.
+func (p Provider) ParseLevels(model, v string) (levels []string, off bool, err error) {
+ v = strings.ToLower(strings.TrimSpace(v))
+ switch v {
+ case "off", "no", "false":
+ return nil, true, nil
+ case Auto, "default", "reset":
+ return nil, false, nil
+ case "":
+ return nil, false, errors.New("reasoning is a range of levels (low-high), a list (low,medium,high), off or auto")
+ }
+ order := p.Known(model)
+ if len(order) == 0 {
+ order = Levels
+ }
+ notLevel := func(l string) error {
+ if len(p.Known(model)) > 0 {
+ return fmt.Errorf("%s/%s has no reasoning level %q (it has %s)", p.ID, model, l, strings.Join(order, ", "))
+ }
+ return fmt.Errorf("%q is not a reasoning level (they are %s)", l, strings.Join(order, ", "))
+ }
+ for _, part := range strings.FieldsFunc(v, func(r rune) bool { return r == ',' || r == ' ' || r == '/' }) {
+ from, to, isRange := strings.Cut(strings.ReplaceAll(part, "–", "-"), "-")
+ a := slices.Index(order, from)
+ if a < 0 {
+ return nil, false, notLevel(from)
+ }
+ b := a
+ if isRange {
+ if b = slices.Index(order, to); b < 0 {
+ return nil, false, notLevel(to)
+ }
+ if b < a {
+ return nil, false, fmt.Errorf("the range %s runs backwards: levels go %s", part, strings.Join(order, ", "))
+ }
+ }
+ for _, l := range order[a : b+1] {
+ if !slices.Contains(levels, l) {
+ levels = append(levels, l)
+ }
+ }
+ }
+ if len(levels) == 0 {
+ return nil, false, errors.New("reasoning is a range of levels (low-high), a list (low,medium,high), off or auto")
+ }
+ return levels, false, nil
+}
+
+// SetModelSpec makes the changes c says to a provider's model, spelt
+// "provider/model", or "provider/*" for every model of that provider (the
+// window and the reply limit only, as `magpie model context` and `output`
+// take it). Every part is checked before anything is written, so a bad
+// one writes nothing; the agents are told once, after them all.
+func SetModelSpec(ref string, c SpecChange) error {
+ if c.Empty() {
+ return errors.New("say what to change: --context, --output, --vision or --reasoning")
+ }
+ r := strings.TrimPrefix(strings.TrimSpace(ref), "magpie/")
+ if strings.HasPrefix(r, settings.AnyProvider) {
+ // a window or a vision answer is one provider's serving of a
+ // model, not the model's own: two relays serve one model at two
+ // windows, and a price's "*/<model>" has no such reading here
+ return fmt.Errorf("a spec is one provider's model: name it as provider/model, not %q", ref)
+ }
+ p, model, err := splitRef(ref)
+ if err != nil {
+ return err
+ }
+ every := model == "*"
+ if every && (c.Vision != nil || c.Reasoning != nil) {
+ return fmt.Errorf("vision and reasoning are set one model at a time, as a provider's models differ in them; %s takes --context and --output", p.ID+"/*")
+ }
+ for _, n := range []*int{c.Context, c.Output} {
+ if n != nil && *n < 0 {
+ return fmt.Errorf("a number of tokens is not %d", *n)
+ }
+ }
+ setting := c.Context != nil && *c.Context > 0 || c.Output != nil && *c.Output > 0 ||
+ c.Vision != nil && !isAuto(*c.Vision) || c.Reasoning != nil && !isAuto(*c.Reasoning)
+ if setting && !every && !p.serves(model) {
+ return fmt.Errorf("%s has no model %s (magpie provider %s lists them)", p.ID, model, p.ID)
+ }
+ key := p.ID + "/" + model
+ if c.Output != nil {
+ if err := settings.CheckModelKey("an output limit", key); err != nil {
+ return err
+ }
+ }
+ var images *bool
+ if c.Vision != nil {
+ if images, err = ParseVision(*c.Vision); err != nil {
+ return err
+ }
+ }
+ var levels []string
+ var off bool
+ if c.Reasoning != nil {
+ if levels, off, err = p.ParseLevels(model, *c.Reasoning); err != nil {
+ return err
+ }
+ if _, err := keptEfforts(p, model, levels); err != nil {
+ return err
+ }
+ }
+
+ // every part is good: write them
+ changed, saved := false, false
+ if c.Output != nil {
+ ch, err := setOutput(key, *c.Output)
+ if err != nil {
+ return touchedIf(changed, err)
+ }
+ changed = changed || ch
+ }
+ if c.Vision != nil {
+ ch, err := setModelImage(key, images)
+ if err != nil {
+ return touchedIf(changed, err)
+ }
+ changed = changed || ch
+ }
+ if c.Reasoning != nil {
+ var ch bool
+ if off {
+ ch, err = setModelReasonless(key, true)
+ } else {
+ // levels given, or none for auto, also take back an "off"
+ ch, err = setModelEfforts(key, levels)
+ }
+ if err != nil {
+ return touchedIf(changed, err)
+ }
+ changed = changed || ch
+ }
+ if c.Context != nil {
+ // the provider's Save tells the agents itself, after the settings
+ // above are written
+ if *c.Context > 0 && p.Contexts[model] != *c.Context {
+ err = SetContext(*p, model, *c.Context)
+ saved = err == nil
+ } else if *c.Context == 0 {
+ saved, err = DropContext(*p, model)
+ }
+ if err != nil {
+ return touchedIf(changed, err)
+ }
+ }
+ return touchedIf(changed && !saved, nil)
+}
+
+func isAuto(v string) bool {
+ switch strings.ToLower(strings.TrimSpace(v)) {
+ case Auto, "default", "reset":
+ return true
+ }
+ return false
+}
+
+// setOutput is SetModelOutput without telling the agents: n over 0 is the
+// limit, 0 takes the user's away. It reports whether anything changed.
+func setOutput(key string, n int) (bool, error) {
+ s := settings.Load()
+ if n == 0 {
+ if _, ok := s.ModelOutputs[key]; !ok {
+ return false, nil
+ }
+ delete(s.ModelOutputs, key)
+ } else {
+ if s.ModelOutputs[key] == n {
+ return false, nil
+ }
+ if s.ModelOutputs == nil {
+ s.ModelOutputs = map[string]int{}
+ }
+ s.ModelOutputs[key] = n
+ }
+ return true, settings.Save(s)
+}
+
+// ResetModelSpec takes every spec the user gave a provider's model away —
+// its window, reply limit, vision answer and levels — leaving it to the
+// vendor's list and models.dev again. "provider/*" takes away the window
+// and reply limit given every model of the provider. It reports what it
+// took away, by part ("context", "output", "vision", "reasoning"). A
+// provider that has gone still has its settings' parts taken away, under
+// the key as typed: a spec outlives the provider it was set for, and `magpie
+// model specs` still lists it.
+func ResetModelSpec(ref string) ([]string, error) {
+ r := strings.TrimPrefix(strings.TrimSpace(ref), "magpie/")
+ if IsGroupRef(r) {
+ return nil, errors.New("that is a routing group, not a provider's model")
+ }
+ pid, model, ok := strings.Cut(r, "/")
+ if !ok || pid == "" || model == "" {
+ return nil, fmt.Errorf("name a model as provider/model, not %q", ref)
+ }
+ var p *Provider
+ if found, err := Find(pid); err == nil {
+ p, pid = found, found.ID
+ }
+ key := pid + "/" + model
+ s := settings.Load()
+ var gone []string
+ if _, ok := s.ModelOutputs[key]; ok {
+ delete(s.ModelOutputs, key)
+ gone = append(gone, "output")
+ }
+ if _, ok := s.ModelImages[key]; ok {
+ delete(s.ModelImages, key)
+ gone = append(gone, "vision")
+ }
+ _, narrowed := s.ModelEfforts[key]
+ if narrowed || s.ModelReasonless[key] {
+ delete(s.ModelEfforts, key)
+ delete(s.ModelReasonless, key)
+ gone = append(gone, "reasoning")
+ }
+ if len(gone) > 0 {
+ if err := settings.Save(s); err != nil {
+ return nil, err
+ }
+ }
+ if p != nil {
+ dropped, err := DropContext(*p, model) // its Save tells the agents
+ if err != nil {
+ return gone, touchedIf(len(gone) > 0, err)
+ }
+ if dropped {
+ return append([]string{"context"}, gone...), nil
+ }
+ }
+ return gone, touchedIf(len(gone) > 0, nil)
+}
+
+// SpecPart is one part of a model's spec as it is in force: its value and
+// where it came from — "model" for what the user set on it, "provider" for
+// what they set on every model of its provider, "" for the vendor's list
+// and models.dev.
+type SpecPart struct {
+ Value string
+ From string
+}
+
+// Spec is a model's spec as magpie goes by it: the entry the gateway routes
+// and the GUI and agents are shown, with where each part came from.
+type Spec struct {
+ Context, Output, Vision, Reasoning SpecPart
+ Entry Entry
+}
+
+// SpecOf is the spec of a model a provider serves, by "provider/model".
+func SpecOf(ref string) (Spec, bool) {
+ p, model, err := splitRef(ref)
+ if err != nil || model == "*" {
+ return Spec{}, false
+ }
+ e, ok := ServedEntryOf(p.ID + "/" + model)
+ if !ok {
+ return Spec{}, false
+ }
+ s := settings.Load()
+ key := e.ID
+ sp := Spec{Entry: e}
+ sp.Context.Value = tokenWord(e.Context)
+ switch {
+ case p.Contexts[model] > 0:
+ sp.Context.From = "model"
+ case p.Contexts["*"] > 0:
+ sp.Context.From = "provider"
+ }
+ sp.Output.Value = tokenWord(e.PublishedOutput())
+ switch {
+ case s.ModelOutputs[key] > 0:
+ sp.Output.From = "model"
+ case s.ModelOutputs[p.ID+"/*"] > 0:
+ sp.Output.From = "provider"
+ }
+ switch {
+ case e.Images:
+ sp.Vision.Value = "on"
+ case e.ImageInput != nil:
+ sp.Vision.Value = "off"
+ default:
+ sp.Vision.Value = "unknown"
+ }
+ if _, ok := s.ModelImages[key]; ok {
+ sp.Vision.From = "model"
+ }
+ switch {
+ case e.Reasonless:
+ sp.Reasoning.Value = "off"
+ case len(e.Efforts) > 0:
+ sp.Reasoning.Value = strings.Join(e.Efforts, ",")
+ case e.Reasoning:
+ sp.Reasoning.Value = "thinks, no levels"
+ default:
+ sp.Reasoning.Value = "unknown"
+ }
+ if _, ok := s.ModelEfforts[key]; ok || s.ModelReasonless[key] {
+ sp.Reasoning.From = "model"
+ }
+ return sp, true
+}
+
+// SpecRow is one model the user gave a spec, as `magpie model specs` lists
+// it: each part as they set it, "" where they set none.
+type SpecRow struct {
+ Key string
+ Context, Output, Vision, Reasoning string
+ // Gone: no provider of that id is there any more
+ Gone bool
+}
+
+// ModelSpecs are the models the user gave any part of a spec, by key.
+func ModelSpecs() []SpecRow {
+ rows := map[string]*SpecRow{}
+ row := func(k string) *SpecRow {
+ if rows[k] == nil {
+ rows[k] = &SpecRow{Key: k}
+ }
+ return rows[k]
+ }
+ for _, p := range All() {
+ for m, n := range p.Contexts {
+ if n > 0 {
+ row(p.ID + "/" + m).Context = tokenWord(n)
+ }
+ }
+ }
+ s := settings.Load()
+ for k, n := range s.ModelOutputs {
+ if n > 0 {
+ row(k).Output = tokenWord(n)
+ }
+ }
+ for k, v := range s.ModelImages {
+ row(k).Vision = map[bool]string{true: "on", false: "off"}[v]
+ }
+ for k, es := range s.ModelEfforts {
+ if len(es) > 0 {
+ row(k).Reasoning = strings.Join(es, ",")
+ }
+ }
+ for k, off := range s.ModelReasonless {
+ if off {
+ row(k).Reasoning = "off"
+ }
+ }
+ var out []SpecRow
+ for _, k := range slices.Sorted(maps.Keys(rows)) {
+ r := *rows[k]
+ pid, _, _ := strings.Cut(k, "/")
+ if _, err := findIn(All(), pid); err != nil {
+ r.Gone = true
+ }
+ out = append(out, r)
+ }
+ return out
+}
+
+// tokenWord is a number of tokens as the CLI says it: 1M, 200k, 12345.
+func tokenWord(n int) string {
+ switch {
+ case n <= 0:
+ return "unknown"
+ case n%1000000 == 0:
+ return fmt.Sprintf("%dM", n/1000000)
+ case n%1000 == 0:
+ return fmt.Sprintf("%dk", n/1000)
+ }
+ return fmt.Sprintf("%d", n)
+}
diff --git a/internal/provider/modelspec_test.go b/internal/provider/modelspec_test.go
new file mode 100644
index 000000000..3d287130e
--- /dev/null
+++ b/internal/provider/modelspec_test.go
@@ -0,0 +1,228 @@
+package provider
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "slices"
+ "testing"
+
+ "github.com/yetone/magpie/internal/catalog"
+ "github.com/yetone/magpie/internal/plugin"
+ "github.com/yetone/magpie/internal/settings"
+)
+
+// specHome is a sandbox with a custom relay whose models no list knows
+// (Dani Ariyanto on Discord: kenari/*:free).
+func specHome(t *testing.T) {
+ t.Helper()
+ limitsHome(t)
+ if err := Save(Provider{ID: "kenari", Name: "Kenari", Chat: "http://127.0.0.1:1/v1", Key: "k",
+ Models: []string{"glm-5:free", "qwen-x:free"}}); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// specFiles are the bytes of the two files a spec is kept in.
+func specFiles(t *testing.T) string {
+ t.Helper()
+ var out string
+ for _, f := range []string{"settings.json", "providers.json"} {
+ b, _ := os.ReadFile(filepath.Join(settings.Dir(), f))
+ out += f + ":" + string(b) + "\n"
+ }
+ return out
+}
+
+// A custom provider's model with nothing known takes a whole spec at once,
+// the catalog entry every reader goes by carries it, `magpie model specs`
+// lists it, and a reset leaves the model as it was before, unknown on
+// every part. The agents are told once for a change that writes both files.
+func TestModelSpecSetListReset(t *testing.T) {
+ specHome(t)
+ e, ok := EntryOf("kenari/glm-5:free")
+ if !ok {
+ t.Fatal("kenari/glm-5:free is not in the catalog")
+ }
+ if e.Context != 0 || e.Output != 0 || e.Images || e.ImageInput != nil || len(e.Efforts) != 0 {
+ t.Fatalf("before: a model nothing knows has %+v", e)
+ }
+ told := 0
+ catalog.Changed = func() { told++ }
+ t.Cleanup(func() { catalog.Changed = nil })
+
+ if err := SetModelSpec("kenari/glm-5:free", SpecChange{
+ Context: new(131072), Output: new(16384), Vision: new("on"), Reasoning: new("low-high"),
+ }); err != nil {
+ t.Fatal(err)
+ }
+ if told != 1 {
+ t.Errorf("the agents were told %d times; want once", told)
+ }
+ e, _ = EntryOf("kenari/glm-5:free")
+ if e.Context != 131072 || e.Output != 16384 || !e.Images || e.ImageInput == nil || !*e.ImageInput {
+ t.Errorf("entry after set: context %d output %d images %v", e.Context, e.Output, e.Images)
+ }
+ if !slices.Equal(e.Efforts, []string{"low", "medium", "high"}) || !e.Reasoning {
+ t.Errorf("levels after set: %v reasoning %v", e.Efforts, e.Reasoning)
+ }
+ // the other model of the provider is left as it was
+ if o, _ := EntryOf("kenari/qwen-x:free"); o.Context != 0 || o.Images || len(o.Efforts) != 0 {
+ t.Errorf("the model given nothing changed: %+v", o)
+ }
+ rows := ModelSpecs()
+ if len(rows) != 1 || rows[0] != (SpecRow{Key: "kenari/glm-5:free", Context: "131072", Output: "16384", Vision: "on", Reasoning: "low,medium,high"}) {
+ t.Errorf("specs: %+v", rows)
+ }
+ sp, ok := SpecOf("kenari/glm-5:free")
+ if !ok || sp.Context.From != "model" || sp.Vision.From != "model" || sp.Reasoning.Value != "low,medium,high" {
+ t.Errorf("spec: %+v", sp)
+ }
+
+ // one part back to auto leaves the others
+ if err := SetModelSpec("kenari/glm-5:free", SpecChange{Vision: new("auto")}); err != nil {
+ t.Fatal(err)
+ }
+ if e, _ = EntryOf("kenari/glm-5:free"); e.Images || e.ImageInput != nil || e.Context != 131072 {
+ t.Errorf("vision auto: images %v %v context %d", e.Images, e.ImageInput, e.Context)
+ }
+
+ gone, err := ResetModelSpec("kenari/glm-5:free")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !slices.Equal(gone, []string{"context", "output", "reasoning"}) {
+ t.Errorf("reset took away %v", gone)
+ }
+ e, _ = EntryOf("kenari/glm-5:free")
+ if e.Context != 0 || e.Output != 0 || e.Images || len(e.Efforts) != 0 || e.Reasoning {
+ t.Errorf("after reset: %+v", e)
+ }
+ if rows := ModelSpecs(); len(rows) != 0 {
+ t.Errorf("specs after reset: %+v", rows)
+ }
+ if gone, err := ResetModelSpec("kenari/glm-5:free"); err != nil || len(gone) != 0 {
+ t.Errorf("a second reset: %v %v", gone, err)
+ }
+}
+
+// A bad part — a level the model hasn't, a level that is no level, a range
+// that runs backwards, a model the provider doesn't serve, vision for a
+// whole provider, a model from any provider — is refused, and nothing of
+// the change is written, not even its good parts.
+func TestModelSpecRefusalWritesNothing(t *testing.T) {
+ specHome(t)
+ if err := SetModelSpec("kenari/glm-5:free", SpecChange{Context: new(65536)}); err != nil {
+ t.Fatal(err)
+ }
+ before := specFiles(t)
+ for name, c := range map[string]struct {
+ ref string
+ c SpecChange
+ }{
+ "not a level": {"kenari/glm-5:free", SpecChange{Context: new(1 << 20), Reasoning: new("turbo")}},
+ "backwards range": {"kenari/glm-5:free", SpecChange{Output: new(4096), Reasoning: new("high-low")}},
+ "bad vision": {"kenari/glm-5:free", SpecChange{Context: new(1 << 20), Vision: new("maybe")}},
+ "unserved model": {"kenari/nope", SpecChange{Context: new(1 << 20)}},
+ "provider vision": {"kenari/*", SpecChange{Context: new(1 << 20), Vision: new("on")}},
+ "provider levels": {"kenari/*", SpecChange{Reasoning: new("low")}},
+ "any provider": {"*/glm-5:free", SpecChange{Context: new(1 << 20)}},
+ "negative": {"kenari/glm-5:free", SpecChange{Output: new(-1), Vision: new("on")}},
+ "nothing said": {"kenari/glm-5:free", SpecChange{}},
+ "no such provider": {"nobody/glm-5:free", SpecChange{Context: new(1 << 20)}},
+ } {
+ if err := SetModelSpec(c.ref, c.c); err == nil {
+ t.Errorf("%s: %s %+v was taken", name, c.ref, c.c)
+ }
+ if got := specFiles(t); got != before {
+ t.Fatalf("%s: a refused spec wrote:\nbefore %s\nafter %s", name, before, got)
+ }
+ }
+}
+
+// A model's own window and reply limit win over the ones given every model
+// of its provider ("kenari/*"), which a model with none of its own takes;
+// a reset of the model leaves it the provider's.
+func TestModelSpecProviderWildcard(t *testing.T) {
+ specHome(t)
+ if err := SetModelSpec("kenari/*", SpecChange{Context: new(65536), Output: new(8192)}); err != nil {
+ t.Fatal(err)
+ }
+ if err := SetModelSpec("kenari/glm-5:free", SpecChange{Context: new(131072), Output: new(16384)}); err != nil {
+ t.Fatal(err)
+ }
+ if e, _ := EntryOf("kenari/glm-5:free"); e.Context != 131072 || e.Output != 16384 {
+ t.Errorf("the model's own: %d %d", e.Context, e.Output)
+ }
+ if e, _ := EntryOf("kenari/qwen-x:free"); e.Context != 65536 || e.Output != 8192 {
+ t.Errorf("the provider's: %d %d", e.Context, e.Output)
+ }
+ if sp, _ := SpecOf("kenari/qwen-x:free"); sp.Context.From != "provider" || sp.Output.From != "provider" {
+ t.Errorf("spec from: %+v", sp)
+ }
+ if _, err := ResetModelSpec("kenari/glm-5:free"); err != nil {
+ t.Fatal(err)
+ }
+ if e, _ := EntryOf("kenari/glm-5:free"); e.Context != 65536 || e.Output != 8192 {
+ t.Errorf("after the model's reset: %d %d", e.Context, e.Output)
+ }
+ if gone, err := ResetModelSpec("kenari/*"); err != nil || !slices.Equal(gone, []string{"context", "output"}) {
+ t.Errorf("provider reset: %v %v", gone, err)
+ }
+ if e, _ := EntryOf("kenari/qwen-x:free"); e.Context != 0 || e.Output != 0 {
+ t.Errorf("after the provider's reset: %d %d", e.Context, e.Output)
+ }
+}
+
+// What the user set wins over what the vendor's own list says — a plugin
+// listing a 32K window, images and reasoning — and survives the list being
+// fetched again with other numbers. A model with no spec keeps its list's.
+func TestModelSpecWinsOverTheListAndASync(t *testing.T) {
+ claudeHome(t)
+ t.Setenv("MAGPIE_BUN", "")
+ catalog.Reset()
+ t.Cleanup(catalog.Reset)
+ spec := "@magpie-community/opencode-x-auth"
+ write := func(name string, v any) {
+ t.Helper()
+ b, _ := json.Marshal(v)
+ os.MkdirAll(settings.Dir(), 0o700)
+ if err := os.WriteFile(filepath.Join(settings.Dir(), name), b, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ }
+ write("plugins.json", map[string]any{"plugins": []map[string]any{{"spec": spec}}})
+ write("plugin-auth.json", map[string]map[string]any{"xp": {"type": "api", "key": "sk-1234"}})
+ list := func(ctx int) {
+ plugin.UseCached([]plugin.Provider{{ID: "xp", Spec: spec, Name: "XP",
+ Methods: []plugin.Method{{Type: "api", Label: "API key"}},
+ Accounts: []plugin.Account{{Key: "xp", Type: "api"}},
+ Models: []plugin.Model{
+ {ID: "deepseek-v4.1-flash-uncensored", Name: "DS", Context: ctx, Output: 8192, Image: true, ImageSaid: true, Reasoning: true},
+ {ID: "other", Name: "Other", Context: ctx, Output: 8192, Image: true, ImageSaid: true},
+ }}})
+ }
+ list(32768)
+ t.Cleanup(func() { plugin.UseCached(nil) })
+ if e, ok := EntryOf("xp/deepseek-v4.1-flash-uncensored"); !ok || e.Context != 32768 || !e.Images {
+ t.Fatalf("the list's: %+v %v", e, ok)
+ }
+ if err := SetModelSpec("xp/deepseek-v4.1-flash-uncensored", SpecChange{
+ Context: new(1 << 20), Output: new(65536), Vision: new("off"), Reasoning: new("off"),
+ }); err != nil {
+ t.Fatal(err)
+ }
+ check := func(when string, listed int) {
+ t.Helper()
+ e, _ := EntryOf("xp/deepseek-v4.1-flash-uncensored")
+ if e.Context != 1<<20 || e.Output != 65536 || e.Images || e.ImageInput == nil || *e.ImageInput || !e.Reasonless || e.Reasoning {
+ t.Errorf("%s: context %d output %d images %v reasonless %v reasoning %v", when, e.Context, e.Output, e.Images, e.Reasonless, e.Reasoning)
+ }
+ if o, _ := EntryOf("xp/other"); o.Context != listed || o.Output != 8192 || !o.Images {
+ t.Errorf("%s: the model with no spec: context %d output %d images %v", when, o.Context, o.Output, o.Images)
+ }
+ }
+ check("set", 32768)
+ list(200000) // fetched again: the vendor says another window
+ check("after the list is fetched again", 200000)
+}
diff --git a/internal/settings/settings.go b/internal/settings/settings.go
index 97121d20c..e629ea6c0 100644
--- a/internal/settings/settings.go
+++ b/internal/settings/settings.go
@@ -652,7 +652,9 @@ type Settings struct {
ModelNames map[string]string `json:"modelNames,omitempty"`
// ModelEfforts are the reasoning levels the user keeps of a model's,
// by "<provider id>/<model id>": the lists magpie hands out offer only
- // those (see provider.SetModelEfforts).
+ // those, and the gateway fits a request's level to them; for a model
+ // with no levels known they are the levels given it (see
+ // provider.SetModelEfforts, magpie model spec).
ModelEfforts map[string][]string `json:"modelEfforts,omitempty"`
// ModelReasonless are the models the user said take no reasoning ask,
// by "<provider id>/<model id>": magpie offers them no levels and