@@ -36,17 +36,8 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
3636
3737 const baseURL = this . options . openAiBaseUrl ?? "https://api.openai.com/v1"
3838 const apiKey = this . options . openAiApiKey ?? "not-provided"
39- let urlHost : string
40-
41- try {
42- urlHost = new URL ( this . options . openAiBaseUrl ?? "" ) . host
43- } catch ( error ) {
44- // Likely an invalid `openAiBaseUrl`; we're still working on
45- // proper settings validation.
46- urlHost = ""
47- }
48-
49- const isAzureAiInference = urlHost . endsWith ( ".services.ai.azure.com" )
39+ const isAzureAiInference = this . _isAzureAiInference ( this . options . openAiBaseUrl )
40+ const urlHost = this . _getUrlHost ( this . options . openAiBaseUrl )
5041 const isAzureOpenAi = urlHost === "azure.com" || urlHost . endsWith ( ".azure.com" ) || options . openAiUseAzure
5142
5243 if ( isAzureAiInference ) {
@@ -76,14 +67,8 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
7667 const modelUrl = this . options . openAiBaseUrl ?? ""
7768 const modelId = this . options . openAiModelId ?? ""
7869 const enabledR1Format = this . options . openAiR1FormatEnabled ?? false
79- // Add Azure AI Inference check within this method
80- let urlHost : string
81- try {
82- urlHost = new URL ( modelUrl ) . host
83- } catch ( error ) {
84- urlHost = ""
85- }
86- const isAzureAiInference = urlHost . endsWith ( ".services.ai.azure.com" )
70+ const isAzureAiInference = this . _isAzureAiInference ( modelUrl )
71+ const urlHost = this . _getUrlHost ( modelUrl )
8772 const deepseekReasoner = modelId . includes ( "deepseek-reasoner" ) || enabledR1Format
8873 const ark = modelUrl . includes ( ".volces.com" )
8974 if ( modelId . startsWith ( "o3-mini" ) ) {
@@ -210,7 +195,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
210195
211196 const response = await this . client . chat . completions . create (
212197 requestOptions ,
213- isAzureAiInference ? { path : AZURE_AI_INFERENCE_PATH } : { } ,
198+ this . _isAzureAiInference ( modelUrl ) ? { path : AZURE_AI_INFERENCE_PATH } : { } ,
214199 )
215200
216201 yield {
@@ -238,14 +223,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
238223
239224 async completePrompt ( prompt : string ) : Promise < string > {
240225 try {
241- // Add Azure AI Inference check within this method
242- let urlHost : string
243- try {
244- urlHost = new URL ( this . options . openAiBaseUrl ?? "" ) . host
245- } catch ( error ) {
246- urlHost = ""
247- }
248- const isAzureAiInference = urlHost . endsWith ( ".services.ai.azure.com" )
226+ const isAzureAiInference = this . _isAzureAiInference ( this . options . openAiBaseUrl )
249227 const requestOptions : OpenAI . Chat . Completions . ChatCompletionCreateParamsNonStreaming = {
250228 model : this . getModel ( ) . id ,
251229 messages : [ { role : "user" , content : prompt } ] ,
@@ -270,14 +248,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
270248 messages : Anthropic . Messages . MessageParam [ ] ,
271249 ) : ApiStream {
272250 if ( this . options . openAiStreamingEnabled ?? true ) {
273- // Add Azure AI Inference check within this method scope
274- let methodUrlHost : string
275- try {
276- methodUrlHost = new URL ( this . options . openAiBaseUrl ?? "" ) . host
277- } catch ( error ) {
278- methodUrlHost = ""
279- }
280- const methodIsAzureAiInference = methodUrlHost . endsWith ( ".services.ai.azure.com" )
251+ const methodIsAzureAiInference = this . _isAzureAiInference ( this . options . openAiBaseUrl )
281252
282253 const stream = await this . client . chat . completions . create (
283254 {
@@ -309,14 +280,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
309280 ] ,
310281 }
311282
312- // Add Azure AI Inference check within this method scope
313- let methodUrlHost : string
314- try {
315- methodUrlHost = new URL ( this . options . openAiBaseUrl ?? "" ) . host
316- } catch ( error ) {
317- methodUrlHost = ""
318- }
319- const methodIsAzureAiInference = methodUrlHost . endsWith ( ".services.ai.azure.com" )
283+ const methodIsAzureAiInference = this . _isAzureAiInference ( this . options . openAiBaseUrl )
320284
321285 const response = await this . client . chat . completions . create (
322286 requestOptions ,
@@ -350,6 +314,18 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
350314 }
351315 }
352316 }
317+ private _getUrlHost ( baseUrl ?: string ) : string {
318+ try {
319+ return new URL ( baseUrl ?? "" ) . host
320+ } catch ( error ) {
321+ return ""
322+ }
323+ }
324+
325+ private _isAzureAiInference ( baseUrl ?: string ) : boolean {
326+ const urlHost = this . _getUrlHost ( baseUrl )
327+ return urlHost . endsWith ( ".services.ai.azure.com" )
328+ }
353329}
354330
355331export async function getOpenAiModels ( baseUrl ?: string , apiKey ?: string ) {
0 commit comments