feat(routing): add capability-specific combo routes

This commit is contained in:
Federico Mangini
2026-11-02 11:38:36 +01:00
parent 92b0a2bf02
commit c7e79d04ea
33 changed files with 1536 additions and 219 deletions

View File

@@ -1 +1 @@
- **feat(routing):** accept saved combo names in `model` on `/v1/search` and `/v1/web/fetch` with priority/fill-first fallback. Mixed combinations skip incompatible web/chat targets before credential selection, preserving account pins and caller key scopes. Combo pickers expose native search/fetch operations for providers without LLM models. Direct chat requests for dedicated search/fetch providers return actionable JSON errors. Related to #10274; fixes #17858.
- **feat(routing):** add endpoint-specific combo routes with independent targets, strategies and config for Chat, search/fetch, embeddings, rerank, images, video, music, OCR and audio surfaces. `/v1/search`, `/v1/web/fetch`, `/v1/music/generations`, `/v1/music`, `/v1/sound-generation` and `/v1/ocr` resolve saved combo names while preserving native response contracts, account pins and caller key scopes. Mixed combinations filter incompatible targets before credential selection. Related to #10274; fixes #17858.

View File

@@ -17862,6 +17862,27 @@ components:
- context-optimized
- fusion
default: priority
routes:
type: object
description: >-
Optional endpoint-specific routing plans. The legacy top-level models, strategy and
config remain the Chat plan. A named route overrides them only for its capability.
additionalProperties: false
properties:
chat: { $ref: "#/components/schemas/ComboCapabilityRoute" }
search: { $ref: "#/components/schemas/ComboCapabilityRoute" }
web-fetch: { $ref: "#/components/schemas/ComboCapabilityRoute" }
embeddings: { $ref: "#/components/schemas/ComboCapabilityRoute" }
rerank: { $ref: "#/components/schemas/ComboCapabilityRoute" }
images: { $ref: "#/components/schemas/ComboCapabilityRoute" }
image-edits: { $ref: "#/components/schemas/ComboCapabilityRoute" }
videos: { $ref: "#/components/schemas/ComboCapabilityRoute" }
music: { $ref: "#/components/schemas/ComboCapabilityRoute" }
ocr: { $ref: "#/components/schemas/ComboCapabilityRoute" }
audio-speech: { $ref: "#/components/schemas/ComboCapabilityRoute" }
audio-transcriptions: { $ref: "#/components/schemas/ComboCapabilityRoute" }
audio-translations: { $ref: "#/components/schemas/ComboCapabilityRoute" }
audio-generation: { $ref: "#/components/schemas/ComboCapabilityRoute" }
universal_handoff:
type: object
nullable: true
@@ -17879,3 +17900,26 @@ components:
ttlMinutes: { type: integer, minimum: 1, maximum: 10080 }
preserveSystemPrompt: { type: boolean }
relayMode: { type: string, enum: [schema-locked, standard] }
ComboCapabilityRoute:
type: object
required: [models]
additionalProperties: false
properties:
models:
type: array
minItems: 1
items:
oneOf:
- type: string
description: "provider/model reference"
- type: object
additionalProperties: true
strategy:
type: string
enum:
[priority, fill-first, round-robin, weighted, random, strict-random, least-used, p2c]
default: priority
config:
type: object
additionalProperties: true

View File

@@ -898,8 +898,10 @@ chat skips the dedicated search/fetch providers and considers OpenAI. Compatibil
comes from the search/fetch executor registries, not from words in a model name.
Filtering precedes credential selection and dispatch, and never edits the saved combo.
Web combinations support `priority` and `fill-first`. Other strategies return `400`
rather than silently applying chat-specific synthesis or scoring to web results.
Non-chat capability routes support `priority`, `fill-first`, `round-robin`, `weighted`,
`random`, `strict-random`, `least-used`, and `p2c`. Chat-only synthesis strategies such as
`fusion`, `pipeline`, and `context-relay` are rejected on non-chat routes rather than being
silently applied to search or generated media.
The existing combo engine handles ordered fallback, including upstream quota failures,
and retains step IDs, account pins, per-step account allowlists, nested references,
caller connection/quota scopes and combo access policy. Results retain their native
@@ -912,6 +914,24 @@ provider pool behind the combo's back. Explicit-provider requests retain their e
single-provider behavior. Direct chat requests targeting dedicated search/fetch providers
return an actionable JSON `400` instead of falling through to an OpenAI-style executor.
### Capability-specific combo routes
Saved combos may contain a `routes` object whose keys are API capabilities. Every route owns
its target list, strategy, and runtime config. The top-level `models`, `strategy`, and `config`
remain the legacy-compatible Chat plan; old combos without `routes` continue to work unchanged.
Supported route keys are `chat`, `search`, `web-fetch`, `embeddings`, `rerank`, `images`,
`image-edits`, `videos`, `music`, `ocr`, `audio-speech`, `audio-transcriptions`,
`audio-translations`, and `audio-generation`. Endpoint projection happens before credential
selection, scoring, or dispatch. Nested combo references select the child combo's route for the
same capability.
`POST /v1/music/generations` and `POST /v1/ocr` accept a combo name in `model`. The native
ElevenLabs-compatible `POST /v1/music` also accepts a combo name in `model_id`, but every usable
target on that native endpoint must be an ElevenLabs music model so its binary response contract
is preserved. `POST /v1/sound-generation` accepts an `audio-generation` combo in `model_id` under
the same native-binary constraint. Use `/v1/music/generations` for heterogeneous music providers.
### Exa and Tavily `provider_options`
Exa and Tavily read provider-specific fields from a namespaced bag:

View File

@@ -892,7 +892,12 @@ export function missingAudioProviderCredentialsMessage(
* Get all audio models as a flat list
*/
export function getAllAudioModels() {
const models = [];
const models: Array<{
id: string;
name: string;
provider: string;
subtype: "transcription" | "speech";
}> = [];
for (const [providerId, config] of Object.entries(AUDIO_TRANSCRIPTION_PROVIDERS)) {
for (const model of config.models) {

View File

@@ -49,7 +49,9 @@ function visibleImageProviders(): Record<string, ImageProviderConfig> {
return { ...IMAGE_PROVIDERS, ...XAI_SUBSCRIPTION_IMAGE_PROVIDERS };
}
function resolveImageModelAlias(modelStr) {
type ResolvedImageModelAlias = { provider: string; model: string };
function resolveImageModelAlias(modelStr: string): ResolvedImageModelAlias | null {
const alias = IMAGE_MODEL_ALIASES[modelStr];
return alias ? { provider: alias.provider, model: alias.model } : null;
}
@@ -60,12 +62,15 @@ function resolveImageModelAlias(modelStr) {
// must NOT override an explicit prefix — #9982 removed the unconditional bare
// fallback because `fal-ai/flux-2-max` was being hijacked to black-forest-labs
// by the bare `flux-2-max` alias.
function resolveSameProviderBareAlias(providerId, model) {
function resolveSameProviderBareAlias(
providerId: string,
model: string
): ResolvedImageModelAlias | null {
const aliased = resolveImageModelAlias(model);
return aliased && aliased.provider === providerId ? aliased : null;
}
function findImageModelConfig(providerId, modelId) {
function findImageModelConfig(providerId: string, modelId: string): ImageModelEntry | null {
const provider = visibleImageProviders()[providerId];
if (!provider) return null;
return (
@@ -73,21 +78,24 @@ function findImageModelConfig(providerId, modelId) {
);
}
function resolveImageProviderModelId(providerId, modelId) {
function resolveImageProviderModelId(providerId: string, modelId: string): string {
return findImageModelConfig(providerId, modelId)?.id || modelId;
}
// Kept out of getImageModelEntry() (which sits at the complexity-ratchet cap) — an
// alias can override imageRequired directly, else it falls back to its target
// model's own flag. Consumers coerce the result with Boolean(), so no `?? false`.
function resolveAliasImageRequired(alias, modelConfig) {
function resolveAliasImageRequired(
alias: ImageModelAliasEntry,
modelConfig: ImageModelEntry | null
): boolean | undefined {
return alias.imageRequired ?? modelConfig?.imageRequired;
}
/**
* Get image provider config by ID
*/
export function getImageProvider(providerId) {
export function getImageProvider(providerId: string): ImageProviderConfig | null {
const providers = visibleImageProviders();
if (providers[providerId]) return providers[providerId];
if (!providerId) return null;
@@ -101,7 +109,10 @@ export function getImageProvider(providerId) {
* Parse image model string (format: "provider/model")
* Returns { provider, model }
*/
export function parseImageModel(modelStr) {
export function parseImageModel(modelStr: string | null | undefined): {
provider: string | null;
model: string | null;
} {
if (!modelStr || hasUnsafeModelIdSyntax(modelStr)) return { provider: null, model: null };
const directAlias = resolveImageModelAlias(modelStr);
@@ -208,11 +219,17 @@ export function getImageModelAliases() {
* `/v1/models`, which returns image/diffusion models with no modality field)
* out of the chat listing when they are already known image-only models.
*/
export function isRegisteredImageModel(providerId, modelId) {
export function isRegisteredImageModel(providerId: string, modelId: string): boolean {
return Boolean(findImageModelConfig(providerId, modelId));
}
export function getImageModelEntry(modelStr) {
export function getImageModelEntry(modelStr: string | null | undefined): {
provider: string;
model: string;
inputModalities: string[];
imageRequired?: boolean;
description?: string;
} | null {
if (!modelStr) return null;
const alias = IMAGE_MODEL_ALIASES[modelStr];
@@ -254,7 +271,7 @@ export function isImageOnlyModel(modelStr: string): boolean {
}
/** Image input is mandatory only for edit-only models (`["image"]`, no `"text"`). Dual-modality models also accept pure t2i. */
export function modalitiesRequireImageInput(inputModalities) {
export function modalitiesRequireImageInput(inputModalities: unknown): boolean {
const list = Array.isArray(inputModalities) ? inputModalities : ["text"];
return list.includes("image") && !list.includes("text");
}

View File

@@ -760,12 +760,6 @@ function resolveRegistryDeclaredTimeoutMs(provider: string, modelStr: string): n
* one metadata-only log line for durability across restarts.
*/
export async function handleComboChat(options: HandleComboChatOptions): Promise<Response> {
if (
(options.endpointCapability === "search" || options.endpointCapability === "web-fetch") &&
!["priority", "fill-first"].includes(options.combo.strategy || "priority")
) {
return errorResponse(400, "Web endpoint combos support priority and fill-first strategies");
}
const projected = projectComboForEndpoint(
options.combo,
options.allCombos,

View File

@@ -35,6 +35,8 @@ import {
narrowMediaConnectionScope,
pinnedConnectionIds,
} from "./mediaComboFallback.ts";
import { projectComboForEndpoint } from "../../src/lib/combos/endpointCapabilities.ts";
import { orderCapabilityTargets } from "../../src/lib/combos/capabilityTargetOrder.ts";
/**
* Caller-facing shape of handleImageGeneration(). The handler is untyped and
@@ -230,13 +232,15 @@ async function resolveImageComboTargets(comboName: string, keyScope: ComboKeySco
}
const allCombos = await getCombos();
const targets = resolveComboTargets(combo as never, allCombos as never);
const projected = projectComboForEndpoint(combo as never, allCombos as never, "images");
const targets = resolveComboTargets(projected.combo as never, projected.allCombos as never);
if (!targets || targets.length === 0) {
const message =
Array.isArray(combo.models) && combo.models.length > 0
? `No images-capable targets in combo "${comboName}"`
: `Combo "${comboName}" has no usable targets`;
return {
response: errorResponse(
HTTP_STATUS.BAD_REQUEST,
`Combo "${comboName}" has no usable targets`
),
response: errorResponse(HTTP_STATUS.BAD_REQUEST, message),
};
}
@@ -257,7 +261,16 @@ async function resolveImageComboTargets(comboName: string, keyScope: ComboKeySco
),
};
}
return { targets: scoped.targets, allowedConnections: scoped.allowedConnections };
return {
targets: orderCapabilityTargets({
comboName,
endpoint: "images",
strategy: projected.combo.strategy,
targets: scoped.targets,
}),
allowedConnections: scoped.allowedConnections,
strategy: projected.combo.strategy || "priority",
};
}
/**
@@ -284,7 +297,7 @@ export async function executeImageCombo(
// 1–2. Resolve combo targets, filtered to images-capable ones the key may use
const resolved = await resolveImageComboTargets(comboName, auth.keyScope);
if ("response" in resolved) return resolved.response;
const { targets: imageTargets, allowedConnections } = resolved;
const { targets: imageTargets, allowedConnections, strategy } = resolved;
// 3. Iterate targets in priority order (first healthy target wins).
// The skip / terminal classification lives in the shared runImageComboTargets
@@ -303,7 +316,7 @@ export async function executeImageCombo(
apiKeyInfo: auth.policy?.apiKeyInfo,
credentials,
startTime,
comboStrategy: "priority",
comboStrategy: strategy,
},
() =>
handleImageGeneration({
@@ -342,7 +355,7 @@ export async function executeImageCombo(
costUsd,
latencyMs: Date.now() - startTime,
requestId: generateRequestId(),
strategy: "priority",
strategy,
fallbackAttempts: run.fallbackCount,
});

View File

@@ -39,6 +39,8 @@ import {
narrowMediaConnectionScope,
pinnedConnectionIds,
} from "./mediaComboFallback.ts";
import { projectComboForEndpoint } from "../../src/lib/combos/endpointCapabilities.ts";
import { orderCapabilityTargets } from "../../src/lib/combos/capabilityTargetOrder.ts";
/**
* Execute a full combo strategy for a text-to-speech request.
@@ -59,9 +61,17 @@ export async function executeSpeechCombo(
}
const allCombos = await getCombos();
const resolvedTargets = resolveComboTargets(combo as never, allCombos as never);
const projected = projectComboForEndpoint(combo as never, allCombos as never, "audio-speech");
const resolvedTargets = resolveComboTargets(
projected.combo as never,
projected.allCombos as never
);
if (!resolvedTargets || resolvedTargets.length === 0) {
return errorResponse(HTTP_STATUS.BAD_REQUEST, `Combo "${comboName}" has no usable targets`);
const message =
Array.isArray(combo.models) && combo.models.length > 0
? `No speech-capable targets in combo "${comboName}"`
: `Combo "${comboName}" has no usable targets`;
return errorResponse(HTTP_STATUS.BAD_REQUEST, message);
}
const targets = await filterComboTargetsForKey(resolvedTargets, keyScope.isTargetAllowed);
if (targets.length === 0) return comboTargetsNotAllowedResponse(comboName);
@@ -74,17 +84,22 @@ export async function executeSpeechCombo(
// provider prefix without checking that the model behind it can speak, so a
// chat model on a speech-capable provider (openai/gpt-4o) would otherwise be
// accepted as a target and only fail once dispatched.
const speechTargets = targets.filter((t) => {
if (!t.modelStr) return false;
const { provider, model } = parseSpeechModel(t.modelStr, dynamicProviders);
if (!provider) return false;
const config =
getSpeechProvider(provider) || dynamicProviders.find((dp) => dp.id === provider) || null;
if (!config) return false;
// Dynamic provider nodes do not always enumerate their models; when the
// list is absent there is nothing to check against, so the target stands.
if (!Array.isArray(config.models) || config.models.length === 0) return true;
return config.models.some((m: { id: string }) => m.id === model || m.id === t.modelStr);
const speechTargets = orderCapabilityTargets({
comboName,
endpoint: "audio-speech",
strategy: projected.combo.strategy,
targets: targets.filter((t) => {
if (!t.modelStr) return false;
const { provider, model } = parseSpeechModel(t.modelStr, dynamicProviders);
if (!provider) return false;
const config =
getSpeechProvider(provider) || dynamicProviders.find((dp) => dp.id === provider) || null;
if (!config) return false;
// Dynamic provider nodes do not always enumerate their models; when the
// list is absent there is nothing to check against, so the target stands.
if (!Array.isArray(config.models) || config.models.length === 0) return true;
return config.models.some((m: { id: string }) => m.id === model || m.id === t.modelStr);
}),
});
if (speechTargets.length === 0) {
@@ -200,7 +215,7 @@ export async function executeSpeechCombo(
costUsd,
latencyMs: Date.now() - startTime,
requestId: generateRequestId(),
strategy: "priority",
strategy: projected.combo.strategy || "priority",
fallbackAttempts: fallbackCount,
});
}

View File

@@ -47,6 +47,8 @@ import { toJsonErrorPayload } from "@/shared/utils/upstreamError";
import { HTTP_STATUS } from "@omniroute/open-sse/config/constants.ts";
import { errorResponse } from "@omniroute/open-sse/utils/error.ts";
import * as logger from "@/sse/utils/logger";
import { projectComboForEndpoint } from "../../src/lib/combos/endpointCapabilities.ts";
import { orderCapabilityTargets } from "../../src/lib/combos/capabilityTargetOrder.ts";
/**
* Execute a full combo strategy for a video generation request.
@@ -76,7 +78,11 @@ export async function executeVideoCombo(
}
const allCombos = await getCombos();
const resolvedTargets = resolveComboTargets(combo as never, allCombos as never);
const projected = projectComboForEndpoint(combo as never, allCombos as never, "videos");
const resolvedTargets = resolveComboTargets(
projected.combo as never,
projected.allCombos as never
);
if (!resolvedTargets || resolvedTargets.length === 0) {
return errorResponse(HTTP_STATUS.BAD_REQUEST, `Combo "${comboName}" has no usable targets`);
}
@@ -89,12 +95,16 @@ export async function executeVideoCombo(
// and filter to video-capable ones. Resolving up front (rather than in the
// execution loop below) lets prompt validation run against the real
// expanded target set instead of the unresolved combo name.
const videoTargets: Array<{ modelStr: string; resolved: VideoModelTarget }> = [];
const videoTargets: Array<{
modelStr: string;
weight?: number;
resolved: VideoModelTarget;
}> = [];
for (const t of targets) {
if (!t.modelStr) continue;
const resolved = await resolveVideoModelTarget(t.modelStr);
if (resolved.provider) {
videoTargets.push({ modelStr: t.modelStr, resolved });
videoTargets.push({ modelStr: t.modelStr, weight: t.weight, resolved });
}
}
@@ -301,8 +311,22 @@ export async function executeVideoCombo(
let selectedResult: MediaGenerationResultLike | null = null;
let selectedFallbackCount = 0;
const tasks = videoTargets.map(({ modelStr, resolved }) => runTarget(modelStr, resolved));
await Promise.race([firstSuccess, Promise.allSettled(tasks)]);
const strategy = projected.combo.strategy || "priority";
const orderedTargets = orderCapabilityTargets({
comboName,
endpoint: "videos",
strategy,
targets: videoTargets,
});
if (strategy === "priority") {
const tasks = orderedTargets.map(({ modelStr, resolved }) => runTarget(modelStr, resolved));
await Promise.race([firstSuccess, Promise.allSettled(tasks)]);
} else {
for (const { modelStr, resolved } of orderedTargets) {
await runTarget(modelStr, resolved);
if (selectedResult) break;
}
}
if (selectedResult) {
return successfulMediaGenerationResponse({
@@ -312,7 +336,7 @@ export async function executeVideoCombo(
model: selectedModel,
startTime,
duration: body.duration,
strategy: "priority",
strategy,
fallbackAttempts: selectedFallbackCount,
});
}

View File

@@ -117,6 +117,11 @@ import { resolveServerErrorMessage } from "@/lib/api/serverErrorMessage";
import { useTranslations } from "next-intl";
import { cn } from "@/shared/utils/cn";
import { strategyBadgeClass } from "./strategyPalette";
import {
COMBO_ENDPOINTS,
COMBO_NON_CHAT_STRATEGIES,
type ComboEndpoint,
} from "@/shared/constants/comboEndpoints";
const ModelSelectModal = dynamic(() => import("@/shared/components/ModelSelectModal"), {
ssr: false,
@@ -127,6 +132,50 @@ const ProxyConfigModal = dynamic(() => import("@/shared/components/ProxyConfigMo
const VALID_NAME_REGEX = /^[a-zA-Z0-9_/.\-\[\] ]+$/;
const COMBO_ENDPOINT_LABELS: Record<ComboEndpoint, string> = {
chat: "Chat",
search: "Search",
"web-fetch": "Fetch",
embeddings: "Embeddings",
rerank: "Rerank",
images: "Images",
"image-edits": "Image edits",
videos: "Video",
music: "Music",
ocr: "OCR",
"audio-speech": "TTS",
"audio-transcriptions": "STT",
"audio-translations": "Audio translation",
"audio-generation": "Sound generation",
};
const COMBO_ENDPOINT_MODEL_TAGS: Record<ComboEndpoint, string[]> = {
chat: ["chat", "responses", "messages", "completions"],
search: ["search"],
"web-fetch": ["web-fetch"],
embeddings: ["embeddings", "multimodal-embeddings"],
rerank: ["rerank"],
images: ["images"],
"image-edits": ["images", "image-edits"],
videos: ["videos"],
music: ["music"],
ocr: ["ocr"],
"audio-speech": ["audio-speech"],
"audio-transcriptions": ["audio-transcriptions"],
"audio-translations": ["audio-transcriptions", "audio-translations"],
"audio-generation": ["audio-generation", "sound-generation"],
};
function modelSupportsComboEndpoint(model: unknown, endpoint: ComboEndpoint): boolean {
const candidate =
model && typeof model === "object" ? (model as { supportedEndpoints?: unknown }) : null;
const supported = Array.isArray(candidate?.supportedEndpoints)
? candidate.supportedEndpoints
: [];
if (supported.length === 0) return endpoint === "chat";
return COMBO_ENDPOINT_MODEL_TAGS[endpoint].some((tag) => supported.includes(tag));
}
const STRATEGY_OPTIONS = ROUTING_STRATEGIES.map((strategy) => ({
value: strategy.value,
labelKey: strategy.labelKey,
@@ -134,6 +183,8 @@ const STRATEGY_OPTIONS = ROUTING_STRATEGIES.map((strategy) => ({
icon: strategy.icon,
}));
const MODALITY_SAFE_STRATEGIES = new Set<string>(COMBO_NON_CHAT_STRATEGIES);
const STRATEGY_LABEL_FALLBACK = {
"context-relay": "Context Relay",
"reset-aware": "Reset-Aware RR",
@@ -2199,6 +2250,11 @@ function ComboFormModal({
comboConfigMode,
routingSettings,
}) {
type CapabilityDraft = {
models: ComboStep[];
strategy: string;
config: Record<string, unknown>;
};
type CreateDraftSnapshot = {
name: string;
models: unknown[];
@@ -2234,12 +2290,20 @@ function ComboFormModal({
const notify = useNotificationStore();
const isExpertMode = normalizeComboConfigMode(comboConfigMode) === "expert";
const createDraftStateRef = useRef<CreateDraftSnapshot>(getEmptyCreateDraftSnapshot());
const initialChatRoute = combo?.routes?.chat;
const [name, setName] = useState(combo?.name || "");
const [description, setDescription] = useState<string>(combo?.description || "");
const [models, setModels] = useState(() => {
return (combo?.models || []).map((m) => normalizeModelEntry(m));
return (initialChatRoute?.models || combo?.models || []).map((m) => normalizeModelEntry(m));
});
const [strategy, setStrategy] = useState(combo?.strategy || "priority");
const [strategy, setStrategy] = useState(
initialChatRoute?.strategy || combo?.strategy || "priority"
);
const [activeEndpoint, setActiveEndpoint] = useState<ComboEndpoint>("chat");
const [configuredEndpoints, setConfiguredEndpoints] = useState<Set<ComboEndpoint>>(
() => new Set(initialChatRoute?.models?.length || combo?.models?.length ? ["chat"] : [])
);
const capabilityDraftsRef = useRef<Partial<Record<ComboEndpoint, CapabilityDraft>>>({});
const [showModelSelect, setShowModelSelect] = useState(false);
const [saving, setSaving] = useState(false);
const [nameError, setNameError] = useState("");
@@ -2261,7 +2325,9 @@ function ComboFormModal({
const [builderError, setBuilderError] = useState("");
const [builderStage, setBuilderStage] = useState<string>(COMBO_BUILDER_STAGES[0]);
const [showAdvanced, setShowAdvanced] = useState(false);
const [config, setConfig] = useState(sanitizeComboRuntimeConfig(combo?.config));
const [config, setConfig] = useState(
sanitizeComboRuntimeConfig(initialChatRoute?.config || combo?.config)
);
// Validate persisted enum; ensure reset on combo change not just first mount.
const initialSortMethod = normalizeSortMethod(
(config.modelSort as { method?: unknown } | undefined)?.method
@@ -2285,6 +2351,38 @@ function ComboFormModal({
useEffect(() => {
sortMethodRef.current = sortMethod;
}, [sortMethod]);
const switchCapability = useCallback(
(endpoint: ComboEndpoint) => {
if (endpoint === activeEndpoint) return;
capabilityDraftsRef.current[activeEndpoint] = {
models: models as ComboStep[],
strategy,
config: sanitizeComboRuntimeConfig(config),
};
setConfiguredEndpoints((current) => {
const nextConfigured = new Set(current);
if (models.length > 0) nextConfigured.add(activeEndpoint);
else nextConfigured.delete(activeEndpoint);
return nextConfigured;
});
const next = capabilityDraftsRef.current[endpoint] || {
models: [],
strategy: "priority",
config: {},
};
setActiveEndpoint(endpoint);
setModels(next.models);
setStrategy(next.strategy);
setConfig(next.config);
const nextSort = normalizeSortMethod(next.config?.modelSort?.method);
setSortMethod(nextSort);
sortMethodRef.current = nextSort;
setBuilderStage(next.models.length > 0 ? "strategy" : "steps");
setBuilderError("");
setManualModelError("");
},
[activeEndpoint, config, models, strategy]
);
const [showStrategyNudge, setShowStrategyNudge] = useState(false);
const strategyChangeMountedRef = useRef(false);
// Agent features (#399 / #401 / #454)
@@ -2298,6 +2396,13 @@ function ComboFormModal({
);
const [contextLengthError, setContextLengthError] = useState<string>("");
const comboBuilderStages = useMemo(() => getComboBuilderStages({ strategy }), [strategy]);
const availableStrategyOptions = useMemo(
() =>
activeEndpoint === "chat"
? STRATEGY_OPTIONS
: STRATEGY_OPTIONS.filter((option) => MODALITY_SAFE_STRATEGIES.has(option.value)),
[activeEndpoint]
);
const visibleStageMeta = useMemo(
() => COMBO_FORM_STAGE_META.filter((stageMeta) => comboBuilderStages.includes(stageMeta.id)),
[comboBuilderStages]
@@ -2307,20 +2412,46 @@ function ComboFormModal({
const resetFormForCombo = useCallback(
(nextCombo, comboDefaults = null) => {
const persistedRoutes =
nextCombo?.routes && typeof nextCombo.routes === "object" ? nextCombo.routes : {};
const nextDrafts: Partial<Record<ComboEndpoint, CapabilityDraft>> = {};
for (const endpoint of COMBO_ENDPOINTS) {
const route = persistedRoutes[endpoint];
if (!route || !Array.isArray(route.models) || route.models.length === 0) continue;
nextDrafts[endpoint] = {
models: route.models.map((entry) => normalizeModelEntry(entry)),
strategy: route.strategy || "priority",
config: sanitizeComboRuntimeConfig(route.config),
};
}
const nextChat = nextDrafts.chat || {
models: (nextCombo?.models || []).map((entry) => normalizeModelEntry(entry)),
strategy: nextCombo?.strategy || comboDefaults?.strategy || "priority",
config: sanitizeComboRuntimeConfig(nextCombo?.config),
};
nextDrafts.chat = nextChat;
capabilityDraftsRef.current = nextDrafts;
setConfiguredEndpoints(
new Set(
COMBO_ENDPOINTS.filter((endpoint) => (nextDrafts[endpoint]?.models.length ?? 0) > 0)
)
);
setActiveEndpoint("chat");
const nextDefaults =
nextCombo || comboDefaults
? {
...(comboDefaults || {}),
}
: {};
const nextConfig = nextCombo?.config
? sanitizeComboRuntimeConfig(nextCombo.config)
: sanitizeComboRuntimeConfig(
Object.fromEntries(Object.entries(nextDefaults).filter(([key]) => key !== "strategy"))
);
const nextConfig =
Object.keys(nextChat.config).length > 0
? sanitizeComboRuntimeConfig(nextChat.config)
: sanitizeComboRuntimeConfig(
Object.fromEntries(Object.entries(nextDefaults).filter(([key]) => key !== "strategy"))
);
// Validate persisted enum; tolerate hand-edited DB values.
const loadedMethod = normalizeSortMethod(nextCombo?.config?.modelSort?.method);
const loadedMethod = normalizeSortMethod(nextChat.config?.modelSort?.method);
// Generation guard so a stale score fetch can't clobber the next combo.
const myGen = ++resetSortGenerationRef.current;
setSortMethod(loadedMethod);
@@ -2330,9 +2461,9 @@ function ComboFormModal({
// Branch so only one setModels runs (no raw-then-sorted double set).
// Score branch is async; guard with generation + cancelled from the caller's effect.
if (loadedMethod === "manual") {
setModels((nextCombo?.models || []).map((m) => normalizeModelEntry(m)));
setModels(nextChat.models);
} else if (loadedMethod === "score") {
const base = (nextCombo?.models || []).map((mm) => normalizeModelEntry(mm)) as ComboStep[];
const base = nextChat.models;
fetchProviderRankings()
.then((rk) => sortComboStepsByScore(base, rk))
.then((sorted) => {
@@ -2344,10 +2475,10 @@ function ComboFormModal({
setModels(base);
});
} else {
const base = (nextCombo?.models || []).map((mm) => normalizeModelEntry(mm)) as ComboStep[];
const base = nextChat.models;
setModels(sortComboStepsSync(base, loadedMethod));
}
setStrategy(nextCombo?.strategy || comboDefaults?.strategy || "priority");
setStrategy(nextChat.strategy);
setConfig(nextConfig);
setShowAdvanced(isExpertMode);
setNameError("");
@@ -2415,8 +2546,16 @@ function ComboFormModal({
const [globalSearchQuery, setGlobalSearchQuery] = useState("");
const builderProviders = useMemo(
() => builderOptions.providers || [],
[builderOptions.providers]
() =>
(builderOptions.providers || [])
.map((provider) => ({
...provider,
models: (provider.models || []).filter((model) =>
modelSupportsComboEndpoint(model, activeEndpoint)
),
}))
.filter((provider) => provider.models.length > 0),
[activeEndpoint, builderOptions.providers]
);
const allGlobalModels = useMemo(() => buildGlobalModelList(builderProviders), [builderProviders]);
@@ -3193,18 +3332,74 @@ function ComboFormModal({
const handleSave = async () => {
if (!validateName(name)) return;
if (hasNoModels || hasInvalidWeightedTotal || hasCostOptimizedWithoutPricing) return;
if (models.length > 0 && (hasInvalidWeightedTotal || hasCostOptimizedWithoutPricing)) return;
const drafts: Partial<Record<ComboEndpoint, CapabilityDraft>> = {
...capabilityDraftsRef.current,
[activeEndpoint]: {
models: models as ComboStep[],
strategy,
config: sanitizeComboRuntimeConfig(config),
},
};
const chatDraft = drafts.chat;
if (!chatDraft || chatDraft.models.length === 0) {
setBuilderError("Add at least one Chat target to keep the combo compatible with clients.");
return;
}
for (const draft of Object.values(drafts)) {
if (!draft || draft.models.length === 0 || draft.strategy !== "weighted") continue;
const total = draft.models.reduce((sum, entry) => sum + (entry.weight || 0), 0);
if (total !== 100) {
setBuilderError("Every weighted capability route must have a total weight of 100%.");
return;
}
}
setSaving(true);
const routes = Object.fromEntries(
COMBO_ENDPOINTS.flatMap((endpoint) => {
const draft = drafts[endpoint];
if (!draft || draft.models.length === 0) return [];
const routeConfig = applyQuotaOnlyFallbackConfig(
draft.strategy,
draft.models,
sanitizeComboRuntimeConfig(draft.config)
);
persistStickyRoundRobinLimit(draft.strategy, routeConfig, draft.config);
persistConnectionAwareExpansion(draft.strategy, routeConfig, draft.config);
if (
isIntelligentBuilderStrategy(draft.strategy) &&
!isExpertMode &&
(!Array.isArray(routeConfig.candidatePool) || routeConfig.candidatePool.length === 0)
) {
const candidates = deriveCandidatePoolFromModels(draft.models);
if (candidates.length > 0) routeConfig.candidatePool = candidates;
}
return [
[
endpoint,
{
models: draft.models,
strategy: draft.strategy,
...(Object.keys(routeConfig).length > 0 ? { config: routeConfig } : {}),
},
],
];
})
) as Partial<Record<ComboEndpoint, CapabilityDraft>>;
const savedChat = routes.chat!;
const saveData: any = {
name: name.trim(),
models,
strategy,
models: savedChat.models,
strategy: savedChat.strategy,
routes,
};
// When editing an existing combo from the dashboard form, synchronize allowedProviders
// and clear legacy family restrictions so adding steps across providers never triggers COMBO_008
Object.assign(saveData, computeAllowedRestrictionSync(isEdit, combo, models));
const allRouteModels = Object.values(routes).flatMap((route) => route?.models || []);
Object.assign(saveData, computeAllowedRestrictionSync(isEdit, combo, allRouteModels));
// Per-combo description (#5005). Free-text, optional, persisted in combo data.
if (description.trim()) {
@@ -3214,31 +3409,7 @@ function ComboFormModal({
saveData.description = null;
}
const configToSave = applyQuotaOnlyFallbackConfig(
strategy,
models,
sanitizeComboRuntimeConfig(config)
);
if (strategy === "round-robin") {
if (config.concurrencyPerModel !== undefined)
configToSave.concurrencyPerModel = config.concurrencyPerModel;
if (config.queueTimeoutMs !== undefined) configToSave.queueTimeoutMs = config.queueTimeoutMs;
}
if (strategy === "weighted" && config.stickyWeightedLimit !== undefined) {
configToSave.stickyWeightedLimit = config.stickyWeightedLimit;
}
persistStickyRoundRobinLimit(strategy, configToSave, config);
persistConnectionAwareExpansion(strategy, configToSave, config);
if (
usesIntelligentBuilderStage &&
!isExpertMode &&
(!Array.isArray(configToSave.candidatePool) || configToSave.candidatePool.length === 0)
) {
const derivedCandidatePool = deriveCandidatePoolFromModels(models);
if (derivedCandidatePool.length > 0) {
configToSave.candidatePool = derivedCandidatePool;
}
}
const configToSave = sanitizeComboRuntimeConfig(savedChat.config);
const hasConfigToSave = Object.keys(configToSave).length > 0;
const hadExistingConfig = Object.keys(sanitizeComboRuntimeConfig(combo?.config)).length > 0;
if (hasConfigToSave || (isEdit && hadExistingConfig)) {
@@ -3305,6 +3476,37 @@ function ComboFormModal({
size="full"
>
<div className="flex flex-col gap-3">
<div className="rounded-lg border border-border bg-surface p-3">
<div className="mb-2">
<p className="text-xs font-semibold text-text-main">Endpoint behavior</p>
<p className="mt-0.5 text-[10px] text-text-muted">
Configure targets and routing strategy independently for each API capability. Empty
capabilities are not saved. Chat remains the legacy-compatible default.
</p>
</div>
<div className="flex flex-wrap gap-2" role="tablist" aria-label="Combo capability">
{COMBO_ENDPOINTS.map((endpoint) => {
const configured =
endpoint === activeEndpoint
? models.length > 0
: configuredEndpoints.has(endpoint);
return (
<Button
key={endpoint}
type="button"
role="tab"
aria-selected={activeEndpoint === endpoint}
variant={activeEndpoint === endpoint ? "primary" : "secondary"}
size="sm"
onClick={() => switchCapability(endpoint)}
>
{COMBO_ENDPOINT_LABELS[endpoint]}
{configured ? " •" : ""}
</Button>
);
})}
</div>
</div>
{!isExpertMode && (
<div className="rounded-lg border border-border bg-bg-subtle p-3">
<div className="flex items-center justify-between gap-2 mb-2">
@@ -3523,7 +3725,7 @@ function ComboFormModal({
onValueChange={setStrategy}
className="[&>.ci-radio-group__items]:grid-cols-2 [&>.ci-radio-group__items]:gap-1.5 sm:[&>.ci-radio-group__items]:grid-cols-3"
>
{STRATEGY_OPTIONS.map((s) => (
{availableStrategyOptions.map((s) => (
<div
key={s.value}
data-testid={`strategy-option-${s.value}`}
@@ -5095,7 +5297,7 @@ function ComboFormModal({
selectedModel={null}
addedModelValues={models.map((m) => m.model)}
keepOpenOnSelect
includeWebTargets
includeWebTargets={activeEndpoint === "search" || activeEndpoint === "web-fetch"}
/>
</>
);

View File

@@ -17,6 +17,10 @@ import { comboErrorResponse } from "@/lib/api/comboErrorResponse";
import { ComboInvariantError } from "@/lib/combos/invariants";
import { buildComboNameCollisionWarning } from "@/lib/combos/modelNameCollision";
import { stripDeadComboConfigKeys } from "@/lib/combos/deadConfigKeys";
import {
comboWithCapabilityRouteSteps,
normalizeComboCapabilityRoutes,
} from "@/lib/combos/capabilityRoutes";
// Minimal shape for the fields we read off a combo row in this route.
// `getComboById` returns a structurally `JsonRecord`-typed object, so we
@@ -138,6 +142,12 @@ export async function PUT(request, { params }) {
if (normalizedUpdate.config && typeof normalizedUpdate.config === "object") {
normalizedUpdate.config = stripDeadComboConfigKeys(normalizedUpdate.config);
}
if (normalizedUpdate.routes && typeof normalizedUpdate.routes === "object") {
normalizedUpdate.routes = normalizeComboCapabilityRoutes(normalizedUpdate.routes, {
comboName: String(comboName),
allCombos: allCombos as never,
});
}
// #15251: the runtime reads `universal_handoff` before the camelCase alias,
// so a stale stored alias must not shadow a validated update (or null-clear).
if (normalizedUpdate.universal_handoff !== undefined) {
@@ -167,10 +177,24 @@ export async function PUT(request, { params }) {
// currently has no allowedProviders restriction, currentProviders is
// empty and unioning it with the new step providers would synthesize a
// brand-new allowlist out of nothing — the opposite of "no restriction".
if (body.models && body.allowedProviders === undefined && currentProviders.length > 0) {
const stepProviders = (
body.models as Array<{ providerId?: string; provider?: string; model?: string }>
)
const routeModels =
body.routes && typeof body.routes === "object" && !Array.isArray(body.routes)
? Object.values(body.routes).flatMap((route) =>
route && typeof route === "object" && !Array.isArray(route)
? ((route as { models?: unknown }).models ?? [])
: []
)
: [];
const providerSteps = [
...(Array.isArray(body.models) ? body.models : []),
...(Array.isArray(routeModels) ? routeModels : []),
] as Array<{ providerId?: string; provider?: string; model?: string }>;
if (
providerSteps.length > 0 &&
body.allowedProviders === undefined &&
currentProviders.length > 0
) {
const stepProviders = providerSteps
.map((m) => {
if (m.providerId) return m.providerId;
if (m.provider) return m.provider;
@@ -227,9 +251,11 @@ export async function PUT(request, { params }) {
}
// Validate nested combo DAG (no circular references, max depth)
if (body.models) {
if (body.models || body.routes) {
// Update the combo in the list temporarily for validation
const updatedCombos = allCombos.map((c) => (c.id === id ? { ...c, ...body } : c));
const updatedCombos = allCombos.map((c) =>
comboWithCapabilityRouteSteps(c.id === id ? { ...c, ...body } : c)
);
if (comboName) {
const configuredDepth = clampComboDepth(
(nextComboState as { config?: { maxComboDepth?: unknown } }).config?.maxComboDepth

View File

@@ -14,6 +14,10 @@ import { computeComboContextLength, createComboContextResolution } from "@/lib/c
import { ComboInvariantError } from "@/lib/combos/invariants";
import { buildComboNameCollisionWarning } from "@/lib/combos/modelNameCollision";
import { stripDeadComboConfigKeys } from "@/lib/combos/deadConfigKeys";
import {
comboWithCapabilityRouteSteps,
normalizeComboCapabilityRoutes,
} from "@/lib/combos/capabilityRoutes";
// GET /api/combos - Get all combos
export async function GET(request: Request) {
@@ -71,6 +75,14 @@ export async function POST(request) {
const comboInput = {
...validation.data,
models: normalizedModels,
...(validation.data.routes
? {
routes: normalizeComboCapabilityRoutes(validation.data.routes, {
comboName: validation.data.name,
allCombos: allCombos as never,
}),
}
: {}),
};
if (comboInput.config && typeof comboInput.config === "object") {
comboInput.config = stripDeadComboConfigKeys(comboInput.config);
@@ -98,16 +110,16 @@ export async function POST(request) {
// Validate nested combo DAG (no circular references, max depth)
// Temporarily add the new combo to validate its graph
const tempCombo = {
const tempCombo = comboWithCapabilityRouteSteps({
...comboInput,
name,
strategy,
config,
};
});
try {
validateComboDAG(
name,
[...allCombos, tempCombo],
[...allCombos.map(comboWithCapabilityRouteSteps), tempCombo],
new Set(),
0,
clampComboDepth((config as { maxComboDepth?: unknown } | undefined)?.maxComboDepth)

View File

@@ -52,18 +52,27 @@ export async function proxyElevenLabsRequest(
request: Request,
pathname: string,
init: Omit<RequestInit, "headers"> = {},
options: { model?: string; contentType?: string } = {}
options: {
model?: string;
contentType?: string;
/** Internal combo dispatch already performed policy + connection-scope checks. */
preauthorizedConnections?: string[] | null;
} = {}
): Promise<Response> {
const policy = await enforceApiKeyPolicy(request, options.model ?? null);
if (policy.rejection) return policy.rejection;
// GHSA-3w2r: the ElevenLabs connection must be one the caller key may use.
const scope = await resolveKeyConnectionScope(policy.apiKeyInfo);
if (scope.rejection) return scope.rejection;
let allowedConnections = options.preauthorizedConnections;
if (allowedConnections === undefined) {
const policy = await enforceApiKeyPolicy(request, options.model ?? null);
if (policy.rejection) return policy.rejection;
// GHSA-3w2r: the ElevenLabs connection must be one the caller key may use.
const scope = await resolveKeyConnectionScope(policy.apiKeyInfo);
if (scope.rejection) return scope.rejection;
allowedConnections = scope.allowedConnections;
}
const selection = await getProviderCredentialsWithQuotaPreflight(
"elevenlabs",
null,
scope.allowedConnections
allowedConnections
);
if (isKeyScopeBlockedCredentials(selection)) {
return keyScopeBlockedResponse("elevenlabs", selection);

View File

@@ -247,7 +247,7 @@ export async function POST(request) {
} catch {}
return handleComboChat({
endpointCapability: "audio-transcriptions",
endpointCapability: "audio-translations",
body: { model: modelStr } as any,
combo: combo as any,
handleSingleModel: async (_reqBody: any, targetModelStr: string) =>

View File

@@ -34,6 +34,8 @@ import {
runImageComboTargets,
type ImageComboDispatchResult,
} from "@omniroute/open-sse/services/imageCombo.ts";
import { projectComboForEndpoint } from "@/lib/combos/endpointCapabilities";
import { orderCapabilityTargets } from "@/lib/combos/capabilityTargetOrder";
import { isAllRateLimitedCredentials } from "@/app/api/v1/_shared/rateLimit";
import {
runImageRequestWithAccounting,
@@ -337,6 +339,7 @@ interface ImageEditContext {
/** A combo target that resolved to an edit-capable provider/node. */
interface EditComboTarget {
modelStr: string;
weight?: number;
parsed: ReturnType<typeof parseImageModel>;
providerConfig: ReturnType<typeof getImageProvider> | null;
/** Credential/connection lookup key (built-in provider id, or custom node id). */
@@ -569,7 +572,8 @@ async function executeImageEditCombo(comboName: string, ctx: ImageEditContext):
return errorResponse(HTTP_STATUS.BAD_REQUEST, `Combo not found: ${comboName}`);
}
const allCombos = await getCombos();
const targets = resolveComboTargets(combo as never, allCombos as never);
const projected = projectComboForEndpoint(combo as never, allCombos as never, "image-edits");
const targets = resolveComboTargets(projected.combo as never, projected.allCombos as never);
if (!targets || targets.length === 0) {
return errorResponse(HTTP_STATUS.BAD_REQUEST, `Combo "${comboName}" has no usable targets`);
}
@@ -594,7 +598,13 @@ async function executeImageEditCombo(comboName: string, ctx: ImageEditContext):
const providerConfig = parsed.provider ? getImageProvider(parsed.provider) : null;
const capability = classifyImageEditTarget(resolved, parsed, providerConfig);
if (!capability) continue;
editTargets.push({ modelStr: resolved, parsed, providerConfig, credKey: capability.credKey });
editTargets.push({
modelStr: resolved,
weight: t.weight,
parsed,
providerConfig,
credKey: capability.credKey,
});
}
if (editTargets.length === 0) {
@@ -604,7 +614,13 @@ async function executeImageEditCombo(comboName: string, ctx: ImageEditContext):
);
}
const run = await runImageComboTargets(editTargets, {
const orderedTargets = orderCapabilityTargets({
comboName,
endpoint: "image-edits",
strategy: projected.combo.strategy,
targets: editTargets,
});
const run = await runImageComboTargets(orderedTargets, {
resolveProvider: (target) => ({ provider: target.credKey, model: target.parsed.model }),
resolveCredentials: (_provider, target) =>
getProviderCredentialsWithQuotaPreflight(
@@ -621,7 +637,7 @@ async function executeImageEditCombo(comboName: string, ctx: ImageEditContext):
target.parsed.provider || target.credKey,
target.parsed.model,
credentials,
"priority"
projected.combo.strategy || "priority"
),
() => dispatchImageEditTarget(target, credentials, ctx)
),

View File

@@ -22,6 +22,8 @@ import {
successfulMediaGenerationResponse,
} from "@/app/api/v1/_shared/mediaGenerationRoute";
import { getSpecialtyModelsResponse } from "@/app/api/v1/_shared/specialtyCatalog";
import { getComboByName } from "@/lib/db/combos";
import { runModelEndpointCombo } from "@/lib/combos/modelEndpointCombo";
export const dynamic = "force-dynamic";
@@ -43,6 +45,62 @@ export async function GET(request?: Request) {
);
}
async function dispatchMusicModel(
request: Request,
body: Record<string, unknown> & { model: string; duration?: number },
allowedConnections: string[] | null,
apiKeyId: string | undefined,
startTime: number
) {
const mismatch = await getRequestedModelEndpointMismatch(body.model, "music");
if (mismatch) return errorResponse(HTTP_STATUS.BAD_REQUEST, mismatch);
const { provider } = parseMusicModel(body.model);
if (!provider) {
return errorResponse(
HTTP_STATUS.BAD_REQUEST,
`Invalid music model: ${body.model}. Use format: provider/model`
);
}
const providerConfig = getMusicProvider(provider);
let credentials = null;
if (providerConfig && providerConfig.authType !== "none") {
credentials = await getProviderCredentialsWithQuotaPreflight(
provider,
null,
allowedConnections
);
if (!credentials) {
return errorResponse(
HTTP_STATUS.BAD_REQUEST,
`No credentials for music provider: ${provider}`
);
}
const unusable = unusableCredentialsResponse(provider, credentials);
if (unusable) return unusable;
} else if (providerConfig?.authType === "none") {
const local = await resolveLocalOverrideCredentials(provider, allowedConnections);
if (local.rejection) return local.rejection;
credentials = local.credentials;
}
const result = await handleMusicGeneration({ body, credentials, log, signal: request.signal });
if (!result.success) {
return failedMediaGenerationResponse(result, "Music generation provider error");
}
await clearRecoveredProviderState(credentials);
return successfulMediaGenerationResponse({
result,
billingMode: "audio",
provider,
model: body.model,
startTime,
duration: body.duration,
apiKeyId,
});
}
/**
* POST /v1/music/generations — generate music
*/
@@ -62,62 +120,34 @@ async function postHandler(request, context) {
// the same scope chat applies.
const scope = await enforceApiKeyPolicyWithScope(request, body.model);
if (scope.rejection) return scope.rejection;
// Respect the authenticated API key's credential scope before checking model capabilities.
const mismatch = await getRequestedModelEndpointMismatch(body.model, "music");
if (mismatch) return errorResponse(HTTP_STATUS.BAD_REQUEST, mismatch);
// Parse model to get provider
const { provider } = parseMusicModel(body.model);
if (!provider) {
return errorResponse(
HTTP_STATUS.BAD_REQUEST,
`Invalid music model: ${body.model}. Use format: provider/model`
);
}
// Check provider config for auth bypass
const providerConfig = getMusicProvider(provider);
// Get credentials — skip for local providers (authType: "none")
let credentials = null;
if (providerConfig && providerConfig.authType !== "none") {
credentials = await getProviderCredentialsWithQuotaPreflight(
provider,
null,
scope.allowedConnections
);
if (!credentials) {
return errorResponse(
HTTP_STATUS.BAD_REQUEST,
`No credentials for music provider: ${provider}`
);
if (typeof body.model === "string" && !body.model.includes("/")) {
const combo = await getComboByName(body.model);
if (combo) {
return runModelEndpointCombo({
name: body.model,
endpoint: "music",
request,
body,
policy: scope.policy,
allowedConnections: scope.allowedConnections,
dispatch: (routedBody, allowedConnections) =>
dispatchMusicModel(
request,
routedBody as Record<string, unknown> & { model: string; duration?: number },
allowedConnections,
scope.policy.apiKeyInfo?.id,
startTime
),
});
}
const unusable = unusableCredentialsResponse(provider, credentials);
if (unusable) return unusable;
} else if (providerConfig?.authType === "none") {
// #6928 + GHSA-3w2r: the shared local-override lookup (also used by the
// video route) keeps the override and the zero-config default in scope.
const local = await resolveLocalOverrideCredentials(provider, scope.allowedConnections);
if (local.rejection) return local.rejection;
credentials = local.credentials;
}
const result = await handleMusicGeneration({ body, credentials, log, signal: request.signal });
if (result.success) {
await clearRecoveredProviderState(credentials);
return successfulMediaGenerationResponse({
result,
billingMode: "audio",
provider,
model: body.model,
startTime,
duration: body.duration,
apiKeyId: scope.policy.apiKeyInfo?.id,
});
}
return failedMediaGenerationResponse(result, "Music generation provider error");
return dispatchMusicModel(
request,
body,
scope.allowedConnections,
scope.policy.apiKeyInfo?.id,
startTime
);
}
export const POST = withInjectionGuard(postHandler);

View File

@@ -1,11 +1,64 @@
import { elevenLabsOptionsResponse } from "@/app/api/v1/_shared/elevenLabsProxy";
import {
elevenLabsOptionsResponse,
proxyElevenLabsRequest,
} from "@/app/api/v1/_shared/elevenLabsProxy";
import { proxyElevenLabsGeneration } from "@/app/api/v1/_shared/elevenLabsGeneration";
import { elevenLabsMusicSchema } from "@/shared/validation/schemas/elevenlabs";
import { getComboByName } from "@/lib/db/combos";
import { runModelEndpointCombo } from "@/lib/combos/modelEndpointCombo";
import { enforceApiKeyPolicy } from "@/shared/utils/apiKeyPolicy";
import { resolveKeyConnectionScope } from "@/sse/services/keyConnectionScope";
import { parseMusicModel } from "@omniroute/open-sse/config/musicRegistry.ts";
import { errorResponse } from "@omniroute/open-sse/utils/error.ts";
export async function OPTIONS() {
return elevenLabsOptionsResponse();
}
export async function POST(request: Request) {
let candidate: unknown;
try {
candidate = await request.clone().json();
} catch {
return proxyElevenLabsGeneration(request, "/music", elevenLabsMusicSchema, "music_v1");
}
const parsed = elevenLabsMusicSchema.safeParse(candidate);
const requestedModel = parsed.success ? parsed.data.model_id : null;
if (typeof requestedModel === "string" && !requestedModel.includes("/")) {
const combo = await getComboByName(requestedModel);
if (combo) {
const policy = await enforceApiKeyPolicy(request, requestedModel);
if (policy.rejection) return policy.rejection;
const scope = await resolveKeyConnectionScope(policy.apiKeyInfo);
if (scope.rejection) return scope.rejection;
return runModelEndpointCombo({
name: requestedModel,
endpoint: "music",
request,
body: { ...parsed.data, model: requestedModel },
policy,
allowedConnections: scope.allowedConnections,
dispatch: (targetBody, allowedConnections) => {
const target = String(targetBody.model || "");
const { provider, model } = parseMusicModel(target);
if (provider !== "elevenlabs" || !model) {
return Promise.resolve(
errorResponse(400, "The native /v1/music endpoint requires an ElevenLabs target")
);
}
return proxyElevenLabsRequest(
request,
"/music",
{ method: "POST", body: JSON.stringify({ ...parsed.data, model_id: model }) },
{
model: target,
contentType: "application/json",
preauthorizedConnections: allowedConnections,
}
);
},
});
}
}
return proxyElevenLabsGeneration(request, "/music", elevenLabsMusicSchema, "music_v1");
}

View File

@@ -24,6 +24,8 @@ import {
isAllRateLimitedCredentials,
rateLimitedProviderResponse,
} from "@/app/api/v1/_shared/rateLimit";
import { getComboByName } from "@/lib/db/combos";
import { runModelEndpointCombo } from "@/lib/combos/modelEndpointCombo";
export { resolveVertexOcrAccessToken };
@@ -68,6 +70,37 @@ export async function OPTIONS() {
});
}
async function dispatchOcrModel(
body: Record<string, unknown> & { model: string },
allowedConnections: string[] | null
): Promise<Response> {
const { provider } = parseOcrModel(body.model);
const resolvedProvider = provider || "mistral";
const credentials = await getProviderCredentialsWithQuotaPreflight(
resolvedProvider,
null,
allowedConnections
);
if (!credentials) {
return errorResponse(
HTTP_STATUS.BAD_REQUEST,
`No credentials for provider: ${resolvedProvider}`
);
}
if (isKeyScopeBlockedCredentials(credentials)) {
return keyScopeBlockedResponse(resolvedProvider, credentials);
}
if (isAllRateLimitedCredentials(credentials)) {
return rateLimitedProviderResponse(resolvedProvider, credentials);
}
const tokenReadyCredentials = await resolveVertexOcrAccessToken(resolvedProvider, credentials);
const ocrCredentials = resolveOcrCredentials(tokenReadyCredentials, resolvedProvider);
const response = await handleOcr({ body, credentials: ocrCredentials });
if (response?.ok) await clearRecoveredProviderState(credentials);
return response;
}
/**
* POST /v1/ocr — document OCR
* Mistral OCR API compatible.
@@ -95,37 +128,26 @@ async function postHandler(request, context) {
// the same scope chat applies.
const scope = await resolveKeyConnectionScope(policy.apiKeyInfo);
if (scope.rejection) return scope.rejection;
const { provider } = parseOcrModel(model);
// Default to mistral if no provider prefix
const resolvedProvider = provider || "mistral";
const credentials = await getProviderCredentialsWithQuotaPreflight(
resolvedProvider,
null,
scope.allowedConnections
);
if (!credentials) {
return errorResponse(
HTTP_STATUS.BAD_REQUEST,
`No credentials for provider: ${resolvedProvider}`
);
const routedBody = { ...body, model };
if (!model.includes("/")) {
const combo = await getComboByName(model);
if (combo) {
return runModelEndpointCombo({
name: model,
endpoint: "ocr",
request,
body: routedBody,
policy,
allowedConnections: scope.allowedConnections,
dispatch: (targetBody, allowedConnections) =>
dispatchOcrModel(
targetBody as Record<string, unknown> & { model: string },
allowedConnections
),
});
}
}
if (isKeyScopeBlockedCredentials(credentials)) {
return keyScopeBlockedResponse(resolvedProvider, credentials);
}
if (isAllRateLimitedCredentials(credentials)) {
return rateLimitedProviderResponse(resolvedProvider, credentials);
}
const tokenReadyCredentials = await resolveVertexOcrAccessToken(resolvedProvider, credentials);
const ocrCredentials = resolveOcrCredentials(tokenReadyCredentials, resolvedProvider);
const response = await handleOcr({ body: { ...body, model }, credentials: ocrCredentials });
if (response?.ok) {
await clearRecoveredProviderState(credentials);
}
return response;
return dispatchOcrModel(routedBody, scope.allowedConnections);
}
export const POST = withInjectionGuard(postHandler);

View File

@@ -1,12 +1,74 @@
import { elevenLabsOptionsResponse } from "@/app/api/v1/_shared/elevenLabsProxy";
import {
elevenLabsOptionsResponse,
proxyElevenLabsRequest,
} from "@/app/api/v1/_shared/elevenLabsProxy";
import { proxyElevenLabsGeneration } from "@/app/api/v1/_shared/elevenLabsGeneration";
import { getComboByName } from "@/lib/db/combos";
import { runModelEndpointCombo } from "@/lib/combos/modelEndpointCombo";
import { enforceApiKeyPolicy } from "@/shared/utils/apiKeyPolicy";
import { resolveKeyConnectionScope } from "@/sse/services/keyConnectionScope";
import { elevenLabsSoundGenerationSchema } from "@/shared/validation/schemas/elevenlabs";
import { errorResponse } from "@omniroute/open-sse/utils/error.ts";
export async function OPTIONS() {
return elevenLabsOptionsResponse();
}
export async function POST(request: Request) {
let candidate: unknown;
try {
candidate = await request.clone().json();
} catch {
return proxyElevenLabsGeneration(
request,
"/sound-generation",
elevenLabsSoundGenerationSchema,
"eleven_text_to_sound_v2"
);
}
const parsed = elevenLabsSoundGenerationSchema.safeParse(candidate);
const requestedModel = parsed.success ? parsed.data.model_id : null;
if (typeof requestedModel === "string" && !requestedModel.includes("/")) {
const combo = await getComboByName(requestedModel);
if (combo) {
const policy = await enforceApiKeyPolicy(request, requestedModel);
if (policy.rejection) return policy.rejection;
const scope = await resolveKeyConnectionScope(policy.apiKeyInfo);
if (scope.rejection) return scope.rejection;
return runModelEndpointCombo({
name: requestedModel,
endpoint: "audio-generation",
request,
body: { ...parsed.data, model: requestedModel },
policy,
allowedConnections: scope.allowedConnections,
dispatch: (targetBody, allowedConnections) => {
const target = String(targetBody.model || "");
const separator = target.indexOf("/");
const provider = separator > 0 ? target.slice(0, separator) : "";
const model = separator > 0 ? target.slice(separator + 1) : "";
if (provider !== "elevenlabs" || !model) {
return Promise.resolve(
errorResponse(
400,
"The native /v1/sound-generation endpoint requires an ElevenLabs target"
)
);
}
return proxyElevenLabsRequest(
request,
"/sound-generation",
{ method: "POST", body: JSON.stringify({ ...parsed.data, model_id: model }) },
{
model: target,
contentType: "application/json",
preauthorizedConnections: allowedConnections,
}
);
},
});
}
}
return proxyElevenLabsGeneration(
request,
"/sound-generation",

View File

@@ -699,6 +699,17 @@ export const OPENAPI_ENDPOINTS: OpenApiEndpoint[] = [
requiresAuth: false,
hasRequestBody: false,
},
{
path: "/api/v1/models/health",
method: "GET",
summary: "Per-model pause state for the caller's key",
description:
"Read-only liveness view over the in-memory model lockout store. `ok` means no pause observed by this instance (not a health verdict); connection-level pauses live outside the store read here. Additive: leaves GET /v1/models and its cache untouched. `retry_after` carries the shortest remaining lockout in seconds, rounded up, only when `state` is `cooling`. One documented difference from the dispatch 429 `model_cooldown` rule: a model with one lockout-covered connection and one connection-level cooldown still reads `cooling` here, while the 429 requires every connection to carry a model cooldown.",
tag: "Models",
tags: ["Models"],
requiresAuth: true,
hasRequestBody: false,
},
{
path: "/api/v1/models/query",
method: "POST",
@@ -919,6 +930,27 @@ export const OPENAPI_ENDPOINTS: OpenApiEndpoint[] = [
requiresAuth: false,
hasRequestBody: false,
},
{
path: "/api/v1/quota-recoveries/{id}",
method: "DELETE",
summary: "Cancel a quota recovery ticket",
description: "",
tag: "V1 Quota Recoveries",
tags: ["V1 Quota Recoveries"],
requiresAuth: true,
hasRequestBody: false,
},
{
path: "/api/v1/quota-recoveries/{id}",
method: "GET",
summary: "Poll a quota recovery ticket",
description:
"Opt-in recovery of a non-streaming `POST /api/v1/chat/completions` or `POST /api/v1/responses` turn that failed with a terminal quota error. A request opts in with `x-omniroute-quota-recovery: auto`, `x-omniroute-session` and `x-omniroute-recovery-turn` (each `[A-Za-z0-9._:-]{1,128}`); it must be `stream: false`, carry no tools / tool history / `previous_response_id`, and not pin a connection. Recovery needs `STORAGE_ENCRYPTION_KEY` (the request is stored encrypted for up to 24 h). Instead of the quota error the caller then receives `202` with `recovery.poll` pointing here. Tickets are visible only to the API key that created them.",
tag: "V1 Quota Recoveries",
tags: ["V1 Quota Recoveries"],
requiresAuth: true,
hasRequestBody: false,
},
{
path: "/api/v1/quotas/check",
method: "GET",

View File

@@ -22,6 +22,8 @@ import {
} from "@/shared/constants/providers";
import type { RegistryModel } from "@omniroute/open-sse/config/providerRegistry.ts";
import { getVideoProvider } from "@omniroute/open-sse/config/videoRegistry.ts";
import { getMusicProvider } from "@omniroute/open-sse/config/musicRegistry.ts";
import { getOcrProvider } from "@omniroute/open-sse/config/ocrRegistry.ts";
import { appendSyncedEffortVariants } from "@omniroute/open-sse/utils/syncedEffortVariants";
type JsonRecord = Record<string, unknown>;
@@ -212,6 +214,7 @@ function hasStaticMediaRoutingMetadata(model: ComboBuilderModelOption): boolean
"videos",
"audio-speech",
"audio-transcriptions",
"audio-generation",
"search",
"web-fetch",
].includes(endpoint)
@@ -466,6 +469,21 @@ function addStaticMediaModelOptions(
const transcriptionProvider = getTranscriptionProvider(providerId);
append(transcriptionProvider?.models, "audio", "audio-transcriptions", "audio-transcriptions");
if (providerId === "elevenlabs") {
append(
[{ id: "eleven_text_to_sound_v2", name: "Eleven Text to Sound V2" }],
"audio",
"audio-generation",
"audio-generation"
);
}
const musicProvider = getMusicProvider(providerId);
append(musicProvider?.models, "audio", "music", "music");
const ocrProvider = getOcrProvider(providerId);
append(ocrProvider?.models, "ocr", "ocr", "ocr");
}
function buildModelOptions(

View File

@@ -0,0 +1,49 @@
import { COMBO_ENDPOINTS, type ComboEndpoint } from "@/shared/constants/comboEndpoints";
import { normalizeComboModels, type ComboStep } from "./steps";
import { stripDeadComboConfigKeys } from "./deadConfigKeys";
export type ComboCapabilityRoute = {
models: ComboStep[];
strategy: string;
config?: Record<string, unknown>;
};
export type ComboCapabilityRoutes = Partial<Record<ComboEndpoint, ComboCapabilityRoute>>;
function isRecord(value: unknown): value is Record<string, unknown> {
return Boolean(value && typeof value === "object" && !Array.isArray(value));
}
export function normalizeComboCapabilityRoutes(
routes: unknown,
options: { comboName: string; allCombos: never }
): ComboCapabilityRoutes | undefined {
if (!isRecord(routes)) return undefined;
const normalized: ComboCapabilityRoutes = {};
for (const endpoint of COMBO_ENDPOINTS) {
const route = routes[endpoint];
if (!isRecord(route) || !Array.isArray(route.models)) continue;
normalized[endpoint] = {
models: normalizeComboModels(route.models, {
comboName: options.comboName,
allCombos: options.allCombos,
}),
strategy: typeof route.strategy === "string" ? route.strategy : "priority",
...(isRecord(route.config) ? { config: stripDeadComboConfigKeys(route.config) } : {}),
};
}
return Object.keys(normalized).length > 0 ? normalized : undefined;
}
/** Include route references in the existing DAG validator without changing its contract. */
export function comboWithCapabilityRouteSteps<T extends Record<string, unknown>>(combo: T): T {
const routes = isRecord(combo.routes) ? combo.routes : {};
const routeSteps = COMBO_ENDPOINTS.flatMap((endpoint) => {
const route = routes[endpoint];
return isRecord(route) && Array.isArray(route.models) ? route.models : [];
});
return {
...combo,
models: [...(Array.isArray(combo.models) ? combo.models : []), ...routeSteps],
};
}

View File

@@ -0,0 +1,93 @@
import type { ComboEndpoint } from "@/shared/constants/comboEndpoints";
const MAX_STATE_ENTRIES = 2048;
const roundRobinCounters = new Map<string, number>();
const targetUseCounts = new Map<string, number>();
function boundedSet(map: Map<string, number>, key: string, value: number): void {
if (!map.has(key) && map.size >= MAX_STATE_ENTRIES) {
const oldest = map.keys().next().value;
if (typeof oldest === "string") map.delete(oldest);
}
map.set(key, value);
}
function rotate<T>(targets: readonly T[], index: number): T[] {
if (targets.length < 2) return [...targets];
const offset = ((index % targets.length) + targets.length) % targets.length;
return [...targets.slice(offset), ...targets.slice(0, offset)];
}
export type CapabilityOrderTarget = {
modelStr: string;
weight?: number;
};
/** Deterministic/non-chat-safe strategy ordering for modal endpoint executors. */
export function orderCapabilityTargets<T extends CapabilityOrderTarget>(options: {
comboName: string;
endpoint: ComboEndpoint;
strategy?: string | null;
targets: readonly T[];
random?: () => number;
}): T[] {
const { comboName, endpoint, targets } = options;
const strategy = options.strategy || "priority";
const random = options.random || Math.random;
if (targets.length < 2 || strategy === "priority" || strategy === "fill-first") {
return [...targets];
}
const routeKey = `${comboName}:${endpoint}`;
if (strategy === "round-robin") {
const index = roundRobinCounters.get(routeKey) || 0;
boundedSet(roundRobinCounters, routeKey, index + 1);
return rotate(targets, index);
}
if (strategy === "random" || strategy === "strict-random") {
const shuffled = [...targets];
for (let index = shuffled.length - 1; index > 0; index -= 1) {
const swap = Math.floor(random() * (index + 1));
[shuffled[index], shuffled[swap]] = [shuffled[swap], shuffled[index]];
}
return shuffled;
}
if (strategy === "weighted") {
const total = targets.reduce((sum, target) => sum + Math.max(0, target.weight || 0), 0);
if (total <= 0) return [...targets];
let point = random() * total;
const winner =
targets.find((target) => {
point -= Math.max(0, target.weight || 0);
return point <= 0;
}) || targets[targets.length - 1];
return [winner, ...targets.filter((target) => target !== winner)];
}
if (strategy === "least-used" || strategy === "p2c") {
const candidates =
strategy === "p2c" && targets.length > 2
? [
targets[Math.floor(random() * targets.length)],
targets[Math.floor(random() * targets.length)],
]
: [...targets];
const winner = candidates.reduce((best, target) => {
const currentCount = targetUseCounts.get(`${routeKey}:${target.modelStr}`) || 0;
const bestCount = targetUseCounts.get(`${routeKey}:${best.modelStr}`) || 0;
return currentCount < bestCount ? target : best;
});
const usageKey = `${routeKey}:${winner.modelStr}`;
boundedSet(targetUseCounts, usageKey, (targetUseCounts.get(usageKey) || 0) + 1);
return [winner, ...targets.filter((target) => target !== winner)];
}
return [...targets];
}
export function resetCapabilityTargetOrderForTests(): void {
roundRobinCounters.clear();
targetUseCounts.clear();
}

View File

@@ -4,22 +4,45 @@ import {
SEARCH_PROVIDERS,
} from "@omniroute/open-sse/config/searchRegistry.ts";
import { WEB_FETCH_PROVIDERS } from "@omniroute/open-sse/config/webFetchProviders.ts";
import { getImageModelEntry } from "@omniroute/open-sse/config/imageRegistry.ts";
import { getVideoProvider } from "@omniroute/open-sse/config/videoRegistry.ts";
import { getMusicProvider } from "@omniroute/open-sse/config/musicRegistry.ts";
import { getOcrProvider } from "@omniroute/open-sse/config/ocrRegistry.ts";
import {
getSpeechProvider,
getTranscriptionProvider,
} from "@omniroute/open-sse/config/audioRegistry.ts";
import type { ComboCollectionLike, ComboLike } from "@omniroute/open-sse/services/combo/types.ts";
import { MAX_COMBO_DEPTH_HARD_CAP } from "@omniroute/open-sse/services/combo/comboPredicates.ts";
import type { ComboEndpoint } from "@/shared/constants/comboEndpoints";
import { getProviderByAlias, getProviderById } from "@/shared/constants/providers";
import { getComboModelString, normalizeComboStep, type ComboStep } from "./steps.ts";
export type ComboEndpoint =
| "chat"
| "search"
| "web-fetch"
| "embeddings"
| "rerank"
| "images"
| "videos"
| "music"
| "audio-speech"
| "audio-transcriptions";
export type { ComboEndpoint } from "@/shared/constants/comboEndpoints";
type CapabilityRoute = {
models?: unknown;
strategy?: unknown;
config?: unknown;
};
function isRecord(value: unknown): value is Record<string, unknown> {
return Boolean(value && typeof value === "object" && !Array.isArray(value));
}
/** Resolve an endpoint-specific plan while keeping legacy combos byte-compatible. */
export function comboForEndpoint(combo: ComboLike, endpoint: ComboEndpoint): ComboLike {
const routes = isRecord(combo.routes) ? combo.routes : null;
const route = routes && isRecord(routes[endpoint]) ? (routes[endpoint] as CapabilityRoute) : null;
if (!route) return combo;
return {
...combo,
models: Array.isArray(route.models) ? route.models : [],
strategy: typeof route.strategy === "string" ? route.strategy : combo.strategy,
config: isRecord(route.config) ? route.config : {},
capabilityRoute: endpoint,
};
}
export function targetProvider(modelStr: string, hint?: string | null): string {
const raw = hint || modelStr.split("/")[0];
@@ -36,10 +59,36 @@ export function targetSupportsEndpoint(
const provider = targetProvider(modelStr, hint);
const search = Object.hasOwn(SEARCH_PROVIDERS, provider) ? SEARCH_PROVIDERS[provider] : undefined;
const fetch = (WEB_FETCH_PROVIDERS as readonly string[]).includes(provider);
const modelId = modelStr.includes("/") ? modelStr.slice(modelStr.indexOf("/") + 1) : modelStr;
const hasModel = (config: { models?: Array<{ id?: string }> } | null): boolean | null => {
if (!config) return null;
if (!Array.isArray(config.models) || config.models.length === 0) return true;
return config.models.some((model) => model.id === modelId || model.id === modelStr);
};
if (endpoint === "search") return Boolean(search && !search.disabled);
if (endpoint === "web-fetch") return fetch;
// Dedicated web executors must never reach DefaultExecutor's OpenAI fallback.
if (search || fetch) return false;
if (endpoint === "images" || endpoint === "image-edits") {
return Boolean(getImageModelEntry(modelStr));
}
if (endpoint === "videos") return hasModel(getVideoProvider(provider)) ?? true;
if (endpoint === "music") return hasModel(getMusicProvider(provider)) ?? true;
if (endpoint === "ocr") return hasModel(getOcrProvider(provider)) ?? true;
if (endpoint === "audio-speech") return hasModel(getSpeechProvider(provider)) ?? true;
if (endpoint === "audio-transcriptions" || endpoint === "audio-translations") {
return hasModel(getTranscriptionProvider(provider)) ?? true;
}
if (endpoint === "audio-generation") return provider === "elevenlabs";
const recognizedModalTarget = [
hasModel(getVideoProvider(provider)),
hasModel(getMusicProvider(provider)),
hasModel(getOcrProvider(provider)),
hasModel(getSpeechProvider(provider)),
hasModel(getTranscriptionProvider(provider)),
getImageModelEntry(modelStr) ? true : null,
].some((supported) => supported === true);
if (endpoint === "chat" && recognizedModalTarget) return false;
// Keep existing non-web endpoint guards and opt-in modality bridges authoritative.
// Unknown/custom models cannot be rejected solely because a static catalog lacks them.
return true;
@@ -54,11 +103,14 @@ export function projectComboForEndpoint(
allCombos: Array<ComboLike & { models: ComboStep[] }>;
} {
const collection = Array.isArray(allCombos) ? allCombos : allCombos?.combos || [];
const stored = collection.map((item) => ({
...item,
name: String(item.name || ""),
models: Array.isArray(item.models) ? item.models : [],
}));
const stored = collection.map((item) => {
const normalized = {
...item,
name: String(item.name || ""),
models: Array.isArray(item.models) ? item.models : [],
} as ComboLike;
return comboForEndpoint(normalized, endpoint);
});
const project = (item: ComboLike) => ({
...item,
models: item.models
@@ -75,7 +127,7 @@ export function projectComboForEndpoint(
return Boolean(model && targetSupportsEndpoint(model, endpoint, step.providerId));
}),
});
const projected = project(combo);
const projected = project(comboForEndpoint(combo, endpoint));
const children = stored.map(project);
const byName = new Map([...children, projected].map((item) => [item.name, item]));
const compatible = new Map<string, boolean>();

View File

@@ -0,0 +1,68 @@
import { handleComboChat } from "@omniroute/open-sse/services/combo.ts";
import type { ComboLike, ResolvedComboTarget } from "@omniroute/open-sse/services/combo/types.ts";
import {
narrowMediaConnectionScope,
pinnedConnectionIds,
} from "@omniroute/open-sse/services/mediaComboFallback.ts";
import { errorResponse } from "@omniroute/open-sse/utils/error.ts";
import { getComboByName, getCombos } from "@/lib/db/combos";
import { createComboTargetKeyGate } from "@/sse/services/keyConnectionScope";
import * as log from "@/sse/utils/logger";
import type { ApiKeyPolicyResult } from "@/shared/utils/apiKeyPolicy";
import type { ComboEndpoint } from "@/shared/constants/comboEndpoints";
interface ModelEndpointComboOptions {
name: string;
endpoint: ComboEndpoint;
request: Request;
body: Record<string, unknown>;
policy: ApiKeyPolicyResult;
allowedConnections: string[] | null;
settings?: Record<string, unknown>;
dispatch: (
body: Record<string, unknown>,
allowedConnections: string[] | null,
target: ResolvedComboTarget
) => Promise<Response>;
}
function asCombo(record: Record<string, unknown>): ComboLike {
return {
...record,
name: String(record.name || ""),
models: Array.isArray(record.models) ? record.models : [],
};
}
export async function runModelEndpointCombo(options: ModelEndpointComboOptions): Promise<Response> {
try {
const record = await getComboByName(options.name);
if (!record) return errorResponse(404, "Unknown routing combo");
if (record.isActive === false) return errorResponse(400, "Routing combo is inactive");
return await handleComboChat({
body: { ...options.body, model: options.name, stream: false },
combo: asCombo(record),
endpointCapability: options.endpoint,
allCombos: (await getCombos()).map(asCombo),
settings: options.settings,
log,
signal: options.request.signal,
apiKeyAllowedConnections: options.allowedConnections,
isModelAvailable: createComboTargetKeyGate(options.policy, options.name),
handleSingleModel: async (routedBody, model, target) => {
if (options.request.signal.aborted) return errorResponse(499, "Request aborted");
if (!target || !("modelStr" in target)) {
return errorResponse(400, "Invalid capability combo target");
}
return options.dispatch(
{ ...routedBody, model },
narrowMediaConnectionScope(options.allowedConnections, pinnedConnectionIds(target)),
target
);
},
});
} catch (error) {
log.error("CAPABILITY_COMBO", "Failed to resolve endpoint combo", error);
return errorResponse(500, "Failed to resolve endpoint combo");
}
}

View File

@@ -0,0 +1,29 @@
export const COMBO_ENDPOINTS = [
"chat",
"search",
"web-fetch",
"embeddings",
"rerank",
"images",
"image-edits",
"videos",
"music",
"ocr",
"audio-speech",
"audio-transcriptions",
"audio-translations",
"audio-generation",
] as const;
export type ComboEndpoint = (typeof COMBO_ENDPOINTS)[number];
export const COMBO_NON_CHAT_STRATEGIES = [
"priority",
"fill-first",
"round-robin",
"weighted",
"random",
"strict-random",
"least-used",
"p2c",
] as const;

View File

@@ -13,6 +13,7 @@ import {
isForbiddenCustomHeaderName,
} from "@/shared/constants/upstreamHeaders";
import { MAX_TIMER_TIMEOUT_MS } from "@/shared/utils/runtimeTimeouts";
import { COMBO_ENDPOINTS, COMBO_NON_CHAT_STRATEGIES } from "@/shared/constants/comboEndpoints";
// ──── Combo Schemas ────
@@ -341,6 +342,48 @@ export const comboNameSchema = z
"Name can only contain letters, numbers, spaces, -, _, /, ., [ and ]."
);
export const comboCapabilityRouteSchema = z
.object({
models: z.array(comboModelEntry).min(1, "a capability route requires at least one model"),
strategy: comboStrategySchema.optional().default("priority"),
config: comboRuntimeConfigSchema.optional(),
})
.strict()
.superRefine(validateQuotaOnlyComboRefs);
export const comboCapabilityRoutesSchema = z
.object({
chat: comboCapabilityRouteSchema.optional(),
search: comboCapabilityRouteSchema.optional(),
"web-fetch": comboCapabilityRouteSchema.optional(),
embeddings: comboCapabilityRouteSchema.optional(),
rerank: comboCapabilityRouteSchema.optional(),
images: comboCapabilityRouteSchema.optional(),
"image-edits": comboCapabilityRouteSchema.optional(),
videos: comboCapabilityRouteSchema.optional(),
music: comboCapabilityRouteSchema.optional(),
ocr: comboCapabilityRouteSchema.optional(),
"audio-speech": comboCapabilityRouteSchema.optional(),
"audio-transcriptions": comboCapabilityRouteSchema.optional(),
"audio-translations": comboCapabilityRouteSchema.optional(),
"audio-generation": comboCapabilityRouteSchema.optional(),
})
.strict()
.superRefine((routes, ctx) => {
const supported = new Set<string>(COMBO_NON_CHAT_STRATEGIES);
for (const endpoint of COMBO_ENDPOINTS) {
if (endpoint === "chat") continue;
const route = routes[endpoint];
if (route && !supported.has(route.strategy)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Strategy ${route.strategy} is not supported for ${endpoint}`,
path: [endpoint, "strategy"],
});
}
}
});
type QuotaOnlyComboRefState = {
models?: Array<z.infer<typeof comboModelEntry>>;
strategy?: string;
@@ -400,6 +443,7 @@ export const createComboSchema = z
models: z.array(comboModelEntry).min(1, "a combo requires at least one model"),
strategy: comboStrategySchema.optional().default("priority"),
config: comboRuntimeConfigSchema.optional(),
routes: comboCapabilityRoutesSchema.optional(),
universal_handoff: universalHandoffSchema.optional(),
universalHandoff: universalHandoffSchema.optional(),
allowedProviders: z.array(z.string().trim().min(1).max(200)).max(100).optional(),
@@ -470,6 +514,7 @@ export const updateComboSchema = z
.optional(),
strategy: comboStrategySchema.optional(),
config: comboRuntimeConfigSchema.optional(),
routes: comboCapabilityRoutesSchema.optional().nullable(),
isActive: z.boolean().optional(),
// Stored on the combo record and honoured by the readers — the builder's
// option list and the dashboard grid both filter on it — but omitted here,
@@ -506,6 +551,7 @@ export const updateComboSchema = z
value.models === undefined &&
value.strategy === undefined &&
value.config === undefined &&
value.routes === undefined &&
value.isActive === undefined &&
value.isHidden === undefined &&
value.allowedProviders === undefined &&

View File

@@ -0,0 +1,36 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
orderCapabilityTargets,
resetCapabilityTargetOrderForTests,
} from "../../src/lib/combos/capabilityTargetOrder.ts";
const targets = [
{ modelStr: "music/a", weight: 10 },
{ modelStr: "music/b", weight: 90 },
];
test.beforeEach(resetCapabilityTargetOrderForTests);
test("round-robin rotates independently per capability", () => {
const next = (endpoint: "music" | "images") =>
orderCapabilityTargets({ comboName: "media", endpoint, strategy: "round-robin", targets })[0]
.modelStr;
assert.equal(next("music"), "music/a");
assert.equal(next("music"), "music/b");
assert.equal(next("images"), "music/a");
});
test("weighted routing places the selected target first and retains fallback", () => {
const ordered = orderCapabilityTargets({
comboName: "media",
endpoint: "music",
strategy: "weighted",
targets,
random: () => 0.5,
});
assert.deepEqual(
ordered.map((target) => target.modelStr),
["music/b", "music/a"]
);
});

View File

@@ -49,6 +49,28 @@ test("static media registry models are selectable for a configured provider with
assert.ok(transcription, "a configured provider's static audio model must be selectable");
});
test("native sound and music models expose their capability tags in the builder", async () => {
await providersDb.createProviderConnection({
provider: "elevenlabs",
authType: "apikey",
name: "elevenlabs-combo-media",
apiKey: "elevenlabs-key-combo-media",
isActive: true,
testStatus: "active",
});
const payload = await getComboBuilderOptions();
const provider = payload.providers.find((entry) => entry.providerId === "elevenlabs");
assert.ok(provider, "configured ElevenLabs must appear in the combo builder");
assert.deepEqual(
provider.models.find((model) => model.id === "eleven_text_to_sound_v2")?.supportedEndpoints,
["audio-generation"]
);
assert.deepEqual(provider.models.find((model) => model.id === "music_v1")?.supportedEndpoints, [
"music",
]);
});
test("default chat metadata on a legacy custom row does not erase static image metadata", async () => {
await modelsDb.addCustomModel(
"openrouter",

View File

@@ -0,0 +1,69 @@
import assert from "node:assert/strict";
import test from "node:test";
import { createComboSchema, updateComboSchema } from "../../src/shared/validation/schemas/combo.ts";
import {
comboWithCapabilityRouteSteps,
normalizeComboCapabilityRoutes,
} from "../../src/lib/combos/capabilityRoutes.ts";
test("combo schemas accept independent capability plans", () => {
const input = {
name: "multi-surface",
models: ["openai/gpt-4o"],
strategy: "priority",
routes: {
chat: { models: ["openai/gpt-4o"], strategy: "priority" },
music: {
models: ["elevenlabs/music_v1", "vertex/lyria-002"],
strategy: "round-robin",
config: { maxRetries: 0 },
},
},
};
assert.equal(createComboSchema.safeParse(input).success, true);
assert.equal(updateComboSchema.safeParse({ routes: input.routes }).success, true);
});
test("combo schemas reject unknown capabilities and empty plans", () => {
assert.equal(
updateComboSchema.safeParse({ routes: { unknown: { models: ["openai/gpt-4o"] } } }).success,
false
);
assert.equal(
updateComboSchema.safeParse({ routes: { music: { models: [], strategy: "priority" } } })
.success,
false
);
assert.equal(
updateComboSchema.safeParse({
routes: { music: { models: ["elevenlabs/music_v1"], strategy: "fusion" } },
}).success,
false
);
});
test("normalization preserves account pins and exposes route refs to DAG validation", () => {
const routes = normalizeComboCapabilityRoutes(
{
search: {
strategy: "fill-first",
models: [
{ kind: "combo-ref", comboName: "search-child" },
{ providerId: "tavily-search", model: "web", connectionId: "account-a" },
],
},
},
{ comboName: "root", allCombos: [{ name: "search-child" }] as never }
);
assert.equal(routes?.search?.models[1].kind, "model");
assert.equal(
routes?.search?.models[1].kind === "model" && routes.search.models[1].connectionId,
"account-a"
);
const combined = comboWithCapabilityRouteSteps({
name: "root",
models: ["openai/gpt-4o"],
routes,
});
assert.equal(combined.models.length, 3);
});

View File

@@ -83,3 +83,35 @@ test("PUT with overrideAllowedProviders on a combo with an EXISTING restriction
const stored = (await combosDb.getComboById(combo.id)) as { allowedProviders?: string[] };
assert.deepEqual([...(stored.allowedProviders ?? [])].sort(), ["claude", "openai"]);
});
test("PUT persists capability routes and widens an existing provider restriction from their targets", async () => {
const combo = await combosDb.createCombo({
name: "restricted-capability-combo",
strategy: "priority",
allowedProviders: ["claude"],
models: [{ provider: "claude", model: "claude-sonnet-5" }],
});
assert.ok(combo?.id);
const response = await comboRoute.PUT(
put(combo.id, {
routes: {
search: {
strategy: "round-robin",
models: [{ providerId: "tavily-search", model: "web" }],
},
},
overrideAllowedProviders: true,
}),
{ params: Promise.resolve({ id: combo.id }) }
);
assert.equal(response.status, 200, await response.clone().text());
const stored = (await combosDb.getComboById(combo.id)) as {
allowedProviders?: string[];
routes?: { search?: { strategy?: string; models?: Array<{ providerId?: string }> } };
};
assert.deepEqual([...(stored.allowedProviders ?? [])].sort(), ["claude", "tavily-search"]);
assert.equal(stored.routes?.search?.strategy, "round-robin");
assert.equal(stored.routes?.search?.models?.[0]?.providerId, "tavily-search");
});

View File

@@ -13,7 +13,10 @@ const { getAllMusicModels, parseMusicModel } =
await import("../../open-sse/config/musicRegistry.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const apiKeysDb = await import("../../src/lib/db/apiKeys.ts");
const combosDb = await import("../../src/lib/db/combos.ts");
const musicRoute = await import("../../src/app/api/v1/music/generations/route.ts");
const nativeMusicRoute = await import("../../src/app/api/v1/music/route.ts");
const nativeSoundRoute = await import("../../src/app/api/v1/sound-generation/route.ts");
const originalFetch = globalThis.fetch;
const credentials = { apiKey: "stored-elevenlabs-test-key" };
const generate = (body: Record<string, unknown>, connection = credentials) =>
@@ -174,3 +177,139 @@ test("unified music route discovers ElevenLabs and generates with the scoped sto
assert.ok(catalog.data.some((entry: { id: string }) => entry.id === `elevenlabs/${model}`));
}
});
test("unified music route applies an endpoint-specific round-robin combo", async () => {
const connection = await providersDb.createProviderConnection({
provider: "elevenlabs",
authType: "apikey",
name: "ElevenLabs combo test",
apiKey: credentials.apiKey,
isActive: true,
testStatus: "active",
providerSpecificData: {},
});
await combosDb.createCombo({
name: "music-round-robin",
strategy: "priority",
models: ["openai/gpt-4o"],
routes: {
music: {
strategy: "round-robin",
models: ["elevenlabs/music_v1", "elevenlabs/music_v2"],
},
},
});
const key = await apiKeysDb.createApiKey("music combo client", "music-combo-machine", [], {
allowedConnections: [String(connection.id)],
});
const seen: string[] = [];
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
seen.push(JSON.parse(String(init?.body)).model_id);
return new Response("ID3music", { headers: { "content-type": "audio/mpeg" } });
}) as typeof fetch;
for (let requestIndex = 0; requestIndex < 2; requestIndex += 1) {
const response = await musicRoute.POST(
new Request("http://localhost/v1/music/generations", {
method: "POST",
headers: {
Authorization: `Bearer ${key.key}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "music-round-robin",
prompt: "Calm piano",
duration: 3,
}),
}),
{} as never
);
assert.equal(response.status, 200, await response.clone().text());
}
assert.deepEqual(seen, ["music_v1", "music_v2"]);
});
test("native music endpoint preserves binary output when model_id names a combo", async () => {
const connection = await providersDb.createProviderConnection({
provider: "elevenlabs",
authType: "apikey",
name: "ElevenLabs native combo test",
apiKey: credentials.apiKey,
isActive: true,
testStatus: "active",
providerSpecificData: {},
});
await combosDb.createCombo({
name: "native-music-combo",
strategy: "priority",
models: ["openai/gpt-4o"],
routes: {
music: { strategy: "priority", models: ["elevenlabs/music_v2_5"] },
},
});
const key = await apiKeysDb.createApiKey("native combo client", "native-combo-machine", [], {
allowedConnections: [String(connection.id)],
});
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
assert.equal(JSON.parse(String(init?.body)).model_id, "music_v2_5");
return new Response("native-audio", { headers: { "content-type": "audio/mpeg" } });
}) as typeof fetch;
const response = await nativeMusicRoute.POST(
new Request("http://localhost/v1/music", {
method: "POST",
headers: {
Authorization: `Bearer ${key.key}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ model_id: "native-music-combo", prompt: "Calm piano" }),
})
);
assert.equal(response.status, 200, await response.clone().text());
assert.equal(await response.text(), "native-audio");
});
test("native sound endpoint applies its audio-generation capability route", async () => {
const connection = await providersDb.createProviderConnection({
provider: "elevenlabs",
authType: "apikey",
name: "ElevenLabs native sound combo test",
apiKey: credentials.apiKey,
isActive: true,
testStatus: "active",
providerSpecificData: {},
});
await combosDb.createCombo({
name: "native-sound-combo",
strategy: "priority",
models: ["openai/gpt-4o"],
routes: {
"audio-generation": {
strategy: "priority",
models: ["elevenlabs/eleven_text_to_sound_v2"],
},
},
});
const key = await apiKeysDb.createApiKey("sound combo client", "sound-combo-machine", [], {
allowedConnections: [String(connection.id)],
});
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
assert.equal(JSON.parse(String(init?.body)).model_id, "eleven_text_to_sound_v2");
return new Response("native-sound", { headers: { "content-type": "audio/mpeg" } });
}) as typeof fetch;
const response = await nativeSoundRoute.POST(
new Request("http://localhost/v1/sound-generation", {
method: "POST",
headers: {
Authorization: `Bearer ${key.key}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model_id: "native-sound-combo",
text: "A soft bell",
duration_seconds: 1,
}),
})
);
assert.equal(response.status, 200, await response.clone().text());
assert.equal(await response.text(), "native-sound");
});

View File

@@ -41,6 +41,9 @@ test("capability matching uses registries rather than a model name containing se
assert.equal(targetSupportsEndpoint("jina-reader/fetch", "chat"), false);
assert.equal(targetSupportsEndpoint("unknown-compatible/custom", "chat"), true);
assert.equal(targetSupportsEndpoint("toString/web", "search"), false);
assert.equal(targetSupportsEndpoint("elevenlabs/music_v1", "music"), true);
assert.equal(targetSupportsEndpoint("elevenlabs/music_v1", "chat"), false);
assert.equal(targetSupportsEndpoint("elevenlabs/not-a-music-model", "music"), false);
});
test("nested combo projection preserves step identity, account pins and priority", () => {
@@ -80,3 +83,68 @@ test("empty nested capability branches are pruned without hiding invalid cycles"
const cycle = { name: "cycle", models: ["cycle"] };
assert.equal(projectComboForEndpoint(cycle, [cycle], "search").combo.models.length, 1);
});
test("a capability route overrides the legacy chat plan without mutating the combo", () => {
const combo = {
name: "multi-surface",
strategy: "priority",
models: ["openai/gpt-4o"],
config: { maxRetries: 1 },
routes: {
music: {
strategy: "round-robin",
models: ["elevenlabs/music_v1", "suno/chirp-v4"],
config: { maxRetries: 0 },
},
},
};
const snapshot = JSON.stringify(combo);
const chat = projectComboForEndpoint(combo, [combo], "chat");
const music = projectComboForEndpoint(combo, [combo], "music");
assert.equal(chat.combo.strategy, "priority");
assert.deepEqual(
chat.combo.models.map((step) => step.model),
["openai/gpt-4o"]
);
assert.equal(music.combo.strategy, "round-robin");
assert.deepEqual(
music.combo.models.map((step) => step.model),
["elevenlabs/music_v1", "suno/chirp-v4"]
);
assert.deepEqual(music.combo.config, { maxRetries: 0 });
assert.equal(JSON.stringify(combo), snapshot);
});
test("nested combos select their own route for the requested capability", () => {
const child = {
name: "child",
models: ["openai/gpt-4o"],
routes: {
search: {
strategy: "priority",
models: ["tavily-search/web"],
},
},
};
const root = {
name: "root",
models: ["openai/gpt-4o-mini"],
routes: {
search: {
strategy: "fill-first",
models: [{ kind: "combo-ref", comboName: "child" }],
},
},
};
const projected = projectComboForEndpoint(root, [root, child], "search");
assert.equal(projected.combo.strategy, "fill-first");
assert.equal(projected.combo.models[0].kind, "combo-ref");
const projectedChild = projected.allCombos.find((combo) => combo.name === "child")!;
assert.deepEqual(
projectedChild.models.map((step) => step.model),
["tavily-search/web"]
);
});