diff --git a/src/lib/db/combos.ts b/src/lib/db/combos.ts index 51cb0892b2..b24f6e7e9b 100644 --- a/src/lib/db/combos.ts +++ b/src/lib/db/combos.ts @@ -125,6 +125,24 @@ export async function getComboByName(name: string) { return normalizeStoredCombo(combo, db, [name]); } +// #4446: case-insensitive name lookup. The opencode dispatch path forwards a +// lowercased combo slug (e.g. "master-light") for a combo provisioned as +// "MASTER-LIGHT"; the default BINARY collation of getComboByName misses it. +// Used only as a fallback after the exact match fails, so it cannot change the +// resolution of any combo that already resolves today. +export async function getComboByNameInsensitive(name: string) { + const db = getDbInstance(); + const row = db + .prepare( + "SELECT data, sort_order, context_cache_protection FROM combos WHERE name = ? COLLATE NOCASE" + ) + .get(name); + const combo = parseComboRow(row); + if (!combo) return null; + const storedName = typeof combo.name === "string" ? combo.name : name; + return normalizeStoredCombo(combo, db, [storedName]); +} + export async function createCombo(data: JsonRecord) { const db = getDbInstance(); const now = new Date().toISOString(); diff --git a/src/lib/localDb.ts b/src/lib/localDb.ts index 68b1245590..1fcadda8f7 100755 --- a/src/lib/localDb.ts +++ b/src/lib/localDb.ts @@ -83,6 +83,7 @@ export { getCombos, getComboById, getComboByName, + getComboByNameInsensitive, createCombo, updateCombo, reorderCombos, diff --git a/src/sse/services/model.ts b/src/sse/services/model.ts index 651694281f..cb49cf4f4e 100644 --- a/src/sse/services/model.ts +++ b/src/sse/services/model.ts @@ -1,5 +1,12 @@ // Re-export from open-sse with localDb integration -import { getModelAliases, getComboByName, getProviderNodes, getCustomModels } from "@/lib/localDb"; +import { + getModelAliases, + getComboByName, + getComboById, + getComboByNameInsensitive, + getProviderNodes, + getCustomModels, +} from "@/lib/localDb"; import { getCachedSettings } from "@/lib/localDb"; import { getComboStepTarget } from "@/lib/combos/steps"; import { @@ -213,6 +220,22 @@ export async function getCombo(modelStr) { } } + // #4446: the opencode-plugin publishes combos as ModelV2 `id: combo.id`, and + // the OpenCode `--model` dispatch path forwards a lowercased bare slug. The + // exact, case-sensitive name match above misses both a combo addressed by its + // stored id (UUID/slug) and a lowercased display name (e.g. "master-light" for + // a combo named "MASTER-LIGHT"). These two fallbacks only run after the exact + // match fails, so they never re-route a combo that already resolves today. + combo = await getComboById(modelStr); + if (combo && combo.models && combo.models.length > 0) { + return combo; + } + + combo = await getComboByNameInsensitive(modelStr); + if (combo && combo.models && combo.models.length > 0) { + return combo; + } + return null; } diff --git a/tests/unit/combo-id-resolution-4446.test.ts b/tests/unit/combo-id-resolution-4446.test.ts new file mode 100644 index 0000000000..7a450a3602 --- /dev/null +++ b/tests/unit/combo-id-resolution-4446.test.ts @@ -0,0 +1,84 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +// #4446 — opencode-plugin publishes combos by `combo.id` and the OpenCode +// `--model` dispatch path forwards a lowercased bare slug (e.g. "master-light") +// for a combo provisioned as "MASTER-LIGHT". OmniRoute's combo resolver only +// matched by EXACT, case-sensitive `name`, so the slug resolved to nothing and +// the request fell through to provider inference → "Unable to determine +// provider for model 'master-light'". These tests lock the additive fallbacks: +// resolve a combo by its `id` and by a case-insensitive `name` match. + +const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-combo-id-4446-")); +process.env.DATA_DIR = TEST_DATA_DIR; + +const core = await import("../../src/lib/db/core.ts"); +const combosDb = await import("../../src/lib/db/combos.ts"); +const sseModelService = await import("../../src/sse/services/model.ts"); + +async function resetStorage() { + core.resetDbInstance(); + fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); + fs.mkdirSync(TEST_DATA_DIR, { recursive: true }); +} + +test.beforeEach(async () => { + await resetStorage(); +}); + +test.after(() => { + core.resetDbInstance(); + fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); +}); + +test("#4446 getComboForModel resolves a combo by a case-insensitive name (lowercased slug)", async () => { + await combosDb.createCombo({ + name: "MASTER-LIGHT", + models: [ + { provider: "groq", model: "llama-3.1-8b-instant" }, + { provider: "cerebras", model: "llama-3.3-70b" }, + ], + }); + + // The opencode `--model opencode-omniroute/master-light` path arrives as the + // bare lowercased slug "master-light". + const resolved = await sseModelService.getComboForModel("master-light"); + + assert.ok(resolved, "expected the lowercased slug to resolve to the MASTER-LIGHT combo"); + assert.equal((resolved as { name: string }).name, "MASTER-LIGHT"); + assert.ok( + Array.isArray((resolved as { models: unknown[] }).models) && + (resolved as { models: unknown[] }).models.length === 2 + ); +}); + +test("#4446 getComboForModel resolves a combo by its stored id", async () => { + const created = (await combosDb.createCombo({ + name: "Explore Combo", + models: [{ provider: "groq", model: "llama-3.1-8b-instant" }], + })) as { id: string; name: string }; + + // The opencode plugin publishes combos as ModelV2 `id: combo.id`, so the + // dispatch path can forward the combo's id rather than its display name. + const resolved = await sseModelService.getComboForModel(created.id); + + assert.ok(resolved, "expected the combo id to resolve to the combo"); + assert.equal((resolved as { name: string }).name, "Explore Combo"); +}); + +test("#4446 exact name match still wins and unknown slugs still return null", async () => { + await combosDb.createCombo({ + name: "MASTER-LIGHT", + models: [{ provider: "groq", model: "llama-3.1-8b-instant" }], + }); + + const exact = await sseModelService.getComboForModel("MASTER-LIGHT"); + assert.ok(exact, "exact name must still resolve"); + assert.equal((exact as { name: string }).name, "MASTER-LIGHT"); + + const unknown = await sseModelService.getComboForModel("no-such-combo-xyz"); + assert.equal(unknown, null, "an unknown slug must not resolve to any combo"); +});