Skip to content

Commit aabcfad

Browse files
committed
refactor: Fetch model lists via ApiHandler interface
1 parent 9af61f8 commit aabcfad

40 files changed

Lines changed: 1262 additions & 717 deletions

‎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 inherit the BaseProvider default that 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
})

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ describe("NanoGptHandler", () => {
669669
await collectStream(new NanoGptHandler({ nanoGptModelId: "model:thinking" }).createMessage("sys", messages))
670670
expect(getModels).toHaveBeenLastCalledWith(
671671
expect.objectContaining({ provider: providerIdentifiers.nanogpt, apiKey: undefined }),
672+
expect.anything(),
672673
)
673674
})
674675

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ vitest.mock("../fetchers/modelCache", () => ({
103103
},
104104
})
105105
}),
106-
refreshModels: vitest.fn(async (options) => {
106+
refreshModels: vitest.fn(async (options, handler) => {
107107
const { getModels } = await import("../fetchers/modelCache")
108-
return getModels(options)
108+
return getModels(options, handler)
109109
}),
110110
}))
111111

@@ -853,4 +853,11 @@ describe("OpenRouterHandler", () => {
853853
)
854854
})
855855
})
856+
857+
describe("model catalog", () => {
858+
it("reports its cache scope", () => {
859+
const handler = new OpenRouterHandler(mockOptions)
860+
expect(handler.getModelCacheScope()).toEqual({ urlScoped: false, keyScoped: false, authScoped: false })
861+
})
862+
})
856863
})

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,14 @@ describe("PoeHandler", () => {
142142
const handler = new PoeHandler(options)
143143
const result = handler.getModel()
144144

145-
expect(getModelsFromCache).toHaveBeenCalledWith({
146-
provider: providerIdentifiers.poe,
147-
apiKey: options.poeApiKey,
148-
baseUrl: options.poeBaseUrl,
149-
})
145+
expect(getModelsFromCache).toHaveBeenCalledWith(
146+
{
147+
provider: providerIdentifiers.poe,
148+
apiKey: options.poeApiKey,
149+
baseUrl: options.poeBaseUrl,
150+
},
151+
expect.anything(),
152+
)
150153
expect(result.id).toBe("anthropic/claude-sonnet-4")
151154
expect(result.info.contextWindow).toBe(200_000)
152155
expect(result.info.maxTokens).toBe(10_000)

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ vitest.mock("../fetchers/modelCache", () => ({
110110
},
111111
})
112112
}),
113-
refreshModels: vitest.fn(async (options) => {
113+
refreshModels: vitest.fn(async (options, handler) => {
114114
const { getModels } = await import("../fetchers/modelCache")
115-
return getModels(options)
115+
return getModels(options, handler)
116116
}),
117117
}))
118118

@@ -703,4 +703,11 @@ describe("RequestyHandler", () => {
703703
await expect(handler.completePrompt("test prompt")).rejects.toThrow("Unexpected error")
704704
})
705705
})
706+
707+
describe("model catalog", () => {
708+
it("reports its cache scope", () => {
709+
const handler = new RequestyHandler(mockOptions)
710+
expect(handler.getModelCacheScope()).toEqual({ urlScoped: true, keyScoped: true, authScoped: false })
711+
})
712+
})
706713
})

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ vi.mock("../fetchers/modelCache", () => ({
3232
description: "GPT-4o",
3333
},
3434
}),
35-
refreshModels: vi.fn(async (options) => {
35+
refreshModels: vi.fn(async (options, handler) => {
3636
const { getModels } = await import("../fetchers/modelCache")
37-
return getModels(options)
37+
return getModels(options, handler)
3838
}),
3939
}))
4040

@@ -201,4 +201,11 @@ describe("UnboundHandler", () => {
201201
}),
202202
)
203203
})
204+
205+
describe("model catalog", () => {
206+
it("reports its cache scope", () => {
207+
const handler = new UnboundHandler({ unboundApiKey: "test-key", unboundModelId: "openai/gpt-4o" })
208+
expect(handler.getModelCacheScope()).toEqual({ urlScoped: false, keyScoped: false, authScoped: false })
209+
})
210+
})
204211
})

‎src/api/providers/__tests__/vercel-ai-gateway.spec.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ vitest.mock("../fetchers/modelCache", () => ({
121121
},
122122
})
123123
}),
124-
refreshModels: vitest.fn(async (options) => {
124+
refreshModels: vitest.fn(async (options, handler) => {
125125
const { getModels } = await import("../fetchers/modelCache")
126-
return getModels(options)
126+
return getModels(options, handler)
127127
}),
128128
getModelsFromCache: vitest.fn().mockReturnValue(undefined),
129129
}))
@@ -792,4 +792,11 @@ describe("VercelAiGatewayHandler", () => {
792792
)
793793
})
794794
})
795+
796+
describe("model catalog", () => {
797+
it("reports its cache scope", () => {
798+
const handler = new VercelAiGatewayHandler(mockOptions)
799+
expect(handler.getModelCacheScope()).toEqual({ urlScoped: false, keyScoped: false, authScoped: false })
800+
})
801+
})
795802
})

‎src/api/providers/base-provider.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
22

3-
import type { ModelInfo } from "@roo-code/types"
3+
import type { ModelInfo, ModelRecord } from "@roo-code/types"
44

5-
import type { ApiHandler, ApiHandlerCreateMessageMetadata } from "../index"
5+
import type { ApiHandler, ApiHandlerCreateMessageMetadata, ModelCacheScope } from "../index"
6+
import type { GetModelsOptions } from "../../shared/api"
67
import { ApiStream } from "../transform/stream"
78
import { sanitizeSurrogates, sanitizeSurrogatesDeep } from "../transform/sanitize-surrogates"
89
import { countTokens } from "../../utils/countTokens"
@@ -127,4 +128,18 @@ export abstract class BaseProvider implements ApiHandler {
127128

128129
return countTokens(content, { useWorker: true })
129130
}
131+
132+
/**
133+
* Default: providers without a dynamic catalog reject. Catalog providers override this.
134+
*/
135+
fetchModels(_options: GetModelsOptions, _signal?: AbortSignal): Promise<ModelRecord> {
136+
return Promise.reject(new Error("This provider does not expose a dynamic model catalog"))
137+
}
138+
139+
/**
140+
* Default: no special cache scoping. Catalog providers override this.
141+
*/
142+
getModelCacheScope(): ModelCacheScope {
143+
return { urlScoped: false, keyScoped: false, authScoped: false }
144+
}
130145
}

0 commit comments

Comments
 (0)