fix(translator): map Claude "refusal" stop_reason to content_filter and surface its explanation

Anthropic's API-level refusal (streaming classifier / ToS) ends the stream
with stop_reason "refusal", stop_details carrying the reason, zero output
tokens and no content blocks. Map refusal to content_filter in both
directions, surface stop_details.explanation as message text, and add
CLAUDE_STOP.REFUSAL to schema.
This commit is contained in:
Welington
2026-09-22 15:25:09 +07:00
parent 1a02713150
commit 0f488c7027
5 changed files with 97 additions and 0 deletions

View File

@@ -149,6 +149,13 @@ export function claudeToOpenAIResponse(chunk, state) {
if (chunk.delta?.stop_reason) {
state.finishReason = convertStopReason(chunk.delta.stop_reason);
// A refusal produces no content blocks at all. Surface Anthropic's own
// explanation as the message text so the client shows *why* the turn is
// empty instead of a blank reply.
const refusalNote = chunk.delta.stop_reason === "refusal" && chunk.delta.stop_details?.explanation;
if (refusalNote) {
results.push(createChunk(state, { content: refusalNote }));
}
const finalChunk = createChunk(state, {}, state.finishReason);
if (state.usage) {