Skip to main content

graphrt_llm/
anthropic.rs

1//! Anthropic Messages API provider (streaming, tool use, extended thinking).
2//!
3//! Extended thinking is enabled through [`ChatRequest::extra`], e.g.
4//! `{"thinking": {"type": "enabled", "budget_tokens": 2048}}`. Signed thinking
5//! blocks are returned as provider state and replayed on the next turn, which
6//! the API requires when thinking is combined with tool use.
7
8use std::collections::HashMap;
9use std::fmt;
10use std::sync::Arc;
11use std::time::Duration;
12
13use futures::future::BoxFuture;
14use graphrt_core::event::Usage;
15use serde_json::{Map, Value as Json, json};
16
17use crate::credentials::{Credential, CredentialSource};
18use crate::error::LlmError;
19use crate::http::{Decoded, SseDecoder, send_sse};
20use crate::sse::SseEvent;
21use crate::types::{
22    ChatDelta, ChatModel, ChatRequest, ChatStream, ContentPart, FinishReason, Message, ResponseFormat, Role,
23    ToolChoice, check_extra,
24};
25
26/// API version header value.
27pub const ANTHROPIC_VERSION: &str = "2023-06-01";
28/// `max_tokens` used when the request does not set one (the API requires it).
29pub const DEFAULT_MAX_TOKENS: u32 = 4096;
30
31/// Configuration of the Anthropic endpoint.
32#[derive(Clone)]
33pub struct AnthropicConfig {
34    /// Base URL without `/v1/messages`.
35    pub base_url: String,
36    pub api_key: Option<String>,
37    /// Extra headers (e.g. `anthropic-beta`).
38    pub headers: Vec<(String, String)>,
39    pub connect_timeout: Duration,
40    /// Per-request credential source. When set it replaces `api_key`; its headers are
41    /// appended and its `base_url`, if any, overrides `base_url`. A 401/403 response
42    /// invalidates the credential and the request is retried once.
43    pub credentials: Option<Arc<dyn CredentialSource>>,
44    /// Provider name passed to the credential source (default `anthropic`).
45    pub credential_name: String,
46}
47
48impl fmt::Debug for AnthropicConfig {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        f.debug_struct("AnthropicConfig")
51            .field("base_url", &self.base_url)
52            .field("api_key", &self.api_key.as_ref().map(|_| "<redacted>"))
53            .field("headers", &self.headers.iter().map(|(k, _)| k).collect::<Vec<_>>())
54            .field("credentials", &self.credentials.as_ref().map(|_| "<source>"))
55            .field("credential_name", &self.credential_name)
56            .finish_non_exhaustive()
57    }
58}
59
60impl Default for AnthropicConfig {
61    fn default() -> Self {
62        Self {
63            base_url: "https://api.anthropic.com".to_owned(),
64            api_key: None,
65            headers: Vec::new(),
66            connect_timeout: Duration::from_secs(10),
67            credentials: None,
68            credential_name: "anthropic".to_owned(),
69        }
70    }
71}
72
73impl AnthropicConfig {
74    pub fn new(api_key: impl Into<String>) -> Self {
75        Self { api_key: Some(api_key.into()), ..Self::default() }
76    }
77
78    /// Ask `source` for a credential before every request (see [`crate::credentials`]).
79    pub fn with_credentials(mut self, source: Arc<dyn CredentialSource>) -> Self {
80        self.credentials = Some(source);
81        self
82    }
83
84    /// Provider name passed to the credential source.
85    pub fn credential_name(mut self, name: impl Into<String>) -> Self {
86        self.credential_name = name.into();
87        self
88    }
89}
90
91/// Anthropic chat model endpoint.
92#[derive(Clone)]
93pub struct AnthropicModel {
94    config: AnthropicConfig,
95    client: crate::http::LazyClient,
96}
97
98impl fmt::Debug for AnthropicModel {
99    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100        f.debug_struct("AnthropicModel").field("config", &self.config).finish_non_exhaustive()
101    }
102}
103
104impl AnthropicModel {
105    pub fn new(config: AnthropicConfig) -> Result<Self, LlmError> {
106        let client = crate::http::LazyClient::new(config.connect_timeout);
107        Ok(Self { config, client })
108    }
109}
110
111impl AnthropicModel {
112    fn request(&self, body: &Json, credential: Option<&Credential>) -> Result<reqwest::RequestBuilder, LlmError> {
113        let endpoint =
114            crate::http::resolve_endpoint(&self.config.base_url, credential.and_then(|c| c.base_url.as_deref()))?;
115        let url = format!("{}/v1/messages", endpoint.base_url.trim_end_matches('/'));
116        let mut builder = self.client.get()?.post(url).json(body).header("anthropic-version", ANTHROPIC_VERSION);
117        let key = credential.map(|c| c.secret.as_str()).or(self.config.api_key.as_deref());
118        if let Some(key) = key {
119            builder = builder.header("x-api-key", key);
120        }
121        if endpoint.send_static {
122            for (k, v) in &self.config.headers {
123                builder = builder.header(k.as_str(), v.as_str());
124            }
125        }
126        if let Some(credential) = credential {
127            for (k, v) in &credential.headers {
128                builder = builder.header(k.as_str(), v.as_str());
129            }
130        }
131        Ok(builder)
132    }
133}
134
135impl ChatModel for AnthropicModel {
136    fn chat(&self, request: ChatRequest) -> BoxFuture<'_, Result<ChatStream, LlmError>> {
137        Box::pin(async move {
138            let body = build_body(&request)?;
139            let name = self.config.credential_name.as_str();
140            let mut retried = false;
141            loop {
142                let credential = match &self.config.credentials {
143                    Some(source) => Some(source.credential(name).await?),
144                    None => None,
145                };
146                let result = send_sse(self.request(&body, credential.as_ref())?, AnthropicDecoder::default()).await;
147                match (result, &self.config.credentials) {
148                    // The credential was rejected: drop it and retry once with a fresh one.
149                    (Err(LlmError::Auth(_)), Some(source)) if !retried => {
150                        source.invalidate(name);
151                        retried = true;
152                    }
153                    (result, _) => return result,
154                }
155            }
156        })
157    }
158}
159
160/// Convert an image URL into an Anthropic image source (`url` or inline `base64`).
161fn image_source(url: &str) -> Json {
162    if let Some(rest) = url.strip_prefix("data:") {
163        if let Some((meta, data)) = rest.split_once(',') {
164            if let Some(media_type) = meta.strip_suffix(";base64") {
165                return json!({"type": "base64", "media_type": media_type, "data": data});
166            }
167        }
168    }
169    json!({"type": "url", "url": url})
170}
171
172fn content_blocks(parts: &[ContentPart]) -> Vec<Json> {
173    parts
174        .iter()
175        .filter_map(|p| match p {
176            ContentPart::Text { text } if text.is_empty() => None,
177            ContentPart::Text { text } => Some(json!({"type": "text", "text": text})),
178            ContentPart::Image { url, .. } => Some(json!({"type": "image", "source": image_source(url)})),
179        })
180        .collect()
181}
182
183/// `tool_result.content`: the text, or text and image blocks when the tool
184/// returned images (Anthropic accepts images inside tool results).
185fn tool_result_content(m: &Message) -> Json {
186    if m.images().next().is_none() {
187        return Json::String(m.text());
188    }
189    Json::Array(content_blocks(&m.content))
190}
191
192/// Build the JSON request body.
193pub(crate) fn build_body(req: &ChatRequest) -> Result<Json, LlmError> {
194    check_extra(req)?;
195    let mut system_parts: Vec<String> = Vec::new();
196    // (role, blocks); consecutive messages with the same role are merged, as the API requires alternation.
197    let mut turns: Vec<(&'static str, Vec<Json>)> = Vec::new();
198    let mut push = |role: &'static str, blocks: Vec<Json>| {
199        if blocks.is_empty() {
200            return;
201        }
202        match turns.last_mut() {
203            Some((last_role, last_blocks)) if *last_role == role => last_blocks.extend(blocks),
204            _ => turns.push((role, blocks)),
205        }
206    };
207    for m in &req.messages {
208        match m.role {
209            Role::System => system_parts.push(m.text()),
210            Role::User => push("user", content_blocks(&m.content)),
211            Role::Tool => {
212                let id = m
213                    .tool_call_id
214                    .as_deref()
215                    .filter(|id| !id.is_empty())
216                    .ok_or_else(|| LlmError::Config("tool message without tool_call_id".into()))?;
217                push(
218                    "user",
219                    vec![json!({"type": "tool_result", "tool_use_id": id, "content": tool_result_content(m)})],
220                );
221            }
222            Role::Assistant => {
223                let mut blocks = Vec::new();
224                if let Some(Json::Array(state)) = &m.provider_state {
225                    blocks.extend(state.iter().cloned());
226                }
227                blocks.extend(content_blocks(&m.content));
228                for c in &m.tool_calls {
229                    // The API needs the input as an object; a malformed call must not be replayed as `{}`.
230                    let input = c.arguments_json().map_err(|e| LlmError::Config(e.to_string()))?;
231                    blocks.push(json!({"type": "tool_use", "id": c.id, "name": c.name, "input": input}));
232                }
233                push("assistant", blocks);
234            }
235        }
236    }
237
238    match &req.response_format {
239        Some(ResponseFormat::JsonObject) => {
240            system_parts.push("Respond with a single valid JSON object and nothing else.".to_owned());
241        }
242        Some(ResponseFormat::JsonSchema { schema, .. }) => system_parts.push(format!(
243            "Respond with a single valid JSON value matching this JSON Schema and nothing else:\n{schema}"
244        )),
245        None | Some(ResponseFormat::Text) => {}
246    }
247
248    let mut body = Map::new();
249    body.insert("model".into(), json!(req.model));
250    body.insert("max_tokens".into(), json!(req.max_tokens.unwrap_or(DEFAULT_MAX_TOKENS)));
251    body.insert("stream".into(), json!(true));
252    let system: Vec<&String> = system_parts.iter().filter(|s| !s.is_empty()).collect();
253    if !system.is_empty() {
254        let joined: Vec<&str> = system.iter().map(|s| s.as_str()).collect();
255        body.insert("system".into(), json!(joined.join("\n\n")));
256    }
257    let messages: Vec<Json> =
258        turns.into_iter().map(|(role, blocks)| json!({"role": role, "content": blocks})).collect();
259    body.insert("messages".into(), Json::Array(messages));
260    if !req.tools.is_empty() {
261        let tools: Vec<Json> = req
262            .tools
263            .iter()
264            .map(|t| json!({"name": t.name, "description": t.description, "input_schema": t.parameters}))
265            .collect();
266        body.insert("tools".into(), Json::Array(tools));
267    }
268    if let Some(choice) = &req.tool_choice {
269        let v = match choice {
270            ToolChoice::Auto => json!({"type": "auto"}),
271            ToolChoice::None => json!({"type": "none"}),
272            ToolChoice::Required => json!({"type": "any"}),
273            ToolChoice::Tool(name) => json!({"type": "tool", "name": name}),
274        };
275        body.insert("tool_choice".into(), v);
276    }
277    if let Some(t) = req.temperature {
278        body.insert("temperature".into(), json!(t));
279    }
280    if let Some(p) = req.top_p {
281        body.insert("top_p".into(), json!(p));
282    }
283    if !req.stop.is_empty() {
284        body.insert("stop_sequences".into(), json!(req.stop));
285    }
286    for (k, v) in &req.extra {
287        body.insert(k.clone(), v.clone());
288    }
289    Ok(Json::Object(body))
290}
291
292/// Kind of an open content block.
293#[derive(Debug)]
294enum Block {
295    Text,
296    ToolUse { call_index: usize },
297    Thinking { thinking: String, signature: String },
298    RedactedThinking { data: String },
299    Other,
300}
301
302/// Decodes Messages API stream events.
303#[derive(Debug, Default)]
304pub(crate) struct AnthropicDecoder {
305    blocks: HashMap<u64, Block>,
306    tool_calls: usize,
307    usage: Usage,
308    /// Completed thinking blocks, replayed as provider state.
309    thinking: Vec<Json>,
310}
311
312fn unknown_block(delta_type: &str, index: u64) -> LlmError {
313    LlmError::Decode(format!("`{delta_type}` for content block {index}, which is not an open block of that kind"))
314}
315
316fn stop_reason(s: &str) -> FinishReason {
317    match s {
318        "end_turn" | "stop_sequence" => FinishReason::Stop,
319        "max_tokens" => FinishReason::Length,
320        "tool_use" => FinishReason::ToolCalls,
321        "refusal" => FinishReason::ContentFilter,
322        other => FinishReason::Other(other.to_owned()),
323    }
324}
325
326impl AnthropicDecoder {
327    fn usage_delta(&mut self, usage: &Json) -> ChatDelta {
328        if let Some(n) = usage.get("input_tokens").and_then(Json::as_u64) {
329            self.usage.prompt_tokens = n;
330        }
331        if let Some(n) = usage.get("output_tokens").and_then(Json::as_u64) {
332            self.usage.completion_tokens = n;
333        }
334        self.usage.total_tokens = self.usage.prompt_tokens.saturating_add(self.usage.completion_tokens);
335        ChatDelta::Usage(self.usage)
336    }
337}
338
339impl SseDecoder for AnthropicDecoder {
340    fn decode(&mut self, event: SseEvent) -> Result<Decoded, LlmError> {
341        if event.data.trim().is_empty() {
342            return Ok(Decoded::default());
343        }
344        let v: Json =
345            serde_json::from_str(&event.data).map_err(|e| LlmError::Decode(format!("{e}: {}", event.data)))?;
346        let kind = v.get("type").and_then(Json::as_str).unwrap_or_default();
347        let index = v.get("index").and_then(Json::as_u64).unwrap_or(0);
348        let mut out = Vec::new();
349        match kind {
350            "message_start" => {
351                if let Some(u) = v.get("message").and_then(|m| m.get("usage")) {
352                    out.push(self.usage_delta(u));
353                }
354            }
355            "content_block_start" => {
356                let block = v.get("content_block").cloned().unwrap_or(Json::Null);
357                let block_type = block.get("type").and_then(Json::as_str).unwrap_or_default();
358                let state = match block_type {
359                    "text" => {
360                        if let Some(t) = block.get("text").and_then(Json::as_str).filter(|t| !t.is_empty()) {
361                            out.push(ChatDelta::Text(t.to_owned()));
362                        }
363                        Block::Text
364                    }
365                    "tool_use" => {
366                        let call_index = self.tool_calls;
367                        self.tool_calls += 1;
368                        // Some servers send the full input up front; it is normally `{}` followed by deltas.
369                        let initial = block
370                            .get("input")
371                            .filter(|i| i.as_object().is_some_and(|o| !o.is_empty()))
372                            .map(Json::to_string)
373                            .unwrap_or_default();
374                        out.push(ChatDelta::ToolCall {
375                            index: call_index,
376                            id: block.get("id").and_then(Json::as_str).map(str::to_owned),
377                            name: block.get("name").and_then(Json::as_str).map(str::to_owned),
378                            arguments: initial,
379                        });
380                        Block::ToolUse { call_index }
381                    }
382                    "thinking" => Block::Thinking {
383                        thinking: block.get("thinking").and_then(Json::as_str).unwrap_or_default().to_owned(),
384                        signature: block.get("signature").and_then(Json::as_str).unwrap_or_default().to_owned(),
385                    },
386                    "redacted_thinking" => Block::RedactedThinking {
387                        data: block.get("data").and_then(Json::as_str).unwrap_or_default().to_owned(),
388                    },
389                    _ => Block::Other,
390                };
391                self.blocks.insert(index, state);
392            }
393            "content_block_delta" => {
394                let delta = v.get("delta").cloned().unwrap_or(Json::Null);
395                let delta_type = delta.get("type").and_then(Json::as_str).unwrap_or_default();
396                let field = |name: &str| delta.get(name).and_then(Json::as_str).unwrap_or_default().to_owned();
397                match (delta_type, self.blocks.get_mut(&index)) {
398                    ("text_delta", _) => out.push(ChatDelta::Text(field("text"))),
399                    ("input_json_delta", Some(Block::ToolUse { call_index })) => out.push(ChatDelta::ToolCall {
400                        index: *call_index,
401                        id: None,
402                        name: None,
403                        arguments: field("partial_json"),
404                    }),
405                    ("input_json_delta", _) => return Err(unknown_block(delta_type, index)),
406                    ("thinking_delta", Some(Block::Thinking { thinking, .. })) => {
407                        let t = field("thinking");
408                        thinking.push_str(&t);
409                        out.push(ChatDelta::Reasoning(t));
410                    }
411                    ("thinking_delta", _) => out.push(ChatDelta::Reasoning(field("thinking"))),
412                    ("signature_delta", Some(Block::Thinking { signature, .. })) => {
413                        signature.push_str(&field("signature"))
414                    }
415                    // A lost signature would make the next turn fail; report it now.
416                    ("signature_delta", _) => return Err(unknown_block(delta_type, index)),
417                    _ => {} // citations and future delta types carry nothing we use
418                }
419            }
420            "content_block_stop" => match self.blocks.remove(&index) {
421                Some(Block::Thinking { thinking, signature }) => {
422                    self.thinking.push(json!({"type": "thinking", "thinking": thinking, "signature": signature}));
423                }
424                Some(Block::RedactedThinking { data }) => {
425                    self.thinking.push(json!({"type": "redacted_thinking", "data": data}));
426                }
427                _ => {}
428            },
429            "message_delta" => {
430                if let Some(reason) = v.get("delta").and_then(|d| d.get("stop_reason")).and_then(Json::as_str) {
431                    out.push(ChatDelta::Finish(stop_reason(reason)));
432                }
433                if let Some(u) = v.get("usage") {
434                    out.push(self.usage_delta(u));
435                }
436            }
437            "message_stop" => {
438                if !self.thinking.is_empty() {
439                    out.push(ChatDelta::ProviderState(Json::Array(std::mem::take(&mut self.thinking))));
440                }
441                return Ok(Decoded::done(out));
442            }
443            "error" => {
444                let err = v.get("error").cloned().unwrap_or(Json::Null);
445                let message = err.get("message").and_then(Json::as_str).unwrap_or("stream error").to_owned();
446                let err_type = err.get("type").and_then(Json::as_str).unwrap_or_default();
447                return Err(match err_type {
448                    "overloaded_error" | "api_error" => LlmError::Server { status: 529, message },
449                    "rate_limit_error" => LlmError::RateLimit { message, retry_after_ms: None },
450                    "authentication_error" | "permission_error" => LlmError::Auth(message),
451                    _ => LlmError::BadRequest(message),
452                });
453            }
454            _ => {} // ping and future event types
455        }
456        Ok(Decoded::deltas(out))
457    }
458}
459
460#[cfg(test)]
461mod tests {
462    use super::*;
463    use crate::sse::SseParser;
464    use crate::types::{ToolCall, collect};
465    use graphrt_core::tool::ToolSpec;
466
467    #[test]
468    fn body_shape() {
469        let mut assistant = Message::assistant("let me check");
470        assistant.tool_calls = vec![ToolCall { id: "tu1".into(), name: "get".into(), arguments: "{\"a\":1}".into() }];
471        assistant.provider_state = Some(json!([{"type": "thinking", "thinking": "t", "signature": "s"}]));
472        let mut req = ChatRequest::new(
473            "claude-sonnet",
474            vec![
475                Message::system("be brief"),
476                Message::user("hi"),
477                Message {
478                    role: Role::User,
479                    content: vec![ContentPart::Image { url: "data:image/png;base64,AAAA".into(), detail: None }],
480                    tool_calls: vec![],
481                    tool_call_id: None,
482                    provider_state: None,
483                },
484                assistant,
485                Message::tool("tu1", "42"),
486                Message::tool("tu2", "43"),
487            ],
488        );
489        req.tools =
490            vec![ToolSpec { name: "get".into(), description: "d".into(), parameters: json!({"type": "object"}) }];
491        req.tool_choice = Some(ToolChoice::Required);
492        req.stop = vec!["END".into()];
493        req.response_format = Some(ResponseFormat::JsonObject);
494        let body = build_body(&req).unwrap();
495        assert_eq!(body["max_tokens"], DEFAULT_MAX_TOKENS);
496        assert!(body["system"].as_str().unwrap().starts_with("be brief"));
497        let msgs = body["messages"].as_array().unwrap();
498        assert_eq!(msgs.len(), 3, "consecutive user turns and tool results are merged");
499        assert_eq!(msgs[0]["content"][1]["source"]["type"], "base64");
500        assert_eq!(msgs[0]["content"][1]["source"]["media_type"], "image/png");
501        assert_eq!(msgs[1]["content"][0]["type"], "thinking");
502        assert_eq!(msgs[1]["content"][2]["input"]["a"], 1);
503        assert_eq!(msgs[2]["content"][1]["tool_use_id"], "tu2");
504        assert_eq!(body["tools"][0]["input_schema"]["type"], "object");
505        assert_eq!(body["tool_choice"]["type"], "any");
506        assert_eq!(body["stop_sequences"][0], "END");
507    }
508
509    #[test]
510    fn tool_images_are_sent_inside_the_tool_result() {
511        let mut assistant = Message::assistant("");
512        assistant.tool_calls = vec![ToolCall { id: "tu1".into(), name: "screenshot".into(), arguments: "{}".into() }];
513        let req = ChatRequest::new(
514            "claude-sonnet",
515            vec![
516                Message::user("look"),
517                assistant,
518                Message::tool_with_images(
519                    "tu1",
520                    "1280x800",
521                    ["data:image/png;base64,AAAA".to_owned(), "https://x/a.jpg".to_owned()],
522                ),
523            ],
524        );
525        let body = build_body(&req).unwrap();
526        assert_eq!(
527            body["messages"][2],
528            json!({"role": "user", "content": [{"type": "tool_result", "tool_use_id": "tu1", "content": [
529                {"type": "text", "text": "1280x800"},
530                {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "AAAA"}},
531                {"type": "image", "source": {"type": "url", "url": "https://x/a.jpg"}}
532            ]}]})
533        );
534        // Without images the content stays a plain string.
535        let req = ChatRequest::new("m", vec![Message::user("x"), req.messages[1].clone(), Message::tool("tu1", "ok")]);
536        assert_eq!(build_body(&req).unwrap()["messages"][2]["content"][0]["content"], "ok");
537    }
538
539    const FIXTURE: &str = concat!(
540        "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"usage\":{\"input_tokens\":10,\"output_tokens\":1}}}\n\n",
541        "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"thinking\",\"thinking\":\"\"}}\n\n",
542        "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"thinking_delta\",\"thinking\":\"hmm\"}}\n\n",
543        "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"signature_delta\",\"signature\":\"sig\"}}\n\n",
544        "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n",
545        "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n",
546        "event: ping\ndata: {\"type\":\"ping\"}\n\n",
547        "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"text_delta\",\"text\":\"Hi\"}}\n\n",
548        "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":1}\n\n",
549        "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":2,\"content_block\":{\"type\":\"tool_use\",\"id\":\"tu_1\",\"name\":\"get\",\"input\":{}}}\n\n",
550        "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":2,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"a\\\":\"}}\n\n",
551        "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":2,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"2}\"}}\n\n",
552        "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":2}\n\n",
553        "event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\"},\"usage\":{\"output_tokens\":20}}\n\n",
554        "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n",
555    );
556
557    #[tokio::test]
558    async fn decode_recorded_stream() {
559        let (deltas, done) = decode_all(FIXTURE);
560        assert!(done);
561        let deltas: Vec<Result<ChatDelta, LlmError>> = deltas.into_iter().map(Ok).collect();
562        let r = collect(Box::pin(futures::stream::iter(deltas)), |_| {}).await.unwrap();
563        assert_eq!(r.text, "Hi");
564        assert_eq!(r.reasoning, "hmm");
565        assert_eq!(
566            r.tool_calls,
567            vec![ToolCall { id: "tu_1".into(), name: "get".into(), arguments: "{\"a\":2}".into() }]
568        );
569        assert_eq!(r.usage, Usage { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 });
570        assert_eq!(r.finish_reason, Some(FinishReason::ToolCalls));
571        assert_eq!(r.provider_state, Some(json!([{"type": "thinking", "thinking": "hmm", "signature": "sig"}])));
572        // The provider state is replayed into the next request.
573        let body = build_body(&ChatRequest::new("m", vec![r.to_message()])).unwrap();
574        assert_eq!(body["messages"][0]["content"][0]["signature"], "sig");
575    }
576
577    /// Decode a fixture; returns the deltas and whether `message_stop` was seen.
578    fn decode_all(fixture: &str) -> (Vec<ChatDelta>, bool) {
579        let mut parser = SseParser::new();
580        let mut decoder = AnthropicDecoder::default();
581        let mut deltas = Vec::new();
582        for ev in parser.push(fixture.as_bytes()).unwrap() {
583            let d = decoder.decode(ev).unwrap();
584            deltas.extend(d.deltas);
585            if d.done {
586                return (deltas, true);
587            }
588        }
589        (deltas, false)
590    }
591
592    #[test]
593    fn stream_without_message_stop_is_truncated() {
594        let cut = FIXTURE.split("event: message_delta").next().unwrap();
595        let (_, done) = decode_all(cut);
596        assert!(!done);
597        let mut decoder = AnthropicDecoder::default();
598        let err = decoder.end_of_stream().unwrap_err();
599        assert!(matches!(err, LlmError::Network(_)) && err.is_retryable());
600    }
601
602    #[test]
603    fn deltas_for_unknown_blocks_are_errors() {
604        let mut d = AnthropicDecoder::default();
605        let ev = SseEvent {
606            data: "{\"type\":\"content_block_delta\",\"index\":4,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{}\"}}".into(),
607            ..SseEvent::default()
608        };
609        assert!(matches!(d.decode(ev), Err(LlmError::Decode(_))));
610    }
611
612    #[test]
613    fn malformed_tool_arguments_are_not_replayed_as_empty_input() {
614        let mut assistant = Message::assistant("");
615        assistant.tool_calls = vec![ToolCall { id: "t".into(), name: "get".into(), arguments: "{\"a\":".into() }];
616        let req = ChatRequest::new("m", vec![Message::user("x"), assistant, Message::tool("t", "r")]);
617        assert!(matches!(build_body(&req), Err(LlmError::Config(_))));
618    }
619
620    #[test]
621    fn final_turn_keeps_tools_with_choice_none() {
622        let req = ChatRequest::new("m", vec![Message::user("x")])
623            .with_tools(vec![ToolSpec { name: "t".into(), description: "d".into(), parameters: json!({}) }])
624            .with_tool_choice(ToolChoice::None);
625        let body = build_body(&req).unwrap();
626        assert_eq!(body["tool_choice"]["type"], "none");
627        assert_eq!(body["tools"][0]["name"], "t");
628    }
629
630    #[test]
631    fn error_event() {
632        let mut d = AnthropicDecoder::default();
633        let ev = SseEvent {
634            data: "{\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"busy\"}}".into(),
635            ..SseEvent::default()
636        };
637        let err = d.decode(ev).unwrap_err();
638        assert!(err.is_retryable());
639    }
640}