MarketAlly.AIPlugin.Extensions/MarketAlly.AIPlugin.Learning/README.md

442 lines
13 KiB
Markdown
Executable File

# 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<string, object>
{
["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<IUnifiedContextService, UnifiedContextService>();
var provider = services.BuildServiceProvider();
var contextService = provider.GetService<IUnifiedContextService>();
// 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<ComprehensiveLearningResult> ExecuteCompleteLearningSessionAsync(
ComprehensiveLearningSession session);
}
```
### UnifiedContextService
Revolutionary service combining real-time and historical intelligence.
```csharp
public interface IUnifiedContextService
{
Task<ComprehensiveContext> PrepareFullContextAsync(string query, string? filePath = null, int maxTokens = 8000);
Task<LearningSessionContext> InitializeLearningSessionAsync(string projectPath, string topic);
Task StoreLearningInsightAsync(string insight, string category, string? filePath = null);
Task<List<HistoricalInsight>> 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<LLMContext> PrepareContextAsync(string query, int maxTokens = 4000);
Task<LLMContext> PrepareCodeAnalysisContextAsync(string filePath, string query, int maxTokens = 4000);
Task<DependencyContext> GetDependencyContextAsync(string symbolName);
Task<ChangeImpactContext> 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<ILearningOrchestrator>();
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<ILLMContextService>();
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.*