mempalace-rs 0.4.2

High-performance, local AI memory with AAAK v3.2 protocol and temporal Knowledge Graph
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
use crate::config::MempalaceConfig;
use crate::storage::MemoryStack;
use crate::vector_storage::VectorStorage;
use anyhow::Result;
use std::collections::HashMap;
use std::path::PathBuf;

// Note: Custom VectorStorage (fastembed + usearch + rusqlite) is used.

/// High-level search interface for retrieving context from the Palace.
pub struct Searcher {
    pub config: MempalaceConfig,
}

impl Searcher {
    pub fn new(config: MempalaceConfig) -> Self {
        Searcher { config }
    }

    fn open_vector_storage(&self) -> Option<VectorStorage> {
        VectorStorage::new(
            self.config.config_dir.join("vectors.db"),
            self.config.config_dir.join("vectors.usearch"),
        )
        .ok()
    }

    pub fn add_memory(
        &self,
        text: &str,
        wing: &str,
        room: &str,
        source_file: Option<&str>,
        source_mtime: Option<f64>,
    ) -> Result<i64> {
        let mut store = self
            .open_vector_storage()
            .ok_or_else(|| anyhow::anyhow!("Vector storage unavailable"))?;
        let id = store.add_memory(text, wing, room, source_file, source_mtime)?;
        store.save_index(self.config.config_dir.join("vectors.usearch"))?;
        Ok(id)
    }

    pub fn delete_memory(&self, memory_id: i64) -> Result<()> {
        let store = self
            .open_vector_storage()
            .ok_or_else(|| anyhow::anyhow!("Vector storage unavailable"))?;
        store.delete_memory(memory_id)?;
        store.save_index(self.config.config_dir.join("vectors.usearch"))?;
        Ok(())
    }

    pub async fn wake_up(&self, wing: Option<String>) -> Result<String> {
        let mut stack = MemoryStack::new(self.config.clone());
        Ok(stack.wake_up(wing).await)
    }

    pub fn build_where_clause(
        wing: Option<&String>,
        room: Option<&String>,
    ) -> Option<serde_json::Value> {
        let mut where_clause = HashMap::<String, serde_json::Value>::new();
        if let (Some(w), Some(r)) = (wing, room) {
            let mut and_vec = Vec::new();
            let mut w_map = HashMap::<String, serde_json::Value>::new();
            w_map.insert("wing".to_string(), serde_json::Value::String(w.to_string()));
            and_vec.push(serde_json::Value::Object(w_map.into_iter().collect()));

            let mut r_map = HashMap::<String, serde_json::Value>::new();
            r_map.insert("room".to_string(), serde_json::Value::String(r.to_string()));
            and_vec.push(serde_json::Value::Object(r_map.into_iter().collect()));

            where_clause.insert("$and".to_string(), serde_json::Value::Array(and_vec));
        } else if let Some(w) = wing {
            where_clause.insert("wing".to_string(), serde_json::Value::String(w.to_string()));
        } else if let Some(r) = room {
            where_clause.insert("room".to_string(), serde_json::Value::String(r.to_string()));
        }

        if where_clause.is_empty() {
            None
        } else {
            Some(serde_json::to_value(where_clause).unwrap())
        }
    }

    pub fn format_search_results(
        query: &str,
        wing: Option<&String>,
        room: Option<&String>,
        docs: &[String],
        metas: &[Option<serde_json::Map<String, serde_json::Value>>],
        dists: &[f32],
    ) -> String {
        if docs.is_empty() || docs[0].is_empty() {
            return format!("\n  No results found for: \"{}\"", query);
        }

        let mut output = String::new();
        output.push_str(&format!("\n{}", "=".repeat(60)));
        output.push_str(&format!("\n  Results for: \"{}\"", query));
        if let Some(w) = &wing {
            output.push_str(&format!("\n  Wing: {}", w));
        }
        if let Some(r) = &room {
            output.push_str(&format!("\n  Room: {}", r));
        }
        output.push_str(&format!("\n{}\n", "=".repeat(60)));

        for i in 0..docs.len() {
            let doc = &docs[i];
            let meta = &metas[i];
            let dist = dists[i];

            let similarity = 1.0 - dist;
            let wing_name = meta
                .as_ref()
                .and_then(|m: &serde_json::Map<String, serde_json::Value>| m.get("wing"))
                .and_then(|v: &serde_json::Value| v.as_str())
                .unwrap_or("?");
            let room_name = meta
                .as_ref()
                .and_then(|m: &serde_json::Map<String, serde_json::Value>| m.get("room"))
                .and_then(|v: &serde_json::Value| v.as_str())
                .unwrap_or("?");
            let source = meta
                .as_ref()
                .and_then(|m: &serde_json::Map<String, serde_json::Value>| m.get("source_file"))
                .and_then(|v: &serde_json::Value| v.as_str())
                .unwrap_or("");
            let source_name = PathBuf::from(source)
                .file_name()
                .and_then(|s| s.to_str())
                .unwrap_or("?")
                .to_string();

            output.push_str(&format!("\n  [{}] {} / {}", i + 1, wing_name, room_name));
            output.push_str(&format!("\n      Source: {}", source_name));
            output.push_str(&format!("\n      Match:  {:.3}\n", similarity));

            let trimmed = doc.trim();
            for line in trimmed.split('\n') {
                output.push_str(&format!("\n      {}", line));
            }
            output.push('\n');
            output.push_str(&format!("\n  {}", "─".repeat(56)));
        }

        output
    }

    pub async fn search(
        &self,
        query: &str,
        wing: Option<String>,
        room: Option<String>,
        n_results: usize,
    ) -> Result<String> {
        // Use pure-Rust VectorStorage (lazy initialization)
        let Some(store) = self.open_vector_storage() else {
            return Ok(format!(
                "\n  No results found for: \"{}\" (vector storage unavailable)",
                query
            ));
        };

        // Use search_room for pre-filtered search if wing+room provided, else global search
        let records = match (&wing, &room) {
            (Some(w), Some(r)) => store.search_room(query, w, r, n_results, None)?,
            _ => store.search(query, n_results)?,
        };

        if records.is_empty() {
            return Ok(format!("\n  No results found for: \"{}\"", query));
        }

        // Convert records to legacy format for display
        let docs: Vec<String> = records.iter().map(|r| r.text_content.clone()).collect();
        let metas: Vec<Option<serde_json::Map<String, serde_json::Value>>> = records
            .iter()
            .map(|r| {
                let mut m = serde_json::Map::new();
                m.insert(
                    "wing".to_string(),
                    serde_json::Value::String(r.wing.clone()),
                );
                m.insert(
                    "room".to_string(),
                    serde_json::Value::String(r.room.clone()),
                );
                m.insert(
                    "valid_from".to_string(),
                    serde_json::Value::Number(r.valid_from.into()),
                );
                Some(m)
            })
            .collect();
        let dists: Vec<f32> = records.iter().map(|r| 1.0 - r.score).collect();

        let output =
            Self::format_search_results(query, wing.as_ref(), room.as_ref(), &docs, &metas, &dists);
        Ok(output)
    }

    pub fn format_json_results(
        query: &str,
        wing: Option<&String>,
        room: Option<&String>,
        docs: &[String],
        metas: &[Option<serde_json::Map<String, serde_json::Value>>],
        dists: &[f32],
    ) -> serde_json::Value {
        let mut hits = Vec::new();
        if !docs.is_empty() && !docs[0].is_empty() {
            for i in 0..docs.len() {
                hits.push(serde_json::json!({
                    "text": docs[i],
                    "wing": metas[i].as_ref().and_then(|m: &serde_json::Map<String, serde_json::Value>| m.get("wing")).and_then(|v: &serde_json::Value| v.as_str()).unwrap_or("unknown"),
                    "room": metas[i].as_ref().and_then(|m: &serde_json::Map<String, serde_json::Value>| m.get("room")).and_then(|v: &serde_json::Value| v.as_str()).unwrap_or("unknown"),
                    "source_file": PathBuf::from(metas[i].as_ref().and_then(|m: &serde_json::Map<String, serde_json::Value>| m.get("source_file")).and_then(|v: &serde_json::Value| v.as_str()).unwrap_or("?")).file_name().and_then(|s| s.to_str()).unwrap_or("?"),
                    "similarity": 1.0 - dists[i]
                }));
            }
        }

        serde_json::json!({
            "query": query,
            "filters": {
                "wing": wing,
                "room": room
            },
            "results": hits
        })
    }

    pub async fn search_memories(
        &self,
        query: &str,
        wing: Option<String>,
        room: Option<String>,
        n_results: usize,
    ) -> Result<serde_json::Value> {
        // Use pure-Rust VectorStorage (lazy initialization)
        let Some(store) = self.open_vector_storage() else {
            return Ok(Self::format_json_results(
                query,
                wing.as_ref(),
                room.as_ref(),
                &[],
                &[],
                &[],
            ));
        };

        let records = match (&wing, &room) {
            (Some(w), Some(r)) => store.search_room(query, w, r, n_results, None)?,
            _ => store.search(query, n_results)?,
        };

        let docs: Vec<String> = records.iter().map(|r| r.text_content.clone()).collect();
        let metas: Vec<Option<serde_json::Map<String, serde_json::Value>>> = records
            .iter()
            .map(|r| {
                let mut m = serde_json::Map::new();
                m.insert(
                    "wing".to_string(),
                    serde_json::Value::String(r.wing.clone()),
                );
                m.insert(
                    "room".to_string(),
                    serde_json::Value::String(r.room.clone()),
                );
                m.insert("id".to_string(), serde_json::Value::Number(r.id.into()));
                Some(m)
            })
            .collect();
        let dists: Vec<f32> = records.iter().map(|r| 1.0 - r.score).collect();

        Ok(Self::format_json_results(
            query,
            wing.as_ref(),
            room.as_ref(),
            &docs,
            &metas,
            &dists,
        ))
    }
}

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

    #[test]
    fn test_searcher_new() {
        let config = MempalaceConfig::default();
        let searcher = Searcher::new(config);
        assert_eq!(
            searcher.config.collection_name,
            MempalaceConfig::default().collection_name
        );
    }

    #[test]
    fn test_format_search_results_empty() {
        let res = Searcher::format_search_results("hello", None, None, &[], &[], &[]);
        assert!(res.contains("No results found for: \"hello\""));

        let res2 =
            Searcher::format_search_results("world", None, None, &[String::new()], &[None], &[0.0]);
        assert!(res2.contains("No results found for: \"world\""));
    }

    #[test]
    fn test_format_search_results_with_data() {
        let docs = vec!["this is a test document".to_string()];

        let mut meta1 = serde_json::Map::new();
        meta1.insert(
            "wing".to_string(),
            serde_json::Value::String("engineering".to_string()),
        );
        meta1.insert(
            "room".to_string(),
            serde_json::Value::String("rust".to_string()),
        );
        meta1.insert(
            "source_file".to_string(),
            serde_json::Value::String("/path/to/some/file.txt".to_string()),
        );
        let metas = vec![Some(meta1)];
        let dists = vec![0.1_f32];

        let wing = Some("engineering".to_string());
        let room = Some("rust".to_string());

        let res = Searcher::format_search_results(
            "test",
            wing.as_ref(),
            room.as_ref(),
            &docs,
            &metas,
            &dists,
        );

        assert!(res.contains("Results for: \"test\""));
        assert!(res.contains("Wing: engineering"));
        assert!(res.contains("Room: rust"));
        assert!(res.contains("[1] engineering / rust"));
        assert!(res.contains("Source: file.txt"));
        assert!(res.contains("Match:  0.900"));
        assert!(res.contains("this is a test document"));
    }

    #[test]
    fn test_format_search_results_missing_metadata() {
        let docs = vec!["missing meta".to_string()];
        let metas = vec![None];
        let dists = vec![0.5_f32];

        let res = Searcher::format_search_results("meta", None, None, &docs, &metas, &dists);
        assert!(res.contains("[1] ? / ?"));
        assert!(res.contains("Source: ?"));
    }

    #[test]
    fn test_format_json_results_empty() {
        let res = Searcher::format_json_results("hello", None, None, &[], &[], &[]);
        assert_eq!(res["query"], "hello");
        assert!(res["results"].as_array().unwrap().is_empty());
    }

    #[test]
    fn test_format_json_results_with_data() {
        let docs = vec!["this is a json doc".to_string()];

        let mut meta1 = serde_json::Map::new();
        meta1.insert(
            "wing".to_string(),
            serde_json::Value::String("ops".to_string()),
        );
        meta1.insert(
            "room".to_string(),
            serde_json::Value::String("general".to_string()),
        );
        meta1.insert(
            "source_file".to_string(),
            serde_json::Value::String("/another/path/docs.md".to_string()),
        );
        let metas = vec![Some(meta1)];
        let dists = vec![0.2_f32];

        let wing = Some("ops".to_string());

        let res = Searcher::format_json_results("json", wing.as_ref(), None, &docs, &metas, &dists);

        assert_eq!(res["query"], "json");
        assert_eq!(res["filters"]["wing"], "ops");
        assert_eq!(res["filters"]["room"], serde_json::Value::Null);

        let results = res["results"].as_array().unwrap();
        assert_eq!(results.len(), 1);
        let first = &results[0];
        assert_eq!(first["text"], "this is a json doc");
        assert_eq!(first["wing"], "ops");
        assert_eq!(first["room"], "general");
        assert_eq!(first["source_file"], "docs.md");

        // Due to f32 float precision 1.0 - 0.2 might be 0.800000011920929
        let sim = first["similarity"].as_f64().unwrap();
        assert!((sim - 0.8).abs() < 0.0001);
    }

    #[test]
    fn test_format_json_results_missing_metadata() {
        let docs = vec!["no meta doc".to_string()];
        let metas = vec![None];
        let dists = vec![0.0_f32];

        let res = Searcher::format_json_results("missing", None, None, &docs, &metas, &dists);
        let results = res["results"].as_array().unwrap();
        assert_eq!(results.len(), 1);
        let first = &results[0];
        assert_eq!(first["wing"], "unknown");
        assert_eq!(first["room"], "unknown");
        assert_eq!(first["source_file"], "?");
    }

    #[test]
    fn test_build_where_clause_empty() {
        let res = Searcher::build_where_clause(None, None);
        assert_eq!(res, None);
    }

    #[test]
    fn test_build_where_clause_wing_only() {
        let wing = "engineering".to_string();
        let res = Searcher::build_where_clause(Some(&wing), None).unwrap();
        assert_eq!(res["wing"], "engineering");
    }

    #[test]
    fn test_build_where_clause_room_only() {
        let room = "rust".to_string();
        let res = Searcher::build_where_clause(None, Some(&room)).unwrap();
        assert_eq!(res["room"], "rust");
    }

    #[test]
    fn test_build_where_clause_wing_and_room() {
        let wing = "engineering".to_string();
        let room = "rust".to_string();
        let res = Searcher::build_where_clause(Some(&wing), Some(&room)).unwrap();

        let and_arr = res["$and"].as_array().unwrap();
        assert_eq!(and_arr.len(), 2);

        let mut has_wing = false;
        let mut has_room = false;

        for item in and_arr {
            if item.get("wing").is_some() {
                assert_eq!(item["wing"], "engineering");
                has_wing = true;
            }
            if item.get("room").is_some() {
                assert_eq!(item["room"], "rust");
                has_room = true;
            }
        }
        assert!(has_wing);
        assert!(has_room);
    }

    #[tokio::test]
    async fn test_search_graceful_when_unavailable() {
        let config = MempalaceConfig::default();
        let searcher = Searcher::new(config);

        let res = searcher.search("query", None, None, 5).await;
        assert!(res.is_ok());

        let res2 = searcher.search_memories("query", None, None, 5).await;
        assert!(res2.is_ok());
    }

    #[test]
    fn test_format_search_results_multiline_doc() {
        let docs = vec!["line 1\nline 2\nline 3".to_string()];
        let metas = vec![None];
        let dists = vec![0.1_f32];

        let res = Searcher::format_search_results("multi", None, None, &docs, &metas, &dists);
        assert!(res.contains("line 1"));
        assert!(res.contains("line 2"));
        assert!(res.contains("line 3"));
    }

    #[test]
    fn test_format_search_results_empty_pure() {
        assert!(
            Searcher::format_search_results("none", None, None, &[], &[], &[])
                .contains("No results found")
        );
    }

    #[test]
    fn test_format_json_results_empty_pure() {
        let res = Searcher::format_json_results("none", None, None, &[], &[], &[]);
        assert_eq!(res["results"].as_array().unwrap().len(), 0);
    }
}