chore: merge release/v3.8.0 into PR branch

This commit is contained in:
diegosouzapw
2026-05-18 15:48:43 -03:00
28 changed files with 3014 additions and 339 deletions

View File

@@ -0,0 +1,61 @@
name: opencode-provider CI
on:
push:
branches: [main, release/v3.8.0]
paths:
- "@omniroute/opencode-provider/**"
pull_request:
branches: [main, release/v3.8.0]
paths:
- "@omniroute/opencode-provider/**"
types: [opened, synchronize, reopened, ready_for_review]
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
defaults:
run:
working-directory: "@omniroute/opencode-provider"
jobs:
test:
name: Test (Node ${{ matrix.node }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node: ["20", "22", "24"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: npm
cache-dependency-path: "@omniroute/opencode-provider/package-lock.json"
- run: npm ci
- run: npm test
build:
name: Build
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "20"
cache: npm
cache-dependency-path: "@omniroute/opencode-provider/package-lock.json"
- run: npm ci
- run: npm run build
- uses: actions/upload-artifact@v4
with:
name: opencode-provider-dist
path: "@omniroute/opencode-provider/dist"
retention-days: 7

View File

@@ -9,6 +9,7 @@
"version": "0.1.0",
"license": "MIT",
"devDependencies": {
"@types/node": "^20.19.41",
"tsup": "^8.5.0",
"tsx": "^4.20.0",
"typescript": "^5.9.0"
@@ -590,9 +591,6 @@
"arm"
],
"dev": true,
"libc": [
"glibc"
],
"license": "MIT",
"optional": true,
"os": [
@@ -607,9 +605,6 @@
"arm"
],
"dev": true,
"libc": [
"musl"
],
"license": "MIT",
"optional": true,
"os": [
@@ -624,9 +619,6 @@
"arm64"
],
"dev": true,
"libc": [
"glibc"
],
"license": "MIT",
"optional": true,
"os": [
@@ -641,9 +633,6 @@
"arm64"
],
"dev": true,
"libc": [
"musl"
],
"license": "MIT",
"optional": true,
"os": [
@@ -658,9 +647,6 @@
"loong64"
],
"dev": true,
"libc": [
"glibc"
],
"license": "MIT",
"optional": true,
"os": [
@@ -675,9 +661,6 @@
"loong64"
],
"dev": true,
"libc": [
"musl"
],
"license": "MIT",
"optional": true,
"os": [
@@ -692,9 +675,6 @@
"ppc64"
],
"dev": true,
"libc": [
"glibc"
],
"license": "MIT",
"optional": true,
"os": [
@@ -709,9 +689,6 @@
"ppc64"
],
"dev": true,
"libc": [
"musl"
],
"license": "MIT",
"optional": true,
"os": [
@@ -726,9 +703,6 @@
"riscv64"
],
"dev": true,
"libc": [
"glibc"
],
"license": "MIT",
"optional": true,
"os": [
@@ -743,9 +717,6 @@
"riscv64"
],
"dev": true,
"libc": [
"musl"
],
"license": "MIT",
"optional": true,
"os": [
@@ -760,9 +731,6 @@
"s390x"
],
"dev": true,
"libc": [
"glibc"
],
"license": "MIT",
"optional": true,
"os": [
@@ -777,9 +745,6 @@
"x64"
],
"dev": true,
"libc": [
"glibc"
],
"license": "MIT",
"optional": true,
"os": [
@@ -794,9 +759,6 @@
"x64"
],
"dev": true,
"libc": [
"musl"
],
"license": "MIT",
"optional": true,
"os": [
@@ -894,6 +856,16 @@
"dev": true,
"license": "MIT"
},
"node_modules/@types/node": {
"version": "20.19.41",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.41.tgz",
"integrity": "sha512-ECymXOukMnOoVkC2bb1Vc/w/836DXncOg5m8Xj1RH7xSHZJWNYY6Zh7EH477vcnD5egKNNfy2RpNOmuChhFPgQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": "~6.21.0"
}
},
"node_modules/acorn": {
"version": "8.16.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.16.0.tgz",
@@ -2014,6 +1986,13 @@
"integrity": "sha512-JFNbkD1Svwe0KvGi8GOeLcP4kAWQ609twvCdcHxq1oSL8svv39ZuSvajcD8B+5D0eL4+s1Is2D/O6KN3qcTeRA==",
"dev": true,
"license": "MIT"
},
"node_modules/undici-types": {
"version": "6.21.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
"integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
"dev": true,
"license": "MIT"
}
}
}

View File

@@ -52,6 +52,7 @@
"access": "public"
},
"devDependencies": {
"@types/node": "^20.19.41",
"tsup": "^8.5.0",
"tsx": "^4.20.0",
"typescript": "^5.9.0"

View File

@@ -38,16 +38,73 @@ export const OPENCODE_CONFIG_SCHEMA = "https://opencode.ai/config.json" as const
/**
* Default catalog of models surfaced to OpenCode when the caller does not
* supply an explicit `models` list. Mirrors the curated set used by the
* OmniRoute dashboard's "OpenCode" config generator.
* supply an explicit `models` list.
*
* Curated set covering the most commonly deployed OmniRoute models. Synced
* with the Alph4d0g/opencode-omniroute-auth OMNIROUTE_DEFAULT_MODELS constant
* (https://github.com/Alph4d0g/opencode-omniroute-auth, MIT) and extended
* with Claude Code passthrough models (`cc/` prefix).
*/
export const OMNIROUTE_DEFAULT_OPENCODE_MODELS = [
"cc/claude-opus-4-7",
"cc/claude-sonnet-4-6",
"cc/claude-haiku-4-5-20251001",
"claude-opus-4-5-thinking",
"claude-sonnet-4-5-thinking",
"gemini-3.1-pro-high",
"gemini-3-flash",
] as const;
/**
* Optional capability flags surfaced to OpenCode's model picker.
*
* OpenCode reads these per-model keys (snake_case in JSON) to render badges
* and to gate features such as image attachments, reasoning mode, temperature
* controls and tool-calling. Omitted flags default to OpenCode's heuristics.
*
* Mirrors the capability shape used by Alph4d0g/opencode-omniroute-auth
* (https://github.com/Alph4d0g/opencode-omniroute-auth, MIT).
*/
export interface ModelCapabilities {
/** Display label shown in the model picker. Falls back to the model id. */
label?: string;
/** Model accepts image / file attachments. */
attachment?: boolean;
/** Model exposes a "reasoning" / extended-thinking surface. */
reasoning?: boolean;
/** Model honours the `temperature` parameter. */
temperature?: boolean;
/** Model supports tool / function calling. */
tool_call?: boolean;
}
/**
* Default per-model capability hints for the curated default catalog.
*
* Conservative defaults: every default model accepts attachments, tool calls
* and temperature; `reasoning` is opt-in per model id. Callers override per
* model via `OmniRouteProviderOptions.modelCapabilities`.
*/
export const OMNIROUTE_DEFAULT_MODEL_CAPABILITIES: Record<string, ModelCapabilities> = {
"cc/claude-opus-4-7": { attachment: true, reasoning: true, temperature: true, tool_call: true },
"cc/claude-sonnet-4-6": { attachment: true, reasoning: true, temperature: true, tool_call: true },
"cc/claude-haiku-4-5-20251001": { attachment: true, temperature: true, tool_call: true },
"claude-opus-4-5-thinking": {
attachment: true,
reasoning: true,
temperature: true,
tool_call: true,
},
"claude-sonnet-4-5-thinking": {
attachment: true,
reasoning: true,
temperature: true,
tool_call: true,
},
"gemini-3.1-pro-high": { attachment: true, reasoning: true, temperature: true, tool_call: true },
"gemini-3-flash": { attachment: true, temperature: true, tool_call: true },
};
export interface OmniRouteProviderOptions {
/** OmniRoute base URL, with or without trailing `/v1`. Required. */
baseURL: string;
@@ -57,8 +114,33 @@ export interface OmniRouteProviderOptions {
displayName?: string;
/** Override the model catalog. Defaults to `OMNIROUTE_DEFAULT_OPENCODE_MODELS`. */
models?: readonly string[];
/** Optional human-readable labels keyed by model id. */
/** Optional human-readable labels keyed by model id. Overridden by `modelCapabilities[id].label`. */
modelLabels?: Record<string, string>;
/**
* Optional capability overrides keyed by model id. Merged on top of
* `OMNIROUTE_DEFAULT_MODEL_CAPABILITIES` for ids in the default catalog;
* for custom ids the override is used verbatim.
*/
modelCapabilities?: Record<string, ModelCapabilities>;
/**
* Primary model for OpenCode (top-level `model` key).
* Emitted as `"omniroute/<id>"`. When omitted the key is not written.
*/
model?: string;
/**
* Secondary / cheap model for OpenCode (top-level `small_model` key).
* Emitted as `"omniroute/<id>"`. When omitted the key is not written.
*/
smallModel?: string;
}
/** Per-model entry written under `provider.omniroute.models[id]`. */
export interface OpenCodeModelEntry {
name: string;
attachment?: boolean;
reasoning?: boolean;
temperature?: boolean;
tool_call?: boolean;
}
export interface OpenCodeProviderEntry {
@@ -72,11 +154,15 @@ export interface OpenCodeProviderEntry {
apiKey: string;
};
/** Model catalog surfaced to OpenCode. */
models: Record<string, { name: string }>;
models: Record<string, OpenCodeModelEntry>;
}
export interface OpenCodeConfigDocument {
$schema: typeof OPENCODE_CONFIG_SCHEMA;
/** Primary model for OpenCode, e.g. `"omniroute/claude-sonnet-4-5-thinking"`. */
model?: string;
/** Secondary / cheap model for OpenCode, e.g. `"omniroute/gemini-3-flash"`. */
small_model?: string;
provider: {
[OMNIROUTE_PROVIDER_KEY]: OpenCodeProviderEntry;
};
@@ -100,7 +186,6 @@ function requireNonEmpty(value: unknown, field: string): string {
export function normalizeBaseURL(rawBaseURL: string): string {
const trimmed = requireNonEmpty(rawBaseURL, "baseURL");
try {
// Reject malformed URLs early.
new URL(trimmed);
} catch {
throw new Error(
@@ -127,14 +212,28 @@ export function createOmniRouteProvider(options: OmniRouteProviderOptions): Open
: [...OMNIROUTE_DEFAULT_OPENCODE_MODELS];
const labels = options.modelLabels ?? {};
const models: Record<string, { name: string }> = {};
const overrides = options.modelCapabilities ?? {};
const models: Record<string, OpenCodeModelEntry> = {};
const seen = new Set<string>();
for (const raw of modelList) {
const id = typeof raw === "string" ? raw.trim() : "";
if (!id || seen.has(id)) continue;
seen.add(id);
const label = typeof labels[id] === "string" && labels[id].trim() ? labels[id].trim() : id;
models[id] = { name: label };
const defaults = OMNIROUTE_DEFAULT_MODEL_CAPABILITIES[id] ?? {};
const override = overrides[id] ?? {};
const merged: ModelCapabilities = { ...defaults, ...override };
const explicitLabel =
typeof merged.label === "string" && merged.label.trim()
? merged.label.trim()
: typeof labels[id] === "string" && labels[id].trim()
? labels[id].trim()
: id;
const entry: OpenCodeModelEntry = { name: explicitLabel };
if (typeof merged.attachment === "boolean") entry.attachment = merged.attachment;
if (typeof merged.reasoning === "boolean") entry.reasoning = merged.reasoning;
if (typeof merged.temperature === "boolean") entry.temperature = merged.temperature;
if (typeof merged.tool_call === "boolean") entry.tool_call = merged.tool_call;
models[id] = entry;
}
return {
@@ -148,16 +247,590 @@ export function createOmniRouteProvider(options: OmniRouteProviderOptions): Open
/**
* Build a full OpenCode config document (with `$schema` + `provider.omniroute`).
* Useful when scaffolding a fresh `opencode.json`.
*
* When `options.model` / `options.smallModel` are supplied they are emitted as
* top-level `model` / `small_model` keys prefixed with `"omniroute/"` so
* OpenCode resolves them through the configured provider.
*/
export function buildOmniRouteOpenCodeConfig(
options: OmniRouteProviderOptions
): OpenCodeConfigDocument {
return {
const doc: OpenCodeConfigDocument = {
$schema: OPENCODE_CONFIG_SCHEMA,
provider: {
[OMNIROUTE_PROVIDER_KEY]: createOmniRouteProvider(options),
},
};
if (options.model !== undefined) {
const id = options.model.trim();
if (id) doc.model = `${OMNIROUTE_PROVIDER_KEY}/${id}`;
}
if (options.smallModel !== undefined) {
const id = options.smallModel.trim();
if (id) doc.small_model = `${OMNIROUTE_PROVIDER_KEY}/${id}`;
}
return doc;
}
/**
* Merge the OmniRoute provider entry (and optional `model` / `small_model`
* keys) into an already-existing OpenCode config object.
*
* Performs a non-destructive merge: all top-level keys in `existing` are
* preserved. The `provider` map is shallow-merged so other providers already
* present are not removed. If `existing.provider.omniroute` already exists it
* is overwritten by the newly built entry.
*
* `model` and `small_model` are only written when supplied in `options`.
*
* @example
* ```ts
* const existing = JSON.parse(readFileSync("opencode.json", "utf8"));
* const updated = mergeIntoExistingConfig(existing, {
* baseURL: "http://localhost:20128",
* apiKey: "sk_omniroute",
* model: "claude-sonnet-4-5-thinking",
* });
* writeFileSync("opencode.json", JSON.stringify(updated, null, 2));
* ```
*/
export function mergeIntoExistingConfig(
existing: Record<string, unknown>,
options: OmniRouteProviderOptions
): Record<string, unknown> {
const partial = buildOmniRouteOpenCodeConfig(options);
const merged: Record<string, unknown> = { ...existing };
if (partial.model !== undefined) merged.model = partial.model;
if (partial.small_model !== undefined) merged.small_model = partial.small_model;
const existingProvider =
typeof existing.provider === "object" && existing.provider !== null
? (existing.provider as Record<string, unknown>)
: {};
merged.provider = {
...existingProvider,
[OMNIROUTE_PROVIDER_KEY]: partial.provider[OMNIROUTE_PROVIDER_KEY],
};
return merged;
}
/**
* The 7 read-only MCP scopes that allow inspection without any write access.
* Suitable for shared / public environments.
*/
export const OMNIROUTE_MCP_DEFAULT_SCOPES = [
"read:health",
"read:combos",
"read:quota",
"read:usage",
"read:models",
"read:cache",
"read:compression",
] as const;
export type OmniRouteMCPScope = (typeof OMNIROUTE_MCP_DEFAULT_SCOPES)[number] | string;
export interface OmniRouteMCPOptions {
/** Absolute path to the MCP server entry point (TypeScript or compiled JS). */
serverPath: string;
/** OmniRoute API key forwarded to the MCP server as `OMNIROUTE_API_KEY`. */
apiKey: string;
/**
* Management API key used for management-scoped operations.
* When supplied it is forwarded as `OMNIROUTE_MANAGEMENT_API_KEY`.
*/
managementApiKey?: string;
/**
* Comma-separated scope list passed as `OMNIROUTE_MCP_SCOPES`.
* When omitted `OMNIROUTE_MCP_ENFORCE_SCOPES` is not set and all scopes are
* available (development default). Pass an explicit list to restrict access.
*/
scopes?: OmniRouteMCPScope[];
/**
* Runtime used to execute the MCP server.
*
* - `"tsx"` (default) — runs via `npx tsx` for TypeScript source files.
* - `"node"` — runs via `node` for compiled JS outputs.
*/
runtime?: "tsx" | "node";
}
export interface OpenCodeMCPServerEntry {
command: string;
args: string[];
env: Record<string, string>;
}
/**
* Build the `mcp.servers.omniroute` entry for an OpenCode config document.
*
* @example
* ```ts
* const mcpEntry = createOmniRouteMCPEntry({
* serverPath: "/home/user/.local/share/omniroute/open-sse/mcp-server/server.ts",
* apiKey: "sk_omniroute",
* managementApiKey: "sk_manage_...",
* scopes: ["read:health", "read:combos", "execute:completions"],
* });
* // Place at config.mcp.servers.omniroute
* ```
*/
export function createOmniRouteMCPEntry(options: OmniRouteMCPOptions): OpenCodeMCPServerEntry {
const serverPath = requireNonEmpty(options.serverPath, "serverPath");
const apiKey = requireNonEmpty(options.apiKey, "apiKey");
const runtime = options.runtime ?? "tsx";
const command = runtime === "tsx" ? "npx" : "node";
const args = runtime === "tsx" ? ["tsx", serverPath] : [serverPath];
const env: Record<string, string> = {
OMNIROUTE_API_KEY: apiKey,
};
if (options.managementApiKey !== undefined) {
const mgmtKey = options.managementApiKey.trim();
if (mgmtKey) env.OMNIROUTE_MANAGEMENT_API_KEY = mgmtKey;
}
if (options.scopes !== undefined && options.scopes.length > 0) {
env.OMNIROUTE_MCP_ENFORCE_SCOPES = "true";
env.OMNIROUTE_MCP_SCOPES = options.scopes.join(",");
}
return { command, args, env };
}
async function fetchJSON<T>(url: string, apiKey: string, timeoutMs: number): Promise<T> {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
const response = await fetch(url, {
headers: { Authorization: `Bearer ${apiKey}` },
signal: controller.signal,
});
if (!response.ok) {
throw new Error(`received HTTP ${response.status}`);
}
return (await response.json()) as T;
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
throw new Error(`@omniroute/opencode-provider: request to ${url} failed: ${message}`);
} finally {
clearTimeout(timer);
}
}
/**
* Lightweight model descriptor returned by `fetchLiveModels`.
* The shape mirrors the subset of fields that OmniRoute's `/v1/models`
* endpoint reliably provides across versions, normalised from both
* camelCase and snake_case variants used by different OmniRoute releases.
*
* Attribution: field-variant normalisation logic adapted from
* https://github.com/Alph4d0g/opencode-omniroute-auth (MIT).
*/
export interface OmniRouteLiveModel {
id: string;
name: string;
}
/**
* Fetch the live model catalog from a running OmniRoute instance.
*
* Returns an array of `{ id, name }` objects from `GET /v1/models`. Handles
* both the camelCase (`modelId`, `displayName`) and snake_case (`model_id`,
* `display_name`) field variants across OmniRoute versions.
*
* Useful for dynamically populating the `models` option of
* `createOmniRouteProvider` / `buildOmniRouteOpenCodeConfig` instead of
* relying on `OMNIROUTE_DEFAULT_OPENCODE_MODELS`.
*
* @param baseURL - OmniRoute base URL (with or without `/v1`).
* @param apiKey - OmniRoute API key.
* @param timeoutMs - Request timeout in milliseconds (default 5000).
*
* @example
* ```ts
* const models = await fetchLiveModels("http://localhost:20128", "sk_omniroute");
* const config = buildOmniRouteOpenCodeConfig({
* baseURL: "http://localhost:20128",
* apiKey: "sk_omniroute",
* models: models.map((m) => m.id),
* modelLabels: Object.fromEntries(models.map((m) => [m.id, m.name])),
* });
* ```
*/
export async function fetchLiveModels(
baseURL: string,
apiKey: string,
timeoutMs = 5_000
): Promise<OmniRouteLiveModel[]> {
const key = requireNonEmpty(apiKey, "apiKey");
const url = `${normalizeBaseURL(baseURL)}/models`;
const body = await fetchJSON<unknown>(url, key, timeoutMs);
const rawList: unknown[] = Array.isArray(body)
? body
: body && typeof body === "object" && Array.isArray((body as { data?: unknown[] }).data)
? ((body as { data: unknown[] }).data as unknown[])
: [];
const models: OmniRouteLiveModel[] = [];
for (const raw of rawList) {
if (typeof raw !== "object" || raw === null) continue;
const r = raw as Record<string, unknown>;
const id =
typeof r.id === "string"
? r.id.trim()
: typeof r.modelId === "string"
? r.modelId.trim()
: typeof r.model_id === "string"
? r.model_id.trim()
: "";
if (!id) continue;
const name =
typeof r.name === "string"
? r.name.trim()
: typeof r.displayName === "string"
? r.displayName.trim()
: typeof r.display_name === "string"
? r.display_name.trim()
: id;
models.push({ id, name: name || id });
}
return models;
}
/**
* Valid per-combo compression override values.
* An empty string clears any existing override (inherits global setting).
*/
export type OmniRouteCompressionOverride =
| ""
| "off"
| "lite"
| "standard"
| "aggressive"
| "ultra"
| "rtk"
| "stacked";
const VALID_COMPRESSION_OVERRIDES = new Set<string>([
"",
"off",
"lite",
"standard",
"aggressive",
"ultra",
"rtk",
"stacked",
]);
/** Slim combo descriptor returned by `listCombos`. */
export interface OmniRouteCombo {
id: string;
name: string;
strategy: string;
active: boolean;
compressionOverride: OmniRouteCompressionOverride;
}
/**
* Fetch the active routing combo list from a running OmniRoute instance.
*
* Returns an array of combo descriptors from `GET /api/combos`. The
* `compressionOverride` field reflects the per-combo compression strategy
* (one of the 8 recognised values; empty string means "inherit global").
*
* Requires a management-scoped API key (Bearer `manage` scope) when the
* instance has `REQUIRE_API_KEY` enabled.
*
* @param baseURL - OmniRoute base URL (with or without `/v1`).
* @param managementApiKey - API key with `manage` scope.
* @param timeoutMs - Request timeout in milliseconds (default 5000).
*/
export async function listCombos(
baseURL: string,
managementApiKey: string,
timeoutMs = 5_000
): Promise<OmniRouteCombo[]> {
const key = requireNonEmpty(managementApiKey, "managementApiKey");
const base = normalizeBaseURL(baseURL).replace(/\/v1$/, "");
const url = `${base}/api/combos`;
const body = await fetchJSON<unknown>(url, key, timeoutMs);
const rawList: unknown[] = Array.isArray(body)
? body
: body && typeof body === "object" && Array.isArray((body as { combos?: unknown[] }).combos)
? ((body as { combos: unknown[] }).combos as unknown[])
: [];
const combos: OmniRouteCombo[] = [];
for (const raw of rawList) {
if (typeof raw !== "object" || raw === null) continue;
const r = raw as Record<string, unknown>;
const id = typeof r.id === "string" ? r.id.trim() : "";
if (!id) continue;
const name = typeof r.name === "string" ? r.name.trim() : id;
const strategy = typeof r.strategy === "string" ? r.strategy : "";
const active = typeof r.active === "boolean" ? r.active : false;
const rawOverride = typeof r.compressionOverride === "string" ? r.compressionOverride : "";
const compressionOverride = VALID_COMPRESSION_OVERRIDES.has(rawOverride)
? (rawOverride as OmniRouteCompressionOverride)
: "";
combos.push({ id, name, strategy, active, compressionOverride });
}
return combos;
}
/**
* Options for `createOmniRouteComboConfig`.
* Mirrors the subset of combo fields exposed by the OmniRoute `/api/combos`
* PATCH / POST payload that are safe to set programmatically.
*/
export interface OmniRouteComboConfigOptions {
/** Human-readable combo name. */
name: string;
/** Routing strategy (e.g. `"priority"`, `"weighted"`, `"round-robin"`). */
strategy: string;
/**
* Per-combo compression override.
* Empty string removes any override (inherits global setting).
*/
compressionOverride?: OmniRouteCompressionOverride;
/** Whether this combo is active for routing. Default: `true`. */
active?: boolean;
/**
* Ordered list of provider IDs in this combo.
* Required for create operations; optional for updates.
*/
providers?: string[];
}
/**
* Build a typed combo payload suitable for OmniRoute's management API.
*
* The returned object is JSON-serialisable and safe to pass as the body of a
* `POST /api/combos` (create) or `PATCH /api/combos/:id` (update) request.
*
* @example
* ```ts
* const payload = createOmniRouteComboConfig({
* name: "claude-primary",
* strategy: "priority",
* compressionOverride: "standard",
* providers: ["anthropic-claude-opus", "anthropic-claude-sonnet"],
* });
* await fetch(`${baseURL}/api/combos`, {
* method: "POST",
* headers: { Authorization: `Bearer ${mgmtKey}`, "Content-Type": "application/json" },
* body: JSON.stringify(payload),
* });
* ```
*/
export function createOmniRouteComboConfig(
options: OmniRouteComboConfigOptions
): Record<string, unknown> {
const name = requireNonEmpty(options.name, "name");
const strategy = requireNonEmpty(options.strategy, "strategy");
const payload: Record<string, unknown> = {
name,
strategy,
active: options.active ?? true,
};
if (options.compressionOverride !== undefined) {
payload.compressionOverride = options.compressionOverride;
}
if (options.providers !== undefined) {
const providers = options.providers.filter((p) => typeof p === "string" && p.trim());
if (providers.length > 0) {
payload.providers = providers;
}
}
return payload;
}
/**
* Override fields supported per agent / mode entry. Mirrors the subset of
* OpenCode's `AgentConfig` schema that is safe to set declaratively from a
* config generator. Only fields present in
* https://opencode.ai/config.json#AgentConfig are exposed.
*/
export interface OmniRouteRoleOverrides {
/** Forward to OpenCode's `temperature` field. */
temperature?: number;
/** Forward to OpenCode's `top_p` field. */
top_p?: number;
}
/** Per-role binding used by `createOmniRouteAgentBlock`. */
export interface OmniRouteAgentRole extends OmniRouteRoleOverrides {
/** OmniRoute model id, e.g. `"claude-sonnet-4-5-thinking"`. */
modelId: string;
/** Optional tools allow-list; per OpenCode schema, map of tool name → enabled. */
tools?: Record<string, boolean>;
/** Optional system prompt for this agent role. */
prompt?: string;
}
/** Options for `createOmniRouteAgentBlock`. */
export interface OmniRouteAgentBlockOptions {
/** Per-role bindings. Keys become entries under OpenCode's `agent` block. */
roles: Record<string, OmniRouteAgentRole>;
}
/** Single entry inside the emitted OpenCode `agent` block. */
export interface OpenCodeAgentEntry extends OmniRouteRoleOverrides {
/** Always emitted as `"omniroute/<modelId>"`. */
model: string;
/** Per OpenCode schema, `Record<string, boolean>`. */
tools?: Record<string, boolean>;
/** Optional system prompt. */
prompt?: string;
}
function buildAgentEntry(role: OmniRouteAgentRole): OpenCodeAgentEntry | undefined {
if (!role || typeof role.modelId !== "string") return undefined;
const modelId = role.modelId.trim();
if (!modelId) return undefined;
const entry: OpenCodeAgentEntry = { model: `${OMNIROUTE_PROVIDER_KEY}/${modelId}` };
if (typeof role.temperature === "number") entry.temperature = role.temperature;
if (typeof role.top_p === "number") entry.top_p = role.top_p;
if (role.tools && typeof role.tools === "object" && !Array.isArray(role.tools)) {
const tools: Record<string, boolean> = {};
for (const [name, enabled] of Object.entries(role.tools)) {
if (typeof name !== "string" || !name.trim()) continue;
if (typeof enabled !== "boolean") continue;
tools[name] = enabled;
}
if (Object.keys(tools).length > 0) entry.tools = tools;
}
if (typeof role.prompt === "string" && role.prompt.trim()) {
entry.prompt = role.prompt;
}
return entry;
}
/**
* Build the OpenCode `agent` block, pre-wired so each agent role routes to a
* specific OmniRoute model. Useful for `.opencode/agent/*.md` defaults and
* scaffolded `opencode.json` files.
*
* Emitted fields are limited to those declared in OpenCode's `AgentConfig`
* schema (`model`, `temperature`, `top_p`, `tools`, `prompt`). The `tools`
* field is a `Record<string, boolean>` per the schema, not a string array.
*
* Roles with empty / missing `modelId` are skipped.
*
* @example
* ```ts
* const agentBlock = createOmniRouteAgentBlock({
* roles: {
* build: { modelId: "claude-sonnet-4-5-thinking", temperature: 0.2 },
* plan: { modelId: "claude-opus-4-5-thinking", top_p: 0.95 },
* review: { modelId: "gemini-3-flash", tools: { edit: false, bash: false } },
* },
* });
* // -> { build: { model: "omniroute/claude-sonnet-4-5-thinking", temperature: 0.2 }, ... }
* ```
*/
export function createOmniRouteAgentBlock(
options: OmniRouteAgentBlockOptions
): Record<string, OpenCodeAgentEntry> {
const out: Record<string, OpenCodeAgentEntry> = {};
const roles = options.roles ?? {};
for (const [roleName, role] of Object.entries(roles)) {
const entry = buildAgentEntry(role);
if (entry) out[roleName] = entry;
}
return out;
}
/**
* Per-mode binding used by `createOmniRouteModesBlock`.
*
* @deprecated OpenCode's top-level `mode` block is deprecated in favour of
* `agent`. Prefer `OmniRouteAgentRole` + `createOmniRouteAgentBlock`. This
* type and the corresponding helper are kept for back-compat with configs
* still using `mode:`.
*/
export interface OmniRouteMode extends OmniRouteAgentRole {}
/**
* Options for `createOmniRouteModesBlock`.
*
* @deprecated See `OmniRouteMode`.
*/
export interface OmniRouteModesBlockOptions {
/** Per-mode bindings. Keys become entries under OpenCode's deprecated top-level `mode` block. */
modes: Record<string, OmniRouteMode>;
}
/**
* Single entry inside the emitted OpenCode `mode` block.
*
* @deprecated See `OmniRouteMode`.
*/
export interface OpenCodeModeEntry extends OpenCodeAgentEntry {}
/**
* Build the OpenCode top-level `mode` block, pre-wired so each mode routes to
* a specific OmniRoute model. Emits the same shape as the `agent` block since
* OpenCode's schema treats them identically (both reference `AgentConfig`).
*
* Modes with empty / missing `modelId` are skipped.
*
* @deprecated OpenCode's top-level `mode` block is deprecated in favour of
* `agent`. Prefer `createOmniRouteAgentBlock`. This helper is kept for
* back-compat with configs still using `mode:`.
*
* @example
* ```ts
* const modesBlock = createOmniRouteModesBlock({
* modes: {
* build: { modelId: "claude-sonnet-4-5-thinking", tools: { edit: true, bash: true } },
* plan: { modelId: "claude-opus-4-5-thinking", prompt: "Plan first, code later." },
* review: { modelId: "gemini-3-flash" },
* },
* });
* ```
*/
export function createOmniRouteModesBlock(
options: OmniRouteModesBlockOptions
): Record<string, OpenCodeModeEntry> {
const out: Record<string, OpenCodeModeEntry> = {};
const modes = options.modes ?? {};
for (const [modeName, mode] of Object.entries(modes)) {
const entry = buildAgentEntry(mode);
if (entry) out[modeName] = entry;
}
return out;
}
export default createOmniRouteProvider;

View File

@@ -1,11 +1,22 @@
import test from "node:test";
import assert from "node:assert/strict";
import { createServer } from "node:http";
import type { Server } from "node:http";
import {
buildOmniRouteOpenCodeConfig,
createOmniRouteAgentBlock,
createOmniRouteComboConfig,
createOmniRouteMCPEntry,
createOmniRouteModesBlock,
createOmniRouteProvider,
fetchLiveModels,
listCombos,
mergeIntoExistingConfig,
normalizeBaseURL,
OMNIROUTE_DEFAULT_MODEL_CAPABILITIES,
OMNIROUTE_DEFAULT_OPENCODE_MODELS,
OMNIROUTE_MCP_DEFAULT_SCOPES,
OMNIROUTE_PROVIDER_NPM,
OPENCODE_CONFIG_SCHEMA,
} from "../src/index.ts";
@@ -66,6 +77,7 @@ test("createOmniRouteProvider seeds the default model catalog", () => {
assert.deepEqual(modelIds, defaultIds);
for (const id of defaultIds) {
assert.equal(provider.models[id]?.name, id);
assert.equal(provider.models[id]?.attachment, true);
}
});
@@ -119,3 +131,437 @@ test("config document is JSON-serialisable", () => {
const round = JSON.parse(JSON.stringify(doc));
assert.deepEqual(round, doc);
});
test("buildOmniRouteOpenCodeConfig emits model and small_model prefixed with provider key", () => {
const doc = buildOmniRouteOpenCodeConfig({
baseURL: "http://localhost:20128",
apiKey: "sk_omniroute",
model: "claude-sonnet-4-5-thinking",
smallModel: "gemini-3-flash",
});
assert.equal(doc.model, "omniroute/claude-sonnet-4-5-thinking");
assert.equal(doc.small_model, "omniroute/gemini-3-flash");
});
test("buildOmniRouteOpenCodeConfig omits model and small_model when not supplied", () => {
const doc = buildOmniRouteOpenCodeConfig({
baseURL: "http://localhost:20128",
apiKey: "sk_omniroute",
});
assert.equal(doc.model, undefined);
assert.equal(doc.small_model, undefined);
assert.ok(!("model" in doc));
assert.ok(!("small_model" in doc));
});
test("buildOmniRouteOpenCodeConfig ignores blank model strings", () => {
const doc = buildOmniRouteOpenCodeConfig({
baseURL: "http://localhost:20128",
apiKey: "sk_omniroute",
model: " ",
smallModel: "",
});
assert.ok(!("model" in doc));
assert.ok(!("small_model" in doc));
});
test("mergeIntoExistingConfig preserves existing provider entries", () => {
const existing = {
$schema: OPENCODE_CONFIG_SCHEMA,
provider: {
anthropic: { npm: "@ai-sdk/anthropic", name: "Anthropic", options: {}, models: {} },
},
keybinds: { submit: "enter" },
};
const result = mergeIntoExistingConfig(existing, {
baseURL: "http://localhost:20128",
apiKey: "sk_omniroute",
});
assert.ok("anthropic" in (result.provider as Record<string, unknown>));
assert.ok("omniroute" in (result.provider as Record<string, unknown>));
assert.deepEqual((result as Record<string, unknown>).keybinds, { submit: "enter" });
});
test("mergeIntoExistingConfig overwrites existing omniroute entry", () => {
const existing = {
provider: {
omniroute: {
npm: "@ai-sdk/openai-compatible",
name: "OLD",
options: { baseURL: "http://old/v1", apiKey: "old" },
models: {},
},
},
};
const result = mergeIntoExistingConfig(existing, {
baseURL: "http://new",
apiKey: "new-key",
displayName: "NEW",
});
const omniroute = (result.provider as Record<string, unknown>).omniroute as { name: string };
assert.equal(omniroute.name, "NEW");
});
test("mergeIntoExistingConfig writes model and small_model when supplied", () => {
const result = mergeIntoExistingConfig(
{},
{
baseURL: "http://localhost:20128",
apiKey: "sk_omniroute",
model: "claude-sonnet-4-5-thinking",
smallModel: "gemini-3-flash",
}
);
assert.equal(result.model, "omniroute/claude-sonnet-4-5-thinking");
assert.equal(result.small_model, "omniroute/gemini-3-flash");
});
test("mergeIntoExistingConfig does not add model keys when not supplied", () => {
const result = mergeIntoExistingConfig(
{},
{ baseURL: "http://localhost:20128", apiKey: "sk_omniroute" }
);
assert.ok(!("model" in result));
assert.ok(!("small_model" in result));
});
test("OMNIROUTE_MCP_DEFAULT_SCOPES contains 7 read-only scopes", () => {
assert.equal(OMNIROUTE_MCP_DEFAULT_SCOPES.length, 7);
assert.ok(OMNIROUTE_MCP_DEFAULT_SCOPES.every((s) => s.startsWith("read:")));
});
test("createOmniRouteMCPEntry defaults to tsx runtime", () => {
const entry = createOmniRouteMCPEntry({
serverPath: "/path/to/server.ts",
apiKey: "sk_omniroute",
});
assert.equal(entry.command, "npx");
assert.deepEqual(entry.args, ["tsx", "/path/to/server.ts"]);
assert.equal(entry.env.OMNIROUTE_API_KEY, "sk_omniroute");
assert.ok(!("OMNIROUTE_MCP_ENFORCE_SCOPES" in entry.env));
assert.ok(!("OMNIROUTE_MANAGEMENT_API_KEY" in entry.env));
});
test("createOmniRouteMCPEntry uses node runtime when specified", () => {
const entry = createOmniRouteMCPEntry({
serverPath: "/path/to/server.js",
apiKey: "sk_omniroute",
runtime: "node",
});
assert.equal(entry.command, "node");
assert.deepEqual(entry.args, ["/path/to/server.js"]);
});
test("createOmniRouteMCPEntry sets management key and scopes when supplied", () => {
const entry = createOmniRouteMCPEntry({
serverPath: "/path/to/server.ts",
apiKey: "sk_omniroute",
managementApiKey: "sk_manage",
scopes: ["read:health", "read:combos", "execute:completions"],
});
assert.equal(entry.env.OMNIROUTE_MANAGEMENT_API_KEY, "sk_manage");
assert.equal(entry.env.OMNIROUTE_MCP_ENFORCE_SCOPES, "true");
assert.equal(entry.env.OMNIROUTE_MCP_SCOPES, "read:health,read:combos,execute:completions");
});
test("createOmniRouteMCPEntry rejects missing required fields", () => {
assert.throws(
() => createOmniRouteMCPEntry({ serverPath: "", apiKey: "x" }),
/serverPath is required/
);
assert.throws(
() => createOmniRouteMCPEntry({ serverPath: "/p", apiKey: "" }),
/apiKey is required/
);
});
function startMockServer(
handler: (path: string) => unknown
): Promise<{ url: string; close: () => void }> {
return new Promise((resolve) => {
const server: Server = createServer((req, res) => {
const body = JSON.stringify(handler(req.url ?? ""));
res.writeHead(200, { "Content-Type": "application/json" });
res.end(body);
});
server.listen(0, "127.0.0.1", () => {
const addr = server.address() as { port: number };
resolve({ url: `http://127.0.0.1:${addr.port}`, close: () => server.close() });
});
});
}
test("fetchLiveModels handles array envelope", async () => {
const { url, close } = await startMockServer(() => [
{ id: "claude-sonnet", name: "Claude Sonnet" },
{ id: "gemini-flash", displayName: "Gemini Flash" },
]);
try {
const models = await fetchLiveModels(url, "sk_test");
assert.equal(models.length, 2);
assert.equal(models[0].id, "claude-sonnet");
assert.equal(models[0].name, "Claude Sonnet");
assert.equal(models[1].id, "gemini-flash");
assert.equal(models[1].name, "Gemini Flash");
} finally {
close();
}
});
test("fetchLiveModels handles data-envelope and snake_case fields", async () => {
const { url, close } = await startMockServer(() => ({
data: [{ model_id: "gpt-4o", display_name: "GPT-4o" }],
}));
try {
const models = await fetchLiveModels(url, "sk_test");
assert.equal(models.length, 1);
assert.equal(models[0].id, "gpt-4o");
assert.equal(models[0].name, "GPT-4o");
} finally {
close();
}
});
test("fetchLiveModels falls back to id as name when no name field", async () => {
const { url, close } = await startMockServer(() => [{ id: "auto" }]);
try {
const models = await fetchLiveModels(url, "sk_test");
assert.equal(models[0].name, "auto");
} finally {
close();
}
});
test("listCombos normalises compressionOverride", async () => {
const { url, close } = await startMockServer(() => ({
combos: [
{
id: "c1",
name: "Primary",
strategy: "priority",
active: true,
compressionOverride: "standard",
},
{
id: "c2",
name: "Cheap",
strategy: "weighted",
active: false,
compressionOverride: "unknown-value",
},
{ id: "c3", name: "Off", strategy: "round-robin", active: true, compressionOverride: "" },
],
}));
try {
const combos = await listCombos(url, "sk_manage");
assert.equal(combos.length, 3);
assert.equal(combos[0].compressionOverride, "standard");
assert.equal(combos[1].compressionOverride, "");
assert.equal(combos[2].compressionOverride, "");
} finally {
close();
}
});
test("createOmniRouteComboConfig builds minimal payload", () => {
const payload = createOmniRouteComboConfig({ name: "my-combo", strategy: "priority" });
assert.equal(payload.name, "my-combo");
assert.equal(payload.strategy, "priority");
assert.equal(payload.active, true);
assert.ok(!("compressionOverride" in payload));
assert.ok(!("providers" in payload));
});
test("createOmniRouteComboConfig includes optional fields when supplied", () => {
const payload = createOmniRouteComboConfig({
name: "full",
strategy: "weighted",
compressionOverride: "aggressive",
active: false,
providers: ["provider-a", "provider-b"],
});
assert.equal(payload.compressionOverride, "aggressive");
assert.equal(payload.active, false);
assert.deepEqual(payload.providers, ["provider-a", "provider-b"]);
});
test("OMNIROUTE_DEFAULT_OPENCODE_MODELS includes cc/ prefixed models", () => {
const defaults = [...OMNIROUTE_DEFAULT_OPENCODE_MODELS];
assert.ok(
defaults.some((m) => m.startsWith("cc/")),
"should have cc/ prefixed models"
);
assert.ok(defaults.length >= 7, "should have at least 7 models");
});
test("OMNIROUTE_DEFAULT_MODEL_CAPABILITIES covers every default model id", () => {
for (const id of OMNIROUTE_DEFAULT_OPENCODE_MODELS) {
const caps = OMNIROUTE_DEFAULT_MODEL_CAPABILITIES[id];
assert.ok(caps, `default capabilities for ${id} missing`);
assert.equal(caps.attachment, true, `${id} should default to attachment=true`);
assert.equal(caps.tool_call, true, `${id} should default to tool_call=true`);
}
});
test("createOmniRouteProvider emits default capability flags inline with the model entry", () => {
const provider = createOmniRouteProvider({
baseURL: "http://localhost:20128",
apiKey: "sk_omniroute",
});
const entry = provider.models["cc/claude-opus-4-7"];
assert.equal(entry.name, "cc/claude-opus-4-7");
assert.equal(entry.attachment, true);
assert.equal(entry.reasoning, true);
assert.equal(entry.temperature, true);
assert.equal(entry.tool_call, true);
});
test("createOmniRouteProvider modelCapabilities overrides defaults and merges per id", () => {
const provider = createOmniRouteProvider({
baseURL: "http://localhost:20128",
apiKey: "sk_omniroute",
modelCapabilities: {
"cc/claude-opus-4-7": { reasoning: false, label: "Opus (no thinking)" },
},
});
const entry = provider.models["cc/claude-opus-4-7"];
assert.equal(entry.name, "Opus (no thinking)");
assert.equal(entry.reasoning, false);
assert.equal(entry.attachment, true);
assert.equal(entry.tool_call, true);
});
test("createOmniRouteProvider applies capability overrides to non-default model ids", () => {
const provider = createOmniRouteProvider({
baseURL: "http://localhost:20128",
apiKey: "sk_omniroute",
models: ["custom-model"],
modelCapabilities: {
"custom-model": { attachment: false, tool_call: true, label: "Custom" },
},
});
const entry = provider.models["custom-model"];
assert.equal(entry.name, "Custom");
assert.equal(entry.attachment, false);
assert.equal(entry.tool_call, true);
assert.equal(entry.reasoning, undefined);
assert.equal(entry.temperature, undefined);
});
test("createOmniRouteProvider modelLabels still works when modelCapabilities omits label", () => {
const provider = createOmniRouteProvider({
baseURL: "http://localhost:20128",
apiKey: "sk_omniroute",
models: ["claude-opus-4-5-thinking"],
modelLabels: { "claude-opus-4-5-thinking": "Opus 4.5 (legacy label)" },
});
assert.equal(provider.models["claude-opus-4-5-thinking"].name, "Opus 4.5 (legacy label)");
});
test("createOmniRouteAgentBlock builds provider-prefixed entries per role", () => {
const block = createOmniRouteAgentBlock({
roles: {
build: { modelId: "claude-sonnet-4-5-thinking", temperature: 0.2 },
plan: { modelId: "claude-opus-4-5-thinking", top_p: 0.95 },
review: { modelId: "gemini-3-flash", temperature: 0.0 },
},
});
assert.equal(block.build.model, "omniroute/claude-sonnet-4-5-thinking");
assert.equal(block.build.temperature, 0.2);
assert.equal(block.plan.model, "omniroute/claude-opus-4-5-thinking");
assert.equal(block.plan.top_p, 0.95);
assert.equal(block.review.model, "omniroute/gemini-3-flash");
assert.equal(block.review.temperature, 0.0);
});
test("createOmniRouteAgentBlock omits optional fields when not supplied", () => {
const block = createOmniRouteAgentBlock({
roles: { build: { modelId: "claude-sonnet-4-5-thinking" } },
});
assert.equal(block.build.model, "omniroute/claude-sonnet-4-5-thinking");
assert.ok(!("temperature" in block.build));
assert.ok(!("top_p" in block.build));
assert.ok(!("tools" in block.build));
assert.ok(!("prompt" in block.build));
});
test("createOmniRouteAgentBlock skips roles with empty modelId", () => {
const block = createOmniRouteAgentBlock({
roles: {
build: { modelId: "claude-sonnet-4-5-thinking" },
plan: { modelId: " " },
review: { modelId: "" },
},
});
assert.deepEqual(Object.keys(block), ["build"]);
});
test("createOmniRouteAgentBlock emits tools as Record<string, boolean> per OC schema", () => {
const block = createOmniRouteAgentBlock({
roles: {
build: {
modelId: "claude-sonnet-4-5-thinking",
tools: { edit: true, bash: true, web: false },
prompt: "Edit files carefully.",
},
},
});
assert.deepEqual(block.build.tools, { edit: true, bash: true, web: false });
assert.equal(block.build.prompt, "Edit files carefully.");
});
test("createOmniRouteAgentBlock filters invalid tool entries and omits empty maps", () => {
const block = createOmniRouteAgentBlock({
roles: {
build: {
modelId: "claude-sonnet-4-5-thinking",
// @ts-expect-error — exercising runtime guard against bad input
tools: { edit: true, bash: "yes", "": true, web: null },
},
plan: {
modelId: "claude-opus-4-5-thinking",
tools: {},
},
},
});
assert.deepEqual(block.build.tools, { edit: true });
assert.ok(!("tools" in block.plan));
});
test("createOmniRouteModesBlock builds provider-prefixed mode entries", () => {
const block = createOmniRouteModesBlock({
modes: {
build: { modelId: "claude-sonnet-4-5-thinking", tools: { edit: true, bash: true } },
plan: { modelId: "claude-opus-4-5-thinking", prompt: "Plan first, code later." },
review: { modelId: "gemini-3-flash" },
},
});
assert.equal(block.build.model, "omniroute/claude-sonnet-4-5-thinking");
assert.deepEqual(block.build.tools, { edit: true, bash: true });
assert.equal(block.plan.prompt, "Plan first, code later.");
assert.equal(block.review.model, "omniroute/gemini-3-flash");
});
test("createOmniRouteModesBlock skips modes with empty modelId", () => {
const block = createOmniRouteModesBlock({
modes: {
build: { modelId: "claude-sonnet-4-5-thinking" },
plan: { modelId: "" },
},
});
assert.deepEqual(Object.keys(block), ["build"]);
});
test("createOmniRouteModesBlock honours numeric overrides limited to OC schema", () => {
const block = createOmniRouteModesBlock({
modes: {
build: {
modelId: "claude-sonnet-4-5-thinking",
temperature: 0.7,
top_p: 0.9,
},
},
});
assert.equal(block.build.temperature, 0.7);
assert.equal(block.build.top_p, 0.9);
});

View File

@@ -4,6 +4,7 @@
"module": "ESNext",
"moduleResolution": "Bundler",
"lib": ["ES2022"],
"types": ["node"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,

View File

@@ -51,6 +51,10 @@
### Fixed
- **fix(providers/llm7):** add `llm7` to the executor registry (`open-sse/config/providerRegistry.ts`). The provider was advertised in the dashboard catalog but missing from the executor table, so every connection test failed with a credential error. Now routes through the standard OpenAI-compatible `https://api.llm7.io/v1/chat/completions` endpoint with optional bearer auth. (#2361)
- **fix(providers/cohere):** switch the Cohere upstream from `https://api.cohere.com/v2/chat` (native shape) to `https://api.cohere.com/compatibility/v1/chat/completions` (OpenAI-compatible). The native endpoint returned `{ message: { content: [...] } }` which the combo test validator could not read, surfacing as `Provider returned HTTP 200 but no text content.` (#2360)
- **fix(combo/dispatch):** add defensive `typeof target.modelStr === "string"` guards around the LKGP fallback findIndex and the combo test target builder. Combo entries whose `modelStr` failed to resolve at routing time (regression after #2338 added per-account LKGP) used to crash the request with `TypeError: e.startsWith is not a function`; we now surface a clean error instead. (#2359)
- **fix(rate-limiter):** Redis is now opt-in. When `REDIS_URL` is unset, the rate limiter and API-key auth cache fall back silently to the in-memory store instead of spamming `connect ECONNREFUSED 127.0.0.1:6379` for every request. Connection-error logging is also deduped so docker logs no longer flood under sustained outage. Single-instance deployments work out of the box; multi-instance deployments continue to use Redis when `REDIS_URL` is provided. (#2357)
- **fix(auto-routing):** stop the `ReferenceError: getSettings is not defined` 500 that every `auto` / `auto/*` request raised. `src/sse/handlers/chat.ts` called the bare `getSettings` symbol without importing it; replaced with the already-imported `getCachedSettings` (same shape, plus the auto-routing hot path benefits from the cache). (#2346)
- **fix(combo/validator):** treat upstream responses carrying a non-empty `reasoning_content` (or `reasoning`) field as valid output, even when `content` is null. Reasoning models like `moonshotai/Kimi-K2.5-TEE`, `zai-org/GLM-5-TEE`, and the QwQ family put their answer in `reasoning_content` only — the quality validator was rejecting them with `502: empty content` and triggering unnecessary combo fallbacks. (#2341)
- **fix(docker):** the Dashboard Docs viewer now actually has documents to show. `.dockerignore` was hiding every file under `docs/` except `openapi.yaml`, so the in-product `/docs/*` viewer threw `ENOENT: no such file or directory, open '/app/docs/...'` for every page. We now ship the ~5 MB English markdown tree and still exclude the ~45 MB of translations/screenshots/raster diagrams that were the original optimization target. (#2348)

View File

@@ -317,6 +317,40 @@ export const IMAGE_PROVIDERS: Record<string, ImageProviderConfig> = {
supportedSizes: ["1:1", "16:9", "9:16", "4:3", "3:4"],
},
haiper: {
id: "haiper",
baseUrl: "https://api.haiper.ai/v1/jobs/gen2/text2image",
statusUrl: "https://api.haiper.ai/v1/jobs",
authType: "apikey",
authHeader: "HAIPER_KEY",
format: "haiper-image",
models: [{ id: "gen2", name: "Gen 2 Image" }],
supportedSizes: ["16:9", "9:16", "1:1", "4:3", "3:4"],
},
leonardo: {
id: "leonardo",
baseUrl: "https://cloud.leonardo.ai/api/rest/v1/generations",
authType: "apikey",
authHeader: "bearer",
format: "leonardo-image",
models: [
{ id: "phoenix", name: "Phoenix" },
{ id: "sdxl", name: "SDXL" },
],
supportedSizes: ["1024x1024", "1024x576", "576x1024"],
},
ideogram: {
id: "ideogram",
baseUrl: "https://api.ideogram.ai/generate",
authType: "apikey",
authHeader: "Api-Key",
format: "ideogram-image",
models: [
{ id: "V_3", name: "Ideogram V3" },
{ id: "V_2A", name: "Ideogram V2A" },
],
supportedSizes: ["1024x1024", "1024x1792", "1792x1024"],
},
sdwebui: {
id: "sdwebui",
baseUrl: "http://localhost:7860/sdapi/v1/txt2img",

View File

@@ -37,6 +37,27 @@ export const MUSIC_PROVIDERS: Record<string, MusicProvider> = {
],
},
suno: {
id: "suno",
baseUrl: "https://studio-api.suno.ai/api/generate/v2/",
statusUrl: "https://studio-api.suno.ai/api/feed/",
authType: "cookie",
authHeader: "cookie",
format: "suno-music",
models: [
{ id: "chirp-v3-5", name: "Chirp V3.5" },
{ id: "chirp-v4", name: "Chirp V4" },
],
},
udio: {
id: "udio",
baseUrl: "https://www.udio.com/api/generate-proxy",
statusUrl: "https://www.udio.com/api/songs",
authType: "cookie",
authHeader: "cookie",
format: "udio-music",
models: [{ id: "udio-default", name: "Udio Default" }],
},
minimax: {
id: "minimax",
baseUrl: "https://api.minimax.io/v1/music_generation",
@@ -50,7 +71,6 @@ export const MUSIC_PROVIDERS: Record<string, MusicProvider> = {
{ id: "music-cover", name: "Music Cover" },
],
},
comfyui: {
id: "comfyui",
baseUrl: "http://localhost:8188",

View File

@@ -1941,6 +1941,68 @@ export const REGISTRY: Record<string, RegistryEntry> = {
],
},
haiper: {
id: "haiper",
alias: "hp",
format: "openai",
executor: "default",
baseUrl: "https://api.haiper.ai/v1",
authType: "apikey",
authHeader: "HAIPER_KEY",
models: [
{ id: "gen2", name: "Gen 2 Video" },
{ id: "gen2-image", name: "Gen 2 Image" },
],
},
leonardo: {
id: "leonardo",
alias: "leo",
format: "openai",
executor: "default",
baseUrl: "https://cloud.leonardo.ai/api/rest/v1",
authType: "apikey",
authHeader: "bearer",
models: [
{ id: "phoenix", name: "Phoenix" },
{ id: "sdxl", name: "SDXL" },
],
},
ideogram: {
id: "ideogram",
alias: "ideo",
format: "openai",
executor: "default",
baseUrl: "https://api.ideogram.ai",
authType: "apikey",
authHeader: "Api-Key",
models: [
{ id: "V_3", name: "Ideogram V3" },
{ id: "V_2A", name: "Ideogram V2A" },
],
},
suno: {
id: "suno",
alias: "suno",
format: "openai",
executor: "default",
baseUrl: "https://studio-api.suno.ai/api/generate/v2/",
authType: "cookie",
authHeader: "cookie",
models: [
{ id: "chirp-v3-5", name: "Chirp V3.5" },
{ id: "chirp-v4", name: "Chirp V4" },
],
},
udio: {
id: "udio",
alias: "udio",
format: "openai",
executor: "default",
baseUrl: "https://www.udio.com/api/generate-proxy",
authType: "apikey",
authHeader: "cookie",
models: [{ id: "udio-default", name: "Udio Default" }],
},
groq: {
id: "groq",
alias: "groq",
@@ -2288,7 +2350,16 @@ export const REGISTRY: Record<string, RegistryEntry> = {
alias: "cohere",
format: "openai",
executor: "default",
baseUrl: "https://api.cohere.com/v2/chat",
// Issue #2360: Cohere's native /v2/chat endpoint returns the upstream
// proprietary shape ({ message: { content: [{type:"text", text:...}] } })
// which the combo test validator (extractComboTestResponseText) does not
// know how to read, surfacing as "Provider returned HTTP 200 but no text
// content." Cohere also publishes an OpenAI-compatible compatibility
// layer at /compatibility/v1 that returns the canonical
// { choices: [{ message: { content: "..." } }] } shape, so we route
// through it instead of needing a Cohere-specific response translator.
baseUrl: "https://api.cohere.com/compatibility/v1/chat/completions",
modelsUrl: "https://api.cohere.com/compatibility/v1/models",
authType: "apikey",
authHeader: "bearer",
models: [
@@ -3120,6 +3191,30 @@ export const REGISTRY: Record<string, RegistryEntry> = {
models: CHAT_OPENAI_COMPAT_MODELS.bytez,
},
// Issue #2361: LLM7.io was visible in the dashboard provider list
// (entry in `src/shared/constants/providers.ts`) but missing from the
// executor registry, so test-connection and chat requests had no
// baseUrl / authType to route to and returned a credential error.
// The provider exposes a standard OpenAI-compatible v1 endpoint with
// an optional bearer token (set the literal string "unused" when no
// key is configured, per upstream docs).
llm7: {
id: "llm7",
alias: "llm7",
format: "openai",
executor: "default",
baseUrl: "https://api.llm7.io/v1/chat/completions",
modelsUrl: "https://api.llm7.io/v1/models",
authType: "apikey",
authHeader: "bearer",
models: [
{ id: "gpt-4o-mini-2024-07-18", name: "GPT-4o mini (LLM7)" },
{ id: "gpt-4.1-nano-2025-04-14", name: "GPT-4.1 nano (LLM7)" },
{ id: "deepseek-r1-0528", name: "DeepSeek R1 (LLM7)" },
{ id: "qwen2.5-coder-32b-instruct", name: "Qwen2.5 Coder 32B (LLM7)" },
],
},
aimlapi: {
id: "aimlapi",
alias: "aiml",

View File

@@ -52,6 +52,24 @@ export const VIDEO_PROVIDERS: Record<string, VideoProvider> = {
],
},
haiper: {
id: "haiper",
baseUrl: "https://api.haiper.ai/v1/jobs/gen2/text2video",
statusUrl: "https://api.haiper.ai/v1/jobs",
authType: "apikey",
authHeader: "HAIPER_KEY",
format: "haiper-video",
models: [{ id: "gen2", name: "Gen 2" }],
},
leonardo: {
id: "leonardo",
baseUrl: "https://cloud.leonardo.ai/api/rest/v1/generations",
statusUrl: "https://cloud.leonardo.ai/api/rest/v1/generations",
authType: "apikey",
authHeader: "bearer",
format: "leonardo-video",
models: [{ id: "phoenix", name: "Phoenix Video" }],
},
pollinations: {
id: "pollinations",
baseUrl: "https://gen.pollinations.ai/video",
@@ -101,7 +119,6 @@ export const VIDEO_PROVIDERS: Record<string, VideoProvider> = {
{ id: "tencent/hunyuan-video", name: "Hunyuan Video" },
],
},
comfyui: {
id: "comfyui",
baseUrl: "http://localhost:8188",

View File

@@ -372,6 +372,30 @@ export async function handleImageGeneration({
});
}
if (providerConfig.format === "haiper-image") {
return handleHaiperImageGeneration({ model, provider, providerConfig, body, credentials, log });
}
if (providerConfig.format === "leonardo-image") {
return handleLeonardoImageGeneration({
model,
provider,
providerConfig,
body,
credentials,
log,
});
}
if (providerConfig.format === "ideogram-image") {
return handleIdeogramImageGeneration({
model,
provider,
providerConfig,
body,
credentials,
log,
});
}
return handleOpenAIImageGeneration({ model, provider, providerConfig, body, credentials, log });
}
@@ -2976,6 +3000,328 @@ async function handleComfyUIImageGeneration({ model, provider, providerConfig, b
}
}
async function handleHaiperImageGeneration({
model,
provider,
providerConfig,
body,
credentials,
log,
}) {
const startTime = Date.now();
const token = credentials?.apiKey || "";
const prompt = typeof body.prompt === "string" ? body.prompt : String(body.prompt ?? "");
if (log) {
log.info("IMAGE", `${provider}/${model} (haiper) | prompt: "${prompt.slice(0, 60)}..."`);
}
try {
const res = await fetch(providerConfig.baseUrl, {
method: "POST",
headers: { "Content-Type": "application/json", HAIPER_KEY: token },
body: JSON.stringify({ prompt, aspect_ratio: body.aspect_ratio || "16:9" }),
});
if (!res.ok) {
const errorText = await res.text();
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: res.status,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: errorText.slice(0, 500),
}).catch(() => {});
return { success: false, status: res.status, error: errorText };
}
const { job_id } = await res.json();
const deadline = Date.now() + 300000;
while (Date.now() < deadline) {
await sleep(5000);
const statusRes = await fetch(`${providerConfig.statusUrl}/${job_id}`, {
headers: { HAIPER_KEY: token },
});
const status = await statusRes.json();
if (status.status === "completed" || status.status === "succeeded") {
const imgUrl = status.creation_url || status.output?.image_url;
if (imgUrl) {
const imgRes = await fetch(imgUrl);
if (!imgRes.ok) {
return {
success: false,
status: imgRes.status,
error: `Failed to download image: ${imgRes.status}`,
};
}
const buf = await imgRes.arrayBuffer();
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: 200,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
}).catch(() => {});
return {
success: true,
data: {
created: Math.floor(Date.now() / 1000),
data: [{ b64_json: Buffer.from(buf).toString("base64") }],
},
};
}
}
if (status.status === "failed") {
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "Haiper image generation failed",
}).catch(() => {});
return { success: false, status: 502, error: "Haiper image generation failed" };
}
}
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: 504,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "Haiper image generation timed out",
}).catch(() => {});
return { success: false, status: 504, error: "Haiper image generation timed out" };
} catch (err) {
if (log) log.error("IMAGE", `${provider} haiper error: ${err.message}`);
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: err.message,
}).catch(() => {});
return { success: false, status: 502, error: `Image provider error: ${err.message}` };
}
}
async function handleLeonardoImageGeneration({
model,
provider,
providerConfig,
body,
credentials,
log,
}) {
const startTime = Date.now();
const token = credentials?.apiKey || "";
const prompt = typeof body.prompt === "string" ? body.prompt : String(body.prompt ?? "");
if (log) {
log.info("IMAGE", `${provider}/${model} (leonardo) | prompt: "${prompt.slice(0, 60)}..."`);
}
try {
const res = await fetch(providerConfig.baseUrl, {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` },
body: JSON.stringify({
modelId: model || "phoenix",
prompt,
width: body.width || 1024,
height: body.height || 1024,
num_images: 1,
}),
});
if (!res.ok) {
const errorText = await res.text();
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: res.status,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: errorText.slice(0, 500),
}).catch(() => {});
return { success: false, status: res.status, error: errorText };
}
const { sdGenerationJob } = await res.json();
const genId = sdGenerationJob?.generationId;
if (!genId) {
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "No generation ID returned",
}).catch(() => {});
return { success: false, status: 502, error: "No generation ID returned" };
}
const deadline = Date.now() + 300000;
while (Date.now() < deadline) {
await sleep(5000);
const statusRes = await fetch(`${providerConfig.baseUrl}/${genId}`, {
headers: { Authorization: `Bearer ${token}` },
});
const status = await statusRes.json();
const gen = status.generations_by_pk || status;
if (gen.status === "COMPLETE") {
const imgUrl = gen.generated_images?.[0]?.url;
if (imgUrl) {
const imgRes = await fetch(imgUrl);
if (!imgRes.ok) {
return {
success: false,
status: imgRes.status,
error: `Failed to download image: ${imgRes.status}`,
};
}
const buf = await imgRes.arrayBuffer();
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: 200,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
}).catch(() => {});
return {
success: true,
data: {
created: Math.floor(Date.now() / 1000),
data: [{ b64_json: Buffer.from(buf).toString("base64") }],
},
};
}
}
if (gen.status === "FAILED") {
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "Leonardo image generation failed",
}).catch(() => {});
return { success: false, status: 502, error: "Leonardo image generation failed" };
}
}
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: 504,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "Leonardo image generation timed out",
}).catch(() => {});
return { success: false, status: 504, error: "Leonardo image generation timed out" };
} catch (err) {
if (log) log.error("IMAGE", `${provider} leonardo error: ${err.message}`);
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: err.message,
}).catch(() => {});
return { success: false, status: 502, error: `Image provider error: ${err.message}` };
}
}
async function handleIdeogramImageGeneration({
model,
provider,
providerConfig,
body,
credentials,
log,
}) {
const startTime = Date.now();
const token = credentials?.apiKey || "";
const prompt = typeof body.prompt === "string" ? body.prompt : String(body.prompt ?? "");
if (log) {
log.info("IMAGE", `${provider}/${model} (ideogram) | prompt: "${prompt.slice(0, 60)}..."`);
}
try {
const res = await fetch(providerConfig.baseUrl, {
method: "POST",
headers: { "Content-Type": "application/json", "Api-Key": token },
body: JSON.stringify({ prompt, aspect_ratio: "ASPECT_16_9", model: model || "V_3" }),
});
if (!res.ok) {
const errorText = await res.text();
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: res.status,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: errorText.slice(0, 500),
}).catch(() => {});
return { success: false, status: res.status, error: errorText };
}
const data = await res.json();
if (data.data && data.data.length > 0) {
const imgUrl = data.data[0].url;
const imgRes = await fetch(imgUrl);
if (!imgRes.ok) {
return {
success: false,
status: imgRes.status,
error: `Failed to download image: ${imgRes.status}`,
};
}
const buf = await imgRes.arrayBuffer();
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: 200,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
}).catch(() => {});
return {
success: true,
data: {
created: Math.floor(Date.now() / 1000),
data: [{ b64_json: Buffer.from(buf).toString("base64") }],
},
};
}
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "No images returned from Ideogram",
}).catch(() => {});
return { success: false, status: 502, error: "No images returned from Ideogram" };
} catch (err) {
if (log) log.error("IMAGE", `${provider} ideogram error: ${err.message}`);
saveCallLog({
method: "POST",
path: "/v1/images/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: err.message,
}).catch(() => {});
return { success: false, status: 502, error: `Image provider error: ${err.message}` };
}
}
type Imagen3ImageGenArgs = {
model: string;
provider: string;

View File

@@ -101,6 +101,13 @@ export async function handleMusicGeneration({ body, credentials, log }) {
return handleKieMusicGeneration({ model, provider, providerConfig, body, credentials, log });
}
if (providerConfig.format === "suno-music") {
return handleSunoMusicGeneration({ model, provider, providerConfig, body, credentials, log });
}
if (providerConfig.format === "udio-music") {
return handleUdioMusicGeneration({ model, provider, providerConfig, body, credentials, log });
}
return {
success: false,
status: 400,
@@ -366,3 +373,231 @@ async function handleKieMusicGeneration({
};
}
}
async function handleSunoMusicGeneration({
model,
provider,
providerConfig,
body,
credentials,
log,
}) {
const startTime = Date.now();
const cookie = credentials?.apiKey || credentials?.providerSpecificData?.cookie || "";
if (!cookie) {
return { success: false, status: 401, error: "Suno session cookie is required" };
}
const prompt = typeof body.prompt === "string" ? body.prompt : String(body.prompt ?? "");
if (log) {
log.info("MUSIC", `${provider}/${model} (suno) | prompt: "${prompt.slice(0, 60)}..."`);
}
try {
const res = await fetch(providerConfig.baseUrl, {
method: "POST",
headers: { "Content-Type": "application/json", Cookie: cookie },
body: JSON.stringify({
gpt_description_prompt: prompt,
mv: model || "chirp-v3-5",
prompt: body.lyrics || "",
title: body.title || "",
tags: body.tags || "",
make_instrumental: body.instrumental || false,
}),
});
if (!res.ok) {
const errorText = await res.text();
saveCallLog({
method: "POST",
path: "/v1/music/generations",
status: res.status,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: errorText.slice(0, 500),
}).catch(() => {});
return { success: false, status: res.status, error: errorText };
}
const clips = await res.json();
const ids = clips.map((c) => c.id).filter(Boolean);
if (ids.length === 0) {
saveCallLog({
method: "POST",
path: "/v1/music/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "No clips returned from Suno",
}).catch(() => {});
return { success: false, status: 502, error: "No clips returned from Suno" };
}
const deadline = Date.now() + 300000;
while (Date.now() < deadline) {
await new Promise((r) => setTimeout(r, 5000));
const feedRes = await fetch(`${providerConfig.statusUrl}?ids=${ids.join(",")}`, {
headers: { Cookie: cookie },
});
const songs = await feedRes.json();
const ready = songs.filter((s) => s.audio_url);
if (ready.length > 0) {
const audioRes = await fetch(ready[0].audio_url);
if (!audioRes.ok) {
return {
success: false,
status: audioRes.status,
error: `Failed to download audio: ${audioRes.status}`,
};
}
const buf = await audioRes.arrayBuffer();
saveCallLog({
method: "POST",
path: "/v1/music/generations",
status: 200,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
}).catch(() => {});
return {
success: true,
data: {
created: Math.floor(Date.now() / 1000),
data: [{ b64_json: Buffer.from(buf).toString("base64"), format: "mp3" }],
},
};
}
}
saveCallLog({
method: "POST",
path: "/v1/music/generations",
status: 504,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "Suno music generation timed out",
}).catch(() => {});
return { success: false, status: 504, error: "Suno music generation timed out" };
} catch (err) {
if (log) log.error("MUSIC", `${provider} suno error: ${err.message}`);
saveCallLog({
method: "POST",
path: "/v1/music/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: err.message,
}).catch(() => {});
return { success: false, status: 502, error: `Music provider error: ${err.message}` };
}
}
async function handleUdioMusicGeneration({
model,
provider,
providerConfig,
body,
credentials,
log,
}) {
const startTime = Date.now();
const cookie = credentials?.apiKey || credentials?.providerSpecificData?.cookie || "";
if (!cookie) {
return { success: false, status: 401, error: "Udio session cookie is required" };
}
const prompt = typeof body.prompt === "string" ? body.prompt : String(body.prompt ?? "");
if (log) {
log.info("MUSIC", `${provider}/${model} (udio) | prompt: "${prompt.slice(0, 60)}..."`);
}
try {
const res = await fetch(providerConfig.baseUrl, {
method: "POST",
headers: { "Content-Type": "application/json", Cookie: cookie },
body: JSON.stringify({ prompt, samplerOptions: { seed: -1 } }),
});
if (!res.ok) {
const errorText = await res.text();
saveCallLog({
method: "POST",
path: "/v1/music/generations",
status: res.status,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: errorText.slice(0, 500),
}).catch(() => {});
return { success: false, status: res.status, error: errorText };
}
const data = await res.json();
const trackIds = data.track_ids || [];
if (trackIds.length === 0) {
saveCallLog({
method: "POST",
path: "/v1/music/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "No tracks returned from Udio",
}).catch(() => {});
return { success: false, status: 502, error: "No tracks returned from Udio" };
}
const deadline = Date.now() + 300000;
while (Date.now() < deadline) {
await new Promise((r) => setTimeout(r, 5000));
const statusRes = await fetch(
`https://www.udio.com/api/songs?songIds=${trackIds.join(",")}`,
{ headers: { Cookie: cookie } }
);
const songs = await statusRes.json();
const ready = songs.filter((s) => s.finished && s.song_path);
if (ready.length > 0) {
const audioRes = await fetch(ready[0].song_path);
if (!audioRes.ok) {
return {
success: false,
status: audioRes.status,
error: `Failed to download audio: ${audioRes.status}`,
};
}
const buf = await audioRes.arrayBuffer();
saveCallLog({
method: "POST",
path: "/v1/music/generations",
status: 200,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
}).catch(() => {});
return {
success: true,
data: {
created: Math.floor(Date.now() / 1000),
data: [{ b64_json: Buffer.from(buf).toString("base64"), format: "mp3" }],
},
};
}
}
saveCallLog({
method: "POST",
path: "/v1/music/generations",
status: 504,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "Udio music generation timed out",
}).catch(() => {});
return { success: false, status: 504, error: "Udio music generation timed out" };
} catch (err) {
if (log) log.error("MUSIC", `${provider} udio error: ${err.message}`);
saveCallLog({
method: "POST",
path: "/v1/music/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: err.message,
}).catch(() => {});
return { success: false, status: 502, error: `Music provider error: ${err.message}` };
}
}

View File

@@ -70,6 +70,20 @@ export async function handleVideoGeneration({ body, credentials, log }) {
return handleRunwayVideoGeneration({ model, provider, providerConfig, body, credentials, log });
}
if (providerConfig.format === "haiper-video") {
return handleHaiperVideoGeneration({ model, provider, providerConfig, body, credentials, log });
}
if (providerConfig.format === "leonardo-video") {
return handleLeonardoVideoGeneration({
model,
provider,
providerConfig,
body,
credentials,
log,
});
}
return {
success: false,
status: 400,
@@ -759,6 +773,192 @@ async function normalizeRunwayVideoResult(task, body) {
return videos;
}
async function handleHaiperVideoGeneration({
model,
provider,
providerConfig,
body,
credentials,
log,
}) {
const startTime = Date.now();
const token = credentials?.apiKey || "";
const res = await fetch(providerConfig.baseUrl, {
method: "POST",
headers: { "Content-Type": "application/json", HAIPER_KEY: token },
body: JSON.stringify({ prompt: body.prompt, duration: 4, aspect_ratio: "16:9" }),
});
if (!res.ok) {
const errorText = await res.text();
saveCallLog({
method: "POST",
path: "/v1/videos/generations",
status: res.status,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: errorText.slice(0, 500),
}).catch(() => {});
return { success: false, status: res.status, error: errorText };
}
const { job_id } = await res.json();
const deadline = Date.now() + 300000;
while (Date.now() < deadline) {
await sleep(5000);
const statusRes = await fetch(`${providerConfig.statusUrl}/${job_id}`, {
headers: { HAIPER_KEY: token },
});
const status = await statusRes.json();
if (status.status === "completed" || status.status === "succeeded") {
const videoUrl = status.creation_url || status.output?.video_url;
if (videoUrl) {
const videoRes = await fetch(videoUrl);
const buf = await videoRes.arrayBuffer();
saveCallLog({
method: "POST",
path: "/v1/videos/generations",
status: 200,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
}).catch(() => {});
return {
success: true,
data: {
created: Math.floor(Date.now() / 1000),
data: [{ b64_json: Buffer.from(buf).toString("base64"), format: "mp4" }],
},
};
}
}
if (status.status === "failed") {
saveCallLog({
method: "POST",
path: "/v1/videos/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "Haiper video generation failed",
}).catch(() => {});
return { success: false, status: 502, error: "Haiper video generation failed" };
}
}
saveCallLog({
method: "POST",
path: "/v1/videos/generations",
status: 504,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "Haiper video generation timed out",
}).catch(() => {});
return { success: false, status: 504, error: "Haiper video generation timed out" };
}
async function handleLeonardoVideoGeneration({
model,
provider,
providerConfig,
body,
credentials,
log,
}) {
const startTime = Date.now();
const token = credentials?.apiKey || "";
const res = await fetch(providerConfig.baseUrl, {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` },
body: JSON.stringify({
modelId: "phoenix",
prompt: body.prompt,
width: 1024,
height: 576,
num_frames: 24,
}),
});
if (!res.ok) {
const errorText = await res.text();
saveCallLog({
method: "POST",
path: "/v1/videos/generations",
status: res.status,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: errorText.slice(0, 500),
}).catch(() => {});
return { success: false, status: res.status, error: errorText };
}
const { sdGenerationJob } = await res.json();
const genId = sdGenerationJob?.generationId;
if (!genId) {
saveCallLog({
method: "POST",
path: "/v1/videos/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "No generation ID returned",
}).catch(() => {});
return { success: false, status: 502, error: "No generation ID returned" };
}
const deadline = Date.now() + 300000;
while (Date.now() < deadline) {
await sleep(5000);
const statusRes = await fetch(`${providerConfig.baseUrl}/${genId}`, {
headers: { Authorization: `Bearer ${token}` },
});
const status = await statusRes.json();
const gen = status.generations_by_pk || status;
if (gen.status === "COMPLETE") {
const imgUrl = gen.generated_images?.[0]?.url;
if (imgUrl) {
const videoRes = await fetch(imgUrl);
const buf = await videoRes.arrayBuffer();
saveCallLog({
method: "POST",
path: "/v1/videos/generations",
status: 200,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
}).catch(() => {});
return {
success: true,
data: {
created: Math.floor(Date.now() / 1000),
data: [{ b64_json: Buffer.from(buf).toString("base64"), format: "mp4" }],
},
};
}
}
if (gen.status === "FAILED") {
saveCallLog({
method: "POST",
path: "/v1/videos/generations",
status: 502,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "Leonardo video generation failed",
}).catch(() => {});
return { success: false, status: 502, error: "Leonardo video generation failed" };
}
}
saveCallLog({
method: "POST",
path: "/v1/videos/generations",
status: 504,
model: `${provider}/${model}`,
provider,
duration: Date.now() - startTime,
error: "Leonardo video generation timed out",
}).catch(() => {});
return { success: false, status: 504, error: "Leonardo video generation timed out" };
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

View File

@@ -1840,7 +1840,17 @@ export async function handleComboChat({
if (lkgpIndex < 0) {
lkgpIndex = orderedTargets.findIndex(
(target) =>
target.provider === providerName || target.modelStr.startsWith(`${providerName}/`)
target.provider === providerName ||
// Issue #2359: Defensive guard. The `target.modelStr` type
// annotation is `string`, but malformed combo entries (e.g.,
// local-provider rows whose `modelStr` failed to resolve when
// the executor catalogue was being rebuilt) have leaked
// through and surfaced as `e.startsWith is not a function`
// 500s on combo test/dispatch. The fast path stays
// unchanged for the common case; this only avoids the
// crash when the field is unexpectedly non-string.
(typeof target.modelStr === "string" &&
target.modelStr.startsWith(`${providerName}/`))
);
}

View File

@@ -128,7 +128,7 @@ export function compressContext(
let messages = [...body.messages];
let currentTokens = estimateTokens(JSON.stringify(messages));
const stats = { original: currentTokens, layers: [] };
const stats = { original: currentTokens, layers: [] as { name: string; tokens: number }[] };
// Already fits
if (currentTokens <= targetTokens) {

View File

@@ -35,12 +35,25 @@ function buildComboTestResult(target, partial = {}) {
async function testComboTarget(target, baseInternalUrl, internalApiKey: string | null) {
const startTime = Date.now();
try {
// Issue #2359: combo entries with a malformed/missing modelStr surfaced
// as `e.startsWith is not a function` / similar TypeError 500s. Coerce
// defensively at the boundary so the test path returns a clean error
// instead of crashing the request handler.
const modelStr = typeof target?.modelStr === "string" ? target.modelStr : "";
if (!modelStr) {
return buildComboTestResult(target, {
status: "error",
error: "Combo step is missing a model id (modelStr). Re-save the combo to refresh it.",
latencyMs: 0,
});
}
const modelLower = modelStr.toLowerCase();
const isEmbedding =
target.modelStr.toLowerCase().includes("embedding") ||
target.modelStr.toLowerCase().includes("bge-") ||
target.modelStr.toLowerCase().includes("text-embed");
modelLower.includes("embedding") ||
modelLower.includes("bge-") ||
modelLower.includes("text-embed");
const internalUrl = `${baseInternalUrl}/v1/${isEmbedding ? "embeddings" : "chat/completions"}`;
const testBody = buildComboTestRequestBody(target.modelStr, isEmbedding);
const testBody = buildComboTestRequestBody(modelStr, isEmbedding);
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 20000);

View File

@@ -119,8 +119,8 @@ const CACHE_TTL = 60 * 1000; // 1 minute TTL
const LAST_USED_UPDATE_TTL = 5 * 60 * 1000;
const MAX_CACHE_SIZE = 1000;
// Compiled regex cache for wildcard patterns
const _regexCache = new Map<string, RegExp>();
// Wildcard scope matching is now handled by `matchesWildcardPattern`
// (deterministic, no RegExp from dynamic strings).
const API_KEY_COLUMN_FALLBACKS = [
{ name: "allowed_models", definition: "allowed_models TEXT" },
@@ -187,6 +187,7 @@ async function deleteRedisAuthCacheEntry(keyHash: unknown): Promise<void> {
try {
const { getRedisClient } = await import("@/shared/utils/rateLimiter");
const redis = getRedisClient();
if (!redis) return; // #2357: Redis is optional; skip when disabled.
await redis.del(`auth:api_key:${keyHash}`);
} catch {
// Redis is an optimization for auth caching; SQLite remains authoritative.
@@ -235,21 +236,57 @@ function evictIfNeeded<TKey, TValue>(cache: Map<TKey, TValue>) {
}
/**
* Get or compile regex for wildcard pattern
* Match an API-key wildcard scope pattern against a model id without
* compiling a RegExp from string concatenation (avoid ReDoS exposure on
* operator-supplied patterns and silence the Semgrep `js/regex-injection`
* advisory for `new RegExp(<dynamic>)`).
*
* Supported pattern syntax (only what real scopes use):
* - literal segments
* - `*` matches any run of characters, but does NOT cross `/`
*
* Walks the pattern token-by-token: each `*` consumes the longest possible
* run within the current path segment, then the next literal anchor must
* appear before the segment boundary. Worst-case complexity is O(n*m)
* where n = pattern length, m = candidate length — there is no nested
* backtracking that could explode adversarially.
*/
function getWildcardRegex(pattern: string): RegExp {
let regex = _regexCache.get(pattern);
if (!regex) {
const regexStr = pattern.replace(/\*/g, ".*");
regex = new RegExp(`^${regexStr}$`);
_regexCache.set(pattern, regex);
// Prevent unbounded growth
if (_regexCache.size > 100) {
const firstKey = _regexCache.keys().next().value;
if (firstKey) _regexCache.delete(firstKey);
}
function matchesWildcardPattern(pattern: string, candidate: string): boolean {
const pSegs = pattern.split("/");
const cSegs = candidate.split("/");
if (pSegs.length !== cSegs.length) return false;
for (let i = 0; i < pSegs.length; i++) {
if (!segmentMatchesWildcard(pSegs[i], cSegs[i])) return false;
}
return regex;
return true;
}
function segmentMatchesWildcard(pattern: string, segment: string): boolean {
if (pattern === segment) return true;
if (!pattern.includes("*")) return false;
const parts = pattern.split("*");
// Anchor first literal to the start.
let cursor = 0;
const first = parts[0];
if (first) {
if (!segment.startsWith(first)) return false;
cursor = first.length;
}
// Anchor last literal to the end.
const last = parts[parts.length - 1];
const endLimit = segment.length - last.length;
if (last) {
if (!segment.endsWith(last)) return false;
}
// Each middle literal must appear in order between cursor and endLimit.
for (let i = 1; i < parts.length - 1; i++) {
const piece = parts[i];
if (!piece) continue;
const idx = segment.indexOf(piece, cursor);
if (idx === -1 || idx + piece.length > endLimit) return false;
cursor = idx + piece.length;
}
return cursor <= endLimit;
}
function ensureApiKeyColumn(
@@ -858,6 +895,7 @@ export async function validateApiKey(key: string | null | undefined) {
try {
const { getRedisClient } = await import("@/shared/utils/rateLimiter");
const redis = getRedisClient();
if (!redis) throw new Error("redis-disabled"); // #2357: optional
const redisKey = `auth:api_key:${hashedKey}`;
const redisData = await redis.get(redisKey);
if (redisData) {
@@ -909,6 +947,9 @@ export async function validateApiKey(key: string | null | undefined) {
try {
const { getRedisClient } = await import("@/shared/utils/rateLimiter");
const redis = getRedisClient();
// #2357: Redis is optional; throw so the catch below skips the write
// without affecting the function's `Promise<boolean>` return type.
if (!redis) throw new Error("redis-disabled");
const redisKey = `auth:api_key:${hashedKey}`;
await redis.set(
redisKey,
@@ -1081,10 +1122,10 @@ export async function isModelAllowedForKey(
break;
}
}
// Support wildcard patterns using cached regex
// Support wildcard patterns via deterministic matcher (no RegExp
// compilation from operator input — avoids ReDoS exposure).
if (pattern.includes("*")) {
const regex = getWildcardRegex(pattern);
if (regex.test(modelId)) {
if (matchesWildcardPattern(pattern, modelId)) {
allowed = true;
break;
}
@@ -1120,7 +1161,6 @@ export function clearApiKeyCaches() {
invalidateCaches();
_lastUsedUpdateCache.clear();
_modelPermissionCache.clear();
_regexCache.clear();
}
/**

View File

@@ -1,11 +0,0 @@
CREATE TABLE IF NOT EXISTS session_account_affinity (
session_key TEXT NOT NULL,
provider TEXT NOT NULL,
connection_id TEXT NOT NULL,
created_at INTEGER NOT NULL,
last_seen_at INTEGER NOT NULL,
PRIMARY KEY (session_key, provider)
);
CREATE INDEX IF NOT EXISTS idx_saa_provider ON session_account_affinity(provider);
CREATE INDEX IF NOT EXISTS idx_saa_last_seen ON session_account_affinity(last_seen_at);

View File

@@ -11,7 +11,8 @@ import { HTTP_STATUS } from "@omniroute/open-sse/config/constants.ts";
import * as log from "@/sse/utils/logger";
import { toJsonErrorPayload } from "@/shared/utils/upstreamError";
import { getProviderCredentials, clearRecoveredProviderState } from "@/sse/services/auth";
import { getProviderNodes } from "@/lib/localDb";
import { getProviderNodes, getComboByName, getCombos, getDatabaseSettings } from "@/lib/localDb";
import { handleComboChat } from "@omniroute/open-sse/services/combo.ts";
type ValidatedEmbeddingBody = Record<string, unknown> & { model: string };
@@ -30,6 +31,42 @@ export async function createEmbeddingResponse(
body: ValidatedEmbeddingBody,
options: EmbeddingHandlerOptions = {}
): Promise<Response> {
const modelStr = body.model;
if (!modelStr.includes("/")) {
try {
const combo = await getComboByName(modelStr);
if (combo) {
let allCombos = [];
try {
allCombos = await getCombos();
} catch {}
let settings = {};
try {
settings = getDatabaseSettings();
} catch {}
return handleComboChat({
body,
combo,
handleSingleModel: async (reqBody: any, targetModelStr: string, target?: any) => {
const newBody = { ...reqBody, model: targetModelStr };
return createEmbeddingResponse(newBody, {
...options,
connectionId: target?.connectionId || options.connectionId,
});
},
log,
settings,
allCombos,
signal: undefined,
});
}
} catch (err) {
log.error("EMBED", `Combo resolution failed for ${modelStr}: ${err}`);
}
}
let dynamicProviders: ReturnType<typeof buildDynamicEmbeddingProvider>[] = [];
try {
const nodes = (await getProviderNodes()) as unknown as EmbeddingProviderNodeRow[];

View File

@@ -260,18 +260,18 @@ function buildTokenHeaders(apiKey: string, providerSpecificData: any = {}) {
return applyCustomUserAgent(headers, providerSpecificData);
}
async function validationRead(url: string, init: RequestInit) {
async function validationRead(url: string, init: RequestInit, isLocal: boolean = false) {
return safeOutboundFetch(url, {
...SAFE_OUTBOUND_FETCH_PRESETS.validationRead,
guard: getProviderOutboundGuard(),
guard: isLocal ? "none" : getProviderOutboundGuard(),
...init,
});
}
async function validationWrite(url: string, init: RequestInit) {
async function validationWrite(url: string, init: RequestInit, isLocal: boolean = false) {
return safeOutboundFetch(url, {
...SAFE_OUTBOUND_FETCH_PRESETS.validationWrite,
guard: getProviderOutboundGuard(),
guard: isLocal ? "none" : getProviderOutboundGuard(),
...init,
});
}
@@ -293,88 +293,112 @@ function toValidationErrorResult(error: unknown) {
}
async function validateOpenAILikeProvider({
provider,
apiKey,
baseUrl,
providerSpecificData = {},
modelId = "gpt-4o-mini",
modelsUrl: customModelsUrl,
}: {
provider: string;
apiKey: string;
baseUrl: string;
providerSpecificData?: any;
modelId?: string;
modelsUrl?: string;
}) {
if (!baseUrl) {
return { valid: false, error: "Missing base URL" };
}
headers = {},
modelId = "gpt-3.5-turbo",
providerSpecificData,
modelsUrl = "",
isLocal = false,
}: any) {
try {
const customModelsUrl = modelsUrl?.trim() || "";
const endpointUrl = customModelsUrl
? customModelsUrl.startsWith("http")
? customModelsUrl
: `${baseUrl.replace(/\/+$/, "")}/${customModelsUrl.replace(/^\/+/, "")}`
: `${baseUrl}/models`;
const modelsUrl = customModelsUrl || addModelsSuffix(baseUrl);
if (!modelsUrl) {
return { valid: false, error: "Invalid models endpoint" };
}
const requestUrl =
typeof providerSpecificData?.modelsUrl === "string" &&
providerSpecificData.modelsUrl.trim() !== ""
? providerSpecificData.modelsUrl.trim()
: endpointUrl;
const modelsRes = await validationRead(modelsUrl, {
method: "GET",
headers: buildBearerHeaders(apiKey, providerSpecificData),
});
const response = await validationRead(
requestUrl,
{
headers: {
...headers,
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
},
},
isLocal
);
if (response.ok) {
return { valid: true, error: null };
}
if (response.status === 401 || response.status === 403) {
return { valid: false, error: "Invalid API key" };
}
const chatUrl = resolveChatUrl("openai", baseUrl, providerSpecificData);
if (!chatUrl) {
return { valid: false, error: `Validation failed: ${response.status}` };
}
const testModelId = (providerSpecificData as any)?.validationModelId || modelId;
const testBody = {
model: testModelId,
messages: [{ role: "user", content: "test" }],
max_tokens: 1,
};
const chatRes = await validationWrite(
chatUrl,
{
method: "POST",
headers: {
...headers,
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
},
body: JSON.stringify(testBody),
},
isLocal
);
if (chatRes.ok) {
return { valid: true, error: null };
}
if (chatRes.status === 401 || chatRes.status === 403) {
return { valid: false, error: "Invalid API key" };
}
if (chatRes.status === 404 || chatRes.status === 405) {
return { valid: false, error: "Provider validation endpoint not supported" };
}
if (chatRes.status >= 500) {
return { valid: false, error: `Provider unavailable (${chatRes.status})` };
}
if (modelsRes.ok) {
return { valid: true, error: null };
} catch (error: any) {
return toValidationErrorResult(error);
}
if (modelsRes.status === 401 || modelsRes.status === 403) {
return { valid: false, error: "Invalid API key" };
}
const chatUrl = resolveChatUrl(provider, baseUrl, providerSpecificData);
if (!chatUrl) {
return { valid: false, error: `Validation failed: ${modelsRes.status}` };
}
const testModelId = (providerSpecificData as any)?.validationModelId || modelId;
const testBody = {
model: testModelId,
messages: [{ role: "user", content: "test" }],
max_tokens: 1,
};
const chatRes = await validationWrite(chatUrl, {
method: "POST",
headers: buildBearerHeaders(apiKey, providerSpecificData),
body: JSON.stringify(testBody),
});
if (chatRes.ok) {
return { valid: true, error: null };
}
if (chatRes.status === 401 || chatRes.status === 403) {
return { valid: false, error: "Invalid API key" };
}
if (chatRes.status === 404 || chatRes.status === 405) {
return { valid: false, error: "Provider validation endpoint not supported" };
}
if (chatRes.status >= 500) {
return { valid: false, error: `Provider unavailable (${chatRes.status})` };
}
// 4xx other than auth (e.g., invalid model/body) usually means auth passed.
return { valid: true, error: null };
}
async function validateDirectChatProvider({ url, headers, body, providerSpecificData = {} }: any) {
async function validateDirectChatProvider({
url,
headers,
body,
providerSpecificData = {},
isLocal = false,
}: any) {
try {
const response = await validationWrite(url, {
method: "POST",
headers: applyCustomUserAgent(headers, providerSpecificData),
body: JSON.stringify(body),
});
const response = await validationWrite(
url,
{
method: "POST",
headers: applyCustomUserAgent(headers, providerSpecificData),
body: JSON.stringify(body),
},
isLocal
);
if (response.status === 401 || response.status === 403) {
return { valid: false, error: "Invalid API key" };
@@ -590,59 +614,80 @@ async function validateRerankApiProvider({ apiKey, providerSpecificData = {}, ur
async function validateAnthropicLikeProvider({
apiKey,
baseUrl,
modelId,
modelId = "claude-3-5-sonnet-20240620",
headers = {},
providerSpecificData = {},
isLocal = false,
}: any) {
if (!baseUrl) {
return { valid: false, error: "Missing base URL" };
try {
const requestUrl =
typeof providerSpecificData?.modelsUrl === "string" &&
providerSpecificData.modelsUrl.trim() !== ""
? providerSpecificData.modelsUrl.trim()
: `${baseUrl}/models`;
const response = await validationRead(
requestUrl,
{
headers: {
"anthropic-version": "2023-06-01",
...headers,
},
},
isLocal
);
if (!baseUrl) {
return { valid: false, error: "Missing base URL" };
}
if (typeof apiKey === "string" && apiKey.startsWith("sk-ant-oat")) {
return validateClaudeOAuthInline({ apiKey, modelId, providerSpecificData });
}
const requestHeaders = applyCustomUserAgent(
{
"Content-Type": "application/json",
...headers,
},
providerSpecificData
);
if (!requestHeaders["x-api-key"] && !requestHeaders["X-API-Key"]) {
requestHeaders["x-api-key"] = apiKey;
}
if (!requestHeaders["anthropic-version"] && !requestHeaders["Anthropic-Version"]) {
requestHeaders["anthropic-version"] = "2023-06-01";
}
const testModelId =
providerSpecificData?.validationModelId || modelId || "claude-3-5-sonnet-20241022";
const chatResponse = await validationWrite(
baseUrl,
{
method: "POST",
headers: requestHeaders,
body: JSON.stringify({
model: testModelId,
max_tokens: 1,
messages: [{ role: "user", content: "test" }],
}),
},
isLocal
);
if (chatResponse.status === 401 || chatResponse.status === 403) {
return { valid: false, error: "Invalid API key" };
}
return { valid: true, error: null };
} catch (error: any) {
return toValidationErrorResult(error);
}
// OAuth tokens need the same Claude Code cloak as production traffic in
// base.ts; a bare validation request gets flagged on the user:sessions:
// claude_code scope.
if (typeof apiKey === "string" && apiKey.startsWith("sk-ant-oat")) {
return validateClaudeOAuthInline({ apiKey, modelId, providerSpecificData });
}
const requestHeaders = applyCustomUserAgent(
{
"Content-Type": "application/json",
...headers,
},
providerSpecificData
);
if (!requestHeaders["x-api-key"] && !requestHeaders["X-API-Key"]) {
requestHeaders["x-api-key"] = apiKey;
}
if (!requestHeaders["anthropic-version"] && !requestHeaders["Anthropic-Version"]) {
requestHeaders["anthropic-version"] = "2023-06-01";
}
const testModelId =
providerSpecificData?.validationModelId || modelId || "claude-3-5-sonnet-20241022";
const response = await validationWrite(baseUrl, {
method: "POST",
headers: requestHeaders,
body: JSON.stringify({
model: testModelId,
max_tokens: 1,
messages: [{ role: "user", content: "test" }],
}),
});
if (response.status === 401 || response.status === 403) {
return { valid: false, error: "Invalid API key" };
}
return { valid: true, error: null };
}
// Probe a Claude OAuth credential through the same executor that handles
// production traffic so the cloak/signing/identity logic isn't duplicated.
async function validateClaudeOAuthInline({
apiKey,
modelId,
@@ -673,7 +718,6 @@ async function validateClaudeOAuthInline({
if (response.status >= 500) {
return { valid: false, error: `Provider unavailable (${response.status})` };
}
// 2xx and non-auth 4xx (429 quota, 400 model) both mean the token is valid.
return { valid: true, error: null };
} catch (error: any) {
return toValidationErrorResult(error);
@@ -683,76 +727,79 @@ async function validateClaudeOAuthInline({
async function validateGeminiLikeProvider({
apiKey,
baseUrl,
authType,
providerSpecificData = {},
authType = "query",
isLocal = false,
}: any) {
if (!baseUrl) {
return { valid: false, error: "Missing base URL" };
}
try {
const requestUrl =
typeof providerSpecificData?.modelsUrl === "string" &&
providerSpecificData.modelsUrl.trim() !== ""
? providerSpecificData.modelsUrl.trim()
: `${baseUrl}/models`;
const urlWithKey =
authType === "query" ? `${requestUrl}?key=${encodeURIComponent(apiKey)}` : requestUrl;
const headers = authType === "header" ? { "x-goog-api-key": apiKey } : {};
// Use the correct auth header based on provider config:
// - gemini (API key): x-goog-api-key
// - gemini-cli (OAuth): Bearer token
const headers: Record<string, string> = { "Content-Type": "application/json" };
if (authType === "oauth") {
headers["Authorization"] = `Bearer ${apiKey}`;
} else {
headers["x-goog-api-key"] = apiKey;
}
applyCustomUserAgent(headers, providerSpecificData);
const response = await validationRead(
urlWithKey,
{
headers,
},
isLocal
);
const response = await validationRead(baseUrl, { method: "GET", headers });
if (response.ok) {
return { valid: true, error: null };
}
// 429 = rate limited, but auth is valid
if (response.status === 429) {
return { valid: true, error: null };
}
// Google returns 400 (not 401/403) for invalid API keys on the models endpoint.
// Parse the response body to detect auth failures.
if (response.status === 400 || response.status === 401 || response.status === 403) {
const isAuthError = (body: any) => {
const message = (body?.error?.message || "").toLowerCase();
const reason = body?.error?.details?.[0]?.reason || "";
const status = body?.error?.status || "";
const authPatterns = [
"api key not valid",
"api key expired",
"api key invalid",
"API_KEY_INVALID",
"API_KEY_EXPIRED",
"PERMISSION_DENIED",
"UNAUTHENTICATED",
];
return authPatterns.some(
(p) => message.includes(p.toLowerCase()) || reason === p || status === p
);
};
try {
const body = await response.json();
if (isAuthError(body)) {
return { valid: false, error: "Invalid API key" };
}
// 401/403 are always auth failures even without matching patterns
if (response.status === 401 || response.status === 403) {
return { valid: false, error: "Invalid API key" };
}
} catch {
// Unparseable body — 401/403 are always auth failures
if (response.status === 401 || response.status === 403) {
return { valid: false, error: "Invalid API key" };
}
// 400 without parseable body — likely auth issue for Gemini
return { valid: false, error: "Invalid API key" };
if (!baseUrl) {
return { valid: false, error: "Missing base URL" };
}
}
return { valid: false, error: `Validation failed: ${response.status}` };
if (response.ok) {
return { valid: true, error: null };
}
if (response.status === 429) {
return { valid: true, error: null };
}
if (response.status === 400 || response.status === 401 || response.status === 403) {
const isAuthError = (body: any) => {
const message = (body?.error?.message || "").toLowerCase();
const reason = body?.error?.details?.[0]?.reason || "";
const status = body?.error?.status || "";
const authPatterns = [
"api key not valid",
"api key expired",
"api key invalid",
"API_KEY_INVALID",
"API_KEY_EXPIRED",
"PERMISSION_DENIED",
"UNAUTHENTICATED",
];
return authPatterns.some(
(p) => message.includes(p.toLowerCase()) || reason === p || status === p
);
};
try {
const body = await response.json();
if (isAuthError(body)) {
return { valid: false, error: "Invalid API key" };
}
if (response.status === 401 || response.status === 403) {
return { valid: false, error: "Invalid API key" };
}
} catch {
if (response.status === 401 || response.status === 403) {
return { valid: false, error: "Invalid API key" };
}
return { valid: false, error: "Invalid API key" };
}
}
return { valid: false, error: `Validation failed: ${response.status}` };
} catch (error: any) {
return toValidationErrorResult(error);
}
}
// ── Specialty providers (non-standard APIs) ──
@@ -2134,7 +2181,11 @@ async function validateOpenAICompatibleProvider({ apiKey, providerSpecificData =
}
}
async function validateAnthropicCompatibleProvider({ apiKey, providerSpecificData = {} }: any) {
async function validateAnthropicCompatibleProvider({
apiKey,
providerSpecificData = {},
isLocal = false,
}: any) {
let baseUrl = normalizeAnthropicBaseUrl(providerSpecificData.baseUrl);
if (!baseUrl) {
return { valid: false, error: "No base URL configured for Anthropic compatible provider" };
@@ -2157,7 +2208,8 @@ async function validateAnthropicCompatibleProvider({ apiKey, providerSpecificDat
{
method: "GET",
headers,
}
},
isLocal
);
if (modelsRes.ok) {
@@ -2184,7 +2236,8 @@ async function validateAnthropicCompatibleProvider({ apiKey, providerSpecificDat
max_tokens: 1,
messages: [{ role: "user", content: "test" }],
}),
}
},
isLocal
);
if (messagesRes.status === 401 || messagesRes.status === 403) {
@@ -2280,15 +2333,31 @@ export async function validateClaudeCodeCompatibleProvider({
// ── Search provider validators (factored) ──
async function validateGenericProvider(
baseUrl: string,
apiKey: string,
providerSpecificData: any = {},
provider: string,
isLocal: boolean = false
) {
const config = SEARCH_VALIDATOR_CONFIGS[provider];
if (!config) {
return { valid: false, error: "Validator not found", unsupported: true };
}
const { url, init } = config(apiKey, providerSpecificData);
return validateSearchProvider(url, init, providerSpecificData, isLocal);
}
async function validateSearchProvider(
url: string,
init: RequestInit,
providerSpecificData: any = {}
providerSpecificData: any = {},
isLocal: boolean = false
): Promise<{ valid: boolean; error: string | null; unsupported: false }> {
try {
const response = await safeOutboundFetch(url, {
...SAFE_OUTBOUND_FETCH_PRESETS.validationWrite,
guard: getProviderOutboundGuard(),
guard: isLocal ? "none" : getProviderOutboundGuard(),
...withCustomUserAgent(init, providerSpecificData),
});
if (response.ok) return { valid: true, error: null, unsupported: false };
@@ -2813,7 +2882,7 @@ async function validatePerplexityWebProvider({ apiKey, providerSpecificData = {}
Origin: "https://www.perplexity.ai",
Referer: "https://www.perplexity.ai/",
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/136.0.0.0",
"X-App-ApiClient": "default",
"X-App-ApiVersion": "client-1.11.0",
...(bearerToken
@@ -2882,7 +2951,7 @@ async function validateBlackboxWebProvider({ apiKey, providerSpecificData = {} }
Origin: "https://app.blackbox.ai",
Referer: "https://app.blackbox.ai/",
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/147.0.0.0",
},
providerSpecificData
);
@@ -2912,7 +2981,7 @@ async function validateBlackboxWebProvider({ apiKey, providerSpecificData = {} }
Origin: "https://app.blackbox.ai",
Referer: "https://app.blackbox.ai/",
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/147.0.0.0",
},
providerSpecificData
);
@@ -3048,6 +3117,7 @@ async function validateMuseSparkWebProvider({ apiKey, providerSpecificData = {}
export async function validateProviderApiKey({ provider, apiKey, providerSpecificData = {} }: any) {
const requiresApiKey = !providerAllowsOptionalApiKey(provider);
const isLocal = isLocalProvider(provider);
if (!provider || (requiresApiKey && !apiKey)) {
return { valid: false, error: "Provider and API key required", unsupported: false };
@@ -3066,7 +3136,11 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
if (isClaudeCodeCompatibleProvider(provider)) {
return await validateClaudeCodeCompatibleProvider({ apiKey, providerSpecificData });
}
return await validateAnthropicCompatibleProvider({ apiKey, providerSpecificData });
return await validateAnthropicCompatibleProvider({
apiKey,
providerSpecificData,
isLocal,
});
} catch (error: any) {
return toValidationErrorResult(error);
}
@@ -3112,6 +3186,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
baseUrl,
modelId: getBedrockValidationModelId(baseUrl),
modelsUrl: buildBedrockModelsUrl(baseUrl),
isLocal,
});
},
modal: ({ apiKey, providerSpecificData }: any) =>
@@ -3121,6 +3196,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
providerSpecificData,
baseUrl: normalizeBaseUrl(providerSpecificData?.baseUrl || ""),
modelId: "Qwen/Qwen3-4B-Thinking-2507-FP8",
isLocal,
}),
"nous-research": validateNousResearchProvider,
petals: validatePetalsProvider,
@@ -3164,11 +3240,15 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
? providerSpecificData.baseUrl.trim()
: "";
const root = (configuredBaseUrl || "https://gitlab.com").replace(/\/$/, "");
const res = await validationWrite(`${root}/api/v4/code_suggestions/direct_access`, {
method: "POST",
headers: buildBearerHeaders(apiKey, providerSpecificData),
body: "{}",
});
const res = await validationWrite(
`${root}/api/v4/code_suggestions/direct_access`,
{
method: "POST",
headers: buildBearerHeaders(apiKey, providerSpecificData),
body: "{}",
},
isLocal
);
if (res.status === 401) {
return { valid: false, error: "Invalid API key" };
}
@@ -3182,7 +3262,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
const { parseSAFromApiKey, getAccessToken } =
await import("@omniroute/open-sse/executors/vertex.ts");
const sa = parseSAFromApiKey(apiKey);
// Validates credentials by successfully exchanging them for a JWT from Google Identity
// Validates credentials by successfully successfully exchanging them for a JWT from Google Identity
await getAccessToken(sa);
return { valid: true, error: null };
} catch (error: any) {
@@ -3203,15 +3283,19 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
// LongCat AI — does not expose /v1/models; validate via chat completions directly (#592)
longcat: async ({ apiKey, providerSpecificData }: any) => {
try {
const res = await validationWrite("https://api.longcat.chat/openai/v1/chat/completions", {
method: "POST",
headers: buildBearerHeaders(apiKey, providerSpecificData),
body: JSON.stringify({
model: "longcat",
messages: [{ role: "user", content: "test" }],
max_tokens: 1,
}),
});
const res = await validationWrite(
"https://api.longcat.chat/openai/v1/chat/completions",
{
method: "POST",
headers: buildBearerHeaders(apiKey, providerSpecificData),
body: JSON.stringify({
model: "longcat",
messages: [{ role: "user", content: "test" }],
max_tokens: 1,
}),
},
isLocal
);
if (res.status === 401 || res.status === 403) {
return { valid: false, error: "Invalid API key" };
}
@@ -3230,15 +3314,19 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
providerSpecificData?.baseUrl || "https://api.xiaomimimo.com/v1"
);
const chatUrl = `${baseUrl.replace(/\/chat\/completions$/, "")}/chat/completions`;
const res = await validationWrite(chatUrl, {
method: "POST",
headers: buildBearerHeaders(apiKey, providerSpecificData),
body: JSON.stringify({
model: "mimo-v2.5-pro",
messages: [{ role: "user", content: "test" }],
max_tokens: 1,
}),
});
const res = await validationWrite(
chatUrl,
{
method: "POST",
headers: buildBearerHeaders(apiKey, providerSpecificData),
body: JSON.stringify({
model: "mimo-v2.5-pro",
messages: [{ role: "user", content: "test" }],
max_tokens: 1,
}),
},
isLocal
);
if (res.status === 401 || res.status === 403) {
return { valid: false, error: "Invalid API key" };
}
@@ -3254,7 +3342,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
id,
({ apiKey, providerSpecificData }: any) => {
const { url, init } = configFn(apiKey, providerSpecificData);
return validateSearchProvider(url, init, providerSpecificData);
return validateSearchProvider(url, init, providerSpecificData, isLocal);
},
])
),
@@ -3278,6 +3366,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
providerSpecificData,
modelId: "local-model",
modelsUrl: addModelsSuffix(providerSpecificData?.baseUrl || ""),
isLocal,
});
}
return { valid: false, error: "Provider validation not supported", unsupported: true };
@@ -3294,12 +3383,13 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
try {
if (OPENAI_LIKE_FORMATS.has(entry.format)) {
return await validateOpenAILikeProvider({
provider,
apiKey,
baseUrl,
headers: entry.headers || {},
providerSpecificData,
modelId,
modelsUrl: entry.modelsUrl,
isLocal,
});
}
@@ -3321,6 +3411,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
modelId,
headers: requestHeaders,
providerSpecificData,
isLocal,
});
}
@@ -3330,6 +3421,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
baseUrl,
providerSpecificData,
authType: entry.authType,
isLocal,
});
}

View File

@@ -938,6 +938,56 @@ export const APIKEY_PROVIDERS = {
freeNote: "Free GPT-5, o-series, DeepSeek-R1, Llama 4, Grok 3 — GitHub account only.",
authHint: "Create a GitHub PAT with 'models: read' scope at github.com/settings/tokens",
},
haiper: {
id: "haiper",
alias: "hp",
name: "Haiper",
icon: "videocam",
color: "#6366F1",
textIcon: "HP",
website: "https://haiper.ai",
authHint: "Get API key at haiper.ai/haiper-api",
},
leonardo: {
id: "leonardo",
alias: "leo",
name: "Leonardo AI",
icon: "palette",
color: "#8B5CF6",
textIcon: "LE",
website: "https://leonardo.ai",
authHint: "Get API key at leonardo.ai/developer",
},
ideogram: {
id: "ideogram",
alias: "ideo",
name: "Ideogram",
icon: "image",
color: "#EC4899",
textIcon: "ID",
website: "https://ideogram.ai",
authHint: "Get API key at ideogram.ai/docs/api",
},
suno: {
id: "suno",
alias: "suno",
name: "Suno",
icon: "music_note",
color: "#F59E0B",
textIcon: "SU",
website: "https://suno.ai",
authHint: "Paste session cookie from suno.ai (Clerk auth)",
},
udio: {
id: "udio",
alias: "udio",
name: "Udio",
icon: "music_note",
color: "#10B981",
textIcon: "UD",
website: "https://udio.com",
authHint: "Paste session cookie from udio.com (Supabase auth)",
},
"cloudflare-ai": {
id: "cloudflare-ai",
alias: "cf",
@@ -1667,6 +1717,8 @@ export const VIDEO_PROVIDER_IDS = new Set([
"minimax",
"together",
"replicate",
"haiper",
"leonardo",
]);
export const EMBEDDING_RERANK_PROVIDER_IDS = new Set(["voyage-ai", "jina-ai"]);

View File

@@ -1,28 +1,46 @@
import Redis from "ioredis";
// Reuse existing REDIS_URL if set, or local redis via default docker-compose
// Use REDIS_URL from env (Docker/Production) or fallback to local redis
const REDIS_URL = process.env.REDIS_URL || "redis://localhost:6379";
if (process.env.NODE_ENV === "production" && !process.env.REDIS_URL) {
console.warn("[REDIS] REDIS_URL is not set in production. Falling back to default.");
}
// Issue #2357: When OmniRoute runs in Docker without a sibling Redis
// container (the default `docker run` / portainer one-click install), every
// rate-limit lookup hits `redis://localhost:6379` inside the container and
// spams `[REDIS] Error: connect ECONNREFUSED 127.0.0.1:6379`. Rate limiting
// is non-essential for a single-instance deployment, so we now:
//
// 1) Treat `REDIS_URL` as opt-in. If it's not set we silently fall back to
// the in-memory store (same code path used by unit tests).
// 2) Even when set, errors degrade gracefully: a single startup warning,
// then suppress per-request error spam after the first occurrence.
const REDIS_URL = process.env.REDIS_URL;
const REDIS_ENABLED = Boolean(REDIS_URL);
let redisClient: Redis | null = null;
let redisErrorLogged = false;
export function getRedisClient() {
export function getRedisClient(): Redis | null {
if (!REDIS_ENABLED) return null;
if (!redisClient) {
redisClient = new Redis(REDIS_URL, {
redisClient = new Redis(REDIS_URL as string, {
maxRetriesPerRequest: 3,
enableReadyCheck: false,
lazyConnect: false,
retryStrategy(times) {
return Math.min(times * 50, 2000); // Exponential backoff
},
});
redisClient.on("error", (err) => console.error("[REDIS] Error:", err.message));
redisClient.on("error", (err) => {
if (!redisErrorLogged) {
console.warn("[REDIS] Connection error — rate limiter degraded to in-memory:", err.message);
redisErrorLogged = true;
}
});
}
return redisClient;
}
export function isRedisEnabled(): boolean {
return REDIS_ENABLED;
}
export interface RateLimitRule {
limit: number;
window: number; // in seconds
@@ -86,37 +104,48 @@ export function setRateLimiterTestMode(enabled: boolean) {
/**
* Checks multi-window rate limits for an API key atomically via Redis.
*/
function checkRateLimitInMemory(keyId: string, rules: RateLimitRule[]): RateLimitResult {
const now = Math.floor(Date.now() / 1000);
for (const rule of rules) {
const currentWindow = Math.floor(now / rule.window);
const windowKey = `rl:api_key:${keyId}:${rule.window}:${currentWindow}`;
const count = TEST_MEMORY_STORE.get(windowKey) || 0;
if (count >= rule.limit) {
return { allowed: false, failedWindow: rule.window };
}
}
for (const rule of rules) {
const currentWindow = Math.floor(now / rule.window);
const windowKey = `rl:api_key:${keyId}:${rule.window}:${currentWindow}`;
TEST_MEMORY_STORE.set(windowKey, (TEST_MEMORY_STORE.get(windowKey) || 0) + 1);
}
return { allowed: true };
}
export async function checkRateLimit(
keyId: string,
rules: RateLimitRule[]
): Promise<RateLimitResult> {
if (!rules || rules.length === 0) return { allowed: true };
// ── In-memory mock for unit tests ──
// ── In-memory path for unit tests AND single-instance deployments ──
// Issue #2357: when REDIS_URL is unset we used to hammer
// localhost:6379 and surface a stream of ECONNREFUSED errors. Now the
// in-memory fallback handles single-instance setups silently. The
// explicit test-mode flag still wins so suites can opt-in even with
// REDIS_URL set.
const isTestMode =
explicitTestMode ||
process.env.NODE_ENV === "test" ||
process.env.DISABLE_SQLITE_AUTO_BACKUP === "true";
if (isTestMode) {
const now = Math.floor(Date.now() / 1000);
for (const rule of rules) {
const currentWindow = Math.floor(now / rule.window);
const windowKey = `rl:api_key:${keyId}:${rule.window}:${currentWindow}`;
const count = TEST_MEMORY_STORE.get(windowKey) || 0;
if (count >= rule.limit) {
return { allowed: false, failedWindow: rule.window };
}
}
for (const rule of rules) {
const currentWindow = Math.floor(now / rule.window);
const windowKey = `rl:api_key:${keyId}:${rule.window}:${currentWindow}`;
TEST_MEMORY_STORE.set(windowKey, (TEST_MEMORY_STORE.get(windowKey) || 0) + 1);
}
return { allowed: true };
if (isTestMode || !isRedisEnabled()) {
return checkRateLimitInMemory(keyId, rules);
}
const redis = getRedisClient();
if (!redis) return checkRateLimitInMemory(keyId, rules);
const args: (string | number)[] = [Math.floor(Date.now() / 1000)];
for (const rule of rules) {
@@ -135,8 +164,16 @@ export async function checkRateLimit(
return { allowed: true };
} catch (error) {
// Fail-open strategy if Redis goes down to prevent complete API outage
console.error("[RATE_LIMITER] Redis eval failed, bypassing rate limit:", error);
// Fail-open strategy if Redis goes down to prevent complete API outage.
// First failure already logged in the connection error handler — keep
// per-request output to a debug line to avoid log spam.
if (!redisErrorLogged) {
console.warn(
"[RATE_LIMITER] Redis eval failed, bypassing rate limit:",
(error as Error)?.message ?? String(error)
);
redisErrorLogged = true;
}
return { allowed: true };
}
}

View File

@@ -0,0 +1,59 @@
/**
* Issue #2359 — defensive null/non-string guards on `target.modelStr`.
*
* Combo entries with a malformed `modelStr` (regression after #2338 added
* per-account LKGP routing) were crashing the combo dispatch path with
* `TypeError: e.startsWith is not a function`. Add guards at the two
* boundaries that consume `target.modelStr` and make sure neither throws
* on a missing/non-string value.
*/
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const COMBO_SRC = path.resolve(__dirname, "../../open-sse/services/combo.ts");
const TEST_ROUTE_SRC = path.resolve(__dirname, "../../src/app/api/combos/test/route.ts");
test("#2359 combo.ts LKGP findIndex guards modelStr against non-string", () => {
const src = fs.readFileSync(COMBO_SRC, "utf8");
// The findIndex on orderedTargets must check `typeof target.modelStr === "string"`
// before calling .startsWith. Anchor on the LKGP fallback branch.
assert.ok(
/typeof target\.modelStr === "string"[\s\S]{0,80}target\.modelStr\.startsWith/.test(src),
"LKGP fallback in combo.ts must type-check target.modelStr before calling .startsWith"
);
});
test("#2359 combo test route falls back instead of throwing on missing modelStr", () => {
const src = fs.readFileSync(TEST_ROUTE_SRC, "utf8");
// We expect the coerced local `modelStr` binding and a graceful early
// return when the combo step is malformed.
assert.ok(
/typeof target\?\.modelStr === "string"/.test(src),
"testComboTarget must coerce target.modelStr before lowercasing"
);
assert.ok(
/Combo step is missing a model id/i.test(src),
"testComboTarget must surface a helpful error on missing modelStr"
);
});
test("#2359 combo.ts has no remaining unguarded target.modelStr.<method> usages", () => {
const src = fs.readFileSync(COMBO_SRC, "utf8");
// Strip the line that contains the guard so the regex below only catches
// direct, unguarded method calls.
const stripped = src.replace(/typeof target\.modelStr === "string"[^\n]*\n[^\n]*/g, "");
// Any `target.modelStr.<method>(` call that survives the strip means
// there's still a code path that could explode on a non-string value.
const RE =
/target\.modelStr\.(?:startsWith|endsWith|includes|toLowerCase|toUpperCase|slice|trim)\b/;
assert.ok(
!RE.test(stripped),
`Found an unguarded target.modelStr.<method> call. ` +
`Audit the LKGP / sortTargets paths and add a typeof guard before calling string methods.`
);
});

View File

@@ -0,0 +1,109 @@
import test from "node:test";
import assert from "node:assert/strict";
// ─── Provider Registrations ─────────────────────────────────────────────────
test("haiper provider is registered", async () => {
const { APIKEY_PROVIDERS } = await import("../../src/shared/constants/providers.ts");
assert.ok(APIKEY_PROVIDERS.haiper, "haiper should be in APIKEY_PROVIDERS");
assert.equal(APIKEY_PROVIDERS.haiper.id, "haiper");
assert.equal(APIKEY_PROVIDERS.haiper.alias, "hp");
});
test("leonardo provider is registered", async () => {
const { APIKEY_PROVIDERS } = await import("../../src/shared/constants/providers.ts");
assert.ok(APIKEY_PROVIDERS.leonardo, "leonardo should be in APIKEY_PROVIDERS");
assert.equal(APIKEY_PROVIDERS.leonardo.id, "leonardo");
});
test("ideogram provider is registered", async () => {
const { APIKEY_PROVIDERS } = await import("../../src/shared/constants/providers.ts");
assert.ok(APIKEY_PROVIDERS.ideogram, "ideogram should be in APIKEY_PROVIDERS");
assert.equal(APIKEY_PROVIDERS.ideogram.id, "ideogram");
});
test("suno provider is registered", async () => {
const { APIKEY_PROVIDERS } = await import("../../src/shared/constants/providers.ts");
assert.ok(APIKEY_PROVIDERS.suno, "suno should be in APIKEY_PROVIDERS");
assert.equal(APIKEY_PROVIDERS.suno.id, "suno");
});
test("udio provider is registered", async () => {
const { APIKEY_PROVIDERS } = await import("../../src/shared/constants/providers.ts");
assert.ok(APIKEY_PROVIDERS.udio, "udio should be in APIKEY_PROVIDERS");
assert.equal(APIKEY_PROVIDERS.udio.id, "udio");
});
// ─── VIDEO_PROVIDER_IDS ─────────────────────────────────────────────────────
test("VIDEO_PROVIDER_IDS includes haiper and leonardo", async () => {
const { VIDEO_PROVIDER_IDS } = await import("../../src/shared/constants/providers.ts");
assert.ok(VIDEO_PROVIDER_IDS.has("haiper"), "haiper should be in VIDEO_PROVIDER_IDS");
assert.ok(VIDEO_PROVIDER_IDS.has("leonardo"), "leonardo should be in VIDEO_PROVIDER_IDS");
});
// ─── Video Registry ─────────────────────────────────────────────────────────
test("haiper video provider is in video registry", async () => {
const { VIDEO_PROVIDERS } = await import("../../open-sse/config/videoRegistry.ts");
assert.ok(VIDEO_PROVIDERS.haiper, "haiper should be in VIDEO_PROVIDERS");
assert.equal(VIDEO_PROVIDERS.haiper.format, "haiper-video");
assert.ok(VIDEO_PROVIDERS.haiper.models.length > 0);
});
test("leonardo video provider is in video registry", async () => {
const { VIDEO_PROVIDERS } = await import("../../open-sse/config/videoRegistry.ts");
assert.ok(VIDEO_PROVIDERS.leonardo, "leonardo should be in VIDEO_PROVIDERS");
assert.equal(VIDEO_PROVIDERS.leonardo.format, "leonardo-video");
});
// ─── Image Registry ─────────────────────────────────────────────────────────
test("haiper image provider is in image registry", async () => {
const { IMAGE_PROVIDERS } = await import("../../open-sse/config/imageRegistry.ts");
assert.ok(IMAGE_PROVIDERS.haiper, "haiper should be in IMAGE_PROVIDERS");
assert.equal(IMAGE_PROVIDERS.haiper.format, "haiper-image");
});
test("leonardo image provider is in image registry", async () => {
const { IMAGE_PROVIDERS } = await import("../../open-sse/config/imageRegistry.ts");
assert.ok(IMAGE_PROVIDERS.leonardo, "leonardo should be in IMAGE_PROVIDERS");
assert.equal(IMAGE_PROVIDERS.leonardo.format, "leonardo-image");
});
test("ideogram image provider is in image registry", async () => {
const { IMAGE_PROVIDERS } = await import("../../open-sse/config/imageRegistry.ts");
assert.ok(IMAGE_PROVIDERS.ideogram, "ideogram should be in IMAGE_PROVIDERS");
assert.equal(IMAGE_PROVIDERS.ideogram.format, "ideogram-image");
});
// ─── Music Registry ─────────────────────────────────────────────────────────
test("suno music provider is in music registry", async () => {
const { MUSIC_PROVIDERS } = await import("../../open-sse/config/musicRegistry.ts");
assert.ok(MUSIC_PROVIDERS.suno, "suno should be in MUSIC_PROVIDERS");
assert.equal(MUSIC_PROVIDERS.suno.format, "suno-music");
});
test("udio music provider is in music registry", async () => {
const { MUSIC_PROVIDERS } = await import("../../open-sse/config/musicRegistry.ts");
assert.ok(MUSIC_PROVIDERS.udio, "udio should be in MUSIC_PROVIDERS");
assert.equal(MUSIC_PROVIDERS.udio.format, "udio-music");
});
// ─── Handler Functions Exist ─────────────────────────────────────────────────
test("videoGeneration handler has haiper-video dispatch", async () => {
const mod = await import("../../open-sse/handlers/videoGeneration.ts");
assert.equal(typeof mod.handleVideoGeneration, "function");
});
test("imageGeneration handler has ideogram-image dispatch", async () => {
const mod = await import("../../open-sse/handlers/imageGeneration.ts");
assert.equal(typeof mod.handleImageGeneration, "function");
});
test("musicGeneration handler has suno-music dispatch", async () => {
const mod = await import("../../open-sse/handlers/musicGeneration.ts");
assert.equal(typeof mod.handleMusicGeneration, "function");
});

View File

@@ -0,0 +1,46 @@
/**
* Issues #2361 + #2360 — Registry entries for LLM7.io and Cohere.
*
* #2361: `llm7` was visible in the dashboard provider catalog (entry in
* `src/shared/constants/providers.ts`) but missing from the executor
* registry, so every connection attempt failed at the test step with a
* credential error. Verify the executor entry now exists with the public
* v1 base URL.
*
* #2360: Cohere was pointed at the native `/v2/chat` endpoint which
* returns the Cohere-proprietary shape — the combo validator could not
* extract any text and surfaced "Provider returned HTTP 200 but no text
* content." Verify the registry now uses the OpenAI-compatible
* compatibility layer.
*/
import test from "node:test";
import assert from "node:assert/strict";
const { REGISTRY } = await import("../../open-sse/config/providerRegistry.ts");
test("#2361 llm7 is registered with the OpenAI-compatible v1 endpoint", () => {
const entry = (REGISTRY as Record<string, Record<string, unknown>>).llm7;
assert.ok(entry, "llm7 should be present in the executor registry");
assert.equal(entry.format, "openai");
assert.equal(entry.baseUrl, "https://api.llm7.io/v1/chat/completions");
assert.equal(entry.authType, "apikey");
assert.equal(entry.authHeader, "bearer");
assert.ok(Array.isArray(entry.models), "llm7 must expose a model catalogue");
});
test("#2360 cohere routes via the OpenAI-compatible compatibility layer", () => {
const entry = (REGISTRY as Record<string, Record<string, unknown>>).cohere;
assert.ok(entry, "cohere should be present in the executor registry");
assert.equal(entry.format, "openai");
// Must be the compatibility endpoint, NOT the native /v2/chat one
// (which returns the proprietary shape the combo validator cannot read).
assert.ok(
typeof entry.baseUrl === "string" &&
entry.baseUrl.includes("/compatibility/v1/chat/completions"),
`cohere baseUrl must use the OpenAI-compatible compatibility layer, got: ${entry.baseUrl}`
);
assert.ok(
typeof entry.modelsUrl === "string" && entry.modelsUrl.includes("/compatibility/v1/models"),
"cohere modelsUrl must use the compatibility endpoint so /v1/models import works"
);
});

View File

@@ -0,0 +1,79 @@
/**
* Issue #2357 — Redis is optional. When `REDIS_URL` is unset, the rate
* limiter must fall back to the in-memory store silently instead of
* spamming `connect ECONNREFUSED 127.0.0.1:6379` for every request.
*
* `ioredis` has a packaging quirk (`@ioredis/commands/built/commands.json`
* is actually JS, not JSON) that prevents `node:test` from importing it
* cleanly, so we verify the contract at the source level instead.
*/
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const RATE_LIMITER_SRC = path.resolve(__dirname, "../../src/shared/utils/rateLimiter.ts");
const src = fs.readFileSync(RATE_LIMITER_SRC, "utf8");
test("#2357 REDIS_URL no longer falls back to localhost:6379 silently", () => {
// The old code was `process.env.REDIS_URL || "redis://localhost:6379"`,
// which made Redis effectively required and produced ECONNREFUSED spam
// when no sibling Redis container existed. The fix gates Redis on the
// explicit env var.
assert.ok(
!/process\.env\.REDIS_URL\s*\|\|\s*"redis:\/\/localhost:6379"/.test(src),
"rateLimiter must not default REDIS_URL to localhost (Redis is optional)"
);
assert.ok(
/REDIS_ENABLED\s*=\s*Boolean\(REDIS_URL\)/.test(src),
"rateLimiter must expose REDIS_ENABLED gated on the env var"
);
});
test("#2357 getRedisClient returns null when REDIS_URL is not set", () => {
// The function must short-circuit instead of constructing a client that
// would spin retrying against localhost:6379.
assert.ok(
/export function getRedisClient\(\)[\s\S]{0,200}if \(!REDIS_ENABLED\) return null/.test(src),
"getRedisClient must return null when REDIS_ENABLED is false"
);
});
test("#2357 checkRateLimit takes the in-memory branch when REDIS_URL is unset", () => {
// Look for the new `isTestMode || !isRedisEnabled()` guard. This is the
// line that routes the request to the in-memory store on docker
// installations without a Redis sidecar.
assert.ok(
/isTestMode\s*\|\|\s*!isRedisEnabled\(\)/.test(src),
"checkRateLimit must route to the in-memory fallback when Redis is disabled"
);
});
test("#2357 connection errors only log once instead of per-request spam", () => {
// The error handler used to be `console.error("[REDIS] Error:", err.message)`
// fired on every reconnect attempt. The new behavior wraps it with a
// `redisErrorLogged` latch so production logs do not get flooded.
assert.ok(
/redisErrorLogged/.test(src),
"Redis error handler must dedupe so docker logs do not flood with ECONNREFUSED"
);
});
test("#2357 RATE_LIMITER eval failure also dedupes its warn", () => {
// The fail-open `console.error` call on Redis eval failure used to fire
// on every request. The new path reuses the same latch so a single warn
// is emitted even under sustained Redis outage. Anchor the search on the
// catch handler that surrounds the RATE_LIMITER warn so we exercise the
// exact code path that produced the user's log flood.
const fenceIdx = src.indexOf("RATE_LIMITER");
assert.ok(fenceIdx > 0, "RATE_LIMITER fail-open warn should exist");
const windowStart = Math.max(0, fenceIdx - 200);
const windowEnd = Math.min(src.length, fenceIdx + 200);
const window = src.slice(windowStart, windowEnd);
assert.ok(
/if \(!redisErrorLogged\)/.test(window),
"Rate limiter eval failure must check the dedup latch before logging"
);
});