@caph42/pi-cursor-provider 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +23 -0
- package/README.md +133 -2
- package/package.json +43 -4
- package/src/auth.ts +272 -0
- package/src/client.ts +90 -0
- package/src/codec.ts +235 -0
- package/src/index.ts +93 -0
- package/src/ledger.ts +292 -0
- package/src/models.ts +336 -0
- package/src/native-session.ts +958 -0
- package/src/pi-stream.ts +330 -0
- package/src/run.ts +1268 -0
- package/src/usage.ts +257 -0
package/src/models.ts
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cursor model picker: AvailableModels filtered by GetUsableModels.
|
|
3
|
+
*
|
|
4
|
+
* Ported from magpie's Cursor provider (MIT):
|
|
5
|
+
* https://github.com/magpie-community/plugins/tree/main/packages/cursor
|
|
6
|
+
* Billing-period windows live in usage.ts; per-step deferred usage in ledger.ts.
|
|
7
|
+
*/
|
|
8
|
+
// @ts-nocheck
|
|
9
|
+
import { createHash } from "node:crypto"
|
|
10
|
+
import { API, DEFAULT_CONTEXT, MODELS_KEEP, unary } from "./client.ts"
|
|
11
|
+
|
|
12
|
+
const LEVEL_RANK = ["none", "minimal", "low", "medium", "high", "xhigh", "max"]
|
|
13
|
+
const EFFORT_PARAMS = ["effort", "reasoning_effort", "reasoning"]
|
|
14
|
+
|
|
15
|
+
// the effort words of Cursor's own ids and the levels they are; extra-high
|
|
16
|
+
// before high
|
|
17
|
+
const ID_EFFORTS = [["extra-high", "xhigh"], ["xhigh", "xhigh"], ["minimal", "minimal"], ["none", "none"], ["low", "low"], ["medium", "medium"], ["high", "high"], ["max", "max"]]
|
|
18
|
+
|
|
19
|
+
const cut = (s, suffix) => (s.endsWith(suffix) && s.length > suffix.length ? s.slice(0, -suffix.length) : null)
|
|
20
|
+
|
|
21
|
+
// splitID is one of Cursor's own ids (a variant's legacySlug, as
|
|
22
|
+
// cursor-agent models lists it) taken apart: its family, and the effort
|
|
23
|
+
// ("" for none named) it is at. Thinking comes before the effort or after
|
|
24
|
+
// it; in the family it goes after, as does fast. The family is the id 0.1.x
|
|
25
|
+
// listed a model by.
|
|
26
|
+
function splitID(id) {
|
|
27
|
+
let s = id
|
|
28
|
+
let fast = false
|
|
29
|
+
let thinking = false
|
|
30
|
+
let effort = ""
|
|
31
|
+
let b
|
|
32
|
+
if ((b = cut(s, "-fast")) !== null) [s, fast] = [b, true]
|
|
33
|
+
if ((b = cut(s, "-thinking")) !== null) [s, thinking] = [b, true]
|
|
34
|
+
for (const [word, level] of ID_EFFORTS) {
|
|
35
|
+
if ((b = cut(s, "-" + word)) !== null) {
|
|
36
|
+
;[s, effort] = [b, level]
|
|
37
|
+
break
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (!thinking && (b = cut(s, "-thinking")) !== null) [s, thinking] = [b, true]
|
|
41
|
+
if (thinking) s += "-thinking"
|
|
42
|
+
if (fast) s += "-fast"
|
|
43
|
+
return [s, effort]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// left out of the CLI's picker as it leaves them out (model-service.ts)
|
|
47
|
+
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"])
|
|
48
|
+
|
|
49
|
+
const clean = (s) => String(s ?? "").replaceAll("\u200b", "").split(/\s+/).filter(Boolean).join(" ")
|
|
50
|
+
|
|
51
|
+
// tokensOf is a context value as tokens: "300k" 300000, "1m" 1000000.
|
|
52
|
+
function tokensOf(value) {
|
|
53
|
+
const m = /^(\d+(?:\.\d+)?)([km])$/i.exec(String(value ?? "").trim())
|
|
54
|
+
return m ? Math.round(Number(m[1]) * (m[2].toLowerCase() === "m" ? 1_000_000 : 1_000)) : 0
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// paramDefs is a picker model's parameters: id to the values it takes.
|
|
58
|
+
function paramDefs(m) {
|
|
59
|
+
const out = {}
|
|
60
|
+
for (const d of m.parameterDefinitions ?? []) {
|
|
61
|
+
const t = d?.parameterType ?? {}
|
|
62
|
+
const vs = (t.enumParameter ?? t.booleanParameter)?.values ?? []
|
|
63
|
+
if (d?.id) out[d.id] = vs.map((v) => String(v.value))
|
|
64
|
+
}
|
|
65
|
+
return out
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const valuesOf = (v) => Object.fromEntries((v?.parameterValues ?? []).map((p) => [String(p.id), String(p.value)]))
|
|
69
|
+
|
|
70
|
+
// defaultVariant is the variant the CLI picks for a model asked for by
|
|
71
|
+
// name: its default without Max Mode, else with it, else the first.
|
|
72
|
+
const defaultVariant = (m) => {
|
|
73
|
+
const vs = m?.variants ?? []
|
|
74
|
+
return vs.find((v) => v.isDefaultNonMaxConfig) ?? vs.find((v) => v.isDefaultMaxConfig) ?? vs[0]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// needsMax is whether a variant is run in Max Mode only, as the CLI reads
|
|
78
|
+
// it: a model with no other mode, or a Max Mode variant.
|
|
79
|
+
const needsMax = (m, v) => m?.supportsNonMaxMode === false || v?.isMaxMode === true
|
|
80
|
+
|
|
81
|
+
// effortsOf is the efforts a model takes, as pi's levels, each with the
|
|
82
|
+
// value Cursor's parameter has for it (extra-high is xhigh).
|
|
83
|
+
function effortsOf(defs) {
|
|
84
|
+
const pid = EFFORT_PARAMS.find((p) => defs[p]?.length)
|
|
85
|
+
// thinking alone, on or off: high and none
|
|
86
|
+
if (!pid && defs.thinking?.includes("true") && defs.thinking.includes("false")) return { pid: "thinking", levels: { none: "false", high: "true" } }
|
|
87
|
+
if (!pid) return { pid: "", levels: {} }
|
|
88
|
+
const levels = {}
|
|
89
|
+
for (const v of defs[pid]) {
|
|
90
|
+
const l = v === "extra-high" ? "xhigh" : v
|
|
91
|
+
if (LEVEL_RANK.includes(l)) levels[l] = v
|
|
92
|
+
}
|
|
93
|
+
return { pid, levels }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// served is a picker model's variants the account is served. With the
|
|
97
|
+
// usable list at hand (the ids cursor-agent models shows), a variant whose
|
|
98
|
+
// own id isn't in it is one the account hasn't got (Sonnet 4.6 at any effort
|
|
99
|
+
// but medium, say). A model the usable list has nothing of is the picker's
|
|
100
|
+
// alone (GLM-5.3), and is kept whole.
|
|
101
|
+
function served(m, ids) {
|
|
102
|
+
const vs = m.variants ?? []
|
|
103
|
+
if (!ids) return vs
|
|
104
|
+
const got = vs.filter((v) => !v.legacySlug || ids.has(v.legacySlug))
|
|
105
|
+
return got.some((v) => v.legacySlug) ? got : vs
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// modelsOf is the picker's models as offered: one entry for each context
|
|
109
|
+
// size a model has, or one with Cursor's limit when it has none. A model
|
|
110
|
+
// served in part is offered at the sizes and efforts it is served at.
|
|
111
|
+
function modelsOf(picker, ids) {
|
|
112
|
+
const out = []
|
|
113
|
+
const used = new Set()
|
|
114
|
+
for (const m of picker) {
|
|
115
|
+
if (!m?.name || m.isHidden || m.isChatOnly || m.onlySupportsCmdK || m.supportsAgent === false || PICKER_SKIP.has(m.name)) continue
|
|
116
|
+
const defs = paramDefs(m)
|
|
117
|
+
const variants = served(m, ids)
|
|
118
|
+
const partial = variants.length < (m.variants ?? []).length
|
|
119
|
+
const has = (pid, value) => !partial || !variants.some((v) => pid in valuesOf(v)) || variants.some((v) => valuesOf(v)[pid] === value)
|
|
120
|
+
const def = defaultVariant({ variants })
|
|
121
|
+
const { pid, levels: all } = effortsOf(defs)
|
|
122
|
+
const levels = Object.fromEntries(Object.entries(all).filter(([, value]) => has(pid, value)))
|
|
123
|
+
const base = { run: m.name, title: clean(m.clientDisplayName || m.name), pid, levels, fast: !!defs.fast?.length, variants, def, partial, model: m }
|
|
124
|
+
const contexts = (defs.context ?? []).filter((c) => has("context", c))
|
|
125
|
+
if (contexts.length) {
|
|
126
|
+
for (const c of contexts) {
|
|
127
|
+
const id = m.name + "@" + c
|
|
128
|
+
if (used.has(id)) continue
|
|
129
|
+
used.add(id)
|
|
130
|
+
out.push({ ...base, id, name: base.title + " @ " + c, context: tokensOf(c) || DEFAULT_CONTEXT, size: c, offersFast: fastAt(variants, c) })
|
|
131
|
+
}
|
|
132
|
+
} else if (!used.has(m.name)) {
|
|
133
|
+
used.add(m.name)
|
|
134
|
+
const limit = needsMax(m, def) ? m.contextTokenLimitForMaxMode || m.contextTokenLimit : m.contextTokenLimit
|
|
135
|
+
out.push({ ...base, id: m.name === "default" ? "auto" : m.name, name: base.title, context: limit > 0 ? limit : DEFAULT_CONTEXT, size: "", offersFast: fastAt(variants, "") })
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return out
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// fastAt is whether Cursor has a fast variant the account is served at
|
|
142
|
+
// the context size (any, for a model without sizes): what magpie offers
|
|
143
|
+
// fast for, which 0.1.x said with a -fast model of its own.
|
|
144
|
+
const fastAt = (variants, size) =>
|
|
145
|
+
variants.some((v) => {
|
|
146
|
+
const have = valuesOf(v)
|
|
147
|
+
return have.fast === "true" && (!size || have.context === undefined || have.context === size)
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
// nearest is the variant of vs most like the parameters wanted, one at
|
|
151
|
+
// their context size before any other.
|
|
152
|
+
function nearest(vs, want) {
|
|
153
|
+
let best
|
|
154
|
+
let most = -1
|
|
155
|
+
for (const v of vs) {
|
|
156
|
+
const have = valuesOf(v)
|
|
157
|
+
const n = Object.keys(want).filter((k) => have[k] === want[k]).length + (have.context === want.context ? 100 : 0)
|
|
158
|
+
if (n > most) [best, most] = [v, n]
|
|
159
|
+
}
|
|
160
|
+
return best
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// pick is the variant a request runs as: the model's context size, the
|
|
164
|
+
// effort asked for (else the default's) and fast as asked, matched against
|
|
165
|
+
// Cursor's variants. With no variant that matches, the parameters go as
|
|
166
|
+
// they are, from the default's; of a model served in part, as the nearest
|
|
167
|
+
// variant the account has. seed is the variant an id of Cursor's own named
|
|
168
|
+
// (request), in the default's place.
|
|
169
|
+
function pick(entry, effort, fast, seed) {
|
|
170
|
+
const def = entry.def
|
|
171
|
+
const want = { ...valuesOf(seed ?? def) }
|
|
172
|
+
if (entry.size) want.context = entry.size
|
|
173
|
+
if (effort === "off") effort = "none"
|
|
174
|
+
if (entry.pid && entry.pid !== "thinking" && effort === "none" && !("none" in entry.levels) && "thinking" in want) {
|
|
175
|
+
// a Claude asked not to think: thinking off at its default effort
|
|
176
|
+
want.thinking = "false"
|
|
177
|
+
} else if (entry.pid) {
|
|
178
|
+
const at = effort ? fitEffort(effort, Object.keys(entry.levels)) : ""
|
|
179
|
+
if (at && entry.levels[at]) want[entry.pid] = entry.levels[at]
|
|
180
|
+
// a variant named says for itself whether it thinks
|
|
181
|
+
if (!seed && entry.pid !== "thinking" && "thinking" in want && at) want.thinking = "true"
|
|
182
|
+
}
|
|
183
|
+
// fast only when asked: it costs more
|
|
184
|
+
if (entry.fast) want.fast = fast ? "true" : "false"
|
|
185
|
+
const same = (v) => {
|
|
186
|
+
const have = valuesOf(v)
|
|
187
|
+
return Object.keys({ ...have, ...want }).every((k) => have[k] === undefined || want[k] === undefined || have[k] === want[k])
|
|
188
|
+
}
|
|
189
|
+
const v = entry.variants.find(same) ?? (seed ? branch(entry, want) : undefined) ?? (entry.partial ? nearest(entry.variants, want) : undefined)
|
|
190
|
+
const params = Object.entries(v ? { ...want, ...valuesOf(v) } : want).map(([id, value]) => ({ id, value }))
|
|
191
|
+
// no variant: Max Mode when the size is more than Cursor allows outside it
|
|
192
|
+
const maxMode = v ? needsMax(entry.model, v) : needsMax(entry.model, def) || tokensOf(entry.size) > (entry.model.contextTokenLimit || Infinity)
|
|
193
|
+
// the agent API takes the variant's own id (its legacySlug, as
|
|
194
|
+
// cursor-agent models lists it: claude-opus-5-5-high), not the picker's
|
|
195
|
+
// model name; the default's when no variant matched
|
|
196
|
+
const id = v?.legacySlug || def?.legacySlug || entry.run
|
|
197
|
+
return { id, params, maxMode }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// branch is, for an id of Cursor's own asked at an effort no variant has
|
|
201
|
+
// with the rest of its parameters (claude-opus-5-low at xhigh: Opus 5
|
|
202
|
+
// without thinking stops at high), the variant with those parameters at the
|
|
203
|
+
// nearest effort it has, not the default's id with parameters that
|
|
204
|
+
// contradict it (claude-opus-5-thinking-high with thinking=false). Where
|
|
205
|
+
// the default differs from what is asked in the effort alone, the effort
|
|
206
|
+
// goes as asked, as for a listed model.
|
|
207
|
+
function branch(entry, want) {
|
|
208
|
+
if (!entry.pid || entry.pid === "thinking") return undefined
|
|
209
|
+
const def = valuesOf(entry.def)
|
|
210
|
+
if (Object.keys(want).every((k) => k === entry.pid || k === "context" || def[k] === undefined || def[k] === want[k])) return undefined
|
|
211
|
+
const vs = entry.variants.filter((v) => {
|
|
212
|
+
const have = valuesOf(v)
|
|
213
|
+
return entry.pid in have && Object.keys(want).every((k) => k === entry.pid || have[k] === undefined || have[k] === want[k])
|
|
214
|
+
})
|
|
215
|
+
const level = (x) => (x === "extra-high" ? "xhigh" : x)
|
|
216
|
+
const at = fitEffort(level(want[entry.pid]), vs.map((v) => level(valuesOf(v)[entry.pid])))
|
|
217
|
+
return at ? vs.find((v) => level(valuesOf(v)[entry.pid]) === at) : undefined
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// fitEffort is the nearest effort a model has to the one asked for; max
|
|
221
|
+
// for ultra, and none only when asked.
|
|
222
|
+
function fitEffort(want, levels) {
|
|
223
|
+
if (want === "ultra") want = "max"
|
|
224
|
+
if (!levels.length || levels.includes(want)) return want
|
|
225
|
+
const at = LEVEL_RANK.indexOf(want)
|
|
226
|
+
if (at < 0) return ""
|
|
227
|
+
let best = ""
|
|
228
|
+
let dist = LEVEL_RANK.length
|
|
229
|
+
for (const l of levels) {
|
|
230
|
+
const i = LEVEL_RANK.indexOf(l)
|
|
231
|
+
if (i < 0 || l === "none") continue
|
|
232
|
+
const d = Math.abs(i - at)
|
|
233
|
+
if (d < dist || (d === dist && i > at)) [best, dist] = [l, d]
|
|
234
|
+
}
|
|
235
|
+
return best
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// usable is the account's list, kept a while, by token: Cursor's picker,
|
|
239
|
+
// less the variants the usable list (AgentService/GetUsableModels, what
|
|
240
|
+
// cursor-agent models shows) says the account hasn't got. A picker Cursor
|
|
241
|
+
// can't give is a failure: magpie keeps the list it had. A usable list it
|
|
242
|
+
// can't give leaves the picker as it is.
|
|
243
|
+
const lists = new Map()
|
|
244
|
+
export async function usable(tok, force = false) {
|
|
245
|
+
const h = createHash("sha256").update(tok).digest("hex")
|
|
246
|
+
const had = lists.get(h)
|
|
247
|
+
if (!force && had && Date.now() - had.at < MODELS_KEEP) return had.raw
|
|
248
|
+
const [j, ids] = await Promise.all([
|
|
249
|
+
unary(API, "aiserver.v1.AiService/AvailableModels", tok, { useModelParameters: true, doNotUseMarkdown: true }, 30_000),
|
|
250
|
+
unary(API, "agent.v1.AgentService/GetUsableModels", tok, {}, 8_000).then(
|
|
251
|
+
(u) => {
|
|
252
|
+
const ids = new Set((u?.models ?? []).flatMap((m) => [m.modelId, m.displayModelId]).filter(Boolean))
|
|
253
|
+
return ids.size ? ids : null
|
|
254
|
+
},
|
|
255
|
+
() => null,
|
|
256
|
+
),
|
|
257
|
+
])
|
|
258
|
+
const raw = modelsOf(Array.isArray(j?.models) ? j.models : [], ids)
|
|
259
|
+
if (!raw.length) throw new Error("Cursor listed no models")
|
|
260
|
+
lists.set(h, { at: Date.now(), raw })
|
|
261
|
+
return raw
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// named is the model and variant an id the list doesn't have stands for,
|
|
265
|
+
// when it is one of Cursor's own, as the CLI reads one and as 0.1.x listed
|
|
266
|
+
// them, so a model picked before the list changed still runs: a variant's
|
|
267
|
+
// own id (claude-opus-5-5-high) is that variant; a family of them, the id
|
|
268
|
+
// 0.1.x listed (claude-opus-5-5, claude-opus-5-5-fast, cursor-grok-4.6,
|
|
269
|
+
// or with an effort the account lacks), is the family's variant nearest
|
|
270
|
+
// the default, fast when the family is and at the id's effort; the picker's
|
|
271
|
+
// name for a model, or a legacy slug of it, is its default. Each at the
|
|
272
|
+
// variant's context size, the default's when the id doesn't say.
|
|
273
|
+
function named(raw, id) {
|
|
274
|
+
const models = raw.filter((e, i) => raw.findIndex((x) => x.model === e.model) === i)
|
|
275
|
+
const at = (e, v) => raw.find((x) => x.model === e.model && x.size === (valuesOf(v).context ?? "")) ?? e
|
|
276
|
+
const as = (e, vs, effort) => {
|
|
277
|
+
const seed = nearest(vs, valuesOf(e.def))
|
|
278
|
+
return { entry: at(e, seed), seed, effort, fast: valuesOf(seed).fast === "true" }
|
|
279
|
+
}
|
|
280
|
+
for (const e of models) {
|
|
281
|
+
const vs = e.variants.filter((v) => v.legacySlug === id)
|
|
282
|
+
if (vs.length) return as(e, vs, "")
|
|
283
|
+
}
|
|
284
|
+
const [family, effort] = splitID(id)
|
|
285
|
+
for (const e of models) {
|
|
286
|
+
const vs = e.variants.filter((v) => v.legacySlug && splitID(v.legacySlug)[0] === family)
|
|
287
|
+
if (vs.length) return as(e, vs, effort)
|
|
288
|
+
}
|
|
289
|
+
for (const e of models) {
|
|
290
|
+
const m = e.model
|
|
291
|
+
if ([m.name, ...(m.legacySlugs ?? []), ...(m.idAliases ?? [])].includes(id)) return { entry: at(e, e.def), seed: undefined, effort: "", fast: false }
|
|
292
|
+
}
|
|
293
|
+
return null
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// request is how a chat's model is asked of Cursor: the variant's own id,
|
|
297
|
+
// its parameters and its mode. An id the list doesn't have that is one of
|
|
298
|
+
// Cursor's own goes as the variant it names, at the effort asked for, fast
|
|
299
|
+
// when it says so or the request does; any other goes as it is named.
|
|
300
|
+
export function request(raw, model, effort, fast) {
|
|
301
|
+
const entry = raw.find((m) => m.id === model)
|
|
302
|
+
if (entry) return pick(entry, effort, fast)
|
|
303
|
+
const old = named(raw, model)
|
|
304
|
+
if (old) return pick(old.entry, effort || old.effort, fast || old.fast, old.seed)
|
|
305
|
+
return { id: model === "auto" ? "default" : model, params: [], maxMode: false }
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const PI_LEVELS = [["off", "none"], ["minimal", "minimal"], ["low", "low"], ["medium", "medium"], ["high", "high"], ["xhigh", "xhigh"], ["max", "max"]]
|
|
309
|
+
|
|
310
|
+
function thinkingLevelMap(levels) {
|
|
311
|
+
if (!levels || !Object.keys(levels).length) return undefined
|
|
312
|
+
const map = {}
|
|
313
|
+
for (const [pi, cur] of PI_LEVELS) {
|
|
314
|
+
if (Object.prototype.hasOwnProperty.call(levels, cur)) map[pi] = levels[cur]
|
|
315
|
+
else if (pi !== "off") map[pi] = null
|
|
316
|
+
}
|
|
317
|
+
return map
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** Pi chat-model configs. Cost stays zero: a subscription bill is not a token price. */
|
|
321
|
+
export function piModelConfigs(raw) {
|
|
322
|
+
return raw.map((m) => {
|
|
323
|
+
const thinking = thinkingLevelMap(m.levels)
|
|
324
|
+
return {
|
|
325
|
+
id: m.id,
|
|
326
|
+
name: m.name,
|
|
327
|
+
api: "cursor-run",
|
|
328
|
+
reasoning: !!thinking,
|
|
329
|
+
...(thinking ? { thinkingLevelMap: thinking } : {}),
|
|
330
|
+
input: ["text", "image"],
|
|
331
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
332
|
+
contextWindow: m.context || DEFAULT_CONTEXT,
|
|
333
|
+
maxTokens: 32_000,
|
|
334
|
+
}
|
|
335
|
+
})
|
|
336
|
+
}
|