An old id names a branch of a model's variants (claude-opus-5 is the Opus 5
without thinking). When the effort asked for exists only in the other branch,
no variant matched and the request went as the default's legacySlug with the
parameters it had been given: cursor/claude-opus-5 at xhigh went as
claude-opus-5-thinking-high with thinking=false, effort=xhigh. On this
account Opus 5 without thinking stops at high, so it happens today: 40 of
5040 old id × effort × fast combinations, all claude-opus-5. 0.1.11 sent
claude-opus-5-high there.
Now, for an old id, a request no variant matches goes as the variant of
its own branch (every parameter but the effort as asked) at the nearest
effort it has (branch()): claude-opus-5 at xhigh or max is claude-opus-5-high,
-fast at xhigh is claude-opus-5-high-fast, as 0.1.11 sent them. Where the
default differs from what is asked in the effort alone, the effort still goes
as asked. A listed id is not changed.
context.test.mjs adds a picker shaped like this account's Opus 5 and checks
those cases and that every id sent agrees with its parameters (which holds
where the default's branch is complete, as in this picker; see Not covered);
it fails on 8cd5a0f's index.mjs. HOME=$(mktemp -d) bun test packages/cursor:
49 pass, 0 fail; check.mjs cursor passes. Offline against this account's
picker and usable list: the contradicting requests go from 40 to 0.
With a real Cursor account, this version installed into magpie (packed from
this code plus one temporary line logging what request() returns):
cursor/claude-opus-5 at reasoning_effort xhigh (max_tokens 16) went as
claude-opus-5-high with thinking=false, context=300k, effort=high,
fast=false, maxMode=false, and answered 200 ("ok", 14 in / 4 out, 5.2s;
magpie's usage log: effort xhigh, status 200). On 0.2.0 the contradicting
request cursor/claude-opus-5-low at xhigh also answered 200, so it failed
silently.
Not covered. branch() steps aside when what is asked differs from the
default variant in the effort alone, so that an effort goes as asked, as
for a listed model (the existing claude-opus-5-5-xhigh test expects
effort=xhigh). So when the branch that stops short is the default's own,
the request still goes as the default's slug with the effort asked: with a
default of m-high (no thinking, low to high) and a thinking branch up to
max, m at xhigh goes as m-high with effort=xhigh. Dropping that early
return fixes it (effort=high) and changes that one assertion; left for the
maintainer to decide. On this account no model's default branch is the
shorter one (of 20 models with two or more branches, only non-default
branches stop short: claude-opus-5's without thinking, and gpt-5.4's fast
one after the usable list).
2033 lines
82 KiB
JavaScript
2033 lines
82 KiB
JavaScript
// Cursor subscriptions as an OpenCode provider plugin, served through the
|
||
// API cursor-agent talks to. Ported from magpie's built-in Cursor account
|
||
// (internal/provider/cursor*.go, internal/gateway/cursor.go).
|
||
//
|
||
// An account is a Cursor access token, got one of three ways:
|
||
// - the sign-in cursor-agent does (cursor.com/loginDeepControl with a PKCE
|
||
// challenge, then /auth/poll until the browser has signed in);
|
||
// - the token cursor-agent itself keeps (the Mac keychain, else its
|
||
// auth.json), read each time and never written;
|
||
// - a Cursor API key, exchanged for a token as the CLI does with
|
||
// CURSOR_API_KEY.
|
||
//
|
||
// Requests go to AgentService/Run, a Connect stream both ways over HTTP/2.
|
||
// Cursor keeps a conversation on its client: each message of the prompt is
|
||
// an AI SDK message in JSON, a blob named by its sha256, which the server
|
||
// asks the client for as it reads them. So each request goes whole, as such
|
||
// a conversation, the caller's system prompt at its head; the Run only
|
||
// resumes it. The caller's tools are MCP tools, which the model calls
|
||
// through Cursor's CallDynamicTool: the server hands each call to the
|
||
// client to run, and once it has said how many a step made, the stream is
|
||
// closed and the calls go back to OpenCode as a chat completion's tool
|
||
// calls. Nothing else the server asks the client to run (a shell, a file
|
||
// read) is run.
|
||
//
|
||
// OpenCode speaks chat completions to it (@ai-sdk/openai-compatible); the
|
||
// fetch here answers them.
|
||
import http2 from "node:http2"
|
||
import { STATUS_CODES } from "node:http"
|
||
import { gunzipSync } from "node:zlib"
|
||
import { createHash, randomBytes, randomUUID } from "node:crypto"
|
||
import { execFile } from "node:child_process"
|
||
import { readFile, stat, readdir, realpath } from "node:fs/promises"
|
||
import { homedir, platform, tmpdir } from "node:os"
|
||
import { basename, dirname, join } from "node:path"
|
||
|
||
const ID = "cursor"
|
||
const API = "https://api2.cursor.sh"
|
||
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 // 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."
|
||
const CLI_MARK = "cursor-agent" // an oauth sign-in's refresh when the token is cursor-agent's own
|
||
|
||
const GOOS = { win32: "windows" }[platform()] ?? platform()
|
||
const EMPTY = Buffer.alloc(0)
|
||
const enc = new TextEncoder()
|
||
|
||
// ---- cursor-agent ---------------------------------------------------------------
|
||
|
||
const exists = (p) => stat(p).then((s) => !s.isDirectory(), () => false)
|
||
|
||
function run(file, args, { timeout = 10_000, env } = {}) {
|
||
return new Promise((resolve) => {
|
||
execFile(file, args, { timeout, env: env ?? process.env, maxBuffer: 8 << 20 }, (err, stdout) =>
|
||
resolve({ ok: !err, out: String(stdout ?? "") }))
|
||
})
|
||
}
|
||
|
||
// cursorExecutable finds the cursor-agent CLI, or Cursor's `agent` by that
|
||
// name.
|
||
async function cursorExecutable() {
|
||
const dirs = (process.env.PATH ?? "").split(GOOS === "windows" ? ";" : ":").filter(Boolean)
|
||
for (const name of ["cursor-agent", "agent"]) {
|
||
for (const d of dirs) {
|
||
const p = join(d, GOOS === "windows" ? name + ".exe" : name)
|
||
if (!(await exists(p))) continue
|
||
if (name === "cursor-agent") return p
|
||
if ((await realpath(p).catch(() => "")).includes("cursor-agent")) return p
|
||
}
|
||
}
|
||
const home = homedir()
|
||
for (const p of [join(home, ".local", "bin", "cursor-agent"), "/usr/local/bin/cursor-agent", "/opt/homebrew/bin/cursor-agent"])
|
||
if (await exists(p)) return p
|
||
return ""
|
||
}
|
||
|
||
const VERSION_RE = /^\d{4}\.\d{2}\.\d{2}-[0-9a-f]+$/
|
||
|
||
// clientVersion is the cursor-agent the API is told it is talking to,
|
||
// "cli-<version>": the installed one, as the API turns away a version it no
|
||
// longer supports.
|
||
let versionSaid = ""
|
||
async function clientVersion() {
|
||
if (versionSaid) return versionSaid
|
||
let v = ""
|
||
const exe = await cursorExecutable()
|
||
if (exe) {
|
||
const d = basename(dirname(await realpath(exe).catch(() => exe)))
|
||
if (VERSION_RE.test(d)) v = d
|
||
}
|
||
if (!v) {
|
||
const dirs = [join(homedir(), ".local", "share", "cursor-agent", "versions")]
|
||
if (GOOS === "windows") dirs.push(join(process.env.LOCALAPPDATA ?? "", "cursor-agent", "versions"))
|
||
for (const dir of dirs)
|
||
for (const n of await readdir(dir).catch(() => [])) if (VERSION_RE.test(n) && n > v) v = n
|
||
}
|
||
versionSaid = "cli-" + (v || VERSION_FALLBACK)
|
||
return versionSaid
|
||
}
|
||
|
||
// authFile is where cursor-agent keeps its sign-in off a Mac's keychain.
|
||
function authFile() {
|
||
const home = homedir()
|
||
if (GOOS === "windows") return join(process.env.APPDATA || join(home, "AppData", "Roaming"), "Cursor", "auth.json")
|
||
if (GOOS === "darwin") return join(home, ".cursor", "auth.json")
|
||
return join(process.env.XDG_CONFIG_HOME || join(home, ".config"), "cursor", "auth.json")
|
||
}
|
||
|
||
// cliToken is the access token cursor-agent signed in with; read, never
|
||
// written.
|
||
async function cliToken() {
|
||
if (GOOS === "darwin") {
|
||
const r = await run("security", ["find-generic-password", "-s", "cursor-access-token", "-a", "cursor-user", "-w"])
|
||
if (r.ok && r.out.trim()) return r.out.trim()
|
||
}
|
||
try {
|
||
return JSON.parse(await readFile(authFile(), "utf8"))?.accessToken ?? ""
|
||
} catch {
|
||
return ""
|
||
}
|
||
}
|
||
|
||
// expiry is when a JWT runs out, in ms; 0 when it doesn't say.
|
||
function expiry(tok) {
|
||
const parts = String(tok ?? "").split(".")
|
||
if (parts.length !== 3) return 0
|
||
try {
|
||
const exp = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"))?.exp
|
||
return typeof exp === "number" && exp > 0 ? exp * 1000 : 0
|
||
} catch {
|
||
return 0
|
||
}
|
||
}
|
||
|
||
const SOON = 5 * 60 * 1000
|
||
|
||
class AuthError extends Error {
|
||
constructor(msg) {
|
||
super(msg)
|
||
this.status = 401
|
||
}
|
||
}
|
||
|
||
// cliTokenNow is cursor-agent's token; one about to run out is renewed by
|
||
// cursor-agent, which does that whenever it runs.
|
||
let cliRefresh = null
|
||
async function cliTokenNow() {
|
||
let tok = await cliToken()
|
||
const exp = expiry(tok)
|
||
if (tok && (!exp || exp - Date.now() > SOON)) return tok
|
||
const exe = await cursorExecutable()
|
||
if (exe) {
|
||
cliRefresh ??= run(exe, ["status"], { timeout: 30_000 }).finally(() => (cliRefresh = null))
|
||
await cliRefresh
|
||
tok = await cliToken()
|
||
}
|
||
// the built-in's words (provider.CursorToken)
|
||
if (!tok) throw new AuthError("Cursor isn't signed in; sign in from magpie's Providers page or run `cursor-agent login`")
|
||
const e = expiry(tok)
|
||
if (e && e <= Date.now()) throw new AuthError("Cursor's sign-in has run out; sign in again from magpie's Providers page or run `cursor-agent login`")
|
||
return tok
|
||
}
|
||
|
||
// ---- the account -----------------------------------------------------------------
|
||
|
||
async function headersFor(tok) {
|
||
return {
|
||
"Content-Type": "application/json",
|
||
"Connect-Protocol-Version": "1",
|
||
...(tok ? { Authorization: `Bearer ${tok}` } : {}),
|
||
"x-cursor-client-version": await clientVersion(),
|
||
"x-cursor-client-type": "cli",
|
||
"x-ghost-mode": "true",
|
||
}
|
||
}
|
||
|
||
// unary is a Connect call in JSON.
|
||
async function unary(base, path, tok, body = {}, timeout = 15_000) {
|
||
const res = await fetch(base + "/" + path, {
|
||
method: "POST",
|
||
headers: await headersFor(tok),
|
||
body: JSON.stringify(body),
|
||
signal: AbortSignal.timeout(timeout),
|
||
})
|
||
const text = await res.text()
|
||
if (!res.ok) {
|
||
const f = failure(res.status, text)
|
||
const e = new Error(f.message)
|
||
e.status = f.status
|
||
throw e
|
||
}
|
||
return JSON.parse(text || "{}")
|
||
}
|
||
|
||
// whoIs is the account's email and plan, as `cursor-agent about` says them.
|
||
async function whoIs(tok) {
|
||
const [me, plan] = await Promise.allSettled([
|
||
unary(API, "aiserver.v1.DashboardService/GetMe", tok),
|
||
unary(API, "aiserver.v1.DashboardService/GetPlanInfo", tok),
|
||
])
|
||
return {
|
||
email: me.status === "fulfilled" ? String(me.value?.email ?? "").trim() : "",
|
||
plan: plan.status === "fulfilled" ? String(plan.value?.planInfo?.planName ?? "").trim() : "",
|
||
}
|
||
}
|
||
|
||
// plans are the plan each token's account is on, as `cursor-agent about`
|
||
// names it (GetPlanInfo's planName, magpie's card title), asked again after
|
||
// an hour.
|
||
const plans = new Map()
|
||
const PLAN_TTL = 3600_000
|
||
async function planOf(tok) {
|
||
const had = plans.get(tok)
|
||
if (had && Date.now() - had.at < PLAN_TTL) return had.plan
|
||
try {
|
||
const plan = String((await unary(API, "aiserver.v1.DashboardService/GetPlanInfo", tok))?.planInfo?.planName ?? "").trim()
|
||
if (plan) plans.set(tok, { plan, at: Date.now() })
|
||
return plan || had?.plan || ""
|
||
} catch {
|
||
return had?.plan ?? ""
|
||
}
|
||
}
|
||
|
||
// exchanged are API keys' tokens, by the key's hash.
|
||
const exchanged = new Map()
|
||
|
||
// keyToken is the access token for a Cursor API key, as the CLI gets it
|
||
// for CURSOR_API_KEY: /auth/exchange_user_api_key, again when it runs out.
|
||
async function keyToken(key) {
|
||
const h = createHash("sha256").update(key).digest("hex")
|
||
const had = exchanged.get(h)
|
||
if (had && (!had.exp || had.exp - Date.now() > SOON)) return had.tok
|
||
const res = await fetch(API + "/auth/exchange_user_api_key", {
|
||
method: "POST",
|
||
headers: { ...(await headersFor(key)), "Connect-Protocol-Version": undefined },
|
||
body: "{}",
|
||
signal: AbortSignal.timeout(15_000),
|
||
}).catch((e) => {
|
||
throw Object.assign(new Error("couldn't reach Cursor's API: " + e.message), { status: 502 })
|
||
})
|
||
if (res.status >= 500) throw Object.assign(new Error(`exchange_user_api_key: HTTP ${res.status}`), { status: 502 })
|
||
const j = res.ok ? await res.json().catch(() => null) : null
|
||
if (!j?.accessToken) throw new AuthError("Cursor didn't take this API key")
|
||
exchanged.set(h, { tok: j.accessToken, exp: expiry(j.accessToken) })
|
||
return j.accessToken
|
||
}
|
||
|
||
// tokenOf is the token to call Cursor's API with, for a sign-in.
|
||
async function tokenOf(auth) {
|
||
if (auth?.type === "api" && auth.key) return keyToken(auth.key)
|
||
if (auth?.type !== "oauth") throw new AuthError("Cursor isn't signed in")
|
||
if (auth.refresh === CLI_MARK) return cliTokenNow()
|
||
const exp = auth.expires || expiry(auth.access)
|
||
if (!auth.access) throw new AuthError("Cursor isn't signed in")
|
||
// Cursor's sign-in has nothing to renew it with: cursor-agent itself
|
||
// signs in again when it runs out (about two months)
|
||
if (exp && exp <= Date.now()) throw new AuthError("Cursor's sign-in has run out; sign in to Cursor again")
|
||
return auth.access
|
||
}
|
||
|
||
// browserSignIn is cursor-agent's `login`: the page signs in, and
|
||
// /auth/poll hands the tokens over once it has.
|
||
async function browserSignIn() {
|
||
const verifier = randomBytes(32).toString("base64url")
|
||
const challenge = createHash("sha256").update(verifier).digest("base64url")
|
||
const uuid = randomUUID()
|
||
const url = new URL("/loginDeepControl", WEBSITE)
|
||
url.searchParams.set("challenge", challenge)
|
||
url.searchParams.set("uuid", uuid)
|
||
url.searchParams.set("mode", "login")
|
||
url.searchParams.set("redirectTarget", "cli")
|
||
return {
|
||
url: url.toString(),
|
||
instructions: "Sign in to Cursor in the browser; this finishes by itself.",
|
||
method: "auto",
|
||
async callback() {
|
||
const headers = { "Content-Type": "application/json", "x-cursor-client-version": await clientVersion(), "x-cursor-client-type": "cli" }
|
||
let errors = 0
|
||
for (let r = 0; r < 150; r++) {
|
||
const wait = Math.min(1000 * 1.2 ** r, 10_000)
|
||
let res
|
||
try {
|
||
res = await fetch(`${API}/auth/poll?uuid=${uuid}&verifier=${verifier}`, { headers, signal: AbortSignal.timeout(15_000) })
|
||
} catch {
|
||
if (++errors >= 3) return { type: "failed", error: "couldn't reach Cursor" }
|
||
await sleep(wait)
|
||
continue
|
||
}
|
||
if (res.status === 404) {
|
||
errors = 0
|
||
await sleep(wait)
|
||
continue
|
||
}
|
||
if (!res.ok) {
|
||
if (res.status === 403 || ++errors >= 3) return { type: "failed", error: res.status === 403 ? "Cursor refused the sign-in (403)" : `Cursor's sign-in answered HTTP ${res.status}` }
|
||
await sleep(wait)
|
||
continue
|
||
}
|
||
const j = await res.json().catch(() => null)
|
||
if (!j?.accessToken || !("refreshToken" in j)) return { type: "failed", error: "Cursor sent back no token" }
|
||
const who = await whoIs(j.accessToken)
|
||
return {
|
||
type: "success",
|
||
access: j.accessToken,
|
||
refresh: j.refreshToken || "none",
|
||
expires: expiry(j.accessToken) || Date.now() + 30 * 24 * 3600 * 1000,
|
||
...(who.email ? { accountId: who.email } : {}),
|
||
...(who.plan ? { plan: who.plan } : {}),
|
||
}
|
||
}
|
||
return { type: "failed", error: "the sign-in timed out" }
|
||
},
|
||
}
|
||
}
|
||
|
||
// cliSignIn takes the account cursor-agent is signed in to; its token is
|
||
// read each time from where cursor-agent keeps it.
|
||
async function cliSignIn() {
|
||
return {
|
||
url: "",
|
||
instructions: "Uses the account `cursor-agent login` signed in to.",
|
||
method: "auto",
|
||
async callback() {
|
||
const tok = await cliToken()
|
||
const exp = expiry(tok)
|
||
if (!tok) return { type: "failed", error: "cursor-agent isn't signed in: run `cursor-agent login`" }
|
||
if (exp && exp <= Date.now()) return { type: "failed", error: "cursor-agent's sign-in has run out: run `cursor-agent login` again" }
|
||
const who = await whoIs(tok)
|
||
if (!who.email) return { type: "failed", error: "Cursor couldn't say which account is signed in" }
|
||
return { type: "success", access: "", refresh: CLI_MARK, expires: 0, accountId: who.email, ...(who.plan ? { plan: who.plan } : {}) }
|
||
},
|
||
}
|
||
}
|
||
|
||
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
|
||
|
||
// ---- usage ------------------------------------------------------------------------
|
||
//
|
||
// How much of the plan's included usage is gone, as magpie's built-in Cursor
|
||
// account shows it (internal/provider/cursor_usage.go): the dashboard's
|
||
// current period, split into the Cursor Models and Other Models pools.
|
||
|
||
// statusText is a refused request's status as magpie says it (Go's
|
||
// http.StatusText).
|
||
const statusText = (s) =>
|
||
({ 413: "Request Entity Too Large", 414: "Request URI Too Long", 416: "Requested Range Not Satisfiable", 418: "I'm a teapot", 509: "" })[s] ?? STATUS_CODES[s] ?? ""
|
||
|
||
const POOL_VARIANT = /-(fast|none|low|medium|high|xhigh|extra-high|max|thinking)$/
|
||
|
||
// 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().replace(/@[^@]*$/, "")
|
||
if (model.startsWith("cursor-")) model = model.slice("cursor-".length)
|
||
for (;;) {
|
||
const b = model.replace(POOL_VARIANT, "")
|
||
if (b === model) return model
|
||
model = b
|
||
}
|
||
}
|
||
|
||
// Cursor's autoBucketModels can lag model releases: it still omitted Grok
|
||
// 4.6/4.7 when the published Cursor Models pool already included them.
|
||
// 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 + "-"))
|
||
}
|
||
|
||
// usage is the account's windows this billing period; an enterprise plan
|
||
// reports spend instead, and gets none. ids are the models it can be
|
||
// asked for, each counted by the pool Cursor bills it to.
|
||
async function usage(tok, ids) {
|
||
const res = await fetch(API + "/aiserver.v1.DashboardService/GetCurrentPeriodUsage", {
|
||
method: "POST",
|
||
headers: { Authorization: `Bearer ${tok}`, "Content-Type": "application/json", "Connect-Protocol-Version": "1" },
|
||
body: "{}",
|
||
signal: AbortSignal.timeout(15_000),
|
||
})
|
||
if (!res.ok) return { error: statusText(res.status), windows: [] }
|
||
const data = await res.json()
|
||
const u = data?.planUsage
|
||
if (!u) return { windows: [] }
|
||
const ms = /^[+-]?\d+$/.test(data.billingCycleEnd ?? "") ? Number(data.billingCycleEnd) : 0
|
||
const resets = ms > 0 ? { resetsAt: new Date(ms).toISOString() } : {}
|
||
const bucket = new Set((data.autoBucketModels ?? []).map(poolBase))
|
||
const inPool = (model) => {
|
||
model = poolBase(model)
|
||
if (model === "auto") model = "default" // the CLI's Auto is default in Cursor's API
|
||
// the server names a family (grok-4.8); the CLI asks for one at an
|
||
// effort or speed (grok-4.8-high-fast)
|
||
return bucket.has(model) || firstParty(model)
|
||
}
|
||
// Auto is always one, so the pool's list is never empty (which would
|
||
// count every model)
|
||
const pool = [...new Set(["auto", ...ids, ...(data.autoBucketModels ?? [])])].filter(inPool)
|
||
const num = (v) => (typeof v === "number" ? v : 0)
|
||
// the two pools fit the line; the total goes in its tooltip
|
||
return {
|
||
windows: [
|
||
{ name: "Cursor Models", used: num(u.autoPercentUsed), ...resets, models: pool },
|
||
{ name: "Other Models", used: num(u.apiPercentUsed), ...resets, notModels: pool },
|
||
{ name: "Total", used: num(u.totalPercentUsed), ...resets, aside: true },
|
||
],
|
||
}
|
||
}
|
||
|
||
// ---- models ----------------------------------------------------------------------
|
||
//
|
||
// 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.
|
||
|
||
const LEVEL_RANK = ["none", "minimal", "low", "medium", "high", "xhigh", "max"]
|
||
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 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
|
||
let thinking = false
|
||
let effort = ""
|
||
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 ID_EFFORTS) {
|
||
if ((b = cut(s, "-" + word)) !== null) {
|
||
;[s, effort] = [b, level]
|
||
break
|
||
}
|
||
}
|
||
if (!thinking && (b = cut(s, "-thinking")) !== null) [s, thinking] = [b, true]
|
||
if (thinking) s += "-thinking"
|
||
if (fast) s += "-fast"
|
||
return [s, effort]
|
||
}
|
||
|
||
// 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", "claude-haiku-4-5"])
|
||
|
||
const clean = (s) => String(s ?? "").replaceAll("\u200b", "").split(/\s+/).filter(Boolean).join(" ")
|
||
|
||
// 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) => {
|
||
const vs = m?.variants ?? []
|
||
return vs.find((v) => v.isDefaultNonMaxConfig) ?? vs.find((v) => v.isDefaultMaxConfig) ?? vs[0]
|
||
}
|
||
|
||
// 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) ?? (seed ? branch(entry, want) : undefined) ?? (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 }
|
||
}
|
||
|
||
// branch is, for an id of Cursor's own asked at an effort no variant has
|
||
// with the rest of its parameters (claude-opus-5-low at xhigh: Opus 5
|
||
// without thinking stops at high), the variant with those parameters at the
|
||
// nearest effort it has, not the default's id with parameters that
|
||
// contradict it (claude-opus-5-thinking-high with thinking=false). Where
|
||
// the default differs from what is asked in the effort alone, the effort
|
||
// goes as asked, as for a listed model.
|
||
function branch(entry, want) {
|
||
if (!entry.pid || entry.pid === "thinking") return undefined
|
||
const def = valuesOf(entry.def)
|
||
if (Object.keys(want).every((k) => k === entry.pid || k === "context" || def[k] === undefined || def[k] === want[k])) return undefined
|
||
const vs = entry.variants.filter((v) => {
|
||
const have = valuesOf(v)
|
||
return entry.pid in have && Object.keys(want).every((k) => k === entry.pid || have[k] === undefined || have[k] === want[k])
|
||
})
|
||
const level = (x) => (x === "extra-high" ? "xhigh" : x)
|
||
const at = fitEffort(level(want[entry.pid]), vs.map((v) => level(valuesOf(v)[entry.pid])))
|
||
return at ? vs.find((v) => level(valuesOf(v)[entry.pid]) === at) : undefined
|
||
}
|
||
|
||
// 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,
|
||
name: m.name,
|
||
api: { id: m.id, url: AGENT, npm: CHAT },
|
||
status: "active",
|
||
headers: {},
|
||
options: {},
|
||
cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },
|
||
limit: { context: m.context || DEFAULT_CONTEXT, output: 0 },
|
||
capabilities: {
|
||
temperature: false,
|
||
reasoning: efforts.length > 0,
|
||
attachment: true,
|
||
toolcall: true,
|
||
input: { text: true, image: true, audio: false, video: false, pdf: false },
|
||
output: { text: true, image: false, audio: false, video: false, pdf: false },
|
||
interleaved: false,
|
||
},
|
||
release_date: "",
|
||
variants: Object.fromEntries(efforts.map((e) => [e, { reasoningEffort: e }])),
|
||
}
|
||
}
|
||
|
||
|
||
// ---- protobuf, by hand ------------------------------------------------------------
|
||
|
||
class PB {
|
||
parts = []
|
||
push(b) {
|
||
this.parts.push(b)
|
||
return this
|
||
}
|
||
uv(v) {
|
||
const out = []
|
||
while (v >= 0x80) {
|
||
out.push((v % 128) | 0x80)
|
||
v = Math.floor(v / 128)
|
||
}
|
||
out.push(v)
|
||
return this.push(Uint8Array.from(out))
|
||
}
|
||
tag(num, wire) {
|
||
return this.uv(num * 8 + wire)
|
||
}
|
||
varint(num, v) {
|
||
return this.tag(num, 0).uv(v)
|
||
}
|
||
bytes(num, v) {
|
||
const b = v instanceof PB ? v.done() : v ?? EMPTY
|
||
return this.tag(num, 2).uv(b.length).push(b)
|
||
}
|
||
str(num, s) {
|
||
return this.bytes(num, Buffer.from(s, "utf8"))
|
||
}
|
||
double(num, v) {
|
||
const b = Buffer.alloc(8)
|
||
b.writeDoubleLE(v)
|
||
return this.tag(num, 1).push(b)
|
||
}
|
||
done() {
|
||
return Buffer.concat(this.parts)
|
||
}
|
||
}
|
||
const pb = () => new PB()
|
||
|
||
// fields reads a message's fields; a malformed tail is dropped.
|
||
function fields(b) {
|
||
b = Buffer.isBuffer(b) ? b : Buffer.from(b ?? EMPTY)
|
||
const out = []
|
||
let i = 0
|
||
const uv = () => {
|
||
let v = 0
|
||
let m = 1
|
||
while (i < b.length) {
|
||
const c = b[i++]
|
||
v += (c & 0x7f) * m
|
||
if (c < 0x80) return v
|
||
m *= 128
|
||
}
|
||
return -1
|
||
}
|
||
while (i < b.length) {
|
||
const key = uv()
|
||
if (key < 0) break
|
||
const f = { num: Math.floor(key / 8), wire: key % 8, n: 0, data: EMPTY }
|
||
switch (f.wire) {
|
||
case 0: {
|
||
const v = uv()
|
||
if (v < 0) return out
|
||
f.n = v
|
||
break
|
||
}
|
||
case 1:
|
||
if (i + 8 > b.length) return out
|
||
f.data = b.subarray(i, i + 8)
|
||
f.n = Number(b.readBigUInt64LE(i))
|
||
i += 8
|
||
break
|
||
case 2: {
|
||
const l = uv()
|
||
if (l < 0 || i + l > b.length) return out
|
||
f.data = b.subarray(i, i + l)
|
||
i += l
|
||
break
|
||
}
|
||
case 5:
|
||
if (i + 4 > b.length) return out
|
||
f.n = b.readUInt32LE(i)
|
||
i += 4
|
||
break
|
||
default:
|
||
return out
|
||
}
|
||
out.push(f)
|
||
}
|
||
return out
|
||
}
|
||
|
||
const field = (fs, num) => fs.find((f) => f.num === num)
|
||
const pbStr = (fs, num) => field(fs, num)?.data.toString("utf8") ?? ""
|
||
const pbNum = (fs, num) => field(fs, num)?.n ?? 0
|
||
|
||
// pbValue is a google.protobuf.Value.
|
||
function pbValue(v) {
|
||
if (v === null || v === undefined) return pb().varint(1, 0)
|
||
if (typeof v === "number") return pb().double(2, v)
|
||
if (typeof v === "string") return pb().str(3, v)
|
||
if (typeof v === "boolean") return pb().varint(4, v ? 1 : 0)
|
||
if (Array.isArray(v)) {
|
||
const l = pb()
|
||
for (const e of v) l.bytes(1, pbValue(e))
|
||
return pb().bytes(6, l)
|
||
}
|
||
if (typeof v === "object") {
|
||
const st = pb()
|
||
for (const k of Object.keys(v).sort()) st.bytes(1, pb().str(1, k).bytes(2, pbValue(v[k])))
|
||
return pb().bytes(5, st)
|
||
}
|
||
return pb().varint(1, 0)
|
||
}
|
||
|
||
// pbAny reads a google.protobuf.Value.
|
||
function pbAny(b) {
|
||
for (const f of fields(b)) {
|
||
switch (f.num) {
|
||
case 1:
|
||
return null
|
||
case 2:
|
||
return f.data.length === 8 ? f.data.readDoubleLE(0) : 0
|
||
case 3:
|
||
return f.data.toString("utf8")
|
||
case 4:
|
||
return f.n !== 0
|
||
case 5: {
|
||
const m = {}
|
||
for (const e of fields(f.data)) {
|
||
if (e.num !== 1) continue
|
||
const kv = fields(e.data)
|
||
const v = field(kv, 2)
|
||
m[pbStr(kv, 1)] = v ? pbAny(v.data) : null
|
||
}
|
||
return m
|
||
}
|
||
case 6:
|
||
return fields(f.data).filter((e) => e.num === 1).map((e) => pbAny(e.data))
|
||
}
|
||
}
|
||
return null
|
||
}
|
||
|
||
// frame wraps a message as one frame of a Connect stream.
|
||
function frame(msg) {
|
||
const head = Buffer.alloc(5)
|
||
head.writeUInt32BE(msg.length, 1)
|
||
return Buffer.concat([head, msg])
|
||
}
|
||
|
||
// frames reads a Connect stream's frames: a message, or the stream's end
|
||
// with its JSON.
|
||
async function* frames(body) {
|
||
let buf = EMPTY
|
||
for await (const chunk of body) {
|
||
buf = buf.length ? Buffer.concat([buf, chunk]) : Buffer.from(chunk)
|
||
while (buf.length >= 5) {
|
||
const n = buf.readUInt32BE(1)
|
||
if (n > 64 << 20) throw new Error("a malformed stream")
|
||
if (buf.length < 5 + n) break
|
||
const flags = buf[0]
|
||
let data = buf.subarray(5, 5 + n)
|
||
buf = buf.subarray(5 + n)
|
||
if (flags & 1) data = gunzipSync(data)
|
||
yield { end: (flags & 2) !== 0, data }
|
||
}
|
||
}
|
||
if (buf.length) throw new Error("unexpected EOF")
|
||
}
|
||
|
||
// ---- failures -----------------------------------------------------------------------
|
||
|
||
const regional = (msg) => msg.toLowerCase().includes("region")
|
||
|
||
// failure is the status and message for Cursor's Connect error, whose
|
||
// details say it best: {"code", "message", "details": [{"debug": {"error",
|
||
// "details": {"title", "detail"}}}]}, or the same under "error" at a
|
||
// stream's end.
|
||
function failure(status, text) {
|
||
let e
|
||
try {
|
||
e = JSON.parse(typeof text === "string" ? text : Buffer.from(text).toString("utf8"))
|
||
} catch {
|
||
return statusOf(status, "", String(text ?? "").trim())
|
||
}
|
||
const f = e?.error && typeof e.error === "object" ? e.error : e ?? {}
|
||
let msg = f.message ?? ""
|
||
for (const d of f.details ?? []) {
|
||
const t = `${d?.debug?.details?.title ?? ""}: ${d?.debug?.details?.detail ?? ""}`.trim()
|
||
if (t !== ":") msg = t.replace(/^[:\s]+|[:\s]+$/g, "")
|
||
}
|
||
if (!msg || msg === "Error") msg = f.code ?? ""
|
||
if (!f.code && status >= 200 && status < 300) return { status, message: msg } // a stream that ended well
|
||
return statusOf(status, f.code ?? "", msg)
|
||
}
|
||
|
||
// statusOf is the status and words for Cursor's error of this code and
|
||
// message. A region the team isn't served in says so, not to sign in.
|
||
function statusOf(status, code, msg) {
|
||
msg ||= statusText(status)
|
||
const low = msg.toLowerCase()
|
||
// magpie names the provider before the message itself
|
||
const out = (status, message) => ({ status, message })
|
||
if (regional(msg)) return out(403, msg + " — Cursor serves your team only in some regions and turned this request away; signing in again won't change that")
|
||
if (code === "permission_denied") return out(403, msg)
|
||
if (code === "unauthenticated" || status === 401 || low.includes("expired"))
|
||
return out(401, msg + " — sign in to Cursor again in magpie")
|
||
if (code === "resource_exhausted" || low.includes("quota") || low.includes("rate limit") || low.includes("usage limit"))
|
||
return out(429, "usage limit reached: " + msg)
|
||
if (low.includes("too long") || low.includes("context length") || low.includes("too many tokens"))
|
||
return out(400, "input is too long for the model's context: " + msg)
|
||
if (code === "invalid_argument") return out(400, msg)
|
||
if (code === "unavailable") return out(503, msg)
|
||
if (code) return out(502, msg)
|
||
return out(status, msg)
|
||
}
|
||
|
||
// ---- the conversation -------------------------------------------------------------
|
||
|
||
const textOf = (c) =>
|
||
typeof c === "string" ? c : Array.isArray(c) ? c.filter((p) => p?.type === "text").map((p) => p.text ?? "").join("") : ""
|
||
|
||
// joinSplitCalls joins an assistant message onto the assistant message
|
||
// before it when that one made tool calls: Anthropic takes consecutive
|
||
// assistant messages as one turn, and an agent can send a turn's parallel
|
||
// calls split over two of them, answered together after (yetone/magpie#1275).
|
||
// Answered at the second message, the first calls would be marked
|
||
// interrupted and their real results dropped. msgs is left as it is.
|
||
function joinSplitCalls(msgs) {
|
||
const out = []
|
||
for (const m of msgs) {
|
||
const prev = out.at(-1)
|
||
if (m?.role === "assistant" && prev?.role === "assistant" && prev.tool_calls?.length) {
|
||
const content = [textOf(prev.content), textOf(m.content)].filter(Boolean).join("\n\n")
|
||
out[out.length - 1] = { ...prev, content, tool_calls: [...prev.tool_calls, ...(m.tool_calls ?? [])] }
|
||
continue
|
||
}
|
||
out.push(m)
|
||
}
|
||
return out
|
||
}
|
||
|
||
function parseArgs(s) {
|
||
if (s && typeof s === "object") return s
|
||
try {
|
||
const v = JSON.parse(s || "{}")
|
||
return v && typeof v === "object" ? v : {}
|
||
} catch {
|
||
return {}
|
||
}
|
||
}
|
||
|
||
// tools are the caller's tools the model may call: none for tool_choice
|
||
// "none", the one named when one is.
|
||
function toolsOf(chat) {
|
||
const choice = chat.tool_choice
|
||
const only = typeof choice === "object" ? choice?.function?.name : ""
|
||
if (choice === "none") return []
|
||
return (chat.tools ?? [])
|
||
.filter((t) => t?.type === "function" && t.function?.name && (!only || t.function.name === only))
|
||
.map((t) => {
|
||
const schema = t.function.parameters ?? { type: "object", properties: {} }
|
||
return { name: t.function.name, description: t.function.description ?? "", schema, schemaText: JSON.stringify(schema) }
|
||
})
|
||
}
|
||
|
||
// catalog lists the caller's tools for the model. It sees MCP tools only
|
||
// through Cursor's GetDynamicTools and CallDynamicTool, and Cursor names
|
||
// them in a prompt of its own, which this conversation goes without:
|
||
// without the list the model says it has no such tool. Each entry is
|
||
// written as the CallDynamicTool call itself: listed as <tool name=...>,
|
||
// some models (GLM-5.3) called the names bare, as tools of their own, and
|
||
// those calls went nowhere (#11).
|
||
function catalog(tools) {
|
||
if (!tools.length) return ""
|
||
let s = `\n\n<dynamic_tool_catalog>\nThe MCP namespace "magpie" has the tools below. They are reached only through \`${CALL}\`, never as tools of their own. Each entry is the exact call to make; its arguments must match the schema given. The schemas are complete, so there is no need to call \`GetDynamicTools\` first.\n`
|
||
for (const t of tools) s += `<call>${CALL}({"namespace":"magpie","toolName":"${t.name}","arguments":{...}})\n${t.description}\narguments schema: ${t.schemaText}\n</call>\n`
|
||
return s + "</dynamic_tool_catalog>"
|
||
}
|
||
|
||
// callID is a call's id as Cursor gave it, before the line in it was sent
|
||
// to the caller as __.
|
||
const callID = (id) => (id.startsWith("call_") ? id.replace("__fc_", "\nfc_") : id)
|
||
|
||
function result(id, text, isError) {
|
||
let res = text
|
||
try {
|
||
res = JSON.parse(text)
|
||
} catch {}
|
||
return {
|
||
type: "tool-result",
|
||
toolCallId: callID(id),
|
||
toolName: CALL,
|
||
result: res,
|
||
experimental_content: [{ type: "text", text }],
|
||
...(isError ? { isError: true } : {}),
|
||
}
|
||
}
|
||
|
||
// messages is the conversation as AI SDK messages, in JSON: the system
|
||
// prompt with the caller's tools listed, then each turn. A tool result
|
||
// answers a call of the reply before it; a call left unanswered is
|
||
// answered for, as the API wants every call to have its result. With it,
|
||
// the user's last words and a guess at the prompt's size.
|
||
function conversation(chat, tools) {
|
||
const out = []
|
||
const add = (m) => out.push(Buffer.from(JSON.stringify(m)))
|
||
let size = 0
|
||
const system = []
|
||
let last = "."
|
||
for (const m of chat.messages ?? [])
|
||
if (m.role === "system" || m.role === "developer") system.push(textOf(m.content))
|
||
const sys = system.filter(Boolean).join("\n\n") + catalog(tools)
|
||
size += sys.length
|
||
if (sys) add({ role: "system", content: sys })
|
||
let pending = []
|
||
let results = []
|
||
const answer = () => {
|
||
for (const id of pending) results.push(result(id, NO_RESULT, true))
|
||
pending = []
|
||
if (results.length) add({ role: "tool", content: results })
|
||
results = []
|
||
}
|
||
for (const m of joinSplitCalls(chat.messages ?? [])) {
|
||
if (m.role === "system" || m.role === "developer") continue
|
||
if (m.role === "tool") {
|
||
const id = m.tool_call_id ?? ""
|
||
const i = pending.indexOf(id)
|
||
if (i >= 0) {
|
||
pending.splice(i, 1)
|
||
const text = textOf(m.content)
|
||
size += text.length
|
||
results.push(result(id, text, false))
|
||
}
|
||
continue
|
||
}
|
||
if (m.role === "assistant") {
|
||
answer()
|
||
const content = []
|
||
const text = textOf(m.content)
|
||
if (text) content.push({ type: "text", text })
|
||
size += text.length
|
||
for (const c of m.tool_calls ?? []) {
|
||
if (c?.type && c.type !== "function") continue
|
||
const args = parseArgs(c.function?.arguments)
|
||
size += (c.function?.arguments ?? "").length + (c.function?.name ?? "").length
|
||
content.push({ type: "tool-call", toolCallId: callID(c.id), toolName: CALL, args: { namespace: "magpie", toolName: c.function?.name, arguments: args } })
|
||
pending.push(c.id)
|
||
}
|
||
if (content.length) add({ role: "assistant", content })
|
||
continue
|
||
}
|
||
// the user's
|
||
const content = []
|
||
const parts = typeof m.content === "string" ? [{ type: "text", text: m.content }] : m.content ?? []
|
||
let first = ""
|
||
for (const p of parts) {
|
||
if (p?.type === "text" && p.text) {
|
||
content.push({ type: "text", text: p.text })
|
||
size += p.text.length
|
||
first ||= p.text
|
||
} else if (p?.type === "image_url") {
|
||
const u = typeof p.image_url === "string" ? p.image_url : p.image_url?.url ?? ""
|
||
const d = /^data:([^;,]+);base64,(.*)$/s.exec(u)
|
||
if (d) content.push({ type: "image", mimeType: d[1], image: { __type: "Uint8Array", hex: Buffer.from(d[2], "base64").toString("hex") } })
|
||
else content.push({ type: "text", text: `[attachment image: ${u}]` })
|
||
} else if (p?.type === "file") {
|
||
content.push({ type: "text", text: `[attachment ${p.file?.filename ?? "file"}]` })
|
||
}
|
||
}
|
||
answer()
|
||
if (first) last = first
|
||
if (content.length) add({ role: "user", content })
|
||
}
|
||
answer()
|
||
for (const t of tools) size += t.name.length + t.description.length + t.schemaText.length
|
||
return { msgs: out, last, estimate: Math.floor(size / 4) }
|
||
}
|
||
|
||
// ---- the request ---------------------------------------------------------------------
|
||
|
||
// toolDef is an McpToolDefinition.
|
||
function toolDef(t) {
|
||
const d = pb().str(1, t.name)
|
||
if (t.description) d.str(2, t.description)
|
||
d.bytes(3, pbValue(t.schema))
|
||
return d.str(4, "magpie").str(5, t.name).str(6, t.schemaText).done()
|
||
}
|
||
|
||
const env = () => pb().str(1, GOOS).str(2, tmpdir()).str(10, "UTC")
|
||
|
||
// SESSION carries the session magpie (or OpenCode) names a request's
|
||
// conversation by, from the chat.headers hook to the loader's fetch. It
|
||
// goes no further: nothing of the request's headers is sent to Cursor.
|
||
const SESSION = "x-magpie-cursor-session"
|
||
|
||
// sessionOf is the session a chat.headers hook is told, "" when it names
|
||
// none. magpie, with no session header from the agent, makes one of the
|
||
// conversation's first user message ("magpie-" and 24 hex digits): that
|
||
// names no session, and is taken as none, as the built-in's
|
||
// nativeSessionOf takes no such header as none.
|
||
function sessionOf(input) {
|
||
const s = String(input?.sessionID ?? "").trim()
|
||
if (!s || /^magpie-[0-9a-f]{24}$/.test(s)) return ""
|
||
return s.slice(0, 128)
|
||
}
|
||
|
||
// uuidOf is a version 4 UUID made of a hash's first 16 bytes.
|
||
function uuidOf(b) {
|
||
b = Buffer.from(b.subarray(0, 16))
|
||
b[6] = (b[6] & 0x0f) | 0x40
|
||
b[8] = (b[8] & 0x3f) | 0x80
|
||
const h = b.toString("hex")
|
||
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`
|
||
}
|
||
|
||
// conversationID is the AgentRunRequest's conversation_id for a
|
||
// conversation, as the built-in's cursorConversation makes it (magpie
|
||
// #498): the same on every request of it, so that Cursor's backend keeps
|
||
// sending it to the machine that has its prompt cached — Grok on Cursor
|
||
// caches by machine. Cursor's own client keeps one conversationId per
|
||
// agent session, so it is made from what names the session — the
|
||
// client's prompt_cache_key (Codex's thread id), else the session magpie
|
||
// or OpenCode names — together with the conversation's first user
|
||
// message, which every later request repeats: subagents running at once
|
||
// under one session (Claude Code's Task agents share its session id) are
|
||
// separate conversations to Cursor, as they are to its own client. With
|
||
// nothing naming the session it is "", and each Run gets a new id as
|
||
// before: a first message alone ("hi") would put strangers' conversations
|
||
// under one id.
|
||
function conversationID(chat, session) {
|
||
const key = (typeof chat?.prompt_cache_key === "string" ? chat.prompt_cache_key.trim() : "") || session || ""
|
||
if (!key) return ""
|
||
const msgs = Array.isArray(chat?.messages) ? chat.messages : []
|
||
const first = msgs.find((m) => m?.role === "user") ?? msgs[0]
|
||
const sum = createHash("sha256")
|
||
.update("cursor conversation\0" + key + "\0" + (first === undefined ? "" : JSON.stringify(first)))
|
||
.digest()
|
||
return uuidOf(sum)
|
||
}
|
||
|
||
// usageOf is a TurnEndedUpdate as magpie counts usage, as the built-in's
|
||
// cursorUsage has it (magpie #498). Its input_tokens is the whole prompt,
|
||
// what was read from the cache and written to it included, as Cursor's
|
||
// own client has it (it takes both out to get the prompt's uncached
|
||
// rest); input is that rest, the cache added back for prompt_tokens. Its
|
||
// reasoning_tokens is kept as reasoning.
|
||
function usageOf(uf) {
|
||
const [inp, cr, cw] = [pbNum(uf, 1), pbNum(uf, 3), pbNum(uf, 4)]
|
||
return { input: Math.max(inp - cr - cw, 0), output: pbNum(uf, 2), cacheRead: cr, cacheWrite: cw, reasoning: pbNum(uf, 5) }
|
||
}
|
||
|
||
// A step that calls the caller's tools has no TurnEndedUpdate: Cursor ends
|
||
// the turn, and tells what it used, only once the client has run the tools
|
||
// and sent their results in the same Run, and the caller runs them after
|
||
// the Run is closed (magpie #676). Before that it sends nothing more than
|
||
// the conversation's checkpoint and heartbeats, and a cancelled Run ends
|
||
// with an error and no usage either. The dashboard's usage events count
|
||
// every Run, by its conversation_id, the uncached input apart from what
|
||
// was read from the cache and written to it, as TurnEndedUpdate does; one
|
||
// shows about 2.5 s after the Run is closed. STEP_WAIT is how long a step's
|
||
// is looked for: first after that long, then every so often until the
|
||
// end (each ask given `ask` to answer).
|
||
//
|
||
// Far from Cursor none shows in that time (yetone/magpie#1053: a round
|
||
// trip of 4.2 s, the event 4–5 s after the Run closes), and waiting longer
|
||
// would hold every tool step longer. So a tool step's Run is owed by its
|
||
// conversation (owed): the step counts every event of the conversation's
|
||
// owed Runs that has shown by the end of its wait, its own or earlier
|
||
// ones, and what hasn't shown is collected by the conversation's next
|
||
// steps, in the background from `owedAt` after the Run closed. A step
|
||
// whose count is still owed says 0 used; the conversation's steps add up
|
||
// to what Cursor counted, some of it a step or two late. A step that ends
|
||
// the turn waits for what is still owed, as nothing may follow it soon.
|
||
// After `skipAfter` steps in a row of an account's left something owed,
|
||
// its steps stop waiting (but every `retryEvery`th), and leave it to the
|
||
// next. A debt not collected in `owedFor` is let go. With no
|
||
// conversation_id naming the session nothing later could collect a Run's
|
||
// event: its step looks for it as before, and is guessed without it.
|
||
const STEP_WAIT = { first: 1500, every: 500, until: 6000, ask: 5000, skipAfter: 3, retryEvery: 10, owedAt: 5000, owedFor: 600_000 }
|
||
|
||
// claimed are the usage events already counted, by conversation and time,
|
||
// so that two steps of one conversation never count the same one.
|
||
const claimed = new Map()
|
||
|
||
// owed are what each conversation still owes, by conversation_id: n Runs
|
||
// whose events are to be counted, the earliest started at since, looked
|
||
// for from ready on; and carry, a count collected that no step gave out.
|
||
const owed = new Map()
|
||
|
||
// flights are each conversation's collections still going, which a step
|
||
// that ends the turn waits for.
|
||
const flights = new Map()
|
||
|
||
// unreadable are the accounts (by token) whose usage events Cursor won't
|
||
// give (a 4xx), for owedFor: their steps are guessed, as before, and owe
|
||
// nothing. misses are how many steps in a row of an account's left
|
||
// something owed.
|
||
const unreadable = new Map()
|
||
const misses = new Map()
|
||
const tokKey = (tok) => createHash("sha256").update(tok).digest("hex")
|
||
function canRead(tok) {
|
||
const at = unreadable.get(tokKey(tok))
|
||
if (at === undefined) return true
|
||
if (Date.now() - at < STEP_WAIT.owedFor) return false
|
||
unreadable.delete(tokKey(tok))
|
||
return true
|
||
}
|
||
|
||
const ZERO = () => ({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reasoning: 0 })
|
||
function addUsage(a, b) {
|
||
if (!b) return a
|
||
if (!a) return { ...b }
|
||
for (const k of Object.keys(a)) a[k] += b[k] ?? 0
|
||
return a
|
||
}
|
||
|
||
// owe adds to conversation conv's debt.
|
||
function owe(conv, { since = Date.now(), n = 0, ready = Date.now(), carry = null }) {
|
||
for (const [k, d] of owed) if (Date.now() - d.since > STEP_WAIT.owedFor) owed.delete(k)
|
||
if (n <= 0 && !carry) return
|
||
const d = owed.get(conv)
|
||
if (!d) return void owed.set(conv, { since, n, ready, carry: carry && { ...carry } })
|
||
d.since = Math.min(d.since, since)
|
||
d.n += n
|
||
d.ready = Math.min(d.ready, ready)
|
||
d.carry = addUsage(d.carry, carry)
|
||
}
|
||
|
||
// usageEvents are the dashboard's events of conversation conv from since
|
||
// on, the `want` oldest not counted yet, summed: usage (null when none),
|
||
// how many were found (got), and refused when the account can't read them.
|
||
// Polling, they are looked for as STEP_WAIT says, from `first` on, until
|
||
// all show; else asked for once.
|
||
async function usageEvents(tok, conv, since, want, { poll = false, first = STEP_WAIT.first, signal } = {}) {
|
||
const end = Date.now() + STEP_WAIT.until
|
||
const wait = (ms) => new Promise((r) => {
|
||
const t = setTimeout(r, ms)
|
||
signal?.addEventListener("abort", () => (clearTimeout(t), r()), { once: true })
|
||
})
|
||
for (const [k, at] of claimed) if (Date.now() - at > 2 * STEP_WAIT.owedFor) claimed.delete(k)
|
||
const num = (v) => (Number.isFinite(Number(v)) ? Number(v) : 0)
|
||
let usage = null
|
||
let got = 0
|
||
if (poll) await wait(first)
|
||
while (!signal?.aborted) {
|
||
try {
|
||
const res = await fetch(API + "/aiserver.v1.DashboardService/GetFilteredUsageEvents", {
|
||
method: "POST",
|
||
headers: await headersFor(tok),
|
||
body: JSON.stringify({ teamId: 0, startDate: String(since - 60_000), endDate: String(Date.now() + 60_000), page: 1, pageSize: 50 }),
|
||
signal: AbortSignal.timeout(STEP_WAIT.ask),
|
||
})
|
||
if (res.status >= 400 && res.status < 500 && res.status !== 429) {
|
||
// not this account's to read
|
||
unreadable.set(tokKey(tok), Date.now())
|
||
return { usage, got, refused: true }
|
||
}
|
||
const evs = res.ok ? ((await res.json())?.usageEventsDisplay ?? []) : []
|
||
const found = evs
|
||
.filter((e) => e?.conversationId === conv && e.tokenUsage && num(e.timestamp) >= since - 1000 && !claimed.has(conv + "\0" + e.timestamp))
|
||
.sort((a, b) => num(a.timestamp) - num(b.timestamp))
|
||
.slice(0, want - got)
|
||
for (const ev of found) {
|
||
claimed.set(conv + "\0" + ev.timestamp, Date.now())
|
||
const t = ev.tokenUsage
|
||
usage = addUsage(usage ?? ZERO(), { input: num(t.inputTokens), output: num(t.outputTokens), cacheRead: num(t.cacheReadTokens), cacheWrite: num(t.cacheWriteTokens) })
|
||
got++
|
||
}
|
||
if (got >= want) break
|
||
} catch {}
|
||
if (!poll || Date.now() + STEP_WAIT.every > end) break
|
||
await wait(STEP_WAIT.every)
|
||
}
|
||
return { usage, got, refused: false }
|
||
}
|
||
|
||
// stepUsage is the usage of the Run of conversation conv started at since,
|
||
// as the dashboard's usage events count it; null when none shows in time.
|
||
async function stepUsage(tok, conv, since, signal) {
|
||
return (await usageEvents(tok, conv, since, 1, { poll: true, signal })).usage
|
||
}
|
||
|
||
// collect takes conversation conv's debt and asks for its events (polling
|
||
// when poll): what was carried and what was found (usage), and whether
|
||
// any is still owed (short), which is owed on. What comes back once the
|
||
// Run that asked is over (over() true) is carried to the next instead.
|
||
function collect(tok, conv, { poll = false, first, signal, over = () => false } = {}) {
|
||
const d = owed.get(conv)
|
||
if (!d) return Promise.resolve({ usage: null, short: false })
|
||
owed.delete(conv)
|
||
const p = (async () => {
|
||
let r = { usage: null, got: 0 }
|
||
if (d.n > 0 && canRead(tok)) r = await usageEvents(tok, conv, d.since, d.n, { poll, first, signal }).catch(() => r)
|
||
const short = d.n > r.got && canRead(tok)
|
||
if (short) owe(conv, { since: d.since, n: d.n - r.got, ready: d.ready })
|
||
const usage = addUsage(d.carry, r.usage)
|
||
if (!over()) return { usage, short }
|
||
owe(conv, { carry: usage })
|
||
return { usage: null, short }
|
||
})()
|
||
const set = flights.get(conv) ?? new Set()
|
||
flights.set(conv, set.add(p))
|
||
p.finally(() => {
|
||
set.delete(p)
|
||
if (!set.size && flights.get(conv) === set) flights.delete(conv)
|
||
})
|
||
return p
|
||
}
|
||
|
||
// ledger is how a Run of conversation conv, started at since, has its
|
||
// usage counted, for decode; kept when the conversation_id names the
|
||
// session, so that a later Run of it can collect what this one owes. A
|
||
// debt the conversation has is collected in the background. own() is a
|
||
// step's count once it has called tools: its Run owed, what has shown of
|
||
// the debt by the end of the wait (or at once, when the account's never
|
||
// show in time); false when nothing has, null when the step is to be
|
||
// guessed (not kept, and its event not found, or an account that can't
|
||
// read its events). owed(final) is what the Run collected otherwise,
|
||
// all that can be had when final. done() ends the Run: what was collected
|
||
// and not given out goes back.
|
||
function ledger(tok, conv, since, signal, kept) {
|
||
const key = tokKey(tok)
|
||
let pending = null // collected, not given out yet
|
||
let over = false
|
||
const gather = (opts) =>
|
||
collect(tok, conv, { ...opts, over: () => over }).then((r) => {
|
||
pending = addUsage(pending, r.usage)
|
||
return r
|
||
})
|
||
let collecting = null
|
||
const debt = kept ? owed.get(conv) : undefined
|
||
const timer = debt ? setTimeout(() => (collecting = gather()), Math.max(0, debt.ready - Date.now())) : null
|
||
timer?.unref?.()
|
||
const take = () => {
|
||
const u = pending
|
||
pending = null
|
||
return u
|
||
}
|
||
return {
|
||
async own() {
|
||
if (!canRead(tok)) return null
|
||
const m = misses.get(key) ?? 0
|
||
const waits = m < STEP_WAIT.skipAfter || m % STEP_WAIT.retryEvery === 0
|
||
if (!kept) {
|
||
const r = waits ? await usageEvents(tok, conv, since, 1, { poll: true, signal }).catch(() => ({})) : {}
|
||
if (r.usage) misses.delete(key)
|
||
else if (!r.refused) misses.set(key, m + 1)
|
||
return r.usage ?? null
|
||
}
|
||
owe(conv, { since, n: 1, ready: Date.now() + STEP_WAIT.owedAt })
|
||
if (waits) {
|
||
const r = await gather({ poll: true, signal })
|
||
if (!canRead(tok)) return null
|
||
if (r.short) misses.set(key, m + 1)
|
||
else misses.delete(key)
|
||
} else misses.set(key, m + 1)
|
||
return take() ?? false
|
||
},
|
||
async owed(final) {
|
||
if (!kept) return null
|
||
if (final) {
|
||
await collecting
|
||
await Promise.all([...(flights.get(conv) ?? [])])
|
||
if (owed.has(conv)) await gather({ poll: owed.get(conv).n > 0, first: 0, signal })
|
||
}
|
||
return take()
|
||
},
|
||
done() {
|
||
over = true
|
||
clearTimeout(timer)
|
||
if (pending) owe(conv, { carry: take() })
|
||
},
|
||
}
|
||
}
|
||
|
||
// buildRun is the Run's first message, an AgentClientMessage with its
|
||
// run_request, and the blobs it names. The conversation state is the
|
||
// messages and one turn, which the server wants there to sample at all.
|
||
// conv is the conversation_id (conversationID), a new one when "". The
|
||
// model goes as the CLI sends it: in Max Mode when maxMode is set (Cursor
|
||
// refuses a model it serves only that way otherwise: "Max Mode
|
||
// Required"), with the picker variant's parameters when it has them.
|
||
function buildRun(msgs, lastUser, tools, model, conv = "", { maxMode = false, params = [] } = {}) {
|
||
const blobs = new Map()
|
||
const put = (b) => {
|
||
const id = createHash("sha256").update(b).digest()
|
||
blobs.set(id.toString("hex"), b)
|
||
return id
|
||
}
|
||
const state = pb()
|
||
for (const m of msgs) state.bytes(1, put(m))
|
||
const mid = randomUUID()
|
||
const user = pb().str(1, lastUser).str(2, mid).varint(4, 1).done() // mode: agent
|
||
const turn = pb().bytes(1, pb().bytes(1, put(user)).str(10, mid)).done()
|
||
state.bytes(8, put(turn)).varint(10, 1).str(22, "cli")
|
||
const defs = tools.map(toolDef)
|
||
const rc = pb().bytes(4, env())
|
||
const mcp = pb()
|
||
for (const d of defs) {
|
||
rc.bytes(7, d)
|
||
mcp.bytes(1, d)
|
||
}
|
||
const action = pb().bytes(2, pb().bytes(2, rc)) // resume_action
|
||
// ModelDetails: model_id, display_model_id, display_name, max_mode (7);
|
||
// RequestedModel: model_id, max_mode (2), parameters (3)
|
||
const details = pb().str(1, model).str(3, model).str(4, model)
|
||
const requested = pb().str(1, model)
|
||
if (maxMode) {
|
||
details.varint(7, 1)
|
||
requested.varint(2, 1)
|
||
}
|
||
for (const p of params) requested.bytes(3, pb().str(1, p.id).str(2, p.value))
|
||
const rr = pb()
|
||
.bytes(1, state)
|
||
.bytes(2, action)
|
||
.bytes(3, details)
|
||
.bytes(4, mcp)
|
||
.str(5, conv || randomUUID())
|
||
.bytes(9, requested)
|
||
.varint(19, 1) // inline images
|
||
return { first: pb().bytes(1, rr).done(), blobs }
|
||
}
|
||
|
||
// ---- the agent API ---------------------------------------------------------------------
|
||
|
||
// endpoints are the agent API picked for each token (by its hash). As
|
||
// cursor-agent does, it is what ServerConfigService/GetServerConfig names —
|
||
// a team may be served in one region only, and the global API turns it
|
||
// away — asked once a token, and again when fresh is set; the global one
|
||
// while it can't be had.
|
||
const endpoints = new Map()
|
||
async function agentURL(tok, fresh) {
|
||
const key = createHash("sha256").update(tok).digest("hex")
|
||
const had = endpoints.get(key)
|
||
if (!fresh && had && (had.listed || Date.now() - had.at < 60_000)) return had.url
|
||
let url = AGENT
|
||
let listed = false
|
||
try {
|
||
const cfg = await unary(API, "aiserver.v1.ServerConfigService/GetServerConfig", tok, {}, 10_000)
|
||
for (const raw of [cfg?.agentUrlConfig?.agentUrl, cfg?.agentUrlConfig?.agentnUrl]) {
|
||
try {
|
||
const u = new URL(raw)
|
||
if ((u.protocol === "https:" || u.protocol === "http:") && u.host) {
|
||
url = raw.replace(/\/+$/, "")
|
||
listed = true
|
||
break
|
||
}
|
||
} catch {}
|
||
}
|
||
} catch {}
|
||
endpoints.set(key, { url, at: Date.now(), listed })
|
||
return url
|
||
}
|
||
|
||
// open starts a Run at base: the session, the stream, and the response's
|
||
// status, or an error the request never got past.
|
||
function open(base, headers, signal) {
|
||
return new Promise((resolve) => {
|
||
const u = new URL(base)
|
||
const session = http2.connect(u.origin)
|
||
let done = false
|
||
const fail = (e) => {
|
||
if (done) return
|
||
done = true
|
||
session.destroy()
|
||
resolve({ error: { status: 502, message: e?.message ?? String(e) } })
|
||
}
|
||
session.on("error", fail)
|
||
const req = session.request({
|
||
":method": "POST",
|
||
":path": u.pathname.replace(/\/+$/, "") + "/agent.v1.AgentService/Run",
|
||
...headers,
|
||
})
|
||
req.on("error", fail)
|
||
req.once("response", (h) => {
|
||
if (done) return
|
||
done = true
|
||
resolve({ session, req, status: Number(h[":status"]) })
|
||
})
|
||
signal?.addEventListener("abort", () => fail(new Error("the request was cancelled")), { once: true })
|
||
})
|
||
}
|
||
|
||
// runOnce is one Run of the chat on the agent API at base: an async
|
||
// iterator of the answer's pieces, or the error it failed with before any.
|
||
async function runOnce({ tok, base, id, tools, conv, convID, signal, maxMode, params }) {
|
||
// the Run's conversation_id, a new one when nothing names the session:
|
||
// its usage event is found by it
|
||
const cid = convID || randomUUID()
|
||
const since = Date.now()
|
||
// how its usage is counted: a debt of the conversation's is collected
|
||
// while it goes, which only a conversation_id that names the session
|
||
// could have run up
|
||
const book = ledger(tok, cid, since, signal, !!convID)
|
||
const { first, blobs } = buildRun(conv.msgs, conv.last, tools, id, cid, { maxMode, params })
|
||
const o = await open(
|
||
base,
|
||
{
|
||
"content-type": "application/connect+proto",
|
||
"connect-protocol-version": "1",
|
||
authorization: `Bearer ${tok}`,
|
||
"x-cursor-client-version": await clientVersion(),
|
||
"x-cursor-client-type": "cli", // else Cursor adds a prompt of its own
|
||
"x-ghost-mode": "true", // privacy mode: nothing kept for training
|
||
"x-request-id": randomUUID(),
|
||
// none of Cursor's own tools, only the caller's
|
||
"x-cursor-agent-allowed-tools": "mcp_tool_call,get_mcp_tools_tool_call",
|
||
},
|
||
signal,
|
||
)
|
||
if (o.error) return book.done(), o
|
||
const { session, req } = o
|
||
let closed = false
|
||
const send = (msg) => {
|
||
if (!closed && !req.destroyed && req.writable) req.write(frame(msg))
|
||
}
|
||
const heartbeat = setInterval(() => send(pb().bytes(7, EMPTY).done()), 5000) // the client is still there, as the CLI says
|
||
const close = () => {
|
||
if (closed) return
|
||
closed = true
|
||
clearInterval(heartbeat)
|
||
try {
|
||
req.close(http2.constants.NGHTTP2_CANCEL)
|
||
} catch {}
|
||
session.close()
|
||
setTimeout(() => session.destroy(), 1000).unref?.()
|
||
}
|
||
signal?.addEventListener("abort", close, { once: true })
|
||
send(first)
|
||
if (o.status < 200 || o.status > 299) {
|
||
const chunks = []
|
||
try {
|
||
for await (const c of req) chunks.push(c)
|
||
} catch {}
|
||
close()
|
||
book.done()
|
||
return { error: failure(o.status, Buffer.concat(chunks).toString("utf8").slice(0, 1 << 20)) }
|
||
}
|
||
const it = decode(frames(req), {
|
||
send,
|
||
blobs,
|
||
tools,
|
||
estimate: conv.estimate,
|
||
close,
|
||
stepUsage: book.own,
|
||
owedUsage: book.owed,
|
||
})
|
||
const wrapped = (async function* () {
|
||
try {
|
||
yield* it
|
||
} finally {
|
||
close()
|
||
book.done()
|
||
}
|
||
})()
|
||
// an error comes before anything of the answer: out of quota, a model the
|
||
// plan doesn't have — answered with its own status
|
||
const head = await wrapped.next()
|
||
if (head.done) return book.done(), { error: { status: 502, message: "an empty reply" } }
|
||
if (head.value.error) {
|
||
await wrapped.return()
|
||
return { error: head.value.error }
|
||
}
|
||
return {
|
||
events: (async function* () {
|
||
yield head.value
|
||
yield* wrapped
|
||
})(),
|
||
}
|
||
}
|
||
|
||
// decode follows the server's messages into the answer's pieces until the
|
||
// turn ends, or the model has made its tool calls.
|
||
async function* decode(stream, { send, blobs, tools, estimate, close, stepUsage, owedUsage }) {
|
||
let calls = 0
|
||
// ended: Cursor said what the turn used
|
||
let ended = false
|
||
let listed = 0
|
||
let said = 0
|
||
// wrote: the reply's text alone; a turn of thinking only says nothing
|
||
let wrote = 0
|
||
let usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reasoning: 0 }
|
||
// counted: the step's usage is the dashboard's events, its own counted
|
||
// with it or by a later step (ledger), never guessed
|
||
let counted = false
|
||
// finish is the step's end, with what earlier steps owed (more), which
|
||
// is no reason not to guess the step's own
|
||
const finish = (more) => {
|
||
if (!counted && !usage.output) usage.output = Math.floor((said + 3) / 4)
|
||
// a guess only when Cursor counted nothing: a prompt read whole from
|
||
// the cache leaves no uncached rest, and is no reason to guess; nor is
|
||
// a prompt counted from the events
|
||
if (!counted && !usage.input && !usage.cacheRead && !usage.cacheWrite) usage.input = estimate
|
||
// the events' output is their steps', counted here as their prompt is
|
||
if (more) for (const k of ["input", "output", "cacheRead", "cacheWrite"]) usage[k] += more[k]
|
||
const prompt = usage.input + usage.cacheRead + usage.cacheWrite
|
||
return {
|
||
stop: calls > 0 ? "tool_calls" : "stop",
|
||
usage: {
|
||
prompt_tokens: prompt,
|
||
completion_tokens: usage.output,
|
||
total_tokens: prompt + usage.output,
|
||
prompt_tokens_details: { cached_tokens: usage.cacheRead, cache_write_tokens: usage.cacheWrite },
|
||
...(usage.reasoning ? { completion_tokens_details: { reasoning_tokens: usage.reasoning } } : {}),
|
||
},
|
||
}
|
||
}
|
||
// settled is finish once a step that called tools has its usage: the
|
||
// dashboard's count, the Run closed first, when the turn didn't end (or
|
||
// owed); and with what the conversation's earlier steps owed, which a
|
||
// step that ends the turn waits for
|
||
const settled = async () => {
|
||
if (calls > 0 && !ended && stepUsage) {
|
||
close?.()
|
||
const u = await stepUsage().catch(() => null)
|
||
if (u) usage = u
|
||
counted = u === false || !!u
|
||
}
|
||
return finish(owedUsage ? await owedUsage(calls === 0).catch(() => null) : null)
|
||
}
|
||
const closeExec = (id) => send(pb().bytes(5, pb().bytes(1, pb().varint(1, id))).done())
|
||
try {
|
||
for await (const f of stream) {
|
||
if (f.end) {
|
||
const e = failure(200, f.data)
|
||
if (e.status < 200 || e.status > 299) yield { error: e }
|
||
else if (wrote === 0 && calls === 0) yield { error: { status: 502, message: "an empty reply" } }
|
||
else yield await settled()
|
||
return
|
||
}
|
||
for (const m of fields(f.data)) {
|
||
switch (m.num) {
|
||
case 1: // interaction_update
|
||
for (const u of fields(m.data)) {
|
||
const uf = fields(u.data)
|
||
switch (u.num) {
|
||
case 1: {
|
||
// text
|
||
const t = pbStr(uf, 1)
|
||
if (t) {
|
||
said += t.length
|
||
wrote += t.length
|
||
yield { text: t }
|
||
}
|
||
break
|
||
}
|
||
case 4: {
|
||
// thinking
|
||
const t = pbStr(uf, 1)
|
||
if (t) {
|
||
said += t.length
|
||
yield { reasoning: t }
|
||
}
|
||
break
|
||
}
|
||
case 14: // turn ended, with what it used
|
||
usage = usageOf(uf)
|
||
ended = true
|
||
if (calls === 0) {
|
||
// nothing written and nothing called: an empty reply,
|
||
// which the client asks again, not a finished turn
|
||
yield wrote === 0 ? { error: { status: 502, message: "an empty reply" } } : await settled()
|
||
return
|
||
}
|
||
break
|
||
case 27: // how many tool calls the step makes, sent before them
|
||
listed = pbNum(uf, 1)
|
||
if (listed > 0 && calls >= listed) {
|
||
yield await settled()
|
||
return
|
||
}
|
||
break
|
||
}
|
||
}
|
||
break
|
||
case 2: {
|
||
// exec_server_message: the server asks the client to run something
|
||
const call = exec(m.data, tools, send, closeExec)
|
||
if (call) {
|
||
said += call.args.length
|
||
yield { tool: { index: calls++, ...call } }
|
||
}
|
||
if (listed > 0 && calls >= listed) {
|
||
// every call made: the caller runs them
|
||
yield await settled()
|
||
return
|
||
}
|
||
break
|
||
}
|
||
case 4: {
|
||
// kv_server_message: a blob wanted, or one to keep
|
||
const kv = fields(m.data)
|
||
const id = pbNum(kv, 1)
|
||
for (const k of kv) {
|
||
if (k.num === 2) {
|
||
const want = field(fields(k.data), 1)?.data.toString("hex") ?? ""
|
||
const b = blobs.get(want)
|
||
const res = b ? pb().bytes(1, b) : pb().bytes(2, pb().str(1, "blob not found"))
|
||
send(pb().bytes(3, pb().varint(1, id).bytes(2, res)).done())
|
||
} else if (k.num === 3) {
|
||
// the server's own record of the turn, not needed
|
||
send(pb().bytes(3, pb().varint(1, id).bytes(3, EMPTY)).done())
|
||
}
|
||
}
|
||
break
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if (calls > 0) {
|
||
yield await settled()
|
||
return
|
||
}
|
||
yield { error: { status: 502, message: "the reply broke off: EOF" } }
|
||
} catch (e) {
|
||
if (calls > 0 && /EOF/.test(e?.message ?? "")) {
|
||
yield await settled()
|
||
return
|
||
}
|
||
yield { error: { status: 502, message: "the reply broke off: " + (e?.message ?? e) } }
|
||
}
|
||
}
|
||
|
||
// exec answers what the server asks the client to run: a call of the
|
||
// caller's tools is returned, the list of them is given, and anything else
|
||
// is refused.
|
||
function exec(msg, tools, send, closeExec) {
|
||
const es = fields(msg)
|
||
const id = pbNum(es, 1)
|
||
const execID = pbStr(es, 15)
|
||
const answer = (num, res) => {
|
||
send(pb().bytes(2, pb().varint(1, id).str(15, execID).bytes(num, res)).done())
|
||
closeExec(id)
|
||
}
|
||
for (const e of es) {
|
||
switch (e.num) {
|
||
case 11: {
|
||
// mcp_args: a call of the caller's tools
|
||
const a = fields(e.data)
|
||
const args = {}
|
||
for (const kv of a) {
|
||
if (kv.num !== 2) continue
|
||
const ef = fields(kv.data)
|
||
const v = field(ef, 2)
|
||
args[pbStr(ef, 1)] = v ? pbAny(v.data) : null
|
||
}
|
||
const name = pbStr(a, 5) || pbStr(a, 1).replace(/^magpie-/, "")
|
||
// an OpenAI model's id is its call's and its item's, a line apart,
|
||
// which no caller would take as an id: the line goes as __
|
||
const cid = pbStr(a, 3).replaceAll("\n", "__") || "call_" + randomBytes(12).toString("hex")
|
||
return { id: cid, name, args: JSON.stringify(args) }
|
||
}
|
||
case 36: {
|
||
// mcp_state_exec_args: the tools there are
|
||
const srv = pb().str(1, "magpie").str(2, "magpie").str(7, "connected")
|
||
for (const t of tools) srv.bytes(5, toolDef(t))
|
||
answer(36, pb().bytes(1, pb().bytes(1, srv)))
|
||
return null
|
||
}
|
||
case 10: // request_context_args
|
||
answer(10, pb().bytes(1, pb().bytes(1, pb().bytes(4, env()))))
|
||
return null
|
||
}
|
||
}
|
||
// a shell, a file read or an edit of Cursor's own: never run
|
||
send(pb().bytes(5, pb().bytes(2, pb().varint(1, id).str(2, "not available"))).done())
|
||
closeExec(id)
|
||
return null
|
||
}
|
||
|
||
// ---- chat completions ------------------------------------------------------------------
|
||
|
||
// Every answer says X-Magpie-Sign-In: kept. The built-in marked no Cursor
|
||
// account lapsed, not for a 401 nor for any "expired" (a token, a session,
|
||
// a trial), and cleared none on a success or a renewed token.
|
||
const errorResponse = ({ status, message }) =>
|
||
new Response(JSON.stringify({ error: { message, type: "cursor_error", code: status } }), {
|
||
status,
|
||
headers: { "Content-Type": "application/json", "X-Magpie-Sign-In": "kept" },
|
||
})
|
||
|
||
function kept(res) {
|
||
if (res.headers.has("X-Magpie-Sign-In")) return res
|
||
const headers = new Headers(res.headers)
|
||
headers.set("X-Magpie-Sign-In", "kept")
|
||
return new Response(res.body, { status: res.status, statusText: res.statusText, headers })
|
||
}
|
||
|
||
// answer runs a chat completion on Cursor, the account kept (errorResponse).
|
||
const answer = async (auth, chat, signal, session) => kept(await answerOf(auth, chat, signal, session))
|
||
|
||
// maxOnly are the ids Cursor said it serves in Max Mode only.
|
||
const maxOnly = new Set()
|
||
|
||
// maxRequired is Cursor's refusal of a model asked for without Max Mode
|
||
// (the CLI's MAX_MODE_REQUIRED): "Max Mode Required: The model "x"
|
||
// requires Max Mode to be enabled. …"
|
||
const maxRequired = (msg) => /max[ _]mode[ _]required|requires max mode/i.test(String(msg ?? ""))
|
||
|
||
// answerOf runs a chat completion on Cursor and answers it as one, streamed
|
||
// or not.
|
||
async function answerOf(auth, chat, signal, session = "") {
|
||
let tok
|
||
try {
|
||
tok = await tokenOf(auth)
|
||
} catch (e) {
|
||
return errorResponse({ status: e.status ?? 401, message: e.message })
|
||
}
|
||
const model = String(chat.model ?? "")
|
||
let raw = []
|
||
try {
|
||
raw = await usable(tok)
|
||
} catch {}
|
||
// 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)
|
||
let base = await agentURL(tok, false)
|
||
const once = () => runOnce({ tok, base, id, tools, conv, convID, signal, maxMode, params })
|
||
let r = await once()
|
||
if (r.error && regional(r.error.message)) {
|
||
// the team moved, or the config was kept from before: once more with
|
||
// what the config says now
|
||
const fresh = await agentURL(tok, true)
|
||
if (fresh !== base) {
|
||
base = fresh
|
||
r = await once()
|
||
}
|
||
}
|
||
if (r.error && !maxMode && maxRequired(r.error.message)) {
|
||
// a model Cursor serves only in Max Mode that neither list said so of:
|
||
// in Max Mode, as the CLI turns it on for such a model, and so from
|
||
// now on. An account Max Mode isn't open to is answered as Cursor
|
||
// answers that.
|
||
maxMode = true
|
||
maxOnly.add(id)
|
||
r = await once()
|
||
}
|
||
if (r.error) return errorResponse(r.error)
|
||
const it = r.events
|
||
const cid = "chatcmpl-" + randomBytes(12).toString("hex")
|
||
const created = Math.floor(Date.now() / 1000)
|
||
|
||
if (!chat.stream) {
|
||
const msg = { role: "assistant", content: "" }
|
||
let reasoning = ""
|
||
const calls = []
|
||
let stop = "stop"
|
||
let usage
|
||
for await (const e of it) {
|
||
if (e.error) return errorResponse(e.error)
|
||
if (e.text) msg.content += e.text
|
||
if (e.reasoning) reasoning += e.reasoning
|
||
if (e.tool) calls.push({ id: e.tool.id, type: "function", function: { name: e.tool.name, arguments: e.tool.args } })
|
||
if (e.stop) [stop, usage] = [e.stop, e.usage]
|
||
}
|
||
if (reasoning) msg.reasoning_content = reasoning
|
||
if (calls.length) msg.tool_calls = calls
|
||
return Response.json({ id: cid, object: "chat.completion", created, model, choices: [{ index: 0, message: msg, finish_reason: stop }], ...(usage ? { usage } : {}) })
|
||
}
|
||
|
||
const chunk = (delta, finish_reason = null, extra = {}) =>
|
||
enc.encode(`data: ${JSON.stringify({ id: cid, object: "chat.completion.chunk", created, model, choices: [{ index: 0, delta, finish_reason }], ...extra })}\n\n`)
|
||
let began = false
|
||
const stream = new ReadableStream({
|
||
async pull(ctl) {
|
||
const r = await it.next()
|
||
if (!began) {
|
||
began = true
|
||
ctl.enqueue(chunk({ role: "assistant", content: "" }))
|
||
}
|
||
if (r.done) {
|
||
ctl.enqueue(enc.encode("data: [DONE]\n\n"))
|
||
return ctl.close()
|
||
}
|
||
const e = r.value
|
||
if (e.text) ctl.enqueue(chunk({ content: e.text }))
|
||
else if (e.reasoning) ctl.enqueue(chunk({ reasoning_content: e.reasoning }))
|
||
else if (e.tool) ctl.enqueue(chunk({ tool_calls: [{ index: e.tool.index, id: e.tool.id, type: "function", function: { name: e.tool.name, arguments: e.tool.args } }] }))
|
||
else if (e.stop) ctl.enqueue(chunk({}, e.stop, e.usage ? { usage: e.usage } : {}))
|
||
else if (e.error) {
|
||
ctl.enqueue(enc.encode(`data: ${JSON.stringify({ error: { message: e.error.message, code: e.error.status } })}\n\n`))
|
||
ctl.close()
|
||
}
|
||
},
|
||
cancel() {
|
||
it.return?.()
|
||
},
|
||
})
|
||
return new Response(stream, { status: 200, headers: { "Content-Type": "text/event-stream", "Cache-Control": "no-cache" } })
|
||
}
|
||
|
||
// ---- the plugin ---------------------------------------------------------------------------
|
||
|
||
export async function CursorAuthPlugin() {
|
||
return {
|
||
auth: {
|
||
provider: ID,
|
||
async loader(getAuth) {
|
||
const auth = await getAuth()
|
||
if (!auth || (auth.type !== "oauth" && auth.type !== "api")) return {}
|
||
return {
|
||
baseURL: AGENT,
|
||
apiKey: "cursor",
|
||
// every chat completion run on Cursor's agent API, with the
|
||
// sign-in as it is now
|
||
async fetch(input, init = {}) {
|
||
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url
|
||
if (!/\/chat\/completions$/.test(new URL(url).pathname))
|
||
return errorResponse({ status: 404, message: "only chat completions are served" })
|
||
let chat
|
||
try {
|
||
const b = init.body ?? (input instanceof Request ? await input.clone().text() : undefined)
|
||
chat = JSON.parse(typeof b === "string" ? b : new TextDecoder().decode(b))
|
||
} catch {
|
||
return errorResponse({ status: 400, message: "a request that isn't JSON" })
|
||
}
|
||
const session = new Headers(init.headers ?? (input instanceof Request ? input.headers : undefined)).get(SESSION) ?? ""
|
||
return answer((await getAuth()) ?? auth, chat, init.signal ?? (input instanceof Request ? input.signal : undefined), session)
|
||
},
|
||
}
|
||
},
|
||
methods: [
|
||
{ type: "oauth", label: "Cursor (browser)", authorize: browserSignIn },
|
||
{ type: "oauth", label: "cursor-agent's sign-in", authorize: cliSignIn },
|
||
{ type: "api", label: "Cursor API key (cursor.com/dashboard → Integrations)" },
|
||
],
|
||
// magpie's: how much of the plan's included usage is gone (the plan
|
||
// is the one the sign-in read). The built-in's read, clean or not,
|
||
// neither marked the account nor cleared it.
|
||
async usage(getAuth, provider) {
|
||
let tok
|
||
try {
|
||
tok = await tokenOf(await getAuth())
|
||
} catch (e) {
|
||
return { error: e.message, windows: [], signIn: "kept" }
|
||
}
|
||
const [u, plan] = await Promise.all([usage(tok, Object.keys(provider?.models ?? {})), planOf(tok)])
|
||
return { ...u, ...(plan ? { plan } : {}), signIn: "kept" }
|
||
},
|
||
},
|
||
// the session the request is part of, for the conversation_id
|
||
// (conversationID): magpie and OpenCode name it here, and the loader's
|
||
// fetch reads it back. Every plugin's hook sees every provider's
|
||
// requests, so it is put on Cursor's alone.
|
||
async "chat.headers"(input, output) {
|
||
if (input?.model?.providerID !== ID && input?.provider?.info?.id !== ID) return
|
||
const s = sessionOf(input)
|
||
if (s) output.headers[SESSION] = s
|
||
},
|
||
async config(config) {
|
||
config.provider ??= {}
|
||
const was = config.provider[ID] ?? {}
|
||
config.provider[ID] = {
|
||
name: "Cursor",
|
||
npm: CHAT,
|
||
api: AGENT,
|
||
...was,
|
||
models: {
|
||
auto: { name: "Auto", limit: { context: DEFAULT_CONTEXT, output: 0 }, attachment: true, tool_call: true },
|
||
...(was.models ?? {}),
|
||
},
|
||
}
|
||
},
|
||
// the account's list: each picker model once for each context size,
|
||
// with that window
|
||
provider: {
|
||
id: ID,
|
||
async models(provider, { auth } = {}) {
|
||
if (!auth) return provider.models
|
||
// 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((await usable(tok)).map((m) => [m.id, runtimeModel(m)]))
|
||
},
|
||
},
|
||
}
|
||
}
|
||
|
||
// for tests
|
||
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 }
|