Skip to content

Commit 203dac5

Browse files
ysfscreamRed-Asuka
authored andcommitted
refactor(ai): streamline Azure provider options handling for api version
1 parent d3b18a5 commit 203dac5

2 files changed

Lines changed: 32 additions & 18 deletions

File tree

src/utils/ai/AIAgent.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ export class AIAgent {
296296
baseURL: this.openAIAPIHost,
297297
apiKey,
298298
providerType,
299-
azureApiVersion: isAzure ? '2025-01-01-preview' : undefined,
300299
}),
301300
providerOptions: {
302301
anthropic: {

src/utils/ai/copilot.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,31 @@ export const isReasoningModel = (model: AIModel) => {
218218
return REASONING_MODEL_REGEX.test(model)
219219
}
220220

221+
/**
222+
* Extracts and prepares the specific options needed for the createAzure provider.
223+
*
224+
* @param baseURL - The original baseURL string which might contain API version or be a resource name.
225+
* @returns An object containing the processed `apiVersion` and either `baseURL` or `resourceName`.
226+
*/
227+
const getAzureProviderOptions = (baseURL: string): Record<string, any> => {
228+
const options: Record<string, any> = {}
229+
230+
const apiVersionMatch = baseURL.match(/api-version=([^&]+)/)
231+
options.apiVersion = apiVersionMatch ? apiVersionMatch[1] : '2025-01-01-preview'
232+
233+
const normalizedBaseURL = baseURL.split('?')[0].trim()
234+
235+
if (normalizedBaseURL && normalizedBaseURL.toLowerCase().startsWith('http')) {
236+
options.baseURL = normalizedBaseURL.includes('/openai/deployments')
237+
? normalizedBaseURL
238+
: `${normalizedBaseURL}/openai/deployments`
239+
} else if (normalizedBaseURL) {
240+
options.resourceName = normalizedBaseURL
241+
}
242+
243+
return options
244+
}
245+
221246
/**
222247
* Gets the appropriate LanguageModelV1 instance for the specified provider.
223248
*
@@ -229,19 +254,16 @@ export const isReasoningModel = (model: AIModel) => {
229254
* @param opts.model - The AI model identifier to use (model name or deployment name for Azure)
230255
* @param opts.baseURL - The base URL for the API endpoint
231256
* @param opts.apiKey - The API key for authentication
232-
* @param opts.providerType - The AI provider type (OpenAI, Azure, Anthropic, etc.)
233-
* @param opts.azureApiVersion - Optional Azure API version (required for Azure provider)
234-
* @param opts.azureResourceName - Optional Azure resource name (used instead of baseURL for Azure)
257+
* @param opts.providerType - The AI provider type (OpenAI, Azure OpenAI, Anthropic, etc.)
235258
* @returns A configured LanguageModelV1 compatible provider instance
236259
*/
237260
export const getModelProvider = (opts: {
238261
model: AIModel
239262
baseURL: string
240263
apiKey: string
241264
providerType: typeof AImodelsOptions[number]['value']
242-
azureApiVersion?: string
243265
}): LanguageModelV1 => {
244-
const { model, baseURL, apiKey, providerType, azureApiVersion } = opts
266+
const { model, baseURL, apiKey, providerType } = opts
245267

246268
const providerConfig = AImodelsOptions.find((p) => p.value === providerType)
247269

@@ -253,22 +275,15 @@ export const getModelProvider = (opts: {
253275
return fallbackProvider(model) as LanguageModelV1
254276
}
255277

256-
const creatorOptions: Record<string, any> = {
278+
let creatorOptions: Record<string, any> = {
257279
apiKey,
258280
}
259281

260-
if (providerType === 'Azure OpenAI' && azureApiVersion) {
261-
creatorOptions.apiVersion = azureApiVersion
262-
263-
if (baseURL) {
264-
if (baseURL.toLowerCase().startsWith('http')) {
265-
creatorOptions.baseURL = !baseURL.includes('/openai/deployments') ? `${baseURL}/openai/deployments` : baseURL
266-
} else {
267-
creatorOptions.resourceName = baseURL
268-
}
269-
}
282+
if (providerType === 'Azure OpenAI') {
283+
const azureOptions = getAzureProviderOptions(baseURL)
284+
creatorOptions = { ...creatorOptions, ...azureOptions }
270285
} else if (baseURL) {
271-
creatorOptions.baseURL = baseURL
286+
creatorOptions.baseURL = baseURL.trim()
272287
}
273288

274289
const providerCreator = providerConfig.providerCreator

0 commit comments

Comments
 (0)