Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/opencode/src/provider/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,27 @@ export type ParsedStreamError =
responseBody: string
}

// OpenRouter relays upstream overflow as a typeless `{ code: 502, message }` chunk.
function isOverflow(body: { error?: { code?: string; message?: string }; message?: string }) {
return body.error?.code === "context_length_exceeded" || isContextOverflow(body.error?.message ?? body.message ?? "")
}

export function parseStreamError(input: unknown): ParsedStreamError | undefined {
const raw = json(input)
const body = typeof raw?.message === "string" ? (json(raw.message) ?? raw) : raw
if (!body) return

const responseBody = JSON.stringify(body)
if (isOverflow(body)) {
return {
type: "context_overflow",
message: "Input exceeds context window of this model",
responseBody,
}
}
if (body.type !== "error") return

switch (body?.error?.code) {
case "context_length_exceeded":
return {
type: "context_overflow",
message: "Input exceeds context window of this model",
responseBody,
}
case "insufficient_quota":
return {
type: "api_error",
Expand Down
17 changes: 17 additions & 0 deletions packages/opencode/test/session/message-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,23 @@ describe("session.message-v2.fromError", () => {
})
})

test("serializes OpenRouter typeless overflow chunks as ContextOverflowError", () => {
const input = {
code: 502,
message: "Your input exceeds the context window of this model. Please adjust your input and try again.",
metadata: { error_type: "provider_unavailable" },
}
const result = MessageV2.fromError(input, { providerID })

expect(result).toStrictEqual({
name: "ContextOverflowError",
data: {
message: "Input exceeds context window of this model",
responseBody: JSON.stringify(input),
},
})
})

test("serializes response error codes", () => {
const cases = [
{
Expand Down
Loading