Claude Code 2.1.323+ keeps an account's models in
<config dir>/cache/model-catalog/<organizationUuid>-<sha256(accountUuid)[:12]>-cc.json
(version 2, catalog.config.models), and magpie read only .claude.json's
additionalModelOptionsCache / modelAccessCache. A model only that catalog
has, and models.dev's anthropic list lacks, was missing from the Claude
provider, `magpie models` and Claude Code's own picker (pwn2ooown).
readClaudeCodeModels, which every reader of those caches goes through
(claudeModels, claudeModelsOf/claudeLists, ClaudeCodeModels,
claudeCodePrice), now adds the catalog of the account its .claude.json
names, from that config directory or Claude Code's own (~/.claude keeps
every account's that signed in there), the one fetched last. A model
models.dev doesn't describe takes the catalog's window, reply limit,
effort levels and mm_images. The legacy caches are read as before.
Another account's catalog is never read; a file of another version or
surface is unknown; a .claude.json or catalog that doesn't parse
(halfway through a write) is what it said last (readKeepingGood), a file
gone is gone.
The name and shape come from this Mac's Claude Code, read only: 7 files
in ~/.claude/cache/model-catalog and 2 in saved account dirs; the name's
first part is organizationUuid and its second sha256(accountUuid)[:12]
for every account whose .claude.json is here.
Run (mote):
- go test -tags nogui ./internal/provider ./internal/agent ./internal/cli
./internal/catalog ./internal/filememo ./internal/proc: all ok (provider 166s, agent 357s, cli 67s)
- go vet ./...; GOOS=linux go vet -tags nogui and GOOS=windows go vet of
internal/provider and internal/agent; gofmt -l clean
- built magpie, `magpie models` in sandbox HOMEs (the issue's repro
adapted): baseline, other account's catalog, half-written catalog: no
Fable 5.2; the issue's catalog: listed; legacy cache: listed; a copy of
a real Claude Code catalog's bytes under synthetic ids: all 13 models
listed.
- after rebasing on 58629a0f8 (no change in these packages): provider
and usage ok again at the pushed head.
Broken by hand (TestClaudeCodeCatalogV2Models red each time):
- catalog not read: "Fable 5.2 from the version-2 catalog not listed"
- last good dropped: "a half-written catalog dropped Fable 5.2"
- name keyed on org only: "another account's catalog listed"
Not tried: Claude Code on Linux/Windows (same path under the config dir
assumed); a real account whose catalog has a model models.dev lacks
(every model in this Mac's catalogs is in models.dev); the full ./...
suite. The issue's repro names its file <org>-<accountUuid>-cc.json,
which Claude Code doesn't write, so that literal name is not read.
320 lines
10 KiB
Go
320 lines
10 KiB
Go
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 返回为准"). Claude Code 2.1.323
|
|
// and later keep the models in a catalog file too (claude_code_catalog.go),
|
|
// read with the .claude.json of the account it names.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"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
|
|
// org and account are the oauthAccount's organizationUuid and
|
|
// accountUuid: whose catalog file goes with this one
|
|
org, account string
|
|
}
|
|
|
|
// claudeExtra is a model Claude Code offers; context, output, efforts and
|
|
// images are its catalog's runtime, when the catalog has it, for a model
|
|
// models.dev doesn't describe.
|
|
type claudeExtra struct {
|
|
id, name string
|
|
context, output int
|
|
efforts []string
|
|
images bool
|
|
}
|
|
|
|
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"`
|
|
Account struct {
|
|
Org string `json:"organizationUuid"`
|
|
Account string `json:"accountUuid"`
|
|
} `json:"oauthAccount"`
|
|
}
|
|
if err := json.Unmarshal(b, &raw); err != nil {
|
|
return claudeCodeModels{}, err
|
|
}
|
|
out := claudeCodeModels{at: raw.At, org: strings.TrimSpace(raw.Account.Org), account: strings.TrimSpace(raw.Account.Account)}
|
|
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: id, name: 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
|
|
}
|
|
|
|
// readClaudeCodeModels is what the .claude.json at path and its
|
|
// account's model catalog say: a file that doesn't parse now is what it
|
|
// said last.
|
|
func readClaudeCodeModels(path string) (claudeCodeModels, bool) {
|
|
m, ok := readKeepingGood("claude models", path, parseClaudeCodeModels)
|
|
if !ok {
|
|
return m, false
|
|
}
|
|
if c, ok := claudeCatalogOf(path, m.org, m.account); ok {
|
|
m = m.withCatalog(c)
|
|
}
|
|
return m, true
|
|
}
|
|
|
|
// 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). Listed once
|
|
// while a request holds the catalog: each Claude model's price asked for
|
|
// them, so a build of an agent's models read the folder once per model.
|
|
func claudeCodeFiles() []string {
|
|
return slices.Clip(heldOf("claude code files", listClaudeCodeFiles))
|
|
}
|
|
|
|
func listClaudeCodeFiles() []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 m.Context == 0 {
|
|
m.Context = x.context
|
|
}
|
|
if m.Output == 0 {
|
|
m.Output = x.output
|
|
}
|
|
if len(m.Efforts) == 0 && len(x.efforts) > 0 {
|
|
m.Efforts, m.Reasoning = slices.Clone(x.efforts), true
|
|
}
|
|
m.Images = m.Images || x.images
|
|
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
|
|
}
|