@magpie-community/opencode-kiro-auth 0.1.29 → 0.1.31
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/index.mjs +117 -35
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -21,7 +21,7 @@ import { createHash, randomBytes, randomUUID } from "node:crypto"
|
|
|
21
21
|
import { execFile } from "node:child_process"
|
|
22
22
|
import { existsSync, statSync } from "node:fs"
|
|
23
23
|
import { readFile, rename, writeFile } from "node:fs/promises"
|
|
24
|
-
import { homedir, hostname } from "node:os"
|
|
24
|
+
import { homedir, hostname, platform, release } from "node:os"
|
|
25
25
|
import { delimiter, join } from "node:path"
|
|
26
26
|
|
|
27
27
|
const ID = "kiro"
|
|
@@ -40,7 +40,10 @@ const CALLBACK_PORTS = [3128, 4649, 6588, 8008, 9091, 49153, 50153, 51153, 52153
|
|
|
40
40
|
const SCOPES = ["codewhisperer:completions", "codewhisperer:analysis", "codewhisperer:conversations",
|
|
41
41
|
"codewhisperer:transformations", "codewhisperer:taskassist"]
|
|
42
42
|
// how the IDE names itself to Kiro's auth service, and when refreshing
|
|
43
|
-
|
|
43
|
+
// MACHINE_ID stands where the IDE puts its machine id (node-machine-id's
|
|
44
|
+
// sha256): one per install, the same on every call.
|
|
45
|
+
const MACHINE_ID = createHash("sha256").update("magpie:" + hostname()).digest("hex")
|
|
46
|
+
const IDE_UA = "KiroIDE-1.1.70-" + MACHINE_ID
|
|
44
47
|
const DESKTOP_UA = "Kiro-Desktop/0.2.13 (darwin; arm64)"
|
|
45
48
|
const SIGN_IN_TIMEOUT = 10 * 60 * 1000
|
|
46
49
|
// A Builder ID sign-in has no profile of its own, and Kiro won't list one
|
|
@@ -704,7 +707,7 @@ function account(client) {
|
|
|
704
707
|
// kiro-cli names no profile in an API key's calls, even one GetProfile
|
|
705
708
|
// found; the profile only says the key's region
|
|
706
709
|
const profile = c.method === "apikey" ? "" : c.profile
|
|
707
|
-
const a = { token: c.access, tokenType: tokenType(c), profile, region: regionOf(c.profile, c.region) }
|
|
710
|
+
const a = { token: c.access, tokenType: tokenType(c), idc: c.method === "idc", profile, region: regionOf(c.profile, c.region) }
|
|
708
711
|
if (!twoRegions(c) || st.builderID || !c.profiles) return a
|
|
709
712
|
// the first enabled region with credits left, in CREDIT_REGIONS' order
|
|
710
713
|
const now = Date.now()
|
|
@@ -1067,14 +1070,14 @@ function inputSchema(s) {
|
|
|
1067
1070
|
|
|
1068
1071
|
// buildKiro is the body of a generateAssistantResponse call, as it is
|
|
1069
1072
|
// before its images are fitted (kiroBody).
|
|
1070
|
-
function buildKiro(req, model, profile, budget) {
|
|
1071
|
-
return JSON.stringify(kiroRequest(req, model, profile, budget))
|
|
1073
|
+
function buildKiro(req, model, profile, budget, conversation) {
|
|
1074
|
+
return JSON.stringify(kiroRequest(req, model, profile, budget, conversation))
|
|
1072
1075
|
}
|
|
1073
1076
|
|
|
1074
1077
|
// kiroBody is the body sent: buildKiro's, with every image it keeps under
|
|
1075
1078
|
// Kiro's size limit.
|
|
1076
|
-
async function kiroBody(req, model, profile, budget) {
|
|
1077
|
-
return JSON.stringify(await fitImages(kiroRequest(req, model, profile, budget)))
|
|
1079
|
+
async function kiroBody(req, model, profile, budget, conversation) {
|
|
1080
|
+
return JSON.stringify(await fitImages(kiroRequest(req, model, profile, budget, conversation)))
|
|
1078
1081
|
}
|
|
1079
1082
|
|
|
1080
1083
|
// UNDERSTOOD is the reply Kiro IDE puts in its history where a turn has
|
|
@@ -1105,7 +1108,7 @@ function contextFirst(messages) {
|
|
|
1105
1108
|
return [...messages.slice(0, start), ...context.map((m) => ({ ...m, role: "user" })), { role: "assistant", content: UNDERSTOOD }, ...turn]
|
|
1106
1109
|
}
|
|
1107
1110
|
|
|
1108
|
-
function kiroRequest(req, model, profile, budget) {
|
|
1111
|
+
function kiroRequest(req, model, profile, budget, conversation) {
|
|
1109
1112
|
const entries = []
|
|
1110
1113
|
// the messages the caller marked for its prompt cache (cache_control),
|
|
1111
1114
|
// which go to Kiro as a cachePoint on the message they end up in (#110)
|
|
@@ -1113,7 +1116,7 @@ function kiroRequest(req, model, profile, budget) {
|
|
|
1113
1116
|
const mark = (msg, parts) => {
|
|
1114
1117
|
if (parts.some((p) => p?.cache_control)) marked.add(msg)
|
|
1115
1118
|
}
|
|
1116
|
-
const user = () => ({ content: "", modelId: model, origin: "
|
|
1119
|
+
const user = () => ({ content: "", modelId: model, origin: "AI_EDITOR" })
|
|
1117
1120
|
const resultsOnly = new WeakSet() // the messages made to carry a reply's web search results
|
|
1118
1121
|
for (const m of contextFirst(req.messages ?? [])) {
|
|
1119
1122
|
const parts = typeof m.content === "string" ? [{ type: "text", text: m.content }] : m.content ?? []
|
|
@@ -1294,31 +1297,76 @@ function kiroRequest(req, model, profile, budget) {
|
|
|
1294
1297
|
}
|
|
1295
1298
|
for (const drop of points.slice(0, Math.max(0, points.length - 4))) drop()
|
|
1296
1299
|
|
|
1297
|
-
const state = { chatTriggerType: "MANUAL", agentTaskType: "vibe", conversationId:
|
|
1300
|
+
const state = { chatTriggerType: "MANUAL", agentTaskType: "vibe", conversationId: conversationID(conversation), currentMessage: { userInputMessage: current } }
|
|
1298
1301
|
if (entries.length > 1) state.history = entries.slice(0, -1)
|
|
1299
|
-
|
|
1302
|
+
// the agent mode goes as a header (x-amzn-kiro-agent-mode), as the IDE's
|
|
1303
|
+
// client binds it, not in the body
|
|
1304
|
+
const out = { conversationState: state }
|
|
1300
1305
|
if (profile) out.profileArn = profile
|
|
1301
1306
|
if (native) out.additionalModelRequestFields = native
|
|
1302
1307
|
return out
|
|
1303
1308
|
}
|
|
1304
1309
|
|
|
1305
|
-
//
|
|
1306
|
-
//
|
|
1310
|
+
// conversationID is the conversationId of a chat call. The IDE keeps one
|
|
1311
|
+
// for a whole conversation; the plugin keeps one for the caller's session
|
|
1312
|
+
// (magpie's X-Magpie-Conversation), the same id on each turn of it, and a
|
|
1313
|
+
// new one when it can't tell the session.
|
|
1314
|
+
function conversationID(key) {
|
|
1315
|
+
if (!key) return randomUUID()
|
|
1316
|
+
const h = createHash("sha256").update("kiro-conversation:" + key).digest("hex")
|
|
1317
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-4${h.slice(13, 16)}-${"89ab"[parseInt(h[16], 16) & 3]}${h.slice(17, 20)}-${h.slice(20, 32)}`
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
// the headers that name the caller's session, the first one sent
|
|
1321
|
+
const SESSION_HEADERS = ["x-magpie-conversation", "x-magpie-session", "x-opencode-session"]
|
|
1322
|
+
function sessionOf(input, init) {
|
|
1323
|
+
const sets = [init?.headers, input instanceof Request ? input.headers : undefined]
|
|
1324
|
+
for (const name of SESSION_HEADERS)
|
|
1325
|
+
for (const h of sets) {
|
|
1326
|
+
if (!h) continue
|
|
1327
|
+
const v = h instanceof Headers ? h.get(name)
|
|
1328
|
+
: Array.isArray(h) ? h.find(([k]) => k.toLowerCase() === name)?.[1]
|
|
1329
|
+
: Object.entries(h).find(([k]) => k.toLowerCase() === name)?.[1]
|
|
1330
|
+
if (v) return String(v)
|
|
1331
|
+
}
|
|
1332
|
+
return ""
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
// What Kiro IDE 1.4.121 (its agent, KAS 0.66.89, on Electron 44's Node
|
|
1336
|
+
// 24.22.1) sends on a chat call, read from the IDE's own extension.js. Its
|
|
1337
|
+
// KiroRuntime client (@amzn/kiro-runtime-service-typescript-client 1.0.0)
|
|
1338
|
+
// is the AWS SDK for JavaScript's: the SDK's user agent, m/N for the
|
|
1339
|
+
// endpoint the IDE names itself, then the IDE's own "KiroIDE <version>
|
|
1340
|
+
// <machine id>" with KAS/<version> and acp-client/kiro-ide, spaces made
|
|
1341
|
+
// "-". Its retry budget is 3 tries at a conversation's start, said in
|
|
1342
|
+
// amz-sdk-request and x-amzn-kiro-attempt; the plugin tries once.
|
|
1343
|
+
const IDE_VERSION = "1.4.121"
|
|
1344
|
+
const IDE_KAS = "0.66.89"
|
|
1345
|
+
const IDE_NODE = "24.22.1"
|
|
1346
|
+
const IDE_APP = `KiroIDE-${IDE_VERSION}-${MACHINE_ID}-KAS/${IDE_KAS} acp-client/kiro-ide`
|
|
1347
|
+
const sdkPart = (s) => String(s).replace(/[^!$%&'*+\-.^_`|~\w#]/g, "-")
|
|
1348
|
+
const IDE_SDK_UA = `aws-sdk-js/1.0.0 ua/2.1 os/${sdkPart(platform())}#${sdkPart(release())} lang/js md/nodejs#${IDE_NODE} api/kiroruntime#1.0.0 m/N ${IDE_APP}`
|
|
1349
|
+
|
|
1350
|
+
// sdkHeaders are the AWS SDK's headers on a call to Kiro's runtime, as the
|
|
1351
|
+
// Kiro IDE's client sends them: who is calling, and as which client. The
|
|
1352
|
+
// IDE names a Builder ID or Identity Center token's kind (SSO_OIDC) too.
|
|
1307
1353
|
function sdkHeaders(a) {
|
|
1308
|
-
const
|
|
1309
|
-
return {
|
|
1354
|
+
const h = {
|
|
1310
1355
|
...authHeaders(a),
|
|
1311
1356
|
"amz-sdk-invocation-id": randomUUID(),
|
|
1312
|
-
"amz-sdk-request": "attempt=1; max=
|
|
1313
|
-
"x-amz-user-agent":
|
|
1314
|
-
"User-Agent":
|
|
1357
|
+
"amz-sdk-request": "attempt=1; max=3",
|
|
1358
|
+
"x-amz-user-agent": `aws-sdk-js/1.0.0 ${IDE_APP}`,
|
|
1359
|
+
"User-Agent": IDE_SDK_UA,
|
|
1360
|
+
"x-amzn-kiro-client-attribution": "kiro-ide",
|
|
1315
1361
|
}
|
|
1362
|
+
if (!a.tokenType && a.idc) h.tokentype = "SSO_OIDC"
|
|
1363
|
+
return h
|
|
1316
1364
|
}
|
|
1317
1365
|
|
|
1318
1366
|
function sendKiro(a, body, signal) {
|
|
1319
1367
|
return fetch(runtime(a.region) + "/generateAssistantResponse", {
|
|
1320
1368
|
method: "POST",
|
|
1321
|
-
headers: { ...sdkHeaders(a), "x-amzn-codewhisperer-optout": "true", "x-amzn-kiro-agent-mode": "vibe",
|
|
1369
|
+
headers: { ...sdkHeaders(a), "x-amzn-kiro-attempt": "1;max=3", "x-amzn-codewhisperer-optout": "true", "x-amzn-kiro-agent-mode": "vibe",
|
|
1322
1370
|
"Content-Type": "application/json", Accept: "application/vnd.amazon.eventstream" },
|
|
1323
1371
|
body,
|
|
1324
1372
|
signal,
|
|
@@ -1337,7 +1385,7 @@ const TOO_LONG = "input is too long for the model's context: "
|
|
|
1337
1385
|
|
|
1338
1386
|
// failure is the status and message for an error Kiro answered with:
|
|
1339
1387
|
// {"message": "...", "reason": "..."}.
|
|
1340
|
-
function failure(status, body) {
|
|
1388
|
+
function failure(status, body, id) {
|
|
1341
1389
|
let msg = String(body ?? "").trim()
|
|
1342
1390
|
try {
|
|
1343
1391
|
const e = JSON.parse(body)
|
|
@@ -1347,9 +1395,33 @@ function failure(status, body) {
|
|
|
1347
1395
|
if (tooLongSaid(msg)) return { status: 400, message: TOO_LONG + msg }
|
|
1348
1396
|
if (msg.includes("INSUFFICIENT_MODEL_CAPACITY")) return { status: 503, message: msg }
|
|
1349
1397
|
if (msg.includes("MONTHLY_REQUEST_COUNT") || msg.includes("USAGE_LIMIT")) return { status: 429, message: "usage limit reached: " + msg }
|
|
1398
|
+
if (msg.includes(USER_RATE)) return { status: 429, message: userRate(msg, id) }
|
|
1350
1399
|
return { status, message: msg }
|
|
1351
1400
|
}
|
|
1352
1401
|
|
|
1402
|
+
// USER_RATE is the reason Kiro gives a 429 for one account's requests
|
|
1403
|
+
// coming too often (the Kiro IDE's ThrottlingException reasons, agent
|
|
1404
|
+
// 1.1.499: apart from the credit rate, the service's rate and the daily
|
|
1405
|
+
// and monthly counts). Kiro's own words, "Too many requests, please wait
|
|
1406
|
+
// before trying again.", say neither whose limit it is nor how long:
|
|
1407
|
+
// onemezao on Discord had it on every request of one account through pi,
|
|
1408
|
+
// 42s each, while the Kiro IDE answered on the same account.
|
|
1409
|
+
const USER_RATE = "USER_REQUEST_RATE_EXCEEDED"
|
|
1410
|
+
// USER_RATE_WAIT is the wait a USER_RATE refusal says when Kiro says none:
|
|
1411
|
+
// longer than magpie's same-account retries (8s), so the request isn't
|
|
1412
|
+
// sent again at 1, 2 and 4s, each try one more on the account's rate.
|
|
1413
|
+
// Other Kiro relays pause 3 to 12s for it; one that outlived those
|
|
1414
|
+
// wasn't going to clear in magpie's next few.
|
|
1415
|
+
const USER_RATE_WAIT = 30_000
|
|
1416
|
+
|
|
1417
|
+
// userRate is a USER_RATE refusal in plain words: whose limit, what to
|
|
1418
|
+
// do, and the request ID Kiro support asks for.
|
|
1419
|
+
const userRate = (msg, id) =>
|
|
1420
|
+
`rate limited: Kiro is limiting how often this Kiro account sends requests (${msg}). ` +
|
|
1421
|
+
"Wait a minute before sending again. If it keeps coming back while the Kiro app works on the same account, " +
|
|
1422
|
+
"the limit is on the account, and Kiro support (app.kiro.dev) can look at it" +
|
|
1423
|
+
(id ? `, with request ID ${id}.` : ".")
|
|
1424
|
+
|
|
1353
1425
|
const ERROR_TYPES = { 400: "invalid_request_error", 401: "authentication_error", 402: "billing_error", 403: "permission_error",
|
|
1354
1426
|
404: "not_found_error", 413: "request_too_large", 429: "rate_limit_error", 503: "overloaded_error", 529: "overloaded_error" }
|
|
1355
1427
|
const errorType = (status) => ERROR_TYPES[status] ?? "api_error"
|
|
@@ -1363,12 +1435,17 @@ const errorResponse = (status, message, wait) =>
|
|
|
1363
1435
|
...(wait > 0 ? { "Retry-After": String(Math.ceil(wait / 1000)) } : {}) },
|
|
1364
1436
|
})
|
|
1365
1437
|
|
|
1438
|
+
// requestID is the ID Kiro gave its answer, which its support asks for:
|
|
1439
|
+
// what the Kiro IDE reads (x-amzn-requestid, else x-request-id).
|
|
1440
|
+
const requestID = (headers) => headers.get("x-amzn-requestid") || headers.get("x-request-id") || ""
|
|
1441
|
+
|
|
1366
1442
|
// waitOf is the milliseconds Kiro's refusal says to wait before asking
|
|
1367
1443
|
// again, or undefined: what Kiro's own clients read. The Kiro IDE (agent
|
|
1368
1444
|
// 1.1.499) reads the x-amzn-kiro-ratelimit-retry-after header and the
|
|
1369
1445
|
// body's retryAfterMilliseconds, both in milliseconds; kiro-cli's AWS SDK
|
|
1370
1446
|
// 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
|
|
1447
|
+
// seconds or as a date. Unsaid, magpie keeps to its own pauses, but for
|
|
1448
|
+
// an account's request rate (USER_RATE_WAIT).
|
|
1372
1449
|
function waitOf(headers, body) {
|
|
1373
1450
|
const ms = (v) => {
|
|
1374
1451
|
const n = v == null || v === "" ? NaN : Number(v)
|
|
@@ -1384,6 +1461,7 @@ function waitOf(headers, body) {
|
|
|
1384
1461
|
const secs = ms(ra)
|
|
1385
1462
|
w = secs !== undefined ? secs * 1000 : ms(Date.parse(ra) - Date.now())
|
|
1386
1463
|
}
|
|
1464
|
+
if (w === undefined && String(body ?? "").includes(USER_RATE)) w = USER_RATE_WAIT
|
|
1387
1465
|
return w
|
|
1388
1466
|
}
|
|
1389
1467
|
|
|
@@ -1464,6 +1542,7 @@ function exception(kind, payload) {
|
|
|
1464
1542
|
} catch {}
|
|
1465
1543
|
let msg = e.message || e.Message || new TextDecoder().decode(payload).trim()
|
|
1466
1544
|
if (e.reason) msg += ` (${e.reason})`
|
|
1545
|
+
if (e.reason === USER_RATE) return userRate(msg)
|
|
1467
1546
|
if (String(kind).toLowerCase().includes("throttl")) return "rate limited: " + msg
|
|
1468
1547
|
return kind ? `${kind}: ${msg}` : msg
|
|
1469
1548
|
}
|
|
@@ -1569,10 +1648,13 @@ async function* events(body, model, budget) {
|
|
|
1569
1648
|
if (budget) yield* think.feed(text)
|
|
1570
1649
|
else if (text) yield { kind: "text", text }
|
|
1571
1650
|
} else if (kind === "reasoningContentEvent") {
|
|
1651
|
+
// one event may carry the reasoning and its signature together (auto,
|
|
1652
|
+
// called as the IDE): the signature goes on the block it ends
|
|
1572
1653
|
if (m.text) {
|
|
1573
1654
|
said += m.text.length
|
|
1574
1655
|
yield { kind: "think", text: m.text }
|
|
1575
|
-
}
|
|
1656
|
+
}
|
|
1657
|
+
if (m.signature) yield { kind: "sig", text: m.signature }
|
|
1576
1658
|
} else if (kind === "toolUseEvent") {
|
|
1577
1659
|
if (m.toolUseId && m.toolUseId !== tool) {
|
|
1578
1660
|
yield* think.end() // the text before a call is all said
|
|
@@ -1680,7 +1762,7 @@ const QUOTA_WORDS = /quota|insufficient|balance|credit|billing|exceeded|rate.?li
|
|
|
1680
1762
|
// error, so it never compacted (ergou0954_54426 on Discord: Kiro in a
|
|
1681
1763
|
// group routed by intent, "超出上下文", the turn failed).
|
|
1682
1764
|
const failedBefore = (text) =>
|
|
1683
|
-
tooLongSaid(text) ? errorResponse(400, TOO_LONG + text) : errorResponse(QUOTA_WORDS.test(text) ? 429 : 502, text)
|
|
1765
|
+
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
1766
|
|
|
1685
1767
|
// reply answers the Messages request from Kiro's events: a stream of
|
|
1686
1768
|
// Messages' events, or one message. names gives back the caller's name of
|
|
@@ -2021,20 +2103,20 @@ function searchAnswer(text, hosted) {
|
|
|
2021
2103
|
// refreshed, when Kiro turns the token down (403). As the built-in: no
|
|
2022
2104
|
// credentials is a 401, a failed refresh after the 403 a 502; none marks
|
|
2023
2105
|
// the account.
|
|
2024
|
-
async function generate(creds, auth, req, signal) {
|
|
2106
|
+
async function generate(creds, auth, req, signal, conversation) {
|
|
2025
2107
|
const model = String(req.model ?? "auto")
|
|
2026
2108
|
const budget = thinking(req, model)
|
|
2027
2109
|
const hosted = hostedSearch(req)
|
|
2028
2110
|
if (hosted?.error) return errorResponse(400, hosted.error)
|
|
2029
|
-
const first = await ask(creds, auth, req, model, budget, signal)
|
|
2111
|
+
const first = await ask(creds, auth, req, model, budget, signal, conversation)
|
|
2030
2112
|
if (!first.res) return errorResponse(first.status, first.message, first.wait)
|
|
2031
|
-
const it = hosted ? searching(creds, auth, req, model, budget, signal, hosted, first) : events(first.res.body, model, budget)
|
|
2113
|
+
const it = hosted ? searching(creds, auth, req, model, budget, signal, hosted, first, conversation) : events(first.res.body, model, budget)
|
|
2032
2114
|
return kept(await reply(it, model, req.stream === true, toolNames(req)))
|
|
2033
2115
|
}
|
|
2034
2116
|
|
|
2035
2117
|
// ask sends one turn to Kiro: { res, a }, a 200, or the { status, message }
|
|
2036
2118
|
// of a failure.
|
|
2037
|
-
async function ask(creds, auth, req, model, budget, signal) {
|
|
2119
|
+
async function ask(creds, auth, req, model, budget, signal, conversation) {
|
|
2038
2120
|
let a
|
|
2039
2121
|
try {
|
|
2040
2122
|
a = await creds(auth)
|
|
@@ -2043,7 +2125,7 @@ async function ask(creds, auth, req, model, budget, signal) {
|
|
|
2043
2125
|
}
|
|
2044
2126
|
let res
|
|
2045
2127
|
try {
|
|
2046
|
-
res = await sendKiro(a, await kiroBody(req, model, a.profile, budget), signal)
|
|
2128
|
+
res = await sendKiro(a, await kiroBody(req, model, a.profile, budget, conversation), signal)
|
|
2047
2129
|
} catch (e) {
|
|
2048
2130
|
if (e?.name === "AbortError") throw e
|
|
2049
2131
|
return { status: 502, message: e.message }
|
|
@@ -2056,7 +2138,7 @@ async function ask(creds, auth, req, model, budget, signal) {
|
|
|
2056
2138
|
return { status: 502, message: e.message }
|
|
2057
2139
|
}
|
|
2058
2140
|
try {
|
|
2059
|
-
res = await sendKiro(a, await kiroBody(req, model, a.profile, budget), signal)
|
|
2141
|
+
res = await sendKiro(a, await kiroBody(req, model, a.profile, budget, conversation), signal)
|
|
2060
2142
|
} catch (e) {
|
|
2061
2143
|
if (e?.name === "AbortError") throw e
|
|
2062
2144
|
return { status: 502, message: e.message }
|
|
@@ -2068,7 +2150,7 @@ async function ask(creds, auth, req, model, budget, signal) {
|
|
|
2068
2150
|
const tried = new Set()
|
|
2069
2151
|
while (!res.ok && a.key && creds.spend) {
|
|
2070
2152
|
const text = (await res.text()).slice(0, 1 << 20)
|
|
2071
|
-
const f = { ...failure(res.status, text), wait: waitOf(res.headers, text) }
|
|
2153
|
+
const f = { ...failure(res.status, text, requestID(res.headers)), wait: waitOf(res.headers, text) }
|
|
2072
2154
|
if (!usedUpSaid(f.message)) return f
|
|
2073
2155
|
tried.add(a.region)
|
|
2074
2156
|
creds.spend(a.key, a.region)
|
|
@@ -2081,7 +2163,7 @@ async function ask(creds, auth, req, model, budget, signal) {
|
|
|
2081
2163
|
if (tried.has(next.region)) return { status: 429, message: regionsUsedUp(next) + f.message, wait: f.wait }
|
|
2082
2164
|
a = next
|
|
2083
2165
|
try {
|
|
2084
|
-
res = await sendKiro(a, await kiroBody(req, model, a.profile, budget), signal)
|
|
2166
|
+
res = await sendKiro(a, await kiroBody(req, model, a.profile, budget, conversation), signal)
|
|
2085
2167
|
} catch (e) {
|
|
2086
2168
|
if (e?.name === "AbortError") throw e
|
|
2087
2169
|
return { status: 502, message: e.message }
|
|
@@ -2090,7 +2172,7 @@ async function ask(creds, auth, req, model, budget, signal) {
|
|
|
2090
2172
|
if (!res.ok) {
|
|
2091
2173
|
// Kiro's own 401 is passed on, as the built-in did, the sign-in kept
|
|
2092
2174
|
const text = (await res.text()).slice(0, 1 << 20)
|
|
2093
|
-
return { ...failure(res.status, text), wait: waitOf(res.headers, text) }
|
|
2175
|
+
return { ...failure(res.status, text, requestID(res.headers)), wait: waitOf(res.headers, text) }
|
|
2094
2176
|
}
|
|
2095
2177
|
return { res, a }
|
|
2096
2178
|
}
|
|
@@ -2111,7 +2193,7 @@ const aborted = () => Object.assign(new Error("the request was cancelled"), { na
|
|
|
2111
2193
|
// caller's (passed on as ever, the searches of its turn with it), or has
|
|
2112
2194
|
// taken hosted.uses + 2 turns (pause_turn, which the caller may send back
|
|
2113
2195
|
// to go on). Searches past hosted.uses are max_uses_exceeded.
|
|
2114
|
-
async function* searching(creds, auth, req, model, budget, signal, hosted, first) {
|
|
2196
|
+
async function* searching(creds, auth, req, model, budget, signal, hosted, first, conversation) {
|
|
2115
2197
|
let { res, a } = first
|
|
2116
2198
|
const messages = [...(req.messages ?? [])]
|
|
2117
2199
|
const usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reported: false, searches: 0 }
|
|
@@ -2223,7 +2305,7 @@ async function* searching(creds, auth, req, model, budget, signal, hosted, first
|
|
|
2223
2305
|
return
|
|
2224
2306
|
}
|
|
2225
2307
|
messages.push({ role: "assistant", content: said })
|
|
2226
|
-
const next = await ask(creds, auth, { ...req, messages }, model, budget, signal)
|
|
2308
|
+
const next = await ask(creds, auth, { ...req, messages }, model, budget, signal, conversation)
|
|
2227
2309
|
if (!next.res) {
|
|
2228
2310
|
yield { kind: "error", status: next.status, text: `Kiro, after its web search: ${next.message} (${next.status})` }
|
|
2229
2311
|
return
|
|
@@ -2567,7 +2649,7 @@ export async function KiroAuthPlugin({ client } = {}) {
|
|
|
2567
2649
|
} catch {
|
|
2568
2650
|
return errorResponse(400, "a request that isn't JSON")
|
|
2569
2651
|
}
|
|
2570
|
-
return generate(creds, (await getAuth()) ?? auth, req, init.signal)
|
|
2652
|
+
return generate(creds, (await getAuth()) ?? auth, req, init.signal, sessionOf(input, init))
|
|
2571
2653
|
},
|
|
2572
2654
|
}
|
|
2573
2655
|
},
|