UXOracle
Mission: Enhances UI/UX through heuristic analysis, automated prototyping, and accessibility checks.
UI Flow Analysis with Heuristic Scoring & Prototype Generation
import logging
import re
import os
import shutil
from typing import List
logging.basicConfig(level=logging.INFO)
class UXOracleAgent:
def __init__(self, base_path="./ui", output_path="./prototypes"):
self.base_path = base_path
self.output_path = output_path
os.makedirs(output_path, exist_ok=True)
def analyze_ui_project(self) -> dict:
"""
Scans .js/.jsx/.tsx or .html files to derive a basic heuristic score
(e.g., usage of ARIA attributes, color contrast checks).
"""
score_data = {'files_scanned': 0, 'accessibility_score': 0, 'issues': []}
file_list = self._collect_ui_files()
for fpath in file_list:
with open(fpath, 'r', encoding='utf-8') as f:
content = f.read()
local_issues = self._check_accessibility(content)
score_data['files_scanned'] += 1
score_data['issues'].extend(local_issues)
# Compute a synthetic accessibility score
total_issues = len(score_data['issues'])
score_data['accessibility_score'] = max(0, 100 - total_issues * 2)
return score_data
def generate_ab_prototypes(self, prototype_count: int = 2):
"""
Creates multiple UI prototypes by duplicating the base UI and injecting
heuristic-driven changes (e.g., color scheme improvements, form layout tweaks).
"""
for i in range(prototype_count):
prototype_dir = os.path.join(self.output_path, f"Prototype_{i+1}")
if os.path.exists(prototype_dir):
shutil.rmtree(prototype_dir)
shutil.copytree(self.base_path, prototype_dir)
logging.info(f"Generated prototype copy: {prototype_dir}")
# Example: Modify style or layout in a main config
config_file = os.path.join(prototype_dir, "style.config")
if os.path.exists(config_file):
with open(config_file, 'a', encoding='utf-8') as f:
new_color = "#"+''.join([str(hex(i*3))[2:] for _ in range(3)])
f.write(f"\n// Auto-injected color scheme improvement: {new_color}\n")
def _collect_ui_files(self) -> List[str]:
matched_files = []
for root, _, files in os.walk(self.base_path):
for fname in files:
if fname.endswith(('.js', '.jsx', '.tsx', '.html')):
matched_files.append(os.path.join(root, fname))
return matched_files
def _check_accessibility(self, content: str) -> List[str]:
issues = []
# Simple pattern to detect missing alt in <img> tags
img_tags = re.findall(r'<img[^>]*>', content, flags=re.IGNORECASE)
for tag in img_tags:
if 'alt=' not in tag:
issues.append(f"Missing alt attribute: {tag}")
# Check ARIA usage or other known patterns
if 'aria-label' not in content:
issues.append("No ARIA labels found in file; might reduce screenreader accessibility.")
return issues
# Usage:
# agent = UXOracleAgent(base_path="./react_app", output_path="./ui_prototypes")
# analysis = agent.analyze_ui_project()
# logging.info(f"UI Analysis => {analysis}")
# agent.generate_ab_prototypes(prototype_count=2)
Last updated