From f34df0e313b1a5e3ae8a929aaeca237c9062ee21 Mon Sep 17 00:00:00 2001 From: ninedrunk Date: Sat, 28 Nov 2026 12:52:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(accounts):=20=E8=AE=A9=E6=89=80?= =?UTF-8?q?=E6=9C=89=E5=8F=97=E6=94=AF=E6=8C=81=E5=B9=B3=E5=8F=B0=E9=83=BD?= =?UTF-8?q?=E8=83=BD=E9=85=8D=E7=BD=AE=E8=B4=A6=E5=8F=B7=E7=BA=A7=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=81=9C=E8=B0=83=E9=98=88=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 后端已对 AllowedSchedulingThresholdPlatforms 中所有平台支持账号级 account_scheduling_threshold 覆盖,系统设置页也通过 SCHEDULING_THRESHOLD_PLATFORMS 展示了完整清单。 但账号编辑弹窗把 override 支持平台硬编码为 openai / anthropic / grok, 导致 Kimi / Zhipu GLM / MiniMax / OpenCode Go / Command Code 账号 即使后端完全支持,也没有 UI 可以配置账号级阈值。 创建账号弹窗则完全没有该选项。 改动: - 编辑弹窗改为从 SCHEDULING_THRESHOLD_PLATFORMS 派生支持平台, 消除前后端清单漂移; - 创建账号弹窗新增同样的账号级阈值覆盖入口; - 沿用 credentials.account_scheduling_threshold 键及原有语义。 不改动后端逻辑,不影响现有平台行为。 --- .../components/account/CreateAccountModal.vue | 54 +++++ .../components/account/EditAccountModal.vue | 6 +- ...itAccountModal.schedulingThreshold.spec.ts | 189 ++++++++++++++++++ .../src/i18n/locales/en/admin/accounts.ts | 1 + .../src/i18n/locales/zh/admin/accounts.ts | 1 + 5 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 frontend/src/components/account/__tests__/EditAccountModal.schedulingThreshold.spec.ts diff --git a/frontend/src/components/account/CreateAccountModal.vue b/frontend/src/components/account/CreateAccountModal.vue index 24ba7aed1c..8478f2056c 100644 --- a/frontend/src/components/account/CreateAccountModal.vue +++ b/frontend/src/components/account/CreateAccountModal.vue @@ -581,6 +581,40 @@ + +
+
+
+ +

+ {{ t('admin.accounts.cnProviders.schedulingThresholdCreateHint') }} +

+
+ +
+
+ + +

{{ t('admin.accounts.accountSchedulingThresholdOverrideDisabledHint') }}

+
+
+
@@ -4164,6 +4198,7 @@ import { isValidWildcardPattern } from '@/composables/useModelWhitelist' import { adminAPI } from '@/api/admin' +import { SCHEDULING_THRESHOLD_PLATFORMS } from '@/api/admin/settings' import { useQuotaNotifyState } from '@/composables/useQuotaNotifyState' import { useAccountOAuth, @@ -4479,6 +4514,14 @@ const isGenericMultiProtocolPlatform = computed( const extraMultiProtocolPlatforms = computed(() => listPlatforms().filter(spec => !!spec.multi_protocol && !isCNProviderPlatform(spec.id) && spec.id !== 'opencode_go') ) +// 账号级自动停调阈值覆盖(创建时可选):写入 credentials.account_scheduling_threshold。 +// 仅对后端白名单平台显示;关闭/留空 = 跟随系统平台阈值。 +const ACCOUNT_SCHEDULING_THRESHOLD_CREDENTIAL_KEY = 'account_scheduling_threshold' +const createSchedulingThresholdOverrideEnabled = ref(false) +const createSchedulingThresholdOverrideValue = ref(100) +const supportsCreateSchedulingThresholdOverride = computed(() => + (SCHEDULING_THRESHOLD_PLATFORMS as readonly string[]).includes(form.platform) +) const genericAccountModes = computed(() => providerAccountModes(form.platform)) // 按模型分流的供应商(OpenCode 等):adaptive 账号携带 protocol_rules。 const routesByModel = computed(() => providerRoutesByModel(form.platform)) @@ -6241,6 +6284,17 @@ const handleSubmit = async () => { } } + // 账号级自动停调阈值覆盖:仅白名单平台、勾选且值在 1..99 时写入 + // (100 = 禁用当前账号自动停调,不写则跟随系统平台阈值)。 + if ( + supportsCreateSchedulingThresholdOverride.value && + createSchedulingThresholdOverrideEnabled.value + ) { + const numeric = Math.trunc(Number(createSchedulingThresholdOverrideValue.value) || 100) + const clamped = Math.min(100, Math.max(1, numeric)) + credentials[ACCOUNT_SCHEDULING_THRESHOLD_CREDENTIAL_KEY] = clamped + } + // Add model mapping if configured(OpenAI 开启自动透传时不应用) if (!isOpenAIModelRestrictionDisabled.value) { const modelMapping = buildModelMappingObject(modelRestrictionMode.value, allowedModels.value, modelMappings.value) diff --git a/frontend/src/components/account/EditAccountModal.vue b/frontend/src/components/account/EditAccountModal.vue index e1e50ec52a..15a1836ef0 100644 --- a/frontend/src/components/account/EditAccountModal.vue +++ b/frontend/src/components/account/EditAccountModal.vue @@ -3422,6 +3422,7 @@ import { useI18n } from 'vue-i18n' import { useAppStore } from '@/stores/app' import { adminAPI } from '@/api/admin' +import { SCHEDULING_THRESHOLD_PLATFORMS } from '@/api/admin/settings' import { useQuotaNotifyState } from '@/composables/useQuotaNotifyState' import type { Account, @@ -5353,7 +5354,10 @@ const applyTempUnschedConfig = (credentials: Record) => { function supportsAccountSchedulingThresholdOverridePlatform(platform: Account['platform'] | undefined) { - return platform === 'openai' || platform === 'anthropic' || platform === 'grok' + // 与后端 AllowedSchedulingThresholdPlatforms 共用同一清单(系统设置页也用这份), + // 避免前端入口与后端白名单再次漂移。 + if (!platform) return false + return (SCHEDULING_THRESHOLD_PLATFORMS as readonly string[]).includes(platform) } function normalizeAccountSchedulingThresholdOverride(value: unknown): number | null { diff --git a/frontend/src/components/account/__tests__/EditAccountModal.schedulingThreshold.spec.ts b/frontend/src/components/account/__tests__/EditAccountModal.schedulingThreshold.spec.ts new file mode 100644 index 0000000000..db6052eaa5 --- /dev/null +++ b/frontend/src/components/account/__tests__/EditAccountModal.schedulingThreshold.spec.ts @@ -0,0 +1,189 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { defineComponent } from 'vue' +import { flushPromises, mount } from '@vue/test-utils' + +// 账号级自动停调阈值覆盖:GLM / MiniMax / OpenCode / Command Code 等 +// 套餐平台现在都能在账号编辑页设置该覆盖(此前仅 openai/anthropic/grok)。 + +const { updateAccountMock, checkMixedChannelRiskMock, authIsSimpleMode } = vi.hoisted(() => ({ + updateAccountMock: vi.fn(), + checkMixedChannelRiskMock: vi.fn(), + authIsSimpleMode: { value: true } +})) + +vi.mock('@/stores/app', () => ({ + useAppStore: () => ({ + showError: vi.fn(), + showSuccess: vi.fn(), + showInfo: vi.fn() + }) +})) + +vi.mock('@/stores/auth', () => ({ + useAuthStore: () => ({ + get isSimpleMode() { + return authIsSimpleMode.value + } + }) +})) + +vi.mock('@/api/admin', () => ({ + adminAPI: { + accounts: { + update: updateAccountMock, + checkMixedChannelRisk: checkMixedChannelRiskMock, + previewUpstreamGroup: vi.fn() + }, + settings: { + getWebSearchEmulationConfig: vi.fn().mockResolvedValue({ enabled: false, providers: [] }), + getSettings: vi.fn().mockResolvedValue({}) + }, + tlsFingerprintProfiles: { + list: vi.fn().mockResolvedValue([]) + } + } +})) + +vi.mock('@/api/admin/accounts', () => ({ + getAntigravityDefaultModelMapping: vi.fn() +})) + +vi.mock('vue-i18n', async () => { + const actual = await vi.importActual('vue-i18n') + return { + ...actual, + useI18n: () => ({ t: (key: string) => key }) + } +}) + +import EditAccountModal from '../EditAccountModal.vue' + +const BaseDialogStub = defineComponent({ + name: 'BaseDialog', + props: { show: { type: Boolean, default: false } }, + template: '
' +}) + +const GroupSelectorStub = defineComponent({ + name: 'GroupSelector', + props: { modelValue: { type: Array, default: () => [] } }, + emits: ['update:modelValue'], + template: '
' +}) + +const ModelWhitelistSelectorStub = defineComponent({ + name: 'ModelWhitelistSelector', + props: { modelMappings: { type: Array, default: () => [] }, modelValue: { type: Array, default: () => [] } }, + emits: ['update:modelValue'], + template: '
' +}) + +const SelectStub = defineComponent({ + name: 'SelectStub', + props: { + modelValue: { type: [String, Number, Boolean, null], default: '' }, + options: { type: Array, default: () => [] } + }, + emits: ['update:modelValue'], + template: ` + + ` +}) + +function buildAccount(platform: string, credentials: Record = {}) { + return { + id: 1, + name: `account-${platform}`, + notes: '', + platform, + type: 'apikey', + credentials: { api_key: 'key-test', base_url: '', ...credentials }, + extra: {}, + proxy_id: null, + concurrency: 1, + priority: 1, + rate_multiplier: 1, + status: 'active', + group_ids: [], + expires_at: null, + auto_pause_on_expired: false + } as any +} + +function mountModal(account: any) { + return mount(EditAccountModal, { + props: { show: true, account, proxies: [], groups: [] }, + global: { + stubs: { + BaseDialog: BaseDialogStub, + Select: SelectStub, + Icon: true, + ProxySelector: true, + GroupSelector: GroupSelectorStub, + ModelWhitelistSelector: ModelWhitelistSelectorStub + } + } + }) +} + +describe('EditAccountModal 账号自动停调阈值覆盖(套餐平台)', () => { + beforeEach(() => { + authIsSimpleMode.value = true + updateAccountMock.mockReset() + checkMixedChannelRiskMock.mockReset().mockResolvedValue({ has_risk: false }) + }) + + it.each(['zhipu', 'minimax', 'opencode_go', 'command_code'])( + '%s 账号显示阈值覆盖区块并能提交 account_scheduling_threshold', + async (platform) => { + const account = buildAccount(platform, { + account_mode: platform === 'zhipu' ? 'coding' : 'payg' + }) + updateAccountMock.mockResolvedValue(account) + const wrapper = mountModal(account) + + expect(wrapper.find('[data-testid="account-scheduling-threshold-section"]').exists()).toBe(true) + + await wrapper.get('[data-testid="account-scheduling-threshold-override-enabled"]').setValue(true) + await wrapper.get('[data-testid="account-scheduling-threshold-override-value"]').setValue('50') + await wrapper.get('form#edit-account-form').trigger('submit.prevent') + await flushPromises() + + expect(updateAccountMock).toHaveBeenCalledTimes(1) + expect(updateAccountMock.mock.calls[0]?.[1]?.credentials?.account_scheduling_threshold).toBe(50) + wrapper.unmount() + } + ) + + it('deepseek(余额型)不显示阈值覆盖区块', async () => { + const account = buildAccount('deepseek', { account_mode: 'payg' }) + updateAccountMock.mockResolvedValue(account) + const wrapper = mountModal(account) + expect(wrapper.find('[data-testid="account-scheduling-threshold-section"]').exists()).toBe(false) + wrapper.unmount() + }) + + it('关闭覆盖后把 account_scheduling_threshold 置空(回退系统阈值)', async () => { + const account = buildAccount('zhipu', { + account_mode: 'coding', + account_scheduling_threshold: 50 + }) + updateAccountMock.mockResolvedValue(account) + const wrapper = mountModal(account) + + const enabled = wrapper.get('[data-testid="account-scheduling-threshold-override-enabled"]') + expect(enabled.element.checked).toBe(true) + const value = wrapper.get('[data-testid="account-scheduling-threshold-override-value"]') + expect(value.element.value).toBe('50') + + await enabled.setValue(false) + await wrapper.get('form#edit-account-form').trigger('submit.prevent') + await flushPromises() + + expect(updateAccountMock).toHaveBeenCalledTimes(1) + expect(updateAccountMock.mock.calls[0]?.[1]?.credentials?.account_scheduling_threshold).toBeNull() + wrapper.unmount() + }) +}) diff --git a/frontend/src/i18n/locales/en/admin/accounts.ts b/frontend/src/i18n/locales/en/admin/accounts.ts index bfdfec7f55..6da92454f7 100644 --- a/frontend/src/i18n/locales/en/admin/accounts.ts +++ b/frontend/src/i18n/locales/en/admin/accounts.ts @@ -154,6 +154,7 @@ export default { example: 'Example: …/organization/org-0610bE2D…/projects/proj_0798F20…/api_keys → org-0610bE2D… goes into "Organization ID", proj_0798F20… into "Project ID"', }, }, + schedulingThresholdCreateHint: 'Optional: override the platform auto-pause threshold for this account (upstream usage-window percent). Leave unchecked to follow the system setting.', balance: 'Balance --', window5h: '5h', windowWeekly: '7d', diff --git a/frontend/src/i18n/locales/zh/admin/accounts.ts b/frontend/src/i18n/locales/zh/admin/accounts.ts index 0d35c23776..4cb68a4bb7 100644 --- a/frontend/src/i18n/locales/zh/admin/accounts.ts +++ b/frontend/src/i18n/locales/zh/admin/accounts.ts @@ -367,6 +367,7 @@ export default { example: '示例:…/organization/org-0610bE2D…/projects/proj_0798F20…/api_keys → org-0610bE2D… 填「组织 ID」,proj_0798F20… 填「项目 ID」', }, }, + schedulingThresholdCreateHint: '可选:为该账号覆盖平台级自动停调阈值(按上游原生窗口百分比)。不勾选则跟随系统设置。', balance: '余额 --', window5h: '5h', windowWeekly: '7d', From 0dba88681bd983a64913e2452545743194d28f34 Mon Sep 17 00:00:00 2001 From: shaw Date: Wed, 2 Dec 2026 11:45:21 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20hide=20create-account=20scheduling?= =?UTF-8?q?=20threshold=20unless=20submit=20writes=20it=20and=20reset=20it?= =?UTF-8?q?=20on=20close=EF=BC=88audit=20follow-up=20for=20#9135=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/account/CreateAccountModal.vue | 20 +++++++- .../__tests__/CreateAccountModal.spec.ts | 47 +++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/account/CreateAccountModal.vue b/frontend/src/components/account/CreateAccountModal.vue index 8478f2056c..f5ba3d6df8 100644 --- a/frontend/src/components/account/CreateAccountModal.vue +++ b/frontend/src/components/account/CreateAccountModal.vue @@ -4519,8 +4519,22 @@ const extraMultiProtocolPlatforms = computed(() => const ACCOUNT_SCHEDULING_THRESHOLD_CREDENTIAL_KEY = 'account_scheduling_threshold' const createSchedulingThresholdOverrideEnabled = ref(false) const createSchedulingThresholdOverrideValue = ref(100) -const supportsCreateSchedulingThresholdOverride = computed(() => - (SCHEDULING_THRESHOLD_PLATFORMS as readonly string[]).includes(form.platform) +// handleSubmit 中走到通用 apikey 创建分支的条件:其余分支(OAuth 流程、Bedrock、 +// Antigravity upstream、service_account)都会先行返回,不会写入阈值。 +const submitsViaGenericApiKeyPath = computed( + () => + !isOAuthFlow.value && + !(form.platform === 'anthropic' && accountCategory.value === 'bedrock') && + !(form.platform === 'antigravity' && antigravityAccountType.value === 'upstream') && + !( + (form.platform === 'gemini' || form.platform === 'anthropic') && + accountCategory.value === 'service_account' + ) +) +const supportsCreateSchedulingThresholdOverride = computed( + () => + (SCHEDULING_THRESHOLD_PLATFORMS as readonly string[]).includes(form.platform) && + submitsViaGenericApiKeyPath.value ) const genericAccountModes = computed(() => providerAccountModes(form.platform)) // 按模型分流的供应商(OpenCode 等):adaptive 账号携带 protocol_rules。 @@ -5748,6 +5762,8 @@ const resetForm = () => { upstreamRequestIdHeader.value = '' upstreamBillingAutoProbeEnabled.value = true upstreamGroupSyncEnabled.value = false + createSchedulingThresholdOverrideEnabled.value = false + createSchedulingThresholdOverrideValue.value = 100 editQuotaLimit.value = null editQuotaDailyLimit.value = null editQuotaWeeklyLimit.value = null diff --git a/frontend/src/components/account/__tests__/CreateAccountModal.spec.ts b/frontend/src/components/account/__tests__/CreateAccountModal.spec.ts index 0e30886822..9360bf518d 100644 --- a/frontend/src/components/account/__tests__/CreateAccountModal.spec.ts +++ b/frontend/src/components/account/__tests__/CreateAccountModal.spec.ts @@ -530,6 +530,53 @@ describe('CreateAccountModal OpenAI long-context billing', () => { expect(probeUpstreamBillingMock).not.toHaveBeenCalled() }) + describe('account scheduling threshold override', () => { + const toggleSelector = '[data-testid="create-account-scheduling-threshold"]' + const enabledSelector = '[data-testid="create-account-scheduling-threshold-enabled"]' + const valueSelector = '[data-testid="create-account-scheduling-threshold-value"]' + + it('is hidden for OAuth-based creation, which never writes the threshold', async () => { + const wrapper = mountModal() + await selectButtonByText(wrapper, 'OpenAI') + expect(wrapper.find(toggleSelector).exists()).toBe(false) + + await selectButtonByText(wrapper, 'API Key') + expect(wrapper.find(toggleSelector).exists()).toBe(true) + }) + + it('writes the threshold into credentials for API key accounts', async () => { + const wrapper = mountModal() + await selectButtonByText(wrapper, 'OpenAI') + await selectButtonByText(wrapper, 'API Key') + await wrapper.get(enabledSelector).setValue(true) + await wrapper.get(valueSelector).setValue(80) + await wrapper.get('form#create-account-form input[type="text"]').setValue('threshold account') + await wrapper.get('form#create-account-form input[type="password"]').setValue('test-api-key') + await wrapper.get('form#create-account-form').trigger('submit.prevent') + await flushPromises() + + expect(createAccountMock).toHaveBeenCalledTimes(1) + expect(createAccountMock.mock.calls[0]?.[0]?.credentials?.account_scheduling_threshold).toBe(80) + }) + + it('does not carry the previous value over to the next created account', async () => { + const wrapper = mountModal() + await selectButtonByText(wrapper, 'OpenAI') + await selectButtonByText(wrapper, 'API Key') + await wrapper.get(enabledSelector).setValue(true) + await wrapper.get(valueSelector).setValue(80) + + await wrapper.setProps({ show: false }) + await wrapper.setProps({ show: true }) + await selectButtonByText(wrapper, 'OpenAI') + await selectButtonByText(wrapper, 'API Key') + + expect((wrapper.get(enabledSelector).element as HTMLInputElement).checked).toBe(false) + await wrapper.get(enabledSelector).setValue(true) + expect((wrapper.get(valueSelector).element as HTMLInputElement).value).toBe('100') + }) + }) + it('submits OpenCode Zen default protocol rules with adaptive endpoints', async () => { const wrapper = mountModal() await selectButtonByText(wrapper, 'OpenCode')