Skip to main content

switchyard_translation/codecs/anthropic/
stream.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Streaming codec for Anthropic Messages events.
5
6use serde_json::{Map, Value, json};
7
8use super::buffered::decode_anthropic_refusal;
9use crate::LlmResponseChunk;
10use crate::codecs::stream::{
11    StreamCodec, StreamTranslationState, record_source_identity,
12    target_message_id_or_source_message_id, target_model_or_source_model,
13};
14use crate::format::{FormatId, WireFormat};
15use crate::util::{desanitize_anthropic_tool_use_id, sanitize_anthropic_tool_use_id};
16
17/// Stream codec for Anthropic Messages events.
18pub struct AnthropicMessagesStreamCodec;
19
20impl StreamCodec for AnthropicMessagesStreamCodec {
21    fn format(&self) -> FormatId {
22        WireFormat::AnthropicMessages.into()
23    }
24
25    fn is_terminal_event(&self, event: &Value) -> bool {
26        event.get("type").and_then(Value::as_str) == Some("message_stop")
27    }
28
29    fn stops_at_terminal_event(&self) -> bool {
30        true
31    }
32
33    fn sse_done_is_terminal(&self) -> bool {
34        false
35    }
36
37    fn stamp_streamed_response_model(&self, event: &mut Value, served_model: &str) {
38        if let Some(message) = event.get_mut("message").and_then(Value::as_object_mut) {
39            message.insert("model".to_string(), Value::String(served_model.to_string()));
40        }
41    }
42
43    fn decode_event(
44        &self,
45        state: &mut StreamTranslationState,
46        event: &Value,
47    ) -> Vec<LlmResponseChunk> {
48        decode_anthropic_stream(state, event)
49    }
50
51    fn encode_event(
52        &self,
53        state: &mut StreamTranslationState,
54        event: LlmResponseChunk,
55    ) -> Vec<Value> {
56        encode_anthropic_stream(state, event)
57    }
58
59    fn observe_replayed_event(
60        &self,
61        state: &mut StreamTranslationState,
62        raw: &Value,
63        normalized: Vec<LlmResponseChunk>,
64    ) {
65        let normalized_stop = normalized
66            .iter()
67            .any(|chunk| matches!(chunk, LlmResponseChunk::MessageStop { .. }));
68        for chunk in normalized {
69            drop(encode_anthropic_stream(state, chunk));
70        }
71
72        match raw.get("type").and_then(Value::as_str) {
73            // Anthropic carries the stop reason and usage in `message_delta`, but the separate
74            // `message_stop` event is what actually terminates the stream.
75            Some("message_delta") if normalized_stop => state.emitted_message_delta = true,
76            Some("message_stop") => state.finished = true,
77            _ => {}
78        }
79    }
80
81    fn finish(&self, state: &mut StreamTranslationState) -> Vec<Value> {
82        finish_anthropic_stream(state)
83    }
84}
85
86// Decodes one Anthropic Messages event into neutral streaming events.
87fn decode_anthropic_stream(
88    state: &mut StreamTranslationState,
89    event: &Value,
90) -> Vec<LlmResponseChunk> {
91    let Some(object) = event.as_object() else {
92        return vec![LlmResponseChunk::DecodeError {
93            message: "Anthropic stream event is not an object".to_string(),
94        }];
95    };
96    match object.get("type").and_then(Value::as_str) {
97        Some("message_start") => {
98            state.saw_message_start = true;
99            let message = object.get("message").and_then(Value::as_object);
100            if let Some(model) = message
101                .and_then(|message| message.get("model"))
102                .and_then(Value::as_str)
103            {
104                state.model = Some(model.to_string());
105            }
106            if let Some(id) = message
107                .and_then(|message| message.get("id"))
108                .and_then(Value::as_str)
109            {
110                state.message_id = Some(id.to_string());
111            }
112            if let Some(message) = message
113                && let Some(usage) = message.get("usage")
114            {
115                capture_anthropic_usage(state, usage);
116            }
117            vec![LlmResponseChunk::MessageStart {
118                id: state.message_id.clone(),
119                model: state.model.clone(),
120            }]
121        }
122        Some("content_block_start") => decode_anthropic_content_block_start(state, object),
123        Some("content_block_delta") => decode_anthropic_content_block_delta(state, object),
124        Some("content_block_stop") => {
125            let index = object.get("index").and_then(Value::as_u64).unwrap_or(0) as usize;
126            if state.empty_anthropic_tool_inputs.remove(&index) {
127                vec![LlmResponseChunk::ToolCallDelta {
128                    index,
129                    id: None,
130                    name: None,
131                    arguments_delta: Some("{}".to_string()),
132                }]
133            } else {
134                Vec::new()
135            }
136        }
137        Some("message_delta") => {
138            let mut out = Vec::new();
139            if let Some(usage) = object.get("usage") {
140                capture_anthropic_usage(state, usage);
141                out.push(LlmResponseChunk::Usage(state.usage.clone()));
142            }
143            if let Some(stop_reason) = object
144                .get("delta")
145                .and_then(Value::as_object)
146                .and_then(|delta| delta.get("stop_reason"))
147                .and_then(Value::as_str)
148            {
149                // Remember the provider stop reason: Anthropic delivers it here, on
150                // `message_delta`, while the terminal `message_stop` carries none of its own.
151                if stop_reason == "refusal" && state.stop_reason.as_deref() != Some("refusal") {
152                    out.push(LlmResponseChunk::TextDelta {
153                        index: 0,
154                        text: decode_anthropic_refusal(
155                            object
156                                .get("delta")
157                                .and_then(|delta| delta.get("stop_details")),
158                        ),
159                    });
160                }
161                state.stop_reason = Some(stop_reason.to_string());
162                out.push(LlmResponseChunk::MessageStop {
163                    reason: Some(stop_reason.to_string()),
164                });
165            }
166            out
167        }
168        // Anthropic's terminal `message_stop` carries no reason of its own; replay the one
169        // remembered from `message_delta` so a chunk accumulator keeps the real stop reason
170        // (e.g. `max_tokens`) instead of overwriting it with a reasonless `EndTurn`.
171        Some("message_stop") => vec![LlmResponseChunk::MessageStop {
172            reason: state.stop_reason.clone(),
173        }],
174        Some("error") => vec![LlmResponseChunk::StreamError {
175            message: object
176                .get("error")
177                .and_then(Value::as_object)
178                .and_then(|error| error.get("message"))
179                .and_then(Value::as_str)
180                .unwrap_or("unknown Anthropic stream error")
181                .to_string(),
182        }],
183        _ => Vec::new(),
184    }
185}
186
187// Encodes neutral streaming events into Anthropic Messages events.
188fn encode_anthropic_stream(
189    state: &mut StreamTranslationState,
190    event: LlmResponseChunk,
191) -> Vec<Value> {
192    // An in-band error is terminal: once the error is emitted, drop every later chunk.
193    if state.errored {
194        return Vec::new();
195    }
196    match event {
197        LlmResponseChunk::MessageStart { id, model } => {
198            record_source_identity(state, id, model);
199            if state.emitted_message_start {
200                Vec::new()
201            } else {
202                state.emitted_message_start = true;
203                vec![json!({
204                    "type": "message_start",
205                    "message": {
206                        "id": anthropic_message_id(state),
207                        "type": "message",
208                        "role": "assistant",
209                        "model": target_model_or_source_model(state),
210                        "content": [],
211                        "stop_reason": Value::Null,
212                        "stop_sequence": Value::Null,
213                        "usage": {"input_tokens": 0, "output_tokens": 0},
214                    },
215                })]
216            }
217        }
218        LlmResponseChunk::TextDelta { text, .. } => {
219            state.output_tokens_seen += 1;
220            let mut out = ensure_anthropic_text_block(state);
221            out.push(json!({
222                "type": "content_block_delta",
223                "index": state.text_block_index.unwrap_or(0),
224                "delta": {"type": "text_delta", "text": text},
225            }));
226            out
227        }
228        LlmResponseChunk::ReasoningDelta { text, .. } => {
229            let mut out = ensure_anthropic_reasoning_block(state);
230            out.push(json!({
231                "type": "content_block_delta",
232                "index": state.reasoning_block_index.unwrap_or(0),
233                "delta": {"type": "thinking_delta", "thinking": text},
234            }));
235            out
236        }
237        LlmResponseChunk::ReasoningDetailsDelta { text, .. } => {
238            if text.is_empty() {
239                return Vec::new();
240            }
241            let mut out = ensure_anthropic_reasoning_block(state);
242            out.push(json!({
243                "type": "content_block_delta",
244                "index": state.reasoning_block_index.unwrap_or(0),
245                "delta": {"type": "thinking_delta", "thinking": text},
246            }));
247            out
248        }
249        LlmResponseChunk::ToolCallDelta {
250            index,
251            id,
252            name,
253            arguments_delta,
254        } => encode_anthropic_tool_delta(state, index, id, name, arguments_delta),
255        LlmResponseChunk::Usage(usage) => {
256            state.usage = usage;
257            state.saw_backend_usage = true;
258            Vec::new()
259        }
260        LlmResponseChunk::MessageStop { reason } => {
261            state.stop_reason = reason.or_else(|| state.stop_reason.clone());
262            Vec::new()
263        }
264        LlmResponseChunk::StreamError { message } | LlmResponseChunk::DecodeError { message } => {
265            // An in-band error is terminal: emit the error, then nothing further.
266            state.finished = true; // finish() adds no success events
267            state.errored = true; // the entry guard drops any later chunk
268            vec![json!({"type": "error", "error": {"type": "api_error", "message": message}})]
269        }
270    }
271}
272
273// Emits any missing Anthropic terminal events and closes open content blocks.
274fn finish_anthropic_stream(state: &mut StreamTranslationState) -> Vec<Value> {
275    if state.finished {
276        return Vec::new();
277    }
278    if state.tool_states.values().any(|tool| {
279        tool.id.as_deref().is_none_or(str::is_empty)
280            || tool.name.as_deref().is_none_or(str::is_empty)
281    }) {
282        return encode_anthropic_stream(
283            state,
284            LlmResponseChunk::StreamError {
285                message: "Tool call ended without a non-empty ID and name".to_string(),
286            },
287        );
288    }
289    let mut out = Vec::new();
290    if !state.emitted_message_start {
291        out.extend(encode_anthropic_stream(
292            state,
293            LlmResponseChunk::MessageStart {
294                id: state.message_id.clone(),
295                model: state.model.clone(),
296            },
297        ));
298    }
299
300    if state.text_block_started {
301        if let Some(index) = state.text_block_index {
302            out.push(json!({"type": "content_block_stop", "index": index}));
303        }
304        state.text_block_started = false;
305    }
306
307    out.extend(close_anthropic_reasoning_block(state));
308
309    for tool in state.tool_states.values_mut() {
310        if tool.started {
311            if let Some(index) = tool.content_index {
312                out.push(json!({"type": "content_block_stop", "index": index}));
313            }
314            tool.started = false;
315        }
316    }
317
318    state.active_anthropic_tool = None;
319    for index in std::mem::take(&mut state.deferred_anthropic_tools) {
320        out.extend(encode_anthropic_tool_delta(state, index, None, None, None));
321        if let Some(tool) = state.tool_states.get_mut(&index) {
322            if let Some(content_index) = tool.content_index {
323                out.push(json!({"type": "content_block_stop", "index": content_index}));
324            }
325            tool.started = false;
326        }
327        state.active_anthropic_tool = None;
328    }
329
330    if !state.emitted_content_block
331        && !matches!(
332            state.stop_reason.as_deref(),
333            Some("refusal" | "content_filter")
334        )
335    {
336        out.push(json!({
337            "type": "content_block_start",
338            "index": 0,
339            "content_block": {"type": "text", "text": ""},
340        }));
341        out.push(json!({"type": "content_block_stop", "index": 0}));
342    }
343
344    if !state.emitted_message_delta {
345        let stop_reason = anthropic_stop_reason(state.stop_reason.as_deref());
346        let stop_details = anthropic_stop_details(&stop_reason);
347        let mut delta = json!({
348            "stop_reason": stop_reason,
349            "stop_sequence": Value::Null,
350        });
351        if !stop_details.is_null() {
352            delta["stop_details"] = stop_details;
353        }
354        out.push(json!({
355            "type": "message_delta",
356            "delta": delta,
357            "usage": anthropic_stream_usage(state),
358        }));
359        state.emitted_message_delta = true;
360    }
361    out.push(json!({"type": "message_stop"}));
362    state.finished = true;
363    out
364}
365
366// Converts Anthropic content-block starts into text or tool-call deltas.
367fn decode_anthropic_content_block_start(
368    state: &mut StreamTranslationState,
369    object: &Map<String, Value>,
370) -> Vec<LlmResponseChunk> {
371    let index = object.get("index").and_then(Value::as_u64).unwrap_or(0) as usize;
372    let block = object.get("content_block").and_then(Value::as_object);
373    match block
374        .and_then(|block| block.get("type"))
375        .and_then(Value::as_str)
376    {
377        Some("text") => block
378            .and_then(|block| block.get("text"))
379            .and_then(Value::as_str)
380            .filter(|text| !text.is_empty())
381            .map(|text| {
382                vec![LlmResponseChunk::TextDelta {
383                    index,
384                    text: text.to_string(),
385                }]
386            })
387            .unwrap_or_default(),
388        Some("thinking") => {
389            let mut out = Vec::new();
390            if let Some(text) = block
391                .and_then(|block| block.get("thinking"))
392                .and_then(Value::as_str)
393                .filter(|text| !text.is_empty())
394            {
395                out.push(LlmResponseChunk::ReasoningDelta {
396                    index,
397                    text: text.to_string(),
398                });
399            }
400            if let Some(signature) = block
401                .and_then(|block| block.get("signature"))
402                .and_then(Value::as_str)
403                .filter(|signature| !signature.is_empty())
404            {
405                out.push(anthropic_signature_delta(index, signature));
406            }
407            out
408        }
409        Some("tool_use") => {
410            let Some(block) = block else {
411                return Vec::new();
412            };
413            if block
414                .get("input")
415                .and_then(Value::as_object)
416                .is_some_and(|input| input.is_empty())
417            {
418                state.empty_anthropic_tool_inputs.insert(index);
419            }
420            vec![LlmResponseChunk::ToolCallDelta {
421                index,
422                id: block
423                    .get("id")
424                    .and_then(Value::as_str)
425                    .map(desanitize_anthropic_tool_use_id),
426                name: block
427                    .get("name")
428                    .and_then(Value::as_str)
429                    .map(ToOwned::to_owned),
430                arguments_delta: block.get("input").and_then(tool_input_delta),
431            }]
432        }
433        _ => Vec::new(),
434    }
435}
436
437// Converts Anthropic content-block deltas into neutral text or argument deltas.
438fn decode_anthropic_content_block_delta(
439    state: &mut StreamTranslationState,
440    object: &Map<String, Value>,
441) -> Vec<LlmResponseChunk> {
442    let index = object.get("index").and_then(Value::as_u64).unwrap_or(0) as usize;
443    let Some(delta) = object.get("delta").and_then(Value::as_object) else {
444        return Vec::new();
445    };
446    match delta.get("type").and_then(Value::as_str) {
447        Some("text_delta") => delta
448            .get("text")
449            .and_then(Value::as_str)
450            .map(|text| {
451                vec![LlmResponseChunk::TextDelta {
452                    index,
453                    text: text.to_string(),
454                }]
455            })
456            .unwrap_or_default(),
457        Some("thinking_delta") => delta
458            .get("thinking")
459            .and_then(Value::as_str)
460            .map(|text| {
461                vec![LlmResponseChunk::ReasoningDelta {
462                    index,
463                    text: text.to_string(),
464                }]
465            })
466            .unwrap_or_default(),
467        Some("signature_delta") => delta
468            .get("signature")
469            .and_then(Value::as_str)
470            .map(|signature| vec![anthropic_signature_delta(index, signature)])
471            .unwrap_or_default(),
472        Some("input_json_delta") => delta
473            .get("partial_json")
474            .and_then(Value::as_str)
475            .map(|partial_json| {
476                if !partial_json.is_empty() {
477                    state.empty_anthropic_tool_inputs.remove(&index);
478                }
479                vec![LlmResponseChunk::ToolCallDelta {
480                    index,
481                    id: None,
482                    name: None,
483                    arguments_delta: Some(partial_json.to_string()),
484                }]
485            })
486            .unwrap_or_default(),
487        _ => Vec::new(),
488    }
489}
490
491fn anthropic_signature_delta(index: usize, signature: &str) -> LlmResponseChunk {
492    LlmResponseChunk::ReasoningDetailsDelta {
493        index,
494        details: vec![json!({"type": "anthropic.signature_delta", "signature": signature})],
495        text: String::new(),
496    }
497}
498
499// Starts an Anthropic text block when the target stream needs one.
500fn ensure_anthropic_text_block(state: &mut StreamTranslationState) -> Vec<Value> {
501    let mut out = close_anthropic_reasoning_block(state);
502    if state.text_block_started {
503        return out;
504    }
505    let index = state.next_content_index;
506    state.next_content_index += 1;
507    state.text_block_index = Some(index);
508    state.text_block_started = true;
509    state.emitted_content_block = true;
510    out.push(json!({
511        "type": "content_block_start",
512        "index": index,
513        "content_block": {"type": "text", "text": ""},
514    }));
515    out
516}
517
518// Starts an Anthropic thinking block when the target stream needs one.
519fn ensure_anthropic_reasoning_block(state: &mut StreamTranslationState) -> Vec<Value> {
520    let mut out = Vec::new();
521    if state.text_block_started {
522        if let Some(index) = state.text_block_index {
523            out.push(json!({"type": "content_block_stop", "index": index}));
524        }
525        state.text_block_started = false;
526    }
527    if state.reasoning_block_started {
528        return out;
529    }
530    let index = state.next_content_index;
531    state.next_content_index += 1;
532    state.reasoning_block_index = Some(index);
533    state.reasoning_block_started = true;
534    state.emitted_content_block = true;
535    out.push(json!({
536        "type": "content_block_start",
537        "index": index,
538        "content_block": {"type": "thinking", "thinking": "", "signature": ""},
539    }));
540    out
541}
542
543// Closes an open Anthropic thinking block before text/tool content starts.
544fn close_anthropic_reasoning_block(state: &mut StreamTranslationState) -> Vec<Value> {
545    if !state.reasoning_block_started {
546        return Vec::new();
547    }
548    let mut out = Vec::new();
549    if let Some(index) = state.reasoning_block_index {
550        out.push(json!({
551            "type": "content_block_delta",
552            "index": index,
553            "delta": {"type": "signature_delta", "signature": ""},
554        }));
555        out.push(json!({"type": "content_block_stop", "index": index}));
556    }
557    state.reasoning_block_started = false;
558    out
559}
560
561// Accumulates tool-call state and emits Anthropic tool-use deltas.
562fn encode_anthropic_tool_delta(
563    state: &mut StreamTranslationState,
564    index: usize,
565    id: Option<String>,
566    name: Option<String>,
567    arguments_delta: Option<String>,
568) -> Vec<Value> {
569    let mut out = Vec::new();
570    out.extend(close_anthropic_reasoning_block(state));
571    if state.text_block_started {
572        if let Some(index) = state.text_block_index {
573            out.push(json!({"type": "content_block_stop", "index": index}));
574        }
575        state.text_block_started = false;
576    }
577
578    // Reserve the first call even while its name is still being assembled.
579    let other_tool_started = *state.active_anthropic_tool.get_or_insert(index) != index;
580    let tool = state.tool_states.entry(index).or_default();
581    if let Some(id) = id.filter(|id| !id.is_empty()) {
582        tool.id = Some(sanitize_anthropic_tool_use_id(&id));
583    }
584    if let Some(name) = name.filter(|name| !name.is_empty()) {
585        tool.name = Some(name);
586    }
587    if let Some(delta) = arguments_delta {
588        tool.arguments.push_str(&delta);
589        tool.pending_arguments.push_str(&delta);
590    }
591
592    // Chat can interleave calls until EOF; Anthropic callbacks require one open block.
593    if other_tool_started {
594        if !state.deferred_anthropic_tools.contains(&index) {
595            state.deferred_anthropic_tools.push(index);
596        }
597        return out;
598    }
599
600    if !tool.started {
601        // A published tool-use ID cannot be replaced when a later snapshot supplies it.
602        let (Some(id), Some(name)) = (
603            tool.id.as_deref().filter(|id| !id.is_empty()),
604            tool.name.as_deref().filter(|name| !name.is_empty()),
605        ) else {
606            return out;
607        };
608        let content_index = state.next_content_index;
609        state.next_content_index += 1;
610        tool.content_index = Some(content_index);
611        tool.started = true;
612        state.emitted_content_block = true;
613        out.push(json!({
614            "type": "content_block_start",
615            "index": content_index,
616            "content_block": {
617                "type": "tool_use",
618                "id": id,
619                "name": name,
620                "input": {},
621            },
622        }));
623        if !tool.pending_arguments.is_empty() {
624            out.push(json!({
625                "type": "content_block_delta",
626                "index": content_index,
627                "delta": {
628                    "type": "input_json_delta",
629                    "partial_json": tool.pending_arguments,
630                },
631            }));
632            tool.pending_arguments.clear();
633        }
634        return out;
635    }
636
637    if let Some(content_index) = tool.content_index
638        && !tool.pending_arguments.is_empty()
639    {
640        out.push(json!({
641            "type": "content_block_delta",
642            "index": content_index,
643            "delta": {
644                "type": "input_json_delta",
645                "partial_json": tool.pending_arguments,
646            },
647        }));
648        tool.pending_arguments.clear();
649    }
650    out
651}
652
653// Preserves Anthropic usage fields and updates normalized token counts.
654fn capture_anthropic_usage(state: &mut StreamTranslationState, usage: &Value) {
655    let Some(usage) = usage.as_object() else {
656        return;
657    };
658    if let Some(value) = usage.get("input_tokens").and_then(Value::as_u64) {
659        state.usage.input_tokens = Some(value);
660    }
661    if let Some(cost) = crate::codecs::common::provider_cost(usage) {
662        state.usage.provider_cost = Some(cost);
663    }
664    if let Some(tokens) = usage
665        .get("cache_creation")
666        .and_then(|details| details.get("ephemeral_1h_input_tokens"))
667        .and_then(Value::as_u64)
668    {
669        state
670            .usage
671            .cache
672            .get_or_insert_with(Default::default)
673            .cache_creation_1h_input_tokens = Some(tokens);
674    }
675    if let Some(value) = usage.get("output_tokens").and_then(Value::as_u64) {
676        state.usage.output_tokens = Some(value);
677    }
678    if let Some(value) = usage
679        .get("cache_creation_input_tokens")
680        .and_then(Value::as_u64)
681    {
682        state.usage.set_cache_creation_input_tokens(value);
683    }
684    if let Some(value) = usage.get("cache_read_input_tokens").and_then(Value::as_u64) {
685        state.usage.set_cached_input_tokens(value);
686    }
687    if let Some(value) = usage.get("output_tokens_details").and_then(|details| {
688        details
689            .get("thinking_tokens")
690            .and_then(Value::as_u64)
691            .or_else(|| details.get("reasoning_tokens").and_then(Value::as_u64))
692    }) {
693        state.usage.reasoning_tokens = Some(value);
694    }
695}
696
697// Builds Anthropic usage payloads from normalized and provider-extra state.
698fn anthropic_stream_usage(state: &StreamTranslationState) -> Value {
699    let mut usage = Map::new();
700    if state.saw_backend_usage {
701        if let Some(input_tokens) = state.usage.input_tokens {
702            usage.insert("input_tokens".to_string(), json!(input_tokens));
703        }
704        usage.insert(
705            "output_tokens".to_string(),
706            json!(state.usage.output_tokens.unwrap_or(0)),
707        );
708    } else {
709        usage.insert("output_tokens".to_string(), json!(state.output_tokens_seen));
710    }
711    if let Some(value) = state.usage.cache_creation_input_tokens() {
712        usage.insert("cache_creation_input_tokens".to_string(), json!(value));
713    }
714    if let Some(value) = state.usage.cached_input_tokens() {
715        usage.insert("cache_read_input_tokens".to_string(), json!(value));
716    }
717    if let Some(value) = state.usage.reasoning_tokens {
718        usage.insert(
719            "output_tokens_details".to_string(),
720            json!({"thinking_tokens": value}),
721        );
722    }
723    crate::codecs::common::encode_usage_metadata(Value::Object(usage), &state.usage)
724}
725
726// Converts any upstream message ID into an Anthropic-looking message ID.
727fn anthropic_message_id(state: &StreamTranslationState) -> String {
728    let Some(id) = target_message_id_or_source_message_id(state) else {
729        return "msg_switchyard".to_string();
730    };
731    if id.starts_with("msg_") {
732        id.to_string()
733    } else {
734        format!("msg_{id}")
735    }
736}
737
738// Maps provider stop reasons into Anthropic's stop-reason vocabulary.
739fn anthropic_stop_reason(reason: Option<&str>) -> String {
740    match reason {
741        Some("length") => "max_tokens".to_string(),
742        Some("tool_calls") | Some("function_call") => "tool_use".to_string(),
743        Some("content_filter" | "refusal") => "refusal".to_string(),
744        Some(reason @ ("end_turn" | "max_tokens" | "tool_use" | "stop_sequence")) => {
745            reason.to_string()
746        }
747        _ => "end_turn".to_string(),
748    }
749}
750
751// Emits the metadata object Anthropic pairs with a `refusal` stop reason.
752// Streaming IR does not distinguish refusal text from ordinary text.
753fn anthropic_stop_details(reason: &str) -> Value {
754    match reason {
755        "refusal" => json!({
756            "type": "refusal",
757            "category": Value::Null,
758            "explanation": Value::Null,
759        }),
760        _ => Value::Null,
761    }
762}
763
764// Converts a streamed tool input fragment into a string delta.
765fn tool_input_delta(value: &Value) -> Option<String> {
766    match value {
767        Value::String(text) if !text.is_empty() => Some(text.clone()),
768        Value::Object(object) if !object.is_empty() => serde_json::to_string(value).ok(),
769        _ => None,
770    }
771}
772
773#[cfg(test)]
774mod tests {
775    use super::*;
776
777    #[test]
778    fn recognizes_stream_completion() {
779        let codec = AnthropicMessagesStreamCodec;
780        assert!(!codec.sse_done_is_terminal());
781        assert!(codec.is_terminal_event(&json!({"type": "message_stop"})));
782        assert!(!codec.is_terminal_event(&json!({"type": "message_delta"})));
783    }
784
785    #[test]
786    fn anthropic_replay_stamps_the_served_model_inside_message() {
787        let mut event = json!({
788            "type": "message_start",
789            "message": {
790                "id": "msg_1",
791                "model": "provider/model",
792                "provider_extension": true,
793            },
794        });
795
796        AnthropicMessagesStreamCodec.stamp_streamed_response_model(&mut event, "served/model");
797
798        assert_eq!(event["message"]["model"], "served/model");
799        assert_eq!(event["message"]["provider_extension"], true);
800    }
801}