@magpie-community/opencode-kiro-auth 0.1.28 → 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.
- package/README.md +7 -0
- package/index.mjs +45 -6
- package/opencode2.mjs +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -253,6 +253,13 @@ With overage on (`overageConfiguration.overageStatus` `ENABLED`), the
|
|
|
253
253
|
month's window counts against the overage cap (`overageCap`), which is the
|
|
254
254
|
month's whole limit; with it off, against the plan's credits.
|
|
255
255
|
|
|
256
|
+
Each request's cost is what Kiro's `meteringEvent`s say, in credits, summed
|
|
257
|
+
over the request (a web search's turns included). It goes on as
|
|
258
|
+
`usage.credit` in the last `message_delta`, or in the message's `usage`, for
|
|
259
|
+
magpie's ledger (yetone/magpie#4328). Kiro gives no token counts, so the
|
|
260
|
+
input is reckoned from how full the context is, and the cache is unknown
|
|
261
|
+
(`magpie_cache_unknown`).
|
|
262
|
+
|
|
256
263
|
### Identity Center credit regions
|
|
257
264
|
|
|
258
265
|
An IAM Identity Center account has a profile, and its own month of credits
|
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
|
}
|
|
@@ -1542,7 +1573,9 @@ async function* events(body, model, budget) {
|
|
|
1542
1573
|
// kiro-cli 2.32's own trace log shows its MetadataEvent with every token
|
|
1543
1574
|
// count None, while a repeated prompt is billed about half the credits,
|
|
1544
1575
|
// so Kiro does cache and doesn't say how much
|
|
1545
|
-
|
|
1576
|
+
// credits: what Kiro's meteringEvents say the request cost, summed;
|
|
1577
|
+
// undefined when it sent none
|
|
1578
|
+
const usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reported: false, credits: undefined }
|
|
1546
1579
|
let pct = 0
|
|
1547
1580
|
let said = 0
|
|
1548
1581
|
let failed = ""
|
|
@@ -1601,6 +1634,8 @@ async function* events(body, model, budget) {
|
|
|
1601
1634
|
}
|
|
1602
1635
|
} else if (kind === "contextUsageEvent") {
|
|
1603
1636
|
if (m.contextUsagePercentage > 0) pct = m.contextUsagePercentage
|
|
1637
|
+
} else if (kind === "meteringEvent") {
|
|
1638
|
+
if (Number.isFinite(m.usage) && m.usage >= 0 && (!m.unit || /^credit/i.test(m.unit))) usage.credits = (usage.credits ?? 0) + m.usage
|
|
1604
1639
|
} else if (["error", "throttlingError", "validationError", "serviceUnavailableError", "internalServerException"].includes(kind)) {
|
|
1605
1640
|
failed = exception(kind, f.payload)
|
|
1606
1641
|
break
|
|
@@ -1656,6 +1691,9 @@ const anthropicUsage = (u) => ({
|
|
|
1656
1691
|
: { input_tokens: u.input, output_tokens: u.output, magpie_cache_unknown: true }),
|
|
1657
1692
|
// the hosted web searches made (searching)
|
|
1658
1693
|
...(u.searches ? { server_tool_use: { web_search_requests: u.searches } } : {}),
|
|
1694
|
+
// what Kiro said the request cost in credits (its meteringEvents), as
|
|
1695
|
+
// magpie reads a plan's credits (yetone/magpie#4328)
|
|
1696
|
+
...(u.credits !== undefined ? { credit: u.credits } : {}),
|
|
1659
1697
|
})
|
|
1660
1698
|
|
|
1661
1699
|
// quotaWords are magpie's (internal/gateway/fallback.go): how a vendor says
|
|
@@ -1673,7 +1711,7 @@ const QUOTA_WORDS = /quota|insufficient|balance|credit|billing|exceeded|rate.?li
|
|
|
1673
1711
|
// error, so it never compacted (ergou0954_54426 on Discord: Kiro in a
|
|
1674
1712
|
// group routed by intent, "超出上下文", the turn failed).
|
|
1675
1713
|
const failedBefore = (text) =>
|
|
1676
|
-
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)
|
|
1677
1715
|
|
|
1678
1716
|
// reply answers the Messages request from Kiro's events: a stream of
|
|
1679
1717
|
// Messages' events, or one message. names gives back the caller's name of
|
|
@@ -2061,7 +2099,7 @@ async function ask(creds, auth, req, model, budget, signal) {
|
|
|
2061
2099
|
const tried = new Set()
|
|
2062
2100
|
while (!res.ok && a.key && creds.spend) {
|
|
2063
2101
|
const text = (await res.text()).slice(0, 1 << 20)
|
|
2064
|
-
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) }
|
|
2065
2103
|
if (!usedUpSaid(f.message)) return f
|
|
2066
2104
|
tried.add(a.region)
|
|
2067
2105
|
creds.spend(a.key, a.region)
|
|
@@ -2083,7 +2121,7 @@ async function ask(creds, auth, req, model, budget, signal) {
|
|
|
2083
2121
|
if (!res.ok) {
|
|
2084
2122
|
// Kiro's own 401 is passed on, as the built-in did, the sign-in kept
|
|
2085
2123
|
const text = (await res.text()).slice(0, 1 << 20)
|
|
2086
|
-
return { ...failure(res.status, text), wait: waitOf(res.headers, text) }
|
|
2124
|
+
return { ...failure(res.status, text, requestID(res.headers)), wait: waitOf(res.headers, text) }
|
|
2087
2125
|
}
|
|
2088
2126
|
return { res, a }
|
|
2089
2127
|
}
|
|
@@ -2155,6 +2193,7 @@ async function* searching(creds, auth, req, model, budget, signal, hosted, first
|
|
|
2155
2193
|
usage.cacheRead += stop.usage.cacheRead
|
|
2156
2194
|
usage.cacheWrite += stop.usage.cacheWrite
|
|
2157
2195
|
usage.reported ||= stop.usage.reported
|
|
2196
|
+
if (stop.usage.credits !== undefined) usage.credits = (usage.credits ?? 0) + stop.usage.credits
|
|
2158
2197
|
for (const b of said)
|
|
2159
2198
|
if (b.type === "tool_use")
|
|
2160
2199
|
try {
|
package/opencode2.mjs
CHANGED
|
@@ -86,7 +86,10 @@ export function fieldsOf(prompts) {
|
|
|
86
86
|
if (p.type === "select" && Array.isArray(p.options))
|
|
87
87
|
f.options = p.options.map((o) => ({ value: String(o.value), label: String(o.label ?? o.value), ...(o.hint ? { description: String(o.hint) } : {}) }))
|
|
88
88
|
// a prompt shown only when another answer says so can't be required
|
|
89
|
-
|
|
89
|
+
// a prompt shown only when another answer says so can't be required, and
|
|
90
|
+
// neither can one whose own validate() accepts an empty answer (the
|
|
91
|
+
// browser sign-in's optional API key)
|
|
92
|
+
if (!p.condition && p.required !== false) f.required = true
|
|
90
93
|
out.push(f)
|
|
91
94
|
}
|
|
92
95
|
return out.length ? out : undefined
|