engram-core 0.19.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
//! Search tool handlers.

use serde_json::{json, Value};

use crate::search::{hybrid_search, RerankConfig, RerankStrategy, Reranker};
use crate::types::*;

use super::HandlerContext;

pub fn memory_search(ctx: &HandlerContext, params: Value) -> Value {
    use crate::search::result_cache::CacheFilterParams;

    let query = params.get("query").and_then(|v| v.as_str()).unwrap_or("");
    let options: SearchOptions = serde_json::from_value(params.clone()).unwrap_or_default();

    let rerank_enabled = params
        .get("rerank")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);
    let rerank_strategy = match params.get("rerank_strategy").and_then(|v| v.as_str()) {
        Some("none") => RerankStrategy::None,
        Some("multi_signal") => RerankStrategy::MultiSignal,
        _ => RerankStrategy::Heuristic,
    };

    let query_embedding = ctx.embedder.embed(query).ok();
    let embedding_ref = query_embedding.as_deref();

    let cache_filters = CacheFilterParams {
        workspace: options.workspace.clone(),
        tier: options.tier.map(|t| t.as_str().to_string()),
        memory_types: options.memory_type.map(|t| vec![t]),
        include_archived: options.include_archived,
        include_transcripts: options.include_transcripts,
        tags: options.tags.clone(),
    };

    let skip_cache = params
        .get("skip_cache")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);

    if !skip_cache && !rerank_enabled {
        if let Some(cached_results) = ctx.search_cache.get(query, embedding_ref, &cache_filters) {
            return json!({"results": cached_results, "cached": true});
        }
    }

    let mut search_config = ctx.search_config.clone();
    if let Ok(cwd) = std::env::current_dir() {
        if let Ok(canonical) = cwd.canonicalize() {
            search_config.project_context_path = Some(canonical.to_string_lossy().to_string());
        }
    }

    ctx.storage
        .with_connection(|conn| {
            let results = hybrid_search(conn, query, embedding_ref, &options, &search_config)?;

            if !rerank_enabled && !skip_cache {
                ctx.search_cache.put(
                    query,
                    query_embedding.clone(),
                    cache_filters.clone(),
                    results.clone(),
                );
            }

            if rerank_enabled && rerank_strategy != RerankStrategy::None {
                let config = RerankConfig {
                    enabled: true,
                    strategy: rerank_strategy,
                    ..Default::default()
                };
                let reranker = Reranker::with_config(config);
                let reranked = reranker.rerank(results, query, None);

                if options.explain {
                    Ok(json!({
                        "results": reranked.iter().map(|r| {
                            json!({
                                "memory": r.result.memory,
                                "score": r.rerank_info.final_score,
                                "match_info": r.result.match_info,
                                "rerank_info": r.rerank_info
                            })
                        }).collect::<Vec<_>>(),
                        "reranked": true,
                        "strategy": format!("{:?}", rerank_strategy)
                    }))
                } else {
                    Ok(json!(reranked
                        .iter()
                        .map(|r| {
                            json!({
                                "memory": r.result.memory,
                                "score": r.rerank_info.final_score,
                                "match_info": r.result.match_info
                            })
                        })
                        .collect::<Vec<_>>()))
                }
            } else {
                Ok(json!(results))
            }
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn search_suggest(ctx: &HandlerContext, params: Value) -> Value {
    let query = params.get("query").and_then(|v| v.as_str()).unwrap_or("");
    let fuzzy = ctx.fuzzy_engine.lock();
    let result = fuzzy.correct_query(query);
    json!(result)
}

pub fn memory_search_by_identity(ctx: &HandlerContext, params: Value) -> Value {
    use crate::storage::search_by_identity;

    let identity = match params.get("identity").and_then(|v| v.as_str()) {
        Some(i) => i,
        None => return json!({"error": "identity is required"}),
    };

    let workspace = params.get("workspace").and_then(|v| v.as_str());
    let limit = params
        .get("limit")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);

    ctx.storage
        .with_connection(|conn| {
            let memories = search_by_identity(conn, identity, workspace, limit)?;
            Ok(json!({"memories": memories}))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn memory_session_search(ctx: &HandlerContext, params: Value) -> Value {
    use crate::storage::search_sessions;

    let query = match params.get("query").and_then(|v| v.as_str()) {
        Some(q) => q,
        None => return json!({"error": "query is required"}),
    };

    let session_id = params.get("session_id").and_then(|v| v.as_str());
    let workspace = params.get("workspace").and_then(|v| v.as_str());
    let limit = params
        .get("limit")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);

    ctx.storage
        .with_connection(|conn| {
            let memories = search_sessions(conn, query, session_id, workspace, limit)?;
            Ok(json!({"memories": memories}))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn find_duplicates(ctx: &HandlerContext, params: Value) -> Value {
    use crate::storage::queries::find_duplicates;

    let threshold = params
        .get("threshold")
        .and_then(|v| v.as_f64())
        .unwrap_or(0.9);

    ctx.storage
        .with_connection(|conn| {
            let duplicates = find_duplicates(conn, threshold)?;
            Ok(json!({
                "count": duplicates.len(),
                "threshold": threshold,
                "duplicates": duplicates
            }))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn find_semantic_duplicates(ctx: &HandlerContext, params: Value) -> Value {
    use crate::storage::queries::find_duplicates_by_embedding;

    let threshold = params
        .get("threshold")
        .and_then(|v| v.as_f64())
        .unwrap_or(0.92) as f32;
    let workspace = params.get("workspace").and_then(|v| v.as_str());
    let limit = params.get("limit").and_then(|v| v.as_i64()).unwrap_or(50) as usize;

    ctx.storage
        .with_connection(|conn| {
            let duplicates = find_duplicates_by_embedding(conn, threshold, workspace, limit)?;
            Ok(json!({
                "count": duplicates.len(),
                "threshold": threshold,
                "method": "embedding_cosine_similarity",
                "duplicates": duplicates
            }))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn search_cache_feedback(ctx: &HandlerContext, params: Value) -> Value {
    use crate::search::CacheFilterParams;

    let query = match params.get("query").and_then(|v| v.as_str()) {
        Some(q) => q,
        None => return json!({"error": "query is required"}),
    };

    let positive = match params.get("positive").and_then(|v| v.as_bool()) {
        Some(p) => p,
        None => return json!({"error": "positive is required"}),
    };

    let workspace = params
        .get("workspace")
        .and_then(|v| v.as_str())
        .map(|s| s.to_string());

    let filters = CacheFilterParams {
        workspace,
        ..Default::default()
    };

    ctx.search_cache.record_feedback(query, &filters, positive);
    let new_threshold = ctx.search_cache.current_threshold();

    json!({
        "recorded": true,
        "feedback": if positive { "positive" } else { "negative" },
        "current_threshold": new_threshold
    })
}

pub fn search_cache_stats(ctx: &HandlerContext, _params: Value) -> Value {
    let stats = ctx.search_cache.stats();
    json!(stats)
}

pub fn search_cache_clear(ctx: &HandlerContext, params: Value) -> Value {
    let workspace = params.get("workspace").and_then(|v| v.as_str());

    if let Some(ws) = workspace {
        ctx.search_cache.invalidate_for_workspace(Some(ws));
        json!({"cleared": true, "scope": "workspace", "workspace": ws})
    } else {
        ctx.search_cache.clear();
        json!({"cleared": true, "scope": "all"})
    }
}

// ── Search Explainability (RML-1242) ────────────────────────────────────────

pub fn memory_explain_search(_ctx: &HandlerContext, params: Value) -> Value {
    use crate::search::explain::SearchExplainer;

    let results = match params.get("results").and_then(|v| v.as_array()) {
        Some(arr) => arr,
        None => {
            return json!({"error": "results array is required (each with memory_id, bm25, vector, fuzzy, recency, importance, final_score, and optional rerank_score)"})
        }
    };

    let reranking_active = params
        .get("reranking_active")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    let rrf_k = params.get("rrf_k").and_then(|v| v.as_f64()).unwrap_or(60.0) as f32;

    let explainer = SearchExplainer::new(rrf_k, reranking_active);

    let batch: Vec<_> = results
        .iter()
        .filter_map(|r| {
            let memory_id = r.get("memory_id")?.as_i64()?;
            let bm25 = r.get("bm25").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
            let vector = r.get("vector").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
            let fuzzy = r.get("fuzzy").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
            let recency = r.get("recency").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
            let importance = r.get("importance").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
            let rerank = r
                .get("rerank_score")
                .and_then(|v| v.as_f64())
                .map(|v| v as f32);
            let final_score = r.get("final_score").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
            Some((
                memory_id,
                bm25,
                vector,
                fuzzy,
                recency,
                importance,
                rerank,
                final_score,
            ))
        })
        .collect();

    let explanations = explainer.explain_batch(batch);
    json!({
        "count": explanations.len(),
        "explanations": explanations
    })
}

// ── Relevance Feedback (RML-1243) ───────────────────────────────────────────

pub fn memory_feedback(ctx: &HandlerContext, params: Value) -> Value {
    use crate::search::feedback::{record_feedback, FeedbackSignal};

    let query = match params.get("query").and_then(|v| v.as_str()) {
        Some(q) => q,
        None => return json!({"error": "query is required"}),
    };

    let memory_id = match params.get("memory_id").and_then(|v| v.as_i64()) {
        Some(id) => id,
        None => return json!({"error": "memory_id is required"}),
    };

    let signal = match params.get("signal").and_then(|v| v.as_str()) {
        Some("useful") => FeedbackSignal::Useful,
        Some("irrelevant") => FeedbackSignal::Irrelevant,
        _ => return json!({"error": "signal must be 'useful' or 'irrelevant'"}),
    };

    let rank_position = params
        .get("rank_position")
        .and_then(|v| v.as_i64())
        .map(|v| v as i32);
    let original_score = params
        .get("original_score")
        .and_then(|v| v.as_f64())
        .map(|v| v as f32);
    let workspace = params
        .get("workspace")
        .and_then(|v| v.as_str())
        .unwrap_or("default");

    ctx.storage
        .with_connection(|conn| {
            let fb = record_feedback(
                conn,
                query,
                memory_id,
                signal,
                rank_position,
                original_score,
                workspace,
            )?;
            Ok(json!(fb))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn memory_feedback_stats(ctx: &HandlerContext, params: Value) -> Value {
    use crate::search::feedback::feedback_stats;

    let workspace = params.get("workspace").and_then(|v| v.as_str());

    ctx.storage
        .with_connection(|conn| {
            let stats = feedback_stats(conn, workspace)?;
            Ok(json!(stats))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

// ── Compact Search + Expand ──────────────────────────────────────────────────

/// Return a compact summary of search results (id, title, created_at, tags).
///
/// Parameters:
///   - `query` (String, required)
///   - `limit` (u64, optional, default 10)
///   - `workspace` (String, optional)
///
/// The `title` field is the first 80 chars of `content`, truncated at the first
/// newline, with "..." appended if truncated.
pub fn memory_search_compact(ctx: &HandlerContext, params: Value) -> Value {
    let query = match params.get("query").and_then(|v| v.as_str()) {
        Some(q) => q,
        None => return json!({"error": "query is required"}),
    };

    let mut options: SearchOptions = serde_json::from_value(params.clone()).unwrap_or_default();

    // Apply default limit of 10 when not supplied
    if options.limit.is_none() {
        let limit_from_param = params.get("limit").and_then(|v| v.as_i64());
        options.limit = Some(limit_from_param.unwrap_or(10));
    }

    let query_embedding = ctx.embedder.embed(query).ok();
    let embedding_ref = query_embedding.as_deref();

    let mut search_config = ctx.search_config.clone();
    if let Ok(cwd) = std::env::current_dir() {
        if let Ok(canonical) = cwd.canonicalize() {
            search_config.project_context_path = Some(canonical.to_string_lossy().to_string());
        }
    }

    ctx.storage
        .with_connection(|conn| {
            let results = hybrid_search(conn, query, embedding_ref, &options, &search_config)?;

            let compact: Vec<Value> = results
                .iter()
                .map(|r| {
                    let memory = &r.memory;
                    // Build title: first 80 chars of content, truncated at first newline,
                    // with "..." appended if the content was longer than the title shown.
                    let first_line = memory.content.lines().next().unwrap_or("");
                    let has_more_lines = memory.content.contains('\n');
                    let title_str = if first_line.len() > 80 {
                        format!("{}...", &first_line[..80])
                    } else if has_more_lines {
                        format!("{}...", first_line)
                    } else {
                        first_line.to_string()
                    };
                    json!({
                        "id": memory.id,
                        "title": title_str,
                        "created_at": memory.created_at,
                        "tags": memory.tags
                    })
                })
                .collect();

            Ok(json!({
                "results": compact,
                "count": compact.len()
            }))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

/// Fetch full Memory objects for a list of IDs.
///
/// Parameters:
///   - `ids` (array of integers, required)
///
/// IDs that do not exist are silently skipped.
/// Returns `{memories: [...], found: N, requested: N}`.
pub fn memory_expand(ctx: &HandlerContext, params: Value) -> Value {
    use crate::error::EngramError;
    use crate::storage::queries::get_memory;

    let ids: Vec<i64> = match params.get("ids").and_then(|v| v.as_array()) {
        Some(arr) => arr
            .iter()
            .filter_map(|v| v.as_i64())
            .collect(),
        None => return json!({"error": "ids array is required"}),
    };

    let requested = ids.len();

    ctx.storage
        .with_connection(|conn| {
            let mut memories: Vec<Value> = Vec::with_capacity(ids.len());
            for id in &ids {
                match get_memory(conn, *id) {
                    Ok(memory) => memories.push(json!(memory)),
                    Err(EngramError::NotFound(_)) => {
                        // Skip silently
                    }
                    Err(e) => return Err(e),
                }
            }
            let found = memories.len();
            Ok(json!({
                "memories": memories,
                "found": found,
                "requested": requested
            }))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}