Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(llm): read Claude responses that start with thinking blocks
Vinícius DS Andrade0a1f618f629f
diff --git a/crates/ai-memory-llm/src/anthropic.rs b/crates/ai-memory-llm/src/anthropic.rsindex 150d2879..5bc04d62 100644--- a/crates/ai-memory-llm/src/anthropic.rs+++ b/crates/ai-memory-llm/src/anthropic.rs@@ -212,6 +212,13 @@ struct AnthropicResponse {enum AnthropicContent {Text { text: String },ToolUse { input: serde_json::Value },+ /// A block this provider does not read, such as `thinking` or+ /// `redacted_thinking`. Models that think by default (Opus 5 and later,+ /// Sonnet 5 and later, Haiku 5.5, Fable, Mythos), and every model sent+ /// adaptive thinking for a configured effort, can start a response with+ /// such blocks. Without this variant one of them fails the whole parse.+ #[serde(other)]+ Other,}#[derive(Debug, Deserialize)]@@ -247,7 +254,7 @@ impl LlmProvider for AnthropicProvider {.iter().filter_map(|c| match c {AnthropicContent::Text { text } => Some(text.as_str()),- AnthropicContent::ToolUse { .. } => None,+ AnthropicContent::ToolUse { .. } | AnthropicContent::Other => None,}).collect::<Vec<_>>().join("\n");@@ -593,6 +600,8 @@ fn version_segment(s: &str) -> Option<u32> {mod tests {use secrecy::SecretString;use serde_json::json;+ use wiremock::matchers::{method, path};+ use wiremock::{Mock, MockServer, ResponseTemplate};use crate::types::{ChatMessage, ReasoningEffort};use rstest::rstest;@@ -737,6 +746,46 @@ mod tests {assert!(plain.get("thinking").is_none());}+ /// A model that thinks opens its response with `thinking` blocks, empty+ /// under the default `display: "omitted"`, and may return+ /// `redacted_thinking`. Both completion paths must read past them.+ #[tokio::test]+ async fn thinking_blocks_do_not_fail_either_completion_path() {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .and(path("/v1/messages"))+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({+ "model": "claude-opus-5",+ "content": [+ {"type": "thinking", "thinking": "", "signature": "sig"},+ {"type": "redacted_thinking", "data": "opaque"},+ {"type": "text", "text": "OK"},+ {+ "type": "tool_use",+ "id": "toolu_1",+ "name": "result",+ "input": {"answer": "OK"}+ }+ ],+ "usage": {"input_tokens": 3, "output_tokens": 5}+ })))+ .expect(2)+ .mount(&server)+ .await;+ let provider = AnthropicProvider::new(SecretString::from("sk-ant-test"), "claude-opus-5")+ .unwrap()+ .with_base_url(server.uri());++ let response = provider.complete(chat_request()).await.unwrap();+ assert_eq!(response.text, "OK");++ let value = provider+ .complete_structured_raw(chat_request(), json!({"type": "object"}))+ .await+ .unwrap();+ assert_eq!(value, json!({"answer": "OK"}));+ }+fn body_for_effort(model: &str, effort: ReasoningEffort) -> serde_json::Value {let provider = AnthropicProvider::new(SecretString::from("sk-ant-test"), model).unwrap()