Files
magpie/internal/agent/zero_option_test.go
T
yetone 47b7ba3030 models: a model its provider lists at $0 says free in the pickers and Routing (jiangguochao666 on X)
jiangguochao666 asked for each provider's free models to show in Routing
and the model settings, to route to them. The green free tag was shown
only for a plan's free model or a model with "free" in its name.

provider.PricedFree says a model is free only when its price for that
provider is known to be $0:
- A price the user set comes first (provider/model, provider/*, then
  the model's own key). A $0 one makes it free, and any other price, a
  paid tier included, makes it paid.
- Otherwise the provider's own catalogs decide. A $0 from a plan's
  catalog (-plan, membership catalogs) is the plan's per-token price,
  not free. A $0 from only the model's maker is not free either: a
  relay serving Zhipu's free glm-4.7-flash may charge for it.
- No known price means not free.
FreeModel (Free first, Cheapest first routing) is unchanged.

From the local models.dev cache this tags:
- every model of NVIDIA NIM and ModelScope;
- the :free / -free models of OpenRouter, OpenCode Zen and Go, Kilo,
  AIHubMix, ZenMux, Vercel, Requesty and other relays;
- Zhipu's and Z.ai's glm-4.x-flash.
No coding or token plan is tagged.

Where it shows, with the hover "free: listed at $0 on this provider":
- an agent's model picker (agent.Option.Zero);
- the provider editor's model chips, and its Free only button
  (modelJSON.Zero);
- Routing's Add a model and classifier pickers (modelRef.Zero).
The word is new in en, zh, zh-TW, ja and de.

Tests, each red on the old code:
- TestPricedFreeIsTheProvidersOwnZero: own, plan, maker-only, unpriced
  and user prices.
- TestPickerSaysAZeroPricedModelIsFree: Option.Zero, cleared at once by
  a price the user saves.
- TestZeroPricedModelsSayFree: the editor's and Routing's JSON.
- zero-priced-free.test.cjs: the three surfaces, in Chromium and WebKit,
  at 1100 and 420px, in all 5 languages. It checks the tag's text,
  hover and fit, that the paid model has no tag, that a plan's free
  model keeps its own hover, no sideways scroll and no page errors.

Broken by hand (each restored, diff checked):
- The source check ignored (maker and plan $0 count): the provider test
  goes red on the plan model and the relay's glm-4.7-flash.
- PricedFree always false: the provider, agent and gui tests go red.
- The user's price ignored: the provider and agent tests go red.
- The badge's o.zero / m.zero and Routing's zero dropped in app.js and
  routing/client.js: zero-priced-free goes red, "the $0 model has no
  free tag in the agent's picker".

Run at this commit's tree (base 3f1f1882a):
- mote, go test -timeout 45m ./...: 84 ok, 19 with no tests, 3 FAIL.
  Each red is filed and fails the same way at origin/main 7d022bdb6:
  - TestEveryRouteHasACommandOrIsNamed: /api/context/reply, #5026.
  - TestMainWindowCorners/macOS_26_SDK: macOS 27, #3949.
  - TestCompactionOverByteCapEveryTime: red only in the full gateway
    run, ok alone here and at main, #4862.
  TestCoreScriptBudget passed; no budget was raised.
- gofmt -l: clean. GOOS=windows go vet (mote): ok. go vet -tags gtk3 on
  Linux (wsl, magpie-desktop): ok. A cross GOOS=linux vet from macOS
  fails inside wails' cgo files on main too.
- wsl: zero-priced-free + i18n-keys, 24 of 24 pass.
- wsl: the whole GUI suite, concurrency 4: 19062 of 19585 pass,
  523 fail in 127 files, at load ~86 on a shared box. Those 127 files
  run again at concurrency 2: 465 red here and 417 at the base
  3f1f1882a, the same files on both. 11 files with a red only on this
  tree, the routing, model and search ones near this change, run again
  alone at load ~3: the same 7 fail on both trees
  (33 here, 27 at the base), and which cases fail changes from run to
  run with the same errors. None is in a test that reads the free tag.
  Filed already: #3096 and #5035/#5088 (routing-rule-*, rest-try-now),
  #4388 (model-chip-steady alone on WSL), #4585 and #4932 (under load).
  The other files red only on this tree (usage wheels, tray, archive
  rows) were not run again alone; nothing in them reads the free tag.

Not tried: a real provider's list in the live app (the tests use
fixture catalogs); Free first routing choosing a $0-priced model it
didn't choose before (FreeModel is unchanged on purpose).
2026-11-04 05:00:18 +08:00

62 lines
2.0 KiB
Go

package agent
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// jiangguochao666 on X: an agent's model picker says free by a model its
// provider lists at $0 (Option.Zero, provider.PricedFree), as OpenRouter's
// foo:free, and not by the paid foo beside it. A price the user sets
// afterwards is seen at once: the list built for the old settings isn't
// kept.
func TestPickerSaysAZeroPricedModelIsFree(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(catalog.CachePath(), []byte(`{"zfree":{"models":{
"foo:free":{"id":"foo:free","cost":{"input":0,"output":0}},
"foo":{"id":"foo","cost":{"input":1,"output":2}}}}}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
if err := provider.Save(provider.Provider{ID: "zf", Name: "ZF", Catalog: "zfree", Key: "k", Chat: "https://zf.example/v1", Models: []string{"foo:free", "foo"}}); err != nil {
t.Fatal(err)
}
of := func() map[string]Option {
out := map[string]Option{}
for _, o := range viaMagpie("opencode", "magpie/") {
out[o.Ref] = o
}
return out
}
got := of()
if o, ok := got["zf/foo:free"]; !ok || !o.Zero || o.Free {
t.Fatalf("the $0 model: %+v %v", o, ok)
}
if o, ok := got["zf/foo"]; !ok || o.Zero {
t.Fatalf("the paid model: %+v %v", o, ok)
}
s := settings.Load()
one, zero := 1.0, 0.0
s.ModelPrices = map[string]settings.ModelPrice{"zf/foo:free": {Input: &one, Output: &one, CacheRead: &zero, CacheWrite: &zero}}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if o := of()["zf/foo:free"]; o.Zero {
t.Fatalf("a model the user pays for is still free: %+v", o)
}
}