engram-core 0.17.0

AI Memory Infrastructure - Persistent memory for AI agents with semantic search
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Entity extraction for automatic identity linking
//!
//! Provides lightweight Named Entity Recognition (NER) for:
//! - @mentions (e.g., @ronaldo, @acme-corp)
//! - Email addresses
//! - URLs with domain extraction
//! - Capitalized names (simple heuristic)
//! - Known identity aliases (database lookup)
//!
//! ## Invariants
//!
//! - Extraction never panics on any input
//! - Empty/whitespace input returns empty results
//! - Duplicate mentions are deduplicated with count
//! - Results are sorted by first occurrence position
//!
//! ## Performance
//!
//! - Regex patterns are compiled once (lazy_static)
//! - Single pass through text for pattern matching
//! - Bounded output: max 100 entities per text

use std::collections::HashMap;

use once_cell::sync::Lazy;
use regex::Regex;
use rusqlite::Connection;
use serde::{Deserialize, Serialize};
use tracing::{debug, instrument, warn};

use crate::error::Result;
use crate::storage::identity_links::{normalize_alias, resolve_alias};

/// Maximum entities to extract from a single text (prevents DoS)
const MAX_ENTITIES_PER_TEXT: usize = 100;

/// Minimum confidence threshold for extraction
const MIN_CONFIDENCE: f32 = 0.3;

/// Compiled regex patterns (compiled once, reused)
static MENTION_PATTERN: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"@([a-zA-Z][a-zA-Z0-9_-]{1,30})").expect("valid regex"));

static EMAIL_PATTERN: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}").expect("valid regex")
});

static URL_PATTERN: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"https?://([a-zA-Z0-9.-]+)(?:/[^\s]*)?").expect("valid regex"));

/// Pattern for capitalized names (2+ words starting with capitals)
static NAME_PATTERN: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"\b([A-Z][a-z]+(?:\s+[A-Z][a-z]+)+)\b").expect("valid regex"));

/// An extracted entity from text
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtractedEntity {
    /// The raw text as found in content
    pub mention_text: String,
    /// Normalized form for matching
    pub normalized: String,
    /// Type of entity detected
    pub entity_type: ExtractedEntityType,
    /// Confidence score (0.0 - 1.0)
    pub confidence: f32,
    /// Position in text (byte offset)
    pub position: usize,
    /// Number of times this entity appears
    pub count: usize,
    /// Resolved canonical ID if matched to existing identity
    pub resolved_id: Option<String>,
}

/// Type of extracted entity
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExtractedEntityType {
    /// @mention style reference
    Mention,
    /// Email address
    Email,
    /// URL/domain
    Url,
    /// Capitalized name pattern
    Name,
    /// Matched existing alias
    KnownAlias,
}

impl ExtractedEntityType {
    /// Default confidence for this entity type
    fn default_confidence(&self) -> f32 {
        match self {
            ExtractedEntityType::Mention => 0.9,
            ExtractedEntityType::Email => 0.95,
            ExtractedEntityType::Url => 0.7,
            ExtractedEntityType::Name => 0.5,
            ExtractedEntityType::KnownAlias => 1.0,
        }
    }
}

/// Configuration for entity extraction
#[derive(Debug, Clone)]
pub struct ExtractionConfig {
    /// Extract @mentions
    pub extract_mentions: bool,
    /// Extract email addresses
    pub extract_emails: bool,
    /// Extract URLs/domains
    pub extract_urls: bool,
    /// Extract capitalized names
    pub extract_names: bool,
    /// Lookup existing aliases in database
    pub lookup_aliases: bool,
    /// Minimum confidence to include
    pub min_confidence: f32,
    /// Maximum entities to return
    pub max_entities: usize,
}

impl Default for ExtractionConfig {
    fn default() -> Self {
        Self {
            extract_mentions: true,
            extract_emails: true,
            extract_urls: true,
            extract_names: true,
            lookup_aliases: true,
            min_confidence: MIN_CONFIDENCE,
            max_entities: MAX_ENTITIES_PER_TEXT,
        }
    }
}

/// Result of entity extraction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtractionResult {
    /// Extracted entities, deduplicated and sorted by position
    pub entities: Vec<ExtractedEntity>,
    /// Total mentions found (before dedup)
    pub total_mentions: usize,
    /// Number of entities resolved to existing identities
    pub resolved_count: usize,
}

/// Extract entities from text content.
///
/// This function never panics. Invalid input returns empty results.
///
/// # Arguments
/// * `content` - Text to extract entities from
/// * `config` - Extraction configuration
/// * `conn` - Optional database connection for alias lookup
///
/// # Returns
/// Extraction result with deduplicated, sorted entities
#[instrument(skip(content, config, conn), fields(content_len = content.len()))]
pub fn extract_entities(
    content: &str,
    config: &ExtractionConfig,
    conn: Option<&Connection>,
) -> ExtractionResult {
    // Handle empty/whitespace input
    let content = content.trim();
    if content.is_empty() {
        return ExtractionResult {
            entities: vec![],
            total_mentions: 0,
            resolved_count: 0,
        };
    }

    // Use HashMap for deduplication (normalized -> entity)
    let mut entities_map: HashMap<String, ExtractedEntity> = HashMap::new();
    let mut total_mentions = 0;

    // Extract @mentions
    if config.extract_mentions {
        for cap in MENTION_PATTERN.captures_iter(content) {
            if let Some(m) = cap.get(1) {
                let mention_text = format!("@{}", m.as_str());
                let normalized = normalize_alias(&mention_text);
                let position = cap.get(0).map(|c| c.start()).unwrap_or(0);

                add_or_increment(
                    &mut entities_map,
                    mention_text,
                    normalized,
                    ExtractedEntityType::Mention,
                    position,
                );
                total_mentions += 1;
            }

            // Bound check
            if entities_map.len() >= config.max_entities {
                break;
            }
        }
    }

    // Extract emails
    if config.extract_emails && entities_map.len() < config.max_entities {
        for cap in EMAIL_PATTERN.find_iter(content) {
            let email = cap.as_str();
            let normalized = normalize_alias(email);

            add_or_increment(
                &mut entities_map,
                email.to_string(),
                normalized,
                ExtractedEntityType::Email,
                cap.start(),
            );
            total_mentions += 1;

            if entities_map.len() >= config.max_entities {
                break;
            }
        }
    }

    // Extract URLs (just domain part)
    if config.extract_urls && entities_map.len() < config.max_entities {
        for cap in URL_PATTERN.captures_iter(content) {
            if let Some(domain) = cap.get(1) {
                let domain_str = domain.as_str();
                // Skip common domains
                if !is_common_domain(domain_str) {
                    let normalized = normalize_alias(domain_str);

                    add_or_increment(
                        &mut entities_map,
                        domain_str.to_string(),
                        normalized,
                        ExtractedEntityType::Url,
                        cap.get(0).map(|c| c.start()).unwrap_or(0),
                    );
                    total_mentions += 1;
                }
            }

            if entities_map.len() >= config.max_entities {
                break;
            }
        }
    }

    // Extract capitalized names
    if config.extract_names && entities_map.len() < config.max_entities {
        for cap in NAME_PATTERN.find_iter(content) {
            let name = cap.as_str();
            // Skip common phrases
            if !is_common_phrase(name) {
                let normalized = normalize_alias(name);

                add_or_increment(
                    &mut entities_map,
                    name.to_string(),
                    normalized,
                    ExtractedEntityType::Name,
                    cap.start(),
                );
                total_mentions += 1;
            }

            if entities_map.len() >= config.max_entities {
                break;
            }
        }
    }

    // Resolve entities against existing identities
    let mut resolved_count = 0;
    if config.lookup_aliases {
        if let Some(conn) = conn {
            for entity in entities_map.values_mut() {
                if let Ok(Some(identity)) = resolve_alias(conn, &entity.normalized) {
                    entity.resolved_id = Some(identity.canonical_id);
                    entity.entity_type = ExtractedEntityType::KnownAlias;
                    entity.confidence = 1.0;
                    resolved_count += 1;
                }
            }
        }
    }

    // Filter by confidence and convert to vec
    let mut entities: Vec<ExtractedEntity> = entities_map
        .into_values()
        .filter(|e| e.confidence >= config.min_confidence)
        .collect();

    // Sort by position for stable output
    entities.sort_by_key(|e| e.position);

    // Truncate to max
    entities.truncate(config.max_entities);

    debug!(
        entity_count = entities.len(),
        total_mentions, resolved_count, "Entity extraction complete"
    );

    ExtractionResult {
        entities,
        total_mentions,
        resolved_count,
    }
}

/// Add entity or increment count if exists
fn add_or_increment(
    map: &mut HashMap<String, ExtractedEntity>,
    mention_text: String,
    normalized: String,
    entity_type: ExtractedEntityType,
    position: usize,
) {
    if let Some(existing) = map.get_mut(&normalized) {
        existing.count += 1;
    } else {
        map.insert(
            normalized.clone(),
            ExtractedEntity {
                mention_text,
                normalized,
                entity_type,
                confidence: entity_type.default_confidence(),
                position,
                count: 1,
                resolved_id: None,
            },
        );
    }
}

/// Check if domain is too common to be meaningful
fn is_common_domain(domain: &str) -> bool {
    const COMMON: &[&str] = &[
        "google.com",
        "github.com",
        "stackoverflow.com",
        "wikipedia.org",
        "twitter.com",
        "x.com",
        "facebook.com",
        "youtube.com",
        "linkedin.com",
        "medium.com",
        "docs.rs",
        "crates.io",
        "rust-lang.org",
    ];
    COMMON.iter().any(|c| domain.eq_ignore_ascii_case(c))
}

/// Check if phrase is too common to be a name
fn is_common_phrase(phrase: &str) -> bool {
    const COMMON: &[&str] = &[
        "New York",
        "Los Angeles",
        "San Francisco",
        "United States",
        "Open Source",
        "Machine Learning",
        "Artificial Intelligence",
        "The End",
        "The Start",
    ];
    COMMON.iter().any(|c| phrase.eq_ignore_ascii_case(c))
}

/// Auto-link entities found in a memory's content to identities.
///
/// This function:
/// 1. Extracts entities from content
/// 2. Creates new identities for unresolved entities (optional)
/// 3. Links all resolved entities to the memory
///
/// # Arguments
/// * `conn` - Database connection
/// * `memory_id` - Memory to link entities to
/// * `content` - Memory content to extract from
/// * `auto_create` - Whether to create new identities for unresolved entities
///
/// # Returns
/// Number of entities linked
#[instrument(skip(conn, content), fields(memory_id, auto_create, content_len = content.len()))]
pub fn auto_link_memory(
    conn: &Connection,
    memory_id: i64,
    content: &str,
    auto_create: bool,
) -> Result<usize> {
    use crate::storage::identity_links::{
        create_identity, link_identity_to_memory, CreateIdentityInput, IdentityType,
    };

    let config = ExtractionConfig::default();
    let result = extract_entities(content, &config, Some(conn));

    let mut linked_count = 0;

    for entity in result.entities {
        let canonical_id = if let Some(id) = entity.resolved_id {
            // Already resolved to existing identity
            id
        } else if auto_create {
            // Create new identity for this entity
            let entity_type = match entity.entity_type {
                ExtractedEntityType::Email => IdentityType::Person,
                ExtractedEntityType::Mention => IdentityType::Person,
                ExtractedEntityType::Url => IdentityType::Organization,
                ExtractedEntityType::Name => IdentityType::Person,
                ExtractedEntityType::KnownAlias => IdentityType::Other,
            };

            let input = CreateIdentityInput {
                canonical_id: format!("auto:{}", entity.normalized),
                display_name: entity.mention_text.clone(),
                entity_type,
                description: Some("Auto-created from entity extraction".to_string()),
                metadata: HashMap::new(),
                aliases: vec![entity.mention_text.clone()],
            };

            match create_identity(conn, &input) {
                Ok(identity) => identity.canonical_id,
                Err(_) => continue, // Skip if creation fails (e.g., already exists)
            }
        } else {
            continue; // Skip unresolved entities
        };

        // Link to memory
        if link_identity_to_memory(conn, memory_id, &canonical_id, Some(&entity.mention_text))
            .is_ok()
        {
            linked_count += 1;
        }
    }

    Ok(linked_count)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_mentions() {
        let config = ExtractionConfig {
            lookup_aliases: false,
            ..Default::default()
        };

        let result = extract_entities("Hello @alice and @bob-smith!", &config, None);

        assert_eq!(result.entities.len(), 2);
        assert_eq!(result.entities[0].mention_text, "@alice");
        assert_eq!(result.entities[1].mention_text, "@bob-smith");
    }

    #[test]
    fn test_extract_emails() {
        let config = ExtractionConfig {
            lookup_aliases: false,
            extract_names: false,
            extract_mentions: false,
            extract_urls: false,
            extract_emails: true,
            ..Default::default()
        };

        let result = extract_entities("Contact us at hello@example.com", &config, None);

        assert_eq!(result.entities.len(), 1);
        assert_eq!(result.entities[0].mention_text, "hello@example.com");
        assert_eq!(result.entities[0].entity_type, ExtractedEntityType::Email);
    }

    #[test]
    fn test_extract_names() {
        let config = ExtractionConfig {
            lookup_aliases: false,
            ..Default::default()
        };

        let result = extract_entities("I met John Smith yesterday", &config, None);

        assert_eq!(result.entities.len(), 1);
        assert_eq!(result.entities[0].mention_text, "John Smith");
        assert_eq!(result.entities[0].entity_type, ExtractedEntityType::Name);
    }

    #[test]
    fn test_empty_input() {
        let config = ExtractionConfig::default();
        let result = extract_entities("", &config, None);
        assert!(result.entities.is_empty());

        let result = extract_entities("   ", &config, None);
        assert!(result.entities.is_empty());
    }

    #[test]
    fn test_deduplication() {
        let config = ExtractionConfig {
            lookup_aliases: false,
            ..Default::default()
        };

        let result = extract_entities("@alice said hello. @alice waved.", &config, None);

        assert_eq!(result.entities.len(), 1);
        assert_eq!(result.entities[0].count, 2);
        assert_eq!(result.total_mentions, 2);
    }

    #[test]
    fn test_max_entities_bound() {
        let config = ExtractionConfig {
            lookup_aliases: false,
            max_entities: 2,
            ..Default::default()
        };

        let result = extract_entities("@a @b @c @d @e", &config, None);

        assert!(result.entities.len() <= 2);
    }

    #[test]
    fn test_normalization_invariant() {
        // Invariant: normalize_alias is idempotent
        let inputs = vec![
            "@Alice",
            "  bob  ",
            "@CHARLIE",
            "user@email.com",
            "  @mixed  CASE  ",
        ];

        for input in inputs {
            let once = normalize_alias(input);
            let twice = normalize_alias(&once);
            assert_eq!(
                once, twice,
                "Normalization should be idempotent for: {}",
                input
            );
        }
    }

    #[test]
    fn test_never_panics_on_bad_input() {
        let config = ExtractionConfig {
            lookup_aliases: false,
            ..Default::default()
        };

        // Pre-allocate strings that need longer lifetime
        let long_a = "a".repeat(10000);
        let long_at = "@".repeat(1000);

        // Various edge cases that shouldn't panic
        let inputs: Vec<&str> = vec![
            "",
            "   ",
            "@",
            "@@@@",
            "@a",
            "a@",
            "http://",
            "https://",
            &long_a,
            &long_at,
            "\0\0\0",
            "emoji: πŸŽ‰πŸŽŠπŸŽ",
            "unicode: ζ—₯本θͺž δΈ­ζ–‡ ν•œκ΅­μ–΄",
        ];

        for input in inputs {
            let result = extract_entities(input, &config, None);
            // Just verify no panic
            let _ = result.entities.len();
        }
    }
}