Files
2026-07-13 13:39:12 +08:00

37 lines
943 B
TypeScript

const OPENAI_FINISH_REASONS = new Set([
"stop",
"length",
"tool_calls",
"content_filter",
"function_call",
]);
const SAFETY_FINISH_REASONS = new Set([
"safety",
"recitation",
"blocklist",
"prohibited_content",
"content_filtered",
"policy_violation",
"malformed_response",
]);
export function normalizeOpenAICompatibleFinishReason(value: unknown): unknown {
if (typeof value !== "string") return value;
const normalized = value.toLowerCase();
if (OPENAI_FINISH_REASONS.has(normalized)) return normalized;
if (normalized === "max_tokens") return "length";
if (SAFETY_FINISH_REASONS.has(normalized)) return "content_filter";
return normalized;
}
export function normalizeOpenAICompatibleFinishReasonString(
value: unknown,
fallback = "stop"
): string {
const normalized = normalizeOpenAICompatibleFinishReason(value);
return typeof normalized === "string" && normalized ? normalized : fallback;
}