diff --git a/packages/kiro/README.md b/packages/kiro/README.md index e1c644c..bfe4d92 100644 --- a/packages/kiro/README.md +++ b/packages/kiro/README.md @@ -152,7 +152,10 @@ request: - Anthropic's hosted web search is answered with Kiro's own web search (see [Web search](#web-search)). - Kiro's failures keep their meaning: - - input too long → 400 + - input too long → 400 "input is too long for the model's context", whether + Kiro says it as an HTTP answer, as an exception in the stream, or by + stopping with `MODEL_CONTEXT_WINDOW_EXCEEDED` before a word (after some + of the reply, it ends with stop reason `model_context_window_exceeded`) - no capacity → 503 - usage limit → 429 - throttling → "rate limited" diff --git a/packages/kiro/errors.test.mjs b/packages/kiro/errors.test.mjs index 4f74412..d8864e3 100644 --- a/packages/kiro/errors.test.mjs +++ b/packages/kiro/errors.test.mjs @@ -66,9 +66,62 @@ test("a quota exception before the answer is a 429", async () => { }) test("another exception before the answer is a 502, not a stream", async () => { - const res = await ask(stream(exception("ValidationException", "Input is too long.")), true) + const res = await ask(stream(exception("InternalServerException", "Encountered an unexpected error")), true) expect(res.status).toBe(502) - expect((await res.json()).error.message).toBe("ValidationException: Input is too long.") + expect((await res.json()).error.message).toBe("InternalServerException: Encountered an unexpected error") +}) + +// magpie's gateway (tooLong) takes a 400 with these words for a conversation +// past the model's window: another member with room is asked, no account +// rests, and a lone agent is told to compact. As a 502 the Kiro account +// rested, was asked again, and the agent got a vendor error it never +// compacted for (ergou0954_54426 on Discord: "kiro 那边报错说超出上下文"). +const saysTooLong = (m) => /^input is too long for the model's context: /.test(m) + +test("Kiro's too-long exception in the stream is a 400 that says too long, streamed or not", async () => { + for (const [kind, msg] of [ + ["ValidationException", "Input is too long."], + ["ValidationException", "Improperly formed request. (CONTENT_LENGTH_EXCEEDS_THRESHOLD)"], + ]) { + for (const s of [true, false]) { + const res = await ask(stream(exception(kind, msg)), s) + expect(res.status).toBe(400) + const b = await res.json() + expect(b.error.type).toBe("invalid_request_error") + expect(saysTooLong(b.error.message)).toBe(true) + expect(b.error.message).toContain(msg) + } + } +}) + +test("a reply Kiro stops for a full window before a word is a 400 that says too long", async () => { + const full = () => stream(frame({ ":message-type": "event", ":event-type": "metadataEvent" }, { stopReason: "MODEL_CONTEXT_WINDOW_EXCEEDED" })) + for (const s of [true, false]) { + const res = await ask(full(), s) + expect(res.status).toBe(400) + expect(saysTooLong((await res.json()).error.message)).toBe(true) + } +}) + +test("a reply Kiro stops for a full window after some of it says it was cut short", async () => { + const cut = () => + stream(text("Half an ans"), frame({ ":message-type": "event", ":event-type": "metadataEvent" }, { stopReason: "MODEL_CONTEXT_WINDOW_EXCEEDED" })) + const res = await ask(cut(), false) + expect(res.status).toBe(200) + expect((await res.json()).stop_reason).toBe("model_context_window_exceeded") + const sse = await (await ask(cut(), true)).text() + expect(sse).toContain('"stop_reason":"model_context_window_exceeded"') +}) + +test("Kiro's HTTP 400 for a too-long input stays a 400 that says too long", () => { + const f = failure(400, JSON.stringify({ message: "Input is too long.", reason: "CONTENT_LENGTH_EXCEEDS_THRESHOLD" })) + expect(f.status).toBe(400) + expect(saysTooLong(f.message)).toBe(true) +}) + +test("a quota or throttle still isn't taken for too long", async () => { + expect((await ask(stream(exception("ServiceQuotaExceededException", "You have reached the limit")), true)).status).toBe(429) + expect((await ask(stream(exception("ThrottlingException", "Too many requests")), true)).status).toBe(429) }) test("an exception after the answer began ends the stream with an error event", async () => { diff --git a/packages/kiro/index.mjs b/packages/kiro/index.mjs index 6e8303c..7851c4a 100644 --- a/packages/kiro/index.mjs +++ b/packages/kiro/index.mjs @@ -1155,6 +1155,16 @@ function sendKiro(a, body, signal) { }) } +// tooLongSaid is whether Kiro's words say the conversation is past the +// model's window: its ValidationException "Input is too long." with reason +// CONTENT_LENGTH_EXCEEDS_THRESHOLD, as an HTTP answer or as an exception +// in the event stream, or a reply it stopped with MODEL_CONTEXT_WINDOW_EXCEEDED. +const tooLongSaid = (msg) => /CONTENT_LENGTH_EXCEEDS_THRESHOLD|MODEL_CONTEXT_WINDOW_EXCEEDED|input is too long/i.test(msg) +// TOO_LONG heads a too-long failure in the words magpie's gateway takes +// for one (tooLong): another member with a larger window is asked, no +// account rests, and with none left the agent is told to compact. +const TOO_LONG = "input is too long for the model's context: " + // failure is the status and message for an error Kiro answered with: // {"message": "...", "reason": "..."}. function failure(status, body) { @@ -1164,8 +1174,7 @@ function failure(status, body) { if (e?.message) msg = e.message + (e.reason ? ` (${e.reason})` : "") } catch {} msg ||= STATUS_TEXT[status] ?? "" - if (msg.includes("CONTENT_LENGTH_EXCEEDS_THRESHOLD") || msg.toLowerCase().includes("input is too long")) - return { status: 400, message: "input is too long for the model's context: " + msg } + if (tooLongSaid(msg)) return { status: 400, message: TOO_LONG + msg } if (msg.includes("INSUFFICIENT_MODEL_CAPACITY")) return { status: 503, message: msg } if (msg.includes("MONTHLY_REQUEST_COUNT") || msg.includes("USAGE_LIMIT")) return { status: 429, message: "usage limit reached: " + msg } return { status, message: msg } @@ -1413,8 +1422,15 @@ async function* events(body, model, budget) { // Kiro says how full the context is rather than how many tokens if (!usage.input && pct > 0 && window > 0) usage.input = Math.floor((pct / 100) * window) if (!usage.output) usage.output = Math.floor((said + 3) / 4) + const full = /^model_context_window_exceeded$/i.test(stop) + if (full && !said && !tools) { + // stopped for the window before a word: the conversation doesn't fit + yield { kind: "error", text: "the reply stopped as the model's context window was full (MODEL_CONTEXT_WINDOW_EXCEEDED)" } + return + } let reason = "end_turn" if (tools > 0) reason = "tool_use" + else if (full) reason = "model_context_window_exceeded" // cut short, as Anthropic's API says it else if (/^max_tokens$/i.test(stop)) reason = "max_tokens" else if (/^content_filtered$/i.test(stop)) reason = "refusal" yield { kind: "stop", reason, usage } @@ -1454,7 +1470,14 @@ const QUOTA_WORDS = /quota|insufficient|balance|credit|billing|exceeded|rate.?li // magpie's built-in relay gives it: a status, not a stream, so another // account can take over — a 429 when it reads as a quota or rate limit, // else a 502. -const failedBefore = (text) => errorResponse(QUOTA_WORDS.test(text) ? 429 : 502, text) +// +// A conversation past the model's window is a 400 that says so, whichever +// way Kiro said it, before the quota words ("exceeded"): as a 502, magpie +// rested the account, asked it again, and told a lone agent of a vendor +// error, so it never compacted (ergou0954_54426 on Discord: Kiro in a +// group routed by intent, "超出上下文", the turn failed). +const failedBefore = (text) => + tooLongSaid(text) ? errorResponse(400, TOO_LONG + text) : errorResponse(QUOTA_WORDS.test(text) ? 429 : 502, text) // reply answers the Messages request from Kiro's events: a stream of // Messages' events, or one message. names gives back the caller's name of @@ -1887,8 +1910,10 @@ async function* searching(creds, auth, req, model, budget, signal, hosted, first else said.push({ type: "text", text: e.text }) } if (e.kind === "error") { - // in a turn after a search, a status, not a finished answer (reply) - yield turn > 1 ? { ...e, status: 502 } : e + // in a turn after a search, a status, not a finished answer + // (reply): one past the window says so, as failedBefore does + if (turn > 1 && tooLongSaid(e.text)) yield { ...e, status: 400, text: TOO_LONG + e.text } + else yield turn > 1 ? { ...e, status: 502 } : e return } yield e diff --git a/packages/kiro/package.json b/packages/kiro/package.json index df42a1c..32ce75e 100644 --- a/packages/kiro/package.json +++ b/packages/kiro/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-kiro-auth", - "version": "0.1.23", + "version": "0.1.24", "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie", "type": "module", "main": "./index.mjs",