diff --git a/packages/cursor/README.md b/packages/cursor/README.md index e30cd78..3990a54 100644 --- a/packages/cursor/README.md +++ b/packages/cursor/README.md @@ -197,6 +197,17 @@ minutes), the way pi-cursor-sdk lists Cursor's catalog: 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. +- A family the usable list has and the picker doesn't (an account whose + picker lacks Claude Opus 5.5 while `cursor-agent models` lists + `claude-opus-5-5-low` to `-max`) is listed once under its family id + (`claude-opus-5-5`), its efforts as variants, and goes as the usable id + those parameters pick, never as the family id Cursor doesn't know. A + picker model whose chosen variant isn't one the account can use goes as + its family's nearest usable id the same way. +- An `@` id the list no longer has (`claude-opus-5-5@1m` when only + 300k is listed, or the model is listed unsized) runs at the model's + nearest size, in Max Mode when the size asked for is over the model's + window, never as the literal `@` id. - 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`, diff --git a/packages/cursor/context.test.mjs b/packages/cursor/context.test.mjs index 1ac13f5..e6318bd 100644 --- a/packages/cursor/context.test.mjs +++ b/packages/cursor/context.test.mjs @@ -51,7 +51,7 @@ export const PICKER = [ 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("/agent.v1.AgentService/GetUsableModels")) return usableIDs ? Response.json({ models: usableIDs.map((m) => (typeof m === "string" ? { modelId: m } : m)) }) : 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) } @@ -284,3 +284,97 @@ test("when the default's own branch stops short, an id of it asked past that goe expect([id, effort, fast, params]).toEqual([id, effort, fast, { ...params, ...slug.get(sent) }]) } }) + +// ysm119 on Discord (magpie #3150): a Cursor Team account whose +// cursor-agent models (GetUsableModels) lists Claude Opus 5.5 at each effort, +// as below word for word, while the picker the plugin listed from had +// nothing of it. cursor/claude-opus-5-5@1m went to Cursor as it was +// ("AI Model Not Found"), so did claude-opus-5-5, and only the usable ids +// themselves ran. +const OPUS55_USABLE = ["low", "medium", "high", "xhigh", "max"].flatMap((e) => { + const word = { low: "Low", medium: "", high: "High", xhigh: "Extra High", max: "Max" }[e] + const name = ("Claude Opus 5.5 1M " + word).trim() + return [ + { modelId: "claude-opus-5-5-" + e, displayModelId: "claude-opus-5-5-" + e, displayName: name, displayNameShort: name, maxMode: false }, + { modelId: "claude-opus-5-5-" + e + "-fast", displayModelId: "claude-opus-5-5-" + e + "-fast", displayName: name + " Fast", displayNameShort: name + " Fast", maxMode: true }, + ] +}) +const REST_USABLE = ["grok-4.7-high-fast", "grok-4.7-low-fast", "grok-4.7-high", "grok-4.7-low", "cursor-grok-4.6-high", "cursor-grok-4.6-low", "composer-2.5", "composer-2.5-fast", { modelId: "default", displayModelId: "auto", displayName: "Auto" }] +const usableIDs = new Set([...OPUS55_USABLE.map((m) => m.modelId), ...REST_USABLE.map((m) => m.modelId ?? m), "auto"]) + +test("a family cursor-agent lists and the picker hasn't is offered once, with its efforts, and runs as its own ids", async () => { + fakeAPI(PICKER.filter((m) => m.name !== "claude-opus-5-5"), [...OPUS55_USABLE, ...REST_USABLE]) + const raw = await usable(tok()) + const opus = raw.find((m) => m.id === "claude-opus-5-5") + expect([opus.name, opus.context, Object.keys(opus.levels), opus.offersFast]).toEqual(["Claude Opus 5.5", 200000, ["low", "medium", "high", "xhigh", "max"], true]) + // nothing else is added: the other ids are the picker's + expect(raw.filter((m) => m.usableOnly).map((m) => m.id)).toEqual(["claude-opus-5-5"]) + expect(raw.some((m) => /^claude-opus-5-5-/.test(m.id))).toBe(false) + const q = (id, effort = "", fast) => { + const r = request(raw, id, effort, fast) + return [r.id, r.maxMode, r.params] + } + // the folded name: Cursor's default effort, medium, or the one asked + expect(q("claude-opus-5-5")).toEqual(["claude-opus-5-5-medium", false, []]) + expect(q("claude-opus-5-5", "low")).toEqual(["claude-opus-5-5-low", false, []]) + expect(q("claude-opus-5-5", "minimal")).toEqual(["claude-opus-5-5-low", false, []]) + expect(q("claude-opus-5-5", "high", true)).toEqual(["claude-opus-5-5-high-fast", true, []]) + // the stale sized name from an earlier list: in Max Mode for 1M, as the + // picker runs Opus 5.5 at 1m + expect(q("claude-opus-5-5@1m")).toEqual(["claude-opus-5-5-medium", true, []]) + expect(q("claude-opus-5-5@1m", "low")).toEqual(["claude-opus-5-5-low", true, []]) + expect(q("claude-opus-5-5@300k", "xhigh")).toEqual(["claude-opus-5-5-xhigh", true, []]) + // the usable ids themselves, the reporter's workaround, as they are + expect(q("claude-opus-5-5-low")).toEqual(["claude-opus-5-5-low", false, []]) + expect(q("claude-opus-5-5-medium")).toEqual(["claude-opus-5-5-medium", false, []]) + expect(q("claude-opus-5-5-max-fast")).toEqual(["claude-opus-5-5-max-fast", true, []]) + // the request's effort still counts, as for a picker model's variant id + expect(q("claude-opus-5-5-low", "high")[0]).toBe("claude-opus-5-5-high") + // a -fast family 0.1.x listed + expect(q("claude-opus-5-5-fast")[0]).toBe("claude-opus-5-5-medium-fast") + // whatever is asked, an Opus 5.5 id goes out as one Cursor listed + for (const id of ["claude-opus-5-5", "claude-opus-5-5@1m", "claude-opus-5-5@300k", "claude-opus-5-5-fast", ...OPUS55_USABLE.map((m) => m.modelId)]) + for (const effort of ["", "off", "none", "minimal", "low", "medium", "high", "xhigh", "max", "ultra"]) + for (const fast of [undefined, false, true]) expect([id, effort, fast, usableIDs.has(request(raw, id, effort, fast).id)]).toEqual([id, effort, fast, true]) +}) + +test("a picker model without variants goes as the usable id of its family, not its bare name", async () => { + // the picker names Opus 5.5 without its variants or their ids: the + // request went as claude-opus-5-5, which Cursor doesn't serve + const bare = { name: "claude-opus-5-5", clientDisplayName: "Claude Opus 5.5", contextTokenLimit: 300000, supportsNonMaxMode: true } + fakeAPI([bare, ...PICKER.filter((m) => m.name !== "claude-opus-5-5")], [...OPUS55_USABLE, ...REST_USABLE]) + const raw = await usable(tok()) + expect(raw.filter((m) => m.run === "claude-opus-5-5").map((m) => [m.id, m.context])).toEqual([["claude-opus-5-5", 300000]]) + const q = (id, effort = "", fast) => { + const r = request(raw, id, effort, fast) + return [r.id, r.maxMode] + } + expect(q("claude-opus-5-5")).toEqual(["claude-opus-5-5-medium", false]) + expect(q("claude-opus-5-5", "low")).toEqual(["claude-opus-5-5-low", false]) + expect(q("claude-opus-5-5", "max", true)).toEqual(["claude-opus-5-5-max-fast", true]) + expect(q("claude-opus-5-5@1m")).toEqual(["claude-opus-5-5-medium", true]) + expect(q("claude-opus-5-5-low")).toEqual(["claude-opus-5-5-low", false]) +}) + +test("an id at a size the list no longer has runs at the nearest size the model has", async () => { + fakeAPI() + const raw = await usable(tok()) + const q = (id, effort = "") => { + const r = request(raw, id, effort, undefined) + return [r.id, r.maxMode, Object.fromEntries(r.params.map((x) => [x.id, x.value])).context] + } + expect(q("claude-opus-5-5@2m")).toEqual(["claude-opus-5-5-medium", true, "1m"]) + expect(q("claude-opus-5-5@200k", "high")).toEqual(["claude-opus-5-5-high", false, "300k"]) + expect(q("grok-4.7@1m")[2]).toBe("500k") + // a model now listed without sizes: itself, in Max Mode past its window + expect(q("grok-4.6@256k")).toEqual(["cursor-grok-4.6-high", false, undefined]) + expect(q("grok-4.6@1m")[1]).toBe(true) + // nothing of it anywhere: as it is named + expect(q("nothing@1m")).toEqual(["nothing@1m", false, undefined]) +}) + +test("a picker that covers every usable id is listed as before", async () => { + fakeAPI(PICKER, [...OPUS55_USABLE.filter((m) => /-(medium|high)(-fast)?$/.test(m.modelId)), ...REST_USABLE]) + const raw = await usable(tok()) + expect(raw.some((m) => m.usableOnly)).toBe(false) +}) diff --git a/packages/cursor/index.mjs b/packages/cursor/index.mjs index fe1f825..c88d22b 100644 --- a/packages/cursor/index.mjs +++ b/packages/cursor/index.mjs @@ -735,12 +735,72 @@ function served(m, ids) { return got.some((v) => v.legacySlug) ? got : vs } +// familyOf is the family one of Cursor's own ids is of, fast or not: +// claude-opus-5-5-low-fast is of claude-opus-5-5, at low, fast. +function familyOf(id) { + let [family, effort] = splitID(id) + const fast = family.endsWith("-fast") && family.length > "-fast".length + if (fast) family = family.slice(0, -"-fast".length) + return { family, effort, fast } +} + +// familiesOf is the usable list (GetUsableModels, what cursor-agent models +// shows: claude-opus-5-5-low, claude-opus-5-5-low-fast, …) by family: each +// id with its effort, whether it is fast, whether Cursor runs it in Max +// Mode, and its name. +function familiesOf(listed) { + const fams = new Map() + for (const u of listed ?? []) { + const id = String(u?.modelId ?? "") + if (!id) continue + const { family, effort, fast } = familyOf(id) + if (!fams.has(family)) fams.set(family, []) + fams.get(family).push({ id, effort, fast, maxMode: u.maxMode === true, name: clean(u.displayName || id) }) + } + return fams +} + +// fromUsable is the id of a family of the usable list's that a request +// runs as: fast as asked (when the family has both), at the effort asked or +// the nearest it has; with none asked, the one without an effort in its +// id, else medium, as Cursor's default is (claude-opus-5-5-medium), else +// the first. +function fromUsable(fam, effort, fast) { + let vs = fam.filter((u) => u.fast === !!fast) + if (!vs.length) vs = fam + if (effort === "off") effort = "none" + const at = effort ? fitEffort(effort, vs.map((u) => u.effort).filter(Boolean)) : "" + return (at && vs.find((u) => u.effort === at)) || vs.find((u) => u.effort === "") || vs.find((u) => u.effort === "medium") || vs[0] +} + +// usableTitle is a usable id's name without what the id says apart: the +// effort, fast, and the 1M Cursor names every Max-capable Claude with +// ("Claude Opus 5.5 1M Low" is Claude Opus 5.5). +function usableTitle(name) { + const drop = new Set(["low", "medium", "high", "xhigh", "extra", "max", "minimal", "none", "fast", "1m", "(1m)"]) + const ws = name.split(" ") + while (ws.length > 1 && drop.has(ws.at(-1).toLowerCase())) ws.pop() + return ws.join(" ") +} + // 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) { +// listed is the usable list: a family of its ids the picker has nothing of +// (a Team's picker can leave out what cursor-agent models lists, magpie +// #3150 ysm119 on Discord) is offered too, once, with its efforts, and each +// entry keeps its family's usable ids, so a request goes as one of those +// and never as a name Cursor would answer "AI Model Not Found". +function modelsOf(picker, ids, listed = []) { const out = [] const used = new Set() + const fams = familiesOf(listed) + // every id the picker names at all, shown or left out + const named = new Set() + for (const m of picker) { + if (!m?.name) continue + for (const x of [m.name, ...(m.legacySlugs ?? []), ...(m.idAliases ?? []), ...(m.variants ?? []).map((v) => v.legacySlug)]) if (x) named.add(x) + } 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) @@ -750,7 +810,8 @@ function modelsOf(picker, ids) { 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 fam = fams.get(familyOf(def?.legacySlug || m.name).family) ?? [] + const base = { run: m.name, title: clean(m.clientDisplayName || m.name), pid, levels, fast: !!defs.fast?.length, variants, def, partial, model: m, fam, ids } const contexts = (defs.context ?? []).filter((c) => has("context", c)) if (contexts.length) { for (const c of contexts) { @@ -765,6 +826,15 @@ function modelsOf(picker, ids) { out.push({ ...base, id: m.name === "default" ? "auto" : m.name, name: base.title, context: limit > 0 ? limit : DEFAULT_CONTEXT, size: "", offersFast: fastAt(variants, "") }) } } + for (const [family, fam] of fams) { + if (fam.some((u) => named.has(u.id)) || named.has(family) || used.has(family)) continue + used.add(family) + const levels = Object.fromEntries(fam.filter((u) => LEVEL_RANK.includes(u.effort)).map((u) => [u.effort, u.effort])) + const def = fromUsable(fam, "", false) + // Cursor's context for it isn't known: its default's, as for a model + // the picker gives no limit for + out.push({ id: family, run: family, name: usableTitle(def.name), title: usableTitle(def.name), pid: "", levels, fast: false, variants: [], def: undefined, partial: false, model: { name: family }, fam, ids, usableOnly: true, context: DEFAULT_CONTEXT, size: "", offersFast: fam.some((u) => u.fast) }) + } return out } @@ -797,6 +867,11 @@ function nearest(vs, want) { // 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) { + // a family the usable list has and the picker doesn't: its own id + if (entry.usableOnly) { + const u = fromUsable(entry.fam, effort, fast) + return { id: u.id, params: [], maxMode: u.maxMode } + } const def = entry.def const want = { ...valuesOf(seed ?? def) } if (entry.size) want.context = entry.size @@ -824,6 +899,14 @@ function pick(entry, effort, fast, seed) { // 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 + // an id the usable list doesn't have, when it has the family's (a + // picker model without variants goes by its name, claude-opus-5-5, + // which Cursor doesn't serve): the family's usable id at the effort + if (entry.ids && !entry.ids.has(id) && entry.fam?.length) { + const level = entry.pid && entry.pid !== "thinking" ? String(want[entry.pid] ?? "").replace("extra-high", "xhigh") : "" + const u = fromUsable(entry.fam, effort || level, fast) + return { id: u.id, params: [], maxMode: maxMode || u.maxMode } + } return { id, params, maxMode } } @@ -878,17 +961,15 @@ 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([ + const [j, listed] = 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, + (u) => (Array.isArray(u?.models) ? u.models : []), + () => [], ), ]) - const raw = modelsOf(Array.isArray(j?.models) ? j.models : [], ids) + const ids = new Set(listed.flatMap((m) => [m?.modelId, m?.displayModelId]).filter(Boolean)) + const raw = modelsOf(Array.isArray(j?.models) ? j.models : [], ids.size ? ids : null, listed) if (!raw.length) throw new Error("Cursor listed no models") lists.set(h, { at: Date.now(), raw }) return raw @@ -921,8 +1002,33 @@ function named(raw, id) { } for (const e of models) { const m = e.model + if (e.usableOnly) continue if ([m.name, ...(m.legacySlugs ?? []), ...(m.idAliases ?? [])].includes(id)) return { entry: at(e, e.def), seed: undefined, effort: "", fast: false } } + // one of the usable list's own ids, or a family of them, that the picker + // has nothing of: that family at the id's effort, fast when it is + const of = familyOf(id) + for (const e of models) { + if (!e.usableOnly) continue + const u = e.fam.find((x) => x.id === id) + if (u) return { entry: e, seed: undefined, effort: u.effort, fast: u.fast } + if (e.id === of.family) return { entry: e, seed: undefined, effort: of.effort, fast: of.fast } + } + // an id at a context size the list no longer has (claude-opus-5-5@1m, + // picked from an earlier list, or from another account's): the model at + // the nearest size it has; a model listed without sizes, in Max Mode for + // a size over its window + const sized = /^(.+)@([^@]+)$/.exec(id) + if (sized) { + const want = tokensOf(sized[2]) + const same = raw.filter((e) => e.run === sized[1] && e.size) + if (same.length) { + const near = same.reduce((a, b) => (Math.abs(tokensOf(b.size) - want) < Math.abs(tokensOf(a.size) - want) ? b : a)) + return { entry: near, seed: undefined, effort: "", fast: false } + } + const old = named(raw, sized[1]) + if (old) return { ...old, max: !old.entry.size && want > old.entry.context } + } return null } @@ -933,8 +1039,17 @@ function named(raw, id) { function request(raw, model, effort, fast) { const entry = raw.find((m) => m.id === model) if (entry) return pick(entry, effort, fast) + const bare = raw.find((m) => m.id === model.replace(/@[^@]*$/, "")) + if (bare && model !== bare.id && !bare.size) { + // a model now listed without sizes, asked at one + const r = pick(bare, effort, fast) + return tokensOf(model.split("@").at(-1)) > bare.context ? { ...r, maxMode: true } : r + } const old = named(raw, model) - if (old) return pick(old.entry, effort || old.effort, fast || old.fast, old.seed) + if (old) { + const r = pick(old.entry, effort || old.effort, fast || old.fast, old.seed) + return old.max ? { ...r, maxMode: true } : r + } return { id: model === "auto" ? "default" : model, params: [], maxMode: false } } diff --git a/packages/cursor/package.json b/packages/cursor/package.json index bdbf59a..efa66a6 100644 --- a/packages/cursor/package.json +++ b/packages/cursor/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-cursor-auth", - "version": "0.2.23", + "version": "0.2.24", "description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to", "type": "module", "main": "./index.mjs",