Skip to main content

Module ai_contract_diff

Module ai_contract_diff 

Source
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§

CapturedRequest
Captured request for contract analysis
ContractDiffAnalyzer
Main contract diff analyzer that orchestrates all components
ContractDiffConfig
Configuration for contract diff analysis
ContractDiffResult
Result of contract diff analysis between a request and a contract specification
CorrectionProposal
Correction proposal with patch operations
DiffMetadata
Metadata about the diff analysis
Mismatch
A detected mismatch between request and contract
Recommendation
AI-generated recommendation for fixing a mismatch
SemanticDriftResult
Semantic drift analysis result

Enums§

ConfidenceLevel
Confidence level categories for visual indicators
MismatchSeverity
Severity levels for mismatches
MismatchType
Types of mismatches that can be detected
PatchOperation
JSON Patch operation types (RFC 6902)
SemanticChangeType
Type of semantic change