Skip to content

Commit 2db2af0

Browse files
WebMadedelauna
andauthored
refactor(api): use canonical model cache provider identifiers (#1020)
* refactor(api): use canonical model cache provider identifiers * test(api): address model cache review feedback * refactor(api): retain auth-scoped provider set --------- Co-authored-by: edelauna <[email protected]>
1 parent c52f118 commit 2db2af0

3 files changed

Lines changed: 156 additions & 80 deletions

File tree

‎src/api/providers/fetchers/__tests__/modelCache.spec.ts‎

Lines changed: 113 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ vi.mock("../../../core/config/ContextProxy", () => ({
5858
}))
5959

6060
// Then imports
61-
import type { Mock } from "vitest"
61+
import type { Mock, Mocked } from "vitest"
62+
import { providerIdentifiers } from "@roo-code/types"
6263
import * as fsSync from "fs"
6364
import NodeCache from "node-cache"
6465
import { getModels, getModelsFromCache } from "../modelCache"
@@ -93,7 +94,7 @@ describe("getModels with new GetModelsOptions", () => {
9394
mockGetLiteLLMModels.mockResolvedValue(mockModels)
9495

9596
const result = await getModels({
96-
provider: "litellm",
97+
provider: providerIdentifiers.litellm,
9798
apiKey: "test-api-key",
9899
baseUrl: "http://localhost:4000",
99100
})
@@ -113,7 +114,24 @@ describe("getModels with new GetModelsOptions", () => {
113114
}
114115
mockGetOpenRouterModels.mockResolvedValue(mockModels)
115116

116-
const result = await getModels({ provider: "openrouter" })
117+
const result = await getModels({ provider: providerIdentifiers.openrouter })
118+
119+
expect(mockGetOpenRouterModels).toHaveBeenCalled()
120+
expect(result).toEqual(mockModels)
121+
})
122+
123+
it("dispatches OpenRouter through its canonical provider identifier", async () => {
124+
const mockModels = {
125+
"openrouter/canonical-model": {
126+
maxTokens: 8192,
127+
contextWindow: 128000,
128+
supportsPromptCache: false,
129+
},
130+
}
131+
132+
mockGetOpenRouterModels.mockResolvedValue(mockModels)
133+
134+
const result = await getModels({ provider: providerIdentifiers.openrouter })
117135

118136
expect(mockGetOpenRouterModels).toHaveBeenCalled()
119137
expect(result).toEqual(mockModels)
@@ -130,12 +148,33 @@ describe("getModels with new GetModelsOptions", () => {
130148
}
131149
mockGetRequestyModels.mockResolvedValue(mockModels)
132150

133-
const result = await getModels({ provider: "requesty", apiKey: DUMMY_REQUESTY_KEY })
151+
const result = await getModels({ provider: providerIdentifiers.requesty, apiKey: DUMMY_REQUESTY_KEY })
134152

135153
expect(mockGetRequestyModels).toHaveBeenCalledWith(undefined, DUMMY_REQUESTY_KEY)
136154
expect(result).toEqual(mockModels)
137155
})
138156

157+
it("dispatches credentialed fetchers through canonical provider identifiers", async () => {
158+
const mockModels = {
159+
"requesty/canonical-model": {
160+
maxTokens: 4096,
161+
contextWindow: 8192,
162+
supportsPromptCache: false,
163+
},
164+
}
165+
166+
mockGetRequestyModels.mockResolvedValue(mockModels)
167+
168+
const result = await getModels({
169+
provider: providerIdentifiers.requesty,
170+
apiKey: DUMMY_REQUESTY_KEY,
171+
baseUrl: "https://router.requesty.ai/v1",
172+
})
173+
174+
expect(mockGetRequestyModels).toHaveBeenCalledWith("https://router.requesty.ai/v1", DUMMY_REQUESTY_KEY)
175+
expect(result).toEqual(mockModels)
176+
})
177+
139178
it("calls getKenariModels with optional API key", async () => {
140179
const mockModels = {
141180
"glm-5-2": {
@@ -147,7 +186,7 @@ describe("getModels with new GetModelsOptions", () => {
147186
}
148187
mockGetKenariModels.mockResolvedValue(mockModels)
149188

150-
const result = await getModels({ provider: "kenari", apiKey: "kenari-key-for-testing" })
189+
const result = await getModels({ provider: providerIdentifiers.kenari, apiKey: "kenari-key-for-testing" })
151190

152191
expect(mockGetKenariModels).toHaveBeenCalledWith("kenari-key-for-testing")
153192
expect(result).toEqual(mockModels)
@@ -159,7 +198,7 @@ describe("getModels with new GetModelsOptions", () => {
159198

160199
await expect(
161200
getModels({
162-
provider: "litellm",
201+
provider: providerIdentifiers.litellm,
163202
apiKey: "test-api-key",
164203
baseUrl: "http://localhost:4000",
165204
}),
@@ -178,7 +217,7 @@ describe("getModels with new GetModelsOptions", () => {
178217
mockGetMoonshotModels.mockResolvedValue(mockModels)
179218

180219
const result = await getModels({
181-
provider: "moonshot",
220+
provider: providerIdentifiers.moonshot,
182221
apiKey: "test-key",
183222
baseUrl: "https://api.moonshot.ai/v1",
184223
})
@@ -190,7 +229,7 @@ describe("getModels with new GetModelsOptions", () => {
190229
it("validates exhaustive provider checking with unknown provider", async () => {
191230
// This test ensures TypeScript catches unknown providers at compile time
192231
// In practice, the discriminated union should prevent this at compile time
193-
const unknownProvider = "unknown" as any
232+
const unknownProvider = "unknown" as typeof providerIdentifiers.openrouter
194233

195234
await expect(
196235
getModels({
@@ -201,13 +240,13 @@ describe("getModels with new GetModelsOptions", () => {
201240
})
202241

203242
describe("getModelsFromCache disk fallback", () => {
204-
let mockCache: any
243+
let mockCache: Mocked<NodeCache>
205244

206245
beforeEach(() => {
207246
vi.clearAllMocks()
208247
// Get the mock cache instance
209248
const MockedNodeCache = vi.mocked(NodeCache)
210-
mockCache = new MockedNodeCache()
249+
mockCache = vi.mocked(new MockedNodeCache())
211250
// Reset memory cache to always miss
212251
mockCache.get.mockReturnValue(undefined)
213252
// Reset fs mocks
@@ -218,7 +257,7 @@ describe("getModelsFromCache disk fallback", () => {
218257
it("returns undefined when both memory and disk cache miss", () => {
219258
vi.mocked(fsSync.existsSync).mockReturnValue(false)
220259

221-
const result = getModelsFromCache("openrouter")
260+
const result = getModelsFromCache(providerIdentifiers.openrouter)
222261

223262
expect(result).toBeUndefined()
224263
})
@@ -234,13 +273,30 @@ describe("getModelsFromCache disk fallback", () => {
234273

235274
mockCache.get.mockReturnValue(memoryModels)
236275

237-
const result = getModelsFromCache("openrouter")
276+
const result = getModelsFromCache(providerIdentifiers.openrouter)
238277

239278
expect(result).toEqual(memoryModels)
240279
// Disk should not be checked when memory cache hits
241280
expect(fsSync.existsSync).not.toHaveBeenCalled()
242281
})
243282

283+
it("isolates authenticated users through the canonical Zoo Gateway identifier", () => {
284+
const previousUserModels = {
285+
"previous-user/model": {
286+
maxTokens: 4096,
287+
contextWindow: 128000,
288+
supportsPromptCache: false,
289+
},
290+
}
291+
292+
mockCache.get.mockReturnValue(previousUserModels)
293+
294+
const result = getModelsFromCache(providerIdentifiers.zooGateway)
295+
296+
expect(result).toBeUndefined()
297+
expect(mockCache.get).not.toHaveBeenCalled()
298+
})
299+
244300
it("returns disk cache data when memory cache misses and context is available", () => {
245301
// Note: This test validates the logic but the ContextProxy mock in test environment
246302
// returns undefined for getCacheDirectoryPathSync, which is expected behavior
@@ -257,7 +313,7 @@ describe("getModelsFromCache disk fallback", () => {
257313
vi.mocked(fsSync.existsSync).mockReturnValue(true)
258314
vi.mocked(fsSync.readFileSync).mockReturnValue(JSON.stringify(diskModels))
259315

260-
const result = getModelsFromCache("openrouter")
316+
const result = getModelsFromCache(providerIdentifiers.openrouter)
261317

262318
// In the test environment, ContextProxy.instance may not be fully initialized,
263319
// so getCacheDirectoryPathSync returns undefined and disk cache is not attempted
@@ -272,7 +328,7 @@ describe("getModelsFromCache disk fallback", () => {
272328

273329
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(function () {})
274330

275-
const result = getModelsFromCache("openrouter")
331+
const result = getModelsFromCache(providerIdentifiers.openrouter)
276332

277333
expect(result).toBeUndefined()
278334
expect(consoleErrorSpy).toHaveBeenCalled()
@@ -286,7 +342,7 @@ describe("getModelsFromCache disk fallback", () => {
286342

287343
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(function () {})
288344

289-
const result = getModelsFromCache("openrouter")
345+
const result = getModelsFromCache(providerIdentifiers.openrouter)
290346

291347
expect(result).toBeUndefined()
292348
expect(consoleErrorSpy).toHaveBeenCalled()
@@ -296,15 +352,15 @@ describe("getModelsFromCache disk fallback", () => {
296352
})
297353

298354
describe("empty cache protection", () => {
299-
let mockCache: any
300-
let mockGet: Mock
301-
let mockSet: Mock
355+
let mockCache: Mocked<NodeCache>
356+
let mockGet: Mocked<NodeCache>["get"]
357+
let mockSet: Mocked<NodeCache>["set"]
302358

303359
beforeEach(() => {
304360
vi.clearAllMocks()
305361
// Get the mock cache instance
306362
const MockedNodeCache = vi.mocked(NodeCache)
307-
mockCache = new MockedNodeCache()
363+
mockCache = vi.mocked(new MockedNodeCache())
308364
mockGet = mockCache.get
309365
mockSet = mockCache.set
310366
// Reset memory cache to always miss by default
@@ -316,7 +372,7 @@ describe("empty cache protection", () => {
316372
// API returns empty object (simulating failure)
317373
mockGetOpenRouterModels.mockResolvedValue({})
318374

319-
const result = await getModels({ provider: "openrouter" })
375+
const result = await getModels({ provider: providerIdentifiers.openrouter })
320376

321377
// Should return empty but NOT cache it
322378
expect(result).toEqual({})
@@ -334,7 +390,7 @@ describe("empty cache protection", () => {
334390
}
335391
mockGetOpenRouterModels.mockResolvedValue(mockModels)
336392

337-
const result = await getModels({ provider: "openrouter" })
393+
const result = await getModels({ provider: providerIdentifiers.openrouter })
338394

339395
expect(result).toEqual(mockModels)
340396
expect(mockSet).toHaveBeenCalledWith("openrouter", mockModels)
@@ -358,7 +414,7 @@ describe("empty cache protection", () => {
358414
mockGetOpenRouterModels.mockResolvedValue({})
359415

360416
const { refreshModels } = await import("../modelCache")
361-
const result = await refreshModels({ provider: "openrouter" })
417+
const result = await refreshModels({ provider: providerIdentifiers.openrouter })
362418

363419
// Should return existing cache, not empty
364420
expect(result).toEqual(existingModels)
@@ -388,7 +444,7 @@ describe("empty cache protection", () => {
388444
mockGetOpenRouterModels.mockResolvedValue(newModels)
389445

390446
const { refreshModels } = await import("../modelCache")
391-
const result = await refreshModels({ provider: "openrouter" })
447+
const result = await refreshModels({ provider: providerIdentifiers.openrouter })
392448

393449
// Should return new models
394450
expect(result).toEqual(newModels)
@@ -410,7 +466,7 @@ describe("empty cache protection", () => {
410466
mockGetOpenRouterModels.mockRejectedValue(new Error("API error"))
411467

412468
const { refreshModels } = await import("../modelCache")
413-
const result = await refreshModels({ provider: "openrouter" })
469+
const result = await refreshModels({ provider: providerIdentifiers.openrouter })
414470

415471
// Should return existing cache on error
416472
expect(result).toEqual(existingModels)
@@ -421,7 +477,7 @@ describe("empty cache protection", () => {
421477
mockGetOpenRouterModels.mockRejectedValue(new Error("API error"))
422478

423479
const { refreshModels } = await import("../modelCache")
424-
const result = await refreshModels({ provider: "openrouter" })
480+
const result = await refreshModels({ provider: providerIdentifiers.openrouter })
425481

426482
// Should return empty when no cache and API fails
427483
expect(result).toEqual({})
@@ -434,7 +490,7 @@ describe("empty cache protection", () => {
434490
mockGetOpenRouterModels.mockResolvedValue({})
435491

436492
const { refreshModels } = await import("../modelCache")
437-
const result = await refreshModels({ provider: "openrouter" })
493+
const result = await refreshModels({ provider: providerIdentifiers.openrouter })
438494

439495
// Should return empty but NOT cache it
440496
expect(result).toEqual({})
@@ -462,8 +518,8 @@ describe("empty cache protection", () => {
462518
const { refreshModels } = await import("../modelCache")
463519

464520
// Start two concurrent refresh calls
465-
const promise1 = refreshModels({ provider: "openrouter" })
466-
const promise2 = refreshModels({ provider: "openrouter" })
521+
const promise1 = refreshModels({ provider: providerIdentifiers.openrouter })
522+
const promise2 = refreshModels({ provider: providerIdentifiers.openrouter })
467523

468524
// API should only be called once (second call reuses in-flight request)
469525
expect(mockGetOpenRouterModels).toHaveBeenCalledTimes(1)
@@ -495,8 +551,8 @@ describe("empty cache protection", () => {
495551

496552
// Different keys -> separate compound keys -> two distinct fetches.
497553
const [a, b] = await Promise.all([
498-
refreshModels({ provider: "requesty", apiKey: "key-one" }),
499-
refreshModels({ provider: "requesty", apiKey: "key-two" }),
554+
refreshModels({ provider: providerIdentifiers.requesty, apiKey: "key-one" }),
555+
refreshModels({ provider: providerIdentifiers.requesty, apiKey: "key-two" }),
500556
])
501557
expect(mockGetRequestyModels).toHaveBeenCalledTimes(2)
502558
expect(a).toEqual(mockModels)
@@ -512,8 +568,8 @@ describe("empty cache protection", () => {
512568
}),
513569
)
514570

515-
const shared1 = refreshModels({ provider: "requesty", apiKey: "same-key" })
516-
const shared2 = refreshModels({ provider: "requesty", apiKey: "same-key" })
571+
const shared1 = refreshModels({ provider: providerIdentifiers.requesty, apiKey: "same-key" })
572+
const shared2 = refreshModels({ provider: providerIdentifiers.requesty, apiKey: "same-key" })
517573

518574
expect(mockGetRequestyModels).toHaveBeenCalledTimes(1)
519575

@@ -529,10 +585,10 @@ describe("key-scoped cache key derivation", () => {
529585
// Exercises the per-API-key cache discriminator that all KEY_SCOPED_PROVIDERS share.
530586
// Requesty is used only because it is a key-scoped provider with a mocked fetcher; the
531587
// behavior under test is provider-agnostic.
532-
const keyScopedProvider = "requesty" as const
588+
const keyScopedProvider = providerIdentifiers.requesty
533589

534-
let mockCache: any
535-
let mockSet: Mock
590+
let mockCache: Mocked<NodeCache>
591+
let mockSet: Mocked<NodeCache>["set"]
536592

537593
const mockModels = {
538594
"key-scoped/model": {
@@ -546,7 +602,7 @@ describe("key-scoped cache key derivation", () => {
546602
beforeEach(() => {
547603
vi.clearAllMocks()
548604
const MockedNodeCache = vi.mocked(NodeCache)
549-
mockCache = new MockedNodeCache()
605+
mockCache = vi.mocked(new MockedNodeCache())
550606
mockCache.get.mockReturnValue(undefined)
551607
mockSet = mockCache.set
552608
mockGetRequestyModels.mockResolvedValue(mockModels)
@@ -627,34 +683,50 @@ describe("compound cache key derivation across scoping dimensions", () => {
627683
}
628684

629685
it("includes both the server URL and the key discriminator for url+key-scoped providers", async () => {
630-
await getModels({ provider: "litellm", apiKey: "compound-key", baseUrl: "http://host:4000" })
686+
await getModels({
687+
provider: providerIdentifiers.litellm,
688+
apiKey: "compound-key",
689+
baseUrl: "http://host:4000",
690+
})
631691
const cacheKey = writtenCacheKey()
632692

633693
// Expected shape: provider:url:keyDiscriminator
634694
expect(cacheKey).toMatch(/^litellm:http:\/\/host:4000:[0-9a-f]{8}$/)
635695
})
636696

637697
it("normalizes trailing slashes in the server URL so equivalent URLs share a cache key", async () => {
638-
await getModels({ provider: "litellm", apiKey: "compound-key", baseUrl: "http://host:4000/" })
698+
await getModels({
699+
provider: providerIdentifiers.litellm,
700+
apiKey: "compound-key",
701+
baseUrl: "http://host:4000/",
702+
})
639703
const withSlash = writtenCacheKey()
640704

641705
mockSet.mockClear()
642-
await getModels({ provider: "litellm", apiKey: "compound-key", baseUrl: "http://host:4000" })
706+
await getModels({
707+
provider: providerIdentifiers.litellm,
708+
apiKey: "compound-key",
709+
baseUrl: "http://host:4000",
710+
})
643711
const withoutSlash = writtenCacheKey()
644712

645713
expect(withSlash).toEqual(withoutSlash)
646714
})
647715

648716
it("includes only the server URL when a url-scoped provider has no API key", async () => {
649-
await getModels({ provider: "litellm", baseUrl: "http://host:4000" })
717+
await getModels({ provider: providerIdentifiers.litellm, baseUrl: "http://host:4000" })
650718
const cacheKey = writtenCacheKey()
651719

652720
// No trailing key discriminator when apiKey is absent.
653721
expect(cacheKey).toBe("litellm:http://host:4000")
654722
})
655723

656724
it("falls back to the bare provider name for providers that are neither url- nor key-scoped", async () => {
657-
await getModels({ provider: "openrouter", apiKey: "ignored-key", baseUrl: "http://ignored:4000" })
725+
await getModels({
726+
provider: providerIdentifiers.openrouter,
727+
apiKey: "ignored-key",
728+
baseUrl: "http://ignored:4000",
729+
})
658730
const cacheKey = writtenCacheKey()
659731

660732
expect(cacheKey).toBe("openrouter")

0 commit comments

Comments
 (0)