mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-06 23:32:12 +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
112 lines
4.2 KiB
TypeScript
112 lines
4.2 KiB
TypeScript
// tests/unit/chatcore-target-format.test.ts
|
|
// Characterization of resolveChatCoreTargetFormat — the wire target-format resolution extracted
|
|
// from handleChatCore (chatCore god-file decomposition, #3501). Resolves the provider alias and the
|
|
// upstream target format: apiFormat==="responses" forces OpenAI Responses; otherwise the model's
|
|
// registry target format, then the custom-model override, then the provider default. Returns both
|
|
// `alias` (reused downstream when stripping the alias/ prefix off the upstream model) and
|
|
// `targetFormat`.
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { resolveChatCoreTargetFormat } from "../../open-sse/handlers/chatCore/targetFormat.ts";
|
|
import {
|
|
PROVIDER_ID_TO_ALIAS,
|
|
getModelTargetFormat,
|
|
} from "../../open-sse/config/providerModels.ts";
|
|
import { getTargetFormat } from "../../open-sse/services/provider.ts";
|
|
import { FORMATS } from "../../open-sse/translator/formats.ts";
|
|
|
|
function expected(
|
|
provider: string,
|
|
resolvedModel: string,
|
|
apiFormat: string | undefined,
|
|
customModelTargetFormat: string | undefined,
|
|
providerSpecificData: unknown
|
|
) {
|
|
const alias = PROVIDER_ID_TO_ALIAS[provider] || provider;
|
|
const modelTargetFormat = getModelTargetFormat(alias, resolvedModel);
|
|
const targetFormat =
|
|
apiFormat === "responses"
|
|
? FORMATS.OPENAI_RESPONSES
|
|
: modelTargetFormat ||
|
|
customModelTargetFormat ||
|
|
getTargetFormat(provider, providerSpecificData);
|
|
return { alias, targetFormat };
|
|
}
|
|
|
|
test("apiFormat='responses' short-circuits to OPENAI_RESPONSES (alias still resolved)", () => {
|
|
const r = resolveChatCoreTargetFormat({
|
|
provider: "openai",
|
|
resolvedModel: "gpt-4o",
|
|
apiFormat: "responses",
|
|
sourceFormat: FORMATS.OPENAI,
|
|
customModelTargetFormat: undefined,
|
|
providerSpecificData: undefined,
|
|
});
|
|
assert.equal(r.targetFormat, FORMATS.OPENAI_RESPONSES);
|
|
assert.equal(r.alias, PROVIDER_ID_TO_ALIAS["openai"] || "openai");
|
|
});
|
|
|
|
test("delegates byte-identically for a normal model (no apiFormat / no custom override)", () => {
|
|
const r = resolveChatCoreTargetFormat({
|
|
provider: "openai",
|
|
resolvedModel: "gpt-4o",
|
|
apiFormat: undefined,
|
|
sourceFormat: FORMATS.OPENAI,
|
|
customModelTargetFormat: undefined,
|
|
providerSpecificData: undefined,
|
|
});
|
|
assert.deepEqual(r, expected("openai", "gpt-4o", undefined, undefined, undefined));
|
|
});
|
|
|
|
test("customModelTargetFormat is used when the model has no registry target format", () => {
|
|
const customModel = "totally-unknown-custom-model-xyz";
|
|
// precondition: the registry has no target format for this unknown model
|
|
assert.ok(!getModelTargetFormat(PROVIDER_ID_TO_ALIAS["openai"] || "openai", customModel));
|
|
const r = resolveChatCoreTargetFormat({
|
|
provider: "openai",
|
|
resolvedModel: customModel,
|
|
apiFormat: undefined,
|
|
sourceFormat: FORMATS.OPENAI,
|
|
customModelTargetFormat: "claude",
|
|
providerSpecificData: undefined,
|
|
});
|
|
assert.equal(r.targetFormat, "claude");
|
|
});
|
|
|
|
test("falls back to getTargetFormat(provider) when neither model nor custom format apply", () => {
|
|
const customModel = "totally-unknown-custom-model-xyz";
|
|
const r = resolveChatCoreTargetFormat({
|
|
provider: "openai",
|
|
resolvedModel: customModel,
|
|
apiFormat: undefined,
|
|
sourceFormat: FORMATS.OPENAI,
|
|
customModelTargetFormat: undefined,
|
|
providerSpecificData: undefined,
|
|
});
|
|
assert.equal(r.targetFormat, getTargetFormat("openai", undefined));
|
|
});
|
|
|
|
test("AgentRouter explicit connection protocol overrides the inferred inbound protocol", () => {
|
|
const r = resolveChatCoreTargetFormat({
|
|
provider: "agentrouter",
|
|
resolvedModel: "gpt-5.6-sol",
|
|
apiFormat: undefined,
|
|
sourceFormat: FORMATS.OPENAI_RESPONSES,
|
|
customModelTargetFormat: undefined,
|
|
providerSpecificData: { targetFormat: FORMATS.CLAUDE },
|
|
});
|
|
assert.equal(r.targetFormat, FORMATS.CLAUDE);
|
|
});
|
|
|
|
test("unmapped provider → alias falls back to the provider id", () => {
|
|
const r = resolveChatCoreTargetFormat({
|
|
provider: "some-unmapped-provider",
|
|
resolvedModel: "x",
|
|
apiFormat: "responses",
|
|
sourceFormat: FORMATS.OPENAI,
|
|
customModelTargetFormat: undefined,
|
|
providerSpecificData: undefined,
|
|
});
|
|
assert.equal(r.alias, "some-unmapped-provider");
|
|
});
|