xobuilder-extractor
Version:
> **A sophisticated web application that converts HTML and CSS to JSON layout through an intelligent multi-step flow process with AI validation.**
289 lines (193 loc) ⢠8.24 kB
Markdown
# XOBuilder Extractor
> **A sophisticated web application that converts HTML and CSS to JSON layout through an intelligent multi-step flow process with AI validation.**

## š„ Overview
XOBuilder Extractor is an advanced web application designed to transform HTML and CSS code into structured JSON layouts through a visual, multi-step processing flow. The application leverages ReactFlow for interactive UI representation and integrates with OpenAI for intelligent content conversion and validation.
## šļø Architecture
The project follows **Clean Architecture** principles with clear separation of concerns:
```
src/
āāā application/ # Business logic and use cases
ā āāā dto/ # Data Transfer Objects
ā āāā use-case/ # Application use cases
āāā domains/ # Core business entities and models
ā āāā entities/ # Domain entities
āāā utils/ # Utility functions and helpers
āāā constants/ # Application constants
āāā index.ts # Main entry point and exported functions
```
## šÆ Core Features
### š Visual Flow Processing
- **Interactive Flow Visualization**: Built with ReactFlow for intuitive process monitoring
- **Real-time Status Updates**: Visual feedback with animated borders and progress indicators
- **Step-by-Step Execution**: Clear progression through processing stages
### š¤ AI-Powered Conversion
- **OpenAI Integration**: Intelligent HTML/CSS to JSON conversion
- **Validation Loop**: Automatic comparison and re-generation for accuracy
- **Smart Retry Logic**: Up to 5 automatic retries with improvement iterations
### šØ Advanced UI States
- **Processing State**: Animated running border with real-time counter
- **Success State**: Green success border indicating completion
- **Failure State**: Red error border for failed operations
- **Connection Types**:
- Solid arrows for step-to-step flow
- Dashed arrows for AI agent connections
## š Processing Flow
The application follows a sophisticated 4-step process:
### Step 1: Data Extraction š„
**Purpose**: Extract and prepare source materials
- **Input**: JSON file containing array of objects
- **Processing**: Extract HTML, CSS, and screenshot data
- **Output**: Structured data ready for conversion
- **Status**: Foundation step - must complete successfully
### Step 2: AI Conversion š¤
**Purpose**: Transform HTML/CSS to raw JSON layout
- **Input**: HTML and CSS from Step 1
- **AI Agent**: OpenAI integration (dashed connection)
- **Processing**: Intelligent conversion using machine learning
- **Output**: Raw JSON layout structure
- **Retry Logic**: Supports up to 5 regeneration attempts
### Step 3: Validation & Comparison š
**Purpose**: Ensure conversion accuracy through visual comparison
- **Input**: Raw JSON from Step 2 + Screenshot from Step 1
- **Processing**:
- Render JSON layout as image
- Compare with original screenshot
- Calculate similarity score
- **Decision Logic**:
- ā
**Similar**: Proceed to Step 4
- ā **Different**: Return to Step 2 (max 5 times)
- **Smart Retry**: Iterative improvement with feedback
### Step 4: Finalization ā
**Purpose**: Complete the conversion process
- **Success Path**: From Step 3 validation success
- **Failure Path**: When Step 3 reaches maximum retry limit
- **Output**: Final result status and processed data
## šØ Visual Flow Design
### Connection Types
- **Step Flow**: `Step 1 āāā Step 2 āāā Step 3 āāā Step 4`
- **AI Integration**: `Step 2 āāāā OpenAI Agent`
- **Retry Loop**: `Step 3 āāāāā Step 2` (conditional)
### Visual States
| State | Visual Indicator | Description |
| -------------- | -------------------------- | --------------------------- |
| **Processing** | š Animated border + timer | Step currently executing |
| **Success** | ā
Green border | Step completed successfully |
| **Failure** | ā Red border | Step failed to complete |
| **Pending** | āŖ Default border | Step waiting to execute |
## š ļø Technology Stack
### Core Dependencies
- **Build System**: Vite + TypeScript
- **DOM Processing**: jsdom, parse5
- **CSS Processing**: css, csso
- **Utilities**: lodash
- **Runtime**: Node.js with TypeScript support
### Development Tools
- **Linting**: ESLint with TypeScript support
- **Type Checking**: TypeScript 5.7+
- **Package Manager**: pnpm
- **Build Tool**: Rollup with TypeScript plugin
## š¦ API Reference
### Core Functions
#### `parseHtml(html: string, css: string)`
Parses HTML and CSS into structured format for processing.
```typescript
const result = parseHtml(htmlContent, cssContent);
```
#### `getEntities({ html, raw }: GetEntitiesProps)`
Fills element attributes using parsed HTML and raw JSON data.
```typescript
const entities = getEntities({
html: htmlString,
raw: sectionRawData,
});
```
#### `getStyles(input: GetStylesInput)`
Extracts and processes CSS styles from input data.
```typescript
const styles = getStyles(styleInput);
```
#### `getStylesAEP(type: string, cssText: string)`
Analyzes element properties from CSS text.
```typescript
const analyzedStyles = getStylesAEP("div", cssText);
```
#### `getEEP(data: RawDataEEPV2[])`
Expands element properties from raw data.
```typescript
const expandedProperties = getEEP(rawDataArray);
```
## š Getting Started
### Prerequisites
- Node.js 18+
- pnpm (recommended) or npm
### Installation
```bash
# Clone the repository
git clone <repository-url>
cd xobuilder-extractor
# Install dependencies
pnpm install
```
### Development
```bash
# Start development server
pnpm dev
# Build for production
pnpm build
# Run production build
pnpm start
```
## š§ Configuration
### Build Configuration
The project uses Vite for modern, fast builds with TypeScript support. Configuration can be found in:
- `vite.config.ts` - Build configuration
- `tsconfig.json` - TypeScript settings
- `eslint.config.js` - Code quality rules
### Environment Setup
Ensure your environment supports:
- ES2020+ features
- TypeScript 5.7+
- Modern DOM APIs through jsdom
## š Performance & Scalability
### Optimization Features
- **Tree Shaking**: Eliminates unused code in production builds
- **Code Splitting**: Modular architecture supports lazy loading
- **Type Safety**: Full TypeScript coverage prevents runtime errors
- **Efficient Parsing**: Optimized HTML/CSS processing with specialized libraries
### Scalability Considerations
- **Clean Architecture**: Easy to extend with new processing steps
- **Modular Design**: Independent use cases and entities
- **Retry Logic**: Robust error handling and recovery
- **AI Integration**: Pluggable AI providers through abstraction layer
## š Troubleshooting
### Common Issues
#### AI Conversion Failures
- **Cause**: Network issues or API limitations
- **Solution**: Check API keys and network connectivity
- **Mitigation**: Automatic retry logic handles temporary failures
#### Visual Comparison Mismatches
- **Cause**: Rendering differences or screenshot quality
- **Solution**: Adjust similarity threshold in validation logic
- **Monitoring**: Track retry patterns for optimization
#### Memory Usage
- **Cause**: Large HTML/CSS files or many concurrent processes
- **Solution**: Implement chunking for large files
- **Prevention**: Monitor and limit concurrent processing
## š¤ Contributing
### Development Workflow
1. Create feature branch from main
2. Implement changes following Clean Architecture
3. Add/update tests for new functionality
4. Ensure TypeScript compliance
5. Submit pull request with detailed description
### Code Standards
- Follow existing architectural patterns
- Maintain type safety throughout
- Document complex business logic
- Use meaningful variable and function names
## š License
This project is licensed under the terms specified in the package.json file.
---
**Built with ā¤ļø by HaiUTC**
For questions or support, please refer to the project documentation or create an issue in the repository.