pub struct MemoryGraph { /* private fields */ }Expand description
Main interface for interacting with the memory graph
MemoryGraph provides a thread-safe, high-level API for managing conversation
sessions, prompts, responses, and their relationships in a graph structure.
§Examples
use llm_memory_graph::{MemoryGraph, Config};
let config = Config::new("./data/my_graph.db");
let graph = MemoryGraph::open(config)?;
let session = graph.create_session()?;
let prompt_id = graph.add_prompt(session.id, "What is Rust?".to_string(), None)?;Implementations§
Source§impl MemoryGraph
impl MemoryGraph
Sourcepub fn open(config: Config) -> Result<Self>
pub fn open(config: Config) -> Result<Self>
Open or create a memory graph with the given configuration
This will create the database directory if it doesn’t exist and initialize all necessary storage trees.
§Errors
Returns an error if:
- The database path is invalid or inaccessible
- Storage initialization fails
- Existing data is corrupted
§Examples
use llm_memory_graph::{MemoryGraph, Config};
let config = Config::new("./data/graph.db");
let graph = MemoryGraph::open(config)?;Sourcepub fn create_session(&self) -> Result<ConversationSession>
pub fn create_session(&self) -> Result<ConversationSession>
Create a new conversation session
Sessions are used to group related prompts and responses together. Each session has a unique ID and can store custom metadata.
§Errors
Returns an error if the session cannot be persisted to storage.
§Examples
let session = graph.create_session()?;
println!("Created session: {}", session.id);Sourcepub fn create_session_with_metadata(
&self,
metadata: HashMap<String, String>,
) -> Result<ConversationSession>
pub fn create_session_with_metadata( &self, metadata: HashMap<String, String>, ) -> Result<ConversationSession>
Sourcepub fn get_session(&self, session_id: SessionId) -> Result<ConversationSession>
pub fn get_session(&self, session_id: SessionId) -> Result<ConversationSession>
Sourcepub fn add_prompt(
&self,
session_id: SessionId,
content: String,
metadata: Option<PromptMetadata>,
) -> Result<NodeId>
pub fn add_prompt( &self, session_id: SessionId, content: String, metadata: Option<PromptMetadata>, ) -> Result<NodeId>
Add a prompt to a session
This creates a new prompt node and automatically creates edges linking it to the session and to the previous prompt if one exists.
§Errors
Returns an error if:
- The session doesn’t exist
- Storage operations fail
§Examples
let prompt_id = graph.add_prompt(
session.id,
"Explain quantum entanglement".to_string(),
None,
)?;Sourcepub fn add_response(
&self,
prompt_id: NodeId,
content: String,
usage: TokenUsage,
metadata: Option<ResponseMetadata>,
) -> Result<NodeId>
pub fn add_response( &self, prompt_id: NodeId, content: String, usage: TokenUsage, metadata: Option<ResponseMetadata>, ) -> Result<NodeId>
Add a response to a prompt
This creates a response node and a RespondsTo edge linking it to the prompt.
§Errors
Returns an error if:
- The prompt doesn’t exist
- Storage operations fail
§Examples
let usage = TokenUsage::new(10, 20);
let response_id = graph.add_response(
prompt_id,
"Quantum entanglement is...".to_string(),
usage,
None,
)?;Sourcepub fn add_tool_invocation(&self, tool: ToolInvocation) -> Result<NodeId>
pub fn add_tool_invocation(&self, tool: ToolInvocation) -> Result<NodeId>
Add a tool invocation node to the graph
This creates a tool invocation record and automatically creates an INVOKES edge from the response to the tool invocation.
§Arguments
tool- The tool invocation to add
§Returns
The node ID of the created tool invocation
§Example
let params = serde_json::json!({"operation": "add", "a": 2, "b": 3});
let tool = ToolInvocation::new(response_id, "calculator".to_string(), params);
let tool_id = graph.add_tool_invocation(tool)?;Sourcepub fn update_tool_invocation(
&self,
tool_id: NodeId,
success: bool,
result_or_error: String,
duration_ms: u64,
) -> Result<()>
pub fn update_tool_invocation( &self, tool_id: NodeId, success: bool, result_or_error: String, duration_ms: u64, ) -> Result<()>
Update an existing tool invocation with results
This method updates a tool invocation’s status, result, and duration after execution.
§Arguments
tool_id- The ID of the tool invocation to updatesuccess- Whether the tool execution was successfulresult_or_error- Either the result (if successful) or error message (if failed)duration_ms- Execution duration in milliseconds
§Example
// Mark tool invocation as successful
let result = serde_json::json!({"result": 5});
graph.update_tool_invocation(tool_id, true, serde_json::to_string(&result)?, 150)?;Sourcepub fn get_response_tools(
&self,
response_id: NodeId,
) -> Result<Vec<ToolInvocation>>
pub fn get_response_tools( &self, response_id: NodeId, ) -> Result<Vec<ToolInvocation>>
Sourcepub fn update_agent(&self, agent: AgentNode) -> Result<()>
pub fn update_agent(&self, agent: AgentNode) -> Result<()>
Sourcepub fn assign_agent_to_prompt(
&self,
prompt_id: NodeId,
agent_node_id: NodeId,
) -> Result<()>
pub fn assign_agent_to_prompt( &self, prompt_id: NodeId, agent_node_id: NodeId, ) -> Result<()>
Sourcepub fn get_prompt_agent(&self, prompt_id: NodeId) -> Result<AgentNode>
pub fn get_prompt_agent(&self, prompt_id: NodeId) -> Result<AgentNode>
Sourcepub fn stats(&self) -> Result<StorageStats>
pub fn stats(&self) -> Result<StorageStats>
Sourcepub fn create_template(&self, template: PromptTemplate) -> Result<TemplateId>
pub fn create_template(&self, template: PromptTemplate) -> Result<TemplateId>
Create and store a new prompt template
Templates are versioned prompt structures that can be instantiated with variables.
§Errors
Returns an error if storage fails.
§Examples
let variables = vec![
VariableSpec::new(
"user_input".to_string(),
"String".to_string(),
true,
"User's question".to_string(),
),
];
let template = PromptTemplate::new(
"Question Answering".to_string(),
"Answer this question: {{user_input}}".to_string(),
variables,
);
let template_id = graph.create_template(template)?;Sourcepub fn get_template(&self, _template_id: TemplateId) -> Result<PromptTemplate>
pub fn get_template(&self, _template_id: TemplateId) -> Result<PromptTemplate>
Sourcepub fn get_template_by_node_id(&self, node_id: NodeId) -> Result<PromptTemplate>
pub fn get_template_by_node_id(&self, node_id: NodeId) -> Result<PromptTemplate>
Sourcepub fn update_template(&self, template: PromptTemplate) -> Result<()>
pub fn update_template(&self, template: PromptTemplate) -> Result<()>
Update an existing template
This will store the updated template data. Note that the template’s version should be bumped appropriately before calling this method.
§Errors
Returns an error if storage update fails.
§Examples
let mut template = graph.get_template_by_node_id(node_id)?;
template.record_usage();
template.bump_version(VersionLevel::Patch);
graph.update_template(template)?;Sourcepub fn link_prompt_to_template(
&self,
prompt_id: NodeId,
template_node_id: NodeId,
) -> Result<()>
pub fn link_prompt_to_template( &self, prompt_id: NodeId, template_node_id: NodeId, ) -> Result<()>
Link a prompt to the template it was instantiated from
Creates an Instantiates edge from the prompt to the template.
§Errors
Returns an error if storage fails.
§Examples
let mut values = HashMap::new();
values.insert("name".to_string(), "World".to_string());
let prompt_text = template.instantiate(&values)?;
let prompt_id = graph.add_prompt(session.id, prompt_text, None)?;
graph.link_prompt_to_template(prompt_id, template_node_id)?;Sourcepub fn create_template_from_parent(
&self,
template: PromptTemplate,
parent_node_id: NodeId,
) -> Result<TemplateId>
pub fn create_template_from_parent( &self, template: PromptTemplate, parent_node_id: NodeId, ) -> Result<TemplateId>
Create a new template that inherits from a parent template
This creates the new template and automatically establishes an Inherits edge.
§Errors
Returns an error if storage fails.
§Examples
let child = PromptTemplate::from_parent(
parent_id,
"Child Template".to_string(),
"Extended: {{x}} with {{y}}".to_string(),
vec![],
);
let child_id = graph.create_template_from_parent(child, parent_node_id)?;