UNPKG

@codai/memorai-core

Version:

Core memory engine with vector operations for AI agents

110 lines 3.44 kB
import { z } from 'zod'; const DEFAULT_CONFIG = { vector_db: { url: process.env.QDRANT_URL ?? 'http://localhost:6333', api_key: process.env.QDRANT_API_KEY, collection: 'memories', dimension: 1536, }, redis: { url: process.env.REDIS_URL ?? 'redis://localhost:6379', password: process.env.REDIS_PASSWORD, db: 0, }, embedding: { provider: 'openai', model: 'text-embedding-3-small', api_key: process.env.OPENAI_API_KEY, endpoint: process.env.OPENAI_ENDPOINT, }, performance: { max_query_time_ms: 100, cache_ttl_seconds: 300, batch_size: 100, }, security: { encryption_key: process.env.MEMORY_ENCRYPTION_KEY ?? '', tenant_isolation: true, audit_logs: true, }, }; export class MemoryConfigManager { config; constructor(overrides = {}) { this.config = this.mergeConfig(DEFAULT_CONFIG, overrides); this.validate(); } get() { return { ...this.config }; } getVectorDB() { return { ...this.config.vector_db }; } getRedis() { return { ...this.config.redis }; } getEmbedding() { return { ...this.config.embedding }; } getPerformance() { return { ...this.config.performance }; } getSecurity() { return { ...this.config.security }; } mergeConfig(defaultConfig, overrides) { return { vector_db: { ...defaultConfig.vector_db, ...overrides.vector_db }, redis: { ...defaultConfig.redis, ...overrides.redis }, embedding: { ...defaultConfig.embedding, ...overrides.embedding }, performance: { ...defaultConfig.performance, ...overrides.performance }, security: { ...defaultConfig.security, ...overrides.security }, }; } validate() { try { // Import the schema from types and validate import('../types/index.js').then(({ MemoryConfigSchema }) => { MemoryConfigSchema.parse(this.config); }).catch((error) => { if (error instanceof z.ZodError) { throw new Error(`Invalid memory configuration: ${error.message}`); } throw error; }); } catch (error) { if (error instanceof z.ZodError) { throw new Error(`Invalid memory configuration: ${error.message}`); } throw error; } // Additional validation if (!this.config.security.encryption_key) { throw new Error('MEMORY_ENCRYPTION_KEY environment variable is required'); } if (this.config.security.encryption_key.length < 32) { throw new Error('Encryption key must be at least 32 characters long'); } } } /** * Static factory class for MemoryConfig - provides convenience methods */ export class MemoryConfigFactory { /** * Create config from environment variables */ static fromEnv() { return new MemoryConfigManager(); } /** * Create config with custom overrides */ static create(overrides = {}) { return new MemoryConfigManager(overrides); } } // Alias for backward compatibility with tests export const MemoryConfig = MemoryConfigFactory; //# sourceMappingURL=MemoryConfig.js.map