Merge pull request #1041 from X-Zero-L/feat/codex-api-configurable-endpoint [skip ci]

feat: configurable provider endpoint for codex-api accounts
This commit is contained in:
Wesley Liddick
2026-03-02 16:23:55 +08:00
committed by GitHub
4 changed files with 96 additions and 6 deletions

View File

@@ -472,9 +472,18 @@ router.post('/openai-responses-accounts/:accountId/test', authenticateAdmin, asy
return res.status(401).json({ error: 'API Key not found or decryption failed' })
}
// 构造测试请求
// 构造测试请求(根据 providerEndpoint 和 baseApi 决定端点路径)
const baseUrl = account.baseApi || 'https://api.openai.com'
const apiUrl = `${baseUrl}/responses`
const providerEndpoint = account.providerEndpoint || 'responses'
let endpointPath = '/responses'
if (providerEndpoint === 'auto') {
endpointPath = '/responses' // 测试时默认用 responses
}
// 防止 baseApi 已含 /v1 时路径重复
if (!baseUrl.endsWith('/v1')) {
endpointPath = `/v1${endpointPath}`
}
const apiUrl = `${baseUrl}${endpointPath}`
const payload = createOpenAITestPayload(model, { stream: false })
const requestConfig = {

View File

@@ -51,7 +51,8 @@ class OpenAIResponsesAccountService {
dailyQuota = 0, // 每日额度限制美元0表示不限制
quotaResetTime = '00:00', // 额度重置时间HH:mm格式
rateLimitDuration = 60, // 限流时间(分钟)
disableAutoProtection = false // 是否关闭自动防护429/401/400/529 不自动禁用)
disableAutoProtection = false, // 是否关闭自动防护429/401/400/529 不自动禁用)
providerEndpoint = 'responses' // Provider 端点类型responses | auto
} = options
// 验证必填字段
@@ -59,6 +60,14 @@ class OpenAIResponsesAccountService {
throw new Error('Base API URL and API Key are required for OpenAI-Responses account')
}
// 验证 providerEndpoint 枚举值
const validEndpoints = ['responses', 'auto']
if (!validEndpoints.includes(providerEndpoint)) {
throw new Error(
`Invalid providerEndpoint: ${providerEndpoint}. Must be one of: ${validEndpoints.join(', ')}`
)
}
// 规范化 baseApi确保不以 / 结尾)
const normalizedBaseApi = baseApi.endsWith('/') ? baseApi.slice(0, -1) : baseApi
@@ -96,7 +105,8 @@ class OpenAIResponsesAccountService {
lastResetDate: redis.getDateStringInTimezone(),
quotaResetTime,
quotaStoppedAt: '',
disableAutoProtection: disableAutoProtection.toString() // 关闭自动防护
disableAutoProtection: disableAutoProtection.toString(), // 关闭自动防护
providerEndpoint // Provider 端点类型responses(默认) | auto
}
// 保存到 Redis
@@ -165,6 +175,16 @@ class OpenAIResponsesAccountService {
// 直接保存,不做任何调整
}
// 验证 providerEndpoint 枚举值
if (updates.providerEndpoint !== undefined) {
const validEndpoints = ['responses', 'auto']
if (!validEndpoints.includes(updates.providerEndpoint)) {
throw new Error(
`Invalid providerEndpoint: ${updates.providerEndpoint}. Must be one of: ${validEndpoints.join(', ')}`
)
}
}
// 自动防护开关
if (updates.disableAutoProtection !== undefined) {
updates.disableAutoProtection = updates.disableAutoProtection.toString()

View File

@@ -91,8 +91,30 @@ class OpenAIResponsesRelayService {
req.once('close', handleClientDisconnect)
res.once('close', handleClientDisconnect)
// 构建目标 URL
const targetUrl = `${fullAccount.baseApi}${req.path}`
// 构建目标 URL(根据 providerEndpoint 配置决定端点路径)
const providerEndpoint = fullAccount.providerEndpoint || 'responses'
let targetPath = req.path
// 根据 providerEndpoint 配置归一化路径
// 注意unified.js 已将 /v1/chat/completions 的请求体转换为 Responses 格式,
// 因此这里只需归一化路径即可;反向 responses→completions 需要同时转换请求体,
// 目前不支持,所以只保留 responses 和 auto 两种模式
if (
providerEndpoint === 'responses' &&
(targetPath === '/v1/chat/completions' || targetPath === '/chat/completions')
) {
const newPath = targetPath.startsWith('/v1') ? '/v1/responses' : '/responses'
logger.info(`📝 Normalized path (${req.path}) → ${newPath} (providerEndpoint=responses)`)
targetPath = newPath
}
// providerEndpoint === 'auto' 时保持原始路径不变
// 防止 baseApi 已含 /v1 时路径重复(如 baseApi=http://host/v1 + targetPath=/v1/responses → /v1/v1/responses
const baseApi = fullAccount.baseApi || ''
if (baseApi.endsWith('/v1') && targetPath.startsWith('/v1/')) {
targetPath = targetPath.slice(3) // '/v1/responses' → '/responses'
}
const targetUrl = `${baseApi}${targetPath}`
logger.info(`🎯 Forwarding to: ${targetUrl}`)
// 构建请求头 - 使用统一的 headerFilter 移除 CDN headers

View File

@@ -1692,6 +1692,23 @@
</p>
</div>
<div>
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300"
>Provider 端点类型</label
>
<select
v-model="form.providerEndpoint"
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
>
<option value="responses">Responses推荐</option>
<option value="auto">自动(保持原始路径)</option>
</select>
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
指定 Provider 支持的端点类型。Responses 会将所有请求路由到(包括来自
/v1/chat/completions 的请求会自动转换);自动则保持客户端请求的原始路径
</p>
</div>
<!-- 限流时长字段 - 隐藏不显示使用默认值60 -->
<input v-model.number="form.rateLimitDuration" type="hidden" value="60" />
</div>
@@ -3440,6 +3457,24 @@
</p>
</div>
<div>
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300"
>Provider 端点类型</label
>
<select
v-model="form.providerEndpoint"
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
>
<option value="responses">Responses推荐</option>
<option value="completions">Chat Completions</option>
<option value="auto">自动(保持原始路径)</option>
</select>
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
指定 Provider 支持的端点类型。Responses 会将所有请求路由到(包括来自
/v1/chat/completions 的请求会自动转换);自动则保持原始路径
</p>
</div>
<!-- 限流时长字段 - 隐藏不显示,保持原值 -->
<input v-model.number="form.rateLimitDuration" type="hidden" />
@@ -4272,6 +4307,7 @@ const form = ref({
endpointType: props.account?.endpointType || 'anthropic',
// OpenAI-Responses 特定字段
baseApi: props.account?.baseApi || '',
providerEndpoint: props.account?.providerEndpoint || 'responses',
// Gemini-API 特定字段
baseUrl: props.account?.baseUrl || 'https://generativelanguage.googleapis.com',
rateLimitDuration: props.account?.rateLimitDuration || 60,
@@ -5434,6 +5470,7 @@ const createAccount = async () => {
data.baseApi = form.value.baseApi
data.apiKey = form.value.apiKey
data.userAgent = form.value.userAgent || ''
data.providerEndpoint = form.value.providerEndpoint || 'responses'
data.priority = form.value.priority || 50
data.rateLimitDuration = 60 // 默认值60不从用户输入获取
data.dailyQuota = form.value.dailyQuota || 0
@@ -5784,6 +5821,7 @@ const updateAccount = async () => {
data.apiKey = form.value.apiKey
}
data.userAgent = form.value.userAgent || ''
data.providerEndpoint = form.value.providerEndpoint || 'responses'
data.priority = form.value.priority || 50
// 编辑时不上传 rateLimitDuration保持原值
data.dailyQuota = form.value.dailyQuota || 0
@@ -6406,6 +6444,7 @@ watch(
deploymentName: newAccount.deploymentName || '',
// OpenAI-Responses 特定字段
baseApi: newAccount.baseApi || '',
providerEndpoint: newAccount.providerEndpoint || 'responses',
// Gemini-API 特定字段
baseUrl: newAccount.baseUrl || 'https://generativelanguage.googleapis.com',
// 额度管理字段