Skip to main content

velesdb_memory/
lib.rs

1#![deny(unsafe_code)]
2//! # VelesDB-memory
3//!
4//! Local-first **memory** layer for AI agents, exposed through a single MCP
5//! server. This crate is the domain core: it maps nine memory operations onto
6//! `VelesDB`'s in-core Agent Memory SDK.
7//!
8//! | Operation           | Meaning                                            |
9//! |--------------------|-----------------------------------------------------|
10//! | `remember`          | store a fact (+ optional links to other memories)  |
11//! | `recall`            | semantic retrieval of similar facts                |
12//! | `recall_where`      | semantic retrieval filtered by metadata            |
13//! | `recall_fused`      | vector + graph fused retrieval                     |
14//! | `relate`            | create a typed edge between two memories           |
15//! | `forget`            | delete a memory                                    |
16//! | `why`               | recall + multi-hop graph traversal                 |
17//! | `feedback`          | reinforce or penalize a memory after use           |
18//! | `remember_extracted`| extract facts from raw text and auto-wire the graph|
19//!
20//! ## License boundary (non-negotiable)
21//!
22//! This crate exposes **memory semantics only** (results), never raw database
23//! capabilities (`query(velesql)`, `create_collection`, `upsert(vectors)`,
24//! `traverse(graph)`). Exposing the raw engine would constitute a "Substantial
25//! Set" of the Software's features and breach the `VelesDB` Core License 1.0
26//! (§1, No Hosted or Managed Service). See `VISION.md` §5 and `PLAN.md` Phase 4A.
27
28/// The deterministic context compiler (EPIC-P-070): classify, dedup, and pack
29/// caller-supplied context fragments under a token budget — no LLM, no cloud,
30/// every decision auditable. Gated behind the default `context` feature.
31#[cfg(feature = "context")]
32pub mod context;
33/// Format recalled facts as a chronological, date-prefixed timeline with a
34/// "now" anchor — the dated-context representation measured to lift temporal
35/// question answering, shipped as product behavior rather than a harness prompt.
36pub mod dated_context;
37pub mod embedder;
38pub mod error;
39pub mod extract;
40/// Vector+graph score fusion — the ranking layer behind
41/// [`service::MemoryService::recall_fused`]. Internal: callers reach it only
42/// through that method.
43mod fusion;
44/// Content-addressed memory ids — internal; ids surface through the service API.
45pub(crate) mod id;
46/// Resource caps (DoS limits) shared by every adapter — the single source of
47/// truth for fact size, recall limit, and `why` hop depth.
48pub mod limits;
49/// The MCP server transport. Gated behind the default `mcp` feature so library
50/// consumers (e.g. the language bindings) can depend on the memory core without
51/// pulling the `rmcp`/`tokio` server stack.
52#[cfg(feature = "mcp")]
53pub mod mcp;
54/// The domain data model — the value types the memory layer exchanges
55/// (`Link`, `Recollection`, `ColumnFilter`, `Explanation`, …), separate from the
56/// service that computes them.
57pub mod model;
58/// Optional second-stage re-scoring of a fused recall pool (bring your own
59/// cross-encoder/LLM). Never wired in by default — see [`rerank::Reranker`].
60pub mod rerank;
61/// Shared JSON Schema post-processing (strips `schemars`' non-standard integer
62/// `format` keywords so strict MCP clients don't warn on every id field).
63mod schema;
64pub mod service;
65/// The storage backend abstraction — [`storage::MemoryStore`] and the
66/// default, file-backed [`storage::NativeStore`]. Implement `MemoryStore` to
67/// run the wedge over a different backend (e.g. an in-memory one for WASM).
68pub mod storage;
69
70/// Default embedding dimension — the single source of truth, taken from the
71/// SDK's own default so the server, library, and tests never restate the
72/// value. `velesdb_core::agent` (where the canonical constant lives) is
73/// itself `persistence`-gated, so a `persistence`-free build (e.g.
74/// `velesdb-wasm`) falls back to `FALLBACK_DIMENSION`.
75#[cfg(feature = "persistence")]
76pub const DEFAULT_DIMENSION: usize = velesdb_core::agent::DEFAULT_DIMENSION;
77#[cfg(not(feature = "persistence"))]
78pub const DEFAULT_DIMENSION: usize = FALLBACK_DIMENSION;
79
80/// The hand-written value the `persistence`-free arm of
81/// [`DEFAULT_DIMENSION`] falls back to (the canonical constant's module is
82/// feature-gated away there). The `persistence` build — CI's default —
83/// statically asserts it still equals the canonical value, so drift fails
84/// to compile instead of silently splitting the wasm default dimension
85/// from the native one.
86const FALLBACK_DIMENSION: usize = 384;
87#[cfg(feature = "persistence")]
88const _: () = assert!(
89    FALLBACK_DIMENSION == velesdb_core::agent::DEFAULT_DIMENSION,
90    "update FALLBACK_DIMENSION to match velesdb_core::agent::DEFAULT_DIMENSION"
91);
92
93#[cfg(feature = "context")]
94pub use context::ContextCompiler;
95pub use dated_context::{format_dated_context, DatedContext};
96pub use embedder::{DynEmbedder, EmbedError, Embedder, HashEmbedder};
97#[cfg(feature = "ollama")]
98pub use embedder::{OllamaEmbedder, DEFAULT_OLLAMA_MODEL, DEFAULT_OLLAMA_URL};
99pub use error::{ErrorCategory, MemoryError};
100#[cfg(feature = "extract")]
101pub use extract::OllamaExtractor;
102pub use extract::{DynExtractor, ExtractError, ExtractedFact, Extractor};
103#[cfg(feature = "mcp")]
104pub use mcp::McpServer;
105pub use model::{
106    ColumnFilter, ColumnOp, Explanation, FusionOptions, Link, MemoryEdge, MemoryNode, Recollection,
107};
108pub use rerank::{DynReranker, RerankError, Reranker};
109pub use service::{MemoryService, Metadata};
110pub use storage::MemoryStore;
111#[cfg(feature = "persistence")]
112pub use storage::NativeStore;