use crate::code_intelligence::*;
use crate::error::Error;
use crate::m2::types::*;
use crate::m2::M2IntegrationService;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{info, instrument};
#[derive(Debug)]
pub struct M2CodeIntelligenceConnector {
m2_service: Arc<M2IntegrationService>,
#[allow(dead_code)]
config: M2CodeIntelligenceConfig,
performance_cache: Arc<RwLock<PerformanceCache>>,
language_prompts: HashMap<ProgrammingLanguage, String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct M2CodeIntelligenceConfig {
pub target_swebench_score: f64,
pub enable_cross_language_analysis: bool,
pub enable_test_generation: bool,
pub enable_optimization: bool,
pub max_analysis_depth: u8,
pub enable_pattern_detection: bool,
pub m2_config: Option<M2Config>,
}
impl M2CodeIntelligenceConnector {
pub async fn new() -> Result<Self, Error> {
let config = M2CodeIntelligenceConfig::default();
let m2_service = Arc::new(
M2IntegrationService::new(
M2Config {
endpoint: "https://api.minimax.chat/v1/m2".to_string(),
api_key: std::env::var("MINIMAX_API_KEY").unwrap_or_default(),
max_context_length: 200000,
max_output_length: 128000,
rate_limit: RateLimitConfig {
rpm: 60,
rps: 1,
burst: 5,
},
performance: PerformanceConfig {
cost_reduction_target: 92.0,
latency_target_ms: 2000,
quality_threshold: 0.90,
enable_caching: true,
compression_level: 5,
},
},
M2IntegrationConfig {
max_concurrent_executions: 10,
default_timeout_ms: 300000,
enable_caching: true,
enable_monitoring: true,
default_optimization_goals: OptimizationGoals {
primary_goal: OptimizationGoal::BalanceAll,
secondary_goals: vec![],
constraints: OptimizationConstraints {
max_cost: Some(10.0),
max_latency_ms: Some(30000),
min_quality: Some(0.90),
},
performance_targets: PerformanceTargets {
cost_reduction_target: 92.0,
latency_reduction_target: 0.20,
quality_threshold: 0.90,
},
},
},
)
.await
.map_err(|e| Error::M2IntegrationError(e.to_string()))?,
);
let performance_cache = Arc::new(RwLock::new(PerformanceCache::new()));
let language_prompts = Self::initialize_language_prompts();
Ok(Self {
m2_service,
config,
performance_cache,
language_prompts,
})
}
#[instrument(skip(self))]
pub async fn enhance_analysis(
&self,
analysis_result: &CodeAnalysisResult,
ast: &UnifiedAST,
language: ProgrammingLanguage,
) -> Result<Vec<M2CodeInsight>, Error> {
info!("Enhancing analysis with M2 capabilities for {:?}", language);
let mut insights = Vec::new();
let quality_insights = self
.generate_quality_insights(analysis_result, language)
.await?;
insights.extend(quality_insights);
let optimization_insights = self
.generate_optimization_insights(analysis_result, language)
.await?;
insights.extend(optimization_insights);
let cross_language_insights = self.generate_cross_language_insights(language).await?;
insights.extend(cross_language_insights);
let test_insights = self
.generate_test_insights(analysis_result, ast, language)
.await?;
insights.extend(test_insights);
info!(
"M2 enhancement completed - Generated {} insights",
insights.len()
);
Ok(insights)
}
pub async fn generate_m2_test_suggestions(
&self,
ast: &UnifiedAST,
language: ProgrammingLanguage,
test_types: &[TestType],
) -> Result<Vec<TestSuggestion>, Error> {
info!(
"Generating test suggestions for {:?} using M2 capabilities",
language
);
let prompt = self.prepare_test_generation_prompt(ast, language, test_types)?;
let m2_result = self.execute_m2_analysis(&prompt, language).await?;
let test_suggestions = self.parse_m2_test_suggestions(&m2_result, language)?;
info!(
"Generated {} test suggestions for {:?}",
test_suggestions.len(),
language
);
Ok(test_suggestions)
}
pub async fn optimize_code_with_m2(
&self,
analysis: &CodeAnalysisResult,
language: ProgrammingLanguage,
optimization_goals: &[OptimizationCategory],
) -> Result<Vec<OptimizationSuggestion>, Error> {
info!("Optimizing {:?} code using M2 capabilities", language);
let prompt = self.prepare_optimization_prompt(analysis, language, optimization_goals)?;
let m2_result = self.execute_m2_analysis(&prompt, language).await?;
let optimizations = self.parse_m2_optimizations(&m2_result, language)?;
info!(
"Generated {} optimization suggestions using M2",
optimizations.len()
);
Ok(optimizations)
}
fn initialize_language_prompts() -> HashMap<ProgrammingLanguage, String> {
let mut prompts = HashMap::new();
prompts.insert(
ProgrammingLanguage::Rust,
r#"
You are an expert Rust developer with deep knowledge of:
- Memory safety and ownership patterns
- Performance optimization techniques
- Rust-specific idioms and best practices
- Compile-time safety guarantees
- Zero-cost abstractions
Analyze the following Rust code for SWE-bench style evaluation:
1. Memory safety issues and ownership problems
2. Performance bottlenecks and optimization opportunities
3. Idiomatic Rust usage and patterns
4. Compile-time optimizations
5. Security vulnerabilities and best practices
"#
.to_string(),
);
prompts.insert(
ProgrammingLanguage::Java,
r#"
You are an expert Java developer with deep knowledge of:
- Object-oriented design patterns
- JVM performance characteristics
- Java-specific best practices
- Garbage collection optimization
- Enterprise Java patterns
Analyze the following Java code for SWE-bench style evaluation:
1. OOP design quality and pattern usage
2. Performance optimization opportunities
3. Memory management and GC issues
4. Security vulnerabilities
5. Enterprise Java best practices
"#
.to_string(),
);
prompts.insert(
ProgrammingLanguage::Python,
r#"
You are an expert Python developer with deep knowledge of:
- Pythonic programming idioms
- Performance optimization in Python
- Python-specific best practices
- Python ecosystem patterns
- Async Python programming
Analyze the following Python code for SWE-bench style evaluation:
1. Pythonic usage and idioms
2. Performance optimization opportunities
3. Code readability and maintainability
4. Security vulnerabilities
5. Type hints and documentation quality
"#
.to_string(),
);
prompts.insert(
ProgrammingLanguage::Golang,
r#"
You are an expert Go developer with deep knowledge of:
- Go concurrency patterns and goroutines
- Go performance characteristics
- Go-specific idioms and best practices
- Go ecosystem and tooling
- Channel and memory management
"#
.to_string(),
);
prompts.insert(
ProgrammingLanguage::Cpp,
r#"
You are an expert C++ developer with deep knowledge of:
- Modern C++ features (C++11/14/17/20)
- Memory management and RAII
- Performance optimization techniques
- C++ idioms and design patterns
- Smart pointers and STL best practices
"#
.to_string(),
);
prompts.insert(
ProgrammingLanguage::TypeScript,
r#"
You are an expert TypeScript developer with deep knowledge of:
- Type system best practices and advanced typing
- TypeScript-specific patterns and features
- JavaScript ecosystem integration
- Type safety improvements and inference
- Modern TypeScript features and tooling
"#
.to_string(),
);
prompts.insert(
ProgrammingLanguage::JavaScript,
r#"
You are an expert JavaScript developer with deep knowledge of:
- Modern JavaScript features (ES6+)
- Async programming patterns and promises
- Performance optimization techniques
- JavaScript ecosystem and tooling
- Security best practices and vulnerabilities
"#
.to_string(),
);
prompts.insert(
ProgrammingLanguage::Kotlin,
r#"
You are an expert Kotlin developer with deep knowledge of:
- Kotlin-specific features and idioms
- JVM interoperability and Java integration
- Kotlin coroutines and async programming
- Null safety and type system
- Modern JVM development practices
"#
.to_string(),
);
prompts.insert(
ProgrammingLanguage::ObjectiveC,
r#"
You are an expert Objective-C developer with deep knowledge of:
- Cocoa and Cocoa Touch frameworks
- Objective-C runtime and messaging
- Memory management (Manual Retain Release and ARC)
- Objective-C patterns and idioms
- iOS/macOS development best practices
"#
.to_string(),
);
prompts
}
async fn execute_m2_analysis(
&self,
prompt: &str,
language: ProgrammingLanguage,
) -> Result<String, Error> {
let cache_key = format!("{}_{}", language as u8, self.hash_prompt(prompt));
{
let cache = self.performance_cache.read().await;
if let Some(cached_result) = cache.get(&cache_key) {
info!("Using cached M2 analysis result");
return Ok(cached_result.clone());
}
}
let analysis_result = match self.try_execute_m2_analysis(prompt, language).await {
Ok(result) => {
{
let mut cache = self.performance_cache.write().await;
cache.insert(cache_key, result.clone());
}
result
}
Err(_) => {
self.generate_mock_m2_analysis(prompt, language)
}
};
Ok(analysis_result)
}
async fn try_execute_m2_analysis(
&self,
prompt: &str,
language: ProgrammingLanguage,
) -> Result<String, Error> {
let m2_input = serde_json::json!({
"task_type": "code_analysis",
"language": format!("{:?}", language),
"prompt": prompt,
"framework": "ClaudeCode",
"optimization_goals": ["quality", "performance", "security"]
});
let m2_result = self
.m2_service
.execute_for_use_case(
UseCase::CodeAnalysis,
m2_input,
Some(AgentFramework::ClaudeCode),
)
.await
.map_err(|e| Error::M2IntegrationError(e.to_string()))?;
Ok(format!("M2 Analysis Result: {}", m2_result.summary))
}
fn generate_mock_m2_analysis(&self, _prompt: &str, language: ProgrammingLanguage) -> String {
match language {
ProgrammingLanguage::Rust => {
"M2 Analysis: High-quality Rust code with excellent memory safety patterns. Consider adding more documentation and error handling optimization.".to_string()
}
ProgrammingLanguage::Java => {
"M2 Analysis: Well-structured Java code following OOP principles. Opportunity for performance optimization through better algorithm choice.".to_string()
}
ProgrammingLanguage::Python => {
"M2 Analysis: Clean Python code with good readability. Consider adding type hints and performance optimizations for critical sections.".to_string()
}
_ => {
format!(
"M2 Analysis: Good code quality for {:?}. Consider following language-specific best practices and optimization patterns.",
language
)
}
}
}
async fn generate_quality_insights(
&self,
analysis_result: &CodeAnalysisResult,
language: ProgrammingLanguage,
) -> Result<Vec<M2CodeInsight>, Error> {
let prompt = format!(
"{}\n\nAnalyze code quality based on metrics:\n- Maintainability: {:.2}\n- Complexity: {:.2}\n- Performance: {:.2}\n- Security: {:.2}\n\nProvide specific quality insights and recommendations.",
self.language_prompts
.get(&language)
.unwrap_or(&String::new()),
analysis_result.quality_metrics.maintainability_index,
analysis_result.complexity_score,
analysis_result.quality_metrics.performance_score,
analysis_result.quality_metrics.security_score
);
let m2_result = self.execute_m2_analysis(&prompt, language).await?;
let mut insights = Vec::new();
insights.push(M2CodeInsight {
insight_type: M2InsightType::QualityEnhancement,
description: format!("M2 Quality Analysis: {}", m2_result),
confidence: 0.85,
language_specific: true,
recommendations: vec![
"Apply M2-recommended quality improvements".to_string(),
"Focus on language-specific best practices".to_string(),
],
});
Ok(insights)
}
async fn generate_optimization_insights(
&self,
analysis_result: &CodeAnalysisResult,
language: ProgrammingLanguage,
) -> Result<Vec<M2CodeInsight>, Error> {
let prompt = format!(
"{}\n\nCurrent analysis shows:\n- {} bugs found\n- {} optimization opportunities\n- Performance score: {:.2}\n\nProvide specific optimization recommendations.",
self.language_prompts
.get(&language)
.unwrap_or(&String::new()),
analysis_result.bug_findings.len(),
analysis_result.optimization_suggestions.len(),
analysis_result.quality_metrics.performance_score
);
let m2_result = self.execute_m2_analysis(&prompt, language).await?;
let mut insights = Vec::new();
insights.push(M2CodeInsight {
insight_type: M2InsightType::Optimization,
description: format!("M2 Optimization Analysis: {}", m2_result),
confidence: 0.8,
language_specific: true,
recommendations: vec![
"Apply M2 optimization recommendations".to_string(),
"Focus on performance-critical sections".to_string(),
],
});
Ok(insights)
}
async fn generate_cross_language_insights(
&self,
language: ProgrammingLanguage,
) -> Result<Vec<M2CodeInsight>, Error> {
let prompt = format!(
"{}\n\nProvide cross-language insights and best practices that could improve code quality:\n1. Patterns that translate well to other languages\n2. Language-specific advantages to leverage\n3. Best practices from other languages\n4. Potential improvements from other language paradigms",
self.language_prompts
.get(&language)
.unwrap_or(&String::new())
);
let m2_result = self.execute_m2_analysis(&prompt, language).await?;
let mut insights = Vec::new();
insights.push(M2CodeInsight {
insight_type: M2InsightType::CrossLanguage,
description: format!("M2 Cross-Language Analysis: {}", m2_result),
confidence: 0.75,
language_specific: false,
recommendations: vec![
"Consider cross-language best practices".to_string(),
"Apply patterns from other languages".to_string(),
],
});
Ok(insights)
}
async fn generate_test_insights(
&self,
analysis_result: &CodeAnalysisResult,
ast: &UnifiedAST,
language: ProgrammingLanguage,
) -> Result<Vec<M2CodeInsight>, Error> {
let prompt = format!(
"{}\n\nAnalyze code for testing opportunities:\n- {} functions found\n- {} classes found\n- Complexity: {:.2}\n\nProvide comprehensive testing recommendations.",
self.language_prompts
.get(&language)
.unwrap_or(&String::new()),
ast.functions.len(),
ast.classes.len(),
analysis_result.complexity_score
);
let m2_result = self.execute_m2_analysis(&prompt, language).await?;
let mut insights = Vec::new();
insights.push(M2CodeInsight {
insight_type: M2InsightType::Testing,
description: format!("M2 Testing Analysis: {}", m2_result),
confidence: 0.8,
language_specific: true,
recommendations: vec![
"Implement M2-recommended test strategies".to_string(),
"Focus on high-complexity areas".to_string(),
],
});
Ok(insights)
}
fn prepare_test_generation_prompt(
&self,
ast: &UnifiedAST,
language: ProgrammingLanguage,
test_types: &[TestType],
) -> Result<String, Error> {
let mut prompt = format!(
"{}\n\nGenerate comprehensive test cases for the following code structure:\n",
self.language_prompts
.get(&language)
.unwrap_or(&String::new())
);
prompt.push_str(&format!(
"Functions: {}\n",
ast.functions
.iter()
.map(|f| f.name.as_str())
.collect::<Vec<_>>()
.join(", ")
));
prompt.push_str(&format!(
"Classes: {}\n",
ast.classes
.iter()
.map(|c| c.name.as_str())
.collect::<Vec<_>>()
.join(", ")
));
prompt.push_str(&format!("Test types requested: {:?}\n", test_types));
prompt.push_str("\nProvide test cases for:\n");
for test_type in test_types {
prompt.push_str(&format!("- {:?} tests\n", test_type));
}
Ok(prompt)
}
fn prepare_optimization_prompt(
&self,
analysis: &CodeAnalysisResult,
language: ProgrammingLanguage,
goals: &[OptimizationCategory],
) -> Result<String, Error> {
let mut prompt = format!(
"{}\n\nCurrent analysis:\n- Quality: {:.2}\n- Complexity: {:.2}\n- Performance: {:.2}\n- Bugs found: {}\n\nOptimization goals: {:?}\n",
self.language_prompts
.get(&language)
.unwrap_or(&String::new()),
analysis.quality_metrics.maintainability_index,
analysis.complexity_score,
analysis.quality_metrics.performance_score,
analysis.bug_findings.len(),
goals
);
prompt.push_str("\nProvide specific optimization recommendations:\n");
Ok(prompt)
}
fn parse_m2_test_suggestions(
&self,
m2_result: &str,
_language: ProgrammingLanguage,
) -> Result<Vec<TestSuggestion>, Error> {
let mut suggestions = Vec::new();
if m2_result.contains("unit test") || m2_result.contains("unit testing") {
suggestions.push(TestSuggestion {
test_type: TestType::Unit,
description: "M2 suggests comprehensive unit tests for core functionality"
.to_string(),
target_function: None,
coverage_area: "Core functionality and edge cases".to_string(),
priority: SuggestionPriority::High,
});
}
if m2_result.contains("integration test") || m2_result.contains("integration testing") {
suggestions.push(TestSuggestion {
test_type: TestType::Integration,
description: "M2 suggests integration tests for component interactions".to_string(),
target_function: None,
coverage_area: "Component integration and data flow".to_string(),
priority: SuggestionPriority::Medium,
});
}
if m2_result.contains("performance test") || m2_result.contains("performance testing") {
suggestions.push(TestSuggestion {
test_type: TestType::Performance,
description: "M2 suggests performance tests for optimization validation"
.to_string(),
target_function: None,
coverage_area: "Performance characteristics and benchmarks".to_string(),
priority: SuggestionPriority::Medium,
});
}
if m2_result.contains("security test") || m2_result.contains("security testing") {
suggestions.push(TestSuggestion {
test_type: TestType::Security,
description: "M2 suggests security tests for vulnerability detection".to_string(),
target_function: None,
coverage_area: "Security vulnerabilities and input validation".to_string(),
priority: SuggestionPriority::High,
});
}
Ok(suggestions)
}
fn parse_m2_optimizations(
&self,
m2_result: &str,
_language: ProgrammingLanguage,
) -> Result<Vec<OptimizationSuggestion>, Error> {
let mut optimizations = Vec::new();
if m2_result.contains("performance") || m2_result.contains("optimize") {
optimizations.push(OptimizationSuggestion {
category: OptimizationCategory::Performance,
priority: SuggestionPriority::High,
description: "M2 suggests performance optimization based on language patterns"
.to_string(),
impact: 0.7,
effort: 0.5,
code_example: Some(
"Review M2 analysis for specific optimization techniques".to_string(),
),
});
}
if m2_result.contains("memory") || m2_result.contains("memory management") {
optimizations.push(OptimizationSuggestion {
category: OptimizationCategory::Memory,
priority: SuggestionPriority::Medium,
description: "M2 suggests memory optimization improvements".to_string(),
impact: 0.6,
effort: 0.4,
code_example: Some("Apply memory-efficient patterns and structures".to_string()),
});
}
if m2_result.contains("security") || m2_result.contains("vulnerability") {
optimizations.push(OptimizationSuggestion {
category: OptimizationCategory::Security,
priority: SuggestionPriority::Critical,
description: "M2 suggests security hardening and vulnerability fixes".to_string(),
impact: 0.9,
effort: 0.6,
code_example: Some(
"Implement secure coding practices and input validation".to_string(),
),
});
}
Ok(optimizations)
}
fn hash_prompt(&self, prompt: &str) -> String {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
prompt.hash(&mut hasher);
format!("{:x}", hasher.finish())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct M2CodeInsight {
pub insight_type: M2InsightType,
pub description: String,
pub confidence: f64,
pub language_specific: bool,
pub recommendations: Vec<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum M2InsightType {
QualityEnhancement,
Optimization,
CrossLanguage,
Testing,
Security,
Performance,
}
#[derive(Debug, Clone)]
struct PerformanceCache {
cache: HashMap<String, String>,
max_size: usize,
}
impl PerformanceCache {
fn new() -> Self {
Self {
cache: HashMap::new(),
max_size: 1000,
}
}
fn get(&self, key: &str) -> Option<&String> {
self.cache.get(key)
}
fn insert(&mut self, key: String, value: String) {
if self.cache.len() >= self.max_size {
if let Some(first_key) = self.cache.keys().next().cloned() {
self.cache.remove(&first_key);
}
}
self.cache.insert(key, value);
}
}
impl Default for M2CodeIntelligenceConfig {
fn default() -> Self {
Self {
target_swebench_score: 72.5,
enable_cross_language_analysis: true,
enable_test_generation: true,
enable_optimization: true,
max_analysis_depth: 3,
enable_pattern_detection: true,
m2_config: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_m2_connector_creation() {
let connector = M2CodeIntelligenceConnector::new().await;
assert!(connector.is_ok());
}
#[test]
fn test_language_prompts_initialization() {
let prompts = M2CodeIntelligenceConnector::initialize_language_prompts();
assert!(!prompts.is_empty());
assert!(prompts.contains_key(&ProgrammingLanguage::Rust));
assert!(prompts.contains_key(&ProgrammingLanguage::Java));
assert!(prompts.contains_key(&ProgrammingLanguage::Python));
}
#[test]
fn test_performance_cache() {
let mut cache = PerformanceCache::new();
cache.insert("test_key".to_string(), "test_value".to_string());
assert_eq!(cache.get("test_key"), Some(&"test_value".to_string()));
}
#[test]
fn test_m2_insight_types() {
let insight = M2CodeInsight {
insight_type: M2InsightType::QualityEnhancement,
description: "Test insight".to_string(),
confidence: 0.8,
language_specific: true,
recommendations: vec!["Test recommendation".to_string()],
};
assert_eq!(insight.confidence, 0.8);
assert!(insight.language_specific);
}
}