Skip to content

Commit d3f020f

Browse files
committed
refactor(providers): remove createOpenAIClientWithErrorHandling; construct clients directly and keep request-time error mapping via handleOpenAIError
1 parent 5f8874b commit d3f020f

File tree

3 files changed

+7
-30
lines changed

3 files changed

+7
-30
lines changed

src/api/providers/huggingface.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from ".
88
import { DEFAULT_HEADERS } from "./constants"
99
import { BaseProvider } from "./base-provider"
1010
import { getHuggingFaceModels, getCachedHuggingFaceModels } from "./fetchers/huggingface"
11-
import { createOpenAIClientWithErrorHandling, handleOpenAIError } from "./utils/openai-error-handler"
11+
import { handleOpenAIError } from "./utils/openai-error-handler"
1212

1313
export class HuggingFaceHandler extends BaseProvider implements SingleCompletionHandler {
1414
private client: OpenAI
@@ -23,15 +23,11 @@ export class HuggingFaceHandler extends BaseProvider implements SingleCompletion
2323
throw new Error("Hugging Face API key is required")
2424
}
2525

26-
this.client = createOpenAIClientWithErrorHandling(
27-
() =>
28-
new OpenAI({
29-
baseURL: "https://router.huggingface.co/v1",
30-
apiKey: this.options.huggingFaceApiKey,
31-
defaultHeaders: DEFAULT_HEADERS,
32-
}),
33-
"HuggingFace",
34-
)
26+
this.client = new OpenAI({
27+
baseURL: "https://router.huggingface.co/v1",
28+
apiKey: this.options.huggingFaceApiKey,
29+
defaultHeaders: DEFAULT_HEADERS,
30+
})
3531

3632
// Try to get cached models first
3733
this.modelCache = getCachedHuggingFaceModels()

src/api/providers/openai-native.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { getModelParams } from "../transform/model-params"
2222

2323
import { BaseProvider } from "./base-provider"
2424
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index"
25-
import { createOpenAIClientWithErrorHandling } from "./utils/openai-error-handler"
2625

2726
export type OpenAiNativeModel = ReturnType<OpenAiNativeHandler["getModel"]>
2827

@@ -61,10 +60,7 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
6160
}
6261
const apiKey = this.options.openAiNativeApiKey ?? "not-provided"
6362

64-
this.client = createOpenAIClientWithErrorHandling(
65-
() => new OpenAI({ baseURL: this.options.openAiNativeBaseUrl, apiKey }),
66-
"OpenAI Native",
67-
)
63+
this.client = new OpenAI({ baseURL: this.options.openAiNativeBaseUrl, apiKey })
6864
}
6965

7066
private normalizeUsage(usage: any, model: OpenAiNativeModel): ApiStreamUsageChunk | undefined {

src/api/providers/utils/openai-error-handler.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,3 @@ export function handleOpenAIError(error: unknown, providerName: string): Error {
2727
// If it's not even an Error object, wrap it
2828
return new Error(`${providerName} error: ${String(error)}`)
2929
}
30-
31-
/**
32-
* Wraps an OpenAI client instantiation with error handling
33-
* @param createClient - Function that creates the OpenAI client
34-
* @param providerName - The name of the provider for error messages
35-
* @returns The created client
36-
* @throws User-friendly error if client creation fails
37-
*/
38-
export function createOpenAIClientWithErrorHandling<T>(createClient: () => T, providerName: string): T {
39-
try {
40-
return createClient()
41-
} catch (error) {
42-
throw handleOpenAIError(error, providerName)
43-
}
44-
}

0 commit comments

Comments
 (0)