1
0
forked from boos/BoosAPI

fix(chatFormats): suppress content when it duplicates reasoning_content

Some upstream providers (e.g. certain OpenAI-compatible aggregators)
emit thinking/reasoning tokens with the same text written to both
delta.content and delta.reasoning_content simultaneously.

When metapi forwarded these chunks as-is, downstream clients would read
delta.content and display the internal reasoning text to end users —
appearing as word-by-word "thinking narration" in chat interfaces.

Fix: after resolving both contentDelta and reasoningDelta, suppress
contentDelta when it is non-empty and identical to reasoningDelta.
This covers the duplicate-write pattern without affecting any legitimate
case (a provider would never intentionally set content == reasoning).
This commit is contained in:
Ruichen Zhou
2026-03-06 11:28:07 +01:00
parent bf216d7011
commit 83608c3521
+9 -1
View File
@@ -736,7 +736,7 @@ export function normalizeUpstreamStreamEvent(
const delta = isRecord(choice?.delta) ? choice.delta : {}; const delta = isRecord(choice?.delta) ? choice.delta : {};
const deltaParsed = extractTextAndReasoning(delta.content ?? delta); const deltaParsed = extractTextAndReasoning(delta.content ?? delta);
const contentDelta = const rawContentDelta =
deltaParsed.content deltaParsed.content
|| (typeof choice?.message?.content === 'string' ? choice.message.content : '') || (typeof choice?.message?.content === 'string' ? choice.message.content : '')
|| ''; || '';
@@ -747,6 +747,14 @@ export function normalizeUpstreamStreamEvent(
|| deltaParsed.reasoning || deltaParsed.reasoning
|| ''; || '';
// Some upstream providers (e.g. certain OpenAI-compatible aggregators) emit thinking
// tokens with the same text duplicated in both delta.content and delta.reasoning_content.
// When the two values are identical it means content is just echoing the reasoning —
// suppress it so internal thinking is never leaked to downstream consumers.
const contentDelta = (reasoningDelta && rawContentDelta === reasoningDelta)
? ''
: rawContentDelta;
const rawToolCalls = Array.isArray((delta as any).tool_calls) const rawToolCalls = Array.isArray((delta as any).tool_calls)
? ((delta as any).tool_calls as unknown[]) ? ((delta as any).tool_calls as unknown[])
: []; : [];