jiabubu asked on Discord #help for a way to mark a provider, model or key
as free, so that Free first routing spends it first. Their example is
ModelScope, which has a free daily quota. Until now routes.Seat.Free
came only from the vendor catalog's Free flag, and there was no way for
the user to set it.
Provider.CountFree ("Free for me" in the provider editor's Advanced fold,
`magpie provider set <id> free=yes|no` in the CLI) makes FreeModel true
for every model of that provider. Routing therefore treats it like a
vendor's free model: Free first uses it before paid ones, and Cheapest
first weighs it at nothing, because PriceAt, which only routing reads,
returns 0 for a free model. Prices shown (ListPrice and the model list)
and the costs Usage counts (EffectivePrice) are unchanged. The mark says
what to spend first, not what a call cost, and a quota that runs out
still bills at the listed price. Key and account (subscription)
providers both carry it, through withPicks, the account literal and the
copy merge.
Tests:
- internal/gateway TestFreeFirstSpendsTheProviderTheUserCountsFree:
- a Free first group [paidv/m, mine/m] sends its first call to mine
once mine is marked, and the trace row says Free;
- EffectivePrice is still the listed 3/15;
- Cheapest picks mine first;
- unmarked, paidv goes first again.
- internal/gui TestProviderSaveKeepsCountFree: the preset and typed-in
providers save the mark and clear it, and /api/providers reports it.
- internal/cli TestProviderSetFree: free=yes and free=no work; the value
shows in `provider <id>`; free=maybe, an empty value and --help are
refused and change nothing.
- GUI internal/gui/tests/provider-count-free.test.cjs covers the
ModelScope preset, a custom provider saved with the mark, and a Codex
account. It runs chromium and webkit, in en, zh, zh-TW, ja and de, at
widths 900 and 440. It checks the Advanced fold, the check and save
POST bodies, that the row fits with no sideways scroll, and that each
string has a translation in I18N.
Core script budget: rebased on fa5e86cf2, the core's scripts came to
1,538,236 bytes, 880 over coreScriptBudget, which TestCoreScriptBudget
caught. The budget is not raised. countFreeField's comment and five
long comments on the editor's helpers (proxyPicker, concurrencyField,
priceRateField, pluginOptionsField) are cut down, so app.js ends 212
bytes smaller than before this change.
Run (mote via mote-run, at this change's tree on base 8ee75adbf):
- go test of internal/gateway (-run FreeFirst|InOrder), internal/gui,
internal/cli and internal/provider: ok.
- GUI test: chromium 30/30 on mote. WebKit could not launch on mote at
all at the time (a bare webkit.launch probe timed out at 60s, and
other sessions' WebKit launches were stuck there too). WebKit was run
on Linux instead (wsl-run, magpie-desktop): 30/30.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./... and
GOOS=windows go vet ./... ok; go build for darwin, linux (nogui) and
windows ok.
- At the pushed head (rebased on fa5e86cf2, comments cut): go test of
internal/gui, internal/cli, internal/provider and internal/gateway
-run FreeFirst|CountFree ok (internal/gui was red only on
TestCoreScriptBudget before the cut); node --check app.js ok; the GUI
test in chromium 30/30.
- The full Go suite (go test -tags nogui -timeout 45m ./...) is still
running on mote at ship time, per the owner's ship-at-once rule. Any
red it finds gets a follow-up commit. The whole GUI suite was not run:
mote's WebKit cannot launch and the Linux box was running other
sessions' GUI suites.
Broken by hand:
- With the CountFree line taken out of FreeModel, all three Go tests
went red, e.g. 'Free first went to "paidv" (free false) first'.
- With `pr.CountFree = in.CountFree` taken out of the preset save
branch, TestProviderSaveKeepsCountFree went red ("the preset's mark
wasn't kept").
Not tried:
- A real ModelScope account; the tests use fake upstreams.
- A per-model mark (optional in the request); the mark is
provider-wide.
- The TUI editor (the CLI setter covers the terminal).
43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// magpie provider set free=yes counts every model of a provider free for
|
|
// Free first (jiabubu on Discord: ModelScope's free daily quota), show
|
|
// says so, free=no takes it back, and any other value is refused and
|
|
// leaves the saved one alone.
|
|
func TestProviderSetFree(t *testing.T) {
|
|
groupsHome(t)
|
|
if _, err := printed(t, func() error { return providerCmd([]string{"provider", "set", "a", "free=yes"}) }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p := mustFind(t, "a"); !p.CountFree || !p.FreeModel("whatever") {
|
|
t.Fatalf("free=yes: %+v", p)
|
|
}
|
|
out, err := printed(t, func() error { return providerCmd([]string{"provider", "a"}) })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(out, "counted free") {
|
|
t.Fatalf("show doesn't say it is counted free:\n%s", out)
|
|
}
|
|
for _, v := range []string{"maybe", "", "--help"} {
|
|
err := providerCmd([]string{"provider", "set", "a", "free=" + v})
|
|
if err == nil || !strings.Contains(err.Error(), "free=yes|no") {
|
|
t.Errorf("free=%q: %v", v, err)
|
|
}
|
|
if !mustFind(t, "a").CountFree {
|
|
t.Errorf("a refused free=%q turned it off", v)
|
|
}
|
|
}
|
|
if _, err := printed(t, func() error { return providerCmd([]string{"provider", "set", "a", "free=no"}) }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if mustFind(t, "a").CountFree {
|
|
t.Fatal("free=no left it on")
|
|
}
|
|
}
|