rrag 0.1.0-alpha.2

High-performance Rust framework for Retrieval-Augmented Generation with pluggable components, async-first design, and comprehensive observability
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! # RRAG Tools System
//!
//! Type-safe tool system leveraging Rust's trait system for zero-cost abstractions.
//! Designed for async execution with proper error handling and resource management.

use crate::{RragError, RragResult};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;

/// Tool execution result with comprehensive metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResult {
    /// Whether the tool executed successfully
    pub success: bool,

    /// Tool output content
    pub output: String,

    /// Execution metadata
    pub metadata: HashMap<String, serde_json::Value>,

    /// Execution time in milliseconds
    pub execution_time_ms: u64,

    /// Resource usage information
    pub resource_usage: Option<ResourceUsage>,
}

impl ToolResult {
    /// Create a successful result
    pub fn success(output: impl Into<String>) -> Self {
        Self {
            success: true,
            output: output.into(),
            metadata: HashMap::new(),
            execution_time_ms: 0,
            resource_usage: None,
        }
    }

    /// Create an error result
    pub fn error(error: impl Into<String>) -> Self {
        Self {
            success: false,
            output: error.into(),
            metadata: HashMap::new(),
            execution_time_ms: 0,
            resource_usage: None,
        }
    }

    /// Add metadata using builder pattern
    pub fn with_metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }

    /// Set execution timing
    pub fn with_timing(mut self, execution_time_ms: u64) -> Self {
        self.execution_time_ms = execution_time_ms;
        self
    }

    /// Set resource usage
    pub fn with_resource_usage(mut self, usage: ResourceUsage) -> Self {
        self.resource_usage = Some(usage);
        self
    }
}

/// Resource usage tracking for tools
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceUsage {
    /// Memory allocated in bytes
    pub memory_bytes: Option<u64>,

    /// CPU time used in microseconds
    pub cpu_time_us: Option<u64>,

    /// Network requests made
    pub network_requests: Option<u32>,

    /// Files accessed
    pub files_accessed: Option<u32>,
}

/// Core tool trait optimized for Rust's async ecosystem
#[async_trait]
pub trait Tool: Send + Sync {
    /// Tool identifier (used for registration and calling)
    fn name(&self) -> &str;

    /// Human-readable description for LLM context
    fn description(&self) -> &str;

    /// JSON schema for parameter validation (optional)
    fn schema(&self) -> Option<serde_json::Value> {
        None
    }

    /// Execute the tool with string input
    async fn execute(&self, input: &str) -> RragResult<ToolResult>;

    /// Execute with structured parameters (default delegates to execute)
    async fn execute_with_params(&self, params: serde_json::Value) -> RragResult<ToolResult> {
        let input = match params {
            serde_json::Value::String(s) => s,
            _ => params.to_string(),
        };
        self.execute(&input).await
    }

    /// Tool capabilities for filtering and discovery
    fn capabilities(&self) -> Vec<&'static str> {
        vec![]
    }

    /// Whether this tool requires authentication
    fn requires_auth(&self) -> bool {
        false
    }

    /// Tool category for organization
    fn category(&self) -> &'static str {
        "general"
    }

    /// Whether this tool can be cached
    fn is_cacheable(&self) -> bool {
        false
    }

    /// Cost estimate for execution (arbitrary units)
    fn cost_estimate(&self) -> u32 {
        1
    }
}

/// Macro for creating simple tools with less boilerplate
#[macro_export]
macro_rules! rrag_tool {
    (
        name: $name:expr,
        description: $desc:expr,
        execute: $exec:expr
    ) => {
        #[derive(Debug)]
        pub struct GeneratedTool;

        #[async_trait::async_trait]
        impl Tool for GeneratedTool {
            fn name(&self) -> &str {
                $name
            }

            fn description(&self) -> &str {
                $desc
            }

            async fn execute(&self, input: &str) -> RragResult<ToolResult> {
                let start = std::time::Instant::now();
                let result = ($exec)(input).await;
                let execution_time = start.elapsed().as_millis() as u64;

                match result {
                    Ok(output) => Ok(ToolResult::success(output).with_timing(execution_time)),
                    Err(e) => Ok(ToolResult::error(e.to_string()).with_timing(execution_time)),
                }
            }
        }
    };

    (
        name: $name:expr,
        description: $desc:expr,
        category: $category:expr,
        execute: $exec:expr
    ) => {
        #[derive(Debug)]
        pub struct GeneratedTool;

        #[async_trait::async_trait]
        impl Tool for GeneratedTool {
            fn name(&self) -> &str {
                $name
            }

            fn description(&self) -> &str {
                $desc
            }

            fn category(&self) -> &'static str {
                $category
            }

            async fn execute(&self, input: &str) -> RragResult<ToolResult> {
                let start = std::time::Instant::now();
                let result = ($exec)(input).await;
                let execution_time = start.elapsed().as_millis() as u64;

                match result {
                    Ok(output) => Ok(ToolResult::success(output).with_timing(execution_time)),
                    Err(e) => Ok(ToolResult::error(e.to_string()).with_timing(execution_time)),
                }
            }
        }
    };
}

/// Thread-safe tool registry using Arc for efficient sharing
#[derive(Clone)]
pub struct ToolRegistry {
    tools: HashMap<String, Arc<dyn Tool>>,
}

impl ToolRegistry {
    /// Create a new empty registry
    pub fn new() -> Self {
        Self {
            tools: HashMap::new(),
        }
    }

    /// Create registry with pre-registered tools
    pub fn with_tools(tools: Vec<Arc<dyn Tool>>) -> Self {
        let mut registry = HashMap::new();
        for tool in tools {
            registry.insert(tool.name().to_string(), tool);
        }

        Self { tools: registry }
    }

    /// Register a new tool
    pub fn register(&mut self, tool: Arc<dyn Tool>) -> RragResult<()> {
        let name = tool.name().to_string();

        if self.tools.contains_key(&name) {
            return Err(RragError::config(
                "tool_name",
                "unique name",
                format!("duplicate: {}", name),
            ));
        }

        self.tools.insert(name, tool);
        Ok(())
    }

    /// Get a tool by name
    pub fn get(&self, name: &str) -> Option<Arc<dyn Tool>> {
        self.tools.get(name).cloned()
    }

    /// List all registered tool names
    pub fn list_tools(&self) -> Vec<String> {
        self.tools.keys().cloned().collect()
    }

    /// List tools by category
    pub fn list_by_category(&self, category: &str) -> Vec<Arc<dyn Tool>> {
        self.tools
            .values()
            .filter(|tool| tool.category() == category)
            .cloned()
            .collect()
    }

    /// List tools by capability
    pub fn list_by_capability(&self, capability: &str) -> Vec<Arc<dyn Tool>> {
        self.tools
            .values()
            .filter(|tool| tool.capabilities().contains(&capability))
            .cloned()
            .collect()
    }

    /// Execute a tool by name
    pub async fn execute(&self, tool_name: &str, input: &str) -> RragResult<ToolResult> {
        let tool = self
            .get(tool_name)
            .ok_or_else(|| RragError::tool_execution(tool_name, "Tool not found"))?;

        tool.execute(input).await
    }

    /// Get tool schemas for LLM context
    pub fn get_tool_schemas(&self) -> HashMap<String, serde_json::Value> {
        self.tools
            .iter()
            .filter_map(|(name, tool)| tool.schema().map(|schema| (name.clone(), schema)))
            .collect()
    }

    /// Get tool descriptions for LLM context
    pub fn get_tool_descriptions(&self) -> HashMap<String, String> {
        self.tools
            .iter()
            .map(|(name, tool)| (name.clone(), tool.description().to_string()))
            .collect()
    }
}

impl Default for ToolRegistry {
    fn default() -> Self {
        Self::new()
    }
}

/// Built-in calculator tool
#[derive(Debug)]
pub struct Calculator;

#[async_trait]
impl Tool for Calculator {
    fn name(&self) -> &str {
        "calculator"
    }

    fn description(&self) -> &str {
        "Performs mathematical calculations. Input should be a mathematical expression like '2+2', '10*5', or '15/3'."
    }

    fn category(&self) -> &'static str {
        "math"
    }

    fn capabilities(&self) -> Vec<&'static str> {
        vec!["math", "calculation", "arithmetic"]
    }

    fn is_cacheable(&self) -> bool {
        true // Math results are deterministic
    }

    async fn execute(&self, input: &str) -> RragResult<ToolResult> {
        let start = Instant::now();

        match calculate(input) {
            Ok(result) => {
                let execution_time = start.elapsed().as_millis() as u64;
                Ok(ToolResult::success(result.to_string())
                    .with_timing(execution_time)
                    .with_metadata("expression", serde_json::Value::String(input.to_string()))
                    .with_metadata(
                        "result_type",
                        serde_json::Value::String("number".to_string()),
                    ))
            }
            Err(e) => {
                let execution_time = start.elapsed().as_millis() as u64;
                Ok(ToolResult::error(format!("Calculation error: {}", e))
                    .with_timing(execution_time))
            }
        }
    }

    fn schema(&self) -> Option<serde_json::Value> {
        Some(serde_json::json!({
            "type": "object",
            "properties": {
                "expression": {
                    "type": "string",
                    "description": "Mathematical expression to evaluate",
                    "examples": ["2+2", "10*5", "15/3", "sqrt(16)", "2^3"]
                }
            },
            "required": ["expression"]
        }))
    }
}

/// Simple calculator implementation
fn calculate(expr: &str) -> RragResult<f64> {
    let expr = expr.trim().replace(" ", "");

    // Handle basic operations in order of precedence
    if let Some(result) = try_parse_number(&expr) {
        return Ok(result);
    }

    // Addition and subtraction (lowest precedence)
    if let Some(pos) = expr.rfind('+') {
        let (left, right) = expr.split_at(pos);
        let right = &right[1..];
        return Ok(calculate(left)? + calculate(right)?);
    }

    if let Some(pos) = expr.rfind('-') {
        if pos > 0 {
            // Avoid treating negative numbers as subtraction
            let (left, right) = expr.split_at(pos);
            let right = &right[1..];
            return Ok(calculate(left)? - calculate(right)?);
        }
    }

    // Multiplication and division
    if let Some(pos) = expr.rfind('*') {
        let (left, right) = expr.split_at(pos);
        let right = &right[1..];
        return Ok(calculate(left)? * calculate(right)?);
    }

    if let Some(pos) = expr.rfind('/') {
        let (left, right) = expr.split_at(pos);
        let right = &right[1..];
        let right_val = calculate(right)?;
        if right_val == 0.0 {
            return Err(RragError::tool_execution("calculator", "Division by zero"));
        }
        return Ok(calculate(left)? / right_val);
    }

    // Power operation
    if let Some(pos) = expr.find('^') {
        let (left, right) = expr.split_at(pos);
        let right = &right[1..];
        return Ok(calculate(left)?.powf(calculate(right)?));
    }

    // Functions
    if expr.starts_with("sqrt(") && expr.ends_with(')') {
        let inner = &expr[5..expr.len() - 1];
        let value = calculate(inner)?;
        if value < 0.0 {
            return Err(RragError::tool_execution(
                "calculator",
                "Square root of negative number",
            ));
        }
        return Ok(value.sqrt());
    }

    if expr.starts_with("sin(") && expr.ends_with(')') {
        let inner = &expr[4..expr.len() - 1];
        return Ok(calculate(inner)?.sin());
    }

    if expr.starts_with("cos(") && expr.ends_with(')') {
        let inner = &expr[4..expr.len() - 1];
        return Ok(calculate(inner)?.cos());
    }

    // Parentheses
    if expr.starts_with('(') && expr.ends_with(')') {
        let inner = &expr[1..expr.len() - 1];
        return calculate(inner);
    }

    Err(RragError::tool_execution(
        "calculator",
        format!("Invalid expression: {}", expr),
    ))
}

fn try_parse_number(s: &str) -> Option<f64> {
    s.parse().ok()
}

/// Echo tool for testing and debugging
#[derive(Debug)]
pub struct EchoTool;

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

    fn description(&self) -> &str {
        "Echoes back the input text. Useful for testing and debugging."
    }

    fn category(&self) -> &'static str {
        "utility"
    }

    fn capabilities(&self) -> Vec<&'static str> {
        vec!["test", "debug", "echo"]
    }

    async fn execute(&self, input: &str) -> RragResult<ToolResult> {
        let start = Instant::now();
        let output = format!("Echo: {}", input);
        let execution_time = start.elapsed().as_millis() as u64;

        Ok(ToolResult::success(output)
            .with_timing(execution_time)
            .with_metadata(
                "input_length",
                serde_json::Value::Number(input.len().into()),
            ))
    }
}

/// HTTP client tool for web requests (requires "http" feature)
#[cfg(feature = "http")]
#[derive(Debug)]
pub struct HttpTool {
    client: reqwest::Client,
}

#[cfg(feature = "http")]
impl HttpTool {
    pub fn new() -> Self {
        Self {
            client: reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(30))
                .build()
                .expect("Failed to create HTTP client"),
        }
    }
}

#[cfg(feature = "http")]
#[async_trait]
impl Tool for HttpTool {
    fn name(&self) -> &str {
        "http"
    }

    fn description(&self) -> &str {
        "Makes HTTP GET requests to fetch web content. Input should be a valid URL."
    }

    fn category(&self) -> &'static str {
        "web"
    }

    fn capabilities(&self) -> Vec<&'static str> {
        vec!["web", "http", "fetch", "scraping"]
    }

    async fn execute(&self, input: &str) -> RragResult<ToolResult> {
        let start = Instant::now();

        let url = input.trim();
        if !url.starts_with("http://") && !url.starts_with("https://") {
            let execution_time = start.elapsed().as_millis() as u64;
            return Ok(ToolResult::error("URL must start with http:// or https://")
                .with_timing(execution_time));
        }

        match self.client.get(url).send().await {
            Ok(response) => {
                let status = response.status();
                let headers_count = response.headers().len();

                match response.text().await {
                    Ok(body) => {
                        let execution_time = start.elapsed().as_millis() as u64;
                        let truncated_body = if body.len() > 10000 {
                            format!(
                                "{}... [truncated from {} chars]",
                                &body[..10000],
                                body.len()
                            )
                        } else {
                            body
                        };

                        Ok(ToolResult::success(truncated_body)
                            .with_timing(execution_time)
                            .with_metadata(
                                "status_code",
                                serde_json::Value::Number(status.as_u16().into()),
                            )
                            .with_metadata(
                                "headers_count",
                                serde_json::Value::Number(headers_count.into()),
                            )
                            .with_metadata("url", serde_json::Value::String(url.to_string())))
                    }
                    Err(e) => {
                        let execution_time = start.elapsed().as_millis() as u64;
                        Ok(
                            ToolResult::error(format!("Failed to read response body: {}", e))
                                .with_timing(execution_time),
                        )
                    }
                }
            }
            Err(e) => {
                let execution_time = start.elapsed().as_millis() as u64;
                Ok(ToolResult::error(format!("HTTP request failed: {}", e))
                    .with_timing(execution_time))
            }
        }
    }

    fn schema(&self) -> Option<serde_json::Value> {
        Some(serde_json::json!({
            "type": "object",
            "properties": {
                "url": {
                    "type": "string",
                    "format": "uri",
                    "description": "The URL to fetch"
                }
            },
            "required": ["url"]
        }))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_calculator_tool() {
        let calc = Calculator;

        let result = calc.execute("2+2").await.unwrap();
        assert!(result.success);
        assert_eq!(result.output, "4");

        let result = calc.execute("10*5").await.unwrap();
        assert!(result.success);
        assert_eq!(result.output, "50");

        let result = calc.execute("sqrt(16)").await.unwrap();
        assert!(result.success);
        assert_eq!(result.output, "4");
    }

    #[tokio::test]
    async fn test_echo_tool() {
        let echo = EchoTool;
        let result = echo.execute("hello world").await.unwrap();

        assert!(result.success);
        assert_eq!(result.output, "Echo: hello world");
        assert!(result.execution_time_ms > 0);
    }

    #[tokio::test]
    async fn test_tool_registry() {
        let mut registry = ToolRegistry::new();

        registry.register(Arc::new(Calculator)).unwrap();
        registry.register(Arc::new(EchoTool)).unwrap();

        assert_eq!(registry.list_tools().len(), 2);
        assert!(registry.list_tools().contains(&"calculator".to_string()));
        assert!(registry.list_tools().contains(&"echo".to_string()));

        let result = registry.execute("calculator", "5*5").await.unwrap();
        assert!(result.success);
        assert_eq!(result.output, "25");
    }

    #[test]
    fn test_calculator_functions() {
        assert_eq!(calculate("2+2").unwrap(), 4.0);
        assert_eq!(calculate("10-3").unwrap(), 7.0);
        assert_eq!(calculate("4*5").unwrap(), 20.0);
        assert_eq!(calculate("15/3").unwrap(), 5.0);
        assert_eq!(calculate("2^3").unwrap(), 8.0);
        assert_eq!(calculate("sqrt(9)").unwrap(), 3.0);
        assert_eq!(calculate("(2+3)*4").unwrap(), 20.0);
    }

    #[test]
    fn test_calculator_errors() {
        assert!(calculate("5/0").is_err());
        assert!(calculate("sqrt(-1)").is_err());
        assert!(calculate("invalid").is_err());
    }

    #[test]
    fn test_tool_categories() {
        let calc = Calculator;
        assert_eq!(calc.category(), "math");
        assert!(calc.capabilities().contains(&"math"));
        assert!(calc.is_cacheable());

        let echo = EchoTool;
        assert_eq!(echo.category(), "utility");
        assert!(echo.capabilities().contains(&"test"));
    }
}