Skip to content

Commit b04d363

Browse files
committed
refactor: consolidate timeout logic
1 parent 147ec36 commit b04d363

File tree

14 files changed

+30
-111
lines changed

14 files changed

+30
-111
lines changed

src/api/providers/anthropic.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,7 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
3131
const apiKeyFieldName =
3232
this.options.anthropicBaseUrl && this.options.anthropicUseAuthToken ? "authToken" : "apiKey"
3333

34-
let timeout = getApiRequestTimeout()
35-
36-
// match behaviour with other SDK where 0 means no timeout instead of instantly timing out
37-
if (timeout === 0) {
38-
// Use 2147483647 (2^31 - 1) as the maximum timeout value for setTimeout
39-
// JavaScript's setTimeout has a maximum delay limit of 2147483647ms (32-bit signed integer max)
40-
// Values larger than this may be clamped to 1ms or cause unexpected behavior
41-
// 2147483647 is the safe maximum value that won't cause issues
42-
timeout = 2147483647
43-
}
34+
const timeout = getApiRequestTimeout()
4435

4536
this.client = new Anthropic({
4637
baseURL: this.options.anthropicBaseUrl || undefined,

src/api/providers/base-openai-compatible-provider.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,7 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
5757
throw new Error("API key is required")
5858
}
5959

60-
let timeout = getApiRequestTimeout()
61-
// match behaviour with other SDK where 0 means no timeout instead of instantly timing out
62-
if (timeout === 0) {
63-
// Use 2147483647 (2^31 - 1) as the maximum timeout value for setTimeout
64-
// JavaScript's setTimeout has a maximum delay limit of 2147483647ms (32-bit signed integer max)
65-
// Values larger than this may be clamped to 1ms or cause unexpected behavior
66-
// 2147483647 is the safe maximum value that won't cause issues
67-
timeout = 2147483647
68-
}
60+
const timeout = getApiRequestTimeout()
6961

7062
this.client = new OpenAI({
7163
baseURL,

src/api/providers/huggingface.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,7 @@ export class HuggingFaceHandler extends BaseProvider implements SingleCompletion
2525
throw new Error("Hugging Face API key is required")
2626
}
2727

28-
let timeout = getApiRequestTimeout()
29-
if (timeout === 0) {
30-
// Use 2147483647 (2^31 - 1) as the maximum timeout value for setTimeout
31-
// JavaScript's setTimeout has a maximum delay limit of 2147483647ms (32-bit signed integer max)
32-
// Values larger than this may be clamped to 1ms or cause unexpected behavior
33-
// 2147483647 is the safe maximum value that won't cause issues
34-
timeout = 2147483647
35-
}
28+
const timeout = getApiRequestTimeout()
3629

3730
this.client = new OpenAI({
3831
baseURL: "https://router.huggingface.co/v1",

src/api/providers/lm-studio.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,7 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
2929
// LM Studio uses "noop" as a placeholder API key
3030
const apiKey = "noop"
3131

32-
let timeout = getApiRequestTimeout()
33-
34-
// match behaviour with other SDK where 0 means no timeout instead of instantly timing out
35-
if (timeout === 0) {
36-
// Use 2147483647 (2^31 - 1) as the maximum timeout value for setTimeout
37-
// JavaScript's setTimeout has a maximum delay limit of 2147483647ms (32-bit signed integer max)
38-
// Values larger than this may be clamped to 1ms or cause unexpected behavior
39-
// 2147483647 is the safe maximum value that won't cause issues
40-
timeout = 2147483647
41-
}
32+
const timeout = getApiRequestTimeout()
4233

4334
this.client = new OpenAI({
4435
baseURL: (this.options.lmStudioBaseUrl || "http://localhost:1234") + "/v1",

src/api/providers/ollama.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,7 @@ export class OllamaHandler extends BaseProvider implements SingleCompletionHandl
3636
headers["Authorization"] = `Bearer ${this.options.ollamaApiKey}`
3737
}
3838

39-
let timeout = getApiRequestTimeout()
40-
41-
// match behaviour with other SDK where 0 means no timeout instead of instantly timing out
42-
if (timeout === 0) {
43-
// Use 2147483647 (2^31 - 1) as the maximum timeout value for setTimeout
44-
// JavaScript's setTimeout has a maximum delay limit of 2147483647ms (32-bit signed integer max)
45-
// Values larger than this may be clamped to 1ms or cause unexpected behavior
46-
// 2147483647 is the safe maximum value that won't cause issues
47-
timeout = 2147483647
48-
}
39+
const timeout = getApiRequestTimeout()
4940

5041
this.client = new OpenAI({
5142
baseURL: (this.options.ollamaBaseUrl || "http://localhost:11434") + "/v1",

src/api/providers/openai-native.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,7 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
6464
}
6565
const apiKey = this.options.openAiNativeApiKey ?? "not-provided"
6666
{
67-
let timeout = getApiRequestTimeout()
68-
if (timeout === 0) {
69-
// Use 2147483647 (2^31) as the maximum timeout value for setTimeout
70-
// JavaScript's setTimeout has a maximum delay limit of 2147483647ms (32-bit signed integer max)
71-
// Values larger than this may be clamped to 1ms or cause unexpected behavior
72-
// 2147483647 is safe as it's just above the limit but won't cause issues
73-
timeout = 2147483647
74-
}
67+
const timeout = getApiRequestTimeout()
7568
this.client = new OpenAI({ baseURL: this.options.openAiNativeBaseUrl, apiKey, timeout })
7669
}
7770
}

src/api/providers/openai.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
4949
...(this.options.openAiHeaders || {}),
5050
}
5151

52-
let timeout = getApiRequestTimeout()
53-
54-
// match behaviour with other SDK where 0 means no timeout instead of instantly timing out
55-
if (timeout === 0) {
56-
// Use 2147483647 (2^31 - 1) as the maximum timeout value for setTimeout
57-
// JavaScript's setTimeout has a maximum delay limit of 2147483647ms (32-bit signed integer max)
58-
// Values larger than this may be clamped to 1ms or cause unexpected behavior
59-
// 2147483647 is the safe maximum value that won't cause issues
60-
timeout = 2147483647
61-
}
52+
const timeout = getApiRequestTimeout()
6253

6354
if (isAzureAiInference) {
6455
// Azure AI Inference Service (e.g., for DeepSeek) uses a different path structure

src/api/providers/openrouter.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,7 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
9696
const baseURL = this.options.openRouterBaseUrl || "https://openrouter.ai/api/v1"
9797
const apiKey = this.options.openRouterApiKey ?? "not-provided"
9898

99-
let timeout = getApiRequestTimeout()
100-
if (timeout === 0) {
101-
// Use 2147483647 (2^31 - 1) as the maximum timeout value for setTimeout
102-
// JavaScript's setTimeout has a maximum delay limit of 2147483647ms (32-bit signed integer max)
103-
// Values larger than this may be clamped to 1ms or cause unexpected behavior
104-
// 2147483647 is the safe maximum value that won't cause issues
105-
timeout = 2147483647
106-
}
99+
const timeout = getApiRequestTimeout()
107100
this.client = new OpenAI({ baseURL, apiKey, defaultHeaders: DEFAULT_HEADERS, timeout })
108101
}
109102

src/api/providers/qwen-code.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,7 @@ export class QwenCodeHandler extends BaseProvider implements SingleCompletionHan
6565
if (!this.client) {
6666
// Create the client instance with dummy key initially
6767
// The API key will be updated dynamically via ensureAuthenticated
68-
let timeout = getApiRequestTimeout()
69-
if (timeout === 0) {
70-
// Use 2147483647 (2^31 - 1) as the maximum timeout value for setTimeout
71-
// JavaScript's setTimeout has a maximum delay limit of 2147483647ms (32-bit signed integer max)
72-
// Values larger than this may be clamped to 1ms or cause unexpected behavior
73-
// 2147483647 is the safe maximum value that won't cause issues
74-
timeout = 2147483647
75-
}
68+
const timeout = getApiRequestTimeout()
7669
this.client = new OpenAI({
7770
apiKey: "dummy-key-will-be-replaced",
7871
baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",

src/api/providers/requesty.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,7 @@ export class RequestyHandler extends BaseProvider implements SingleCompletionHan
5454

5555
const apiKey = this.options.requestyApiKey ?? "not-provided"
5656

57-
let timeout = getApiRequestTimeout()
58-
if (timeout === 0) {
59-
// Use 2147483647 (2^31) as the maximum timeout value for setTimeout
60-
// JavaScript's setTimeout has a maximum delay limit of 2147483647ms (32-bit signed integer max)
61-
// Values larger than this may be clamped to 1ms or cause unexpected behavior
62-
// 2147483647 is safe as it's just above the limit but won't cause issues
63-
timeout = 2147483647
64-
}
57+
const timeout = getApiRequestTimeout()
6558

6659
this.client = new OpenAI({
6760
baseURL: this.baseURL,

0 commit comments

Comments
 (0)