Skip to content

Commit cab2d53

Browse files
committed
refactor(shared): use canonical profile provider identifiers
1 parent d5a8c4a commit cab2d53

2 files changed

Lines changed: 108 additions & 22 deletions

File tree

src/shared/ProfileValidator.ts

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

33
export class ProfileValidator {
44
public static isProfileAllowed(profile: ProviderSettings, allowList: OrganizationAllowList): boolean {
@@ -51,36 +51,36 @@ export class ProfileValidator {
5151

5252
private static getModelIdFromProfile(profile: ProviderSettings): string | undefined {
5353
switch (profile.apiProvider) {
54-
case "openai":
54+
case providerIdentifiers.openai:
5555
return profile.openAiModelId
56-
case "anthropic":
57-
case "openai-native":
58-
case "bedrock":
59-
case "vertex":
60-
case "gemini":
61-
case "mistral":
62-
case "deepseek":
63-
case "xai":
64-
case "sambanova":
65-
case "fireworks":
66-
case "friendli":
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:
6767
return profile.apiModelId
68-
case "litellm":
68+
case providerIdentifiers.litellm:
6969
return profile.litellmModelId
70-
case "lmstudio":
70+
case providerIdentifiers.lmstudio:
7171
return profile.lmStudioModelId
72-
case "vscode-lm":
72+
case providerIdentifiers.vscodeLm:
7373
// We probably need something more flexible for this one, if we need to really support it here.
7474
return profile.vsCodeLmModelSelector?.id
75-
case "openrouter":
75+
case providerIdentifiers.openrouter:
7676
return profile.openRouterModelId
77-
case "ollama":
77+
case providerIdentifiers.ollama:
7878
return profile.ollamaModelId
79-
case "requesty":
79+
case providerIdentifiers.requesty:
8080
return profile.requestyModelId
81-
case "unbound":
81+
case providerIdentifiers.unbound:
8282
return profile.unboundModelId
83-
case "fake-ai":
83+
case providerIdentifiers.fakeAi:
8484
default:
8585
return undefined
8686
}

src/shared/__tests__/ProfileValidator.spec.ts

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

3-
import type { ProviderSettings, OrganizationAllowList } from "@roo-code/types"
3+
import { providerIdentifiers, type ProviderSettings, type OrganizationAllowList } from "@roo-code/types"
44

55
import { ProfileValidator } from "../ProfileValidator"
66

77
describe("ProfileValidator", () => {
88
describe("isProfileAllowed", () => {
9+
it.each([
10+
["openai", { openAiModelId: "model" }],
11+
["anthropic", { apiModelId: "model" }],
12+
["openaiNative", { apiModelId: "model" }],
13+
["bedrock", { apiModelId: "model" }],
14+
["vertex", { apiModelId: "model" }],
15+
["gemini", { apiModelId: "model" }],
16+
["mistral", { apiModelId: "model" }],
17+
["deepseek", { apiModelId: "model" }],
18+
["xai", { apiModelId: "model" }],
19+
["sambanova", { apiModelId: "model" }],
20+
["fireworks", { apiModelId: "model" }],
21+
["friendli", { apiModelId: "model" }],
22+
["litellm", { litellmModelId: "model" }],
23+
["lmstudio", { lmStudioModelId: "model" }],
24+
["vscodeLm", { vsCodeLmModelSelector: { id: "model" } }],
25+
["openrouter", { openRouterModelId: "model" }],
26+
["ollama", { ollamaModelId: "model" }],
27+
["requesty", { requestyModelId: "model" }],
28+
["unbound", { unboundModelId: "model" }],
29+
])("resolves %s model fields through canonical identifiers", (identifierKey, profileSettings) => {
30+
const identifiers = providerIdentifiers as Record<string, string>
31+
const originalIdentifier = identifiers[identifierKey]
32+
const canonicalIdentifier = `canonical-${identifierKey}`
33+
const modelId = "model"
34+
35+
try {
36+
identifiers[identifierKey] = canonicalIdentifier
37+
38+
const profile = {
39+
apiProvider: canonicalIdentifier,
40+
...profileSettings,
41+
} as ProviderSettings
42+
const allowList: OrganizationAllowList = {
43+
allowAll: false,
44+
providers: {
45+
[canonicalIdentifier]: { allowAll: false, models: [modelId] },
46+
},
47+
}
48+
49+
expect(ProfileValidator.isProfileAllowed(profile, allowList)).toBe(true)
50+
} finally {
51+
identifiers[identifierKey] = originalIdentifier
52+
}
53+
})
54+
55+
it.each([
56+
{ identifierKey: "fakeAi", profileSettings: {}, modelId: undefined, expected: false },
57+
{
58+
identifierKey: "fakeAi",
59+
profileSettings: {},
60+
modelId: undefined,
61+
expected: true,
62+
providerAllowAll: true,
63+
},
64+
])(
65+
"preserves canonical fallback behavior when provider allowAll is $providerAllowAll",
66+
({ identifierKey, profileSettings, modelId, expected, providerAllowAll = false }) => {
67+
const identifiers = providerIdentifiers as Record<string, string>
68+
const originalIdentifier = identifiers[identifierKey]
69+
const canonicalIdentifier = `canonical-${identifierKey}`
70+
71+
try {
72+
identifiers[identifierKey] = canonicalIdentifier
73+
74+
const profile = {
75+
apiProvider: canonicalIdentifier,
76+
...profileSettings,
77+
} as unknown as ProviderSettings
78+
const allowList: OrganizationAllowList = {
79+
allowAll: false,
80+
providers: {
81+
[canonicalIdentifier]: {
82+
allowAll: providerAllowAll,
83+
models: modelId ? [modelId] : undefined,
84+
},
85+
},
86+
}
87+
88+
expect(ProfileValidator.isProfileAllowed(profile, allowList)).toBe(expected)
89+
} finally {
90+
identifiers[identifierKey] = originalIdentifier
91+
}
92+
},
93+
)
94+
995
it("should allow any profile when allowAll is true", () => {
1096
const allowList: OrganizationAllowList = {
1197
allowAll: true,

0 commit comments

Comments
 (0)