| Crates.io | cortex-mem-rig |
| lib.rs | cortex-mem-rig |
| version | 1.0.0 |
| created_at | 2025-12-31 07:40:38.779664+00 |
| updated_at | 2025-12-31 07:40:38.779664+00 |
| description | Rig framework integration for Rust agent memory system |
| homepage | |
| repository | |
| max_upload_size | |
| id | 2014187 |
| size | 110,352 |
This crate provides integration between the cortex-mem memory system and the RIG AI agent framework. It offers tools that AI agents can use to store, search, and retrieve memories.
The cortex-mem-rig crate has been refactored to provide a consistent interface with cortex-mem-mcp. It now offers four distinct tools that mirror the MCP protocol tools:
store_memory - Store a new memoryquery_memory - Search memories using semantic similaritylist_memories - List memories with optional filteringget_memory - Retrieve a specific memory by IDThis design ensures that AI agents have a consistent experience whether they're using MCP or RIG to interface with the cortex-mem system.
Add this to your Cargo.toml:
[dependencies]
cortex-mem-core = { version = "0.1" }
cortex-mem-config = { version = "0.1" }
cortex-mem-rig = { version = "0.1" }
rig = { version = "0.1" }
tokio = { version = "1" }
tracing = "0.1"
tracing-subscriber = "0.3"
use std::sync::Arc;
use cortex_mem_config::Config;
use cortex_mem_core::{
init::initialize_memory_system,
memory::MemoryManager,
};
use cortex_mem_rig:: create_memory_tools;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load configuration
let config = Config::load("config.toml")?;
// Initialize memory system
let (vector_store, llm_client) = initialize_memory_system(&config).await?;
// Create memory manager
let memory_manager = Arc::new(MemoryManager::new(
vector_store,
llm_client,
config.memory.clone(),
));
// Create memory tools
let memory_tools = create_memory_tools(
memory_manager,
&config,
None, // Use default configuration
);
Ok(())
}
use cortex_mem_rig::{
MemoryTools, StoreMemoryArgs, QueryMemoryArgs,
ListMemoriesArgs, GetMemoryArgs
};
// Store a memory
let store_args = StoreMemoryArgs {
content: "The user prefers working in the morning".to_string(),
user_id: Some("user123".to_string()),
agent_id: Some("agent456".to_string()),
memory_type: Some("personal".to_string()),
topics: Some(vec!["preferences".to_string()]),
};
let result = memory_tools.store_memory().call(store_args).await?;
println!("Stored memory: {}", result.success);
// Query memories
let query_args = QueryMemoryArgs {
query: "What are the user's preferences?".to_string(),
k: Some(5),
memory_type: None,
min_salience: Some(0.5),
topics: Some(vec!["preferences".to_string()]),
user_id: Some("user123".to_string()),
agent_id: Some("agent456".to_string()),
};
let result = memory_tools.query_memory().call(query_args).await?;
if let Some(data) = result.data {
println!("Found memories: {}", data);
}
// List memories
let list_args = ListMemoriesArgs {
limit: Some(10),
memory_type: Some("personal".to_string()),
user_id: Some("user123".to_string()),
agent_id: Some("agent456".to_string()),
};
let result = memory_tools.list_memories().call(list_args).await?;
if let Some(data) = result.data {
println!("Memories: {}", data);
}
// Get a specific memory
let get_args = GetMemoryArgs {
memory_id: "memory_123".to_string(),
};
let result = memory_tools.get_memory().call(get_args).await?;
if let Some(data) = result.data {
println!("Memory: {}", data);
}
You can customize the memory tools with a MemoryToolConfig:
use cortex_mem_rig::MemoryToolConfig;
let config = MemoryToolConfig {
default_user_id: Some("default_user".to_string()),
default_agent_id: Some("default_agent".to_string()),
max_search_results: Some(20),
auto_enhance: Some(true),
search_similarity_threshold: Some(0.7),
};
let memory_tools = create_memory_tools(
memory_manager,
&global_config,
Some(config),
);
The tools are designed to work seamlessly with the RIG AI agent framework:
use rig::agent::Agent;
use rig::providers::openai;
// Create an agent with memory capabilities
let agent = Agent::builder()
.model(openai::GPT_4_O) // Example model
.preamble("You are a helpful assistant with access to memories.")
.tool(memory_tools.store_memory())
.tool(memory_tools.query_memory())
.tool(memory_tools.list_memories())
.tool(memory_tools.get_memory())
.build();
// The agent can now use these tools to interact with memories
Stores a new memory in the system.
Parameters:
content (required, string): The content of the memoryuser_id (optional, string): User ID associated with the memoryagent_id (optional, string): Agent ID associated with the memorymemory_type (optional, string): Type of memory (conversational, procedural, factual, semantic, episodic, personal)topics (optional, array of strings): Topics to associate with the memorySearches memories using semantic similarity.
Parameters:
query (required, string): Query string for semantic searchk (optional, integer): Maximum number of results to return (default: 10)memory_type (optional, string): Type of memory to filter bymin_salience (optional, number): Minimum salience/importance score threshold (0-1)topics (optional, array of strings): Topics to filter memories byuser_id (optional, string): User ID to filter memoriesagent_id (optional, string): Agent ID to filter memoriesRetrieves memories with optional filtering.
Parameters:
limit (optional, integer): Maximum number of memories to return (default: 100, max: 1000)memory_type (optional, string): Type of memory to filter byuser_id (optional, string): User ID to filter memoriesagent_id (optional, string): Agent ID to filter memoriesRetrieves a specific memory by its exact ID.
Parameters:
memory_id (required, string): Exact ID of the memory to retrieveIf you were using the old single-tool interface, you can still use the backward-compatible wrapper:
// Old way (deprecated)
use cortex_mem_rig::{create_memory_tool, MemoryTool};
let tool = create_memory_tool(memory_manager, &config, None);
// New way (recommended)
use cortex_mem_rig::create_memory_tools;
let tools = create_memory_tools(memory_manager, &config, None);
The old MemoryTool is still available but marked as deprecated. It now internally uses the new tool structure, so your existing code will continue to work, but you should migrate to the new interface for cleaner code and better type safety.
See the examples directory for complete working examples:
memory_tools_example.rs: Basic usage example with all toolsThe crate shares the core functionality with cortex-mem-mcp through the cortex-mem-tools crate:
cortex-mem-tools: Provides shared operations and tool definitionscortex-mem-rig: Implements RIG-specific tool interfacescortex-mem-mcp: Implements MCP-specific tool interfacesBoth rig and mcp use the same underlying operations, ensuring consistent behavior across different AI frameworks.
This project is licensed under the MIT License.