diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index f4acc236f..b7d283ca0 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -13,6 +13,7 @@ import ( "net/http" "os" "path/filepath" + "regexp" "slices" "sort" "strconv" @@ -979,6 +980,9 @@ type priceIndex struct { // canon and canonDashed are the price the vendor's own entries under a // canonical id (bare, and dashed) give, nil where they disagree canon, canonDashed map[string]*Price + // canonSnap is canonDashed with the canonical's snapshot date taken + // off (deepseek-v4.1-flash-1017 is deepseek-v4.1-flash) + canonSnap map[string]*Price } var priceIndexes struct { @@ -998,13 +1002,13 @@ func priceIndexOf(all map[string]mdProvider, at int, pid string) *priceIndex { if x, ok := priceIndexes.by[pid]; ok { return x } - x := &priceIndex{bare: map[string]string{}, dashed: map[string]string{}, canon: map[string]*Price{}, canonDashed: map[string]*Price{}} + x := &priceIndex{bare: map[string]string{}, dashed: map[string]string{}, canon: map[string]*Price{}, canonDashed: map[string]*Price{}, canonSnap: map[string]*Price{}} first := func(by map[string]string, k, key string) { if was, ok := by[k]; !ok || key < was { by[k] = key } } - seen := map[*map[string]*Price]map[string]bool{&x.canon: {}, &x.canonDashed: {}} + seen := map[*map[string]*Price]map[string]bool{&x.canon: {}, &x.canonDashed: {}, &x.canonSnap: {}} agree := func(by *map[string]*Price, k string, p *Price) { if !seen[by][k] { seen[by][k] = true @@ -1027,6 +1031,9 @@ func priceIndexOf(all map[string]mdProvider, at int, pid string) *priceIndex { c := bareID(m.Canonical) agree(&x.canon, c, m.Cost) agree(&x.canonDashed, dashed(c), m.Cost) + if sn := snapshotOf(dashed(c)); sn != dashed(c) { + agree(&x.canonSnap, sn, m.Cost) + } } priceIndexes.by[pid] = x return x @@ -1047,6 +1054,10 @@ func canonicallyPriced(all map[string]mdProvider, at int, providers []string, b for _, by := range []func(*priceIndex) (*Price, bool){ func(x *priceIndex) (*Price, bool) { p, ok := x.canon[b]; return p, ok }, func(x *priceIndex) (*Price, bool) { p, ok := x.canonDashed[dashed(b)]; return p, ok }, + // and as the snapshot it is: models.dev says DeepSeek's deepseek-flash + // is deepseek/deepseek-v4.1-flash-1017, and the relays serve it as + // deepseek-v4.1-flash (Open Design's, Winnie7572 on X) + func(x *priceIndex) (*Price, bool) { p, ok := x.canonSnap[dashed(b)]; return p, ok }, } { for _, pid := range providers { if p, ok := by(priceIndexOf(all, at, pid)); ok && p != nil { @@ -1168,6 +1179,14 @@ func makerPriced(all map[string]mdProvider, at int, prefer []string, id string) return Price{}, false } +// mmddSnapshot is the date a vendor ends a snapshot's id with: -0813, +// -1017 (month and day), -20261017 or -2026-10-17. +var mmddSnapshot = regexp.MustCompile(`-(?:(?:0[1-9]|1[0-2])(?:0[1-9]|[12][0-9]|3[01])|20[0-9]{2}(?:-?)(?:0[1-9]|1[0-2])(?:-?)(?:0[1-9]|[12][0-9]|3[01]))$`) + +// snapshotOf is an id without the date of its snapshot, as the model it is +// a snapshot of: deepseek-v4.1-flash-1017 is deepseek-v4.1-flash. +func snapshotOf(id string) string { return mmddSnapshot.ReplaceAllString(id, "") } + // dashed is an id with its dots as dashes: the spelling two ids are compared // in when one vendor writes a model's version 4.6 and another 4-6. func dashed(id string) string { return strings.ReplaceAll(id, ".", "-") } diff --git a/internal/provider/peak_test.go b/internal/provider/peak_test.go index af66a06df..c8edafbd2 100644 --- a/internal/provider/peak_test.go +++ b/internal/provider/peak_test.go @@ -290,8 +290,8 @@ func TestOllamaCloudOffPeakPrice(t *testing.T) { // commandCodeCatalog is models.dev's api.json as magpie had it on // 2026-10-30, these entries byte for byte (descriptions and the like left // out). DeepSeek lists V4.1 Flash only as deepseek-flash, by a dated -// canonical id, so no maker price is found for Command Code's undated -// deepseek/deepseek-v4.1-flash; Command Code's own catalog lists it. +// canonical id; Command Code's own catalog lists its undated +// deepseek/deepseek-v4.1-flash, which comes first for its plan. const commandCodeCatalog = `{ "deepseek":{"id":"deepseek","api":"https://api.deepseek.com","name":"DeepSeek","models":{ "deepseek-flash":{"id":"deepseek-flash","name":"DeepSeek Flash","cost":{"input":0.15,"output":0.6,"cache_read":0.003},"canonical_model_id":"deepseek/deepseek-v4.1-flash-1017"}, @@ -350,8 +350,10 @@ func TestCommandCodePlanPrice(t *testing.T) { t.Errorf("set at %s: %+v at peak, %+v off-peak", key, a, b) } } - // another provider's id says nothing of Command Code's catalog - if _, ok := (Provider{ID: "relay", Chat: "https://relay.example/v1"}).ListPrice("deepseek/deepseek-v4.1-flash"); ok { - t.Error("a relay took Command Code's list price") + // another provider's id says nothing of Command Code's catalog: a relay + // has its maker's price, DeepSeek's deepseek-flash, whose dated + // canonical id (deepseek-v4.1-flash-1017) is this model's snapshot + if pr, ok := (Provider{ID: "relay", Chat: "https://relay.example/v1"}).ListPrice("deepseek/deepseek-v4.1-flash"); !ok || pr.Input != 0.15 || pr.Output != 0.6 || pr.CacheRead != 0.003 { + t.Errorf("a relay's price: %+v %v; want DeepSeek's 0.15/0.6/0.003", pr, ok) } } diff --git a/internal/provider/relay_snapshot_price_test.go b/internal/provider/relay_snapshot_price_test.go new file mode 100644 index 000000000..0fcfddfdc --- /dev/null +++ b/internal/provider/relay_snapshot_price_test.go @@ -0,0 +1,48 @@ +package provider + +import ( + "os" + "path/filepath" + "testing" + + "github.com/yetone/magpie/internal/catalog" +) + +// A relay that serves DeepSeek's V4.1 Flash under the id the maker's +// canonical_model_id gives it, with a snapshot date on it in models.dev +// (deepseek-flash is deepseek/deepseek-v4.1-flash-1017), is priced at +// DeepSeek's list price: the Open Design plugin's deepseek-v4.1-flash showed +// no estimated cost (Winnie7572 on X). The ids are those the plugin lists +// (packages/opendesign models.mjs); testdata is models.dev's entries for them, +// as read 2026-11-13. +func TestRelayDeepSeekSnapshotCanonicalPriced(t *testing.T) { + claudeHome(t) + b, err := os.ReadFile("testdata/models_dev_opendesign.json") + if err != nil { + t.Fatal(err) + } + os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755) + os.WriteFile(catalog.CachePath(), b, 0o644) + catalog.Reset() + t.Cleanup(catalog.Reset) + for _, c := range []struct { + model string + input, outp float64 + }{ + {"deepseek-v4.1-flash", 0.15, 0.6}, + {"deepseek-v4-flash", 0.15, 0.6}, + {"deepseek-v4-flash-vision-exp", 0.15, 0.6}, + {"deepseek-v4-pro", 0.66, 1.98}, + } { + pr, ok := EffectivePrice("opendesign", c.model) + if !ok || pr.Input != c.input || pr.Output != c.outp { + t.Errorf("opendesign/%s: %+v, %v; want DeepSeek's $%v/$%v", c.model, pr, ok, c.input, c.outp) + } + } + // another vendor's id that only looks alike stays unpriced + for _, m := range []string{"deepseek-v4.1-flash-0000", "other-v4.1-flash", "deepseek-v4.1-flash-turbo"} { + if pr, ok := EffectivePrice("opendesign", m); ok { + t.Errorf("opendesign/%s priced %+v", m, pr) + } + } +} diff --git a/internal/provider/testdata/models_dev_opendesign.json b/internal/provider/testdata/models_dev_opendesign.json new file mode 100644 index 000000000..09fe163a3 --- /dev/null +++ b/internal/provider/testdata/models_dev_opendesign.json @@ -0,0 +1 @@ +{"deepseek": {"id": "deepseek", "models": {"deepseek-v4-pro": {"id": "deepseek-v4-pro", "cost": {"input": 0.66, "output": 1.98, "reasoning": 1.98, "cache_read": 0.022}, "canonical_model_id": "deepseek/deepseek-v4-pro-0813"}, "deepseek-flash": {"id": "deepseek-flash", "cost": {"input": 0.15, "output": 0.6, "reasoning": 0.6, "cache_read": 0.003}, "canonical_model_id": "deepseek/deepseek-v4.1-flash-1017"}, "deepseek-v4-flash-vision-exp": {"id": "deepseek-v4-flash-vision-exp", "cost": {"input": 0.15, "output": 0.6, "reasoning": 0.6, "cache_read": 0.003}, "canonical_model_id": "deepseek/deepseek-v4.1-flash-1017"}, "deepseek-v4-flash": {"id": "deepseek-v4-flash", "cost": {"input": 0.15, "output": 0.6, "reasoning": 0.6, "cache_read": 0.003}, "canonical_model_id": "deepseek/deepseek-v4.1-flash-1017"}}}, "anthropic": {"id": "anthropic", "models": {"claude-sonnet-5": {"id": "claude-sonnet-5", "cost": {"input": 2, "output": 10, "cache_read": 0.2, "cache_write": 2.5}, "canonical_model_id": null}, "claude-haiku-4-5": {"id": "claude-haiku-4-5", "cost": {"input": 1, "output": 5, "cache_read": 0.1, "cache_write": 1.25}, "canonical_model_id": "anthropic/claude-haiku-4-5"}, "claude-sonnet-5-5": {"id": "claude-sonnet-5-5", "cost": {"input": 2, "output": 10, "cache_read": 0.1, "cache_write": 2.5}, "canonical_model_id": "anthropic/claude-sonnet-5-5"}, "claude-opus-5-5": {"id": "claude-opus-5-5", "cost": {"input": 4, "output": 20, "cache_read": 0.2, "cache_write": 5}, "canonical_model_id": "anthropic/claude-opus-5-5"}, "claude-haiku-5-5": {"id": "claude-haiku-5-5", "cost": {"input": 0.1, "output": 0.5, "cache_read": 0.01, "cache_write": 0.125, "tiers": [{"input": 0.5, "output": 2.5, "cache_read": 0.05, "cache_write": 0.625, "tier": {"type": "context", "size": 100000}}]}, "canonical_model_id": "anthropic/claude-haiku-5-5"}, "claude-fable-5": {"id": "claude-fable-5", "cost": {"input": 10, "output": 50, "cache_read": 1, "cache_write": 12.5}, "canonical_model_id": null}, "claude-opus-4-8": {"id": "claude-opus-4-8", "cost": {"input": 5, "output": 25, "cache_read": 0.5, "cache_write": 6.25}, "canonical_model_id": null}, "claude-fable-5-2": {"id": "claude-fable-5-2", "cost": {"input": 8, "output": 40, "cache_read": 0.2, "cache_write": 10}, "canonical_model_id": "anthropic/claude-fable-5-2"}, "claude-sonnet-4-5": {"id": "claude-sonnet-4-5", "cost": {"input": 3, "output": 15, "cache_read": 0.3, "cache_write": 3.75}, "canonical_model_id": "anthropic/claude-sonnet-4-5"}, "claude-opus-4-7": {"id": "claude-opus-4-7", "cost": {"input": 5, "output": 25, "cache_read": 0.5, "cache_write": 6.25}, "canonical_model_id": null}, "claude-opus-4-6": {"id": "claude-opus-4-6", "cost": {"input": 5, "output": 25, "cache_read": 0.5, "cache_write": 6.25}, "canonical_model_id": null}, "claude-sonnet-4-5-20250929": {"id": "claude-sonnet-4-5-20250929", "cost": {"input": 3, "output": 15, "cache_read": 0.3, "cache_write": 3.75}, "canonical_model_id": null}, "claude-haiku-4-5-20251001": {"id": "claude-haiku-4-5-20251001", "cost": {"input": 1, "output": 5, "cache_read": 0.1, "cache_write": 1.25}, "canonical_model_id": null}, "claude-opus-4-5-20251101": {"id": "claude-opus-4-5-20251101", "cost": {"input": 5, "output": 25, "cache_read": 0.5, "cache_write": 6.25}, "canonical_model_id": null}, "claude-opus-4-5": {"id": "claude-opus-4-5", "cost": {"input": 5, "output": 25, "cache_read": 0.5, "cache_write": 6.25}, "canonical_model_id": "anthropic/claude-opus-4-5"}, "claude-sonnet-4-6": {"id": "claude-sonnet-4-6", "cost": {"input": 3, "output": 15, "cache_read": 0.3, "cache_write": 3.75}, "canonical_model_id": null}, "claude-fable-5-1": {"id": "claude-fable-5-1", "cost": {"input": 10, "output": 50, "cache_read": 0.25, "cache_write": 12.5}, "canonical_model_id": "anthropic/claude-fable-5-1"}, "claude-opus-5": {"id": "claude-opus-5", "cost": {"input": 5, "output": 25, "cache_read": 0.5, "cache_write": 6.25}, "canonical_model_id": "anthropic/claude-opus-5"}}}, "zhipuai": {"id": "zhipuai", "models": {"glm-4.6v-flash": {"id": "glm-4.6v-flash", "cost": {"input": 0, "output": 0, "cache_read": 0, "cache_write": 0}, "canonical_model_id": "zhipuai/glm-4.6v-flash"}, "glm-4.7": {"id": "glm-4.7", "cost": {"input": 0.44819, "output": 2.09153, "cache_read": 0.08964, "cache_write": 0, "tiers": [{"input": 0.59758, "output": 2.39032, "cache_read": 0.11952, "cache_write": 0, "tier": {"type": "context", "size": 32000}}]}, "canonical_model_id": "zhipuai/glm-4.7"}, "glm-5.2": {"id": "glm-5.2", "cost": {"input": 1.19516, "output": 4.18306, "cache_read": 0.29879, "cache_write": 0}, "canonical_model_id": "zhipuai/glm-5.2"}, "glm-4.7-flashx": {"id": "glm-4.7-flashx", "cost": {"input": 0.0747, "output": 0.44819, "cache_read": 0.01494, "cache_write": 0}, "canonical_model_id": "zhipuai/glm-4.7-flashx"}, "glm-5.3-flashx": {"id": "glm-5.3-flashx", "cost": {"input": 0.29879, "output": 1.04577, "cache_read": 0.08516, "cache_write": 0}, "canonical_model_id": "zhipuai/glm-5.3-flash"}, "glm-5.3": {"id": "glm-5.3", "cost": {"input": 1.19516, "output": 4.18306, "cache_read": 0.29879, "cache_write": 0}, "canonical_model_id": "zhipuai/glm-5.3"}, "glm-4.5-flash": {"id": "glm-4.5-flash", "cost": {"input": 0, "output": 0, "cache_read": 0, "cache_write": 0}, "canonical_model_id": "zhipuai/glm-4.5-flash"}, "glm-5v-turbo": {"id": "glm-5v-turbo", "cost": {"input": 0.74698, "output": 3.28669, "cache_read": 0.17927, "cache_write": 0, "tiers": [{"input": 1.04577, "output": 3.88427, "cache_read": 0.26891, "cache_write": 0, "tier": {"type": "context", "size": 32000}}]}, "canonical_model_id": "zhipuai/glm-5v-turbo"}, "glm-4.5": {"id": "glm-4.5", "cost": {"input": 0.6, "output": 2.2, "cache_read": 0.11, "cache_write": 0}, "canonical_model_id": "zhipuai/glm-4.5"}, "glm-4.6v": {"id": "glm-4.6v", "cost": {"input": 0.1494, "output": 0.44819, "cache_read": 0.02988, "cache_write": 0, "tiers": [{"input": 0.29879, "output": 0.89637, "cache_read": 0.05976, "cache_write": 0, "tier": {"type": "context", "size": 32000}}]}, "canonical_model_id": "zhipuai/glm-4.6v"}, "glm-4.5-air": {"id": "glm-4.5-air", "cost": {"input": 0.11952, "output": 0.89637, "cache_read": 0.0239, "cache_write": 0, "tiers": [{"input": 0.17927, "output": 1.19516, "cache_read": 0.03585, "cache_write": 0, "tier": {"type": "context", "size": 32000}}]}, "canonical_model_id": "zhipuai/glm-4.5-air"}, "glm-4.5v": {"id": "glm-4.5v", "cost": {"input": 0.29879, "output": 0.89637, "cache_read": 0.05976, "cache_write": 0, "tiers": [{"input": 0.59758, "output": 1.79274, "cache_read": 0.11952, "cache_write": 0, "tier": {"type": "context", "size": 32000}}]}, "canonical_model_id": "zhipuai/glm-4.5v"}, "glm-4.6": {"id": "glm-4.6", "cost": {"input": 0.6, "output": 2.2, "cache_read": 0.11, "cache_write": 0}, "canonical_model_id": "zhipuai/glm-4.6"}, "glm-5.1": {"id": "glm-5.1", "cost": {"input": 0.89637, "output": 3.58548, "cache_read": 0.19421, "cache_write": 0, "tiers": [{"input": 1.19516, "output": 4.18306, "cache_read": 0.29879, "cache_write": 0, "tier": {"type": "context", "size": 32000}}]}, "canonical_model_id": "zhipuai/glm-5.1"}, "glm-5.3-flash": {"id": "glm-5.3-flash", "cost": {"input": 0.11952, "output": 0.41831, "cache_read": 0.03436, "cache_write": 0}, "canonical_model_id": "zhipuai/glm-5.3-flash"}, "glm-4.7-flash": {"id": "glm-4.7-flash", "cost": {"input": 0, "output": 0, "cache_read": 0, "cache_write": 0}, "canonical_model_id": "zhipuai/glm-4.7-flash"}, "glm-5": {"id": "glm-5", "cost": {"input": 0.59758, "output": 2.68911, "cache_read": 0.1494, "cache_write": 0, "tiers": [{"input": 0.89637, "output": 3.28669, "cache_read": 0.22409, "cache_write": 0, "tier": {"type": "context", "size": 32000}}]}, "canonical_model_id": "zhipuai/glm-5"}}}, "moonshotai": {"id": "moonshotai", "models": {"kimi-k2.7-code": {"id": "kimi-k2.7-code", "cost": {"input": 0.95, "output": 4, "cache_read": 0.19}, "canonical_model_id": "moonshotai/kimi-k2.7-code"}, "kimi-k2.6": {"id": "kimi-k2.6", "cost": {"input": 0.95, "output": 4, "cache_read": 0.16}, "canonical_model_id": "moonshotai/kimi-k2.6"}, "kimi-k2.7-code-highspeed": {"id": "kimi-k2.7-code-highspeed", "cost": {"input": 1.9, "output": 8, "cache_read": 0.38}, "canonical_model_id": "moonshotai/kimi-k2.7-code"}, "kimi-k3.1-pro-highspeed": {"id": "kimi-k3.1-pro-highspeed", "cost": {"input": 4, "output": 20, "cache_read": 0.2, "cache_write": 4}, "canonical_model_id": "moonshotai/kimi-k3.1-pro"}, "kimi-k3": {"id": "kimi-k3", "cost": {"input": 3, "output": 15, "cache_read": 0.3, "cache_write": 3}, "canonical_model_id": "moonshotai/kimi-k3"}, "kimi-k3.1-pro": {"id": "kimi-k3.1-pro", "cost": {"input": 2, "output": 10, "cache_read": 0.2, "cache_write": 2}, "canonical_model_id": "moonshotai/kimi-k3.1-pro"}}}, "xiaomi": {"id": "xiaomi", "models": {"mimo-v2.5-pro": {"id": "mimo-v2.5-pro", "cost": {"input": 0.435, "output": 0.87, "cache_read": 0.0036}, "canonical_model_id": "xiaomi/mimo-v2.5-pro"}, "mimo-v2.5-pro-ultraspeed": {"id": "mimo-v2.5-pro-ultraspeed", "cost": {"input": 1.305, "output": 2.61, "cache_read": 0.0108}, "canonical_model_id": "xiaomi/mimo-v2.5-pro-ultraspeed"}, "mimo-v2.6-pro": {"id": "mimo-v2.6-pro", "cost": {"input": 0.435, "output": 0.87, "cache_read": 0.0036}, "canonical_model_id": "xiaomi/mimo-v2.6-pro"}, "mimo-v2.5": {"id": "mimo-v2.5", "cost": {"input": 0.14, "output": 0.28, "cache_read": 0.0028}, "canonical_model_id": "xiaomi/mimo-v2.5"}, "mimo-v2.6-flash": {"id": "mimo-v2.6-flash", "cost": {"input": 0.14, "output": 0.28, "cache_read": 0.0028}, "canonical_model_id": "xiaomi/mimo-v2.6-flash"}, "mimo-v2.6-pro-ultraspeed": {"id": "mimo-v2.6-pro-ultraspeed", "cost": {"input": 4.35, "output": 8.7, "cache_read": 0.036}, "canonical_model_id": "xiaomi/mimo-v2.6-pro-ultraspeed"}}}, "minimax": {"id": "minimax", "models": {"MiniMax-M3": {"id": "MiniMax-M3", "cost": {"input": 0.3, "output": 1.2, "cache_read": 0.06, "tiers": [{"input": 0.6, "output": 2.4, "cache_read": 0.12, "tier": {"type": "context", "size": 512000}}], "context_over_200k": {"input": 0.6, "output": 2.4, "cache_read": 0.12}}, "canonical_model_id": null}, "MiniMax-M2.7": {"id": "MiniMax-M2.7", "cost": {"input": 0.3, "output": 1.2, "cache_read": 0.06, "cache_write": 0.375}, "canonical_model_id": null}, "MiniMax-M2": {"id": "MiniMax-M2", "cost": {"input": 0.3, "output": 1.2}, "canonical_model_id": null}, "MiniMax-M2.1": {"id": "MiniMax-M2.1", "cost": {"input": 0.3, "output": 1.2, "cache_read": 0.03, "cache_write": 0.375}, "canonical_model_id": null}, "MiniMax-M2.7-highspeed": {"id": "MiniMax-M2.7-highspeed", "cost": {"input": 0.6, "output": 2.4, "cache_read": 0.06, "cache_write": 0.375}, "canonical_model_id": null}, "MiniMax-M2.5-highspeed": {"id": "MiniMax-M2.5-highspeed", "cost": {"input": 0.6, "output": 2.4, "cache_read": 0.03, "cache_write": 0.375}, "canonical_model_id": null}, "MiniMax-M2.5": {"id": "MiniMax-M2.5", "cost": {"input": 0.3, "output": 1.2, "cache_read": 0.03, "cache_write": 0.375}, "canonical_model_id": null}, "MiniMax-M3.1-Flash": {"id": "MiniMax-M3.1-Flash", "cost": {"input": 0.15, "output": 0.6, "cache_read": 0.015}, "canonical_model_id": "minimax/MiniMax-M3.1-Flash"}}}} \ No newline at end of file