@magpie-community/opencode-cursor-auth 0.1.4 → 0.1.5

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.
Files changed (2) hide show
  1. package/index.mjs +27 -16
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -158,9 +158,10 @@ async function cliTokenNow() {
158
158
  await cliRefresh
159
159
  tok = await cliToken()
160
160
  }
161
- if (!tok) throw new AuthError("Cursor isn't signed in: run `cursor-agent login`, or sign in again")
161
+ // the built-in's words (provider.CursorToken)
162
+ if (!tok) throw new AuthError("Cursor isn't signed in; sign in from magpie's Providers page or run `cursor-agent login`")
162
163
  const e = expiry(tok)
163
- if (e && e <= Date.now()) throw new AuthError("Cursor's sign-in has run out: run `cursor-agent login`, or sign in again")
164
+ 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`")
164
165
  return tok
165
166
  }
166
167
 
@@ -847,21 +848,17 @@ function failure(status, text) {
847
848
  return statusOf(status, f.code ?? "", msg)
848
849
  }
849
850
 
850
- // signInExpired is a message of a token or session run out; a trial, a
851
- // plan or a link run out is no lapsed sign-in, and magpie marks the
852
- // account lapsed on a 401.
853
- const signInExpired = (low) => low.includes("expired") && /token|session|sign[- ]?in|log[- ]?in|auth|credential/.test(low)
854
-
855
851
  // statusOf is the status and words for Cursor's error of this code and
856
852
  // message. A region the team isn't served in says so, not to sign in.
857
853
  function statusOf(status, code, msg) {
858
- msg ||= statusText(status) || `HTTP ${status}`
854
+ msg ||= statusText(status)
859
855
  const low = msg.toLowerCase()
860
856
  // magpie names the provider before the message itself
861
857
  const out = (status, message) => ({ status, message })
862
858
  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")
863
859
  if (code === "permission_denied") return out(403, msg)
864
- if (code === "unauthenticated" || status === 401 || signInExpired(low)) return out(401, msg + " — sign in to Cursor again")
860
+ if (code === "unauthenticated" || status === 401 || low.includes("expired"))
861
+ return out(401, msg + " — sign in to Cursor again in magpie")
865
862
  if (code === "resource_exhausted" || low.includes("quota") || low.includes("rate limit") || low.includes("usage limit"))
866
863
  return out(429, "usage limit reached: " + msg)
867
864
  if (low.includes("too long") || low.includes("context length") || low.includes("too many tokens"))
@@ -1357,15 +1354,28 @@ function exec(msg, tools, send, closeExec) {
1357
1354
 
1358
1355
  // ---- chat completions ------------------------------------------------------------------
1359
1356
 
1357
+ // Every answer says X-Magpie-Sign-In: kept. The built-in marked no Cursor
1358
+ // account lapsed, not for a 401 nor for any "expired" (a token, a session,
1359
+ // a trial), and cleared none on a success or a renewed token.
1360
1360
  const errorResponse = ({ status, message }) =>
1361
1361
  new Response(JSON.stringify({ error: { message, type: "cursor_error", code: status } }), {
1362
1362
  status,
1363
- headers: { "Content-Type": "application/json" },
1363
+ headers: { "Content-Type": "application/json", "X-Magpie-Sign-In": "kept" },
1364
1364
  })
1365
1365
 
1366
- // answer runs a chat completion on Cursor and answers it as one, streamed
1366
+ function kept(res) {
1367
+ if (res.headers.has("X-Magpie-Sign-In")) return res
1368
+ const headers = new Headers(res.headers)
1369
+ headers.set("X-Magpie-Sign-In", "kept")
1370
+ return new Response(res.body, { status: res.status, statusText: res.statusText, headers })
1371
+ }
1372
+
1373
+ // answer runs a chat completion on Cursor, the account kept (errorResponse).
1374
+ const answer = async (auth, chat, signal) => kept(await answerOf(auth, chat, signal))
1375
+
1376
+ // answerOf runs a chat completion on Cursor and answers it as one, streamed
1367
1377
  // or not.
1368
- async function answer(auth, chat, signal) {
1378
+ async function answerOf(auth, chat, signal) {
1369
1379
  let tok
1370
1380
  try {
1371
1381
  tok = await tokenOf(auth)
@@ -1480,16 +1490,17 @@ export async function CursorAuthPlugin() {
1480
1490
  { type: "api", label: "Cursor API key (cursor.com/dashboard → Integrations)" },
1481
1491
  ],
1482
1492
  // magpie's: how much of the plan's included usage is gone (the plan
1483
- // is the one the sign-in read)
1493
+ // is the one the sign-in read). The built-in's read, clean or not,
1494
+ // neither marked the account nor cleared it.
1484
1495
  async usage(getAuth, provider) {
1485
1496
  let tok
1486
1497
  try {
1487
1498
  tok = await tokenOf(await getAuth())
1488
1499
  } catch (e) {
1489
- return { error: e.message, windows: [] }
1500
+ return { error: e.message, windows: [], signIn: "kept" }
1490
1501
  }
1491
1502
  const [u, plan] = await Promise.all([usage(tok, Object.keys(provider?.models ?? {})), planOf(tok)])
1492
- return plan ? { ...u, plan } : u
1503
+ return { ...u, ...(plan ? { plan } : {}), signIn: "kept" }
1493
1504
  },
1494
1505
  },
1495
1506
  async config(config) {
@@ -1522,4 +1533,4 @@ export async function CursorAuthPlugin() {
1522
1533
  }
1523
1534
 
1524
1535
  // for tests
1525
- export const _internal = { poolBase, splitID, families, byEffort, offered, modelID, conversation, buildRun, decode, exec, fields, pb, pbValue, pbAny, frame, frames, failure, toolsOf, expiry }
1536
+ export const _internal = { errorResponse, kept, poolBase, splitID, families, byEffort, offered, modelID, conversation, buildRun, decode, exec, fields, pb, pbValue, pbAny, frame, frames, failure, toolsOf, expiry }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-cursor-auth",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",