llm-memory-graph 0.1.0

Graph-based context-tracking and prompt-lineage database for LLM systems
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
//! Event types for Observatory integration
//!
//! This module defines all events that can be emitted by the memory graph
//! for real-time monitoring and analysis.

use crate::types::{
    AgentId, EdgeId, EdgeType, NodeId, NodeType, SessionId, TemplateId, TokenUsage,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Events emitted by the memory graph
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum MemoryGraphEvent {
    /// Node created event
    NodeCreated {
        /// ID of the created node
        node_id: NodeId,
        /// Type of node created
        node_type: NodeType,
        /// Session ID (if applicable)
        #[serde(skip_serializing_if = "Option::is_none")]
        session_id: Option<SessionId>,
        /// Event timestamp
        timestamp: DateTime<Utc>,
        /// Additional metadata
        #[serde(default, skip_serializing_if = "HashMap::is_empty")]
        metadata: HashMap<String, String>,
    },

    /// Edge created event
    EdgeCreated {
        /// ID of the created edge
        edge_id: EdgeId,
        /// Type of edge created
        edge_type: EdgeType,
        /// Source node ID
        from: NodeId,
        /// Target node ID
        to: NodeId,
        /// Event timestamp
        timestamp: DateTime<Utc>,
    },

    /// Prompt submitted event
    PromptSubmitted {
        /// ID of the prompt
        prompt_id: NodeId,
        /// Session ID
        session_id: SessionId,
        /// Length of prompt content
        content_length: usize,
        /// Model used for the prompt
        model: String,
        /// Event timestamp
        timestamp: DateTime<Utc>,
    },

    /// Response generated event
    ResponseGenerated {
        /// ID of the response
        response_id: NodeId,
        /// ID of the prompt this responds to
        prompt_id: NodeId,
        /// Length of response content
        content_length: usize,
        /// Token usage statistics
        tokens_used: TokenUsage,
        /// Response generation latency in milliseconds
        latency_ms: u64,
        /// Event timestamp
        timestamp: DateTime<Utc>,
    },

    /// Tool invoked event
    ToolInvoked {
        /// ID of the tool invocation
        tool_id: NodeId,
        /// Name of the tool
        tool_name: String,
        /// Whether the tool invocation succeeded
        success: bool,
        /// Tool execution duration in milliseconds
        duration_ms: u64,
        /// Event timestamp
        timestamp: DateTime<Utc>,
    },

    /// Agent handoff event
    AgentHandoff {
        /// Agent transferring from
        from_agent: AgentId,
        /// Agent transferring to
        to_agent: AgentId,
        /// Session ID
        session_id: SessionId,
        /// Reason for handoff
        reason: String,
        /// Event timestamp
        timestamp: DateTime<Utc>,
    },

    /// Template instantiated event
    TemplateInstantiated {
        /// Template ID
        template_id: TemplateId,
        /// Prompt ID created from template
        prompt_id: NodeId,
        /// Template version used
        version: String,
        /// Variable bindings used
        variables: HashMap<String, String>,
        /// Event timestamp
        timestamp: DateTime<Utc>,
    },

    /// Query executed event
    QueryExecuted {
        /// Type of query executed
        query_type: String,
        /// Number of results returned
        results_count: usize,
        /// Query execution duration in milliseconds
        duration_ms: u64,
        /// Event timestamp
        timestamp: DateTime<Utc>,
    },
}

impl MemoryGraphEvent {
    /// Get a unique key for this event (for Kafka partitioning)
    pub fn key(&self) -> String {
        match self {
            Self::NodeCreated { node_id, .. } => format!("node:{}", node_id),
            Self::EdgeCreated { edge_id, .. } => format!("edge:{}", edge_id),
            Self::PromptSubmitted { session_id, .. } | Self::AgentHandoff { session_id, .. } => {
                format!("session:{}", session_id)
            }
            Self::ResponseGenerated { prompt_id, .. } => {
                format!("prompt:{}", prompt_id)
            }
            Self::ToolInvoked { tool_id, .. } => format!("tool:{}", tool_id),
            Self::TemplateInstantiated { template_id, .. } => format!("template:{}", template_id),
            Self::QueryExecuted { query_type, .. } => format!("query:{}", query_type),
        }
    }

    /// Get the event type name
    pub fn event_type(&self) -> &'static str {
        match self {
            Self::NodeCreated { .. } => "node_created",
            Self::EdgeCreated { .. } => "edge_created",
            Self::PromptSubmitted { .. } => "prompt_submitted",
            Self::ResponseGenerated { .. } => "response_generated",
            Self::ToolInvoked { .. } => "tool_invoked",
            Self::AgentHandoff { .. } => "agent_handoff",
            Self::TemplateInstantiated { .. } => "template_instantiated",
            Self::QueryExecuted { .. } => "query_executed",
        }
    }

    /// Get the timestamp of this event
    pub fn timestamp(&self) -> DateTime<Utc> {
        match self {
            Self::NodeCreated { timestamp, .. }
            | Self::EdgeCreated { timestamp, .. }
            | Self::PromptSubmitted { timestamp, .. }
            | Self::ResponseGenerated { timestamp, .. }
            | Self::ToolInvoked { timestamp, .. }
            | Self::AgentHandoff { timestamp, .. }
            | Self::TemplateInstantiated { timestamp, .. }
            | Self::QueryExecuted { timestamp, .. } => *timestamp,
        }
    }
}

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

    #[test]
    fn test_event_serialization() {
        let event = MemoryGraphEvent::NodeCreated {
            node_id: NodeId::new(),
            node_type: NodeType::Prompt,
            session_id: Some(SessionId::new()),
            timestamp: Utc::now(),
            metadata: HashMap::new(),
        };

        let json = serde_json::to_string(&event).unwrap();
        let deserialized: MemoryGraphEvent = serde_json::from_str(&json).unwrap();

        assert_eq!(event.event_type(), deserialized.event_type());
    }

    #[test]
    fn test_event_key_generation() {
        let node_id = NodeId::new();
        let event = MemoryGraphEvent::NodeCreated {
            node_id,
            node_type: NodeType::Prompt,
            session_id: None,
            timestamp: Utc::now(),
            metadata: HashMap::new(),
        };

        let key = event.key();
        assert!(key.starts_with("node:"));
        assert!(key.contains(&node_id.to_string()));
    }

    #[test]
    fn test_all_event_types() {
        let events = vec![
            MemoryGraphEvent::NodeCreated {
                node_id: NodeId::new(),
                node_type: NodeType::Prompt,
                session_id: None,
                timestamp: Utc::now(),
                metadata: HashMap::new(),
            },
            MemoryGraphEvent::EdgeCreated {
                edge_id: EdgeId::new(),
                edge_type: EdgeType::Follows,
                from: NodeId::new(),
                to: NodeId::new(),
                timestamp: Utc::now(),
            },
            MemoryGraphEvent::PromptSubmitted {
                prompt_id: NodeId::new(),
                session_id: SessionId::new(),
                content_length: 100,
                model: "gpt-4".to_string(),
                timestamp: Utc::now(),
            },
            MemoryGraphEvent::ResponseGenerated {
                response_id: NodeId::new(),
                prompt_id: NodeId::new(),
                content_length: 200,
                tokens_used: TokenUsage::new(10, 20),
                latency_ms: 150,
                timestamp: Utc::now(),
            },
            MemoryGraphEvent::ToolInvoked {
                tool_id: NodeId::new(),
                tool_name: "calculator".to_string(),
                success: true,
                duration_ms: 50,
                timestamp: Utc::now(),
            },
            MemoryGraphEvent::AgentHandoff {
                from_agent: AgentId::new(),
                to_agent: AgentId::new(),
                session_id: SessionId::new(),
                reason: "specialized task".to_string(),
                timestamp: Utc::now(),
            },
            MemoryGraphEvent::TemplateInstantiated {
                template_id: TemplateId::new(),
                prompt_id: NodeId::new(),
                version: "1.0.0".to_string(),
                variables: HashMap::new(),
                timestamp: Utc::now(),
            },
            MemoryGraphEvent::QueryExecuted {
                query_type: "session_nodes".to_string(),
                results_count: 42,
                duration_ms: 25,
                timestamp: Utc::now(),
            },
        ];

        for event in events {
            assert!(!event.key().is_empty());
            assert!(!event.event_type().is_empty());
        }
    }

    #[test]
    fn test_tool_invoked_event() {
        let event = MemoryGraphEvent::ToolInvoked {
            tool_id: NodeId::new(),
            tool_name: "weather_api".to_string(),
            success: true,
            duration_ms: 250,
            timestamp: Utc::now(),
        };

        assert_eq!(event.event_type(), "tool_invoked");
        assert!(event.key().starts_with("tool:"));

        let json = serde_json::to_string(&event).unwrap();
        let deserialized: MemoryGraphEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event.event_type(), deserialized.event_type());
    }

    #[test]
    fn test_agent_handoff_event() {
        let from_agent = AgentId::new();
        let to_agent = AgentId::new();
        let session_id = SessionId::new();

        let event = MemoryGraphEvent::AgentHandoff {
            from_agent,
            to_agent,
            session_id,
            reason: "expertise required".to_string(),
            timestamp: Utc::now(),
        };

        assert_eq!(event.event_type(), "agent_handoff");
        assert!(event.key().contains(&session_id.to_string()));

        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("expertise required"));
    }

    #[test]
    fn test_template_instantiated_event() {
        let template_id = TemplateId::new();
        let prompt_id = NodeId::new();
        let mut variables = HashMap::new();
        variables.insert("name".to_string(), "Alice".to_string());
        variables.insert("topic".to_string(), "AI".to_string());

        let event = MemoryGraphEvent::TemplateInstantiated {
            template_id,
            prompt_id,
            version: "2.1.0".to_string(),
            variables: variables.clone(),
            timestamp: Utc::now(),
        };

        assert_eq!(event.event_type(), "template_instantiated");
        assert!(event.key().contains(&template_id.to_string()));

        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("Alice"));
        assert!(json.contains("2.1.0"));
    }

    #[test]
    fn test_query_executed_event() {
        let event = MemoryGraphEvent::QueryExecuted {
            query_type: "filtered_search".to_string(),
            results_count: 128,
            duration_ms: 45,
            timestamp: Utc::now(),
        };

        assert_eq!(event.event_type(), "query_executed");
        assert!(event.key().contains("filtered_search"));

        let json = serde_json::to_string(&event).unwrap();
        let deserialized: MemoryGraphEvent = serde_json::from_str(&json).unwrap();

        if let MemoryGraphEvent::QueryExecuted { results_count, .. } = deserialized {
            assert_eq!(results_count, 128);
        } else {
            panic!("Wrong event type");
        }
    }

    #[test]
    fn test_event_timestamp() {
        let timestamp = Utc::now();
        let event = MemoryGraphEvent::NodeCreated {
            node_id: NodeId::new(),
            node_type: NodeType::Response,
            session_id: Some(SessionId::new()),
            timestamp,
            metadata: HashMap::new(),
        };

        assert_eq!(event.timestamp(), timestamp);
    }

    #[test]
    fn test_metadata_serialization() {
        let mut metadata = HashMap::new();
        metadata.insert("model".to_string(), "gpt-4".to_string());
        metadata.insert("temperature".to_string(), "0.7".to_string());

        let event = MemoryGraphEvent::NodeCreated {
            node_id: NodeId::new(),
            node_type: NodeType::Prompt,
            session_id: Some(SessionId::new()),
            timestamp: Utc::now(),
            metadata: metadata.clone(),
        };

        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("gpt-4"));
        assert!(json.contains("0.7"));

        let deserialized: MemoryGraphEvent = serde_json::from_str(&json).unwrap();
        if let MemoryGraphEvent::NodeCreated { metadata: meta, .. } = deserialized {
            assert_eq!(meta.get("model").unwrap(), "gpt-4");
            assert_eq!(meta.get("temperature").unwrap(), "0.7");
        } else {
            panic!("Wrong event type");
        }
    }

    #[test]
    fn test_response_generated_with_token_usage() {
        let tokens = TokenUsage::new(150, 300);
        let event = MemoryGraphEvent::ResponseGenerated {
            response_id: NodeId::new(),
            prompt_id: NodeId::new(),
            content_length: 500,
            tokens_used: tokens,
            latency_ms: 1250,
            timestamp: Utc::now(),
        };

        let json = serde_json::to_string(&event).unwrap();
        let deserialized: MemoryGraphEvent = serde_json::from_str(&json).unwrap();

        if let MemoryGraphEvent::ResponseGenerated {
            tokens_used,
            latency_ms,
            ..
        } = deserialized
        {
            assert_eq!(tokens_used.prompt_tokens, 150);
            assert_eq!(tokens_used.completion_tokens, 300);
            assert_eq!(latency_ms, 1250);
        } else {
            panic!("Wrong event type");
        }
    }
}