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, ResponseFormat, Role, ToolChoice,
23    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/// Build the JSON request body.
184pub(crate) fn build_body(req: &ChatRequest) -> Result<Json, LlmError> {
185    check_extra(req)?;
186    let mut system_parts: Vec<String> = Vec::new();
187    // (role, blocks); consecutive messages with the same role are merged, as the API requires alternation.
188    let mut turns: Vec<(&'static str, Vec<Json>)> = Vec::new();
189    let mut push = |role: &'static str, blocks: Vec<Json>| {
190        if blocks.is_empty() {
191            return;
192        }
193        match turns.last_mut() {
194            Some((last_role, last_blocks)) if *last_role == role => last_blocks.extend(blocks),
195            _ => turns.push((role, blocks)),
196        }
197    };
198    for m in &req.messages {
199        match m.role {
200            Role::System => system_parts.push(m.text()),
201            Role::User => push("user", content_blocks(&m.content)),
202            Role::Tool => {
203                let id = m
204                    .tool_call_id
205                    .as_deref()
206                    .filter(|id| !id.is_empty())
207                    .ok_or_else(|| LlmError::Config("tool message without tool_call_id".into()))?;
208                push("user", vec![json!({"type": "tool_result", "tool_use_id": id, "content": m.text()})]);
209            }
210            Role::Assistant => {
211                let mut blocks = Vec::new();
212                if let Some(Json::Array(state)) = &m.provider_state {
213                    blocks.extend(state.iter().cloned());
214                }
215                blocks.extend(content_blocks(&m.content));
216                for c in &m.tool_calls {
217                    // The API needs the input as an object; a malformed call must not be replayed as `{}`.
218                    let input = c.arguments_json().map_err(|e| LlmError::Config(e.to_string()))?;
219                    blocks.push(json!({"type": "tool_use", "id": c.id, "name": c.name, "input": input}));
220                }
221                push("assistant", blocks);
222            }
223        }
224    }
225
226    match &req.response_format {
227        Some(ResponseFormat::JsonObject) => {
228            system_parts.push("Respond with a single valid JSON object and nothing else.".to_owned());
229        }
230        Some(ResponseFormat::JsonSchema { schema, .. }) => system_parts.push(format!(
231            "Respond with a single valid JSON value matching this JSON Schema and nothing else:\n{schema}"
232        )),
233        None | Some(ResponseFormat::Text) => {}
234    }
235
236    let mut body = Map::new();
237    body.insert("model".into(), json!(req.model));
238    body.insert("max_tokens".into(), json!(req.max_tokens.unwrap_or(DEFAULT_MAX_TOKENS)));
239    body.insert("stream".into(), json!(true));
240    let system: Vec<&String> = system_parts.iter().filter(|s| !s.is_empty()).collect();
241    if !system.is_empty() {
242        let joined: Vec<&str> = system.iter().map(|s| s.as_str()).collect();
243        body.insert("system".into(), json!(joined.join("\n\n")));
244    }
245    let messages: Vec<Json> =
246        turns.into_iter().map(|(role, blocks)| json!({"role": role, "content": blocks})).collect();
247    body.insert("messages".into(), Json::Array(messages));
248    if !req.tools.is_empty() {
249        let tools: Vec<Json> = req
250            .tools
251            .iter()
252            .map(|t| json!({"name": t.name, "description": t.description, "input_schema": t.parameters}))
253            .collect();
254        body.insert("tools".into(), Json::Array(tools));
255    }
256    if let Some(choice) = &req.tool_choice {
257        let v = match choice {
258            ToolChoice::Auto => json!({"type": "auto"}),
259            ToolChoice::None => json!({"type": "none"}),
260            ToolChoice::Required => json!({"type": "any"}),
261            ToolChoice::Tool(name) => json!({"type": "tool", "name": name}),
262        };
263        body.insert("tool_choice".into(), v);
264    }
265    if let Some(t) = req.temperature {
266        body.insert("temperature".into(), json!(t));
267    }
268    if let Some(p) = req.top_p {
269        body.insert("top_p".into(), json!(p));
270    }
271    if !req.stop.is_empty() {
272        body.insert("stop_sequences".into(), json!(req.stop));
273    }
274    for (k, v) in &req.extra {
275        body.insert(k.clone(), v.clone());
276    }
277    Ok(Json::Object(body))
278}
279
280/// Kind of an open content block.
281#[derive(Debug)]
282enum Block {
283    Text,
284    ToolUse { call_index: usize },
285    Thinking { thinking: String, signature: String },
286    RedactedThinking { data: String },
287    Other,
288}
289
290/// Decodes Messages API stream events.
291#[derive(Debug, Default)]
292pub(crate) struct AnthropicDecoder {
293    blocks: HashMap<u64, Block>,
294    tool_calls: usize,
295    usage: Usage,
296    /// Completed thinking blocks, replayed as provider state.
297    thinking: Vec<Json>,
298}
299
300fn unknown_block(delta_type: &str, index: u64) -> LlmError {
301    LlmError::Decode(format!("`{delta_type}` for content block {index}, which is not an open block of that kind"))
302}
303
304fn stop_reason(s: &str) -> FinishReason {
305    match s {
306        "end_turn" | "stop_sequence" => FinishReason::Stop,
307        "max_tokens" => FinishReason::Length,
308        "tool_use" => FinishReason::ToolCalls,
309        "refusal" => FinishReason::ContentFilter,
310        other => FinishReason::Other(other.to_owned()),
311    }
312}
313
314impl AnthropicDecoder {
315    fn usage_delta(&mut self, usage: &Json) -> ChatDelta {
316        if let Some(n) = usage.get("input_tokens").and_then(Json::as_u64) {
317            self.usage.prompt_tokens = n;
318        }
319        if let Some(n) = usage.get("output_tokens").and_then(Json::as_u64) {
320            self.usage.completion_tokens = n;
321        }
322        self.usage.total_tokens = self.usage.prompt_tokens.saturating_add(self.usage.completion_tokens);
323        ChatDelta::Usage(self.usage)
324    }
325}
326
327impl SseDecoder for AnthropicDecoder {
328    fn decode(&mut self, event: SseEvent) -> Result<Decoded, LlmError> {
329        if event.data.trim().is_empty() {
330            return Ok(Decoded::default());
331        }
332        let v: Json =
333            serde_json::from_str(&event.data).map_err(|e| LlmError::Decode(format!("{e}: {}", event.data)))?;
334        let kind = v.get("type").and_then(Json::as_str).unwrap_or_default();
335        let index = v.get("index").and_then(Json::as_u64).unwrap_or(0);
336        let mut out = Vec::new();
337        match kind {
338            "message_start" => {
339                if let Some(u) = v.get("message").and_then(|m| m.get("usage")) {
340                    out.push(self.usage_delta(u));
341                }
342            }
343            "content_block_start" => {
344                let block = v.get("content_block").cloned().unwrap_or(Json::Null);
345                let block_type = block.get("type").and_then(Json::as_str).unwrap_or_default();
346                let state = match block_type {
347                    "text" => {
348                        if let Some(t) = block.get("text").and_then(Json::as_str).filter(|t| !t.is_empty()) {
349                            out.push(ChatDelta::Text(t.to_owned()));
350                        }
351                        Block::Text
352                    }
353                    "tool_use" => {
354                        let call_index = self.tool_calls;
355                        self.tool_calls += 1;
356                        // Some servers send the full input up front; it is normally `{}` followed by deltas.
357                        let initial = block
358                            .get("input")
359                            .filter(|i| i.as_object().is_some_and(|o| !o.is_empty()))
360                            .map(Json::to_string)
361                            .unwrap_or_default();
362                        out.push(ChatDelta::ToolCall {
363                            index: call_index,
364                            id: block.get("id").and_then(Json::as_str).map(str::to_owned),
365                            name: block.get("name").and_then(Json::as_str).map(str::to_owned),
366                            arguments: initial,
367                        });
368                        Block::ToolUse { call_index }
369                    }
370                    "thinking" => Block::Thinking {
371                        thinking: block.get("thinking").and_then(Json::as_str).unwrap_or_default().to_owned(),
372                        signature: block.get("signature").and_then(Json::as_str).unwrap_or_default().to_owned(),
373                    },
374                    "redacted_thinking" => Block::RedactedThinking {
375                        data: block.get("data").and_then(Json::as_str).unwrap_or_default().to_owned(),
376                    },
377                    _ => Block::Other,
378                };
379                self.blocks.insert(index, state);
380            }
381            "content_block_delta" => {
382                let delta = v.get("delta").cloned().unwrap_or(Json::Null);
383                let delta_type = delta.get("type").and_then(Json::as_str).unwrap_or_default();
384                let field = |name: &str| delta.get(name).and_then(Json::as_str).unwrap_or_default().to_owned();
385                match (delta_type, self.blocks.get_mut(&index)) {
386                    ("text_delta", _) => out.push(ChatDelta::Text(field("text"))),
387                    ("input_json_delta", Some(Block::ToolUse { call_index })) => out.push(ChatDelta::ToolCall {
388                        index: *call_index,
389                        id: None,
390                        name: None,
391                        arguments: field("partial_json"),
392                    }),
393                    ("input_json_delta", _) => return Err(unknown_block(delta_type, index)),
394                    ("thinking_delta", Some(Block::Thinking { thinking, .. })) => {
395                        let t = field("thinking");
396                        thinking.push_str(&t);
397                        out.push(ChatDelta::Reasoning(t));
398                    }
399                    ("thinking_delta", _) => out.push(ChatDelta::Reasoning(field("thinking"))),
400                    ("signature_delta", Some(Block::Thinking { signature, .. })) => {
401                        signature.push_str(&field("signature"))
402                    }
403                    // A lost signature would make the next turn fail; report it now.
404                    ("signature_delta", _) => return Err(unknown_block(delta_type, index)),
405                    _ => {} // citations and future delta types carry nothing we use
406                }
407            }
408            "content_block_stop" => match self.blocks.remove(&index) {
409                Some(Block::Thinking { thinking, signature }) => {
410                    self.thinking.push(json!({"type": "thinking", "thinking": thinking, "signature": signature}));
411                }
412                Some(Block::RedactedThinking { data }) => {
413                    self.thinking.push(json!({"type": "redacted_thinking", "data": data}));
414                }
415                _ => {}
416            },
417            "message_delta" => {
418                if let Some(reason) = v.get("delta").and_then(|d| d.get("stop_reason")).and_then(Json::as_str) {
419                    out.push(ChatDelta::Finish(stop_reason(reason)));
420                }
421                if let Some(u) = v.get("usage") {
422                    out.push(self.usage_delta(u));
423                }
424            }
425            "message_stop" => {
426                if !self.thinking.is_empty() {
427                    out.push(ChatDelta::ProviderState(Json::Array(std::mem::take(&mut self.thinking))));
428                }
429                return Ok(Decoded::done(out));
430            }
431            "error" => {
432                let err = v.get("error").cloned().unwrap_or(Json::Null);
433                let message = err.get("message").and_then(Json::as_str).unwrap_or("stream error").to_owned();
434                let err_type = err.get("type").and_then(Json::as_str).unwrap_or_default();
435                return Err(match err_type {
436                    "overloaded_error" | "api_error" => LlmError::Server { status: 529, message },
437                    "rate_limit_error" => LlmError::RateLimit { message, retry_after_ms: None },
438                    "authentication_error" | "permission_error" => LlmError::Auth(message),
439                    _ => LlmError::BadRequest(message),
440                });
441            }
442            _ => {} // ping and future event types
443        }
444        Ok(Decoded::deltas(out))
445    }
446}
447
448#[cfg(test)]
449mod tests {
450    use super::*;
451    use crate::sse::SseParser;
452    use crate::types::{Message, ToolCall, collect};
453    use graphrt_core::tool::ToolSpec;
454
455    #[test]
456    fn body_shape() {
457        let mut assistant = Message::assistant("let me check");
458        assistant.tool_calls = vec![ToolCall { id: "tu1".into(), name: "get".into(), arguments: "{\"a\":1}".into() }];
459        assistant.provider_state = Some(json!([{"type": "thinking", "thinking": "t", "signature": "s"}]));
460        let mut req = ChatRequest::new(
461            "claude-sonnet",
462            vec![
463                Message::system("be brief"),
464                Message::user("hi"),
465                Message {
466                    role: Role::User,
467                    content: vec![ContentPart::Image { url: "data:image/png;base64,AAAA".into(), detail: None }],
468                    tool_calls: vec![],
469                    tool_call_id: None,
470                    provider_state: None,
471                },
472                assistant,
473                Message::tool("tu1", "42"),
474                Message::tool("tu2", "43"),
475            ],
476        );
477        req.tools =
478            vec![ToolSpec { name: "get".into(), description: "d".into(), parameters: json!({"type": "object"}) }];
479        req.tool_choice = Some(ToolChoice::Required);
480        req.stop = vec!["END".into()];
481        req.response_format = Some(ResponseFormat::JsonObject);
482        let body = build_body(&req).unwrap();
483        assert_eq!(body["max_tokens"], DEFAULT_MAX_TOKENS);
484        assert!(body["system"].as_str().unwrap().starts_with("be brief"));
485        let msgs = body["messages"].as_array().unwrap();
486        assert_eq!(msgs.len(), 3, "consecutive user turns and tool results are merged");
487        assert_eq!(msgs[0]["content"][1]["source"]["type"], "base64");
488        assert_eq!(msgs[0]["content"][1]["source"]["media_type"], "image/png");
489        assert_eq!(msgs[1]["content"][0]["type"], "thinking");
490        assert_eq!(msgs[1]["content"][2]["input"]["a"], 1);
491        assert_eq!(msgs[2]["content"][1]["tool_use_id"], "tu2");
492        assert_eq!(body["tools"][0]["input_schema"]["type"], "object");
493        assert_eq!(body["tool_choice"]["type"], "any");
494        assert_eq!(body["stop_sequences"][0], "END");
495    }
496
497    const FIXTURE: &str = concat!(
498        "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"usage\":{\"input_tokens\":10,\"output_tokens\":1}}}\n\n",
499        "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"thinking\",\"thinking\":\"\"}}\n\n",
500        "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"thinking_delta\",\"thinking\":\"hmm\"}}\n\n",
501        "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"signature_delta\",\"signature\":\"sig\"}}\n\n",
502        "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n",
503        "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n",
504        "event: ping\ndata: {\"type\":\"ping\"}\n\n",
505        "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"text_delta\",\"text\":\"Hi\"}}\n\n",
506        "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":1}\n\n",
507        "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":2,\"content_block\":{\"type\":\"tool_use\",\"id\":\"tu_1\",\"name\":\"get\",\"input\":{}}}\n\n",
508        "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":2,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"a\\\":\"}}\n\n",
509        "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":2,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"2}\"}}\n\n",
510        "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":2}\n\n",
511        "event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\"},\"usage\":{\"output_tokens\":20}}\n\n",
512        "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n",
513    );
514
515    #[tokio::test]
516    async fn decode_recorded_stream() {
517        let (deltas, done) = decode_all(FIXTURE);
518        assert!(done);
519        let deltas: Vec<Result<ChatDelta, LlmError>> = deltas.into_iter().map(Ok).collect();
520        let r = collect(Box::pin(futures::stream::iter(deltas)), |_| {}).await.unwrap();
521        assert_eq!(r.text, "Hi");
522        assert_eq!(r.reasoning, "hmm");
523        assert_eq!(
524            r.tool_calls,
525            vec![ToolCall { id: "tu_1".into(), name: "get".into(), arguments: "{\"a\":2}".into() }]
526        );
527        assert_eq!(r.usage, Usage { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 });
528        assert_eq!(r.finish_reason, Some(FinishReason::ToolCalls));
529        assert_eq!(r.provider_state, Some(json!([{"type": "thinking", "thinking": "hmm", "signature": "sig"}])));
530        // The provider state is replayed into the next request.
531        let body = build_body(&ChatRequest::new("m", vec![r.to_message()])).unwrap();
532        assert_eq!(body["messages"][0]["content"][0]["signature"], "sig");
533    }
534
535    /// Decode a fixture; returns the deltas and whether `message_stop` was seen.
536    fn decode_all(fixture: &str) -> (Vec<ChatDelta>, bool) {
537        let mut parser = SseParser::new();
538        let mut decoder = AnthropicDecoder::default();
539        let mut deltas = Vec::new();
540        for ev in parser.push(fixture.as_bytes()).unwrap() {
541            let d = decoder.decode(ev).unwrap();
542            deltas.extend(d.deltas);
543            if d.done {
544                return (deltas, true);
545            }
546        }
547        (deltas, false)
548    }
549
550    #[test]
551    fn stream_without_message_stop_is_truncated() {
552        let cut = FIXTURE.split("event: message_delta").next().unwrap();
553        let (_, done) = decode_all(cut);
554        assert!(!done);
555        let mut decoder = AnthropicDecoder::default();
556        let err = decoder.end_of_stream().unwrap_err();
557        assert!(matches!(err, LlmError::Network(_)) && err.is_retryable());
558    }
559
560    #[test]
561    fn deltas_for_unknown_blocks_are_errors() {
562        let mut d = AnthropicDecoder::default();
563        let ev = SseEvent {
564            data: "{\"type\":\"content_block_delta\",\"index\":4,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{}\"}}".into(),
565            ..SseEvent::default()
566        };
567        assert!(matches!(d.decode(ev), Err(LlmError::Decode(_))));
568    }
569
570    #[test]
571    fn malformed_tool_arguments_are_not_replayed_as_empty_input() {
572        let mut assistant = Message::assistant("");
573        assistant.tool_calls = vec![ToolCall { id: "t".into(), name: "get".into(), arguments: "{\"a\":".into() }];
574        let req = ChatRequest::new("m", vec![Message::user("x"), assistant, Message::tool("t", "r")]);
575        assert!(matches!(build_body(&req), Err(LlmError::Config(_))));
576    }
577
578    #[test]
579    fn final_turn_keeps_tools_with_choice_none() {
580        let req = ChatRequest::new("m", vec![Message::user("x")])
581            .with_tools(vec![ToolSpec { name: "t".into(), description: "d".into(), parameters: json!({}) }])
582            .with_tool_choice(ToolChoice::None);
583        let body = build_body(&req).unwrap();
584        assert_eq!(body["tool_choice"]["type"], "none");
585        assert_eq!(body["tools"][0]["name"], "t");
586    }
587
588    #[test]
589    fn error_event() {
590        let mut d = AnthropicDecoder::default();
591        let ev = SseEvent {
592            data: "{\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"busy\"}}".into(),
593            ..SseEvent::default()
594        };
595        let err = d.decode(ev).unwrap_err();
596        assert!(err.is_retryable());
597    }
598}