Files
9router/open-sse/translator/concerns/eosStrip.js
T
asepharyana 9fd0fb0181 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.
2026-09-23 15:24:15 +07:00

32 lines
1.3 KiB
JavaScript

// 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;
}