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/// Wall-clock "today" as a `YYYYMMDD` integer, read only by `remember`'s
29/// auto-date stamping (see [`storage::AUTO_DATE_FIELD`]) — never by the
30/// context compiler, which stays clock-free and deterministic. Internal:
31/// nothing outside the crate needs to read the clock directly.
32mod clock;
33/// The deterministic context compiler (EPIC-P-070): classify, dedup, and pack
34/// caller-supplied context fragments under a token budget — no LLM, no cloud,
35/// every decision auditable. Gated behind the default `context` feature.
36#[cfg(feature = "context")]
37pub mod context;
38/// Format recalled facts as a chronological, date-prefixed timeline with a
39/// "now" anchor — the dated-context representation measured to lift temporal
40/// question answering, shipped as product behavior rather than a harness prompt.
41pub mod dated_context;
42pub mod embedder;
43pub mod error;
44pub mod extract;
45/// Vector+graph score fusion — the ranking layer behind
46/// [`service::MemoryService::recall_fused`]. Internal: callers reach it only
47/// through that method.
48mod fusion;
49/// The streamable-HTTP transport (multi-client mode): lets several MCP
50/// clients share ONE `velesdb-memory` process instead of each spawning its
51/// own stdio process and fighting over the store's single-writer `flock`.
52/// Gated behind the (non-default) `http` feature — see the module docs and
53/// the crate README's "HTTP transport (multi-client)" section.
54#[cfg(feature = "http")]
55pub mod http;
56/// Content-addressed memory ids — internal; ids surface through the service API.
57pub(crate) mod id;
58/// Resource caps (DoS limits) shared by every adapter — the single source of
59/// truth for fact size, recall limit, and `why` hop depth.
60pub mod limits;
61/// The MCP server transport. Gated behind the default `mcp` feature so library
62/// consumers (e.g. the language bindings) can depend on the memory core without
63/// pulling the `rmcp`/`tokio` server stack.
64#[cfg(feature = "mcp")]
65pub mod mcp;
66/// The domain data model — the value types the memory layer exchanges
67/// (`Link`, `Recollection`, `ColumnFilter`, `Explanation`, …), separate from the
68/// service that computes them.
69pub mod model;
70/// Synchronous retry + actionable failure reporting shared by the two blocking
71/// Ollama call sites ([`embedder`] and [`extract`]). Internal: it exists to make
72/// those two backends resilient, not to be a general-purpose retry API.
73#[cfg(any(feature = "ollama", feature = "extract"))]
74mod ollama_retry;
75/// Optional second-stage re-scoring of a fused recall pool (bring your own
76/// cross-encoder/LLM). Never wired in by default — see [`rerank::Reranker`].
77pub mod rerank;
78/// Shared JSON Schema post-processing (strips `schemars`' non-standard integer
79/// `format` keywords so strict MCP clients don't warn on every id field).
80mod schema;
81pub mod service;
82/// The storage backend abstraction — [`storage::MemoryStore`] and the
83/// default, file-backed [`storage::NativeStore`]. Implement `MemoryStore` to
84/// run the wedge over a different backend (e.g. an in-memory one for WASM).
85pub mod storage;
86/// Locally-generated TLS material (a cached self-signed CA + short-lived
87/// leaf certs) for the streamable-HTTP transport's HTTPS-by-default
88/// listener — see the module docs for the full design rationale. Gated
89/// behind `http` since it exists only to serve that transport.
90#[cfg(feature = "http")]
91pub mod tls;
92
93/// Default embedding dimension — the single source of truth, taken from the
94/// SDK's own default so the server, library, and tests never restate the
95/// value. `velesdb_core::agent` (where the canonical constant lives) is
96/// itself `persistence`-gated, so a `persistence`-free build (e.g.
97/// `velesdb-wasm`) falls back to `FALLBACK_DIMENSION`.
98#[cfg(feature = "persistence")]
99pub const DEFAULT_DIMENSION: usize = velesdb_core::agent::DEFAULT_DIMENSION;
100#[cfg(not(feature = "persistence"))]
101pub const DEFAULT_DIMENSION: usize = FALLBACK_DIMENSION;
102
103/// The hand-written value the `persistence`-free arm of
104/// [`DEFAULT_DIMENSION`] falls back to (the canonical constant's module is
105/// feature-gated away there). The `persistence` build — CI's default —
106/// statically asserts it still equals the canonical value, so drift fails
107/// to compile instead of silently splitting the wasm default dimension
108/// from the native one.
109const FALLBACK_DIMENSION: usize = 384;
110#[cfg(feature = "persistence")]
111const _: () = assert!(
112    FALLBACK_DIMENSION == velesdb_core::agent::DEFAULT_DIMENSION,
113    "update FALLBACK_DIMENSION to match velesdb_core::agent::DEFAULT_DIMENSION"
114);
115
116#[cfg(feature = "context")]
117pub use context::ContextCompiler;
118pub use dated_context::{format_dated_context, DatedContext};
119pub use embedder::{DynEmbedder, EmbedError, Embedder, HashEmbedder};
120#[cfg(feature = "ollama")]
121pub use embedder::{OllamaEmbedder, DEFAULT_OLLAMA_MODEL, DEFAULT_OLLAMA_URL};
122pub use error::{ErrorCategory, MemoryError};
123#[cfg(feature = "extract")]
124pub use extract::OllamaExtractor;
125pub use extract::{DynExtractor, ExtractError, ExtractedFact, Extractor};
126#[cfg(feature = "mcp")]
127pub use mcp::McpServer;
128pub use model::{
129    ColumnFilter, ColumnOp, Explanation, FusionOptions, Link, MemoryEdge, MemoryNode, Recollection,
130};
131pub use rerank::{DynReranker, RerankError, Reranker};
132pub use service::{MemoryService, Metadata};
133#[cfg(feature = "persistence")]
134pub use storage::NativeStore;
135pub use storage::{MemoryStore, AUTO_DATE_FIELD};