agent-base 0.1.10

A lightweight Agent Runtime Kernel for building AI agents in 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
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
//! Quickstart Demo — Corresponding to the QUICKSTART.md tutorial
//!
//! A complete server health check Agent demonstrating:
//!   - Tool definitions (disk check, memory check, service restart)
//!   - Approval flow (ToolPolicy + ApprovalHandler)
//!   - Middleware (anti-hallucination nudge)
//!   - Real-time event stream
//!   - Multi-turn REPL conversation
//!
//! How to run:
//!   cp .env.example .env
//!   # Edit .env and fill in your API Key
//!   cargo run --example quickstart_demo

use std::io::{self, Write};
use std::sync::Arc;

use agent_base::{
    AgentBuilder, AgentError, AgentResult, ApprovalDecision, ApprovalHandler, ApprovalRequest,
    OpenAiClient, RiskLevel, RuntimeEvent, Tool, ToolContext, ToolControlFlow, ToolOutput,
    ToolPolicy,
};
use async_trait::async_trait;
use dotenvy::dotenv;
use serde_json::{Value, json};

// ============================================================================
// Tool definitions
// ============================================================================

/// Disk usage check tool
struct DiskCheckTool;

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

    fn definition(&self) -> Value {
        json!({
            "type": "function",
            "function": {
                "name": "check_disk",
                "description": "Check server disk usage. Returns used/total space and usage percentage.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "path": {
                            "type": "string",
                            "description": "Filesystem path to check (e.g. '/', '/home', '/var')"
                        }
                    },
                    "required": ["path"]
                }
            }
        })
    }

    async fn call(&self, args: &Value, _ctx: &ToolContext) -> AgentResult<ToolOutput> {
        let path = args["path"].as_str().unwrap_or("/");
        let output = format!(
            "Filesystem: {}\nTotal: 50G  Used: 32G  Available: 18G  Usage: 64%",
            path
        );
        Ok(ToolOutput {
            summary: output,
            raw: Some(json!({ "path": path, "used_gb": 32, "total_gb": 50, "percent": 64 })),
            control_flow: ToolControlFlow::Continue,
            truncation: None,
        })
    }
}

/// Memory usage check tool
struct MemCheckTool;

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

    fn definition(&self) -> Value {
        json!({
            "type": "function",
            "function": {
                "name": "check_memory",
                "description": "Check server memory usage. Returns used/total memory and usage percentage.",
                "parameters": {
                    "type": "object",
                    "properties": {}
                }
            }
        })
    }

    async fn call(&self, _args: &Value, _ctx: &ToolContext) -> AgentResult<ToolOutput> {
        Ok(ToolOutput {
            summary: "Total: 16G  Used: 12G  Available: 4G  Usage: 75%\nSwap: Total 4G  Used 512M"
                .into(),
            raw: Some(
                json!({ "total_gb": 16, "used_gb": 12, "percent": 75, "swap_total_gb": 4, "swap_used_gb": 0.5 }),
            ),
            control_flow: ToolControlFlow::Continue,
            truncation: None,
        })
    }
}

/// Service restart tool (sensitive operation, requires approval)
struct RestartServiceTool;

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

    fn definition(&self) -> Value {
        json!({
            "type": "function",
            "function": {
                "name": "restart_service",
                "description": "Restart a specified system service. This operation causes a brief service interruption and requires manual approval.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "service": {
                            "type": "string",
                            "description": "Service name (e.g. nginx, mysql, redis)"
                        }
                    },
                    "required": ["service"]
                }
            }
        })
    }

    async fn call(&self, args: &Value, _ctx: &ToolContext) -> AgentResult<ToolOutput> {
        let service = args["service"].as_str().unwrap_or("unknown");
        Ok(ToolOutput {
            summary: format!(
                "Service '{}' has been successfully restarted. Status: active (running)",
                service
            ),
            raw: Some(json!({ "service": service, "status": "restarted", "success": true })),
            control_flow: ToolControlFlow::Continue,
            truncation: None,
        })
    }
}

// ============================================================================
// Approval: ToolPolicy + ApprovalHandler
// ============================================================================

/// Approval policy: restart_service requires manual approval, others are auto-approved
struct HealthCheckPolicy;

#[async_trait]
impl ToolPolicy for HealthCheckPolicy {
    async fn evaluate_approval(&self, tool_name: &str, args: &Value) -> Option<ApprovalRequest> {
        match tool_name {
            "restart_service" => {
                let service = args
                    .get("service")
                    .and_then(Value::as_str)
                    .unwrap_or("unknown");
                Some(ApprovalRequest {
                    title: "Restart Service".into(),
                    message: format!(
                        "Allow restart of service '{}'? This will cause a brief interruption.",
                        service
                    ),
                    risk_level: RiskLevel::Sensitive,
                    action_key: Some(format!("restart:{}", service)),
                    raw: None,
                })
            }
            _ => None,
        }
    }

    fn before_call(&self, _tool_name: &str, _args: &Value, _ctx: &ToolContext) -> AgentResult<()> {
        Ok(())
    }

    fn after_call(
        &self,
        _tool_name: &str,
        _args: &Value,
        _result: &ToolOutput,
        _ctx: &ToolContext,
    ) -> AgentResult<()> {
        Ok(())
    }
}

/// CLI-based approval interaction
struct CliApproval;

#[async_trait]
impl ApprovalHandler for CliApproval {
    async fn approve(
        &self,
        request: ApprovalRequest,
        _cancel_token: tokio_util::sync::CancellationToken,
    ) -> AgentResult<ApprovalDecision> {
        println!();
        println!("⚠️  Approval Request: {}", request.title);
        println!("   Risk Level: {:?}", request.risk_level);
        println!("   Details: {}", request.message);

        loop {
            print!("   Choose [y=allow once / a=allow always / n=deny]: ");
            io::stdout()
                .flush()
                .map_err(|e| AgentError::internal(format!("flush stdout failed: {e}")))?;

            let mut input = String::new();
            match io::stdin().read_line(&mut input) {
                Ok(0) => {
                    println!("   [stdin EOF, defaulting to deny]");
                    return Ok(ApprovalDecision::Deny);
                }
                Ok(_) => {}
                Err(e) => {
                    return Err(AgentError::internal(format!("read stdin failed: {e}")));
                }
            }
            match input.trim().to_ascii_lowercase().as_str() {
                "y" | "yes" => return Ok(ApprovalDecision::AllowOnce),
                "a" | "always" => return Ok(ApprovalDecision::AllowAlways),
                "n" | "no" => return Ok(ApprovalDecision::Deny),
                "" => {
                    return Ok(ApprovalDecision::Deny);
                }
                _ => println!("   Invalid input, enter y / a / n"),
            }
        }
    }
}

// ============================================================================
// Event printing
// ============================================================================

struct CliEventPrinter {
    assistant_prefix_printed: bool,
}

impl CliEventPrinter {
    fn new() -> Self {
        Self {
            assistant_prefix_printed: false,
        }
    }

    fn handle(&mut self, event: RuntimeEvent) -> AgentResult<()> {
        match event {
            RuntimeEvent::TextDelta { text, .. } => {
                if !self.assistant_prefix_printed {
                    print!("Assistant > ");
                    self.assistant_prefix_printed = true;
                }
                print!("{}", text);
                io::stdout()
                    .flush()
                    .map_err(|e| AgentError::internal(format!("flush failed: {e}")))?;
            }
            RuntimeEvent::ThoughtDelta { text, .. } => {
                print!("\x1b[90m[Thought] {} \x1b[0m", text);
                io::stdout()
                    .flush()
                    .map_err(|e| AgentError::internal(format!("flush failed: {e}")))?;
            }
            RuntimeEvent::ToolCallStarted {
                tool_name,
                args_json,
                ..
            } => {
                self.finish();
                println!("[Tool Call] {} ({})", tool_name, args_json);
            }
            RuntimeEvent::ToolCallFinished {
                tool_name, summary, ..
            } => {
                self.finish();
                let display = if summary.len() > 300 {
                    format!("{}...", &summary[..300])
                } else {
                    summary.clone()
                };
                println!("[Tool Finish] {}", tool_name);
                println!("  -> {}", display);
            }
            RuntimeEvent::AwaitingApproval { .. } => {
                self.finish();
            }
            RuntimeEvent::RunFinished { .. } => {
                self.finish();
            }
            _ => {}
        }
        Ok(())
    }

    fn finish(&mut self) {
        if self.assistant_prefix_printed {
            println!();
            self.assistant_prefix_printed = false;
        }
    }
}

// ============================================================================
// System Prompt
// ============================================================================

const SYSTEM_PROMPT: &str = r#"You are a server health check assistant.

You have the following tools available:
- check_disk: Check disk usage at a specified path
- check_memory: Check memory usage
- restart_service: Restart a system service (requires approval)

When the user asks about server health, **you must call the tools** to retrieve data — do not fabricate numbers.
Keep your answers concise and report results in bullet points.
If usage is high, proactively alert the user and offer recommendations."#;

// ============================================================================
// Main
// ============================================================================

#[tokio::main]
async fn main() -> AgentResult<()> {
    dotenv().ok();

    let api_key = std::env::var("OPENAI_API_KEY")
        .or_else(|_| std::env::var("DASHSCOPE_API_KEY"))
        .map_err(|_| {
            AgentError::internal("Please set OPENAI_API_KEY or DASHSCOPE_API_KEY in your .env file")
        })?;

    let model = std::env::var("OPENAI_MODEL")
        .or_else(|_| std::env::var("DASHSCOPE_MODEL"))
        .unwrap_or_else(|_| "gpt-4o-mini".to_string());

    let base_url = std::env::var("OPENAI_BASE_URL")
        .or_else(|_| std::env::var("DASHSCOPE_BASE_URL"))
        .unwrap_or_else(|_| "https://api.openai.com/v1".to_string());

    let llm: Arc<OpenAiClient> =
        Arc::new(OpenAiClient::new(api_key, model.clone(), Some(base_url)));

    let runtime = AgentBuilder::new(llm)
        .system_prompt(SYSTEM_PROMPT)
        .enable_thought(false)
        .enable_thinking(false)
        .register_tool(DiskCheckTool)
        .register_tool(MemCheckTool)
        .register_tool(RestartServiceTool)
        .tool_policy(Arc::new(HealthCheckPolicy))
        .approval_handler(Arc::new(CliApproval))
        .build()?;

    let mut session_id = runtime.create_session().await;

    println!("╔══════════════════════════════════════════════════════╗");
    println!("║     agent-base Quickstart Demo (Health Check)       ║");
    println!("╠══════════════════════════════════════════════════════╣");
    println!("║  Model: {:<46} ║", model);
    println!("║                                                      ║");
    println!("║  Available tools:                                    ║");
    println!("║    · check_disk      Check disk usage                ║");
    println!("║    · check_memory    Check memory usage              ║");
    println!("║    · restart_service Restart service (needs approval)║");
    println!("║                                                      ║");
    println!("║  Try saying:                                         ║");
    println!("\"Check the disk\"");
    println!("\"How's the memory?\"");
    println!("\"Restart nginx\"");
    println!("\"Full server health check\"");
    println!("║                                                      ║");
    println!("║  Commands: exit=quit  reset=reset session            ║");
    println!("║             session=view chat history                ║");
    println!("╚══════════════════════════════════════════════════════╝");
    println!();

    loop {
        print!("User > ");
        io::stdout()
            .flush()
            .map_err(|e| AgentError::internal(format!("flush failed: {e}")))?;

        let mut input = String::new();
        match io::stdin().read_line(&mut input) {
            Ok(0) => {
                println!("Goodbye!");
                break;
            }
            Ok(_) => {}
            Err(e) => {
                return Err(AgentError::internal(format!("read stdin failed: {e}")));
            }
        }
        let input = input.trim().to_string();

        if input.is_empty() {
            continue;
        }
        if matches!(input.as_str(), "exit" | "quit") {
            println!("Goodbye!");
            break;
        }
        if input == "reset" {
            session_id = runtime.create_session().await;
            println!("Session reset\n");
            continue;
        }
        if input == "session" {
            if let Some(session) = runtime.session(&session_id).await {
                println!("\n--- Chat History ---");
                for msg in session.chat_messages() {
                    match msg {
                        agent_base::ChatMessage::System { content, .. } => {
                            println!("[System] {}...", &content[..content.len().min(80)]);
                        }
                        agent_base::ChatMessage::User { content, .. } => {
                            println!("[User] {}", content);
                        }
                        agent_base::ChatMessage::Assistant {
                            content,
                            tool_calls,
                            ..
                        } => {
                            if let Some(tc) = tool_calls {
                                println!(
                                    "[Assistant] Tool calls: {:?}",
                                    tc.iter()
                                        .map(|t| format!("{}({})", t.name, t.arguments))
                                        .collect::<Vec<_>>()
                                );
                            } else if let Some(c) = content {
                                let display = if c.len() > 120 {
                                    format!("{}...", &c[..120])
                                } else {
                                    c.clone()
                                };
                                println!("[Assistant] {}", display);
                            }
                        }
                        agent_base::ChatMessage::Tool {
                            tool_call_id,
                            content,
                            ..
                        } => {
                            let display = if content.len() > 120 {
                                format!("{}...", &content[..120])
                            } else {
                                content.clone()
                            };
                            println!("[Tool:{}] {}", tool_call_id, display);
                        }
                    }
                }
                println!("---------------\n");
            }
            continue;
        }

        let mut printer = CliEventPrinter::new();
        match runtime
            .run_turn(session_id.clone(), &input, |event| printer.handle(event))
            .await
        {
            Ok(_outcome) => {
                printer.finish();
            }
            Err(e) => {
                printer.finish();
                if e.is_cancelled() {
                    println!("[Cancelled]");
                } else {
                    eprintln!("[Error] {}", e);
                }
            }
        }
    }

    Ok(())
}