Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

185 KB
Raw Blame
//! OpenAI Responses API 流式转换模块
//!
//! 实现 Responses API SSE → Anthropic SSE 格式转换。
//!
//! Responses API 使用命名事件 (named events) 的生命周期模型:
//! response.created → output_item.added → content_part.added →
//! output_text.delta → content_part.done → output_item.done → response.completed
//!
//! 与 Chat Completions 的 delta chunk 模型完全不同,需要独立的状态机处理。

use super::reasoning_bridge::{encode_openai_reasoning_item, reasoning_summary_text};
use super::streaming::hold_block_stops_until_next_event;
use super::transform_responses::{
    build_anthropic_usage_from_responses, map_responses_stop_reason,
    merge_web_search_result_metadata, responses_to_anthropic_with_web_search_options,
    sanitize_anthropic_tool_use_input_json, text_with_url_citations, web_search_action_input,
    web_search_max_uses_exceeded_error, web_search_results_from_action,
    web_search_results_from_output_item, web_search_tool_result_error,
};
use crate::proxy::sse::{strip_sse_field, SseBuffer};
use bytes::Bytes;
use futures::stream::{Stream, StreamExt};
use serde_json::{json, Value};
use std::collections::{HashMap, HashSet, VecDeque};
use std::ops::ControlFlow;

#[inline]
fn response_object_from_event(data: &Value) -> &Value {
    data.get("response").unwrap_or(data)
}

fn anthropic_sse(event_name: &str, payload: &Value) -> Bytes {
    Bytes::from(format!(
        "event: {event_name}\ndata: {}\n\n",
        serde_json::to_string(payload).unwrap_or_default()
    ))
}

fn responses_error_details(data: &Value, fallback: &str) -> (String, String) {
    let response = response_object_from_event(data);
    let error = response.get("error").unwrap_or(response);
    let message = error
        .get("message")
        .and_then(Value::as_str)
        .or_else(|| error.as_str())
        .filter(|message| !message.trim().is_empty())
        .unwrap_or(fallback)
        .to_string();
    let error_type = error
        .get("type")
        .and_then(Value::as_str)
        .or_else(|| error.get("code").and_then(Value::as_str))
        .unwrap_or("upstream_error")
        .to_string();
    (message, error_type)
}

fn anthropic_error_sse(message: &str, error_type: &str) -> Bytes {
    anthropic_sse(
        "error",
        &json!({
            "type": "error",
            "error": {"type": error_type, "message": message}
        }),
    )
}

fn anthropic_ping_sse() -> Bytes {
    anthropic_sse("ping", &json!({"type": "ping"}))
}

const UNPROCESSED_REJECTION_MESSAGE: &str = "Upstream rejected the request before processing it \
(terminal response.incomplete reported max_output_tokens with zero input/output usage). Real \
output-token truncation reports non-zero usage, so this is a request rejection — commonly caused \
by a tool schema the upstream refuses (e.g. a `pattern` regex with nested quantifiers), not by \
hitting the output-token limit.";

/// A terminal `incomplete` response that reports zero input and output usage
/// never ran on the upstream: genuine max-output-token truncation always
/// reports non-zero usage. The ChatGPT Codex backend produces exactly this
/// terminal (with `incomplete_details.reason = "max_output_tokens"`) when it
/// refuses a request before processing it, e.g. over a tool schema `pattern`
/// regex with nested quantifiers. Mapping such a terminal to
/// `stop_reason: "max_tokens"` makes clients report an output-token limit that
/// was never hit, so callers should surface it as an upstream error instead.
fn is_unprocessed_max_output_rejection(response_obj: &Value, status: Option<&str>) -> bool {
    if status != Some("incomplete") {
        return false;
    }
    if !matches!(
        response_obj
            .pointer("/incomplete_details/reason")
            .and_then(Value::as_str),
        Some("max_output_tokens") | Some("max_tokens") | None
    ) {
        return false;
    }
    response_obj
        .pointer("/usage/input_tokens")
        .and_then(Value::as_u64)
        .unwrap_or(0)
        == 0
        && response_obj
            .pointer("/usage/output_tokens")
            .and_then(Value::as_u64)
            .unwrap_or(0)
            == 0
}

/// Convert a compatible gateway's non-streaming Responses JSON into a complete
/// Anthropic SSE lifecycle. This is used when the client requested streaming but
/// the upstream ignored `stream:true` and returned `application/json`.
fn responses_json_to_anthropic_sse(
    body: Value,
    hosted_web_search_name: Option<&str>,
    max_web_search_uses: Option<u64>,
) -> Vec<Bytes> {
    // Zero-usage incomplete terminals are request rejections, not truncation;
    // decide before `body` is consumed by the conversion below.
    let unprocessed_rejection =
        is_unprocessed_max_output_rejection(&body, body.get("status").and_then(Value::as_str));
    let message = match responses_to_anthropic_with_web_search_options(
        body,
        hosted_web_search_name,
        max_web_search_uses,
    ) {
        Ok(message) => message,
        Err(error) => {
            return vec![anthropic_error_sse(
                &error.to_string(),
                "response_transform_error",
            )]
        }
    };

    let usage = message.get("usage").cloned().unwrap_or_else(|| json!({}));
    let mut start_usage = usage.clone();
    start_usage["output_tokens"] = json!(0);
    let mut events = vec![anthropic_sse(
        "message_start",
        &json!({
            "type": "message_start",
            "message": {
                "id": message.get("id").cloned().unwrap_or_else(|| json!("")),
                "type": "message",
                "role": "assistant",
                "model": message.get("model").cloned().unwrap_or_else(|| json!("")),
                "usage": start_usage
            }
        }),
    )];

    if let Some(content) = message.get("content").and_then(Value::as_array) {
        for (index, block) in content.iter().enumerate() {
            let index = index as u64;
            match block.get("type").and_then(Value::as_str) {
                Some("text") => {
                    events.push(anthropic_sse(
                        "content_block_start",
                        &json!({"type":"content_block_start","index":index,"content_block":{"type":"text","text":""}}),
                    ));
                    if let Some(text) = block.get("text").and_then(Value::as_str) {
                        if !text.is_empty() {
                            events.push(anthropic_sse(
                                "content_block_delta",
                                &json!({"type":"content_block_delta","index":index,"delta":{"type":"text_delta","text":text}}),
                            ));
                        }
                    }
                    events.push(anthropic_sse(
                        "content_block_stop",
                        &json!({"type":"content_block_stop","index":index}),
                    ));
                }
                Some("tool_use") => {
                    events.push(anthropic_sse(
                        "content_block_start",
                        &json!({
                            "type":"content_block_start",
                            "index":index,
                            "content_block":{
                                "type":"tool_use",
                                "id":block.get("id").cloned().unwrap_or_else(|| json!("")),
                                "name":block.get("name").cloned().unwrap_or_else(|| json!("")),
                                "input":{}
                            }
                        }),
                    ));
                    let input = block.get("input").cloned().unwrap_or_else(|| json!({}));
                    events.push(anthropic_sse(
                        "content_block_delta",
                        &json!({
                            "type":"content_block_delta",
                            "index":index,
                            "delta":{"type":"input_json_delta","partial_json":serde_json::to_string(&input).unwrap_or_else(|_| "{}".to_string())}
                        }),
                    ));
                    events.push(anthropic_sse(
                        "content_block_stop",
                        &json!({"type":"content_block_stop","index":index}),
                    ));
                }
                Some("server_tool_use") => {
                    events.push(anthropic_sse(
                        "content_block_start",
                        &json!({
                            "type":"content_block_start",
                            "index":index,
                            "content_block":{
                                "type":"server_tool_use",
                                "id":block.get("id").cloned().unwrap_or_else(|| json!("")),
                                "name":block.get("name").cloned().unwrap_or_else(|| json!("web_search")),
                                "input":{},
                                "caller":{"type":"direct"}
                            }
                        }),
                    ));
                    let input = block.get("input").cloned().unwrap_or_else(|| json!({}));
                    events.push(anthropic_sse(
                        "content_block_delta",
                        &json!({
                            "type":"content_block_delta",
                            "index":index,
                            "delta":{
                                "type":"input_json_delta",
                                "partial_json":serde_json::to_string(&input).unwrap_or_else(|_| "{}".to_string())
                            }
                        }),
                    ));
                    events.push(anthropic_sse(
                        "content_block_stop",
                        &json!({"type":"content_block_stop","index":index}),
                    ));
                }
                Some("web_search_tool_result") => {
                    events.push(anthropic_sse(
                        "content_block_start",
                        &json!({
                            "type":"content_block_start",
                            "index":index,
                            "content_block":block
                        }),
                    ));
                    events.push(anthropic_sse(
                        "content_block_stop",
                        &json!({"type":"content_block_stop","index":index}),
                    ));
                }
                Some("thinking") => {
                    events.push(anthropic_sse(
                        "content_block_start",
                        &json!({"type":"content_block_start","index":index,"content_block":{"type":"thinking","thinking":""}}),
                    ));
                    if let Some(thinking) = block.get("thinking").and_then(Value::as_str) {
                        if !thinking.is_empty() {
                            events.push(anthropic_sse(
                                "content_block_delta",
                                &json!({"type":"content_block_delta","index":index,"delta":{"type":"thinking_delta","thinking":thinking}}),
                            ));
                        }
                    }
                    if let Some(signature) = block.get("signature").and_then(Value::as_str) {
                        if !signature.is_empty() {
                            events.push(anthropic_sse(
                                "content_block_delta",
                                &json!({"type":"content_block_delta","index":index,"delta":{"type":"signature_delta","signature":signature}}),
                            ));
                        }
                    }
                    events.push(anthropic_sse(
                        "content_block_stop",
                        &json!({"type":"content_block_stop","index":index}),
                    ));
                }
                Some("redacted_thinking") => {
                    events.push(anthropic_sse(
                        "content_block_start",
                        &json!({"type":"content_block_start","index":index,"content_block":block}),
                    ));
                    events.push(anthropic_sse(
                        "content_block_stop",
                        &json!({"type":"content_block_stop","index":index}),
                    ));
                }
                _ => {}
            }
        }
    }

    if unprocessed_rejection
        && message
            .get("content")
            .and_then(Value::as_array)
            .is_none_or(|content| content.is_empty())
    {
        log::warn!(
            "[Claude/Responses] terminal incomplete with zero usage and no content; \
emitting api_error for the unprocessed upstream rejection instead of max_tokens"
        );
        events.push(anthropic_error_sse(
            UNPROCESSED_REJECTION_MESSAGE,
            "api_error",
        ));
        return events;
    }

    events.push(anthropic_sse(
        "message_delta",
        &json!({
            "type":"message_delta",
            "delta":{
                "stop_reason":message.get("stop_reason").cloned().unwrap_or(Value::Null),
                "stop_sequence":null
            },
            "usage":usage
        }),
    ));
    events.push(anthropic_sse(
        "message_stop",
        &json!({"type":"message_stop"}),
    ));
    events
}

#[inline]
fn content_part_key(data: &Value) -> Option<String> {
    if let (Some(item_id), Some(content_index)) = (
        data.get("item_id").and_then(|v| v.as_str()),
        data.get("content_index").and_then(|v| v.as_u64()),
    ) {
        return Some(format!("part:{item_id}:{content_index}"));
    }
    if let (Some(output_index), Some(content_index)) = (
        data.get("output_index").and_then(|v| v.as_u64()),
        data.get("content_index").and_then(|v| v.as_u64()),
    ) {
        return Some(format!("part:out:{output_index}:{content_index}"));
    }
    None
}

#[derive(Default)]
struct StreamedTextPart {
    text: String,
    output_keys: Vec<(u64, u64)>,
    item_keys: Vec<(String, u64)>,
    discarded: bool,
}

#[derive(Default)]
struct StreamedTextState {
    parts: Vec<StreamedTextPart>,
    by_output_part: HashMap<(u64, u64), usize>,
    by_item_part: HashMap<(String, u64), usize>,
    active_keyed_part: Option<usize>,
    unkeyed: String,
    unkeyed_follows_keyed: bool,
}

impl StreamedTextState {
    fn output_part_index(&self, key: (u64, u64)) -> Option<usize> {
        self.by_output_part
            .get(&key)
            .copied()
            .filter(|index| self.parts.get(*index).is_some_and(|part| !part.discarded))
    }

    fn item_part_index(&self, key: &(String, u64)) -> Option<usize> {
        self.by_item_part
            .get(key)
            .copied()
            .filter(|index| self.parts.get(*index).is_some_and(|part| !part.discarded))
    }

    fn key_pair_conflicts(
        &self,
        output_key: Option<(u64, u64)>,
        item_key: Option<&(String, u64)>,
    ) -> bool {
        let output_index = output_key.and_then(|key| self.output_part_index(key));
        let item_index = item_key.and_then(|key| self.item_part_index(key));
        if let (Some(output_index), Some(item_key)) = (output_index, item_key) {
            let part = &self.parts[output_index];
            if !part.item_keys.is_empty() && !part.item_keys.contains(item_key) {
                return true;
            }
        }
        if let (Some(item_index), Some(output_key)) = (item_index, output_key) {
            let part = &self.parts[item_index];
            if !part.output_keys.is_empty() && !part.output_keys.contains(&output_key) {
                return true;
            }
        }
        false
    }

    fn push_part(
        &mut self,
        output_key: Option<(u64, u64)>,
        item_key: Option<(String, u64)>,
    ) -> usize {
        let index = self.parts.len();
        let mut part = StreamedTextPart::default();
        if let Some(key) = output_key {
            part.output_keys.push(key);
            self.by_output_part.insert(key, index);
        }
        if let Some(key) = item_key {
            part.item_keys.push(key.clone());
            self.by_item_part.insert(key, index);
        }
        self.parts.push(part);
        index
    }

    fn bind_keys(
        &mut self,
        index: usize,
        output_key: Option<(u64, u64)>,
        item_key: Option<(String, u64)>,
    ) {
        let Some(part) = self.parts.get_mut(index).filter(|part| !part.discarded) else {
            return;
        };
        if let Some(key) = output_key {
            if !part.output_keys.contains(&key) {
                part.output_keys.push(key);
            }
            self.by_output_part.insert(key, index);
        }
        if let Some(key) = item_key {
            if !part.item_keys.contains(&key) {
                part.item_keys.push(key.clone());
            }
            self.by_item_part.insert(key, index);
        }
    }

    fn merge_part_indices(&mut self, first: usize, second: usize) -> usize {
        if first == second {
            return first;
        }
        // Parts are created by their first delta, so index order is also delta
        // arrival order. Distinct pre-alias delta streams are additive even when
        // their payload text happens to be identical.
        let (keep, discard) = if first < second {
            (first, second)
        } else {
            (second, first)
        };
        let discarded = std::mem::take(&mut self.parts[discard]);
        self.parts[discard].discarded = true;
        self.parts[keep].text.push_str(&discarded.text);
        for key in discarded.output_keys {
            if !self.parts[keep].output_keys.contains(&key) {
                self.parts[keep].output_keys.push(key);
            }
            self.by_output_part.insert(key, keep);
        }
        for key in discarded.item_keys {
            if !self.parts[keep].item_keys.contains(&key) {
                self.parts[keep].item_keys.push(key.clone());
            }
            self.by_item_part.insert(key, keep);
        }
        if self.active_keyed_part == Some(discard) {
            self.active_keyed_part = Some(keep);
        }
        keep
    }

    fn existing_keyed_part_index(
        &mut self,
        output_key: Option<(u64, u64)>,
        item_key: Option<&(String, u64)>,
    ) -> Option<usize> {
        if self.key_pair_conflicts(output_key, item_key) {
            return None;
        }
        let output_index = output_key.and_then(|key| self.output_part_index(key));
        let item_index = item_key.and_then(|key| self.item_part_index(key));
        match (output_index, item_index) {
            (Some(output), Some(item)) => Some(self.merge_part_indices(output, item)),
            (Some(index), None) | (None, Some(index)) => Some(index),
            (None, None) => None,
        }
    }

    fn resolve_keyed_part(
        &mut self,
        output_key: Option<(u64, u64)>,
        item_key: Option<(String, u64)>,
    ) -> Option<usize> {
        if self.key_pair_conflicts(output_key, item_key.as_ref()) {
            return None;
        }
        let index = self
            .existing_keyed_part_index(output_key, item_key.as_ref())
            .unwrap_or_else(|| self.push_part(output_key, item_key.clone()));
        self.bind_keys(index, output_key, item_key);
        Some(index)
    }

    fn has_part_matching_terminal(
        &self,
        full_text: &str,
        output_index: Option<u64>,
        item_id: Option<&str>,
        content_index: u64,
    ) -> bool {
        output_index.is_some_and(|index| self.output_part_index((index, content_index)).is_some())
            || item_id.is_some_and(|id| {
                self.item_part_index(&(id.to_string(), content_index))
                    .is_some()
            })
            || (!self.unkeyed.is_empty()
                && (self.unkeyed.starts_with(full_text) || full_text.starts_with(&self.unkeyed)))
    }

    fn record_delta(&mut self, data: &Value, delta: &str) {
        if delta.is_empty() {
            return;
        }
        let content_index = data.get("content_index").and_then(Value::as_u64);
        let output_key = data
            .get("output_index")
            .and_then(Value::as_u64)
            .zip(content_index);
        let item_key = data
            .get("item_id")
            .and_then(Value::as_str)
            .map(ToString::to_string)
            .zip(content_index);
        if output_key.is_some() || item_key.is_some() {
            let Some(index) = self.resolve_keyed_part(output_key, item_key) else {
                return;
            };
            self.parts[index].text.push_str(delta);
            self.active_keyed_part = Some(index);
            return;
        }
        if self.unkeyed.is_empty() {
            self.unkeyed_follows_keyed = self.active_keyed_part.is_some();
        }
        self.unkeyed.push_str(delta);
    }

    fn finish_part(&mut self, data: &Value) {
        let content_index = data.get("content_index").and_then(Value::as_u64);
        let output_key = data
            .get("output_index")
            .and_then(Value::as_u64)
            .zip(content_index);
        let item_key = data
            .get("item_id")
            .and_then(Value::as_str)
            .map(ToString::to_string)
            .zip(content_index);
        if output_key.is_none() && item_key.is_none() {
            self.active_keyed_part = None;
            return;
        }
        let finished = self.existing_keyed_part_index(output_key, item_key.as_ref());
        if finished.is_some() && finished == self.active_keyed_part {
            self.active_keyed_part = None;
        }
    }

    fn missing_suffix(
        &mut self,
        full_text: &str,
        output_index: Option<u64>,
        item_id: Option<&str>,
        content_index: u64,
    ) -> String {
        let output_key = output_index.map(|index| (index, content_index));
        let item_key = item_id.map(|id| (id.to_string(), content_index));
        if self.key_pair_conflicts(output_key, item_key.as_ref()) {
            return String::new();
        }
        let keyed_index = self.existing_keyed_part_index(output_key, item_key.as_ref());
        let emitted = keyed_index.map(|index| self.parts[index].text.clone());

        let missing = if !self.unkeyed.is_empty() {
            let unkeyed = self.unkeyed.clone();
            let combined = emitted.as_deref().map(|keyed| {
                if self.unkeyed_follows_keyed {
                    format!("{keyed}{unkeyed}")
                } else {
                    format!("{unkeyed}{keyed}")
                }
            });
            if let Some(suffix) = combined
                .as_deref()
                .and_then(|candidate| full_text.strip_prefix(candidate))
            {
                self.unkeyed.clear();
                self.unkeyed_follows_keyed = false;
                suffix.to_string()
            } else if self.unkeyed_follows_keyed
                && emitted.as_deref().is_some_and(|keyed| {
                    keyed.starts_with(full_text) || full_text.starts_with(keyed)
                })
            {
                emitted
                    .as_deref()
                    .and_then(|keyed| full_text.strip_prefix(keyed))
                    .unwrap_or_default()
                    .to_string()
            } else if combined
                .as_deref()
                .is_some_and(|candidate| candidate.starts_with(full_text))
            {
                if let Some(remaining) = unkeyed.strip_prefix(full_text) {
                    self.unkeyed = remaining.to_string();
                } else {
                    self.unkeyed.clear();
                    self.unkeyed_follows_keyed = false;
                }
                String::new()
            } else if let Some(remaining) = unkeyed.strip_prefix(full_text) {
                self.unkeyed = remaining.to_string();
                String::new()
            } else if let Some(suffix) = full_text.strip_prefix(&unkeyed) {
                self.unkeyed.clear();
                self.unkeyed_follows_keyed = false;
                if emitted.as_deref().is_some_and(|keyed| suffix == keyed) {
                    String::new()
                } else {
                    suffix.to_string()
                }
            } else if let Some(emitted) = emitted.as_deref() {
                if let Some(suffix) = full_text.strip_prefix(emitted) {
                    suffix.to_string()
                } else if emitted.starts_with(full_text) {
                    String::new()
                } else {
                    log::warn!(
                        "[Claude/Responses] Terminal text did not extend the streamed text; avoiding duplicate replay"
                    );
                    String::new()
                }
            } else {
                log::warn!(
                    "[Claude/Responses] Could not correlate terminal text with an unkeyed streamed delta; avoiding duplicate replay"
                );
                String::new()
            }
        } else if let Some(emitted) = emitted.as_deref() {
            if let Some(suffix) = full_text.strip_prefix(emitted) {
                suffix.to_string()
            } else if emitted.starts_with(full_text) {
                String::new()
            } else {
                log::warn!(
                    "[Claude/Responses] Terminal text did not extend the streamed text; avoiding duplicate replay"
                );
                String::new()
            }
        } else {
            full_text.to_string()
        };

        if output_key.is_some() || item_key.is_some() {
            let index = keyed_index.unwrap_or_else(|| self.push_part(output_key, item_key.clone()));
            self.bind_keys(index, output_key, item_key);
            self.parts[index].text = full_text.to_string();
            if self.active_keyed_part == Some(index) {
                self.active_keyed_part = None;
            }
        }
        missing
    }
}

#[derive(Clone)]
struct BufferedCitationAnnotation {
    value: Value,
    observed_text_end: usize,
    emitted: bool,
}

#[derive(Clone, Default)]
struct BufferedCitationPart {
    text: String,
    annotations: Vec<BufferedCitationAnnotation>,
    output_keys: Vec<(u64, u64)>,
    item_keys: Vec<(String, u64)>,
    emitted_bytes: usize,
    discarded: bool,
    originated_unkeyed: bool,
    received_delta: bool,
}

type BufferedCitationKeys = (Option<(u64, u64)>, Option<(String, u64)>);

enum EmittedUnkeyedTerminalMatch {
    NoMatch,
    FullyEmitted(Vec<Value>),
    MissingSuffix {
        suffix: String,
        emitted_annotations: Vec<Value>,
    },
}

#[derive(Default)]
struct BufferedCitationTextState {
    parts: Vec<BufferedCitationPart>,
    open_part: Option<usize>,
    last_unkeyed_part: Option<usize>,
    emitted_unkeyed_history: String,
    emitted_unkeyed_annotations: Vec<BufferedCitationAnnotation>,
    emitted_output_parts: HashSet<(u64, u64)>,
    emitted_item_parts: HashSet<(String, u64)>,
}

impl BufferedCitationTextState {
    fn keys(data: &Value) -> BufferedCitationKeys {
        let content_index = data.get("content_index").and_then(Value::as_u64);
        let output_key = data
            .get("output_index")
            .and_then(Value::as_u64)
            .zip(content_index);
        let item_key = data
            .get("item_id")
            .and_then(Value::as_str)
            .map(ToString::to_string)
            .zip(content_index);
        (output_key, item_key)
    }

    fn append_annotation(part: &mut BufferedCitationPart, annotation: &Value) {
        let observed_text_end = part.text.chars().count();
        if !part.annotations.iter().any(|buffered| {
            buffered.value == *annotation && buffered.observed_text_end == observed_text_end
        }) {
            part.annotations.push(BufferedCitationAnnotation {
                value: annotation.clone(),
                observed_text_end,
                emitted: false,
            });
        }
    }

    fn pending_annotation_values(part: &BufferedCitationPart) -> Vec<Value> {
        let mut values = Vec::new();
        for annotation in part
            .annotations
            .iter()
            .filter(|annotation| !annotation.emitted)
        {
            if !values.contains(&annotation.value) {
                values.push(annotation.value.clone());
            }
        }
        values
    }

    fn has_pending_output(part: &BufferedCitationPart) -> bool {
        part.emitted_bytes < part.text.len()
            || part
                .annotations
                .iter()
                .any(|annotation| !annotation.emitted)
    }

    fn push_part(
        &mut self,
        output_key: Option<(u64, u64)>,
        item_key: Option<(String, u64)>,
        originated_unkeyed: bool,
    ) -> usize {
        let mut part = BufferedCitationPart {
            originated_unkeyed,
            ..BufferedCitationPart::default()
        };
        if let Some(key) = output_key {
            part.output_keys.push(key);
        }
        if let Some(key) = item_key {
            part.item_keys.push(key);
        }
        self.parts.push(part);
        self.parts.len() - 1
    }

    fn output_part_index(&self, key: (u64, u64)) -> Option<usize> {
        self.parts
            .iter()
            .rposition(|part| !part.discarded && part.output_keys.contains(&key))
    }

    fn item_part_index(&self, key: &(String, u64)) -> Option<usize> {
        self.parts
            .iter()
            .rposition(|part| !part.discarded && part.item_keys.contains(key))
    }

    fn key_pair_conflicts(
        &self,
        output_key: Option<(u64, u64)>,
        item_key: Option<&(String, u64)>,
    ) -> bool {
        let output_index = output_key.and_then(|key| self.output_part_index(key));
        let item_index = item_key.and_then(|key| self.item_part_index(key));
        if let (Some(output_index), Some(item_key)) = (output_index, item_key) {
            let part = &self.parts[output_index];
            if !part.item_keys.is_empty() && !part.item_keys.contains(item_key) {
                return true;
            }
        }
        if let (Some(item_index), Some(output_key)) = (item_index, output_key) {
            let part = &self.parts[item_index];
            if !part.output_keys.is_empty() && !part.output_keys.contains(&output_key) {
                return true;
            }
        }
        false
    }

    fn mark_part_emitted(&mut self, index: usize) {
        if self.parts.get(index).is_none_or(|part| part.discarded) {
            return;
        }
        let output_keys = self.parts[index].output_keys.clone();
        let item_keys = self.parts[index].item_keys.clone();
        self.parts[index].emitted_bytes = self.parts[index].text.len();
        for annotation in &mut self.parts[index].annotations {
            annotation.emitted = true;
        }
        self.emitted_output_parts.extend(output_keys);
        self.emitted_item_parts.extend(item_keys);
        if self.open_part == Some(index) {
            self.open_part = None;
            if self.parts[index].originated_unkeyed {
                self.last_unkeyed_part = Some(index);
            }
        }
    }

    fn bind_keys(
        &mut self,
        index: usize,
        output_key: Option<(u64, u64)>,
        item_key: Option<(String, u64)>,
    ) {
        let Some(part) = self.parts.get_mut(index).filter(|part| !part.discarded) else {
            return;
        };
        if let Some(key) = output_key {
            if !part.output_keys.contains(&key) {
                part.output_keys.push(key);
            }
            if part.emitted_bytes > 0
                || part.annotations.iter().any(|annotation| annotation.emitted)
            {
                self.emitted_output_parts.insert(key);
            }
        }
        if let Some(key) = item_key {
            if !part.item_keys.contains(&key) {
                part.item_keys.push(key.clone());
            }
            if part.emitted_bytes > 0
                || part.annotations.iter().any(|annotation| annotation.emitted)
            {
                self.emitted_item_parts.insert(key);
            }
        }
    }

    fn merge_part_indices(&mut self, first: usize, second: usize) -> usize {
        if first == second {
            return first;
        }
        let (keep, discard) = if first < second {
            (first, second)
        } else {
            (second, first)
        };
        let discarded = self.parts[discard].clone();
        let kept_text = self.parts[keep].text.clone();
        let discarded_text = discarded.text.clone();
        let kept_emitted =
            kept_text[..self.parts[keep].emitted_bytes.min(kept_text.len())].to_string();
        let discarded_emitted =
            discarded_text[..discarded.emitted_bytes.min(discarded_text.len())].to_string();
        let additive_histories = self.parts[keep].received_delta && discarded.received_delta;
        let concatenated = !kept_text.is_empty()
            && !discarded_text.is_empty()
            && (additive_histories
                || (!discarded_text.contains(kept_text.as_str())
                    && !kept_text.contains(discarded_text.as_str())));
        let merged_text = if additive_histories {
            format!("{kept_text}{discarded_text}")
        } else if kept_text.is_empty() || discarded_text.contains(kept_text.as_str()) {
            discarded_text.clone()
        } else if discarded_text.is_empty() || kept_text.contains(discarded_text.as_str()) {
            kept_text.clone()
        } else {
            format!("{kept_text}{discarded_text}")
        };
        let mut emitted_bytes = [kept_emitted.as_str(), discarded_emitted.as_str()]
            .into_iter()
            .filter(|prefix| merged_text.starts_with(prefix))
            .map(str::len)
            .max()
            .unwrap_or_default();
        if concatenated && self.parts[keep].emitted_bytes == kept_text.len() {
            let concatenated_emitted = format!("{kept_text}{discarded_emitted}");
            if merged_text.starts_with(&concatenated_emitted) {
                emitted_bytes = emitted_bytes.max(concatenated_emitted.len());
            }
        }
        let merged_text_end = merged_text.chars().count();

        {
            let kept = &mut self.parts[keep];
            kept.text = merged_text;
            kept.emitted_bytes = emitted_bytes;
            for annotation in discarded.annotations {
                if let Some(existing) = kept
                    .annotations
                    .iter_mut()
                    .find(|candidate| candidate.value == annotation.value)
                {
                    existing.emitted |= annotation.emitted;
                } else {
                    kept.annotations.push(BufferedCitationAnnotation {
                        value: annotation.value,
                        observed_text_end: merged_text_end,
                        emitted: annotation.emitted,
                    });
                }
            }
            for key in discarded.output_keys {
                if !kept.output_keys.contains(&key) {
                    kept.output_keys.push(key);
                }
            }
            for key in discarded.item_keys {
                if !kept.item_keys.contains(&key) {
                    kept.item_keys.push(key);
                }
            }
            kept.originated_unkeyed |= discarded.originated_unkeyed;
            kept.received_delta |= discarded.received_delta;
        }
        self.parts[discard].discarded = true;
        self.parts[discard].emitted_bytes = self.parts[discard].text.len();
        if self.open_part == Some(discard) {
            self.open_part = Some(keep);
        }
        if self.last_unkeyed_part == Some(discard) {
            self.last_unkeyed_part = Some(keep);
        }
        if self.parts[keep].emitted_bytes > 0 {
            self.emitted_output_parts
                .extend(self.parts[keep].output_keys.iter().copied());
            self.emitted_item_parts
                .extend(self.parts[keep].item_keys.iter().cloned());
        }
        keep
    }

    fn existing_keyed_part_index(
        &mut self,
        output_key: Option<(u64, u64)>,
        item_key: Option<&(String, u64)>,
    ) -> Option<usize> {
        if self.key_pair_conflicts(output_key, item_key) {
            return None;
        }
        let output_index = output_key.and_then(|key| self.output_part_index(key));
        let item_index = item_key.and_then(|key| self.item_part_index(key));
        match (output_index, item_index) {
            (Some(output), Some(item)) => Some(self.merge_part_indices(output, item)),
            (Some(index), None) | (None, Some(index)) => Some(index),
            (None, None) => None,
        }
    }

    fn snapshot_matches(part: &BufferedCitationPart, text: &str) -> bool {
        part.text.is_empty() || part.text.starts_with(text) || text.starts_with(&part.text)
    }

    fn keys_allow_open_part_adoption(
        part: &BufferedCitationPart,
        output_key: Option<(u64, u64)>,
        item_key: Option<&(String, u64)>,
    ) -> bool {
        let has_keys = !part.output_keys.is_empty() || !part.item_keys.is_empty();
        if !has_keys {
            return part.originated_unkeyed;
        }
        let shares_key = output_key.is_some_and(|key| part.output_keys.contains(&key))
            || item_key.is_some_and(|key| part.item_keys.contains(key));
        let output_conflicts = output_key
            .is_some_and(|key| !part.output_keys.is_empty() && !part.output_keys.contains(&key));
        let item_conflicts =
            item_key.is_some_and(|key| !part.item_keys.is_empty() && !part.item_keys.contains(key));
        shares_key && !output_conflicts && !item_conflicts
    }

    fn resolve_keyed_part(
        &mut self,
        output_key: Option<(u64, u64)>,
        item_key: Option<(String, u64)>,
        adopt_open_part: bool,
        snapshot: Option<&str>,
    ) -> Option<usize> {
        if self.key_pair_conflicts(output_key, item_key.as_ref()) {
            return None;
        }
        let existing = self.existing_keyed_part_index(output_key, item_key.as_ref());
        let index = existing
            .or_else(|| {
                adopt_open_part
                    .then_some(self.open_part)
                    .flatten()
                    .filter(|index| {
                        self.parts.get(*index).is_some_and(|part| {
                            !part.discarded
                                && Self::keys_allow_open_part_adoption(
                                    part,
                                    output_key,
                                    item_key.as_ref(),
                                )
                        })
                    })
            })
            .or_else(|| {
                snapshot.and_then(|text| {
                    self.last_unkeyed_part.filter(|index| {
                        self.parts.get(*index).is_some_and(|part| {
                            !part.discarded
                                && Self::has_pending_output(part)
                                && part.originated_unkeyed
                                && Self::keys_allow_open_part_adoption(
                                    part,
                                    output_key,
                                    item_key.as_ref(),
                                )
                                && Self::snapshot_matches(part, text)
                        })
                    })
                })
            })
            .unwrap_or_else(|| self.push_part(output_key, item_key.clone(), false));
        self.bind_keys(index, output_key, item_key);
        Some(index)
    }

    fn close_open_part(&mut self) {
        let Some(index) = self.open_part.take() else {
            return;
        };
        if self
            .parts
            .get(index)
            .is_some_and(|part| !part.discarded && part.originated_unkeyed)
        {
            self.last_unkeyed_part = Some(index);
        }
    }

    fn finish_event_part(&mut self, data: &Value) {
        let (output_key, item_key) = Self::keys(data);
        if self.key_pair_conflicts(output_key, item_key.as_ref()) {
            return;
        }
        if output_key.is_none() && item_key.is_none() {
            self.close_open_part();
            return;
        }
        let finished = self.existing_keyed_part_index(output_key, item_key.as_ref());
        if finished.is_some() && finished == self.open_part {
            self.close_open_part();
        }
    }

    fn start_unkeyed_part(&mut self, value: &Value) {
        self.close_open_part();
        let index = self.push_part(None, None, true);
        self.open_part = Some(index);
        if let Some(text) = value
            .get("text")
            .and_then(Value::as_str)
            .filter(|text| !text.is_empty())
        {
            Self::merge_snapshot(&mut self.parts[index], text);
        }
        if let Some(annotations) = value.get("annotations").and_then(Value::as_array) {
            for annotation in annotations {
                Self::append_annotation(&mut self.parts[index], annotation);
            }
        }
    }

    fn merge_snapshot(part: &mut BufferedCitationPart, text: &str) {
        if text.starts_with(&part.text) {
            part.text = text.to_string();
            return;
        }
        if part.text.starts_with(text) {
            return;
        }
        if part.emitted_bytes == 0 {
            part.text = text.to_string();
            return;
        }
        let emitted_prefix = &part.text[..part.emitted_bytes.min(part.text.len())];
        if text.starts_with(emitted_prefix) && text.len() > part.text.len() {
            part.text = text.to_string();
        }
    }

    fn finish_unkeyed_text(&mut self, text: &str) -> bool {
        if let Some(index) = self.open_part {
            if self.parts.get(index).is_some_and(|part| part.text == text) {
                self.close_open_part();
                return true;
            }
            if self.emitted_unkeyed_history == text {
                return false;
            }
            let cumulative_continuation = if self.emitted_unkeyed_history.is_empty() {
                None
            } else {
                text.strip_prefix(&self.emitted_unkeyed_history)
                    .filter(|continuation| !continuation.is_empty())
            };
            if let Some(continuation) = cumulative_continuation {
                let continuation_matches = self
                    .parts
                    .get(index)
                    .is_some_and(|part| Self::snapshot_matches(part, continuation));
                if continuation_matches {
                    Self::merge_snapshot(&mut self.parts[index], continuation);
                    self.close_open_part();
                    return true;
                }
                return false;
            }
            let previous_matches = self.last_unkeyed_part.is_some_and(|previous| {
                previous != index
                    && self.parts.get(previous).is_some_and(|part| {
                        !part.discarded && part.originated_unkeyed && part.text == text
                    })
            });
            let active_matches = self
                .parts
                .get(index)
                .is_some_and(|part| Self::snapshot_matches(part, text));
            if previous_matches && (!active_matches || self.parts[index].text.is_empty()) {
                return false;
            }
            if !active_matches && !self.emitted_unkeyed_history.is_empty() {
                return false;
            }
            Self::merge_snapshot(&mut self.parts[index], text);
            self.close_open_part();
            return true;
        }

        if let Some(index) = self.last_unkeyed_part.filter(|index| {
            self.parts
                .get(*index)
                .is_some_and(|part| !part.discarded && part.originated_unkeyed)
        }) {
            if self.parts[index].text.contains(text) {
                return true;
            }
            if text.starts_with(&self.parts[index].text) {
                Self::merge_snapshot(&mut self.parts[index], text);
                return true;
            }
        }

        let index = self.push_part(None, None, true);
        self.parts[index].text = text.to_string();
        self.last_unkeyed_part = Some(index);
        true
    }

    fn finish_unkeyed_part(&mut self, value: &Value) -> bool {
        if let Some(text) = value
            .get("text")
            .and_then(Value::as_str)
            .filter(|text| !text.is_empty())
        {
            if !self.finish_unkeyed_text(text) {
                return false;
            }
        } else {
            self.close_open_part();
        }
        if let Some(annotations) = value.get("annotations").and_then(Value::as_array) {
            if let Some(index) = self.last_unkeyed_part {
                for annotation in annotations {
                    Self::append_annotation(&mut self.parts[index], annotation);
                }
            }
        }
        true
    }

    fn record_text(&mut self, data: &Value, text: &str) -> bool {
        let (output_key, item_key) = Self::keys(data);
        let unkeyed = output_key.is_none() && item_key.is_none();
        if unkeyed {
            return self.finish_unkeyed_text(text);
        }
        let Some(index) = self.resolve_keyed_part(output_key, item_key, true, Some(text)) else {
            return false;
        };
        Self::merge_snapshot(&mut self.parts[index], text);
        if self.open_part == Some(index) {
            self.close_open_part();
        }
        true
    }

    fn merge_part_value(part: &mut BufferedCitationPart, value: &Value) {
        if let Some(text) = value
            .get("text")
            .and_then(Value::as_str)
            .filter(|text| !text.is_empty())
        {
            Self::merge_snapshot(part, text);
        }
        if let Some(annotations) = value.get("annotations").and_then(Value::as_array) {
            for annotation in annotations {
                Self::append_annotation(part, annotation);
            }
        }
    }

    fn record_part(&mut self, data: &Value, part: &Value, finalize_unkeyed: bool) -> bool {
        let (output_key, item_key) = Self::keys(data);
        let unkeyed = output_key.is_none() && item_key.is_none();
        if unkeyed {
            if finalize_unkeyed {
                return self.finish_unkeyed_part(part);
            } else {
                self.start_unkeyed_part(part);
            }
            return true;
        }

        let snapshot = part
            .get("text")
            .and_then(Value::as_str)
            .filter(|text| !text.is_empty());
        let adopt_open_part = finalize_unkeyed
            || self.open_part.is_some_and(|index| {
                self.parts
                    .get(index)
                    .is_some_and(|candidate| candidate.text.is_empty())
            });
        if !finalize_unkeyed && !adopt_open_part {
            self.close_open_part();
        }
        let Some(index) = self.resolve_keyed_part(output_key, item_key, adopt_open_part, snapshot)
        else {
            return false;
        };
        Self::merge_part_value(&mut self.parts[index], part);
        if finalize_unkeyed {
            if self.open_part == Some(index) {
                self.close_open_part();
            }
        } else {
            self.open_part = Some(index);
        }
        true
    }

    fn record_delta(&mut self, data: &Value, delta: &str) {
        if delta.is_empty() {
            return;
        }
        let (output_key, item_key) = Self::keys(data);
        let unkeyed = output_key.is_none() && item_key.is_none();
        if unkeyed {
            let index = self.open_part.unwrap_or_else(|| {
                let index = self.push_part(None, None, true);
                self.open_part = Some(index);
                index
            });
            self.parts[index].text.push_str(delta);
            self.parts[index].received_delta = true;
            return;
        }
        let Some(index) = self.resolve_keyed_part(output_key, item_key, true, None) else {
            return;
        };
        self.parts[index].text.push_str(delta);
        self.parts[index].received_delta = true;
        if self.open_part != Some(index) {
            self.close_open_part();
            self.open_part = Some(index);
        }
    }

    fn record_annotation(&mut self, data: &Value, annotation: &Value) {
        let (output_key, item_key) = Self::keys(data);
        let unkeyed = output_key.is_none() && item_key.is_none();
        if unkeyed {
            let index = self
                .open_part
                .or(self.last_unkeyed_part)
                .unwrap_or_else(|| {
                    let index = self.push_part(None, None, true);
                    self.open_part = Some(index);
                    index
                });
            Self::append_annotation(&mut self.parts[index], annotation);
            return;
        }
        let Some(index) = self.resolve_keyed_part(output_key, item_key, true, None) else {
            return;
        };
        Self::append_annotation(&mut self.parts[index], annotation);
    }

    fn was_emitted(
        &self,
        output_key: Option<(u64, u64)>,
        item_key: Option<&(String, u64)>,
    ) -> bool {
        output_key.is_some_and(|key| self.emitted_output_parts.contains(&key))
            || item_key.is_some_and(|key| self.emitted_item_parts.contains(key))
    }

    fn mark_emitted(&mut self, output_key: Option<(u64, u64)>, item_key: Option<(String, u64)>) {
        if self.key_pair_conflicts(output_key, item_key.as_ref()) {
            return;
        }
        if let Some(key) = output_key {
            self.emitted_output_parts.insert(key);
        }
        if let Some(key) = item_key.as_ref() {
            self.emitted_item_parts.insert(key.clone());
        }
        if let Some(index) = self.existing_keyed_part_index(output_key, item_key.as_ref()) {
            self.bind_keys(index, output_key, item_key);
            self.mark_part_emitted(index);
        }
    }

    fn collect_annotations(
        &self,
        output_key: Option<(u64, u64)>,
        item_key: Option<&(String, u64)>,
        part: Option<&Value>,
    ) -> Vec<Value> {
        let matching_parts = self
            .parts
            .iter()
            .filter(|buffered| {
                !buffered.discarded
                    && (output_key.is_some_and(|key| buffered.output_keys.contains(&key))
                        || item_key.is_some_and(|key| buffered.item_keys.contains(key)))
            })
            .collect::<Vec<_>>();
        let emitted_annotations = matching_parts
            .iter()
            .flat_map(|buffered| &buffered.annotations)
            .filter(|annotation| annotation.emitted)
            .map(|annotation| &annotation.value)
            .collect::<Vec<_>>();
        let mut annotations = Vec::new();
        let mut append = |candidate: &Value| {
            if !annotations.contains(candidate) {
                annotations.push(candidate.clone());
            }
        };
        if let Some(part_annotations) = part
            .and_then(|part| part.get("annotations"))
            .and_then(Value::as_array)
        {
            for annotation in part_annotations {
                if !emitted_annotations.contains(&annotation) {
                    append(annotation);
                }
            }
        }
        for buffered in matching_parts {
            for annotation in buffered
                .annotations
                .iter()
                .filter(|annotation| !annotation.emitted)
            {
                append(&annotation.value);
            }
        }
        annotations
    }

    fn consume_part_annotations(
        part: &mut BufferedCitationPart,
        consumed_chars: usize,
        consume_all: bool,
    ) -> Vec<Value> {
        let mut consumed = Vec::new();
        let mut remaining = Vec::new();
        for mut annotation in std::mem::take(&mut part.annotations) {
            if consume_all || annotation.observed_text_end <= consumed_chars {
                if !consumed.contains(&annotation.value) {
                    consumed.push(annotation.value);
                }
            } else {
                annotation.observed_text_end -= consumed_chars;
                remaining.push(annotation);
            }
        }
        part.annotations = remaining;
        consumed
    }

    fn consume_emitted_unkeyed_annotations(
        &mut self,
        consumed_chars: usize,
        consume_all: bool,
    ) -> Vec<Value> {
        let mut consumed = Vec::new();
        let mut remaining = Vec::new();
        for mut annotation in std::mem::take(&mut self.emitted_unkeyed_annotations) {
            if consume_all || annotation.observed_text_end <= consumed_chars {
                if !consumed.contains(&annotation.value) {
                    consumed.push(annotation.value);
                }
            } else {
                annotation.observed_text_end -= consumed_chars;
                remaining.push(annotation);
            }
        }
        self.emitted_unkeyed_annotations = remaining;
        consumed
    }

    fn reconcile_unkeyed_terminal_text(
        &mut self,
        terminal_text: &str,
        has_terminal_key: bool,
    ) -> Vec<Value> {
        if !has_terminal_key {
            return Vec::new();
        }

        let Some(index) = self.parts.iter().position(|part| {
            !part.discarded
                && Self::has_pending_output(part)
                && part.originated_unkeyed
                && part.output_keys.is_empty()
                && part.item_keys.is_empty()
                && !part.text.is_empty()
        }) else {
            return Vec::new();
        };
        let unkeyed_text = self.parts[index].text.clone();
        if let Some(remaining) = unkeyed_text.strip_prefix(terminal_text) {
            let consumed_chars = terminal_text.chars().count();
            let consume_all = remaining.is_empty();
            let annotations =
                Self::consume_part_annotations(&mut self.parts[index], consumed_chars, consume_all);
            self.parts[index].text = remaining.to_string();
            if consume_all {
                self.mark_part_emitted(index);
            }
            return annotations;
        }
        if terminal_text.starts_with(&unkeyed_text) {
            let annotations = Self::consume_part_annotations(
                &mut self.parts[index],
                unkeyed_text.chars().count(),
                true,
            );
            self.parts[index].text.clear();
            self.mark_part_emitted(index);
            return annotations;
        }

        log::warn!(
            "[Claude/Responses] Terminal text did not match the next unkeyed buffered part; preferring the terminal snapshot"
        );
        self.mark_part_emitted(index);
        Vec::new()
    }

    fn reconcile_emitted_unkeyed_terminal_text(
        &mut self,
        terminal_text: &str,
    ) -> EmittedUnkeyedTerminalMatch {
        if self.emitted_unkeyed_history.is_empty() {
            return EmittedUnkeyedTerminalMatch::NoMatch;
        }

        let emitted = self.emitted_unkeyed_history.clone();
        if let Some(remaining) = emitted.strip_prefix(terminal_text) {
            let consumed_chars = terminal_text.chars().count();
            let consume_all = remaining.is_empty();
            let emitted_annotations =
                self.consume_emitted_unkeyed_annotations(consumed_chars, consume_all);
            self.emitted_unkeyed_history = remaining.to_string();
            return EmittedUnkeyedTerminalMatch::FullyEmitted(emitted_annotations);
        }
        if let Some(suffix) = terminal_text.strip_prefix(&emitted) {
            let emitted_annotations =
                self.consume_emitted_unkeyed_annotations(emitted.chars().count(), true);
            self.emitted_unkeyed_history.clear();
            return if suffix.is_empty() {
                EmittedUnkeyedTerminalMatch::FullyEmitted(emitted_annotations)
            } else {
                EmittedUnkeyedTerminalMatch::MissingSuffix {
                    suffix: suffix.to_string(),
                    emitted_annotations,
                }
            };
        }

        EmittedUnkeyedTerminalMatch::NoMatch
    }

    fn record_done_event(&mut self, data: &Value) {
        let mut accept_annotations = true;
        if let Some(part) = data.get("part") {
            accept_annotations = self.record_part(data, part, true);
        }
        if let Some(text) = data
            .get("text")
            .and_then(Value::as_str)
            .filter(|text| !text.is_empty())
        {
            if accept_annotations {
                accept_annotations = self.record_text(data, text);
            }
        }
        if !accept_annotations {
            return;
        }
        if let Some(annotation) = data.get("annotation") {
            self.record_annotation(data, annotation);
        }
        if let Some(annotations) = data.get("annotations").and_then(Value::as_array) {
            for annotation in annotations {
                self.record_annotation(data, annotation);
            }
        }
        self.finish_event_part(data);
    }

    fn render_pending_parts(&mut self) -> Vec<String> {
        let mut rendered = Vec::new();
        for index in 0..self.parts.len() {
            if let Some(text) = self.render_part_pending(index) {
                rendered.push(text);
            }
        }
        rendered
    }

    fn render_part_pending(&mut self, index: usize) -> Option<String> {
        let part = self
            .parts
            .get(index)
            .filter(|part| !part.discarded && Self::has_pending_output(part))?;
        let emitted_bytes = part.emitted_bytes.min(part.text.len());
        let missing = part.text[emitted_bytes..].to_string();
        let annotations = Self::pending_annotation_values(part);
        let unkeyed = part.output_keys.is_empty() && part.item_keys.is_empty();
        let emitted_unkeyed_annotations = if unkeyed {
            let emitted_chars = part.text[..emitted_bytes].chars().count();
            let missing_chars = missing.chars().count();
            part.annotations
                .iter()
                .filter(|annotation| !annotation.emitted)
                .map(|annotation| {
                    (
                        annotation.value.clone(),
                        annotation
                            .observed_text_end
                            .saturating_sub(emitted_chars)
                            .min(missing_chars),
                    )
                })
                .collect::<Vec<_>>()
        } else {
            Vec::new()
        };
        if unkeyed {
            let history_chars = self.emitted_unkeyed_history.chars().count();
            self.emitted_unkeyed_history.push_str(&missing);
            for (value, relative_end) in emitted_unkeyed_annotations {
                let observed_text_end = history_chars + relative_end;
                if !self.emitted_unkeyed_annotations.iter().any(|annotation| {
                    annotation.value == value && annotation.observed_text_end == observed_text_end
                }) {
                    self.emitted_unkeyed_annotations
                        .push(BufferedCitationAnnotation {
                            value,
                            observed_text_end,
                            emitted: true,
                        });
                }
            }
        }
        self.mark_part_emitted(index);

        if emitted_bytes == 0 && !missing.is_empty() {
            return Some(text_with_url_citations(&missing, &annotations));
        }
        let sources = text_with_url_citations("", &annotations);
        match (missing.is_empty(), sources.is_empty()) {
            (false, false) => Some(format!("{missing}\n\n{sources}")),
            (false, true) => Some(missing),
            (true, false) => Some(sources),
            (true, true) => None,
        }
    }

    fn append_part_annotations(&mut self, index: usize, value: &Value) {
        if let Some(annotations) = value.get("annotations").and_then(Value::as_array) {
            for annotation in annotations {
                Self::append_annotation(&mut self.parts[index], annotation);
            }
        }
    }

    fn remember_rendered_terminal_part(
        &mut self,
        text: &str,
        annotations: &[Value],
        output_key: Option<(u64, u64)>,
        item_key: Option<(String, u64)>,
    ) {
        if output_key.is_none() && item_key.is_none() {
            return;
        }
        if self.key_pair_conflicts(output_key, item_key.as_ref()) {
            return;
        }
        let index = self
            .existing_keyed_part_index(output_key, item_key.as_ref())
            .unwrap_or_else(|| self.push_part(output_key, item_key.clone(), false));
        self.bind_keys(index, output_key, item_key);
        Self::merge_snapshot(&mut self.parts[index], text);
        for annotation in annotations {
            Self::append_annotation(&mut self.parts[index], annotation);
        }
        self.mark_part_emitted(index);
    }

    fn render_message_part(
        &mut self,
        part: &Value,
        output_index: Option<u64>,
        item_id: Option<&str>,
        content_index: u64,
    ) -> Option<String> {
        let output_key = output_index.map(|index| (index, content_index));
        let item_key = item_id.map(|id| (id.to_string(), content_index));
        if self.key_pair_conflicts(output_key, item_key.as_ref()) {
            return None;
        }
        let keyed_index = self.existing_keyed_part_index(output_key, item_key.as_ref());
        let text = part
            .get("text")
            .and_then(Value::as_str)
            .filter(|text| !text.is_empty())?;
        if let Some(index) = keyed_index.filter(|index| {
            Self::has_pending_output(&self.parts[*index])
                && Self::snapshot_matches(&self.parts[*index], text)
        }) {
            self.bind_keys(index, output_key, item_key.clone());
            Self::merge_snapshot(&mut self.parts[index], text);
            self.append_part_annotations(index, part);
            return self.render_part_pending(index);
        }
        match self.reconcile_emitted_unkeyed_terminal_text(text) {
            EmittedUnkeyedTerminalMatch::FullyEmitted(emitted_annotations) => {
                let annotations = part
                    .get("annotations")
                    .and_then(Value::as_array)
                    .map(|annotations| {
                        annotations
                            .iter()
                            .filter(|annotation| !emitted_annotations.contains(annotation))
                            .cloned()
                            .collect::<Vec<_>>()
                    })
                    .unwrap_or_default();
                if let Some(index) =
                    keyed_index.filter(|index| Self::has_pending_output(&self.parts[*index]))
                {
                    self.bind_keys(index, output_key, item_key.clone());
                    if let Some(terminal_annotations) =
                        part.get("annotations").and_then(Value::as_array)
                    {
                        for annotation in terminal_annotations {
                            Self::append_annotation(&mut self.parts[index], annotation);
                        }
                    }
                    return self.render_part_pending(index);
                }
                let terminal_annotations = part
                    .get("annotations")
                    .and_then(Value::as_array)
                    .map(Vec::as_slice)
                    .unwrap_or_default();
                self.remember_rendered_terminal_part(
                    text,
                    terminal_annotations,
                    output_key,
                    item_key,
                );
                let sources = text_with_url_citations("", &annotations);
                return (!sources.is_empty()).then_some(sources);
            }
            EmittedUnkeyedTerminalMatch::MissingSuffix {
                suffix,
                emitted_annotations,
            } => {
                if let Some(index) = keyed_index {
                    self.bind_keys(index, output_key, item_key.clone());
                    Self::merge_snapshot(&mut self.parts[index], &suffix);
                    if let Some(annotations) = part.get("annotations").and_then(Value::as_array) {
                        for annotation in annotations
                            .iter()
                            .filter(|annotation| !emitted_annotations.contains(annotation))
                        {
                            Self::append_annotation(&mut self.parts[index], annotation);
                        }
                    }
                    return self.render_part_pending(index);
                }
                let mut annotations: Vec<Value> = part
                    .get("annotations")
                    .and_then(Value::as_array)
                    .map(|annotations| {
                        annotations
                            .iter()
                            .filter(|annotation| !emitted_annotations.contains(annotation))
                            .cloned()
                            .collect()
                    })
                    .unwrap_or_default();
                for annotation in self.reconcile_unkeyed_terminal_text(
                    &suffix,
                    output_key.is_some() || item_key.is_some(),
                ) {
                    if !annotations.contains(&annotation) {
                        annotations.push(annotation);
                    }
                }
                let mut remembered_annotations = part
                    .get("annotations")
                    .and_then(Value::as_array)
                    .cloned()
                    .unwrap_or_default();
                for annotation in &annotations {
                    if !remembered_annotations.contains(annotation) {
                        remembered_annotations.push(annotation.clone());
                    }
                }
                self.remember_rendered_terminal_part(
                    text,
                    &remembered_annotations,
                    output_key,
                    item_key,
                );
                let sources = text_with_url_citations("", &annotations);
                return match (suffix.is_empty(), sources.is_empty()) {
                    (false, false) => Some(format!("{suffix}\n\n{sources}")),
                    (false, true) => Some(suffix),
                    (true, false) => Some(sources),
                    (true, true) => None,
                };
            }
            EmittedUnkeyedTerminalMatch::NoMatch => {}
        }
        if keyed_index.is_none() && self.was_emitted(output_key, item_key.as_ref()) {
            return None;
        }
        if let Some(index) = keyed_index {
            self.bind_keys(index, output_key, item_key.clone());
            Self::merge_snapshot(&mut self.parts[index], text);
            self.append_part_annotations(index, part);
            return self.render_part_pending(index);
        }
        let mut annotations = self.collect_annotations(output_key, item_key.as_ref(), Some(part));
        for annotation in
            self.reconcile_unkeyed_terminal_text(text, output_key.is_some() || item_key.is_some())
        {
            if !annotations.contains(&annotation) {
                annotations.push(annotation);
            }
        }
        let rendered = text_with_url_citations(text, &annotations);
        self.remember_rendered_terminal_part(text, &annotations, output_key, item_key);
        Some(rendered)
    }
}

fn missing_message_text_parts(
    item: &Value,
    output_index: Option<u64>,
    streamed_text: &mut StreamedTextState,
    mut buffered_citations: Option<&mut BufferedCitationTextState>,
) -> Vec<String> {
    if item.get("type").and_then(Value::as_str) != Some("message") {
        return Vec::new();
    }
    let item_id = item.get("id").and_then(Value::as_str);
    let mut missing_parts = Vec::new();
    for (content_index, part) in item
        .get("content")
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .enumerate()
    {
        match part.get("type").and_then(Value::as_str) {
            Some("output_text") => {
                if let Some(buffered) = buffered_citations.as_deref_mut() {
                    let content_index = content_index as u64;
                    if let Some(full_text) = part
                        .get("text")
                        .and_then(Value::as_str)
                        .filter(|text| !text.is_empty())
                    {
                        if streamed_text.has_part_matching_terminal(
                            full_text,
                            output_index,
                            item_id,
                            content_index,
                        ) {
                            let missing = streamed_text.missing_suffix(
                                full_text,
                                output_index,
                                item_id,
                                content_index,
                            );
                            let output_key = output_index.map(|index| (index, content_index));
                            let item_key = item_id.map(|id| (id.to_string(), content_index));
                            let annotations = buffered.collect_annotations(
                                output_key,
                                item_key.as_ref(),
                                Some(part),
                            );
                            buffered.mark_emitted(output_key, item_key);
                            let sources = text_with_url_citations("", &annotations);
                            match (missing.is_empty(), sources.is_empty()) {
                                (false, false) => {
                                    missing_parts.push(format!("{missing}\n\n{sources}"));
                                }
                                (false, true) => missing_parts.push(missing),
                                (true, false) => missing_parts.push(sources),
                                (true, true) => {}
                            }
                        } else if let Some(text) =
                            buffered.render_message_part(part, output_index, item_id, content_index)
                        {
                            missing_parts.push(text);
                        }
                    }
                } else if let Some(full_text) = part
                    .get("text")
                    .and_then(Value::as_str)
                    .filter(|text| !text.is_empty())
                {
                    let missing = streamed_text.missing_suffix(
                        full_text,
                        output_index,
                        item_id,
                        content_index as u64,
                    );
                    if !missing.is_empty() {
                        missing_parts.push(missing);
                    }
                }
            }
            Some("refusal") => {
                if let Some(full_text) = part
                    .get("refusal")
                    .and_then(Value::as_str)
                    .filter(|text| !text.is_empty())
                {
                    let missing = streamed_text.missing_suffix(
                        full_text,
                        output_index,
                        item_id,
                        content_index as u64,
                    );
                    if !missing.is_empty() {
                        missing_parts.push(missing);
                    }
                }
            }
            _ => {}
        }
    }
    missing_parts
}

fn text_block_events(index: u32, text: &str) -> [Bytes; 3] {
    [
        anthropic_sse(
            "content_block_start",
            &json!({
                "type":"content_block_start",
                "index":index,
                "content_block":{"type":"text","text":""}
            }),
        ),
        anthropic_sse(
            "content_block_delta",
            &json!({
                "type":"content_block_delta",
                "index":index,
                "delta":{"type":"text_delta","text":text}
            }),
        ),
        anthropic_sse(
            "content_block_stop",
            &json!({"type":"content_block_stop","index":index}),
        ),
    ]
}

#[inline]
fn tool_item_key_from_added(data: &Value, item: &Value) -> Option<String> {
    if let Some(item_id) = item.get("id").and_then(|v| v.as_str()) {
        return Some(format!("tool:{item_id}"));
    }
    if let Some(item_id) = data.get("item_id").and_then(|v| v.as_str()) {
        return Some(format!("tool:{item_id}"));
    }
    if let Some(output_index) = data.get("output_index").and_then(|v| v.as_u64()) {
        return Some(format!("tool:out:{output_index}"));
    }
    None
}

#[inline]
fn tool_item_key_from_event(data: &Value) -> Option<String> {
    if let Some(item_id) = data.get("item_id").and_then(|v| v.as_str()) {
        return Some(format!("tool:{item_id}"));
    }
    if let Some(output_index) = data.get("output_index").and_then(|v| v.as_u64()) {
        return Some(format!("tool:out:{output_index}"));
    }
    None
}

fn web_search_item_keys(data: &Value, item: Option<&Value>) -> Vec<String> {
    let mut keys = Vec::with_capacity(2);
    if let Some(item_id) = item
        .and_then(|value| value.get("id"))
        .and_then(Value::as_str)
        .or_else(|| data.get("item_id").and_then(Value::as_str))
        .filter(|item_id| !item_id.is_empty())
    {
        keys.push(format!("web-search:{item_id}"));
    }
    if let Some(output_index) = data.get("output_index").and_then(Value::as_u64) {
        keys.push(format!("web-search:out:{output_index}"));
    }
    keys
}

fn web_search_result_events(index: u32, tool_use_id: &str, content: Value) -> [Bytes; 2] {
    [
        anthropic_sse(
            "content_block_start",
            &json!({
                "type":"content_block_start",
                "index":index,
                "content_block":{
                    "type":"web_search_tool_result",
                    "tool_use_id":tool_use_id,
                    "content":content,
                    "caller":{"type":"direct"}
                }
            }),
        ),
        anthropic_sse(
            "content_block_stop",
            &json!({"type":"content_block_stop","index":index}),
        ),
    ]
}

fn take_web_search_result_events(
    id_order: &[String],
    results_by_id: &mut HashMap<String, Vec<Value>>,
    errors_by_id: &mut HashMap<String, Value>,
    result_index_by_id: &mut HashMap<String, u32>,
    next_content_index: &mut u32,
) -> Vec<Bytes> {
    let mut events = Vec::new();
    for search_id in id_order {
        let index = result_index_by_id.remove(search_id).unwrap_or_else(|| {
            let index = *next_content_index;
            *next_content_index += 1;
            index
        });
        let results = results_by_id.remove(search_id).unwrap_or_default();
        let content = errors_by_id
            .remove(search_id)
            .unwrap_or(Value::Array(results));
        events.extend(web_search_result_events(index, search_id, content));
    }
    events
}

fn reserve_web_search_result_index(
    search_id: &str,
    result_index_by_id: &mut HashMap<String, u32>,
    next_content_index: &mut u32,
) {
    result_index_by_id
        .entry(search_id.to_string())
        .or_insert_with(|| {
            let index = *next_content_index;
            *next_content_index += 1;
            index
        });
}

fn take_open_web_search_block_stop_events(
    open_indices: &mut HashSet<u32>,
    web_search_id_by_index: &HashMap<u32, String>,
) -> Vec<Bytes> {
    let mut indices: Vec<u32> = open_indices
        .iter()
        .copied()
        .filter(|index| web_search_id_by_index.contains_key(index))
        .collect();
    indices.sort_unstable();
    indices
        .into_iter()
        .map(|index| {
            open_indices.remove(&index);
            anthropic_sse(
                "content_block_stop",
                &json!({"type":"content_block_stop","index":index}),
            )
        })
        .collect()
}

fn web_search_limit_stop_events(web_search_count: u64, has_tool_use: bool) -> [Bytes; 2] {
    let stop_reason = if has_tool_use { "tool_use" } else { "end_turn" };
    [
        anthropic_sse(
            "message_delta",
            &json!({
                "type":"message_delta",
                "delta":{"stop_reason":stop_reason,"stop_sequence":null},
                "usage":{
                    "input_tokens":0,
                    "output_tokens":0,
                    "server_tool_use":{"web_search_requests":web_search_count}
                }
            }),
        ),
        anthropic_sse("message_stop", &json!({"type":"message_stop"})),
    ]
}

fn append_unique_web_search_results(target: &mut Vec<Value>, results: Vec<Value>) {
    merge_web_search_result_metadata(target, &results);
    let mut seen: HashSet<String> = target
        .iter()
        .filter_map(|result| result.get("url").and_then(Value::as_str))
        .map(ToString::to_string)
        .collect();
    for result in results {
        let Some(url) = result.get("url").and_then(Value::as_str) else {
            continue;
        };
        if seen.insert(url.to_string()) {
            target.push(result);
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum WebSearchCallDisposition {
    Accepted,
    LimitExceeded,
    Ignored,
}

struct WebSearchRecordState<'a> {
    ids_seen: &'a mut HashSet<String>,
    id_order: &'a mut Vec<String>,
    results_by_id: &'a mut HashMap<String, Vec<Value>>,
    errors_by_id: &'a mut HashMap<String, Value>,
    request_count: &'a mut u64,
    max_uses: Option<u64>,
    limit_exceeded_id: &'a mut Option<String>,
}

fn record_web_search_call(
    search_id: &str,
    item: &Value,
    state: &mut WebSearchRecordState<'_>,
) -> WebSearchCallDisposition {
    if state.ids_seen.contains(search_id) {
        if state.limit_exceeded_id.as_deref() == Some(search_id) {
            return WebSearchCallDisposition::LimitExceeded;
        }
        if !state.id_order.iter().any(|existing| existing == search_id) {
            return WebSearchCallDisposition::Ignored;
        }
    } else {
        state.ids_seen.insert(search_id.to_string());
        if state
            .max_uses
            .is_some_and(|limit| *state.request_count >= limit)
        {
            if state.limit_exceeded_id.is_some() {
                return WebSearchCallDisposition::Ignored;
            }
            *state.limit_exceeded_id = Some(search_id.to_string());
            state.id_order.push(search_id.to_string());
            state
                .errors_by_id
                .insert(search_id.to_string(), web_search_max_uses_exceeded_error());
            return WebSearchCallDisposition::LimitExceeded;
        }
        *state.request_count += 1;
        state.id_order.push(search_id.to_string());
    }
    append_unique_web_search_results(
        state
            .results_by_id
            .entry(search_id.to_string())
            .or_default(),
        web_search_results_from_action(item),
    );
    if let Some(error) = web_search_tool_result_error(item) {
        state.errors_by_id.insert(search_id.to_string(), error);
    } else {
        // A later successful/terminal item is authoritative if an earlier
        // lifecycle event was still in progress or transiently carried an
        // error field. Compatible gateways sometimes omit the final status.
        state.errors_by_id.remove(search_id);
    }
    WebSearchCallDisposition::Accepted
}

#[inline]
fn reasoning_item_key(data: &Value, item: Option<&Value>) -> Option<String> {
    if let Some(item_id) = item
        .and_then(|value| value.get("id"))
        .and_then(Value::as_str)
        .or_else(|| data.get("item_id").and_then(Value::as_str))
    {
        return Some(format!("reasoning:{item_id}"));
    }
    data.get("output_index")
        .and_then(Value::as_u64)
        .map(|index| format!("reasoning:out:{index}"))
}

/// Resolve content index for a text/refusal content part event.
///
/// Uses `content_part_key` to look up or assign a stable index, falling back to
/// `fallback_open_index` when no key is available.
#[inline]
fn resolve_content_index(
    data: &Value,
    next_content_index: &mut u32,
    index_by_key: &mut HashMap<String, u32>,
    fallback_open_index: &mut Option<u32>,
) -> u32 {
    if let Some(k) = content_part_key(data) {
        if let Some(existing) = index_by_key.get(&k).copied() {
            existing
        } else {
            let assigned = *next_content_index;
            *next_content_index += 1;
            index_by_key.insert(k, assigned);
            assigned
        }
    } else if let Some(existing) = *fallback_open_index {
        existing
    } else {
        let assigned = *next_content_index;
        *next_content_index += 1;
        *fallback_open_index = Some(assigned);
        assigned
    }
}

#[derive(Default)]
struct WebSearchResultOrderingState {
    waiting_tool_use_id: Option<String>,
    server_block_index: Option<u64>,
    result_block_index: Option<u64>,
}

impl WebSearchResultOrderingState {
    fn is_waiting(&self) -> bool {
        self.waiting_tool_use_id.is_some()
    }

    fn can_emit(&self, event: Option<&Value>) -> bool {
        if !self.is_waiting() {
            return true;
        }
        let Some(event) = event else {
            return false;
        };
        let event_type = event.get("type").and_then(Value::as_str);
        if matches!(event_type, Some("ping" | "error")) {
            return true;
        }
        let event_index = event.get("index").and_then(Value::as_u64);
        if let Some(result_index) = self.result_block_index {
            return event_index == Some(result_index)
                && matches!(
                    event_type,
                    Some("content_block_delta" | "content_block_stop")
                );
        }
        if event_index == self.server_block_index
            && matches!(
                event_type,
                Some("content_block_delta" | "content_block_stop")
            )
        {
            return true;
        }
        event_type == Some("content_block_start")
            && event.pointer("/content_block/type").and_then(Value::as_str)
                == Some("web_search_tool_result")
            && event
                .pointer("/content_block/tool_use_id")
                .and_then(Value::as_str)
                == self.waiting_tool_use_id.as_deref()
    }

    fn observe_emitted(&mut self, event: Option<&Value>, hosted_web_search_name: &str) {
        let Some(event) = event else {
            return;
        };
        let event_type = event.get("type").and_then(Value::as_str);
        if !self.is_waiting()
            && event_type == Some("content_block_start")
            && event.pointer("/content_block/type").and_then(Value::as_str)
                == Some("server_tool_use")
            && event.pointer("/content_block/name").and_then(Value::as_str)
                == Some(hosted_web_search_name)
        {
            self.waiting_tool_use_id = event
                .pointer("/content_block/id")
                .and_then(Value::as_str)
                .map(ToString::to_string);
            self.server_block_index = event.get("index").and_then(Value::as_u64);
            return;
        }
        if !self.is_waiting() {
            return;
        }
        if self.result_block_index.is_none()
            && event_type == Some("content_block_start")
            && event.pointer("/content_block/type").and_then(Value::as_str)
                == Some("web_search_tool_result")
            && event
                .pointer("/content_block/tool_use_id")
                .and_then(Value::as_str)
                == self.waiting_tool_use_id.as_deref()
        {
            self.result_block_index = event.get("index").and_then(Value::as_u64);
            return;
        }
        if self.result_block_index.is_some()
            && event_type == Some("content_block_stop")
            && event.get("index").and_then(Value::as_u64) == self.result_block_index
        {
            *self = Self::default();
        }
    }
}

fn anthropic_event_value(bytes: &Bytes) -> Option<Value> {
    let body = std::str::from_utf8(bytes).ok()?;
    let data = body
        .lines()
        .filter_map(|line| strip_sse_field(line, "data"))
        .collect::<Vec<_>>()
        .join("\n");
    serde_json::from_str(data.trim()).ok()
}

fn order_anthropic_web_search_result_stream(
    stream: impl Stream<Item = Result<Bytes, std::io::Error>> + Send + 'static,
    hosted_web_search_name: String,
) -> impl Stream<Item = Result<Bytes, std::io::Error>> + Send {
    async_stream::stream! {
        let mut state = WebSearchResultOrderingState::default();
        let mut pending = VecDeque::<Bytes>::new();
        let mut upstream_done = false;
        tokio::pin!(stream);

        loop {
            let can_emit_pending = pending.front().is_some_and(|bytes| {
                let event = anthropic_event_value(bytes);
                state.can_emit(event.as_ref())
            });
            if can_emit_pending {
                let Some(bytes) = pending.pop_front() else {
                    continue;
                };
                let event = anthropic_event_value(&bytes);
                state.observe_emitted(event.as_ref(), &hosted_web_search_name);
                yield Ok(bytes);
                continue;
            }

            if upstream_done {
                if state.is_waiting() {
                    yield Ok(anthropic_error_sse(
                        "Responses upstream ended before a hosted web-search result could be ordered",
                        "stream_truncated",
                    ));
                }
                break;
            }

            match stream.next().await {
                Some(Ok(bytes)) => {
                    let event = anthropic_event_value(&bytes);
                    if event
                        .as_ref()
                        .and_then(|value| value.get("type"))
                        .and_then(Value::as_str)
                        == Some("error")
                    {
                        pending.clear();
                        yield Ok(bytes);
                        break;
                    }
                    if state.can_emit(event.as_ref()) {
                        state.observe_emitted(event.as_ref(), &hosted_web_search_name);
                        yield Ok(bytes);
                    } else {
                        pending.push_back(bytes);
                        // Keep the downstream stream observably active while semantic
                        // content waits for the paired hosted-search result.
                        yield Ok(anthropic_ping_sse());
                    }
                }
                Some(Err(error)) => {
                    yield Err(error);
                    break;
                }
                None => upstream_done = true,
            }
        }
    }
}

/// 创建从 Responses API SSE 到 Anthropic SSE 的转换流
///
/// 状态机跟踪: message_id, current_model, has_sent_message_start, item/content index map
/// SSE 解析支持 named events (event: + data: 行)
#[cfg(test)]
fn create_anthropic_sse_stream_from_responses<E: std::error::Error + Send + 'static>(
    stream: impl Stream<Item = Result<Bytes, E>> + Send + 'static,
) -> impl Stream<Item = Result<Bytes, std::io::Error>> + Send {
    create_anthropic_sse_stream_from_responses_with_web_search_options(stream, None, None)
}

pub(crate) fn create_anthropic_sse_stream_from_responses_with_web_search_options<
    E: std::error::Error + Send + 'static,
>(
    stream: impl Stream<Item = Result<Bytes, E>> + Send + 'static,
    hosted_web_search_name: Option<String>,
    max_web_search_uses: Option<u64>,
) -> impl Stream<Item = Result<Bytes, std::io::Error>> + Send {
    let can_preserve_web_search_citations = hosted_web_search_name
        .as_deref()
        .is_some_and(|name| !name.is_empty());
    let hosted_web_search_name = hosted_web_search_name
        .filter(|name| !name.is_empty())
        .unwrap_or_else(|| "web_search".to_string());
    let raw_stream = create_anthropic_sse_stream_from_responses_raw(
        stream,
        hosted_web_search_name.clone(),
        max_web_search_uses,
        can_preserve_web_search_citations,
    );
    hold_block_stops_until_next_event(order_anthropic_web_search_result_stream(
        raw_stream,
        hosted_web_search_name,
    ))
}

/// The closing `message_delta` + `message_stop` of a completed response. The
/// stream loop renders each frame right before yielding it, so the usage
/// summary and its logs happen only once the client has read everything
/// before them.
struct MessageEnd {
    stop_reason: Option<&'static str>,
    usage: Value,
    web_search_count: u64,
}

impl MessageEnd {
    fn message_delta(&self) -> Bytes {
        // Defensive: Always build usage_json, even if usage field missing
        // Some() wrapper with fallback to {} ensures build_anthropic_usage_from_responses
        // always receives valid input, preventing null pointer errors in VSCode Extension
        let mut usage_json = build_anthropic_usage_from_responses(Some(&self.usage));
        if self.web_search_count > 0 {
            usage_json["server_tool_use"] = json!({
                "web_search_requests": self.web_search_count
            });
        }

        // Emit message_delta (with usage + stop_reason)
        let delta_event = json!({
            "type": "message_delta",
            "delta": {
                "stop_reason": self.stop_reason,
                "stop_sequence": null
            },
            "usage": usage_json
        });
        let sse = format!(
            "event: message_delta\ndata: {}\n\n",
            serde_json::to_string(&delta_event).unwrap_or_default()
        );
        log::debug!("[Claude/Responses] >>> Anthropic SSE: message_delta");
        Bytes::from(sse)
    }

    fn message_stop() -> Bytes {
        // Emit message_stop
        let stop_event = json!({"type": "message_stop"});
        let stop_sse = format!(
            "event: message_stop\ndata: {}\n\n",
            serde_json::to_string(&stop_event).unwrap_or_default()
        );
        log::debug!("[Claude/Responses] >>> Anthropic SSE: message_stop");
        Bytes::from(stop_sse)
    }
}

/// Mutable state of one Responses → Anthropic SSE conversion. Each upstream
/// event goes through [`Self::handle_event`], which appends the Anthropic SSE
/// frames it produces to `out`; the stream loop yields them in order.
struct ResponsesToAnthropicState {
    hosted_web_search_name: String,
    max_web_search_uses: Option<u64>,
    can_preserve_web_search_citations: bool,
    message_id: Option<String>,
    current_model: Option<String>,
    has_sent_message_start: bool,
    has_tool_use: bool,
    next_content_index: u32,
    index_by_key: HashMap<String, u32>,
    open_indices: HashSet<u32>,
    fallback_open_index: Option<u32>,
    current_text_index: Option<u32>,
    streamed_text: StreamedTextState,
    buffered_citation_text: BufferedCitationTextState,
    preserve_web_search_citations: bool,
    tool_index_by_item_id: HashMap<String, u32>,
    tool_name_by_index: HashMap<u32, String>,
    tool_args_by_index: HashMap<u32, String>,
    tool_had_delta: HashSet<u32>,
    last_tool_index: Option<u32>,
    web_search_index_by_item_id: HashMap<String, u32>,
    web_search_id_by_index: HashMap<u32, String>,
    web_search_result_index_by_id: HashMap<String, u32>,
    web_search_ids_seen: HashSet<String>,
    web_search_ids_completed: HashSet<String>,
    web_search_limit_exceeded_id: Option<String>,
    web_search_id_order: Vec<String>,
    web_search_results_by_id: HashMap<String, Vec<Value>>,
    web_search_errors_by_id: HashMap<String, Value>,
    pending_web_search_results: Vec<Value>,
    seen_web_search_result_urls: HashSet<String>,
    web_search_count: u64,
    reasoning_index_by_item_id: HashMap<String, u32>,
    reasoning_item_by_index: HashMap<u32, Value>,
    reasoning_text_by_index: HashMap<u32, String>,
    legacy_reasoning_index: Option<u32>,
    has_substantive_output: bool,
    terminated: bool,
    /// Set by a completed response; the stream loop yields it after the event.
    message_end: Option<MessageEnd>,
}

impl ResponsesToAnthropicState {
    fn new(
        hosted_web_search_name: String,
        max_web_search_uses: Option<u64>,
        can_preserve_web_search_citations: bool,
    ) -> Self {
        Self {
            hosted_web_search_name,
            max_web_search_uses,
            can_preserve_web_search_citations,
            message_id: None,
            current_model: None,
            has_sent_message_start: false,
            has_tool_use: false,
            next_content_index: 0,
            index_by_key: HashMap::new(),
            open_indices: HashSet::new(),
            fallback_open_index: None,
            current_text_index: None,
            streamed_text: StreamedTextState::default(),
            buffered_citation_text: BufferedCitationTextState::default(),
            preserve_web_search_citations: false,
            tool_index_by_item_id: HashMap::new(),
            tool_name_by_index: HashMap::new(),
            tool_args_by_index: HashMap::new(),
            tool_had_delta: HashSet::new(),
            last_tool_index: None,
            web_search_index_by_item_id: HashMap::new(),
            web_search_id_by_index: HashMap::new(),
            web_search_result_index_by_id: HashMap::new(),
            web_search_ids_seen: HashSet::new(),
            web_search_ids_completed: HashSet::new(),
            web_search_limit_exceeded_id: None,
            web_search_id_order: Vec::new(),
            web_search_results_by_id: HashMap::new(),
            web_search_errors_by_id: HashMap::new(),
            pending_web_search_results: Vec::new(),
            seen_web_search_result_urls: HashSet::new(),
            web_search_count: 0_u64,
            reasoning_index_by_item_id: HashMap::new(),
            reasoning_item_by_index: HashMap::new(),
            reasoning_text_by_index: HashMap::new(),
            legacy_reasoning_index: None,
            has_substantive_output: false,
            terminated: false,
            message_end: None,
        }
    }

    /// Emits the placeholder `message_start` once, for streams whose first
    /// frame is not `response.created`.
    fn ensure_message_start(&mut self, out: &mut Vec<Bytes>) {
        if self.has_sent_message_start {
            return;
        }
        out.push(anthropic_sse(
            "message_start",
            &json!({
                "type":"message_start",
                "message":{
                    "id":self.message_id.clone().unwrap_or_default(),
                    "type":"message",
                    "role":"assistant",
                    "model":self.current_model.clone().unwrap_or_default(),
                    "usage":{"input_tokens":0,"output_tokens":0}
                }
            }),
        ));
        self.has_sent_message_start = true;
    }

    /// Converts one parsed upstream event. `Break` ends the stream loop.
    fn handle_event(
        &mut self,
        event_name: &str,
        data: &Value,
        out: &mut Vec<Bytes>,
    ) -> ControlFlow<()> {
        let delta_requires_message_start = matches!(
            event_name,
            "response.output_text.delta"
                | "response.refusal.delta"
                | "response.function_call_arguments.delta"
                | "response.reasoning_summary_text.delta"
                | "response.reasoning_text.delta"
                | "response.reasoning.delta"
        );
        if delta_requires_message_start {
            self.has_substantive_output = true;
        }
        if delta_requires_message_start {
            self.ensure_message_start(out);
        }

        match event_name {
            // ================================================
            // response.created → message_start
            // ================================================
            "response.created" => self.on_response_created(data, out),

            // ================================================
            // response.content_part.added → content_block_start (text)
            // ================================================
            "response.content_part.added" => self.on_content_part_added(data, out),

            // ================================================
            // response.output_text.delta → content_block_delta (text_delta)
            // ================================================
            "response.output_text.delta" => self.on_output_text_delta(data, out),

            "response.output_text.annotation.added" if self.preserve_web_search_citations => {
                if let Some(annotation) = data.get("annotation") {
                    self.buffered_citation_text
                        .record_annotation(data, annotation);
                }
            }
            "response.output_text.annotation.added" => {}

            // ================================================
            // response.refusal.delta → content_block_delta (text_delta)
            // ================================================
            "response.refusal.delta" => self.on_refusal_delta(data, out),

            // ================================================
            // response.content_part.done → content_block_stop
            // ================================================
            "response.content_part.done" if self.preserve_web_search_citations => {
                self.on_buffered_content_part_done(data)
            }
            "response.content_part.done" => {
                self.streamed_text.finish_part(data);
            }

            // ================================================
            // response.output_item.added (function_call) → content_block_start (tool_use)
            // ================================================
            "response.output_item.added" => return self.on_output_item_added(data, out),

            // ================================================
            // response.function_call_arguments.delta → content_block_delta (input_json_delta)
            // ================================================
            "response.function_call_arguments.delta" => {
                self.on_function_call_arguments_delta(data, out)
            }

            // ================================================
            // response.function_call_arguments.done → content_block_stop
            // ================================================
            "response.function_call_arguments.done" => {
                self.on_function_call_arguments_done(data, out)
            }

            // ================================================
            // response.refusal.done → content_block_stop
            // ================================================
            "response.refusal.done" => self.on_refusal_done(data, out),

            // ================================================
            // Official reasoning text events → thinking_delta.
            // response.reasoning.delta is kept as a compatibility alias.
            // ================================================
            "response.reasoning_summary_text.delta"
            | "response.reasoning_text.delta"
            | "response.reasoning.delta" => self.on_reasoning_delta(data, out),

            // ================================================
            // Official done events carry the complete visible text. If a
            // gateway omitted deltas, emit the text here. The block stays
            // open until output_item.done supplies encrypted_content.
            // ================================================
            "response.reasoning_summary_text.done" | "response.reasoning_text.done" => {
                self.on_reasoning_text_done(data, out)
            }

            // Legacy gateways do not emit output_item.done, so retain the
            // old close behavior for their non-standard done event.
            "response.reasoning.done" => self.on_legacy_reasoning_done(data, out),

            // ================================================
            // response.completed / response.incomplete → message_delta + message_stop
            // ================================================
            "response.completed" | "response.incomplete" => {
                self.on_response_completed(event_name, data, out)
            }

            // ================================================
            // Semantic failures can be carried inside an HTTP 2xx SSE.
            // Preserve the upstream details instead of silently ending.
            // ================================================
            "response.failed" | "error" => self.on_response_failed(event_name, data, out),

            // Lifecycle events that don't need Anthropic counterparts.
            // Listed explicitly so new events trigger a match-completeness review.
            "response.output_text.done" => self.on_output_text_done(data, out),
            "response.output_item.done" => return self.on_output_item_done(data, out),
            "response.reasoning_summary_part.added"
            | "response.reasoning_summary_part.done"
            | "response.in_progress" => {}

            // Any other unknown/future events — silently skip.
            _ => {}
        }
        ControlFlow::Continue(())
    }

    fn on_response_created(&mut self, data: &Value, out: &mut Vec<Bytes>) {
        let response_obj = response_object_from_event(data);
        if let Some(id) = response_obj.get("id").and_then(|i| i.as_str()) {
            self.message_id = Some(id.to_string());
        }
        if let Some(model) = response_obj.get("model").and_then(|m| m.as_str()) {
            self.current_model = Some(model.to_string());
        }

        self.has_sent_message_start = true;
        // Build usage with defensive null handling
        // Some() wrapper ensures build function always receives valid input
        // Fallback to empty object {} if usage field missing, ensuring message_start
        // event always has valid usage structure for VSCode Extension compatibility
        let start_usage = build_anthropic_usage_from_responses(Some(
            response_obj.get("usage").unwrap_or(&json!({})),
        ));

        let event = json!({
            "type": "message_start",
            "message": {
                "id": self.message_id.clone().unwrap_or_default(),
                "type": "message",
                "role": "assistant",
                "model": self.current_model.clone().unwrap_or_default(),
                "usage": start_usage
            }
        });
        let sse = format!(
            "event: message_start\ndata: {}\n\n",
            serde_json::to_string(&event).unwrap_or_default()
        );
        log::debug!("[Claude/Responses] >>> Anthropic SSE: message_start");
        out.push(Bytes::from(sse));
    }

    fn on_content_part_added(&mut self, data: &Value, out: &mut Vec<Bytes>) {
        // 确保 message_start 已发送
        self.ensure_message_start(out);

        if let Some(part) = data.get("part") {
            let part_type = part.get("type").and_then(|t| t.as_str());
            if matches!(part_type, Some("output_text") | Some("refusal")) {
                if self.preserve_web_search_citations && part_type == Some("output_text") {
                    self.buffered_citation_text.record_part(data, part, false);
                    if self.current_text_index.is_none()
                        && part
                            .get("text")
                            .and_then(Value::as_str)
                            .is_some_and(|text| !text.is_empty())
                    {
                        self.current_text_index = Some(resolve_content_index(
                            data,
                            &mut self.next_content_index,
                            &mut self.index_by_key,
                            &mut self.fallback_open_index,
                        ));
                    }
                    return;
                }

                let index = if let Some(index) = self.current_text_index {
                    index
                } else {
                    let index = resolve_content_index(
                        data,
                        &mut self.next_content_index,
                        &mut self.index_by_key,
                        &mut self.fallback_open_index,
                    );
                    self.current_text_index = Some(index);
                    index
                };

                if self.open_indices.contains(&index) {
                    return;
                }

                let event = json!({
                    "type": "content_block_start",
                    "index": index,
                    "content_block": {
                        "type": "text",
                        "text": ""
                    }
                });
                let sse = format!(
                    "event: content_block_start\ndata: {}\n\n",
                    serde_json::to_string(&event).unwrap_or_default()
                );
                out.push(Bytes::from(sse));
                self.open_indices.insert(index);
            }
        }
    }

    fn on_output_text_delta(&mut self, data: &Value, out: &mut Vec<Bytes>) {
        if let Some(delta) = data.get("delta").and_then(|d| d.as_str()) {
            if self.preserve_web_search_citations {
                self.buffered_citation_text.record_delta(data, delta);
                if self.current_text_index.is_none() {
                    self.current_text_index = Some(resolve_content_index(
                        data,
                        &mut self.next_content_index,
                        &mut self.index_by_key,
                        &mut self.fallback_open_index,
                    ));
                }
                out.push(anthropic_ping_sse());
                return;
            }
            self.streamed_text.record_delta(data, delta);
            let index = if let Some(index) = self.current_text_index {
                index
            } else {
                let index = resolve_content_index(
                    data,
                    &mut self.next_content_index,
                    &mut self.index_by_key,
                    &mut self.fallback_open_index,
                );
                self.current_text_index = Some(index);
                index
            };

            if !self.open_indices.contains(&index) {
                let start_event = json!({
                    "type": "content_block_start",
                    "index": index,
                    "content_block": {
                        "type": "text",
                        "text": ""
                    }
                });
                let start_sse = format!(
                    "event: content_block_start\ndata: {}\n\n",
                    serde_json::to_string(&start_event).unwrap_or_default()
                );
                out.push(Bytes::from(start_sse));
                self.open_indices.insert(index);
            }
            let event = json!({
                "type": "content_block_delta",
                "index": index,
                "delta": {
                    "type": "text_delta",
                    "text": delta
                }
            });
            let sse = format!(
                "event: content_block_delta\ndata: {}\n\n",
                serde_json::to_string(&event).unwrap_or_default()
            );
            out.push(Bytes::from(sse));
        }
    }

    fn on_refusal_delta(&mut self, data: &Value, out: &mut Vec<Bytes>) {
        if let Some(delta) = data.get("delta").and_then(|d| d.as_str()) {
            self.streamed_text.record_delta(data, delta);
            let index = if let Some(index) = self.current_text_index {
                index
            } else {
                let index = resolve_content_index(
                    data,
                    &mut self.next_content_index,
                    &mut self.index_by_key,
                    &mut self.fallback_open_index,
                );
                self.current_text_index = Some(index);
                index
            };

            if !self.open_indices.contains(&index) {
                let start_event = json!({
                    "type": "content_block_start",
                    "index": index,
                    "content_block": {
                        "type": "text",
                        "text": ""
                    }
                });
                let start_sse = format!(
                    "event: content_block_start\ndata: {}\n\n",
                    serde_json::to_string(&start_event).unwrap_or_default()
                );
                out.push(Bytes::from(start_sse));
                self.open_indices.insert(index);
            }

            let event = json!({
                "type": "content_block_delta",
                "index": index,
                "delta": {
                    "type": "text_delta",
                    "text": delta
                }
            });
            let sse = format!(
                "event: content_block_delta\ndata: {}\n\n",
                serde_json::to_string(&event).unwrap_or_default()
            );
            out.push(Bytes::from(sse));
        }
    }

    fn on_buffered_content_part_done(&mut self, data: &Value) {
        let accepted = if let Some(part) = data
            .get("part")
            .filter(|part| part.get("type").and_then(Value::as_str) == Some("output_text"))
        {
            self.buffered_citation_text.record_part(data, part, true)
        } else {
            true
        };
        if accepted {
            self.buffered_citation_text.finish_event_part(data);
        }
    }

    fn on_output_item_added(&mut self, data: &Value, out: &mut Vec<Bytes>) -> ControlFlow<()> {
        if let Some(item) = data.get("item") {
            let item_type = item.get("type").and_then(|t| t.as_str()).unwrap_or("");
            if self.can_preserve_web_search_citations && item_type == "web_search_call" {
                self.preserve_web_search_citations = true;
            }
            if self.preserve_web_search_citations && item_type != "message" {
                let pending_text = self.buffered_citation_text.render_pending_parts();
                let mut reusable_text_index = None;
                if !pending_text.is_empty() {
                    if let Some(index) = self.current_text_index.take() {
                        let was_open = self.open_indices.remove(&index);
                        if was_open {
                            out.push(anthropic_sse(
                                "content_block_stop",
                                &json!({"type":"content_block_stop","index":index}),
                            ));
                        } else {
                            reusable_text_index = Some(index);
                        }
                        if self.fallback_open_index == Some(index) {
                            self.fallback_open_index = None;
                        }
                    }
                }
                for text in pending_text {
                    let index = reusable_text_index.take().unwrap_or_else(|| {
                        let index = self.next_content_index;
                        self.next_content_index += 1;
                        index
                    });
                    out.extend(text_block_events(index, &text));
                }
            }
            match item_type {
                "function_call" => self.on_function_call_item_added(data, item, out),
                "web_search_call" => return self.on_web_search_call_item_added(data, item, out),
                "reasoning" => self.on_reasoning_item_added(data, item, out),
                // message type output_item.added is handled via content_part.added
                _ => {}
            }
        }
        ControlFlow::Continue(())
    }

    fn on_function_call_item_added(&mut self, data: &Value, item: &Value, out: &mut Vec<Bytes>) {
        self.has_tool_use = true;
        self.has_substantive_output = true;
        if let Some(index) = self.current_text_index.take() {
            if self.open_indices.remove(&index) {
                let stop_event = json!({
                    "type": "content_block_stop",
                    "index": index
                });
                let stop_sse = format!(
                    "event: content_block_stop\ndata: {}\n\n",
                    serde_json::to_string(&stop_event).unwrap_or_default()
                );
                out.push(Bytes::from(stop_sse));
            }
            if self.fallback_open_index == Some(index) {
                self.fallback_open_index = None;
            }
        }
        // 确保 message_start 已发送
        self.ensure_message_start(out);

        let call_id = item.get("call_id").and_then(|i| i.as_str()).unwrap_or("");
        let name = item.get("name").and_then(|n| n.as_str()).unwrap_or("");
        let index = if let Some(k) = tool_item_key_from_added(data, item) {
            if let Some(existing) = self.index_by_key.get(&k).copied() {
                existing
            } else {
                let assigned = self.next_content_index;
                self.next_content_index += 1;
                self.index_by_key.insert(k, assigned);
                assigned
            }
        } else {
            let assigned = self.next_content_index;
            self.next_content_index += 1;
            assigned
        };
        if let Some(item_id) = item
            .get("id")
            .and_then(|v| v.as_str())
            .or_else(|| data.get("item_id").and_then(|v| v.as_str()))
        {
            self.tool_index_by_item_id
                .insert(item_id.to_string(), index);
        }
        self.tool_name_by_index.insert(index, name.to_string());
        self.last_tool_index = Some(index);

        if self.open_indices.contains(&index) {
            return;
        }

        self.tool_args_by_index.insert(index, String::new());

        let event = json!({
            "type": "content_block_start",
            "index": index,
            "content_block": {
                "type": "tool_use",
                "id": call_id,
                "name": name
            }
        });
        let sse = format!(
            "event: content_block_start\ndata: {}\n\n",
            serde_json::to_string(&event).unwrap_or_default()
        );
        out.push(Bytes::from(sse));
        self.open_indices.insert(index);
    }

    fn on_web_search_call_item_added(
        &mut self,
        data: &Value,
        item: &Value,
        out: &mut Vec<Bytes>,
    ) -> ControlFlow<()> {
        self.has_substantive_output = true;
        if let Some(index) = self.current_text_index.take() {
            if self.open_indices.remove(&index) {
                out.push(anthropic_sse(
                    "content_block_stop",
                    &json!({"type":"content_block_stop","index":index}),
                ));
            }
            if self.fallback_open_index == Some(index) {
                self.fallback_open_index = None;
            }
        }
        self.ensure_message_start(out);

        let item_keys = web_search_item_keys(data, Some(item));
        let index = if let Some(existing) = item_keys
            .iter()
            .find_map(|key| self.index_by_key.get(key).copied())
        {
            existing
        } else {
            let assigned = self.next_content_index;
            self.next_content_index += 1;
            assigned
        };
        for key in item_keys {
            self.index_by_key.insert(key, index);
        }
        let search_id = self
            .web_search_id_by_index
            .get(&index)
            .cloned()
            .or_else(|| {
                item.get("id")
                    .and_then(Value::as_str)
                    .or_else(|| data.get("item_id").and_then(Value::as_str))
                    .filter(|id| !id.is_empty())
                    .map(ToString::to_string)
            })
            .unwrap_or_else(|| format!("ws_stream_{index}"));
        let disposition = record_web_search_call(
            &search_id,
            item,
            &mut WebSearchRecordState {
                ids_seen: &mut self.web_search_ids_seen,
                id_order: &mut self.web_search_id_order,
                results_by_id: &mut self.web_search_results_by_id,
                errors_by_id: &mut self.web_search_errors_by_id,
                request_count: &mut self.web_search_count,
                max_uses: self.max_web_search_uses,
                limit_exceeded_id: &mut self.web_search_limit_exceeded_id,
            },
        );
        if disposition == WebSearchCallDisposition::Ignored {
            return ControlFlow::Continue(());
        }
        self.web_search_index_by_item_id
            .insert(search_id.clone(), index);
        self.web_search_id_by_index.insert(index, search_id.clone());
        reserve_web_search_result_index(
            &search_id,
            &mut self.web_search_result_index_by_id,
            &mut self.next_content_index,
        );

        if !self.open_indices.contains(&index) {
            out.push(anthropic_sse(
                "content_block_start",
                &json!({
                    "type":"content_block_start",
                    "index":index,
                    "content_block":{
                        "type":"server_tool_use",
                        "id":search_id,
                        "name":self.hosted_web_search_name.as_str(),
                        "input":{},
                        "caller":{"type":"direct"}
                    }
                }),
            ));
            self.open_indices.insert(index);
        }
        if disposition == WebSearchCallDisposition::LimitExceeded {
            out.push(anthropic_sse(
                "content_block_delta",
                &json!({
                    "type":"content_block_delta",
                    "index":index,
                    "delta":{
                        "type":"input_json_delta",
                        "partial_json":"{}"
                    }
                }),
            ));
            if self.open_indices.remove(&index) {
                out.push(anthropic_sse(
                    "content_block_stop",
                    &json!({"type":"content_block_stop","index":index}),
                ));
            }
            self.web_search_ids_completed.insert(search_id);
            out.extend(take_open_web_search_block_stop_events(
                &mut self.open_indices,
                &self.web_search_id_by_index,
            ));
            out.extend(take_web_search_result_events(
                &self.web_search_id_order,
                &mut self.web_search_results_by_id,
                &mut self.web_search_errors_by_id,
                &mut self.web_search_result_index_by_id,
                &mut self.next_content_index,
            ));
            if self.open_indices.is_empty() {
                for event in web_search_limit_stop_events(self.web_search_count, self.has_tool_use)
                {
                    out.push(event);
                }
            } else {
                out.push(anthropic_error_sse(
                    "Responses upstream started a web search beyond max_uses while another content block was incomplete",
                    "stream_truncated",
                ));
            }
            self.terminated = true;
            return ControlFlow::Break(());
        }
        ControlFlow::Continue(())
    }

    fn on_reasoning_item_added(&mut self, data: &Value, item: &Value, out: &mut Vec<Bytes>) {
        self.ensure_message_start(out);

        let index = if let Some(key) = reasoning_item_key(data, Some(item)) {
            if let Some(existing) = self.index_by_key.get(&key).copied() {
                existing
            } else {
                let assigned = self.next_content_index;
                self.next_content_index += 1;
                self.index_by_key.insert(key, assigned);
                assigned
            }
        } else {
            let assigned = self.next_content_index;
            self.next_content_index += 1;
            assigned
        };
        if let Some(item_id) = item
            .get("id")
            .and_then(Value::as_str)
            .or_else(|| data.get("item_id").and_then(Value::as_str))
        {
            self.reasoning_index_by_item_id
                .insert(item_id.to_string(), index);
        }
        self.reasoning_item_by_index.insert(index, item.clone());
        self.reasoning_text_by_index.entry(index).or_default();
    }

    fn on_function_call_arguments_delta(&mut self, data: &Value, out: &mut Vec<Bytes>) {
        if let Some(delta) = data.get("delta").and_then(|d| d.as_str()) {
            self.has_tool_use = true;
            let item_id = data.get("item_id").and_then(|v| v.as_str());
            let index = if let Some(id) = item_id {
                self.tool_index_by_item_id.get(id).copied()
            } else {
                None
            }
            .or_else(|| {
                tool_item_key_from_event(data).and_then(|k| self.index_by_key.get(&k).copied())
            })
            .or(self.last_tool_index)
            .unwrap_or_else(|| {
                let assigned = self.next_content_index;
                self.next_content_index += 1;
                assigned
            });

            if let Some(id) = item_id {
                self.tool_index_by_item_id.insert(id.to_string(), index);
            }
            if let Some(name) = data.get("name").and_then(Value::as_str) {
                self.tool_name_by_index.insert(index, name.to_string());
            } else {
                self.tool_name_by_index.entry(index).or_default();
            }
            self.last_tool_index = Some(index);

            if !self.open_indices.contains(&index) {
                let start_event = json!({
                    "type": "content_block_start",
                    "index": index,
                    "content_block": {
                        "type": "tool_use",
                        "id": data
                            .get("call_id")
                            .and_then(|v| v.as_str())
                            .or(item_id)
                            .unwrap_or(""),
                        "name": data
                            .get("name")
                            .and_then(|v| v.as_str())
                            .unwrap_or("")
                    }
                });
                let start_sse = format!(
                    "event: content_block_start\ndata: {}\n\n",
                    serde_json::to_string(&start_event).unwrap_or_default()
                );
                out.push(Bytes::from(start_sse));
                self.open_indices.insert(index);
            }

            self.tool_args_by_index
                .entry(index)
                .or_default()
                .push_str(delta);
            self.tool_had_delta.insert(index);

            if self.tool_name_by_index.get(&index).map(String::as_str) == Some("Read") {
                return;
            }

            let event = json!({
                "type": "content_block_delta",
                "index": index,
                "delta": {
                    "type": "input_json_delta",
                    "partial_json": delta
                }
            });
            let sse = format!(
                "event: content_block_delta\ndata: {}\n\n",
                serde_json::to_string(&event).unwrap_or_default()
            );
            out.push(Bytes::from(sse));
        }
    }

    fn on_function_call_arguments_done(&mut self, data: &Value, out: &mut Vec<Bytes>) {
        self.has_tool_use = true;
        let item_id = data.get("item_id").and_then(|v| v.as_str());
        let index = if let Some(id) = item_id {
            self.tool_index_by_item_id.get(id).copied()
        } else {
            None
        }
        .or_else(|| tool_item_key_from_event(data).and_then(|k| self.index_by_key.get(&k).copied()))
        .or(self.last_tool_index);
        if let Some(index) = index {
            if !self.open_indices.remove(&index) {
                return;
            }
            if self.tool_name_by_index.get(&index).map(String::as_str) == Some("Read") {
                // 有的兼容网关在 done 事件里给空串 arguments,完整参数只在分片里:
                // 空串当作没给,回退到已缓冲的分片
                let raw = data
                    .get("arguments")
                    .or_else(|| data.pointer("/item/arguments"))
                    .and_then(|v| v.as_str())
                    .filter(|value| !value.is_empty())
                    .map(str::to_string)
                    .unwrap_or_else(|| {
                        self.tool_args_by_index
                            .get(&index)
                            .cloned()
                            .unwrap_or_default()
                    });
                let sanitized = sanitize_anthropic_tool_use_input_json("Read", &raw);
                if !sanitized.is_empty() {
                    let event = json!({
                        "type": "content_block_delta",
                        "index": index,
                        "delta": {
                            "type": "input_json_delta",
                            "partial_json": sanitized
                        }
                    });
                    let sse = format!(
                        "event: content_block_delta\ndata: {}\n\n",
                        serde_json::to_string(&event).unwrap_or_default()
                    );
                    out.push(Bytes::from(sse));
                }
            } else if !self.tool_had_delta.contains(&index) {
                // Some compatible gateways skip delta events and only
                // provide the complete arguments on the done event.
                if let Some(arguments) = data
                    .get("arguments")
                    .or_else(|| data.pointer("/item/arguments"))
                    .and_then(Value::as_str)
                    .filter(|value| !value.is_empty())
                {
                    let event = json!({
                        "type": "content_block_delta",
                        "index": index,
                        "delta": {
                            "type": "input_json_delta",
                            "partial_json": arguments
                        }
                    });
                    let sse = format!(
                        "event: content_block_delta\ndata: {}\n\n",
                        serde_json::to_string(&event).unwrap_or_default()
                    );
                    out.push(Bytes::from(sse));
                }
            }
            let event = json!({
                "type": "content_block_stop",
                "index": index
            });
            let sse = format!(
                "event: content_block_stop\ndata: {}\n\n",
                serde_json::to_string(&event).unwrap_or_default()
            );
            out.push(Bytes::from(sse));
            if let Some(item_id) = item_id {
                self.tool_index_by_item_id.remove(item_id);
            }
            self.tool_name_by_index.remove(&index);
            self.tool_args_by_index.remove(&index);
            self.tool_had_delta.remove(&index);
        }
    }

    fn on_refusal_done(&mut self, data: &Value, out: &mut Vec<Bytes>) {
        self.streamed_text.finish_part(data);
        let index = self.current_text_index.take().or_else(|| {
            let key = content_part_key(data);
            if let Some(k) = key {
                self.index_by_key.get(&k).copied()
            } else {
                self.fallback_open_index
            }
        });
        if let Some(index) = index {
            if !self.open_indices.remove(&index) {
                return;
            }
            let event = json!({
                "type": "content_block_stop",
                "index": index
            });
            let sse = format!(
                "event: content_block_stop\ndata: {}\n\n",
                serde_json::to_string(&event).unwrap_or_default()
            );
            out.push(Bytes::from(sse));
            if self.fallback_open_index == Some(index) {
                self.fallback_open_index = None;
            }
        }
    }

    fn on_reasoning_delta(&mut self, data: &Value, out: &mut Vec<Bytes>) {
        if let Some(delta) = data
            .get("delta")
            .or_else(|| data.get("text"))
            .and_then(|d| d.as_str())
        {
            if let Some(index) = self.current_text_index.take() {
                if self.open_indices.remove(&index) {
                    let stop_event = json!({
                        "type": "content_block_stop",
                        "index": index
                    });
                    let stop_sse = format!(
                        "event: content_block_stop\ndata: {}\n\n",
                        serde_json::to_string(&stop_event).unwrap_or_default()
                    );
                    out.push(Bytes::from(stop_sse));
                }
                if self.fallback_open_index == Some(index) {
                    self.fallback_open_index = None;
                }
            }
            let item_id = data.get("item_id").and_then(Value::as_str);
            let item_key = reasoning_item_key(data, None);
            let is_keyless = item_id.is_none() && item_key.is_none();
            let index = item_id
                .and_then(|id| self.reasoning_index_by_item_id.get(id).copied())
                .or_else(|| {
                    item_key
                        .as_ref()
                        .and_then(|key| self.index_by_key.get(key).copied())
                })
                .or_else(|| is_keyless.then_some(self.legacy_reasoning_index).flatten())
                .unwrap_or_else(|| {
                    let assigned = self.next_content_index;
                    self.next_content_index += 1;
                    if let Some(key) = item_key {
                        self.index_by_key.insert(key, assigned);
                    }
                    if let Some(id) = item_id {
                        self.reasoning_index_by_item_id
                            .insert(id.to_string(), assigned);
                    } else if is_keyless {
                        self.legacy_reasoning_index = Some(assigned);
                    }
                    assigned
                });

            if !self.open_indices.contains(&index) {
                let start_event = json!({
                    "type": "content_block_start",
                    "index": index,
                    "content_block": {
                        "type": "thinking",
                        "thinking": ""
                    }
                });
                let start_sse = format!(
                    "event: content_block_start\ndata: {}\n\n",
                    serde_json::to_string(&start_event).unwrap_or_default()
                );
                out.push(Bytes::from(start_sse));
                self.open_indices.insert(index);
            }

            self.reasoning_text_by_index
                .entry(index)
                .or_default()
                .push_str(delta);

            let event = json!({
                "type": "content_block_delta",
                "index": index,
                "delta": {
                    "type": "thinking_delta",
                    "thinking": delta
                }
            });
            let sse = format!(
                "event: content_block_delta\ndata: {}\n\n",
                serde_json::to_string(&event).unwrap_or_default()
            );
            out.push(Bytes::from(sse));
        }
    }

    fn on_reasoning_text_done(&mut self, data: &Value, out: &mut Vec<Bytes>) {
        let item_id = data.get("item_id").and_then(Value::as_str);
        let item_key = reasoning_item_key(data, None);
        let index = item_id
            .and_then(|id| self.reasoning_index_by_item_id.get(id).copied())
            .or_else(|| {
                item_key
                    .as_ref()
                    .and_then(|key| self.index_by_key.get(key).copied())
            })
            .or_else(|| {
                (item_id.is_none() && item_key.is_none())
                    .then_some(self.legacy_reasoning_index)
                    .flatten()
            });
        if let Some(index) = index {
            let already_emitted = self
                .reasoning_text_by_index
                .get(&index)
                .is_some_and(|value| !value.is_empty());
            if !already_emitted {
                if let Some(text) = data
                    .get("text")
                    .and_then(Value::as_str)
                    .filter(|value| !value.is_empty())
                {
                    if !self.open_indices.contains(&index) {
                        let start_event = json!({
                            "type": "content_block_start",
                            "index": index,
                            "content_block": {"type": "thinking", "thinking": ""}
                        });
                        let start_sse = format!(
                            "event: content_block_start\ndata: {}\n\n",
                            serde_json::to_string(&start_event).unwrap_or_default()
                        );
                        out.push(Bytes::from(start_sse));
                        self.open_indices.insert(index);
                    }
                    self.reasoning_text_by_index
                        .entry(index)
                        .or_default()
                        .push_str(text);
                    let event = json!({
                        "type": "content_block_delta",
                        "index": index,
                        "delta": {"type": "thinking_delta", "thinking": text}
                    });
                    let sse = format!(
                        "event: content_block_delta\ndata: {}\n\n",
                        serde_json::to_string(&event).unwrap_or_default()
                    );
                    out.push(Bytes::from(sse));
                }
            }
        }
    }

    fn on_legacy_reasoning_done(&mut self, data: &Value, out: &mut Vec<Bytes>) {
        let item_id = data.get("item_id").and_then(Value::as_str);
        let item_key = reasoning_item_key(data, None);
        let index = item_id
            .and_then(|id| self.reasoning_index_by_item_id.get(id).copied())
            .or_else(|| {
                item_key
                    .as_ref()
                    .and_then(|key| self.index_by_key.get(key).copied())
            })
            .or_else(|| {
                (item_id.is_none() && item_key.is_none())
                    .then_some(self.legacy_reasoning_index)
                    .flatten()
            });
        if let Some(index) = index {
            if self.open_indices.remove(&index) {
                let event = json!({"type": "content_block_stop", "index": index});
                let sse = format!(
                    "event: content_block_stop\ndata: {}\n\n",
                    serde_json::to_string(&event).unwrap_or_default()
                );
                out.push(Bytes::from(sse));
            }
            if self.legacy_reasoning_index == Some(index) {
                self.legacy_reasoning_index = None;
            }
        }
    }

    fn on_response_completed(&mut self, event_name: &str, data: &Value, out: &mut Vec<Bytes>) {
        let response_obj = response_object_from_event(data);
        if matches!(
            response_obj.get("status").and_then(Value::as_str),
            Some("failed" | "cancelled")
        ) || response_obj
            .get("error")
            .is_some_and(|error| !error.is_null())
        {
            let (message, error_type) = responses_error_details(
                data,
                "Responses upstream returned a failed terminal response",
            );
            out.push(anthropic_error_sse(&message, &error_type));
            self.terminated = true;
            return;
        }
        if self.next_content_index == 0
            && !self.has_substantive_output
            && response_obj
                .get("output")
                .and_then(Value::as_array)
                .is_some()
        {
            let mut terminal_body = response_obj.clone();
            if terminal_body
                .get("status")
                .and_then(Value::as_str)
                .is_none()
            {
                terminal_body["status"] = json!(if event_name == "response.incomplete" {
                    "incomplete"
                } else {
                    "completed"
                });
            }
            for event in responses_json_to_anthropic_sse(
                terminal_body,
                Some(self.hosted_web_search_name.as_str()),
                self.max_web_search_uses,
            ) {
                if self.has_sent_message_start && event.starts_with(b"event: message_start\n") {
                    continue;
                }
                out.push(event);
            }
            self.terminated = true;
            return;
        }
        if !self.has_sent_message_start {
            if let Some(id) = response_obj.get("id").and_then(Value::as_str) {
                self.message_id = Some(id.to_string());
            }
            if let Some(model) = response_obj.get("model").and_then(Value::as_str) {
                self.current_model = Some(model.to_string());
            }
            out.push(anthropic_sse(
                "message_start",
                &json!({
                    "type":"message_start",
                    "message":{
                        "id":self.message_id.clone().unwrap_or_default(),
                        "type":"message",
                        "role":"assistant",
                        "model":self.current_model.clone().unwrap_or_default(),
                        "usage":{"input_tokens":0,"output_tokens":0}
                    }
                }),
            ));
            self.has_sent_message_start = true;
        }

        let mut terminal_web_search_results = std::mem::take(&mut self.pending_web_search_results);
        let mut terminal_message_items = Vec::new();
        let mut terminal_web_search_limit_exceeded = false;
        let mut terminal_reusable_text_index = None;
        if let Some(output) = response_obj.get("output").and_then(Value::as_array) {
            let has_web_search_output = output
                .iter()
                .any(|item| item.get("type").and_then(Value::as_str) == Some("web_search_call"));
            if has_web_search_output {
                if self.can_preserve_web_search_citations {
                    self.preserve_web_search_citations = true;
                }
                if let Some(text_index) = self.current_text_index.take() {
                    let was_open = self.open_indices.remove(&text_index);
                    if was_open {
                        out.push(anthropic_sse(
                            "content_block_stop",
                            &json!({"type":"content_block_stop","index":text_index}),
                        ));
                    } else if self.preserve_web_search_citations {
                        terminal_reusable_text_index = Some(text_index);
                    }
                    if self.fallback_open_index == Some(text_index) {
                        self.fallback_open_index = None;
                    }
                }
            }

            for (output_index, item) in output.iter().enumerate() {
                if item.get("type").and_then(Value::as_str) == Some("message") {
                    terminal_message_items.push((output_index as u64, item.clone()));
                }
                if item.get("type").and_then(Value::as_str) == Some("web_search_call") {
                    self.has_substantive_output = true;
                    let provisional_index = self
                        .index_by_key
                        .get(&format!("web-search:out:{output_index}"))
                        .copied();
                    let search_id = provisional_index
                        .and_then(|index| self.web_search_id_by_index.get(&index).cloned())
                        .or_else(|| {
                            item.get("id")
                                .and_then(Value::as_str)
                                .filter(|id| !id.is_empty())
                                .map(ToString::to_string)
                        })
                        .or_else(|| provisional_index.map(|index| format!("ws_stream_{index}")))
                        .unwrap_or_else(|| format!("ws_terminal_{output_index}"));
                    let disposition = record_web_search_call(
                        &search_id,
                        item,
                        &mut WebSearchRecordState {
                            ids_seen: &mut self.web_search_ids_seen,
                            id_order: &mut self.web_search_id_order,
                            results_by_id: &mut self.web_search_results_by_id,
                            errors_by_id: &mut self.web_search_errors_by_id,
                            request_count: &mut self.web_search_count,
                            max_uses: self.max_web_search_uses,
                            limit_exceeded_id: &mut self.web_search_limit_exceeded_id,
                        },
                    );
                    if disposition == WebSearchCallDisposition::Ignored {
                        continue;
                    }

                    if !self.web_search_ids_completed.contains(&search_id) {
                        let index = self
                            .web_search_index_by_item_id
                            .get(&search_id)
                            .copied()
                            .or(provisional_index)
                            .unwrap_or_else(|| {
                                let assigned = self.next_content_index;
                                self.next_content_index += 1;
                                assigned
                            });
                        self.web_search_index_by_item_id
                            .insert(search_id.clone(), index);
                        self.web_search_id_by_index.insert(index, search_id.clone());
                        reserve_web_search_result_index(
                            &search_id,
                            &mut self.web_search_result_index_by_id,
                            &mut self.next_content_index,
                        );
                        if !self.open_indices.contains(&index) {
                            out.push(anthropic_sse(
                                "content_block_start",
                                &json!({
                                    "type":"content_block_start",
                                    "index":index,
                                    "content_block":{
                                        "type":"server_tool_use",
                                        "id":search_id,
                                        "name":self.hosted_web_search_name.as_str(),
                                        "input":{},
                                        "caller":{"type":"direct"}
                                    }
                                }),
                            ));
                            self.open_indices.insert(index);
                        }
                        let input = web_search_action_input(item);
                        out.push(anthropic_sse(
                            "content_block_delta",
                            &json!({
                                "type":"content_block_delta",
                                "index":index,
                                "delta":{
                                    "type":"input_json_delta",
                                    "partial_json":serde_json::to_string(&input)
                                        .unwrap_or_else(|_| "{}".to_string())
                                }
                            }),
                        ));
                        if self.open_indices.remove(&index) {
                            out.push(anthropic_sse(
                                "content_block_stop",
                                &json!({"type":"content_block_stop","index":index}),
                            ));
                        }
                        self.web_search_ids_completed.insert(search_id);
                    }
                    if disposition == WebSearchCallDisposition::LimitExceeded {
                        terminal_web_search_limit_exceeded = true;
                        break;
                    }
                }

                for result in web_search_results_from_output_item(item) {
                    let Some(url) = result.get("url").and_then(Value::as_str) else {
                        continue;
                    };
                    if self.seen_web_search_result_urls.insert(url.to_string()) {
                        terminal_web_search_results.push(result);
                    }
                }
            }
        }
        if terminal_web_search_limit_exceeded {
            terminal_message_items.clear();
            terminal_web_search_results.clear();
        }

        for (search_id, results) in &mut self.web_search_results_by_id {
            if !self.web_search_errors_by_id.contains_key(search_id) {
                merge_web_search_result_metadata(results, &terminal_web_search_results);
            }
        }
        let attributed_web_search_urls: HashSet<String> = self
            .web_search_results_by_id
            .iter()
            .filter(|(search_id, _)| !self.web_search_errors_by_id.contains_key(*search_id))
            .flat_map(|(_, results)| results)
            .filter_map(|result| {
                result
                    .get("url")
                    .and_then(Value::as_str)
                    .map(ToString::to_string)
            })
            .collect();
        // Final-message citations have no search-call ID.
        // Prefer action.sources recorded for each call,
        // then put only otherwise-unassigned citations on
        // the last call. Every earlier call still receives
        // an empty successful result rather than remaining
        // structurally unmatched.
        if let Some(last_search_id) = self
            .web_search_id_order
            .iter()
            .rev()
            .find(|search_id| !self.web_search_errors_by_id.contains_key(*search_id))
            .cloned()
        {
            terminal_web_search_results.retain(|result| {
                result
                    .get("url")
                    .and_then(Value::as_str)
                    .is_some_and(|url| !attributed_web_search_urls.contains(url))
            });
            append_unique_web_search_results(
                self.web_search_results_by_id
                    .entry(last_search_id)
                    .or_default(),
                terminal_web_search_results,
            );
        }

        if !self.web_search_id_order.is_empty() {
            if let Some(text_index) = self.current_text_index.take() {
                let was_open = self.open_indices.remove(&text_index);
                if was_open {
                    out.push(anthropic_sse(
                        "content_block_stop",
                        &json!({"type":"content_block_stop","index":text_index}),
                    ));
                } else if self.preserve_web_search_citations
                    && terminal_reusable_text_index.is_none()
                {
                    terminal_reusable_text_index = Some(text_index);
                }
                if self.fallback_open_index == Some(text_index) {
                    self.fallback_open_index = None;
                }
            }
            // An incomplete terminal response can arrive before
            // output_item.done. Close any still-open server tool
            // block before emitting its paired error result.
            for search_id in self.web_search_id_order.clone() {
                if self.web_search_ids_completed.contains(&search_id) {
                    continue;
                }
                let Some(index) = self.web_search_index_by_item_id.get(&search_id).copied() else {
                    continue;
                };
                if self.open_indices.contains(&index) {
                    out.push(anthropic_sse(
                        "content_block_delta",
                        &json!({
                            "type":"content_block_delta",
                            "index":index,
                            "delta":{
                                "type":"input_json_delta",
                                "partial_json":"{}"
                            }
                        }),
                    ));
                }
                if self.open_indices.remove(&index) {
                    out.push(anthropic_sse(
                        "content_block_stop",
                        &json!({"type":"content_block_stop","index":index}),
                    ));
                }
                self.web_search_ids_completed.insert(search_id);
            }
            for search_id in self.web_search_id_order.clone() {
                let index = self
                    .web_search_result_index_by_id
                    .remove(&search_id)
                    .unwrap_or_else(|| {
                        let index = self.next_content_index;
                        self.next_content_index += 1;
                        index
                    });
                let results = self
                    .web_search_results_by_id
                    .remove(&search_id)
                    .unwrap_or_default();
                let content = self
                    .web_search_errors_by_id
                    .remove(&search_id)
                    .unwrap_or(Value::Array(results));
                out.extend(web_search_result_events(index, &search_id, content));
            }
        }

        for (output_index, item) in terminal_message_items {
            let buffered_citations = self
                .preserve_web_search_citations
                .then_some(&mut self.buffered_citation_text);
            let missing_text = missing_message_text_parts(
                &item,
                Some(output_index),
                &mut self.streamed_text,
                buffered_citations,
            );
            if missing_text.is_empty() {
                continue;
            }
            self.has_substantive_output = true;
            let mut reusable_text_index = terminal_reusable_text_index.take();
            if let Some(text_index) = self.current_text_index.take() {
                let was_open = self.open_indices.remove(&text_index);
                if was_open {
                    out.push(anthropic_sse(
                        "content_block_stop",
                        &json!({"type":"content_block_stop","index":text_index}),
                    ));
                } else if self.preserve_web_search_citations {
                    reusable_text_index.get_or_insert(text_index);
                }
                if self.fallback_open_index == Some(text_index) {
                    self.fallback_open_index = None;
                }
            }
            for text in missing_text {
                let index = reusable_text_index.take().unwrap_or_else(|| {
                    let index = self.next_content_index;
                    self.next_content_index += 1;
                    index
                });
                out.extend(text_block_events(index, &text));
            }
        }
        if self.preserve_web_search_citations {
            let pending_text = self.buffered_citation_text.render_pending_parts();
            let mut reusable_text_index = terminal_reusable_text_index.take();
            if !pending_text.is_empty() {
                self.has_substantive_output = true;
                if let Some(text_index) = self.current_text_index.take() {
                    let was_open = self.open_indices.remove(&text_index);
                    if was_open {
                        out.push(anthropic_sse(
                            "content_block_stop",
                            &json!({"type":"content_block_stop","index":text_index}),
                        ));
                    } else {
                        reusable_text_index.get_or_insert(text_index);
                    }
                }
            }
            for text in pending_text {
                let index = reusable_text_index.take().unwrap_or_else(|| {
                    let index = self.next_content_index;
                    self.next_content_index += 1;
                    index
                });
                out.extend(text_block_events(index, &text));
            }
        }

        let terminal_status =
            response_obj
                .get("status")
                .and_then(Value::as_str)
                .or(match event_name {
                    "response.incomplete" => Some("incomplete"),
                    "response.completed" => Some("completed"),
                    _ => None,
                });
        let stop_reason = map_responses_stop_reason(
            terminal_status,
            self.has_tool_use,
            response_obj
                .pointer("/incomplete_details/reason")
                .and_then(|r| r.as_str()),
        );

        // Zero-usage max_tokens stops are upstream request
        // rejections, not truncation; report them as an
        // error instead of a fabricated limit hit.
        if stop_reason == Some("max_tokens")
            && !self.has_substantive_output
            && is_unprocessed_max_output_rejection(response_obj, terminal_status)
        {
            log::warn!(
                "[Claude/Responses] terminal incomplete with zero usage \
    and no content; emitting api_error for the unprocessed upstream rejection instead of max_tokens"
            );
            out.push(anthropic_error_sse(
                UNPROCESSED_REJECTION_MESSAGE,
                "api_error",
            ));
            self.terminated = true;
            return;
        }

        // Best effort: close any dangling blocks before message_delta/message_stop.
        if !self.open_indices.is_empty() {
            let mut remaining: Vec<u32> = self.open_indices.iter().copied().collect();
            remaining.sort_unstable();
            for index in remaining {
                let stop_event = json!({
                    "type": "content_block_stop",
                    "index": index
                });
                let stop_sse = format!(
                    "event: content_block_stop\ndata: {}\n\n",
                    serde_json::to_string(&stop_event).unwrap_or_default()
                );
                out.push(Bytes::from(stop_sse));
                self.open_indices.remove(&index);
            }
        }
        self.fallback_open_index = None;

        // message_delta and message_stop are built as they are yielded, so a
        // client that stops reading after the dangling block stops leaves the
        // same trace as before (see `MessageEnd`).
        self.message_end = Some(MessageEnd {
            stop_reason,
            usage: response_obj
                .get("usage")
                .cloned()
                .unwrap_or_else(|| json!({})),
            web_search_count: self.web_search_count,
        });
        self.terminated = true;
    }

    fn on_response_failed(&mut self, event_name: &str, data: &Value, out: &mut Vec<Bytes>) {
        let (message, error_type) = responses_error_details(
            data,
            if event_name == "response.failed" {
                "Responses upstream reported response.failed"
            } else {
                "Responses upstream emitted an error event"
            },
        );
        out.push(anthropic_error_sse(&message, &error_type));
        self.terminated = true;
    }

    fn on_output_text_done(&mut self, data: &Value, out: &mut Vec<Bytes>) {
        if self.preserve_web_search_citations {
            self.buffered_citation_text.record_done_event(data);
            return;
        }
        self.streamed_text.finish_part(data);
        if let Some(index) = self.current_text_index.take() {
            if self.open_indices.remove(&index) {
                let stop_event = json!({
                    "type": "content_block_stop",
                    "index": index
                });
                let stop_sse = format!(
                    "event: content_block_stop\ndata: {}\n\n",
                    serde_json::to_string(&stop_event).unwrap_or_default()
                );
                out.push(Bytes::from(stop_sse));
            }
            if self.fallback_open_index == Some(index) {
                self.fallback_open_index = None;
            }
        }
    }

    fn on_output_item_done(&mut self, data: &Value, out: &mut Vec<Bytes>) -> ControlFlow<()> {
        let Some(item) = data.get("item") else {
            return ControlFlow::Continue(());
        };
        let item_type = item.get("type").and_then(Value::as_str);
        if self.can_preserve_web_search_citations && item_type == Some("web_search_call") {
            self.preserve_web_search_citations = true;
        }
        match item_type {
            Some("function_call") => self.on_function_call_item_done(data, item, out),
            Some("web_search_call") => return self.on_web_search_call_item_done(data, item, out),
            Some("reasoning") => self.on_reasoning_item_done(data, item, out),
            Some("message") => self.on_message_item_done(data, item, out),
            _ => {}
        }
        ControlFlow::Continue(())
    }

    fn on_function_call_item_done(&mut self, data: &Value, item: &Value, out: &mut Vec<Bytes>) {
        self.has_tool_use = true;
        let item_id = item
            .get("id")
            .and_then(Value::as_str)
            .or_else(|| data.get("item_id").and_then(Value::as_str));
        let index = item_id
            .and_then(|id| self.tool_index_by_item_id.get(id).copied())
            .or_else(|| {
                tool_item_key_from_event(data).and_then(|key| self.index_by_key.get(&key).copied())
            })
            .or(self.last_tool_index);
        if let Some(index) = index.filter(|value| self.open_indices.contains(value)) {
            let name = self
                .tool_name_by_index
                .get(&index)
                .map(String::as_str)
                .unwrap_or("");
            if !self.tool_had_delta.contains(&index) || name == "Read" {
                let raw = item
                    .get("arguments")
                    .and_then(Value::as_str)
                    .filter(|value| !value.is_empty())
                    .map(str::to_string)
                    .unwrap_or_else(|| {
                        self.tool_args_by_index
                            .get(&index)
                            .cloned()
                            .unwrap_or_default()
                    });
                let arguments = if name == "Read" {
                    sanitize_anthropic_tool_use_input_json(name, &raw)
                } else {
                    raw
                };
                if !arguments.is_empty() {
                    let event = json!({
                        "type": "content_block_delta",
                        "index": index,
                        "delta": {
                            "type": "input_json_delta",
                            "partial_json": arguments
                        }
                    });
                    let sse = format!(
                        "event: content_block_delta\ndata: {}\n\n",
                        serde_json::to_string(&event).unwrap_or_default()
                    );
                    out.push(Bytes::from(sse));
                }
            }
            self.open_indices.remove(&index);
            let event = json!({"type": "content_block_stop", "index": index});
            let sse = format!(
                "event: content_block_stop\ndata: {}\n\n",
                serde_json::to_string(&event).unwrap_or_default()
            );
            out.push(Bytes::from(sse));
            if let Some(id) = item_id {
                self.tool_index_by_item_id.remove(id);
            }
            self.tool_name_by_index.remove(&index);
            self.tool_args_by_index.remove(&index);
            self.tool_had_delta.remove(&index);
        }
    }

    fn on_web_search_call_item_done(
        &mut self,
        data: &Value,
        item: &Value,
        out: &mut Vec<Bytes>,
    ) -> ControlFlow<()> {
        self.has_substantive_output = true;
        if self.preserve_web_search_citations {
            let pending_text = self.buffered_citation_text.render_pending_parts();
            let mut reusable_text_index = None;
            if !pending_text.is_empty() {
                if let Some(text_index) = self.current_text_index.take() {
                    let was_open = self.open_indices.remove(&text_index);
                    if was_open {
                        out.push(anthropic_sse(
                            "content_block_stop",
                            &json!({"type":"content_block_stop","index":text_index}),
                        ));
                    } else {
                        reusable_text_index = Some(text_index);
                    }
                    if self.fallback_open_index == Some(text_index) {
                        self.fallback_open_index = None;
                    }
                }
            }
            for text in pending_text {
                let index = reusable_text_index.take().unwrap_or_else(|| {
                    let index = self.next_content_index;
                    self.next_content_index += 1;
                    index
                });
                out.extend(text_block_events(index, &text));
            }
        }
        self.ensure_message_start(out);

        let item_keys = web_search_item_keys(data, Some(item));
        let provisional_index = item_keys
            .iter()
            .find_map(|key| self.index_by_key.get(key).copied());
        let search_id = provisional_index
            .and_then(|index| self.web_search_id_by_index.get(&index).cloned())
            .or_else(|| {
                item.get("id")
                    .and_then(Value::as_str)
                    .or_else(|| data.get("item_id").and_then(Value::as_str))
                    .filter(|id| !id.is_empty())
                    .map(ToString::to_string)
            })
            .or_else(|| provisional_index.map(|index| format!("ws_stream_{index}")))
            .unwrap_or_else(|| format!("ws_stream_{}", self.next_content_index));
        let disposition = record_web_search_call(
            &search_id,
            item,
            &mut WebSearchRecordState {
                ids_seen: &mut self.web_search_ids_seen,
                id_order: &mut self.web_search_id_order,
                results_by_id: &mut self.web_search_results_by_id,
                errors_by_id: &mut self.web_search_errors_by_id,
                request_count: &mut self.web_search_count,
                max_uses: self.max_web_search_uses,
                limit_exceeded_id: &mut self.web_search_limit_exceeded_id,
            },
        );
        if disposition == WebSearchCallDisposition::Ignored {
            return ControlFlow::Continue(());
        }
        if self.web_search_ids_completed.contains(&search_id) {
            return ControlFlow::Continue(());
        }

        let index = self
            .web_search_index_by_item_id
            .get(&search_id)
            .copied()
            .or(provisional_index)
            .unwrap_or_else(|| {
                let assigned = self.next_content_index;
                self.next_content_index += 1;
                assigned
            });
        for key in item_keys {
            self.index_by_key.insert(key, index);
        }
        self.web_search_index_by_item_id
            .insert(search_id.clone(), index);
        self.web_search_id_by_index.insert(index, search_id.clone());
        reserve_web_search_result_index(
            &search_id,
            &mut self.web_search_result_index_by_id,
            &mut self.next_content_index,
        );

        if let Some(text_index) = self.current_text_index.take() {
            if self.open_indices.remove(&text_index) {
                out.push(anthropic_sse(
                    "content_block_stop",
                    &json!({"type":"content_block_stop","index":text_index}),
                ));
            }
            if self.fallback_open_index == Some(text_index) {
                self.fallback_open_index = None;
            }
        }

        if !self.open_indices.contains(&index) {
            out.push(anthropic_sse(
                "content_block_start",
                &json!({
                    "type":"content_block_start",
                    "index":index,
                    "content_block":{
                        "type":"server_tool_use",
                        "id":search_id,
                        "name":self.hosted_web_search_name.as_str(),
                        "input":{},
                        "caller":{"type":"direct"}
                    }
                }),
            ));
            self.open_indices.insert(index);
        }

        let input = web_search_action_input(item);
        out.push(anthropic_sse(
            "content_block_delta",
            &json!({
                "type":"content_block_delta",
                "index":index,
                "delta":{
                    "type":"input_json_delta",
                    "partial_json":serde_json::to_string(&input)
                        .unwrap_or_else(|_| "{}".to_string())
                }
            }),
        ));
        if self.open_indices.remove(&index) {
            out.push(anthropic_sse(
                "content_block_stop",
                &json!({"type":"content_block_stop","index":index}),
            ));
        }
        self.web_search_ids_completed.insert(search_id);
        if disposition == WebSearchCallDisposition::LimitExceeded {
            out.extend(take_open_web_search_block_stop_events(
                &mut self.open_indices,
                &self.web_search_id_by_index,
            ));
            out.extend(take_web_search_result_events(
                &self.web_search_id_order,
                &mut self.web_search_results_by_id,
                &mut self.web_search_errors_by_id,
                &mut self.web_search_result_index_by_id,
                &mut self.next_content_index,
            ));
            if self.open_indices.is_empty() {
                for event in web_search_limit_stop_events(self.web_search_count, self.has_tool_use)
                {
                    out.push(event);
                }
            } else {
                out.push(anthropic_error_sse(
                    "Responses upstream started a web search beyond max_uses while another content block was incomplete",
                    "stream_truncated",
                ));
            }
            self.terminated = true;
            return ControlFlow::Break(());
        }
        ControlFlow::Continue(())
    }

    fn on_reasoning_item_done(&mut self, data: &Value, item: &Value, out: &mut Vec<Bytes>) {
        let item_id = item
            .get("id")
            .and_then(Value::as_str)
            .or_else(|| data.get("item_id").and_then(Value::as_str));
        let index = item_id
            .and_then(|id| self.reasoning_index_by_item_id.get(id).copied())
            .or_else(|| {
                reasoning_item_key(data, Some(item))
                    .and_then(|key| self.index_by_key.get(&key).copied())
            })
            .unwrap_or_else(|| {
                let assigned = self.next_content_index;
                self.next_content_index += 1;
                assigned
            });
        self.reasoning_item_by_index.insert(index, item.clone());

        let final_item = self
            .reasoning_item_by_index
            .get(&index)
            .cloned()
            .unwrap_or_else(|| item.clone());
        let full_text = reasoning_summary_text(&final_item);
        let emitted_text = self
            .reasoning_text_by_index
            .get(&index)
            .cloned()
            .unwrap_or_default();
        if emitted_text.is_empty() && !full_text.is_empty() {
            let start_event = json!({
                "type": "content_block_start",
                "index": index,
                "content_block": {"type": "thinking", "thinking": ""}
            });
            let start_sse = format!(
                "event: content_block_start\ndata: {}\n\n",
                serde_json::to_string(&start_event).unwrap_or_default()
            );
            out.push(Bytes::from(start_sse));
            self.open_indices.insert(index);
            let delta_event = json!({
                "type": "content_block_delta",
                "index": index,
                "delta": {"type": "thinking_delta", "thinking": full_text}
            });
            let delta_sse = format!(
                "event: content_block_delta\ndata: {}\n\n",
                serde_json::to_string(&delta_event).unwrap_or_default()
            );
            out.push(Bytes::from(delta_sse));
        } else if self.open_indices.contains(&index) {
            // Deltas may cover only the start of the summary
            // (e.g. a later summary part arrived without
            // deltas). Emit the missing tail; snapshots that
            // do not extend the streamed text are ignored.
            if let Some(suffix) = full_text
                .strip_prefix(emitted_text.as_str())
                .filter(|suffix| !suffix.is_empty())
            {
                let delta_event = json!({
                    "type": "content_block_delta",
                    "index": index,
                    "delta": {"type": "thinking_delta", "thinking": suffix}
                });
                let delta_sse = format!(
                    "event: content_block_delta\ndata: {}\n\n",
                    serde_json::to_string(&delta_event).unwrap_or_default()
                );
                out.push(Bytes::from(delta_sse));
            }
        }

        let encrypted = final_item
            .get("encrypted_content")
            .and_then(Value::as_str)
            .is_some_and(|value| !value.is_empty());
        if encrypted {
            if let Some(envelope) = encode_openai_reasoning_item(&final_item) {
                if self.open_indices.contains(&index) {
                    let signature_event = json!({
                        "type": "content_block_delta",
                        "index": index,
                        "delta": {
                            "type": "signature_delta",
                            "signature": envelope
                        }
                    });
                    let signature_sse = format!(
                        "event: content_block_delta\ndata: {}\n\n",
                        serde_json::to_string(&signature_event).unwrap_or_default()
                    );
                    out.push(Bytes::from(signature_sse));
                } else {
                    // Encrypted-only item: an empty thinking block
                    // carries the envelope (Claude Code's VS Code
                    // extension cannot render redacted_thinking).
                    let start_event = json!({
                        "type": "content_block_start",
                        "index": index,
                        "content_block": {"type": "thinking", "thinking": ""}
                    });
                    let start_sse = format!(
                        "event: content_block_start\ndata: {}\n\n",
                        serde_json::to_string(&start_event).unwrap_or_default()
                    );
                    out.push(Bytes::from(start_sse));
                    self.open_indices.insert(index);
                    let signature_event = json!({
                        "type": "content_block_delta",
                        "index": index,
                        "delta": {
                            "type": "signature_delta",
                            "signature": envelope
                        }
                    });
                    let signature_sse = format!(
                        "event: content_block_delta\ndata: {}\n\n",
                        serde_json::to_string(&signature_event).unwrap_or_default()
                    );
                    out.push(Bytes::from(signature_sse));
                }
            }
        }
        if self.open_indices.remove(&index) {
            let stop_event = json!({"type": "content_block_stop", "index": index});
            let stop_sse = format!(
                "event: content_block_stop\ndata: {}\n\n",
                serde_json::to_string(&stop_event).unwrap_or_default()
            );
            out.push(Bytes::from(stop_sse));
        }
        if let Some(id) = item_id {
            self.reasoning_index_by_item_id.remove(id);
        }
        self.reasoning_item_by_index.remove(&index);
        self.reasoning_text_by_index.remove(&index);
    }

    fn on_message_item_done(&mut self, data: &Value, item: &Value, out: &mut Vec<Bytes>) {
        let buffered_citations = self
            .preserve_web_search_citations
            .then_some(&mut self.buffered_citation_text);
        let missing_text = missing_message_text_parts(
            item,
            data.get("output_index").and_then(Value::as_u64),
            &mut self.streamed_text,
            buffered_citations,
        );
        if !missing_text.is_empty() {
            self.has_substantive_output = true;
            self.ensure_message_start(out);
            let mut reusable_text_index = None;
            if let Some(index) = self.current_text_index.take() {
                let was_open = self.open_indices.remove(&index);
                if was_open {
                    out.push(anthropic_sse(
                        "content_block_stop",
                        &json!({"type":"content_block_stop","index":index}),
                    ));
                } else if self.preserve_web_search_citations {
                    reusable_text_index = Some(index);
                }
                if self.fallback_open_index == Some(index) {
                    self.fallback_open_index = None;
                }
            }
            for text in missing_text {
                let index = reusable_text_index.take().unwrap_or_else(|| {
                    let index = self.next_content_index;
                    self.next_content_index += 1;
                    index
                });
                out.extend(text_block_events(index, &text));
            }
        }

        let mut new_results = Vec::new();
        for result in web_search_results_from_output_item(item) {
            let Some(url) = result.get("url").and_then(Value::as_str) else {
                continue;
            };
            if self.seen_web_search_result_urls.insert(url.to_string()) {
                new_results.push(result);
            }
        }
        append_unique_web_search_results(&mut self.pending_web_search_results, new_results);
    }

    /// The upstream ended without a terminal event.
    fn finish_unterminated(&mut self, out: &mut Vec<Bytes>) {
        let has_open_tool = self.open_indices.iter().any(|index| {
            self.tool_name_by_index.contains_key(index)
                || self.tool_args_by_index.contains_key(index)
        });
        // Hosted-search results are intentionally deferred until the
        // response terminal event so final citations can be paired. Any
        // observed search at clean EOF is therefore still structurally
        // unpaired, even if its server_tool_use block was already closed.
        let has_unpaired_server_tool = !self.web_search_id_order.is_empty();
        let has_open_reasoning = self.open_indices.iter().any(|index| {
            self.reasoning_item_by_index.contains_key(index)
                || self.reasoning_text_by_index.contains_key(index)
                || self.legacy_reasoning_index == Some(*index)
        });

        if self.has_substantive_output
            && !has_open_tool
            && !has_unpaired_server_tool
            && !has_open_reasoning
        {
            if self.preserve_web_search_citations {
                let pending_text = self.buffered_citation_text.render_pending_parts();
                let mut reusable_text_index = None;
                if !pending_text.is_empty() {
                    if let Some(index) = self.current_text_index.take() {
                        let was_open = self.open_indices.remove(&index);
                        if was_open {
                            out.push(anthropic_sse(
                                "content_block_stop",
                                &json!({"type":"content_block_stop","index":index}),
                            ));
                        } else {
                            reusable_text_index = Some(index);
                        }
                    }
                }
                for text in pending_text {
                    let index = reusable_text_index.take().unwrap_or_else(|| {
                        let index = self.next_content_index;
                        self.next_content_index += 1;
                        index
                    });
                    out.extend(text_block_events(index, &text));
                }
            }
            // Text-only partial output is safe to expose as a max-token style
            // incomplete turn. Close blocks before the terminal events.
            let mut remaining: Vec<u32> = self.open_indices.iter().copied().collect();
            remaining.sort_unstable();
            for index in remaining {
                out.push(anthropic_sse(
                    "content_block_stop",
                    &json!({"type":"content_block_stop","index":index}),
                ));
            }
            self.ensure_message_start(out);
            out.push(anthropic_sse(
                "message_delta",
                &json!({
                    "type":"message_delta",
                    "delta":{"stop_reason":"max_tokens","stop_sequence":null},
                    "usage":{"input_tokens":0,"output_tokens":0}
                }),
            ));
            out.push(anthropic_sse(
                "message_stop",
                &json!({"type":"message_stop"}),
            ));
        } else {
            // A truncated tool/reasoning block cannot be safely finalized: tool
            // JSON may be partial and thinking may be missing its signature.
            out.push(anthropic_error_sse(
                "Responses upstream stream ended before a terminal event",
                "stream_truncated",
            ));
        }
    }
}

fn create_anthropic_sse_stream_from_responses_raw<E: std::error::Error + Send + 'static>(
    stream: impl Stream<Item = Result<Bytes, E>> + Send + 'static,
    hosted_web_search_name: String,
    max_web_search_uses: Option<u64>,
    can_preserve_web_search_citations: bool,
) -> impl Stream<Item = Result<Bytes, std::io::Error>> + Send {
    async_stream::stream! {
        let mut sse = SseBuffer::new();
        let mut state = ResponsesToAnthropicState::new(
            hosted_web_search_name,
            max_web_search_uses,
            can_preserve_web_search_citations,
        );
        let mut out = Vec::new();

        // Append an EOF sentinel so the same parser handles a final SSE event that
        // omitted its trailing blank line. The boolean distinguishes the sentinel
        // from a legitimate empty upstream chunk.
        let stream = stream
            .map(|result| (result, false))
            .chain(futures::stream::once(async {
                (Ok::<Bytes, E>(Bytes::new()), true)
            }));
        tokio::pin!(stream);

        'stream_loop: while let Some((chunk, is_eof)) = stream.next().await {
            match chunk {
                Ok(bytes) => {
                    if let Err(error) = sse.try_push_bytes(&bytes) {
                        if !state.terminated {
                            yield Ok(anthropic_error_sse(&error.to_string(), "stream_error"));
                        }
                        return;
                    }

                    // A few compatible gateways ignore stream:true and return one
                    // JSON document. Hold it intact until EOF, including any pretty-
                    // printed blank lines that would otherwise look like SSE separators.
                    let looks_like_json = matches!(
                        sse.pending()
                            .trim_start_matches(|ch: char| ch.is_whitespace() || ch == '\u{feff}')
                            .as_bytes()
                            .first(),
                        Some(b'{') | Some(b'[')
                    );
                    if looks_like_json && !is_eof {
                        if let Err(error) = sse.check_pending_document_size() {
                            yield Ok(anthropic_error_sse(&error.to_string(), "stream_error"));
                            return;
                        }
                        continue;
                    }
                    if looks_like_json && is_eof {
                        match serde_json::from_str::<Value>(sse.pending().trim()) {
                            Ok(body) => {
                                for event in responses_json_to_anthropic_sse(
                                    body,
                                    Some(state.hosted_web_search_name.as_str()),
                                    state.max_web_search_uses,
                                ) {
                                    yield Ok(event);
                                }
                                state.terminated = true;
                            }
                            Err(error) => {
                                yield Ok(anthropic_error_sse(
                                    &format!("Invalid JSON response from Responses upstream: {error}"),
                                    "response_parse_error",
                                ));
                                state.terminated = true;
                            }
                        }
                        sse.clear();
                        continue;
                    }

                    if is_eof && !sse.pending().trim().is_empty() {
                        sse.push_str("\n\n");
                    }

                    // SSE 事件由 \n\n 分隔
                    while let Some(block) = sse.next_block() {
                        if block.trim().is_empty() {
                            continue;
                        }

                        // 解析 SSE 块:提取 event: 和 data: 行
                        let mut event_type: Option<String> = None;
                        let mut data_parts: Vec<String> = Vec::new();

                        for line in block.lines() {
                            if let Some(evt) = strip_sse_field(line, "event") {
                                event_type = Some(evt.trim().to_string());
                            } else if let Some(d) = strip_sse_field(line, "data") {
                                data_parts.push(d.to_string());
                            }
                        }

                        if data_parts.is_empty() {
                            continue;
                        }

                        let data_str = data_parts.join("\n");

                        // 解析 JSON 数据
                        let data: Value = match serde_json::from_str(&data_str) {
                            Ok(v) => v,
                            Err(_) => continue,
                        };

                        // Official streams use both a named SSE event and `type` in
                        // the JSON payload. Compatible gateways sometimes omit the
                        // `event:` line, so fall back to the payload type.
                        let event_name = event_type
                            .as_deref()
                            .filter(|name| !name.is_empty())
                            .or_else(|| data.get("type").and_then(Value::as_str))
                            .unwrap_or("");

                        log::debug!("[Claude/Responses] <<< SSE event: {event_name}");

                        // Ignore every event after a terminal response. In particular,
                        // do not synthesize message_start if a broken gateway emits a
                        // late delta after response.failed/error.
                        if state.terminated {
                            continue;
                        }

                        let flow = state.handle_event(event_name, &data, &mut out);
                        for bytes in out.drain(..) {
                            yield Ok(bytes);
                        }
                        if let Some(end) = state.message_end.take() {
                            yield Ok(end.message_delta());
                            yield Ok(MessageEnd::message_stop());
                        }
                        if flow.is_break() {
                            break 'stream_loop;
                        }
                    }
                }
                Err(e) => {
                    // A late transport failure cannot invalidate an already emitted
                    // terminal response or append a second terminal event.
                    if state.terminated {
                        break;
                    }
                    log::error!("Responses stream error: {e}");
                    let error_event = json!({
                        "type": "error",
                        "error": {
                            "type": "stream_error",
                            "message": format!("Stream error: {e}")
                        }
                    });
                    let sse = format!("event: error\ndata: {}\n\n",
                        serde_json::to_string(&error_event).unwrap_or_default());
                    yield Ok(Bytes::from(sse));
                    state.terminated = true;
                    break;
                }
            }
        }

        if !state.terminated {
            state.finish_unterminated(&mut out);
            for bytes in out.drain(..) {
                yield Ok(bytes);
            }
        }
    }
}

#[cfg(test)]
mod tests;