Skip to content

Commit 7098c46

Browse files
committed
refactor: Fetch model lists via ApiHandler interface
1 parent ecab519 commit 7098c46

43 files changed

Lines changed: 1677 additions & 728 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/api/__tests__/index.spec.ts‎

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,42 @@ vitest.mock("vscode", () => ({
99
},
1010
}))
1111

12-
// Handler constructors can require credentials or initialize SDK clients. Replace them
13-
// with inert classes so these tests exercise only the factory's routing behavior.
12+
// Replace handlers with inert classes (no-op constructors) that keep the real prototype
13+
// chain, so tests exercise routing and can still inspect methods such as fetchModels.
14+
const { inertClass } = vitest.hoisted(() => ({
15+
inertClass: (actual: unknown): unknown => {
16+
if (typeof actual !== "function" || !actual.prototype) {
17+
return actual
18+
}
19+
const Inert = class {}
20+
Object.setPrototypeOf(Inert.prototype, actual.prototype)
21+
return Inert
22+
},
23+
}))
24+
1425
vitest.mock("../providers", async () => {
1526
const providers = await vitest.importActual<Record<string, unknown>>("../providers")
1627

17-
return Object.fromEntries(Object.keys(providers).map((name) => [name, class {}]))
28+
return Object.fromEntries(Object.entries(providers).map(([name, actual]) => [name, inertClass(actual)]))
1829
})
1930

20-
vitest.mock("../providers/native-ollama", () => ({
21-
NativeOllamaHandler: class {},
22-
}))
31+
vitest.mock("../providers/native-ollama", async () => {
32+
const actual = await vitest.importActual<typeof import("../providers/native-ollama")>("../providers/native-ollama")
33+
34+
return { NativeOllamaHandler: inertClass(actual.NativeOllamaHandler) }
35+
})
2336

2437
import {
38+
dynamicProviders,
39+
localProviders,
2540
providerIdentifiers,
2641
retiredProviderIdentifiers,
2742
type ProviderName,
2843
type ProviderNameWithRetired,
2944
} from "@roo-code/types"
3045

46+
import type { RouterName } from "../../shared/api"
47+
3148
import { buildApiHandler } from "../index"
3249
import {
3350
AnthropicHandler,
@@ -155,3 +172,29 @@ describe("buildApiHandler", () => {
155172
expect(handler).toBeInstanceOf(AnthropicHandler)
156173
})
157174
})
175+
176+
// fetchModels is optional on ApiHandler, so this replaces the old fetcher switch's
177+
// exhaustiveness check: every RouterName must build a handler that implements it.
178+
describe("buildApiHandler model catalog coverage", () => {
179+
const routerNames: RouterName[] = [...dynamicProviders, ...localProviders]
180+
181+
it("covers at least one router", () => {
182+
expect(routerNames.length).toBeGreaterThan(0)
183+
})
184+
185+
it.each(routerNames)("returns a handler implementing fetchModels for %s", (apiProvider) => {
186+
const handler = buildApiHandler({ apiProvider })
187+
188+
expect(handler).not.toBeInstanceOf(AnthropicHandler)
189+
expect(typeof handler.fetchModels).toBe("function")
190+
expect(typeof handler.getModelCacheScope).toBe("function")
191+
})
192+
193+
it("does not expose fetchModels on the default fallback handler", () => {
194+
const handler = buildApiHandler({
195+
apiProvider: "unsupported-provider" as ProviderNameWithRetired,
196+
})
197+
198+
expect(handler.fetchModels).toBeUndefined()
199+
})
200+
})

‎src/api/index.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import {
77
retiredProviderIdentifiers,
88
type ProviderSettings,
99
type ModelInfo,
10+
type ModelRecord,
1011
} from "@roo-code/types"
1112

1213
import { getRouterRemovalMessage } from "../core/config/routerRemoval"
14+
import type { GetModelsOptions } from "../shared/api"
1315
import { ApiStream } from "./transform/stream"
1416

1517
import {
@@ -117,6 +119,18 @@ export interface ApiHandlerCreateMessageMetadata {
117119
abortSignal?: AbortSignal
118120
}
119121

122+
/**
123+
* Describes how the model cache must key and persist a provider's catalog.
124+
*/
125+
export interface ModelCacheScope {
126+
/** Cache key must include the normalized baseUrl. */
127+
urlScoped: boolean
128+
/** Cache key must include a short, irreversible API-key discriminator. */
129+
keyScoped: boolean
130+
/** Never persist to disk/memory; results are per signed-in user. */
131+
authScoped: boolean
132+
}
133+
120134
export interface ApiHandler {
121135
createMessage(
122136
systemPrompt: string,
@@ -156,6 +170,18 @@ export interface ApiHandler {
156170
* @returns A promise resolving to the token count
157171
*/
158172
countTokens(content: Array<Anthropic.Messages.ContentBlockParam>): Promise<number>
173+
174+
/**
175+
* Fetch this provider's live model catalog. Only dynamic-catalog providers
176+
* implement it; others leave it undefined and the model cache rejects.
177+
*/
178+
fetchModels?(options: GetModelsOptions, signal?: AbortSignal): Promise<ModelRecord>
179+
180+
/**
181+
* How the model cache must key and persist this provider's catalog.
182+
* Defaults to all-false in BaseProvider.
183+
*/
184+
getModelCacheScope(): ModelCacheScope
159185
}
160186

161187
export function buildApiHandler(configuration: ProviderSettings): ApiHandler {

‎src/api/providers/__tests__/kenari.spec.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ vitest.mock("../fetchers/modelCache", () => ({
3434
},
3535
}),
3636
),
37-
refreshModels: vitest.fn(async (options) => {
37+
refreshModels: vitest.fn(async (options, handler) => {
3838
const { getModels } = await import("../fetchers/modelCache")
39-
return getModels(options)
39+
return getModels(options, handler)
4040
}),
4141
getModelsFromCache: vitest.fn().mockReturnValue(undefined),
4242
}))
@@ -371,4 +371,11 @@ describe("KenariHandler", () => {
371371
expect("temperature" in callArgs).toBe(false)
372372
})
373373
})
374+
375+
describe("model catalog", () => {
376+
it("reports its cache scope", () => {
377+
const handler = new KenariHandler(mockOptions)
378+
expect(handler.getModelCacheScope()).toEqual({ urlScoped: false, keyScoped: false, authScoped: false })
379+
})
380+
})
374381
})

‎src/api/providers/__tests__/lite-llm.spec.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ vi.mock("../fetchers/modelCache", () => ({
6868
"vertex_ai/gemini-3-pro": { ...litellmDefaultModelInfo, maxTokens: 8192 },
6969
})
7070
}),
71-
refreshModels: vi.fn(async (options) => {
71+
refreshModels: vi.fn(async (options, handler) => {
7272
const { getModels } = await import("../fetchers/modelCache")
73-
return getModels(options)
73+
return getModels(options, handler)
7474
}),
7575
getModelsFromCache: vi.fn().mockReturnValue(undefined),
7676
}))
@@ -1380,4 +1380,11 @@ describe("LiteLLMHandler", () => {
13801380
expect(requestHeaders).not.toHaveProperty("X-Zoo-Session-ID")
13811381
})
13821382
})
1383+
1384+
describe("model catalog", () => {
1385+
it("reports its cache scope", () => {
1386+
const handler = new LiteLLMHandler(mockOptions)
1387+
expect(handler.getModelCacheScope()).toEqual({ urlScoped: true, keyScoped: true, authScoped: false })
1388+
})
1389+
})
13831390
})

0 commit comments

Comments
 (0)