mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-18 07:58:27 +00:00
fix(opencode): route each key-based model to the endpoint the published catalog declares
(cherry picked from commit bcef42dccf48e11893968bdcdf397add4d750f73)
This commit is contained in:
@@ -0,0 +1 @@
|
||||
- **fix(opencode):** route each key-based model to the endpoint the published catalog declares ([#16798](https://github.com/diegosouzapw/OmniRoute/pull/16798)) — thanks @maxmad64bis
|
||||
@@ -1,5 +1,9 @@
|
||||
import { generateModels, generateAliasMap, type RegistryModel } from "./providerRegistry.ts";
|
||||
import { getVertexModelTargetFormat } from "./vertexModels.ts";
|
||||
import {
|
||||
getPublishedModelNpm,
|
||||
surfaceTargetFormatForNpm,
|
||||
} from "../utils/opencodePublishedCatalog.ts";
|
||||
|
||||
// Lazy PROVIDER_MODELS: deferred until first property access to speed up startup.
|
||||
// The Proxy defers `generateModels()` from module-evaluation time to the first read.
|
||||
@@ -259,6 +263,39 @@ export function stripOpencodeModelPrefix(model: unknown): unknown {
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* The published catalog declares each model's surface per model id; an explicit
|
||||
* registry entry stays an override and wins. Unmeasured SDKs decide nothing.
|
||||
*/
|
||||
function publishedCatalogFormat(aliasOrId: string, bareModelId: string): string | null {
|
||||
return surfaceTargetFormatForNpm(getPublishedModelNpm(aliasOrId, bareModelId));
|
||||
}
|
||||
|
||||
/**
|
||||
* OpenCode's Muse Spark family is Responses-only. Resolved models can still carry
|
||||
* the raw provider id (for example "opencode/muse-spark-1.3-contributor-free")
|
||||
* even when the public alias is "oc": match the family against the final model
|
||||
* segment so both forms work.
|
||||
*/
|
||||
function museSparkTargetFormat(alias: string, bareModelId: string): string | null {
|
||||
if (!OPENCODE_MUSE_SPARK_ALIASES.has(alias)) return null;
|
||||
const modelFamilyId = bareModelId.split("/").pop() || bareModelId;
|
||||
if (!MUSE_SPARK_MODEL_PATTERN.test(modelFamilyId)) return null;
|
||||
return "openai-responses";
|
||||
}
|
||||
|
||||
/**
|
||||
* Effort suffixes (gpt-6-astra-high, gpt-5.6-sol-xhigh) are not separate
|
||||
* catalog rows on the public OpenAI provider. They must keep the base
|
||||
* model's endpoint, or tools+reasoning land on /v1/chat/completions and
|
||||
* OpenAI returns a 400 that the Responses API would have accepted.
|
||||
*/
|
||||
function effortBaseTargetFormat(alias: string, bareModelId: string): string | null {
|
||||
const effortStripped = bareModelId.replace(/-(?:ultra|max|xhigh|high|medium|low|none)$/i, "");
|
||||
if (effortStripped === bareModelId) return null;
|
||||
return PROVIDER_MODELS[alias]?.find((m) => m.id === effortStripped)?.targetFormat ?? null;
|
||||
}
|
||||
|
||||
export function getModelTargetFormat(aliasOrId: string, modelId: string): string | null {
|
||||
// Accept either the public alias ("cmd") or the raw provider id ("command-code"),
|
||||
// mirroring getProviderModels (same pattern as #2798/#3870).
|
||||
@@ -269,22 +306,12 @@ export function getModelTargetFormat(aliasOrId: string, modelId: string): string
|
||||
const bareModelId = prefix ? modelId.slice(prefix.length) : modelId;
|
||||
const found = PROVIDER_MODELS[alias]?.find((m) => m.id === bareModelId);
|
||||
if (found?.targetFormat) return found.targetFormat;
|
||||
// Resolved models can still carry an extra provider segment (for example
|
||||
// "opencode/muse-spark-1.3-contributor-free" under opencode-zen). Match the
|
||||
// family against the final model segment so both forms work.
|
||||
const modelFamilyId = bareModelId.split("/").pop() || bareModelId;
|
||||
if (OPENCODE_MUSE_SPARK_ALIASES.has(alias) && MUSE_SPARK_MODEL_PATTERN.test(modelFamilyId)) {
|
||||
return "openai-responses";
|
||||
}
|
||||
// Effort suffixes (gpt-6-astra-high, gpt-5.6-sol-xhigh) are not separate
|
||||
// catalog rows on the public OpenAI provider. They must keep the base
|
||||
// model's endpoint, or tools+reasoning land on /v1/chat/completions and
|
||||
// OpenAI returns a 400 that the Responses API would have accepted.
|
||||
const effortStripped = bareModelId.replace(/-(?:ultra|max|xhigh|high|medium|low|none)$/i, "");
|
||||
if (effortStripped !== bareModelId) {
|
||||
const base = PROVIDER_MODELS[alias]?.find((m) => m.id === effortStripped);
|
||||
if (base?.targetFormat) return base.targetFormat;
|
||||
}
|
||||
const catalogFormat = publishedCatalogFormat(aliasOrId, bareModelId);
|
||||
if (catalogFormat) return catalogFormat;
|
||||
const sparkFormat = museSparkTargetFormat(alias, bareModelId);
|
||||
if (sparkFormat) return sparkFormat;
|
||||
const effortFormat = effortBaseTargetFormat(alias, bareModelId);
|
||||
if (effortFormat) return effortFormat;
|
||||
// #5842: OpenAI "*-pro" reasoning models (o1-pro, gpt-5.x-pro) are only served by
|
||||
// the native /v1/responses endpoint — /v1/chat/completions 404s ("only supported
|
||||
// in v1/responses"). Curated catalog entries are tagged explicitly; this heuristic
|
||||
|
||||
126
open-sse/utils/opencodePublishedCatalog.ts
Normal file
126
open-sse/utils/opencodePublishedCatalog.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* Model surfaces the published OpenCode catalog declares (`provider.npm`), kept apart from
|
||||
* the endpoint reader so `open-sse/config/providerModels.ts` can read them from the client
|
||||
* bundle: that reader reaches `node:fs`, `node:os` and `node:path` for its persisted cache.
|
||||
* Same split as #10692 (data apart from endpoint-only logic). Nothing here networks, reads a
|
||||
* file, or imports a `node:` builtin.
|
||||
*
|
||||
* The map is filled by the endpoint module from the same `models.opencode.ai/api.json` read
|
||||
* that resolves the base URL, and rewritten as a block on every catalog read: one entry per
|
||||
* published model, no per-request growth, and a model the catalog moves to another SDK
|
||||
* shows its new surface on the next refresh.
|
||||
*/
|
||||
|
||||
/** Catalog sections read for the model surface, mapped from the normalized alias. */
|
||||
const NPM_CATALOG_KEY_BY_ALIAS: Record<string, string> = {
|
||||
oc: "opencode",
|
||||
"opencode-zen": "opencode",
|
||||
"opencode-go": "opencode-go",
|
||||
};
|
||||
|
||||
/** Raw provider ids map to the same sections as their public alias. */
|
||||
const NPM_ALIAS_BY_PROVIDER_ID: Record<string, string> = {
|
||||
opencode: "oc",
|
||||
"opencode-zen": "opencode-zen",
|
||||
"opencode-go": "opencode-go",
|
||||
};
|
||||
|
||||
const OPENCODE_MODEL_PREFIXES = ["opencode/", "oc/", "opencode-zen/", "opencode-go/"] as const;
|
||||
|
||||
/** Raw `provider.npm` by `${catalogKey} ${modelId}`; rewritten as a block per catalog read. */
|
||||
const npmByModel = new Map<string, string>();
|
||||
|
||||
/** Set by the endpoint module: the coalesced catalog refresh that keeps this map current. */
|
||||
let requestCatalogRefresh: (() => void) | null = null;
|
||||
|
||||
/**
|
||||
* Registered once by the endpoint module, which owns the catalog fetch and the refresh
|
||||
* rhythm. A process that never loads that module (the client bundle) reads an empty map
|
||||
* and falls back to the registry.
|
||||
*/
|
||||
export function setPublishedCatalogRefreshTrigger(trigger: () => void): void {
|
||||
requestCatalogRefresh = trigger;
|
||||
}
|
||||
|
||||
/** Raw `provider.npm` values by `${catalogKey} ${modelId}`, from one catalog read. */
|
||||
export function readPublishedNpmByModel(catalog: unknown): Map<string, string> {
|
||||
const map = new Map<string, string>();
|
||||
if (!catalog || typeof catalog !== "object") return map;
|
||||
const sections = catalog as Record<string, { models?: Record<string, unknown> } | undefined>;
|
||||
for (const sectionKey of new Set(Object.values(NPM_CATALOG_KEY_BY_ALIAS))) {
|
||||
const models = sections[sectionKey]?.models;
|
||||
if (!models || typeof models !== "object") continue;
|
||||
for (const [modelId, entry] of Object.entries(models)) {
|
||||
const npm = (entry as { provider?: { npm?: unknown } } | null | undefined)?.provider?.npm;
|
||||
if (typeof npm === "string" && npm.trim()) map.set(`${sectionKey} ${modelId}`, npm);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/** Replace every entry with one catalog read's result (the endpoint module owns the rhythm). */
|
||||
export function replacePublishedCatalogNpm(next: Map<string, string>): void {
|
||||
npmByModel.clear();
|
||||
for (const [key, npm] of next) npmByModel.set(key, npm);
|
||||
}
|
||||
|
||||
/** Drop every entry: the persisted-cache load and the test reset both start from empty. */
|
||||
export function clearPublishedCatalogNpm(): void {
|
||||
npmByModel.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync hot path: the raw `provider.npm` the catalog publishes for this model, or
|
||||
* `undefined` when the catalog is silent. Never networks; the registered trigger starts one
|
||||
* coalesced background refresh when the map is stale or cold.
|
||||
*/
|
||||
export function getPublishedModelNpm(
|
||||
providerId: string | null | undefined,
|
||||
modelId: string | null | undefined
|
||||
): string | undefined {
|
||||
const catalogKey = normalizeCatalogKey(providerId);
|
||||
const bareModel = stripCatalogModelPrefix(modelId);
|
||||
if (!catalogKey || !bareModel) return undefined;
|
||||
requestCatalogRefresh?.();
|
||||
return (
|
||||
npmByModel.get(`${catalogKey} ${bareModel}`) ??
|
||||
npmByModel.get(`${catalogKey} ${stripEffortSuffix(bareModel)}`)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The target format a published SDK rides, or `null` when the catalog decides nothing.
|
||||
* Only the measured SDKs map: everything else falls back to the registry heuristics.
|
||||
*/
|
||||
export function surfaceTargetFormatForNpm(npm: string | null | undefined): string | null {
|
||||
if (npm === "@ai-sdk/openai") return "openai-responses";
|
||||
if (npm === "@ai-sdk/anthropic") return "claude";
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test seam. Seeds the raw catalog surface map from a catalog-shaped object, without
|
||||
* networking: `catalog[section].models[modelId].provider.npm` (non-empty strings kept).
|
||||
*/
|
||||
export function configurePublishedCatalogForTests(catalog: unknown): void {
|
||||
replacePublishedCatalogNpm(readPublishedNpmByModel(catalog));
|
||||
}
|
||||
|
||||
/** Alias, raw id or prefixed id → the catalog section key, or `undefined` outside opencode. */
|
||||
function normalizeCatalogKey(providerId: string | null | undefined): string | undefined {
|
||||
if (typeof providerId !== "string" || !providerId) return undefined;
|
||||
const alias = NPM_ALIAS_BY_PROVIDER_ID[providerId] ?? providerId;
|
||||
return NPM_CATALOG_KEY_BY_ALIAS[alias];
|
||||
}
|
||||
|
||||
function stripCatalogModelPrefix(modelId: string | null | undefined): string | undefined {
|
||||
if (typeof modelId !== "string" || !modelId) return undefined;
|
||||
for (const prefix of OPENCODE_MODEL_PREFIXES) {
|
||||
if (modelId.startsWith(prefix)) return modelId.slice(prefix.length);
|
||||
}
|
||||
return modelId;
|
||||
}
|
||||
|
||||
function stripEffortSuffix(modelId: string): string {
|
||||
return modelId.replace(/-(?:ultra|max|xhigh|high|medium|low|none)$/i, "");
|
||||
}
|
||||
120
tests/unit/opencode-model-surface-from-catalog.test.ts
Normal file
120
tests/unit/opencode-model-surface-from-catalog.test.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
/**
|
||||
* Each key-based opencode model follows the request surface the published catalog
|
||||
* declares for it (`models.opencode.ai/api.json`, `provider.npm`), falling back to
|
||||
* the registry entry when the catalog is silent. No test touches the network: the
|
||||
* catalog map is seeded through the module's seam, and every entry is read back
|
||||
* through the production lookup (`getPublishedModelNpm`).
|
||||
*/
|
||||
import assert from "node:assert/strict";
|
||||
import { afterEach, beforeEach, test } from "node:test";
|
||||
|
||||
import { getModelTargetFormat } from "../../open-sse/config/providerModels.ts";
|
||||
import { resolveOpencodeTargetFormat } from "../../open-sse/executors/opencode.ts";
|
||||
import {
|
||||
configurePublishedCatalogForTests,
|
||||
getPublishedModelNpm,
|
||||
surfaceTargetFormatForNpm,
|
||||
} from "../../open-sse/utils/opencodePublishedCatalog.ts";
|
||||
import { resetPublishedEndpointCache } from "../../open-sse/utils/opencodePublishedEndpoint.ts";
|
||||
|
||||
function catalogWithEntries() {
|
||||
return {
|
||||
opencode: {
|
||||
id: "opencode",
|
||||
api: "https://opencode.ai/inference/v1",
|
||||
models: {
|
||||
"respond-model": { provider: { npm: "@ai-sdk/openai" } },
|
||||
"message-model": { provider: { npm: "@ai-sdk/anthropic" } },
|
||||
"google-model": { provider: { npm: "@ai-sdk/google" } },
|
||||
"no-npm-model": { provider: {} },
|
||||
},
|
||||
},
|
||||
"opencode-go": {
|
||||
id: "opencode-go",
|
||||
api: "https://opencode.ai/inference/go/v1",
|
||||
models: {
|
||||
"respond-model": { provider: { npm: "@ai-sdk/openai" } },
|
||||
"message-model": { provider: { npm: "@ai-sdk/anthropic" } },
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
resetPublishedEndpointCache();
|
||||
configurePublishedCatalogForTests(catalogWithEntries());
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
resetPublishedEndpointCache();
|
||||
});
|
||||
|
||||
test("a model published with @ai-sdk/openai resolves to /responses", () => {
|
||||
assert.equal(getPublishedModelNpm("opencode-zen", "respond-model"), "@ai-sdk/openai");
|
||||
assert.equal(surfaceTargetFormatForNpm("@ai-sdk/openai"), "openai-responses");
|
||||
assert.equal(resolveOpencodeTargetFormat("opencode-zen", "respond-model"), "openai-responses");
|
||||
});
|
||||
|
||||
test("a model published with @ai-sdk/anthropic resolves to /messages", () => {
|
||||
assert.equal(getPublishedModelNpm("opencode-zen", "message-model"), "@ai-sdk/anthropic");
|
||||
assert.equal(surfaceTargetFormatForNpm("@ai-sdk/anthropic"), "claude");
|
||||
assert.equal(resolveOpencodeTargetFormat("opencode-zen", "message-model"), "claude");
|
||||
});
|
||||
|
||||
test("the raw provider id and the public alias read the same catalog section", () => {
|
||||
// `opencode` (raw id) normalizes to the `oc` alias and reads the `opencode` section.
|
||||
assert.equal(getPublishedModelNpm("opencode", "respond-model"), "@ai-sdk/openai");
|
||||
assert.equal(getPublishedModelNpm("oc", "respond-model"), "@ai-sdk/openai");
|
||||
assert.equal(resolveOpencodeTargetFormat("opencode", "respond-model"), "openai-responses");
|
||||
});
|
||||
|
||||
test("a model published without npm falls back to the registry", () => {
|
||||
assert.equal(getPublishedModelNpm("opencode-zen", "no-npm-model"), undefined);
|
||||
assert.equal(surfaceTargetFormatForNpm(undefined), null);
|
||||
// No registry entry and no catalog surface: chat completions.
|
||||
assert.equal(resolveOpencodeTargetFormat("opencode-zen", "no-npm-model"), "openai");
|
||||
});
|
||||
|
||||
test("a missing catalog keeps the registry target format", () => {
|
||||
resetPublishedEndpointCache();
|
||||
configurePublishedCatalogForTests(null);
|
||||
// Registry pins this model to Responses; the silent catalog must not move it.
|
||||
assert.equal(getPublishedModelNpm("opencode-go", "deepseek-v4-pro"), undefined);
|
||||
assert.equal(resolveOpencodeTargetFormat("opencode-go", "deepseek-v4-pro"), "openai-responses");
|
||||
});
|
||||
|
||||
test("a model unknown to the catalog keeps the registry target format", () => {
|
||||
assert.equal(getPublishedModelNpm("opencode-go", "deepseek-v4-pro"), undefined);
|
||||
assert.equal(resolveOpencodeTargetFormat("opencode-go", "deepseek-v4-pro"), "openai-responses");
|
||||
});
|
||||
|
||||
test("a published SDK outside the measured set keeps the registry target format", () => {
|
||||
assert.equal(getPublishedModelNpm("opencode-zen", "google-model"), "@ai-sdk/google");
|
||||
assert.equal(surfaceTargetFormatForNpm("@ai-sdk/google"), null);
|
||||
assert.equal(surfaceTargetFormatForNpm("@ai-sdk/mistral"), null);
|
||||
assert.equal(resolveOpencodeTargetFormat("opencode-zen", "google-model"), "openai");
|
||||
});
|
||||
|
||||
test("the registry override wins over the catalog", () => {
|
||||
// Registry pins this model to Messages; the catalog must not move it.
|
||||
assert.equal(resolveOpencodeTargetFormat("opencode-go", "qwen3.8-max"), "claude");
|
||||
assert.equal(getModelTargetFormat("opencode-go", "qwen3.8-max"), "claude");
|
||||
});
|
||||
|
||||
test("non-opencode providers never read the catalog", () => {
|
||||
assert.equal(getPublishedModelNpm("openai", "respond-model"), undefined);
|
||||
assert.equal(getPublishedModelNpm("github", "respond-model"), undefined);
|
||||
});
|
||||
|
||||
test("re-seeding the catalog drops every entry the new read does not carry", () => {
|
||||
// N lookups against a seeded map add nothing; the next catalog read rewrites the map as
|
||||
// a block, so an id the new read omits is gone instead of piling up per request.
|
||||
for (let index = 0; index < 5; index += 1) {
|
||||
assert.equal(getPublishedModelNpm("opencode-zen", "respond-model"), "@ai-sdk/openai");
|
||||
}
|
||||
configurePublishedCatalogForTests({
|
||||
opencode: { models: { "only-model": { provider: { npm: "@ai-sdk/anthropic" } } } },
|
||||
});
|
||||
assert.equal(getPublishedModelNpm("opencode-zen", "respond-model"), undefined);
|
||||
assert.equal(getPublishedModelNpm("opencode-zen", "only-model"), "@ai-sdk/anthropic");
|
||||
});
|
||||
Reference in New Issue
Block a user