Skip to content

Commit 70d3787

Browse files
committed
Try to fix some timeout errors
1 parent ab065a3 commit 70d3787

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

web/src/llm-api/openrouter.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Agent } from 'undici'
2+
13
import { PROFIT_MARGIN } from '@codebuff/common/constants/limits'
24
import { getErrorObject } from '@codebuff/common/util/error'
35
import { env } from '@codebuff/internal/env'
@@ -23,6 +25,15 @@ import type {
2325

2426
type StreamState = { responseText: string; reasoningText: string }
2527

28+
// Extended timeout for deep-thinking models (e.g., gpt-5) that can take
29+
// a long time to start streaming.
30+
const OPENROUTER_HEADERS_TIMEOUT_MS = 10 * 60 * 1000
31+
32+
const openrouterAgent = new Agent({
33+
headersTimeout: OPENROUTER_HEADERS_TIMEOUT_MS,
34+
bodyTimeout: 0, // No body timeout for streaming responses
35+
})
36+
2637
/** Result from processing a line, including optional billed credits for final chunk */
2738
type LineResult = {
2839
state: StreamState
@@ -44,6 +55,9 @@ function createOpenRouterRequest(params: {
4455
'Content-Type': 'application/json',
4556
},
4657
body: JSON.stringify(body),
58+
// Use custom agent with extended headers timeout for deep-thinking models
59+
// @ts-expect-error - dispatcher is a valid undici option not in fetch types
60+
dispatcher: openrouterAgent,
4761
})
4862
}
4963

0 commit comments

Comments
 (0)