diff --git a/internal/agent/legacy.go b/internal/agent/legacy.go index e418763ac..19c3aa060 100644 --- a/internal/agent/legacy.go +++ b/internal/agent/legacy.go @@ -8,6 +8,7 @@ import ( "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/gateway" + "github.com/yetone/magpie/internal/provider" ) // legacyID is what agents knew the gateway by before magpie was called magpie. @@ -60,3 +61,53 @@ func RenameLegacy() { } } } + +// MoveCursorEfforts moves an agent set to one of Cursor's ids at an effort +// (cursor/grok-4.7-low), from before magpie offered each family as one +// model, to that model (cursor/grok-4.7) spelled the same way, and — when +// the agent's effort is unset and it has that one — to the effort the id +// was at. A model the picker doesn't offer is left: the gateway still takes +// Cursor's id. It runs at start-up; errors are swallowed as RenameLegacy's. +func MoveCursorEfforts() { moveEfforts("cursor", provider.CursorBase) } + +func moveEfforts(pid string, base func(string) (string, string, bool)) { + offers := func(opts []Option, v string) bool { + return slices.ContainsFunc(opts, func(o Option) bool { return o.Value == v }) + } + for _, a := range All() { + if _, err := os.Stat(a.Path); err != nil { + continue + } + var vals map[string]string + for i := range a.Fields { + f := &a.Fields[i] + if f.Options == nil || f.Key == "effort" { + continue + } + v := f.Get() + pre, rest := "", v + if r, ok := strings.CutPrefix(v, magpieID+"/"); ok { + pre, rest = magpieID+"/", r + } + id, ok := strings.CutPrefix(rest, pid+"/") + if !ok { + continue + } + b, effort, ok := base(id) + if !ok { + continue + } + nv := pre + pid + "/" + b + if vals == nil { + vals = a.Values() + } + if !offers(f.Options(vals), nv) || f.Set(nv) != nil { + continue + } + vals[f.Key] = nv + if e := a.Field("effort"); f.Key == "model" && effort != "" && e != nil && e.Options != nil && e.Get() == "" && offers(e.Options(vals), effort) { + _ = e.Set(effort) + } + } + } +} diff --git a/internal/agent/legacy_test.go b/internal/agent/legacy_test.go index 60e106b7b..d67b5478a 100644 --- a/internal/agent/legacy_test.go +++ b/internal/agent/legacy_test.go @@ -97,3 +97,49 @@ func TestRenameLegacy(t *testing.T) { } } } + +// An agent set to one of Cursor's ids at an effort, from before magpie +// offered the family as one model, is set to that model, at that effort +// where its own is unset; an id magpie offers no model for is left. +func TestMoveCursorEfforts(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + if err := provider.Save(provider.Provider{ID: "curs", Name: "Curs", Chat: "https://curs.example/v1", Key: "k", Models: []string{"grok-4.7"}}); err != nil { + t.Fatal(err) + } + base := func(id string) (string, string, bool) { + if id == "grok-4.7-low" || id == "gone-low" { + return strings.TrimSuffix(id, "-low"), "low", true + } + return "", "", false + } + pi := filepath.Join(home, ".pi", "agent", "settings.json") + write := func(body string) { + if err := os.MkdirAll(filepath.Dir(pi), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(pi, []byte(body), 0o644); err != nil { + t.Fatal(err) + } + } + write(`{"defaultProvider": "magpie", "defaultModel": "curs/grok-4.7-low", "theme": "dark"}`) + moveEfforts("curs", base) + for k, want := range map[string]string{"defaultProvider": "magpie", "defaultModel": "curs/grok-4.7", "defaultThinkingLevel": "low", "theme": "dark"} { + if v, _ := edit.GetJSON(pi, k); v != want { + t.Errorf("%s = %q, want %q", k, v, want) + } + } + // an effort of its own stays; a model the picker doesn't offer stays + write(`{"defaultProvider": "magpie", "defaultModel": "curs/grok-4.7-low", "defaultThinkingLevel": "high"}`) + moveEfforts("curs", base) + if v, _ := edit.GetJSON(pi, "defaultThinkingLevel"); v != "high" { + t.Errorf("thinking level %q", v) + } + write(`{"defaultProvider": "magpie", "defaultModel": "curs/gone-low"}`) + moveEfforts("curs", base) + if v, _ := edit.GetJSON(pi, "defaultModel"); v != "curs/gone-low" { + t.Errorf("model %q", v) + } +} diff --git a/internal/gateway/chat.go b/internal/gateway/chat.go index 0a925a8af..ebe6c6554 100644 --- a/internal/gateway/chat.go +++ b/internal/gateway/chat.go @@ -46,6 +46,7 @@ type cRequest struct { Stream bool `json:"stream,omitempty"` ReasoningEffort string `json:"reasoning_effort,omitempty"` ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"` + ServiceTier string `json:"service_tier,omitempty"` } func parseChat(body []byte) (*Request, error) { @@ -54,7 +55,7 @@ func parseChat(body []byte) (*Request, error) { return nil, fmt.Errorf("invalid request: %v", err) } r := &Request{Model: c.Model, MaxTokens: c.MaxCompletionTokens, Temp: c.Temperature, TopP: c.TopP, - Stream: c.Stream, Effort: effortOf(c.ReasoningEffort), Parallel: c.ParallelToolCalls} + Stream: c.Stream, Effort: effortOf(c.ReasoningEffort), Parallel: c.ParallelToolCalls, Fast: c.ServiceTier == "priority"} if r.MaxTokens == 0 { r.MaxTokens = c.MaxTokens } diff --git a/internal/gateway/cursor.go b/internal/gateway/cursor.go index 897566bf4..442c386a2 100644 --- a/internal/gateway/cursor.go +++ b/internal/gateway/cursor.go @@ -41,13 +41,15 @@ import ( // cursorToken and cursorVersion are the sign-in and the CLI version the // API is told of, cursorAPI the API that says where a team's agent API is, // cursorAgent the one used when it can't say, and cursorVariants the ids a -// model magpie offers stands for; vars so tests can stand in for them. +// model magpie offers stands for, cursorBaseOf the other way round; vars +// so tests can stand in for them. var ( cursorToken = provider.CursorToken cursorVersion = provider.CursorClientVersion cursorAPI = "https://api2.cursor.sh" cursorAgent = "https://agentn.global.api5.cursor.sh" cursorVariants = provider.CursorVariants + cursorBaseOf = provider.CursorBase ) // cursorEndpoint is the agent API last picked, for the token (by its @@ -137,8 +139,24 @@ func cursorRegional(msg string) bool { // asked for: the family's variant at it; else, for an effort between the // ones it has, the one Cursor picks by default (gpt-5.2 is low, high and // one without an effort); else the nearest. Fast, when asked, is from the -// family's fast one. An id of Cursor's own goes as it is. +// family's fast one. An id of Cursor's own at an effort (grok-4.7-low, one +// an agent was set to before) is at the effort asked for, and fast, where +// Cursor has that id; else it goes as it is. func cursorModelID(model, effort string, fast bool) string { + if base, at, ok := cursorBaseOf(model); ok { + if effort == "" { + effort = at + } + if fast && !strings.HasSuffix(base, "-fast") { + if _, ok := cursorVariants(base + "-fast"); ok { + base += "-fast" + } + } + if vs, ok := cursorVariants(base); ok && vs[effort] != "" && effort != "" { + return vs[effort] + } + return model + } if fast && !strings.HasSuffix(model, "-fast") { if _, ok := cursorVariants(model + "-fast"); ok { model += "-fast" diff --git a/internal/gateway/cursor_test.go b/internal/gateway/cursor_test.go index a33306845..49e66159b 100644 --- a/internal/gateway/cursor_test.go +++ b/internal/gateway/cursor_test.go @@ -413,7 +413,8 @@ func TestCursorFailureWords(t *testing.T) { } // A model magpie offers goes to Cursor as the id its effort picks; one of -// Cursor's own ids goes as it is. +// Cursor's own ids at an effort goes at the effort asked for, where Cursor +// has that one, else as it is. func TestCursorModelID(t *testing.T) { t.Setenv("XDG_CACHE_HOME", t.TempDir()) var raw []catalog.Model @@ -459,7 +460,13 @@ claude-4.5-sonnet - Claude Sonnet 4.5`), "\n") { {"claude-4.6-opus", "medium", false, "claude-4.6-opus-high"}, {"claude-4.6-opus-thinking", "xhigh", false, "claude-4.6-opus-max-thinking"}, {"gpt-5.5", "xhigh", false, "gpt-5.5-extra-high"}, - {"grok-4.7-low-fast", "high", false, "grok-4.7-low-fast"}, // Cursor's own id + {"grok-4.7-low-fast", "high", false, "grok-4.7-high-fast"}, // Cursor's own id: the effort asked for wins + {"grok-4.7-low", "high", false, "grok-4.7-high"}, + {"grok-4.7-low", "", false, "grok-4.7-low"}, + {"grok-4.7-low", "", true, "grok-4.7-low-fast"}, + {"grok-4.7-low", "minimal", false, "grok-4.7-low"}, // Cursor has none at minimal + {"gpt-5.2-high", "low", true, "gpt-5.2-low"}, // no fast one + {"claude-4.6-opus-high-thinking", "max", false, "claude-4.6-opus-max-thinking"}, {"claude-4.5-sonnet", "high", false, "claude-4.5-sonnet"}, {"auto", "", false, "auto"}, } { diff --git a/internal/gateway/fast_test.go b/internal/gateway/fast_test.go index 017c32c5e..072a6dcb0 100644 --- a/internal/gateway/fast_test.go +++ b/internal/gateway/fast_test.go @@ -23,3 +23,15 @@ func TestResponsesFastTier(t *testing.T) { t.Error("flex read as fast") } } + +// Chat Completions says priority the same way (Cursor's fast ids). +func TestChatFastTier(t *testing.T) { + r, err := parseChat([]byte(`{"model":"cursor/grok-4.7","messages":[{"role":"user","content":"hi"}],"service_tier":"priority"}`)) + if err != nil || !r.Fast { + t.Fatalf("fast not read: %v %+v", err, r) + } + r, _ = parseChat([]byte(`{"model":"m","messages":[{"role":"user","content":"hi"}],"service_tier":"auto"}`)) + if r.Fast { + t.Error("auto read as fast") + } +} diff --git a/internal/gateway/ir.go b/internal/gateway/ir.go index c0b5b74c4..a997c0171 100644 --- a/internal/gateway/ir.go +++ b/internal/gateway/ir.go @@ -97,7 +97,7 @@ type Request struct { Thinking bool // the client asked for visible reasoning Parallel *bool // parallel tool calls allowed WebSearch bool // the client offered its provider's own web search - Fast bool // the client asked for priority processing (Codex's Fast mode) + Fast bool // the client asked for priority processing: service_tier priority (Codex's Fast mode) // Namespaced are the tools a Responses client offered inside a // namespace, by the flat name the model is offered them under. Namespaced map[string]nsTool diff --git a/internal/provider/cursor_models.go b/internal/provider/cursor_models.go index 06c4b3663..b7d236fe8 100644 --- a/internal/provider/cursor_models.go +++ b/internal/provider/cursor_models.go @@ -188,3 +188,49 @@ func cursorVariantsIn(raw []catalog.Model, model string) (map[string]string, boo } return nil, false } + +// CursorBase is the model magpie offers for one of Cursor's ids at an +// effort ("grok-4.7-low" is grok-4.7 at low, "grok-4.7-low-fast" +// grok-4.7-fast at low), and that effort; ok is false for an id that is +// itself a model magpie offers, or that no family has. Picks and agents' +// models saved before the families were one model still name these ids. +func CursorBase(id string) (base, effort string, ok bool) { + raw, _, live := catalog.Live("cursor") + if !live { + return "", "", false + } + return cursorBaseIn(raw, id) +} + +func cursorBaseIn(raw []catalog.Model, id string) (string, string, bool) { + family, effort := splitCursorID(id) + if family == id { + return "", "", false + } + for _, f := range cursorFamilies(raw) { + if f.id == family && len(f.variants) > 1 && slices.ContainsFunc(f.variants, func(m catalog.Model) bool { return m.ID == id }) { + return family, effort, true + } + } + return "", "", false +} + +// cursorPicks are the user's picks of Cursor's models with each of +// Cursor's own ids the model magpie offers for it, in the order picked, +// each once. +func cursorPicks(ids []string) []string { + raw, _, live := catalog.Live("cursor") + if !live { + return ids + } + var out []string + for _, id := range ids { + if base, _, ok := cursorBaseIn(raw, id); ok { + id = base + } + if !slices.Contains(out, id) { + out = append(out, id) + } + } + return out +} diff --git a/internal/provider/cursor_test.go b/internal/provider/cursor_test.go index 7154e56f6..741a4ce06 100644 --- a/internal/provider/cursor_test.go +++ b/internal/provider/cursor_test.go @@ -8,6 +8,8 @@ import ( "strings" "testing" "time" + + "github.com/yetone/magpie/internal/catalog" ) func TestParseCursorModels(t *testing.T) { @@ -85,6 +87,53 @@ func TestCollapseCursorModels(t *testing.T) { } } +// Picks saved before Cursor's efforts were one model name Cursor's own +// ids: each is the model magpie offers for it now, once, where it was, +// with its efforts; ids of a family of one, or none, stay. +func TestCursorLegacyPicks(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + was := cursorStatus + defer func() { cursorStatus = was }() + cursorStatus = &cliIdentity{name: "cursor-test", exe: func() string { return "/bin/sh" }, ask: func() (string, string, bool) { return "me@example.com", "Pro", true }} + raw := withCursorContexts(parseCursorModels(cursorListed)) + if err := catalog.SaveLive("cursor", "", raw); err != nil { + t.Fatal(err) + } + for id, want := range map[string]string{"grok-4.7-low": "grok-4.7|low", "grok-4.7-xhigh-fast": "grok-4.7-fast|xhigh", + "claude-4.6-opus-high-thinking": "claude-4.6-opus-thinking|high", "gpt-5.5-extra-high": "gpt-5.5|xhigh"} { + if b, e, ok := cursorBaseIn(raw, id); !ok || b+"|"+e != want { + t.Errorf("%s: %s|%s %v, want %s", id, b, e, ok, want) + } + } + for _, id := range []string{"grok-4.7", "gpt-5.3-codex-high-fast", "composer-2.5", "claude-4.6-sonnet-medium-thinking", "grok-4.7-high", "nope-low"} { + if b, _, ok := cursorBaseIn(raw, id); ok { + t.Errorf("%s taken for %s", id, b) + } + } + legacy := []string{"grok-4.7-low", "composer-2.5", "grok-4.7-medium", "grok-4.7-low-fast", "grok-4.7-xhigh", "grok-4.7-xhigh-fast", "gpt-5.3-codex-high-fast", "gone-model"} + if err := Save(Provider{ID: "cursor", Models: legacy}); err != nil { + t.Fatal(err) + } + p, err := Find("cursor") + if err != nil { + t.Fatal(err) + } + want := []string{"grok-4.7", "composer-2.5", "grok-4.7-fast", "gpt-5.3-codex-high-fast", "gone-model"} + if strings.Join(p.Models, ",") != strings.Join(want, ",") { + t.Fatalf("picks %v, want %v", p.Models, want) + } + var got []string + for _, m := range p.Exposed() { + got = append(got, m.ID+"|"+strings.Join(m.Efforts, ",")) + } + if strings.Join(got, " ") != "grok-4.7|low,medium,xhigh composer-2.5| grok-4.7-fast|low,medium,xhigh gpt-5.3-codex-high-fast| gone-model|" { + t.Fatalf("exposed %v", got) + } +} + // Cursor's names say which of its models hold 1M; the rest hold its 200K func TestCursorContext(t *testing.T) { for _, c := range []struct { diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 2698a59d7..bb35938eb 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -210,6 +210,9 @@ func All() []Provider { } pk := picks[a.ID] a.Models, a.Unlisted, a.Fallback, a.Routing, a.Affinity, a.Contexts, a.Family = pk.Models, pk.Unlisted, pk.Fallback, pk.Routing, pk.Affinity, pk.Contexts, pk.Family + if a.ID == "cursor" { // picked before its efforts were one model + a.Models = cursorPicks(a.Models) + } out = append(out, a) } return out diff --git a/main.go b/main.go index 205703886..53811bbb5 100644 --- a/main.go +++ b/main.go @@ -104,6 +104,7 @@ func main() { func run(args []string) error { settings.Migrate() agent.RenameLegacy() + agent.MoveCursorEfforts() // a provider added, edited or removed, or a list fetched anew, reaches // the model lists agents keep in files of their own catalog.Changed = agent.SyncCatalog