catalog, provider: a model only models.dev's lab lists is known, and a Claude account lists the gated models Anthropic told its Claude Code of (iebb on Discord)
iebb on Discord (10-27): his account has Claude Mythos 5.1, magpie doesn't list it. models.dev took the gated Mythos 5.1 out of anthropic's provider list (models.dev #11327) and kept only its lab file, models/anthropic/claude-mythos-5-1.toml (#11328). Cause. magpie read only models.dev's api.json (providers -> models). The Claude subscription's list was exactly catalog.Provider("anthropic"), so Mythos 5.1 left it with that PR. And a vendor list that still returns it (an Anthropic key's /v1/models) got no window, reply limit, name or thinking for it, since nothing read the lab catalog. Fix. - catalog: Sync also keeps models.dev's lab catalog (models.json) as labs.json beside the cache, through temp+rename; a failed fetch keeps the last one, and a cache without it is Stale once a process. A model only a lab lists votes into the same tables as a listed one (ContextOf, OutputOf, NameOf, Thinks, SeesImages); a model a provider lists keeps what providers say. A thinking lab-only model with no levels takes those of the newest of its family a provider gives (labKinEfforts: Mythos 5.1 takes Mythos 5's). catalog.Describe builds a Model from an id for lists that name ids alone. - provider: the Claude account's list is models.dev's anthropic list plus what Anthropic told the account's Claude Code beyond it, read from .claude.json's additionalModelOptionsCache / modelAccessCache / additionalModelCostsCache (Claude Code's own sign-in and each saved account's directory). Account.Lists honours modelAccessCache's entitled per account, ListPrice falls back to Anthropic's price from additionalModelCostsCache, and the agent's own Claude Code picker (claudeOwn) offers the signed-in account's extras. WSL keeps the catalog list. magpie still asks Anthropic nothing with a Claude sign-in; nothing is sent differently, no Claude Code mimicry change. - docs/subsystems/providers-accounts.md: two rows, the lab catalog and a Claude account's models. Testdata: models.dev's api.json and models.json as served on 10-27, cut to the anthropic and azure providers' Claude models and the anthropic lab. Tests never reach the network. Run: - TestLabOnlyModelIsKnown, TestLabsLeaveListedModels, TestSyncKeepsLabs, TestClaudeAccountListsGatedModels, TestAnthropicKeyGatedModelDescribed: pass. - go test -count=1 -timeout 45m ./internal/catalog/ ./internal/provider/ ./internal/agent/ ./internal/gateway/ at2e165fe9d+ this change: catalog, agent (362s), gateway (557s) ok; provider crashed in TestUnreadLoginsWriteErrorsKeepCredentials ("close of synctest channel from outside bubble" in filememo.Forget). It crashes the same alone on a fresh origin/main19dc5b925and is #3632, open. - rebased onto origin/main2afacd7d1(this commit's parent): catalog and provider again with -skip TestUnreadLoginsWriteErrorsKeepCredentials, both ok. agent and gateway were not run again after the rebase. - gofmt -l internal/: nothing. go vet for darwin, linux and windows on catalog, provider, agent, gateway at the rebased head: ok. - Real use: magpie built from this tree (linux, nogui) in a golang:1.26.8 container with a sandbox HOME, so the Mac's Keychain and ~/.claude are never read. `magpie sync` fetched models.dev's real catalogs today (api.json's anthropic has no claude-mythos-5-1; labs.json has it). With a fake Claude sign-in and a .claude.json whose bootstrap caches name claude-mythos-5-1, `magpie models` lists claude/claude-mythos-5-1 "Mythos 5.1" low/medium/high/xhigh; `magpie model spec` gives 1M, 128k, vision on; `magpie model price` gives $25/$125, cache read $2.5, write $31.25 / $50 (1h). A custom Anthropic-protocol provider against a fake /v1/models returning claude-mythos-5-1 lists fakeanth/claude-mythos-5-1 "Claude Mythos 5.1" with 1M / 128k / low..xhigh. Broken by hand, each red: - lab reading off in loadGen: every lab assertion in the catalog tests and the provider tests (no window, no levels, the API-key entry's context 0). - syncLabs off: TestSyncKeepsLabs. - labKinEfforts off: the efforts assertions. - the Claude list back to catalog.Provider("anthropic"): "Claude account's list has no Mythos 5.1". - claudeLists / claudeCodePrice off: the entitlement and price assertions. Not tried: - a real account with Mythos 5.1: none here. The .claude.json keys and shapes were read from Claude Code 2.1.311's own binary, not a real account's file. - a custom Anthropic-protocol provider without catalog=anthropic gets no price for a lab-only model (labs carry none). - the plugin host's mdev() (host.js) reads api.json only; Gemini CLI's own picker stays catalog-only; a saved Claude account whose Claude Code never ran has no .claude.json, so its gated models show once it has run there.
This commit is contained in:
@@ -52,6 +52,8 @@ keys and accounts per request is in [Gateway routing and fallback](gateway-routi
|
||||
| An endpoint's catalog | A key's provider with no catalog of its own (none named, none from a preset) is priced from the models.dev catalog whose `api` is one of its endpoints (`EndpointCatalog`, `catalog.AtEndpoint`; a trailing slash and case don't count, and an Anthropic base, kept as its root, is also matched with its `/v1`). Only when exactly one catalog is at that address, and it isn't a plan's (an id with `-plan`, or a `membershipCatalogs` one): a plan lists its membership's $0, and two catalogs at one address can't be told apart. A subscription's account never gets one. A provider at AMD's own endpoint is priced from `amd`, which lists Qwen3.8-27B at $0, a price, where its maker sells no such model and it had none (#2052); a relay's endpoint matches nothing and its model stays unpriced, never priced at a reseller's guess. `magpie model price` names the catalog, and says how to give a price when none is known. | [`models.go`](../../internal/provider/models.go), [`catalog.go`](../../internal/catalog/catalog.go) |
|
||||
| A membership's $0 catalog | models.dev lists every model of Kimi Code's plans (`kimi-code-plan-global`, `kimi-code-plan-cn`) and of Xiaomi MiMo's Token Plans (`xiaomi-token-plan-cn`, `-sgp`, `-ams`) at $0: what the membership charges per token, not what the model costs. `membershipCatalogs` are never read for a price (`priceCatalogs`, `makerPriceCatalogs`), so a Xiaomi MiMo model is priced at `xiaomi`'s list price at every region, and each region of the preset names its own catalog, pay as you go `xiaomi` alone (#1873). A Kimi Code provider's model is priced at the Kimi API model it is (`kimiCodeAPI`, from Kimi Code's docs: k3 and k3-256k are kimi-k3, kimi-for-coding-highspeed is kimi-k2.7-code-highspeed), and kimi-for-coding (K2.8 Preview, not on the API) is unpriced rather than free (#1370). The alias is Kimi Code's alone (`kimiCodePlans`): a relay's own `k3` keeps its own price. A catalog other than these that lists a model at $0 still prices it at $0: models.dev has truly free models (nvidia, modelscope). The Requests list names the API model it was priced as (`PricedNameFor`). | [`models.go`](../../internal/provider/models.go) |
|
||||
| A plugin's own price | A model no catalog prices (models.dev has no vendor's own model: Devin's SWE-2 and SWE-1.7 Lightning, Cursor's auto, Qoder's efficient) is priced at what its plugin lists it at (`ListPrice` → `pluginListPrice`, `plugin.Model.Cost`, read by `pluginPrice`: an input or output above 0, OpenCode's 0 being none), else at $0 when the plugin says the plan serves it free (`pluginFree`, `plugin.Model.Free`: Devin's Free cost tier, Qoder's and WorkBuddy's x0). Both come after every catalog, so a price a catalog gives is kept over the plugin's. Devin's plugin (0.1.18) lists each paid model at the price Devin's GetCliModelConfigs gives it (input, cached input and output per 1M tokens); a moved Devin user's calls on Devin's own models had no cost on Usage before (太陽と向日葵 on Discord, `TestPluginFreeModelCostsNothing`). A plugin that gives no price and doesn't say free leaves the model unpriced. | [`models.go`](../../internal/provider/models.go), [`plugins.go`](../../internal/provider/plugins.go), [`providers.go`](../../internal/plugin/providers.go) |
|
||||
| A lab's models | models.dev keeps two catalogs: `api.json`, each provider's models with that host's prices, and `models.json`, each lab's models with only what is the model's on every host (name, window, reply limit, thinking, inputs, release), no price. `Sync` keeps the second beside the first as `labs.json` (`LabsPath`, `syncLabs`: best effort, through a rename, a failed fetch keeping the last; a catalog with none beside it is `Stale` once a process, so it comes at the next look). A model only a lab lists, no provider (models.dev took the gated Claude Mythos 5.1 out of anthropic's list in #11327 and kept its lab file), is voted into the same tables as a listed one (`loadGen`, `labs`), so `ContextOf`, `OutputOf`, `NameOf`, `Thinks`, `SeesImages` and every entry built on them know it wherever a vendor's or an account's list has it; a thinking one with no levels takes the levels of the newest of its family a provider gives them (`labKinEfforts`: Mythos 5.1 takes Mythos 5's). A model a provider lists keeps what providers say of it. `catalog.Describe` builds a model from these for a list that names an id alone. `TestLabOnlyModelIsKnown`, `TestLabsLeaveListedModels`, `TestSyncKeepsLabs`, `TestAnthropicKeyGatedModelDescribed` (iebb on Discord). | [`labs.go`](../../internal/catalog/labs.go), [`catalog.go`](../../internal/catalog/catalog.go) |
|
||||
| A Claude account's models | magpie never asks Anthropic with a Claude sign-in, so a Claude account's list is models.dev's anthropic list and what Anthropic told the account's Claude Code beyond it. Claude Code keeps that in its `.claude.json` from its own bootstrap: `additionalModelOptionsCache` (models its /model picker offers beyond its own; a `null` value or a `disabled` one is not a model), `modelAccessCache` (`entitled` per API id) and `additionalModelCostsCache` (Anthropic's prices). `claudeModels` reads the file of Claude Code's own sign-in (`claudeProfilePath`) and of each saved account's directory (`claudeAccountDir`), through `filememo`, and adds each model none of them lists, described by `catalog.Describe` and priced as Anthropic told it. `Account.Lists` for a Claude account goes by its own file (`claudeLists`: the saved directory's, or Claude Code's own when its `oauthAccount` is that account, the one Anthropic answered last): a model it isn't entitled to is out, so routing tries the accounts entitled to a gated model first; with no file, every model is taken to be in, as before. `ListPrice` of a provider whose catalogs include anthropic falls back to that price after every catalog (`claudeCodePrice`). The agent's own Claude Code model picker (`claudeOwn`) offers what `ClaudeCodeModels` gives for the account signed in here; a WSL distro's keeps the catalog's. Not done: a Claude Code that never started on a saved account has no file, so its gated models show only once it has run there. `TestClaudeAccountListsGatedModels`. | [`claude_code_models.go`](../../internal/provider/claude_code_models.go), [`account.go`](../../internal/provider/account.go), [`codex_models.go`](../../internal/provider/codex_models.go), [`claude.go`](../../internal/agent/claude.go) |
|
||||
| A key's own windows | A sub2api key given a spending limit per 5 hours, day or 7 days answers its `/v1/usage` with `rate_limits` and no `remaining` (`readSub2APIKeyLimits`). With the Balance URL set to it, the key's card shows them as the windows *5 hours*, *1 day*, *7 days*, in USD, beside a balance field the user wrote; *Check balance* and `magpie provider show` give what its fullest window has left (`Balance`). A Balance URL at an account query left without its token (a sub2api panel's `/api/v1/user/profile`, new-api's `/api/user/self`, after the token was removed) asks the same panel's query for the key instead, the saved URL untouched (`keyQueryOf`, `BalanceQuery`), unless the user's Headers carry an Authorization or Cookie of their own. A Balance URL of the user's own is asked with GET, or with POST and no body when the provider's `BalanceMethod` says so (#2368: an endpoint that answers only a POST; the editor's GET/POST beside the URL, `balance.method=` on the command line); any other method is refused before anything is sent (`balanceMethodOf`). `KeyAllowance` gives routing the same windows: never waited for, read in the background at most once a minute, a failed read keeping the last, and seeded from the card's last reading (`quotas.json`) after a restart. A key bought as a plan (Kimi Code, GLM Coding Plan and Z.ai, MiniMax Coding Plan, OpenCode Go, Command Code, Ollama Cloud: `planQuotaSourceOf`) gives routing its card's windows the same way (`planKeyWindows`, Zhipu's team quota behind a key with none of its own); the Usage page's read of its card is taken at once (`noteKeyAllowance`), and a key there that turns out pay-as-you-go, with no windows, is asked again only every 30 minutes (`keyNoWindowsAge`). A Command Code plan's key has its month of credits as a *Credits* window beside the 5-hour and weekly ones, as the signed-in subscription's card has (`cmdKeyWindows`, #2451): billing/credits leaves the plan out, so it is the key's billing/subscriptions plan, asked at most every ten minutes and the last one kept while it can't be read; a key whose plan was never read has its two windows alone. Ollama Cloud's `ollama.com/api/balance` (#1888, `readOllamaPlan`) tells the plan's included allowance as one window in USD, *Month* when its period is a month long, renewing at `period.until`, and Aside while credits bought on top are left; those credits are the key's balance card (`readOllamaCredits`), and a reply that names none has no balance card rather than $0. StepFun's Step Plan is read through a platform sign-in, not the key, and isn't one of them: the sign-in is the provider's own, kept by provider id in `stepfun-auth.json` (two StepFun providers on one site can be two accounts, #2384), one card per signed-in provider; a file from before that, kept by site, is given on read to the StepFun providers on that site (`stepfunBySite`), and copies of one sign-in are renewed together. Nor are Volcengine Ark's Coding Plan and Agent Plan (#1427): their 5-hour, weekly and monthly windows are told only to the account's access key (`AccessKeyID`, `SecretAccessKey`), asked of `open.volcengineapi.com` (`GetCodingPlanUsage`, `GetAFPUsage`) and signed as Volcengine's own SDK signs (`volcSign`), so they are cards on the Usage page (`volcPlanQuotas` in `PlanQuotas`), never routing's windows. Nor is Tencent Cloud's Token Plan 企业版 (#1574): what a team plan has used of its cycle's credits (专业套餐) or tokens (轻享套餐) is told only to the account's SecretId and SecretKey (kept as `AccessKeyID`, `SecretAccessKey`), asked of `tokenhub.tencentcloudapi.com` (`DescribeTokenPlanList`, `DescribeTokenPlan`, version 2026-03-22) and signed TC3-HMAC-SHA256 (`tc3Sign`), one card per team plan (`tencentPlanQuotas`). | [`sub2api_usage.go`](../../internal/provider/sub2api_usage.go), [`key_allowance.go`](../../internal/provider/key_allowance.go), [`volcengine_usage.go`](../../internal/provider/volcengine_usage.go), [`tencent_usage.go`](../../internal/provider/tencent_usage.go) |
|
||||
| Account names and seats | `AccountNames` (by the account, in lower case, like `AccountCaps`) are names the user gives accounts, set by `SetAccountName` (GUI `provider/accountname`, `magpie accounts alias`); they live in providers.json only, never in a vendor's or plugin's sign-in. A GLM Coding Plan read with a key (`glmPlan`) and a ZCode account (built-in or `zcode-plugin`) whose windows' resets agree are one seat (`seatMatch`, #90/#953), shown once, on the account's card (`seatCards`, used by `quotas`, `Allotted` and `CachedCards`). A match is remembered in `seats.json` beside `quotas.json`: a failed read or a window with no reset yet is unknown and keeps the key's card off while the account is signed in; windows that disagree forget it (#1515). Cards carry `Seat` (the key provider's name, and the key's when it has several) and `Alias` (the user's name, else `Seat`), as `/v1/magpie/quotas`, `magpie quota` and `magpie accounts` do. Records (a trace's `who`, usage's `ProviderAccount`) keep the vendor's name, the key the user's name is kept by, and every list of them looks the name up as it is shown (`AccountShown`, `AccountShownOf`, `AccountRef.Shown`), so a request from before a rename reads by the new name too: the Routing page's accounts and requests (`userShown` in `plugins/routing/client.js`, from `/api/providers`' `accountNames`), its Several accounts pools (`onOf`), the Usage page's Accounts and the request log's Account filter (`usageapi`), `magpie usage`, and a gateway key's accounts (`AccountNames`, `/api/caller-keys`). Before plugins#310 (a renamed Kiro account read by its old name on every request after) those showed the vendor's name. | [`account_names.go`](../../internal/provider/account_names.go), [`account_seats.go`](../../internal/provider/account_seats.go) |
|
||||
| Last served | `served.json` records when each account, plan or key last answered, shown as `lastServedAt` (#570) on `/v1/magpie/quotas` and on the Usage page's own cards (`Quotas` → `cardsServed`, this computer's cards only, the caches left alone). The Usage page and the tray panel show the last-answered account in full, and a menu bar card that follows the account in use (`codex|*`) takes the one that answered within the last hour, else `InUseLogin`'s guess, else the one that answered last at all (a plugin's accounts, #1516) (`trayInUseCard`; okingkee on X: A spent, B answering, the menu bar stayed on A). `NoteServed` calls `OnServedMoved` when a provider's answers move to another account or key, and `SwitchLogin` after a switch, so the menu bar reads its cards again at once. | [`served.go`](../../internal/provider/served.go), [`trayusage.go`](../../internal/gui/trayusage.go) |
|
||||
|
||||
@@ -1662,7 +1662,7 @@ func claudeIn(at place) *Agent {
|
||||
}
|
||||
return env(tierEnv(t))
|
||||
}
|
||||
own := claudeOwn(cur["model"], tier)
|
||||
own := claudeOwn(cur["model"], tier, provider.ClaudeCodeModels())
|
||||
for i := range own {
|
||||
own[i].Direct = direct
|
||||
}
|
||||
@@ -2268,14 +2268,15 @@ func claudeDropModelEffort(path, n string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// claudeOwn is Anthropic's models as Claude Code takes them: only the
|
||||
// catalog's; Claude Code's own short aliases are not something any API
|
||||
// lists, and a compiled-in copy would just go stale. One the user's
|
||||
// claudeOwn is Anthropic's models as Claude Code takes them, ms: the
|
||||
// catalog's, and on this computer the ones Anthropic told Claude Code
|
||||
// its account has beyond them (provider.ClaudeCodeModels: Claude Mythos
|
||||
// 5.1); Claude Code's own short aliases are not something any API lists,
|
||||
// and a compiled-in copy would just go stale. One the user's
|
||||
// settings.json names (cur: "model": "sonnet") comes first, said as the
|
||||
// model it stands for, so the row doesn't show a bare word; tier is the
|
||||
// model the user's env gives a tier, "" for none.
|
||||
func claudeOwn(cur string, tier func(string) string) []Option {
|
||||
ms := catalog.Provider("anthropic")
|
||||
func claudeOwn(cur string, tier func(string) string, ms []catalog.Model) []Option {
|
||||
var own []Option
|
||||
if o, ok := claudeAliasOption(cur, ms, tier); ok {
|
||||
own = append(own, o)
|
||||
|
||||
@@ -349,7 +349,7 @@ var wslKinds = []wslKind{
|
||||
case "model":
|
||||
// whether it has a base URL of its own is the distro's file
|
||||
return func(cur map[string]string) []Option {
|
||||
return append(group("Claude Code", claudeOwn(cur["model"], nil)), claudeViaMagpie(false)...)
|
||||
return append(group("Claude Code", claudeOwn(cur["model"], nil, catalog.Provider("anthropic"))), claudeViaMagpie(false)...)
|
||||
}
|
||||
case "effort":
|
||||
return nil
|
||||
|
||||
@@ -469,6 +469,9 @@ var (
|
||||
// listed are the bare ids of every model models.dev lists, whatever it
|
||||
// says of them: an id listed is never taken for another (renamed)
|
||||
listed map[string]bool
|
||||
// labs are the models models.dev lists under their lab alone
|
||||
// (models.json), by bare id: none of its providers lists them
|
||||
labs map[string]mdModel
|
||||
// canonical are the bare ids of every model models.dev says an entry is
|
||||
// (canonical_model_id, #1520): DeepSeek's deepseek-flash is
|
||||
// deepseek/deepseek-v4.1-flash, the id WorkBuddy and the relays serve it
|
||||
@@ -602,6 +605,31 @@ func loadGen() (map[string]mdProvider, int) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// and a model no provider lists at all, only its lab
|
||||
// (models.dev's models.json): a gated one, as Claude
|
||||
// Mythos 5.1 is since models.dev took it out of
|
||||
// anthropic's list, that a vendor's own list or the
|
||||
// account's still serves (iebb on Discord). Its lab
|
||||
// file holds what is the model's on every host — name,
|
||||
// window, reply limit, thinking, images — and no
|
||||
// price. One a provider lists keeps its answers.
|
||||
labs = map[string]mdModel{}
|
||||
for id, x := range readLabs() {
|
||||
b := bareID(id)
|
||||
if listed[b] || canonical[b] {
|
||||
continue
|
||||
}
|
||||
if x.ID == "" {
|
||||
x.ID = id
|
||||
}
|
||||
if _, ok := labs[b]; !ok || id < labs[b].ID {
|
||||
labs[b] = x
|
||||
}
|
||||
vote(b, b, "lab:"+id, x)
|
||||
}
|
||||
for b := range labs {
|
||||
listed[b] = true
|
||||
}
|
||||
windows = map[string]int{}
|
||||
for id, by := range sizes {
|
||||
windows[id] = mostGiven(by)
|
||||
@@ -630,6 +658,7 @@ func loadGen() (map[string]mdProvider, int) {
|
||||
images[id] = true
|
||||
}
|
||||
}
|
||||
labKinEfforts(m)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -647,7 +676,7 @@ func Reset() {
|
||||
loaded = false
|
||||
built = nil
|
||||
gen++
|
||||
mdev, images, windows, outputs, efforts, thinks, names, listed, canonical = nil, nil, nil, nil, nil, nil, nil, nil, nil
|
||||
mdev, images, windows, outputs, efforts, thinks, names, listed, canonical, labs = nil, nil, nil, nil, nil, nil, nil, nil, nil, nil
|
||||
loadMu.Unlock()
|
||||
if Forget != nil {
|
||||
Forget()
|
||||
@@ -704,16 +733,19 @@ func syncLock(ctx context.Context, onlyIfMissing bool) error {
|
||||
if err := os.WriteFile(p, b, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
// the lab catalog beside it is best effort: without it the provider
|
||||
// catalog answers alone, and one kept before is kept
|
||||
_ = syncLabs(ctx)
|
||||
Reset()
|
||||
Touched() // names, reasoning levels and context windows may be others
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stale reports whether no catalog exists or the cache is older than a day
|
||||
// (fresh.go).
|
||||
// (fresh.go), or magpie's has no lab catalog beside it yet (labs.go).
|
||||
func Stale() bool {
|
||||
src := Source()
|
||||
if src == "" {
|
||||
if src == "" || labsMissing() {
|
||||
return true
|
||||
}
|
||||
st, err := os.Stat(src)
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
package catalog
|
||||
|
||||
// models.dev keeps two lists. api.json is each provider's models, with
|
||||
// what that host charges; models.json is each lab's models (labs/<maker>),
|
||||
// with only what is the model's on every host: name, window, reply limit,
|
||||
// thinking, inputs, release. A model can be in the second alone: models.dev
|
||||
// took the gated Claude Mythos 5.1 out of anthropic's provider list
|
||||
// (models.dev #11327) and kept its lab file (#11328), since not every
|
||||
// account may call it. An account that has it still lists it — Anthropic's
|
||||
// /v1/models with its key, the models Claude Code was told a Claude account
|
||||
// has — and magpie describes it from its lab file (iebb on Discord).
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// LabsPath is where Sync keeps models.dev's lab catalog (models.json),
|
||||
// beside its provider catalog.
|
||||
func LabsPath() string { return filepath.Join(filepath.Dir(CachePath()), "labs.json") }
|
||||
|
||||
// labsTried is set once this process asked models.dev for its lab
|
||||
// catalog, answered or not: one models.dev stops serving doesn't have the
|
||||
// provider catalog fetched again every hour (Stale).
|
||||
var labsTried atomic.Bool
|
||||
|
||||
// labsURL is the lab catalog beside the provider catalog modelsDevURL
|
||||
// names, or "" when it names none (a test's own server root).
|
||||
func labsURL() string {
|
||||
if base, ok := strings.CutSuffix(modelsDevURL, "/api.json"); ok {
|
||||
return base + "/models.json"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// readLabs is the lab catalog Sync kept, by "lab/model", or none when it
|
||||
// has none or can't be read: the provider catalog answers alone, as it did
|
||||
// before there was one.
|
||||
func readLabs() map[string]mdModel {
|
||||
b, err := os.ReadFile(LabsPath())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var m map[string]mdModel
|
||||
if json.Unmarshal(b, &m) != nil {
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// syncLabs fetches the lab catalog into LabsPath, written whole through a
|
||||
// rename. A failed fetch keeps the one kept before.
|
||||
func syncLabs(ctx context.Context) error {
|
||||
u := labsURL()
|
||||
if u == "" {
|
||||
return nil
|
||||
}
|
||||
labsTried.Store(true)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("User-Agent", "magpie")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("models.dev labs: %s", resp.Status)
|
||||
}
|
||||
b, err := io.ReadAll(io.LimitReader(resp.Body, 64<<20))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var probe map[string]mdModel
|
||||
if err := json.Unmarshal(b, &probe); err != nil || len(probe) == 0 {
|
||||
return fmt.Errorf("models.dev labs: unexpected payload")
|
||||
}
|
||||
p := LabsPath()
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
tmp, err := os.CreateTemp(filepath.Dir(p), ".labs-*.json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tmp.Write(b); err != nil {
|
||||
tmp.Close()
|
||||
os.Remove(tmp.Name())
|
||||
return err
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
os.Remove(tmp.Name())
|
||||
return err
|
||||
}
|
||||
if err := os.Chmod(tmp.Name(), 0o644); err != nil {
|
||||
os.Remove(tmp.Name())
|
||||
return err
|
||||
}
|
||||
if err := os.Rename(tmp.Name(), p); err != nil {
|
||||
os.Remove(tmp.Name())
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// labsMissing says the provider catalog is magpie's own but no lab catalog
|
||||
// was kept beside it, and none was asked for yet: one kept before magpie
|
||||
// read labs is fetched again at once, not a day later.
|
||||
func labsMissing() bool {
|
||||
if labsTried.Load() || labsURL() == "" || Source() != CachePath() {
|
||||
return false
|
||||
}
|
||||
_, err := os.Stat(LabsPath())
|
||||
return err != nil
|
||||
}
|
||||
|
||||
// labKinEfforts gives a lab-only model that thinks and lists no levels
|
||||
// (lab files have none) the levels of the newest thinking model of its
|
||||
// family released by then that any provider gives levels: Claude Mythos
|
||||
// 5.1 takes Mythos 5's. Called under loadMu, as the tables are built.
|
||||
func labKinEfforts(all map[string]mdProvider) {
|
||||
type kin struct{ id, date string }
|
||||
byFamily := map[string][]kin{}
|
||||
for _, p := range all {
|
||||
for id, x := range p.Models {
|
||||
if x.Family != "" {
|
||||
byFamily[x.Family] = append(byFamily[x.Family], kin{bareID(id), x.ReleaseDate})
|
||||
}
|
||||
}
|
||||
}
|
||||
for b, x := range labs {
|
||||
if len(efforts[b]) > 0 || !thinks[b] || x.Family == "" {
|
||||
continue
|
||||
}
|
||||
var best kin
|
||||
for _, k := range byFamily[x.Family] {
|
||||
if len(efforts[k.id]) == 0 || !thinks[k.id] || x.ReleaseDate != "" && k.date > x.ReleaseDate {
|
||||
continue
|
||||
}
|
||||
if best.id == "" || k.date > best.date || k.date == best.date && k.id > best.id {
|
||||
best = k
|
||||
}
|
||||
}
|
||||
if best.id != "" {
|
||||
efforts[b] = slices.Clone(efforts[best.id])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Describe is a model a vendor or an account lists by id, described as
|
||||
// models.dev knows it on any host — most providers' word, else its lab's —
|
||||
// for a list that has it where the provider's own catalog doesn't: its
|
||||
// window, reply limit, thinking, levels, images and release. name is the
|
||||
// list's own, kept when it gives one. It has no price: a lab gives none,
|
||||
// and another host's isn't this one's.
|
||||
func Describe(id, name string) Model {
|
||||
load()
|
||||
m := Model{ID: id, Name: strings.TrimSpace(name)}
|
||||
if m.Name == "" || m.Name == id {
|
||||
m.Name = id
|
||||
if n := NameOf(id); n != "" {
|
||||
m.Name = n
|
||||
}
|
||||
}
|
||||
m.Context, m.Output = ContextOf(id), OutputOf(id)
|
||||
m.Efforts = EffortsOf(id)
|
||||
m.Reasoning = Thinks(id) || len(m.Efforts) > 0
|
||||
m.Images = SeesImages(id)
|
||||
if x, ok := lookup(id, labs); ok {
|
||||
m.ImageInput = imageInput(x.Modalities.Input)
|
||||
m.Released, m.Temperature = x.ReleaseDate, x.Temperature
|
||||
m.InputModalities, m.OutputModalities = x.Modalities.Input, x.Modalities.Output
|
||||
}
|
||||
return m
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The catalogs as models.dev serves them on 10-27, cut to Claude: its
|
||||
// anthropic and azure providers (api.json) and the anthropic lab
|
||||
// (models.json). models.dev took the gated Claude Mythos 5.1 out of
|
||||
// anthropic's provider list (#11327) and kept its lab file (#11328).
|
||||
const (
|
||||
claudeAPI10_27 = "testdata/models.dev-2026-10-27-claude.json"
|
||||
claudeLabs10_27 = "testdata/models.dev-labs-2026-10-27-anthropic.json"
|
||||
)
|
||||
|
||||
func useCatalogs(t *testing.T, api, labs string) {
|
||||
t.Helper()
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
t.Cleanup(Reset)
|
||||
for src, dst := range map[string]string{api: CachePath(), labs: LabsPath()} {
|
||||
if src == "" {
|
||||
continue
|
||||
}
|
||||
b, err := os.ReadFile(src)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
os.MkdirAll(filepath.Dir(dst), 0o755)
|
||||
if err := os.WriteFile(dst, b, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reset()
|
||||
}
|
||||
|
||||
// A model models.dev keeps under its lab alone is still known: an account
|
||||
// or a vendor's list that has Claude Mythos 5.1 gets its name, 1M window,
|
||||
// reply limit, thinking and images from the lab file, and Mythos 5's
|
||||
// levels, the newest of its family any provider gives (iebb on Discord).
|
||||
func TestLabOnlyModelIsKnown(t *testing.T) {
|
||||
useCatalogs(t, claudeAPI10_27, claudeLabs10_27)
|
||||
for _, m := range Provider("anthropic") {
|
||||
if m.ID == "claude-mythos-5-1" {
|
||||
t.Fatal("fixture: models.dev's anthropic list has Mythos 5.1 again")
|
||||
}
|
||||
}
|
||||
const id = "claude-mythos-5-1"
|
||||
if got := NameOf(id); got != "Claude Mythos 5.1" {
|
||||
t.Errorf("NameOf = %q", got)
|
||||
}
|
||||
if got := ContextOf(id); got != 1_000_000 {
|
||||
t.Errorf("ContextOf = %d", got)
|
||||
}
|
||||
if got := OutputOf(id); got != 128_000 {
|
||||
t.Errorf("OutputOf = %d", got)
|
||||
}
|
||||
if !Thinks(id) || !SeesImages(id) {
|
||||
t.Errorf("Thinks %v, SeesImages %v", Thinks(id), SeesImages(id))
|
||||
}
|
||||
want := EffortsOf("claude-mythos-5")
|
||||
if len(want) == 0 {
|
||||
t.Fatal("fixture: Mythos 5 has no levels")
|
||||
}
|
||||
if got := EffortsOf(id); !slices.Equal(got, want) {
|
||||
t.Errorf("EffortsOf = %v, want Mythos 5's %v", got, want)
|
||||
}
|
||||
// a vendor prefix and Claude Code's [1m] reach it as they reach any
|
||||
for _, as := range []string{"anthropic/claude-mythos-5-1", "claude-mythos-5-1[1m]"} {
|
||||
if got := ContextOf(as); got != 1_000_000 {
|
||||
t.Errorf("ContextOf(%q) = %d", as, got)
|
||||
}
|
||||
}
|
||||
|
||||
d := Describe(id, "")
|
||||
if d.Name != "Claude Mythos 5.1" || d.Context != 1_000_000 || d.Output != 128_000 ||
|
||||
!d.Reasoning || !slices.Equal(d.Efforts, want) || !d.Images || d.Released != "2026-09-01" ||
|
||||
d.Temperature == nil || *d.Temperature || d.Price != nil {
|
||||
t.Errorf("Describe = %+v", d)
|
||||
}
|
||||
if d := Describe(id, "Mythos (account)"); d.Name != "Mythos (account)" {
|
||||
t.Errorf("Describe kept no list name: %q", d.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// A model a provider lists keeps what providers say of it: its lab file
|
||||
// adds nothing to it, and without a lab catalog the catalog answers as
|
||||
// before.
|
||||
func TestLabsLeaveListedModels(t *testing.T) {
|
||||
useCatalogs(t, claudeAPI10_27, "")
|
||||
want := map[string]int{}
|
||||
for _, m := range Provider("anthropic") {
|
||||
want[m.ID] = ContextOf(m.ID)
|
||||
}
|
||||
if ContextOf("claude-mythos-5-1") != 0 {
|
||||
t.Fatal("Mythos 5.1 known without a lab catalog")
|
||||
}
|
||||
useCatalogs(t, claudeAPI10_27, claudeLabs10_27)
|
||||
for id, w := range want {
|
||||
if got := ContextOf(id); got != w {
|
||||
t.Errorf("%s: window %d with labs, %d without", id, got, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sync keeps models.dev's lab catalog beside its provider catalog, and a
|
||||
// lab fetch that fails keeps the one it had.
|
||||
func TestSyncKeepsLabs(t *testing.T) {
|
||||
api, _ := os.ReadFile(claudeAPI10_27)
|
||||
labs, _ := os.ReadFile(claudeLabs10_27)
|
||||
labsDown := false
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/api.json":
|
||||
w.Write(api)
|
||||
case "/models.json":
|
||||
if labsDown {
|
||||
http.Error(w, "down", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
w.Write(labs)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
useCatalogs(t, "", "")
|
||||
was := modelsDevURL
|
||||
modelsDevURL = srv.URL + "/api.json"
|
||||
t.Cleanup(func() { modelsDevURL = was; labsTried.Store(false) })
|
||||
|
||||
labsTried.Store(false)
|
||||
if err := Sync(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := ContextOf("claude-mythos-5-1"); got != 1_000_000 {
|
||||
t.Fatalf("after Sync, Mythos 5.1's window = %d", got)
|
||||
}
|
||||
if Stale() {
|
||||
t.Error("a catalog just synced with its labs is stale")
|
||||
}
|
||||
|
||||
labsDown = true
|
||||
if err := Sync(context.Background()); err != nil {
|
||||
t.Fatal("a lab fetch failing failed Sync:", err)
|
||||
}
|
||||
if got := ContextOf("claude-mythos-5-1"); got != 1_000_000 {
|
||||
t.Errorf("a failed lab fetch lost the kept labs: window %d", got)
|
||||
}
|
||||
|
||||
// a cache kept before magpie read labs is fetched again at once
|
||||
os.Remove(LabsPath())
|
||||
labsTried.Store(false)
|
||||
if !Stale() {
|
||||
t.Error("a catalog without its labs isn't stale")
|
||||
}
|
||||
labsDown = false
|
||||
if err := Sync(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if Stale() {
|
||||
t.Error("stale after the labs came")
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -826,12 +826,13 @@ func claudeProvider(acct *Account) Provider {
|
||||
// account runs Claude Code itself (the gateway's bridge, a test), so
|
||||
// one that would go straight to the API with its sign-in is refused
|
||||
acct.sign = func(context.Context, *http.Request, []byte) error { return errClaudeViaCLI }
|
||||
acct.models = func() []catalog.Model { return catalog.Provider("anthropic") }
|
||||
acct.models = claudeModels
|
||||
acct.magpieList = true
|
||||
// Claude's models are the ones magpie knows: listing them would ask
|
||||
// Claude's models are the ones magpie knows and the ones Claude Code
|
||||
// was told an account has (claude_models.go): listing them would ask
|
||||
// Anthropic with the account's sign-in, which magpie never does
|
||||
acct.fetch = func(context.Context) ([]catalog.Model, error) {
|
||||
ms := catalog.Provider("anthropic")
|
||||
ms := claudeModels()
|
||||
return ms, catalog.SaveLive("claude", claudeBase, ms)
|
||||
}
|
||||
return Provider{ID: "claude", Name: "Claude Code", Icon: "claudecode-color", Anthropic: claudeBase,
|
||||
|
||||
@@ -0,0 +1,278 @@
|
||||
package provider
|
||||
|
||||
// A Claude account's models are the ones models.dev lists under anthropic
|
||||
// and the ones Anthropic told Claude Code the account has beyond them.
|
||||
// models.dev's list is what every account may call; a gated model, as
|
||||
// Claude Mythos 5.1 is since models.dev took it out of that list
|
||||
// (models.dev #11327), or one kept for some accounts (Opus 3), is only in
|
||||
// what Claude Code was told. Claude Code asks Anthropic itself, at its
|
||||
// start (/api/claude_cli/bootstrap), and keeps the answer in its global
|
||||
// state file (.claude.json): additionalModelOptionsCache, the models its
|
||||
// /model picker offers beyond its own; modelAccessCache, the models the
|
||||
// account is or isn't entitled to; additionalModelCostsCache, their
|
||||
// prices. magpie reads them back, as it reads the account's identity from
|
||||
// the same file: nothing is asked of Anthropic in Claude Code's name
|
||||
// (iebb on Discord: "最好以 claude code 返回为准").
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
)
|
||||
|
||||
// claudeCodeModels is what Claude Code keeps of an account's models.
|
||||
type claudeCodeModels struct {
|
||||
// at is when Anthropic answered (additionalModelOptionsAnsweredAt),
|
||||
// in ms: of two files of one account, the later one's word goes
|
||||
at float64
|
||||
// extra are the models it offers beyond its own, by API id, with the
|
||||
// name it gives them
|
||||
extra []claudeExtra
|
||||
// entitled is, by API id, whether the account may call the model
|
||||
entitled map[string]bool
|
||||
// prices are Anthropic's, by API id, USD per million tokens
|
||||
prices map[string]catalog.Price
|
||||
}
|
||||
|
||||
type claudeExtra struct{ id, name string }
|
||||
|
||||
func parseClaudeCodeModels(b []byte) (claudeCodeModels, error) {
|
||||
var raw struct {
|
||||
Options []struct {
|
||||
Value *string `json:"value"`
|
||||
Label string `json:"label"`
|
||||
Disabled bool `json:"disabled"`
|
||||
} `json:"additionalModelOptionsCache"`
|
||||
At float64 `json:"additionalModelOptionsAnsweredAt"`
|
||||
Access []struct {
|
||||
APIName string `json:"apiName"`
|
||||
Entitled *bool `json:"entitled"`
|
||||
} `json:"modelAccessCache"`
|
||||
Costs map[string]struct {
|
||||
Input float64 `json:"inputTokens"`
|
||||
Output float64 `json:"outputTokens"`
|
||||
Write float64 `json:"promptCacheWriteTokens"`
|
||||
Write1h *float64 `json:"promptCacheWrite1hTokens"`
|
||||
CacheRead float64 `json:"promptCacheReadTokens"`
|
||||
} `json:"additionalModelCostsCache"`
|
||||
}
|
||||
if err := json.Unmarshal(b, &raw); err != nil {
|
||||
return claudeCodeModels{}, err
|
||||
}
|
||||
out := claudeCodeModels{at: raw.At}
|
||||
seen := map[string]bool{}
|
||||
for _, o := range raw.Options {
|
||||
if o.Value == nil || o.Disabled {
|
||||
continue
|
||||
}
|
||||
id := claudeAPIID(*o.Value)
|
||||
if id == "" || seen[id] {
|
||||
continue
|
||||
}
|
||||
seen[id] = true
|
||||
out.extra = append(out.extra, claudeExtra{id, strings.TrimSpace(o.Label)})
|
||||
}
|
||||
for _, a := range raw.Access {
|
||||
if id := claudeAPIID(a.APIName); id != "" && a.Entitled != nil {
|
||||
if out.entitled == nil {
|
||||
out.entitled = map[string]bool{}
|
||||
}
|
||||
out.entitled[id] = *a.Entitled
|
||||
if *a.Entitled && !seen[id] {
|
||||
seen[id] = true
|
||||
out.extra = append(out.extra, claudeExtra{id: id})
|
||||
}
|
||||
}
|
||||
}
|
||||
for id, c := range raw.Costs {
|
||||
id = claudeAPIID(id)
|
||||
if id == "" || c.Input <= 0 || c.Output <= 0 {
|
||||
continue
|
||||
}
|
||||
p := catalog.Price{Input: c.Input, Output: c.Output, CacheRead: c.CacheRead, CacheWrite: c.Write}
|
||||
if c.Write1h != nil && *c.Write1h > 0 {
|
||||
p.CacheWrite1h = *c.Write1h
|
||||
}
|
||||
catalog.OneHourFor(id, &p)
|
||||
if out.prices == nil {
|
||||
out.prices = map[string]catalog.Price{}
|
||||
}
|
||||
out.prices[id] = p
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// claudeAPIID is a model id as Claude Code names it, without the window
|
||||
// it takes in the name (claude-mythos-5-1[1m]): the id the API knows. ""
|
||||
// for one that isn't a Claude model's id (an alias, "default").
|
||||
func claudeAPIID(v string) string {
|
||||
v = strings.ToLower(strings.TrimSpace(v))
|
||||
if i := strings.IndexByte(v, '['); i > 0 && strings.HasSuffix(v, "]") {
|
||||
v = v[:i]
|
||||
}
|
||||
if !strings.HasPrefix(v, "claude-") || strings.ContainsAny(v, " /") {
|
||||
return ""
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func readClaudeCodeModels(path string) (claudeCodeModels, bool) {
|
||||
m, err := filememo.Read("claude models", path, parseClaudeCodeModels)
|
||||
return m, err == nil
|
||||
}
|
||||
|
||||
// claudeCodeFiles are the state files of every Claude Code magpie knows of
|
||||
// an account in: the one Claude Code itself is signed in to, then each
|
||||
// saved account's own config directory (claude_dirs.go).
|
||||
func claudeCodeFiles() []string {
|
||||
if settings.AccountDiscoveryOff() {
|
||||
return nil
|
||||
}
|
||||
out := []string{claudeProfilePath()}
|
||||
saved, _ := filepath.Glob(filepath.Join(claudeDirsRoot(), "*", ".claude.json"))
|
||||
sort.Strings(saved)
|
||||
return append(out, saved...)
|
||||
}
|
||||
|
||||
// claudeModelsOf is what Claude Code keeps of the account user's models:
|
||||
// from its saved directory or Claude Code's own sign-in, whichever
|
||||
// Anthropic answered last, as that file's account is the user's.
|
||||
func claudeModelsOf(user string) (claudeCodeModels, bool) {
|
||||
var best claudeCodeModels
|
||||
found := false
|
||||
take := func(m claudeCodeModels) {
|
||||
if !found || m.at > best.at {
|
||||
best, found = m, true
|
||||
}
|
||||
}
|
||||
if user == "" {
|
||||
return best, false
|
||||
}
|
||||
if m, ok := readClaudeCodeModels(filepath.Join(claudeAccountDir(user), ".claude.json")); ok {
|
||||
take(m)
|
||||
}
|
||||
if acct, ok := claudeProfileAccount(); ok {
|
||||
email, _ := acct["emailAddress"].(string)
|
||||
email = strings.ToLower(strings.TrimSpace(email))
|
||||
u := strings.ToLower(strings.TrimSpace(user))
|
||||
if email != "" && (u == email || strings.HasPrefix(u, email+" · ")) {
|
||||
if m, ok := readClaudeCodeModels(claudeProfilePath()); ok {
|
||||
take(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
return best, found
|
||||
}
|
||||
|
||||
// claudeModels is the Claude provider's list: models.dev's anthropic list,
|
||||
// and each model an account's Claude Code offers beyond it, described as
|
||||
// models.dev knows it anywhere (its lab file for a gated one).
|
||||
func claudeModels() []catalog.Model {
|
||||
out := catalog.Provider("anthropic")
|
||||
have := map[string]bool{}
|
||||
for _, m := range out {
|
||||
have[m.ID] = true
|
||||
}
|
||||
for _, f := range claudeCodeFiles() {
|
||||
cm, ok := readClaudeCodeModels(f)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, x := range cm.extra {
|
||||
if have[x.id] {
|
||||
continue
|
||||
}
|
||||
have[x.id] = true
|
||||
m := catalog.Describe(x.id, x.name)
|
||||
m.Provider = "anthropic"
|
||||
if p, ok := cm.prices[x.id]; ok {
|
||||
m.Price = &p
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
sort.SliceStable(out, func(i, j int) bool {
|
||||
if out[i].Released != out[j].Released {
|
||||
return out[i].Released > out[j].Released
|
||||
}
|
||||
return out[i].ID < out[j].ID
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
// claudeLists says whether the Claude account user has model, as its
|
||||
// Claude Code was told: a model it isn't entitled to is left out, and one
|
||||
// it offers or models.dev lists for every account is in. ok is false when
|
||||
// no Claude Code of the account has said.
|
||||
func claudeLists(user, model string) (has, ok bool) {
|
||||
cm, found := claudeModelsOf(user)
|
||||
if !found {
|
||||
return false, false
|
||||
}
|
||||
id := claudeAPIID(model)
|
||||
if id == "" {
|
||||
return true, true
|
||||
}
|
||||
if e, said := cm.entitled[id]; said {
|
||||
return e, true
|
||||
}
|
||||
return true, true
|
||||
}
|
||||
|
||||
// ClaudeCodeModels is the list Claude Code, as it is signed in on this
|
||||
// computer, offers: models.dev's anthropic list without what the account
|
||||
// isn't entitled to, and the models Anthropic told it the account has
|
||||
// beyond them (Claude Mythos 5.1).
|
||||
func ClaudeCodeModels() []catalog.Model {
|
||||
if settings.AccountDiscoveryOff() {
|
||||
return catalog.Provider("anthropic")
|
||||
}
|
||||
cm, ok := readClaudeCodeModels(claudeProfilePath())
|
||||
return claudeModelsWith(cm, ok)
|
||||
}
|
||||
|
||||
// claudeModelsWith is the provider's list as one account's Claude Code
|
||||
// was told it: none it isn't entitled to, and of the models models.dev
|
||||
// doesn't list for every account only its own.
|
||||
func claudeModelsWith(cm claudeCodeModels, ok bool) []catalog.Model {
|
||||
if !ok {
|
||||
return catalog.Provider("anthropic")
|
||||
}
|
||||
base := map[string]bool{}
|
||||
for _, m := range catalog.Provider("anthropic") {
|
||||
base[m.ID] = true
|
||||
}
|
||||
own := map[string]bool{}
|
||||
for _, x := range cm.extra {
|
||||
own[x.id] = true
|
||||
}
|
||||
return slices.DeleteFunc(claudeModels(), func(m catalog.Model) bool {
|
||||
if e, said := cm.entitled[m.ID]; said && !e {
|
||||
return true
|
||||
}
|
||||
return !base[m.ID] && !own[m.ID]
|
||||
})
|
||||
}
|
||||
|
||||
// claudeCodePrice is Anthropic's price for a model Claude Code was told
|
||||
// of, from any account's: a gated model models.dev prices nowhere.
|
||||
func claudeCodePrice(model string) (catalog.Price, bool) {
|
||||
id := claudeAPIID(model)
|
||||
if id == "" {
|
||||
return catalog.Price{}, false
|
||||
}
|
||||
for _, f := range claudeCodeFiles() {
|
||||
if cm, ok := readClaudeCodeModels(f); ok {
|
||||
if p, ok := cm.prices[id]; ok {
|
||||
return p, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return catalog.Price{}, false
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/agentenv"
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
)
|
||||
|
||||
// mythosCatalogs puts models.dev's catalogs as they were on 10-27, cut to
|
||||
// Claude, where magpie keeps them: Claude Mythos 5.1 is in the anthropic
|
||||
// lab (models.json) and in no provider's list (api.json).
|
||||
func mythosCatalogs(t *testing.T) {
|
||||
t.Helper()
|
||||
for src, dst := range map[string]string{
|
||||
"../catalog/testdata/models.dev-2026-10-27-claude.json": catalog.CachePath(),
|
||||
"../catalog/testdata/models.dev-labs-2026-10-27-anthropic.json": catalog.LabsPath(),
|
||||
} {
|
||||
b, err := os.ReadFile(src)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
os.MkdirAll(filepath.Dir(dst), 0o755)
|
||||
if err := os.WriteFile(dst, b, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
catalog.Reset()
|
||||
t.Cleanup(catalog.Reset)
|
||||
filememo.Forget()
|
||||
}
|
||||
|
||||
// what Claude Code keeps in .claude.json from Anthropic's answer to its
|
||||
// bootstrap, in the shape its 2.1.311 reads it
|
||||
func claudeCodeState(t *testing.T, path, email string, options []map[string]any, access []map[string]any, costs map[string]any) {
|
||||
t.Helper()
|
||||
writeFile(t, path, map[string]any{
|
||||
"oauthAccount": map[string]any{"emailAddress": email, "accountUuid": "u-" + email},
|
||||
"additionalModelOptionsCache": options,
|
||||
"additionalModelOptionsAnsweredAt": time.Now().UnixMilli(),
|
||||
"modelAccessCache": access,
|
||||
"additionalModelCostsCache": costs,
|
||||
})
|
||||
}
|
||||
|
||||
// A Claude account lists a gated model Anthropic told its Claude Code it
|
||||
// has, though models.dev's anthropic list no longer does: Claude Mythos
|
||||
// 5.1, with its lab file's window and name and Anthropic's own price; an
|
||||
// account not entitled to it isn't routed it (iebb on Discord).
|
||||
func TestClaudeAccountListsGatedModels(t *testing.T) {
|
||||
home := claudeHome(t)
|
||||
claudeSignIn(t, home, time.Now().Add(time.Hour))
|
||||
claudeBase = "http://127.0.0.1:1" // asked nothing
|
||||
mythosCatalogs(t)
|
||||
if slices.Contains(modelIDs(catalog.Provider("anthropic")), "claude-mythos-5-1") {
|
||||
t.Fatal("fixture: models.dev lists Mythos 5.1 under anthropic again")
|
||||
}
|
||||
|
||||
no := false
|
||||
claudeCodeState(t, filepath.Join(home, ".claude.json"), "iebb@example.com",
|
||||
[]map[string]any{
|
||||
{"value": "claude-mythos-5-1[1m]", "label": "Mythos 5.1", "description": "Restricted research model"},
|
||||
{"value": nil, "label": "Default", "description": "Recommended"},
|
||||
{"value": "claude-opus-3", "label": "Opus 3", "description": "Retired", "disabled": true},
|
||||
},
|
||||
[]map[string]any{{"apiName": "claude-mythos-5-1", "entitled": true}, {"apiName": "claude-opus-4-5", "entitled": no}},
|
||||
map[string]any{"claude-mythos-5-1": map[string]any{
|
||||
"inputTokens": 25, "outputTokens": 125, "promptCacheWriteTokens": 31.25,
|
||||
"promptCacheWrite1hTokens": 50, "promptCacheReadTokens": 2.5, "webSearchRequests": 0.01}})
|
||||
filememo.Forget()
|
||||
|
||||
p, ok := find(All(), "claude")
|
||||
if !ok || p.Account == nil {
|
||||
t.Fatal("no Claude account")
|
||||
}
|
||||
ms := p.Available()
|
||||
i := slices.IndexFunc(ms, func(m catalog.Model) bool { return m.ID == "claude-mythos-5-1" })
|
||||
if i < 0 {
|
||||
t.Fatalf("Claude account's list has no Mythos 5.1: %v", modelIDs(ms))
|
||||
}
|
||||
if m := ms[i]; m.Name != "Mythos 5.1" || m.Context != 1_000_000 || m.Output != 128_000 || !m.Reasoning || len(m.Efforts) == 0 {
|
||||
t.Errorf("Mythos 5.1 = %+v", m)
|
||||
}
|
||||
if slices.Contains(modelIDs(ms), "claude-opus-3") {
|
||||
t.Error("a disabled option listed")
|
||||
}
|
||||
if pr, ok := p.ListPrice("claude-mythos-5-1"); !ok || pr.Input != 25 || pr.Output != 125 || pr.CacheWrite1h != 50 {
|
||||
t.Errorf("price = %+v %v", pr, ok)
|
||||
}
|
||||
fetched, err := p.Fetch(context.Background())
|
||||
if err != nil || !slices.Contains(modelIDs(fetched), "claude-mythos-5-1") {
|
||||
t.Fatalf("fetch: %v %v", modelIDs(fetched), err)
|
||||
}
|
||||
if e, ok := EntryOf("claude/claude-mythos-5-1"); !ok || e.Context != 1_000_000 {
|
||||
t.Errorf("entry: %+v %v", e, ok)
|
||||
}
|
||||
|
||||
// routed to the account Anthropic entitles to it, not one it doesn't
|
||||
if !p.Account.Lists("claude-mythos-5-1") {
|
||||
t.Error("the entitled account doesn't list Mythos 5.1")
|
||||
}
|
||||
if p.Account.Lists("claude-opus-4-5") {
|
||||
t.Error("a model the account isn't entitled to is listed")
|
||||
}
|
||||
if !p.Account.Lists("claude-sonnet-5") {
|
||||
t.Error("a model models.dev lists for every account isn't listed")
|
||||
}
|
||||
other := &Account{Agent: "claude", User: "someone@example.com"}
|
||||
claudeCodeState(t, filepath.Join(claudeAccountDir(other.User), ".claude.json"), other.User,
|
||||
nil, []map[string]any{{"apiName": "claude-mythos-5-1", "entitled": no}}, nil)
|
||||
filememo.Forget()
|
||||
if other.Lists("claude-mythos-5-1") {
|
||||
t.Error("an account not entitled to Mythos 5.1 lists it")
|
||||
}
|
||||
if !other.Lists("claude-sonnet-5") {
|
||||
t.Error("the other account lost what every account has")
|
||||
}
|
||||
if nobody := (&Account{Agent: "claude", User: "unknown@example.com"}); !nobody.Lists("claude-mythos-5-1") {
|
||||
t.Error("an account no Claude Code has said of is taken to lack it")
|
||||
}
|
||||
|
||||
// Claude Code's own picker on this computer offers it too
|
||||
if own := modelIDs(ClaudeCodeModels()); !slices.Contains(own, "claude-mythos-5-1") || slices.Contains(own, "claude-opus-4-5") {
|
||||
t.Errorf("ClaudeCodeModels = %v", own)
|
||||
}
|
||||
}
|
||||
|
||||
// A vendor's own list that has a model models.dev keeps under its lab
|
||||
// alone gets the lab's window, reply limit, name and thinking: an
|
||||
// Anthropic API key whose /v1/models has Claude Mythos 5.1.
|
||||
func TestAnthropicKeyGatedModelDescribed(t *testing.T) {
|
||||
isolate(t)
|
||||
h := t.TempDir()
|
||||
t.Setenv("HOME", h)
|
||||
t.Setenv("USERPROFILE", h)
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
||||
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
||||
t.Setenv("PATH", h)
|
||||
for _, v := range agentenv.Vars {
|
||||
t.Setenv(v, "")
|
||||
}
|
||||
mythosCatalogs(t)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/models" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Write([]byte(`{"data":[{"type":"model","id":"claude-mythos-5-1","created_at":"2026-09-01T00:00:00Z"},` +
|
||||
`{"type":"model","id":"claude-sonnet-5","display_name":"Claude Sonnet 5"}],"has_more":false}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
if err := Save(Provider{ID: "anth", Name: "Anthropic key", Anthropic: srv.URL + "/v1", Key: "sk-ant-test"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
q, err := Find("anth")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := q.Fetch(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e, ok := EntryOf("anth/claude-mythos-5-1")
|
||||
if !ok {
|
||||
t.Fatal("Mythos 5.1 not listed")
|
||||
}
|
||||
if e.Context != 1_000_000 || e.Output != 128_000 || e.Default != "Claude Mythos 5.1" && e.Name != "Claude Mythos 5.1" || len(e.Efforts) == 0 {
|
||||
t.Errorf("entry = id %s name %q default %q context %d output %d efforts %v", e.ID, e.Name, e.Default, e.Context, e.Output, e.Efforts)
|
||||
}
|
||||
}
|
||||
@@ -242,6 +242,13 @@ func (a *Account) Lists(model string) bool {
|
||||
if a.plugin != nil {
|
||||
return a.pluginLists(model)
|
||||
}
|
||||
if a.Agent == "claude" {
|
||||
// what the account's Claude Code was told it has: a gated model
|
||||
// (Claude Mythos 5.1) goes to the accounts entitled to it
|
||||
if has, ok := claudeLists(a.User, model); ok {
|
||||
return has
|
||||
}
|
||||
}
|
||||
live, _, ok := catalog.Live(accountModels(a.Agent, a.User))
|
||||
if !ok {
|
||||
return true
|
||||
|
||||
@@ -1855,6 +1855,13 @@ func (p Provider) ListPrice(model string) (catalog.Price, bool) {
|
||||
return pr, true
|
||||
}
|
||||
}
|
||||
if slices.Contains(cats, "anthropic") {
|
||||
// a gated Claude model models.dev prices nowhere (Claude Mythos
|
||||
// 5.1): Anthropic's price, as it told Claude Code
|
||||
if pr, ok := claudeCodePrice(model); ok {
|
||||
return pr, true
|
||||
}
|
||||
}
|
||||
if pr, ok := p.pluginListPrice(model); ok {
|
||||
return pr, true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user