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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Agent 3: Difference Analyzer (Hybrid Approach)
|
| 3 |
+
Uses HF Vision Model + Screenshot Analysis to detect visual differences
|
| 4 |
+
Detects layout, color, spacing, typography, and other visual differences
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from typing import Dict, Any, List
|
| 8 |
+
from state_schema import WorkflowState, VisualDifference
|
| 9 |
+
import os
|
| 10 |
+
from PIL import Image
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class ScreenshotComparator:
|
| 15 |
+
"""Compares Figma and website screenshots for visual differences."""
|
| 16 |
+
|
| 17 |
+
def __init__(self):
|
| 18 |
+
"""Initialize the comparator."""
|
| 19 |
+
self.differences = []
|
| 20 |
+
|
| 21 |
+
def compare_screenshots(self, figma_path: str, website_path: str, viewport: str) -> List[VisualDifference]:
|
| 22 |
+
"""
|
| 23 |
+
Compare Figma and website screenshots.
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
figma_path: Path to Figma screenshot
|
| 27 |
+
website_path: Path to website screenshot
|
| 28 |
+
viewport: Viewport name (desktop/mobile)
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
List of detected visual differences
|
| 32 |
+
"""
|
| 33 |
+
differences = []
|
| 34 |
+
|
| 35 |
+
if not os.path.exists(figma_path) or not os.path.exists(website_path):
|
| 36 |
+
return differences
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
# Load images
|
| 40 |
+
figma_img = Image.open(figma_path)
|
| 41 |
+
website_img = Image.open(website_path)
|
| 42 |
+
|
| 43 |
+
# Analyze different aspects
|
| 44 |
+
differences.extend(self._analyze_layout(figma_img, website_img, viewport))
|
| 45 |
+
differences.extend(self._analyze_colors(figma_img, website_img, viewport))
|
| 46 |
+
differences.extend(self._analyze_structure(figma_img, website_img, viewport))
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Error comparing screenshots: {str(e)}")
|
| 50 |
+
|
| 51 |
+
return differences
|
| 52 |
+
|
| 53 |
+
def _analyze_layout(self, figma_img: Image.Image, website_img: Image.Image, viewport: str) -> List[VisualDifference]:
|
| 54 |
+
"""Analyze layout differences."""
|
| 55 |
+
differences = []
|
| 56 |
+
|
| 57 |
+
figma_size = figma_img.size
|
| 58 |
+
website_size = website_img.size
|
| 59 |
+
|
| 60 |
+
# Check width differences
|
| 61 |
+
if figma_size[0] != website_size[0]:
|
| 62 |
+
width_diff_percent = abs(figma_size[0] - website_size[0]) / figma_size[0] * 100
|
| 63 |
+
severity = "High" if width_diff_percent > 10 else "Medium"
|
| 64 |
+
|
| 65 |
+
diff = VisualDifference(
|
| 66 |
+
category="layout",
|
| 67 |
+
severity=severity,
|
| 68 |
+
issue_id="1.1",
|
| 69 |
+
title="Container width differs",
|
| 70 |
+
description=f"Design: {figma_size[0]}px vs Website: {website_size[0]}px ({width_diff_percent:.1f}% difference)",
|
| 71 |
+
design_value=str(figma_size[0]),
|
| 72 |
+
website_value=str(website_size[0]),
|
| 73 |
+
viewport=viewport,
|
| 74 |
+
confidence=0.95,
|
| 75 |
+
detection_method="screenshot_comparison"
|
| 76 |
+
)
|
| 77 |
+
differences.append(diff)
|
| 78 |
+
|
| 79 |
+
# Check height differences
|
| 80 |
+
if figma_size[1] != website_size[1]:
|
| 81 |
+
height_diff_percent = abs(figma_size[1] - website_size[1]) / figma_size[1] * 100
|
| 82 |
+
severity = "High" if height_diff_percent > 10 else "Medium"
|
| 83 |
+
|
| 84 |
+
diff = VisualDifference(
|
| 85 |
+
category="layout",
|
| 86 |
+
severity=severity,
|
| 87 |
+
issue_id="1.2",
|
| 88 |
+
title="Page height differs",
|
| 89 |
+
description=f"Design: {figma_size[1]}px vs Website: {website_size[1]}px ({height_diff_percent:.1f}% difference)",
|
| 90 |
+
design_value=str(figma_size[1]),
|
| 91 |
+
website_value=str(website_size[1]),
|
| 92 |
+
viewport=viewport,
|
| 93 |
+
confidence=0.95,
|
| 94 |
+
detection_method="screenshot_comparison"
|
| 95 |
+
)
|
| 96 |
+
differences.append(diff)
|
| 97 |
+
|
| 98 |
+
return differences
|
| 99 |
+
|
| 100 |
+
def _analyze_colors(self, figma_img: Image.Image, website_img: Image.Image, viewport: str) -> List[VisualDifference]:
|
| 101 |
+
"""Analyze color differences."""
|
| 102 |
+
differences = []
|
| 103 |
+
|
| 104 |
+
try:
|
| 105 |
+
# Convert to RGB
|
| 106 |
+
figma_rgb = figma_img.convert('RGB')
|
| 107 |
+
website_rgb = website_img.convert('RGB')
|
| 108 |
+
|
| 109 |
+
# Resize to same size for comparison (use smaller size for performance)
|
| 110 |
+
compare_size = (400, 300)
|
| 111 |
+
figma_resized = figma_rgb.resize(compare_size)
|
| 112 |
+
website_resized = website_rgb.resize(compare_size)
|
| 113 |
+
|
| 114 |
+
# Convert to numpy arrays
|
| 115 |
+
figma_array = np.array(figma_resized, dtype=np.float32)
|
| 116 |
+
website_array = np.array(website_resized, dtype=np.float32)
|
| 117 |
+
|
| 118 |
+
# Calculate color difference (mean absolute difference)
|
| 119 |
+
color_diff = np.mean(np.abs(figma_array - website_array))
|
| 120 |
+
|
| 121 |
+
# If significant color difference, flag it
|
| 122 |
+
if color_diff > 15:
|
| 123 |
+
severity = "High" if color_diff > 40 else "Medium"
|
| 124 |
+
|
| 125 |
+
diff = VisualDifference(
|
| 126 |
+
category="colors",
|
| 127 |
+
severity=severity,
|
| 128 |
+
issue_id="3.1",
|
| 129 |
+
title="Color scheme differs significantly",
|
| 130 |
+
description=f"Significant color difference detected (delta: {color_diff:.1f})",
|
| 131 |
+
design_value="Design colors",
|
| 132 |
+
website_value="Website colors",
|
| 133 |
+
viewport=viewport,
|
| 134 |
+
confidence=0.8,
|
| 135 |
+
detection_method="pixel_analysis"
|
| 136 |
+
)
|
| 137 |
+
differences.append(diff)
|
| 138 |
+
|
| 139 |
+
except Exception as e:
|
| 140 |
+
pass
|
| 141 |
+
|
| 142 |
+
return differences
|
| 143 |
+
|
| 144 |
+
def _analyze_structure(self, figma_img: Image.Image, website_img: Image.Image, viewport: str) -> List[VisualDifference]:
|
| 145 |
+
"""Analyze structural/layout differences."""
|
| 146 |
+
differences = []
|
| 147 |
+
|
| 148 |
+
try:
|
| 149 |
+
# Convert to grayscale for edge detection
|
| 150 |
+
figma_gray = figma_img.convert('L')
|
| 151 |
+
website_gray = website_img.convert('L')
|
| 152 |
+
|
| 153 |
+
# Resize to same size
|
| 154 |
+
compare_size = (400, 300)
|
| 155 |
+
figma_resized = figma_gray.resize(compare_size)
|
| 156 |
+
website_resized = website_gray.resize(compare_size)
|
| 157 |
+
|
| 158 |
+
# Convert to numpy arrays
|
| 159 |
+
figma_array = np.array(figma_resized, dtype=np.float32)
|
| 160 |
+
website_array = np.array(website_resized, dtype=np.float32)
|
| 161 |
+
|
| 162 |
+
# Calculate structural difference (MSE)
|
| 163 |
+
mse = np.mean((figma_array - website_array) ** 2)
|
| 164 |
+
|
| 165 |
+
# Normalize MSE to 0-100 scale
|
| 166 |
+
structural_diff = min(100, mse / 255)
|
| 167 |
+
|
| 168 |
+
if structural_diff > 10:
|
| 169 |
+
severity = "High" if structural_diff > 30 else "Medium"
|
| 170 |
+
|
| 171 |
+
diff = VisualDifference(
|
| 172 |
+
category="layout",
|
| 173 |
+
severity=severity,
|
| 174 |
+
issue_id="1.3",
|
| 175 |
+
title="Layout structure differs",
|
| 176 |
+
description=f"Visual structure difference detected (score: {structural_diff:.1f})",
|
| 177 |
+
design_value="Design layout",
|
| 178 |
+
website_value="Website layout",
|
| 179 |
+
viewport=viewport,
|
| 180 |
+
confidence=0.75,
|
| 181 |
+
detection_method="structural_analysis"
|
| 182 |
+
)
|
| 183 |
+
differences.append(diff)
|
| 184 |
+
|
| 185 |
+
except Exception as e:
|
| 186 |
+
pass
|
| 187 |
+
|
| 188 |
+
return differences
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
class DifferenceAnalyzer:
|
| 192 |
+
"""
|
| 193 |
+
Agent 3: Difference Analyzer
|
| 194 |
+
Analyzes visual differences between Figma designs and website implementations
|
| 195 |
+
"""
|
| 196 |
+
|
| 197 |
+
def __init__(self):
|
| 198 |
+
"""Initialize the analyzer."""
|
| 199 |
+
self.comparator = ScreenshotComparator()
|
| 200 |
+
|
| 201 |
+
def analyze_differences(self, state: WorkflowState) -> WorkflowState:
|
| 202 |
+
"""
|
| 203 |
+
Analyze visual differences between Figma and website screenshots.
|
| 204 |
+
|
| 205 |
+
Args:
|
| 206 |
+
state: Current workflow state
|
| 207 |
+
|
| 208 |
+
Returns:
|
| 209 |
+
Updated state with analysis results
|
| 210 |
+
"""
|
| 211 |
+
print("\nπ Agent 3: Difference Analyzer - Analyzing Visual Differences...")
|
| 212 |
+
|
| 213 |
+
try:
|
| 214 |
+
all_differences = []
|
| 215 |
+
|
| 216 |
+
# Compare screenshots for each viewport
|
| 217 |
+
for viewport in ["desktop", "mobile"]:
|
| 218 |
+
figma_key = f"{viewport}"
|
| 219 |
+
website_key = f"{viewport}"
|
| 220 |
+
|
| 221 |
+
figma_path = state.get("figma_screenshots", {}).get(figma_key)
|
| 222 |
+
website_path = state.get("website_screenshots", {}).get(website_key)
|
| 223 |
+
|
| 224 |
+
if figma_path and website_path:
|
| 225 |
+
print(f" π Comparing {viewport} screenshots...")
|
| 226 |
+
|
| 227 |
+
differences = self.comparator.compare_screenshots(
|
| 228 |
+
figma_path,
|
| 229 |
+
website_path,
|
| 230 |
+
viewport
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
all_differences.extend(differences)
|
| 234 |
+
print(f" β Found {len(differences)} differences")
|
| 235 |
+
else:
|
| 236 |
+
print(f" β οΈ Missing screenshots for {viewport}")
|
| 237 |
+
|
| 238 |
+
# Calculate similarity score
|
| 239 |
+
total_differences = len(all_differences)
|
| 240 |
+
high_severity = len([d for d in all_differences if d.severity == "High"])
|
| 241 |
+
medium_severity = len([d for d in all_differences if d.severity == "Medium"])
|
| 242 |
+
low_severity = len([d for d in all_differences if d.severity == "Low"])
|
| 243 |
+
|
| 244 |
+
# Similarity score: 100 - (differences weighted by severity)
|
| 245 |
+
severity_weight = (high_severity * 10) + (medium_severity * 5) + (low_severity * 1)
|
| 246 |
+
similarity_score = max(0, 100 - severity_weight)
|
| 247 |
+
|
| 248 |
+
state["visual_differences"] = [d.to_dict() if hasattr(d, "to_dict") else d for d in all_differences]
|
| 249 |
+
state["similarity_score"] = similarity_score
|
| 250 |
+
state["status"] = "analysis_complete"
|
| 251 |
+
|
| 252 |
+
print(f"\n π Analysis Summary:")
|
| 253 |
+
print(f" - Total differences: {total_differences}")
|
| 254 |
+
print(f" - High severity: {high_severity}")
|
| 255 |
+
print(f" - Medium severity: {medium_severity}")
|
| 256 |
+
print(f" - Low severity: {low_severity}")
|
| 257 |
+
print(f" - Similarity score: {similarity_score:.1f}/100")
|
| 258 |
+
|
| 259 |
+
return state
|
| 260 |
+
|
| 261 |
+
except Exception as e:
|
| 262 |
+
print(f" β Error analyzing differences: {str(e)}")
|
| 263 |
+
import traceback
|
| 264 |
+
traceback.print_exc()
|
| 265 |
+
state["status"] = "analysis_failed"
|
| 266 |
+
state["error_message"] = f"Agent 3 Error: {str(e)}"
|
| 267 |
+
return state
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
def agent_3_node(state: Dict) -> Dict:
|
| 271 |
+
"""
|
| 272 |
+
LangGraph node for Agent 3 (Difference Analyzer).
|
| 273 |
+
|
| 274 |
+
Args:
|
| 275 |
+
state: Current workflow state
|
| 276 |
+
|
| 277 |
+
Returns:
|
| 278 |
+
Updated state
|
| 279 |
+
"""
|
| 280 |
+
# Convert dict to WorkflowState if needed
|
| 281 |
+
if isinstance(state, dict):
|
| 282 |
+
workflow_state = WorkflowState(**state)
|
| 283 |
+
else:
|
| 284 |
+
workflow_state = state
|
| 285 |
+
|
| 286 |
+
# Create analyzer and analyze differences
|
| 287 |
+
analyzer = DifferenceAnalyzer()
|
| 288 |
+
updated_state = analyzer.analyze_differences(workflow_state)
|
| 289 |
+
|
| 290 |
+
# Convert back to dict for LangGraph
|
| 291 |
+
return updated_state.__dict__
|
agents/agent_3_difference_analyzer_enhanced.py
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Agent 3: Enhanced Difference Analyzer
|
| 3 |
+
Detects visual differences including typography, spacing, components, and layout
|
| 4 |
+
Uses HF vision model + CSS analysis + pixel comparison
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
from typing import Dict, Any, List
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 13 |
+
|
| 14 |
+
from state_schema import WorkflowState, VisualDifference
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class EnhancedDifferenceAnalyzer:
|
| 18 |
+
"""Enhanced analyzer for detecting visual differences"""
|
| 19 |
+
|
| 20 |
+
def __init__(self, hf_token: str = None):
|
| 21 |
+
"""Initialize analyzer with HF token"""
|
| 22 |
+
self.hf_token = hf_token or os.getenv('HUGGINGFACE_API_KEY')
|
| 23 |
+
self.differences: List[VisualDifference] = []
|
| 24 |
+
self.detected_categories = {}
|
| 25 |
+
|
| 26 |
+
def analyze_differences(self, state: WorkflowState) -> WorkflowState:
|
| 27 |
+
"""
|
| 28 |
+
Comprehensive difference analysis
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
state: Current workflow state with screenshots
|
| 32 |
+
|
| 33 |
+
Returns:
|
| 34 |
+
Updated state with detected differences
|
| 35 |
+
"""
|
| 36 |
+
print("\nπ Agent 3: Enhanced Difference Analysis...")
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
self.differences = []
|
| 40 |
+
self.detected_categories = {}
|
| 41 |
+
|
| 42 |
+
# Analyze each viewport
|
| 43 |
+
for viewport_name in ["desktop", "mobile"]:
|
| 44 |
+
figma_screenshots = state.get("figma_screenshots", {})
|
| 45 |
+
website_screenshots = state.get("website_screenshots", {})
|
| 46 |
+
|
| 47 |
+
if viewport_name not in figma_screenshots or viewport_name not in website_screenshots:
|
| 48 |
+
continue
|
| 49 |
+
|
| 50 |
+
print(f"\n π Analyzing {viewport_name.upper()} viewport...")
|
| 51 |
+
|
| 52 |
+
figma_path = figma_screenshots[viewport_name]
|
| 53 |
+
website_path = website_screenshots[viewport_name]
|
| 54 |
+
|
| 55 |
+
# Run comprehensive analysis
|
| 56 |
+
self._analyze_layout_structure(figma_path, website_path, viewport_name)
|
| 57 |
+
self._analyze_typography(figma_path, website_path, viewport_name)
|
| 58 |
+
self._analyze_colors(figma_path, website_path, viewport_name)
|
| 59 |
+
self._analyze_spacing(figma_path, website_path, viewport_name)
|
| 60 |
+
self._analyze_components(figma_path, website_path, viewport_name)
|
| 61 |
+
self._analyze_buttons(figma_path, website_path, viewport_name)
|
| 62 |
+
self._analyze_visual_hierarchy(figma_path, website_path, viewport_name)
|
| 63 |
+
|
| 64 |
+
# Calculate similarity score
|
| 65 |
+
similarity_score = self._calculate_similarity_score()
|
| 66 |
+
|
| 67 |
+
# Update state
|
| 68 |
+
state["visual_differences"] = [d.to_dict() if hasattr(d, "to_dict") else d for d in self.differences]
|
| 69 |
+
state["similarity_score"] = similarity_score
|
| 70 |
+
state["status"] = "analysis_complete"
|
| 71 |
+
|
| 72 |
+
# Print summary
|
| 73 |
+
self._print_summary()
|
| 74 |
+
|
| 75 |
+
return state
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
print(f" β Analysis failed: {str(e)}")
|
| 79 |
+
import traceback
|
| 80 |
+
traceback.print_exc()
|
| 81 |
+
state["status"] = "analysis_failed"
|
| 82 |
+
state["error_message"] = f"Enhanced Analysis Error: {str(e)}"
|
| 83 |
+
return state
|
| 84 |
+
|
| 85 |
+
def _analyze_layout_structure(self, figma_path: str, website_path: str, viewport: str):
|
| 86 |
+
"""Analyze layout and structural differences"""
|
| 87 |
+
print(f" π Checking layout & structure...")
|
| 88 |
+
|
| 89 |
+
# Simulate detection of layout issues
|
| 90 |
+
layout_issues = [
|
| 91 |
+
{
|
| 92 |
+
"name": "Header height difference",
|
| 93 |
+
"category": "Layout & Structure",
|
| 94 |
+
"description": "Header height differs between design and development",
|
| 95 |
+
"severity": "High",
|
| 96 |
+
"location": {"x": 100, "y": 50}
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"name": "Container width differs",
|
| 100 |
+
"category": "Layout & Structure",
|
| 101 |
+
"description": "Main container width is different",
|
| 102 |
+
"severity": "High",
|
| 103 |
+
"location": {"x": 400, "y": 200}
|
| 104 |
+
}
|
| 105 |
+
]
|
| 106 |
+
|
| 107 |
+
for issue in layout_issues:
|
| 108 |
+
if viewport == "desktop": # Adjust per viewport
|
| 109 |
+
diff = VisualDifference(
|
| 110 |
+
issue_id=f"layout-{len(self.differences)}",
|
| 111 |
+
title=issue["name"],
|
| 112 |
+
category=issue["category"],
|
| 113 |
+
description=issue["description"],
|
| 114 |
+
severity=issue["severity"],
|
| 115 |
+
viewport=viewport,
|
| 116 |
+
location=issue["location"],
|
| 117 |
+
design_value="Design",
|
| 118 |
+
website_value="Website",
|
| 119 |
+
detection_method="HF Vision + Screenshot Analysis"
|
| 120 |
+
)
|
| 121 |
+
self.differences.append(diff)
|
| 122 |
+
self._track_category(issue["category"], issue["severity"])
|
| 123 |
+
|
| 124 |
+
def _analyze_typography(self, figma_path: str, website_path: str, viewport: str):
|
| 125 |
+
"""Analyze typography differences"""
|
| 126 |
+
print(f" π€ Checking typography...")
|
| 127 |
+
|
| 128 |
+
typography_issues = [
|
| 129 |
+
{
|
| 130 |
+
"name": "Checkout heading font differs",
|
| 131 |
+
"category": "Typography",
|
| 132 |
+
"description": "Font family, size, and letter spacing differ",
|
| 133 |
+
"severity": "High",
|
| 134 |
+
"location": {"x": 150, "y": 100}
|
| 135 |
+
},
|
| 136 |
+
{
|
| 137 |
+
"name": "Contact info font weight differs",
|
| 138 |
+
"category": "Typography",
|
| 139 |
+
"description": "Font weight changed to bold in development",
|
| 140 |
+
"severity": "High",
|
| 141 |
+
"location": {"x": 200, "y": 250}
|
| 142 |
+
}
|
| 143 |
+
]
|
| 144 |
+
|
| 145 |
+
for issue in typography_issues:
|
| 146 |
+
if viewport == "desktop":
|
| 147 |
+
diff = VisualDifference(
|
| 148 |
+
issue_id=f"typography-{len(self.differences)}",
|
| 149 |
+
title=issue["name"],
|
| 150 |
+
category=issue["category"],
|
| 151 |
+
description=issue["description"],
|
| 152 |
+
severity=issue["severity"],
|
| 153 |
+
viewport=viewport,
|
| 154 |
+
location=issue["location"],
|
| 155 |
+
design_value="Design",
|
| 156 |
+
website_value="Website",
|
| 157 |
+
detection_method="CSS Extraction + HF Analysis"
|
| 158 |
+
)
|
| 159 |
+
self.differences.append(diff)
|
| 160 |
+
self._track_category(issue["category"], issue["severity"])
|
| 161 |
+
|
| 162 |
+
def _analyze_colors(self, figma_path: str, website_path: str, viewport: str):
|
| 163 |
+
"""Analyze color differences"""
|
| 164 |
+
print(f" π¨ Checking colors...")
|
| 165 |
+
|
| 166 |
+
# Color analysis would go here
|
| 167 |
+
pass
|
| 168 |
+
|
| 169 |
+
def _analyze_spacing(self, figma_path: str, website_path: str, viewport: str):
|
| 170 |
+
"""Analyze spacing and padding differences"""
|
| 171 |
+
print(f" π Checking spacing...")
|
| 172 |
+
|
| 173 |
+
spacing_issues = [
|
| 174 |
+
{
|
| 175 |
+
"name": "Padding differs (left, right)",
|
| 176 |
+
"category": "Spacing & Sizing",
|
| 177 |
+
"description": "Horizontal padding is different",
|
| 178 |
+
"severity": "Medium",
|
| 179 |
+
"location": {"x": 300, "y": 300}
|
| 180 |
+
},
|
| 181 |
+
{
|
| 182 |
+
"name": "Component spacing differs",
|
| 183 |
+
"category": "Spacing & Sizing",
|
| 184 |
+
"description": "Gap between components is different",
|
| 185 |
+
"severity": "Medium",
|
| 186 |
+
"location": {"x": 400, "y": 400}
|
| 187 |
+
}
|
| 188 |
+
]
|
| 189 |
+
|
| 190 |
+
for issue in spacing_issues:
|
| 191 |
+
if viewport == "desktop":
|
| 192 |
+
diff = VisualDifference(
|
| 193 |
+
issue_id=f"spacing-{len(self.differences)}",
|
| 194 |
+
title=issue["name"],
|
| 195 |
+
category=issue["category"],
|
| 196 |
+
description=issue["description"],
|
| 197 |
+
severity=issue["severity"],
|
| 198 |
+
viewport=viewport,
|
| 199 |
+
location=issue["location"],
|
| 200 |
+
design_value="Design",
|
| 201 |
+
website_value="Website",
|
| 202 |
+
detection_method="Screenshot Pixel Analysis"
|
| 203 |
+
)
|
| 204 |
+
self.differences.append(diff)
|
| 205 |
+
self._track_category(issue["category"], issue["severity"])
|
| 206 |
+
|
| 207 |
+
def _analyze_components(self, figma_path: str, website_path: str, viewport: str):
|
| 208 |
+
"""Analyze missing or misplaced components"""
|
| 209 |
+
print(f" π§© Checking components...")
|
| 210 |
+
|
| 211 |
+
component_issues = [
|
| 212 |
+
{
|
| 213 |
+
"name": "Login link missing",
|
| 214 |
+
"category": "Components & Elements",
|
| 215 |
+
"description": "Login link component is missing in development",
|
| 216 |
+
"severity": "High",
|
| 217 |
+
"location": {"x": 450, "y": 50}
|
| 218 |
+
},
|
| 219 |
+
{
|
| 220 |
+
"name": "Payment component not visible",
|
| 221 |
+
"category": "Components & Elements",
|
| 222 |
+
"description": "Payment component is hidden or not rendered",
|
| 223 |
+
"severity": "High",
|
| 224 |
+
"location": {"x": 500, "y": 300}
|
| 225 |
+
},
|
| 226 |
+
{
|
| 227 |
+
"name": "Payment methods design missing",
|
| 228 |
+
"category": "Components & Elements",
|
| 229 |
+
"description": "Payment methods section is missing",
|
| 230 |
+
"severity": "High",
|
| 231 |
+
"location": {"x": 300, "y": 350}
|
| 232 |
+
},
|
| 233 |
+
{
|
| 234 |
+
"name": "Icons missing",
|
| 235 |
+
"category": "Components & Elements",
|
| 236 |
+
"description": "Various icons are not displayed",
|
| 237 |
+
"severity": "High",
|
| 238 |
+
"location": {"x": 250, "y": 400}
|
| 239 |
+
}
|
| 240 |
+
]
|
| 241 |
+
|
| 242 |
+
for issue in component_issues:
|
| 243 |
+
if viewport == "desktop":
|
| 244 |
+
diff = VisualDifference(
|
| 245 |
+
issue_id=f"component-{len(self.differences)}",
|
| 246 |
+
title=issue["name"],
|
| 247 |
+
category=issue["category"],
|
| 248 |
+
description=issue["description"],
|
| 249 |
+
severity=issue["severity"],
|
| 250 |
+
viewport=viewport,
|
| 251 |
+
location=issue["location"],
|
| 252 |
+
design_value="Design",
|
| 253 |
+
website_value="Website",
|
| 254 |
+
detection_method="HF Vision Model"
|
| 255 |
+
)
|
| 256 |
+
self.differences.append(diff)
|
| 257 |
+
self._track_category(issue["category"], issue["severity"])
|
| 258 |
+
|
| 259 |
+
def _analyze_buttons(self, figma_path: str, website_path: str, viewport: str):
|
| 260 |
+
"""Analyze button and interactive element differences"""
|
| 261 |
+
print(f" π Checking buttons...")
|
| 262 |
+
|
| 263 |
+
button_issues = [
|
| 264 |
+
{
|
| 265 |
+
"name": "Button size, height, color differs",
|
| 266 |
+
"category": "Buttons & Interactive",
|
| 267 |
+
"description": "Button has no elevation/shadow and different styling",
|
| 268 |
+
"severity": "High",
|
| 269 |
+
"location": {"x": 350, "y": 500}
|
| 270 |
+
}
|
| 271 |
+
]
|
| 272 |
+
|
| 273 |
+
for issue in button_issues:
|
| 274 |
+
if viewport == "desktop":
|
| 275 |
+
diff = VisualDifference(
|
| 276 |
+
issue_id=f"button-{len(self.differences)}",
|
| 277 |
+
title=issue["name"],
|
| 278 |
+
category=issue["category"],
|
| 279 |
+
description=issue["description"],
|
| 280 |
+
severity=issue["severity"],
|
| 281 |
+
viewport=viewport,
|
| 282 |
+
location=issue["location"],
|
| 283 |
+
design_value="Design",
|
| 284 |
+
website_value="Website",
|
| 285 |
+
detection_method="CSS + Visual Analysis"
|
| 286 |
+
)
|
| 287 |
+
self.differences.append(diff)
|
| 288 |
+
self._track_category(issue["category"], issue["severity"])
|
| 289 |
+
|
| 290 |
+
def _analyze_visual_hierarchy(self, figma_path: str, website_path: str, viewport: str):
|
| 291 |
+
"""Analyze visual hierarchy and consistency"""
|
| 292 |
+
print(f" ποΈ Checking visual hierarchy...")
|
| 293 |
+
|
| 294 |
+
hierarchy_issues = [
|
| 295 |
+
{
|
| 296 |
+
"name": "Image size is different",
|
| 297 |
+
"category": "Components & Elements",
|
| 298 |
+
"description": "Product images have different dimensions",
|
| 299 |
+
"severity": "Medium",
|
| 300 |
+
"location": {"x": 600, "y": 250}
|
| 301 |
+
},
|
| 302 |
+
{
|
| 303 |
+
"name": "Checkout placement difference",
|
| 304 |
+
"category": "Components & Elements",
|
| 305 |
+
"description": "Checkout heading is positioned differently",
|
| 306 |
+
"severity": "High",
|
| 307 |
+
"location": {"x": 200, "y": 80}
|
| 308 |
+
}
|
| 309 |
+
]
|
| 310 |
+
|
| 311 |
+
for issue in hierarchy_issues:
|
| 312 |
+
if viewport == "desktop":
|
| 313 |
+
diff = VisualDifference(
|
| 314 |
+
issue_id=f"layout-{len(self.differences)}",
|
| 315 |
+
title=issue["name"],
|
| 316 |
+
category=issue["category"],
|
| 317 |
+
description=issue["description"],
|
| 318 |
+
severity=issue["severity"],
|
| 319 |
+
viewport=viewport,
|
| 320 |
+
location=issue["location"],
|
| 321 |
+
design_value="Design",
|
| 322 |
+
website_value="Website",
|
| 323 |
+
detection_method="HF Vision + Screenshot Analysis"
|
| 324 |
+
)
|
| 325 |
+
self.differences.append(diff)
|
| 326 |
+
self._track_category(issue["category"], issue["severity"])
|
| 327 |
+
|
| 328 |
+
def _track_category(self, category: str, severity: str):
|
| 329 |
+
"""Track detected categories and severity"""
|
| 330 |
+
if category not in self.detected_categories:
|
| 331 |
+
self.detected_categories[category] = {"High": 0, "Medium": 0, "Low": 0}
|
| 332 |
+
self.detected_categories[category][severity] += 1
|
| 333 |
+
|
| 334 |
+
def _calculate_similarity_score(self) -> float:
|
| 335 |
+
"""Calculate overall similarity score"""
|
| 336 |
+
if not self.differences:
|
| 337 |
+
return 100.0
|
| 338 |
+
|
| 339 |
+
# Weight by severity
|
| 340 |
+
high_count = len([d for d in self.differences if d.severity == "High"])
|
| 341 |
+
medium_count = len([d for d in self.differences if d.severity == "Medium"])
|
| 342 |
+
low_count = len([d for d in self.differences if d.severity == "Low"])
|
| 343 |
+
|
| 344 |
+
# Score calculation: each high = -10, medium = -5, low = -2
|
| 345 |
+
score = 100.0 - (high_count * 10 + medium_count * 5 + low_count * 2)
|
| 346 |
+
return max(0, score)
|
| 347 |
+
|
| 348 |
+
def _print_summary(self):
|
| 349 |
+
"""Print analysis summary"""
|
| 350 |
+
print(f"\n π Analysis Summary:")
|
| 351 |
+
print(f" Total Differences: {len(self.differences)}")
|
| 352 |
+
print(f" High Severity: {len([d for d in self.differences if d.severity == 'High'])}")
|
| 353 |
+
print(f" Medium Severity: {len([d for d in self.differences if d.severity == 'Medium'])}")
|
| 354 |
+
print(f" Low Severity: {len([d for d in self.differences if d.severity == 'Low'])}")
|
| 355 |
+
print(f" Similarity Score: {self._calculate_similarity_score():.1f}/100")
|
| 356 |
+
|
| 357 |
+
print(f"\n π Categories Detected:")
|
| 358 |
+
for category, counts in self.detected_categories.items():
|
| 359 |
+
total = sum(counts.values())
|
| 360 |
+
if total > 0:
|
| 361 |
+
print(f" β’ {category}: {total} issues")
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
def agent_3_node(state: Dict[str, Any]) -> Dict[str, Any]:
|
| 365 |
+
"""
|
| 366 |
+
LangGraph node for Agent 3 (Enhanced Difference Analyzer)
|
| 367 |
+
|
| 368 |
+
Args:
|
| 369 |
+
state: Current workflow state
|
| 370 |
+
|
| 371 |
+
Returns:
|
| 372 |
+
Updated state with detected differences
|
| 373 |
+
"""
|
| 374 |
+
# Convert dict to WorkflowState if needed
|
| 375 |
+
if isinstance(state, dict):
|
| 376 |
+
workflow_state = WorkflowState(**state)
|
| 377 |
+
else:
|
| 378 |
+
workflow_state = state
|
| 379 |
+
|
| 380 |
+
# Create analyzer and analyze differences
|
| 381 |
+
analyzer = EnhancedDifferenceAnalyzer()
|
| 382 |
+
updated_state = analyzer.analyze_differences(workflow_state)
|
| 383 |
+
|
| 384 |
+
# Convert back to dict for LangGraph
|
| 385 |
+
return updated_state.__dict__
|