Skip to content

Commit e9468bf

Browse files
fix(providers): harden OpenCode Zen model discovery
Two correctness fixes from review: - Auth/server errors no longer seed. Discovery previously suppressed every non-OK Zen response and fell through to the seed list, so a bad API key recorded a successful discovery. Now only a 404 (missing /v1/models, per anomalyco/opencode#2901) or an OK-but-empty response seeds; 401/403/5xx surface as discovery failures. - Drop Anthropic/Claude IDs from Zen discovery. Zen serves Anthropic models on /v1/messages, which the OpenAI-completions runtime path cannot call, so an enabled claude-* model would fail at runtime. Filter them out at discovery. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 057b55b commit e9468bf

2 files changed

Lines changed: 73 additions & 4 deletions

File tree

‎src/__tests__/core/providers.test.js‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ describe("providers", () => {
415415
expect(models[0].capabilities).toMatchObject({ reasoning: true, reasoning_mode: "effort" });
416416
});
417417

418-
it("seeds curated OpenCode Zen models when discovery returns none", async () => {
418+
it("seeds curated OpenCode Zen models when discovery returns an empty list", async () => {
419419
const provider = createProvider({
420420
db,
421421
dataDir,
@@ -434,4 +434,63 @@ describe("providers", () => {
434434
expect(models.length).toBeGreaterThan(0);
435435
expect(models.some((model) => model.model_name.startsWith("gpt-5"))).toBe(true);
436436
});
437+
438+
it("seeds curated OpenCode Zen models when the discovery endpoint is missing (404)", async () => {
439+
const provider = createProvider({
440+
db,
441+
dataDir,
442+
name: "zen",
443+
provider_type: "opencode-zen",
444+
base_url: "https://opencode.ai/zen/v1",
445+
trust_public_url: true,
446+
});
447+
const models = await discoverModels({
448+
db,
449+
dataDir,
450+
providerId: provider.id,
451+
fetchImpl: async () => ({ ok: false, status: 404, json: async () => ({}) }),
452+
});
453+
expect(models.length).toBe(ZEN_SEED_MODELS.length);
454+
});
455+
456+
it("treats OpenCode Zen auth failures as discovery errors instead of seeding", async () => {
457+
const provider = createProvider({
458+
db,
459+
dataDir,
460+
name: "zen",
461+
provider_type: "opencode-zen",
462+
base_url: "https://opencode.ai/zen/v1",
463+
trust_public_url: true,
464+
});
465+
await expect(discoverModels({
466+
db,
467+
dataDir,
468+
providerId: provider.id,
469+
fetchImpl: async () => ({ ok: false, status: 401, json: async () => ({}) }),
470+
})).rejects.toThrow(/401/);
471+
expect(listModels({ db, providerId: provider.id })).toHaveLength(0);
472+
});
473+
474+
it("drops Anthropic models from OpenCode Zen discovery the runtime cannot call", async () => {
475+
const provider = createProvider({
476+
db,
477+
dataDir,
478+
name: "zen",
479+
provider_type: "opencode-zen",
480+
base_url: "https://opencode.ai/zen/v1",
481+
trust_public_url: true,
482+
});
483+
const models = await discoverModels({
484+
db,
485+
dataDir,
486+
providerId: provider.id,
487+
fetchImpl: async () => ({
488+
ok: true,
489+
json: async () => ({ data: [{ id: "gpt-5.1" }, { id: "claude-opus-4" }] }),
490+
}),
491+
});
492+
const names = models.map((model) => model.model_name);
493+
expect(names).toContain("gpt-5.1");
494+
expect(names).not.toContain("claude-opus-4");
495+
});
437496
});

‎src/core/providers.js‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,13 @@ export function inferOpenCodeZenCapabilities(model) {
605605
return inferOpenAICompatCapabilities(model);
606606
}
607607

608+
// Zen serves Anthropic/Claude models on a separate /v1/messages endpoint that the
609+
// OpenAI-completions runtime path (createVercelClient / resolveCustomPiModel) cannot
610+
// reach, so a discovered claude-* model would fail at runtime. Never expose them.
611+
function isZenUnsupportedModel(modelName) {
612+
return /claude|anthropic/i.test(String(modelName || ""));
613+
}
614+
608615
export async function discoverModels({ db, dataDir, providerId, fetchImpl = fetch, timeoutMs = 0 }) {
609616
const provider = getProvider({ db, id: providerId, includeKey: true, dataDir });
610617
if (!provider) throw new Error("provider not found");
@@ -627,14 +634,17 @@ export async function discoverModels({ db, dataDir, providerId, fetchImpl = fetc
627634
const zen = provider.provider_type === "opencode-zen";
628635
const inferCapabilities = zen ? inferOpenCodeZenCapabilities : inferOpenAICompatCapabilities;
629636
const resp = await providerFetch(modelsUrl(provider.base_url), { headers: authHeaders(provider) });
630-
// The Zen gateway may not advertise /v1/models yet (sst/opencode#2901); fall
631-
// back to a curated seed list instead of failing the whole discovery.
632-
if (!resp.ok && !zen) throw new Error(`/v1/models returned ${resp.status}`);
637+
// The Zen gateway may not expose /v1/models yet (sst/opencode#2901); fall back
638+
// to a curated seed list only for that missing/empty case. Auth and server
639+
// errors must still surface as discovery failures rather than seeding success.
640+
const zenDiscoveryMissing = zen && resp.status === 404;
641+
if (!resp.ok && !zenDiscoveryMissing) throw new Error(`/v1/models returned ${resp.status}`);
633642
if (resp.ok) {
634643
const data = await resp.json();
635644
for (const model of Array.isArray(data.data) ? data.data : Array.isArray(data) ? data : []) {
636645
const modelName = model.id || model.name;
637646
if (!modelName) continue;
647+
if (zen && isZenUnsupportedModel(modelName)) continue;
638648
discovered.push({
639649
modelName,
640650
displayName: model.name || model.id,

0 commit comments

Comments
 (0)