mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-19 05:12:16 +03:00
fix(agentrouter): infer protocol from client endpoint - /v1/responses resolves AgentRouter as openai-responses - /v1/chat/completions resolves AgentRouter as openai - /v1/messages resolves AgentRouter as claude - Per-request protocol and credential cloning (no SQLite mutation) - Codex 0.146.0 and Claude Code 2.1.220 identity alignment - response.completed.usage.total_tokens normalization for strict Codex clients Closes #9224
58 lines
2.3 KiB
TypeScript
58 lines
2.3 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:
|
|
* apiFormat==="responses" forces OpenAI Responses; otherwise the model's registry target format, then
|
|
* the per-model custom override (#2905), then AgentRouter's matching inbound protocol when the
|
|
* connection has no explicit override, then the provider default. Returns both `alias` (reused by
|
|
* the handler when stripping the `alias/` prefix off the upstream model id) and `targetFormat`.
|
|
* Side-effect-free; sits alongside the other request-setup resolvers
|
|
* (resolveChatCoreRequestSetup / resolveChatCoreRequestFormat).
|
|
*/
|
|
|
|
import { PROVIDER_ID_TO_ALIAS, getModelTargetFormat } from "../../config/providerModels.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;
|
|
}) {
|
|
const {
|
|
provider,
|
|
resolvedModel,
|
|
apiFormat,
|
|
sourceFormat,
|
|
customModelTargetFormat,
|
|
providerSpecificData,
|
|
} = 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 targetFormat =
|
|
apiFormat === "responses"
|
|
? FORMATS.OPENAI_RESPONSES
|
|
: modelTargetFormat ||
|
|
customModelTargetFormat ||
|
|
inferredAgentRouterTargetFormat ||
|
|
getTargetFormat(provider, providerSpecificData);
|
|
return { alias, targetFormat };
|
|
}
|
|
|
|
export type ChatCoreTargetFormat = ReturnType<typeof resolveChatCoreTargetFormat>;
|