Skip to content

Commit 71d08e9

Browse files
fix(opencode): retry xAI capacity stream errors (anomalyco#43640)
Co-authored-by: rekram1-node <[email protected]>
1 parent b155b15 commit 71d08e9

4 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎packages/opencode/src/provider/error.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ export function parseStreamError(input: unknown): ParsedStreamError | undefined
144144
responseBody,
145145
}
146146
}
147+
148+
return {
149+
type: "api_error",
150+
message: typeof body?.error?.message === "string" ? body.error.message : "Server error.",
151+
isRetryable: true,
152+
responseBody,
153+
}
147154
}
148155

149156
export type ParsedAPICallError =

‎packages/opencode/src/session/retry.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const RETRYABLE_MESSAGE_PATTERNS = [
3737
/terminated|fetch failed|failed to fetch|network error|upstream connect|connection error|connection refused|connection lost|socket connection was closed|socket hang up|reset before headers|getaddrinfo|enotfound|eai_again|econnrefused|econnreset|etimedout/i,
3838
/^timeout$|\b(?:request|response|connection|network|stream|read) (?:timeout|timed out|time out)\b/i,
3939
/try your request again|retry your request|resource exhausted|resource_exhausted/i,
40+
/\btry again (?:later|in\b)|\b(?:currently|temporarily) at capacity\b/i,
4041
]
4142

4243
function cap(ms: number) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { ProviderError } from "@/provider/error"
3+
4+
describe("provider stream errors", () => {
5+
test("retries provider stream errors without a code", () => {
6+
const messages = [
7+
"The model is currently at capacity due to high demand. Please try again in a few minutes, or use a higher service tier for priority processing: https://docs.x.ai/developers/advanced-api-usage/priority-processing",
8+
"The model is temporarily unavailable.",
9+
]
10+
11+
for (const message of messages)
12+
expect(
13+
ProviderError.parseStreamError({
14+
type: "error",
15+
error: { message },
16+
}),
17+
).toEqual({
18+
type: "api_error",
19+
message,
20+
isRetryable: true,
21+
responseBody: JSON.stringify({ type: "error", error: { message } }),
22+
})
23+
})
24+
})

‎packages/opencode/test/session/retry.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ describe("session.retry.retryable", () => {
215215
"response timed out",
216216
"Please retry your request",
217217
"try your request again",
218+
"Please try again in a few minutes",
219+
"The model is currently at capacity due to high demand",
220+
"The service is temporarily at capacity",
218221
"upstream returned status 524",
219222
])("retries matching API error text: %s", (message) => {
220223
expect(SessionRetry.retryable(wrap(message), retryProvider)).toEqual({ message })

0 commit comments

Comments
 (0)