adk-anthropic 0.6.0

Dedicated Anthropic API client for ADK-Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use serde::{Deserialize, Serialize};

use crate::types::{
    CodeExecutionResultBlock, DocumentBlock, ImageBlock, ProgrammaticToolUseBlock,
    RedactedThinkingBlock, ServerToolUseBlock, TextBlock, ThinkingBlock, ToolResultBlock,
    ToolUseBlock, WebSearchToolResultBlock,
};

/// A block of content in a message.
///
/// This enum represents the different types of content blocks that can be included
/// in a message's content.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")]
pub enum ContentBlock {
    /// A block of text content
    #[serde(rename = "text")]
    Text(TextBlock),

    /// An image block
    #[serde(rename = "image")]
    Image(ImageBlock),

    /// A block representing a tool use request
    #[serde(rename = "tool_use")]
    ToolUse(ToolUseBlock),

    /// A block representing a server-side tool use request
    #[serde(rename = "server_tool_use")]
    ServerToolUse(ServerToolUseBlock),

    /// A web search tool result block
    #[serde(rename = "web_search_tool_result")]
    WebSearchToolResult(WebSearchToolResultBlock),

    /// A tool result block
    #[serde(rename = "tool_result")]
    ToolResult(ToolResultBlock),

    /// A document block
    #[serde(rename = "document")]
    Document(DocumentBlock),

    /// A block containing model thinking
    #[serde(rename = "thinking")]
    Thinking(ThinkingBlock),

    /// A block containing redacted thinking data
    #[serde(rename = "redacted_thinking")]
    RedactedThinking(RedactedThinkingBlock),

    /// A code execution result block
    #[serde(rename = "code_execution_result")]
    CodeExecutionResult(CodeExecutionResultBlock),

    /// A programmatic tool use block from code execution
    #[serde(rename = "programmatic_tool_use")]
    ProgrammaticToolUse(ProgrammaticToolUseBlock),
}

impl ContentBlock {
    /// Returns true if this block is a text block
    pub fn is_text(&self) -> bool {
        matches!(self, ContentBlock::Text(_))
    }

    /// Returns true if this block is an image block
    pub fn is_image(&self) -> bool {
        matches!(self, ContentBlock::Image(_))
    }

    /// Returns true if this block is a tool use block
    pub fn is_tool_use(&self) -> bool {
        matches!(self, ContentBlock::ToolUse(_))
    }

    /// Returns true if this block is a server tool use block
    pub fn is_server_tool_use(&self) -> bool {
        matches!(self, ContentBlock::ServerToolUse(_))
    }

    /// Returns true if this block is a web search tool result block
    pub fn is_web_search_tool_result(&self) -> bool {
        matches!(self, ContentBlock::WebSearchToolResult(_))
    }

    /// Returns true if this block is a tool result block
    pub fn is_tool_result(&self) -> bool {
        matches!(self, ContentBlock::ToolResult(_))
    }

    /// Returns true if this block is a document block
    pub fn is_document(&self) -> bool {
        matches!(self, ContentBlock::Document(_))
    }

    /// Returns true if this block is a thinking block
    pub fn is_thinking(&self) -> bool {
        matches!(self, ContentBlock::Thinking(_))
    }

    /// Returns true if this block is a redacted thinking block
    pub fn is_redacted_thinking(&self) -> bool {
        matches!(self, ContentBlock::RedactedThinking(_))
    }

    /// Returns a reference to the inner TextBlock if this is a Text variant,
    /// or None otherwise.
    pub fn as_text(&self) -> Option<&TextBlock> {
        match self {
            ContentBlock::Text(block) => Some(block),
            _ => None,
        }
    }

    /// Returns a reference to the inner ImageBlock if this is an Image variant,
    /// or None otherwise.
    pub fn as_image(&self) -> Option<&ImageBlock> {
        match self {
            ContentBlock::Image(block) => Some(block),
            _ => None,
        }
    }

    /// Returns a reference to the inner ToolUseBlock if this is a ToolUse variant,
    /// or None otherwise.
    pub fn as_tool_use(&self) -> Option<&ToolUseBlock> {
        match self {
            ContentBlock::ToolUse(block) => Some(block),
            _ => None,
        }
    }

    /// Returns a reference to the inner ServerToolUseBlock if this is a ServerToolUse variant,
    /// or None otherwise.
    pub fn as_server_tool_use(&self) -> Option<&ServerToolUseBlock> {
        match self {
            ContentBlock::ServerToolUse(block) => Some(block),
            _ => None,
        }
    }

    /// Returns a reference to the inner WebSearchToolResultBlock if this is a WebSearchToolResult variant,
    /// or None otherwise.
    pub fn as_web_search_tool_result(&self) -> Option<&WebSearchToolResultBlock> {
        match self {
            ContentBlock::WebSearchToolResult(block) => Some(block),
            _ => None,
        }
    }

    /// Returns a reference to the inner ToolResultBlock if this is a ToolResult variant,
    /// or None otherwise.
    pub fn as_tool_result(&self) -> Option<&ToolResultBlock> {
        match self {
            ContentBlock::ToolResult(block) => Some(block),
            _ => None,
        }
    }

    /// Returns a reference to the inner DocumentBlock if this is a Document variant,
    /// or None otherwise.
    pub fn as_document(&self) -> Option<&DocumentBlock> {
        match self {
            ContentBlock::Document(block) => Some(block),
            _ => None,
        }
    }

    /// Returns a reference to the inner ThinkingBlock if this is a Thinking variant,
    /// or None otherwise.
    pub fn as_thinking(&self) -> Option<&ThinkingBlock> {
        match self {
            ContentBlock::Thinking(block) => Some(block),
            _ => None,
        }
    }

    /// Returns a reference to the inner RedactedThinkingBlock if this is a RedactedThinking variant,
    /// or None otherwise.
    pub fn as_redacted_thinking(&self) -> Option<&RedactedThinkingBlock> {
        match self {
            ContentBlock::RedactedThinking(block) => Some(block),
            _ => None,
        }
    }

    /// Returns true if this block is a code execution result block
    pub fn is_code_execution_result(&self) -> bool {
        matches!(self, ContentBlock::CodeExecutionResult(_))
    }

    /// Returns a reference to the inner CodeExecutionResultBlock if this is a CodeExecutionResult variant,
    /// or None otherwise.
    pub fn as_code_execution_result(&self) -> Option<&CodeExecutionResultBlock> {
        match self {
            ContentBlock::CodeExecutionResult(block) => Some(block),
            _ => None,
        }
    }

    /// Returns true if this block is a programmatic tool use block
    pub fn is_programmatic_tool_use(&self) -> bool {
        matches!(self, ContentBlock::ProgrammaticToolUse(_))
    }

    /// Returns a reference to the inner ProgrammaticToolUseBlock if this is a ProgrammaticToolUse variant,
    /// or None otherwise.
    pub fn as_programmatic_tool_use(&self) -> Option<&ProgrammaticToolUseBlock> {
        match self {
            ContentBlock::ProgrammaticToolUse(block) => Some(block),
            _ => None,
        }
    }
}

/// Helper methods to create ContentBlock variants
impl From<TextBlock> for ContentBlock {
    fn from(block: TextBlock) -> Self {
        ContentBlock::Text(block)
    }
}

impl From<ImageBlock> for ContentBlock {
    fn from(block: ImageBlock) -> Self {
        ContentBlock::Image(block)
    }
}

impl From<ToolUseBlock> for ContentBlock {
    fn from(block: ToolUseBlock) -> Self {
        ContentBlock::ToolUse(block)
    }
}

impl From<ServerToolUseBlock> for ContentBlock {
    fn from(block: ServerToolUseBlock) -> Self {
        ContentBlock::ServerToolUse(block)
    }
}

impl From<WebSearchToolResultBlock> for ContentBlock {
    fn from(block: WebSearchToolResultBlock) -> Self {
        ContentBlock::WebSearchToolResult(block)
    }
}

impl From<ToolResultBlock> for ContentBlock {
    fn from(block: ToolResultBlock) -> Self {
        ContentBlock::ToolResult(block)
    }
}

impl From<DocumentBlock> for ContentBlock {
    fn from(block: DocumentBlock) -> Self {
        ContentBlock::Document(block)
    }
}

impl From<ThinkingBlock> for ContentBlock {
    fn from(block: ThinkingBlock) -> Self {
        ContentBlock::Thinking(block)
    }
}

impl From<RedactedThinkingBlock> for ContentBlock {
    fn from(block: RedactedThinkingBlock) -> Self {
        ContentBlock::RedactedThinking(block)
    }
}

impl From<CodeExecutionResultBlock> for ContentBlock {
    fn from(block: CodeExecutionResultBlock) -> Self {
        ContentBlock::CodeExecutionResult(block)
    }
}

impl From<ProgrammaticToolUseBlock> for ContentBlock {
    fn from(block: ProgrammaticToolUseBlock) -> Self {
        ContentBlock::ProgrammaticToolUse(block)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn text_block_serialization() {
        let text_block = TextBlock::new("This is some text content.");
        let content_block = ContentBlock::from(text_block);

        let json = serde_json::to_string(&content_block).unwrap();
        let expected = r#"{"type":"text","text":"This is some text content."}"#;

        assert_eq!(json, expected);
    }

    #[test]
    fn tool_use_block_serialization() {
        let input_json = serde_json::json!({
            "query": "weather in San Francisco",
            "limit": 5
        });

        let tool_block = ToolUseBlock::new("tool_123", "search", input_json);
        let content_block = ContentBlock::from(tool_block);

        let json = serde_json::to_string(&content_block).unwrap();
        let actual: serde_json::Value = serde_json::from_str(&json).unwrap();
        let expected: serde_json::Value = serde_json::json!({
            "type": "tool_use",
            "id": "tool_123",
            "input": {"limit": 5, "query": "weather in San Francisco"},
            "name": "search"
        });

        assert_eq!(actual, expected);
    }

    #[test]
    fn server_tool_use_block_serialization() {
        let server_block =
            ServerToolUseBlock::new_web_search("tool_123", "weather in San Francisco");
        let content_block = ContentBlock::from(server_block);

        let json = serde_json::to_string(&content_block).unwrap();
        let expected = r#"{"type":"server_tool_use","id":"tool_123","input":{"query":"weather in San Francisco"},"name":"web_search"}"#;

        assert_eq!(json, expected);
    }

    #[test]
    fn thinking_block_serialization() {
        let thinking_block = ThinkingBlock::new(
            "Let me think through this problem step by step...",
            "abc123signature",
        );
        let content_block = ContentBlock::from(thinking_block);

        let json = serde_json::to_string(&content_block).unwrap();
        let expected = r#"{"type":"thinking","signature":"abc123signature","thinking":"Let me think through this problem step by step..."}"#;

        assert_eq!(json, expected);
    }

    #[test]
    fn redacted_thinking_block_serialization() {
        let redacted_block = RedactedThinkingBlock::new("encoded-thinking-data-123");
        let content_block = ContentBlock::from(redacted_block);

        let json = serde_json::to_string(&content_block).unwrap();
        let expected = r#"{"type":"redacted_thinking","data":"encoded-thinking-data-123"}"#;

        assert_eq!(json, expected);
    }

    #[test]
    fn deserialization() {
        let json = r#"{"text":"This is some text content.","type":"text"}"#;
        let content_block: ContentBlock = serde_json::from_str(json).unwrap();

        assert!(content_block.is_text());
        assert!(!content_block.is_tool_use());

        if let ContentBlock::Text(block) = content_block {
            assert_eq!(block.text, "This is some text content.");
        } else {
            panic!("Expected TextBlock");
        }

        let json = r#"{"id":"tool_123","input":{"query":"weather in San Francisco"},"name":"web_search","type":"server_tool_use"}"#;
        let content_block: ContentBlock = serde_json::from_str(json).unwrap();

        assert!(content_block.is_server_tool_use());
        assert!(!content_block.is_text());

        if let ContentBlock::ServerToolUse(block) = content_block {
            assert_eq!(block.id, "tool_123");
            assert_eq!(block.name, "web_search");
        } else {
            panic!("Expected ServerToolUseBlock");
        }
    }

    #[test]
    fn as_methods() {
        let text_block = TextBlock::new("This is some text content.");
        let content_block = ContentBlock::from(text_block);

        assert!(content_block.as_text().is_some());
        assert!(content_block.as_image().is_none());
        assert!(content_block.as_tool_use().is_none());
        assert!(content_block.as_server_tool_use().is_none());
        assert!(content_block.as_web_search_tool_result().is_none());
        assert!(content_block.as_tool_result().is_none());
        assert!(content_block.as_document().is_none());
        assert!(content_block.as_thinking().is_none());
        assert!(content_block.as_redacted_thinking().is_none());

        let text_ref = content_block.as_text().unwrap();
        assert_eq!(text_ref.text, "This is some text content.");
    }

    #[test]
    fn image_block_serialization() {
        let image_source =
            crate::types::UrlImageSource::new("https://example.com/image.jpg".to_string());
        let image_block = ImageBlock::new_with_url(image_source);
        let content_block = ContentBlock::from(image_block);

        let json = serde_json::to_string(&content_block).unwrap();
        let expected =
            r#"{"type":"image","source":{"type":"url","url":"https://example.com/image.jpg"}}"#;

        assert_eq!(json, expected);
    }
}