mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-06 23:32:12 +03:00
Validated in local merge-train (devbox-vm-06-dev002) @ combined-tip (FAST gates — only pre-existing audit.test.ts flake).
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { restoreOpenAIToolNames } from "../translator/helpers/toolCallHelper.ts";
|
|
|
|
type JsonRecord = Record<string, unknown>;
|
|
|
|
export function normalizeFinalOpenAIStreamChunk(
|
|
parsed: JsonRecord,
|
|
toolNameMap: unknown
|
|
): { changed: boolean; hasFinishReason: boolean } {
|
|
let changed = false;
|
|
if (parsed.id != null && typeof parsed.id !== "string") {
|
|
parsed.id = String(parsed.id);
|
|
changed = true;
|
|
}
|
|
|
|
if (Array.isArray(parsed.choices)) {
|
|
for (const choice of parsed.choices as JsonRecord[]) {
|
|
const delta = (choice as JsonRecord | null | undefined)?.delta as JsonRecord | undefined;
|
|
if (!Array.isArray(delta?.tool_calls)) continue;
|
|
for (const toolCall of delta.tool_calls as JsonRecord[]) {
|
|
if (toolCall?.id != null && typeof toolCall.id !== "string") {
|
|
toolCall.id = String(toolCall.id);
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
changed = restoreOpenAIToolNames(parsed, toolNameMap) || changed;
|
|
const firstChoice = Array.isArray(parsed.choices)
|
|
? (parsed.choices[0] as JsonRecord | undefined)
|
|
: undefined;
|
|
return { changed, hasFinishReason: Boolean(firstChoice?.finish_reason) };
|
|
}
|