Files
OmniRoute/src/lib/db/featureFlags.ts
Praveen K Palaniswamy 65e81158ab fix(ollama): route models by advertised capability (#11088)
Landed with the design call resolved per the owner's pick — **option 1**: the synced store is now endpoint-agnostic (persistDiscoveredModels and managedModelImport no longer drop non-chat models at write time), and chat selectability moved to read time (auto-pool expansion in autoStrategy applies filterChatSelectableModels; the models-route projection already had its chatOnly filter). Your discovery test now passes end-to-end (3/3): /api/show capabilities persist per connection and image/embedding requests route through the advertising host.

Reconciliation notes: conflicted areas merged onto the current tip (adobe discovery import, requestedModel preflight signature, resolvedProvider fast-path coexists with the synced-route override — explicit resolution wins); carried base-red drains (#10055 memoization, #11071 test variants) dropped as already-landed; the managed-model-import exclusion test was propagated to the new contract (image/video models persist; the read filter still hides them from chat pickers — pinned by a new assertion). Full battery: 205/206 focused (the one red is a confirmed periodic-timer timing flake on the loaded devbox — 20/20 isolated), autoCombo vitest 30/30, combo suites 46/46, gates + typecheck clean.

Thank you @yourspraveen — the capability probe + routing design was right; it just needed the store contract opened up. Fixes #11087.
2026-08-23 11:45:01 -03:00

104 lines
3.1 KiB
TypeScript

/**
* db/featureFlags.ts — Feature flag DB overrides.
*
* Stores per-flag override values in the key_value table under the
* "feature_flags" namespace. When an override is present it takes precedence
* over the process environment variable of the same name.
*/
import { FEATURE_FLAG_DEFINITIONS } from "@/shared/constants/featureFlagDefinitions";
import { getDbInstance } from "./core";
import { finishModelCatalogWriteWithoutBackup } from "./models/modelCatalogWriteSignals";
const NAMESPACE = "feature_flags";
const CATALOG_RELEVANT_FEATURE_FLAGS = new Set([
"MODEL_CATALOG_INCLUDE_NAMES",
"MODELS_CATALOG_PREFIX_MODE",
"EXPOSE_CC_DISCOVERY_ALIASES",
]);
/**
* Returns all feature flag overrides as a key→value map.
*/
export function getFeatureFlagOverrides(): Record<string, string> {
const db = getDbInstance();
const rows = db
.prepare("SELECT key, value FROM key_value WHERE namespace = ?")
.all(NAMESPACE) as Array<{ key: string; value: string }>;
const result: Record<string, string> = {};
for (const row of rows) {
result[row.key] = row.value;
}
return result;
}
/**
* Returns the override value for a single flag, or undefined if no override
* is stored.
*/
export function getFeatureFlagOverride(key: string): string | undefined {
const db = getDbInstance();
const row = db
.prepare("SELECT value FROM key_value WHERE namespace = ? AND key = ?")
.get(NAMESPACE, key) as { value: string } | undefined;
return row?.value;
}
/**
* Persists (or replaces) an override for a single flag.
*/
export function setFeatureFlagOverride(key: string, value: string): void {
const definition = FEATURE_FLAG_DEFINITIONS.find((d) => d.key === key);
if (!definition) {
throw new Error(`Unknown feature flag key: ${key}`);
}
if (
definition.type === "enum" &&
definition.enumValues &&
!definition.enumValues.includes(value)
) {
throw new Error(
`Invalid value "${value}" for enum flag ${key}. Allowed: ${definition.enumValues.join(", ")}`,
);
}
const db = getDbInstance();
db.prepare("INSERT OR REPLACE INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
NAMESPACE,
key,
value,
);
if (CATALOG_RELEVANT_FEATURE_FLAGS.has(key)) {
finishModelCatalogWriteWithoutBackup();
}
}
/**
* Removes the override for a single flag, restoring env-var / default
* behaviour.
*/
export function removeFeatureFlagOverride(key: string): void {
const db = getDbInstance();
db.prepare("DELETE FROM key_value WHERE namespace = ? AND key = ?").run(NAMESPACE, key);
if (CATALOG_RELEVANT_FEATURE_FLAGS.has(key)) {
finishModelCatalogWriteWithoutBackup();
}
}
/**
* Removes all stored feature flag overrides.
*/
export function clearAllFeatureFlagOverrides(): void {
const db = getDbInstance();
const hadRelevantOverride = Boolean(
db
.prepare("SELECT 1 FROM key_value WHERE namespace = ? AND key IN (?, ?, ?) LIMIT 1")
.get(NAMESPACE, ...Array.from(CATALOG_RELEVANT_FEATURE_FLAGS)),
);
db.prepare("DELETE FROM key_value WHERE namespace = ?").run(NAMESPACE);
if (hadRelevantOverride) {
finishModelCatalogWriteWithoutBackup();
}
}