agentwerk 0.1.1

A minimal Rust crate that gives any application agentic capabilities.
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use serde_json::Value;

use crate::error::{AgenticError, Result};

use super::types::{ContentBlock, Message, ModelResponse, StopReason, StreamEvent, TokenUsage};
use super::r#trait::{CompletionRequest, LlmProvider, ToolChoice};

pub struct AnthropicProvider {
    api_key: String,
    base_url: String,
    client: reqwest::Client,
}

impl AnthropicProvider {
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            base_url: "https://api.anthropic.com".into(),
            client: reqwest::Client::new(),
        }
    }

    pub(crate) fn from_env() -> Result<(Self, String)> {
        use super::environment::{env_or, env_required};
        let provider = Self::new(env_required("ANTHROPIC_API_KEY")?)
            .base_url(env_or("ANTHROPIC_BASE_URL", "https://api.anthropic.com"));
        let model = env_or("ANTHROPIC_MODEL", "claude-sonnet-4-20250514");
        Ok((provider, model))
    }

    pub fn with_client(api_key: impl Into<String>, client: reqwest::Client) -> Self {
        Self {
            api_key: api_key.into(),
            base_url: "https://api.anthropic.com".into(),
            client,
        }
    }

    pub fn base_url(mut self, url: String) -> Self {
        self.base_url = url;
        self
    }

    fn api_headers(&self) -> Vec<(String, String)> {
        vec![
            ("x-api-key".into(), self.api_key.clone()),
            ("anthropic-version".into(), "2023-06-01".into()),
            ("content-type".into(), "application/json".into()),
        ]
    }

    fn serialize_request(&self, request: &CompletionRequest) -> Value {
        let messages = serialize_messages(&request.messages);
        let tools: Vec<Value> = request.tools.iter().map(serialize_tool_definition).collect();

        let mut body = serde_json::json!({
            "model": request.model,
            "system": request.system_prompt,
            "messages": messages,
        });

        if let Some(n) = request.max_tokens {
            body["max_tokens"] = Value::from(n);
        }

        if !tools.is_empty() {
            body["tools"] = Value::Array(tools);
        }
        if let Some(ref choice) = request.tool_choice {
            body["tool_choice"] = serialize_tool_choice(choice);
        }

        body
    }

    fn parse_response(&self, json: Value) -> Result<ModelResponse> {
        Ok(ModelResponse {
            content: parse_content(&json),
            stop_reason: parse_stop_reason(&json),
            usage: parse_usage(&json),
            model: json["model"].as_str().unwrap_or("unknown").to_string(),
        })
    }

    async fn send_request(&self, body: Value) -> Result<reqwest::Response> {
        let url = format!("{}/v1/messages", self.base_url);
        let mut req = self.client.post(&url).json(&body);
        for (k, v) in self.api_headers() {
            req = req.header(k, v);
        }
        let resp = req.send().await.map_err(|e| AgenticError::Api {
            message: e.to_string(),
            status: None,
            retryable: true,
            retry_after_ms: None,
        })?;

        super::check_http_error(resp).await
    }
}

impl LlmProvider for AnthropicProvider {
    fn prewarm(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
        Box::pin(async { super::r#trait::prewarm_connection(&self.client, &self.base_url).await })
    }

    fn complete(
        &self,
        request: CompletionRequest,
    ) -> Pin<Box<dyn Future<Output = Result<ModelResponse>> + Send + '_>> {
        let body = self.serialize_request(&request);

        Box::pin(async move {
            let resp = self.send_request(body).await?;
            let json: Value = resp.json().await.map_err(|e| AgenticError::Other(e.to_string()))?;
            self.parse_response(json)
        })
    }

    fn complete_streaming(
        &self,
        request: CompletionRequest,
        on_event: Arc<dyn Fn(StreamEvent) + Send + Sync>,
    ) -> Pin<Box<dyn Future<Output = Result<ModelResponse>> + Send + '_>> {
        let mut body = self.serialize_request(&request);
        body["stream"] = Value::Bool(true);

        Box::pin(async move {
            let resp = self.send_request(body).await?;
            stream_response(resp, &on_event).await
        })
    }
}

async fn stream_response(
    response: reqwest::Response,
    on_event: &Arc<dyn Fn(StreamEvent) + Send + Sync>,
) -> Result<ModelResponse> {
    use futures_util::StreamExt;
    use super::stream::{SseEvent, StreamParser};

    let mut parser = StreamParser::new();
    let mut model = String::from("unknown");
    let mut usage = TokenUsage::default();
    let mut stop_reason = StopReason::EndTurn;
    let mut content_blocks: Vec<ContentBlock> = Vec::new();

    // Per-block accumulators, keyed by content block index
    let mut texts: Vec<Option<String>> = Vec::new();
    let mut tool_ids: Vec<Option<String>> = Vec::new();
    let mut tool_names: Vec<Option<String>> = Vec::new();
    let mut tool_inputs: Vec<Option<String>> = Vec::new();

    let mut stream = response.bytes_stream();
    while let Some(chunk) = stream.next().await {
        let chunk = chunk.map_err(|e| AgenticError::Other(e.to_string()))?;

        for event in parser.push(&chunk) {
            match event {
                SseEvent::Done => {
                    on_event(StreamEvent::MessageDone);
                    continue;
                }
                SseEvent::Data(json) => {

            let event_type = json["type"].as_str().unwrap_or("");
            match event_type {
                "message_start" => {
                    model = json["message"]["model"]
                        .as_str()
                        .unwrap_or("unknown")
                        .to_string();
                    usage.input_tokens =
                        json["message"]["usage"]["input_tokens"].as_u64().unwrap_or(0);
                    usage.cache_read_input_tokens = json["message"]["usage"]
                        ["cache_read_input_tokens"]
                        .as_u64()
                        .unwrap_or(0);
                    usage.cache_creation_input_tokens = json["message"]["usage"]
                        ["cache_creation_input_tokens"]
                        .as_u64()
                        .unwrap_or(0);
                }
                "content_block_start" => {
                    let idx = json["index"].as_u64().unwrap_or(0) as usize;
                    let block = &json["content_block"];

                    // Grow accumulators to fit this index
                    while texts.len() <= idx {
                        texts.push(None);
                        tool_ids.push(None);
                        tool_names.push(None);
                        tool_inputs.push(None);
                    }

                    match block["type"].as_str().unwrap_or("") {
                        "tool_use" => {
                            tool_ids[idx] =
                                Some(block["id"].as_str().unwrap_or("").to_string());
                            tool_names[idx] =
                                Some(block["name"].as_str().unwrap_or("").to_string());
                            tool_inputs[idx] = Some(String::new());
                        }
                        _ => {
                            texts[idx] = Some(String::new());
                        }
                    }
                }
                "content_block_delta" => {
                    let idx = json["index"].as_u64().unwrap_or(0) as usize;
                    let delta = &json["delta"];

                    match delta["type"].as_str().unwrap_or("") {
                        "text_delta" => {
                            let text = delta["text"].as_str().unwrap_or("").to_string();
                            if let Some(Some(ref mut buf)) = texts.get_mut(idx) {
                                buf.push_str(&text);
                            }
                            on_event(StreamEvent::TextDelta { index: idx, text });
                        }
                        "input_json_delta" => {
                            let partial = delta["partial_json"].as_str().unwrap_or("");
                            if let Some(Some(ref mut buf)) = tool_inputs.get_mut(idx) {
                                buf.push_str(partial);
                            }
                            on_event(StreamEvent::InputJsonDelta {
                                index: idx,
                                partial_json: partial.to_string(),
                            });
                        }
                        _ => {}
                    }
                }
                "content_block_stop" => {
                    let idx = json["index"].as_u64().unwrap_or(0) as usize;

                    if let Some(Some(text)) = texts.get_mut(idx).map(|t| t.take()) {
                        content_blocks.push(ContentBlock::Text { text });
                    } else if let Some(Some(json_input)) =
                        tool_inputs.get_mut(idx).map(|t| t.take())
                    {
                        let input = serde_json::from_str(&json_input)
                            .unwrap_or(Value::Object(Default::default()));
                        content_blocks.push(ContentBlock::ToolUse {
                            id: tool_ids
                                .get_mut(idx)
                                .and_then(|t| t.take())
                                .unwrap_or_default(),
                            name: tool_names
                                .get_mut(idx)
                                .and_then(|t| t.take())
                                .unwrap_or_default(),
                            input,
                        });
                    }

                    on_event(StreamEvent::ContentBlockStop { index: idx });
                }
                "message_delta" => {
                    stop_reason = match json["delta"]["stop_reason"].as_str() {
                        Some("tool_use") => StopReason::ToolUse,
                        Some("max_tokens") => StopReason::MaxTokens,
                        _ => StopReason::EndTurn,
                    };
                    usage.output_tokens =
                        json["usage"]["output_tokens"].as_u64().unwrap_or(0);
                    on_event(StreamEvent::MessageDelta {
                        stop_reason: stop_reason.clone(),
                        usage: usage.clone(),
                    });
                }
                _ => {}
            }

                } // SseEvent::Data
            } // match event
        } // for event
    } // while chunk

    Ok(ModelResponse {
        content: content_blocks,
        stop_reason,
        usage,
        model,
    })
}

fn serialize_messages(messages: &[Message]) -> Vec<Value> {
    messages
        .iter()
        .filter_map(|msg| {
            let (role, content) = match msg {
                Message::System { .. } => return None,
                Message::User { content } => ("user", content),
                Message::Assistant { content } => ("assistant", content),
            };
            Some(serde_json::json!({
                "role": role,
                "content": serialize_content_blocks(content),
            }))
        })
        .collect()
}

fn serialize_content_blocks(blocks: &[ContentBlock]) -> Vec<Value> {
    blocks.iter().map(serialize_content_block).collect()
}

fn serialize_content_block(block: &ContentBlock) -> Value {
    match block {
        ContentBlock::Text { text } => {
            serde_json::json!({"type": "text", "text": text})
        }
        ContentBlock::ToolUse { id, name, input } => {
            serde_json::json!({"type": "tool_use", "id": id, "name": name, "input": input})
        }
        ContentBlock::ToolResult {
            tool_use_id,
            content,
            is_error,
        } => {
            serde_json::json!({"type": "tool_result", "tool_use_id": tool_use_id, "content": content, "is_error": is_error})
        }
    }
}

fn serialize_tool_definition(tool: &crate::tools::tool::ToolDefinition) -> Value {
    serde_json::json!({
        "name": tool.name,
        "description": tool.description,
        "input_schema": tool.input_schema,
    })
}

fn serialize_tool_choice(choice: &ToolChoice) -> Value {
    match choice {
        ToolChoice::Auto => serde_json::json!({"type": "auto"}),
        ToolChoice::Specific { name } => serde_json::json!({"type": "tool", "name": name}),
    }
}

fn parse_content(json: &Value) -> Vec<ContentBlock> {
    let Some(blocks) = json["content"].as_array() else {
        return Vec::new();
    };
    blocks
        .iter()
        .filter_map(|block| match block["type"].as_str()? {
            "text" => Some(ContentBlock::Text {
                text: block["text"].as_str().unwrap_or("").to_string(),
            }),
            "tool_use" => Some(ContentBlock::ToolUse {
                id: block["id"].as_str().unwrap_or("").to_string(),
                name: block["name"].as_str().unwrap_or("").to_string(),
                input: block["input"].clone(),
            }),
            _ => None,
        })
        .collect()
}

fn parse_stop_reason(json: &Value) -> StopReason {
    match json["stop_reason"].as_str().unwrap_or("end_turn") {
        "tool_use" => StopReason::ToolUse,
        "max_tokens" => StopReason::MaxTokens,
        _ => StopReason::EndTurn,
    }
}

fn parse_usage(json: &Value) -> TokenUsage {
    let usage = &json["usage"];
    TokenUsage {
        input_tokens: usage["input_tokens"].as_u64().unwrap_or(0),
        output_tokens: usage["output_tokens"].as_u64().unwrap_or(0),
        cache_read_input_tokens: usage["cache_read_input_tokens"].as_u64().unwrap_or(0),
        cache_creation_input_tokens: usage["cache_creation_input_tokens"]
            .as_u64()
            .unwrap_or(0),
    }
}

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

    fn simple_request() -> CompletionRequest {
        CompletionRequest {
            model: "test-model".into(),
            system_prompt: "You are helpful.".into(),
            messages: vec![Message::User {
                content: vec![ContentBlock::Text { text: "Hi".into() }],
            }],
            tools: vec![],
            max_tokens: Some(1024),
            tool_choice: None,
        }
    }

    fn provider() -> AnthropicProvider {
        AnthropicProvider::new("test-key")
    }

    #[test]
    fn serialize_request_sets_model_and_system() {
        let body = provider().serialize_request(&simple_request());
        assert_eq!(body["model"], "test-model");
        assert_eq!(body["system"], "You are helpful.");
        assert_eq!(body["max_tokens"], 1024);
    }

    #[test]
    fn serialize_request_excludes_system_from_messages() {
        let body = provider().serialize_request(&simple_request());
        let messages = body["messages"].as_array().unwrap();
        for msg in messages {
            assert_ne!(msg["role"], "system");
        }
    }

    #[test]
    fn serialize_request_includes_tool_choice() {
        let mut req = simple_request();
        req.tool_choice = Some(ToolChoice::Specific { name: "read_file".into() });
        let body = provider().serialize_request(&req);
        assert_eq!(body["tool_choice"]["type"], "tool");
        assert_eq!(body["tool_choice"]["name"], "read_file");
    }

    #[test]
    fn parse_response_extracts_text() {
        let json = serde_json::json!({
            "content": [{"type": "text", "text": "Hello!"}],
            "stop_reason": "end_turn",
            "usage": {"input_tokens": 10, "output_tokens": 5},
            "model": "claude-sonnet-4-20250514"
        });
        let resp = provider().parse_response(json).unwrap();
        assert_eq!(resp.content.len(), 1);
        assert!(matches!(&resp.content[0], ContentBlock::Text { text } if text == "Hello!"));
        assert_eq!(resp.stop_reason, StopReason::EndTurn);
        assert_eq!(resp.usage.input_tokens, 10);
    }

    #[test]
    fn parse_response_extracts_tool_use() {
        let json = serde_json::json!({
            "content": [{"type": "tool_use", "id": "t1", "name": "read", "input": {"path": "/tmp"}}],
            "stop_reason": "tool_use",
            "usage": {"input_tokens": 0, "output_tokens": 0},
            "model": "mock"
        });
        let resp = provider().parse_response(json).unwrap();
        assert_eq!(resp.stop_reason, StopReason::ToolUse);
        match &resp.content[0] {
            ContentBlock::ToolUse { name, input, .. } => {
                assert_eq!(name, "read");
                assert_eq!(input["path"], "/tmp");
            }
            other => panic!("Expected ToolUse, got {other:?}"),
        }
    }

    #[test]
    fn parse_response_empty_content() {
        let json = serde_json::json!({
            "content": [],
            "stop_reason": "end_turn",
            "usage": {"input_tokens": 0, "output_tokens": 0},
            "model": "mock"
        });
        let resp = provider().parse_response(json).unwrap();
        assert!(resp.content.is_empty());
    }

    #[test]
    fn parse_response_maps_stop_reasons() {
        for (reason, expected) in [
            ("end_turn", StopReason::EndTurn),
            ("tool_use", StopReason::ToolUse),
            ("max_tokens", StopReason::MaxTokens),
        ] {
            let json = serde_json::json!({
                "content": [], "stop_reason": reason,
                "usage": {"input_tokens": 0, "output_tokens": 0}, "model": "m"
            });
            assert_eq!(provider().parse_response(json).unwrap().stop_reason, expected);
        }
    }
}