fix(frontend): Ollama Cloud 编辑弹窗支持月度信用额度,非法额度拒绝提交

编辑弹窗缺少 monthly_credit_usd 输入,credits 账号建后无法修改额度;新建时
非法额度被静默丢弃。credits 模式编辑可改额度、legacy 模式移除该字段,两处
均校验正数。
This commit is contained in:
shaw committed 2026-10-09 17:20:49 +08:00
1 parent 68de872287
commit 7a79afd06b
7 files changed
+113 -5

No files matched your search

@@ -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') {
@@ -148,6 +148,19 @@
</button>
</div>
</div>
<!-- Ollama Cloud credits 模式的月度信用额度(USD):额度分母 -->
<div v-if="isOllamaCreditsAccount" data-testid="edit-ollama-monthly-credit">
<label class="input-label">{{ t('admin.accounts.ollamaCloud.accountMode.monthlyCreditUsd') }}</label>
<input
v-model="editOllamaMonthlyCreditUsd"
type="number"
min="0"
step="1"
class="input"
:placeholder="t('admin.accounts.ollamaCloud.accountMode.monthlyCreditUsdPlaceholder')"
data-testid="edit-ollama-monthly-credit-usd"
/>
</div>
<!-- Account Mode Selection (CN providers) -->
<div v-if="isCNApiKeyAccount && isCNProviderPlatform(account.platform)">
<label class="input-label">{{ t('admin.accounts.cnProviders.accountMode.title') }}</label>
@@ -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<CnProviderPlatform>(() => {
@@ -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<string, unknown> = { ...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())
@@ -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)
@@ -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'
@@ -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<string, unknown>,
enabled: boolean,
@@ -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',
@@ -143,7 +143,8 @@ export default {
credits: 'Credits(月度信用池)',
creditsDesc: '按月度美元信用额度计费与监控',
monthlyCreditUsd: '月度信用额度(USD)',
monthlyCreditUsdPlaceholder: '例如 60'
monthlyCreditUsdPlaceholder: '例如 60',
monthlyCreditUsdInvalid: '月度信用额度须为正数'
},
sessionSecurityHint: '浏览器会话会加密落库,且只发送到固定的 Ollama 官方设置页。',
configured: '已配置',