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
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
//! # Agent Node Implementation
//!
//! Agent nodes represent autonomous AI agents that can reason, make decisions,
//! and use tools to accomplish tasks.

use crate::core::{ExecutionContext, ExecutionResult, Node, NodeId};
use crate::state::{GraphState, StateValue};
use crate::tools::Tool;
use crate::{RGraphError, RGraphResult};
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;

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

/// Configuration for an agent node
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct AgentNodeConfig {
    /// Agent name
    pub name: String,

    /// System prompt for the agent
    pub system_prompt: String,

    /// Available tools
    pub tools: Vec<String>,

    /// Maximum number of reasoning steps
    pub max_steps: usize,

    /// Temperature for generation
    pub temperature: f32,

    /// Maximum tokens for generation
    pub max_tokens: Option<usize>,

    /// Whether to use structured output
    pub structured_output: bool,

    /// Custom instructions
    pub instructions: Vec<String>,
}

impl Default for AgentNodeConfig {
    fn default() -> Self {
        Self {
            name: "assistant".to_string(),
            system_prompt: "You are a helpful AI assistant.".to_string(),
            tools: Vec::new(),
            max_steps: 10,
            temperature: 0.7,
            max_tokens: Some(1000),
            structured_output: false,
            instructions: Vec::new(),
        }
    }
}

/// An agent node that can reason and use tools
pub struct AgentNode {
    id: NodeId,
    config: AgentNodeConfig,
    tools: HashMap<String, Arc<dyn Tool>>,
}

impl AgentNode {
    /// Create a new agent node
    pub fn new(id: impl Into<NodeId>, config: AgentNodeConfig) -> Self {
        Self {
            id: id.into(),
            config,
            tools: HashMap::new(),
        }
    }

    /// Add a tool to the agent
    pub fn with_tool(mut self, name: String, tool: Arc<dyn Tool>) -> Self {
        self.tools.insert(name, tool);
        self
    }

    /// Set the system prompt
    pub fn with_system_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.config.system_prompt = prompt.into();
        self
    }

    /// Add tools by name
    pub fn with_tools(mut self, tools: Vec<String>) -> Self {
        self.config.tools = tools;
        self
    }

    /// Set temperature
    pub fn with_temperature(mut self, temperature: f32) -> Self {
        self.config.temperature = temperature.clamp(0.0, 2.0);
        self
    }

    /// Execute the agent's reasoning loop
    async fn reasoning_loop(
        &self,
        state: &mut GraphState,
        context: &ExecutionContext,
        initial_input: &str,
    ) -> RGraphResult<String> {
        let mut conversation_history = Vec::new();
        let mut step_count = 0;

        // Load previous conversation from persistent memory if available
        #[cfg(feature = "rexis-rag-integration")]
        if let Some(memory) = context.memory() {
            let conv_key = format!("agent::{}::conversation", self.id.as_str());
            if let Ok(Some(value)) = memory.get(&conv_key).await {
                if let Some(json) = value.as_json() {
                    if let Ok(history) = serde_json::from_value::<Vec<AgentMessage>>(json.clone()) {
                        conversation_history = history;
                        tracing::debug!(
                            "Loaded {} previous messages from persistent memory",
                            conversation_history.len()
                        );
                    }
                }
            }
        }

        // Add system prompt if not already present
        if conversation_history.is_empty()
            || conversation_history.first().map(|m| m.role) != Some(MessageRole::System)
        {
            conversation_history.insert(
                0,
                AgentMessage {
                    role: MessageRole::System,
                    content: self.config.system_prompt.clone(),
                    tool_calls: None,
                },
            );
        }

        // Add user input
        conversation_history.push(AgentMessage {
            role: MessageRole::User,
            content: initial_input.to_string(),
            tool_calls: None,
        });

        loop {
            if step_count >= self.config.max_steps {
                break;
            }

            step_count += 1;

            // Generate agent response
            let agent_response = self.generate_response(&conversation_history, state).await?;

            // Check if agent wants to use tools
            if let Some(tool_calls) = &agent_response.tool_calls {
                // Execute tool calls
                let mut tool_results = Vec::new();

                for tool_call in tool_calls {
                    if let Some(tool) = self.tools.get(&tool_call.name) {
                        match tool.execute(&tool_call.arguments, state).await {
                            Ok(result) => {
                                tool_results.push(ToolCallResult {
                                    call_id: tool_call.id.clone(),
                                    name: tool_call.name.clone(),
                                    result: result.output,
                                    success: true,
                                    error: None,
                                });
                            }
                            Err(e) => {
                                tool_results.push(ToolCallResult {
                                    call_id: tool_call.id.clone(),
                                    name: tool_call.name.clone(),
                                    result: serde_json::Value::Null,
                                    success: false,
                                    error: Some(e.to_string()),
                                });
                            }
                        }
                    } else {
                        tool_results.push(ToolCallResult {
                            call_id: tool_call.id.clone(),
                            name: tool_call.name.clone(),
                            result: serde_json::Value::Null,
                            success: false,
                            error: Some(format!("Tool '{}' not found", tool_call.name)),
                        });
                    }
                }

                // Add assistant message with tool calls
                conversation_history.push(agent_response);

                // Add tool results
                for tool_result in tool_results {
                    conversation_history.push(AgentMessage {
                        role: MessageRole::Tool,
                        content: if tool_result.success {
                            serde_json::to_string_pretty(&tool_result.result)
                                .unwrap_or_else(|_| "Tool execution completed".to_string())
                        } else {
                            format!(
                                "Error: {}",
                                tool_result
                                    .error
                                    .unwrap_or_else(|| "Unknown error".to_string())
                            )
                        },
                        tool_calls: None,
                    });
                }
            } else {
                // Agent provided final response
                conversation_history.push(agent_response.clone());

                // Save conversation to persistent memory before returning
                self.save_conversation(context, &conversation_history)
                    .await?;

                return Ok(agent_response.content);
            }
        }

        // If we exit the loop without a final response, return the last agent message
        let final_response = conversation_history
            .iter()
            .filter(|msg| msg.role == MessageRole::Assistant)
            .last()
            .map(|msg| msg.content.clone())
            .unwrap_or_else(|| "Maximum reasoning steps reached without conclusion".to_string());

        // Save conversation to persistent memory
        self.save_conversation(context, &conversation_history)
            .await?;

        Ok(final_response)
    }

    /// Save conversation to persistent memory
    async fn save_conversation(
        &self,
        context: &ExecutionContext,
        conversation: &[AgentMessage],
    ) -> RGraphResult<()> {
        #[cfg(feature = "rexis-rag-integration")]
        if let Some(memory) = context.memory() {
            let conv_key = format!("agent::{}::conversation", self.id.as_str());
            let value = serde_json::to_value(conversation).map_err(|e| {
                RGraphError::node(
                    self.id.as_str(),
                    format!("Failed to serialize conversation: {}", e),
                )
            })?;

            memory
                .set(&conv_key, rexis_rag::storage::MemoryValue::Json(value))
                .await
                .map_err(|e| {
                    RGraphError::node(
                        self.id.as_str(),
                        format!("Failed to save conversation to memory: {}", e),
                    )
                })?;

            tracing::debug!("Saved {} messages to persistent memory", conversation.len());
        }

        #[cfg(not(feature = "rexis-rag-integration"))]
        {
            let _ = (context, conversation); // Suppress unused variable warnings
        }

        Ok(())
    }

    /// Generate a response from the agent
    async fn generate_response(
        &self,
        conversation: &[AgentMessage],
        _state: &GraphState,
    ) -> RGraphResult<AgentMessage> {
        // In a real implementation, this would call an LLM API
        // For now, we'll simulate an agent response

        let empty_string = String::new();
        let last_user_message = conversation
            .iter()
            .filter(|msg| msg.role == MessageRole::User)
            .last()
            .map(|msg| &msg.content)
            .unwrap_or(&empty_string);

        // Check if we should use tools based on the input
        if self.should_use_tools(last_user_message) && !self.tools.is_empty() {
            // Simulate tool usage decision
            let tool_name = self.tools.keys().next().unwrap().clone();

            Ok(AgentMessage {
                role: MessageRole::Assistant,
                content: format!(
                    "I'll help you with that. Let me use the {} tool.",
                    tool_name
                ),
                tool_calls: Some(vec![ToolCall {
                    id: uuid::Uuid::new_v4().to_string(),
                    name: tool_name,
                    arguments: serde_json::json!({
                        "query": last_user_message
                    }),
                }]),
            })
        } else {
            // Generate a direct response
            Ok(AgentMessage {
                role: MessageRole::Assistant,
                content: format!(
                    "Based on your request '{}', I can provide assistance. This is a simulated response from the {} agent.",
                    last_user_message,
                    self.config.name
                ),
                tool_calls: None,
            })
        }
    }

    /// Determine if the agent should use tools for this input
    fn should_use_tools(&self, input: &str) -> bool {
        // Simple heuristic - in a real implementation this would be more sophisticated
        let tool_keywords = ["search", "calculate", "analyze", "find", "lookup", "query"];
        let input_lower = input.to_lowercase();

        tool_keywords
            .iter()
            .any(|keyword| input_lower.contains(keyword))
    }
}

#[async_trait]
impl Node for AgentNode {
    async fn execute(
        &self,
        state: &mut GraphState,
        context: &ExecutionContext,
    ) -> RGraphResult<ExecutionResult> {
        // Get input from state
        let input = state
            .get("user_input")
            .or_else(|_| state.get("query"))
            .or_else(|_| state.get("prompt"))
            .map_err(|_| {
                RGraphError::node(
                    self.id.as_str(),
                    "No input found in state (expected 'user_input', 'query', or 'prompt')",
                )
            })?;

        let input_text = match input {
            StateValue::String(s) => s,
            _ => {
                return Err(RGraphError::node(
                    self.id.as_str(),
                    "Input must be a string",
                ))
            }
        };

        // Execute reasoning loop
        let response = self.reasoning_loop(state, context, &input_text).await?;

        // Store the response in state
        state.set_with_context(
            context.current_node.as_str(),
            "agent_response",
            response.clone(),
        );

        // Also store in a generic output key
        state.set_with_context(context.current_node.as_str(), "output", response);

        Ok(ExecutionResult::Continue)
    }

    fn id(&self) -> &NodeId {
        &self.id
    }

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

    fn input_keys(&self) -> Vec<&str> {
        vec!["user_input", "query", "prompt"]
    }

    fn output_keys(&self) -> Vec<&str> {
        vec!["agent_response", "output"]
    }

    fn validate(&self, state: &GraphState) -> RGraphResult<()> {
        // Check that we have input
        if !state.contains_key("user_input")
            && !state.contains_key("query")
            && !state.contains_key("prompt")
        {
            return Err(RGraphError::validation(
                "Agent node requires 'user_input', 'query', or 'prompt' in state",
            ));
        }

        Ok(())
    }
}

/// Message in agent conversation
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct AgentMessage {
    pub role: MessageRole,
    pub content: String,
    pub tool_calls: Option<Vec<ToolCall>>,
}

/// Role of message in conversation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum MessageRole {
    System,
    User,
    Assistant,
    Tool,
}

/// Tool call from agent
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ToolCall {
    pub id: String,
    pub name: String,
    pub arguments: serde_json::Value,
}

/// Result of a tool call
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct ToolCallResult {
    pub call_id: String,
    pub name: String,
    pub result: serde_json::Value,
    pub success: bool,
    pub error: Option<String>,
}

// Helper trait for pipe operations
trait Pipe<T> {
    fn pipe<U, F>(self, f: F) -> U
    where
        F: FnOnce(T) -> U;
}

impl<T> Pipe<T> for T {
    fn pipe<U, F>(self, f: F) -> U
    where
        F: FnOnce(T) -> U,
    {
        f(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::ExecutionContext;
    use crate::tools::{Tool, ToolError, ToolResult};

    // Mock tool for testing
    struct MockTool {
        name: String,
    }

    #[async_trait]
    impl Tool for MockTool {
        async fn execute(
            &self,
            _arguments: &serde_json::Value,
            _state: &GraphState,
        ) -> Result<ToolResult, ToolError> {
            Ok(ToolResult {
                output: serde_json::json!({
                    "tool": self.name,
                    "result": "mock result"
                }),
                metadata: HashMap::new(),
            })
        }

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

        fn description(&self) -> &str {
            "Mock tool for testing"
        }
    }

    #[tokio::test]
    async fn test_agent_node_creation() {
        let config = AgentNodeConfig::default();
        let agent = AgentNode::new("test_agent", config);

        assert_eq!(agent.id().as_str(), "test_agent");
        assert_eq!(agent.name(), "assistant");
    }

    #[tokio::test]
    async fn test_agent_node_with_tools() {
        let config = AgentNodeConfig::default();
        let tool = Arc::new(MockTool {
            name: "search".to_string(),
        });

        let agent = AgentNode::new("test_agent", config).with_tool("search".to_string(), tool);

        assert!(agent.tools.contains_key("search"));
    }

    #[tokio::test]
    async fn test_agent_execution() {
        let config = AgentNodeConfig::default();
        let agent = AgentNode::new("test_agent", config);

        let mut state = GraphState::new();
        state.set("user_input", "Hello, how can you help me?");

        let context = ExecutionContext::new("test_graph".to_string(), NodeId::new("test_agent"));
        let result = agent.execute(&mut state, &context).await.unwrap();

        assert!(matches!(result, ExecutionResult::Continue));
        assert!(state.contains_key("agent_response"));
    }

    #[test]
    fn test_should_use_tools() {
        let config = AgentNodeConfig::default();
        let agent = AgentNode::new("test_agent", config);

        assert!(agent.should_use_tools("Please search for information"));
        assert!(agent.should_use_tools("Can you calculate this?"));
        assert!(!agent.should_use_tools("Hello there"));
    }

    #[test]
    fn test_agent_message() {
        let message = AgentMessage {
            role: MessageRole::User,
            content: "Test message".to_string(),
            tool_calls: None,
        };

        assert_eq!(message.role, MessageRole::User);
        assert_eq!(message.content, "Test message");
        assert!(message.tool_calls.is_none());
    }

    #[test]
    fn test_tool_call() {
        let tool_call = ToolCall {
            id: "test-123".to_string(),
            name: "search".to_string(),
            arguments: serde_json::json!({"query": "test"}),
        };

        assert_eq!(tool_call.id, "test-123");
        assert_eq!(tool_call.name, "search");
        assert_eq!(tool_call.arguments["query"], "test");
    }
}