diff --git a/packages/cursor/README.md b/packages/cursor/README.md index f25d392..416e93f 100644 --- a/packages/cursor/README.md +++ b/packages/cursor/README.md @@ -73,25 +73,42 @@ plugin's `fetch` answers them on Cursor's agent API: ## Models The `config` hook declares `auto` (Cursor's pick). Once signed in, the -`provider.models` hook lists the account's models -(`AgentService/GetUsableModels`, cached 10 minutes), by the ids -`cursor-agent models` shows: +`provider.models` hook lists the account's models from Cursor's model +picker (`AiService/AvailableModels`, as the CLI asks for it, cached 10 +minutes), the way pi-cursor-sdk lists Cursor's catalog: -- Cursor lists a model once per effort (`gpt-5.6-low`, `gpt-5.6-high`, …). - Each family is one model here, with the efforts as variants; the - request's `reasoning_effort` picks the id, fitted to the nearest one there - is. `-fast` and `-thinking` models stay models of their own; a - `service_tier` of `priority` picks the fast one. -- The context window is 1M for a model whose name says 1M, else 200K. -- Cursor's model picker (`AiService/AvailableModels`, as the CLI asks for - it) adds the models it offers that the usable list leaves out (GLM-5.3, - GLM-5.3 Flash), run with the picker variant's parameters; hidden and - Tab-only models stay out. -- A model Cursor serves only in Max Mode (the usable list's `maxMode`, a - picker model with no other mode, or a Max Mode variant) is asked for in - Max Mode, as the CLI turns it on for one. A model neither says it of is - asked again in Max Mode when Cursor answers "Max Mode Required", and so - from then on. +- A model with a `context` parameter is listed once per size, as + `@` named ` @ ` (`claude-opus-5-5@300k`, + "Claude Opus 5.5 @ 300k"), its window that many tokens. A model with none + is listed once, its window Cursor's `contextTokenLimit` (Max Mode's for a + model served only in Max Mode); 200K when Cursor gives none. +- Effort is not a model of its own: the model's efforts are its variants, + and the request's `reasoning_effort` (fitted to the nearest there is) is + sent as the model's effort parameter. `none` turns a Claude's thinking off. +- Fast is not a model of its own either: a `service_tier` of `priority` + or `fast` sends `fast=true`, anything else `fast=false` (it costs more, + so only when asked). +- A request goes as the picker's variant those parameters pick, in Max Mode + when that variant is Max Mode's (a 1M size, say), as the CLI turns it on. + A size with no variant goes in Max Mode when it is over Cursor's limit + outside it. A model Cursor still answers "Max Mode Required" for is asked + again in Max Mode, and so from then on. +- Hidden, Tab-only and chat-only models stay out, as in the CLI's picker. +- The usable list (`AgentService/GetUsableModels`, what `cursor-agent + models` shows) says which variants the account has: a variant whose own + id isn't in it is neither offered nor asked for (Sonnet 4.6 at any effort + but medium, say), and a request for one goes as the nearest the account + has. A model the usable list has nothing of is the picker's alone + (GLM-5.3) and is kept whole; so is every model when Cursor can't give + the usable list. +- An id from before 0.2.0 still runs. A variant's own id + (`claude-opus-5-5-high`) is that variant; a family 0.1.x listed + (`claude-opus-5-5`, `claude-opus-5-5-fast`, `claude-haiku-5-5-thinking`, + `cursor-grok-4.6`) is its variant nearest the default, fast for a `-fast` + one; the picker's name for a model or a legacy slug of it is its default. + Each is at the default context size, the request's `reasoning_effort` + and `service_tier` counting as for a listed model. The list doesn't show + these ids: pick the `@` model for a window that is the model's. ## Not included diff --git a/packages/cursor/context.test.mjs b/packages/cursor/context.test.mjs new file mode 100644 index 0000000..985cb39 --- /dev/null +++ b/packages/cursor/context.test.mjs @@ -0,0 +1,199 @@ +// The list is Cursor's picker, as pi-cursor-sdk lists Cursor's catalog: a +// model once for each context size, named " @ <size>" with that +// window; one without sizes once, with Cursor's limit. Effort and fast are +// the request's, sent as the variant's parameters. Nothing reaches Cursor. +import "./nonet.mjs" +import { afterEach, expect, test } from "bun:test" +import { CursorAuthPlugin, _internal } from "./index.mjs" + +const { usable, request } = _internal +const real = globalThis.fetch +afterEach(() => (globalThis.fetch = real)) +let n = 0 +const tok = () => "tok-context-" + ++n + +const en = (...vs) => ({ enumParameter: { values: vs.map((value) => ({ value })) } }) +const bool = { booleanParameter: { values: [{ value: "false" }, { value: "true" }] } } +const v = (pv, o = {}) => ({ parameterValues: Object.entries(pv).map(([id, value]) => ({ id, value })), ...o }) +export const PICKER = [ + { + name: "claude-opus-5-5", legacySlugs: ["claude-5.5-opus"], clientDisplayName: "Claude Opus 5.5", contextTokenLimit: 300000, contextTokenLimitForMaxMode: 1000000, supportsNonMaxMode: true, + parameterDefinitions: [{ id: "context", parameterType: en("300k", "1m") }, { id: "effort", parameterType: en("low", "medium", "high", "xhigh", "max") }, { id: "fast", parameterType: bool }], + variants: [ + v({ context: "300k", effort: "medium", fast: "false" }, { isDefaultNonMaxConfig: true, legacySlug: "claude-opus-5-5-medium" }), + v({ context: "300k", effort: "high", fast: "false" }, { legacySlug: "claude-opus-5-5-high" }), + v({ context: "300k", effort: "medium", fast: "true" }, { isMaxMode: true, legacySlug: "claude-opus-5-5-medium-fast" }), + v({ context: "300k", effort: "high", fast: "true" }, { isMaxMode: true, legacySlug: "claude-opus-5-5-high-fast" }), + v({ context: "1m", effort: "medium", fast: "false" }, { isMaxMode: true, isDefaultMaxConfig: true, legacySlug: "claude-opus-5-5-medium" }), + ], + }, + { + name: "claude-haiku-5-5", clientDisplayName: "Claude Haiku 5.5", contextTokenLimit: 300000, contextTokenLimitForMaxMode: 1000000, supportsNonMaxMode: true, + parameterDefinitions: [{ id: "thinking", parameterType: bool }, { id: "context", parameterType: en("300k", "1m") }, { id: "reasoning_effort", parameterType: en("low", "medium", "high") }], + variants: [v({ thinking: "true", context: "300k", reasoning_effort: "high" }, { isDefaultNonMaxConfig: true, legacySlug: "claude-haiku-5-5-thinking-high" }), v({ thinking: "true", context: "300k", reasoning_effort: "low" }, { legacySlug: "claude-haiku-5-5-thinking-low" }), v({ thinking: "false", context: "300k", reasoning_effort: "high" }, { legacySlug: "claude-haiku-5-5-high" }), v({ thinking: "false", context: "300k", reasoning_effort: "low" }, { legacySlug: "claude-haiku-5-5-low" })], + }, + { + name: "grok-4.7", clientDisplayName: "Grok 4.7", contextTokenLimit: 256000, contextTokenLimitForMaxMode: 500000, supportsNonMaxMode: true, + parameterDefinitions: [{ id: "context", parameterType: en("256k", "500k") }, { id: "reasoning_effort", parameterType: en("low", "high") }, { id: "fast", parameterType: bool }], + variants: [v({ context: "256k", reasoning_effort: "high", fast: "true" }, { isDefaultNonMaxConfig: true, legacySlug: "grok-4.7-high-fast" }), v({ context: "256k", reasoning_effort: "low", fast: "true" }, { legacySlug: "grok-4.7-low-fast" }), v({ context: "256k", reasoning_effort: "high", fast: "false" }, { legacySlug: "grok-4.7-high" }), v({ context: "256k", reasoning_effort: "low", fast: "false" }, { legacySlug: "grok-4.7-low" }), v({ context: "500k", reasoning_effort: "high", fast: "true" }, { isMaxMode: true })], + }, + { name: "grok-4.6", clientDisplayName: "Grok 4.6", contextTokenLimit: 256000, contextTokenLimitForMaxMode: 256000, supportsNonMaxMode: true, parameterDefinitions: [{ id: "effort", parameterType: en("low", "high") }], variants: [v({ effort: "high" }, { isDefaultNonMaxConfig: true, legacySlug: "cursor-grok-4.6-high" }), v({ effort: "low" }, { legacySlug: "cursor-grok-4.6-low" })] }, + { name: "composer-2.5", clientDisplayName: "Composer 2.5", contextTokenLimit: 200000, supportsNonMaxMode: true, parameterDefinitions: [{ id: "fast", parameterType: bool }], variants: [v({ fast: "true" }, { isDefaultNonMaxConfig: true, legacySlug: "composer-2.5-fast" }), v({ fast: "false" }, { legacySlug: "composer-2.5" })] }, + { name: "default", clientDisplayName: "Auto", supportsNonMaxMode: true, variants: [v({}, { isDefaultNonMaxConfig: true, legacySlug: "default" })] }, + { name: "max-only", clientDisplayName: "Max Only", contextTokenLimit: 200000, contextTokenLimitForMaxMode: 400000, supportsNonMaxMode: false }, + { name: "secret", clientDisplayName: "Secret", isHidden: true }, + { name: "tab-only", clientDisplayName: "Tab", onlySupportsCmdK: true }, + // the CLI's picker leaves these out + { name: "claude-4.5-haiku", clientDisplayName: "Haiku 4.5", contextTokenLimit: 200000, supportsNonMaxMode: true }, + { name: "claude-haiku-4-5", clientDisplayName: "Claude Haiku 4.5", contextTokenLimit: 200000, supportsNonMaxMode: true }, +] + +export function fakeAPI(picker = PICKER, usableIDs) { + globalThis.fetch = async (url) => { + const u = String(url) + if (u.endsWith("/agent.v1.AgentService/GetUsableModels")) return usableIDs ? Response.json({ models: usableIDs.map((modelId) => ({ modelId })) }) : new Response("down", { status: 503 }) + if (u.endsWith("/aiserver.v1.AiService/AvailableModels")) return picker ? Response.json({ models: picker }) : new Response("down", { status: 503 }) + throw new Error("the test asked " + u) + } +} + +test("a model once for each context size, named with it and with that window", async () => { + fakeAPI() + const raw = await usable(tok()) + const by = Object.fromEntries(raw.map((m) => [m.id, [m.name, m.context]])) + expect(by).toEqual({ + "claude-opus-5-5@300k": ["Claude Opus 5.5 @ 300k", 300000], + "claude-opus-5-5@1m": ["Claude Opus 5.5 @ 1m", 1000000], + "claude-haiku-5-5@300k": ["Claude Haiku 5.5 @ 300k", 300000], + "claude-haiku-5-5@1m": ["Claude Haiku 5.5 @ 1m", 1000000], + "grok-4.7@256k": ["Grok 4.7 @ 256k", 256000], + "grok-4.7@500k": ["Grok 4.7 @ 500k", 500000], + // no sizes: Cursor's limit (for a Max Mode only model, Max Mode's) + "grok-4.6": ["Grok 4.6", 256000], + "composer-2.5": ["Composer 2.5", 200000], + "max-only": ["Max Only", 400000], + // Cursor's default, Auto; no limit given + auto: ["Auto", 200000], + }) + // hidden, Tab-only and the ones the CLI's picker leaves out are not there + for (const gone of ["secret", "tab-only", "claude-4.5-haiku", "claude-haiku-4-5"]) expect(raw.some((m) => m.run === gone)).toBe(false) +}) + +test("the provider's list, efforts as variants, no model of its own for fast or an effort", async () => { + fakeAPI() + const hooks = await CursorAuthPlugin() + const jwt = ["e30", Buffer.from(JSON.stringify({ exp: Math.floor(Date.now() / 1000) + 7200 + ++n })).toString("base64url"), "s"].join(".") + const list = await hooks.provider.models({ models: {} }, { auth: { type: "oauth", access: jwt, refresh: "", expires: 0 } }) + expect(list["claude-opus-5-5@300k"].limit.context).toBe(300000) + expect(Object.keys(list["claude-opus-5-5@300k"].variants)).toEqual(["low", "medium", "high", "xhigh", "max"]) + expect(Object.keys(list["claude-haiku-5-5@300k"].variants)).toEqual(["low", "medium", "high"]) + expect(list["composer-2.5"].capabilities.reasoning).toBe(false) + expect(Object.keys(list).some((id) => /-fast$|-(low|medium|high)$/.test(id))).toBe(false) +}) + +test("a request goes as the variant its size, effort and fast pick, in that variant's mode", async () => { + fakeAPI() + const raw = await usable(tok()) + const p = (r) => Object.fromEntries(r.params.map((x) => [x.id, x.value])) + // the variant's own id, which the agent API takes (not the picker's name) + let r = request(raw, "claude-opus-5-5@300k", "", undefined) + expect([r.id, r.maxMode, p(r)]).toEqual(["claude-opus-5-5-medium", false, { context: "300k", effort: "medium", fast: "false" }]) + r = request(raw, "claude-opus-5-5@300k", "high", true) + expect([r.id, r.maxMode, p(r)]).toEqual(["claude-opus-5-5-high-fast", true, { context: "300k", effort: "high", fast: "true" }]) + r = request(raw, "claude-opus-5-5@1m", "medium", undefined) + expect([r.id, r.maxMode, p(r).context]).toEqual(["claude-opus-5-5-medium", true, "1m"]) + expect(request(raw, "grok-4.6", "low", undefined).id).toBe("cursor-grok-4.6-low") + expect(request(raw, "composer-2.5", "", undefined).id).toBe("composer-2.5") + // an effort it lacks: the nearest + expect(p(request(raw, "claude-opus-5-5@300k", "minimal", undefined)).effort).toBe("low") + // a Claude asked not to think + expect(p(request(raw, "claude-haiku-5-5@300k", "none", undefined))).toEqual({ thinking: "false", context: "300k", reasoning_effort: "high" }) + // fast off unless asked, though Cursor's default has it on + expect(p(request(raw, "grok-4.7@256k", "high", undefined)).fast).toBe("false") + expect(p(request(raw, "composer-2.5", "", true))).toEqual({ fast: "true" }) + // a size more than Cursor allows outside Max Mode, with no variant for it + r = request(raw, "grok-4.7@500k", "low", false) + expect([r.maxMode, p(r)]).toEqual([true, { context: "500k", reasoning_effort: "low", fast: "false" }]) + expect(request(raw, "max-only", "", undefined).maxMode).toBe(true) + expect(request(raw, "auto", "", undefined)).toMatchObject({ id: "default", params: [], maxMode: false }) + // not in the list: as it is named + expect(request(raw, "some-cursor-id", "", undefined)).toEqual({ id: "some-cursor-id", params: [], maxMode: false }) +}) + +test("a picker Cursor can't give is a failure, not an empty list", async () => { + fakeAPI(null) + await expect(usable(tok())).rejects.toThrow() +}) + +// what 0.1.11 listed for this picker, each family of the variants' ids one +// model (offered() there, given the legacySlugs above as the usable list): +// claude-opus-5-5, claude-opus-5-5-fast, claude-haiku-5-5-thinking, +// claude-haiku-5-5, grok-4.7-fast, grok-4.7, cursor-grok-4.6, +// composer-2.5-fast, composer-2.5, default. They are in agents' configs +// and magpie's picks; the picker's bare name is a 502 from the agent API. +test("an id 0.1.11 listed still runs, as the variant it stood for, at the effort asked", async () => { + fakeAPI() + const raw = await usable(tok()) + const q = (id, effort = "", fast) => { + const r = request(raw, id, effort, fast) + return [r.id, r.maxMode, Object.fromEntries(r.params.map((x) => [x.id, x.value]))] + } + // a family that is the picker's name too: its default size, not fast + expect(q("claude-opus-5-5")).toEqual(["claude-opus-5-5-medium", false, { context: "300k", effort: "medium", fast: "false" }]) + expect(q("claude-opus-5-5", "high")).toEqual(["claude-opus-5-5-high", false, { context: "300k", effort: "high", fast: "false" }]) + expect(q("claude-opus-5-5", "", true)).toEqual(["claude-opus-5-5-medium-fast", true, { context: "300k", effort: "medium", fast: "true" }]) + expect(q("grok-4.7", "low")).toEqual(["grok-4.7-low", false, { context: "256k", reasoning_effort: "low", fast: "false" }]) + expect(q("cursor-grok-4.6")).toEqual(["cursor-grok-4.6-high", false, { effort: "high" }]) + expect(q("cursor-grok-4.6", "low")).toEqual(["cursor-grok-4.6-low", false, { effort: "low" }]) + // a -fast family: fast, though the request doesn't ask + expect(q("claude-opus-5-5-fast")).toEqual(["claude-opus-5-5-medium-fast", true, { context: "300k", effort: "medium", fast: "true" }]) + expect(q("claude-opus-5-5-fast", "high")).toEqual(["claude-opus-5-5-high-fast", true, { context: "300k", effort: "high", fast: "true" }]) + expect(q("grok-4.7-fast", "low")).toEqual(["grok-4.7-low-fast", false, { context: "256k", reasoning_effort: "low", fast: "true" }]) + expect(q("composer-2.5-fast")).toEqual(["composer-2.5-fast", false, { fast: "true" }]) + // a Claude with thinking apart from one without + expect(q("claude-haiku-5-5-thinking", "low")).toEqual(["claude-haiku-5-5-thinking-low", false, { thinking: "true", context: "300k", reasoning_effort: "low" }]) + expect(q("claude-haiku-5-5")).toEqual(["claude-haiku-5-5-high", false, { thinking: "false", context: "300k", reasoning_effort: "high" }]) + expect(q("claude-haiku-5-5", "low")).toEqual(["claude-haiku-5-5-low", false, { thinking: "false", context: "300k", reasoning_effort: "low" }]) + expect(q("default")).toEqual(["default", false, {}]) +}) + +test("a variant's own id, and a model's legacy slug, run as what they name", async () => { + fakeAPI() + const raw = await usable(tok()) + const q = (id, effort = "", fast) => { + const r = request(raw, id, effort, fast) + return [r.id, r.maxMode, Object.fromEntries(r.params.map((x) => [x.id, x.value]))] + } + expect(q("claude-opus-5-5-high")).toEqual(["claude-opus-5-5-high", false, { context: "300k", effort: "high", fast: "false" }]) + expect(q("claude-opus-5-5-high-fast")).toEqual(["claude-opus-5-5-high-fast", true, { context: "300k", effort: "high", fast: "true" }]) + // the request's effort and tier still count + expect(q("claude-opus-5-5-high", "medium")[0]).toBe("claude-opus-5-5-medium") + expect(q("claude-opus-5-5-high", "", true)[0]).toBe("claude-opus-5-5-high-fast") + // an id two sizes share: the default size, outside Max Mode + expect(q("claude-opus-5-5-medium")).toEqual(["claude-opus-5-5-medium", false, { context: "300k", effort: "medium", fast: "false" }]) + // an effort no variant here is named for: the family, at that effort + expect(q("claude-opus-5-5-xhigh")[2]).toEqual({ context: "300k", effort: "xhigh", fast: "false" }) + expect(q("claude-5.5-opus")).toEqual(["claude-opus-5-5-medium", false, { context: "300k", effort: "medium", fast: "false" }]) +}) + +test("a variant the usable list says the account hasn't got is neither offered nor asked for", async () => { + // Opus at medium only, Grok 4.6 at high only; nothing of Haiku or Grok 4.7, + // which are the picker's alone and kept whole + fakeAPI(PICKER, ["claude-opus-5-5-medium", "cursor-grok-4.6-high", "composer-2.5", "composer-2.5-fast", "default"]) + const raw = await usable(tok()) + const levels = Object.fromEntries(raw.map((m) => [m.id, Object.keys(m.levels)])) + expect(levels["claude-opus-5-5@300k"]).toEqual(["medium"]) + expect(levels["claude-opus-5-5@1m"]).toEqual(["medium"]) + expect(levels["grok-4.6"]).toEqual(["high"]) + expect(levels["claude-haiku-5-5@300k"]).toEqual(["low", "medium", "high"]) + expect(levels["grok-4.7@256k"]).toEqual(["low", "high"]) + const q = (id, effort = "", fast) => { + const r = request(raw, id, effort, fast) + return [r.id, r.maxMode, Object.fromEntries(r.params.map((x) => [x.id, x.value]))] + } + // high and fast, which it hasn't: the variant it has + expect(q("claude-opus-5-5@300k", "high", true)).toEqual(["claude-opus-5-5-medium", false, { context: "300k", effort: "medium", fast: "false" }]) + expect(q("claude-opus-5-5@1m", "high")).toEqual(["claude-opus-5-5-medium", true, { context: "1m", effort: "medium", fast: "false" }]) + expect(q("cursor-grok-4.6", "low")).toEqual(["cursor-grok-4.6-high", false, { effort: "high" }]) + expect(q("composer-2.5", "", true)).toEqual(["composer-2.5-fast", false, { fast: "true" }]) +}) diff --git a/packages/cursor/index.mjs b/packages/cursor/index.mjs index 42a9425..8990877 100644 --- a/packages/cursor/index.mjs +++ b/packages/cursor/index.mjs @@ -39,7 +39,7 @@ const WEBSITE = "https://cursor.com" const AGENT = "https://agentn.global.api5.cursor.sh" // when the server config can't say const CHAT = "@ai-sdk/openai-compatible" const VERSION_FALLBACK = "2026.09.23-86fc751" // the CLI version said when no install names one -const DEFAULT_CONTEXT = 200_000 // what Cursor gives a model it doesn't name as a 1M one +const DEFAULT_CONTEXT = 200_000 // a model Cursor gives no limit for const MODELS_KEEP = 10 * 60 * 1000 const CALL = "CallDynamicTool" // how the model calls an MCP tool const NO_RESULT = "Tool use was interrupted and did not produce a result." @@ -351,10 +351,11 @@ const statusText = (s) => const POOL_VARIANT = /-(fast|none|low|medium|high|xhigh|extra-high|max|thinking)$/ -// poolBase names a model's family: lower case, without cursor- and the -// effort and speed the CLI adds to it. +// poolBase names a model's family: lower case, without cursor-, the +// context size the list adds to it (@300k) and the effort and speed the +// CLI adds to it. function poolBase(model) { - model = String(model).trim().toLowerCase() + model = String(model).trim().toLowerCase().replace(/@[^@]*$/, "") if (model.startsWith("cursor-")) model = model.slice("cursor-".length) for (;;) { const b = model.replace(POOL_VARIANT, "") @@ -368,6 +369,7 @@ function poolBase(model) { // Keep those documented families alongside the server's exact model list. // See https://cursor.com/docs/models-and-pricing. function firstParty(model) { + model = model.replace(/@[^@]*$/, "") if (model.startsWith("cursor-")) model = model.slice("cursor-".length) if (model === "default" || model === "composer" || model.startsWith("composer-")) return true return ["grok-4.5", "grok-4.6", "grok-4.7"].some((b) => model === b || model.startsWith(b + "-")) @@ -413,26 +415,29 @@ async function usage(tok, ids) { // ---- models ---------------------------------------------------------------------- // -// Cursor lists a model once for each effort and speed it serves it at -// ("grok-4.7-low", "grok-4.7-low-fast", … "grok-4.7-xhigh-fast"), and a -// Claude with thinking apart from one without. One model a family is -// offered, with the efforts there are, and the request asks for the id the -// effort picks. Fast stays a model of its own. +// The list is Cursor's model picker (AiService/AvailableModels, asked as the +// CLI asks it), the way pi-cursor-sdk lists Cursor's SDK catalog: each model +// is offered once for each of its context sizes, named "Claude Opus 5.5 @ +// 300k" and with that many tokens as its window; one without a context +// parameter is offered once, with Cursor's limit for it. Effort is the +// request's reasoning_effort and fast its service_tier, sent as the model's +// own parameters, so neither is a model of its own. A request goes as the +// variant those parameters pick, in Max Mode when that variant is Max Mode's. -// the effort words of Cursor's ids, the levels they are and the words -// Cursor's names say them in; extra-high before high -const EFFORTS = [ - ["extra-high", "xhigh", "Extra High"], ["xhigh", "xhigh", "Extra High"], ["minimal", "minimal", "Minimal"], - ["none", "none", "None"], ["low", "low", "Low"], ["medium", "medium", "Medium"], ["high", "high", "High"], ["max", "max", "Max"], -] const LEVEL_RANK = ["none", "minimal", "low", "medium", "high", "xhigh", "max"] -const EFFORT_RANK = ["none", "minimal", "low", "medium", "high", "xhigh", "max", "ultra"] +const EFFORT_PARAMS = ["effort", "reasoning_effort", "reasoning"] + +// the effort words of Cursor's own ids and the levels they are; extra-high +// before high +const ID_EFFORTS = [["extra-high", "xhigh"], ["xhigh", "xhigh"], ["minimal", "minimal"], ["none", "none"], ["low", "low"], ["medium", "medium"], ["high", "high"], ["max", "max"]] const cut = (s, suffix) => (s.endsWith(suffix) && s.length > suffix.length ? s.slice(0, -suffix.length) : null) -// splitID is a Cursor id taken apart: its family, and the effort ("" for -// none named) it is at. Thinking comes before the effort or after it; in -// the family it goes after, as does fast. +// splitID is one of Cursor's own ids (a variant's legacySlug, as +// cursor-agent models lists it) taken apart: its family, and the effort +// ("" for none named) it is at. Thinking comes before the effort or after +// it; in the family it goes after, as does fast. The family is the id 0.1.x +// listed a model by. function splitID(id) { let s = id let fast = false @@ -441,7 +446,7 @@ function splitID(id) { let b if ((b = cut(s, "-fast")) !== null) [s, fast] = [b, true] if ((b = cut(s, "-thinking")) !== null) [s, thinking] = [b, true] - for (const [word, level] of EFFORTS) { + for (const [word, level] of ID_EFFORTS) { if ((b = cut(s, "-" + word)) !== null) { ;[s, effort] = [b, level] break @@ -453,248 +458,30 @@ function splitID(id) { return [s, effort] } -const labelOf = (level) => EFFORTS.find((e) => e[1] === level)?.[2] ?? "" - -// withoutWords is name with the first run of these words taken out, and -// whether it had them. -function withoutWords(name, words) { - const ns = name.split(/\s+/).filter(Boolean) - const ws = words.split(/\s+/).filter(Boolean) - if (!ws.length) return [name, false] - for (let i = 0; i + ws.length <= ns.length; i++) - if (ws.every((w, j) => ns[i + j] === w)) return [[...ns.slice(0, i), ...ns.slice(i + ws.length)].join(" "), true] - return [name, false] -} - -function families(raw) { - const out = [] - const by = new Map() - for (const m of raw) { - const [id, effort] = splitID(m.id) - let f = by.get(id) - if (!f) { - f = { id, variants: [], efforts: [] } - by.set(id, f) - out.push(f) - } - f.variants.push(m) - f.efforts.push(effort) - } - return out -} - -// byEffort is the id for each effort, "" the one Cursor picks by default: -// the one without an effort in its id, else the one whose name doesn't say -// its effort, else medium, else the first. -function byEffort(f) { - const out = {} - let def = f.efforts.indexOf("") - f.variants.forEach((v, i) => { - const e = f.efforts[i] - if (!(e in out)) out[e] = v.id - if (def < 0 && e !== "" && !withoutWords(v.name, labelOf(e))[1]) def = i - }) - if (def < 0) def = Math.max(0, f.efforts.indexOf("medium")) - out[""] = f.variants[def].id - return out -} - -function familyModel(f) { - const def = byEffort(f)[""] - const m = { id: f.id, name: f.id, context: 0, efforts: [] } - f.variants.forEach((v, i) => { - if (v.id === def) m.name = withoutWords(v.name, labelOf(f.efforts[i]))[0] - if (v.context > 0 && (!m.context || v.context < m.context)) m.context = v.context - }) - for (const l of LEVEL_RANK) if (f.efforts.includes(l)) m.efforts.push(l) - return m -} - -// contextOf is how much of a conversation Cursor lets a model hold: what -// the name says ("Claude Opus 5.5 1M"), else Cursor's default. -function contextOf(name) { - const m = /\b(\d+)M\b/.exec(name) - return m ? Number(m[1]) * 1_000_000 : DEFAULT_CONTEXT -} - -const CAPACITY = /\(\s*\d+M\s*\)|\b\d+M\b/g - -// withoutCapacity takes the context out of each name, which is shown apart; -// a name that would then be another model's keeps it. -function withoutCapacity(ms) { - const strip = (n) => n.replace(CAPACITY, " ").split(/\s+/).filter(Boolean).join(" ") - const names = new Map() - const add = (n) => names.set(n.toLowerCase(), (names.get(n.toLowerCase()) ?? 0) + 1) - for (const m of ms) { - add(m.name) - const s = strip(m.name) - if (s !== m.name) add(s) - } - return ms.map((m) => { - const s = strip(m.name) - return s === m.name || !s || names.get(s.toLowerCase()) > 1 ? m : { ...m, name: s } - }) -} - -// offered is Cursor's list as it is offered: each family one model. -function offered(raw) { - return withoutCapacity(families(raw).map((f) => (f.variants.length === 1 ? { ...f.variants[0], efforts: [] } : familyModel(f)))) -} - -function variantsIn(raw, model) { - const f = families(raw).find((f) => f.id === model && f.variants.length > 1) - return f ? byEffort(f) : null -} - -function baseIn(raw, id) { - const [family, effort] = splitID(id) - if (family === id) return null - const f = families(raw).find((f) => f.id === family && f.variants.length > 1 && f.variants.some((m) => m.id === id)) - return f ? [family, effort] : null -} - -function fitEffort(want, levels) { - if (want === "ultra" && !levels.includes(want)) want = "max" - if (!levels.length || levels.includes(want)) return want - const at = EFFORT_RANK.indexOf(want) - if (at < 0) return want - let best = want - let dist = EFFORT_RANK.length - for (const l of levels) { - const i = EFFORT_RANK.indexOf(l) - if (i < 0 || l === "none") continue - const d = Math.abs(i - at) - if (d < dist || (d === dist && i > at)) [best, dist] = [l, d] - } - return best -} - -// modelID is Cursor's id for a model offered, at the effort asked for: the -// family's variant at it; else, for an effort between the ones it has, the -// one Cursor picks by default; else the nearest. Fast, when asked, is from -// the family's fast one. An id of Cursor's own at an effort is at the -// effort asked for where Cursor has that id; else it goes as it is. -function modelID(raw, model, effort, fast) { - const base = baseIn(raw, model) - if (base) { - let [b, at] = base - effort ||= at - if (fast && !b.endsWith("-fast") && variantsIn(raw, b + "-fast")) b += "-fast" - const vs = variantsIn(raw, b) - return vs && effort && vs[effort] ? vs[effort] : model - } - if (fast && !model.endsWith("-fast") && variantsIn(raw, model + "-fast")) model += "-fast" - const vs = variantsIn(raw, model) - if (!vs) return model - if (!effort) return vs[""] - if (vs[effort]) return vs[effort] - const levels = [] - let unnamed = true // the default has no effort of its own - for (const l of EFFORT_RANK) { - if (vs[l]) { - levels.push(l) - unnamed &&= vs[l] !== vs[""] - } - } - if (!levels.length) return vs[""] - const at = EFFORT_RANK.indexOf(effort) - if (unnamed && at > EFFORT_RANK.indexOf(levels[0]) && at < EFFORT_RANK.indexOf(levels.at(-1))) return vs[""] - return vs[fitEffort(effort, levels)] -} - -// usable is the account's list: what `cursor-agent models` has -// (GetUsableModels: its ids, names and the id each is run by, and whether -// Cursor serves it in Max Mode), and the models Cursor's model picker -// offers that it leaves out (parameterized). Kept a while, by token. -const lists = new Map() -async function usable(tok) { - const h = createHash("sha256").update(tok).digest("hex") - const had = lists.get(h) - if (had && Date.now() - had.at < MODELS_KEEP) return had.raw - const [j, picker] = await Promise.all([ - unary(API, "agent.v1.AgentService/GetUsableModels", tok, {}, 30_000), - parameterized(tok), - ]) - const raw = [] - for (const m of j?.models ?? []) { - const id = m.displayModelId || m.modelId - if (!id) continue - // some names come with zero-width spaces and doubled ones - let name = String(m.displayName || id).replaceAll("​", "").split(/\s+/).filter(Boolean).join(" ") - name = name.replace(/\(default\)$/, "").trim().replace(/\(current\)$/, "").trim() - const run = m.modelId || id - // Max Mode as the CLI sends it: the list's own say, else the picker's - // (a model with no other mode, or a variant that is Max Mode's) - const maxMode = m.maxMode === true || !!(picker.slugs.get(id)?.maxMode || picker.slugs.get(run)?.maxMode) - raw.push({ id, name, context: contextOf(name), run, ...(maxMode ? { maxMode } : {}) }) - } - if (!raw.length) throw new Error("Cursor listed no models") - for (const m of picker.extra(raw)) raw.push(m) - lists.set(h, { at: Date.now(), raw }) - return raw -} - // left out of the CLI's picker as it leaves them out (model-service.ts) -const PICKER_SKIP = new Set(["claude-4.5-haiku", "claude-4.5-haiku-thinking", "gemini-2.5-pro", "gemini-2.5-flash"]) +const PICKER_SKIP = new Set(["claude-4.5-haiku", "claude-4.5-haiku-thinking", "gemini-2.5-pro", "gemini-2.5-flash", "claude-haiku-4-5"]) -// needsMax is whether a variant of a picker's model is run in Max Mode -// only, as the CLI reads it: a model with no other mode, or a Max Mode -// variant. -const needsMax = (m, v) => m?.supportsNonMaxMode === false || v?.isMaxMode === true +const clean = (s) => String(s ?? "").replaceAll("\u200b", "").split(/\s+/).filter(Boolean).join(" ") -// parameterized is Cursor's model picker (AiService/AvailableModels, as -// the CLI asks for it): each id a model goes by (its name, legacy slugs, -// each variant's slug) with the model and variant it is, and extra(raw), -// the picker's models the usable list doesn't have, as entries of it (a -// model added since, such as GLM-5.3, or one the account hasn't turned -// on). A picker Cursor can't give is none. -async function parameterized(tok) { - let models = [] - try { - const j = await unary(API, "aiserver.v1.AiService/AvailableModels", tok, { useModelParameters: true, doNotUseMarkdown: true }, 8_000) - models = Array.isArray(j?.models) ? j.models : [] - } catch {} - const slugs = new Map() - const namesOf = (m) => { - const vs = m.variants ?? [] - return [m.name, m.serverModelName, ...(m.legacySlugs ?? []), ...(m.idAliases ?? []), ...vs.flatMap((v) => [v.legacySlug, v.variantStringRepresentation])].filter(Boolean) - } - for (const m of models) { - const vs = m.variants ?? [] - for (const v of vs) for (const s of [v.legacySlug, v.variantStringRepresentation]) if (s && !slugs.has(s)) slugs.set(s, { maxMode: needsMax(m, v) }) - for (const s of [m.name, m.serverModelName, ...(m.legacySlugs ?? [])]) if (s && !slugs.has(s)) slugs.set(s, { maxMode: needsMax(m, defaultVariant(m)) }) - } - const extra = (raw) => { - const have = new Set(raw.flatMap((r) => [r.id, r.run])) - const out = [] - for (const m of models) { - if (!m?.name || m.isHidden || m.isChatOnly || m.onlySupportsCmdK || m.supportsAgent === false || PICKER_SKIP.has(m.name)) continue - if (namesOf(m).some((s) => have.has(s))) continue - const title = clean(m.clientDisplayName || m.name) - const params = (v) => (v?.parameterValues ?? []).map((p) => ({ id: String(p.id ?? ""), value: String(p.value ?? "") })).filter((p) => p.id) - const entry = (id, name, v) => { - const maxMode = needsMax(m, v) - const limit = (maxMode && m.contextTokenLimitForMaxMode) || m.contextTokenLimit - return { id, name, context: limit > 0 ? limit : contextOf(name), run: m.name, params: params(v), ...(maxMode ? { maxMode } : {}) } - } - const named = (m.variants ?? []).filter((v) => v.legacySlug) - if (named.length) { - for (const v of named) { - if (have.has(v.legacySlug)) continue - have.add(v.legacySlug) - const label = clean(v.displayNameOutsidePicker || [title, v.displayName].filter(Boolean).join(" ")) - out.push(entry(v.legacySlug, label, v)) - } - } else if (!have.has(m.name)) { - have.add(m.name) - out.push(entry(m.name, title, defaultVariant(m))) - } - } - return out - } - return { slugs, extra } +// tokensOf is a context value as tokens: "300k" 300000, "1m" 1000000. +function tokensOf(value) { + const m = /^(\d+(?:\.\d+)?)([km])$/i.exec(String(value ?? "").trim()) + return m ? Math.round(Number(m[1]) * (m[2].toLowerCase() === "m" ? 1_000_000 : 1_000)) : 0 } +// paramDefs is a picker model's parameters: id to the values it takes. +function paramDefs(m) { + const out = {} + for (const d of m.parameterDefinitions ?? []) { + const t = d?.parameterType ?? {} + const vs = (t.enumParameter ?? t.booleanParameter)?.values ?? [] + if (d?.id) out[d.id] = vs.map((v) => String(v.value)) + } + return out +} + +const valuesOf = (v) => Object.fromEntries((v?.parameterValues ?? []).map((p) => [String(p.id), String(p.value)])) + // defaultVariant is the variant the CLI picks for a model asked for by // name: its default without Max Mode, else with it, else the first. const defaultVariant = (m) => { @@ -702,9 +489,210 @@ const defaultVariant = (m) => { return vs.find((v) => v.isDefaultNonMaxConfig) ?? vs.find((v) => v.isDefaultMaxConfig) ?? vs[0] } -const clean = (s) => String(s ?? "").replaceAll("​", "").split(/\s+/).filter(Boolean).join(" ") +// needsMax is whether a variant is run in Max Mode only, as the CLI reads +// it: a model with no other mode, or a Max Mode variant. +const needsMax = (m, v) => m?.supportsNonMaxMode === false || v?.isMaxMode === true + +// effortsOf is the efforts a model takes, as pi's levels, each with the +// value Cursor's parameter has for it (extra-high is xhigh). +function effortsOf(defs) { + const pid = EFFORT_PARAMS.find((p) => defs[p]?.length) + // thinking alone, on or off: high and none + if (!pid && defs.thinking?.includes("true") && defs.thinking.includes("false")) return { pid: "thinking", levels: { none: "false", high: "true" } } + if (!pid) return { pid: "", levels: {} } + const levels = {} + for (const v of defs[pid]) { + const l = v === "extra-high" ? "xhigh" : v + if (LEVEL_RANK.includes(l)) levels[l] = v + } + return { pid, levels } +} + +// served is a picker model's variants the account is served. With the +// usable list at hand (the ids cursor-agent models shows), a variant whose +// own id isn't in it is one the account hasn't got (Sonnet 4.6 at any effort +// but medium, say). A model the usable list has nothing of is the picker's +// alone (GLM-5.3), and is kept whole. +function served(m, ids) { + const vs = m.variants ?? [] + if (!ids) return vs + const got = vs.filter((v) => !v.legacySlug || ids.has(v.legacySlug)) + return got.some((v) => v.legacySlug) ? got : vs +} + +// modelsOf is the picker's models as offered: one entry for each context +// size a model has, or one with Cursor's limit when it has none. A model +// served in part is offered at the sizes and efforts it is served at. +function modelsOf(picker, ids) { + const out = [] + const used = new Set() + for (const m of picker) { + if (!m?.name || m.isHidden || m.isChatOnly || m.onlySupportsCmdK || m.supportsAgent === false || PICKER_SKIP.has(m.name)) continue + const defs = paramDefs(m) + const variants = served(m, ids) + const partial = variants.length < (m.variants ?? []).length + const has = (pid, value) => !partial || !variants.some((v) => pid in valuesOf(v)) || variants.some((v) => valuesOf(v)[pid] === value) + const def = defaultVariant({ variants }) + const { pid, levels: all } = effortsOf(defs) + const levels = Object.fromEntries(Object.entries(all).filter(([, value]) => has(pid, value))) + const base = { run: m.name, title: clean(m.clientDisplayName || m.name), pid, levels, fast: !!defs.fast?.length, variants, def, partial, model: m } + const contexts = (defs.context ?? []).filter((c) => has("context", c)) + if (contexts.length) { + for (const c of contexts) { + const id = m.name + "@" + c + if (used.has(id)) continue + used.add(id) + out.push({ ...base, id, name: base.title + " @ " + c, context: tokensOf(c) || DEFAULT_CONTEXT, size: c }) + } + } else if (!used.has(m.name)) { + used.add(m.name) + const limit = needsMax(m, def) ? m.contextTokenLimitForMaxMode || m.contextTokenLimit : m.contextTokenLimit + out.push({ ...base, id: m.name === "default" ? "auto" : m.name, name: base.title, context: limit > 0 ? limit : DEFAULT_CONTEXT, size: "" }) + } + } + return out +} + +// nearest is the variant of vs most like the parameters wanted, one at +// their context size before any other. +function nearest(vs, want) { + let best + let most = -1 + for (const v of vs) { + const have = valuesOf(v) + const n = Object.keys(want).filter((k) => have[k] === want[k]).length + (have.context === want.context ? 100 : 0) + if (n > most) [best, most] = [v, n] + } + return best +} + +// pick is the variant a request runs as: the model's context size, the +// effort asked for (else the default's) and fast as asked, matched against +// Cursor's variants. With no variant that matches, the parameters go as +// they are, from the default's; of a model served in part, as the nearest +// variant the account has. seed is the variant an id of Cursor's own named +// (request), in the default's place. +function pick(entry, effort, fast, seed) { + const def = entry.def + const want = { ...valuesOf(seed ?? def) } + if (entry.size) want.context = entry.size + if (effort === "off") effort = "none" + if (entry.pid && entry.pid !== "thinking" && effort === "none" && !("none" in entry.levels) && "thinking" in want) { + // a Claude asked not to think: thinking off at its default effort + want.thinking = "false" + } else if (entry.pid) { + const at = effort ? fitEffort(effort, Object.keys(entry.levels)) : "" + if (at && entry.levels[at]) want[entry.pid] = entry.levels[at] + // a variant named says for itself whether it thinks + if (!seed && entry.pid !== "thinking" && "thinking" in want && at) want.thinking = "true" + } + // fast only when asked: it costs more + if (entry.fast) want.fast = fast ? "true" : "false" + const same = (v) => { + const have = valuesOf(v) + return Object.keys({ ...have, ...want }).every((k) => have[k] === undefined || want[k] === undefined || have[k] === want[k]) + } + const v = entry.variants.find(same) ?? (entry.partial ? nearest(entry.variants, want) : undefined) + const params = Object.entries(v ? { ...want, ...valuesOf(v) } : want).map(([id, value]) => ({ id, value })) + // no variant: Max Mode when the size is more than Cursor allows outside it + const maxMode = v ? needsMax(entry.model, v) : needsMax(entry.model, def) || tokensOf(entry.size) > (entry.model.contextTokenLimit || Infinity) + // the agent API takes the variant's own id (its legacySlug, as + // cursor-agent models lists it: claude-opus-5-5-high), not the picker's + // model name; the default's when no variant matched + const id = v?.legacySlug || def?.legacySlug || entry.run + return { id, params, maxMode } +} + +// fitEffort is the nearest effort a model has to the one asked for; max +// for ultra, and none only when asked. +function fitEffort(want, levels) { + if (want === "ultra") want = "max" + if (!levels.length || levels.includes(want)) return want + const at = LEVEL_RANK.indexOf(want) + if (at < 0) return "" + let best = "" + let dist = LEVEL_RANK.length + for (const l of levels) { + const i = LEVEL_RANK.indexOf(l) + if (i < 0 || l === "none") continue + const d = Math.abs(i - at) + if (d < dist || (d === dist && i > at)) [best, dist] = [l, d] + } + return best +} + +// usable is the account's list, kept a while, by token: Cursor's picker, +// less the variants the usable list (AgentService/GetUsableModels, what +// cursor-agent models shows) says the account hasn't got. A picker Cursor +// can't give is a failure: magpie keeps the list it had. A usable list it +// can't give leaves the picker as it is. +const lists = new Map() +async function usable(tok) { + const h = createHash("sha256").update(tok).digest("hex") + const had = lists.get(h) + if (had && Date.now() - had.at < MODELS_KEEP) return had.raw + const [j, ids] = await Promise.all([ + unary(API, "aiserver.v1.AiService/AvailableModels", tok, { useModelParameters: true, doNotUseMarkdown: true }, 30_000), + unary(API, "agent.v1.AgentService/GetUsableModels", tok, {}, 8_000).then( + (u) => { + const ids = new Set((u?.models ?? []).flatMap((m) => [m.modelId, m.displayModelId]).filter(Boolean)) + return ids.size ? ids : null + }, + () => null, + ), + ]) + const raw = modelsOf(Array.isArray(j?.models) ? j.models : [], ids) + if (!raw.length) throw new Error("Cursor listed no models") + lists.set(h, { at: Date.now(), raw }) + return raw +} + +// named is the model and variant an id the list doesn't have stands for, +// when it is one of Cursor's own, as the CLI reads one and as 0.1.x listed +// them, so a model picked before the list changed still runs: a variant's +// own id (claude-opus-5-5-high) is that variant; a family of them, the id +// 0.1.x listed (claude-opus-5-5, claude-opus-5-5-fast, cursor-grok-4.6, +// or with an effort the account lacks), is the family's variant nearest +// the default, fast when the family is and at the id's effort; the picker's +// name for a model, or a legacy slug of it, is its default. Each at the +// variant's context size, the default's when the id doesn't say. +function named(raw, id) { + const models = raw.filter((e, i) => raw.findIndex((x) => x.model === e.model) === i) + const at = (e, v) => raw.find((x) => x.model === e.model && x.size === (valuesOf(v).context ?? "")) ?? e + const as = (e, vs, effort) => { + const seed = nearest(vs, valuesOf(e.def)) + return { entry: at(e, seed), seed, effort, fast: valuesOf(seed).fast === "true" } + } + for (const e of models) { + const vs = e.variants.filter((v) => v.legacySlug === id) + if (vs.length) return as(e, vs, "") + } + const [family, effort] = splitID(id) + for (const e of models) { + const vs = e.variants.filter((v) => v.legacySlug && splitID(v.legacySlug)[0] === family) + if (vs.length) return as(e, vs, effort) + } + for (const e of models) { + const m = e.model + if ([m.name, ...(m.legacySlugs ?? []), ...(m.idAliases ?? [])].includes(id)) return { entry: at(e, e.def), seed: undefined, effort: "", fast: false } + } + return null +} + +// request is how a chat's model is asked of Cursor: the variant's own id, +// its parameters and its mode. An id the list doesn't have that is one of +// Cursor's own goes as the variant it names, at the effort asked for, fast +// when it says so or the request does; any other goes as it is named. +function request(raw, model, effort, fast) { + const entry = raw.find((m) => m.id === model) + if (entry) return pick(entry, effort, fast) + const old = named(raw, model) + if (old) return pick(old.entry, effort || old.effort, fast || old.fast, old.seed) + return { id: model === "auto" ? "default" : model, params: [], maxMode: false } +} function runtimeModel(m) { + const efforts = LEVEL_RANK.filter((l) => l in m.levels) return { id: m.id, providerID: ID, @@ -717,7 +705,7 @@ function runtimeModel(m) { limit: { context: m.context || DEFAULT_CONTEXT, output: 0 }, capabilities: { temperature: false, - reasoning: m.efforts.length > 0, + reasoning: efforts.length > 0, attachment: true, toolcall: true, input: { text: true, image: true, audio: false, video: false, pdf: false }, @@ -725,10 +713,11 @@ function runtimeModel(m) { interleaved: false, }, release_date: "", - variants: Object.fromEntries(m.efforts.map((e) => [e, { reasoningEffort: e }])), + variants: Object.fromEntries(efforts.map((e) => [e, { reasoningEffort: e }])), } } + // ---- protobuf, by hand ------------------------------------------------------------ class PB { @@ -1846,13 +1835,14 @@ async function answerOf(auth, chat, signal, session = "") { try { raw = await usable(tok) } catch {} - const fast = chat.service_tier === "priority" || chat.service_tier === "fast" - let id = modelID(raw, model, chat.reasoning_effort ?? "", fast) - const entry = raw.find((m) => m.id === id) - id = entry?.run ?? id - if (id === "auto") id = "default" // Cursor's pick, which its API calls default - const params = entry?.params ?? [] - let maxMode = !!entry?.maxMode || maxOnly.has(id) + // fast only when asked: a service_tier of priority or fast is on; any + // other tier, or none, is off, unless the id is a fast one of Cursor's own + const tier = chat.service_tier + const fast = tier === "priority" || tier === "fast" ? true : tier ? false : undefined + const req = request(raw, model, chat.reasoning_effort ?? "", fast) + const id = req.id + const params = req.params + let maxMode = req.maxMode || maxOnly.has(id) const tools = toolsOf(chat) const conv = conversation(chat, tools) const convID = conversationID(chat, session) @@ -2003,8 +1993,8 @@ export async function CursorAuthPlugin() { }, } }, - // the account's list, each family of Cursor's ids one model with the - // efforts there are + // the account's list: each picker model once for each context size, + // with that window provider: { id: ID, async models(provider, { auth } = {}) { @@ -2012,11 +2002,11 @@ export async function CursorAuthPlugin() { // a list Cursor couldn't give is a failure, not the few models // configured: magpie keeps the list it had const tok = await tokenOf(auth) - return Object.fromEntries(offered(await usable(tok)).map((m) => [m.id, runtimeModel(m)])) + return Object.fromEntries((await usable(tok)).map((m) => [m.id, runtimeModel(m)])) }, }, } } // for tests -export const _internal = { STEP_WAIT, stepUsage, ledger, owed, flights, misses, unreadable, catalog, usable, maxRequired, conversationID, usageOf, sessionOf, SESSION, errorResponse, kept, poolBase, splitID, families, byEffort, offered, modelID, conversation, buildRun, decode, exec, fields, pb, pbValue, pbAny, frame, frames, failure, toolsOf, expiry } +export const _internal = { STEP_WAIT, stepUsage, ledger, owed, flights, misses, unreadable, catalog, usable, request, modelsOf, tokensOf, maxRequired, conversationID, usageOf, sessionOf, SESSION, errorResponse, kept, poolBase, conversation, buildRun, decode, exec, fields, pb, pbValue, pbAny, frame, frames, failure, toolsOf, expiry } diff --git a/packages/cursor/maxmode.test.mjs b/packages/cursor/maxmode.test.mjs index 16f28a9..00d42ae 100644 --- a/packages/cursor/maxmode.test.mjs +++ b/packages/cursor/maxmode.test.mjs @@ -1,15 +1,15 @@ -// A model Cursor serves only in Max Mode is asked for in Max Mode, as the -// CLI does ("Max Mode Required: The model "gpt-5.6-luna-low" requires Max -// Mode to be enabled", ARNO on Discord), and the models Cursor's picker -// offers beyond the CLI's usable list (GLM-5.3, GLM-5.3 Flash) are listed -// and run — against a fake Cursor: its API a stand-in fetch, its agent API -// an HTTP/2 server here. Nothing reaches Cursor. +// A request goes to the agent API as the picker's model with the variant's +// parameters, in Max Mode when that variant is Max Mode's; and a model +// Cursor turns away for want of Max Mode ("Max Mode Required: The model +// "gpt-5.6-luna" requires Max Mode to be enabled", ARNO on Discord) is asked +// again in it — against a fake Cursor: its API a stand-in fetch, its agent +// API an HTTP/2 server here. Nothing reaches Cursor. import "./nonet.mjs" // first: no request leaves this machine import { afterAll, afterEach, beforeAll, expect, test } from "bun:test" import http2 from "node:http2" import { CursorAuthPlugin, _internal } from "./index.mjs" -const { fields, pb, frame, usable, offered, maxRequired } = _internal +const { fields, pb, frame, maxRequired } = _internal const real = globalThis.fetch afterEach(() => (globalThis.fetch = real)) @@ -19,49 +19,25 @@ let n = 0 // a sign-in of its own, so no list kept for another is used const fresh = () => ({ type: "oauth", access: jwt(Math.floor(Date.now() / 1000) + 7200 + 50_000 + ++n), refresh: "", expires: 0, accountId: "a@b.c" }) -const USABLE = [ - { modelId: "gpt-5.6-luna-low", displayName: "GPT-5.6 Luna Low" }, - { modelId: "gpt-5.6-luna-high", displayName: "GPT-5.6 Luna High" }, - { modelId: "claude-opus-5-5-max", displayName: "Claude Opus 5.5 1M Max", maxMode: true }, - { modelId: "composer-2.5", displayName: "Composer 2.5" }, - { modelId: "grok-4.7-low", displayName: "Grok 4.7 Low" }, -] - +const en = (...vs) => ({ enumParameter: { values: vs.map((value) => ({ value })) } }) +const v = (pv, o = {}) => ({ parameterValues: Object.entries(pv).map(([id, value]) => ({ id, value })), ...o }) // Cursor's picker, as AvailableModels answers it in JSON const PICKER = [ { - name: "gpt-5.6-luna", - clientDisplayName: "GPT-5.6 Luna", - supportsMaxMode: true, - supportsNonMaxMode: false, - variants: [ - { parameterValues: [{ id: "reasoning", value: "low" }], displayName: "Low", isMaxMode: true, legacySlug: "gpt-5.6-luna-low" }, - { parameterValues: [{ id: "reasoning", value: "high" }], displayName: "High", isMaxMode: true, legacySlug: "gpt-5.6-luna-high" }, - ], + name: "claude-opus-5-5", clientDisplayName: "Claude Opus 5.5", contextTokenLimit: 300000, contextTokenLimitForMaxMode: 1000000, supportsNonMaxMode: true, + parameterDefinitions: [{ id: "context", parameterType: en("300k", "1m") }, { id: "effort", parameterType: en("medium", "high") }], + variants: [v({ context: "300k", effort: "medium" }, { isDefaultNonMaxConfig: true, legacySlug: "claude-opus-5-5-medium" }), v({ context: "1m", effort: "medium" }, { isMaxMode: true, legacySlug: "claude-opus-5-5-medium" })], }, - { name: "composer-2.5", clientDisplayName: "Composer 2.5", supportsNonMaxMode: true, variants: [{ displayName: "", isMaxMode: false, legacySlug: "composer-2.5" }] }, - { - name: "glm-5.3", - clientDisplayName: "GLM-5.3", - supportsNonMaxMode: true, - supportsMaxMode: true, - contextTokenLimit: 200000, - variants: [ - { parameterValues: [{ id: "thinking", value: "true" }], displayName: "", isMaxMode: false, isDefaultNonMaxConfig: true }, - { parameterValues: [{ id: "thinking", value: "true" }], displayName: "", isMaxMode: true, isDefaultMaxConfig: true }, - ], - }, - { name: "glm-5.3-flash", clientDisplayName: "GLM-5.3 Flash", supportsNonMaxMode: true }, - { name: "secret-model", clientDisplayName: "Secret", isHidden: true }, - { name: "tab-only", clientDisplayName: "Tab", onlySupportsCmdK: true }, - { name: "claude-4.5-haiku", clientDisplayName: "Haiku 4.5" }, + { name: "composer-2.5", clientDisplayName: "Composer 2.5", contextTokenLimit: 200000, supportsNonMaxMode: true, variants: [v({}, { isDefaultNonMaxConfig: true, legacySlug: "composer-2.5" })] }, + // Cursor says nothing of Max Mode for it, and refuses it without + { name: "gpt-5.6-luna", clientDisplayName: "GPT-5.6 Luna", contextTokenLimit: 272000, supportsNonMaxMode: true, parameterDefinitions: [{ id: "reasoning", parameterType: en("low", "high") }], variants: [v({ reasoning: "high" }, { isDefaultNonMaxConfig: true, legacySlug: "gpt-5.6-luna-high" })] }, ] // the agent API: each Run's model noted; one asked for without Max Mode // that only has it is refused as Cursor refuses it let server, base const runs = [] -const maxOnly = new Set(["gpt-5.6-luna-low", "gpt-5.6-luna-high", "secret-max"]) +const maxOnly = new Set(["gpt-5.6-luna-high"]) const str = (fs, num) => fs.find((f) => f.num === num)?.data?.toString() ?? "" const num = (fs, k) => fs.find((f) => f.num === k)?.n ?? 0 beforeAll(async () => { @@ -108,21 +84,16 @@ beforeAll(async () => { }) afterAll(() => server.close()) -function fakeAPI({ picker = PICKER, pickerDown = false } = {}) { - const asked = [] +function fakeAPI() { globalThis.fetch = async (url, init) => { const u = String(url) - asked.push(u) if (u === "https://api2.cursor.sh/aiserver.v1.ServerConfigService/GetServerConfig") return Response.json({ agentUrlConfig: { agentUrl: base } }) - if (u === "https://api2.cursor.sh/agent.v1.AgentService/GetUsableModels") return Response.json({ models: USABLE }) if (u === "https://api2.cursor.sh/aiserver.v1.AiService/AvailableModels") { - if (pickerDown) return new Response("down", { status: 503 }) expect(JSON.parse(init.body)).toEqual({ useModelParameters: true, doNotUseMarkdown: true }) - return Response.json({ models: picker }) + return Response.json({ models: PICKER }) } throw new Error("the test asked " + u) } - return asked } async function ask(auth, model, effort) { @@ -139,49 +110,26 @@ test("Cursor's Max Mode refusal is recognised", () => { expect(maxRequired("usage limit reached: slow down")).toBe(false) }) -test("the list has the picker's models the usable list lacks, and says which need Max Mode", async () => { - fakeAPI() - const tok = fresh().access - const raw = await usable(tok) - const by = Object.fromEntries(raw.map((m) => [m.id, m])) - expect(by["gpt-5.6-luna-low"].maxMode).toBe(true) // the picker's say - expect(by["claude-opus-5-5-max"].maxMode).toBe(true) // the usable list's own - expect(by["composer-2.5"].maxMode).toBeUndefined() - expect(by["grok-4.7-low"].maxMode).toBeUndefined() // in neither: as before - expect(by["glm-5.3"]).toEqual({ id: "glm-5.3", name: "GLM-5.3", context: 200000, run: "glm-5.3", params: [{ id: "thinking", value: "true" }] }) - expect(by["glm-5.3-flash"]).toMatchObject({ id: "glm-5.3-flash", name: "GLM-5.3 Flash", run: "glm-5.3-flash", params: [] }) - for (const gone of ["secret-model", "tab-only", "claude-4.5-haiku", "gpt-5.6-luna", "composer-2.5-1"]) expect(by[gone]).toBeUndefined() - // offered: GLM-5.3 and its Flash beside the families - const ids = offered(raw).map((m) => m.id) - expect(ids).toEqual(["gpt-5.6-luna", "claude-opus-5-5-max", "composer-2.5", "grok-4.7-low", "glm-5.3", "glm-5.3-flash"]) - // the provider's list, as magpie asks for it - fakeAPI() - const hooks = await CursorAuthPlugin() - const listed = await hooks.provider.models({ models: {} }, { auth: fresh() }) - expect(Object.keys(listed)).toContain("glm-5.3") - expect(Object.keys(listed)).toContain("glm-5.3-flash") -}) - -test("a picker Cursor can't give leaves the usable list as it was", async () => { - fakeAPI({ pickerDown: true }) - const raw = await usable(fresh().access) - expect(raw.map((m) => m.id)).toEqual(USABLE.map((m) => m.modelId)) - expect(raw.find((m) => m.id === "claude-opus-5-5-max").maxMode).toBe(true) -}) - -test("a Max Mode model is run in Max Mode, once, and a picker-only model with its parameters", async () => { +test("a request goes as the picker's model, with the size's parameters, in Max Mode for a Max Mode size", async () => { fakeAPI() const auth = fresh() runs.length = 0 - let r = await ask(auth, "gpt-5.6-luna", "low") + let r = await ask(auth, "claude-opus-5-5@300k") expect([r.status, r.body.error]).toEqual([200, undefined]) expect(r.body.choices[0].message.content).toBe("hi") - expect(runs).toEqual([{ model: "gpt-5.6-luna-low", detailsMax: 1, requested: "gpt-5.6-luna-low", requestedMax: 1, params: [] }]) + expect(runs).toEqual([{ model: "claude-opus-5-5-medium", detailsMax: 0, requested: "claude-opus-5-5-medium", requestedMax: 0, params: [["context", "300k"], ["effort", "medium"]] }]) runs.length = 0 - r = await ask(auth, "glm-5.3") + r = await ask(auth, "claude-opus-5-5@1m", "medium") expect(r.status).toBe(200) - expect(runs).toEqual([{ model: "glm-5.3", detailsMax: 0, requested: "glm-5.3", requestedMax: 0, params: [["thinking", "true"]] }]) + expect(runs).toEqual([{ model: "claude-opus-5-5-medium", detailsMax: 1, requested: "claude-opus-5-5-medium", requestedMax: 1, params: [["context", "1m"], ["effort", "medium"]] }]) + + // an id 0.1.11 listed: the variant it stood for, not the picker's bare + // name, which Cursor answers 502 "AI Model Not Found" + runs.length = 0 + r = await ask(auth, "claude-opus-5-5") + expect(r.status).toBe(200) + expect(runs).toEqual([{ model: "claude-opus-5-5-medium", detailsMax: 0, requested: "claude-opus-5-5-medium", requestedMax: 0, params: [["context", "300k"], ["effort", "medium"]] }]) runs.length = 0 r = await ask(auth, "composer-2.5") @@ -189,15 +137,13 @@ test("a Max Mode model is run in Max Mode, once, and a picker-only model with it expect(runs).toEqual([{ model: "composer-2.5", detailsMax: 0, requested: "composer-2.5", requestedMax: 0, params: [] }]) }) -test("a model neither list says needs Max Mode is asked again in Max Mode when Cursor says so", async () => { - // no picker: the usable list alone, which doesn't say it - fakeAPI({ pickerDown: true }) +test("a model Cursor says needs Max Mode is asked again in Max Mode, and so from then on", async () => { + fakeAPI() const auth = fresh() runs.length = 0 let r = await ask(auth, "gpt-5.6-luna", "high") expect([r.status, r.body.error]).toEqual([200, undefined]) expect(runs.map((x) => [x.model, x.detailsMax, x.requestedMax])).toEqual([["gpt-5.6-luna-high", 0, 0], ["gpt-5.6-luna-high", 1, 1]]) - // and from then on in Max Mode at once runs.length = 0 r = await ask(auth, "gpt-5.6-luna", "high") expect(r.status).toBe(200) diff --git a/packages/cursor/package.json b/packages/cursor/package.json index f2fc993..846a7bc 100644 --- a/packages/cursor/package.json +++ b/packages/cursor/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-cursor-auth", - "version": "0.1.11", + "version": "0.2.0", "description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to", "type": "module", "main": "./index.mjs", diff --git a/packages/cursor/usage.test.mjs b/packages/cursor/usage.test.mjs index b40d114..682a1f7 100644 --- a/packages/cursor/usage.test.mjs +++ b/packages/cursor/usage.test.mjs @@ -16,9 +16,9 @@ const auth = { type: "oauth", access: tok, refresh: "", expires: 0, accountId: " let n = 0 const fresh = () => ({ ...auth, access: jwt(Math.floor(Date.now() / 1000) + 7200 + ++n) }) -const firstParty = ["grok-4.7-xhigh-fast", "cursor-grok-4.7-high-fast", "cursor-grok-4.6-high-fast", "grok-4.5-fast-high", "auto", "default", "composer-2.5", "COMPOSER-2.5-FAST", "composer"] +const firstParty = ["grok-4.7@256k", "grok-4.7@500k", "grok-4.6", "grok-4.7-xhigh-fast", "cursor-grok-4.7-high-fast", "cursor-grok-4.6-high-fast", "grok-4.5-fast-high", "auto", "default", "composer-2.5", "COMPOSER-2.5-FAST", "composer"] const bucketed = ["future-first-party", "grok-4.8-high", "cursor-grok-4.8-xhigh-fast"] -const others = ["claude-opus-5-5", "gpt-5.6-sol", "gemini-3.1-pro", "grok-3", "grok-4.70", "grok-4.80-high", "unknown-model"] +const others = ["claude-opus-5-5@300k", "claude-opus-5-5@1m", "claude-opus-5-5", "gpt-5.6-sol", "gemini-3.1-pro", "grok-3", "grok-4.70", "grok-4.80-high", "unknown-model"] const provider = { models: Object.fromEntries([...firstParty, ...bucketed, ...others].map((id) => [id, { id }])) } async function run(a, reply) {