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
//! Quality and salience tool handlers.

use serde_json::{json, Value};

use super::HandlerContext;

pub fn quality_score(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::{calculate_quality_score, ContextQualityConfig};

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

    let config = ContextQualityConfig::default();

    ctx.storage
        .with_transaction(|conn| {
            let score = calculate_quality_score(conn, id, &config)?;
            Ok(json!(score))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn quality_report(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::generate_quality_report;

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

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

pub fn quality_find_duplicates(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::find_near_duplicates;

    let threshold = params
        .get("threshold")
        .and_then(|v| v.as_f64())
        .unwrap_or(0.85) as f32;

    let limit = params.get("limit").and_then(|v| v.as_i64()).unwrap_or(100);

    ctx.storage
        .with_transaction(|conn| {
            let duplicates = find_near_duplicates(conn, threshold, limit)?;
            Ok(json!({"found": duplicates.len(), "duplicates": duplicates}))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn quality_get_duplicates(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::get_pending_duplicates;

    let limit = params.get("limit").and_then(|v| v.as_i64()).unwrap_or(50);

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

pub fn quality_find_conflicts(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::{detect_conflicts, ContextQualityConfig};

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

    let config = ContextQualityConfig::default();

    ctx.storage
        .with_transaction(|conn| {
            let conflicts = detect_conflicts(conn, id, &config)?;
            Ok(json!({"found": conflicts.len(), "conflicts": conflicts}))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn quality_get_conflicts(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::get_unresolved_conflicts;

    let limit = params.get("limit").and_then(|v| v.as_i64()).unwrap_or(50);

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

pub fn quality_resolve_conflict(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::{resolve_conflict, ResolutionType};

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

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

    let resolution_type = match resolution_str {
        "keep_a" => ResolutionType::KeepA,
        "keep_b" => ResolutionType::KeepB,
        "merge" => ResolutionType::Merge,
        "keep_both" => ResolutionType::KeepBoth,
        "delete_both" => ResolutionType::DeleteBoth,
        "false_positive" => ResolutionType::FalsePositive,
        _ => return json!({"error": format!("Invalid resolution type: {}", resolution_str)}),
    };

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

    ctx.storage
        .with_transaction(|conn| {
            resolve_conflict(conn, conflict_id, resolution_type, notes)?;
            Ok(json!({
                "conflict_id": conflict_id,
                "resolution": resolution_str,
                "resolved": true
            }))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn quality_source_trust(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::{get_source_trust, update_source_trust};

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

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

    if let Some(trust_score) = params.get("trust_score").and_then(|v| v.as_f64()) {
        let notes = params.get("notes").and_then(|v| v.as_str());

        return ctx
            .storage
            .with_transaction(|conn| {
                update_source_trust(
                    conn,
                    source_type,
                    source_identifier,
                    trust_score as f32,
                    notes,
                )?;
                Ok(json!({
                    "source_type": source_type,
                    "source_identifier": source_identifier,
                    "trust_score": trust_score,
                    "updated": true
                }))
            })
            .unwrap_or_else(|e| json!({"error": e.to_string()}));
    }

    ctx.storage
        .with_connection(
            |conn| match get_source_trust(conn, source_type, source_identifier) {
                Ok(score) => Ok(json!(score)),
                Err(_) => Ok(json!({
                    "source_type": source_type,
                    "source_identifier": source_identifier,
                    "trust_score": 0.7,
                    "notes": "Default trust score"
                })),
            },
        )
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn quality_improve(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::{calculate_quality_score, ContextQualityConfig};

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

    let config = ContextQualityConfig::default();

    ctx.storage
        .with_transaction(|conn| {
            let score = calculate_quality_score(conn, id, &config)?;
            Ok(json!({
                "memory_id": id,
                "current_quality": score.overall,
                "grade": score.grade.to_string(),
                "suggestions": score.suggestions,
                "component_scores": {
                    "clarity": score.clarity,
                    "completeness": score.completeness,
                    "freshness": score.freshness,
                    "consistency": score.consistency,
                    "source_trust": score.source_trust
                }
            }))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

// ── Salience Tools ────────────────────────────────────────────────────────────

pub fn salience_get(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::{get_memory_salience_with_feedback, SalienceConfig};

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

    let feedback_signal = params
        .get("feedback_signal")
        .and_then(|v| v.as_f64())
        .unwrap_or(0.0) as f32;
    let feedback_normalized = ((feedback_signal + 1.0) / 2.0).clamp(0.0, 1.0);

    ctx.storage
        .with_connection(|conn| {
            let config = SalienceConfig::default();
            let score = get_memory_salience_with_feedback(conn, id, &config, feedback_normalized)?;
            Ok(json!(score))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn salience_set_importance(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::set_memory_importance;

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

    let importance = match params.get("importance").and_then(|v| v.as_f64()) {
        Some(imp) => imp as f32,
        None => return json!({"error": "importance is required"}),
    };

    ctx.storage
        .with_transaction(|conn| {
            set_memory_importance(conn, id, importance)?;
            Ok(json!({"id": id, "importance": importance, "updated": true}))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn salience_boost(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::boost_memory_salience;

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

    let boost_amount = params
        .get("boost_amount")
        .and_then(|v| v.as_f64())
        .unwrap_or(0.2) as f32;

    ctx.storage
        .with_transaction(|conn| {
            let entry = boost_memory_salience(conn, id, boost_amount)?;
            Ok(json!(entry))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn salience_demote(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::demote_memory_salience;

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

    let demote_amount = params
        .get("demote_amount")
        .and_then(|v| v.as_f64())
        .unwrap_or(0.2) as f32;

    ctx.storage
        .with_transaction(|conn| {
            let entry = demote_memory_salience(conn, id, demote_amount)?;
            Ok(json!(entry))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn salience_decay_run(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::{run_salience_decay_in_workspace, SalienceConfig};

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

    let record_history = params
        .get("record_history")
        .and_then(|v| v.as_bool())
        .unwrap_or(!dry_run);

    let workspace = params
        .get("workspace")
        .and_then(|v| v.as_str())
        .map(String::from);

    let mut config = SalienceConfig::default();

    if let Some(days) = params.get("stale_threshold_days").and_then(|v| v.as_f64()) {
        config.stale_threshold_days = days.max(1.0).round() as i64;
    } else if let Some(days) = params.get("stale_threshold").and_then(|v| v.as_f64()) {
        config.stale_threshold_days = days.max(1.0).round() as i64;
    }

    if let Some(days) = params
        .get("archive_threshold_days")
        .and_then(|v| v.as_f64())
    {
        config.archive_threshold_days = days.max(1.0).round() as i64;
    } else if let Some(days) = params.get("archive_threshold").and_then(|v| v.as_f64()) {
        config.archive_threshold_days = days.max(1.0).round() as i64;
    }

    if dry_run {
        return ctx
            .storage
            .with_connection(|conn| {
                conn.execute("BEGIN IMMEDIATE", [])?;
                let result =
                    run_salience_decay_in_workspace(conn, &config, false, workspace.as_deref());
                let _ = conn.execute("ROLLBACK", []);
                Ok(json!({"dry_run": true, "result": result?}))
            })
            .unwrap_or_else(|e| json!({"error": e.to_string()}));
    }

    ctx.storage
        .with_transaction(|conn| {
            let result = run_salience_decay_in_workspace(
                conn,
                &config,
                record_history,
                workspace.as_deref(),
            )?;
            Ok(json!(result))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn salience_stats(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::{get_salience_stats_in_workspace, SalienceConfig};

    let workspace = params
        .get("workspace")
        .and_then(|v| v.as_str())
        .map(String::from);

    ctx.storage
        .with_connection(|conn| {
            let config = SalienceConfig::default();
            let stats = get_salience_stats_in_workspace(conn, &config, workspace.as_deref())?;
            Ok(json!(stats))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn salience_history(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::get_salience_history;

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

    let limit = params.get("limit").and_then(|v| v.as_i64()).unwrap_or(50);

    ctx.storage
        .with_connection(|conn| {
            let history = get_salience_history(conn, id, limit)?;
            Ok(json!(history))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}

pub fn salience_top(ctx: &HandlerContext, params: Value) -> Value {
    use crate::intelligence::{SalienceCalculator, SalienceConfig};
    use crate::storage::queries::list_memories;
    use crate::types::ListOptions;

    let limit = params.get("limit").and_then(|v| v.as_i64()).unwrap_or(20) as usize;

    let workspace = params
        .get("workspace")
        .and_then(|v| v.as_str())
        .map(String::from);

    let config = SalienceConfig::default();
    let calculator = SalienceCalculator::new(config);

    ctx.storage
        .with_connection(|conn| {
            let options = ListOptions {
                workspace: workspace.clone(),
                limit: Some(1000),
                ..Default::default()
            };
            let memories = list_memories(conn, &options)?;
            let mut scored = calculator.priority_queue(&memories);
            scored.truncate(limit);
            let items: Vec<Value> = scored
                .into_iter()
                .map(|s| {
                    json!({
                        "id": s.memory.id,
                        "content": s.memory.content,
                        "memory_type": s.memory.memory_type,
                        "workspace": s.memory.workspace,
                        "importance": s.memory.importance,
                        "salience": s.salience
                    })
                })
                .collect();
            Ok(json!({"count": items.len(), "memories": items}))
        })
        .unwrap_or_else(|e| json!({"error": e.to_string()}))
}