Skip to main content

rig_core/providers/anthropic/streaming/
document.rs

1//! The Messages reassembler: a streamed reply rebuilt into the `Message` a
2//! unary reply is, so one reading of `raw` serves both.
3
4use std::collections::{BTreeMap, BTreeSet};
5
6use serde_json::{Map, Value, json};
7
8use super::MessagesEvent;
9use crate::wire::document::{Reassemble, keep, slot};
10
11/// Rebuilds the `Message` a streamed Messages reply adds up to.
12///
13/// `message_start.message` is the skeleton. Each `content_block_start`
14/// appends its block to `content`, whatever its wire index says. Text,
15/// thinking and signature deltas append; `input_json_delta` fragments are
16/// parsed into the block's `input` at `content_block_stop` (`{}` when none
17/// streamed); a `citations_delta` appends to `citations`; any other delta
18/// merges into its block as the decoder merges it. `message_delta` folds
19/// its fields and its cumulative `usage` over the skeleton. A whole
20/// `message` frame is the document. A reply that failed or was cut short
21/// yields the message so far, and one that stated none yields `Null`.
22#[derive(Debug, Default)]
23pub struct Message {
24    /// The message so far; `None` until a frame states part of it.
25    message: Option<Map<String, Value>>,
26    /// The input JSON streamed to each block not yet stopped.
27    inputs: BTreeMap<usize, String>,
28    closed: BTreeSet<usize>,
29    starts: BTreeMap<usize, Vec<Map<String, Value>>>,
30    positions: BTreeMap<usize, usize>,
31    reconnecting: bool,
32    replayed: BTreeSet<usize>,
33    message_started: bool,
34}
35
36impl Message {
37    fn message(&mut self) -> &mut Map<String, Value> {
38        self.message.get_or_insert_with(Map::new)
39    }
40
41    /// The block at `index` in `content`, when one was placed there.
42    fn block(&mut self, index: usize) -> Option<&mut Map<String, Value>> {
43        let index = self.positions.get(&index).copied()?;
44        self.message
45            .as_mut()?
46            .get_mut("content")?
47            .as_array_mut()?
48            .get_mut(index)?
49            .as_object_mut()
50    }
51
52    /// Append a new block to `content`, mapping its wire index to where it
53    /// went for subsequent deltas. The wire index never sizes `content`.
54    fn place(&mut self, index: usize, block: Map<String, Value>) {
55        if let Value::Array(content) = slot(self.message(), "content", Value::Array) {
56            let position = content.len();
57            content.push(Value::Object(block));
58            self.positions.insert(index, position);
59        }
60    }
61
62    fn delta(&mut self, index: usize, delta: &Map<String, Value>) {
63        let kind = delta
64            .get("type")
65            .and_then(Value::as_str)
66            .unwrap_or_default();
67        // A gateway that skips `content_block_start` still streams the
68        // block's text, so the first delta opens it, as the decoder does.
69        if self.block(index).is_none() {
70            let opened = match kind {
71                "text_delta" => json!({"type": "text", "text": ""}),
72                "thinking_delta" => json!({"type": "thinking", "thinking": ""}),
73                _ => Value::Null,
74            };
75            if let Value::Object(block) = opened {
76                self.place(index, block);
77            }
78        }
79        if kind == "input_json_delta" {
80            if let Some(fragment) = delta.get("partial_json").and_then(Value::as_str) {
81                self.inputs.entry(index).or_default().push_str(fragment);
82            }
83            return;
84        }
85        let Some(block) = self.block(index) else {
86            return;
87        };
88        if kind == "citations_delta" {
89            let citation = delta.get("citation").cloned().unwrap_or_default();
90            match block.get_mut("citations") {
91                Some(Value::Array(citations)) => citations.push(citation),
92                _ => {
93                    block.insert("citations".to_owned(), Value::Array(vec![citation]));
94                }
95            }
96            return;
97        }
98        let mut item = Value::Object(std::mem::take(block));
99        crate::operation::completion::merge(&mut item, delta);
100        if let Value::Object(item) = item {
101            *block = item;
102        }
103    }
104
105    /// Set the `input` of the block at `index` from the JSON streamed to
106    /// it. Input that does not parse leaves the input the block opened
107    /// with.
108    fn settle(&mut self, index: usize) {
109        let Some(json) = self.inputs.remove(&index) else {
110            return;
111        };
112        let Ok(input) = crate::json_utils::parse_tool_arguments(&json) else {
113            return;
114        };
115        if let Some(block) = self.block(index) {
116            block.insert("input".to_owned(), input);
117        }
118    }
119
120    fn message_delta(&mut self, event: &MessagesEvent) {
121        if let Some(Value::Object(delta)) = event.fields.get("delta") {
122            let message = self.message();
123            for (key, value) in delta {
124                keep(message, key, value);
125            }
126        }
127        if let Some(Value::Object(terminal)) = event.fields.get("usage") {
128            let usage = self
129                .message()
130                .entry("usage")
131                .or_insert_with(|| Value::Object(Map::new()));
132            if !usage.is_object() {
133                *usage = Value::Object(Map::new());
134            }
135            if let Value::Object(usage) = usage {
136                // Zero input is how some gateways leave the count out of
137                // `message_delta`, as the decoder reads it.
138                let started = usage
139                    .get("input_tokens")
140                    .and_then(Value::as_u64)
141                    .is_some_and(|tokens| tokens > 0);
142                for (key, value) in terminal {
143                    if key == "input_tokens" && started && value.as_u64() == Some(0) {
144                        continue;
145                    }
146                    keep(usage, key, value);
147                }
148            }
149        }
150    }
151}
152
153impl crate::wire::document::Serves<crate::operation::Completion> for Message {}
154
155impl Reassemble<MessagesEvent> for Message {
156    fn absorb(&mut self, event: &MessagesEvent) {
157        match event.kind() {
158            "message" => {
159                if let Value::Object(message) = &event.fields {
160                    self.message = Some(message.clone());
161                    self.inputs.clear();
162                }
163            }
164            "message_start" => {
165                if self.message_started && event.fields.get("message").is_some_and(Value::is_object)
166                {
167                    self.reconnecting = true;
168                }
169                if !self.message_started
170                    && let Some(Value::Object(start)) = event.fields.get("message")
171                {
172                    self.message_started = true;
173                    let message = self.message();
174                    for (key, value) in start {
175                        keep(message, key, value);
176                    }
177                }
178            }
179            "content_block_start" => {
180                if let (Ok(index), Ok(block)) = (event.index(), event.item("content_block")) {
181                    if self.closed.contains(&index) {
182                        if self.starts.get(&index).is_some_and(|previous| {
183                            super::replayed_start(previous, block, self.reconnecting)
184                        }) {
185                            self.replayed.insert(index);
186                            return;
187                        }
188                        self.closed.remove(&index);
189                        self.replayed.remove(&index);
190                        self.reconnecting = false;
191                        self.starts.entry(index).or_default().push(block.clone());
192                        self.place(index, block.clone());
193                    } else if self.block(index).is_none() {
194                        self.reconnecting = false;
195                        self.starts.entry(index).or_default().push(block.clone());
196                        self.place(index, block.clone());
197                    }
198                }
199            }
200            "content_block_delta" => {
201                if let (Ok(index), Ok(delta)) = (event.index(), event.item("delta"))
202                    && !self.replayed.contains(&index)
203                {
204                    self.delta(index, delta);
205                }
206            }
207            "content_block_stop" => {
208                if let Ok(index) = event.index()
209                    && !self.replayed.remove(&index)
210                {
211                    self.settle(index);
212                    self.closed.insert(index);
213                }
214            }
215            "message_delta" => self.message_delta(event),
216            // `message_stop`, `ping` and `error` add nothing: a failed
217            // reply keeps the message so far.
218            _ => {}
219        }
220    }
221
222    fn finish(mut self) -> Value {
223        let open: Vec<usize> = self.inputs.keys().copied().collect();
224        for index in open {
225            self.settle(index);
226        }
227        self.message.map_or(Value::Null, Value::Object)
228    }
229}
230
231#[cfg(test)]
232mod tests;