mongodb-memory-bank-mcp
Version:
FIXED: MongoDB Memory Bank MCP with bulletproof error handling, smart operations, and session state management. Eliminates [object Object] errors and user confusion.
434 lines (388 loc) • 16.3 kB
JavaScript
/**
* Cline's Memory Bank Structure Implementation
* Replaces template bloat with intelligent hierarchical memory organization
* Based on Cline's proven memory bank structure
*/
/**
* Cline's 6 Core Memory Files
* Hierarchical structure: projectbrief → (productContext/systemPatterns/techContext) → activeContext → progress
*/
export const CLINE_CORE_FILES = {
PROJECT_BRIEF: 'projectbrief.md',
PRODUCT_CONTEXT: 'productContext.md',
SYSTEM_PATTERNS: 'systemPatterns.md',
TECH_CONTEXT: 'techContext.md',
ACTIVE_CONTEXT: 'activeContext.md',
PROGRESS: 'progress.md'
};
/**
* Memory Type to Core File Mapping
* Intelligent routing based on content analysis
*/
export const MEMORY_TYPE_TO_CORE_FILE = {
'project-overview': CLINE_CORE_FILES.PROJECT_BRIEF,
'requirements': CLINE_CORE_FILES.PROJECT_BRIEF,
'architecture': CLINE_CORE_FILES.SYSTEM_PATTERNS,
'implementation': CLINE_CORE_FILES.SYSTEM_PATTERNS,
'configuration': CLINE_CORE_FILES.TECH_CONTEXT,
'documentation': CLINE_CORE_FILES.ACTIVE_CONTEXT,
'notes': CLINE_CORE_FILES.ACTIVE_CONTEXT,
'research': CLINE_CORE_FILES.ACTIVE_CONTEXT,
'meeting-notes': CLINE_CORE_FILES.ACTIVE_CONTEXT,
'decisions': CLINE_CORE_FILES.ACTIVE_CONTEXT,
'issues': CLINE_CORE_FILES.PROGRESS,
'tasks': CLINE_CORE_FILES.PROGRESS,
'progress': CLINE_CORE_FILES.PROGRESS,
'other': CLINE_CORE_FILES.ACTIVE_CONTEXT
};
/**
* Cline Memory Structure Manager
* Handles intelligent content routing and core file management
*/
export class ClineMemoryStructure {
/**
* 🚀 CONTENT DISTRIBUTION BREAKTHROUGH: Analyze content and ALWAYS route to core files
*/
static analyzeContentRouting(fileName, content) {
const lowerFileName = fileName.toLowerCase();
const lowerContent = content.toLowerCase();
// Direct filename mapping
if (Object.values(CLINE_CORE_FILES).includes(fileName)) {
return {
coreFile: fileName,
confidence: 100,
reasoning: 'Direct core file match',
shouldUpdate: true,
mergeStrategy: 'merge'
};
}
// 🎯 BREAKTHROUGH: Content-based analysis with FORCED routing
const analysis = this.analyzeContentType(lowerFileName, lowerContent);
// 🚀 FORCED DISTRIBUTION: Always route with minimum 75% confidence
const forcedConfidence = Math.max(analysis.confidence, 75);
// 🚨 LARGE CONTENT DETECTION: Suggest distribution for large content
const wordCount = content.split(/\s+/).length;
if (wordCount > 2000) {
console.warn(`[CONTENT-DISTRIBUTION] ⚠️ Large content (${wordCount} words) routing to ${analysis.coreFile}`);
console.warn(`[CONTENT-DISTRIBUTION] 💡 Breakthrough: Content will be intelligently distributed to core files`);
}
return {
coreFile: analysis.coreFile,
confidence: forcedConfidence,
reasoning: `🚀 BREAKTHROUGH: ${analysis.reasoning} (forced distribution to core files)`,
shouldUpdate: true, // 🎯 ALWAYS route - never create separate files
mergeStrategy: analysis.mergeStrategy
};
}
/**
* Get core file initialization templates
*/
static getCoreFileTemplate(coreFile, projectName) {
switch (coreFile) {
case CLINE_CORE_FILES.PROJECT_BRIEF:
return this.getProjectBriefTemplate(projectName);
case CLINE_CORE_FILES.PRODUCT_CONTEXT:
return this.getProductContextTemplate(projectName);
case CLINE_CORE_FILES.SYSTEM_PATTERNS:
return this.getSystemPatternsTemplate(projectName);
case CLINE_CORE_FILES.TECH_CONTEXT:
return this.getTechContextTemplate(projectName);
case CLINE_CORE_FILES.ACTIVE_CONTEXT:
return this.getActiveContextTemplate(projectName);
case CLINE_CORE_FILES.PROGRESS:
return this.getProgressTemplate(projectName);
default:
return `# ${coreFile}\n\nContent for ${projectName}`;
}
}
/**
* Check if all core files exist for a project
*/
static getCoreFilesList() {
return Object.values(CLINE_CORE_FILES);
}
// Private implementation methods
static analyzeContentType(fileName, content) {
// 🎯 PRIORITY-BASED ROUTING: Most specific patterns first, avoid conflicts
// 1. PROGRESS/STATUS (highest priority - very specific)
if (this.matchesSpecificPatterns(content, [
'progress', 'status', 'done', 'completed', 'milestone', 'phase',
'what works', 'what\'s left', 'current status', 'known issues',
'timeline', 'roadmap', 'next steps', 'todo', 'remaining work'
]) || this.matchesSpecificPatterns(fileName, ['progress', 'status', 'roadmap', 'timeline'])) {
return {
coreFile: CLINE_CORE_FILES.PROGRESS,
confidence: 95,
reasoning: 'Contains progress/status/timeline information',
mergeStrategy: 'section-update'
};
}
// 2. TECHNICAL IMPLEMENTATION (high priority - specific tech content)
if (this.matchesSpecificPatterns(content, [
'javascript', 'typescript', 'react', 'vue', 'angular', 'node.js',
'html', 'css', 'framework', 'library', 'dependency', 'package.json',
'npm', 'yarn', 'webpack', 'vite', 'build', 'deployment', 'ci/cd',
'browser support', 'performance', 'lighthouse', 'optimization'
]) || this.matchesSpecificPatterns(fileName, ['tech', 'technology', 'stack', 'setup', 'config', 'env'])) {
return {
coreFile: CLINE_CORE_FILES.TECH_CONTEXT,
confidence: 90,
reasoning: 'Contains technical implementation details',
mergeStrategy: 'section-update'
};
}
// 3. SYSTEM ARCHITECTURE (high priority - specific architecture content)
if (this.matchesSpecificPatterns(content, [
'architecture', 'design pattern', 'component', 'module', 'class',
'interface', 'api design', 'data flow', 'system design',
'mvc', 'mvvm', 'microservices', 'monolith', 'database schema',
'authentication', 'authorization', 'security', 'scalability'
]) || this.matchesSpecificPatterns(fileName, ['architecture', 'design', 'pattern', 'system', 'api'])) {
return {
coreFile: CLINE_CORE_FILES.SYSTEM_PATTERNS,
confidence: 90,
reasoning: 'Contains system architecture and design patterns',
mergeStrategy: 'section-update'
};
}
// 4. USER/PRODUCT CONTEXT (medium priority - user-focused content)
if (this.matchesSpecificPatterns(content, [
'user problem', 'pain point', 'user experience', 'ux', 'ui',
'persona', 'user journey', 'workflow', 'use case', 'user story',
'target audience', 'market need', 'business value', 'customer'
]) || this.matchesSpecificPatterns(fileName, ['user', 'ux', 'product', 'customer', 'market'])) {
return {
coreFile: CLINE_CORE_FILES.PRODUCT_CONTEXT,
confidence: 85,
reasoning: 'Contains user/product/UX context',
mergeStrategy: 'section-update'
};
}
// 5. PROJECT BRIEF (lower priority - only very specific project overview content)
if (this.matchesSpecificPatterns(content, [
'project brief', 'project overview', 'core requirements', 'project goals',
'success criteria', 'project scope', 'definition of done'
]) || this.matchesSpecificPatterns(fileName, ['brief', 'overview', 'requirements', 'goals'])) {
return {
coreFile: CLINE_CORE_FILES.PROJECT_BRIEF,
confidence: 80,
reasoning: 'Contains project brief/overview information',
mergeStrategy: 'section-update'
};
}
// 6. DEFAULT: Active context (lowest priority)
return {
coreFile: CLINE_CORE_FILES.ACTIVE_CONTEXT,
confidence: 70,
reasoning: 'General content - routing to active context',
mergeStrategy: 'section-update'
};
}
/**
* 🎯 IMPROVED PATTERN MATCHING: More precise matching to avoid conflicts
*/
static matchesSpecificPatterns(text, patterns) {
const lowerText = text.toLowerCase();
return patterns.some(pattern => {
const lowerPattern = pattern.toLowerCase();
// More specific matching - require word boundaries or specific contexts
return lowerText.includes(lowerPattern) && (
// Pattern appears as whole word or phrase
new RegExp(`\\b${lowerPattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`).test(lowerText) ||
// Pattern appears in context (with common prefixes/suffixes)
lowerText.includes(`${lowerPattern}:`) ||
lowerText.includes(`${lowerPattern} -`) ||
lowerText.includes(`${lowerPattern}s`) ||
lowerText.includes(`${lowerPattern}ing`));
});
}
static matchesPatterns(text, patterns) {
return patterns.some(pattern => text.includes(pattern));
}
/**
* 🎯 SMART DISTRIBUTION: Find the best file to route content to based on what needs content most
* This helps ensure all 6 core files get populated with real content
*/
static findBestDistributionTarget(content) {
// This would need access to the memory repository to check which files need content
// For now, return null to use existing logic
// TODO: Implement when we have access to memory repository in this context
return null;
}
// Template methods
static getProjectBriefTemplate(projectName) {
return `# Project Brief: ${projectName}
## Project Overview
**What We're Building**: [Brief description of the project]
**Why It Matters**: [Business/user value proposition]
**Timeline**: [Expected completion timeframe]
## Core Requirements
**Must-Have Features**:
- Requirement 1: [specific functionality]
- Requirement 2: [specific capability]
- Requirement 3: [specific outcome]
**Technical Requirements**:
- Performance: [speed/scale requirements]
- Security: [security standards]
- Compatibility: [browser/device support]
## Project Goals
**Primary Goals**:
- Goal 1: [measurable outcome]
- Goal 2: [specific achievement]
- Goal 3: [business impact]
**Success Metrics**:
- Metric 1: [how to measure success]
- Metric 2: [quantifiable target]
- Metric 3: [user satisfaction indicator]
## Scope
**In Scope**:
- Feature set: [what we're building]
- User types: [who we're serving]
- Platforms: [where it will run]
**Out of Scope** (for now):
- Advanced features: [future enhancements]
- Integrations: [potential future connections]
- Scale: [current vs future scale]
## Success Criteria
**Definition of Done**:
- [ ] All core requirements implemented
- [ ] Performance targets met
- [ ] User acceptance criteria satisfied
- [ ] Security requirements fulfilled
**Launch Readiness**:
- [ ] Testing complete
- [ ] Documentation ready
- [ ] Deployment pipeline working
- [ ] Monitoring in place
---
*This is the foundation document. All other memory files build upon this.*`;
}
static getProductContextTemplate(projectName) {
return `# Product Context: ${projectName}
## Why This Project Exists
**Problem Statement**: Users struggle with [specific problem]
**Market Need**: There's a gap in [market area]
**Business Value**: This project delivers [specific value]
## User Problems & Pain Points
**Primary Pain Points**:
- Users can't easily [specific task]
- Current solutions are [limitation]
- Manual processes cause [inefficiency]
**User Frustrations**:
- "I wish I could [user quote]"
- Time wasted on [specific activity]
- Confusion around [specific area]
## Solution Approach
**Core Solution**: We solve this by [approach]
**Key Features**:
- Feature 1: [solves problem X]
- Feature 2: [addresses pain point Y]
- Feature 3: [improves workflow Z]
## User Experience Goals
**Primary UX Goals**:
- Reduce task completion time by [X]%
- Eliminate [specific friction point]
- Make [complex process] intuitive
**User Workflows**:
- New User: [onboarding flow]
- Power User: [advanced workflow]
- Admin: [management workflow]
## Target Users
**Primary Persona**: [Name] - [Role]
- Goals: [what they want to achieve]
- Frustrations: [current pain points]
- Tech comfort: [skill level]
**Secondary Persona**: [Name] - [Role]
- Different needs: [how they differ]
- Usage patterns: [when/how they use it]
---
*User problems, solution approach, UX goals, and target personas belong here.*`;
}
static getSystemPatternsTemplate(projectName) {
return `# System Patterns: ${projectName}
## System Architecture
**Pattern**: [MVC/Component-based/Modular/Microservices]
**Structure**: [Describe folder/module organization]
**Data Flow**: [How data moves through the system]
## Key Technical Decisions
**State Management**: [Redux/Context/Local state approach]
**Routing**: [Client-side/Server-side routing strategy]
**API Design**: [REST/GraphQL/RPC patterns]
**Error Handling**: [Global/Local error handling strategy]
## Design Patterns
**Creational**: [Factory/Builder/Singleton patterns used]
**Structural**: [Adapter/Decorator/Facade patterns used]
**Behavioral**: [Observer/Strategy/Command patterns used]
## Component Relationships
**Parent-Child**: [Component hierarchy and props flow]
**Sibling Communication**: [Event system/state management]
**Service Integration**: [How services interact]
## Critical Implementation Paths
**Authentication Flow**: [Login/logout/token management]
**Data Persistence**: [Database/localStorage/API patterns]
**Performance Optimization**: [Caching/lazy loading/bundling]
---
*Code architecture, design patterns, and system structure details belong here.*`;
}
static getTechContextTemplate(projectName) {
return `# Tech Context: ${projectName}
## Technologies Used
**Frontend**: JavaScript ES6, HTML5, CSS3
**Frameworks**: [React/Vue/Angular or Vanilla JS]
**Build Tools**: [Webpack/Vite/Parcel]
**Package Manager**: [npm/yarn/pnpm]
## Development Setup
**Prerequisites**: Node.js 18+, Git
**Installation**: \`npm install\`
**Development**: \`npm run dev\`
**Build**: \`npm run build\`
## Technical Constraints
**Browser Support**: Modern browsers (ES6+)
**Performance**: <3s load time, 95+ Lighthouse score
**Accessibility**: WCAG 2.1 AA compliance
## Dependencies
**Production**: [List runtime dependencies]
**Development**: [List dev dependencies]
**Security**: Regular dependency audits
## Tool Usage Patterns
**Code Quality**: ESLint, Prettier, TypeScript
**Testing**: Jest, Cypress, Testing Library
**Deployment**: [CI/CD pipeline details]
---
*Technical implementation details, code architecture, and development environment specifics belong here.*`;
}
static getActiveContextTemplate(projectName) {
return `# Active Context: ${projectName}
## Current Work Focus
- [What we're currently working on]
## Recent Changes
- [Recent changes and updates]
## Next Steps
- [Immediate next steps]
## Active Decisions
- [Decisions currently being made]
## Important Patterns & Preferences
- [Patterns and preferences discovered]
## Learnings & Insights
- [Key learnings and project insights]
---
*Builds on: productContext.md, systemPatterns.md, techContext.md*`;
}
static getProgressTemplate(projectName) {
return `# Progress: ${projectName}
## What Works
- [List what's currently working]
## What's Left to Build
- [List remaining work]
## Current Status
- [Overall project status]
## Known Issues
- [List known issues and bugs]
## Evolution of Decisions
- [How decisions have evolved]
## Milestones
- [Key milestones and achievements]
---
*Builds on: activeContext.md*`;
}
}