open-agent-sdk 0.6.4

Production-ready Rust SDK for building AI agents with local OpenAI-compatible servers (LMStudio, Ollama, llama.cpp, vLLM). Features streaming, tools, hooks, retry logic, and comprehensive examples.
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
//! Log Analyzer Agent - Intelligent log file analysis with pattern detection
//!
//! This agent analyzes application logs to identify errors, patterns, and anomalies.
//! It provides actionable insights to help debug production issues quickly.
//!
//! Usage:
//!     # Analyze a log file
//!     cargo run --example log_analyzer_agent /path/to/app.log
//!
//! Features:
//! - Automatic error and warning detection
//! - Pattern recognition across log entries
//! - Root cause suggestions via LLM
//! - Prioritized recommendations

use open_agent::{AgentOptions, Client, ContentBlock};
use regex::Regex;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};

/// Log entry with parsed fields
#[derive(Debug, Clone)]
struct LogEntry {
    raw: String,
    level: String,
    message: String,
    line_number: usize,
}

impl LogEntry {
    fn new(raw: String, level: String, message: String, line_number: usize) -> Self {
        Self {
            raw,
            level: level.to_uppercase(),
            message,
            line_number,
        }
    }
}

/// Parses log files
struct LogParser {
    // Common log pattern: timestamp, level, message
    pattern: Regex,
}

impl LogParser {
    fn new() -> Self {
        // Pattern matches: [2025-01-01T12:00:00] ERROR some message
        // Or: 2025-01-01 12:00:00 [ERROR] message
        // Or: ERROR: message
        let pattern = Regex::new(
            r"(?i)(?:\d{4}-\d{2}-\d{2}[T\s]\d{2}:\d{2}:\d{2}[^\s]*)?\s*(?:\[)?(?P<level>ERROR|FATAL|CRITICAL|WARN|WARNING|INFO|DEBUG)(?:\])?\s*:?\s*(?P<message>.*)"
        ).unwrap();

        Self { pattern }
    }

    fn parse_line(&self, line: &str, line_number: usize) -> Option<LogEntry> {
        let line = line.trim();
        if line.is_empty() {
            return None;
        }

        // Try JSON format
        if line.starts_with('{') && line.ends_with('}') {
            if let Ok(json_value) = serde_json::from_str::<Value>(line) {
                let level = json_value
                    .get("level")
                    .or_else(|| json_value.get("severity"))
                    .and_then(|v| v.as_str())
                    .unwrap_or("INFO");

                let message = json_value
                    .get("message")
                    .or_else(|| json_value.get("msg"))
                    .and_then(|v| v.as_str())
                    .unwrap_or(line);

                return Some(LogEntry::new(
                    line.to_string(),
                    level.to_string(),
                    message.to_string(),
                    line_number,
                ));
            }
        }

        // Try regex pattern
        if let Some(caps) = self.pattern.captures(line) {
            let level = caps.name("level").map(|m| m.as_str()).unwrap_or("INFO");
            let message = caps.name("message").map(|m| m.as_str()).unwrap_or(line);

            return Some(LogEntry::new(
                line.to_string(),
                level.to_string(),
                message.to_string(),
                line_number,
            ));
        }

        // Fallback: check for common error keywords
        let lower = line.to_lowercase();
        if lower.contains("error") || lower.contains("exception") || lower.contains("fatal") {
            return Some(LogEntry::new(
                line.to_string(),
                "ERROR".to_string(),
                line.to_string(),
                line_number,
            ));
        } else if lower.contains("warn") {
            return Some(LogEntry::new(
                line.to_string(),
                "WARNING".to_string(),
                line.to_string(),
                line_number,
            ));
        }

        // Default to INFO
        Some(LogEntry::new(
            line.to_string(),
            "INFO".to_string(),
            line.to_string(),
            line_number,
        ))
    }
}

/// Analyzes parsed logs
struct LogAnalyzer {
    entries: Vec<LogEntry>,
    errors: Vec<LogEntry>,
    warnings: Vec<LogEntry>,
}

impl LogAnalyzer {
    fn new(entries: Vec<LogEntry>) -> Self {
        let errors: Vec<LogEntry> = entries
            .iter()
            .filter(|e| matches!(e.level.as_str(), "ERROR" | "FATAL" | "CRITICAL"))
            .cloned()
            .collect();

        let warnings: Vec<LogEntry> = entries
            .iter()
            .filter(|e| matches!(e.level.as_str(), "WARN" | "WARNING"))
            .cloned()
            .collect();

        Self {
            entries,
            errors,
            warnings,
        }
    }

    fn get_summary(&self) -> Value {
        let total = self.entries.len();
        let error_rate = if total > 0 {
            (self.errors.len() as f64 / total as f64) * 100.0
        } else {
            0.0
        };

        // Find error patterns
        let error_patterns = self.find_error_patterns();

        json!({
            "total_entries": total,
            "errors": self.errors.len(),
            "warnings": self.warnings.len(),
            "error_rate": format!("{:.1}%", error_rate),
            "top_error_patterns": error_patterns,
        })
    }

    fn find_error_patterns(&self) -> Vec<(String, usize)> {
        if self.errors.is_empty() {
            return Vec::new();
        }

        let mut patterns: HashMap<String, usize> = HashMap::new();

        let exception_re = Regex::new(r"(\w+(?:Exception|Error))").unwrap();
        let file_re = Regex::new(r"(?:file|at)\s+([^\s:]+\.\w+)").unwrap();

        for error in &self.errors {
            let msg = &error.message;

            // Look for exception names
            if let Some(caps) = exception_re.captures(msg) {
                let exception = caps.get(1).unwrap().as_str().to_string();
                *patterns.entry(exception).or_insert(0) += 1;
                continue;
            }

            // Look for file paths
            if let Some(caps) = file_re.captures(msg) {
                let file = format!("File: {}", caps.get(1).unwrap().as_str());
                *patterns.entry(file).or_insert(0) += 1;
                continue;
            }

            // Generic classification
            let lower = msg.to_lowercase();
            if lower.contains("connection") {
                *patterns.entry("Connection Error".to_string()).or_insert(0) += 1;
            } else if lower.contains("timeout") {
                *patterns.entry("Timeout Error".to_string()).or_insert(0) += 1;
            } else if lower.contains("permission") || lower.contains("denied") {
                *patterns.entry("Permission Error".to_string()).or_insert(0) += 1;
            } else if lower.contains("not found") || lower.contains("404") {
                *patterns.entry("Not Found Error".to_string()).or_insert(0) += 1;
            } else {
                // Use first few words as pattern
                let words: Vec<&str> = msg.split_whitespace().take(5).collect();
                let pattern = words.join(" ");
                if !pattern.is_empty() {
                    *patterns.entry(pattern).or_insert(0) += 1;
                }
            }
        }

        // Convert to sorted vector
        let mut sorted_patterns: Vec<(String, usize)> = patterns.into_iter().collect();
        sorted_patterns.sort_by_key(|p| std::cmp::Reverse(p.1)); // Sort by count, descending
        sorted_patterns.into_iter().take(5).collect()
    }
}

/// Log Analyzer Agent
struct LogAnalyzerAgent {
    options: AgentOptions,
}

impl LogAnalyzerAgent {
    fn new(options: AgentOptions) -> Self {
        Self { options }
    }

    async fn analyze_logs(
        &self,
        log_file: &Path,
        analyzer: &LogAnalyzer,
    ) -> Result<String, Box<dyn std::error::Error>> {
        let summary = analyzer.get_summary();

        // Prepare context for LLM
        let mut context = format!(
            "Analyze these application logs from {} and provide actionable insights.\n\n",
            log_file.display()
        );
        context.push_str(&format!(
            "Log Summary:\n{}\n\n",
            serde_json::to_string_pretty(&summary)?
        ));

        context.push_str("Sample Errors (first 10):\n");
        for (i, error) in analyzer.errors.iter().take(10).enumerate() {
            let display = if error.raw.len() > 150 {
                format!("{}...", &error.raw[..150])
            } else {
                error.raw.clone()
            };
            context.push_str(&format!(
                "{}. Line {}: {}\n",
                i + 1,
                error.line_number,
                display
            ));
        }

        context.push_str(
            r#"

Based on this analysis, provide:
1. Main issues identified (prioritized by severity)
2. Root cause analysis for the top errors
3. Specific recommendations to fix each issue
4. Monitoring suggestions to prevent future issues

Be specific and actionable. If you see patterns like connection errors,
timeout issues, or permission problems, provide concrete steps to diagnose
and resolve them."#,
        );

        let mut client = Client::new(self.options.clone())?;
        client.send(&context).await?;

        let mut response = String::new();
        while let Some(block) = client.receive().await? {
            if let ContentBlock::Text(text) = block {
                response.push_str(&text.text);
            }
        }

        Ok(response)
    }

    async fn run(&self, log_file: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
        println!("📁 Analyzing log file: {}", log_file.display());
        println!("{}", "=".repeat(50));

        // Read log file
        let file = File::open(&log_file)?;
        let reader = BufReader::new(file);
        let lines: Vec<String> = reader.lines().collect::<Result<_, _>>()?;

        println!("📊 Parsing {} log lines...", lines.len());

        // Parse entries
        let parser = LogParser::new();
        let mut entries = Vec::new();

        for (line_number, line) in lines.iter().enumerate() {
            if let Some(entry) = parser.parse_line(line, line_number + 1) {
                entries.push(entry);
            }
        }

        if entries.is_empty() {
            println!("❌ No valid log entries found");
            return Ok(());
        }

        println!("✓ Parsed {} log entries", entries.len());

        // Analyze logs
        let analyzer = LogAnalyzer::new(entries);
        let summary = analyzer.get_summary();

        // Print summary
        println!("\n📈 Log Summary:");
        println!("  Total entries: {}", summary["total_entries"]);
        println!(
            "  Errors: {} ({})",
            summary["errors"], summary["error_rate"]
        );
        println!("  Warnings: {}", summary["warnings"]);

        if let Some(patterns) = summary["top_error_patterns"].as_array() {
            if !patterns.is_empty() {
                println!("\n🔴 Top Error Patterns:");
                for pattern in patterns {
                    if let Some(arr) = pattern.as_array() {
                        if let (Some(name), Some(count)) = (arr.first(), arr.get(1)) {
                            println!("  - {}: {} occurrences", name, count);
                        }
                    }
                }
            }
        }

        // Generate analysis
        println!("\n🤖 Generating intelligent analysis...");
        let analysis = self.analyze_logs(&log_file, &analyzer).await?;

        println!("\n{}", "=".repeat(50));
        println!("📋 ANALYSIS REPORT");
        println!("{}", "=".repeat(50));
        println!("{}", analysis);
        println!("{}", "=".repeat(50));

        Ok(())
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args: Vec<String> = env::args().collect();

    if args.len() < 2 {
        eprintln!("Usage: {} <log_file>", args[0]);
        eprintln!("\nExample:");
        eprintln!("  cargo run --example log_analyzer_agent /var/log/app.log");
        std::process::exit(1);
    }

    let log_file = PathBuf::from(&args[1]);

    if !log_file.exists() {
        eprintln!("❌ Log file not found: {}", log_file.display());
        std::process::exit(1);
    }

    // Configuration
    let options = AgentOptions::builder()
        .system_prompt(
            "You are an expert log analyzer and site reliability engineer. \
             You excel at finding patterns in logs, identifying root causes of errors, \
             and providing actionable recommendations. You understand various log formats \
             and can correlate events to find issues. Always be specific and practical \
             in your recommendations.",
        )
        .model("qwen3:8b")
        .base_url("http://localhost:11434/v1")
        .temperature(0.3) // Lower for consistent analysis
        .max_tokens(2000)
        .build()?;

    let agent = LogAnalyzerAgent::new(options);
    agent.run(log_file).await?;

    Ok(())
}