Expand description
Pillars: [Contracts][AI]
AI-powered contract diff analysis
This module provides intelligent contract diff analysis that compares front-end requests against backend API contract specifications, detects mismatches, and generates AI-powered recommendations and correction proposals.
§Features
- Structural Diff Analysis: Detects mismatches between requests and contract specs
- AI-Powered Recommendations: Uses LLM to generate contextual recommendations
- Correction Proposals: Generates JSON Patch files for schema corrections
- Confidence Scoring: Provides confidence scores for all suggestions
§Example Usage
ⓘ
use mockforge_core::ai_contract_diff::{
ContractDiffAnalyzer, ContractDiffConfig, CapturedRequest,
};
use mockforge_core::openapi::OpenApiSpec;
async fn example() -> mockforge_core::Result<()> {
// Load contract specification
let spec = OpenApiSpec::from_file("api.yaml").await?;
// Configure contract diff
let config = ContractDiffConfig {
enabled: true,
llm_provider: "openai".to_string(),
llm_model: "gpt-4".to_string(),
confidence_threshold: 0.5,
..Default::default()
};
// Create analyzer
let analyzer = ContractDiffAnalyzer::new(config)?;
// Capture a request
let request = CapturedRequest::new("POST", "/api/users", "browser_extension")
.with_body(serde_json::json!({"name": "Alice", "email": "alice@example.com"}));
// Analyze request against contract
let result = analyzer.analyze(request, &spec).await?;
// Check results
if !result.matches {
println!("Found {} mismatches", result.mismatches.len());
for mismatch in &result.mismatches {
println!(" - {}: {}", mismatch.path, mismatch.description);
}
// Generate recommendations
for recommendation in &result.recommendations {
println!(" Recommendation: {}", recommendation.recommendation);
}
// Generate correction proposals
for correction in &result.corrections {
println!(" Correction: {}", correction.description);
}
}
Ok(())
}Re-exports§
pub use confidence_scorer::ConfidenceScorer;pub use confidence_scorer::ScoringContext;pub use correction_proposer::CorrectionProposer;pub use diff_analyzer::DiffAnalyzer;pub use recommendation_engine::RecommendationEngine;pub use recommendation_engine::RequestContext;pub use semantic_analyzer::SemanticAnalyzer;
Modules§
- confidence_
scorer - Confidence scoring system for AI contract diff recommendations
- correction_
proposer - Schema correction proposal generator
- diff_
analyzer - Core diff analysis engine for contract comparison
- recommendation_
engine - AI-powered recommendation engine for contract diff analysis
- semantic_
analyzer - Semantic drift analysis for contract diffs
- types
- Re-exported from mockforge_foundation::contract_diff_types (Phase 6 / A5).
Structs§
- Captured
Request - Captured request for contract analysis
- Contract
Diff Analyzer - Main contract diff analyzer that orchestrates all components
- Contract
Diff Config - Configuration for contract diff analysis
- Contract
Diff Result - Result of contract diff analysis between a request and a contract specification
- Correction
Proposal - Correction proposal with patch operations
- Diff
Metadata - Metadata about the diff analysis
- Mismatch
- A detected mismatch between request and contract
- Recommendation
- AI-generated recommendation for fixing a mismatch
- Semantic
Drift Result - Semantic drift analysis result
Enums§
- Confidence
Level - Confidence level categories for visual indicators
- Mismatch
Severity - Severity levels for mismatches
- Mismatch
Type - Types of mismatches that can be detected
- Patch
Operation - JSON Patch operation types (RFC 6902)
- Semantic
Change Type - Type of semantic change