agent-works 0.1.0

Batteries-included Agent toolbox built on agent-base — MCP multi-server, Skills, built-in file tools, CLI REPL
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
use std::pin::Pin;
use std::sync::Arc;
use std::sync::Mutex;

use agent_works::{
    AgentBuilder, AgentEvent, AgentResult, ChatMessage, LlmCapabilities, LlmClient,
    ResponseFormat, RunOutcome, StreamChunk, Tool, ToolContext, ToolControlFlow, ToolOutput,
};
use async_trait::async_trait;
use futures_core::Stream;
use serde_json::{json, Value};

type ChunkStream = Pin<Box<dyn Stream<Item = AgentResult<StreamChunk>> + Send>>;

struct MockLlmClient {
    responses: Mutex<std::vec::IntoIter<Vec<StreamChunk>>>,
    call_count: Mutex<usize>,
}

impl MockLlmClient {
    fn new(scripted_responses: Vec<Vec<StreamChunk>>) -> Self {
        Self {
            responses: Mutex::new(scripted_responses.into_iter()),
            call_count: Mutex::new(0),
        }
    }

    fn call_count(&self) -> usize {
        *self.call_count.lock().unwrap()
    }
}

#[async_trait]
impl LlmClient for MockLlmClient {
    async fn chat(
        &self,
        _messages: &[ChatMessage],
        _tools: &[Value],
        _reasoning: Option<&agent_works::ReasoningConfig>,
        _response_format: Option<&ResponseFormat>,
    ) -> AgentResult<Value> {
        unimplemented!()
    }

    async fn chat_stream(
        &self,
        _messages: &[ChatMessage],
        _tools: &[Value],
        _reasoning: Option<&agent_works::ReasoningConfig>,
        _response_format: Option<&ResponseFormat>,
    ) -> AgentResult<ChunkStream> {
        *self.call_count.lock().unwrap() += 1;
        let chunks: Vec<AgentResult<StreamChunk>> = self
            .responses
            .lock()
            .unwrap()
            .next()
            .unwrap_or_default()
            .into_iter()
            .map(Ok)
            .collect();
        let stream = futures_util::stream::iter(chunks);
        Ok(Box::pin(stream))
    }

    fn capabilities(&self) -> LlmCapabilities {
        LlmCapabilities {
            supports_streaming: true,
            supports_tools: true,
            supports_vision: false,
            supports_thinking: false,
            max_context_tokens: None,
            max_output_tokens: None,
        }
    }
}

struct EchoTool;

#[async_trait]
impl Tool for EchoTool {
    fn name(&self) -> &'static str {
        "echo"
    }

    fn definition(&self) -> Value {
        json!({
            "type": "function",
            "function": {
                "name": "echo",
                "description": "echo back the message",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "message": { "type": "string" }
                    },
                    "required": ["message"]
                }
            }
        })
    }

    async fn call(&self, args: &Value, _ctx: &ToolContext) -> AgentResult<ToolOutput> {
        let msg = args["message"].as_str().unwrap_or("");
        Ok(ToolOutput {
            summary: format!("echo: {msg}"),
            raw: Some(json!({ "echo": msg })),
            control_flow: ToolControlFlow::Continue,
            truncation: None,
        })
    }
}

// ---------------------------------------------------------------------------
// Builder forwarding tests (without skill feature)
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_builder_forwarding_text_reply() {
    let llm = Arc::new(MockLlmClient::new(vec![vec![
        StreamChunk::Text("Hello, world!".to_string()),
        StreamChunk::Stop,
    ]]));

    let runtime = AgentBuilder::new(llm.clone())
        .system_prompt("You are a helpful assistant")
        .build()
        .unwrap();

    let session_id = runtime.create_session().await;
    let result = runtime.run_turn_stream(session_id.clone(), "Hi").await;
    assert!(result.is_ok(), "Expected ok, got: {result:?}");
    let (_events, outcome) = result.unwrap();
    assert_eq!(outcome, RunOutcome::Completed);
    assert_eq!(llm.call_count(), 1);
}

#[tokio::test]
async fn test_builder_forwarding_with_tool() {
    let llm = Arc::new(MockLlmClient::new(vec![
        vec![
            StreamChunk::ToolCall(json!({
                "delta": {
                    "tool_calls": [{
                        "id": "call_1",
                        "function": {
                            "name": "echo",
                            "arguments": "{\"message\": \"hello\"}"
                        }
                    }]
                }
            })),
            StreamChunk::Stop,
        ],
        vec![StreamChunk::Text("Done!".to_string()), StreamChunk::Stop],
    ]));

    let runtime = AgentBuilder::new(llm.clone())
        .register_tool(EchoTool)
        .build()
        .unwrap();

    let session_id = runtime.create_session().await;
    let result = runtime.run_turn_stream(session_id, "Echo hello").await;
    assert!(result.is_ok(), "Expected ok, got: {result:?}");
    assert_eq!(llm.call_count(), 2);
}

#[tokio::test]
async fn test_builder_forwarding_middleware() {
    use std::sync::atomic::{AtomicBool, Ordering};

    let triggered = Arc::new(AtomicBool::new(false));

    struct FlagMiddleware {
        flag: Arc<AtomicBool>,
    }

    #[async_trait]
    impl agent_works::Middleware for FlagMiddleware {
        async fn on_post_llm(
            &self,
            _ctx: &mut agent_works::PostLlmCtx,
        ) -> AgentResult<()> {
            self.flag.store(true, Ordering::SeqCst);
            Ok(())
        }
    }

    let llm = Arc::new(MockLlmClient::new(vec![vec![
        StreamChunk::Text("reply".to_string()),
        StreamChunk::Stop,
    ]]));

    let runtime = AgentBuilder::new(llm)
        .system_prompt("sys")
        .middleware(FlagMiddleware {
            flag: triggered.clone(),
        })
        .build()
        .unwrap();

    let session_id = runtime.create_session().await;
    let result = runtime.run_turn_stream(session_id, "test").await;
    assert!(result.is_ok());
    assert!(triggered.load(Ordering::SeqCst), "Middleware should be triggered");
}

// ---------------------------------------------------------------------------
// Builder forwarding - error recovery
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_builder_forwarding_error_recovery() {
    let llm = Arc::new(MockLlmClient::new(vec![vec![
        StreamChunk::ToolCall(json!({
            "delta": {
                "tool_calls": [{
                    "id": "call_1",
                    "function": {
                        "name": "echo",
                        "arguments": "{\"message\": \"test\"}"
                    }
                }]
            }
        })),
        StreamChunk::Stop,
    ]]));

    let runtime = AgentBuilder::new(llm)
        .register_tool(EchoTool)
        .tool_timeout(30_000)
        .max_tool_output_chars(4096)
        .language(agent_works::Language::Zh)
        .build()
        .unwrap();

    let session_id = runtime.create_session().await;
    let result = runtime.run_turn_stream(session_id, "test").await;
    assert!(result.is_ok());
}

// ---------------------------------------------------------------------------
// ToolEnforcementMiddleware tests (in agent-base, re-exported by agent-works)
// ---------------------------------------------------------------------------

#[tokio::test]
    async fn test_tool_enforcement_available_via_works() {
        let llm = Arc::new(MockLlmClient::new(vec![vec![
            StreamChunk::Text("I will do it...".to_string()),
            StreamChunk::Stop,
        ]]));

        let config = agent_works::ToolEnforcementConfig::default();
        let runtime = AgentBuilder::new(llm)
            .register_tool(EchoTool)
            .system_prompt("sys")
            .middleware(agent_works::ToolEnforcementMiddleware::new(config))
            .build()
            .unwrap();

        let session_id = runtime.create_session().await;
        let result = runtime.run_turn_stream(session_id, "do something").await;
        assert!(result.is_ok(), "Expected ok: {result:?}");
    }

// ---------------------------------------------------------------------------
// Skill feature tests
// ---------------------------------------------------------------------------

#[cfg(feature = "skill")]
mod skill_tests {
    use super::*;
    use agent_works::skill::{LazySkillPrompter, Skill, SkillPrompter};
    use std::sync::Arc;
    use serde_json::Value;

    struct AddTool;

    #[async_trait]
    impl Tool for AddTool {
        fn name(&self) -> &'static str {
            "add"
        }

        fn definition(&self) -> Value {
            json!({
                "type": "function",
                "function": {
                    "name": "add",
                    "description": "Calculate the sum of two integers",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "a": { "type": "integer", "description": "First addend" },
                            "b": { "type": "integer", "description": "Second addend" }
                        },
                        "required": ["a", "b"]
                    }
                }
            })
        }

        async fn call(&self, args: &Value, _ctx: &ToolContext) -> AgentResult<ToolOutput> {
            let a = args["a"].as_i64().unwrap_or(0);
            let b = args["b"].as_i64().unwrap_or(0);
            Ok(ToolOutput {
                summary: format!("{a} + {b} = {}", a + b),
                raw: Some(json!({ "result": a + b })),
                control_flow: ToolControlFlow::Break,
                truncation: None,
            })
        }
    }

    struct MathSkill;

    impl Skill for MathSkill {
        fn name(&self) -> &'static str {
            "math"
        }

        fn brief_description(&self) -> String {
            "Math: supports addition".to_string()
        }

        fn detailed_description(&self) -> String {
            "## Math Skill\n\n- **add**: Calculate the sum of two integers".to_string()
        }

        fn tools(&self) -> Vec<Arc<dyn Tool>> {
            vec![Arc::new(AddTool)]
        }
    }

    #[tokio::test]
    async fn test_register_skill_with_builder() {
        let llm = Arc::new(super::MockLlmClient::new(vec![vec![
            StreamChunk::ToolCall(json!({
                "delta": {
                    "tool_calls": [{
                        "id": "call_1",
                        "function": {
                            "name": "add",
                            "arguments": "{\"a\": 1, \"b\": 2}"
                        }
                    }]
                }
            })),
            StreamChunk::Stop,
        ]]));

        let runtime = AgentBuilder::new(llm)
            .system_prompt("You are a math assistant")
            .register_skill(MathSkill)
            .build()
            .unwrap();

        let session_id = runtime.create_session().await;
        let result = runtime.run_turn_stream(session_id, "1+2=?").await;
        assert!(result.is_ok(), "Expected ok, got: {result:?}");

        let (events, _outcome) = result.unwrap();
        let tool_done = events.iter().any(|e| {
            matches!(e, AgentEvent::ToolCallFinished { tool_name, .. } if tool_name == "add")
        });
        assert!(tool_done, "add tool should be called");
    }

    #[tokio::test]
    async fn test_skill_disable_prompt_injection() {
        let llm = Arc::new(super::MockLlmClient::new(vec![vec![
            StreamChunk::ToolCall(json!({
                "delta": {
                    "tool_calls": [{
                        "id": "call_1",
                        "function": {
                            "name": "add",
                            "arguments": "{\"a\": 3, \"b\": 4}"
                        }
                    }]
                }
            })),
            StreamChunk::Stop,
        ]]));

        let runtime = AgentBuilder::new(llm)
            .system_prompt("My custom prompt")
            .register_skill(MathSkill)
            .disable_skill_prompt_injection()
            .build()
            .unwrap();

        let session_id = runtime.create_session().await;
        let result = runtime.run_turn_stream(session_id, "3+4=?").await;
        assert!(result.is_ok(), "Expected ok, got: {result:?}");
    }

    #[tokio::test]
    async fn test_skill_custom_detail_tool_name() {
        let llm = Arc::new(super::MockLlmClient::new(vec![vec![
            StreamChunk::ToolCall(json!({
                "delta": {
                    "tool_calls": [{
                        "id": "call_1",
                        "function": {
                            "name": "skill_info",
                            "arguments": "{\"name\": \"math\"}"
                        }
                    }]
                }
            })),
            StreamChunk::Stop,
        ]]));

        let runtime = AgentBuilder::new(llm)
            .system_prompt("sys")
            .register_skill(MathSkill)
            .skill_detail_tool_name("skill_info")
            .build()
            .unwrap();

        let session_id = runtime.create_session().await;
        let result = runtime.run_turn_stream(session_id, "tell me about math skill").await;
        assert!(result.is_ok(), "Expected ok, got: {result:?}");

        let (events, _outcome) = result.unwrap();
        let skill_loaded = events.iter().any(|e| {
            matches!(e, AgentEvent::ToolCallFinished { tool_name, summary, .. }
                if tool_name == "skill_info" && summary.contains("Math Skill"))
        });
        assert!(skill_loaded, "skill_info tool should return Math Skill detail");
    }

    #[tokio::test]
    async fn test_skill_tool_name_conflict() {
        let llm = Arc::new(super::MockLlmClient::new(vec![]));

        let result = AgentBuilder::new(llm)
            .register_tool(AddTool) // registers "add" directly
            .register_skill(MathSkill) // MathSkill also registers "add"
            .build();

        assert!(result.is_err(), "Tool name conflict should be detected");
        let err_msg = match result {
            Err(e) => e.to_string(),
            Ok(_) => panic!("Expected error"),
        };
        assert!(
            err_msg.contains("Tool name conflict"),
            "Error should mention tool name conflict: {err_msg}"
        );
    }

    #[test]
    fn test_lazy_skill_prompter() {
        let skills: Vec<Arc<dyn Skill>> = vec![Arc::new(MathSkill)];
        let prompter = LazySkillPrompter::new();
        let prompt = prompter.build_prompt(&skills);
        assert!(prompt.contains("math"), "Prompt should contain skill name");
        assert!(prompt.contains("get_skill_detail"), "Prompt should contain instruction");
    }

    #[test]
    fn test_lazy_skill_prompter_custom_config() {
        let skills: Vec<Arc<dyn Skill>> = vec![Arc::new(MathSkill)];
        let prompter = LazySkillPrompter::new()
            .title("## My Skills")
            .instruction("> Use my_get_detail to see details")
            .item_prefix("+ ");
        let prompt = prompter.build_prompt(&skills);
        assert!(prompt.contains("## My Skills"));
        assert!(prompt.contains("my_get_detail"));
        assert!(prompt.contains("+ "));
    }

    #[test]
    fn test_full_detail_prompter() {
        let skills: Vec<Arc<dyn Skill>> = vec![Arc::new(MathSkill)];
        let prompter = agent_works::skill::FullDetailPrompter;
        let prompt = prompter.build_prompt(&skills);
        assert!(prompt.contains("math"), "Should contain skill name");
        assert!(prompt.contains("Math Skill"), "Should contain detailed description");
    }

    #[test]
    fn test_skill_default_methods() {
        assert_eq!(MathSkill.version(), "0.1.0");
        assert!(MathSkill.tags().is_empty());
        assert_eq!(MathSkill.author(), "");
    }
}

// ---------------------------------------------------------------------------
// Builtin tools tests
// ---------------------------------------------------------------------------

#[cfg(feature = "builtin-tools")]
mod builtin_tests {
    use super::*;
    use agent_works::builtin::*;
    use std::path::PathBuf;

    #[tokio::test]
    async fn test_read_file_builtin() {
        let tool = ReadFileTool {
            workspace: PathBuf::from("."),
        };
        assert_eq!(tool.name(), "read_file");

        let def = tool.definition();
        let func = def.get("function").unwrap();
        assert_eq!(func.get("name").unwrap().as_str().unwrap(), "read_file");
    }

    #[tokio::test]
    async fn test_write_file_builtin() {
        let tool = WriteFileTool {
            workspace: PathBuf::from("."),
        };
        assert_eq!(tool.name(), "write_file");
    }

    #[tokio::test]
    async fn test_list_directory_builtin() {
        let tool = ListDirectoryTool {
            workspace: PathBuf::from("."),
        };
        assert_eq!(tool.name(), "list_directory");
    }

    #[tokio::test]
    async fn test_file_exists_builtin() {
        let tool = FileExistsTool {
            workspace: PathBuf::from("."),
        };
        assert_eq!(tool.name(), "file_exists");
    }

    #[tokio::test]
    async fn test_search_replace_builtin() {
        let tool = SearchReplaceTool {
            workspace: PathBuf::from("."),
        };
        assert_eq!(tool.name(), "search_replace");
    }
}

// ---------------------------------------------------------------------------
// MCP module tests
// ---------------------------------------------------------------------------

#[cfg(feature = "mcp")]
mod mcp_tests {
    use super::*;
    use agent_works::mcp::*;

    #[test]
    fn test_mcp_tool_info_creation() {
        let info = McpToolInfo {
            name: "test_tool".to_string(),
            description: "A test tool".to_string(),
            input_schema: json!({"type": "object"}),
        };
        assert_eq!(info.name, "test_tool");
        assert_eq!(info.description, "A test tool");
    }

    #[test]
    fn test_mcp_transport_variants() {
        let http = McpTransport::Http {
            url: "http://localhost:8080".to_string(),
        };
        assert!(matches!(http, McpTransport::Http { .. }));

        let stdio = McpTransport::Stdio {
            command: "npx".to_string(),
            args: vec!["-y".to_string(), "mcp-server".to_string()],
        };
        assert!(matches!(stdio, McpTransport::Stdio { .. }));
    }

    #[test]
    fn test_mcp_server_config() {
        let config = McpServerConfig {
            name: "my-server".to_string(),
            transport: McpTransport::Http {
                url: "http://localhost:8080".to_string(),
            },
            auto_reconnect: true,
        };
        assert_eq!(config.name, "my-server");
        assert!(config.auto_reconnect);
    }
}

// ---------------------------------------------------------------------------
// Skill detail tool standalone tests
// ---------------------------------------------------------------------------

#[cfg(feature = "skill")]
#[tokio::test]
async fn test_skill_detail_tool_standalone() {
    use agent_works::skill::{Skill, SkillDetailTool};
    use std::sync::Arc;

    struct SimpleSkill;
    impl Skill for SimpleSkill {
        fn name(&self) -> &'static str {
            "simple"
        }
        fn brief_description(&self) -> String {
            "A simple skill".to_string()
        }
        fn detailed_description(&self) -> String {
            "Detailed info about simple skill".to_string()
        }
        fn tools(&self) -> Vec<Arc<dyn Tool>> {
            vec![]
        }
    }

    let skills: Vec<Arc<dyn Skill>> = vec![Arc::new(SimpleSkill)];
    let detail_tool = SkillDetailTool::new(skills, "get_skill_detail".to_string());

    assert_eq!(detail_tool.name(), "get_skill_detail");

    let def = detail_tool.definition();
    let func = def.get("function").unwrap();
    assert_eq!(func.get("name").unwrap().as_str().unwrap(), "get_skill_detail");
}