cortex-mem-rig 2.5.1

Rig framework integration for Cortex Memory V2
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
512
513
514
515
516
517
518
519
520
# Cortex Memory Rig Integration


`cortex-mem-rig` provides integration with the [Rig](https://github.com/0xPlaygrounds/rig) AI framework, enabling AI agents to interact with the Cortex Memory system through tool calls.

## ๐Ÿง  Overview


Cortex Memory Rig implements access tools, allowing AI agents to efficiently retrieve and manipulate memories:

### Three-Tier Access Architecture


| Layer | Size | Purpose | Tool |
|-------|------|---------|------|
| **L0 Abstract** | ~100 tokens | Quick relevance judgment | `abstract_tool` |
| **L1 Overview** | ~2000 tokens | Partial context understanding | `overview_tool` |
| **L2 Full** | Complete content | Deep analysis and processing | `read_tool` |

### Tool Categories


- ๐Ÿ“Š **Tiered Access Tools**: `abstract`, `overview`, `read`
- ๐Ÿ” **Search Tools**: `search`, `find`
- ๐Ÿ“ **Filesystem Tools**: `ls`, `explore`
- ๐Ÿ’พ **Storage Tools**: `store`

## ๐Ÿš€ Quick Start


### Installation


```toml
[dependencies]
cortex-mem-rig = { path = "../cortex-mem-rig" }
cortex-mem-tools = { path = "../cortex-mem-tools" }
cortex-mem-core = { path = "../cortex-mem-core" }
rig-core = "0.11"
tokio = { version = "1", features = ["full"] }
```

### Basic Usage


```rust
use cortex_mem_rig::{MemoryTools, create_memory_tools_with_tenant_and_vector};
use cortex_mem_core::llm::{LLMClientImpl, LLMConfig};
use std::sync::Arc;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create LLM client
    let llm_config = LLMConfig {
        api_base_url: "https://api.openai.com/v1".to_string(),
        api_key: "your-api-key".to_string(),
        model_efficient: "gpt-4o-mini".to_string(),
        temperature: 0.1,
        max_tokens: 4096,
    };
    let llm_client = Arc::new(LLMClientImpl::new(llm_config)?);
    
    // Create memory tools with vector search support
    let memory_tools = create_memory_tools_with_tenant_and_vector(
        "./cortex-data",
        "default",
        llm_client,
        "http://localhost:6333",
        "cortex_memories",
        "https://api.openai.com/v1",
        "your-embedding-key",
        "text-embedding-3-small",
        Some(1536),
        None,
    ).await?;
    
    // Get individual tools for Rig agent
    let abstract_tool = memory_tools.abstract_tool();
    let overview_tool = memory_tools.overview_tool();
    let read_tool = memory_tools.read_tool();
    let search_tool = memory_tools.search_tool();
    let store_tool = memory_tools.store_tool();
    
    // Use with Rig agent...
    
    Ok(())
}
```

## ๐Ÿ“š API Reference


### MemoryTools


Main struct providing access to all memory tools.

```rust
pub struct MemoryTools {
    operations: Arc<MemoryOperations>,
}

impl MemoryTools {
    /// Create from existing MemoryOperations
    pub fn new(operations: Arc<MemoryOperations>) -> Self
    
    /// Get underlying operations
    pub fn operations(&self) -> &Arc<MemoryOperations>
    
    // Tiered Access Tools
    pub fn abstract_tool(&self) -> AbstractTool
    pub fn overview_tool(&self) -> OverviewTool
    pub fn read_tool(&self) -> ReadTool
    
    // Search Tools
    pub fn search_tool(&self) -> SearchTool
    pub fn find_tool(&self) -> FindTool
    
    // Filesystem Tools
    pub fn ls_tool(&self) -> LsTool
    pub fn explore_tool(&self) -> ExploreTool
    
    // Storage Tools
    pub fn store_tool(&self) -> StoreTool
}
```

### Factory Functions


```rust
/// Create MemoryTools from existing MemoryOperations
pub fn create_memory_tools(operations: Arc<MemoryOperations>) -> MemoryTools

/// Create MemoryTools with tenant isolation and vector search support
pub async fn create_memory_tools_with_tenant_and_vector(
    data_dir: &str,
    tenant_id: &str,
    llm_client: Arc<dyn LLMClient>,
    qdrant_url: &str,
    qdrant_collection: &str,
    embedding_api_base_url: &str,
    embedding_api_key: &str,
    embedding_model_name: &str,
    embedding_dim: Option<usize>,
    user_id: Option<String>,
) -> Result<MemoryTools, Box<dyn std::error::Error>>
```

## ๐Ÿ› ๏ธ Tool Definitions


### Tiered Access Tools


#### AbstractTool (`"abstract"`)


Get L0 abstract (~100 tokens) for quick relevance checking.

**Parameters:**
```rust
pub struct AbstractArgs {
    pub uri: String,  // Required: Memory URI
}
```

**Response:**
```rust
pub struct AbstractResponse {
    pub uri: String,
    pub abstract_text: String,
    pub layer: String,       // "L0"
    pub token_count: usize,
}
```

#### OverviewTool (`"overview"`)


Get L1 overview (~2000 tokens) for partial context.

**Parameters:**
```rust
pub struct OverviewArgs {
    pub uri: String,  // Required: Memory URI
}
```

**Response:**
```rust
pub struct OverviewResponse {
    pub uri: String,
    pub overview_text: String,
    pub layer: String,       // "L1"
    pub token_count: usize,
}
```

#### ReadTool (`"read"`)


Get L2 full content for deep analysis.

**Parameters:**
```rust
pub struct ReadArgs {
    pub uri: String,  // Required: Memory URI
}
```

**Response:**
```rust
pub struct ReadResponse {
    pub uri: String,
    pub content: String,
    pub layer: String,       // "L2"
    pub token_count: usize,
    pub metadata: Option<FileMetadata>,
}
```

### Search Tools


#### SearchTool (`"search"`)


Intelligent vector search with LLM query rewriting and layered retrieval.

**Parameters:**
```rust
pub struct SearchArgs {
    pub query: String,                    // Required: Search query
    pub recursive: Option<bool>,          // Default: true
    pub return_layers: Option<Vec<String>>, // ["L0", "L1", "L2"]
    pub scope: Option<String>,            // Search scope URI
    pub limit: Option<usize>,             // Default: 10
}
```

**Response:**
```rust
pub struct SearchResponse {
    pub query: String,
    pub results: Vec<SearchResult>,
    pub total: usize,
    pub engine_used: String,
}

pub struct SearchResult {
    pub uri: String,
    pub score: f32,
    pub snippet: String,
    pub content: Option<String>,
}
```

#### FindTool (`"find"`)


Quick search returning only L0 abstracts.

**Parameters:**
```rust
pub struct FindArgs {
    pub query: String,          // Required: Search query
    pub scope: Option<String>,  // Search scope URI
    pub limit: Option<usize>,   // Default: 10
}
```

**Response:**
```rust
pub struct FindResponse {
    pub query: String,
    pub results: Vec<FindResult>,
    pub total: usize,
}

pub struct FindResult {
    pub uri: String,
    pub score: f32,
    pub abstract_text: String,
}
```

### Filesystem Tools


#### LsTool (`"ls"`)


List directory contents.

**Parameters:**
```rust
pub struct LsArgs {
    pub uri: String,                    // Default: "cortex://session"
    pub recursive: Option<bool>,        // Default: false
    pub include_abstracts: Option<bool>, // Default: false
}
```

**Response:**
```rust
pub struct LsResponse {
    pub uri: String,
    pub entries: Vec<LsEntry>,
    pub total: usize,
}

pub struct LsEntry {
    pub name: String,
    pub uri: String,
    pub is_directory: bool,
    pub size: Option<u64>,
    pub abstract_text: Option<String>,
}
```

#### ExploreTool (`"explore"`)


Intelligent memory exploration.

**Parameters:**
```rust
pub struct ExploreArgs {
    pub query: String,                   // Required: Exploration query
    pub start_uri: Option<String>,       // Default: "cortex://session"
    pub max_depth: Option<usize>,        // Default: 3
    pub return_layers: Option<Vec<String>>, // Default: ["L0"]
}
```

**Response:**
```rust
pub struct ExploreResponse {
    pub query: String,
    pub exploration_path: Vec<String>,
    pub matches: Vec<ExploreMatch>,
    pub total_explored: usize,
    pub total_matches: usize,
}
```

### Storage Tool


#### StoreTool (`"store"`)


Store content with automatic L0/L1 layer generation.

**Parameters:**
```rust
pub struct StoreArgs {
    pub content: String,                  // Required: Content to store
    pub thread_id: String,                // Default: ""
    pub metadata: Option<Value>,          // Optional metadata
    pub auto_generate_layers: Option<bool>, // Default: true
    pub scope: String,                    // "session", "user", or "agent"
    pub user_id: Option<String>,          // Required for user scope
    pub agent_id: Option<String>,         // Required for agent scope
}
```

**Response:**
```rust
pub struct StoreResponse {
    pub uri: String,
    pub layers_generated: Vec<String>,
    pub success: bool,
}
```

## ๐Ÿ”ง Rig Framework Integration


### Tool Trait Implementation


Each tool implements the `rig::tool::Tool` trait:

```rust
impl Tool for AbstractTool {
    const NAME: &'static str = "abstract";

    type Error = ToolsError;
    type Args = AbstractArgs;
    type Output = AbstractResponse;

    fn definition(&self, _prompt: String) -> impl Future<Output = ToolDefinition> + Send + Sync {
        async {
            ToolDefinition {
                name: Self::NAME.to_string(),
                description: "...".to_string(),
                parameters: json!({ /* JSON Schema */ }),
            }
        }
    }

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        Ok(self.operations.get_abstract(&args.uri).await?)
    }
}
```

### Agent Integration Example


```rust
use rig::providers::openai::{Client, GPT_4O_MINI};
use cortex_mem_rig::MemoryTools;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize OpenAI client
    let client = Client::from_env();
    
    // Create memory tools
    let memory_tools = create_memory_tools_with_tenant_and_vector(
        "./cortex-data",
        "default",
        llm_client,
        "http://localhost:6333",
        "cortex_memories",
        "https://api.openai.com/v1",
        "your-key",
        "text-embedding-3-small",
        Some(1536),
        None,
    ).await?;
    
    // Create agent with tools
    let agent = client
        .agent(GPT_4O_MINI)
        .preamble("You are an AI assistant with persistent memory capabilities.")
        .tool(memory_tools.abstract_tool())
        .tool(memory_tools.overview_tool())
        .tool(memory_tools.read_tool())
        .tool(memory_tools.search_tool())
        .tool(memory_tools.store_tool())
        .build();
    
    // Use the agent
    let response = agent.prompt(
        "Search for user preferences and store that they like dark theme."
    ).await?;
    
    println!("Agent response: {}", response);
    
    Ok(())
}
```

## ๐ŸŽฏ Best Practices


### Tiered Access Pattern


1. **Use `abstract` first** for quick relevance checking
2. **Use `overview` if relevant** for more context
3. **Use `read` only when necessary** for complete content

### Search Optimization


```rust
// Limit search scope for better performance
agent.prompt("Search 'error handling' in session 'rust-discussion'").await?;

// Use find for quick lookups (returns only L0 abstracts)
agent.prompt("Find memories about 'OAuth' and show abstracts").await?;

// Combine with tiered access
agent.prompt(
    "Search 'async programming', get abstracts for top 3, then read the most relevant one"
).await?;
```

### Memory Storage


```rust
// Store in session scope (default)
agent.prompt("Store 'User is learning Rust async' in current session").await?;

// Store in user scope for long-term memory
agent.prompt("Store 'User prefers dark mode' as a user preference").await?;
```

## ๐Ÿงช Testing


```bash
# Run Rig integration tests

cargo test -p cortex-mem-rig

# Run all tests

cargo test --all
```

## ๐Ÿšจ Common Issues


### Tool Call Failed


Ensure:
- Cortex Memory Core is properly initialized
- Data directory has write permissions
- Search index is built

### Empty Abstract Content


Possible causes:
- File does not exist
- Content too short to generate summary
- LLM service unavailable

### Inaccurate Search Results


Optimization tips:
- Use more specific queries
- Limit search scope
- Use `search` instead of `find` for comprehensive results

## ๐Ÿ“ฆ Dependencies


- `cortex-mem-tools` - High-level memory operations
- `cortex-mem-core` - Core library
- `rig-core` - Rig AI framework
- `tokio` - Async runtime
- `serde` / `serde_json` - Serialization
- `anyhow` / `thiserror` - Error handling

## ๐Ÿ“„ License


MIT License - see the [LICENSE](../../LICENSE) file for details.

## ๐Ÿ”— Related Resources


- [Cortex Memory Core]../cortex-mem-core/README.md
- [Cortex Memory Tools]../cortex-mem-tools/README.md
- [Rig Framework]https://github.com/0xPlaygrounds/rig
- [Rig Documentation]https://docs.rs/rig/

---

**Built with โค๏ธ using Rust and Rig AI Framework**