radkit 0.0.5

Rust AI Agent Development Kit
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
//! Integration tests for tool macro with LlmWorker.
//!
//! These tests recreate the worker_execution.rs tests but using the #[tool] macro
//! instead of manual FunctionTool construction, demonstrating the macro's value
//! in real-world usage.

use radkit::agent::LlmWorker;
use radkit::macros::{tool, LLMOutput};
use radkit::models::{Content, ContentPart, LlmResponse, Thread, TokenUsage};
use radkit::test_support::{structured_response, FakeLlm};
use radkit::tools::{BaseTool, ToolCall, ToolContext, ToolResult};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::json;

// ============================================================================
// Test 1: Basic weather tool using macro
// ============================================================================

#[derive(Debug, PartialEq, Deserialize, LLMOutput, Serialize, JsonSchema)]
struct WeatherReport {
    location: String,
    temperature: f64,
    condition: String,
}

#[derive(Deserialize, LLMOutput, JsonSchema)]
struct GetWeatherArgs {
    location: String,
}

#[tool(description = "Get current weather")]
async fn get_weather(args: GetWeatherArgs) -> ToolResult {
    ToolResult::success(json!({
        "temperature": 72.5,
        "condition": "sunny",
        "location": args.location
    }))
}

#[tokio::test]
async fn test_llm_worker_with_macro_tool() {
    // First response: LLM calls the weather tool
    let tool_call_response = LlmResponse::new(
        Content::from_parts(vec![ContentPart::ToolCall(ToolCall::new(
            "call-1",
            "get_weather",
            json!({"location": "Seattle"}),
        ))]),
        TokenUsage::empty(),
    );

    // Second response: LLM returns structured output after seeing tool result
    let final_response = WeatherReport {
        location: "Seattle".to_string(),
        temperature: 72.5,
        condition: "sunny".to_string(),
    };

    // Create worker with macro-generated tool
    let worker_llm = FakeLlm::with_responses(
        "fake-llm",
        [
            Ok(tool_call_response),
            Ok(structured_response(&final_response)),
        ],
    );

    let worker = LlmWorker::<WeatherReport>::builder(worker_llm)
        .with_tool(get_weather)
        .build();

    let thread = Thread::from_user("What's the weather in Seattle?");
    let report = worker.run(thread).await.unwrap();

    assert_eq!(report.location, "Seattle");
    assert_eq!(report.temperature, 72.5);
    assert_eq!(report.condition, "sunny");
}

// ============================================================================
// Test 2: Multiple tools with macro
// ============================================================================

#[derive(Debug, PartialEq, Deserialize, LLMOutput, Serialize, JsonSchema)]
struct CalculationResult {
    result: f64,
    steps: Vec<String>,
}

#[derive(Deserialize, LLMOutput, JsonSchema)]
struct MathArgs {
    a: f64,
    b: f64,
}

#[tool(description = "Add two numbers")]
async fn add(args: MathArgs) -> ToolResult {
    ToolResult::success(json!(args.a + args.b))
}

#[tool(description = "Multiply two numbers")]
async fn multiply(args: MathArgs) -> ToolResult {
    ToolResult::success(json!(args.a * args.b))
}

#[tokio::test]
async fn test_llm_worker_multiple_macro_tools() {
    // Simulate: (2 + 3) * 4 = 20
    // First call: add(2, 3) = 5
    let response1 = LlmResponse::new(
        Content::from_parts(vec![ContentPart::ToolCall(ToolCall::new(
            "call-1",
            "add",
            json!({"a": 2.0, "b": 3.0}),
        ))]),
        TokenUsage::empty(),
    );

    // Second call: multiply(5, 4) = 20
    let response2 = LlmResponse::new(
        Content::from_parts(vec![ContentPart::ToolCall(ToolCall::new(
            "call-2",
            "multiply",
            json!({"a": 5.0, "b": 4.0}),
        ))]),
        TokenUsage::empty(),
    );

    // Final response: structured output
    let final_result = CalculationResult {
        result: 20.0,
        steps: vec![
            "add(2, 3) = 5".to_string(),
            "multiply(5, 4) = 20".to_string(),
        ],
    };

    let llm = FakeLlm::with_responses(
        "fake-llm",
        [
            Ok(response1),
            Ok(response2),
            Ok(structured_response(&final_result)),
        ],
    );

    let worker = LlmWorker::<CalculationResult>::builder(llm)
        .with_tool(add)
        .with_tool(multiply)
        .build();

    let thread = Thread::from_user("Calculate: (2 + 3) * 4");
    let result = worker.run(thread).await.unwrap();

    assert_eq!(result.result, 20.0);
    assert_eq!(result.steps.len(), 2);
}

// ============================================================================
// Test 3: Tool with ToolContext using macro
// ============================================================================

#[derive(Debug, Deserialize, LLMOutput, Serialize, JsonSchema)]
struct StateResult {
    saved: bool,
    value: String,
}

#[derive(Deserialize, LLMOutput, JsonSchema)]
struct SaveStateArgs {
    key: String,
    value: String,
}

#[tool(description = "Save state")]
async fn save_state(args: SaveStateArgs, ctx: &ToolContext<'_>) -> ToolResult {
    ctx.state().set_state(&args.key, json!(args.value));
    ToolResult::success(json!({"saved": true, "key": args.key}))
}

#[tool(description = "Get state")]
async fn get_state(args: GetStateArgs, ctx: &ToolContext<'_>) -> ToolResult {
    let value = ctx.state().get_state(&args.key);
    match value {
        Some(val) => ToolResult::success(json!({"found": true, "value": val})),
        None => ToolResult::success(json!({"found": false})),
    }
}

#[derive(Deserialize, LLMOutput, JsonSchema)]
struct GetStateArgs {
    key: String,
}

#[tokio::test]
async fn test_llm_worker_with_context_aware_tools() {
    // First call: save state
    let save_call = LlmResponse::new(
        Content::from_parts(vec![ContentPart::ToolCall(ToolCall::new(
            "call-1",
            "save_state",
            json!({"key": "user_name", "value": "Alice"}),
        ))]),
        TokenUsage::empty(),
    );

    // Second call: get state
    let get_call = LlmResponse::new(
        Content::from_parts(vec![ContentPart::ToolCall(ToolCall::new(
            "call-2",
            "get_state",
            json!({"key": "user_name"}),
        ))]),
        TokenUsage::empty(),
    );

    // Final response
    let final_response = StateResult {
        saved: true,
        value: "Alice".to_string(),
    };

    let llm = FakeLlm::with_responses(
        "fake-llm",
        [
            Ok(save_call),
            Ok(get_call),
            Ok(structured_response(&final_response)),
        ],
    );

    let worker = LlmWorker::<StateResult>::builder(llm)
        .with_tool(save_state)
        .with_tool(get_state)
        .build();

    let thread = Thread::from_user("Save 'Alice' as user_name and retrieve it");
    let result = worker.run(thread).await.unwrap();

    assert!(result.saved);
    assert_eq!(result.value, "Alice");
}

// ============================================================================
// Test 4: Function name becomes tool name
// ============================================================================

#[derive(Deserialize, LLMOutput, JsonSchema)]
struct ApiArgs {
    endpoint: String,
}

#[tool(description = "Fetch data from API")]
async fn fetch_api(args: ApiArgs) -> ToolResult {
    ToolResult::success(json!({
        "endpoint": args.endpoint,
        "data": "mock data"
    }))
}

#[tokio::test]
async fn test_macro_function_name_as_tool_name() {
    let tool = &fetch_api as &dyn BaseTool;
    assert_eq!(tool.name(), "fetch_api");
    assert_eq!(tool.description(), "Fetch data from API");
}

// ============================================================================
// Test 5: Optional parameters with defaults
// ============================================================================

fn default_limit() -> usize {
    10
}

#[derive(Deserialize, LLMOutput, JsonSchema)]
struct SearchArgs {
    query: String,
    #[serde(default = "default_limit")]
    limit: usize,
}

#[tool(description = "Search with optional limit")]
async fn search(args: SearchArgs) -> ToolResult {
    ToolResult::success(json!({
        "query": args.query,
        "limit": args.limit,
        "results": vec!["result1", "result2"]
    }))
}

#[derive(Debug, Deserialize, LLMOutput, Serialize, JsonSchema)]
struct SearchResult {
    query: String,
    count: usize,
}

#[tokio::test]
async fn test_macro_tool_with_optional_params() {
    // Test with explicit limit
    let call1 = LlmResponse::new(
        Content::from_parts(vec![ContentPart::ToolCall(ToolCall::new(
            "call-1",
            "search",
            json!({"query": "rust", "limit": 5}),
        ))]),
        TokenUsage::empty(),
    );

    // Test with default limit
    let call2 = LlmResponse::new(
        Content::from_parts(vec![ContentPart::ToolCall(ToolCall::new(
            "call-2",
            "search",
            json!({"query": "async"}),
        ))]),
        TokenUsage::empty(),
    );

    let final_response = SearchResult {
        query: "async".to_string(),
        count: 2,
    };

    let llm = FakeLlm::with_responses(
        "fake-llm",
        [
            Ok(call1),
            Ok(call2),
            Ok(structured_response(&final_response)),
        ],
    );

    let worker = LlmWorker::<SearchResult>::builder(llm)
        .with_tool(search)
        .build();

    let thread = Thread::from_user("Search for rust then async");
    let result = worker.run(thread).await.unwrap();

    assert_eq!(result.query, "async");
    assert_eq!(result.count, 2);
}

// ============================================================================
// Test 6: Complex nested structures
// ============================================================================

#[derive(Clone, Deserialize, LLMOutput, JsonSchema)]
struct Address {
    #[allow(dead_code)] // Required for JSON deserialization but not accessed in test assertions
    street: String,
    city: String,
    country: String,
}

#[derive(Deserialize, LLMOutput, JsonSchema)]
struct CreateUserArgs {
    name: String,
    age: u32,
    address: Address,
}

#[tool(description = "Create user with nested address")]
async fn create_user(args: CreateUserArgs) -> ToolResult {
    ToolResult::success(json!({
        "user_id": "u123",
        "name": args.name,
        "age": args.age,
        "city": args.address.city,
        "country": args.address.country
    }))
}

#[derive(Debug, Deserialize, LLMOutput, Serialize, JsonSchema)]
struct UserCreated {
    user_id: String,
    success: bool,
}

#[tokio::test]
async fn test_macro_tool_with_nested_structures() {
    let tool_call = LlmResponse::new(
        Content::from_parts(vec![ContentPart::ToolCall(ToolCall::new(
            "call-1",
            "create_user",
            json!({
                "name": "Alice",
                "age": 30,
                "address": {
                    "street": "123 Main St",
                    "city": "Seattle",
                    "country": "USA"
                }
            }),
        ))]),
        TokenUsage::empty(),
    );

    let final_response = UserCreated {
        user_id: "u123".to_string(),
        success: true,
    };

    let llm = FakeLlm::with_responses(
        "fake-llm",
        [Ok(tool_call), Ok(structured_response(&final_response))],
    );

    let worker = LlmWorker::<UserCreated>::builder(llm)
        .with_tool(create_user)
        .build();

    let thread = Thread::from_user("Create user Alice in Seattle");
    let result = worker.run(thread).await.unwrap();

    assert_eq!(result.user_id, "u123");
    assert!(result.success);
}

// ============================================================================
// Test 7: Verify tool declarations are correct
// ============================================================================

#[tokio::test]
async fn test_macro_tool_declarations() {
    let add_tool = &add as &dyn BaseTool;
    let declaration = add_tool.declaration();

    // Verify name and description
    assert_eq!(declaration.name(), "add");
    assert_eq!(declaration.description(), "Add two numbers");

    // Verify schema structure
    let schema = declaration.parameters();
    assert!(schema.is_object());

    let properties = schema.get("properties").expect("Should have properties");
    assert!(properties.is_object());

    // Verify parameter 'a' exists
    let props = properties.as_object().unwrap();
    assert!(props.contains_key("a"), "Should have parameter 'a'");
    assert!(props.contains_key("b"), "Should have parameter 'b'");

    // Verify required fields
    let required = schema.get("required").expect("Should have required");
    assert!(required.is_array());
    let req_arr = required.as_array().unwrap();
    assert!(
        req_arr.iter().any(|v| v.as_str() == Some("a")),
        "Should require 'a'"
    );
    assert!(
        req_arr.iter().any(|v| v.as_str() == Some("b")),
        "Should require 'b'"
    );
}

// ============================================================================
// Test 8: Error handling with invalid arguments
// ============================================================================

#[tokio::test]
async fn test_macro_tool_invalid_arguments() {
    use radkit::tools::DefaultExecutionState;

    let tool = &add as &dyn BaseTool;
    let state = DefaultExecutionState::new();
    let ctx = ToolContext::builder()
        .with_state(&state)
        .build()
        .expect("context");

    // Missing required parameter 'b'
    let result = tool
        .run_async(
            std::collections::HashMap::from([("a".to_string(), json!(5.0))]),
            &ctx,
        )
        .await;

    assert!(!result.is_success());
    assert!(result
        .error_message()
        .unwrap()
        .contains("missing field `b`"));
}