@cortex-memory/core
v1.0.0
Published
Shared core library for MemoryAI packages - config, offline cache, API client
Maintainers
Readme
@memoryai/core
Shared core library for MemoryAI packages - config resolution, offline cache, and API client.
Features
- Unified Config System: Resolves config from multiple sources with priority: Per-IDE > Server > Shared Local > Env Vars > Defaults
- Offline Cache: SQLite-based cache for memories, works without network
- API Client: Robust client with retry logic, exponential backoff, and timeout handling
- Type Safety: Full TypeScript types and interfaces
Installation
npm install @memoryai/coreUsage
Basic Usage
import { MemoryClient } from '@memoryai/core';
const client = new MemoryClient({
config: {
endpoint: 'https://memoryai.dev',
apiKey: 'hm_sk_...',
}
});
// Recall memories
const result = await client.recall({
query: 'user preferences',
limit: 5
});
// Store memory
await client.store({
content: 'User prefers concise responses',
memory_type: 'preference'
});
// Compact context
await client.compact({
summary: 'Session summary...'
});Config Resolution
Config is resolved from multiple sources with priority:
- Per-IDE config (passed to constructor)
- Server overrides (fetched from API)
- Shared local config (
~/.memoryai/config.json) - Environment variables (
HM_ENDPOINT,HM_API_KEY, etc.) - Hardcoded defaults
// Shared config file: ~/.memoryai/config.json
{
"endpoint": "https://memoryai.dev",
"apiKey": "hm_sk_...",
"contextCap": 1000000,
"compactAt": 20,
"criticalAt": 30
}Offline Mode
The client automatically caches recall results in SQLite. If the server is unreachable, cached results are returned:
const client = new MemoryClient({ enableCache: true });
// First call - hits server and caches result
const result1 = await client.recall({ query: 'test' });
console.log(result1.cached); // false
// Second call - returns cached result
const result2 = await client.recall({ query: 'test' });
console.log(result2.cached); // true
// Clean old cache entries (older than 24h)
client.cleanCache(86400000);Health Check
const health = await client.health();
console.log(health.online); // true/false
console.log(health.latency_ms); // 123API
MemoryClient
Main client class.
Constructor:
new MemoryClient(options?: MemoryClientOptions)Methods:
recall(options: RecallOptions): Promise<RecallResult>store(options: StoreOptions): Promise<{ id: string }>compact(options: CompactOptions): Promise<void>health(): Promise<HealthStatus>updateConfig(updates: Partial<MemoryAIConfig>): voidgetConfig(): MemoryAIConfigcleanCache(maxAgeMs?: number): numbergetCacheStats(): { memories: number; queries: number; size_bytes: number } | nullclearCache(): voidclose(): void
Config Utilities
import { resolveConfig, readSharedConfig, writeSharedConfig, validateConfig } from '@memoryai/core';
// Read shared config
const config = readSharedConfig();
// Write shared config
writeSharedConfig({
endpoint: 'https://memoryai.dev',
apiKey: 'hm_sk_...'
});
// Validate config
const errors = validateConfig(config);Architecture
Used by:
memoryai-mcp- MCP server for Claude Desktop, VS Code, etc.memoryai-claude- HTTP hooks for Claude Code CLI
License
MIT
