diff --git a/backend/internal/service/platform_catalog.go b/backend/internal/service/platform_catalog.go index 7ef15368c5..eda3fefc49 100644 --- a/backend/internal/service/platform_catalog.go +++ b/backend/internal/service/platform_catalog.go @@ -25,9 +25,10 @@ type PlatformCatalogEntry struct { // ProviderProfileInfo 是 ProviderProfile 的对外形状。 type ProviderProfileInfo struct { - DefaultMode string `json:"default_mode"` - Routing ProviderRouting `json:"routing"` - ResponsesPath string `json:"responses_path,omitempty"` + DefaultMode string `json:"default_mode"` + DefaultAPIProtocol string `json:"default_api_protocol,omitempty"` + Routing ProviderRouting `json:"routing"` + ResponsesPath string `json:"responses_path,omitempty"` // ModelCatalog 表示模型支持的协议由上游 /models 自动获取(账号规则仍优先)。 ModelCatalog bool `json:"model_catalog,omitempty"` // Modes 以默认模式开头,其余按模式名排序。 @@ -76,11 +77,12 @@ func providerProfileInfo(profile *ProviderProfile) *ProviderProfileInfo { modes = append([]string{profile.DefaultMode}, modes...) } info := &ProviderProfileInfo{ - DefaultMode: profile.DefaultMode, - Routing: routing, - ResponsesPath: profile.ResponsesPath, - ModelCatalog: profile.ModelCatalog, - Modes: make([]ProviderModeInfo, 0, len(modes)), + DefaultMode: profile.DefaultMode, + DefaultAPIProtocol: profile.DefaultAPIProtocol, + Routing: routing, + ResponsesPath: profile.ResponsesPath, + ModelCatalog: profile.ModelCatalog, + Modes: make([]ProviderModeInfo, 0, len(modes)), } for _, mode := range modes { endpoints := profile.Modes[mode] diff --git a/backend/internal/service/platform_catalog_test.go b/backend/internal/service/platform_catalog_test.go index b6da5ae5d6..8476db463a 100644 --- a/backend/internal/service/platform_catalog_test.go +++ b/backend/internal/service/platform_catalog_test.go @@ -26,6 +26,7 @@ func TestBuildPlatformCatalogMirrorsPlatformListAndProfiles(t *testing.T) { } require.NotNil(t, entry.MultiProtocol, entry.ID) require.Equal(t, profile.DefaultMode, entry.MultiProtocol.DefaultMode, entry.ID) + require.Equal(t, profile.DefaultAPIProtocol, entry.MultiProtocol.DefaultAPIProtocol, entry.ID) require.Equal(t, profile.DefaultMode, entry.MultiProtocol.Modes[0].Mode, entry.ID) require.Equal(t, profile.ResponsesPath, entry.MultiProtocol.ResponsesPath, entry.ID) require.Len(t, entry.MultiProtocol.Modes, len(profile.Modes), entry.ID) diff --git a/frontend/src/components/account/EditAccountModal.vue b/frontend/src/components/account/EditAccountModal.vue index 1eab54094d..99da3379b1 100644 --- a/frontend/src/components/account/EditAccountModal.vue +++ b/frontend/src/components/account/EditAccountModal.vue @@ -3481,6 +3481,7 @@ import { buildPlanTypeOptions, cloneOpenCodeGoProtocolRules, defaultOpenCodeProtocolRules, + defaultProviderApiProtocol, defaultProviderProtocolRules, isMultiProtocolApiKeyPlatform, parseOllamaMonthlyCreditUsd, @@ -3826,7 +3827,11 @@ watch(editAccountMode, (mode, previousMode) => { editBaseUrl.value = editAdaptiveBaseUrls.value.chat_completions return } - editBaseUrl.value = defaultCNBaseUrl(props.account!.platform, mode, editApiProtocol.value) + const previousDefault = defaultCNBaseUrl(props.account!.platform, previousMode, editApiProtocol.value) + const nextDefault = defaultCNBaseUrl(props.account!.platform, mode, editApiProtocol.value) + if (!editBaseUrl.value.trim() || editBaseUrl.value.trim() === previousDefault) { + editBaseUrl.value = nextDefault + } }) watch(editOpenCodeAccountMode, (mode, previousMode) => { if (!isCNApiKeyAccount.value || props.account?.platform !== 'opencode_go' || syncingForm.value) return @@ -4957,7 +4962,7 @@ const syncFormFromAccount = (newAccount: Account | null) => { storedProtocol === 'anthropic' || storedProtocol === 'responses' ? storedProtocol - : 'chat_completions' + : defaultProviderApiProtocol(newAccount.platform) if (!cnSupportsNativeResponses(newAccount.platform, currentOpenCodeOrCNMode()) && editApiProtocol.value === 'responses') { editApiProtocol.value = 'chat_completions' } @@ -5021,6 +5026,20 @@ const syncFormFromAccount = (newAccount: Account | null) => { // Load model mappings and detect mode loadModelRestrictionFromMapping(credentials.model_mapping as Record | undefined) + if ( + newAccount.platform === 'ollama_cloud' && + allowedModels.value.length === 0 && + modelMappings.value.length === 0 && + Array.isArray((newAccount.extra as Record | undefined)?.allowed_models) + ) { + const extraAllowed = ((newAccount.extra as Record).allowed_models as unknown[]) + .filter((item): item is string => typeof item === 'string' && item.trim() !== '') + .map((item) => item.trim()) + if (extraAllowed.length > 0) { + allowedModels.value = Array.from(new Set(extraAllowed)) + modelRestrictionMode.value = 'whitelist' + } + } // Load pool mode poolModeEnabled.value = credentials.pool_mode === true @@ -5410,12 +5429,20 @@ const applyAccountSchedulingThresholdOverridePatch = ( currentCredentials: Record, platform: Account['platform'] | undefined = props.account?.platform ) => { - if (!supportsAccountSchedulingThresholdOverridePlatform(platform) || isOllamaCreditsAccount.value) { + if (!supportsAccountSchedulingThresholdOverridePlatform(platform)) { return } const current = normalizeAccountSchedulingThresholdOverride( currentCredentials[ACCOUNT_SCHEDULING_THRESHOLD_CREDENTIAL_KEY] ) + if (isOllamaCreditsAccount.value) { + if (current !== null) { + credentials[ACCOUNT_SCHEDULING_THRESHOLD_CREDENTIAL_KEY] = null + } else { + delete credentials[ACCOUNT_SCHEDULING_THRESHOLD_CREDENTIAL_KEY] + } + return + } if (!accountSchedulingThresholdOverrideEnabled.value) { if (current !== null) { credentials[ACCOUNT_SCHEDULING_THRESHOLD_CREDENTIAL_KEY] = null diff --git a/frontend/src/components/account/__tests__/EditAccountModal.spec.ts b/frontend/src/components/account/__tests__/EditAccountModal.spec.ts index 7d37d7b9b7..28a347e1e9 100644 --- a/frontend/src/components/account/__tests__/EditAccountModal.spec.ts +++ b/frontend/src/components/account/__tests__/EditAccountModal.spec.ts @@ -2224,3 +2224,85 @@ describe('EditAccountModal temp-unschedulable rule scope', () => { wrapper.unmount() }) }) + +describe('EditAccountModal ollama_cloud regressions', () => { + beforeEach(() => { + resetPlatformCatalog() + authIsSimpleMode.value = true + updateAccountMock.mockReset() + checkMixedChannelRiskMock.mockReset().mockResolvedValue({ has_risk: false }) + }) + + it('defaults missing api_protocol to adaptive and loads extra.allowed_models whitelist', async () => { + const account = { + ...buildAccount(), + platform: 'ollama_cloud', + type: 'apikey', + credentials: { + api_key: 'sk-ollama', + account_mode: 'ollama_legacy' + }, + extra: { + allowed_models: ['qwen3.5:397b', 'gpt-oss:120b'] + } + } as any + updateAccountMock.mockResolvedValue(account) + const wrapper = mountModal(account) + + expect(wrapper.get('[data-testid="model-whitelist-value"]').text()).toBe('qwen3.5:397b,gpt-oss:120b') + + await wrapper.get('form#edit-account-form').trigger('submit.prevent') + expect(updateAccountMock).toHaveBeenCalledTimes(1) + const submitted = updateAccountMock.mock.calls[0]?.[1]?.credentials + expect(submitted).toMatchObject({ + account_mode: 'ollama_legacy', + api_protocol: 'adaptive', + base_url: 'https://ollama.com/v1', + api_base_urls: { + chat_completions: 'https://ollama.com/v1', + responses: 'https://ollama.com/v1', + anthropic: 'https://ollama.com' + } + }) + wrapper.unmount() + }) + + it('preserves custom base_url in chat_completions protocol and clears threshold override when switching to credits', async () => { + const account = { + ...buildAccount(), + platform: 'ollama_cloud', + type: 'apikey', + credentials: { + api_key: 'sk-ollama', + account_mode: 'ollama_legacy', + api_protocol: 'chat_completions', + base_url: 'https://ollama-relay.internal/v1', + account_scheduling_threshold: 85, + model_mapping: { 'qwen3.5:397b': 'qwen3.5:397b' } + }, + extra: {} + } as any + updateAccountMock.mockResolvedValue(account) + const wrapper = mountModal(account) + + const creditsBtn = wrapper + .get('[data-testid="edit-generic-account-mode"]') + .findAll('button') + .find((btn) => btn.text().includes('admin.accounts.ollamaCloud.accountMode.credits')) + expect(creditsBtn).toBeDefined() + await creditsBtn!.trigger('click') + await flushPromises() + + await wrapper.get('form#edit-account-form').trigger('submit.prevent') + expect(updateAccountMock).toHaveBeenCalledTimes(1) + const submitted = updateAccountMock.mock.calls[0]?.[1]?.credentials + expect(submitted).toMatchObject({ + account_mode: 'ollama_credits', + api_protocol: 'chat_completions', + base_url: 'https://ollama-relay.internal/v1', + monthly_credit_usd: 60, + account_scheduling_threshold: null + }) + wrapper.unmount() + }) +}) diff --git a/frontend/src/components/account/credentialsBuilder.ts b/frontend/src/components/account/credentialsBuilder.ts index 0218070e19..d55a4ab595 100644 --- a/frontend/src/components/account/credentialsBuilder.ts +++ b/frontend/src/components/account/credentialsBuilder.ts @@ -385,6 +385,20 @@ export function providerRoutesByModel(platform: string): boolean { return getProviderProfile(platform)?.routing === 'by_model' } +/** 供应商未显式配置 api_protocol 时的默认协议(与后端 Account.GetAPIProtocol 对齐)。 */ +export function defaultProviderApiProtocol(platform: string): CnApiProtocol { + const configured = getProviderProfile(platform)?.default_api_protocol + if ( + configured === 'adaptive' || + configured === 'chat_completions' || + configured === 'anthropic' || + configured === 'responses' + ) { + return configured + } + return providerRoutesByModel(platform) ? 'adaptive' : 'chat_completions' +} + /** 上游模型目录给出每个模型支持的协议;未命中分流规则的模型据此选协议。 */ export function providerHasModelCatalog(platform: string): boolean { return getProviderProfile(platform)?.model_catalog === true diff --git a/frontend/src/constants/platformCatalog.builtin.json b/frontend/src/constants/platformCatalog.builtin.json index c4fec9271f..f5d2000f12 100644 --- a/frontend/src/constants/platformCatalog.builtin.json +++ b/frontend/src/constants/platformCatalog.builtin.json @@ -286,6 +286,7 @@ "cn_provider": false, "multi_protocol": { "default_mode": "ollama_legacy", + "default_api_protocol": "adaptive", "routing": "by_inbound", "modes": [ { diff --git a/frontend/src/constants/platformCatalog.ts b/frontend/src/constants/platformCatalog.ts index 16bc44e22a..24020af317 100644 --- a/frontend/src/constants/platformCatalog.ts +++ b/frontend/src/constants/platformCatalog.ts @@ -30,6 +30,7 @@ export interface ProviderModeInfo { export interface ProviderProfileInfo { default_mode: string + default_api_protocol?: string routing: ProviderRouting responses_path?: string /** 上游 /models 为每个模型给出 supported_endpoints,按模型分流时未命中规则的模型据此选协议。 */