UNPKG

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
# XOBuilder Extractor > **A sophisticated web application that converts HTML and CSS to JSON layout through an intelligent multi-step flow process with AI validation.** ![Flow Diagram](./public/flow.png) ## šŸ”„ 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.