Skip to content

Commit c42716d

Browse files
add safe provider model discovery
1 parent a70e0cc commit c42716d

7 files changed

Lines changed: 506 additions & 25 deletions

File tree

‎src/codex/catalog/provider-fetch.ts‎

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,53 @@ export type ProviderModelsApiItem = {
6363
};
6464
};
6565

66-
export function isProviderModelsApiItems(value: unknown): value is ProviderModelsApiItem[] {
67-
return Array.isArray(value) && value.every(item =>
68-
item !== null
69-
&& typeof item === "object"
70-
&& !Array.isArray(item)
71-
&& typeof (item as { id?: unknown }).id === "string"
72-
&& (item as { id: string }).id.trim().length > 0
73-
);
66+
/** Normalize the common model-catalog envelopes without weakening malformed-response fallback. */
67+
export function parseProviderModelsApiItems(value: unknown, allowModelsEnvelope = false): ProviderModelsApiItem[] | null {
68+
const rows = Array.isArray(value)
69+
? value
70+
: value !== null && typeof value === "object"
71+
? (Array.isArray((value as { data?: unknown }).data)
72+
? (value as { data: unknown[] }).data
73+
: allowModelsEnvelope && Array.isArray((value as { models?: unknown }).models)
74+
? (value as { models: unknown[] }).models
75+
: null)
76+
: null;
77+
if (!rows) return null;
78+
79+
const normalized: ProviderModelsApiItem[] = [];
80+
for (const row of rows) {
81+
if (typeof row === "string" && row.trim()) {
82+
normalized.push({ id: row.trim() });
83+
continue;
84+
}
85+
if (row === null || typeof row !== "object" || Array.isArray(row)) return null;
86+
const item = row as Record<string, unknown>;
87+
const rawId = typeof item.id === "string" ? item.id
88+
: typeof item.name === "string" ? item.name
89+
: typeof item.model === "string" ? item.model
90+
: "";
91+
const id = rawId.trim()
92+
.replace(/^models\//, "")
93+
.replace(/^accounts\/fireworks\/models\//, "");
94+
if (!id) return null;
95+
const contextLength = [
96+
item.context_length,
97+
item.context_window,
98+
item.contextWindow,
99+
item.inputTokenLimit,
100+
item.max_context_length,
101+
item.max_model_len,
102+
].find(candidate => typeof candidate === "number" && Number.isFinite(candidate) && candidate > 0) as number | undefined;
103+
normalized.push({
104+
id,
105+
...(typeof item.owned_by === "string" ? { owned_by: item.owned_by } : {}),
106+
...(contextLength ? { context_length: contextLength } : {}),
107+
...(item.metadata !== null && typeof item.metadata === "object" && !Array.isArray(item.metadata)
108+
? { metadata: item.metadata as ProviderModelsApiItem["metadata"] }
109+
: {}),
110+
});
111+
}
112+
return normalized;
74113
}
75114

76115
export function configuredContextWindow(prov: OcxProviderConfig, id: string): number | undefined {
@@ -326,7 +365,7 @@ export async function fetchProviderModels(name: string, prov: OcxProviderConfig,
326365
return models;
327366
}
328367

329-
const res = await fetch(url, { headers, signal: AbortSignal.timeout(8000) });
368+
const res = await fetch(url, { headers, redirect: "error", signal: AbortSignal.timeout(8000) });
330369
if (!res.ok) {
331370
const { models, fallback } = failedDiscoveryFallback({ reason: "http", httpStatus: res.status });
332371
console.warn(
@@ -352,17 +391,16 @@ export async function fetchProviderModels(name: string, prov: OcxProviderConfig,
352391
);
353392
return models;
354393
}
355-
const data = json !== null && typeof json === "object" && !Array.isArray(json)
356-
? (json as { data?: unknown }).data
357-
: undefined;
358-
if (!isProviderModelsApiItems(data)) {
394+
const allowsModelsEnvelope = (prov.adapter === "google" && (prov.googleMode ?? "ai-studio") === "ai-studio")
395+
|| new URL(url).pathname.replace(/\/+$/, "").endsWith("/api/tags");
396+
const items = parseProviderModelsApiItems(json, allowsModelsEnvelope);
397+
if (!items) {
359398
const { models, fallback } = failedDiscoveryFallback({ reason: "invalid_response" });
360399
console.warn(
361400
`[opencodex] Provider model discovery for "${name}" returned malformed 2xx data [status=${res.status}, contentType=${contentType}, urlClass=${urlClass}, fallback=${fallback}].`,
362401
);
363402
return models;
364403
}
365-
const items = data;
366404
const live = items.map(m => applyProviderConfigHints(name, prov, {
367405
id: m.id,
368406
provider: name,

‎src/oauth/index.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { loginAntigravity, refreshAntigravityToken } from "./google-antigravity"
1313
import { loginCursor, refreshCursorToken } from "./cursor";
1414
import { loginGithubCopilot, refreshGithubCopilotToken, validateCopilotApiBaseUrl } from "./github-copilot";
1515
import { deriveOAuthDefaultModel, deriveOAuthProviderConfig } from "../providers/derive";
16-
import { effectiveGoogleMode } from "../providers/registry";
16+
import { effectiveGoogleMode, getProviderRegistryEntry } from "../providers/registry";
1717
import { resolveProviderTransport } from "../providers/xai-transport";
1818
import { detectClaudeCodeToken, detectGrokCliToken, hasComparableGrokIdentity, isSameGrokIdentity, shouldAdoptGrokGeneration } from "./local-token-detect";
1919

@@ -436,13 +436,16 @@ export function buildModelsRequest(prov: OcxProviderConfig, apiKey: string | und
436436
providerName === "github-copilot" ? getOAuthCredentialApiBaseUrl(providerName) : undefined,
437437
);
438438
const headers: Record<string, string> = { ...(effectiveProvider.headers ?? {}) };
439+
const registry = getProviderRegistryEntry(providerName);
440+
const sameRegistryEndpoint = registry?.baseUrl.replace(/\/+$/, "") === effectiveProvider.baseUrl.replace(/\/+$/, "");
441+
const trustedModelsUrl = sameRegistryEndpoint ? registry?.modelsUrl : undefined;
439442
if (effectiveGoogleMode(providerName, effectiveProvider) === "ai-studio") {
440443
// Generative Language API: API key goes in x-goog-api-key (never Authorization: Bearer),
441444
// models live under /v1beta (v1 misses preview models), and pageSize maxes at 1000 —
442445
// enough to list everything without a pageToken loop. Vertex/antigravity keep the
443446
// generic branch (they fall back to their static model lists).
444447
if (apiKey) headers["x-goog-api-key"] = apiKey;
445-
return { url: `${effectiveProvider.baseUrl}/v1beta/models?pageSize=1000`, headers };
448+
return { url: trustedModelsUrl ?? `${effectiveProvider.baseUrl}/v1beta/models?pageSize=1000`, headers };
446449
}
447450
if (effectiveProvider.adapter === "anthropic") {
448451
headers["anthropic-version"] = "2023-06-01";
@@ -452,10 +455,10 @@ export function buildModelsRequest(prov: OcxProviderConfig, apiKey: string | und
452455
} else if (apiKey) {
453456
headers["x-api-key"] = apiKey;
454457
}
455-
return { url: `${effectiveProvider.baseUrl}/v1/models?limit=1000`, headers };
458+
return { url: trustedModelsUrl ?? `${effectiveProvider.baseUrl}/v1/models?limit=1000`, headers };
456459
}
457460
if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;
458-
return { url: `${effectiveProvider.baseUrl}/models`, headers };
461+
return { url: trustedModelsUrl ?? `${effectiveProvider.baseUrl}/models`, headers };
459462
}
460463

461464
/**

‎src/server/management/provider-routes.ts‎

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto";
22
import { readFileSync } from "node:fs";
33
import type { CatalogModel } from "../../codex/catalog";
44
import { catalogModelSlug, invalidateCodexModelsCache, nativeModelRows, uniqueCatalogModelsForPublicList } from "../../codex/catalog";
5+
import { parseProviderModelsApiItems } from "../../codex/catalog/provider-fetch";
56
import {
67
DEFAULT_SUBAGENT_MODELS,
78
codexAutoStartEnabled,
@@ -24,7 +25,7 @@ import {
2425
import { removeCredential } from "../../oauth/store";
2526
import { providerDestinationResolvedError } from "../../lib/destination-policy";
2627
import { enrichProviderFromCatalog, listKeyLoginProviders } from "../../oauth/key-providers";
27-
import { deriveProviderPresets } from "../../providers/derive";
28+
import { deriveProviderPresets, providerConfigSeed } from "../../providers/derive";
2829
import { providerCodexAccountMode } from "../../providers/registry";
2930
import { routedSlug, slugEquals } from "../../providers/slug-codec";
3031
import { clearProviderQuotaCache, fetchProviderQuotaReports } from "../../providers/quota";
@@ -406,5 +407,113 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise<Resp
406407
if (url.pathname === "/api/provider-presets" && req.method === "GET") {
407408
return jsonResponse({ providers: deriveProviderPresets() });
408409
}
410+
411+
if (url.pathname === "/api/provider-presets/discover" && req.method === "POST") {
412+
let body: { presetId?: unknown; provider?: unknown };
413+
try { body = await req.json(); } catch { return jsonResponse({ error: "invalid JSON body" }, 400); }
414+
const presetId = typeof body.presetId === "string" ? body.presetId.trim() : "";
415+
if (!presetId || !isValidProviderName(presetId)) return jsonResponse({ error: "invalid presetId" }, 400);
416+
const entry = getProviderRegistryEntry(presetId);
417+
if (!entry) return jsonResponse({ error: "unknown provider preset" }, 404);
418+
if (entry.supportLevel === "reference") {
419+
return jsonResponse({ error: "reference-only provider presets cannot be connected automatically" }, 400);
420+
}
421+
const providerError = providerManagementConfigError(presetId, body.provider);
422+
if (providerError) return jsonResponse({ error: providerError }, 400);
423+
const supplied = stripCodexRuntimeProviderFields(body.provider as OcxProviderConfig);
424+
const suppliedBaseUrl = supplied.baseUrl?.trim();
425+
let matchesResolvedTemplate = false;
426+
if (suppliedBaseUrl && entry.baseUrl.includes("{")) {
427+
try {
428+
const marker = "opencodex-template-value";
429+
const templateUrl = new URL(entry.baseUrl.replace(/\{[^}]+\}/g, marker));
430+
const candidateUrl = new URL(suppliedBaseUrl);
431+
const escapedPath = templateUrl.pathname.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
432+
const pathPattern = new RegExp(`^${escapedPath.replaceAll(marker, "[A-Za-z0-9_-]+")}/?$`);
433+
matchesResolvedTemplate = candidateUrl.origin === templateUrl.origin
434+
&& !candidateUrl.search
435+
&& !candidateUrl.hash
436+
&& pathPattern.test(candidateUrl.pathname);
437+
} catch {
438+
matchesResolvedTemplate = false;
439+
}
440+
}
441+
const normalizedSuppliedBaseUrl = suppliedBaseUrl?.replace(/\/+$/, "");
442+
const matchesOfficialChoice = !!normalizedSuppliedBaseUrl && !!entry.baseUrlChoices?.some(choice =>
443+
choice.baseUrl?.replace(/\/+$/, "") === normalizedSuppliedBaseUrl
444+
);
445+
const acceptsResolvedBaseUrl = entry.allowBaseUrlOverride === true || matchesResolvedTemplate || matchesOfficialChoice;
446+
if (suppliedBaseUrl && suppliedBaseUrl.replace(/\/+$/, "") !== entry.baseUrl.replace(/\/+$/, "") && !acceptsResolvedBaseUrl) {
447+
return jsonResponse({ error: "provider baseUrl does not match the trusted preset" }, 400);
448+
}
449+
const provider = {
450+
...providerConfigSeed(entry),
451+
...(suppliedBaseUrl ? { baseUrl: suppliedBaseUrl } : {}),
452+
...(supplied.apiKey ? { apiKey: supplied.apiKey } : {}),
453+
...(supplied.defaultModel ? { defaultModel: supplied.defaultModel } : {}),
454+
...(supplied.allowPrivateNetwork === true ? { allowPrivateNetwork: true } : {}),
455+
...(supplied.project ? { project: supplied.project } : {}),
456+
...(supplied.location ? { location: supplied.location } : {}),
457+
};
458+
const staticModels = (entry.models ?? []).map(id => ({
459+
id,
460+
...(entry.modelContextWindows?.[id] ? { contextWindow: entry.modelContextWindows[id] } : {}),
461+
}));
462+
const destinationError = await providerDestinationResolvedError(presetId, provider);
463+
if (destinationError) return jsonResponse({ error: destinationError }, 400);
464+
if (entry.discovery === "static" || entry.discovery === "unsupported" || entry.liveModels === false) {
465+
return jsonResponse(staticModels.length
466+
? { ok: true, models: staticModels, source: "static", latencyMs: 0 }
467+
: { ok: false, models: [], source: "static", latencyMs: 0, error: "No static model catalog is available for this preset" });
468+
}
469+
const { resolveModelsAuthToken, buildModelsRequest } = await import("../../oauth");
470+
const apiKey = await resolveModelsAuthToken(presetId, provider);
471+
const request = buildModelsRequest(provider, apiKey, presetId);
472+
const modelsDestinationError = await providerDestinationResolvedError(presetId, { baseUrl: request.url });
473+
if (modelsDestinationError) return jsonResponse({ error: modelsDestinationError }, 400);
474+
const started = Date.now();
475+
try {
476+
const response = await fetch(request.url, { headers: request.headers, redirect: "error", signal: AbortSignal.timeout(8000) });
477+
const latencyMs = Date.now() - started;
478+
if (!response.ok) {
479+
const error = `upstream model discovery returned ${response.status}`;
480+
return jsonResponse(staticModels.length
481+
? { ok: true, models: staticModels, source: "static", latencyMs, error }
482+
: { ok: false, models: [], source: "live", latencyMs, error });
483+
}
484+
const allowsModelsEnvelope = (entry.adapter === "google" && (entry.googleMode ?? "ai-studio") === "ai-studio")
485+
|| new URL(request.url).pathname.replace(/\/+$/, "").endsWith("/api/tags");
486+
const rows = parseProviderModelsApiItems(await response.json().catch(() => null), allowsModelsEnvelope);
487+
if (!rows) {
488+
const error = "upstream model discovery returned an unexpected shape";
489+
return jsonResponse(staticModels.length
490+
? { ok: true, models: staticModels, source: "static", latencyMs, error }
491+
: { ok: false, models: [], source: "live", latencyMs, error });
492+
}
493+
const uniqueRows = new Map<string, (typeof rows)[number]>();
494+
for (const row of rows) {
495+
const current = uniqueRows.get(row.id);
496+
if (!current || (!current.context_length && row.context_length)) uniqueRows.set(row.id, row);
497+
if (uniqueRows.size >= 2000) break;
498+
}
499+
const models = [...uniqueRows.values()].map(row => ({
500+
id: row.id,
501+
...(row.context_length ? { contextWindow: row.context_length } : {}),
502+
}));
503+
if (models.length === 0) {
504+
const error = "upstream model discovery returned an empty catalog";
505+
return jsonResponse(staticModels.length
506+
? { ok: true, models: staticModels, source: "static", latencyMs, error }
507+
: { ok: false, models: [], source: "live", latencyMs, error });
508+
}
509+
return jsonResponse({ ok: true, models, source: "live", latencyMs });
510+
} catch {
511+
const latencyMs = Date.now() - started;
512+
const error = "model discovery request failed";
513+
return jsonResponse(staticModels.length
514+
? { ok: true, models: staticModels, source: "static", latencyMs, error }
515+
: { ok: false, models: [], source: "live", latencyMs, error });
516+
}
517+
}
409518
return null;
410519
}

‎tests/google-models-listing.test.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("buildModelsRequest google routing", () => {
5050
});
5151

5252
describe("google models listing via catalog", () => {
53-
test("treats a { models } 2xx shape as malformed and degrades to the static seed", async () => {
53+
test("normalizes the Google { models } envelope and caches the live catalog", async () => {
5454
clearModelCache("google");
5555
const warning = spyOn(console, "warn").mockImplementation(() => {});
5656
const seen: { url: string; headers: Record<string, string> }[] = [];
@@ -77,11 +77,15 @@ describe("google models listing via catalog", () => {
7777
expect(seen[0].url).toBe("https://generativelanguage.googleapis.com/v1beta/models?pageSize=1000");
7878
expect(seen[0].headers["x-goog-api-key"]).toBe("gk-123");
7979
const ids = models.filter(m => m.provider === "google").map(m => m.id);
80-
expect(ids).toEqual(["gemini-3.1-pro-preview", "gemini-3.5-flash", "gemini-3.5-flash-lite", "gemini-3.6-flash"]);
81-
expect(ids).not.toContain("gemini-3-pro");
82-
expect(ids).not.toContain("gemini-3-flash");
83-
expect(getStaleCached("google")).toBeNull();
84-
expect(warning.mock.calls.flat().join(" ")).toContain("google");
80+
expect(ids).toEqual(["gemini-3-flash", "gemini-3-pro", "text-embedding-004"]);
81+
expect(getStaleCached("google")?.map(model => model.id)).toEqual([
82+
"gemini-3-pro",
83+
"text-embedding-004",
84+
"gemini-3-flash",
85+
]);
86+
const warningText = warning.mock.calls.flat().join(" ");
87+
expect(warningText).toContain("omitted configured model ids");
88+
expect(warningText).not.toContain("malformed 2xx");
8589
} finally {
8690
warning.mockRestore();
8791
}

0 commit comments

Comments
 (0)