mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
fix(frontend, catalog): 导出多协议默认协议并修复 ollama_cloud 编辑弹窗协议/基址/白名单回填
This commit is contained in:
1 parent
e3c23b25e0
commit
ad1ead4541
7 files changed
+139
-11
No files matched your search
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<string, unknown> | undefined)
|
||||
if (
|
||||
newAccount.platform === 'ollama_cloud' &&
|
||||
allowedModels.value.length === 0 &&
|
||||
modelMappings.value.length === 0 &&
|
||||
Array.isArray((newAccount.extra as Record<string, unknown> | undefined)?.allowed_models)
|
||||
) {
|
||||
const extraAllowed = ((newAccount.extra as Record<string, unknown>).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<string, unknown>,
|
||||
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
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
})
|
||||
@@ -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
|
||||
|
||||
@@ -286,6 +286,7 @@
|
||||
"cn_provider": false,
|
||||
"multi_protocol": {
|
||||
"default_mode": "ollama_legacy",
|
||||
"default_api_protocol": "adaptive",
|
||||
"routing": "by_inbound",
|
||||
"modes": [
|
||||
{
|
||||
|
||||
@@ -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,按模型分流时未命中规则的模型据此选协议。 */
|
||||
|
||||
Reference in new issue
Block a user