Files
OmniRoute/open-sse/services/roleNormalizer.ts
diegosouzapw 33dfbf0177 refactor: harden open-sse services, eliminate any casts, add dashboard pages
- Replace all `as any` casts in MCP advancedTools with typed helpers (toRecord, toString, toNumber)
- Harden open-sse services: rateLimitManager, sessionManager, usage, roleNormalizer, signatureCache, comboMetrics
- Improve responseSanitizer and responseTranslator type safety
- Remove deprecated openai-responses request translator
- Add dashboard pages: /a2a, /mcp, /auto-combo with live data
- Improve error/loading/not-found pages with consistent design
- Add root loading.tsx and typecheck tsconfig variants
- Add check-t11-any-budget.mjs audit script
2026-03-04 19:38:34 -03:00

195 lines
6.2 KiB
TypeScript

/**
* Role Normalizer — Converts message roles for provider compatibility.
*
* Fixes Issues:
* 1. GLM/ZhipuAI rejects `system` role → merged into first `user` message
* 2. OpenAI `developer` role not understood by non-OpenAI providers → normalized to `system`
* 3. Some providers don't support `system` role at all → prepended to user message
*
* Provider capability matrix is defined here rather than in the registry to
* avoid breaking changes to the existing RegistryEntry interface.
*/
// ── Provider capabilities ──────────────────────────────────────────────────
/**
* Providers that do NOT support the `system` role in messages.
* For these, system messages are merged into the first user message.
*
* Note: This applies only to OpenAI-format passthrough providers.
* Claude and Gemini have their own system message handling in dedicated translators.
*/
const PROVIDERS_WITHOUT_SYSTEM_ROLE = new Set([
// Known to reject system role (from troubleshooting report)
// GLM uses Claude format, so this is handled through claude translator
// But if accessed through OpenAI-format providers like nvidia, it needs this:
]);
/**
* Models that are known to reject the `system` role regardless of provider.
* Uses prefix matching (e.g., "glm-" matches "glm-4.7", "glm-4.5", etc.)
*/
const MODELS_WITHOUT_SYSTEM_ROLE = [
"glm-", // ZhipuAI GLM models
"ernie-", // Baidu ERNIE models
];
interface MessageContentPart {
type?: string;
text?: string;
[key: string]: unknown;
}
interface NormalizedMessage {
role?: string;
content?: unknown;
[key: string]: unknown;
}
function extractTextFromContent(content: unknown): string {
if (typeof content === "string") return content;
if (!Array.isArray(content)) return "";
return content
.filter(
(part): part is MessageContentPart =>
!!part &&
typeof part === "object" &&
"type" in part &&
(part as MessageContentPart).type === "text"
)
.map((part) => (typeof part.text === "string" ? part.text : ""))
.join("\n");
}
/**
* Check if a provider+model combo supports the system role.
*/
function supportsSystemRole(provider: string, model: string): boolean {
if (PROVIDERS_WITHOUT_SYSTEM_ROLE.has(provider)) return false;
const modelLower = (model || "").toLowerCase();
for (const prefix of MODELS_WITHOUT_SYSTEM_ROLE) {
if (modelLower.startsWith(prefix)) return false;
}
return true;
}
/**
* Normalize the `developer` role to `system` for non-OpenAI providers.
* OpenAI introduced `developer` as a replacement for `system` in newer models,
* but most other providers still expect `system`.
*
* @param messages - Array of messages
* @param targetFormat - The target format (e.g., "openai", "claude", "gemini")
* @returns Modified messages array
*/
export function normalizeDeveloperRole(
messages: NormalizedMessage[] | unknown,
targetFormat: string
): NormalizedMessage[] | unknown {
if (!Array.isArray(messages)) return messages;
// For OpenAI format, keep developer role as-is (it's valid)
// For all other formats, convert developer → system
if (targetFormat === "openai") return messages;
return messages.map((msg: NormalizedMessage) => {
if (msg.role === "developer") {
return { ...msg, role: "system" };
}
return msg;
});
}
/**
* Convert `system` messages to user messages for providers that don't support
* the system role. The system content is prepended to the first user message
* with a clear delimiter.
*
* @param messages - Array of messages
* @param provider - Provider name
* @param model - Model name
* @returns Modified messages array
*/
export function normalizeSystemRole(
messages: NormalizedMessage[] | unknown,
provider: string,
model: string
): NormalizedMessage[] | unknown {
if (!Array.isArray(messages) || messages.length === 0) return messages;
if (supportsSystemRole(provider, model)) return messages;
// Extract system messages
const systemMessages = messages.filter(
(message: NormalizedMessage) => message.role === "system" || message.role === "developer"
);
if (systemMessages.length === 0) return messages;
// Build system content string
const systemContent = systemMessages
.map((message: NormalizedMessage) => extractTextFromContent(message.content))
.filter(Boolean)
.join("\n\n");
if (!systemContent) {
return messages.filter(
(message: NormalizedMessage) => message.role !== "system" && message.role !== "developer"
);
}
// Remove system messages and merge into first user message
const nonSystemMessages = messages.filter(
(message: NormalizedMessage) => message.role !== "system" && message.role !== "developer"
);
// Find first user message and prepend system content
const firstUserIdx = nonSystemMessages.findIndex(
(message: NormalizedMessage) => message.role === "user"
);
if (firstUserIdx >= 0) {
const userMsg = nonSystemMessages[firstUserIdx];
const userContent = extractTextFromContent(userMsg.content);
nonSystemMessages[firstUserIdx] = {
...userMsg,
content: `[System Instructions]\n${systemContent}\n\n[User Message]\n${userContent}`,
};
} else {
// No user message found — insert as a user message at the beginning
nonSystemMessages.unshift({
role: "user",
content: `[System Instructions]\n${systemContent}`,
});
}
return nonSystemMessages;
}
/**
* Full role normalization pipeline.
* Call this before sending the request to the provider.
*
* @param messages - Array of messages
* @param provider - Provider name/id
* @param model - Model name
* @param targetFormat - Target API format
* @returns Normalized messages array
*/
export function normalizeRoles(
messages: NormalizedMessage[] | unknown,
provider: string,
model: string,
targetFormat: string
): NormalizedMessage[] | unknown {
if (!Array.isArray(messages)) return messages;
// Step 1: Normalize developer → system (for non-OpenAI formats)
let result = normalizeDeveloperRole(messages, targetFormat);
// Step 2: Normalize system → user (for providers that don't support system role)
result = normalizeSystemRole(result, provider, model);
return result;
}