Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions @omniroute/opencode-plugin-v2/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @omniroute/opencode-plugin-v2

OpenCode v2 plugin (`define({ id, setup })`, Promise API) that publishes the live OmniRoute catalog — models from `/v1/models`, combos from `/api/combos` (least-common-denominator join), auto-combos from `/api/combos/auto`, enrichment (names + pricing), and usable-provider filtering — into the v2 `catalog.transform`, with `key` + `env` auth via `integration.transform`.
OpenCode v2 plugin (`define({ id, setup })`, Promise API) that publishes the live OmniRoute catalog — models from `/v1/models`, combos from `/api/combos` (least-common-denominator join), enrichment (names + pricing), and usable-provider filtering — into the v2 `catalog.transform`, with `key` + `env` auth via `integration.transform`.

Companion to `@omniroute/opencode-plugin` (OpenCode v1, same repo). The two packages are independent: this one carries its own catalog-mapping logic and the v1 plugin is left untouched.

Expand Down Expand Up @@ -106,9 +106,9 @@ catalog is never a mystery.
| `apiKey` | connected credential, then `OMNIROUTE_API_KEY` | Chat key for `/v1/*` — see [Credentials](#credentials) |
| `managementReadToken` | option, then `OMNIROUTE_MANAGEMENT_API_KEY`, then `apiKey` | Management key for `/api/*` (combos, providers, enrichment) — usually **not** the same key |
| `displayName` | `"OmniRoute"` | Provider display name |
| `timeoutMs` | `10000` | Per-endpoint fetch timeout (auto-combos use 5s) |
| `timeoutMs` | `10000` | Per-endpoint fetch timeout |
| `modelCacheTtlMs` | `300000` | Catalog cache TTL; disk snapshot warms cold starts |
| `timeouts` | per-endpoint override | `{ models, combos, autoCombos, enrichment }` in ms; falls back to `timeoutMs` |
| `timeouts` | per-endpoint override | `{ models, combos, enrichment }` in ms; falls back to `timeoutMs` (`autoCombos` still parses but is ignored — the virtual-entries route is no longer requested) |
| `enrichment` | `true` | Fetch names + pricing (`/api/pricing*`, `/api/free-tier/summary`) |
| `providerTag` | `true` | Prefix a display name with the upstream provider it routes to |
| `geminiSanitization` | `true` | Strip `$schema`/`additionalProperties` from tool schemas sent to Gemini models (`$ref` tools are forwarded untouched) |
Expand Down
18 changes: 9 additions & 9 deletions @omniroute/opencode-plugin-v2/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
OmniRouteEnrichmentEntry,
OmniRouteEnrichmentMap,
OmniRouteProviderConnection,
OmniRouteRawAutoCombo,
OmniRouteRawCombo,
OmniRouteRawModelEntry,
} from "./shared/index.js";
Expand All @@ -25,7 +24,6 @@ export const UNREACHABLE_COOLDOWN_MS = 15_000 as const;
export interface CatalogSnapshot {
models: OmniRouteRawModelEntry[];
combos: OmniRouteRawCombo[];
autoCombos: OmniRouteRawAutoCombo[];
providers?: OmniRouteProviderConnection[];
enrichment?: OmniRouteEnrichmentMap;
fetchedAt: number;
Expand Down Expand Up @@ -61,7 +59,6 @@ interface DiskSnapshotV2 {
identityFingerprint: string;
models: OmniRouteRawModelEntry[];
combos: OmniRouteRawCombo[];
autoCombos?: OmniRouteRawAutoCombo[];
providers?: OmniRouteProviderConnection[];
/**
* Display names, provider labels, pricing and free-tier budgets, as
Expand Down Expand Up @@ -135,7 +132,12 @@ export async function readDiskSnapshot(
): Promise<CatalogSnapshot | undefined> {
try {
const body = await readFile(diskSnapshotPath(providerId), "utf8");
const parsed = JSON.parse(body) as Partial<DiskSnapshotV2>;
// A retired snapshot field survives JSON.parse as an unknown key and is
// never read back, so legacy snapshots load with it ignored.
const parsed = JSON.parse(body) as Partial<DiskSnapshotV2> & {
autoCombos?: unknown;
};
void (parsed as { autoCombos?: unknown }).autoCombos;
if (
!parsed ||
typeof parsed.v !== "number" ||
Expand All @@ -157,15 +159,14 @@ export async function readDiskSnapshot(
(entry) => !isStaleSnapshotModel(entry)
);
if (stale > 0) {
logger?.warn(`[omniroute-v2] dropping ${stale} stale snapshot entries with an unusable api block`);
logger?.warn(
`[omniroute-v2] dropping ${stale} stale snapshot entries with an unusable api block`
);
}
if (models.length === 0) return undefined;
return {
models,
combos: parsed.combos as OmniRouteRawCombo[],
autoCombos: Array.isArray(parsed.autoCombos)
? (parsed.autoCombos as OmniRouteRawAutoCombo[])
: [],
providers: Array.isArray(parsed.providers)
? (parsed.providers as OmniRouteProviderConnection[])
: [],
Expand Down Expand Up @@ -202,7 +203,6 @@ export async function writeDiskSnapshot(
identityFingerprint,
models: snapshot.models,
combos: snapshot.combos,
autoCombos: snapshot.autoCombos,
providers: snapshot.providers ?? [],
enrichment: snapshot.enrichment ? [...snapshot.enrichment.entries()] : undefined,
writtenAt: Date.now(),
Expand Down
110 changes: 33 additions & 77 deletions @omniroute/opencode-plugin-v2/src/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
buildProviderResolve,
compileProviderFilter,
filterAllUnknown,
passesProviderAutoCombo,
passesProviderCombo,
passesProviderFilter,
warnNoVocabulary,
Expand All @@ -17,14 +16,12 @@ import {
type ApiFormatV2,
type LogLevel,
type Logger,
type OmniRouteAutoCombosFetcher,
type OmniRouteCombosFetcher,
type OmniRouteEnrichmentFetcher,
type OmniRouteEnrichmentMap,
type OmniRouteModelsFetcher,
type OmniRouteProviderConnection,
type OmniRouteProvidersFetcher,
type OmniRouteRawAutoCombo,
type OmniRouteRawCombo,
type OmniRouteRawModelEntry,
applyEnrichment,
Expand All @@ -37,22 +34,19 @@ import {
isUsableCombo,
isUsableRawModelId,
lookupEnrichment,
mapAutoComboToModelV2,
mapComboToModelV2,
mapRawModelToModelV2,
usableProviderAliasSet,
} from "./shared/index.js";

export type ModelsFetcher = OmniRouteModelsFetcher;
export type CombosFetcher = OmniRouteCombosFetcher;
export type AutoCombosFetcher = OmniRouteAutoCombosFetcher;
export type ProvidersFetcher = OmniRouteProvidersFetcher;
export type EnrichmentFetcher = OmniRouteEnrichmentFetcher;

export interface EndpointTimeouts {
models?: number;
combos?: number;
autoCombos?: number;
enrichment?: number;
}

Expand Down Expand Up @@ -87,12 +81,10 @@ export interface ResolvedOptions {
export interface CatalogFetchers {
fetcher?: ModelsFetcher;
combosFetcher?: CombosFetcher;
autoCombosFetcher?: AutoCombosFetcher;
providersFetcher?: ProvidersFetcher;
enrichmentFetcher?: EnrichmentFetcher;
models?: ModelsFetcher;
combos?: CombosFetcher;
autoCombos?: AutoCombosFetcher;
providers?: ProvidersFetcher;
enrichment?: EnrichmentFetcher;
/**
Expand All @@ -106,6 +98,29 @@ export interface CatalogFetchers {
export type StableModelInfo = Model.Info;
export type StableProviderInfo = Provider.Info;

/**
* Every path the refresh flow may request. The removed virtual-entries route
* is absent by construction: no fetcher may build it, and the route-guard
* test below asserts no stubbed call ever matches it.
*/
export const DECLARED_REFRESH_ROUTES = [
"/v1/models",
"/api/combos",
"/api/providers",
"/api/pricing/models",
"/api/pricing",
"/api/free-tier/summary",
] as const;

/**
* Whether a request pathname belongs to the declared refresh set. Exact
* equality on purpose: a prefix check would re-admit the removed route via
* its `/api/combos/` prefix.
*/
export function isDeclaredRefreshPath(pathname: string): boolean {
return (DECLARED_REFRESH_ROUTES as readonly string[]).includes(pathname);
}

/**
* Structural mirror of the stable `ctx.provider.transform` editor, used as
* the parameter type where the payload is handed to the host (and in tests
Expand Down Expand Up @@ -182,9 +197,7 @@ export function legacyToStable(
capabilities: { tools: m.capabilities.toolcall, input, output },
variants,
time: { released: Number.isNaN(parsed) ? 0 : parsed },
cost: [
{ input: m.cost.input, output: m.cost.output, cache: { ...m.cost.cache } },
],
cost: [{ input: m.cost.input, output: m.cost.output, cache: { ...m.cost.cache } }],
status: m.status,
enabled: true,
limit: { ...m.limit },
Expand Down Expand Up @@ -275,7 +288,6 @@ function legacyToInfo(providerID: string, modelID: string, m: LegacyModel): Stab
export interface PublishCounts {
models: number;
combos: number;
autoCombos: number;
}

export interface ModelListFilter {
Expand Down Expand Up @@ -469,7 +481,7 @@ interface PublishContext {
providerAllUnknown: boolean;
combosFetcher: CatalogFetchers["combos"] | undefined;
combosTimeout: number;
/** Shared with the auto-combos pass: one collision warning per key, per run. */
/** Shared with the combos pass: one collision warning per key, per run. */
warnedCombos: Set<string>;
cacheKey: string;
}
Expand Down Expand Up @@ -671,7 +683,7 @@ function synthesizeNestedMember(name: string, nested: LegacyModel): OmniRouteRaw
}

/**
* Collect the full catalog (models + combos + auto-combos) as legacy entries
* Collect the full catalog (models + combos) as legacy entries
* keyed `providerId/bareId`, then project them onto the stable contract in
* `buildProviderPayload`. Collect-then-project keeps every fetch/filter/LCD
* behavior identical to the beta path while the only host touchpoint is the
Expand All @@ -690,19 +702,14 @@ export async function collectCatalog(
const log = opts.logger ?? createLogger(opts.startupDebug ? "debug" : (opts.logLevel ?? "warn"));
const modelsTimeout = opts.timeouts?.models ?? opts.timeoutMs;
const combosTimeout = opts.timeouts?.combos ?? opts.timeoutMs;
// v1 parity keeps the 5s auto-combos budget when no per-endpoint value is
// set (P2 resolves it in index.ts; direct publishCatalog callers may only
// pass timeoutMs).
const autoCombosTimeout = opts.timeouts?.autoCombos ?? 5_000;

const modelsFetcher = fetchers?.fetcher ?? fetchers?.models;
const combosFetcher = fetchers?.combosFetcher ?? fetchers?.combos;
const autoCombosFetcher = fetchers?.autoCombosFetcher ?? fetchers?.autoCombos;
const providersFetcher = fetchers?.providersFetcher ?? fetchers?.providers;

const empty: CollectedCatalog = {
entries: new Map(),
counts: { models: 0, combos: 0, autoCombos: 0 },
counts: { models: 0, combos: 0 },
};
let rawModels: OmniRouteRawModelEntry[];
try {
Expand Down Expand Up @@ -814,7 +821,7 @@ export async function collectCatalog(
cacheKey,
});
if (comboResult === undefined)
return { entries: collected, counts: { models: modelCount, combos: 0, autoCombos: 0 } };
return { entries: collected, counts: { models: modelCount, combos: 0 } };
const comboCount = comboResult.published;
providerDroppedCount += comboResult.providerDropped;

Expand All @@ -823,67 +830,13 @@ export async function collectCatalog(
// Re-publishing under "opencode-"+X here is FORBIDDEN: a double
// publish would double chat entries in the picker.

// Auto combos: virtual server-side entries from /api/combos/auto, keyed
// "auto" / "auto/<variant>" (v1 parity). Fail-open: a fetcher throw keeps
// models + combos and only warns - old gateways may not serve the
// endpoint at all (the default fetcher maps 404 to [] itself).
let rawAutoCombos: OmniRouteRawAutoCombo[];
try {
rawAutoCombos = autoCombosFetcher
? await autoCombosFetcher(
opts.baseURL,
opts.managementReadToken ?? opts.apiKey,
autoCombosTimeout
)
: [];
} catch (err) {
log.warn(
`[omniroute-v2] auto combos fetch failed, falling back to models+combos catalog: ${err instanceof Error ? err.message : String(err)}`
);
return { entries: collected, counts: { models: modelCount, combos: comboCount, autoCombos: 0 } };
}

let autoComboCount = 0;
for (const autoCombo of rawAutoCombos) {
if (!autoCombo || !autoCombo.id) continue;
if (autoCombo.isHidden === true) continue;
// Auto combos are catalog entries like any other: an id a user asked to
// hide must stay hidden, and an allowlist that excludes it must exclude
// it. They used to skip both filters entirely.
if (!passesModelAllowlist(autoCombo.id, visibleFilter, hiddenFilter)) continue;
if (usable && !isUsableRawModelId(autoCombo.id, usable)) continue;
if (!passesProviderAutoCombo(autoCombo, providerFilter, providerResolve, providerAllUnknown)) {
providerDroppedCount += 1;
continue;
}
const mapped = mapAutoComboToModelV2(autoCombo, X, opts.baseURL, opts.apiFormat);
applyEnrichment(mapped, lookupEnrichment(autoCombo.id, enrichment, canonicalToAlias), {
isCombo: true,
isAutoCombo: true,
});
const key = X + "/" + mapped.id;
if (publishedKeys.has(key)) {
const dedupeKey = `${cacheKey}::${key}`;
if (!warnedCombos.has(dedupeKey)) {
warnedCombos.add(dedupeKey);
log.warn(
`[omniroute-v2] auto combo key "${key}" collides with a model id; auto combo wins.`
);
}
}
collected.set(key, mapped);
publishedKeys.add(key);
publishedModelIds.set(key, mapped.id);
autoComboCount += 1;
}

if (providerFilter && collected.size === 0 && providerDroppedCount > 0) {
warnProviderMatchedNothing(providerFilter, warnedCombos, log);
}

return {
entries: collected,
counts: { models: modelCount, combos: comboCount, autoCombos: autoComboCount },
counts: { models: modelCount, combos: comboCount },
};
}

Expand Down Expand Up @@ -961,7 +914,10 @@ export async function publishCatalog(
const settings = (info.settings ?? {}) as Record<string, unknown>;
const npm = String(info.package ?? "").replace("@opencode/ai/providers/", "@ai-sdk/");
p["api"] = { type: "aisdk", package: npm, url: settings["baseURL"] };
p["request"] = { headers: (info.headers ?? {}) as Record<string, string>, body: (info.body ?? {}) as Record<string, unknown> };
p["request"] = {
headers: (info.headers ?? {}) as Record<string, string>,
body: (info.body ?? {}) as Record<string, unknown>,
};
});
for (const m of collected.entries.keys()) {
const slash = m.indexOf("/");
Expand Down
Loading
Loading