From 06b34cc12ca62e71f0004fd33541ec057864d75d Mon Sep 17 00:00:00 2001 From: diegosouzapw Date: Mon, 18 May 2026 10:55:33 -0300 Subject: [PATCH 01/12] fix(providers): register llm7 + route Cohere via compatibility layer (#2361 #2360) Two related provider-registry fixes: #2361: LLM7.io was visible in the dashboard provider catalog (entry in src/shared/constants/providers.ts) but the executor registry in open-sse/config/providerRegistry.ts had no llm7 entry. Every connection test fell through with a credential error because there was no baseUrl or authType configured. Added the standard OpenAI-compatible v1 endpoint with a small seed model catalogue. #2360: Cohere was pointed at the native /v2/chat upstream which returns the proprietary { message: { content: [{type:'text', text:...}] } } shape. The combo test validator (extractComboTestResponseText) only reads the OpenAI choices[] envelope, so a successful Cohere call surfaced as 'Provider returned HTTP 200 but no text content.' Cohere publishes an OpenAI-compatible compatibility layer at /compatibility/v1 that returns { choices: [{ message: { content }}] } so we route there instead of needing a Cohere-specific translator. Regression test asserts both registry entries match the expected shape so a future refactor that reverts the URLs fails loudly. --- open-sse/config/providerRegistry.ts | 35 +++++++++++++- .../provider-registry-llm7-cohere.test.ts | 46 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 tests/unit/provider-registry-llm7-cohere.test.ts diff --git a/open-sse/config/providerRegistry.ts b/open-sse/config/providerRegistry.ts index f1a7101f6b..90490194c6 100644 --- a/open-sse/config/providerRegistry.ts +++ b/open-sse/config/providerRegistry.ts @@ -2288,7 +2288,16 @@ export const REGISTRY: Record = { 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 +3129,30 @@ export const REGISTRY: Record = { 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", diff --git a/tests/unit/provider-registry-llm7-cohere.test.ts b/tests/unit/provider-registry-llm7-cohere.test.ts new file mode 100644 index 0000000000..04e20a0360 --- /dev/null +++ b/tests/unit/provider-registry-llm7-cohere.test.ts @@ -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>).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>).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" + ); +}); From f2e368830a5c6898c13ed4a4d422e47f94258792 Mon Sep 17 00:00:00 2001 From: diegosouzapw Date: Mon, 18 May 2026 10:56:27 -0300 Subject: [PATCH 02/12] fix(combo): guard target.modelStr against non-string before .startsWith (#2359) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Combo dispatch and the combo test button used to crash with 'TypeError: e.startsWith is not a function' when a step's modelStr failed to resolve (regression after #2338 added per-account LKGP routing for local/Docker providers). The TypeScript annotation says the field is always a string, but malformed combo rows leaked through to the dispatch path. Two defensive boundary guards: 1. open-sse/services/combo.ts LKGP fallback findIndex — type-check target.modelStr before calling startsWith. 2. src/app/api/combos/test/route.ts testComboTarget — coerce target.modelStr at the entry, surface a clean 'Combo step is missing a model id' error instead of crashing. Regression test parses the source and asserts no unguarded target.modelStr. usages remain in combo.ts, so a future refactor that reintroduces the pattern fails loudly. --- open-sse/services/combo.ts | 12 +++- src/app/api/combos/test/route.ts | 21 +++++-- .../combo-target-defensive-modelstr.test.ts | 59 +++++++++++++++++++ 3 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 tests/unit/combo-target-defensive-modelstr.test.ts diff --git a/open-sse/services/combo.ts b/open-sse/services/combo.ts index 40fd49c3a4..70481b0eab 100644 --- a/open-sse/services/combo.ts +++ b/open-sse/services/combo.ts @@ -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}/`)) ); } diff --git a/src/app/api/combos/test/route.ts b/src/app/api/combos/test/route.ts index 7aa613e519..7b37cfcd86 100644 --- a/src/app/api/combos/test/route.ts +++ b/src/app/api/combos/test/route.ts @@ -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); diff --git a/tests/unit/combo-target-defensive-modelstr.test.ts b/tests/unit/combo-target-defensive-modelstr.test.ts new file mode 100644 index 0000000000..f26b86c8cc --- /dev/null +++ b/tests/unit/combo-target-defensive-modelstr.test.ts @@ -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. 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.(` 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. call. ` + + `Audit the LKGP / sortTargets paths and add a typeof guard before calling string methods.` + ); +}); From b17fd87470e400e231ce450a878a61b6e2d7da1f Mon Sep 17 00:00:00 2001 From: diegosouzapw Date: Mon, 18 May 2026 10:57:23 -0300 Subject: [PATCH 03/12] fix(rate-limiter): Redis is now opt-in via REDIS_URL (#2357) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Docker images launched without a sibling Redis container used to spam '[REDIS] Error: connect ECONNREFUSED 127.0.0.1:6379' for every rate-limit and API-key-auth cache lookup. The root cause was a default of process.env.REDIS_URL || 'redis://localhost:6379' that turned the opt-in cache into a hard dependency. Three coordinated changes: 1. src/shared/utils/rateLimiter.ts — gate Redis on REDIS_URL being explicitly set. getRedisClient() returns null when disabled; the single connection-error handler dedupes via a redisErrorLogged latch so a sustained outage produces one warn instead of per-request flood. checkRateLimit() routes to the existing in-memory store on both the 'disabled' and 'test' paths. 2. src/lib/db/apiKeys.ts — short-circuit Redis-backed auth cache reads and writes when getRedisClient() returns null. SQLite remains authoritative; the cache is purely an optimization. 3. Same file — replace the wildcard scope matcher's dynamic RegExp compilation with a deterministic segment walker. Eliminates the ReDoS surface on operator-supplied scope patterns and silences the Semgrep js/regex-injection advisory that previously blocked edits to this file. Single-instance deployments now work silently out of the box; multi-instance setups continue to use Redis when REDIS_URL is set. --- src/lib/db/apiKeys.ts | 78 ++++++++++++---- src/shared/utils/rateLimiter.ts | 93 +++++++++++++------ .../unit/rate-limiter-redis-optional.test.ts | 79 ++++++++++++++++ 3 files changed, 203 insertions(+), 47 deletions(-) create mode 100644 tests/unit/rate-limiter-redis-optional.test.ts diff --git a/src/lib/db/apiKeys.ts b/src/lib/db/apiKeys.ts index 406fa0d133..f056550f88 100644 --- a/src/lib/db/apiKeys.ts +++ b/src/lib/db/apiKeys.ts @@ -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(); +// 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 { 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(cache: Map) { } /** - * 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()`). + * + * 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` 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(); } /** diff --git a/src/shared/utils/rateLimiter.ts b/src/shared/utils/rateLimiter.ts index e736c3b414..2ea7030b7e 100644 --- a/src/shared/utils/rateLimiter.ts +++ b/src/shared/utils/rateLimiter.ts @@ -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 { 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 }; } } diff --git a/tests/unit/rate-limiter-redis-optional.test.ts b/tests/unit/rate-limiter-redis-optional.test.ts new file mode 100644 index 0000000000..2bdb7478bf --- /dev/null +++ b/tests/unit/rate-limiter-redis-optional.test.ts @@ -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" + ); +}); From 531c9de8caead701b291e9ff196f887adbcb23d7 Mon Sep 17 00:00:00 2001 From: diegosouzapw Date: Mon, 18 May 2026 10:58:30 -0300 Subject: [PATCH 04/12] docs(changelog): document #2357 #2359 #2360 #2361 fixes --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0edab2f11..bee8fe83c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) From e50126e63956fb0c5e45a22317d625edfb695e52 Mon Sep 17 00:00:00 2001 From: Mourad Maatoug Date: Mon, 18 May 2026 16:25:31 +0200 Subject: [PATCH 05/12] feat(@omniroute/opencode-provider): expand config helpers, MCP entry, live model fetch, combo builder - T1: model/small_model top-level keys in buildOmniRouteOpenCodeConfig - T2: mergeIntoExistingConfig() non-destructive provider merge - T3: createOmniRouteMCPEntry() + OMNIROUTE_MCP_DEFAULT_SCOPES (7 read scopes) - T4: fetchLiveModels() async helper, plain fetch, camelCase+snake_case normalisation (field-variant logic adapted from Alph4d0g/opencode-omniroute-auth, MIT) - T5: listCombos() hits GET /api/combos, normalises compressionOverride - T6: createOmniRouteComboConfig() typed POST/PATCH payload builder - T7: OMNIROUTE_DEFAULT_OPENCODE_MODELS expanded to 7 (added cc/ prefix models) - T8: CI workflow path-filtered on @omniroute/opencode-provider/**, Node 20/22/24 - 32 tests (was 12), 0 failures --- .github/workflows/opencode-provider-ci.yml | 61 +++ @omniroute/opencode-provider/src/index.ts | 444 +++++++++++++++++- .../opencode-provider/tests/index.test.ts | 270 +++++++++++ 3 files changed, 771 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/opencode-provider-ci.yml diff --git a/.github/workflows/opencode-provider-ci.yml b/.github/workflows/opencode-provider-ci.yml new file mode 100644 index 0000000000..1b09274ddd --- /dev/null +++ b/.github/workflows/opencode-provider-ci.yml @@ -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 diff --git a/@omniroute/opencode-provider/src/index.ts b/@omniroute/opencode-provider/src/index.ts index 92fac28390..38d060f688 100644 --- a/@omniroute/opencode-provider/src/index.ts +++ b/@omniroute/opencode-provider/src/index.ts @@ -38,10 +38,17 @@ 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", @@ -59,6 +66,16 @@ export interface OmniRouteProviderOptions { models?: readonly string[]; /** Optional human-readable labels keyed by model id. */ modelLabels?: Record; + /** + * Primary model for OpenCode (top-level `model` key). + * Emitted as `"omniroute/"`. When omitted the key is not written. + */ + model?: string; + /** + * Secondary / cheap model for OpenCode (top-level `small_model` key). + * Emitted as `"omniroute/"`. When omitted the key is not written. + */ + smallModel?: string; } export interface OpenCodeProviderEntry { @@ -77,6 +94,10 @@ export interface OpenCodeProviderEntry { 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 +121,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( @@ -145,16 +165,432 @@ 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, + options: OmniRouteProviderOptions +): Record { + const partial = buildOmniRouteOpenCodeConfig(options); + + const merged: Record = { ...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) + : {}; + + 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; +} + +/** + * 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 = { + 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(url: string, apiKey: string, timeoutMs: number): Promise { + const controller = new AbortController(); + const timer = setTimeout(() => controller.abort(), timeoutMs); + + let response: Response; + try { + response = await fetch(url, { + headers: { Authorization: `Bearer ${apiKey}` }, + signal: controller.signal, + }); + } catch (err) { + throw new Error( + `@omniroute/opencode-provider: request to ${url} failed: ${(err as Error).message}` + ); + } finally { + clearTimeout(timer); + } + + if (!response.ok) { + throw new Error(`@omniroute/opencode-provider: received HTTP ${response.status} from ${url}`); + } + + return response.json() as Promise; +} + +/** + * 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 { + const key = requireNonEmpty(apiKey, "apiKey"); + const url = `${normalizeBaseURL(baseURL)}/models`; + + const body = await fetchJSON(url, key, timeoutMs); + + const rawList: unknown[] = Array.isArray(body) + ? body + : 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; + + 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([ + "", + "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 { + const key = requireNonEmpty(managementApiKey, "managementApiKey"); + const base = normalizeBaseURL(baseURL).replace(/\/v1$/, ""); + const url = `${base}/api/combos`; + + const body = await fetchJSON(url, key, timeoutMs); + const rawList: unknown[] = Array.isArray(body) + ? body + : 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; + + 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 { + const name = requireNonEmpty(options.name, "name"); + const strategy = requireNonEmpty(options.strategy, "strategy"); + + const payload: Record = { + name, + strategy, + active: options.active ?? true, + }; + + if (options.compressionOverride !== undefined) { + payload.compressionOverride = options.compressionOverride; + } + + if (options.providers !== undefined && options.providers.length > 0) { + payload.providers = options.providers.filter((p) => typeof p === "string" && p.trim()); + } + + return payload; } export default createOmniRouteProvider; diff --git a/@omniroute/opencode-provider/tests/index.test.ts b/@omniroute/opencode-provider/tests/index.test.ts index d346ff57bb..af94f7faf9 100644 --- a/@omniroute/opencode-provider/tests/index.test.ts +++ b/@omniroute/opencode-provider/tests/index.test.ts @@ -1,11 +1,19 @@ import test from "node:test"; import assert from "node:assert/strict"; +import { createServer } from "node:http"; +import type { Server } from "node:http"; import { buildOmniRouteOpenCodeConfig, + createOmniRouteComboConfig, + createOmniRouteMCPEntry, createOmniRouteProvider, + fetchLiveModels, + listCombos, + mergeIntoExistingConfig, normalizeBaseURL, OMNIROUTE_DEFAULT_OPENCODE_MODELS, + OMNIROUTE_MCP_DEFAULT_SCOPES, OMNIROUTE_PROVIDER_NPM, OPENCODE_CONFIG_SCHEMA, } from "../src/index.ts"; @@ -119,3 +127,265 @@ 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)); + assert.ok("omniroute" in (result.provider as Record)); + assert.deepEqual((result as Record).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).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"); +}); From 0c44185d0dd88c6decae4547b5956690a018417f Mon Sep 17 00:00:00 2001 From: Mourad Maatoug Date: Mon, 18 May 2026 16:43:09 +0200 Subject: [PATCH 06/12] fix(@omniroute/opencode-provider): address gemini-code-assist review - fetchJSON: consolidate all ops inside try, handle non-Error throws, catch JSON parse errors - fetchLiveModels: null-safe data-envelope check - listCombos: null-safe combos-envelope check - createOmniRouteComboConfig: omit providers key when filtered list empty --- @omniroute/opencode-provider/src/index.ts | 31 ++++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/@omniroute/opencode-provider/src/index.ts b/@omniroute/opencode-provider/src/index.ts index 38d060f688..d13b50afd9 100644 --- a/@omniroute/opencode-provider/src/index.ts +++ b/@omniroute/opencode-provider/src/index.ts @@ -330,25 +330,23 @@ async function fetchJSON(url: string, apiKey: string, timeoutMs: number): Pro const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), timeoutMs); - let response: Response; try { - response = await fetch(url, { + 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) { - throw new Error( - `@omniroute/opencode-provider: request to ${url} failed: ${(err as Error).message}` - ); + const message = err instanceof Error ? err.message : String(err); + throw new Error(`@omniroute/opencode-provider: request to ${url} failed: ${message}`); } finally { clearTimeout(timer); } - - if (!response.ok) { - throw new Error(`@omniroute/opencode-provider: received HTTP ${response.status} from ${url}`); - } - - return response.json() as Promise; } /** @@ -403,7 +401,7 @@ export async function fetchLiveModels( const rawList: unknown[] = Array.isArray(body) ? body - : Array.isArray((body as { data?: unknown[] }).data) + : body && typeof body === "object" && Array.isArray((body as { data?: unknown[] }).data) ? ((body as { data: unknown[] }).data as unknown[]) : []; @@ -498,7 +496,7 @@ export async function listCombos( const body = await fetchJSON(url, key, timeoutMs); const rawList: unknown[] = Array.isArray(body) ? body - : Array.isArray((body as { combos?: unknown[] }).combos) + : body && typeof body === "object" && Array.isArray((body as { combos?: unknown[] }).combos) ? ((body as { combos: unknown[] }).combos as unknown[]) : []; @@ -586,8 +584,11 @@ export function createOmniRouteComboConfig( payload.compressionOverride = options.compressionOverride; } - if (options.providers !== undefined && options.providers.length > 0) { - payload.providers = options.providers.filter((p) => typeof p === "string" && p.trim()); + if (options.providers !== undefined) { + const providers = options.providers.filter((p) => typeof p === "string" && p.trim()); + if (providers.length > 0) { + payload.providers = providers; + } } return payload; From 57354ac6d765abd2f50ae6bc103e1ec03c2e190b Mon Sep 17 00:00:00 2001 From: Mourad Maatoug Date: Mon, 18 May 2026 17:06:13 +0200 Subject: [PATCH 07/12] feat(@omniroute/opencode-provider): model capabilities, agent block, mode block (UI helpers) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds three UI-surface helpers on top of T1–T8 in PR #2375: A) Model capability flags - ModelCapabilities interface (label, attachment, reasoning, temperature, tool_call) - OMNIROUTE_DEFAULT_MODEL_CAPABILITIES seeds capabilities for all 7 default model ids - OmniRouteProviderOptions.modelCapabilities merges over defaults per id - createOmniRouteProvider emits capability flags inline in models[id], per OpenCode's ProviderConfig.models schema (snake_case JSON keys, optional) - Label precedence: modelCapabilities[id].label > modelLabels[id] > id B) createOmniRouteAgentBlock - OmniRouteAgentRole + OmniRouteAgentBlockOptions + OpenCodeAgentEntry - Emits Record', temperature?, top_p?, tools?: Record, prompt? }> - Only fields present in OpenCode's AgentConfig schema are emitted - Tools normalized to Record per schema (not string[]) - Roles with empty modelId are skipped C) createOmniRouteModesBlock (deprecated alias) - Same shape as createOmniRouteAgentBlock since OpenCode treats top-level 'mode' block identically to 'agent' (both reference AgentConfig) - Helper kept for back-compat; @deprecated tags steer callers to agent Shared helper buildAgentEntry eliminates duplication between A/B helpers. Schema validation - All emitted keys verified against https://opencode.ai/config.json - Removed initially-considered reasoningEffort + max_tokens fields (not in AgentConfig schema) - tools shape changed from string[] to Record per schema Build hygiene - tsconfig.json narrowed to lib: ['ES2022'] + types: ['node'] (no DOM lib leakage); @types/node added as devDep - Tests: 32 → 45 green (+13 net) - Build: ESM 10.39 KB / CJS 11.01 KB / DTS 18.87 KB --- .../opencode-provider/package-lock.json | 57 ++-- @omniroute/opencode-provider/package.json | 1 + @omniroute/opencode-provider/src/index.ts | 246 +++++++++++++++++- .../opencode-provider/tests/index.test.ts | 176 +++++++++++++ @omniroute/opencode-provider/tsconfig.json | 1 + 5 files changed, 437 insertions(+), 44 deletions(-) diff --git a/@omniroute/opencode-provider/package-lock.json b/@omniroute/opencode-provider/package-lock.json index c7b0102595..ad01c86c67 100644 --- a/@omniroute/opencode-provider/package-lock.json +++ b/@omniroute/opencode-provider/package-lock.json @@ -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" } } } diff --git a/@omniroute/opencode-provider/package.json b/@omniroute/opencode-provider/package.json index 3259f24da7..6df9e352aa 100644 --- a/@omniroute/opencode-provider/package.json +++ b/@omniroute/opencode-provider/package.json @@ -52,6 +52,7 @@ "access": "public" }, "devDependencies": { + "@types/node": "^20.19.41", "tsup": "^8.5.0", "tsx": "^4.20.0", "typescript": "^5.9.0" diff --git a/@omniroute/opencode-provider/src/index.ts b/@omniroute/opencode-provider/src/index.ts index d13b50afd9..2b9981b56f 100644 --- a/@omniroute/opencode-provider/src/index.ts +++ b/@omniroute/opencode-provider/src/index.ts @@ -55,6 +55,56 @@ export const OMNIROUTE_DEFAULT_OPENCODE_MODELS = [ "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 = { + "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; @@ -64,8 +114,14 @@ 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; + /** + * 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; /** * Primary model for OpenCode (top-level `model` key). * Emitted as `"omniroute/"`. When omitted the key is not written. @@ -78,6 +134,15 @@ export interface OmniRouteProviderOptions { 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 { /** Identifier of the OpenCode runtime package that will speak to OmniRoute. */ npm: typeof OMNIROUTE_PROVIDER_NPM; @@ -89,7 +154,7 @@ export interface OpenCodeProviderEntry { apiKey: string; }; /** Model catalog surfaced to OpenCode. */ - models: Record; + models: Record; } export interface OpenCodeConfigDocument { @@ -144,14 +209,28 @@ export function createOmniRouteProvider(options: OmniRouteProviderOptions): Open : [...OMNIROUTE_DEFAULT_OPENCODE_MODELS]; const labels = options.modelLabels ?? {}; - const models: Record = {}; + const overrides = options.modelCapabilities ?? {}; + const models: Record = {}; const seen = new Set(); 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 { @@ -594,4 +673,161 @@ export function createOmniRouteComboConfig( 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; + /** 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; +} + +/** Single entry inside the emitted OpenCode `agent` block. */ +export interface OpenCodeAgentEntry extends OmniRouteRoleOverrides { + /** Always emitted as `"omniroute/"`. */ + model: string; + /** Per OpenCode schema, `Record`. */ + tools?: Record; + /** 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 = {}; + 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` 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 { + const out: Record = {}; + 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; +} + +/** + * 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 { + const out: Record = {}; + 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; diff --git a/@omniroute/opencode-provider/tests/index.test.ts b/@omniroute/opencode-provider/tests/index.test.ts index af94f7faf9..324ae68bfa 100644 --- a/@omniroute/opencode-provider/tests/index.test.ts +++ b/@omniroute/opencode-provider/tests/index.test.ts @@ -5,13 +5,16 @@ 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, @@ -74,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); } }); @@ -389,3 +393,175 @@ test("OMNIROUTE_DEFAULT_OPENCODE_MODELS includes 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 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); +}); diff --git a/@omniroute/opencode-provider/tsconfig.json b/@omniroute/opencode-provider/tsconfig.json index ac1430ae95..05ee599fbc 100644 --- a/@omniroute/opencode-provider/tsconfig.json +++ b/@omniroute/opencode-provider/tsconfig.json @@ -4,6 +4,7 @@ "module": "ESNext", "moduleResolution": "Bundler", "lib": ["ES2022"], + "types": ["node"], "strict": true, "esModuleInterop": true, "skipLibCheck": true, From 55160bc52a593798544149597f7b5f623909d7ee Mon Sep 17 00:00:00 2001 From: oyi77 Date: Mon, 18 May 2026 23:41:27 +0700 Subject: [PATCH 08/12] feat(content): add Haiper, Leonardo, Ideogram, Suno, Udio providers 5 new content generation providers with full handler implementations: Video: haiper (gen2), leonardo (phoenix) Image: haiper (gen2), leonardo (phoenix/sdxl), ideogram (V3/V2A) Music: suno (chirp-v3-5/v4), udio (web wrapper) All async handlers: 5s poll interval, 5 min timeout (60 polls). Auth: API key (haiper/leonardo/ideogram), cookie (suno/udio). Closes #2376 Co-Authored-By: OpenClaude (mimo-v2.5-pro) --- open-sse/config/imageRegistry.ts | 34 +++ open-sse/config/musicRegistry.ts | 21 ++ open-sse/config/providerRegistry.ts | 62 +++++ open-sse/config/videoRegistry.ts | 18 ++ open-sse/handlers/imageGeneration.ts | 325 +++++++++++++++++++++++++++ open-sse/handlers/musicGeneration.ts | 221 ++++++++++++++++++ open-sse/handlers/videoGeneration.ts | 200 +++++++++++++++++ src/shared/constants/providers.ts | 52 ++++- 8 files changed, 932 insertions(+), 1 deletion(-) diff --git a/open-sse/config/imageRegistry.ts b/open-sse/config/imageRegistry.ts index cb964f5745..72fd474609 100644 --- a/open-sse/config/imageRegistry.ts +++ b/open-sse/config/imageRegistry.ts @@ -317,6 +317,40 @@ export const IMAGE_PROVIDERS: Record = { 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", diff --git a/open-sse/config/musicRegistry.ts b/open-sse/config/musicRegistry.ts index 2535a99afb..ab3893dbbb 100644 --- a/open-sse/config/musicRegistry.ts +++ b/open-sse/config/musicRegistry.ts @@ -37,6 +37,27 @@ export const MUSIC_PROVIDERS: Record = { ], }, + 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" }], + }, comfyui: { id: "comfyui", baseUrl: "http://localhost:8188", diff --git a/open-sse/config/providerRegistry.ts b/open-sse/config/providerRegistry.ts index 85f82731e2..390c746776 100644 --- a/open-sse/config/providerRegistry.ts +++ b/open-sse/config/providerRegistry.ts @@ -1941,6 +1941,68 @@ export const REGISTRY: Record = { ], }, + 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: "apikey", + 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", diff --git a/open-sse/config/videoRegistry.ts b/open-sse/config/videoRegistry.ts index efe64754a2..05e1c1a7f2 100644 --- a/open-sse/config/videoRegistry.ts +++ b/open-sse/config/videoRegistry.ts @@ -52,6 +52,24 @@ export const VIDEO_PROVIDERS: Record = { ], }, + 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" }], + }, comfyui: { id: "comfyui", baseUrl: "http://localhost:8188", diff --git a/open-sse/handlers/imageGeneration.ts b/open-sse/handlers/imageGeneration.ts index b0f90c1b93..2291e34819 100644 --- a/open-sse/handlers/imageGeneration.ts +++ b/open-sse/handlers/imageGeneration.ts @@ -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,307 @@ 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: "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); + 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); + 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); + 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; diff --git a/open-sse/handlers/musicGeneration.ts b/open-sse/handlers/musicGeneration.ts index 14fce3df0b..bebfeb703c 100644 --- a/open-sse/handlers/musicGeneration.ts +++ b/open-sse/handlers/musicGeneration.ts @@ -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,217 @@ 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(`https://studio-api.suno.ai/api/feed/?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); + 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); + 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}` }; + } +} diff --git a/open-sse/handlers/videoGeneration.ts b/open-sse/handlers/videoGeneration.ts index bd4fafb75f..301268ac85 100644 --- a/open-sse/handlers/videoGeneration.ts +++ b/open-sse/handlers/videoGeneration.ts @@ -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)); } diff --git a/src/shared/constants/providers.ts b/src/shared/constants/providers.ts index 93db9d9fc0..9d36954c99 100644 --- a/src/shared/constants/providers.ts +++ b/src/shared/constants/providers.ts @@ -912,6 +912,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", @@ -1634,7 +1684,7 @@ export const ENTERPRISE_CLOUD_PROVIDER_IDS = new Set([ "modal", ]); -export const VIDEO_PROVIDER_IDS = new Set(["runwayml"]); +export const VIDEO_PROVIDER_IDS = new Set(["runwayml", "haiper", "leonardo"]); export const EMBEDDING_RERANK_PROVIDER_IDS = new Set(["voyage-ai", "jina-ai"]); From 32b35494259b07ee0e0b5f0bb10873dc5f74b478 Mon Sep 17 00:00:00 2001 From: oyi77 Date: Mon, 18 May 2026 23:49:51 +0700 Subject: [PATCH 09/12] test(content): add unit tests for 5 new content providers 16 tests covering: - Provider registrations (haiper, leonardo, ideogram, suno, udio) - VIDEO_PROVIDER_IDS includes haiper, leonardo - Video registry entries (haiper-video, leonardo-video) - Image registry entries (haiper-image, leonardo-image, ideogram-image) - Music registry entries (suno-music, udio-music) - Handler function exports exist Co-Authored-By: OpenClaude (mimo-v2.5-pro) --- tests/unit/new-content-providers.test.ts | 109 +++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 tests/unit/new-content-providers.test.ts diff --git a/tests/unit/new-content-providers.test.ts b/tests/unit/new-content-providers.test.ts new file mode 100644 index 0000000000..f4adca3f29 --- /dev/null +++ b/tests/unit/new-content-providers.test.ts @@ -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"); +}); From 6401faf9f07ccbb0cfcea7dde1daf9b76375e6a4 Mon Sep 17 00:00:00 2001 From: oyi77 Date: Tue, 19 May 2026 00:12:55 +0700 Subject: [PATCH 10/12] =?UTF-8?q?fix(content):=20address=20Gemini=20review?= =?UTF-8?q?=20=E2=80=94=20error=20checks,=20auth=20consistency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 7 fixes from code review: - Add image download error checks in haiper/leonardo/ideogram handlers - Add audio download error checks in suno/udio handlers - Use providerConfig.statusUrl for suno polling (not hardcoded URL) - Fix suno authType to "cookie" in providerRegistry Co-Authored-By: OpenClaude (mimo-v2.5-pro) --- open-sse/config/providerRegistry.ts | 2 +- open-sse/handlers/imageGeneration.ts | 23 ++++++++++++++++++++++- open-sse/handlers/musicGeneration.ts | 16 +++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/open-sse/config/providerRegistry.ts b/open-sse/config/providerRegistry.ts index 390c746776..883a55d149 100644 --- a/open-sse/config/providerRegistry.ts +++ b/open-sse/config/providerRegistry.ts @@ -1986,7 +1986,7 @@ export const REGISTRY: Record = { format: "openai", executor: "default", baseUrl: "https://studio-api.suno.ai/api/generate/v2/", - authType: "apikey", + authType: "cookie", authHeader: "cookie", models: [ { id: "chirp-v3-5", name: "Chirp V3.5" }, diff --git a/open-sse/handlers/imageGeneration.ts b/open-sse/handlers/imageGeneration.ts index 2291e34819..5dbb3eff02 100644 --- a/open-sse/handlers/imageGeneration.ts +++ b/open-sse/handlers/imageGeneration.ts @@ -3018,7 +3018,7 @@ async function handleHaiperImageGeneration({ const res = await fetch(providerConfig.baseUrl, { method: "POST", headers: { "Content-Type": "application/json", HAIPER_KEY: token }, - body: JSON.stringify({ prompt, aspect_ratio: "16:9" }), + body: JSON.stringify({ prompt, aspect_ratio: body.aspect_ratio || "16:9" }), }); if (!res.ok) { const errorText = await res.text(); @@ -3045,6 +3045,13 @@ async function handleHaiperImageGeneration({ 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", @@ -3166,6 +3173,13 @@ async function handleLeonardoImageGeneration({ 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", @@ -3259,6 +3273,13 @@ async function handleIdeogramImageGeneration({ 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", diff --git a/open-sse/handlers/musicGeneration.ts b/open-sse/handlers/musicGeneration.ts index bebfeb703c..6528f3ec6d 100644 --- a/open-sse/handlers/musicGeneration.ts +++ b/open-sse/handlers/musicGeneration.ts @@ -434,13 +434,20 @@ async function handleSunoMusicGeneration({ const deadline = Date.now() + 300000; while (Date.now() < deadline) { await new Promise((r) => setTimeout(r, 5000)); - const feedRes = await fetch(`https://studio-api.suno.ai/api/feed/?ids=${ids.join(",")}`, { + 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", @@ -545,6 +552,13 @@ async function handleUdioMusicGeneration({ 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", From 5ed96f507233158de3bf932b6cef52a387ce51b1 Mon Sep 17 00:00:00 2001 From: diegosouzapw Date: Mon, 18 May 2026 11:34:38 -0300 Subject: [PATCH 11/12] fix(routing): implement embedding combos, local provider validation bypass, and resolve migration collisions --- open-sse/services/contextManager.ts | 2 +- .../041_session_account_affinity.sql | 11 - ...sql => 058_command_code_auth_sessions.sql} | 0 src/lib/embeddings/service.ts | 39 +- src/lib/providers/validation.ts | 476 ++++++++++-------- 5 files changed, 312 insertions(+), 216 deletions(-) delete mode 100644 src/lib/db/migrations/041_session_account_affinity.sql rename src/lib/db/migrations/{055_command_code_auth_sessions.sql => 058_command_code_auth_sessions.sql} (100%) diff --git a/open-sse/services/contextManager.ts b/open-sse/services/contextManager.ts index ccf3478653..338afa149a 100644 --- a/open-sse/services/contextManager.ts +++ b/open-sse/services/contextManager.ts @@ -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) { diff --git a/src/lib/db/migrations/041_session_account_affinity.sql b/src/lib/db/migrations/041_session_account_affinity.sql deleted file mode 100644 index 5a93707fb3..0000000000 --- a/src/lib/db/migrations/041_session_account_affinity.sql +++ /dev/null @@ -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); diff --git a/src/lib/db/migrations/055_command_code_auth_sessions.sql b/src/lib/db/migrations/058_command_code_auth_sessions.sql similarity index 100% rename from src/lib/db/migrations/055_command_code_auth_sessions.sql rename to src/lib/db/migrations/058_command_code_auth_sessions.sql diff --git a/src/lib/embeddings/service.ts b/src/lib/embeddings/service.ts index 41d8a0f1c6..99db52255d 100644 --- a/src/lib/embeddings/service.ts +++ b/src/lib/embeddings/service.ts @@ -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 & { model: string }; @@ -30,6 +31,42 @@ export async function createEmbeddingResponse( body: ValidatedEmbeddingBody, options: EmbeddingHandlerOptions = {} ): Promise { + 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[] = []; try { const nodes = (await getProviderNodes()) as unknown as EmbeddingProviderNodeRow[]; diff --git a/src/lib/providers/validation.ts b/src/lib/providers/validation.ts index b9e599edcf..64b9bf7a2c 100644 --- a/src/lib/providers/validation.ts +++ b/src/lib/providers/validation.ts @@ -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,106 @@ 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 +608,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 +712,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 +721,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 = { "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 +2175,7 @@ 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 +2198,8 @@ async function validateAnthropicCompatibleProvider({ apiKey, providerSpecificDat { method: "GET", headers, - } + }, + isLocal ); if (modelsRes.ok) { @@ -2184,7 +2226,8 @@ async function validateAnthropicCompatibleProvider({ apiKey, providerSpecificDat max_tokens: 1, messages: [{ role: "user", content: "test" }], }), - } + }, + isLocal ); if (messagesRes.status === 401 || messagesRes.status === 403) { @@ -2280,15 +2323,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 +2872,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 +2941,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 +2971,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 +3107,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 +3126,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 +3176,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi baseUrl, modelId: getBedrockValidationModelId(baseUrl), modelsUrl: buildBedrockModelsUrl(baseUrl), + isLocal, }); }, modal: ({ apiKey, providerSpecificData }: any) => @@ -3121,6 +3186,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, @@ -3168,7 +3234,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi method: "POST", headers: buildBearerHeaders(apiKey, providerSpecificData), body: "{}", - }); + }, isLocal); if (res.status === 401) { return { valid: false, error: "Invalid API key" }; } @@ -3182,7 +3248,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) { @@ -3211,7 +3277,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi messages: [{ role: "user", content: "test" }], max_tokens: 1, }), - }); + }, isLocal); if (res.status === 401 || res.status === 403) { return { valid: false, error: "Invalid API key" }; } @@ -3238,7 +3304,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi 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 +3320,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 +3344,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 +3361,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 +3389,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi modelId, headers: requestHeaders, providerSpecificData, + isLocal, }); } @@ -3330,6 +3399,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi baseUrl, providerSpecificData, authType: entry.authType, + isLocal, }); } From b7316d56e89cddef9231a1d294d5fd86eacf9e47 Mon Sep 17 00:00:00 2001 From: diegosouzapw Date: Mon, 18 May 2026 11:45:53 -0300 Subject: [PATCH 12/12] style(providers): reformat provider validation calls for readability Wrap long function signatures and validation requests across multiple lines to match project formatting conventions and improve scanability. --- src/lib/embeddings/service.ts | 2 +- src/lib/providers/validation.ts | 72 +++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/lib/embeddings/service.ts b/src/lib/embeddings/service.ts index 99db52255d..e6e9b45b8f 100644 --- a/src/lib/embeddings/service.ts +++ b/src/lib/embeddings/service.ts @@ -41,7 +41,7 @@ export async function createEmbeddingResponse( try { allCombos = await getCombos(); } catch {} - + let settings = {}; try { settings = getDatabaseSettings(); diff --git a/src/lib/providers/validation.ts b/src/lib/providers/validation.ts index 64b9bf7a2c..876eb3933d 100644 --- a/src/lib/providers/validation.ts +++ b/src/lib/providers/validation.ts @@ -382,7 +382,13 @@ async function validateOpenAILikeProvider({ } } -async function validateDirectChatProvider({ url, headers, body, providerSpecificData = {}, isLocal = false }: any) { +async function validateDirectChatProvider({ + url, + headers, + body, + providerSpecificData = {}, + isLocal = false, +}: any) { try { const response = await validationWrite( url, @@ -2175,7 +2181,11 @@ async function validateOpenAICompatibleProvider({ apiKey, providerSpecificData = } } -async function validateAnthropicCompatibleProvider({ apiKey, providerSpecificData = {}, isLocal = false }: 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" }; @@ -3230,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: "{}", - }, isLocal); + 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" }; } @@ -3269,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, - }), - }, isLocal); + 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" }; } @@ -3296,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, - }), - }, isLocal); + 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" }; }