Files
magpie/internal/agent/claudedesktop_models.go
T
yetone 13e8002641 claude-desktop: two providers' Claude model read "Opus 5.5 · agy-cli", not the id's tail (onkashiko on X)
Claude Desktop names a row listed as magpie-<n>.anthropic.claude-opus-5-5
after its own catalog ("Opus 5.5"), whatever display_name says, so
agy-cli's and another provider's claude-opus-5-5 read alike and Desktop
5.11492 told them apart by the part of the ids that differs: "Opus 5.5
(…<n>.anthropic.claude-opu…)" (xVn/wVn in its app.asar). Only an
inferenceModels entry's labelOverride comes before the catalog's name,
and Desktop never suffixes such a label (keep in xVn).

magpie now writes the profile's inferenceModels, each row's display_name
as labelOverride, while two rows are named after one Claude model of
Desktop's catalog (desktopNamesClash), not only while a tier is picked.
The ids are unchanged. As with a tier's list, Desktop reads it at start
and its menu stays as listed until the next start; the Agents row's
Restart shows.

desktopNames' own fallback for two rows display_name still leaves alike
(one provider's models named alike) is now the model's id, "Z · D (z2)",
not the whole catalog id; two providers of one name keep the catalog id.
Claude Science takes the same names.

Run (mote, at origin/main 9dcec6751 + this):
- go test ./internal/gateway ./internal/agent ./internal/gui
  ./internal/provider -count=1 -timeout 45m: all ok.
- GOOS=linux and GOOS=windows go vet ./internal/gateway ./internal/agent: ok.
- gofmt -l: clean.
Rebased on 023a56373 (Windows install dialog only); at that head
go test ./internal/gateway ./internal/agent -run 'Desktop|Science': ok.
Broken by hand: with the list written only while a tier is picked (the old
rule), TestDesktopInferenceModelsCatalogNamesClash is red ("two Opus 5.5
rows, no list").
Not tried: a real Claude Desktop 5.11492 reading the list (the labelOverride
precedence and keep are read from its app.asar), and the reporter's setup.
2026-11-16 12:05:45 +08:00

123 lines
4.1 KiB
Go

package agent
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"slices"
"strings"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
)
// While a model is picked for one of Claude Desktop's tiers, magpie's
// profile carries inferenceModels (gateway.DesktopInferenceModels): the
// list Desktop's Code tab hands Claude Code each pick's behavesAs from,
// so a pick runs with the prompt of the Claude model of its tier
// (moer2831), and while two rows Desktop names after one Claude model of
// its catalog would read alike (labelOverride, onkashiko on X). Without
// either there is none, and Desktop finds magpie's
// models on its own as before. The list is magpie's only while it is what
// magpie wrote: desktopModelsMark keeps the hash of each list magpie
// wrote (the one before a write too, until the write is done), and a list
// with any other hash is the user's, which magpie never writes over or
// takes away. Desktop reads its profile at start-up only.
const desktopModelsMark = "claude-desktop.models"
// desktopDiscoveryMark says magpie wrote the profile's
// modelDiscoveryEnabled (gateway.DesktopDiscovers): magpie takes it away
// only then, and only while it is still true. One the user wrote, either
// way, stays as they wrote it.
const desktopDiscoveryMark = "claude-desktop.discovery"
// desktopModelsHash is a list's hash, the same however it is spaced.
func desktopModelsHash(raw []byte) string {
var v any
if json.Unmarshal(raw, &v) != nil {
return ""
}
b, _ := json.Marshal(v)
sum := sha256.Sum256(b)
return hex.EncodeToString(sum[:])
}
// desktopSyncModels writes p's inferenceModels as magpie lists them now,
// takes away the one magpie wrote when nothing is picked, and leaves the
// user's own alone.
func desktopSyncModels(p desktopPaths) error {
if !desktopWired(p) {
return nil
}
want, ok := gateway.DesktopInferenceModels()
if !ok {
return nil // the picks unknown: what is there stays
}
key := desktopModelsMark + p.key
marks := strings.Fields(stashShared()[key])
b, err := edit.Read(p.prof)
if err != nil || b == nil {
return err // no profile: desktopOn writes one, then this
}
raw, has := edit.GetJSONRaw(p.prof, "inferenceModels")
if has && !slices.Contains(marks, desktopModelsHash([]byte(raw))) {
return desktopSyncDiscovery(p, false) // the list is the user's
}
discovers, _ := gateway.DesktopDiscovers()
if err := desktopSyncDiscovery(p, want != nil && discovers); err != nil {
return err
}
if want == nil {
if has {
if err := edit.DelJSON(p.prof, "inferenceModels"); err != nil {
return err
}
}
forget(key)
return nil
}
next, err := json.Marshal(want)
if err != nil {
return err
}
h := desktopModelsHash(next)
if has && desktopModelsHash([]byte(raw)) == h {
stash(map[string]string{key: h})
return nil
}
// marked before it is written: a write cut short leaves a list magpie
// still knows for its own
stash(map[string]string{key: strings.Join(append(marks, h), " ")})
if err := edit.SetJSON(p.prof, edit.KV{Path: "inferenceModels", Value: json.RawMessage(next)}); err != nil {
return err
}
stash(map[string]string{key: h})
return nil
}
// desktopSyncDiscovery has Desktop read /v1/models beside magpie's
// inferenceModels while on, where Desktop's Code tab finds a later tier of
// a model picked for two (gateway.DesktopDiscovers), and takes back the
// modelDiscoveryEnabled magpie wrote once off. Unset, Desktop skips
// /v1/models while the list names no bare tier word.
func desktopSyncDiscovery(p desktopPaths, on bool) error {
key := desktopDiscoveryMark + p.key
ours := stashShared()[key] != ""
raw, has := edit.GetJSONRaw(p.prof, "modelDiscoveryEnabled")
if ours && (!has || strings.TrimSpace(raw) != "true") {
forget(key) // taken away or changed: the user's now
ours = false
}
switch {
case on && !has:
stash(map[string]string{key: "1"})
return edit.SetJSON(p.prof, edit.KV{Path: "modelDiscoveryEnabled", Value: true})
case !on && ours:
if err := edit.DelJSON(p.prof, "modelDiscoveryEnabled"); err != nil {
return err
}
forget(key)
}
return nil
}