Skip to content

Commit 3d4aed8

Browse files
author
Zoo (VP)
committed
feat(api): dynamically fetch Gemini and Vertex model catalogs
Add gemini and vertex to the router-models pipeline so newly released Gemini models appear in the picker without an extension update. Both fetchers list models via the already-installed @google/genai SDK (models.list): ML Dev for gemini (apiKey + optional googleGeminiBaseUrl), the publisher catalog for vertex (project/region with the same credentials-json > key-file > ADC chain as GeminiHandler). Catalogs keep only generative gemini-* IDs (embeddings/TTS/Imagen and Vertex Model Garden partner models are excluded — the handlers cannot call them, see issue Zoo-Code-Org#785). Known IDs reuse the curated static specs; unknown IDs get Gemini-family defaults with no pricing so cost reporting shows unknown instead of guessed rates. Fetch candidates are gated on credentials (geminiApiKey; any of vertexProjectId/vertexKeyFile/vertexJsonCredentials) and honor unsaved form values like the other credential-carrying providers. gemini is URL+key-scoped in the model cache; vertex stays provider-scoped.
1 parent f6a39c2 commit 3d4aed8

15 files changed

Lines changed: 1020 additions & 0 deletions

File tree

‎packages/types/src/__tests__/provider-identifiers.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ describe("provider identifiers", () => {
108108
providerIdentifiers.poe,
109109
providerIdentifiers.deepseek,
110110
providerIdentifiers.moonshot,
111+
providerIdentifiers.gemini,
112+
providerIdentifiers.vertex,
111113
providerIdentifiers.opencodeGo,
112114
providerIdentifiers.kenari,
113115
providerIdentifiers.nanogpt,

‎packages/types/src/provider-settings.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export const dynamicProviders = [
7070
providerIdentifiers.poe,
7171
providerIdentifiers.deepseek,
7272
providerIdentifiers.moonshot,
73+
providerIdentifiers.gemini,
74+
providerIdentifiers.vertex,
7375
providerIdentifiers.opencodeGo,
7476
providerIdentifiers.kenari,
7577
providerIdentifiers.nanogpt,
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Mocks must come first, before imports
2+
3+
const { mockList } = vi.hoisted(() => ({ mockList: vi.fn() }))
4+
5+
vi.mock("@google/genai", () => ({
6+
GoogleGenAI: class {
7+
models = { list: mockList }
8+
},
9+
}))
10+
11+
import { getGeminiModels } from "../gemini"
12+
import { geminiModels } from "@roo-code/types"
13+
14+
// Builds the async-iterable pager that client.models.list() resolves to. The
15+
// fetcher only iterates it, so a plain async iterable of minimal Model-likes
16+
// (name only) is sufficient — no real SDK Pager is constructed.
17+
function fakePager(names: Array<string | undefined>) {
18+
const models = names.map((name) => ({ name }))
19+
return {
20+
[Symbol.asyncIterator]() {
21+
let index = 0
22+
return {
23+
next(): Promise<IteratorResult<{ name?: string }>> {
24+
if (index < models.length) {
25+
return Promise.resolve({ value: models[index++], done: false })
26+
}
27+
return Promise.resolve({ value: undefined, done: true })
28+
},
29+
}
30+
},
31+
}
32+
}
33+
34+
describe("getGeminiModels", () => {
35+
beforeEach(() => {
36+
vi.clearAllMocks()
37+
mockList.mockResolvedValue(fakePager(["models/gemini-2.5-flash"]))
38+
})
39+
40+
it("strips the models/ prefix and returns a null-prototype record", async () => {
41+
mockList.mockResolvedValue(fakePager(["models/gemini-2.5-flash", "models/gemini-3.8-flash"]))
42+
43+
const models = await getGeminiModels("test-key")
44+
45+
expect(Object.keys(models).sort()).toEqual(["gemini-2.5-flash", "gemini-3.8-flash"])
46+
expect(Object.getPrototypeOf(models)).toBeNull()
47+
})
48+
49+
it("copies static specs from geminiModels for known model IDs", async () => {
50+
mockList.mockResolvedValue(fakePager(["models/gemini-2.5-flash"]))
51+
52+
const models = await getGeminiModels("test-key")
53+
54+
expect(models["gemini-2.5-flash"]).toEqual(geminiModels["gemini-2.5-flash"])
55+
// Known pricing must survive the merge so cost reporting stays accurate.
56+
expect(models["gemini-2.5-flash"].inputPrice).toBe(geminiModels["gemini-2.5-flash"].inputPrice)
57+
})
58+
59+
it("applies Gemini-family defaults without pricing for unknown model IDs", async () => {
60+
mockList.mockResolvedValue(fakePager(["models/gemini-9.9-future"]))
61+
62+
const models = await getGeminiModels("test-key")
63+
64+
expect(models["gemini-9.9-future"]).toEqual({
65+
maxTokens: 65_536,
66+
contextWindow: 1_048_576,
67+
supportsImages: true,
68+
supportsPromptCache: true,
69+
description: "Gemini model: gemini-9.9-future",
70+
})
71+
// No pricing fields: cost must show "unknown", not charge default rates.
72+
expect(models["gemini-9.9-future"].inputPrice).toBeUndefined()
73+
expect(models["gemini-9.9-future"].outputPrice).toBeUndefined()
74+
})
75+
76+
it("excludes embedding, TTS, and Imagen families and non-gemini IDs", async () => {
77+
mockList.mockResolvedValue(
78+
fakePager([
79+
"models/gemini-2.5-flash",
80+
"models/gemini-embedding-001",
81+
"models/gemini-2.5-flash-preview-tts",
82+
"models/imagen-3.0-generate-002",
83+
"models/embedding-001",
84+
"models/aqa",
85+
]),
86+
)
87+
88+
const models = await getGeminiModels("test-key")
89+
90+
// The catalog is limited to generative gemini-* models the handler can call.
91+
expect(Object.keys(models)).toEqual(["gemini-2.5-flash"])
92+
})
93+
94+
it("skips entries with a missing resource name", async () => {
95+
mockList.mockResolvedValue(fakePager([undefined, "models/gemini-2.5-flash"]))
96+
97+
const models = await getGeminiModels("test-key")
98+
99+
expect(Object.keys(models)).toEqual(["gemini-2.5-flash"])
100+
})
101+
102+
it("throws a clear error when no API key is provided", async () => {
103+
await expect(getGeminiModels()).rejects.toThrow("Failed to fetch Gemini models: API key is required.")
104+
expect(mockList).not.toHaveBeenCalled()
105+
})
106+
107+
it("forwards the base URL through httpOptions", async () => {
108+
await getGeminiModels("test-key", "https://gemini-proxy.example")
109+
110+
expect(mockList).toHaveBeenCalledWith({
111+
config: expect.objectContaining({
112+
httpOptions: { baseUrl: "https://gemini-proxy.example" },
113+
}),
114+
})
115+
})
116+
117+
it("omits httpOptions when no base URL is provided", async () => {
118+
await getGeminiModels("test-key")
119+
120+
expect(mockList).toHaveBeenCalledWith({
121+
config: expect.objectContaining({
122+
httpOptions: undefined,
123+
}),
124+
})
125+
})
126+
127+
it("requests a single page large enough to hold the full catalog", async () => {
128+
await getGeminiModels("test-key")
129+
130+
expect(mockList).toHaveBeenCalledWith({
131+
config: expect.objectContaining({
132+
pageSize: 1000,
133+
}),
134+
})
135+
})
136+
137+
it("passes the caller's abort signal to the request", async () => {
138+
const controller = new AbortController()
139+
140+
await getGeminiModels("test-key", undefined, { signal: controller.signal })
141+
142+
expect(mockList).toHaveBeenCalledWith({
143+
config: expect.objectContaining({
144+
abortSignal: controller.signal,
145+
}),
146+
})
147+
})
148+
})

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

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ vi.mock("../requesty")
4747
vi.mock("../kenari")
4848
vi.mock("../nanogpt")
4949
vi.mock("../moonshot")
50+
vi.mock("../gemini")
51+
vi.mock("../vertex")
5052
vi.mock("../zoo-gateway")
5153

5254
// Mock ContextProxy with a simple static instance
@@ -75,6 +77,8 @@ import { getRequestyModels } from "../requesty"
7577
import { getKenariModels } from "../kenari"
7678
import { getNanoGptModels } from "../nanogpt"
7779
import { getMoonshotModels } from "../moonshot"
80+
import { getGeminiModels } from "../gemini"
81+
import { getVertexModels } from "../vertex"
7882
import { getZooGatewayModels } from "../zoo-gateway"
7983

8084
const mockGetLiteLLMModels = getLiteLLMModels as Mock<typeof getLiteLLMModels>
@@ -83,6 +87,8 @@ const mockGetRequestyModels = getRequestyModels as Mock<typeof getRequestyModels
8387
const mockGetKenariModels = getKenariModels as Mock<typeof getKenariModels>
8488
const mockGetNanoGptModels = getNanoGptModels as Mock<typeof getNanoGptModels>
8589
const mockGetMoonshotModels = getMoonshotModels as Mock<typeof getMoonshotModels>
90+
const mockGetGeminiModels = getGeminiModels as Mock<typeof getGeminiModels>
91+
const mockGetVertexModels = getVertexModels as Mock<typeof getVertexModels>
8692
const mockGetZooGatewayModels = getZooGatewayModels as Mock<typeof getZooGatewayModels>
8793

8894
const DUMMY_REQUESTY_KEY = "requesty-key-for-testing"
@@ -263,6 +269,72 @@ describe("getModels with new GetModelsOptions", () => {
263269
expect(result).toEqual(mockModels)
264270
})
265271

272+
it("calls getGeminiModels with the API key, base URL, and the flight signal", async () => {
273+
const mockModels = {
274+
"gemini-2.5-flash": {
275+
maxTokens: 64_000,
276+
contextWindow: 1_048_576,
277+
supportsImages: true,
278+
supportsPromptCache: true,
279+
description: "Gemini 2.5 Flash",
280+
},
281+
}
282+
mockGetGeminiModels.mockResolvedValue(mockModels)
283+
284+
const result = await getModels({
285+
provider: providerIdentifiers.gemini,
286+
apiKey: "gemini-key",
287+
baseUrl: "https://gemini-proxy.example",
288+
})
289+
290+
expect(mockGetGeminiModels).toHaveBeenCalledWith("gemini-key", "https://gemini-proxy.example", {
291+
signal: expect.any(AbortSignal),
292+
})
293+
expect(result).toEqual(mockModels)
294+
})
295+
296+
it("calls getGeminiModels without a base URL while preserving the signal arity", async () => {
297+
mockGetGeminiModels.mockResolvedValue({
298+
"gemini-2.5-flash": {
299+
maxTokens: 64_000,
300+
contextWindow: 1_048_576,
301+
supportsImages: true,
302+
supportsPromptCache: true,
303+
},
304+
})
305+
306+
await getModels({ provider: providerIdentifiers.gemini, apiKey: "gemini-key" })
307+
308+
expect(mockGetGeminiModels).toHaveBeenCalledWith("gemini-key", undefined, {
309+
signal: expect.any(AbortSignal),
310+
})
311+
})
312+
313+
it("calls getVertexModels with project, region, credentials, and the flight signal", async () => {
314+
const mockModels = {
315+
"gemini-3.7-flash": {
316+
maxTokens: 65_536,
317+
contextWindow: 1_048_576,
318+
supportsImages: true,
319+
supportsPromptCache: true,
320+
description: "Vertex Gemini 3.7 Flash",
321+
},
322+
}
323+
mockGetVertexModels.mockResolvedValue(mockModels)
324+
325+
const result = await getModels({
326+
provider: providerIdentifiers.vertex,
327+
projectId: "gcp-project",
328+
region: "us-central1",
329+
keyFile: "/keys/sa.json",
330+
})
331+
332+
expect(mockGetVertexModels).toHaveBeenCalledWith("gcp-project", "us-central1", "/keys/sa.json", undefined, {
333+
signal: expect.any(AbortSignal),
334+
})
335+
expect(result).toEqual(mockModels)
336+
})
337+
266338
it("validates exhaustive provider checking with unknown provider", async () => {
267339
// This test ensures TypeScript catches unknown providers at compile time
268340
// In practice, the discriminated union should prevent this at compile time
@@ -1136,6 +1208,103 @@ describe("NanoGPT key-scoped cache isolation", () => {
11361208
})
11371209
})
11381210

1211+
describe("Gemini url+key-scoped cache isolation", () => {
1212+
// Gemini belongs to BOTH URL_SCOPED_PROVIDERS and KEY_SCOPED_PROVIDERS (modelCache.ts):
1213+
// its catalog can differ per API key and a custom base URL (googleGeminiBaseUrl) can
1214+
// point at an entirely different server, so neither dimension may collapse into a
1215+
// shared cache identity. Mirrors the MiMo/NanoGPT isolation tests above.
1216+
const geminiCatalog = {
1217+
"gemini-2.5-flash": {
1218+
maxTokens: 64_000,
1219+
contextWindow: 1_048_576,
1220+
supportsImages: true,
1221+
supportsPromptCache: true,
1222+
},
1223+
}
1224+
1225+
beforeEach(() => {
1226+
vi.clearAllMocks()
1227+
mockGetGeminiModels.mockResolvedValue(geminiCatalog)
1228+
})
1229+
1230+
it("separates cache identities by base URL and API key without exposing raw keys", async () => {
1231+
const mockCache = vi.mocked(new (vi.mocked(NodeCache))())
1232+
mockCache.get.mockReturnValue(undefined)
1233+
1234+
await getModels({ provider: providerIdentifiers.gemini })
1235+
await getModels({ provider: providerIdentifiers.gemini, baseUrl: "https://gemini-proxy.example" })
1236+
await getModels({
1237+
provider: providerIdentifiers.gemini,
1238+
baseUrl: "https://gemini-proxy.example",
1239+
apiKey: "gemini-key-a",
1240+
})
1241+
await getModels({
1242+
provider: providerIdentifiers.gemini,
1243+
baseUrl: "https://gemini-proxy.example",
1244+
apiKey: "gemini-key-b",
1245+
})
1246+
1247+
const cacheKeys = mockCache.set.mock.calls.map(([key]) => key as string)
1248+
expect(new Set(cacheKeys).size).toBe(4)
1249+
// Bare provider fallback when neither URL nor key is set; url-only component when
1250+
// the key is absent (catalog varies per key on the same server).
1251+
expect(cacheKeys).toContain("gemini")
1252+
expect(cacheKeys).toContain("gemini:https://gemini-proxy.example")
1253+
// url+key compound identity for each distinct key on the proxy server.
1254+
expect(cacheKeys.filter((key) => key.startsWith("gemini:https://gemini-proxy.example:"))).toHaveLength(2)
1255+
// Raw secrets must never appear in the on-disk-bound cache keys.
1256+
expect(cacheKeys.every((key) => !key.includes("gemini-key-a") && !key.includes("gemini-key-b"))).toBe(true)
1257+
})
1258+
1259+
it("keys the catalog per base URL so two servers never share an entry", async () => {
1260+
const mockCache = vi.mocked(new (vi.mocked(NodeCache))())
1261+
mockCache.get.mockReturnValue(undefined)
1262+
1263+
await getModels({ provider: providerIdentifiers.gemini, apiKey: "shared-key", baseUrl: "https://a.example" })
1264+
await getModels({ provider: providerIdentifiers.gemini, apiKey: "shared-key", baseUrl: "https://b.example" })
1265+
1266+
const cacheKeys = mockCache.set.mock.calls.map(([key]) => key as string)
1267+
expect(new Set(cacheKeys).size).toBe(2)
1268+
expect(cacheKeys.every((key) => key.startsWith("gemini:https://"))).toBe(true)
1269+
})
1270+
})
1271+
1272+
describe("Vertex provider-scoped cache identity", () => {
1273+
// Vertex is in NEITHER URL_SCOPED_PROVIDERS nor KEY_SCOPED_PROVIDERS (modelCache.ts):
1274+
// its options carry no apiKey/baseUrl discriminator and the gemini-* catalog is global
1275+
// to the project/region, so getCacheKey() falls back to the bare provider name and the
1276+
// 5-minute TTL bounds staleness.
1277+
const vertexCatalog = {
1278+
"gemini-3.7-flash": {
1279+
maxTokens: 65_536,
1280+
contextWindow: 1_048_576,
1281+
supportsImages: true,
1282+
supportsPromptCache: true,
1283+
},
1284+
}
1285+
1286+
beforeEach(() => {
1287+
vi.clearAllMocks()
1288+
mockGetVertexModels.mockResolvedValue(vertexCatalog)
1289+
})
1290+
1291+
it("falls back to the bare provider name regardless of project, region, or credentials", async () => {
1292+
const mockCache = vi.mocked(new (vi.mocked(NodeCache))())
1293+
mockCache.get.mockReturnValue(undefined)
1294+
1295+
await getModels({ provider: providerIdentifiers.vertex, projectId: "project-a", region: "us-central1" })
1296+
await getModels({
1297+
provider: providerIdentifiers.vertex,
1298+
projectId: "project-b",
1299+
region: "europe-west1",
1300+
keyFile: "/keys/sa.json",
1301+
})
1302+
1303+
const cacheKeys = mockCache.set.mock.calls.map(([key]) => key as string)
1304+
expect(cacheKeys).toEqual(["vertex", "vertex"])
1305+
})
1306+
})
1307+
11391308
describe("compound cache key derivation across scoping dimensions", () => {
11401309
// Exercises every branch of getCacheKey via the public getModels() entry point.
11411310
// litellm is url-scoped AND key-scoped; openrouter is neither, so it hits the bare

0 commit comments

Comments
 (0)