@magpie-community/opencode-kiro-auth 0.1.26 → 0.1.27

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 +33 -6
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -1202,13 +1202,39 @@ const ERROR_TYPES = { 400: "invalid_request_error", 401: "authentication_error",
1202
1202
  404: "not_found_error", 413: "request_too_large", 429: "rate_limit_error", 503: "overloaded_error", 529: "overloaded_error" }
1203
1203
  const errorType = (status) => ERROR_TYPES[status] ?? "api_error"
1204
1204
  // Each failure says X-Magpie-Sign-In: kept, as the built-in never marked
1205
- // an account lapsed (see gone).
1206
- const errorResponse = (status, message) =>
1205
+ // an account lapsed (see gone). One Kiro said when to ask again (waitOf)
1206
+ // says it as Retry-After, in whole seconds, for magpie to wait as long.
1207
+ const errorResponse = (status, message, wait) =>
1207
1208
  new Response(JSON.stringify({ type: "error", error: { type: errorType(status), message } }), {
1208
1209
  status,
1209
- headers: { "Content-Type": "application/json", "X-Magpie-Sign-In": "kept" },
1210
+ headers: { "Content-Type": "application/json", "X-Magpie-Sign-In": "kept",
1211
+ ...(wait > 0 ? { "Retry-After": String(Math.ceil(wait / 1000)) } : {}) },
1210
1212
  })
1211
1213
 
1214
+ // waitOf is the milliseconds Kiro's refusal says to wait before asking
1215
+ // again, or undefined: what Kiro's own clients read. The Kiro IDE (agent
1216
+ // 1.1.499) reads the x-amzn-kiro-ratelimit-retry-after header and the
1217
+ // body's retryAfterMilliseconds, both in milliseconds; kiro-cli's AWS SDK
1218
+ // reads x-amz-retry-after, in milliseconds; and Retry-After is HTTP's, in
1219
+ // seconds or as a date. Unsaid, magpie keeps to its own pauses.
1220
+ function waitOf(headers, body) {
1221
+ const ms = (v) => {
1222
+ const n = v == null || v === "" ? NaN : Number(v)
1223
+ return Number.isFinite(n) && n > 0 ? n : undefined
1224
+ }
1225
+ let w = ms(headers.get("x-amzn-kiro-ratelimit-retry-after")) ?? ms(headers.get("x-amz-retry-after"))
1226
+ if (w === undefined)
1227
+ try {
1228
+ w = ms(JSON.parse(body)?.retryAfterMilliseconds)
1229
+ } catch {}
1230
+ const ra = headers.get("retry-after")
1231
+ if (w === undefined && ra) {
1232
+ const secs = ms(ra)
1233
+ w = secs !== undefined ? secs * 1000 : ms(Date.parse(ra) - Date.now())
1234
+ }
1235
+ return w
1236
+ }
1237
+
1212
1238
  // kept is res saying the account is kept: a success too, which the
1213
1239
  // built-in never took for a sign-in come back.
1214
1240
  function kept(res) {
@@ -1842,7 +1868,7 @@ async function generate(creds, auth, req, signal) {
1842
1868
  const hosted = hostedSearch(req)
1843
1869
  if (hosted?.error) return errorResponse(400, hosted.error)
1844
1870
  const first = await ask(creds, auth, req, model, budget, signal)
1845
- if (!first.res) return errorResponse(first.status, first.message)
1871
+ if (!first.res) return errorResponse(first.status, first.message, first.wait)
1846
1872
  const it = hosted ? searching(creds, auth, req, model, budget, signal, hosted, first) : events(first.res.body, model, budget)
1847
1873
  return kept(await reply(it, model, req.stream === true, toolNames(req)))
1848
1874
  }
@@ -1879,7 +1905,8 @@ async function ask(creds, auth, req, model, budget, signal) {
1879
1905
  }
1880
1906
  if (!res.ok) {
1881
1907
  // Kiro's own 401 is passed on, as the built-in did, the sign-in kept
1882
- return failure(res.status, (await res.text()).slice(0, 1 << 20))
1908
+ const text = (await res.text()).slice(0, 1 << 20)
1909
+ return { ...failure(res.status, text), wait: waitOf(res.headers, text) }
1883
1910
  }
1884
1911
  return { res, a }
1885
1912
  }
@@ -2418,4 +2445,4 @@ export async function KiroAuthPlugin({ client } = {}) {
2418
2445
  }
2419
2446
 
2420
2447
  // for tests
2421
- export const _internal = { generate, refresh, usageOf, buildKiro, kiroBody, IMAGE_LIMIT, events, reply, failure, toolID, toolName, toolNames, thinking, nativeFields, listModels, contextFirst, configModel, runtimeModel, readCLI, readIDE, regionOf, planName, frames }
2448
+ export const _internal = { generate, waitOf, refresh, usageOf, buildKiro, kiroBody, IMAGE_LIMIT, events, reply, failure, toolID, toolName, toolNames, thinking, nativeFields, listModels, contextFirst, configModel, runtimeModel, readCLI, readIDE, regionOf, planName, frames }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-kiro-auth",
3
- "version": "0.1.26",
3
+ "version": "0.1.27",
4
4
  "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",