# MarketAlly.AIPlugin.Learning [![.NET 8.0](https://img.shields.io/badge/.NET-8.0-blue.svg)](https://dotnet.microsoft.com/) [![License](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE) [![Build Status](https://img.shields.io/badge/build-passing-brightgreen.svg)]() ## ๐Ÿš€ Revolutionary AI-Powered Learning & Refactoring System MarketAlly.AIPlugin.Learning is the **world's first unified AI development assistant** that combines real-time code intelligence with historical memory to create an intelligent, self-improving refactoring system. It learns from past decisions, avoids previous mistakes, and builds organizational knowledge over time. ## โœจ Key Features ### ๐Ÿง  Unified Context Intelligence - **Real-time Code Analysis**: Live code understanding with dependency tracking - **Historical Memory**: Learns from past conversations and decisions - **Context-Informed Refactoring**: Uses historical patterns to guide decisions - **Predictive Analysis**: Identifies issues before they occur ### ๐Ÿ”’ Enterprise-Grade Security - **Path Validation**: Prevents directory traversal attacks - **Input Sanitization**: Removes unsafe characters and validates inputs - **File Access Control**: Restricts access to approved file types and locations - **Configuration Validation**: Comprehensive settings validation ### ๐Ÿ“Š Advanced Analytics - **Structured Logging**: Correlation IDs track operations across services - **Performance Metrics**: Detailed timing and resource usage statistics - **Failure Pattern Analysis**: Learns from unsuccessful attempts - **Success Pattern Recognition**: Identifies and reuses successful approaches ### ๐Ÿ—๏ธ Service-Oriented Architecture - **Dependency Injection**: Fully testable and maintainable design - **Custom Exception Hierarchy**: Specific error types for different scenarios - **Resource Management**: Proper IDisposable patterns throughout - **Thread-Safe Operations**: Concurrent collections and safe multi-threading ## ๐Ÿš€ Quick Start ### Installation 1. **Clone the repository** ```bash git clone https://github.com/your-org/MarketAlly.AIPlugin.git cd MarketAlly.AIPlugin/MarketAlly.AIPlugin.Learning ``` 2. **Restore dependencies** ```bash dotnet restore ``` 3. **Build the project** ```bash dotnet build ``` ### Basic Usage #### Using the Comprehensive Learning Plugin ```csharp var plugin = new ComprehensiveLearningRefactorPlugin(); var parameters = new Dictionary { ["solutionPath"] = @"C:\YourProject\Solution.sln", ["learningMode"] = "conservative", ["enableSemanticSearch"] = true, ["openAIApiKey"] = "your-openai-api-key", ["maxIterations"] = 20, ["verboseReporting"] = true }; var result = await plugin.ExecuteAsync(parameters); ``` #### Using the Unified Context Service ```csharp // Initialize services var services = new ServiceCollection(); services.AddSingleton(); var provider = services.BuildServiceProvider(); var contextService = provider.GetService(); // Get comprehensive context combining real-time + historical data var context = await contextService.PrepareFullContextAsync( "refactor this class for better maintainability", filePath: "MyClass.cs", maxTokens: 8000 ); // The context includes: // - Current code analysis // - Historical insights from past sessions // - Related decisions from similar refactoring attempts // - Project-wide context information ``` ## ๐Ÿ“‹ Configuration ### Learning Modes | Mode | Description | Max Iterations | Risk Level | |------|-------------|----------------|------------| | **Conservative** | Safe, minimal changes | 10 | Low | | **Moderate** | Balanced approach | 20 | Medium | | **Aggressive** | Comprehensive refactoring | 50 | High | ### Configuration Example ```json { "Learning": { "Git": { "BranchPrefix": "ai-refactoring", "CommitterName": "AI Learning System", "CommitterEmail": "ai@learning.system" }, "LearningModes": { "Conservative": { "MaxIterations": 10, "MaxAttemptsPerFile": 2, "EnableRiskyRefactorings": false }, "Moderate": { "MaxIterations": 20, "MaxAttemptsPerFile": 3, "EnableRiskyRefactorings": true }, "Aggressive": { "MaxIterations": 50, "MaxAttemptsPerFile": 5, "EnableRiskyRefactorings": true } }, "AI": { "EnableSemanticSearch": true, "MaxSearchResults": 10, "MinSimilarityScore": 0.7, "MaxContextTokens": 8000 }, "Security": { "ForbiddenDirectories": ["bin", "obj", ".git", "node_modules"], "AllowedFileExtensions": [".cs", ".csproj", ".sln"], "MaxFileSize": 10485760 } } } ``` ## ๐ŸŽฏ Core Services ### LearningOrchestrator Main orchestration service that coordinates the entire learning session. ```csharp public interface ILearningOrchestrator : IDisposable { Task ExecuteCompleteLearningSessionAsync( ComprehensiveLearningSession session); } ``` ### UnifiedContextService Revolutionary service combining real-time and historical intelligence. ```csharp public interface IUnifiedContextService { Task PrepareFullContextAsync(string query, string? filePath = null, int maxTokens = 8000); Task InitializeLearningSessionAsync(string projectPath, string topic); Task StoreLearningInsightAsync(string insight, string category, string? filePath = null); Task> FindSimilarPastIssuesAsync(string currentIssue); Task StoreRefactoringDecisionAsync(string decision, string reasoning, string filePath, bool successful); } ``` ### SecurityService Comprehensive security validation and sanitization. ```csharp public interface ISecurityService { bool IsPathSafe(string path); bool IsFileAllowed(string filePath); string SanitizeInput(string input); ValidationResult ValidateConfiguration(LearningConfiguration config); string GenerateSecureSessionId(); } ``` ### LLMContextService Intelligent LLM context preparation with dependency tracking. ```csharp public interface ILLMContextService { Task PrepareContextAsync(string query, int maxTokens = 4000); Task PrepareCodeAnalysisContextAsync(string filePath, string query, int maxTokens = 4000); Task GetDependencyContextAsync(string symbolName); Task AnalyzeChangeImpactAsync(string filePath, int lineNumber); } ``` ## ๐Ÿ”ง Advanced Usage ### Learning Session with Historical Context ```csharp // Initialize learning session var sessionContext = await unifiedContextService.InitializeLearningSessionAsync( projectPath: @"C:\MyProject", topic: "Performance optimization refactoring" ); // Get context for specific refactoring decision var context = await unifiedContextService.PrepareFullContextAsync( "optimize database queries in UserService", filePath: "Services/UserService.cs" ); // Check for similar past issues var similarIssues = await unifiedContextService.FindSimilarPastIssuesAsync( "database query performance UserService" ); if (similarIssues.Any(i => i.Tags.Contains("successful"))) { Console.WriteLine("Found successful patterns from previous attempts!"); } // Store refactoring decision for future learning await unifiedContextService.StoreRefactoringDecisionAsync( decision: "Applied query caching pattern", reasoning: "Reduced database calls by 80% with minimal complexity", filePath: "Services/UserService.cs", successful: true ); ``` ### Custom Learning Mode ```csharp var customSession = new ComprehensiveLearningSession { SessionId = Guid.NewGuid(), SolutionPath = @"C:\MyProject\Solution.sln", LearningMode = "aggressive", MaxIterations = 30, EnableSemanticSearch = true, OpenAIApiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY"), SessionTimeoutMinutes = 120, VerboseReporting = true }; using var orchestrator = serviceProvider.GetRequiredService(); var result = await orchestrator.ExecuteCompleteLearningSessionAsync(customSession); ``` ## ๐Ÿ“Š Monitoring and Observability ### Structured Logging All operations include correlation IDs for tracing: ```csharp _logger.LogInformation( "๐Ÿš€ Starting learning session for: {ProjectName} [CorrelationId: {CorrelationId}]", projectName, correlationId); ``` ### Performance Metrics Track operation performance: ```csharp var result = await orchestrator.ExecuteCompleteLearningSessionAsync(session); Console.WriteLine($"Session Duration: {result.TotalDuration.TotalMinutes:F1} minutes"); Console.WriteLine($"Successful Iterations: {result.Iterations.Count(i => i.Success)}"); Console.WriteLine($"Failed Attempts: {result.FailedAttempts.Count}"); Console.WriteLine($"AI Features Enabled: {result.AIFeaturesEnabled}"); ``` ## ๐Ÿงช Testing ### Unit Tests ```csharp [Test] public async Task UnifiedContextService_Should_CombineRealTimeAndHistorical() { // Arrange var mockLLMService = new Mock(); var service = new UnifiedContextService(mockLLMService.Object, options, logger); // Act var context = await service.PrepareFullContextAsync("test query"); // Assert Assert.That(context.CurrentCodeAnalysis, Is.Not.Null); Assert.That(context.HistoricalInsights, Is.Not.Empty); } ``` ### Integration Tests ```csharp [Test] public async Task LearningOrchestrator_Should_ExecuteCompleteSession() { // Arrange var session = CreateTestSession(); // Act var result = await orchestrator.ExecuteCompleteLearningSessionAsync(session); // Assert Assert.That(result.Success, Is.True); Assert.That(result.Iterations, Is.Not.Empty); } ``` ## ๐Ÿ›ก๏ธ Security Considerations ### Path Validation ```csharp // Automatically validates all file paths if (!securityService.IsPathSafe(filePath)) { throw new SecurityException("Path validation failed"); } ``` ### Input Sanitization ```csharp // All user inputs are sanitized var sanitizedQuery = securityService.SanitizeInput(userQuery); ``` ### File Access Control ```csharp // Only approved file types are processed var allowedExtensions = new[] { ".cs", ".csproj", ".sln" }; ``` ## ๐Ÿš€ Performance Optimizations ### Context Caching - LLM context results are cached to avoid redundant computation - Cache keys based on query, file path, and token limits - Thread-safe concurrent dictionary implementation ### Resource Management - Proper IDisposable patterns throughout - Using statements for automatic cleanup - Service provider disposal handling ### Thread Safety - ConcurrentDictionary for file attempt tracking - Thread-safe collections for multi-threaded scenarios - Correlation ID tracking across async operations ## ๐Ÿ“ˆ Metrics and Analytics ### Session Metrics - Total duration and iteration count - Success/failure ratios - AI feature utilization - Resource consumption patterns ### Learning Analytics - Historical pattern recognition - Decision success rates - Failure pattern analysis - Organizational knowledge growth ## ๐Ÿ”ฎ Roadmap ### Upcoming Features - [ ] **Health Checks**: Service monitoring and diagnostics - [ ] **Retry Policies**: Resilient operation handling - [ ] **Event Sourcing**: Complete audit trail capability - [ ] **ML Model Integration**: Custom learning models - [ ] **Real-time Collaboration**: Multi-developer session support ### Integration Enhancements - [ ] **IDE Extensions**: Visual Studio and VS Code plugins - [ ] **CI/CD Integration**: GitHub Actions and Azure DevOps - [ ] **Cloud Services**: Azure/AWS deployment options - [ ] **Enterprise SSO**: Active Directory integration ## ๐Ÿค Contributing We welcome contributions! Please see our [Contributing Guidelines](CONTRIBUTING.md) for details. ### Development Setup 1. **Prerequisites** - .NET 8.0 SDK - Git - Visual Studio 2022 or VS Code 2. **Development Environment** ```bash git clone https://github.com/your-org/MarketAlly.AIPlugin.git cd MarketAlly.AIPlugin/MarketAlly.AIPlugin.Learning dotnet restore dotnet build dotnet test ``` 3. **Code Standards** - Follow C# coding conventions - Include comprehensive XML documentation - Add unit tests for new features - Update documentation for API changes ## ๐Ÿ“„ License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. ## ๐Ÿ™ Acknowledgments - **RefactorIQ**: Advanced code analysis capabilities - **LibGit2Sharp**: Git operations integration - **Microsoft.CodeAnalysis**: Roslyn compiler platform - **OpenAI**: AI embeddings and semantic search ## ๐Ÿ“ž Support - **Documentation**: [API Reference](API_REFERENCE.md) - **Issues**: [GitHub Issues](https://github.com/your-org/MarketAlly.AIPlugin/issues) - **Discussions**: [GitHub Discussions](https://github.com/your-org/MarketAlly.AIPlugin/discussions) --- **Built with โค๏ธ by the MarketAlly Team** *Revolutionizing AI-assisted development through intelligent learning and historical memory.*