From 5014cb4de3fe90990a224aba7c5235af6e0cd893 Mon Sep 17 00:00:00 2001 From: initiatione <269353933@qq.com> Date: Wed, 6 May 2026 17:53:07 +0800 Subject: [PATCH] Add configurable SUB2API account priority --- background.js | 16 ++++++ background/panel-bridge.js | 1 + background/steps/platform-verify.js | 1 + content/sub2api-panel.js | 18 ++++++- sidepanel/sidepanel.html | 4 ++ sidepanel/sidepanel.js | 40 ++++++++++++++ tests/background-panel-bridge-module.test.js | 46 ++++++++++++++++ ...background-platform-verify-cpa-api.test.js | 31 +++++++++++ ...idepanel-sub2api-priority-settings.test.js | 52 +++++++++++++++++++ tests/sub2api-panel-proxy.test.js | 46 ++++++++++++++++ 10 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 tests/sidepanel-sub2api-priority-settings.test.js diff --git a/background.js b/background.js index 74003d3..9a053e1 100644 --- a/background.js +++ b/background.js @@ -242,6 +242,7 @@ const DEFAULT_CODEX2API_URL = 'http://localhost:8080/admin/accounts'; const DEFAULT_GPC_HELPER_API_URL = 'https://gopay.hwork.pro'; const DEFAULT_SUB2API_GROUP_NAME = 'codex'; const DEFAULT_SUB2API_PROXY_NAME = ''; +const DEFAULT_SUB2API_ACCOUNT_PRIORITY = 1; const CONTRIBUTION_SOURCE_CPA = 'cpa'; const CONTRIBUTION_SOURCE_SUB2API = 'sub2api'; const CONTRIBUTION_SUB2API_DEFAULT_GROUP_NAME = 'codex号池'; @@ -574,6 +575,7 @@ const PERSISTED_SETTING_DEFAULTS = { sub2apiPassword: '', sub2apiGroupName: DEFAULT_SUB2API_GROUP_NAME, sub2apiGroupNames: DEFAULT_SUB2API_GROUP_NAMES, + sub2apiAccountPriority: DEFAULT_SUB2API_ACCOUNT_PRIORITY, sub2apiDefaultProxyName: DEFAULT_SUB2API_PROXY_NAME, ipProxyEnabled: false, ipProxyService: DEFAULT_IP_PROXY_SERVICE, @@ -2105,6 +2107,18 @@ function normalizeSub2ApiGroupNames(value = '') { return names; } +function normalizeSub2ApiAccountPriority(value, fallback = DEFAULT_SUB2API_ACCOUNT_PRIORITY) { + const rawValue = String(value ?? '').trim(); + const numeric = Number(rawValue); + if (!rawValue || !Number.isSafeInteger(numeric) || numeric < 1) { + const fallbackNumber = Number(fallback); + return Number.isSafeInteger(fallbackNumber) && fallbackNumber >= 1 + ? fallbackNumber + : DEFAULT_SUB2API_ACCOUNT_PRIORITY; + } + return numeric; +} + function normalizePersistentSettingValue(key, value) { switch (key) { case 'panelMode': @@ -2125,6 +2139,8 @@ function normalizePersistentSettingValue(key, value) { return String(value || '').trim(); case 'sub2apiGroupNames': return normalizeSub2ApiGroupNames(value); + case 'sub2apiAccountPriority': + return normalizeSub2ApiAccountPriority(value); case 'sub2apiDefaultProxyName': return String(value || '').trim(); case 'ipProxyEnabled': diff --git a/background/panel-bridge.js b/background/panel-bridge.js index 2b0f077..a4dff4c 100644 --- a/background/panel-bridge.js +++ b/background/panel-bridge.js @@ -293,6 +293,7 @@ sub2apiPassword: state.sub2apiPassword, sub2apiGroupName: groupName, sub2apiDefaultProxyName: state.sub2apiDefaultProxyName, + sub2apiAccountPriority: state.sub2apiAccountPriority, logStep: 7, }, }, { diff --git a/background/steps/platform-verify.js b/background/steps/platform-verify.js index 8237339..e9fb06c 100644 --- a/background/steps/platform-verify.js +++ b/background/steps/platform-verify.js @@ -380,6 +380,7 @@ sub2apiPassword: state.sub2apiPassword, sub2apiGroupName: state.sub2apiGroupName, sub2apiDefaultProxyName: state.sub2apiDefaultProxyName, + sub2apiAccountPriority: state.sub2apiAccountPriority, sub2apiProxyId: state.sub2apiProxyId, sub2apiSessionId: state.sub2apiSessionId, sub2apiOAuthState: state.sub2apiOAuthState, diff --git a/content/sub2api-panel.js b/content/sub2api-panel.js index 1bf82b7..8f5c39c 100644 --- a/content/sub2api-panel.js +++ b/content/sub2api-panel.js @@ -252,6 +252,21 @@ function resolveSub2ApiProxyPreference(payload = {}, backgroundState = {}) { return SUB2API_DEFAULT_PROXY_NAME; } +function resolveSub2ApiAccountPriority(payload = {}, backgroundState = {}) { + const candidate = payload.sub2apiAccountPriority !== undefined + ? payload.sub2apiAccountPriority + : backgroundState.sub2apiAccountPriority; + const rawValue = String(candidate ?? '').trim(); + if (!rawValue) { + return SUB2API_DEFAULT_PRIORITY; + } + const numeric = Number(rawValue); + if (!Number.isSafeInteger(numeric) || numeric < 1) { + throw new Error('SUB2API 账号优先级必须是大于等于 1 的整数。'); + } + return numeric; +} + function normalizeProxyId(value) { if (value === undefined || value === null || value === '') { return null; @@ -568,6 +583,7 @@ async function step9_submitOpenAiCallback(payload = {}) { const proxySelector = preferredProxyId || proxyPreference; const proxy = proxySelector ? await resolveSub2ApiProxy(origin, token, proxySelector) : null; const proxyId = normalizeProxyId(proxy?.id); + const accountPriority = resolveSub2ApiAccountPriority(payload, backgroundState); const storedGroupIds = Array.isArray(payload.sub2apiGroupIds) ? payload.sub2apiGroupIds : (Array.isArray(backgroundState.sub2apiGroupIds) ? backgroundState.sub2apiGroupIds : []); @@ -627,7 +643,7 @@ async function step9_submitOpenAiCallback(payload = {}) { type: 'oauth', credentials, concurrency: SUB2API_DEFAULT_CONCURRENCY, - priority: SUB2API_DEFAULT_PRIORITY, + priority: accountPriority, rate_multiplier: SUB2API_DEFAULT_RATE_MULTIPLIER, group_ids: groupIds, auto_pause_on_expired: true, diff --git a/sidepanel/sidepanel.html b/sidepanel/sidepanel.html index 6c96092..43ffcbe 100644 --- a/sidepanel/sidepanel.html +++ b/sidepanel/sidepanel.html @@ -210,6 +210,10 @@ +