fix: refactor provider headers logic in providerConfig (#11849)

Simplifies and centralizes header construction by merging defaultAppHeaders and extra_headers, and sets X-Api-Key for OpenAI providers. Removes redundant header assignment logic for improved maintainability.
This commit is contained in:
Pleasure1234 2025-12-17 07:21:06 +00:00 committed by GitHub
parent 784fdd4fed
commit bfeef7ef91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -32,6 +32,7 @@ import {
isSupportStreamOptionsProvider, isSupportStreamOptionsProvider,
isVertexProvider isVertexProvider
} from '@renderer/utils/provider' } from '@renderer/utils/provider'
import { defaultAppHeaders } from '@shared/utils'
import { cloneDeep, isEmpty } from 'lodash' import { cloneDeep, isEmpty } from 'lodash'
import type { AiSdkConfig } from '../types' import type { AiSdkConfig } from '../types'
@ -197,18 +198,13 @@ export function providerToAiSdkConfig(actualProvider: Provider, model: Model): A
extraOptions.mode = 'chat' extraOptions.mode = 'chat'
} }
// 添加额外headers extraOptions.headers = {
if (actualProvider.extra_headers) { ...defaultAppHeaders(),
extraOptions.headers = actualProvider.extra_headers ...actualProvider.extra_headers
// copy from openaiBaseClient/openaiResponseApiClient }
if (aiSdkProviderId === 'openai') {
extraOptions.headers = { if (aiSdkProviderId === 'openai') {
...extraOptions.headers, extraOptions.headers['X-Api-Key'] = baseConfig.apiKey
'HTTP-Referer': 'https://cherry-ai.com',
'X-Title': 'Cherry Studio',
'X-Api-Key': baseConfig.apiKey
}
}
} }
// azure // azure
// https://learn.microsoft.com/en-us/azure/ai-foundry/openai/latest // https://learn.microsoft.com/en-us/azure/ai-foundry/openai/latest