Skip to main content

Module ai_contract_diff

Module ai_contract_diff 

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

CapturedRequest
Captured request for contract analysis
ConfidenceScorer
Confidence scorer for contract diff recommendations
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
CorrectionProposer
Correction proposal generator
DiffAnalyzer
Contract diff analyzer
DiffMetadata
Metadata about the diff analysis
Mismatch
A detected mismatch between request and contract
Recommendation
AI-generated recommendation for fixing a mismatch
RecommendationEngine
AI-powered recommendation engine
RequestContext
Context for recommendation generation
ScoringContext
Context for confidence scoring
SemanticAnalyzer
Semantic analyzer for detecting meaning changes
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