Skip to main content

ainl_runtime/
lib.rs

1//! AINL Runtime - graph-based agent programming runtime
2//!
3//! This crate provides the execution runtime for AINL (AI Native Language),
4//! integrating with the ainl-memory graph substrate for persistent memory.
5
6use ainl_memory::{GraphMemory, GraphStore};
7use serde::{Deserialize, Serialize};
8
9/// Configuration for the AINL runtime
10#[derive(Serialize, Deserialize, Debug, Clone)]
11pub struct RuntimeConfig {
12    /// Maximum depth for delegation chains
13    pub max_delegation_depth: u32,
14
15    /// Enable graph-based memory persistence
16    pub enable_graph_memory: bool,
17}
18
19impl Default for RuntimeConfig {
20    fn default() -> Self {
21        Self {
22            max_delegation_depth: 10,
23            enable_graph_memory: true,
24        }
25    }
26}
27
28/// AINL runtime context for execution
29pub struct RuntimeContext {
30    _config: RuntimeConfig,
31    memory: Option<GraphMemory>,
32}
33
34impl RuntimeContext {
35    /// Create a new runtime context with the given memory backend
36    pub fn new(config: RuntimeConfig, memory: Option<GraphMemory>) -> Self {
37        Self {
38            _config: config,
39            memory,
40        }
41    }
42
43    /// Record an agent delegation as an episode node
44    pub fn record_delegation(
45        &self,
46        delegated_to: String,
47        trace_event: Option<serde_json::Value>,
48    ) -> Result<uuid::Uuid, String> {
49        if let Some(ref memory) = self.memory {
50            memory.write_episode(
51                vec!["agent_delegate".to_string()],
52                Some(delegated_to),
53                trace_event,
54            )
55        } else {
56            Err("Memory not initialized".to_string())
57        }
58    }
59
60    /// Record a tool execution as an episode node
61    pub fn record_tool_execution(
62        &self,
63        tool_name: String,
64        trace_event: Option<serde_json::Value>,
65    ) -> Result<uuid::Uuid, String> {
66        if let Some(ref memory) = self.memory {
67            memory.write_episode(vec![tool_name], None, trace_event)
68        } else {
69            Err("Memory not initialized".to_string())
70        }
71    }
72
73    /// Get direct access to the underlying store for advanced queries
74    pub fn store(&self) -> Option<&dyn GraphStore> {
75        self.memory.as_ref().map(|m| m.store())
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn test_runtime_config_default() {
85        let config = RuntimeConfig::default();
86        assert_eq!(config.max_delegation_depth, 10);
87        assert!(config.enable_graph_memory);
88    }
89}