claude-flow 2.7.23 → 2.7.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/memory/swarm-memory.ts"],"sourcesContent":["import { EventEmitter } from 'node:events';\nimport { Logger } from '../core/logger.js';\nimport { MemoryManager } from './manager.js';\nimport { EventBus } from '../core/event-bus.js';\nimport { generateId } from '../utils/helpers.js';\nimport * as fs from 'node:fs/promises';\nimport * as path from 'node:path';\n\nexport interface SwarmMemoryEntry {\n id: string;\n agentId: string;\n type: 'knowledge' | 'result' | 'state' | 'communication' | 'error';\n content: any;\n timestamp: Date;\n metadata: {\n taskId?: string;\n objectiveId?: string;\n tags?: string[];\n priority?: number;\n shareLevel?: 'private' | 'team' | 'public';\n };\n}\n\nexport interface SwarmMemoryQuery {\n agentId?: string;\n type?: SwarmMemoryEntry['type'];\n taskId?: string;\n objectiveId?: string;\n tags?: string[];\n since?: Date;\n before?: Date;\n limit?: number;\n shareLevel?: SwarmMemoryEntry['metadata']['shareLevel'];\n}\n\nexport interface SwarmKnowledgeBase {\n id: string;\n name: string;\n description: string;\n entries: SwarmMemoryEntry[];\n metadata: {\n domain: string;\n expertise: string[];\n contributors: string[];\n lastUpdated: Date;\n };\n}\n\nexport interface SwarmMemoryConfig {\n namespace: string;\n enableDistribution: boolean;\n enableReplication: boolean;\n syncInterval: number;\n maxEntries: number;\n compressionThreshold: number;\n enableKnowledgeBase: boolean;\n enableCrossAgentSharing: boolean;\n persistencePath: string;\n}\n\nexport class SwarmMemoryManager extends EventEmitter {\n private logger: Logger;\n private config: SwarmMemoryConfig;\n private baseMemory: MemoryManager;\n private entries: Map<string, SwarmMemoryEntry>;\n private knowledgeBases: Map<string, SwarmKnowledgeBase>;\n private agentMemories: Map<string, Set<string>>; // agentId -> set of entry IDs\n private syncTimer?: NodeJS.Timeout;\n private isInitialized: boolean = false;\n\n constructor(config: Partial<SwarmMemoryConfig> = {}) {\n super();\n this.logger = new Logger('SwarmMemoryManager');\n this.config = {\n namespace: 'swarm',\n enableDistribution: true,\n enableReplication: true,\n syncInterval: 10000, // 10 seconds\n maxEntries: 10000,\n compressionThreshold: 1000,\n enableKnowledgeBase: true,\n enableCrossAgentSharing: true,\n persistencePath: './swarm-memory',\n ...config,\n };\n\n this.entries = new Map();\n this.knowledgeBases = new Map();\n this.agentMemories = new Map();\n\n const eventBus = EventBus.getInstance();\n this.baseMemory = new MemoryManager(\n {\n backend: 'sqlite',\n namespace: this.config.namespace,\n cacheSizeMB: 50,\n syncOnExit: true,\n maxEntries: this.config.maxEntries,\n ttlMinutes: 60,\n },\n eventBus,\n this.logger,\n );\n }\n\n async initialize(): Promise<void> {\n if (this.isInitialized) return;\n\n this.logger.info('Initializing swarm memory manager...');\n\n // Initialize base memory\n await this.baseMemory.initialize();\n\n // Create persistence directory\n await fs.mkdir(this.config.persistencePath, { recursive: true });\n\n // Load existing memory\n await this.loadMemoryState();\n\n // Start sync timer\n if (this.config.syncInterval > 0) {\n this.syncTimer = setInterval(() => {\n this.syncMemoryState();\n }, this.config.syncInterval);\n }\n\n this.isInitialized = true;\n this.emit('memory:initialized');\n }\n\n async shutdown(): Promise<void> {\n if (!this.isInitialized) return;\n\n this.logger.info('Shutting down swarm memory manager...');\n\n // Stop sync timer\n if (this.syncTimer) {\n clearInterval(this.syncTimer);\n this.syncTimer = undefined;\n }\n\n // Save final state\n await this.saveMemoryState();\n\n this.isInitialized = false;\n this.emit('memory:shutdown');\n }\n\n async remember(\n agentId: string,\n type: SwarmMemoryEntry['type'],\n content: any,\n metadata: Partial<SwarmMemoryEntry['metadata']> = {},\n ): Promise<string> {\n const entryId = generateId('mem');\n const entry: SwarmMemoryEntry = {\n id: entryId,\n agentId,\n type,\n content,\n timestamp: new Date(),\n metadata: {\n shareLevel: 'team',\n priority: 1,\n ...metadata,\n },\n };\n\n this.entries.set(entryId, entry);\n\n // Associate with agent\n if (!this.agentMemories.has(agentId)) {\n this.agentMemories.set(agentId, new Set());\n }\n this.agentMemories.get(agentId)!.add(entryId);\n\n // Store in base memory for persistence\n await this.baseMemory.remember({\n namespace: this.config.namespace,\n key: `entry:${entryId}`,\n content: JSON.stringify(entry),\n metadata: {\n type: 'swarm-memory',\n agentId,\n entryType: type,\n shareLevel: entry.metadata.shareLevel,\n },\n });\n\n this.logger.debug(`Agent ${agentId} remembered: ${type} - ${entryId}`);\n this.emit('memory:added', entry);\n\n // Update knowledge base if applicable\n if (type === 'knowledge' && this.config.enableKnowledgeBase) {\n await this.updateKnowledgeBase(entry);\n }\n\n // Check for memory limits\n await this.enforceMemoryLimits();\n\n return entryId;\n }\n\n async recall(query: SwarmMemoryQuery): Promise<SwarmMemoryEntry[]> {\n let results = Array.from(this.entries.values());\n\n // Apply filters\n if (query.agentId) {\n results = results.filter((e) => e.agentId === query.agentId);\n }\n\n if (query.type) {\n results = results.filter((e) => e.type === query.type);\n }\n\n if (query.taskId) {\n results = results.filter((e) => e.metadata.taskId === query.taskId);\n }\n\n if (query.objectiveId) {\n results = results.filter((e) => e.metadata.objectiveId === query.objectiveId);\n }\n\n if (query.tags && query.tags.length > 0) {\n results = results.filter(\n (e) => e.metadata.tags && query.tags!.some((tag) => e.metadata.tags!.includes(tag)),\n );\n }\n\n if (query.since) {\n results = results.filter((e) => e.timestamp >= query.since!);\n }\n\n if (query.before) {\n results = results.filter((e) => e.timestamp <= query.before!);\n }\n\n if (query.shareLevel) {\n results = results.filter((e) => e.metadata.shareLevel === query.shareLevel);\n }\n\n // Sort by timestamp (newest first)\n results.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime());\n\n // Apply limit\n if (query.limit) {\n results = results.slice(0, query.limit);\n }\n\n this.logger.debug(`Recalled ${results.length} memories for query`);\n return results;\n }\n\n async shareMemory(entryId: string, targetAgentId: string): Promise<void> {\n const entry = this.entries.get(entryId);\n if (!entry) {\n throw new Error('Memory entry not found');\n }\n\n if (!this.config.enableCrossAgentSharing) {\n throw new Error('Cross-agent sharing is disabled');\n }\n\n // Check share level permissions\n if (entry.metadata.shareLevel === 'private') {\n throw new Error('Memory entry is private and cannot be shared');\n }\n\n // Create a shared copy for the target agent\n const sharedEntry: SwarmMemoryEntry = {\n ...entry,\n id: generateId('mem'),\n metadata: {\n ...entry.metadata,\n originalId: entryId,\n sharedFrom: entry.agentId,\n sharedTo: targetAgentId,\n sharedAt: new Date(),\n },\n };\n\n this.entries.set(sharedEntry.id, sharedEntry);\n\n // Associate with target agent\n if (!this.agentMemories.has(targetAgentId)) {\n this.agentMemories.set(targetAgentId, new Set());\n }\n this.agentMemories.get(targetAgentId)!.add(sharedEntry.id);\n\n this.logger.info(`Shared memory ${entryId} from ${entry.agentId} to ${targetAgentId}`);\n this.emit('memory:shared', { original: entry, shared: sharedEntry });\n }\n\n async broadcastMemory(entryId: string, agentIds?: string[]): Promise<void> {\n const entry = this.entries.get(entryId);\n if (!entry) {\n throw new Error('Memory entry not found');\n }\n\n if (entry.metadata.shareLevel === 'private') {\n throw new Error('Cannot broadcast private memory');\n }\n\n const targets =\n agentIds || Array.from(this.agentMemories.keys()).filter((id) => id !== entry.agentId);\n\n for (const targetId of targets) {\n try {\n await this.shareMemory(entryId, targetId);\n } catch (error) {\n this.logger.warn(`Failed to share memory to ${targetId}:`, error);\n }\n }\n\n this.logger.info(`Broadcasted memory ${entryId} to ${targets.length} agents`);\n }\n\n async createKnowledgeBase(\n name: string,\n description: string,\n domain: string,\n expertise: string[],\n ): Promise<string> {\n const kbId = generateId('kb');\n const knowledgeBase: SwarmKnowledgeBase = {\n id: kbId,\n name,\n description,\n entries: [],\n metadata: {\n domain,\n expertise,\n contributors: [],\n lastUpdated: new Date(),\n },\n };\n\n this.knowledgeBases.set(kbId, knowledgeBase);\n\n this.logger.info(`Created knowledge base: ${name} (${kbId})`);\n this.emit('knowledgebase:created', knowledgeBase);\n\n return kbId;\n }\n\n async updateKnowledgeBase(entry: SwarmMemoryEntry): Promise<void> {\n if (!this.config.enableKnowledgeBase) return;\n\n // Find relevant knowledge bases\n const relevantKBs = Array.from(this.knowledgeBases.values()).filter((kb) => {\n // Simple matching based on tags and content\n const tags = entry.metadata.tags || [];\n return tags.some((tag) =>\n kb.metadata.expertise.some(\n (exp) =>\n exp.toLowerCase().includes(tag.toLowerCase()) ||\n tag.toLowerCase().includes(exp.toLowerCase()),\n ),\n );\n });\n\n for (const kb of relevantKBs) {\n // Add entry to knowledge base\n kb.entries.push(entry);\n kb.metadata.lastUpdated = new Date();\n\n // Add contributor\n if (!kb.metadata.contributors.includes(entry.agentId)) {\n kb.metadata.contributors.push(entry.agentId);\n }\n\n this.logger.debug(`Updated knowledge base ${kb.id} with entry ${entry.id}`);\n }\n }\n\n async searchKnowledge(\n query: string,\n domain?: string,\n expertise?: string[],\n ): Promise<SwarmMemoryEntry[]> {\n const allEntries: SwarmMemoryEntry[] = [];\n\n // Search in knowledge bases\n for (const kb of this.knowledgeBases.values()) {\n if (domain && kb.metadata.domain !== domain) continue;\n\n if (expertise && !expertise.some((exp) => kb.metadata.expertise.includes(exp))) {\n continue;\n }\n\n allEntries.push(...kb.entries);\n }\n\n // Simple text search (in real implementation, use better search)\n const queryLower = query.toLowerCase();\n const results = allEntries.filter((entry) => {\n const contentStr = JSON.stringify(entry.content).toLowerCase();\n return contentStr.includes(queryLower);\n });\n\n return results.slice(0, 50); // Limit results\n }\n\n async getAgentMemorySnapshot(agentId: string): Promise<{\n totalEntries: number;\n recentEntries: SwarmMemoryEntry[];\n knowledgeContributions: number;\n sharedEntries: number;\n }> {\n const agentEntryIds = this.agentMemories.get(agentId) || new Set();\n const agentEntries = Array.from(agentEntryIds)\n .map((id) => this.entries.get(id))\n .filter(Boolean) as SwarmMemoryEntry[];\n\n const recentEntries = agentEntries\n .sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime())\n .slice(0, 10);\n\n const knowledgeContributions = agentEntries.filter((e) => e.type === 'knowledge').length;\n\n const sharedEntries = agentEntries.filter(\n (e) => e.metadata.shareLevel === 'public' || e.metadata.shareLevel === 'team',\n ).length;\n\n return {\n totalEntries: agentEntries.length,\n recentEntries,\n knowledgeContributions,\n sharedEntries,\n };\n }\n\n private async loadMemoryState(): Promise<void> {\n try {\n // Load entries\n const entriesFile = path.join(this.config.persistencePath, 'entries.json');\n try {\n const entriesData = await fs.readFile(entriesFile, 'utf-8');\n const entriesArray = JSON.parse(entriesData);\n\n for (const entry of entriesArray) {\n this.entries.set(entry.id, {\n ...entry,\n timestamp: new Date(entry.timestamp),\n });\n\n // Rebuild agent memory associations\n if (!this.agentMemories.has(entry.agentId)) {\n this.agentMemories.set(entry.agentId, new Set());\n }\n this.agentMemories.get(entry.agentId)!.add(entry.id);\n }\n\n this.logger.info(`Loaded ${entriesArray.length} memory entries`);\n } catch (error) {\n this.logger.warn('No existing memory entries found');\n }\n\n // Load knowledge bases\n const kbFile = path.join(this.config.persistencePath, 'knowledge-bases.json');\n try {\n const kbData = await fs.readFile(kbFile, 'utf-8');\n const kbArray = JSON.parse(kbData);\n\n for (const kb of kbArray) {\n this.knowledgeBases.set(kb.id, {\n ...kb,\n metadata: {\n ...kb.metadata,\n lastUpdated: new Date(kb.metadata.lastUpdated),\n },\n entries: kb.entries.map((e: any) => ({\n ...e,\n timestamp: new Date(e.timestamp),\n })),\n });\n }\n\n this.logger.info(`Loaded ${kbArray.length} knowledge bases`);\n } catch (error) {\n this.logger.warn('No existing knowledge bases found');\n }\n } catch (error) {\n this.logger.error('Error loading memory state:', error);\n }\n }\n\n private async saveMemoryState(): Promise<void> {\n try {\n // Save entries\n const entriesArray = Array.from(this.entries.values());\n const entriesFile = path.join(this.config.persistencePath, 'entries.json');\n await fs.writeFile(entriesFile, JSON.stringify(entriesArray, null, 2));\n\n // Save knowledge bases\n const kbArray = Array.from(this.knowledgeBases.values());\n const kbFile = path.join(this.config.persistencePath, 'knowledge-bases.json');\n await fs.writeFile(kbFile, JSON.stringify(kbArray, null, 2));\n\n this.logger.debug('Saved memory state to disk');\n } catch (error) {\n this.logger.error('Error saving memory state:', error);\n }\n }\n\n private async syncMemoryState(): Promise<void> {\n try {\n await this.saveMemoryState();\n this.emit('memory:synced');\n } catch (error) {\n this.logger.error('Error syncing memory state:', error);\n }\n }\n\n private async enforceMemoryLimits(): Promise<void> {\n if (this.entries.size <= this.config.maxEntries) return;\n\n this.logger.info('Enforcing memory limits...');\n\n // Remove oldest entries that are not marked as important\n const entries = Array.from(this.entries.values())\n .filter((e) => (e.metadata.priority || 1) <= 1) // Only remove low priority\n .sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime());\n\n const toRemove = entries.slice(0, this.entries.size - this.config.maxEntries);\n\n for (const entry of toRemove) {\n this.entries.delete(entry.id);\n\n // Remove from agent memory\n const agentEntries = this.agentMemories.get(entry.agentId);\n if (agentEntries) {\n agentEntries.delete(entry.id);\n }\n\n this.logger.debug(`Removed old memory entry: ${entry.id}`);\n }\n\n this.emit('memory:cleaned', toRemove.length);\n }\n\n // Public API methods\n getMemoryStats(): {\n totalEntries: number;\n entriesByType: Record<string, number>;\n entriesByAgent: Record<string, number>;\n knowledgeBases: number;\n memoryUsage: number;\n } {\n const entries = Array.from(this.entries.values());\n const entriesByType: Record<string, number> = {};\n const entriesByAgent: Record<string, number> = {};\n\n for (const entry of entries) {\n entriesByType[entry.type] = (entriesByType[entry.type] || 0) + 1;\n entriesByAgent[entry.agentId] = (entriesByAgent[entry.agentId] || 0) + 1;\n }\n\n // Rough memory usage calculation\n const memoryUsage = JSON.stringify(entries).length;\n\n return {\n totalEntries: entries.length,\n entriesByType,\n entriesByAgent,\n knowledgeBases: this.knowledgeBases.size,\n memoryUsage,\n };\n }\n\n async exportMemory(agentId?: string): Promise<any> {\n const entries = agentId ? await this.recall({ agentId }) : Array.from(this.entries.values());\n\n return {\n entries,\n knowledgeBases: agentId\n ? Array.from(this.knowledgeBases.values()).filter((kb) =>\n kb.metadata.contributors.includes(agentId),\n )\n : Array.from(this.knowledgeBases.values()),\n exportedAt: new Date(),\n stats: this.getMemoryStats(),\n };\n }\n\n async clearMemory(agentId?: string): Promise<void> {\n if (agentId) {\n // Clear specific agent's memory\n const entryIds = this.agentMemories.get(agentId) || new Set();\n for (const entryId of entryIds) {\n this.entries.delete(entryId);\n }\n this.agentMemories.delete(agentId);\n this.logger.info(`Cleared memory for agent ${agentId}`);\n } else {\n // Clear all memory\n this.entries.clear();\n this.agentMemories.clear();\n this.knowledgeBases.clear();\n this.logger.info('Cleared all swarm memory');\n }\n\n this.emit('memory:cleared', { agentId });\n }\n\n // Compatibility methods for hive.ts\n async store(key: string, value: any): Promise<void> {\n // Extract namespace and actual key from the path\n const parts = key.split('/');\n const type = (parts[0] as SwarmMemoryEntry['type']) || 'state';\n const agentId = parts[1] || 'system';\n\n await this.remember(agentId, type, value, {\n tags: [parts[0], parts[1]].filter(Boolean),\n shareLevel: 'team',\n });\n }\n\n async search(pattern: string, limit: number = 10): Promise<any[]> {\n // Simple pattern matching on stored keys/content\n const results: any[] = [];\n\n for (const entry of this.entries.values()) {\n const entryString = JSON.stringify(entry);\n if (entryString.includes(pattern.replace('*', ''))) {\n results.push(entry.content);\n if (results.length >= limit) break;\n }\n }\n\n return results;\n }\n}\n"],"names":["EventEmitter","Logger","MemoryManager","EventBus","generateId","fs","path","SwarmMemoryManager","logger","config","baseMemory","entries","knowledgeBases","agentMemories","syncTimer","isInitialized","namespace","enableDistribution","enableReplication","syncInterval","maxEntries","compressionThreshold","enableKnowledgeBase","enableCrossAgentSharing","persistencePath","Map","eventBus","getInstance","backend","cacheSizeMB","syncOnExit","ttlMinutes","initialize","info","mkdir","recursive","loadMemoryState","setInterval","syncMemoryState","emit","shutdown","clearInterval","undefined","saveMemoryState","remember","agentId","type","content","metadata","entryId","entry","id","timestamp","Date","shareLevel","priority","set","has","Set","get","add","key","JSON","stringify","entryType","debug","updateKnowledgeBase","enforceMemoryLimits","recall","query","results","Array","from","values","filter","e","taskId","objectiveId","tags","length","some","tag","includes","since","before","sort","a","b","getTime","limit","slice","shareMemory","targetAgentId","Error","sharedEntry","originalId","sharedFrom","sharedTo","sharedAt","original","shared","broadcastMemory","agentIds","targets","keys","targetId","error","warn","createKnowledgeBase","name","description","domain","expertise","kbId","knowledgeBase","contributors","lastUpdated","relevantKBs","kb","exp","toLowerCase","push","searchKnowledge","allEntries","queryLower","contentStr","getAgentMemorySnapshot","agentEntryIds","agentEntries","map","Boolean","recentEntries","knowledgeContributions","sharedEntries","totalEntries","entriesFile","join","entriesData","readFile","entriesArray","parse","kbFile","kbData","kbArray","writeFile","size","toRemove","delete","getMemoryStats","entriesByType","entriesByAgent","memoryUsage","exportMemory","exportedAt","stats","clearMemory","entryIds","clear","store","value","parts","split","search","pattern","entryString","replace"],"mappings":"AAAA,SAASA,YAAY,QAAQ,cAAc;AAC3C,SAASC,MAAM,QAAQ,oBAAoB;AAC3C,SAASC,aAAa,QAAQ,eAAe;AAC7C,SAASC,QAAQ,QAAQ,uBAAuB;AAChD,SAASC,UAAU,QAAQ,sBAAsB;AACjD,YAAYC,QAAQ,mBAAmB;AACvC,YAAYC,UAAU,YAAY;AAsDlC,OAAO,MAAMC,2BAA2BP;IAC9BQ,OAAe;IACfC,OAA0B;IAC1BC,WAA0B;IAC1BC,QAAuC;IACvCC,eAAgD;IAChDC,cAAwC;IACxCC,UAA2B;IAC3BC,gBAAyB,MAAM;IAEvC,YAAYN,SAAqC,CAAC,CAAC,CAAE;QACnD,KAAK;QACL,IAAI,CAACD,MAAM,GAAG,IAAIP,OAAO;QACzB,IAAI,CAACQ,MAAM,GAAG;YACZO,WAAW;YACXC,oBAAoB;YACpBC,mBAAmB;YACnBC,cAAc;YACdC,YAAY;YACZC,sBAAsB;YACtBC,qBAAqB;YACrBC,yBAAyB;YACzBC,iBAAiB;YACjB,GAAGf,MAAM;QACX;QAEA,IAAI,CAACE,OAAO,GAAG,IAAIc;QACnB,IAAI,CAACb,cAAc,GAAG,IAAIa;QAC1B,IAAI,CAACZ,aAAa,GAAG,IAAIY;QAEzB,MAAMC,WAAW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nG,IAAIoG,KAAK,CAAC;QACxB,MAAMnH,OAAO,AAACkH,KAAK,CAAC,EAAE,IAAiC;QACvD,MAAMnH,UAAUmH,KAAK,CAAC,EAAE,IAAI;QAE5B,MAAM,IAAI,CAACpH,QAAQ,CAACC,SAASC,MAAMiH,OAAO;YACxCjF,MAAM;gBAACkF,KAAK,CAAC,EAAE;gBAAEA,KAAK,CAAC,EAAE;aAAC,CAACtF,MAAM,CAACwD;YAClC5E,YAAY;QACd;IACF;IAEA,MAAM4G,OAAOC,OAAe,EAAE1E,QAAgB,EAAE,EAAkB;QAEhE,MAAMnB,UAAiB,EAAE;QAEzB,KAAK,MAAMpB,SAAS,IAAI,CAACvC,OAAO,CAAC8D,MAAM,GAAI;YACzC,MAAM2F,cAActG,KAAKC,SAAS,CAACb;YACnC,IAAIkH,YAAYlF,QAAQ,CAACiF,QAAQE,OAAO,CAAC,KAAK,MAAM;gBAClD/F,QAAQmD,IAAI,CAACvE,MAAMH,OAAO;gBAC1B,IAAIuB,QAAQS,MAAM,IAAIU,OAAO;YAC/B;QACF;QAEA,OAAOnB;IACT;AACF"}
1
+ {"version":3,"sources":["../../../src/memory/swarm-memory.js"],"sourcesContent":["/**\n * SwarmMemory - MCP-specific memory persistence extending SharedMemory\n * Provides swarm-specific features like agent coordination, task tracking, and neural patterns\n *\n * @module swarm-memory\n */\n\nimport { SharedMemory } from './shared-memory.js';\nimport path from 'path';\n\n/**\n * Swarm-specific namespaces\n */\nconst SWARM_NAMESPACES = {\n AGENTS: 'swarm:agents',\n TASKS: 'swarm:tasks',\n COMMUNICATIONS: 'swarm:communications',\n CONSENSUS: 'swarm:consensus',\n PATTERNS: 'swarm:patterns',\n METRICS: 'swarm:metrics',\n COORDINATION: 'swarm:coordination',\n};\n\n/**\n * SwarmMemory class - Extends SharedMemory with MCP features\n */\nexport class SwarmMemory extends SharedMemory {\n constructor(options = {}) {\n // Default to .swarm directory for MCP\n super({\n directory: options.directory || '.swarm',\n filename: options.filename || 'swarm-memory.db',\n ...options,\n });\n\n this.swarmId = options.swarmId || 'default';\n this.mcpMode = options.mcpMode !== false;\n\n // Additional swarm-specific caches\n this.agentCache = new Map();\n this.taskCache = new Map();\n this.patternCache = new Map();\n }\n\n /**\n * Initialize with swarm-specific setup\n */\n async initialize() {\n await super.initialize();\n\n // Initialize swarm-specific namespaces\n await this._initializeSwarmNamespaces();\n\n // Load active agents and tasks into cache\n await this._loadSwarmState();\n\n this.emit('swarm:initialized', { swarmId: this.swarmId });\n }\n\n /**\n * Store agent information\n */\n async storeAgent(agentId, agentData) {\n const key = `agent:${agentId}`;\n const enrichedData = {\n ...agentData,\n swarmId: this.swarmId,\n lastUpdated: new Date().toISOString(),\n };\n\n await this.store(key, enrichedData, {\n namespace: SWARM_NAMESPACES.AGENTS,\n tags: ['agent', agentData.type, agentData.status],\n metadata: {\n swarmId: this.swarmId,\n agentType: agentData.type,\n },\n });\n\n // Update agent cache\n this.agentCache.set(agentId, enrichedData);\n\n this.emit('swarm:agentStored', { agentId, type: agentData.type });\n\n return { agentId, stored: true };\n }\n\n /**\n * Retrieve agent information\n */\n async getAgent(agentId) {\n // Check cache first\n if (this.agentCache.has(agentId)) {\n return this.agentCache.get(agentId);\n }\n\n const key = `agent:${agentId}`;\n const agent = await this.retrieve(key, SWARM_NAMESPACES.AGENTS);\n\n if (agent) {\n this.agentCache.set(agentId, agent);\n }\n\n return agent;\n }\n\n /**\n * List all agents in swarm\n */\n async listAgents(filter = {}) {\n const agents = await this.list(SWARM_NAMESPACES.AGENTS, {\n limit: filter.limit || 100,\n });\n\n return agents\n .map((entry) => entry.value)\n .filter((agent) => {\n if (filter.type && agent.type !== filter.type) return false;\n if (filter.status && agent.status !== filter.status) return false;\n if (filter.swarmId && agent.swarmId !== filter.swarmId) return false;\n return true;\n });\n }\n\n /**\n * Store task information\n */\n async storeTask(taskId, taskData) {\n const key = `task:${taskId}`;\n const enrichedData = {\n ...taskData,\n swarmId: this.swarmId,\n createdAt: taskData.createdAt || new Date().toISOString(),\n updatedAt: new Date().toISOString(),\n };\n\n await this.store(key, enrichedData, {\n namespace: SWARM_NAMESPACES.TASKS,\n tags: ['task', taskData.status, taskData.priority],\n metadata: {\n swarmId: this.swarmId,\n assignedAgents: taskData.assignedAgents || [],\n },\n });\n\n // Update task cache\n this.taskCache.set(taskId, enrichedData);\n\n this.emit('swarm:taskStored', { taskId, status: taskData.status });\n\n return { taskId, stored: true };\n }\n\n /**\n * Update task status\n */\n async updateTaskStatus(taskId, status, result = null) {\n const task = await this.getTask(taskId);\n if (!task) {\n throw new Error(`Task ${taskId} not found`);\n }\n\n task.status = status;\n task.updatedAt = new Date().toISOString();\n\n if (result) {\n task.result = result;\n }\n\n if (status === 'completed') {\n task.completedAt = new Date().toISOString();\n }\n\n await this.storeTask(taskId, task);\n\n this.emit('swarm:taskStatusUpdated', { taskId, status });\n\n return { taskId, status, updated: true };\n }\n\n /**\n * Get task information\n */\n async getTask(taskId) {\n // Check cache first\n if (this.taskCache.has(taskId)) {\n return this.taskCache.get(taskId);\n }\n\n const key = `task:${taskId}`;\n const task = await this.retrieve(key, SWARM_NAMESPACES.TASKS);\n\n if (task) {\n this.taskCache.set(taskId, task);\n }\n\n return task;\n }\n\n /**\n * Store inter-agent communication\n */\n async storeCommunication(fromAgent, toAgent, message) {\n const commId = `comm:${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;\n const communication = {\n id: commId,\n fromAgent,\n toAgent,\n message,\n swarmId: this.swarmId,\n timestamp: new Date().toISOString(),\n };\n\n await this.store(commId, communication, {\n namespace: SWARM_NAMESPACES.COMMUNICATIONS,\n ttl: 86400, // 24 hours\n tags: ['communication', message.type],\n metadata: {\n fromAgent,\n toAgent,\n messageType: message.type,\n },\n });\n\n this.emit('swarm:communication', { fromAgent, toAgent, type: message.type });\n\n return { id: commId, stored: true };\n }\n\n /**\n * Store consensus decision\n */\n async storeConsensus(consensusId, decision) {\n const key = `consensus:${consensusId}`;\n const consensusData = {\n ...decision,\n swarmId: this.swarmId,\n timestamp: new Date().toISOString(),\n };\n\n await this.store(key, consensusData, {\n namespace: SWARM_NAMESPACES.CONSENSUS,\n tags: ['consensus', decision.status],\n metadata: {\n swarmId: this.swarmId,\n taskId: decision.taskId,\n threshold: decision.threshold,\n },\n });\n\n this.emit('swarm:consensus', { consensusId, status: decision.status });\n\n return { consensusId, stored: true };\n }\n\n /**\n * Store neural pattern\n */\n async storePattern(patternId, pattern) {\n const key = `pattern:${patternId}`;\n const patternData = {\n ...pattern,\n swarmId: this.swarmId,\n createdAt: new Date().toISOString(),\n usageCount: 0,\n successRate: 0,\n };\n\n await this.store(key, patternData, {\n namespace: SWARM_NAMESPACES.PATTERNS,\n tags: ['pattern', pattern.type],\n metadata: {\n swarmId: this.swarmId,\n patternType: pattern.type,\n confidence: pattern.confidence || 0,\n },\n });\n\n // Cache frequently used patterns\n if (pattern.type === 'coordination' || pattern.type === 'optimization') {\n this.patternCache.set(patternId, patternData);\n }\n\n this.emit('swarm:patternStored', { patternId, type: pattern.type });\n\n return { patternId, stored: true };\n }\n\n /**\n * Update pattern usage and success metrics\n */\n async updatePatternMetrics(patternId, success = true) {\n const pattern = await this.getPattern(patternId);\n if (!pattern) {\n throw new Error(`Pattern ${patternId} not found`);\n }\n\n pattern.usageCount++;\n pattern.lastUsedAt = new Date().toISOString();\n\n // Update success rate with exponential moving average\n const alpha = 0.1; // Smoothing factor\n const currentSuccess = success ? 1 : 0;\n pattern.successRate = alpha * currentSuccess + (1 - alpha) * (pattern.successRate || 0);\n\n await this.storePattern(patternId, pattern);\n\n return { patternId, usageCount: pattern.usageCount, successRate: pattern.successRate };\n }\n\n /**\n * Get pattern\n */\n async getPattern(patternId) {\n // Check cache first\n if (this.patternCache.has(patternId)) {\n return this.patternCache.get(patternId);\n }\n\n const key = `pattern:${patternId}`;\n return await this.retrieve(key, SWARM_NAMESPACES.PATTERNS);\n }\n\n /**\n * Find best patterns for a given context\n */\n async findBestPatterns(context, limit = 5) {\n const patterns = await this.search({\n namespace: SWARM_NAMESPACES.PATTERNS,\n tags: context.tags,\n limit: 100,\n });\n\n // Score patterns based on success rate and relevance\n const scored = patterns.map((entry) => {\n const pattern = entry.value;\n const score =\n pattern.successRate * 0.7 +\n (pattern.confidence || 0) * 0.2 +\n (pattern.usageCount > 0 ? 0.1 : 0);\n\n return { ...pattern, score };\n });\n\n // Sort by score and return top patterns\n return scored.sort((a, b) => b.score - a.score).slice(0, limit);\n }\n\n /**\n * Store coordination state\n */\n async storeCoordination(key, state) {\n await this.store(key, state, {\n namespace: SWARM_NAMESPACES.COORDINATION,\n ttl: 3600, // 1 hour\n metadata: {\n swarmId: this.swarmId,\n timestamp: new Date().toISOString(),\n },\n });\n\n return { key, stored: true };\n }\n\n /**\n * Get coordination state\n */\n async getCoordination(key) {\n return await this.retrieve(key, SWARM_NAMESPACES.COORDINATION);\n }\n\n /**\n * Store performance metrics\n */\n async storeMetrics(metricsId, metrics) {\n const key = `metrics:${metricsId}`;\n await this.store(key, metrics, {\n namespace: SWARM_NAMESPACES.METRICS,\n ttl: 86400 * 7, // 7 days\n tags: ['metrics', metrics.type],\n metadata: {\n swarmId: this.swarmId,\n agentId: metrics.agentId,\n timestamp: new Date().toISOString(),\n },\n });\n\n this.emit('swarm:metricsStored', { metricsId, type: metrics.type });\n\n return { metricsId, stored: true };\n }\n\n /**\n * Get swarm statistics\n */\n async getSwarmStats() {\n const baseStats = await this.getStats();\n\n // Add swarm-specific stats\n const agentCount = await this._countNamespace(SWARM_NAMESPACES.AGENTS);\n const taskCount = await this._countNamespace(SWARM_NAMESPACES.TASKS);\n const patternCount = await this._countNamespace(SWARM_NAMESPACES.PATTERNS);\n\n // Get active agents\n const activeAgents = Array.from(this.agentCache.values()).filter(\n (agent) => agent.status === 'active' || agent.status === 'busy',\n ).length;\n\n // Get task statistics\n const tasks = Array.from(this.taskCache.values());\n const taskStats = {\n total: tasks.length,\n pending: tasks.filter((t) => t.status === 'pending').length,\n inProgress: tasks.filter((t) => t.status === 'in_progress').length,\n completed: tasks.filter((t) => t.status === 'completed').length,\n failed: tasks.filter((t) => t.status === 'failed').length,\n };\n\n return {\n ...baseStats,\n swarm: {\n swarmId: this.swarmId,\n agents: {\n total: agentCount,\n active: activeAgents,\n cached: this.agentCache.size,\n },\n tasks: taskStats,\n patterns: {\n total: patternCount,\n cached: this.patternCache.size,\n },\n namespaces: Object.values(SWARM_NAMESPACES),\n },\n };\n }\n\n /**\n * Clean up old swarm data\n */\n async cleanupSwarmData(options = {}) {\n const {\n maxAge = 86400 * 7, // 7 days\n keepPatterns = true,\n keepConsensus = true,\n } = options;\n\n const cutoffTime = Date.now() - maxAge * 1000;\n let cleaned = 0;\n\n // Clean old communications\n const comms = await this.list(SWARM_NAMESPACES.COMMUNICATIONS);\n for (const comm of comms) {\n if (new Date(comm.value.timestamp).getTime() < cutoffTime) {\n await this.delete(comm.key, SWARM_NAMESPACES.COMMUNICATIONS);\n cleaned++;\n }\n }\n\n // Clean completed tasks\n const tasks = await this.list(SWARM_NAMESPACES.TASKS);\n for (const task of tasks) {\n if (\n task.value.status === 'completed' &&\n new Date(task.value.completedAt).getTime() < cutoffTime\n ) {\n await this.delete(task.key, SWARM_NAMESPACES.TASKS);\n this.taskCache.delete(task.value.id);\n cleaned++;\n }\n }\n\n // Clean old metrics\n const metrics = await this.list(SWARM_NAMESPACES.METRICS);\n for (const metric of metrics) {\n if (new Date(metric.createdAt).getTime() < cutoffTime) {\n await this.delete(metric.key, SWARM_NAMESPACES.METRICS);\n cleaned++;\n }\n }\n\n this.emit('swarm:cleanup', { cleaned, maxAge });\n\n return { cleaned };\n }\n\n /**\n * Export swarm state\n */\n async exportSwarmState() {\n const agents = await this.listAgents();\n const tasks = Array.from(this.taskCache.values());\n const patterns = await this.list(SWARM_NAMESPACES.PATTERNS);\n\n return {\n swarmId: this.swarmId,\n exportedAt: new Date().toISOString(),\n agents: agents,\n tasks: tasks,\n patterns: patterns.map((p) => p.value),\n statistics: await this.getSwarmStats(),\n };\n }\n\n /**\n * Import swarm state\n */\n async importSwarmState(state) {\n let imported = {\n agents: 0,\n tasks: 0,\n patterns: 0,\n };\n\n // Import agents\n if (state.agents) {\n for (const agent of state.agents) {\n await this.storeAgent(agent.id, agent);\n imported.agents++;\n }\n }\n\n // Import tasks\n if (state.tasks) {\n for (const task of state.tasks) {\n await this.storeTask(task.id, task);\n imported.tasks++;\n }\n }\n\n // Import patterns\n if (state.patterns) {\n for (const pattern of state.patterns) {\n await this.storePattern(pattern.id, pattern);\n imported.patterns++;\n }\n }\n\n this.emit('swarm:imported', imported);\n\n return imported;\n }\n\n /**\n * Private helper methods\n */\n\n async _initializeSwarmNamespaces() {\n // Create swarm metadata\n await this.store(\n 'swarm:metadata',\n {\n swarmId: this.swarmId,\n createdAt: new Date().toISOString(),\n version: '1.0.0',\n namespaces: Object.values(SWARM_NAMESPACES),\n },\n {\n namespace: 'swarm:system',\n },\n );\n }\n\n async _loadSwarmState() {\n // Load active agents\n const agents = await this.list(SWARM_NAMESPACES.AGENTS, { limit: 100 });\n for (const entry of agents) {\n if (entry.value.status === 'active' || entry.value.status === 'busy') {\n this.agentCache.set(entry.value.id, entry.value);\n }\n }\n\n // Load in-progress tasks\n const tasks = await this.search({\n namespace: SWARM_NAMESPACES.TASKS,\n tags: ['in_progress'],\n limit: 100,\n });\n for (const entry of tasks) {\n this.taskCache.set(entry.value.id, entry.value);\n }\n\n // Load high-confidence patterns\n const patterns = await this.list(SWARM_NAMESPACES.PATTERNS, { limit: 50 });\n for (const entry of patterns) {\n if (entry.value.confidence > 0.7 || entry.value.successRate > 0.8) {\n this.patternCache.set(entry.value.id, entry.value);\n }\n }\n }\n\n async _countNamespace(namespace) {\n const stats = await this.getStats();\n return stats.namespaces[namespace]?.count || 0;\n }\n}\n\n// Export factory function for easy creation\nexport function createSwarmMemory(options = {}) {\n return new SwarmMemory(options);\n}\n\n// Export for backwards compatibility\nexport default SwarmMemory;\n"],"names":["SharedMemory","SWARM_NAMESPACES","AGENTS","TASKS","COMMUNICATIONS","CONSENSUS","PATTERNS","METRICS","COORDINATION","SwarmMemory","options","directory","filename","swarmId","mcpMode","agentCache","Map","taskCache","patternCache","initialize","_initializeSwarmNamespaces","_loadSwarmState","emit","storeAgent","agentId","agentData","key","enrichedData","lastUpdated","Date","toISOString","store","namespace","tags","type","status","metadata","agentType","set","stored","getAgent","has","get","agent","retrieve","listAgents","filter","agents","list","limit","map","entry","value","storeTask","taskId","taskData","createdAt","updatedAt","priority","assignedAgents","updateTaskStatus","result","task","getTask","Error","completedAt","updated","storeCommunication","fromAgent","toAgent","message","commId","now","Math","random","toString","substr","communication","id","timestamp","ttl","messageType","storeConsensus","consensusId","decision","consensusData","threshold","storePattern","patternId","pattern","patternData","usageCount","successRate","patternType","confidence","updatePatternMetrics","success","getPattern","lastUsedAt","alpha","currentSuccess","findBestPatterns","context","patterns","search","scored","score","sort","a","b","slice","storeCoordination","state","getCoordination","storeMetrics","metricsId","metrics","getSwarmStats","baseStats","getStats","agentCount","_countNamespace","taskCount","patternCount","activeAgents","Array","from","values","length","tasks","taskStats","total","pending","t","inProgress","completed","failed","swarm","active","cached","size","namespaces","Object","cleanupSwarmData","maxAge","keepPatterns","keepConsensus","cutoffTime","cleaned","comms","comm","getTime","delete","metric","exportSwarmState","exportedAt","p","statistics","importSwarmState","imported","version","stats","count","createSwarmMemory"],"mappings":"AAOA,SAASA,YAAY,QAAQ,qBAAqB;AAMlD,MAAMC,mBAAmB;IACvBC,QAAQ;IACRC,OAAO;IACPC,gBAAgB;IAChBC,WAAW;IACXC,UAAU;IACVC,SAAS;IACTC,cAAc;AAChB;AAKA,OAAO,MAAMC,oBAAoBT;IAC/B,YAAYU,UAAU,CAAC,CAAC,CAAE;QAExB,KAAK,CAAC;YACJC,WAAWD,QAAQC,SAAS,IAAI;YAChCC,UAAUF,QAAQE,QAAQ,IAAI;YAC9B,GAAGF,OAAO;QACZ;QAEA,IAAI,CAACG,OAAO,GAAGH,QAAQG,OAAO,IAAI;QAClC,IAAI,CA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QACA,KAAK,MAAME,SAAS8E,MAAO;YACzB,IAAI,CAAChH,SAAS,CAACqB,GAAG,CAACa,MAAMC,KAAK,CAAC0B,EAAE,EAAE3B,MAAMC,KAAK;QAChD;QAGA,MAAMmD,WAAW,MAAM,IAAI,CAACvD,IAAI,CAAC/C,iBAAiBK,QAAQ,EAAE;YAAE2C,OAAO;QAAG;QACxE,KAAK,MAAME,SAASoD,SAAU;YAC5B,IAAIpD,MAAMC,KAAK,CAAC0C,UAAU,GAAG,OAAO3C,MAAMC,KAAK,CAACwC,WAAW,GAAG,KAAK;gBACjE,IAAI,CAAC1E,YAAY,CAACoB,GAAG,CAACa,MAAMC,KAAK,CAAC0B,EAAE,EAAE3B,MAAMC,KAAK;YACnD;QACF;IACF;IAEA,MAAMqE,gBAAgBzF,SAAS,EAAE;QAC/B,MAAMiI,QAAQ,MAAM,IAAI,CAAC1C,QAAQ;QACjC,OAAO0C,MAAMpB,UAAU,CAAC7G,UAAU,EAAEkI,SAAS;IAC/C;AACF;AAGA,OAAO,SAASC,kBAAkBzJ,UAAU,CAAC,CAAC;IAC5C,OAAO,IAAID,YAAYC;AACzB;AAGA,eAAeD,YAAY"}
@@ -166,14 +166,4 @@ export class MetricsReader {
166
166
  }
167
167
  }
168
168
 
169
- //# sourceMappingURL=metrics-reader.js.map processCount: 0,
170
- orchestratorRunning: false,
171
- port: null,
172
- connections: 0
173
- };
174
- }
175
- }
176
- };
177
- export { MetricsReader };
178
-
179
169
  //# sourceMappingURL=metrics-reader.js.map
@@ -0,0 +1,462 @@
1
+ # Optional: Local Semantic Search with Transformers.js
2
+
3
+ ## Overview
4
+
5
+ By default, the memory system works perfectly **without any embeddings** using database pattern matching. This optional enhancement adds true semantic search using local AI models.
6
+
7
+ ## Current Default Behavior ✅
8
+
9
+ **Already works out of the box:**
10
+ ```bash
11
+ npx claude-flow@alpha memory store "api-design" "REST with JWT"
12
+ npx claude-flow@alpha memory list
13
+ # ✅ Works perfectly - no setup required!
14
+ ```
15
+
16
+ **Memory query uses database fallback:**
17
+ ```bash
18
+ npx claude-flow@alpha memory query "api"
19
+ # ✅ Works with pattern matching (exact/partial text search)
20
+ ```
21
+
22
+ ## Optional Enhancement: True Semantic Search
23
+
24
+ Add AI-powered semantic similarity for better query results.
25
+
26
+ ### Installation Options
27
+
28
+ **Option 1: Package-level (Recommended for Development)**
29
+
30
+ ```bash
31
+ # In agentic-flow repository
32
+ npm install @xenova/transformers --save-optional
33
+
34
+ # Or in claude-flow
35
+ npm install @xenova/transformers --save-optional
36
+ ```
37
+
38
+ **Option 2: User-level (Recommended for Production)**
39
+
40
+ ```bash
41
+ # Users can enable semantic search with:
42
+ npm install -g @xenova/transformers
43
+
44
+ # Or with npx:
45
+ npx -p @xenova/transformers -p claude-flow@alpha claude-flow memory query "api"
46
+ ```
47
+
48
+ **Option 3: Environment Variable Flag**
49
+
50
+ ```bash
51
+ # Enable semantic search
52
+ export CLAUDE_FLOW_SEMANTIC_SEARCH=true
53
+
54
+ # Run memory commands
55
+ npx claude-flow@alpha memory query "authentication patterns"
56
+ ```
57
+
58
+ ---
59
+
60
+ ## Implementation Strategy
61
+
62
+ ### 1. Make @xenova/transformers Optional in package.json
63
+
64
+ **File: `agentic-flow/package.json` or `claude-flow/package.json`**
65
+
66
+ ```json
67
+ {
68
+ "dependencies": {
69
+ "agentdb": "^1.4.3",
70
+ "better-sqlite3": "^11.10.0"
71
+ // ... existing deps
72
+ },
73
+ "optionalDependencies": {
74
+ "@xenova/transformers": "^3.2.0"
75
+ },
76
+ "peerDependencies": {
77
+ "@xenova/transformers": "^3.0.0"
78
+ },
79
+ "peerDependenciesMeta": {
80
+ "@xenova/transformers": {
81
+ "optional": true
82
+ }
83
+ }
84
+ }
85
+ ```
86
+
87
+ ### 2. Update Embeddings Utility with Graceful Fallback
88
+
89
+ **File: `src/reasoningbank/utils/embeddings.ts`**
90
+
91
+ ```typescript
92
+ /**
93
+ * Embedding utilities with optional local model support
94
+ */
95
+
96
+ let transformersAvailable = false;
97
+ let embeddingPipeline: any = null;
98
+
99
+ /**
100
+ * Check if transformers.js is available
101
+ */
102
+ async function checkTransformersAvailable(): Promise<boolean> {
103
+ try {
104
+ const transformers = await import('@xenova/transformers');
105
+ return true;
106
+ } catch (error) {
107
+ return false;
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Initialize embeddings if available
113
+ */
114
+ async function initializeEmbeddings(): Promise<boolean> {
115
+ if (embeddingPipeline) return true; // Already initialized
116
+
117
+ // Check if feature is enabled
118
+ const semanticSearchEnabled = process.env.CLAUDE_FLOW_SEMANTIC_SEARCH === 'true';
119
+ if (!semanticSearchEnabled) {
120
+ console.log('[Embeddings] Semantic search disabled (set CLAUDE_FLOW_SEMANTIC_SEARCH=true to enable)');
121
+ return false;
122
+ }
123
+
124
+ // Check if transformers is available
125
+ transformersAvailable = await checkTransformersAvailable();
126
+
127
+ if (!transformersAvailable) {
128
+ console.log('[Embeddings] @xenova/transformers not installed');
129
+ console.log('');
130
+ console.log('📝 To enable semantic search, run:');
131
+ console.log(' npm install -g @xenova/transformers');
132
+ console.log(' export CLAUDE_FLOW_SEMANTIC_SEARCH=true');
133
+ console.log('');
134
+ return false;
135
+ }
136
+
137
+ try {
138
+ const { pipeline } = await import('@xenova/transformers');
139
+
140
+ console.log('[Embeddings] Loading local model (first time: ~23 MB download)...');
141
+ embeddingPipeline = await pipeline(
142
+ 'feature-extraction',
143
+ 'Xenova/all-MiniLM-L6-v2'
144
+ );
145
+
146
+ console.log('[Embeddings] ✅ Semantic search enabled (Xenova/all-MiniLM-L6-v2)');
147
+ return true;
148
+
149
+ } catch (error) {
150
+ console.warn('[Embeddings] Failed to initialize:', error.message);
151
+ return false;
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Compute embedding (returns null if not available)
157
+ */
158
+ export async function computeEmbedding(text: string): Promise<Float32Array | null> {
159
+ const initialized = await initializeEmbeddings();
160
+
161
+ if (!initialized || !embeddingPipeline) {
162
+ return null; // Feature not available
163
+ }
164
+
165
+ try {
166
+ const output = await embeddingPipeline(text, {
167
+ pooling: 'mean',
168
+ normalize: true
169
+ });
170
+ return new Float32Array(output.data);
171
+ } catch (error) {
172
+ console.error('[Embeddings] Generation failed:', error.message);
173
+ return null;
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Check if semantic search is available
179
+ */
180
+ export async function isSemanticSearchAvailable(): Promise<boolean> {
181
+ return await initializeEmbeddings();
182
+ }
183
+
184
+ /**
185
+ * Clear embedding cache (no-op if not initialized)
186
+ */
187
+ export function clearEmbeddingCache(): void {
188
+ // No-op for now
189
+ }
190
+ ```
191
+
192
+ ### 3. Update Retrieval to Handle Optional Embeddings
193
+
194
+ **File: `src/reasoningbank/core/retrieve.ts`**
195
+
196
+ ```typescript
197
+ import { computeEmbedding, isSemanticSearchAvailable } from '../utils/embeddings.js';
198
+ import { cosineSimilarity } from '../utils/mmr.js';
199
+ import { fetchMemoryCandidates } from '../db/queries.js';
200
+
201
+ export async function retrieveMemories(
202
+ query: string,
203
+ options: RetrievalOptions
204
+ ): Promise<any[]> {
205
+ console.log(`[INFO] Retrieving memories for query: ${query}...`);
206
+
207
+ // Fetch candidates from database
208
+ const candidates = fetchMemoryCandidates(options);
209
+
210
+ if (candidates.length === 0) {
211
+ console.log('[INFO] No memory candidates found');
212
+ return [];
213
+ }
214
+
215
+ console.log(`[INFO] Found ${candidates.length} candidates`);
216
+
217
+ // Check if semantic search is available
218
+ const semanticAvailable = await isSemanticSearchAvailable();
219
+
220
+ if (!semanticAvailable) {
221
+ console.log('[INFO] Using database pattern matching (semantic search not enabled)');
222
+ return candidates.slice(0, options.k);
223
+ }
224
+
225
+ // Try semantic search
226
+ try {
227
+ const queryEmbedding = await computeEmbedding(query);
228
+
229
+ if (!queryEmbedding) {
230
+ console.log('[INFO] Semantic search unavailable, using database fallback');
231
+ return candidates.slice(0, options.k);
232
+ }
233
+
234
+ console.log('[INFO] Using semantic search with local embeddings');
235
+
236
+ // Compute similarities
237
+ const scoredCandidates = candidates
238
+ .map(candidate => {
239
+ if (!candidate.embedding || candidate.embedding.length !== queryEmbedding.length) {
240
+ return { ...candidate, similarity: 0 };
241
+ }
242
+ const similarity = cosineSimilarity(queryEmbedding, candidate.embedding);
243
+ return { ...candidate, similarity };
244
+ })
245
+ .sort((a, b) => b.similarity - a.similarity);
246
+
247
+ return scoredCandidates.slice(0, options.k);
248
+
249
+ } catch (error) {
250
+ console.error('[ERROR] Semantic search failed:', error.message);
251
+ console.log('[INFO] Falling back to database pattern matching');
252
+ return candidates.slice(0, options.k);
253
+ }
254
+ }
255
+ ```
256
+
257
+ ### 4. Update CLI to Show Status
258
+
259
+ **File: `src/cli/simple-commands/memory.js`**
260
+
261
+ ```javascript
262
+ async function handleQueryCommand(query, options) {
263
+ console.log('\nℹ️ 🧠 Using ReasoningBank mode...');
264
+
265
+ // Check semantic search availability
266
+ const { isSemanticSearchAvailable } = await import('../../reasoningbank/utils/embeddings.js');
267
+ const semanticEnabled = await isSemanticSearchAvailable();
268
+
269
+ if (!semanticEnabled) {
270
+ console.log('💡 Tip: Enable semantic search for better results:');
271
+ console.log(' npm install -g @xenova/transformers');
272
+ console.log(' export CLAUDE_FLOW_SEMANTIC_SEARCH=true');
273
+ console.log('');
274
+ }
275
+
276
+ // ... rest of query logic
277
+ }
278
+ ```
279
+
280
+ ---
281
+
282
+ ## User Experience
283
+
284
+ ### Default Experience (No Setup)
285
+
286
+ ```bash
287
+ $ npx claude-flow@alpha memory query "api"
288
+
289
+ ℹ️ 🧠 Using ReasoningBank mode...
290
+ [ReasoningBank] Initializing...
291
+ [ReasoningBank] Enabled: true (initializing...)
292
+ [INFO] Retrieving memories for query: api...
293
+ [INFO] Found 5 candidates
294
+ [INFO] Using database pattern matching (semantic search not enabled)
295
+ ✅ Found 2 results
296
+
297
+ 📌 api-design
298
+ Value: REST with JWT auth
299
+ ```
300
+
301
+ ### Enhanced Experience (With Setup)
302
+
303
+ ```bash
304
+ $ npm install -g @xenova/transformers
305
+ $ export CLAUDE_FLOW_SEMANTIC_SEARCH=true
306
+ $ npx claude-flow@alpha memory query "authentication patterns"
307
+
308
+ ℹ️ 🧠 Using ReasoningBank mode...
309
+ [ReasoningBank] Initializing...
310
+ [Embeddings] Loading local model (first time: ~23 MB download)...
311
+ [Embeddings] ✅ Semantic search enabled (Xenova/all-MiniLM-L6-v2)
312
+ [INFO] Using semantic search with local embeddings
313
+ ✅ Found 3 results (semantic similarity):
314
+
315
+ 📌 api-design
316
+ Value: REST with JWT auth
317
+ Match Score: 89.3% ← Semantic similarity!
318
+
319
+ 📌 oauth-config
320
+ Value: OAuth 2.0 with PKCE
321
+ Match Score: 76.2% ← Related concept!
322
+ ```
323
+
324
+ ---
325
+
326
+ ## Documentation Updates
327
+
328
+ ### README.md
329
+
330
+ ```markdown
331
+ ## Memory System
332
+
333
+ The memory system works out of the box with database pattern matching.
334
+
335
+ ### Optional: Semantic Search
336
+
337
+ For AI-powered semantic similarity, install the optional transformer models:
338
+
339
+ ```bash
340
+ # Enable semantic search
341
+ npm install -g @xenova/transformers
342
+ export CLAUDE_FLOW_SEMANTIC_SEARCH=true
343
+
344
+ # Now queries use semantic similarity
345
+ npx claude-flow@alpha memory query "authentication"
346
+ ```
347
+
348
+ **Benefits:**
349
+ - Find related concepts (e.g., "auth" matches "JWT", "OAuth")
350
+ - 50-100ms per query
351
+ - Works offline
352
+ - Free (no API costs)
353
+ - ~23 MB one-time download
354
+
355
+ **Without semantic search:**
356
+ - Database pattern matching (exact/partial text)
357
+ - Instant (no model loading)
358
+ - Zero dependencies
359
+ - Always works
360
+ ```
361
+
362
+ ---
363
+
364
+ ## Package Configuration
365
+
366
+ ### package.json Changes
367
+
368
+ ```json
369
+ {
370
+ "optionalDependencies": {
371
+ "@xenova/transformers": "^3.2.0"
372
+ },
373
+ "scripts": {
374
+ "postinstall": "node scripts/check-optional-deps.js"
375
+ }
376
+ }
377
+ ```
378
+
379
+ ### scripts/check-optional-deps.js
380
+
381
+ ```javascript
382
+ // Check if optional dependencies are available
383
+ const fs = require('fs');
384
+ const path = require('path');
385
+
386
+ const transformersPath = path.join(__dirname, '../node_modules/@xenova/transformers');
387
+
388
+ if (fs.existsSync(transformersPath)) {
389
+ console.log('✅ Optional: Semantic search available (@xenova/transformers installed)');
390
+ } else {
391
+ console.log('');
392
+ console.log('💡 Optional Enhancement Available:');
393
+ console.log(' Install @xenova/transformers for semantic search');
394
+ console.log(' npm install -g @xenova/transformers');
395
+ console.log('');
396
+ }
397
+ ```
398
+
399
+ ---
400
+
401
+ ## Benefits of Optional Approach
402
+
403
+ ### ✅ Advantages
404
+
405
+ 1. **Zero friction for basic use** - Works immediately with npx
406
+ 2. **No large downloads required** - Users choose when to download 23 MB model
407
+ 3. **Graceful degradation** - Falls back to database search automatically
408
+ 4. **User choice** - Install only if semantic search is needed
409
+ 5. **Clear communication** - Users know what they're missing and how to get it
410
+
411
+ ### ❌ Avoids Problems
412
+
413
+ 1. No mandatory 23 MB download for users who don't need semantic search
414
+ 2. No install failures due to native dependencies
415
+ 3. No confusion about why embeddings aren't working
416
+ 4. No breaking changes to existing functionality
417
+
418
+ ---
419
+
420
+ ## Testing Strategy
421
+
422
+ ### Test 1: Default (No Semantic Search)
423
+ ```bash
424
+ # Fresh install
425
+ npx claude-flow@alpha memory store "test" "value"
426
+ npx claude-flow@alpha memory query "test"
427
+ # ✅ Should work with database pattern matching
428
+ ```
429
+
430
+ ### Test 2: Enhanced (With Semantic Search)
431
+ ```bash
432
+ # Install optional dep
433
+ npm install -g @xenova/transformers
434
+ export CLAUDE_FLOW_SEMANTIC_SEARCH=true
435
+
436
+ npx claude-flow@alpha memory query "test"
437
+ # ✅ Should use semantic similarity
438
+ ```
439
+
440
+ ### Test 3: Graceful Fallback
441
+ ```bash
442
+ # Enable flag but don't install package
443
+ export CLAUDE_FLOW_SEMANTIC_SEARCH=true
444
+ unset npm_config_prefix # Ensure transformers not available
445
+
446
+ npx claude-flow@alpha memory query "test"
447
+ # ✅ Should show helpful message and fall back to database search
448
+ ```
449
+
450
+ ---
451
+
452
+ ## Summary
453
+
454
+ This approach gives you:
455
+
456
+ ✅ **Works out of the box** - No setup required for basic memory system
457
+ ✅ **Optional enhancement** - Users install @xenova/transformers only if they want semantic search
458
+ ✅ **Graceful fallback** - Always works, even without transformers installed
459
+ ✅ **Clear messaging** - Users know what's available and how to enable it
460
+ ✅ **Zero breaking changes** - Existing functionality unchanged
461
+
462
+ **Recommendation:** Implement this optional approach for maximum user flexibility and zero friction.