Skip to main content

agent_block_testkit/shapes/
anthropic.rs

1//! Anthropic Messages API response shapes.
2//!
3//! Tool calls are `type: "tool_use"` content blocks with an `input` **object**
4//! and a mandatory `id`; a tool-calling turn ends with
5//! `stop_reason: "tool_use"`
6//! (<https://platform.claude.com/docs/en/agents-and-tools/tool-use/overview>).
7
8use serde_json::{json, Value};
9use std::sync::atomic::{AtomicUsize, Ordering};
10
11static RESPONSE_SEQ: AtomicUsize = AtomicUsize::new(0);
12
13fn next_id(prefix: &str) -> String {
14    format!(
15        "{prefix}-{}",
16        RESPONSE_SEQ.fetch_add(1, Ordering::Relaxed) + 1
17    )
18}
19
20/// A `tool_use` content block.
21pub fn tool_use(id: &str, name: &str, input: Value) -> Value {
22    json!({ "type": "tool_use", "id": id, "name": name, "input": input })
23}
24
25/// Assistant text response (`stop_reason: "end_turn"`).
26pub fn text_response(text: &str) -> Value {
27    json!({
28        "id": next_id("msg-testkit"),
29        "type": "message",
30        "role": "assistant",
31        "content": [{ "type": "text", "text": text }],
32        "model": "claude-testkit-mock",
33        "stop_reason": "end_turn",
34        "usage": { "input_tokens": 10, "output_tokens": 10 }
35    })
36}
37
38/// Assistant tool-use response (`stop_reason: "tool_use"`).
39pub fn tool_use_response(blocks: Vec<Value>) -> Value {
40    json!({
41        "id": next_id("msg-testkit"),
42        "type": "message",
43        "role": "assistant",
44        "content": blocks,
45        "model": "claude-testkit-mock",
46        "stop_reason": "tool_use",
47        "usage": { "input_tokens": 10, "output_tokens": 10 }
48    })
49}