lellm-agent 0.4.9

Agent Runtime for LeLLM — ToolUseLoop, Executor, Fallback
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use lellm_agent::schemars::JsonSchema;
use lellm_agent::serde::Deserialize;
use lellm_agent::{
    AgentBuilder, ContextBudget, ContextCompactor, ExecutableTool, LocalCompactor, StaticCatalog,
    ToolArgs, ToolCategory, ToolExecutor, estimate_message, estimate_tokens,
};
use lellm_core::{ChatResponse, ContentBlock, Message, TokenUsage, ToolCall, ToolDefinition};
use lellm_derive::Tool;
use lellm_provider::{MockProvider, ResolvedModel};
use std::sync::Arc;

#[tokio::test]
async fn test_tool_use_loop_no_tool_calls() {
    let response = ChatResponse::new(
        vec![ContentBlock::text("hello")],
        TokenUsage::default(),
        serde_json::json!(null),
    );
    let provider = Arc::new(MockProvider::reply_with(response));

    let model = ResolvedModel {
        context_window: None,
        provider,
        model: "test-model".to_string(),
    };

    let messages = vec![Message::user_text("test")];

    let result = AgentBuilder::new(model)
        .max_iterations(5)
        .compile()
        .invoke(messages)
        .await
        .unwrap();

    assert_eq!(result.iterations, 1);
    assert!(!result.response.has_tool_calls());
}

#[tokio::test]
async fn test_tool_executor_snapshot_and_execute() {
    let def = ToolDefinition {
        name: "echo".to_string(),
        description: "echo tool".to_string(),
        parameters: serde_json::json!({
            "type": "object",
            "properties": {
                "text": { "type": "string" }
            }
        }),
        cache_control: None,
    };
    let reg = ExecutableTool::safe(def, |args: &serde_json::Value| {
        let text = args
            .get("text")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        async move { Ok(serde_json::json!(format!("echo: {}", text))) }
    });

    let catalog = StaticCatalog::from_tools(vec![reg]);
    let executor = ToolExecutor::with_catalog(Arc::new(catalog));

    let call = ToolCall {
        id: "1".to_string(),
        name: "echo".to_string(),
        arguments: serde_json::json!({"text": "hello"}),
    };

    let snapshot = executor.snapshot().await;
    let result = executor.execute_with_snapshot(&call, &snapshot).await;
    assert!(result.is_ok());
    let val = result.unwrap();
    assert_eq!(val, serde_json::json!("echo: hello"));
}

#[test]
fn test_tool_category() {
    assert_eq!(ToolCategory::FILE_IO.0, "file_io");
    assert_eq!(ToolCategory::NETWORK.0, "network");
    assert_eq!(ToolCategory::DATABASE.0, "database");
}

// ─── ToolArgs trait + derive(Tool) 测试 ───

#[derive(Deserialize, JsonSchema, Tool)]
#[tool(name = "weather_search", description = "搜索天气信息")]
#[allow(dead_code)]
struct WeatherArgs {
    /// 城市名称
    city: String,
    /// 单位(摄氏度/华氏度)
    unit: Option<String>,
    /// 是否包含预报
    include_forecast: bool,
}

#[test]
fn test_tool_args_trait() {
    assert_eq!(WeatherArgs::NAME, "weather_search");
    assert_eq!(WeatherArgs::DESCRIPTION, "搜索天气信息");
}

#[test]
fn test_tool_args_backward_compat() {
    assert_eq!(WeatherArgs::__name(), "weather_search");
    assert_eq!(WeatherArgs::__description(), "搜索天气信息");
}

#[test]
fn test_tool_definition_generation() {
    let def = WeatherArgs::tool_definition();

    assert_eq!(def.name, "weather_search");
    assert_eq!(def.description, "搜索天气信息");

    // 检查 Schema 结构
    let schema = &def.parameters;
    assert_eq!(schema.get("type").unwrap(), "object");

    let properties = schema.get("properties").unwrap().as_object().unwrap();
    assert_eq!(properties.len(), 3);

    // city — String → required + string
    assert_eq!(properties["city"]["type"], "string");
    assert_eq!(properties["city"]["description"], "城市名称");

    // unit — Option<String> → not required + ["string", "null"] (JSON Schema nullable)
    let unit_type = &properties["unit"]["type"];
    assert!(
        *unit_type == "string" || *unit_type == serde_json::json!(["string", "null"]),
        "expected string or [string, null], got {}",
        unit_type
    );
    assert_eq!(properties["unit"]["description"], "单位(摄氏度/华氏度)");

    // include_forecast — bool → required + boolean
    assert_eq!(properties["include_forecast"]["type"], "boolean");
    assert_eq!(
        properties["include_forecast"]["description"],
        "是否包含预报"
    );

    // 检查 required 字段
    let required = schema.get("required").unwrap().as_array().unwrap();
    assert!(required.iter().any(|v| v.as_str() == Some("city")));
    assert!(
        required
            .iter()
            .any(|v| v.as_str() == Some("include_forecast"))
    );
    // unit 是 Option,不应在 required 中
    assert!(!required.iter().any(|v| v.as_str() == Some("unit")));
}

#[test]
fn test_tool_args_schema_backward_compat() {
    let schema = WeatherArgs::schema();
    let def_schema = WeatherArgs::tool_definition().parameters;
    assert_eq!(schema, def_schema);
}

#[test]
fn test_tool_definition_default_name() {
    // 不指定 name,应自动转换为 snake_case
    #[derive(Deserialize, JsonSchema, Tool)]
    #[tool(description = "测试默认命名")]
    #[allow(dead_code)]
    struct MySearchTool {
        pub query: String,
    }

    assert_eq!(MySearchTool::NAME, "my_search_tool");
    assert_eq!(MySearchTool::__name(), "my_search_tool");
}

#[test]
fn test_option_type_inference() {
    // 验证 Option<T> 正确推导内部类型
    #[derive(Deserialize, JsonSchema, Tool)]
    #[tool(name = "typed_test", description = "测试类型推导")]
    #[allow(dead_code)]
    struct TypedTestArgs {
        /// 可选字符串
        opt_string: Option<String>,
        /// 可选整数
        opt_int: Option<i32>,
        /// 可选布尔
        opt_bool: Option<bool>,
        /// 可选浮点数
        opt_float: Option<f64>,
    }

    let def = TypedTestArgs::tool_definition();
    let properties = def
        .parameters
        .get("properties")
        .unwrap()
        .as_object()
        .unwrap();

    // Option<T> → [T, "null"] in JSON Schema (schemars standard)
    fn expect_type(actual: &serde_json::Value, expected: &str) {
        assert!(
            *actual == expected || *actual == serde_json::json!([expected, "null"]),
            "expected {} or [{} , \"null\"], got {}",
            expected,
            expected,
            actual
        );
    }
    expect_type(&properties["opt_string"]["type"], "string");
    expect_type(&properties["opt_int"]["type"], "integer");
    expect_type(&properties["opt_bool"]["type"], "boolean");
    expect_type(&properties["opt_float"]["type"], "number");

    // 全部是 Option,required 应为空
    let required = def.parameters.get("required");
    assert!(required.map_or(true, |r| r.as_array().map_or(true, |a| a.is_empty())));
}

// ─── Level 2: safe() 便捷方法测试 ───

#[derive(Deserialize, JsonSchema, Tool)]
#[tool(name = "greet_tool", description = "打招呼")]
struct GreetArgs {
    /// 名字
    name: String,
}

#[test]
fn test_tool_safe_method() {
    // 验证 safe() 方法正常工作
    let reg =
        GreetArgs::safe(
            |args| async move { Ok(serde_json::json!(format!("你好, {}!", args.name))) },
        );

    assert_eq!(reg.definition().name, "greet_tool");
    assert_eq!(reg.definition().description, "打招呼");
}

#[tokio::test]
async fn test_tool_safe_execution() {
    let reg =
        GreetArgs::safe(
            |args| async move { Ok(serde_json::json!(format!("你好, {}!", args.name))) },
        );

    let catalog = StaticCatalog::from_tools(vec![reg]);
    let executor = ToolExecutor::with_catalog(Arc::new(catalog));

    let call = ToolCall {
        id: "1".to_string(),
        name: "greet_tool".to_string(),
        arguments: serde_json::json!({"name": "世界"}),
    };

    let snapshot = executor.snapshot().await;
    let result = executor.execute_with_snapshot(&call, &snapshot).await;
    assert!(result.is_ok());
    let val = result.unwrap();
    assert_eq!(val, serde_json::json!("你好, 世界!"));
}

// ─── AgentBuilder 测试 ───

#[tokio::test]
async fn test_builder_basic_build() {
    let response = ChatResponse::new(
        vec![ContentBlock::text("done")],
        TokenUsage::default(),
        serde_json::json!(null),
    );
    let provider = Arc::new(MockProvider::reply_with(response));
    let model = ResolvedModel {
        context_window: None,
        provider,
        model: "test-model".to_string(),
    };

    let agent = AgentBuilder::new(model).compile();

    let messages = vec![Message::user_text("hello")];
    let result = agent.invoke(messages).await.unwrap();

    assert_eq!(result.iterations, 1);
    assert!(result.is_success());
}

#[tokio::test]
async fn test_builder_with_config() {
    let response = ChatResponse::new(
        vec![ContentBlock::text("done")],
        TokenUsage::default(),
        serde_json::json!(null),
    );
    let provider = Arc::new(MockProvider::reply_with(response));
    let model = ResolvedModel {
        context_window: None,
        provider,
        model: "test-model".to_string(),
    };

    let agent = AgentBuilder::new(model)
        .system("你是助手".to_string())
        .max_iterations(20)
        .compile();

    let messages = vec![Message::user_text("hello")];
    let result = agent.invoke(messages).await.unwrap();

    assert!(result.is_success());
}

#[tokio::test]
async fn test_builder_with_tool() {
    let def = ToolDefinition {
        name: "echo".to_string(),
        description: "echo tool".to_string(),
        parameters: serde_json::json!({
            "type": "object",
            "properties": { "msg": { "type": "string" } }
        }),
        cache_control: None,
    };

    let reg = ExecutableTool::safe(def, |args| {
        let m = args
            .get("msg")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        async move { Ok(serde_json::json!(format!("reply: {}", m))) }
    });

    let response = ChatResponse::new(
        vec![ContentBlock::text("ok")],
        TokenUsage::default(),
        serde_json::json!(null),
    );
    let provider = Arc::new(MockProvider::reply_with(response));
    let model = ResolvedModel {
        context_window: None,
        provider,
        model: "test".to_string(),
    };

    let _agent = AgentBuilder::new(model).tool(reg).compile();

    // 如果 build() 成功,说明 tool() 方法工作正常
}

#[test]
fn test_builder_chain_api() {
    // 测试链式调用 API 的编译正确性
    let response = ChatResponse::new(
        vec![ContentBlock::text("ok")],
        TokenUsage::default(),
        serde_json::json!(null),
    );
    let provider = Arc::new(MockProvider::reply_with(response));
    let model = ResolvedModel {
        context_window: None,
        provider,
        model: "test".to_string(),
    };

    let def = ToolDefinition {
        name: "test_tool".to_string(),
        description: "test".to_string(),
        parameters: serde_json::json!({"type": "object", "properties": {}}),
        cache_control: None,
    };
    let reg = ExecutableTool::safe(def, |_| async { Ok(serde_json::json!("done")) });

    // 完整链式调用
    let _agent = AgentBuilder::new(model)
        .system("你是测试助手".to_string())
        .tool(reg)
        .max_iterations(15)
        .compile();
}

// ─── 糖衣 API 测试 ───

#[tokio::test]
async fn test_create_agent() {
    let response = ChatResponse::new(
        vec![ContentBlock::text("hi")],
        TokenUsage::default(),
        serde_json::json!(null),
    );
    let provider = Arc::new(MockProvider::reply_with(response));
    let model = ResolvedModel {
        context_window: None,
        provider,
        model: "test".to_string(),
    };

    let agent = lellm_agent::create_agent(model);
    let messages = vec![Message::user_text("hello")];
    let result = agent.invoke(messages).await.unwrap();

    assert!(result.is_success());
}

#[tokio::test]
async fn test_create_agent_with_tools() {
    let def = ToolDefinition {
        name: "greet".to_string(),
        description: "greet someone".to_string(),
        parameters: serde_json::json!({
            "type": "object",
            "properties": { "name": { "type": "string" } }
        }),
        cache_control: None,
    };
    let reg = ExecutableTool::safe(def, |args| {
        let n = args
            .get("name")
            .and_then(|v| v.as_str())
            .unwrap_or("world")
            .to_string();
        async move { Ok(serde_json::json!(format!("hello {}", n))) }
    });

    let response = ChatResponse::new(
        vec![ContentBlock::text("done")],
        TokenUsage::default(),
        serde_json::json!(null),
    );
    let provider = Arc::new(MockProvider::reply_with(response));
    let model = ResolvedModel {
        context_window: None,
        provider,
        model: "test".to_string(),
    };

    let _agent = lellm_agent::create_agent_with_tools(model, vec![reg]);
    // 如果构建成功,API 即工作正常
}

#[tokio::test]
async fn test_create_agent_with_system() {
    let response = ChatResponse::new(
        vec![ContentBlock::text("ok")],
        TokenUsage::default(),
        serde_json::json!(null),
    );
    let provider = Arc::new(MockProvider::reply_with(response));
    let model = ResolvedModel {
        context_window: None,
        provider,
        model: "test".to_string(),
    };

    let agent = lellm_agent::create_agent_with_system(model, "你是助手".to_string());

    let messages = vec![Message::user_text("hi")];
    let result = agent.invoke(messages).await.unwrap();

    assert!(result.is_success());
}

#[test]
fn test_create_agent_full() {
    let response = ChatResponse::new(
        vec![ContentBlock::text("ok")],
        TokenUsage::default(),
        serde_json::json!(null),
    );
    let provider = Arc::new(MockProvider::reply_with(response));
    let model = ResolvedModel {
        context_window: None,
        provider,
        model: "test".to_string(),
    };

    let def = ToolDefinition {
        name: "t".to_string(),
        description: "test".to_string(),
        parameters: serde_json::json!({"type": "object", "properties": {}}),
        cache_control: None,
    };
    let reg = ExecutableTool::safe(def, |_| async { Ok(serde_json::json!("ok")) });

    let _agent = lellm_agent::create_agent_full(model, "你是助手".to_string(), vec![reg], 20);
}

// ─── 上下文预算管理测试 ───

#[test]
fn test_estimate_tokens_ascii() {
    // "Hello World" ≈ 11/4 ≈ 2 tokens + 4 overhead = ~6
    let msg = Message::user_text("Hello World");
    let tokens = estimate_message(&msg);
    assert!(tokens >= 4 && tokens <= 10);
}

#[test]
fn test_estimate_tokens_chinese() {
    // "你好世界" ≈ 4 * 1.5 = 6 tokens + 4 overhead = ~10
    let msg = Message::user_text("你好世界");
    let tokens = estimate_message(&msg);
    assert!(tokens >= 6 && tokens <= 15);
}

#[test]
fn test_estimate_tokens_empty() {
    let tokens = estimate_tokens(&[]);
    assert_eq!(tokens, 0);
}

#[test]
fn test_truncate_tool_result_short() {
    let budget = ContextBudget::default();
    let short = lellm_core::text_block("short result".to_string());
    let result = budget.truncate_tool_result_blocks(&short);
    assert_eq!(result.len(), 1);
}

#[test]
fn test_truncate_tool_result_long() {
    let budget = ContextBudget {
        max_tool_result_chars: 10,
        ..Default::default()
    };
    let long = lellm_core::text_block("0123456789ABCDEFG".to_string());
    let result = budget.truncate_tool_result_blocks(&long);
    assert!(result.len() >= 1);
    let text = result
        .iter()
        .filter_map(|b: &lellm_core::ContentBlock| b.as_text())
        .collect::<String>();
    assert!(text.starts_with("0123456789"));
    assert!(text.contains("[truncated"));
}

#[test]
fn test_should_compact() {
    let budget = ContextBudget {
        max_tokens: 1000,
        warning_ratio: 0.8,
        ..Default::default()
    };
    assert!(!budget.should_compact(500)); // 50%
    assert!(!budget.should_compact(799)); // 79.9%
    assert!(!budget.should_compact(800)); // 80% exactly, threshold is strict >
    assert!(budget.should_compact(801)); // 80.1%
    assert!(budget.should_compact(900)); // 90%
}

#[test]
fn test_local_compactor_no_op_when_under_limit() {
    let budget = ContextBudget {
        keep_recent_turns: 5,
        ..Default::default()
    };

    // 只有 2 个 turn,不需要压缩
    let messages = vec![
        Message::user_text("turn 1 user"),
        Message::assistant_text("turn 1 assistant"),
        Message::user_text("turn 2 user"),
        Message::assistant_text("turn 2 assistant"),
    ];

    let compactor = LocalCompactor::new();
    let result = compactor.compact(&messages, &budget);

    // 不需要压缩,原样返回
    assert_eq!(result.messages.len(), messages.len());
    assert_eq!(result.removed_messages, 0);
}

#[test]
fn test_local_compactor_compresses_old_turns() {
    let budget = ContextBudget {
        keep_recent_turns: 1,
        ..Default::default()
    };

    let mut messages = Vec::new();
    // 创建 3 个 turns(6 条消息)
    for i in 1..=3 {
        messages.push(Message::user(lellm_core::text_block(format!("user {}", i))));
        messages.push(Message::assistant(lellm_core::text_block(format!(
            "assistant {}",
            i
        ))));
    }

    let compactor = LocalCompactor::new();
    let result = compactor.compact(&messages, &budget);

    // 移除了旧 turns(6 条 → summary + 2 条 = 3 条)
    assert!(result.removed_messages > 0);
    assert!(result.messages.len() < messages.len());
    // 摘要消息应存在(System 角色)
    assert!(result
        .messages
        .iter()
        .any(|m| matches!(m, Message::System { content } if content.iter().any(|b| b.as_text().map(|t| t.contains("Compressed")).unwrap_or(false)))));
}