diff --git a/backend/internal/handler/gateway_handler.go b/backend/internal/handler/gateway_handler.go index 1a9f69a4a5..ade087fc49 100644 --- a/backend/internal/handler/gateway_handler.go +++ b/backend/internal/handler/gateway_handler.go @@ -1287,10 +1287,10 @@ func (h *GatewayHandler) Models(c *gin.Context) { return } - // ollama_cloud 无静态默认模型列表(defaultModelIDsForPlatform 返回空): - // 账号侧清单缺失时返回空列表,不得回落 Claude 默认模型。 + // ollama_cloud 账号侧清单缺失时回落实测默认目录(DefaultOllamaCloudModelIDs), + // 不得回落 Claude 默认模型。 if platform == service.PlatformOllamaCloud { - writeModelsListResponse(c, []claude.Model{}) + writeModelsList(c, platform, service.DefaultOllamaCloudModelIDs()) return } @@ -1580,17 +1580,14 @@ func defaultModelIDsForPlatform(platform string) []string { case service.PlatformTypeSafe: return []string{"jev-latest"} case service.PlatformOllamaCloud: - // ollama_cloud 没有静态默认模型列表:可服务模型完全来自账号侧的 - // model_mapping / extra.allowed_models(缺失时 IsModelSupported 为 - // deny-all)。default 分支的 Claude 列表对它只会造成虚假广告。 - return nil + return service.DefaultOllamaCloudModelIDs() case service.PlatformComposite: ids := make([]string, 0) seen := make(map[string]struct{}) // TypeSafe is deliberately absent: jev-latest only works through // /v1/systemone, so the static fallback never advertises it to LLM // clients. compositeAvailableModels lists it when the group can serve it. - for _, concretePlatform := range []string{service.PlatformAnthropic, service.PlatformGemini, service.PlatformOpenAI, service.PlatformAntigravity, service.PlatformGrok, service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek, service.PlatformMiniMax, service.PlatformOpenCodeGo} { + for _, concretePlatform := range []string{service.PlatformAnthropic, service.PlatformGemini, service.PlatformOpenAI, service.PlatformAntigravity, service.PlatformGrok, service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek, service.PlatformMiniMax, service.PlatformOpenCodeGo, service.PlatformOllamaCloud} { for _, id := range defaultModelIDsForPlatform(concretePlatform) { if _, ok := seen[id]; ok { continue diff --git a/backend/internal/handler/gateway_handler_ollama_cloud_models_test.go b/backend/internal/handler/gateway_handler_ollama_cloud_models_test.go index 56931d21d6..a1d63ebd09 100644 --- a/backend/internal/handler/gateway_handler_ollama_cloud_models_test.go +++ b/backend/internal/handler/gateway_handler_ollama_cloud_models_test.go @@ -12,11 +12,14 @@ import ( "github.com/stretchr/testify/require" ) -// ollama_cloud 没有静态默认模型列表:defaultModelIDsForPlatform 不得回落 -// Claude 列表(default 分支),/models 在账号侧清单缺失时返回空列表。 -func TestDefaultModelIDsForPlatform_OllamaCloudReturnsEmpty(t *testing.T) { - require.Empty(t, defaultModelIDsForPlatform(service.PlatformOllamaCloud)) +// ollama_cloud 的默认模型目录 = DefaultOllamaCloudModelIDs:defaultModelIDsForPlatform +// 不得回落 Claude 列表(default 分支),/models 在账号侧清单缺失时回落该目录。 +func TestDefaultModelIDsForPlatform_OllamaCloudReturnsDefaultCatalog(t *testing.T) { + require.Equal(t, service.DefaultOllamaCloudModelIDs(), defaultModelIDsForPlatform(service.PlatformOllamaCloud)) require.NotEmpty(t, defaultModelIDsForPlatform(service.PlatformAnthropic)) + for _, id := range defaultModelIDsForPlatform(service.PlatformOllamaCloud) { + require.NotContains(t, id, "claude") + } } func TestGatewayModels_OllamaCloudGroupListsAllowedModels(t *testing.T) { @@ -53,8 +56,8 @@ func TestGatewayModels_OllamaCloudGroupListsAllowedModels(t *testing.T) { require.Equal(t, []string{"gpt-oss:120b"}, modelIDsForTest(got.Data)) } -// 账号侧清单缺失(deny-all)时 /models 必须返回空,而不是回落 Claude 列表。 -func TestGatewayModels_OllamaCloudGroupWithoutListReturnsEmpty(t *testing.T) { +// 账号侧清单缺失(deny-all)时 /models 回落 ollama 默认目录,而不是 Claude 列表。 +func TestGatewayModels_OllamaCloudGroupWithoutListFallsBackToDefaultCatalog(t *testing.T) { gin.SetMode(gin.TestMode) groupID := int64(9202) @@ -80,6 +83,37 @@ func TestGatewayModels_OllamaCloudGroupWithoutListReturnsEmpty(t *testing.T) { require.Equal(t, http.StatusOK, rec.Code) var got gatewayModelsResponseForTest require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got)) - require.Empty(t, got.Data, "ollama_cloud 不得回落 Claude 默认模型列表") - require.NotContains(t, modelIDsForTest(got.Data), "claude-sonnet-4-6") + require.Equal(t, service.DefaultOllamaCloudModelIDs(), modelIDsForTest(got.Data), + "ollama_cloud 不得回落 Claude 默认模型列表,应回落实测默认目录") +} + +// ollama_cloud 分组的 composite 白名单来源不串入 Claude 目录之外的默认回落。 +func TestGatewayModels_CompositeIncludesOllamaDefaultCatalog(t *testing.T) { + gin.SetMode(gin.TestMode) + + groupID := int64(9203) + h := newGatewayModelsHandlerForTest( + &gatewayModelsAccountRepoStub{byGroup: map[int64][]service.Account{groupID: {}}}, + ) + + rec := httptest.NewRecorder() + c, _ := gin.CreateTestContext(rec) + c.Request = httptest.NewRequest(http.MethodGet, "/v1/models", nil) + c.Set(string(middleware2.ContextKeyAPIKey), &service.APIKey{ + Group: &service.Group{ID: groupID, Platform: service.PlatformComposite}, + }) + + h.Models(c) + + require.Equal(t, http.StatusOK, rec.Code) + var got gatewayModelsResponseForTest + require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got)) + ids := modelIDsForTest(got.Data) + idsSet := make(map[string]struct{}, len(ids)) + for _, id := range ids { + idsSet[id] = struct{}{} + } + for _, id := range service.DefaultOllamaCloudModelIDs() { + require.Contains(t, idsSet, id, "composite /v1/models 兜底须含 ollama 目录条目 %q", id) + } } diff --git a/backend/internal/service/admin_group.go b/backend/internal/service/admin_group.go index 909c08bfce..56297543d5 100644 --- a/backend/internal/service/admin_group.go +++ b/backend/internal/service/admin_group.go @@ -300,11 +300,7 @@ func defaultModelsListCandidateIDs(platform string) []string { case PlatformTypeSafe: return []string{typesafe.JevLatestModel} case PlatformOllamaCloud: - // ollama_cloud 没有静态默认模型列表:可服务模型完全来自账号侧的 - // model_mapping / extra.allowed_models(缺失时 IsModelSupported 为 - // deny-all)。default 分支的 Claude 列表对它只会造成虚假广告; - // 显式返回空,与 gateway_handler.defaultModelIDsForPlatform 对齐。 - return nil + return DefaultOllamaCloudModelIDs() case PlatformComposite: return compositeDefaultModelsListCandidateIDs() default: @@ -328,7 +324,7 @@ func compositeDefaultModelsListCandidateIDs() []string { // TypeSafe stays out of the static composite candidates (jev-latest only works // through /v1/systemone); groups with TypeSafe accounts still get it from the // account model mappings collected by GetGroupModelsListCandidates. - for _, platform := range []string{PlatformAnthropic, PlatformGemini, PlatformOpenAI, PlatformAntigravity, PlatformGrok, PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax, PlatformOpenCodeGo} { + for _, platform := range []string{PlatformAnthropic, PlatformGemini, PlatformOpenAI, PlatformAntigravity, PlatformGrok, PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax, PlatformOpenCodeGo, PlatformOllamaCloud} { for _, id := range defaultModelsListCandidateIDs(platform) { if _, ok := seen[id]; ok { continue diff --git a/backend/internal/service/ollama_cloud_models.go b/backend/internal/service/ollama_cloud_models.go new file mode 100644 index 0000000000..a4e462e70d --- /dev/null +++ b/backend/internal/service/ollama_cloud_models.go @@ -0,0 +1,36 @@ +package service + +// DefaultOllamaCloudModelIDs 是 Ollama Cloud 的默认模型目录:实测 +// GET https://ollama.com/v1/models(2026-09-12,HTTP 200,20 个模型, +// owned_by 全部为 ollama,含两个带 tag 的快照名)。供分组默认模型列表、 +// composite 候选与 /v1/models 末尾兜底使用;条目均可被计费价目命中 +// (见 ollama_cloud_pricing_test.go 的逐名钉测)。 +// +// 前端同源静态表:frontend/src/composables/useModelWhitelist.ts 的 +// getModelsByPlatform 'ollama_cloud' case 必须与这里保持同一集合, +// 两侧各有测试钉住(ollama_cloud_models_test.go / useModelWhitelist.spec.ts), +// 改动任一侧须同步另一侧。 +func DefaultOllamaCloudModelIDs() []string { + return []string{ + "nemotron-3-super", + "glm-5.3", + "gpt-oss:120b", + "glm-5.3-flash", + "kimi-k2.6", + "kimi-k3", + "deepseek-v4.1-flash", + "minimax-m2.7", + "mistral-large-3:675b", + "glm-5.1", + "glm-5.2", + "gpt-oss:20b", + "qwen3.5:397b", + "kimi-k2.7-code", + "nemotron-3-nano:30b", + "minimax-m3", + "gemma4:31b", + "nemotron-3-ultra", + "deepseek-v4-flash:0731", + "deepseek-v4-pro:0813", + } +} diff --git a/backend/internal/service/ollama_cloud_models_test.go b/backend/internal/service/ollama_cloud_models_test.go new file mode 100644 index 0000000000..b2e536bbf1 --- /dev/null +++ b/backend/internal/service/ollama_cloud_models_test.go @@ -0,0 +1,73 @@ +//go:build unit + +package service + +// C15/C14/C22:ollama_cloud 默认模型清单的数据源钉测。 +// +// 权威数据:GET https://ollama.com/v1/models 实测(2026-09-12,HTTP 200, +// 20 个模型,含 deepseek-v4-flash:0731 / deepseek-v4-pro:0813 两个带 tag 快照名)。 +// +// 前端同源静态表:frontend/src/composables/useModelWhitelist.ts 的 +// getModelsByPlatform 'ollama_cloud' case 必须与 DefaultOllamaCloudModelIDs() +// 保持同一集合,两侧各有测试钉住(对侧:useModelWhitelist.spec.ts), +// 改动任一侧须同步另一侧。 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +// TestDefaultOllamaCloudModelIDs 钉住目录的形状:数量、去重、无 claude 串入、 +// 带 tag 快照名与各家族代表条目不得缺失。 +func TestDefaultOllamaCloudModelIDs(t *testing.T) { + ids := DefaultOllamaCloudModelIDs() + + require.Len(t, ids, 20) + + seen := make(map[string]struct{}, len(ids)) + for _, id := range ids { + require.NotEmpty(t, id) + require.NotContains(t, seen, id, "duplicate model id %q", id) + seen[id] = struct{}{} + require.NotContains(t, id, "claude", "claude 模型不得串入 ollama_cloud 目录: %q", id) + } + + for _, key := range []string{ + "deepseek-v4-flash:0731", + "deepseek-v4-pro:0813", + "gpt-oss:120b", + "gpt-oss:20b", + "glm-5.3", + "kimi-k3", + "minimax-m3", + "qwen3.5:397b", + } { + require.Contains(t, ids, key) + } +} + +// C15 验收:defaultModelsListCandidateIDs 的 ollama case 返回目录本身; +// C14/C22 验收:composite 候选包含全部 ollama 条目。 +func TestDefaultModelsListCandidateIDs_OllamaCloud(t *testing.T) { + ollama := defaultModelsListCandidateIDs(PlatformOllamaCloud) + require.Equal(t, DefaultOllamaCloudModelIDs(), ollama) + + composite := defaultModelsListCandidateIDs(PlatformComposite) + for _, id := range DefaultOllamaCloudModelIDs() { + require.Contains(t, composite, id) + } +} + +// B2-② 联动断言:目录内每个模型都必须可计费(两级查价非 nil 且单价为正), +// 防止清单与价目表漂移后出现「默认列表 ∩ 定价表 = 空」与保存期门禁打架。 +func TestDefaultOllamaCloudModelIDsAllPriceable(t *testing.T) { + bs := newTestBillingService() + for _, id := range DefaultOllamaCloudModelIDs() { + pricing, err := bs.getModelPricingAt(id, ollamaPricingAt) + require.NoError(t, err, "model %s", id) + require.NotNil(t, pricing, "model %s must be priceable", id) + require.Positive(t, pricing.InputPricePerToken, "model %s", id) + require.Positive(t, pricing.OutputPricePerToken, "model %s", id) + } +} diff --git a/frontend/src/components/account/ModelWhitelistSelector.vue b/frontend/src/components/account/ModelWhitelistSelector.vue index d1e7e557cb..10b372faad 100644 --- a/frontend/src/components/account/ModelWhitelistSelector.vue +++ b/frontend/src/components/account/ModelWhitelistSelector.vue @@ -211,7 +211,10 @@ const upstreamSyncPlatforms = new Set([ 'zhipu', 'deepseek', 'minimax', - 'opencode_go' + 'opencode_go', + // ollama_cloud:后端探测路径已就绪(upstream_models.go 的 IsOllamaCloud 谓词), + // 打开真实 /v1/models 上游同步。 + 'ollama_cloud' ]) const canSyncUpstream = computed(() => { if (props.accountId) { diff --git a/frontend/src/composables/__tests__/useModelWhitelist.spec.ts b/frontend/src/composables/__tests__/useModelWhitelist.spec.ts index 9cd3ec3648..99a2a8ab13 100644 --- a/frontend/src/composables/__tests__/useModelWhitelist.spec.ts +++ b/frontend/src/composables/__tests__/useModelWhitelist.spec.ts @@ -4,9 +4,51 @@ vi.mock('@/api/admin/accounts', () => ({ getAntigravityDefaultModelMapping: vi.fn() })) -import { buildModelMappingObject, getModelsByPlatform, getPresetMappingsByPlatform, splitModelMappingObject } from '../useModelWhitelist' +import { buildModelMappingObject, claudeModels, getModelsByPlatform, getPresetMappingsByPlatform, splitModelMappingObject } from '../useModelWhitelist' describe('useModelWhitelist', () => { + // ollama_cloud 与后端同源静态表(backend/internal/service/ollama_cloud_models.go + // DefaultOllamaCloudModelIDs(),/v1/models 实测目录)——集合必须与后端一致, + // 两侧各有测试钉住(对侧:ollama_cloud_models_test.go),改任一侧须同步另一侧。 + const ollamaCloudBackendCatalog = [ + 'nemotron-3-super', + 'glm-5.3', + 'gpt-oss:120b', + 'glm-5.3-flash', + 'kimi-k2.6', + 'kimi-k3', + 'deepseek-v4.1-flash', + 'minimax-m2.7', + 'mistral-large-3:675b', + 'glm-5.1', + 'glm-5.2', + 'gpt-oss:20b', + 'qwen3.5:397b', + 'kimi-k2.7-code', + 'nemotron-3-nano:30b', + 'minimax-m3', + 'gemma4:31b', + 'nemotron-3-ultra', + 'deepseek-v4-flash:0731', + 'deepseek-v4-pro:0813' + ] + + it('ollama_cloud 模型列表与后端 DefaultOllamaCloudModelIDs 同一集合', () => { + const models = getModelsByPlatform('ollama_cloud') + + expect(models).toEqual(ollamaCloudBackendCatalog) + }) + + it('ollama_cloud 模型列表不是 Claude 列表且不含 claude 模型', () => { + const models = getModelsByPlatform('ollama_cloud') + + expect(models).not.toBe(claudeModels) + expect(models).not.toEqual(claudeModels) + for (const model of models) { + expect(model).not.toContain('claude') + } + }) + it('openai 模型列表包含 GPT-5.4 官方快照', () => { const models = getModelsByPlatform('openai') diff --git a/frontend/src/composables/useModelWhitelist.ts b/frontend/src/composables/useModelWhitelist.ts index 765ef379c4..d3c9241ab8 100644 --- a/frontend/src/composables/useModelWhitelist.ts +++ b/frontend/src/composables/useModelWhitelist.ts @@ -476,6 +476,31 @@ export function getModelsByPlatform(platform: string): string[] { 'hy4-preview', 'hy3', 'omen-alpha' ] case 'typesafe': return ['jev-latest'] + // 与后端同源静态表:backend/internal/service/ollama_cloud_models.go 的 + // DefaultOllamaCloudModelIDs()(/v1/models 实测目录)——两侧集合必须一致, + // 各有测试钉住(对侧:ollama_cloud_models_test.go),改任一侧须同步另一侧。 + case 'ollama_cloud': return [ + 'nemotron-3-super', + 'glm-5.3', + 'gpt-oss:120b', + 'glm-5.3-flash', + 'kimi-k2.6', + 'kimi-k3', + 'deepseek-v4.1-flash', + 'minimax-m2.7', + 'mistral-large-3:675b', + 'glm-5.1', + 'glm-5.2', + 'gpt-oss:20b', + 'qwen3.5:397b', + 'kimi-k2.7-code', + 'nemotron-3-nano:30b', + 'minimax-m3', + 'gemma4:31b', + 'nemotron-3-ultra', + 'deepseek-v4-flash:0731', + 'deepseek-v4-pro:0813' + ] case 'doubao': return doubaoModels case 'minimax': return minimaxModels case 'baidu': return baiduModels