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

@@ -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) }
});
}