rexis-graph 0.1.0

Rexis Graph - Graph-based agent orchestration with hybrid state management and memory integration
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
//! # Tool System for RGraph Agents
//!
//! This module provides the tool system that allows agents to interact with
//! external systems, perform computations, and access data.

use crate::state::GraphState;
// Future use for tool implementations
use async_trait::async_trait;
use std::collections::HashMap;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Result of tool execution
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ToolResult {
    /// Tool output
    pub output: serde_json::Value,

    /// Additional metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Error that can occur during tool execution
#[derive(Debug, thiserror::Error)]
pub enum ToolError {
    #[error("Tool execution error: {message}")]
    Execution { message: String },

    #[error("Invalid arguments: {message}")]
    InvalidArguments { message: String },

    #[error("Tool not found: {name}")]
    NotFound { name: String },

    #[error("Permission denied for tool: {name}")]
    PermissionDenied { name: String },

    #[error("Tool timeout: {name}")]
    Timeout { name: String },

    #[error("Network error: {message}")]
    Network { message: String },

    #[error("Other error: {0}")]
    Other(#[from] anyhow::Error),
}

/// Configuration for a tool
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ToolConfig {
    /// Tool name
    pub name: String,

    /// Tool description
    pub description: String,

    /// Tool version
    pub version: String,

    /// Whether the tool requires authentication
    pub requires_auth: bool,

    /// Maximum execution time in milliseconds
    pub timeout_ms: Option<u64>,

    /// Tool-specific configuration
    pub config: serde_json::Value,
}

/// Core trait for all tools
#[async_trait]
pub trait Tool: Send + Sync {
    /// Execute the tool with given arguments
    async fn execute(
        &self,
        arguments: &serde_json::Value,
        state: &GraphState,
    ) -> Result<ToolResult, ToolError>;

    /// Get the tool name
    fn name(&self) -> &str;

    /// Get the tool description
    fn description(&self) -> &str;

    /// Get the tool's argument schema (JSON Schema)
    fn argument_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {},
            "additionalProperties": true
        })
    }

    /// Validate tool arguments
    fn validate_arguments(&self, _arguments: &serde_json::Value) -> Result<(), ToolError> {
        Ok(())
    }

    /// Check if the tool requires authentication
    fn requires_auth(&self) -> bool {
        false
    }

    /// Get tool metadata
    fn metadata(&self) -> HashMap<String, serde_json::Value> {
        HashMap::new()
    }
}

/// Simple echo tool for testing
pub struct EchoTool {
    name: String,
}

impl EchoTool {
    pub fn new() -> Self {
        Self {
            name: "echo".to_string(),
        }
    }
}

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

#[async_trait]
impl Tool for EchoTool {
    async fn execute(
        &self,
        arguments: &serde_json::Value,
        _state: &GraphState,
    ) -> Result<ToolResult, ToolError> {
        let message = arguments
            .get("message")
            .and_then(|v| v.as_str())
            .unwrap_or("Hello from EchoTool!");

        Ok(ToolResult {
            output: serde_json::json!({
                "echo": message,
                "timestamp": chrono::Utc::now().to_rfc3339()
            }),
            metadata: HashMap::new(),
        })
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        "A simple tool that echoes back the input message"
    }

    fn argument_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "message": {
                    "type": "string",
                    "description": "The message to echo back"
                }
            },
            "required": ["message"]
        })
    }
}

/// Calculator tool for basic arithmetic
pub struct CalculatorTool {
    name: String,
}

impl CalculatorTool {
    pub fn new() -> Self {
        Self {
            name: "calculator".to_string(),
        }
    }
}

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

#[async_trait]
impl Tool for CalculatorTool {
    async fn execute(
        &self,
        arguments: &serde_json::Value,
        _state: &GraphState,
    ) -> Result<ToolResult, ToolError> {
        let operation = arguments
            .get("operation")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidArguments {
                message: "Missing 'operation' field".to_string(),
            })?;

        let a = arguments.get("a").and_then(|v| v.as_f64()).ok_or_else(|| {
            ToolError::InvalidArguments {
                message: "Missing or invalid 'a' field".to_string(),
            }
        })?;

        let b = arguments.get("b").and_then(|v| v.as_f64()).ok_or_else(|| {
            ToolError::InvalidArguments {
                message: "Missing or invalid 'b' field".to_string(),
            }
        })?;

        let result = match operation {
            "add" => a + b,
            "subtract" => a - b,
            "multiply" => a * b,
            "divide" => {
                if b == 0.0 {
                    return Err(ToolError::Execution {
                        message: "Division by zero".to_string(),
                    });
                }
                a / b
            }
            _ => {
                return Err(ToolError::InvalidArguments {
                    message: format!("Unknown operation: {}", operation),
                })
            }
        };

        Ok(ToolResult {
            output: serde_json::json!({
                "operation": operation,
                "operands": [a, b],
                "result": result
            }),
            metadata: HashMap::new(),
        })
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        "A calculator tool for basic arithmetic operations"
    }

    fn argument_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "operation": {
                    "type": "string",
                    "enum": ["add", "subtract", "multiply", "divide"],
                    "description": "The arithmetic operation to perform"
                },
                "a": {
                    "type": "number",
                    "description": "First operand"
                },
                "b": {
                    "type": "number",
                    "description": "Second operand"
                }
            },
            "required": ["operation", "a", "b"]
        })
    }

    fn validate_arguments(&self, arguments: &serde_json::Value) -> Result<(), ToolError> {
        if !arguments.is_object() {
            return Err(ToolError::InvalidArguments {
                message: "Arguments must be an object".to_string(),
            });
        }

        // Check required fields
        let required_fields = ["operation", "a", "b"];
        for field in &required_fields {
            if !arguments.get(field).is_some() {
                return Err(ToolError::InvalidArguments {
                    message: format!("Missing required field: {}", field),
                });
            }
        }

        // Validate operation
        if let Some(op) = arguments.get("operation").and_then(|v| v.as_str()) {
            if !["add", "subtract", "multiply", "divide"].contains(&op) {
                return Err(ToolError::InvalidArguments {
                    message: format!("Invalid operation: {}", op),
                });
            }
        }

        Ok(())
    }
}

/// Tool registry for managing available tools
pub struct ToolRegistry {
    tools: HashMap<String, Box<dyn Tool>>,
}

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

    /// Register a tool
    pub fn register(&mut self, tool: Box<dyn Tool>) {
        let name = tool.name().to_string();
        self.tools.insert(name, tool);
    }

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

    /// Get all available tool names
    pub fn tool_names(&self) -> Vec<String> {
        self.tools.keys().cloned().collect()
    }

    /// Execute a tool
    pub async fn execute(
        &self,
        tool_name: &str,
        arguments: &serde_json::Value,
        state: &GraphState,
    ) -> Result<ToolResult, ToolError> {
        let tool = self.get(tool_name).ok_or_else(|| ToolError::NotFound {
            name: tool_name.to_string(),
        })?;

        // Validate arguments
        tool.validate_arguments(arguments)?;

        // Execute tool
        tool.execute(arguments, state).await
    }
}

impl Default for ToolRegistry {
    fn default() -> Self {
        let mut registry = Self::new();

        // Register default tools
        registry.register(Box::new(EchoTool::new()));
        registry.register(Box::new(CalculatorTool::new()));

        registry
    }
}

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

    #[tokio::test]
    async fn test_echo_tool() {
        let tool = EchoTool::new();
        let state = GraphState::new();
        let arguments = serde_json::json!({
            "message": "Hello, World!"
        });

        let result = tool.execute(&arguments, &state).await.unwrap();

        assert_eq!(result.output["echo"], "Hello, World!");
        assert!(result.output.get("timestamp").is_some());
    }

    #[tokio::test]
    async fn test_calculator_tool() {
        let tool = CalculatorTool::new();
        let state = GraphState::new();

        // Test addition
        let arguments = serde_json::json!({
            "operation": "add",
            "a": 5.0,
            "b": 3.0
        });

        let result = tool.execute(&arguments, &state).await.unwrap();
        assert_eq!(result.output["result"], 8.0);

        // Test division by zero
        let arguments = serde_json::json!({
            "operation": "divide",
            "a": 5.0,
            "b": 0.0
        });

        let result = tool.execute(&arguments, &state).await;
        assert!(result.is_err());
    }

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

        assert!(registry.get("echo").is_some());
        assert!(registry.get("nonexistent").is_none());

        let tool_names = registry.tool_names();
        assert!(tool_names.contains(&"echo".to_string()));

        // Test execution through registry
        let arguments = serde_json::json!({
            "message": "Test"
        });
        let state = GraphState::new();

        let result = registry.execute("echo", &arguments, &state).await.unwrap();
        assert_eq!(result.output["echo"], "Test");
    }

    #[test]
    fn test_calculator_validation() {
        let tool = CalculatorTool::new();

        // Valid arguments
        let valid_args = serde_json::json!({
            "operation": "add",
            "a": 1.0,
            "b": 2.0
        });
        assert!(tool.validate_arguments(&valid_args).is_ok());

        // Invalid operation
        let invalid_args = serde_json::json!({
            "operation": "invalid",
            "a": 1.0,
            "b": 2.0
        });
        assert!(tool.validate_arguments(&invalid_args).is_err());

        // Missing field
        let missing_field = serde_json::json!({
            "operation": "add",
            "a": 1.0
        });
        assert!(tool.validate_arguments(&missing_field).is_err());
    }

    #[test]
    fn test_tool_schemas() {
        let echo_tool = EchoTool::new();
        let calc_tool = CalculatorTool::new();

        let echo_schema = echo_tool.argument_schema();
        assert_eq!(echo_schema["type"], "object");
        assert!(echo_schema["properties"].get("message").is_some());

        let calc_schema = calc_tool.argument_schema();
        assert_eq!(calc_schema["type"], "object");
        assert!(calc_schema["properties"].get("operation").is_some());
        assert!(calc_schema["properties"].get("a").is_some());
        assert!(calc_schema["properties"].get("b").is_some());
    }
}