mem0-rust 0.2.0

Rust implementation of mem0 - Universal memory layer for AI Agents
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
//! Data models for mem0-rust.
//!
//! This module provides all the core data structures used throughout the library.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use uuid::Uuid;

/// A stored memory record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryRecord {
    /// Unique identifier
    pub id: Uuid,

    /// Memory content
    pub content: String,

    /// Associated metadata
    pub metadata: HashMap<String, serde_json::Value>,

    /// User ID scope
    pub user_id: Option<String>,

    /// Agent ID scope
    pub agent_id: Option<String>,

    /// Run ID scope
    pub run_id: Option<String>,

    /// Content hash for deduplication
    pub hash: String,

    /// Creation timestamp
    pub created_at: DateTime<Utc>,

    /// Last update timestamp
    pub updated_at: DateTime<Utc>,
}

impl MemoryRecord {
    /// Create a new memory record
    pub fn new(content: impl Into<String>, metadata: serde_json::Value) -> Self {
        let content = content.into();
        let hash = Self::compute_hash(&content);
        let now = Utc::now();

        let metadata_map = match metadata {
            serde_json::Value::Object(map) => map.into_iter().collect(),
            _ => HashMap::new(),
        };

        Self {
            id: Uuid::new_v4(),
            content,
            metadata: metadata_map,
            user_id: None,
            agent_id: None,
            run_id: None,
            hash,
            created_at: now,
            updated_at: now,
        }
    }

    /// Create with scoping
    pub fn with_scoping(
        content: impl Into<String>,
        metadata: serde_json::Value,
        user_id: Option<String>,
        agent_id: Option<String>,
        run_id: Option<String>,
    ) -> Self {
        let mut record = Self::new(content, metadata);
        record.user_id = user_id;
        record.agent_id = agent_id;
        record.run_id = run_id;
        record
    }

    /// Compute content hash
    fn compute_hash(content: &str) -> String {
        let mut hasher = Sha256::new();
        hasher.update(content.as_bytes());
        hex::encode(hasher.finalize())
    }

    /// Update the content and hash
    pub fn update_content(&mut self, content: impl Into<String>) {
        self.content = content.into();
        self.hash = Self::compute_hash(&self.content);
        self.updated_at = Utc::now();
    }
}

/// A memory with its similarity score
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScoredMemory {
    /// The memory record
    pub record: MemoryRecord,

    /// Similarity score
    pub score: f32,
}

/// Message role for chat-style input
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Role {
    /// System message
    System,
    /// User message
    User,
    /// Assistant message
    Assistant,
}

/// A chat message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    /// Message role
    pub role: Role,

    /// Message content
    pub content: String,

    /// Optional actor name
    pub name: Option<String>,
}

impl Message {
    /// Create a user message
    pub fn user(content: impl Into<String>) -> Self {
        Self {
            role: Role::User,
            content: content.into(),
            name: None,
        }
    }

    /// Create an assistant message
    pub fn assistant(content: impl Into<String>) -> Self {
        Self {
            role: Role::Assistant,
            content: content.into(),
            name: None,
        }
    }

    /// Create a system message
    pub fn system(content: impl Into<String>) -> Self {
        Self {
            role: Role::System,
            content: content.into(),
            name: None,
        }
    }

    /// Set the actor name
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }
}

/// Input that can be converted to messages
#[derive(Debug, Clone)]
pub enum Messages {
    /// Plain text input
    Text(String),
    /// Single message
    Single(Message),
    /// Multiple messages
    Multiple(Vec<Message>),
}

impl From<&str> for Messages {
    fn from(s: &str) -> Self {
        Messages::Text(s.to_string())
    }
}

impl From<String> for Messages {
    fn from(s: String) -> Self {
        Messages::Text(s)
    }
}

impl From<Message> for Messages {
    fn from(m: Message) -> Self {
        Messages::Single(m)
    }
}

impl From<Vec<Message>> for Messages {
    fn from(v: Vec<Message>) -> Self {
        Messages::Multiple(v)
    }
}

impl Messages {
    /// Convert to a list of messages
    pub fn into_messages(self) -> Vec<Message> {
        match self {
            Messages::Text(s) => vec![Message::user(s)],
            Messages::Single(m) => vec![m],
            Messages::Multiple(v) => v,
        }
    }
}

/// Options for adding memories
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AddOptions {
    /// User ID scope
    pub user_id: Option<String>,

    /// Agent ID scope
    pub agent_id: Option<String>,

    /// Run ID scope
    pub run_id: Option<String>,

    /// Additional metadata
    pub metadata: Option<HashMap<String, serde_json::Value>>,

    /// Whether to use LLM for inference (default: true)
    pub infer: bool,
}

impl AddOptions {
    /// Create options with user scope
    pub fn for_user(user_id: impl Into<String>) -> Self {
        Self {
            user_id: Some(user_id.into()),
            infer: true,
            ..Default::default()
        }
    }

    /// Create options with agent scope
    pub fn for_agent(agent_id: impl Into<String>) -> Self {
        Self {
            agent_id: Some(agent_id.into()),
            infer: true,
            ..Default::default()
        }
    }

    /// Disable LLM inference
    pub fn raw(mut self) -> Self {
        self.infer = false;
        self
    }
}

/// Result of adding memories
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddResult {
    /// List of memory operations performed
    pub results: Vec<MemoryEvent>,
}

/// A memory operation event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryEvent {
    /// Memory ID
    pub id: Uuid,

    /// Memory content
    pub memory: String,

    /// Event type
    pub event: EventType,
}

/// Type of memory event
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "UPPERCASE")]
pub enum EventType {
    /// Memory was added
    Add,
    /// Memory was updated
    Update,
    /// Memory was deleted
    Delete,
    /// No change
    Noop,
}

/// Options for searching memories
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SearchOptions {
    /// User ID filter
    pub user_id: Option<String>,

    /// Agent ID filter
    pub agent_id: Option<String>,

    /// Run ID filter
    pub run_id: Option<String>,

    /// Maximum number of results
    pub limit: Option<usize>,

    /// Minimum similarity threshold
    pub threshold: Option<f32>,

    /// Additional metadata filters
    pub filters: Option<Filters>,

    /// Whether to rerank results
    pub rerank: bool,
}

impl SearchOptions {
    /// Create options with user filter
    pub fn for_user(user_id: impl Into<String>) -> Self {
        Self {
            user_id: Some(user_id.into()),
            limit: Some(10),
            ..Default::default()
        }
    }

    /// Set the result limit
    pub fn with_limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Set the similarity threshold
    pub fn with_threshold(mut self, threshold: f32) -> Self {
        self.threshold = Some(threshold);
        self
    }
}

/// Search result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    /// Found memories with scores
    pub results: Vec<ScoredMemory>,
}

/// Metadata filters
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Filters {
    /// Filter conditions
    pub conditions: Vec<FilterCondition>,

    /// Logic operator between conditions
    pub logic: FilterLogic,
}

/// A filter condition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FilterCondition {
    /// Field name
    pub field: String,

    /// Operator
    pub operator: FilterOperator,

    /// Value to compare
    pub value: serde_json::Value,
}

/// Filter operators
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum FilterOperator {
    /// Equal
    Eq,
    /// Not equal
    Ne,
    /// Greater than
    Gt,
    /// Greater than or equal
    Gte,
    /// Less than
    Lt,
    /// Less than or equal
    Lte,
    /// In list
    In,
    /// Not in list
    Nin,
    /// Contains (for strings)
    Contains,
    /// Contains (case-insensitive)
    IContains,
}

/// Logic for combining filter conditions
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "UPPERCASE")]
pub enum FilterLogic {
    #[default]
    And,
    Or,
}

/// Options for listing all memories
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GetAllOptions {
    /// User ID filter
    pub user_id: Option<String>,

    /// Agent ID filter
    pub agent_id: Option<String>,

    /// Run ID filter
    pub run_id: Option<String>,

    /// Maximum number of results
    pub limit: Option<usize>,
}

/// A history entry for a memory
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryEntry {
    /// History entry ID
    pub id: Uuid,

    /// Memory ID this history belongs to
    pub memory_id: Uuid,

    /// Previous content
    pub previous_content: Option<String>,

    /// New content
    pub new_content: String,

    /// Event type
    pub event: EventType,

    /// Timestamp
    pub timestamp: DateTime<Utc>,
}

/// Options for resetting memories
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ResetOptions {
    /// User ID scope (if set, only reset this user's memories)
    pub user_id: Option<String>,

    /// Agent ID scope
    pub agent_id: Option<String>,
}

/// Payload for vector store operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Payload {
    /// Memory content
    pub data: String,

    /// Memory hash
    pub hash: String,

    /// Creation timestamp
    pub created_at: DateTime<Utc>,

    /// User ID
    pub user_id: Option<String>,

    /// Agent ID
    pub agent_id: Option<String>,

    /// Run ID
    pub run_id: Option<String>,

    /// Additional metadata
    #[serde(flatten)]
    pub metadata: HashMap<String, serde_json::Value>,
}

impl From<&MemoryRecord> for Payload {
    fn from(record: &MemoryRecord) -> Self {
        Self {
            data: record.content.clone(),
            hash: record.hash.clone(),
            created_at: record.created_at,
            user_id: record.user_id.clone(),
            agent_id: record.agent_id.clone(),
            run_id: record.run_id.clone(),
            metadata: record.metadata.clone(),
        }
    }
}