reasonkit-mem 0.1.7

High-performance vector database & RAG memory layer - hybrid search, embeddings, RAPTOR trees, BM25 fusion, and semantic retrieval for AI systems
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
//! `rk-mem` — ReasonKit Mem utilities.
//!
//! Currently this focuses on docset ingestion (Cursor-like `@Docs`) without requiring
//! `reasonkit-org`.
//!
//! Build:
//! - `cargo build --bin rk-mem --features docset`
//!
//! Examples:
//! - `rk-mem docs add react https://react.dev/reference/`
//! - `rk-mem docs list`
//! - `rk-mem docs refresh --due`
//! - `rk-mem docs remove <DOCSET_ID>`

use anyhow::{anyhow, Context as _};
use chrono::Utc;
use reasonkit_mem::docset::{
    open_default_docset_retriever, Docset, DocsetIngestOptions, DocsetIngestor, DocsetStore,
    DocsetStoreConfig, RefreshStatus,
};
use reasonkit_mem::storage::{AccessContext, AccessLevel};
use serde::Serialize;
use std::collections::HashMap;
use std::env;
use std::path::{Path, PathBuf};
use uuid::Uuid;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let args: Vec<String> = env::args().collect();
    if args.len() < 2 {
        print_usage();
        return Ok(());
    }

    match args[1].as_str() {
        "docs" => docs_main(&args[2..]).await?,
        "help" | "-h" | "--help" => print_usage(),
        other => {
            eprintln!("Unknown command: {other}");
            print_usage();
        }
    }

    Ok(())
}

fn print_usage() {
    println!(
        r#"rk-mem — ReasonKit Mem utilities

USAGE:
    rk-mem <COMMAND> [ARGS...]

COMMANDS:
    docs    Docset ingestion and refresh (Cursor-like @Docs)

Run:
    rk-mem docs --help

ENV:
    RKMEM_DATA_DIR   Base directory for docsets and indexes
"#
    );
}

async fn docs_main(args: &[String]) -> anyhow::Result<()> {
    if args.is_empty() || matches!(args[0].as_str(), "help" | "-h" | "--help") {
        print_docs_usage();
        return Ok(());
    }

    let data_dir = data_dir();
    let store = DocsetStore::new(&data_dir, DocsetStoreConfig::default());

    match args[0].as_str() {
        "add" => cmd_docs_add(&store, args).await,
        "list" => cmd_docs_list(&store).await,
        "query" => cmd_docs_query(&store, &data_dir, args).await,
        "remove" | "delete" => cmd_docs_remove(&store, &data_dir, args).await,
        "refresh" => cmd_docs_refresh(&store, &data_dir, args).await,
        other => Err(anyhow!("Unknown docs subcommand: {other}")),
    }
}

fn print_docs_usage() {
    println!(
        r#"rk-mem docs — docset ingestion (Cursor-like @Docs)

USAGE:
    rk-mem docs add <NAME> <START_URL> [ALLOWED_PREFIX...]
    rk-mem docs list
    rk-mem docs query <QUERY> [--docset <NAME|ID>] [--top-k N] [--json]
    rk-mem docs remove <DOCSET_ID> [--keep-index]
    rk-mem docs refresh [--due] [--max-pages N] [--concurrency N] [--timeout-secs N]

NOTES:
  - If ALLOWED_PREFIX is omitted, START_URL is used as the only allowed prefix.
  - `refresh --due` only refreshes docsets whose refresh policy is due.

ENV:
  - RKMEM_DATA_DIR (default: platform data dir + /reasonkit/mem)
"#
    );
}

fn data_dir() -> PathBuf {
    if let Ok(dir) = env::var("RKMEM_DATA_DIR") {
        return PathBuf::from(dir);
    }
    dirs::data_local_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join("reasonkit")
        .join("mem")
}

async fn cmd_docs_add(store: &DocsetStore, args: &[String]) -> anyhow::Result<()> {
    if args.len() < 3 {
        print_docs_usage();
        return Ok(());
    }

    let name = args[1].clone();
    let start_url = args[2].clone();
    let allowed_prefixes: Vec<String> = if args.len() > 3 {
        args[3..].to_vec()
    } else {
        vec![start_url.clone()]
    };

    let docset = Docset::new(name, start_url, allowed_prefixes);
    let saved = store.upsert(docset).await?;
    println!("Added docset: {} ({})", saved.name, saved.id);
    Ok(())
}

async fn cmd_docs_list(store: &DocsetStore) -> anyhow::Result<()> {
    let docsets = store.load().await?;
    if docsets.is_empty() {
        println!("No docsets configured.");
        return Ok(());
    }

    let now = Utc::now();
    for ds in docsets {
        let due = ds.is_due(now);
        println!(
            "{}\n  name: {}\n  start_url: {}\n  refresh: {:?}\n  status: {:?}\n  due: {}\n",
            ds.id, ds.name, ds.start_url, ds.refresh, ds.status, due
        );
    }
    Ok(())
}

async fn cmd_docs_remove(
    store: &DocsetStore,
    data_dir: &Path,
    args: &[String],
) -> anyhow::Result<()> {
    if args.len() < 2 {
        print_docs_usage();
        return Ok(());
    }
    let id = Uuid::parse_str(&args[1]).context("Invalid DOCSET_ID")?;
    let keep_index = args.iter().any(|a| a == "--keep-index");

    let deleted = store.delete(id).await?;
    if !deleted {
        println!("Docset not found: {id}");
        return Ok(());
    }

    println!("Removed docset config: {id}");

    if keep_index {
        return Ok(());
    }

    // Best-effort: delete any indexed documents tagged with this docset id.
    let retriever = open_default_docset_retriever(data_dir.to_path_buf()).await?;
    let ctx = AccessContext::new(
        "rk-mem".to_string(),
        AccessLevel::Admin,
        "docs_remove".to_string(),
    );

    let doc_ids = retriever
        .storage()
        .list_documents(&ctx)
        .await
        .context("Failed to list documents")?;

    let wanted_tag = format!("docset_id:{id}");
    let mut removed_docs = 0usize;
    for doc_id in doc_ids {
        let doc = retriever
            .storage()
            .get_document(&doc_id, &ctx)
            .await
            .context("Failed to read document")?;
        let Some(doc) = doc else {
            continue;
        };
        if doc.metadata.tags.iter().any(|t| t == &wanted_tag) {
            let _ = retriever.delete_document(&doc_id).await;
            removed_docs += 1;
        }
    }

    println!("Removed {removed_docs} document(s) from index.");
    Ok(())
}

#[derive(Debug, Serialize)]
struct DocsQueryHit {
    score: f32,
    sparse_score: Option<f32>,
    doc_id: String,
    chunk_id: String,
    url: Option<String>,
    title: Option<String>,
    docset: Option<String>,
    docset_id: Option<String>,
    text: String,
}

async fn cmd_docs_query(
    store: &DocsetStore,
    data_dir: &Path,
    args: &[String],
) -> anyhow::Result<()> {
    if args.len() < 2 {
        print_docs_usage();
        return Ok(());
    }

    let query = args[1].as_str();
    let top_k: usize = if let Some(v) = parse_arg_value(args, "--top-k") {
        v.parse().context("Invalid --top-k")?
    } else {
        8
    };

    let json = args.iter().any(|a| a == "--json");

    let docset_filter = parse_arg_value(args, "--docset");
    let wanted_docset_id = if let Some(filter) = docset_filter.as_deref() {
        if let Ok(id) = Uuid::parse_str(filter) {
            Some(id)
        } else {
            let docsets = store.load().await?;
            let Some(found) = docsets.iter().find(|d| d.name.eq_ignore_ascii_case(filter)) else {
                return Err(anyhow!(
                    "Unknown docset: {filter} (expected name or UUID; see `rk-mem docs list`)"
                ));
            };
            Some(found.id)
        }
    } else {
        None
    };

    let retriever = open_default_docset_retriever(data_dir.to_path_buf()).await?;
    let ctx = AccessContext::new(
        "rk-mem".to_string(),
        AccessLevel::Admin,
        "docs_query".to_string(),
    );

    // Over-fetch, then filter to docset-tagged docs.
    let candidate_k = if top_k >= 200 {
        top_k
    } else {
        top_k.saturating_mul(5).min(200)
    };
    let results = retriever.search_sparse(query, candidate_k).await?;

    let mut doc_cache: HashMap<Uuid, Option<reasonkit_mem::Document>> = HashMap::new();
    let mut hits: Vec<DocsQueryHit> = Vec::with_capacity(top_k);

    for r in results {
        if hits.len() >= top_k {
            break;
        }

        let doc = if let Some(cached) = doc_cache.get(&r.doc_id) {
            cached.clone()
        } else {
            let loaded = retriever
                .storage()
                .get_document(&r.doc_id, &ctx)
                .await
                .context("Failed to read document")?;
            doc_cache.insert(r.doc_id, loaded.clone());
            loaded
        };

        let Some(doc) = doc else {
            continue;
        };

        // Restrict to docset docs by default.
        let docset_id_tag = doc
            .metadata
            .tags
            .iter()
            .find(|t| t.starts_with("docset_id:"))
            .cloned();
        if docset_id_tag.is_none() {
            continue;
        }

        if let Some(wanted) = wanted_docset_id {
            let wanted_tag = format!("docset_id:{wanted}");
            if !doc.metadata.tags.iter().any(|t| t == &wanted_tag) {
                continue;
            }
        }

        let docset_tag = doc
            .metadata
            .tags
            .iter()
            .find(|t| t.starts_with("docset:"))
            .map(|t| t.trim_start_matches("docset:").to_string());

        hits.push(DocsQueryHit {
            score: r.score,
            sparse_score: r.sparse_score,
            doc_id: r.doc_id.to_string(),
            chunk_id: r.chunk_id.to_string(),
            url: doc.source.url.clone(),
            title: doc.metadata.title.clone(),
            docset: docset_tag,
            docset_id: docset_id_tag.map(|t| t.trim_start_matches("docset_id:").to_string()),
            text: r.text,
        });
    }

    if json {
        println!("{}", serde_json::to_string_pretty(&hits)?);
        return Ok(());
    }

    if hits.is_empty() {
        println!("No results.");
        return Ok(());
    }

    for (i, h) in hits.iter().enumerate() {
        let url = h.url.as_deref().unwrap_or("<no-url>");
        let title = h.title.as_deref().unwrap_or("<no-title>");
        println!(
            "#{:02} score={:.4} docset={} url={}\n  {}\n",
            i + 1,
            h.score,
            h.docset.as_deref().unwrap_or("<unknown>"),
            url,
            title
        );
    }

    Ok(())
}

async fn cmd_docs_refresh(
    store: &DocsetStore,
    data_dir: &Path,
    args: &[String],
) -> anyhow::Result<()> {
    let mut opts = DocsetIngestOptions {
        manifest_dir: Some(data_dir.join("docsets")),
        ..Default::default()
    };

    for w in args.iter().skip(1) {
        if w.as_str() == "--due" {
            opts.refresh_due_only = true;
        }
    }

    if let Some(v) = parse_arg_value(args, "--max-pages") {
        opts.max_pages = v.parse().context("Invalid --max-pages")?;
    }
    if let Some(v) = parse_arg_value(args, "--concurrency") {
        opts.concurrency = v.parse().context("Invalid --concurrency")?;
    }
    if let Some(v) = parse_arg_value(args, "--timeout-secs") {
        let secs: u64 = v.parse().context("Invalid --timeout-secs")?;
        opts.request_timeout = std::time::Duration::from_secs(secs);
    }

    let mut docsets = store.load().await?;
    if docsets.is_empty() {
        println!("No docsets configured.");
        return Ok(());
    }

    let retriever = open_default_docset_retriever(data_dir.to_path_buf()).await?;
    let ingestor = DocsetIngestor::new(retriever)?;

    for ds in docsets.iter_mut() {
        let res = ingestor.ingest_docset(ds, &opts).await;
        match res {
            Ok(report) => {
                println!(
                    "Docset {} ({}) — method={}, discovered={}, fetched={}, indexed={}, skipped={}, removed={}, failures={}",
                    report.docset_name,
                    report.docset_id,
                    report.discovery_method,
                    report.discovered_urls,
                    report.fetched_pages,
                    report.indexed_pages,
                    report.skipped_unchanged,
                    report.removed_pages,
                    report.failures
                );
            }
            Err(e) => {
                ds.status = RefreshStatus::Error {
                    at: Utc::now(),
                    message: e.to_string(),
                };
                eprintln!("Docset {} ({}) — ERROR: {e}", ds.name, ds.id);
            }
        }

        // Persist latest status / timestamps.
        let _ = store.upsert(ds.clone()).await;
    }

    Ok(())
}

fn parse_arg_value(args: &[String], key: &str) -> Option<String> {
    args.iter()
        .position(|a| a == key)
        .and_then(|idx| args.get(idx + 1))
        .cloned()
}