mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-24 15:20:38 +00:00
fix(openai): gate routing by endpoint capability
This commit is contained in:
1 parent
8c1a07852c
commit
ed1b57c597
16 files changed
+740
-65
No files matched your search
@@ -2679,7 +2679,7 @@
|
||||
<!-- OpenAI APIKey Responses API support mode -->
|
||||
<div
|
||||
v-if="form.platform === 'openai' && accountCategory === 'apikey'"
|
||||
class="border-t border-gray-200 pt-4 dark:border-dark-600"
|
||||
class="space-y-4 border-t border-gray-200 pt-4 dark:border-dark-600"
|
||||
>
|
||||
<div class="flex items-center justify-between gap-4">
|
||||
<div>
|
||||
@@ -2696,6 +2696,26 @@
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="input-label mb-2 block">{{ t('admin.accounts.openai.endpointCapabilities') }}</label>
|
||||
<div class="grid grid-cols-1 gap-2 sm:grid-cols-2">
|
||||
<label
|
||||
v-for="option in openAIEndpointCapabilityOptions"
|
||||
:key="option.value"
|
||||
class="flex cursor-pointer items-center gap-2 rounded-lg border border-gray-200 px-3 py-2 text-sm dark:border-dark-600"
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
class="rounded border-gray-300 text-primary-600 focus:ring-primary-500 dark:border-dark-500"
|
||||
:data-testid="`openai-endpoint-capability-${option.value}`"
|
||||
:checked="openAIEndpointCapabilities.includes(option.value)"
|
||||
@change="toggleOpenAIEndpointCapability(option.value, $event)"
|
||||
/>
|
||||
<span class="text-gray-700 dark:text-gray-200">{{ option.label }}</span>
|
||||
</label>
|
||||
</div>
|
||||
<p class="input-hint">{{ t('admin.accounts.openai.endpointCapabilitiesDesc') }}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
@@ -3172,7 +3192,8 @@ import type {
|
||||
CreateAccountRequest,
|
||||
CodexSessionImportMessage,
|
||||
OpenAICompactMode,
|
||||
OpenAIResponsesMode
|
||||
OpenAIResponsesMode,
|
||||
OpenAIEndpointCapability
|
||||
} from '@/types'
|
||||
import BaseDialog from '@/components/common/BaseDialog.vue'
|
||||
import ConfirmDialog from '@/components/common/ConfirmDialog.vue'
|
||||
@@ -3350,6 +3371,7 @@ const autoPauseOnExpired = ref(true)
|
||||
const openaiPassthroughEnabled = ref(false)
|
||||
const openAICompactMode = ref<OpenAICompactMode>('auto')
|
||||
const openAIResponsesMode = ref<OpenAIResponsesMode>('auto')
|
||||
const openAIEndpointCapabilities = ref<OpenAIEndpointCapability[]>(['chat_completions', 'embeddings'])
|
||||
const openaiOAuthResponsesWebSocketV2Mode = ref<OpenAIWSMode>(OPENAI_WS_MODE_OFF)
|
||||
const openaiAPIKeyResponsesWebSocketV2Mode = ref<OpenAIWSMode>(OPENAI_WS_MODE_OFF)
|
||||
const codexCLIOnlyEnabled = ref(false)
|
||||
@@ -3412,6 +3434,43 @@ const openAIResponsesModeOptions = computed(() => [
|
||||
{ value: 'force_responses', label: t('admin.accounts.openai.responsesModeForceResponses') },
|
||||
{ value: 'force_chat_completions', label: t('admin.accounts.openai.responsesModeForceChatCompletions') }
|
||||
])
|
||||
const openAIEndpointCapabilityOptions = computed<{ value: OpenAIEndpointCapability; label: string }[]>(() => [
|
||||
{ value: 'chat_completions', label: t('admin.accounts.openai.capabilityChatCompletions') },
|
||||
{ value: 'embeddings', label: t('admin.accounts.openai.capabilityEmbeddings') }
|
||||
])
|
||||
|
||||
const normalizeOpenAIEndpointCapabilities = (values: OpenAIEndpointCapability[]) => {
|
||||
const allowed: OpenAIEndpointCapability[] = ['chat_completions', 'embeddings']
|
||||
const selected = allowed.filter((value) => values.includes(value))
|
||||
return selected.length > 0 ? selected : allowed
|
||||
}
|
||||
|
||||
const toggleOpenAIEndpointCapability = (capability: OpenAIEndpointCapability, event?: Event) => {
|
||||
if (openAIEndpointCapabilities.value.includes(capability)) {
|
||||
if (openAIEndpointCapabilities.value.length <= 1) {
|
||||
const input = event?.target as HTMLInputElement | null
|
||||
if (input) input.checked = true
|
||||
return
|
||||
}
|
||||
openAIEndpointCapabilities.value = openAIEndpointCapabilities.value.filter(
|
||||
(value) => value !== capability
|
||||
)
|
||||
return
|
||||
}
|
||||
openAIEndpointCapabilities.value = normalizeOpenAIEndpointCapabilities([
|
||||
...openAIEndpointCapabilities.value,
|
||||
capability
|
||||
])
|
||||
}
|
||||
|
||||
const applyOpenAIEndpointCapabilities = (credentials: Record<string, unknown>) => {
|
||||
const capabilities = normalizeOpenAIEndpointCapabilities(openAIEndpointCapabilities.value)
|
||||
if (capabilities.length === 2) {
|
||||
delete credentials.openai_capabilities
|
||||
return
|
||||
}
|
||||
credentials.openai_capabilities = capabilities
|
||||
}
|
||||
|
||||
function buildAntigravityExtra(): Record<string, unknown> | undefined {
|
||||
const extra: Record<string, unknown> = {}
|
||||
@@ -3721,6 +3780,7 @@ watch(
|
||||
}
|
||||
if (newPlatform !== 'openai') {
|
||||
openaiPassthroughEnabled.value = false
|
||||
openAIEndpointCapabilities.value = ['chat_completions', 'embeddings']
|
||||
openaiOAuthResponsesWebSocketV2Mode.value = OPENAI_WS_MODE_OFF
|
||||
openaiAPIKeyResponsesWebSocketV2Mode.value = OPENAI_WS_MODE_OFF
|
||||
codexCLIOnlyEnabled.value = false
|
||||
@@ -4120,6 +4180,7 @@ const resetForm = () => {
|
||||
openaiPassthroughEnabled.value = false
|
||||
openAICompactMode.value = 'auto'
|
||||
openAIResponsesMode.value = 'auto'
|
||||
openAIEndpointCapabilities.value = ['chat_completions', 'embeddings']
|
||||
openaiOAuthResponsesWebSocketV2Mode.value = OPENAI_WS_MODE_OFF
|
||||
openaiAPIKeyResponsesWebSocketV2Mode.value = OPENAI_WS_MODE_OFF
|
||||
codexCLIOnlyEnabled.value = false
|
||||
@@ -4498,6 +4559,7 @@ const handleSubmit = async () => {
|
||||
}
|
||||
}
|
||||
if (form.platform === 'openai') {
|
||||
applyOpenAIEndpointCapabilities(credentials)
|
||||
const compactModelMapping = buildOpenAICompactModelMapping()
|
||||
if (compactModelMapping) {
|
||||
credentials.compact_model_mapping = compactModelMapping
|
||||
@@ -4620,6 +4682,9 @@ const createAccountAndFinish = async (
|
||||
}
|
||||
}
|
||||
if (platform === 'openai') {
|
||||
if (type === 'apikey') {
|
||||
applyOpenAIEndpointCapabilities(credentials)
|
||||
}
|
||||
const compactModelMapping = buildOpenAICompactModelMapping()
|
||||
if (compactModelMapping) {
|
||||
credentials.compact_model_mapping = compactModelMapping
|
||||
|
||||
@@ -1439,7 +1439,7 @@
|
||||
<!-- OpenAI APIKey Responses API support mode -->
|
||||
<div
|
||||
v-if="account?.platform === 'openai' && account?.type === 'apikey'"
|
||||
class="border-t border-gray-200 pt-4 dark:border-dark-600 space-y-3"
|
||||
class="space-y-4 border-t border-gray-200 pt-4 dark:border-dark-600"
|
||||
>
|
||||
<div class="flex items-center justify-between gap-4">
|
||||
<div>
|
||||
@@ -1459,6 +1459,26 @@
|
||||
<div class="rounded-lg bg-gray-50 px-3 py-2 text-xs text-gray-600 dark:bg-dark-700 dark:text-gray-300">
|
||||
<span class="font-medium">{{ t(openAIResponsesStatusKey) }}</span>
|
||||
</div>
|
||||
<div>
|
||||
<label class="input-label mb-2 block">{{ t('admin.accounts.openai.endpointCapabilities') }}</label>
|
||||
<div class="grid grid-cols-1 gap-2 sm:grid-cols-2">
|
||||
<label
|
||||
v-for="option in openAIEndpointCapabilityOptions"
|
||||
:key="option.value"
|
||||
class="flex cursor-pointer items-center gap-2 rounded-lg border border-gray-200 px-3 py-2 text-sm dark:border-dark-600"
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
class="rounded border-gray-300 text-primary-600 focus:ring-primary-500 dark:border-dark-500"
|
||||
:data-testid="`openai-endpoint-capability-${option.value}`"
|
||||
:checked="openAIEndpointCapabilities.includes(option.value)"
|
||||
@change="toggleOpenAIEndpointCapability(option.value, $event)"
|
||||
/>
|
||||
<span class="text-gray-700 dark:text-gray-200">{{ option.label }}</span>
|
||||
</label>
|
||||
</div>
|
||||
<p class="input-hint">{{ t('admin.accounts.openai.endpointCapabilitiesDesc') }}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Anthropic API Key 自动透传开关 -->
|
||||
@@ -2245,7 +2265,15 @@ import { useAppStore } from '@/stores/app'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { adminAPI } from '@/api/admin'
|
||||
import { useQuotaNotifyState } from '@/composables/useQuotaNotifyState'
|
||||
import type { Account, Proxy, AdminGroup, CheckMixedChannelResponse, OpenAICompactMode, OpenAIResponsesMode } from '@/types'
|
||||
import type {
|
||||
Account,
|
||||
Proxy,
|
||||
AdminGroup,
|
||||
CheckMixedChannelResponse,
|
||||
OpenAICompactMode,
|
||||
OpenAIResponsesMode,
|
||||
OpenAIEndpointCapability
|
||||
} from '@/types'
|
||||
import BaseDialog from '@/components/common/BaseDialog.vue'
|
||||
import ConfirmDialog from '@/components/common/ConfirmDialog.vue'
|
||||
import Select from '@/components/common/Select.vue'
|
||||
@@ -2433,6 +2461,7 @@ const customBaseUrl = ref('')
|
||||
const openaiPassthroughEnabled = ref(false)
|
||||
const openAICompactMode = ref<OpenAICompactMode>('auto')
|
||||
const openAIResponsesMode = ref<OpenAIResponsesMode>('auto')
|
||||
const openAIEndpointCapabilities = ref<OpenAIEndpointCapability[]>(['chat_completions', 'embeddings'])
|
||||
const openaiOAuthResponsesWebSocketV2Mode = ref<OpenAIWSMode>(OPENAI_WS_MODE_OFF)
|
||||
const openaiAPIKeyResponsesWebSocketV2Mode = ref<OpenAIWSMode>(OPENAI_WS_MODE_OFF)
|
||||
const codexCLIOnlyEnabled = ref(false)
|
||||
@@ -2539,6 +2568,63 @@ const openAIResponsesModeOptions = computed(() => [
|
||||
{ value: 'force_responses', label: t('admin.accounts.openai.responsesModeForceResponses') },
|
||||
{ value: 'force_chat_completions', label: t('admin.accounts.openai.responsesModeForceChatCompletions') }
|
||||
])
|
||||
const openAIEndpointCapabilityOptions = computed<{ value: OpenAIEndpointCapability; label: string }[]>(() => [
|
||||
{ value: 'chat_completions', label: t('admin.accounts.openai.capabilityChatCompletions') },
|
||||
{ value: 'embeddings', label: t('admin.accounts.openai.capabilityEmbeddings') }
|
||||
])
|
||||
|
||||
const normalizeOpenAIEndpointCapabilities = (values: OpenAIEndpointCapability[]) => {
|
||||
const allowed: OpenAIEndpointCapability[] = ['chat_completions', 'embeddings']
|
||||
const selected = allowed.filter((value) => values.includes(value))
|
||||
return selected.length > 0 ? selected : allowed
|
||||
}
|
||||
|
||||
const readOpenAIEndpointCapabilities = (credentials?: Record<string, unknown>): OpenAIEndpointCapability[] => {
|
||||
const raw = credentials?.openai_capabilities
|
||||
if (Array.isArray(raw)) {
|
||||
return normalizeOpenAIEndpointCapabilities(
|
||||
raw.filter((value): value is OpenAIEndpointCapability =>
|
||||
value === 'chat_completions' || value === 'embeddings'
|
||||
)
|
||||
)
|
||||
}
|
||||
if (raw !== null && typeof raw === 'object') {
|
||||
const capabilityMap = raw as Record<string, unknown>
|
||||
return normalizeOpenAIEndpointCapabilities(
|
||||
openAIEndpointCapabilityOptions.value
|
||||
.map((option) => option.value)
|
||||
.filter((value) => capabilityMap[value] === true)
|
||||
)
|
||||
}
|
||||
return ['chat_completions', 'embeddings']
|
||||
}
|
||||
|
||||
const toggleOpenAIEndpointCapability = (capability: OpenAIEndpointCapability, event?: Event) => {
|
||||
if (openAIEndpointCapabilities.value.includes(capability)) {
|
||||
if (openAIEndpointCapabilities.value.length <= 1) {
|
||||
const input = event?.target as HTMLInputElement | null
|
||||
if (input) input.checked = true
|
||||
return
|
||||
}
|
||||
openAIEndpointCapabilities.value = openAIEndpointCapabilities.value.filter(
|
||||
(value) => value !== capability
|
||||
)
|
||||
return
|
||||
}
|
||||
openAIEndpointCapabilities.value = normalizeOpenAIEndpointCapabilities([
|
||||
...openAIEndpointCapabilities.value,
|
||||
capability
|
||||
])
|
||||
}
|
||||
|
||||
const applyOpenAIEndpointCapabilities = (credentials: Record<string, unknown>) => {
|
||||
const capabilities = normalizeOpenAIEndpointCapabilities(openAIEndpointCapabilities.value)
|
||||
if (capabilities.length === 2) {
|
||||
delete credentials.openai_capabilities
|
||||
return
|
||||
}
|
||||
credentials.openai_capabilities = capabilities
|
||||
}
|
||||
const normalizeOpenAIResponsesMode = (mode: unknown): OpenAIResponsesMode => {
|
||||
if (mode === 'force_responses' || mode === 'force_chat_completions') {
|
||||
return mode
|
||||
@@ -2724,6 +2810,7 @@ const syncFormFromAccount = (newAccount: Account | null) => {
|
||||
openaiPassthroughEnabled.value = false
|
||||
openAICompactMode.value = 'auto'
|
||||
openAIResponsesMode.value = 'auto'
|
||||
openAIEndpointCapabilities.value = ['chat_completions', 'embeddings']
|
||||
openAICompactModelMappings.value = []
|
||||
openaiOAuthResponsesWebSocketV2Mode.value = OPENAI_WS_MODE_OFF
|
||||
openaiAPIKeyResponsesWebSocketV2Mode.value = OPENAI_WS_MODE_OFF
|
||||
@@ -2736,6 +2823,9 @@ const syncFormFromAccount = (newAccount: Account | null) => {
|
||||
openAICompactMode.value = (extra?.openai_compact_mode as OpenAICompactMode) || 'auto'
|
||||
if (newAccount.type === 'apikey') {
|
||||
openAIResponsesMode.value = normalizeOpenAIResponsesMode(extra?.openai_responses_mode)
|
||||
openAIEndpointCapabilities.value = readOpenAIEndpointCapabilities(
|
||||
newAccount.credentials as Record<string, unknown> | undefined
|
||||
)
|
||||
}
|
||||
const codexImageGenerationBridgeValue = typeof extra?.codex_image_generation_bridge === 'boolean'
|
||||
? extra.codex_image_generation_bridge
|
||||
@@ -3476,6 +3566,7 @@ const handleSubmit = async () => {
|
||||
newCredentials.model_mapping = currentCredentials.model_mapping
|
||||
}
|
||||
if (props.account.platform === 'openai') {
|
||||
applyOpenAIEndpointCapabilities(newCredentials)
|
||||
const compactModelMapping = buildModelMappingObject('mapping', [], openAICompactModelMappings.value)
|
||||
if (compactModelMapping) {
|
||||
newCredentials.compact_model_mapping = compactModelMapping
|
||||
|
||||
@@ -310,6 +310,63 @@ describe('EditAccountModal', () => {
|
||||
expect(updateAccountMock.mock.calls[0]?.[1]?.extra?.openai_responses_supported).toBe(true)
|
||||
})
|
||||
|
||||
it('submits OpenAI APIKey endpoint capabilities from credentials', async () => {
|
||||
const account = buildAccount()
|
||||
account.credentials.openai_capabilities = ['chat_completions']
|
||||
updateAccountMock.mockReset()
|
||||
checkMixedChannelRiskMock.mockReset()
|
||||
checkMixedChannelRiskMock.mockResolvedValue({ has_risk: false })
|
||||
updateAccountMock.mockResolvedValue(account)
|
||||
|
||||
const wrapper = mountModal(account)
|
||||
|
||||
expect(wrapper.findAll('input[type="checkbox"]').some((input) => (input.element as HTMLInputElement).checked)).toBe(true)
|
||||
|
||||
await wrapper.get('form#edit-account-form').trigger('submit.prevent')
|
||||
|
||||
expect(updateAccountMock).toHaveBeenCalledTimes(1)
|
||||
expect(updateAccountMock.mock.calls[0]?.[1]?.credentials?.openai_capabilities).toEqual([
|
||||
'chat_completions'
|
||||
])
|
||||
})
|
||||
|
||||
it('keeps at least one OpenAI APIKey endpoint capability selected', async () => {
|
||||
const account = buildAccount()
|
||||
updateAccountMock.mockReset()
|
||||
checkMixedChannelRiskMock.mockReset()
|
||||
checkMixedChannelRiskMock.mockResolvedValue({ has_risk: false })
|
||||
updateAccountMock.mockResolvedValue(account)
|
||||
|
||||
const wrapper = mountModal(account)
|
||||
|
||||
const chatCheckbox = wrapper.get<HTMLInputElement>(
|
||||
'[data-testid="openai-endpoint-capability-chat_completions"]'
|
||||
)
|
||||
const embeddingsCheckbox = wrapper.get<HTMLInputElement>(
|
||||
'[data-testid="openai-endpoint-capability-embeddings"]'
|
||||
)
|
||||
|
||||
expect(chatCheckbox.element.checked).toBe(true)
|
||||
expect(embeddingsCheckbox.element.checked).toBe(true)
|
||||
|
||||
await embeddingsCheckbox.setValue(false)
|
||||
|
||||
expect(chatCheckbox.element.checked).toBe(true)
|
||||
expect(embeddingsCheckbox.element.checked).toBe(false)
|
||||
|
||||
await chatCheckbox.setValue(false)
|
||||
|
||||
expect(chatCheckbox.element.checked).toBe(true)
|
||||
expect(embeddingsCheckbox.element.checked).toBe(false)
|
||||
|
||||
await wrapper.get('form#edit-account-form').trigger('submit.prevent')
|
||||
|
||||
expect(updateAccountMock).toHaveBeenCalledTimes(1)
|
||||
expect(updateAccountMock.mock.calls[0]?.[1]?.credentials?.openai_capabilities).toEqual([
|
||||
'chat_completions'
|
||||
])
|
||||
})
|
||||
|
||||
it('submits account-level Codex image generation bridge override', async () => {
|
||||
const account = buildAccount()
|
||||
account.extra = {
|
||||
|
||||
@@ -3353,6 +3353,11 @@ export default {
|
||||
responsesModeAuto: 'Auto',
|
||||
responsesModeForceResponses: 'Force Responses',
|
||||
responsesModeForceChatCompletions: 'Force Chat Completions',
|
||||
endpointCapabilities: 'Endpoint capabilities',
|
||||
endpointCapabilitiesDesc:
|
||||
'Used by account routing. Both endpoints are allowed by default; if the upstream only supports one, select only the supported endpoint.',
|
||||
capabilityChatCompletions: 'Chat Completions',
|
||||
capabilityEmbeddings: 'Embeddings',
|
||||
responsesStatusAutoSupported: 'Auto probe: Responses',
|
||||
responsesStatusAutoUnsupported: 'Auto probe: Chat Completions',
|
||||
responsesStatusAutoUnknown: 'Auto probe: unknown',
|
||||
|
||||
@@ -3499,6 +3499,11 @@ export default {
|
||||
responsesModeAuto: '自动',
|
||||
responsesModeForceResponses: '强制 Responses',
|
||||
responsesModeForceChatCompletions: '强制 Chat Completions',
|
||||
endpointCapabilities: '端点能力',
|
||||
endpointCapabilitiesDesc:
|
||||
'用于调度筛选。默认两个端点都可用;如果上游只支持其中一个,请只勾选实际支持的端点。',
|
||||
capabilityChatCompletions: 'Chat Completions',
|
||||
capabilityEmbeddings: 'Embeddings',
|
||||
responsesStatusAutoSupported: '自动探测:Responses',
|
||||
responsesStatusAutoUnsupported: '自动探测:Chat Completions',
|
||||
responsesStatusAutoUnknown: '自动探测:未探测',
|
||||
|
||||
@@ -997,6 +997,7 @@ export interface CodexUsageSnapshot {
|
||||
|
||||
export type OpenAICompactMode = 'auto' | 'force_on' | 'force_off'
|
||||
export type OpenAIResponsesMode = 'auto' | 'force_responses' | 'force_chat_completions'
|
||||
export type OpenAIEndpointCapability = 'chat_completions' | 'embeddings'
|
||||
|
||||
export interface OpenAICompactState {
|
||||
openai_compact_mode?: OpenAICompactMode
|
||||
|
||||
Reference in new issue
Block a user