diff --git a/frontend/src/components/account/CreateAccountModal.vue b/frontend/src/components/account/CreateAccountModal.vue index 55c9035363..b900bc1975 100644 --- a/frontend/src/components/account/CreateAccountModal.vue +++ b/frontend/src/components/account/CreateAccountModal.vue @@ -4123,6 +4123,7 @@ import { isCNProviderPlatform, isHeaderOverrideCapable, isMultiProtocolApiKeyPlatform, + parseOllamaMonthlyCreditUsd, providerAccountModes, providerModeLabel, providerNativeProtocols, @@ -6037,10 +6038,12 @@ const handleSubmit = async () => { credentials.account_mode = currentOpenCodeOrCNMode() if (form.platform === 'ollama_cloud' && ollamaAccountMode.value === 'ollama_credits') { // Ollama Cloud credits 模式需同时录入月度信用额度(USD)作为分母。 - const monthlyCreditUsd = Number.parseFloat(ollamaMonthlyCreditUsdInput.value) - if (Number.isFinite(monthlyCreditUsd) && monthlyCreditUsd > 0) { - credentials.monthly_credit_usd = monthlyCreditUsd + const monthlyCreditUsd = parseOllamaMonthlyCreditUsd(ollamaMonthlyCreditUsdInput.value) + if (monthlyCreditUsd === null) { + appStore.showError(t('admin.accounts.ollamaCloud.accountMode.monthlyCreditUsdInvalid')) + return } + credentials.monthly_credit_usd = monthlyCreditUsd } credentials.api_protocol = apiProtocol.value if (apiProtocol.value === 'adaptive') { diff --git a/frontend/src/components/account/EditAccountModal.vue b/frontend/src/components/account/EditAccountModal.vue index 949ac2386b..0d757f98e0 100644 --- a/frontend/src/components/account/EditAccountModal.vue +++ b/frontend/src/components/account/EditAccountModal.vue @@ -148,6 +148,19 @@ + +
+ + +
@@ -3174,6 +3187,7 @@ import { defaultOpenCodeProtocolRules, defaultProviderProtocolRules, isMultiProtocolApiKeyPlatform, + parseOllamaMonthlyCreditUsd, parseOpenCodeGoProtocolRules, providerAccountModes, providerModeLabel, @@ -3396,6 +3410,11 @@ const isGenericMultiProtocolAccount = computed( props.account?.platform !== 'opencode_go' ) const genericAccountModes = computed(() => providerAccountModes(props.account?.platform ?? '')) +// Ollama Cloud credits 模式的月度信用额度(USD)输入;仅 credits 模式写入凭据。 +const editOllamaMonthlyCreditUsd = ref('60') +const isOllamaCreditsAccount = computed( + () => isGenericMultiProtocolAccount.value && props.account?.platform === 'ollama_cloud' && editAccountMode.value === 'ollama_credits' +) // CnBaseUrlPresets 的 platform prop 是平台字面量联合类型,模板里不能写 // `as` 断言(其中的 `|` 会被 eslint 误判为 Vue2 filter 语法),经此 computed 传递。 const cnPresetPlatform = computed(() => { @@ -4421,6 +4440,10 @@ const syncFormFromAccount = (newAccount: Account | null) => { } else { editAccountMode.value = resolveProviderAccountMode(newAccount.platform, credentials.account_mode) } + if (newAccount.platform === 'ollama_cloud') { + const storedCredit = Number(credentials.monthly_credit_usd) + editOllamaMonthlyCreditUsd.value = Number.isFinite(storedCredit) && storedCredit > 0 ? String(storedCredit) : '60' + } const storedProtocol = credentials.api_protocol editApiProtocol.value = storedProtocol === 'adaptive' || @@ -5180,6 +5203,14 @@ const handleSubmit = async () => { } } + const ollamaMonthlyCreditUsd = isOllamaCreditsAccount.value + ? parseOllamaMonthlyCreditUsd(editOllamaMonthlyCreditUsd.value) + : null + if (isOllamaCreditsAccount.value && ollamaMonthlyCreditUsd === null) { + appStore.showError(t('admin.accounts.ollamaCloud.accountMode.monthlyCreditUsdInvalid')) + return + } + const updatePayload: Record = { ...form } try { // 后端期望 proxy_id: 0 表示清除代理,而不是 null @@ -5218,6 +5249,10 @@ const handleSubmit = async () => { // 国产供应商:模式与协议写入凭据(决定额度/余额探测与转发端点/格式)。 if (isCNApiKeyAccount.value) { newCredentials.account_mode = currentOpenCodeOrCNMode() + if (props.account.platform === 'ollama_cloud') { + if (ollamaMonthlyCreditUsd !== null) newCredentials.monthly_credit_usd = ollamaMonthlyCreditUsd + else delete newCredentials.monthly_credit_usd + } newCredentials.api_protocol = editApiProtocol.value if (editApiProtocol.value === 'adaptive') { const defaults = defaultCNAdaptiveBaseUrls(adaptivePresetPlatform.value, currentOpenCodeOrCNMode()) diff --git a/frontend/src/components/account/__tests__/CreateAccountModal.spec.ts b/frontend/src/components/account/__tests__/CreateAccountModal.spec.ts index 01e5e0d861..4460809d91 100644 --- a/frontend/src/components/account/__tests__/CreateAccountModal.spec.ts +++ b/frontend/src/components/account/__tests__/CreateAccountModal.spec.ts @@ -981,6 +981,19 @@ describe('CreateAccountModal Ollama Cloud protocol and quota mode', () => { }) }) + it('rejects ollama_credits mode without a positive monthly credit', async () => { + const wrapper = mountModal() + await selectOllamaCloudPlatform(wrapper) + await selectButtonByText(wrapper, 'admin.accounts.ollamaCloud.accountMode.credits') + await wrapper.get('[data-testid="ollama-monthly-credit-usd"]').setValue('0') + await wrapper.get('form#create-account-form input[type="text"]').setValue('ollama credits') + await wrapper.get('form#create-account-form input[type="password"]').setValue('sk-ollama') + await wrapper.get('form#create-account-form').trigger('submit.prevent') + await flushPromises() + + expect(createAccountMock).not.toHaveBeenCalled() + }) + it('applies the 300 USD quick preset to the monthly credit input', async () => { const wrapper = mountModal() await selectOllamaCloudPlatform(wrapper) diff --git a/frontend/src/components/account/__tests__/EditAccountModal.spec.ts b/frontend/src/components/account/__tests__/EditAccountModal.spec.ts index 2281bacd4d..b1a3836efa 100644 --- a/frontend/src/components/account/__tests__/EditAccountModal.spec.ts +++ b/frontend/src/components/account/__tests__/EditAccountModal.spec.ts @@ -516,6 +516,55 @@ describe('EditAccountModal', () => { }) }) + it('edits the Ollama Cloud monthly credit in credits mode', async () => { + const account = buildAccount() + account.platform = 'ollama_cloud' + account.credentials = { + api_key: 'sk-ollama', + account_mode: 'ollama_credits', + api_protocol: 'chat_completions', + base_url: 'https://ollama.com/v1', + monthly_credit_usd: 60 + } + updateAccountMock.mockReset().mockResolvedValue(account) + checkMixedChannelRiskMock.mockReset().mockResolvedValue({ has_risk: false }) + + const wrapper = mountModal(account) + const input = wrapper.get('[data-testid="edit-ollama-monthly-credit-usd"]') + expect((input.element as HTMLInputElement).value).toBe('60') + await input.setValue('300') + await wrapper.get('form#edit-account-form').trigger('submit.prevent') + + expect(updateAccountMock).toHaveBeenCalledTimes(1) + expect(updateAccountMock.mock.calls[0]?.[1]?.credentials).toMatchObject({ + account_mode: 'ollama_credits', + monthly_credit_usd: 300 + }) + }) + + it('drops the Ollama Cloud monthly credit in legacy mode', async () => { + const account = buildAccount() + account.platform = 'ollama_cloud' + account.credentials = { + api_key: 'sk-ollama', + account_mode: 'ollama_legacy', + api_protocol: 'chat_completions', + base_url: 'https://ollama.com/v1', + monthly_credit_usd: 60 + } + updateAccountMock.mockReset().mockResolvedValue(account) + checkMixedChannelRiskMock.mockReset().mockResolvedValue({ has_risk: false }) + + const wrapper = mountModal(account) + expect(wrapper.find('[data-testid="edit-ollama-monthly-credit-usd"]').exists()).toBe(false) + await wrapper.get('form#edit-account-form').trigger('submit.prevent') + + expect(updateAccountMock).toHaveBeenCalledTimes(1) + const credentials = updateAccountMock.mock.calls[0]?.[1]?.credentials + expect(credentials).toMatchObject({ account_mode: 'ollama_legacy' }) + expect('monthly_credit_usd' in credentials).toBe(false) + }) + it('treats a legacy OpenCode account without account_mode as GO', async () => { const account = buildAccount() account.platform = 'opencode_go' diff --git a/frontend/src/components/account/credentialsBuilder.ts b/frontend/src/components/account/credentialsBuilder.ts index 7f5bb515c8..a0dfbf8dc6 100644 --- a/frontend/src/components/account/credentialsBuilder.ts +++ b/frontend/src/components/account/credentialsBuilder.ts @@ -6,6 +6,12 @@ import { type ProviderProtocolRule } from '@/constants/platformCatalog' +/** Ollama Cloud credits 模式的月度信用额度(USD):仅接受正有限数,否则返回 null。 */ +export function parseOllamaMonthlyCreditUsd(input: string): number | null { + const value = Number.parseFloat(input) + return Number.isFinite(value) && value > 0 ? value : null +} + export function applyInterceptWarmup( credentials: Record, enabled: boolean, diff --git a/frontend/src/i18n/locales/en/admin/accounts.ts b/frontend/src/i18n/locales/en/admin/accounts.ts index df99b01bbd..78bc09cd2d 100644 --- a/frontend/src/i18n/locales/en/admin/accounts.ts +++ b/frontend/src/i18n/locales/en/admin/accounts.ts @@ -298,7 +298,8 @@ export default { credits: 'Credits (monthly pool)', creditsDesc: 'Metered and monitored via a monthly USD credit pool', monthlyCreditUsd: 'Monthly credit (USD)', - monthlyCreditUsdPlaceholder: 'e.g. 60' + monthlyCreditUsdPlaceholder: 'e.g. 60', + monthlyCreditUsdInvalid: 'Monthly credit must be a positive number' }, sessionSecurityHint: 'The browser session is encrypted at rest and sent only to the fixed official settings URL.', configured: 'Configured', diff --git a/frontend/src/i18n/locales/zh/admin/accounts.ts b/frontend/src/i18n/locales/zh/admin/accounts.ts index e2a04fedfb..4b60dc124a 100644 --- a/frontend/src/i18n/locales/zh/admin/accounts.ts +++ b/frontend/src/i18n/locales/zh/admin/accounts.ts @@ -143,7 +143,8 @@ export default { credits: 'Credits(月度信用池)', creditsDesc: '按月度美元信用额度计费与监控', monthlyCreditUsd: '月度信用额度(USD)', - monthlyCreditUsdPlaceholder: '例如 60' + monthlyCreditUsdPlaceholder: '例如 60', + monthlyCreditUsdInvalid: '月度信用额度须为正数' }, sessionSecurityHint: '浏览器会话会加密落库,且只发送到固定的 Ollama 官方设置页。', configured: '已配置',