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
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
//! Integration tests for enhanced edge types with strongly-typed properties
//!
//! Tests all 5 new edge types:
//! - INSTANTIATES (Prompt → Template)
//! - INHERITS (Template → Template)
//! - INVOKES (Response → ToolInvocation)
//! - TRANSFERS_TO (Response → Agent)
//! - REFERENCES (Prompt → ExternalContext)

use llm_memory_graph::{
    AgentNode, Config, ContextType, EdgeType, InheritsProperties, InstantiatesProperties,
    InvokesProperties, MemoryGraph, Priority, PromptTemplate, ReferencesProperties, TokenUsage,
    ToolInvocation, TransfersToProperties, VariableSpec,
};
use std::collections::HashMap;
use tempfile::tempdir;

#[test]
fn test_instantiates_edge_full_workflow() {
    let dir = tempdir().unwrap();
    let config = Config::new(dir.path());
    let graph = MemoryGraph::open(config).unwrap();

    let session = graph.create_session().unwrap();

    // Create a template
    let variables = vec![VariableSpec::new(
        "topic".to_string(),
        "String".to_string(),
        true,
        "Topic to explain".to_string(),
    )];

    let template = PromptTemplate::new(
        "Explanation Template".to_string(),
        "Explain {{topic}} in simple terms".to_string(),
        variables,
    );

    let template_node_id = template.node_id;
    let template_version = template.version.to_string();
    graph.create_template(template.clone()).unwrap();

    // Instantiate the template
    let mut values = HashMap::new();
    values.insert("topic".to_string(), "quantum computing".to_string());

    let prompt_text = template.instantiate(&values).unwrap();
    let prompt_id = graph.add_prompt(session.id, prompt_text, None).unwrap();

    // Create INSTANTIATES edge with properties
    let instantiate_props = InstantiatesProperties::new(template_version, values);
    let edge =
        llm_memory_graph::types::Edge::instantiates(prompt_id, template_node_id, instantiate_props);

    graph.add_edge(edge.from, edge.to, edge.edge_type).unwrap();

    // Verify edge was created
    let edges = graph.get_outgoing_edges(prompt_id).unwrap();
    let inst_edges: Vec<_> = edges
        .iter()
        .filter(|e| e.edge_type == EdgeType::Instantiates)
        .collect();

    assert_eq!(inst_edges.len(), 1);
    assert_eq!(inst_edges[0].to, template_node_id);
}

#[test]
fn test_inherits_edge_full_workflow() {
    let dir = tempdir().unwrap();
    let config = Config::new(dir.path());
    let graph = MemoryGraph::open(config).unwrap();

    // Create parent template
    let parent = PromptTemplate::new(
        "Base Template".to_string(),
        "Question: {{question}}".to_string(),
        vec![],
    );

    let parent_node_id = parent.node_id;
    graph.create_template(parent).unwrap();

    // Create child template
    let child = PromptTemplate::new(
        "Enhanced Template".to_string(),
        "Question: {{question}}\nContext: {{context}}".to_string(),
        vec![],
    );

    let child_node_id = child.node_id;
    graph.create_template(child).unwrap();

    // Create INHERITS edge with properties
    let inherits_props = InheritsProperties::new(
        vec!["template".to_string(), "variables".to_string()],
        "Added context variable".to_string(),
        1,
    );

    let edge =
        llm_memory_graph::types::Edge::inherits(child_node_id, parent_node_id, inherits_props);

    graph.add_edge(edge.from, edge.to, edge.edge_type).unwrap();

    // Verify edge
    let edges = graph.get_outgoing_edges(child_node_id).unwrap();
    let inherit_edges: Vec<_> = edges
        .iter()
        .filter(|e| e.edge_type == EdgeType::Inherits)
        .collect();

    assert_eq!(inherit_edges.len(), 1);
    assert_eq!(inherit_edges[0].to, parent_node_id);
}

#[test]
fn test_invokes_edge_full_workflow() {
    let dir = tempdir().unwrap();
    let config = Config::new(dir.path());
    let graph = MemoryGraph::open(config).unwrap();

    let session = graph.create_session().unwrap();
    let prompt_id = graph
        .add_prompt(session.id, "Calculate 2+2".to_string(), None)
        .unwrap();

    let usage = TokenUsage::new(10, 20);
    let response_id = graph
        .add_response(prompt_id, "The answer is 4".to_string(), usage, None)
        .unwrap();

    // Create tool invocations (these automatically create basic INVOKES edges)
    let tool1 = ToolInvocation::new(
        response_id,
        "calculator".to_string(),
        serde_json::json!({"operation": "add", "args": [2, 2]}),
    );
    graph.add_tool_invocation(tool1).unwrap();

    let tool2 = ToolInvocation::new(
        response_id,
        "formatter".to_string(),
        serde_json::json!({"format": "decimal"}),
    );
    graph.add_tool_invocation(tool2).unwrap();

    // Note: add_tool_invocation already creates INVOKES edges automatically
    // To use edges with custom properties, you would create tool nodes directly
    // and then create edges with properties separately

    // Verify edges
    let edges = graph.get_outgoing_edges(response_id).unwrap();
    let invoke_edges: Vec<_> = edges
        .iter()
        .filter(|e| e.edge_type == EdgeType::Invokes)
        .collect();

    assert_eq!(invoke_edges.len(), 2);
}

#[test]
fn test_transfers_to_edge_full_workflow() {
    let dir = tempdir().unwrap();
    let config = Config::new(dir.path());
    let graph = MemoryGraph::open(config).unwrap();

    let session = graph.create_session().unwrap();
    let prompt_id = graph
        .add_prompt(
            session.id,
            "I need help with quantum physics".to_string(),
            None,
        )
        .unwrap();

    let usage = TokenUsage::new(15, 25);
    let response_id = graph
        .add_response(
            prompt_id,
            "Let me transfer you to our physics specialist".to_string(),
            usage,
            None,
        )
        .unwrap();

    // Create specialist agent
    let agent = AgentNode::new(
        "Physics Specialist".to_string(),
        "physics_expert".to_string(),
        vec!["quantum_physics".to_string(), "relativity".to_string()],
    );

    let agent_node_id = agent.node_id;
    graph.add_agent(agent).unwrap();

    // Create TRANSFERS_TO edge with properties
    let transfer_props = TransfersToProperties::new(
        "User needs expert physics assistance".to_string(),
        "Question about quantum physics fundamentals".to_string(),
        Priority::High,
    );

    let edge =
        llm_memory_graph::types::Edge::transfers_to(response_id, agent_node_id, transfer_props);

    graph.add_edge(edge.from, edge.to, edge.edge_type).unwrap();

    // Verify edge
    let edges = graph.get_outgoing_edges(response_id).unwrap();
    let transfer_edges: Vec<_> = edges
        .iter()
        .filter(|e| e.edge_type == EdgeType::TransfersTo)
        .collect();

    assert_eq!(transfer_edges.len(), 1);
    assert_eq!(transfer_edges[0].to, agent_node_id);
}

#[test]
fn test_references_edge_full_workflow() {
    let dir = tempdir().unwrap();
    let config = Config::new(dir.path());
    let graph = MemoryGraph::open(config).unwrap();

    let session = graph.create_session().unwrap();

    // Create a prompt that references external context
    let prompt_id = graph
        .add_prompt(
            session.id,
            "Based on the documentation, how does feature X work?".to_string(),
            None,
        )
        .unwrap();

    // Create placeholder nodes for external context
    // (In a real system, these would be actual external context nodes)
    let doc_context_id = graph
        .add_prompt(
            session.id,
            "CONTEXT: Documentation for feature X".to_string(),
            None,
        )
        .unwrap();

    let web_context_id = graph
        .add_prompt(
            session.id,
            "CONTEXT: Web article about feature X".to_string(),
            None,
        )
        .unwrap();

    // Create REFERENCES edges with properties
    let ref1_props =
        ReferencesProperties::new(ContextType::Document, 0.95, Some("section_3.2".to_string()));

    let edge1 = llm_memory_graph::types::Edge::references(prompt_id, doc_context_id, ref1_props);
    graph
        .add_edge(edge1.from, edge1.to, edge1.edge_type)
        .unwrap();

    let ref2_props = ReferencesProperties::new(ContextType::WebPage, 0.72, None);

    let edge2 = llm_memory_graph::types::Edge::references(prompt_id, web_context_id, ref2_props);
    graph
        .add_edge(edge2.from, edge2.to, edge2.edge_type)
        .unwrap();

    // Verify edges
    let edges = graph.get_outgoing_edges(prompt_id).unwrap();
    let ref_edges: Vec<_> = edges
        .iter()
        .filter(|e| e.edge_type == EdgeType::References)
        .collect();

    assert_eq!(ref_edges.len(), 2);
}

#[test]
fn test_edge_property_persistence() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().to_path_buf();

    let prompt_id;
    let template_id;

    // Create edge with properties and close
    {
        let config = Config::new(&db_path);
        let graph = MemoryGraph::open(config).unwrap();
        let session = graph.create_session().unwrap();

        let template = PromptTemplate::new("Test".to_string(), "{{content}}".to_string(), vec![]);
        template_id = template.node_id;
        graph.create_template(template).unwrap();

        prompt_id = graph
            .add_prompt(session.id, "Test prompt".to_string(), None)
            .unwrap();

        let mut bindings = HashMap::new();
        bindings.insert("content".to_string(), "Test".to_string());

        let props = InstantiatesProperties::new("1.0.0".to_string(), bindings);
        let edge = llm_memory_graph::types::Edge::instantiates(prompt_id, template_id, props);

        graph.add_edge(edge.from, edge.to, edge.edge_type).unwrap();
        graph.flush().unwrap();
    }

    // Reopen and verify
    {
        let config = Config::new(&db_path);
        let graph = MemoryGraph::open(config).unwrap();

        let edges = graph.get_outgoing_edges(prompt_id).unwrap();
        let inst_edges: Vec<_> = edges
            .iter()
            .filter(|e| e.edge_type == EdgeType::Instantiates)
            .collect();

        assert_eq!(inst_edges.len(), 1);
        assert_eq!(inst_edges[0].to, template_id);
    }
}

#[test]
fn test_complex_multi_edge_workflow() {
    let dir = tempdir().unwrap();
    let config = Config::new(dir.path());
    let graph = MemoryGraph::open(config).unwrap();

    let session = graph.create_session().unwrap();

    // Create template
    let template = PromptTemplate::new(
        "Multi-Edge Test".to_string(),
        "Process: {{action}}".to_string(),
        vec![],
    );
    let template_id = template.node_id;
    graph.create_template(template).unwrap();

    // Create prompt from template
    let mut bindings = HashMap::new();
    bindings.insert("action".to_string(), "analysis".to_string());

    let prompt_id = graph
        .add_prompt(session.id, "Process: analysis".to_string(), None)
        .unwrap();

    // INSTANTIATES edge
    let inst_props = InstantiatesProperties::new("1.0.0".to_string(), bindings);
    let inst_edge = llm_memory_graph::types::Edge::instantiates(prompt_id, template_id, inst_props);
    graph
        .add_edge(inst_edge.from, inst_edge.to, inst_edge.edge_type)
        .unwrap();

    // REFERENCES edge (external context)
    let context_id = graph
        .add_prompt(session.id, "CONTEXT".to_string(), None)
        .unwrap();
    let ref_props = ReferencesProperties::new(ContextType::Document, 0.88, None);
    let ref_edge = llm_memory_graph::types::Edge::references(prompt_id, context_id, ref_props);
    graph
        .add_edge(ref_edge.from, ref_edge.to, ref_edge.edge_type)
        .unwrap();

    // Response with tools
    let usage = TokenUsage::new(20, 30);
    let response_id = graph
        .add_response(prompt_id, "Analysis complete".to_string(), usage, None)
        .unwrap();

    // INVOKES edge
    let tool = ToolInvocation::new(
        response_id,
        "analyzer".to_string(),
        serde_json::json!({"depth": "full"}),
    );
    let tool_id = tool.id;
    graph.add_tool_invocation(tool).unwrap();

    let invoke_props = InvokesProperties::new(0, true, true);
    let invoke_edge = llm_memory_graph::types::Edge::invokes(response_id, tool_id, invoke_props);
    graph
        .add_edge(invoke_edge.from, invoke_edge.to, invoke_edge.edge_type)
        .unwrap();

    // TRANSFERS_TO edge
    let agent = AgentNode::new("Specialist".to_string(), "specialist".to_string(), vec![]);
    let agent_id = agent.node_id;
    graph.add_agent(agent).unwrap();

    let transfer_props = TransfersToProperties::new(
        "Need specialist review".to_string(),
        "Analysis results".to_string(),
        Priority::Normal,
    );
    let transfer_edge =
        llm_memory_graph::types::Edge::transfers_to(response_id, agent_id, transfer_props);
    graph
        .add_edge(
            transfer_edge.from,
            transfer_edge.to,
            transfer_edge.edge_type,
        )
        .unwrap();

    // Verify all edges
    let prompt_edges = graph.get_outgoing_edges(prompt_id).unwrap();
    assert!(prompt_edges
        .iter()
        .any(|e| e.edge_type == EdgeType::Instantiates));
    assert!(prompt_edges
        .iter()
        .any(|e| e.edge_type == EdgeType::References));

    let response_edges = graph.get_outgoing_edges(response_id).unwrap();
    assert!(response_edges
        .iter()
        .any(|e| e.edge_type == EdgeType::Invokes));
    assert!(response_edges
        .iter()
        .any(|e| e.edge_type == EdgeType::TransfersTo));
}

#[test]
fn test_priority_levels_in_transfers() {
    let dir = tempdir().unwrap();
    let config = Config::new(dir.path());
    let graph = MemoryGraph::open(config).unwrap();

    let session = graph.create_session().unwrap();
    let agent = AgentNode::new("Agent".to_string(), "agent".to_string(), vec![]);
    let agent_id = agent.node_id;
    graph.add_agent(agent).unwrap();

    let priorities = vec![
        Priority::Low,
        Priority::Normal,
        Priority::High,
        Priority::Critical,
    ];

    for (i, priority) in priorities.iter().enumerate() {
        let prompt_id = graph
            .add_prompt(session.id, format!("Prompt {}", i), None)
            .unwrap();

        let usage = TokenUsage::new(10, 10);
        let response_id = graph
            .add_response(prompt_id, format!("Response {}", i), usage, None)
            .unwrap();

        let props = TransfersToProperties::new(
            format!("Reason {}", i),
            format!("Context {}", i),
            *priority,
        );

        let edge = llm_memory_graph::types::Edge::transfers_to(response_id, agent_id, props);
        graph.add_edge(edge.from, edge.to, edge.edge_type).unwrap();
    }

    // Verify all transfer edges were created
    let stats = graph.stats().unwrap();
    assert!(stats.edge_count >= 4);
}

#[test]
fn test_context_types_in_references() {
    let dir = tempdir().unwrap();
    let config = Config::new(dir.path());
    let graph = MemoryGraph::open(config).unwrap();

    let session = graph.create_session().unwrap();
    let prompt_id = graph
        .add_prompt(session.id, "Multi-context prompt".to_string(), None)
        .unwrap();

    let context_types = vec![
        ContextType::Document,
        ContextType::WebPage,
        ContextType::Database,
        ContextType::VectorSearch,
        ContextType::Memory,
    ];

    for (i, ctx_type) in context_types.iter().enumerate() {
        let context_id = graph
            .add_prompt(session.id, format!("Context {}", i), None)
            .unwrap();

        let props = ReferencesProperties::new(*ctx_type, 0.8, Some(format!("chunk_{}", i)));
        let edge = llm_memory_graph::types::Edge::references(prompt_id, context_id, props);
        graph.add_edge(edge.from, edge.to, edge.edge_type).unwrap();
    }

    // Verify all reference edges
    let edges = graph.get_outgoing_edges(prompt_id).unwrap();
    let ref_edges: Vec<_> = edges
        .iter()
        .filter(|e| e.edge_type == EdgeType::References)
        .collect();

    assert_eq!(ref_edges.len(), 5);
}