breeth-bui-compiler
v1.2.0
Published
Breeth .bui compiler for parsing, validating, and merging .bui files
Maintainers
Readme
Breeth BUI Compiler
A robust, production-grade compiler for parsing, validating, and merging .bui files. This compiler provides comprehensive error reporting, validation, and a powerful CLI interface for working with Breeth UI configuration files.
📦 Package: breeth-bui-compiler
👤 Author: @srk0102
🔗 NPM: https://www.npmjs.com/package/breeth-bui-compiler
🚀 Quick Start
npm install breeth-bui-compilerimport { renderBUIString } from 'breeth-bui-compiler';
// Pass BUI content, get rendered output back (like axios)
const htmlOutput = renderBUIString(buiContent, 'html', 'light');
const reactOutput = renderBUIString(buiContent, 'react', 'dark');
const jsonOutput = renderBUIString(buiContent, 'json');No file creation - just pass content and get rendered output back!
Features
🚀 Production-Grade Compiler
- Robust Error Handling: Comprehensive error codes and detailed error messages
- Advanced Validation: Schema validation with Zod, custom validation rules
- Security Features: Path traversal protection, file size limits, suspicious file detection
- Performance Monitoring: Parse time tracking, file size statistics
📁 Multi-File Support
- Project Organization: Split large configurations across multiple files
- Dependency Management: Automatic file inclusion and merging
- Circular Dependency Detection: Prevents infinite loops
- File Validation: Security checks and size limits
🔍 Comprehensive Validation
- Profile Validation: Name, description, logo, website, contact validation
- BPod Validation: API endpoints, file types, body templates
- URL Validation: HTTPS enforcement, accessibility checking
- Schema Validation: Type safety with comprehensive error reporting
- File Type Restriction: Only .bui files are supported
- HTTP Methods: Only GET and POST methods are allowed
🛠️ Powerful CLI Interface
- Compile Command: Full compilation with options
- Validate Command: Quick validation without compilation
- Info Command: Detailed file analysis
- Multiple Output Formats: JSON and human-readable output
📊 Advanced Error Reporting
- Error Codes: Standardized error codes for programmatic handling
- Context Information: Line numbers, file paths, surrounding code
- Suggestions: Helpful hints for fixing issues
- Grouped Output: Errors organized by file for better readability
Installation
npm install breeth-bui-compilerQuick Start
Basic Usage
import { parseBUI } from 'breeth-bui-compiler';
const result = parseBUI('config.bui');
if (result.success) {
console.log(`Compiled ${result.ast.bPods.length} BPods`);
} else {
console.log(`Compilation failed with ${result.errors.length} errors`);
}CLI Usage
# Compile a .bui file
npx breeth-bui-compiler compile index.bui
# Validate a .bui file
npx breeth-bui-compiler validate index.bui
# Render a .bui file to HTML, React, or JSON
npx breeth-bui-compiler render index.bui
# Get detailed information
npx breeth-bui-compiler info index.buiCLI Command: npx breeth-bui-compiler or npx breeth-bui-compiler --help
Available Commands
| Command | Description | Usage |
|---------|-------------|-------|
| compile | Compile and validate .bui files | npx breeth-bui-compiler compile <file> |
| validate | Quick validation without compilation | npx breeth-bui-compiler validate <file> |
| render | Render to HTML/React/JSON | npx breeth-bui-compiler render <file> |
| info | Get detailed file information | npx breeth-bui-compiler info <file> |
Compile Command
npx breeth-bui-compiler compile <entry-file> [options]Options:
-o, --output <file>: Save output to file (default: console)-f, --format <format>: Output format:jsonorpretty(default: pretty)--strict: Enable strict validation mode--no-warnings: Hide warning messages--max-file-size <bytes>: Max file size limit (default: 1MB)--max-files <count>: Max number of files (default: 100)--validate-urls: Check if API URLs are accessible--timeout <ms>: URL validation timeout (default: 5000ms)--metadata: Include build metadata in output
Render Command
npx breeth-bui-compiler render <entry-file> [options]Options:
-f, --format <format>: Output format:html,react, orjson(default: html)-t, --theme <theme>: Theme:lightordark(default: light)-o, --output <file>: Save output to file (default: console)--show-warnings: Include warnings in output--show-errors: Include errors in output--class-name <name>: Custom CSS class name for styling
Examples:
# Render to HTML with light theme
npx breeth-bui-compiler render index.bui -f html -t light
# Render to React component with dark theme
npx breeth-bui-compiler render index.bui -f react -t dark
# Render to JSON and save to file
npx breeth-bui-compiler render index.bui -f json -o output.json
# Render to HTML with custom class and save to file
npx breeth-bui-compiler render index.bui -f html -t light --class-name my-app -o app.html📝 BUI File Format
BUI files define AI plugin panels with services, inputs, and API configurations. Here's how to write them:
Basic BUI File
version: "1.0.0"
---
profile: {
"name": "My AI Service",
"description": "Process audio files with AI",
"logo": "https://example.com/logo.png",
"website": "https://example.com",
"contact": "[email protected]"
}
---
b-pod: "Audio Processor" {
"accepts": ["mp3", "wav", "aac"],
"description": "Convert and enhance audio files",
"inputs": [
{
"name": "quality",
"type": "dropdown",
"label": "Output Quality",
"options": ["low", "medium", "high"],
"required": true
},
{
"name": "format",
"type": "radio",
"label": "Output Format",
"options": ["mp3", "wav", "aac"],
"required": true
}
],
"submit": { "label": "Process Audio", "action": "processAudio" },
"api": {
"url": "https://api.example.com/v1/audio/process",
"method": "POST",
"fileParams": ["audioFile"],
"bodyTemplate": {
"file": "{audioFile}",
"quality": "{quality}",
"format": "{format}",
"webhook_url": "{webhook_url}"
},
"responseType": "file"
}
}Multi-File Project
For larger projects, you can split your BUI into multiple files:
index.bui (Main file):
version: "1.0.0"
---
profile: {
"name": "AI Studio",
"description": "Complete AI processing suite",
"logo": "https://example.com/logo.png",
"website": "https://example.com"
}
---
files: ["services/audio.bui", "services/text.bui"]
---
b-pod: "Main Controller" {
"accepts": ["txt", "mp3", "jpg"],
"submit": { "label": "Process", "action": "process" },
"api": {
"url": "https://api.example.com/v1/process",
"method": "POST",
"fileParams": ["inputFile"],
"bodyTemplate": {
"file": "{inputFile}",
"webhook_url": "{webhook_url}"
},
"responseType": "json"
}
}services/audio.bui:
b-pod: "Audio Processor" {
"accepts": ["mp3", "wav", "aac"],
"description": "Process audio files",
"inputs": [
{
"name": "quality",
"type": "dropdown",
"label": "Quality",
"options": ["low", "medium", "high"],
"required": true
}
],
"submit": { "label": "Process Audio", "action": "processAudio" },
"api": {
"url": "https://api.example.com/v1/audio",
"method": "POST",
"fileParams": ["audioFile"],
"bodyTemplate": {
"file": "{audioFile}",
"quality": "{quality}",
"webhook_url": "{webhook_url}"
},
"responseType": "file"
}
}Writing Rules
- Main File: Use
index.buias your main file - Profile: Put your profile info in
index.buionly - BPods: Define your services in
index.buior separate files - File References: Use
files: ["file1.bui", "file2.bui"]to include other files - Separators: Use
---to separate different sections
Input Types
- text: Single line text input
- textarea: Multi-line text input
- number: Numeric input with optional min/max validation
- checkbox: Boolean true/false input
- radio: Single selection from options
- dropdown: Dropdown selection from options
API Configuration
- url: Must be HTTPS URL
- method: "GET" or "POST" only
- fileParams: Array of file parameter names
- bodyTemplate: JSON template with
{variable}placeholders - responseType: "json" or "file"
- webhook_url: Always include
{webhook_url}in bodyTemplate
Renderer
The BUI compiler includes a powerful renderer that can convert compiled BUI definitions into multiple output formats:
Supported Formats
- HTML: Complete HTML pages with embedded CSS and JavaScript
- React: React components with hooks and state management
- JSON: Structured data for programmatic access
Renderer Functions
renderBUI(entryPath, options): Main renderer functionrenderBUICLI(entryPath, options): CLI-friendly renderer function
Renderer Options
interface RenderOptions {
format: 'react' | 'html' | 'json';
theme?: 'light' | 'dark';
showErrors?: boolean;
showWarnings?: boolean;
className?: string;
onAction?: (bpodName: string, action: string, data: any) => void;
}Programmatic Usage
Simple String Renderer (Like Axios)
import { renderBUIString } from '@srk0102/bui-compiler';
// Render to HTML (default)
const htmlOutput = renderBUIString(buiContent, 'html', 'light');
// Render to React component
const reactOutput = renderBUIString(buiContent, 'react', 'dark');
// Render to JSON
const jsonOutput = renderBUIString(buiContent, 'json');Advanced Renderer with Options
import { renderBUIContent } from 'breeth-bui-compiler';
const result = renderBUIContent(buiContent, {
format: 'html',
theme: 'dark',
showWarnings: true,
showErrors: true,
className: 'my-custom-app'
});
if (result.success) {
console.log('HTML output:', result.output);
console.log('Warnings:', result.warnings);
} else {
console.log('Errors:', result.errors);
}Direct Parsing
import { parseBUI } from 'breeth-bui-compiler';
const parseResult = parseBUI('/tmp/demo.bui', {
content: buiContent,
withMetadata: true
});
if (parseResult.success) {
console.log('Version:', parseResult.ast.version);
console.log('Profile:', parseResult.ast.profile?.name);
console.log('BPods:', parseResult.ast.bPods.length);
}File-based Rendering (Legacy)
import { renderBUI } from 'breeth-bui-compiler';
const htmlResult = renderBUI('index.bui', {
format: 'html',
theme: 'light',
showWarnings: true,
className: 'my-app'
});
if (htmlResult.success) {
console.log('HTML output:', htmlResult.output);
} else {
console.log('Errors:', htmlResult.errors);
}CLI-specific Functions
import { renderBUICLI } from 'breeth-bui-compiler/cli';
// CLI-specific functions for programmatic CLI usage
renderBUICLI('index.bui', {
format: 'html',
theme: 'light'
});Error Handling
The renderer gracefully handles compilation errors and warnings:
- Compilation Errors: Shows detailed error information with error codes, messages, and file locations
- Compilation Warnings: Displays warnings that don't prevent rendering
- Format-Specific Output: Errors are displayed in the appropriate format (HTML, React, or JSON)
Demo
Run the included demo to see the renderer in action:
# Build the project first
npm run build
# Run the demo
node examples/render-demo.jsThis will generate sample HTML, React, and JSON outputs from a demo BUI file.
API Reference
Core Functions
parseBUI(entryPath: string, options?: CompileOptions): CompileResult
Main compilation function that parses and validates .bui files.
Options:
strict: Enable strict validation modeallowWarnings: Allow compilation to succeed with warningsmaxFileSize: Maximum file size in bytes (default: 1MB)maxFiles: Maximum number of files (default: 100)validateUrls: Validate API URLs for accessibilitytimeout: Timeout for URL validation in millisecondswithMetadata: Include metadata in output
Returns:
interface CompileResult {
ast: {
version: string;
profile?: Profile;
bPods: BPod[];
};
metadata?: {
includedFiles: string[];
bPodFileMap: Record<string, string>;
parseTime: number;
totalSize: number;
warnings: CompilerWarning[];
};
errors: CompilerError[];
warnings: CompilerWarning[];
success: boolean;
}Validation Functions
validateProfile(profile: Profile): ValidationResult<Profile>
validateBPod(bPod: BPod): ValidationResult<BPod>
validateFileExtensions(extensions: string[]): ValidationResult<string[]>
validateApiUrl(url: string, options?: object): Promise<ValidationResult<string>>
validateBodyTemplate(template: Record<string, string>, fileParams: string[]): ValidationResult<Record<string, string>>
Error Handling
createError(code: ErrorCode, message: string, context: ParseContext, suggestion?: string): CompilerError
createWarning(code: WarningCode, message: string, context: ParseContext, suggestion?: string): CompilerWarning
formatError(error: CompilerError): string
formatWarning(warning: CompilerWarning): string
groupErrorsByFile(errors: CompilerError[]): Record<string, CompilerError[]>
getErrorStats(errors: CompilerError[], warnings: CompilerWarning[]): object
File Management
mergeBuiFiles(entryPath: string, fileList: string[], options?: CompileOptions): MergeResult
validateFilePath(filePath: string, baseDir: string): { valid: boolean; error?: string }
getFileStats(filePath: string): { size: number; lastModified: Date; exists: boolean }
isFileReadable(filePath: string): boolean
Error Codes
Parsing Errors (E001-E099)
E001: Invalid syntaxE002: Missing colon in declarationE003: Invalid versionE004: Invalid profile JSONE005: Invalid BPod JSONE006: Missing BPod nameE007: Duplicate BPod nameE008: Invalid files block
Validation Errors (E101-E199)
E101: Profile name requiredE102: Profile description too longE103: Invalid logo URLE104: Invalid website URL
BPod Validation Errors (E201-E299)
E201: BPod name requiredE202: BPod accepts array emptyE203: BPod accepts invalid formatE204: BPod submit label requiredE205: BPod submit action requiredE206: BPod API URL invalidE207: BPod API method invalidE208: BPod body template missing webhookE209: BPod input options required
File Handling Errors (E301-E399)
E301: File not foundE302: File too largeE303: Too many filesE304: File read errorE305: Invalid file path
System Errors (E401-E499)
E401: Timeout errorE402: Memory errorE499: Unknown error
CLI Commands
compile - Compile a .bui file
breeth-bui-compiler compile <entry-file> [options]Options:
-o, --output <file>: Output file for compilation result-f, --format <format>: Output format (json, pretty) [default: pretty]--strict: Enable strict mode with additional validations--no-warnings: Suppress warnings--max-file-size <bytes>: Maximum file size in bytes [default: 1048576]--max-files <count>: Maximum number of files [default: 100]--validate-urls: Validate API URLs--timeout <ms>: Timeout for URL validation in milliseconds [default: 5000]--metadata: Include metadata in output
validate - Validate a .bui file
breeth-bui-compiler validate <file> [options]Options:
--strict: Enable strict validation
info - Display file information
breeth-bui-compiler info <file>Configuration
Compile Options
interface CompileOptions {
strict?: boolean; // Enable strict validation
allowWarnings?: boolean; // Allow warnings
maxFileSize?: number; // Max file size in bytes
maxFiles?: number; // Max number of files
validateUrls?: boolean; // Validate API URLs
timeout?: number; // Timeout in milliseconds
withMetadata?: boolean; // Include metadata
}Environment Variables
BUI_MAX_FILE_SIZE: Maximum file size in bytesBUI_MAX_FILES: Maximum number of filesBUI_TIMEOUT: Default timeout in millisecondsBUI_STRICT: Enable strict mode by default
Security Features
Path Traversal Protection
The compiler prevents path traversal attacks by ensuring all file paths resolve within the base directory.
File Size Limits
Configurable file size limits prevent memory exhaustion attacks.
File Type Restriction
Only .bui files are supported, preventing execution of potentially dangerous file types.
HTTPS Enforcement
All URLs must use HTTPS for security.
Performance
Optimizations
- Efficient file parsing with minimal memory allocation
- Lazy validation for large files
- Optimized error reporting with context caching
Monitoring
- Parse time tracking
- File size statistics
- Memory usage monitoring
- Performance metrics collection
Testing
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run specific test file
npm test -- compiler.spec.tsContributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
MIT License - see LICENSE file for details.
Support
For issues and questions:
- Create an issue on GitHub
- Check the error codes and suggestions in the output
- Review the validation rules and requirements
Changelog
v1.0.0
- Initial production release
- Comprehensive error handling and reporting
- Multi-file support with dependency management
- Advanced validation with Zod schemas
- Powerful CLI interface
- Security features and performance optimizations
- Extensive test coverage
