MenteDB: AI Agent Memory Database
MenteDB is a purpose-built database engine designed for AI agent memory, providing capabilities like vector similarity search, a typed knowledge graph, token budget-aware context assembly, contradiction detection, and trajectory tracking. It delivers its full Rust-based engine as a native Node.js addon via `napi-rs`, offering zero runtime dependencies for prebuilt binaries. The package is currently at version 0.3.1, indicating an early development stage with potentially rapid API evolution. It releases frequently, with minor versions and bug fixes appearing regularly across its main package and internal sub-packages like `mentedb-storage` and `mentedb-query`. Its key differentiators include its focus on agent-specific cognitive features and its performance benefits from the Rust-native implementation.
Common errors
-
Error: No prebuilt binary for your platform...
cause The `mentedb` package includes a native Node.js addon, and `npm install` failed to find a precompiled binary for your operating system and architecture.fixInstall Rust and `napi-rs` build tools, then try installing again. Alternatively, check the GitHub releases for prebuilt binaries for your specific platform or consult the project's documentation for supported environments. -
TypeError: MenteDB is not a constructor
cause This error typically occurs when attempting to use CommonJS `require()` syntax to import `mentedb`, which is an ESM-first package.fixEnsure your project is configured for ES Modules (add `"type": "module"` to `package.json`) and use `import { MenteDB } from 'mentedb';` syntax. -
Error: Failed to open database at path...
cause The specified `dataDir` path for `new MenteDB(dataDir)` is invalid, lacks write permissions, or points to a corrupted database file.fixVerify the `dataDir` path exists and is writable by the Node.js process. Ensure no other process is actively using the same database directory. If corruption is suspected, try deleting the directory and restarting (losing data).
Warnings
- breaking As a pre-1.0 release (currently v0.3.1), MenteDB's API is subject to frequent changes. Major and minor version bumps may introduce breaking changes without extensive migration guides.
- gotcha If a prebuilt native binary is not available for your platform, `npm install` may fail. Building from source requires Rust and the `napi-rs` toolchain to be installed and correctly configured on your system.
- gotcha MenteDB's `store` and `search` methods rely on external embedding vectors. You must integrate a separate AI model (e.g., an LLM embedding API) to generate these `number[]` arrays before interacting with the database.
Install
-
npm install mentedb -
yarn add mentedb -
pnpm add mentedb
Imports
- MenteDB
const MenteDB = require('mentedb');import { MenteDB } from 'mentedb'; - MemoryType
import { MemoryType } from 'mentedb'; - CognitionStream
import CognitionStream from 'mentedb';
import { CognitionStream } from 'mentedb'; - EdgeType
import { EdgeType } from 'mentedb';
Quickstart
import { MenteDB, MemoryType, EdgeType, CognitionStream, TrajectoryTracker } from 'mentedb';
// Initialize your embedding model (example placeholder)
const embeddingFromYourModel = Array(1536).fill(0.123);
const queryEmbedding = Array(1536).fill(0.456);
async function runMenteDB() {
// Open or create a database
const db = new MenteDB('./my-agent-memory-data');
console.log('MenteDB initialized.');
// Store a memory
const id = db.store({
content: 'The deploy key rotates every 90 days',
memoryType: MemoryType.Semantic,
embedding: embeddingFromYourModel,
tags: ['infra', 'security'],
});
console.log(`Stored memory with ID: ${id}`);
// Vector similarity search
const hits = db.search(queryEmbedding, 2);
console.log('Search hits:', hits);
// MQL recall with token budget
const ctx = db.recall('RECALL similar("deploy key rotation") LIMIT 10');
console.log('Recall context:', ctx.text, `(${ctx.totalTokens} tokens)`);
// Relate memories (assuming 'otherId' exists, e.g., from another store call)
// const otherId = db.store({ content: 'Another related fact', memoryType: MemoryType.Semantic, embedding: embeddingFromYourModel });
// db.relate(id, otherId, EdgeType.Supersedes);
// console.log(`Related ${id} to ${otherId}`);
// Simulate an LLM token stream for cognition
const stream = new CognitionStream();
const llmTokens = ['Hello', ' ', 'world', '!'];
for (const token of llmTokens) {
stream.feedToken(token);
}
console.log('Drained CognitionStream buffer:', stream.drainBuffer());
// Track reasoning trajectory
const tracker = new TrajectoryTracker();
tracker.recordTurn('Discuss JWT auth', 'investigating', ['Which algorithm?', 'Token lifetime?']);
tracker.recordTurn('Token lifetime', 'decided:15 minutes');
console.log('Resume context:', tracker.getResumeContext());
console.log('Predicted next topics:', tracker.predictNextTopics());
// Forget a memory
db.forget(id);
console.log(`Forgot memory with ID: ${id}`);
// Close the database
db.close();
console.log('MenteDB closed.');
}
runMenteDB().catch(console.error);