oxidizedgraph 0.2.0

A humble attempt at LangGraph in Rust - R-LangGraph framework for building AI agent workflows
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
//! Tool node implementation for oxidizedgraph
//!
//! Provides `ToolNode` for executing tools and `ToolRegistry` for managing
//! available tools.

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

use crate::error::NodeError;
use crate::graph::{NodeExecutor, NodeOutput};
use crate::state::{Message, SharedState, ToolCall};

/// Result of a tool execution
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ToolResult {
    /// The tool call ID this result corresponds to
    pub tool_call_id: String,
    /// The result content (success case)
    pub content: Option<String>,
    /// Error message (failure case)
    pub error: Option<String>,
}

impl ToolResult {
    /// Create a successful result
    pub fn success(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            tool_call_id: tool_call_id.into(),
            content: Some(content.into()),
            error: None,
        }
    }

    /// Create an error result
    pub fn error(tool_call_id: impl Into<String>, error: impl Into<String>) -> Self {
        Self {
            tool_call_id: tool_call_id.into(),
            content: None,
            error: Some(error.into()),
        }
    }

    /// Check if this is a success
    pub fn is_success(&self) -> bool {
        self.content.is_some() && self.error.is_none()
    }

    /// Get the content or error as a string
    pub fn as_str(&self) -> &str {
        self.content
            .as_deref()
            .or(self.error.as_deref())
            .unwrap_or("")
    }
}

/// Trait for implementing tools
#[async_trait]
pub trait Tool: Send + Sync {
    /// Get the tool name
    fn name(&self) -> &str;

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

    /// Get the JSON schema for the tool's parameters
    fn parameters_schema(&self) -> serde_json::Value;

    /// Execute the tool with the given arguments
    async fn execute(&self, arguments: serde_json::Value) -> Result<String, NodeError>;
}

/// Type alias for a boxed tool
pub type BoxedTool = Arc<dyn Tool>;

/// A registry of available tools
#[derive(Clone, Default)]
pub struct ToolRegistry {
    tools: HashMap<String, BoxedTool>,
}

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

    /// Register a tool
    pub fn register(mut self, tool: impl Tool + 'static) -> Self {
        self.tools.insert(tool.name().to_string(), Arc::new(tool));
        self
    }

    /// Register a boxed tool
    pub fn register_boxed(mut self, tool: BoxedTool) -> Self {
        self.tools.insert(tool.name().to_string(), tool);
        self
    }

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

    /// Check if a tool exists
    pub fn has(&self, name: &str) -> bool {
        self.tools.contains_key(name)
    }

    /// Get all tool names
    pub fn names(&self) -> Vec<&str> {
        self.tools.keys().map(|s| s.as_str()).collect()
    }

    /// Get tool definitions for LLM context
    pub fn definitions(&self) -> Vec<serde_json::Value> {
        self.tools
            .values()
            .map(|t| {
                serde_json::json!({
                    "name": t.name(),
                    "description": t.description(),
                    "parameters": t.parameters_schema()
                })
            })
            .collect()
    }

    /// Execute a tool call
    pub async fn execute(&self, call: &ToolCall) -> ToolResult {
        match self.get(&call.name) {
            Some(tool) => match tool.execute(call.arguments.clone()).await {
                Ok(result) => ToolResult::success(&call.id, result),
                Err(e) => ToolResult::error(&call.id, e.to_string()),
            },
            None => ToolResult::error(&call.id, format!("Tool '{}' not found", call.name)),
        }
    }
}

/// A node that executes pending tool calls
pub struct ToolNode {
    id: String,
    registry: ToolRegistry,
}

impl ToolNode {
    /// Create a new tool node
    pub fn new(id: impl Into<String>, registry: ToolRegistry) -> Self {
        Self {
            id: id.into(),
            registry,
        }
    }
}

#[async_trait]
impl NodeExecutor for ToolNode {
    fn id(&self) -> &str {
        &self.id
    }

    async fn execute(&self, state: SharedState) -> Result<NodeOutput, NodeError> {
        // Get pending tool calls
        let tool_calls = {
            let guard = state
                .read()
                .map_err(|e| NodeError::execution_failed(e.to_string()))?;
            guard.tool_calls.clone()
        };

        if tool_calls.is_empty() {
            return Ok(NodeOutput::cont());
        }

        // Execute each tool call
        let mut results = Vec::new();
        for call in &tool_calls {
            let result = self.registry.execute(call).await;
            results.push(result);
        }

        // Update state with results
        {
            let mut guard = state
                .write()
                .map_err(|e| NodeError::execution_failed(e.to_string()))?;

            // Add tool results as messages
            for result in results {
                guard.messages.push(Message::tool_result(
                    &result.tool_call_id,
                    result.as_str(),
                ));
            }

            // Clear pending tool calls
            guard.clear_tool_calls();
        }

        Ok(NodeOutput::cont())
    }

    fn description(&self) -> Option<&str> {
        Some("Executes pending tool calls")
    }
}

/// A simple function-based tool implementation
pub struct FunctionTool<F>
where
    F: Fn(serde_json::Value) -> Result<String, String> + Send + Sync,
{
    name: String,
    description: String,
    parameters_schema: serde_json::Value,
    func: F,
}

impl<F> FunctionTool<F>
where
    F: Fn(serde_json::Value) -> Result<String, String> + Send + Sync,
{
    /// Create a new function tool
    pub fn new(
        name: impl Into<String>,
        description: impl Into<String>,
        parameters_schema: serde_json::Value,
        func: F,
    ) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            parameters_schema,
            func,
        }
    }
}

#[async_trait]
impl<F> Tool for FunctionTool<F>
where
    F: Fn(serde_json::Value) -> Result<String, String> + Send + Sync,
{
    fn name(&self) -> &str {
        &self.name
    }

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

    fn parameters_schema(&self) -> serde_json::Value {
        self.parameters_schema.clone()
    }

    async fn execute(&self, arguments: serde_json::Value) -> Result<String, NodeError> {
        (self.func)(arguments).map_err(NodeError::tool_error)
    }
}

/// An async function-based tool implementation
pub struct AsyncFunctionTool<F, Fut>
where
    F: Fn(serde_json::Value) -> Fut + Send + Sync,
    Fut: std::future::Future<Output = Result<String, String>> + Send,
{
    name: String,
    description: String,
    parameters_schema: serde_json::Value,
    func: F,
}

impl<F, Fut> AsyncFunctionTool<F, Fut>
where
    F: Fn(serde_json::Value) -> Fut + Send + Sync,
    Fut: std::future::Future<Output = Result<String, String>> + Send,
{
    /// Create a new async function tool
    pub fn new(
        name: impl Into<String>,
        description: impl Into<String>,
        parameters_schema: serde_json::Value,
        func: F,
    ) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            parameters_schema,
            func,
        }
    }
}

#[async_trait]
impl<F, Fut> Tool for AsyncFunctionTool<F, Fut>
where
    F: Fn(serde_json::Value) -> Fut + Send + Sync,
    Fut: std::future::Future<Output = Result<String, String>> + Send,
{
    fn name(&self) -> &str {
        &self.name
    }

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

    fn parameters_schema(&self) -> serde_json::Value {
        self.parameters_schema.clone()
    }

    async fn execute(&self, arguments: serde_json::Value) -> Result<String, NodeError> {
        (self.func)(arguments).await.map_err(NodeError::tool_error)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::AgentState;
    use std::sync::RwLock;

    #[test]
    fn test_tool_result() {
        let result = ToolResult::success("call_1", "Success!");
        assert!(result.is_success());
        assert_eq!(result.as_str(), "Success!");

        let result = ToolResult::error("call_2", "Failed");
        assert!(!result.is_success());
        assert_eq!(result.as_str(), "Failed");
    }

    #[tokio::test]
    async fn test_function_tool() {
        let tool = FunctionTool::new(
            "add",
            "Adds two numbers",
            serde_json::json!({
                "type": "object",
                "properties": {
                    "a": { "type": "number" },
                    "b": { "type": "number" }
                }
            }),
            |args| {
                let a = args["a"].as_f64().unwrap_or(0.0);
                let b = args["b"].as_f64().unwrap_or(0.0);
                Ok(format!("{}", a + b))
            },
        );

        let result = tool
            .execute(serde_json::json!({"a": 2, "b": 3}))
            .await
            .unwrap();
        assert_eq!(result, "5");
    }

    #[tokio::test]
    async fn test_tool_registry() {
        let tool = FunctionTool::new(
            "greet",
            "Greets someone",
            serde_json::json!({}),
            |args| {
                let name = args["name"].as_str().unwrap_or("World");
                Ok(format!("Hello, {}!", name))
            },
        );

        let registry = ToolRegistry::new().register(tool);

        assert!(registry.has("greet"));
        assert!(!registry.has("unknown"));

        let call = ToolCall::new("1", "greet", serde_json::json!({"name": "Rust"}));
        let result = registry.execute(&call).await;
        assert!(result.is_success());
        assert_eq!(result.as_str(), "Hello, Rust!");
    }

    #[tokio::test]
    async fn test_tool_node() {
        let tool = FunctionTool::new(
            "echo",
            "Echoes input",
            serde_json::json!({}),
            |args| Ok(args["message"].as_str().unwrap_or("").to_string()),
        );

        let registry = ToolRegistry::new().register(tool);
        let node = ToolNode::new("tools", registry);

        let mut state = AgentState::new();
        state
            .tool_calls
            .push(ToolCall::new("1", "echo", serde_json::json!({"message": "Hello"})));

        let shared = Arc::new(RwLock::new(state));
        let result = node.execute(shared.clone()).await.unwrap();
        assert!(!result.is_terminal());

        let guard = shared.read().unwrap();
        assert!(guard.tool_calls.is_empty()); // Cleared
        assert_eq!(guard.messages.len(), 1); // Tool result added
    }
}