fix(anthropic): non-stream /v1/messages returned OpenAI shape; strip EOS sentinel from text deltas

BUG: Anthropic client (sourceFormat=CLAUDE) hitting a claude-target provider
got a raw OpenAI chat.completion body on non-streaming requests. The
needsTranslation(CLAUDE,CLAUDE) gate is false when target===source, so the
translator never ran; a claude-transport executor replying OpenAI JSON
(opencode/big-pickle) leaked choices[]/prompt_tokens to the client, which
Anthropic SDKs cannot parse (no content[] blocks, no type:"message").

FIX: shape-aware guard toClaudeMessageShape() in nonStreamingHandler — when
sourceFormat is CLAUDE, convert any OpenAI-shape body to a proper Claude
message (type, content blocks with thinking/text/tool_use, stop_reason via
finish mapping, usage input/output tokens). Claude-shaped bodies pass through.

Also: strip <|im_end|>/<|endoftext|>/<|eot_id|> EOS sentinels from Claude
text deltas in openai-to-claude and kiro-to-claude translators — the upstream
EOS token leaks into the final text_delta (observed 'OK<|im_end|>').

Tests: anthropic-nonstream-shape.test.js (6: eos strip + shape guard incl
tool_calls→tool_use, pass-through, finish mapping). 28/28 related tests green.
This commit is contained in:
asepharyana committed 2026-09-23 15:24:15 +07:00
1 parent e0eef05b6b
commit 9fd0fb0181
5 files changed
+137 -3

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
// EOS sentinel strip for translated streaming text.
//
// Some upstream executors/providers (observed: opencode/big-pickle behind a
// claude transport) append their special end-of-stream token verbatim to the
// final text delta — e.g. "OK<|im_end|>" — because the tokenizer emits it and
// the executor's OpenAI-compatible decoder passes it through as content. An
// Anthropic client receiving such a delta renders the literal marker in the
// transcript (and a strict schema check flags the unknown character run).
// Stripping is safe: these tokens only ever appear exactly once, as a suffix
// of the final chunk, and never carry meaning in a translated stream.
const EOS_SENTINELS = [
"<|im_end|>",
"<|endoftext|>",
"<|eot_id|>",
"<|end|>",
];
/** Remove a trailing EOS sentinel from streaming text (suffix-only, once). */
export function stripEosSentinel(text) {
if (!text || typeof text !== "string") return text;
for (const sentinel of EOS_SENTINELS) {
if (text.endsWith(sentinel)) {
return text.slice(0, -sentinel.length);
}
}
// Some providers emit it mid-chunk boundary (sentinel split across two
// deltas) — strip any occurrence, since these tokens have no legitimate
// place in model output.
const hasAny = EOS_SENTINELS.find((s) => text.includes(s));
if (hasAny) return text.split(hasAny).join("");
return text;
}
@@ -15,6 +15,7 @@
*/
import { register } from "../index.js";
import { FORMATS } from "../formats.js";
import { stripEosSentinel } from "../concerns/eosStrip.js";
function stopThinkingBlock(state, results) {
if (!state.thinkingBlockStarted) return;
@@ -153,7 +154,7 @@ export function kiroToClaudeResponse(chunk, state) {
results.push({
type: "content_block_delta",
index: state.textBlockIndex,
delta: { type: "text_delta", text: delta.content },
delta: { type: "text_delta", text: stripEosSentinel(delta.content) },
});
}
@@ -3,6 +3,7 @@ import { FORMATS } from "../formats.js";
import { ROLE, CLAUDE_BLOCK, MODEL_FALLBACK } from "../schema/index.js";
import { fromOpenAIFinish } from "../concerns/finishReason.js";
import { extractReasoningText } from "../concerns/reasoning.js";
import { stripEosSentinel } from "../concerns/eosStrip.js";
// Legacy "proxy_" prefix used by older request translators. Response strips it
// defensively so tool names from such turns resolve back (e.g. proxy_Read → Read
@@ -175,7 +176,7 @@ export function openaiToClaudeResponse(chunk, state) {
results.push({
type: "content_block_delta",
index: state.textBlockIndex,
delta: { type: "text_delta", text: delta.content }
delta: { type: "text_delta", text: stripEosSentinel(delta.content) }
});
}