Skip to content

Commit 0713778

Browse files
committed
fix(cli): build ocx opencode catalog from proxy /api/models
Fetch the live model list from the running proxy instead of calling fetchAllModels in the CLI process, so env-backed provider keys resolve in the proxy environment and namespaced selectors carry display metadata.
1 parent 2cfe07b commit 0713778

2 files changed

Lines changed: 277 additions & 33 deletions

File tree

‎src/cli/opencode.ts‎

Lines changed: 143 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ export interface OpencodeRoutedModel {
4242
displayName?: string;
4343
}
4444

45+
/** Row shape from authenticated GET /api/models on the running proxy. */
46+
export interface OpencodeProxyModelRow {
47+
provider?: string;
48+
id?: string;
49+
namespaced?: string;
50+
native?: boolean;
51+
disabled?: boolean;
52+
displayName?: string;
53+
contextWindow?: number;
54+
}
55+
56+
/** Visible catalog entry keyed by the proxy's canonical namespaced selector. */
57+
export interface OpencodeCatalogModel {
58+
namespaced: string;
59+
native?: boolean;
60+
provider?: string;
61+
id?: string;
62+
contextWindow?: number;
63+
displayName?: string;
64+
}
65+
4566
export interface OpencodeModelEntry {
4667
name: string;
4768
limit?: { context: number; output: number };
@@ -244,33 +265,36 @@ function opencodeProviderOptions(baseURL: string, config: OcxConfig): OpencodePr
244265
return options;
245266
}
246267

268+
function opencodeModelEntryLabel(model: OpencodeCatalogModel): string {
269+
const providerLabel = model.native ? "native" : (model.provider ?? "routed");
270+
const id = model.id ?? model.namespaced;
271+
if (model.displayName && model.displayName.length > 0) {
272+
return `${model.displayName} (${providerLabel})`;
273+
}
274+
return `${id} (${providerLabel})`;
275+
}
276+
247277
/**
248-
* Build the `opencodex` provider block from the proxy's visible catalog.
278+
* Build the `opencodex` provider block from proxy catalog rows keyed by each row's
279+
* canonical `namespaced` selector.
249280
*
250281
* `limit.context` is emitted ONLY from an authoritative context window — never guessed.
251282
* When none is available the whole `limit` block is dropped and opencode keeps its own
252283
* defaults; when one is present, `limit.output` rides along (opencode's schema requires
253284
* the pair) clamped to the context window.
254285
*/
255-
export function buildOpencodeProviderBlock(
286+
export function buildOpencodeProviderBlockFromCatalog(
256287
port: number,
257-
nativeSlugs: readonly string[],
258-
routedModels: readonly OpencodeRoutedModel[],
259-
nativeContextWindow: (slug: string) => number | undefined = () => undefined,
288+
catalogModels: readonly OpencodeCatalogModel[],
260289
hostname?: string,
261290
config: OcxConfig = OPENCODE_PROVIDER_BLOCK_DEFAULT_CONFIG,
262291
): OpencodeProviderBlock {
263292
const models: Record<string, OpencodeModelEntry> = {};
264-
const candidates: OpencodeRoutedModel[] = [
265-
...nativeSlugs.map(id => ({ provider: "native", id, contextWindow: nativeContextWindow(id) })),
266-
...routedModels,
267-
];
268-
for (const { provider, id, contextWindow, displayName } of candidates) {
269-
const key = opencodeModelKey(provider, id);
270-
if (models[key]) continue; // first entry wins; native slugs are registered first
271-
const entry: OpencodeModelEntry = {
272-
name: displayName && displayName.length > 0 ? `${displayName} (${provider})` : `${id} (${provider})`,
273-
};
293+
for (const model of catalogModels) {
294+
const key = model.namespaced;
295+
if (models[key]) continue; // first entry wins; native rows lead /api/models
296+
const entry: OpencodeModelEntry = { name: opencodeModelEntryLabel(model) };
297+
const { contextWindow } = model;
274298
if (typeof contextWindow === "number" && Number.isFinite(contextWindow) && contextWindow > 0) {
275299
const context = Math.floor(contextWindow);
276300
entry.limit = { context, output: Math.min(SCHEMA_REQUIRED_OUTPUT_BUDGET, context) };
@@ -285,6 +309,102 @@ export function buildOpencodeProviderBlock(
285309
};
286310
}
287311

312+
/** Back-compat helper for unit tests that assemble slugs/routed rows directly. */
313+
export function buildOpencodeProviderBlock(
314+
port: number,
315+
nativeSlugs: readonly string[],
316+
routedModels: readonly OpencodeRoutedModel[],
317+
nativeContextWindow: (slug: string) => number | undefined = () => undefined,
318+
hostname?: string,
319+
config: OcxConfig = OPENCODE_PROVIDER_BLOCK_DEFAULT_CONFIG,
320+
): OpencodeProviderBlock {
321+
const catalog: OpencodeCatalogModel[] = [
322+
...nativeSlugs.map(id => ({
323+
namespaced: id,
324+
native: true,
325+
provider: "openai",
326+
id,
327+
contextWindow: nativeContextWindow(id),
328+
})),
329+
...routedModels.map(model => ({
330+
namespaced: opencodeModelKey(model.provider, model.id),
331+
native: false,
332+
provider: model.provider,
333+
id: model.id,
334+
contextWindow: model.contextWindow,
335+
displayName: model.displayName,
336+
})),
337+
];
338+
return buildOpencodeProviderBlockFromCatalog(port, catalog, hostname, config);
339+
}
340+
341+
/** Fetch the live model catalog from a running proxy's management API. */
342+
export async function fetchOpencodeProxyModels(
343+
live: LiveProxy,
344+
apiKey: string,
345+
deps: { fetchImpl?: typeof fetch } = {},
346+
): Promise<OpencodeProxyModelRow[]> {
347+
const baseUrl = `http://${probeHostname(live.hostname)}:${live.port}`;
348+
const fetchImpl = deps.fetchImpl ?? fetch;
349+
const headers = new Headers({ Accept: "application/json" });
350+
const token = apiKey.trim();
351+
if (token) headers.set("X-OpenCodex-API-Key", token);
352+
353+
let response: Response;
354+
try {
355+
response = await fetchImpl(`${baseUrl}/api/models`, { headers });
356+
} catch (error) {
357+
throw new Error(
358+
`Management API is unreachable: ${error instanceof Error ? error.message : String(error)}`,
359+
);
360+
}
361+
const text = await response.text();
362+
let body: unknown = null;
363+
if (text) {
364+
try { body = JSON.parse(text); }
365+
catch { body = text; }
366+
}
367+
if (!response.ok) {
368+
const message = body && typeof body === "object" && typeof (body as Record<string, unknown>).error === "string"
369+
? (body as Record<string, string>).error
370+
: `Management request failed (${response.status})`;
371+
throw new Error(message);
372+
}
373+
if (!Array.isArray(body)) {
374+
throw new Error("Management API returned an unexpected /api/models payload.");
375+
}
376+
return body as OpencodeProxyModelRow[];
377+
}
378+
379+
/**
380+
* Visible OpenCode catalog entries from proxy /api/models rows. Disabled rows are omitted;
381+
* native rows are omitted in Codex Direct mode.
382+
*/
383+
export function opencodeCatalogFromProxyRows(
384+
rows: readonly OpencodeProxyModelRow[],
385+
config: OcxConfig,
386+
): OpencodeCatalogModel[] {
387+
const omitNative = providerCodexAccountMode("openai", config.providers?.openai) === "direct";
388+
const seen = new Set<string>();
389+
const catalog: OpencodeCatalogModel[] = [];
390+
for (const row of rows) {
391+
const namespaced = row.namespaced?.trim();
392+
if (!namespaced || row.disabled === true) continue;
393+
if (omitNative && row.native === true) continue;
394+
if (seen.has(namespaced)) continue;
395+
seen.add(namespaced);
396+
catalog.push({
397+
namespaced,
398+
native: row.native === true,
399+
provider: row.provider,
400+
id: row.id,
401+
contextWindow: row.contextWindow,
402+
displayName: row.displayName,
403+
});
404+
}
405+
return catalog;
406+
}
407+
288408
export type OpencodeRuntimeConfigError = { error: string };
289409

290410
/** True when mergeOpencodeRuntimeConfig rejected inherited inline config. */
@@ -502,42 +622,32 @@ export async function cmdOpencode(args: string[]): Promise<number> {
502622
return 1;
503623
}
504624

505-
const { fetchAllModels } = await import("../server/management-api");
506-
const { filterCatalogVisibleModels, nativeOpenAiContextWindow } = await import("../codex/catalog");
507-
let allModels: Awaited<ReturnType<typeof fetchAllModels>>;
625+
const apiKey = opencodeApiKey(config);
626+
let proxyModels: OpencodeProxyModelRow[];
508627
try {
509-
allModels = await fetchAllModels(config);
628+
proxyModels = await fetchOpencodeProxyModels(live, apiKey);
510629
} catch (error) {
511630
const reason = error instanceof Error ? error.message : String(error);
512631
console.error(`❌ Could not fetch the model catalog from the proxy: ${reason}`);
513632
return 1;
514633
}
515-
const routed = filterCatalogVisibleModels(allModels, config).map(m => ({
516-
provider: m.provider,
517-
id: m.id,
518-
contextWindow: m.contextWindow,
519-
displayName: m.displayName,
520-
}));
521-
const nativeSlugs = opencodeLaunchNativeSlugs(config);
522-
523-
const providerBlock = buildOpencodeProviderBlock(
634+
const catalog = opencodeCatalogFromProxyRows(proxyModels, config);
635+
const providerBlock = buildOpencodeProviderBlockFromCatalog(
524636
live.port,
525-
nativeSlugs,
526-
routed,
527-
nativeOpenAiContextWindow,
637+
catalog,
528638
live.hostname,
529639
config,
530640
);
531641
const baseUrl = providerBlock.options.baseURL;
532-
const modelCount = nativeSlugs.length + routed.length;
642+
const modelCount = catalog.length;
533643
console.error(`✅ opencode wired to ${baseUrl} — ${modelCount} model(s) under provider \`${OPENCODE_PROVIDER_ID}\`.`);
534644
console.error(" Your existing opencode config files are left untouched; only the runtime provider block is injected.");
535645
const providerOverride = opencodeProviderOverridePath(process.cwd());
536646
if (providerOverride) {
537647
console.error(`ℹ ${providerOverride} also defines provider.${OPENCODE_PROVIDER_ID}; the runtime layer from ocx opencode overrides it for this launch.`);
538648
}
539649

540-
const builtEnv = buildOpencodeEnv(providerBlock, opencodeApiKey(config), process.env);
650+
const builtEnv = buildOpencodeEnv(providerBlock, apiKey, process.env);
541651
if ("error" in builtEnv) {
542652
console.error(`❌ ${builtEnv.error}`);
543653
return 1;

‎tests/opencode-cli.test.ts‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test";
22
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
5+
import { clearModelCache } from "../src/codex/model-cache";
56
import { loadServiceTokenFromFile, serviceApiTokenFilePath } from "../src/lib/service-secrets";
67
import {
78
OPENCODE_API_KEY_ENV,
@@ -12,9 +13,12 @@ import {
1213
buildOpencodeConfig,
1314
buildOpencodeEnv,
1415
buildOpencodeProviderBlock,
16+
buildOpencodeProviderBlockFromCatalog,
17+
fetchOpencodeProxyModels,
1518
isOpencodeRuntimeConfigError,
1619
mergeOpencodeRuntimeConfig,
1720
opencodeApiKey,
21+
opencodeCatalogFromProxyRows,
1822
opencodeGlobalConfigPath,
1923
opencodeLaunchNativeSlugs,
2024
opencodeModelKey,
@@ -211,6 +215,136 @@ describe("ocx opencode JSONC parsing", () => {
211215
});
212216
});
213217

218+
describe("ocx opencode proxy model catalog", () => {
219+
const ENV_KEY = "OCX_TEST_OPENCODE_PROXY_ONLY_KEY";
220+
const RESOLVED = "proxy-only-resolved-key";
221+
const PROVIDER = "proxyenv";
222+
223+
test("uses /api/models namespaced selectors and resolves env-backed provider keys only in the proxy", async () => {
224+
const originalFetch = globalThis.fetch;
225+
let requestedAuth: string | undefined;
226+
globalThis.fetch = (async (url: string | URL | Request, init?: RequestInit) => {
227+
const target = String(url);
228+
if (target.includes("proxyenv.test")) {
229+
requestedAuth = new Headers(init?.headers).get("authorization") ?? undefined;
230+
if (requestedAuth === `Bearer ${RESOLVED}`) {
231+
return new Response(JSON.stringify({
232+
data: [{ id: "live-via-proxy-env", context_length: 128_000 }],
233+
}), { status: 200, headers: { "content-type": "application/json" } });
234+
}
235+
return new Response("unauthorized", { status: 401 });
236+
}
237+
return originalFetch(url, init);
238+
}) as typeof fetch;
239+
240+
const config = {
241+
port: 10100,
242+
defaultProvider: PROVIDER,
243+
providers: {
244+
[PROVIDER]: {
245+
adapter: "openai-chat",
246+
authMode: "key",
247+
baseUrl: "https://proxyenv.test/v1",
248+
apiKey: `\${${ENV_KEY}}`,
249+
models: ["static-fallback"],
250+
},
251+
},
252+
} as OcxConfig;
253+
254+
const previous = process.env[ENV_KEY];
255+
delete process.env[ENV_KEY];
256+
clearModelCache(PROVIDER);
257+
try {
258+
const { fetchAllModels } = await import("../src/server/management/shared");
259+
const cliModels = await fetchAllModels(config);
260+
const cliIds = cliModels.filter(m => m.provider === PROVIDER).map(m => m.id).sort();
261+
expect(cliIds).toEqual(["static-fallback"]);
262+
expect(cliIds).not.toContain("live-via-proxy-env");
263+
264+
process.env[ENV_KEY] = RESOLVED;
265+
clearModelCache(PROVIDER);
266+
requestedAuth = undefined;
267+
268+
const { handleManagementAPI } = await import("../src/server/management-api");
269+
const modelsRes = await handleManagementAPI(
270+
new Request("http://localhost/api/models"),
271+
new URL("http://localhost/api/models"),
272+
config,
273+
);
274+
const rows = await modelsRes!.json() as Array<{
275+
namespaced?: string;
276+
contextWindow?: number;
277+
}>;
278+
expect(requestedAuth).toBe(`Bearer ${RESOLVED}`);
279+
280+
const liveRow = rows.find(r => r.namespaced === `${PROVIDER}/live-via-proxy-env`);
281+
expect(liveRow).toBeTruthy();
282+
expect(liveRow?.contextWindow).toBe(128_000);
283+
284+
const catalog = opencodeCatalogFromProxyRows(rows, config);
285+
expect(catalog.map(m => m.namespaced)).toContain(`${PROVIDER}/live-via-proxy-env`);
286+
287+
const block = buildOpencodeProviderBlockFromCatalog(10100, catalog, undefined, config);
288+
expect(block.models[`${PROVIDER}/live-via-proxy-env`]?.limit?.context).toBe(128_000);
289+
expect(block.models[`${PROVIDER}/live-via-proxy-env`]?.name).toBe("live-via-proxy-env (proxyenv)");
290+
291+
const fetched = await fetchOpencodeProxyModels(
292+
{ port: 10100, hostname: "127.0.0.1", pid: 1 },
293+
"sk-mgmt",
294+
{
295+
fetchImpl: async (url, init) => {
296+
expect(String(url)).toBe("http://127.0.0.1:10100/api/models");
297+
expect(new Headers(init?.headers).get("X-OpenCodex-API-Key")).toBe("sk-mgmt");
298+
return new Response(JSON.stringify(rows), { status: 200 });
299+
},
300+
},
301+
);
302+
expect(fetched.find(r => r.namespaced === `${PROVIDER}/live-via-proxy-env`)).toBeTruthy();
303+
} finally {
304+
globalThis.fetch = originalFetch;
305+
clearModelCache(PROVIDER);
306+
if (previous === undefined) delete process.env[ENV_KEY];
307+
else process.env[ENV_KEY] = previous;
308+
}
309+
});
310+
311+
test("opencodeCatalogFromProxyRows omits disabled and direct-mode native rows", () => {
312+
const directConfig = cfg({
313+
providers: {
314+
mock: { adapter: "openai-chat", baseUrl: "http://x/v1" },
315+
openai: {
316+
adapter: "openai-responses",
317+
baseUrl: "https://chatgpt.com/backend-api/codex",
318+
authMode: "forward",
319+
codexAccountMode: "direct",
320+
},
321+
},
322+
});
323+
const rows = [
324+
{ namespaced: "gpt-5.6-sol", native: true, disabled: false, provider: "openai", id: "gpt-5.6-sol" },
325+
{ namespaced: "gpt-5.5", native: true, disabled: true, provider: "openai", id: "gpt-5.5" },
326+
{ namespaced: "kiro/glm-5", native: false, disabled: false, provider: "kiro", id: "glm-5", displayName: "GLM-5" },
327+
];
328+
expect(opencodeCatalogFromProxyRows(rows, directConfig).map(m => m.namespaced)).toEqual(["kiro/glm-5"]);
329+
330+
const poolConfig = cfg({
331+
providers: {
332+
mock: { adapter: "openai-chat", baseUrl: "http://x/v1" },
333+
openai: {
334+
adapter: "openai-responses",
335+
baseUrl: "https://chatgpt.com/backend-api/codex",
336+
authMode: "forward",
337+
codexAccountMode: "pool",
338+
},
339+
},
340+
});
341+
expect(opencodeCatalogFromProxyRows(rows, poolConfig).map(m => m.namespaced)).toEqual([
342+
"gpt-5.6-sol",
343+
"kiro/glm-5",
344+
]);
345+
});
346+
});
347+
214348
describe("ocx opencode native slug selection", () => {
215349
test("omits native slugs in Codex Direct mode", () => {
216350
const config = cfg({

0 commit comments

Comments
 (0)