Files
magpie/internal/provider/kimi_code_price_test.go
T
yetone ee657c0969 usage: a Kimi Code membership's k3 is counted at Kimi's API price for kimi-k3, not ≈¥0.000, and kimi-for-coding is unpriced rather than free (#1370, maicent: Kimi Code 的模型计费都是 0)
Why it was 0: magpie already counts a subscription's calls at the API list
price (a Claude or Codex account at its maker's, cf6b82f7). A Kimi Code
provider is looked up in its own models.dev catalog first, and models.dev
lists every model of kimi-code-plan-global / kimi-code-plan-cn at $0 (what
the membership charges per token), so k3, k3-256k, kimi-for-coding and
kimi-for-coding-highspeed were all "priced" at $0. The same $0 catalogs
were also among the maker catalogs, so MakerPrice("k3") was $0 too.

What changed (internal/provider/models.go):
- membershipCatalogs (the two Kimi Code plan catalogs) are never read for a
  price: ListPrice, MakerPrice and PricedName skip them.
- A provider served at a Kimi Code catalog prices its model as the Kimi API
  model it is (kimiCodeAPI), per Kimi Code's docs
  (https://www.kimi.com/code/docs/en/, read 2026-10-09): k3 and k3-256k are
  K3 -> kimi-k3; kimi-for-coding-highspeed is K2.7 Code HighSpeed ->
  kimi-k2.7-code-highspeed. models.dev's canonical_model_id agrees. The
  prices come from moonshotai's catalog, which matches Kimi's pricing page
  (https://platform.kimi.ai/docs/pricing/chat: kimi-k3 $3 in, $15 out,
  $0.30 cached, $3 cache write; kimi-k2.7-code-highspeed $1.90/$8/$0.38).
- kimi-for-coding is K2.8 Preview, which the Kimi API doesn't sell: it has
  no API price, so it is now unpriced (the "—" / "no known price" the GUI
  already shows) instead of ¥0.000. The user can give it a price with
  `magpie model price kimi-code/kimi-for-coding …` or in Names & levels.
- The alias is scoped to Kimi Code providers: a relay's own "k3" keeps its
  own catalog price; Moonshot pay-as-you-go's kimi-k3, an OpenCode free
  model ($0) and Codex's maker price are unchanged.
- PricedNameFor(provider, model): the Requests list's "API price
  reference" names kimi-k3 for a Kimi Code k3 (internal/gui/usage.go).
Docs: reference.md (What a model costs) and providers-accounts.md.

Tests: TestKimiCodeListPrice (internal/provider; fixture
testdata/kimi_code_catalog.json is models.dev's own moonshotai and Kimi Code
plan entries) and TestKimiCodeCost (internal/usage; the reporter's k3 row,
240K in / 78.4K out / 10.0M cached, counted at $4.896 in the ledger and the
summary, kimi-for-coding unpriced, moonshot/kimi-k3 unchanged). Both fail on
the old models.go (every Kimi Code model priced at $0, MakerPrice(k3) $0,
k3 cost=0, unpriced 0); PricedNameFor's check fails with the alias turned
off ("k3-256k").

Run: gofmt -l (empty); go vet -tags nogui, GOOS=linux go vet -tags nogui,
GOOS=windows go vet -tags nogui on internal/provider, internal/usage,
internal/gui; go build, GOOS=linux go build -tags nogui, GOOS=windows go
build; go test -tags nogui -count=1 ./... under a temp HOME with pinned
caches: all ok; -race -count=3 on the new tests ok. No GUI asset changed,
so the Playwright suite wasn't run. Not tried with a real Kimi Code
account or the reporter's usage log.
2026-10-09 11:48:41 +08:00

110 lines
4.1 KiB
Go

package provider
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// useKimiCodeCatalog puts testdata/kimi_code_catalog.json in the catalog
// cache: models.dev's own entries (2026-10-09) for moonshotai and both Kimi
// Code plans, which list k3, k3-256k, kimi-for-coding and
// kimi-for-coding-highspeed at $0, beside gpt-6-luna and a free OpenCode
// model; plus a relay's catalog that lists a "k3" of its own at $0.
func useKimiCodeCatalog(t *testing.T) {
t.Helper()
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"))
b, err := os.ReadFile(filepath.Join("testdata", "kimi_code_catalog.json"))
if err != nil {
t.Fatal(err)
}
var m map[string]any
if err := json.Unmarshal(b, &m); err != nil {
t.Fatal(err)
}
m["freebie"] = map[string]any{"id": "freebie", "models": map[string]any{
"k3": map[string]any{"id": "k3", "cost": map[string]any{"input": 0, "output": 0}}}}
b, _ = json.Marshal(m)
if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(catalog.CachePath(), b, 0o600); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
}
// #1370: a Kimi Code membership's models are priced at the Kimi API model
// each is, not at the $0 its plan's models.dev catalog lists, and
// kimi-for-coding (K2.8 Preview, not sold on the API) is unpriced rather
// than free. Every other provider's price is what it was.
func TestKimiCodeListPrice(t *testing.T) {
useKimiCodeCatalog(t)
k3 := catalog.Price{Input: 3, Output: 15, CacheRead: 0.3, CacheWrite: 3}
highspeed := catalog.Price{Input: 1.9, Output: 8, CacheRead: 0.38}
for _, preset := range []string{"kimi-code", "kimi-code-cn"} {
p, err := FromPreset(preset)
if err != nil {
t.Fatal(err)
}
for model, want := range map[string]catalog.Price{"k3": k3, "K3": k3, "k3-256k": k3, "kimi-for-coding-highspeed": highspeed} {
if pr, ok := p.ListPrice(model); !ok || !pr.Same(want) {
t.Errorf("%s %s: %+v %v, want %+v", preset, model, pr, ok, want)
}
}
if pr, ok := p.ListPrice("kimi-for-coding"); ok {
t.Errorf("%s kimi-for-coding priced at %+v: the API sells no K2.8 Preview", preset, pr)
}
}
// the API's own kimi-k3, at Kimi's pay-as-you-go endpoint, is unchanged
moon, _ := FromPreset("moonshot")
if pr, ok := moon.ListPrice("kimi-k3"); !ok || !pr.Same(k3) {
t.Errorf("moonshot kimi-k3: %+v %v", pr, ok)
}
// a relay's own k3 at $0 stays $0: the alias is Kimi Code's alone
relay := Provider{ID: "relay", Catalog: "freebie"}
if pr, ok := relay.ListPrice("k3"); !ok || !pr.Same(catalog.Price{}) {
t.Errorf("relay k3: %+v %v, want its own $0", pr, ok)
}
// a model listed free elsewhere is still free, and Codex's still at its maker's
zen := Provider{ID: "zen", Catalog: "opencode"}
if pr, ok := zen.ListPrice("qwen3.6-plus-free"); !ok || !pr.Same(catalog.Price{}) {
t.Errorf("opencode free model: %+v %v", pr, ok)
}
if pr, ok := (Provider{ID: "codex"}).ListPrice("gpt-6-luna"); !ok || pr.Input != 0.1 || pr.Output != 0.5 {
t.Errorf("codex gpt-6-luna: %+v %v", pr, ok)
}
// with the provider gone, a bare k3 has no maker price: no $0 either
if pr, ok := MakerPrice("k3"); ok {
t.Errorf("MakerPrice(k3) = %+v from a membership catalog", pr)
}
// the Requests list names the API model a k3 is priced as, a relay's k3 none
kc, _ := FromPreset("kimi-code-cn")
kc.Key = "sk-test"
relay.Name, relay.Key, relay.Chat = "Relay", "sk-test", "https://relay.example/v1"
for _, p := range []Provider{kc, relay} {
if err := Save(p); err != nil {
t.Fatal(err)
}
}
if got := PricedNameFor("kimi-code-cn", "k3-256k"); got != "kimi-k3" {
t.Errorf("PricedNameFor(kimi-code-cn, k3-256k) = %q", got)
}
if got := PricedNameFor("relay", "k3"); got != "k3" {
t.Errorf("PricedNameFor(relay, k3) = %q", got)
}
if got := PricedNameFor("codex", "codex-auto-review"); got != PricedName("codex-auto-review") {
t.Errorf("PricedNameFor(codex, codex-auto-review) = %q", got)
}
}