use serde_json::{Value, json};
use causa_kernel::{
CacheDirective, ContextFrame, GenerationOptions, ModelInvokeError, ModelInvokeErrorKind,
ModelOutput, ModelRef, ModelResponse, ModelStopReason, ReasoningPayload, TextPayload,
ToolCallDraft, ToolResultStatus, ToolSurface,
};
use super::context_frame::{self, ResolvedMedia, Role, Segment};
use super::media::MediaSet;
pub const DEFAULT_MAX_TOKENS: u32 = 4096;
pub fn render_anthropic_messages(
frame: &ContextFrame,
media: &MediaSet,
tool_surface: &ToolSurface,
generation: &GenerationOptions,
model: &ModelRef,
cache: CacheDirective,
) -> Result<Value, ModelInvokeError> {
super::tool_history::validate_frame(frame)?;
let normalized = context_frame::normalize(frame, media);
let mut system_parts: Vec<String> = Vec::new();
let mut messages: Vec<(&'static str, Vec<Value>)> = Vec::new();
for segment in &normalized.segments {
match segment {
Segment::Text {
role: Role::System,
text,
} => system_parts.push(text.clone()),
Segment::Text {
role: Role::User,
text,
} => {
append_message(&mut messages, "user", json!({"type": "text", "text": text}));
}
Segment::Text {
role: Role::Assistant,
text,
} => {
append_message(
&mut messages,
"assistant",
json!({"type": "text", "text": text}),
);
}
Segment::Media { role, media } => match media {
ResolvedMedia::Image {
media_type,
data_base64,
} => append_message(
&mut messages,
role_name(*role),
image_block(media_type, data_base64),
),
ResolvedMedia::Placeholder(text) => {
if *role == Role::System {
system_parts.push(text.clone());
} else {
append_message(
&mut messages,
role_name(*role),
json!({"type": "text", "text": text}),
);
}
}
},
Segment::ToolCall(call) => append_message(
&mut messages,
"assistant",
json!({
"type": "tool_use",
"id": call.wire_id,
"name": call.name,
"input": call.arguments,
}),
),
Segment::ToolResult {
wire_id,
status,
content,
notes,
media,
} => {
let mut blocks = vec![json!({"type": "text", "text": content})];
if !notes.is_empty() {
blocks.push(json!({"type": "text", "text": "Notes:"}));
blocks.extend(notes.iter().map(|note| {
json!({
"type": "text",
"text": format!("- {note}"),
})
}));
}
blocks.extend(media.iter().map(media_block_json));
let content_json = if notes.is_empty() && media.is_empty() {
json!(content)
} else {
json!(blocks)
};
let mut block_json = json!({
"type": "tool_result",
"tool_use_id": wire_id,
"content": content_json,
});
if *status != ToolResultStatus::Succeeded {
block_json["is_error"] = json!(true);
}
append_message(&mut messages, "user", block_json);
}
}
}
super::tool_history::validate_anthropic(&messages)?;
if messages.is_empty() {
return Err(ModelInvokeError::new(
ModelInvokeErrorKind::InvalidRequest,
"frame rendered to zero messages; nothing to send",
));
}
if matches!(cache, CacheDirective::StablePrefix)
&& let Some(anchor_blocks) = stable_prefix_message(&mut messages)
&& let Some(last_block) = anchor_blocks.last_mut()
{
last_block["cache_control"] = json!({"type": "ephemeral"});
}
let mut body = json!({
"model": model.0,
"max_tokens": generation.max_tokens.unwrap_or(DEFAULT_MAX_TOKENS),
"messages": messages
.into_iter()
.map(|(role, content)| json!({"role": role, "content": content}))
.collect::<Vec<_>>(),
});
if let Some(temperature) = generation.temperature {
body["temperature"] = json!(temperature);
}
if !system_parts.is_empty() {
if matches!(cache, CacheDirective::StablePrefix) {
body["system"] = json!([{
"type": "text",
"text": system_parts.join("\n"),
"cache_control": {"type": "ephemeral"},
}]);
} else {
body["system"] = json!(system_parts.join("\n"));
}
}
if !tool_surface.definitions.is_empty() {
let mut tools =
context_frame::tool_definitions(tool_surface, context_frame::ToolShape::Anthropic);
if matches!(cache, CacheDirective::StablePrefix)
&& let Some(last_tool) = tools.last_mut()
{
last_tool["cache_control"] = json!({"type": "ephemeral"});
}
body["tools"] = json!(tools);
}
Ok(body)
}
fn stable_prefix_message<'a>(
messages: &'a mut [(&'static str, Vec<Value>)],
) -> Option<&'a mut Vec<Value>> {
let last = messages.last()?;
let dispatching = last
.1
.iter()
.any(|b| b.get("type").and_then(Value::as_str) == Some("tool_result"));
let index = if dispatching && messages.len() > 1 {
messages.len() - 2
} else {
messages.len() - 1
};
Some(&mut messages[index].1)
}
fn role_name(role: Role) -> &'static str {
match role {
Role::System => "system",
Role::User => "user",
Role::Assistant => "assistant",
}
}
fn image_block(media_type: &str, data_base64: &str) -> Value {
json!({
"type": "image",
"source": {"type": "base64", "media_type": media_type, "data": data_base64},
})
}
fn media_block_json(media: &ResolvedMedia) -> Value {
match media {
ResolvedMedia::Image {
media_type,
data_base64,
} => image_block(media_type, data_base64),
ResolvedMedia::Placeholder(text) => json!({"type": "text", "text": text}),
}
}
pub fn parse_anthropic_response(value: &Value) -> Result<ModelOutput, ModelInvokeError> {
fn permanent(message: impl Into<String>) -> ModelInvokeError {
ModelInvokeError::new(ModelInvokeErrorKind::Permanent, message)
}
let empty = Vec::new();
let content = match value.get("content") {
None | Some(Value::Null) => &empty,
Some(Value::Array(items)) => items,
Some(_) => return Err(permanent("content: expected an array")),
};
let mut texts: Vec<String> = Vec::new();
let mut tool_calls: Vec<ToolCallDraft> = Vec::new();
let mut thinking_parts: Vec<String> = Vec::new();
let mut signature: Option<String> = None;
for block in content {
match block.get("type").and_then(Value::as_str) {
Some("text") => {
let text = block
.get("text")
.and_then(Value::as_str)
.ok_or_else(|| permanent("text block: missing `text` string"))?;
texts.push(text.to_string());
}
Some("tool_use") => {
let id = block
.get("id")
.and_then(Value::as_str)
.ok_or_else(|| permanent("tool_use block: missing `id` string"))?;
let name = block
.get("name")
.and_then(Value::as_str)
.ok_or_else(|| permanent("tool_use block: missing `name` string"))?;
tool_calls.push(ToolCallDraft {
tool_name: name.to_string(),
arguments: block.get("input").cloned().unwrap_or_else(|| json!({})),
provider_call_id: Some(id.to_string()),
});
}
Some("thinking") => {
let text = block
.get("thinking")
.and_then(Value::as_str)
.ok_or_else(|| permanent("thinking block: missing `thinking` string"))?;
thinking_parts.push(text.to_string());
if let Some(sig) = block.get("signature").and_then(Value::as_str) {
signature = Some(sig.to_string());
}
}
_ => {}
}
}
let stop_reason = match value.get("stop_reason") {
Some(Value::String(s)) => match s.as_str() {
"end_turn" => ModelStopReason::EndTurn,
"tool_use" => ModelStopReason::ToolUse,
"max_tokens" => ModelStopReason::MaxTokens,
"refusal" => ModelStopReason::Refusal,
_ => ModelStopReason::EndTurn,
},
None | Some(Value::Null) => return Err(permanent("missing stop_reason")),
Some(_) => return Err(permanent("stop_reason: expected a string")),
};
let usage = value
.get("usage")
.filter(|v| v.is_object())
.map(crate::translation::usage::model_usage_from_anthropic);
Ok(ModelOutput {
response: ModelResponse {
text: TextPayload::new(texts.join("\n")),
tool_calls,
},
usage,
stop_reason,
reasoning: if thinking_parts.is_empty() {
None
} else {
Some(ReasoningPayload {
text: thinking_parts.join("\n"),
signature,
})
},
})
}
fn append_message(
messages: &mut Vec<(&'static str, Vec<Value>)>,
role: &'static str,
block: Value,
) {
match messages.last_mut() {
Some((last_role, content)) if *last_role == role => content.push(block),
_ => messages.push((role, vec![block])),
}
}
#[cfg(test)]
mod tests {
use super::*;
use causa_kernel::{MediaRef, ModelUsage, ToolDefinition};
use crate::translation::media::{MediaPayload, MediaSet};
use crate::translation::test_support::{
call, frame, media_part, parts, result, result_with_media, text, text_part,
};
fn render(frame: &ContextFrame) -> Value {
render_anthropic_messages(
frame,
&MediaSet::new(),
&ToolSurface::empty(),
&GenerationOptions::default(),
&ModelRef::new("claude-test"),
CacheDirective::None,
)
.unwrap()
}
#[test]
fn source_vocabulary_full_branch_coverage() {
let f = frame(vec![
text(0, "be terse", Some("system")),
text(1, "model said", None),
text(2, "user said", Some("user")),
text(3, "replayed", Some("assistant")),
text(4, "injected", Some("inject:note")),
text(5, "bare inject", Some("inject")),
text(6, "unknown tag", Some("provider:gpt-x")),
]);
let v = render(&f);
assert_eq!(v["system"], json!("be terse"));
let msgs = v["messages"].as_array().unwrap();
assert_eq!(msgs.len(), 4);
assert_eq!(msgs[0]["role"], "assistant");
assert_eq!(
msgs[0]["content"],
json!([{"type": "text", "text": "model said"}])
);
assert_eq!(msgs[1]["role"], "user");
assert_eq!(msgs[2]["role"], "assistant");
assert_eq!(msgs[3]["role"], "user");
assert_eq!(
msgs[3]["content"],
json!([{"type": "text", "text": "injected\nbare inject\nunknown tag"}])
);
}
#[test]
fn empty_text_blocks_are_skipped() {
let f = frame(vec![
text(0, "", Some("user")),
text(1, "real", Some("user")),
text(2, "", Some("system")),
text(3, "still here", None),
]);
let v = render(&f);
let msgs = v["messages"].as_array().unwrap();
assert_eq!(msgs.len(), 2);
assert_eq!(
msgs[0]["content"],
json!([{"type": "text", "text": "real"}])
);
assert!(v.get("system").is_none());
}
#[test]
fn tool_round_trip_pairing_and_consecutive_merge() {
let fallback_id = call(2, "kc2", None, "list", json!({})).id().0.to_string();
let f = frame(vec![
text(0, "reading now", None),
call(1, "kc1", Some("toolu_a"), "read", json!({"path": "a"})),
call(2, "kc2", None, "list", json!({})),
result(3, "kc1", ToolResultStatus::Succeeded, json!("file-a")),
result(4, "kc2", ToolResultStatus::Failed, json!({"error": "boom"})),
]);
let v = render(&f);
let msgs = v["messages"].as_array().unwrap();
assert_eq!(msgs.len(), 2);
assert_eq!(msgs[0]["role"], "assistant");
assert_eq!(
msgs[0]["content"][0],
json!({"type": "text", "text": "reading now"})
);
assert_eq!(
msgs[0]["content"][1],
json!({"type": "tool_use", "id": "toolu_a", "name": "read", "input": {"path": "a"}})
);
assert_eq!(
msgs[0]["content"][2],
json!({"type": "tool_use", "id": fallback_id, "name": "list", "input": {}})
);
assert_eq!(msgs[1]["role"], "user");
assert_eq!(
msgs[1]["content"][0],
json!({"type": "tool_result", "tool_use_id": "toolu_a", "content": "file-a"})
);
assert_eq!(
msgs[1]["content"][1],
json!({
"type": "tool_result",
"tool_use_id": fallback_id,
"content": "{\"error\":\"boom\"}",
"is_error": true,
})
);
}
#[test]
fn unpaired_tool_result_is_invalid_before_http() {
let f = frame(vec![result(
0,
"orphan",
ToolResultStatus::Succeeded,
json!("x"),
)]);
let error = render_anthropic_messages(
&f,
&MediaSet::new(),
&ToolSurface::empty(),
&GenerationOptions::default(),
&ModelRef::new("test"),
CacheDirective::None,
)
.unwrap_err();
assert_eq!(error.kind, ModelInvokeErrorKind::InvalidRequest);
}
#[test]
fn tools_generation_and_byte_determinism() {
let surface = ToolSurface::from_definitions(vec![ToolDefinition {
name: "read".into(),
description: "read a file".into(),
parameters: json!({"type": "object", "properties": {"path": {"type": "string"}}}),
}]);
let f = frame(vec![text(0, "hi", Some("user"))]);
let generation = GenerationOptions {
temperature: Some(0.5),
max_tokens: None,
..GenerationOptions::default()
};
let v = render_anthropic_messages(
&f,
&MediaSet::new(),
&surface,
&generation,
&ModelRef::new("claude-test"),
CacheDirective::None,
)
.unwrap();
assert_eq!(v["model"], json!("claude-test"));
assert_eq!(v["temperature"], json!(0.5));
assert_eq!(v["max_tokens"], json!(4096));
assert_eq!(
v["tools"],
json!([{
"name": "read",
"description": "read a file",
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}}},
}])
);
let generation = GenerationOptions {
temperature: Some(0.5),
max_tokens: Some(100),
..GenerationOptions::default()
};
let v2 = render_anthropic_messages(
&f,
&MediaSet::new(),
&surface,
&generation,
&ModelRef::new("claude-test"),
CacheDirective::None,
)
.unwrap();
assert_eq!(v2["max_tokens"], json!(100));
let again = render_anthropic_messages(
&f,
&MediaSet::new(),
&surface,
&generation,
&ModelRef::new("claude-test"),
CacheDirective::None,
)
.unwrap();
assert_eq!(
serde_json::to_string(&v2).unwrap(),
serde_json::to_string(&again).unwrap()
);
}
#[test]
fn empty_frame_is_invalid_request() {
let f = frame(vec![]);
let e = render_anthropic_messages(
&f,
&MediaSet::new(),
&ToolSurface::empty(),
&GenerationOptions::default(),
&ModelRef::new("m"),
CacheDirective::None,
)
.unwrap_err();
assert!(matches!(e.kind(), ModelInvokeErrorKind::InvalidRequest));
}
#[test]
fn cache_none_omits_every_cache_control_key() {
let f = frame(vec![
text(0, "be terse", Some("system")),
text(1, "hi", Some("user")),
]);
let surface = ToolSurface::from_definitions(vec![ToolDefinition {
name: "read".into(),
description: "read a file".into(),
parameters: json!({"type": "object"}),
}]);
let v = render_anthropic_messages(
&f,
&MediaSet::new(),
&surface,
&GenerationOptions::default(),
&ModelRef::new("claude-test"),
CacheDirective::None,
)
.unwrap();
assert!(!serde_json::to_string(&v).unwrap().contains("cache_control"));
}
#[test]
fn stable_prefix_marks_tools_system_and_previous_message() {
let f = frame(vec![
text(0, "be terse", Some("system")),
text(1, "hi", Some("user")),
call(2, "kc1", Some("toolu_a"), "read", json!({"path": "a"})),
result(3, "kc1", ToolResultStatus::Succeeded, json!("file-a")),
]);
let surface = ToolSurface::from_definitions(vec![ToolDefinition {
name: "read".into(),
description: "read a file".into(),
parameters: json!({"type": "object"}),
}]);
let v = render_anthropic_messages(
&f,
&MediaSet::new(),
&surface,
&GenerationOptions::default(),
&ModelRef::new("claude-test"),
CacheDirective::StablePrefix,
)
.unwrap();
assert_eq!(
v["system"],
json!([{
"type": "text",
"text": "be terse",
"cache_control": {"type": "ephemeral"},
}])
);
let msgs = v["messages"].as_array().unwrap();
assert_eq!(msgs.len(), 3);
assert!(msgs[1]["content"][0].get("cache_control").is_some());
assert_eq!(v["tools"][0]["cache_control"], json!({"type": "ephemeral"}));
}
#[test]
fn stable_prefix_without_dispatch_anchors_the_final_message() {
let f = frame(vec![text(0, "hi", Some("user"))]);
let v = render_anthropic_messages(
&f,
&MediaSet::new(),
&ToolSurface::empty(),
&GenerationOptions::default(),
&ModelRef::new("claude-test"),
CacheDirective::StablePrefix,
)
.unwrap();
let msgs = v["messages"].as_array().unwrap();
assert!(msgs[0]["content"][0].get("cache_control").is_some());
}
fn resolved(reference: &str) -> MediaSet {
let mut m = MediaSet::new();
m.insert(reference, MediaPayload::new("image/png", "AAAA"));
m
}
#[test]
fn image_parts_render_as_image_blocks_in_user_position() {
let f = frame(vec![parts(
0,
vec![
text_part("caption"),
media_part("image/png", "asset-1"),
text_part("after"),
],
Some("user"),
)]);
let v = render_anthropic_messages(
&f,
&resolved("asset-1"),
&ToolSurface::empty(),
&GenerationOptions::default(),
&ModelRef::new("claude-test"),
CacheDirective::None,
)
.unwrap();
assert_eq!(
v["messages"][0]["content"],
json!([
{"type": "text", "text": "caption"},
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "AAAA"}},
{"type": "text", "text": "after"},
])
);
}
#[test]
fn unresolved_media_degrades_to_the_deterministic_placeholder() {
let f = frame(vec![parts(
0,
vec![media_part("image/png", "missing-asset")],
Some("user"),
)]);
let v = render_anthropic_messages(
&f,
&MediaSet::new(),
&ToolSurface::empty(),
&GenerationOptions::default(),
&ModelRef::new("claude-test"),
CacheDirective::None,
)
.unwrap();
assert_eq!(
v["messages"][0]["content"],
json!([{"type": "text", "text": "[media: image/png missing-asset]"}])
);
}
#[test]
fn non_image_media_degrades_even_when_resolved() {
let f = frame(vec![parts(
0,
vec![media_part("application/pdf", "doc-1")],
Some("user"),
)]);
let mut media = MediaSet::new();
media.insert("doc-1", MediaPayload::new("application/pdf", "AAAA"));
let v = render_anthropic_messages(
&f,
&media,
&ToolSurface::empty(),
&GenerationOptions::default(),
&ModelRef::new("claude-test"),
CacheDirective::None,
)
.unwrap();
assert_eq!(
v["messages"][0]["content"],
json!([{"type": "text", "text": "[media: application/pdf doc-1]"}])
);
}
#[test]
fn media_in_system_position_degrades_to_placeholder_text() {
let f = frame(vec![
parts(0, vec![media_part("image/png", "asset-1")], Some("system")),
text(1, "hi", Some("user")),
]);
let v = render_anthropic_messages(
&f,
&resolved("asset-1"),
&ToolSurface::empty(),
&GenerationOptions::default(),
&ModelRef::new("claude-test"),
CacheDirective::None,
)
.unwrap();
assert_eq!(v["system"], json!("[media: image/png asset-1]"));
}
#[test]
fn tool_result_media_embeds_in_the_tool_result_content() {
let f = frame(vec![
call(0, "kc1", Some("toolu_a"), "render", json!({})),
result_with_media(
1,
"kc1",
ToolResultStatus::Succeeded,
json!("chart ready"),
vec![
MediaRef::new("image/png", "asset-1"),
MediaRef::new("image/png", "asset-2"),
],
),
]);
let mut media = MediaSet::new();
media.insert("asset-1", MediaPayload::new("image/png", "AAAA"));
let v = render_anthropic_messages(
&f,
&media,
&ToolSurface::empty(),
&GenerationOptions::default(),
&ModelRef::new("claude-test"),
CacheDirective::None,
)
.unwrap();
assert_eq!(
v["messages"][1]["content"][0]["content"],
json!([
{"type": "text", "text": "chart ready"},
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "AAAA"}},
{"type": "text", "text": "[media: image/png asset-2]"},
])
);
}
#[test]
fn result_without_media_keeps_the_string_content_shape() {
let f = frame(vec![
call(0, "kc1", None, "read", json!({})),
result(1, "kc1", ToolResultStatus::Succeeded, json!("ok")),
]);
let v = render_anthropic_messages(
&f,
&resolved("unused"),
&ToolSurface::empty(),
&GenerationOptions::default(),
&ModelRef::new("claude-test"),
CacheDirective::None,
)
.unwrap();
assert_eq!(v["messages"][1]["content"][0]["content"], json!("ok"));
}
#[test]
fn stable_prefix_anchor_lands_on_parts_messages() {
let f = frame(vec![
text(0, "hi", Some("user")),
parts(
1,
vec![text_part("see"), media_part("image/png", "asset-1")],
Some("user"),
),
]);
let v = render_anthropic_messages(
&f,
&resolved("asset-1"),
&ToolSurface::empty(),
&GenerationOptions::default(),
&ModelRef::new("claude-test"),
CacheDirective::StablePrefix,
)
.unwrap();
let msgs = v["messages"].as_array().unwrap();
assert_eq!(msgs.len(), 1);
assert_eq!(
msgs[0]["content"][0],
json!({"type": "text", "text": "hi\nsee"})
);
assert!(
msgs[0]["content"]
.as_array()
.unwrap()
.last()
.unwrap()
.get("cache_control")
.is_some()
);
}
#[test]
fn parse_three_content_shapes_with_provider_id_passthrough() {
let v = json!({
"content": [
{"type": "thinking", "thinking": "hmm", "signature": "sig1"},
{"type": "text", "text": "Let me check."},
{"type": "tool_use", "id": "toolu_9", "name": "read", "input": {"path": "a"}},
],
"stop_reason": "tool_use",
"usage": {"input_tokens": 10, "output_tokens": 5},
});
let out = parse_anthropic_response(&v).unwrap();
assert_eq!(out.response.text.0, "Let me check.");
assert_eq!(out.response.tool_calls.len(), 1);
let tc = &out.response.tool_calls[0];
assert_eq!(tc.tool_name, "read");
assert_eq!(tc.arguments, json!({"path": "a"}));
assert_eq!(tc.provider_call_id.as_deref(), Some("toolu_9"));
assert!(matches!(out.stop_reason, ModelStopReason::ToolUse));
let reasoning = out.reasoning.unwrap();
assert_eq!(reasoning.text, "hmm");
assert_eq!(reasoning.signature.as_deref(), Some("sig1"));
}
#[test]
fn usage_mapping_includes_cache_fields() {
let v = json!({
"content": [{"type": "text", "text": "ok"}],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 100,
"output_tokens": 20,
"cache_read_input_tokens": 11,
"cache_creation_input_tokens": 3,
},
});
let out = parse_anthropic_response(&v).unwrap();
let u: ModelUsage = out.usage.unwrap();
assert_eq!(u.input_tokens, 100);
assert_eq!(u.output_tokens, 20);
assert_eq!(u.cache_read_tokens, Some(11));
assert_eq!(u.cache_write_tokens, Some(3));
assert_eq!(u.reasoning_tokens, None);
}
#[test]
fn absent_usage_is_none() {
let v = json!({"content": [{"type": "text", "text": "ok"}], "stop_reason": "end_turn"});
let out = parse_anthropic_response(&v).unwrap();
assert!(out.usage.is_none());
}
#[test]
fn stop_reason_mapping_full_and_unknown_degrades_to_end_turn() {
for (wire, expected) in [
("end_turn", ModelStopReason::EndTurn),
("tool_use", ModelStopReason::ToolUse),
("max_tokens", ModelStopReason::MaxTokens),
("refusal", ModelStopReason::Refusal),
("model_context_overflow", ModelStopReason::EndTurn),
] {
let v = json!({"content": [], "stop_reason": wire});
let out = parse_anthropic_response(&v).unwrap();
assert_eq!(
std::mem::discriminant(&out.stop_reason),
std::mem::discriminant(&expected),
"wire stop_reason = {wire}"
);
}
}
#[test]
fn missing_stop_reason_is_permanent_error() {
let v = json!({"content": []});
let e = parse_anthropic_response(&v).unwrap_err();
assert!(matches!(e.kind(), ModelInvokeErrorKind::Permanent));
assert!(e.message.contains("stop_reason"));
}
#[test]
fn multiple_texts_join_and_exotic_blocks_are_skipped() {
let v = json!({
"content": [
{"type": "redacted_thinking", "data": "encrypted"},
{"type": "text", "text": "a"},
{"type": "server_tool_use", "id": "x", "name": "web_search"},
{"type": "text", "text": "b"},
],
"stop_reason": "end_turn",
});
let out = parse_anthropic_response(&v).unwrap();
assert_eq!(out.response.text.0, "a\nb");
assert!(out.response.tool_calls.is_empty());
assert!(out.reasoning.is_none());
}
#[test]
fn malformed_tool_use_is_permanent_error() {
let v = json!({"content": [{"type": "tool_use", "id": "x"}], "stop_reason": "tool_use"});
let e = parse_anthropic_response(&v).unwrap_err();
assert!(matches!(e.kind(), ModelInvokeErrorKind::Permanent));
assert!(e.message.contains("name"));
}
}