@magpie-community/opencode-kiro-auth 0.1.29 → 0.1.30

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 +36 -5
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -1337,7 +1337,7 @@ const TOO_LONG = "input is too long for the model's context: "
1337
1337
 
1338
1338
  // failure is the status and message for an error Kiro answered with:
1339
1339
  // {"message": "...", "reason": "..."}.
1340
- function failure(status, body) {
1340
+ function failure(status, body, id) {
1341
1341
  let msg = String(body ?? "").trim()
1342
1342
  try {
1343
1343
  const e = JSON.parse(body)
@@ -1347,9 +1347,33 @@ function failure(status, body) {
1347
1347
  if (tooLongSaid(msg)) return { status: 400, message: TOO_LONG + msg }
1348
1348
  if (msg.includes("INSUFFICIENT_MODEL_CAPACITY")) return { status: 503, message: msg }
1349
1349
  if (msg.includes("MONTHLY_REQUEST_COUNT") || msg.includes("USAGE_LIMIT")) return { status: 429, message: "usage limit reached: " + msg }
1350
+ if (msg.includes(USER_RATE)) return { status: 429, message: userRate(msg, id) }
1350
1351
  return { status, message: msg }
1351
1352
  }
1352
1353
 
1354
+ // USER_RATE is the reason Kiro gives a 429 for one account's requests
1355
+ // coming too often (the Kiro IDE's ThrottlingException reasons, agent
1356
+ // 1.1.499: apart from the credit rate, the service's rate and the daily
1357
+ // and monthly counts). Kiro's own words, "Too many requests, please wait
1358
+ // before trying again.", say neither whose limit it is nor how long:
1359
+ // onemezao on Discord had it on every request of one account through pi,
1360
+ // 42s each, while the Kiro IDE answered on the same account.
1361
+ const USER_RATE = "USER_REQUEST_RATE_EXCEEDED"
1362
+ // USER_RATE_WAIT is the wait a USER_RATE refusal says when Kiro says none:
1363
+ // longer than magpie's same-account retries (8s), so the request isn't
1364
+ // sent again at 1, 2 and 4s, each try one more on the account's rate.
1365
+ // Other Kiro relays pause 3 to 12s for it; one that outlived those
1366
+ // wasn't going to clear in magpie's next few.
1367
+ const USER_RATE_WAIT = 30_000
1368
+
1369
+ // userRate is a USER_RATE refusal in plain words: whose limit, what to
1370
+ // do, and the request ID Kiro support asks for.
1371
+ const userRate = (msg, id) =>
1372
+ `rate limited: Kiro is limiting how often this Kiro account sends requests (${msg}). ` +
1373
+ "Wait a minute before sending again. If it keeps coming back while the Kiro app works on the same account, " +
1374
+ "the limit is on the account, and Kiro support (app.kiro.dev) can look at it" +
1375
+ (id ? `, with request ID ${id}.` : ".")
1376
+
1353
1377
  const ERROR_TYPES = { 400: "invalid_request_error", 401: "authentication_error", 402: "billing_error", 403: "permission_error",
1354
1378
  404: "not_found_error", 413: "request_too_large", 429: "rate_limit_error", 503: "overloaded_error", 529: "overloaded_error" }
1355
1379
  const errorType = (status) => ERROR_TYPES[status] ?? "api_error"
@@ -1363,12 +1387,17 @@ const errorResponse = (status, message, wait) =>
1363
1387
  ...(wait > 0 ? { "Retry-After": String(Math.ceil(wait / 1000)) } : {}) },
1364
1388
  })
1365
1389
 
1390
+ // requestID is the ID Kiro gave its answer, which its support asks for:
1391
+ // what the Kiro IDE reads (x-amzn-requestid, else x-request-id).
1392
+ const requestID = (headers) => headers.get("x-amzn-requestid") || headers.get("x-request-id") || ""
1393
+
1366
1394
  // waitOf is the milliseconds Kiro's refusal says to wait before asking
1367
1395
  // again, or undefined: what Kiro's own clients read. The Kiro IDE (agent
1368
1396
  // 1.1.499) reads the x-amzn-kiro-ratelimit-retry-after header and the
1369
1397
  // body's retryAfterMilliseconds, both in milliseconds; kiro-cli's AWS SDK
1370
1398
  // reads x-amz-retry-after, in milliseconds; and Retry-After is HTTP's, in
1371
- // seconds or as a date. Unsaid, magpie keeps to its own pauses.
1399
+ // seconds or as a date. Unsaid, magpie keeps to its own pauses, but for
1400
+ // an account's request rate (USER_RATE_WAIT).
1372
1401
  function waitOf(headers, body) {
1373
1402
  const ms = (v) => {
1374
1403
  const n = v == null || v === "" ? NaN : Number(v)
@@ -1384,6 +1413,7 @@ function waitOf(headers, body) {
1384
1413
  const secs = ms(ra)
1385
1414
  w = secs !== undefined ? secs * 1000 : ms(Date.parse(ra) - Date.now())
1386
1415
  }
1416
+ if (w === undefined && String(body ?? "").includes(USER_RATE)) w = USER_RATE_WAIT
1387
1417
  return w
1388
1418
  }
1389
1419
 
@@ -1464,6 +1494,7 @@ function exception(kind, payload) {
1464
1494
  } catch {}
1465
1495
  let msg = e.message || e.Message || new TextDecoder().decode(payload).trim()
1466
1496
  if (e.reason) msg += ` (${e.reason})`
1497
+ if (e.reason === USER_RATE) return userRate(msg)
1467
1498
  if (String(kind).toLowerCase().includes("throttl")) return "rate limited: " + msg
1468
1499
  return kind ? `${kind}: ${msg}` : msg
1469
1500
  }
@@ -1680,7 +1711,7 @@ const QUOTA_WORDS = /quota|insufficient|balance|credit|billing|exceeded|rate.?li
1680
1711
  // error, so it never compacted (ergou0954_54426 on Discord: Kiro in a
1681
1712
  // group routed by intent, "超出上下文", the turn failed).
1682
1713
  const failedBefore = (text) =>
1683
- tooLongSaid(text) ? errorResponse(400, TOO_LONG + text) : errorResponse(QUOTA_WORDS.test(text) ? 429 : 502, text)
1714
+ tooLongSaid(text) ? errorResponse(400, TOO_LONG + text) : text.includes(USER_RATE) ? errorResponse(429, text, USER_RATE_WAIT) : errorResponse(QUOTA_WORDS.test(text) ? 429 : 502, text)
1684
1715
 
1685
1716
  // reply answers the Messages request from Kiro's events: a stream of
1686
1717
  // Messages' events, or one message. names gives back the caller's name of
@@ -2068,7 +2099,7 @@ async function ask(creds, auth, req, model, budget, signal) {
2068
2099
  const tried = new Set()
2069
2100
  while (!res.ok && a.key && creds.spend) {
2070
2101
  const text = (await res.text()).slice(0, 1 << 20)
2071
- const f = { ...failure(res.status, text), wait: waitOf(res.headers, text) }
2102
+ const f = { ...failure(res.status, text, requestID(res.headers)), wait: waitOf(res.headers, text) }
2072
2103
  if (!usedUpSaid(f.message)) return f
2073
2104
  tried.add(a.region)
2074
2105
  creds.spend(a.key, a.region)
@@ -2090,7 +2121,7 @@ async function ask(creds, auth, req, model, budget, signal) {
2090
2121
  if (!res.ok) {
2091
2122
  // Kiro's own 401 is passed on, as the built-in did, the sign-in kept
2092
2123
  const text = (await res.text()).slice(0, 1 << 20)
2093
- return { ...failure(res.status, text), wait: waitOf(res.headers, text) }
2124
+ return { ...failure(res.status, text, requestID(res.headers)), wait: waitOf(res.headers, text) }
2094
2125
  }
2095
2126
  return { res, a }
2096
2127
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-kiro-auth",
3
- "version": "0.1.29",
3
+ "version": "0.1.30",
4
4
  "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",