Skip to content

Commit 21b1676

Browse files
committed
refactor(shared): resolve profile model IDs through provider definitions
ProfileValidator kept a private switch that duplicated the provider model-ID mapping already owned by the provider definitions. Replace it with getModelId() so the allow-list check has a single source of truth. This also closes gaps in the old switch: - Providers it never handled (poe, moonshot, kimiCode, minimax, mimo, openaiCodex, qwenCode, zai, baseten, geminiCli, vercelAiGateway, zooGateway, nanogpt, kenari, opencodeGo) resolved no model and could only pass via a provider-level allowAll. They are now subject to the model allow-list. - fakeAi returned undefined; it now resolves apiModelId. - Retired providers returned undefined; they now resolve through the legacy modelIdKeys precedence. - getModelId() validates the profile against the provider schema, so an invalid profile resolves no model and fails closed unless the provider is allowAll. Extend the tests to cover the newly handled providers and each of these behavioral deltas.
1 parent 3f51e9d commit 21b1676

2 files changed

Lines changed: 95 additions & 43 deletions

File tree

‎src/shared/ProfileValidator.ts‎

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { providerIdentifiers, type ProviderSettings, type OrganizationAllowList } from "@roo-code/types"
1+
import { getModelId, type ProviderSettings, type OrganizationAllowList } from "@roo-code/types"
22

33
export class ProfileValidator {
44
public static isProfileAllowed(profile: ProviderSettings, allowList: OrganizationAllowList): boolean {
@@ -14,7 +14,7 @@ export class ProfileValidator {
1414
return false
1515
}
1616

17-
const modelId = this.getModelIdFromProfile(profile)
17+
const modelId = getModelId(profile)
1818

1919
if (!modelId) {
2020
return allowList.providers[profile.apiProvider]?.allowAll === true
@@ -48,44 +48,4 @@ export class ProfileValidator {
4848

4949
return providerAllowList.models?.includes(modelId) ?? false
5050
}
51-
52-
private static getModelIdFromProfile(profile: ProviderSettings): string | undefined {
53-
switch (profile.apiProvider) {
54-
case providerIdentifiers.openai:
55-
return profile.openAiModelId
56-
case providerIdentifiers.anthropic:
57-
case providerIdentifiers.openaiNative:
58-
case providerIdentifiers.bedrock:
59-
case providerIdentifiers.vertex:
60-
case providerIdentifiers.gemini:
61-
case providerIdentifiers.mistral:
62-
case providerIdentifiers.deepseek:
63-
case providerIdentifiers.xai:
64-
case providerIdentifiers.sambanova:
65-
case providerIdentifiers.fireworks:
66-
case providerIdentifiers.friendli:
67-
return profile.apiModelId
68-
case providerIdentifiers.litellm:
69-
return profile.litellmModelId
70-
case providerIdentifiers.lmstudio:
71-
return profile.lmStudioModelId
72-
case providerIdentifiers.vscodeLm:
73-
case providerIdentifiers.githubCopilot:
74-
// We probably need something more flexible for this one, if we need to really support it here.
75-
return profile.vsCodeLmModelSelector?.id
76-
case providerIdentifiers.openrouter:
77-
return profile.openRouterModelId
78-
case providerIdentifiers.ollama:
79-
return profile.ollamaModelId
80-
case providerIdentifiers.requesty:
81-
return profile.requestyModelId
82-
case providerIdentifiers.ioIntelligence:
83-
return profile.ioIntelligenceModelId
84-
case providerIdentifiers.unbound:
85-
return profile.unboundModelId
86-
case providerIdentifiers.fakeAi:
87-
default:
88-
return undefined
89-
}
90-
}
9151
}

‎src/shared/__tests__/ProfileValidator.spec.ts‎

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// npx vitest run src/shared/__tests__/ProfileValidator.spec.ts
22

3-
import { providerIdentifiers, type ProviderSettings, type OrganizationAllowList } from "@roo-code/types"
3+
import {
4+
providerIdentifiers,
5+
retiredProviderIdentifiers,
6+
type ProviderSettings,
7+
type OrganizationAllowList,
8+
} from "@roo-code/types"
49

510
import { ProfileValidator } from "../ProfileValidator"
611

@@ -27,6 +32,21 @@ describe("ProfileValidator", () => {
2732
["requesty", { requestyModelId: "model" }],
2833
["unbound", { unboundModelId: "model" }],
2934
["ioIntelligence", { ioIntelligenceModelId: "model" }],
35+
["poe", { apiModelId: "model" }],
36+
["moonshot", { apiModelId: "model" }],
37+
["kimiCode", { apiModelId: "model" }],
38+
["minimax", { apiModelId: "model" }],
39+
["mimo", { apiModelId: "model" }],
40+
["openaiCodex", { apiModelId: "model" }],
41+
["qwenCode", { apiModelId: "model" }],
42+
["zai", { apiModelId: "model" }],
43+
["baseten", { apiModelId: "model" }],
44+
["geminiCli", { apiModelId: "model" }],
45+
["vercelAiGateway", { vercelAiGatewayModelId: "model" }],
46+
["zooGateway", { zooGatewayModelId: "model" }],
47+
["nanogpt", { nanoGptModelId: "model" }],
48+
["kenari", { kenariModelId: "model" }],
49+
["opencodeGo", { opencodeGoModelId: "model" }],
3050
])("resolves %s model fields through canonical identifiers", (identifierKey, profileSettings) => {
3151
const canonicalIdentifier = providerIdentifiers[identifierKey as keyof typeof providerIdentifiers]
3252
const modelId = "model"
@@ -376,6 +396,78 @@ describe("ProfileValidator", () => {
376396
expect(ProfileValidator.isProfileAllowed(profile, allowList)).toBe(true)
377397
})
378398

399+
it("enforces the model allow-list for the fake-ai provider", () => {
400+
const allowList: OrganizationAllowList = {
401+
allowAll: false,
402+
providers: { [providerIdentifiers.fakeAi]: { allowAll: false, models: ["allowed-model"] } },
403+
}
404+
405+
expect(
406+
ProfileValidator.isProfileAllowed(
407+
{ apiProvider: providerIdentifiers.fakeAi, apiModelId: "allowed-model" },
408+
allowList,
409+
),
410+
).toBe(true)
411+
expect(
412+
ProfileValidator.isProfileAllowed(
413+
{ apiProvider: providerIdentifiers.fakeAi, apiModelId: "other-model" },
414+
allowList,
415+
),
416+
).toBe(false)
417+
})
418+
419+
it("enforces the model allow-list for retired providers using legacy model ID precedence", () => {
420+
// The removed local switch returned `undefined` for retired providers, so they
421+
// could only pass via a provider-level `allowAll`. `getModelId()` resolves them
422+
// through the legacy `modelIdKeys` precedence, so the model list now applies.
423+
const allowList: OrganizationAllowList = {
424+
allowAll: false,
425+
providers: {
426+
[retiredProviderIdentifiers.groq]: { allowAll: false, models: ["allowed-model"] },
427+
},
428+
}
429+
430+
expect(
431+
ProfileValidator.isProfileAllowed(
432+
{ apiProvider: retiredProviderIdentifiers.groq, requestyModelId: "allowed-model" },
433+
allowList,
434+
),
435+
).toBe(true)
436+
expect(
437+
ProfileValidator.isProfileAllowed(
438+
{ apiProvider: retiredProviderIdentifiers.groq, requestyModelId: "other-model" },
439+
allowList,
440+
),
441+
).toBe(false)
442+
})
443+
444+
it("fails closed when the profile does not satisfy the provider schema", () => {
445+
// `getModelId()` validates the profile before reading the model field, so an
446+
// invalid profile resolves no model. That must deny rather than bypass the
447+
// model allow-list.
448+
const profile = {
449+
apiProvider: providerIdentifiers.openai,
450+
openAiModelId: "gpt-4",
451+
openAiExtraBody: "{not-json",
452+
} as ProviderSettings
453+
454+
expect(
455+
ProfileValidator.isProfileAllowed(profile, {
456+
allowAll: false,
457+
providers: { [providerIdentifiers.openai]: { allowAll: false, models: ["gpt-4"] } },
458+
}),
459+
).toBe(false)
460+
461+
// A provider-level `allowAll` still permits the profile, matching the
462+
// missing-model fallback.
463+
expect(
464+
ProfileValidator.isProfileAllowed(profile, {
465+
allowAll: false,
466+
providers: { [providerIdentifiers.openai]: { allowAll: true } },
467+
}),
468+
).toBe(true)
469+
})
470+
379471
it("should handle providers with undefined models list gracefully", () => {
380472
const allowList: OrganizationAllowList = {
381473
allowAll: false,

0 commit comments

Comments
 (0)