Expand description
ai_contract_diff lives in mockforge_intelligence::ai_contract_diff
(Issue #562 phase 4). Re-exported here so existing
crate::ai_contract_diff::* call sites inside core (ai_studio /
contract_diff_handler, request_capture) and external
mockforge_core::ai_contract_diff::* consumers (mockforge-http,
mockforge-cli) keep compiling unchanged.
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(())
}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
- Confidence
Scorer - Confidence scorer for contract diff recommendations
- 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
- Correction
Proposer - Correction proposal generator
- Diff
Analyzer - Contract diff analyzer
- Diff
Metadata - Metadata about the diff analysis
- Mismatch
- A detected mismatch between request and contract
- Recommendation
- AI-generated recommendation for fixing a mismatch
- Recommendation
Engine - AI-powered recommendation engine
- Request
Context - Context for recommendation generation
- Scoring
Context - Context for confidence scoring
- Semantic
Analyzer - Semantic analyzer for detecting meaning changes
- 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