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

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

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

80 lines
3.6 KiB
TypeScript

/**
* chatCore wire target-format resolver (Quality Gate v2 / Fase 9 — chatCore god-file
* decomposition, #3501).
*
* Pure resolution of the provider alias + the upstream target format used to translate the request.
* Model/custom overrides win first. A declared connection-level alternate protocol wins next. A
* Responses-shaped inbound request otherwise keeps the Responses wire format, except for custom
* OpenAI-compatible connections explicitly configured for Chat.
* AgentRouter may inherit the inbound protocol when no explicit connection override exists.
* Returns both `alias` (reused by the handler when stripping the `alias/` prefix off the upstream
* model id) and `targetFormat`.
*/
import { PROVIDER_ID_TO_ALIAS, getModelTargetFormat } from "../../config/providerModels.ts";
import { getRegistryEntry } from "../../config/providerRegistry.ts";
import { resolveAlternateFormat } from "../../config/providers/alternateFormats.ts";
import { getTargetFormat } from "../../services/provider.ts";
import { FORMATS } from "../../translator/formats.ts";
export function resolveChatCoreTargetFormat(opts: {
provider: string;
resolvedModel: string;
apiFormat: string | undefined;
sourceFormat?: string;
customModelTargetFormat: string | undefined;
providerSpecificData: unknown;
nativeXaiResponsesPassthrough?: boolean;
nativeOpenAICompatibleResponsesPassthrough?: boolean;
}) {
const {
provider,
resolvedModel,
apiFormat,
sourceFormat,
customModelTargetFormat,
providerSpecificData,
nativeXaiResponsesPassthrough = false,
nativeOpenAICompatibleResponsesPassthrough = false,
} = opts;
const alias = PROVIDER_ID_TO_ALIAS[provider] || provider;
const modelTargetFormat = getModelTargetFormat(alias, resolvedModel);
const explicitConnectionTargetFormat = (
providerSpecificData as { targetFormat?: unknown } | null | undefined
)?.targetFormat;
const inferredAgentRouterTargetFormat =
provider === "agentrouter" &&
!(typeof explicitConnectionTargetFormat === "string" && explicitConnectionTargetFormat) &&
(sourceFormat === FORMATS.OPENAI_RESPONSES ||
sourceFormat === FORMATS.OPENAI ||
sourceFormat === FORMATS.CLAUDE)
? sourceFormat
: undefined;
const providerTargetFormat = getTargetFormat(provider, providerSpecificData);
const declaredConnectionAlternate = resolveAlternateFormat(
getRegistryEntry(provider),
providerSpecificData
);
const customOpenAICompatible = provider.startsWith("openai-compatible-");
// #8994: model-level targetFormat overrides (from registry or custom-model DB override)
// take precedence over apiFormat="responses" — otherwise Vertex Claude models with
// targetFormat="claude" get wrongly routed to OpenAI Responses format.
// #9161: a custom OpenAI-compatible Chat connection must likewise keep its configured
// outbound protocol when a Responses-shaped client (for example Codex) calls /responses.
// Registry-declared connection alternates are equally explicit: a DeepSeek connection set to
// Anthropic must stay on /anthropic/v1/messages even when the caller speaks Responses.
let targetFormat =
modelTargetFormat ||
customModelTargetFormat ||
declaredConnectionAlternate?.format ||
(apiFormat === "responses" && !customOpenAICompatible
? FORMATS.OPENAI_RESPONSES
: inferredAgentRouterTargetFormat || providerTargetFormat);
if (nativeXaiResponsesPassthrough || nativeOpenAICompatibleResponsesPassthrough) {
targetFormat = FORMATS.OPENAI_RESPONSES;
}
return { alias, targetFormat };
}
export type ChatCoreTargetFormat = ReturnType<typeof resolveChatCoreTargetFormat>;