#!/usr/bin/env node
/**
* Generate Professional PDF from Brainy Architecture Documentation
*
* This script converts the Markdown documentation with Mermaid diagrams
* into a beautiful, professional PDF using Puppeteer and modern CSS styling.
*/
import puppeteer from 'puppeteer'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
// Configuration
const config = {
readmeFile: path.join(__dirname, '../../README.md'),
architectureFile: path.join(__dirname, '../docs/brainy_architecture_diagram.md'),
outputFile: path.join(__dirname, '../../docs/Brainy_Architecture_Documentation.pdf')
}
// Apple-inspired CSS styling with Brainy retro sci-fi aesthetic
const appleInspiredCSS = `
`
// SVG Diagram Generation Functions
function generateArchitectureDiagram() {
return `
1. Executive Summary
Brainy represents a revolutionary approach to AI-powered data management, combining the semantic understanding of vector databases with the relational power of graph structures. This platform automatically adapts to any computing environment while providing enterprise-grade performance and scalability.
π Universal Deployment
Runs seamlessly across browsers, servers, containers, and cloud platforms
π§ Intelligent Adaptation
Automatically optimizes for your environment and usage patterns
β‘ Advanced Performance
Built-in TensorFlow.js with GPU acceleration and multithreading
π Real-time Sync
WebSocket and WebRTC support for distributed systems
${overviewMatch ? overviewMatch[1].trim() : ''}
Why Choose Brainy?
Brainy eliminates the complexity typically associated with vector databases and graph systems. It automatically handles environment detection, storage optimization, performance tuning, and scalingβallowing developers to focus on building innovative applications rather than managing infrastructure.
Zero Configuration Required: Works out-of-the-box with intelligent defaults that adapt to your specific use case and environment.
## 2. System Overview & Key Features
${featuresMatch ? featuresMatch[1].trim() : ''}
### Advanced Capabilities
- **Multi-Environment Deployment**: Seamlessly runs in browsers, Node.js, serverless functions, containers, and dedicated servers
- **Intelligent Storage Selection**: Automatically chooses optimal storage (OPFS, filesystem, S3, or memory) based on environment
- **GPU-Accelerated Processing**: Leverages WebGL and TensorFlow.js for high-performance vector operations
- **Real-time Data Streaming**: Built-in WebSocket support for live data processing
- **Extensible Augmentation System**: Modular architecture for custom functionality
- **Enterprise-Ready Scaling**: Handles datasets from small collections to terabyte-scale deployments
## 3. Core Architecture & Design Principles
${howItWorksMatch ? howItWorksMatch[1].trim() : ''}
### Architectural Principles
Brainy is built on seven core principles that ensure scalability, performance, and ease of use:
π Environment Agnostic
Automatically adapts to any computing environment without configuration changes
π§ Intelligent Storage
Multi-tier caching with automatic storage selection and optimization
π Vector + Graph
Combines semantic search with graph relationships in a unified model
π§ Extensible Pipeline
Modular augmentation system for custom processing and integration
β‘ Performance Optimized
GPU acceleration, multithreading, and intelligent caching
π Scalable Sync
WebSocket and WebRTC for real-time synchronization
π€ AI Integration
MCP protocol for external AI model integration
## 4. Advanced Data Processing Pipeline
${pipelineMatch ? pipelineMatch[1].trim() : ''}
### Pipeline Innovation
The Brainy pipeline represents a breakthrough in data processing architecture:
- **Adaptive Learning**: The system learns from usage patterns and automatically optimizes performance
- **Multi-threaded Execution**: Parallel processing across Web Workers and Worker Threads
- **Streaming Data Support**: Real-time processing of incoming data streams
- **Intelligent Caching**: Multi-level caching that adapts to data access patterns
- **Error Resilience**: Robust error handling and recovery mechanisms
## 5. Vector Search & Graph Database Technology
### HNSW Algorithm Implementation
Brainy uses an optimized Hierarchical Navigable Small World (HNSW) algorithm for fast similarity search:
- **Hierarchical Structure**: Multi-layer graph for logarithmic search complexity
- **Memory Efficiency**: Product quantization for large datasets
- **Disk-Based Storage**: Hybrid approach for datasets exceeding memory limits
- **Configurable Parameters**: Tunable for precision vs. performance tradeoffs
### Vector Embedding Technology
- **TensorFlow Universal Sentence Encoder**: High-quality text embeddings
- **GPU Acceleration**: WebGL backend for fast computation
- **Batch Processing**: Efficient handling of multiple embeddings
- **Custom Embedding Support**: Pluggable embedding functions
## 6. Storage Architecture & Environment Adaptation
### Multi-Tier Storage Strategy
Brainy implements a sophisticated three-tier storage architecture:
1. **Hot Cache (RAM)**: Most frequently accessed data with LRU eviction
2. **Warm Cache (Local Storage)**: Recently accessed data with TTL management
3. **Cold Storage (Persistent)**: Complete dataset with batch optimization
### Environment-Specific Optimizations
- **Browser**: Origin Private File System (OPFS) with IndexedDB fallback
- **Node.js**: File system with optional S3 integration
- **Serverless**: In-memory with cloud persistence options
- **Container**: Automatic detection and adaptation
- **Server**: Full S3-compatible cloud storage support
## 7. Performance & Scalability Features
${performanceMatch ? performanceMatch[1].trim() : ''}
### Scaling Capabilities
${scalingMatch ? scalingMatch[1].trim() : ''}
### Performance Optimizations
- **Intelligent Defaults**: Automatic parameter tuning based on environment and dataset
- **Memory-Aware Caching**: Dynamic cache sizing based on available resources
- **Batch Operations**: Optimized bulk data processing
- **Lazy Loading**: On-demand data loading for large datasets
- **Query Optimization**: Smart query planning and execution
## 8. Cross-Platform Integration & Synchronization
### Real-Time Synchronization
Brainy provides multiple synchronization mechanisms:
- **WebSocket Conduits**: Browser-server and server-server synchronization
- **WebRTC Conduits**: Direct peer-to-peer browser communication
- **Change Logging**: Efficient delta synchronization
- **Conflict Resolution**: Automatic handling of concurrent updates
### Model Control Protocol (MCP)
Integration with external AI models through standardized protocols:
- **Data Access**: Secure access to Brainy data for external models
- **Tool Integration**: Expose Brainy functionality as AI tools
- **Service Architecture**: WebSocket and REST API support
## 9. Technical Implementation Details
${dataModelMatch ? dataModelMatch[1].trim() : ''}
### Advanced Features
- **Type Safety**: Full TypeScript support with generics
- **Field Standardization**: Cross-service field mapping and search
- **Backup & Restore**: Complete data portability
- **Testing Suite**: Comprehensive test coverage with Vitest
- **CLI Tools**: Command-line interface for data management
### Development & Operations
- **Zero Configuration**: Works out-of-the-box with intelligent defaults
- **Monitoring**: Built-in statistics and performance metrics
- **Documentation**: Comprehensive guides and API reference
- **Community**: Open-source with active development
## 10. Architecture Diagrams & Visual Guide
${generateArchitectureDiagram()}
${generateDataFlowDiagram()}
${generateHNSWDiagram()}
### Original Architecture Documentation
${architectureContent}
Conclusion
Brainy represents the next generation of AI-powered data platforms, combining ease of use with enterprise-grade capabilities. Its intelligent adaptation, powerful features, and comprehensive architecture make it the ideal choice for modern applications requiring semantic search, graph relationships, and real-time data processing.
Whether you're building a simple browser application or a complex distributed system, Brainy automatically adapts to provide optimal performance and functionality in any environment.
*For more information, visit the [Brainy GitHub repository](https://github.com/soulcraft-research/brainy) or try the [live demo](https://soulcraft-research.github.io/brainy/demo/index.html).*
`
return content
}
// Function to convert markdown to HTML with enhanced formatting
function markdownToHTML(markdown) {
let html = markdown
// Convert headers
html = html.replace(/^# (.*$)/gm, '
$1
')
html = html.replace(/^## (.*$)/gm, '
$1
')
html = html.replace(/^### (.*$)/gm, '
$1
')
html = html.replace(/^#### (.*$)/gm, '
$1
')
// Convert bold and italic
html = html.replace(/\*\*(.*?)\*\*/g, '
$1')
html = html.replace(/\*(.*?)\*/g, '
$1')
// Convert inline code
html = html.replace(/`([^`]+)`/g, '
$1')
// Convert links
html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '
$1')
// Convert horizontal rules
html = html.replace(/^---$/gm, '
')
// Remove image syntax (since we can't embed images in PDF easily)
html = html.replace(/!\[([^\]]*)\]\([^)]+\)/g, '')
// Convert paragraphs
html = html.replace(/\n\n/g, '
')
html = '
' + html + '
'
// Clean up empty paragraphs
html = html.replace(/
<\/p>/g, '')
html = html.replace(/
(\s*)\s*<\/p>/g, '$1')
html = html.replace(/(\s*
)\s*<\/p>/g, '$1')
html = html.replace(/(\s*
)\s*<\/p>/g, '$1')
// Convert lists
html = html.replace(/^\* (.*$)/gm, '
$1')
html = html.replace(/^- (.*$)/gm, '
$1')
html = html.replace(/^(\d+)\. (.*$)/gm, '
$2')
// Wrap consecutive list items in ul/ol tags
html = html.replace(/(
.*?<\/li>)(\s*.*?<\/li>)*/gs, (match) => {
return ''
})
// Handle code blocks
html = html.replace(/```(\w+)?\n([\s\S]*?)\n```/g, (match, lang, code) => {
return `${code}
`
})
// Convert custom SVG diagram calls
html = html.replace(/\$\{generateBrainyLogo\(\)\}/g, generateBrainyLogo())
html = html.replace(/\$\{generateArchitectureDiagram\(\)\}/g, generateArchitectureDiagram())
html = html.replace(/\$\{generateDataFlowDiagram\(\)\}/g, generateDataFlowDiagram())
html = html.replace(/\$\{generateHNSWDiagram\(\)\}/g, generateHNSWDiagram())
// Handle special divs
html = html.replace(/([\s\S]*?)<\/div>/g, '
$2
')
// Add page breaks for major sections
html = html.replace(/
/g, '
')
// Enhance key features section
html = html.replace(/### Key Features([\s\S]*?)(?=|$)/g, (match) => {
return match.replace(/
/g, '')
})
return html
}
// Main function to generate PDF
async function generatePDF() {
try {
console.log('π Starting PDF generation...')
// Check required files exist
if (!fs.existsSync(config.readmeFile)) {
throw new Error(`README file not found: ${config.readmeFile}`)
}
if (!fs.existsSync(config.architectureFile)) {
throw new Error(`Architecture file not found: ${config.architectureFile}`)
}
console.log('π Generating comprehensive content...')
const comprehensiveContent = generateComprehensiveContent()
console.log('π Converting to HTML...')
const contentHTML = markdownToHTML(comprehensiveContent)
const fullHTML = htmlTemplate.replace('{{CONTENT}}', contentHTML)
// Launch Puppeteer
console.log('π Launching browser...')
const browser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox', '--disable-setuid-sandbox']
})
const page = await browser.newPage()
// Set content and wait for SVG diagrams to render
await page.setContent(fullHTML, { waitUntil: 'networkidle0' })
console.log('π¨ Rendering SVG diagrams...')
// Wait for all SVG diagrams to be present
await page.waitForFunction(() => {
const svgElements = document.querySelectorAll('.svg-diagram svg')
return svgElements.length >= 3 // We have 3 main diagrams
}, { timeout: 15000 })
// Generate PDF with professional settings
console.log('π Generating PDF...')
await page.pdf({
path: config.outputFile,
format: 'A4',
printBackground: true,
margin: {
top: '20mm',
right: '15mm',
bottom: '20mm',
left: '15mm'
},
displayHeaderFooter: true,
headerTemplate: `
Brainy Architecture Documentation
`,
footerTemplate: `
Generated from Brainy v0.34.0 | Page of
`
})
await browser.close()
console.log('β
PDF generated successfully!')
console.log(`π Output file: ${config.outputFile}`)
// Check file size
const stats = fs.statSync(config.outputFile)
const fileSizeInMB = (stats.size / (1024 * 1024)).toFixed(2)
console.log(`π File size: ${fileSizeInMB} MB`)
} catch (error) {
console.error('β Error generating PDF:', error)
process.exit(1)
}
}
// Check dependencies
async function checkDependencies() {
try {
const puppeteerVersion = (await import('puppeteer')).default
console.log('β
Dependencies verified')
} catch (error) {
console.error('β Missing dependencies. Run: npm install puppeteer')
process.exit(1)
}
}
// Main execution
async function main() {
console.log('ποΈ Brainy Architecture PDF Generator')
console.log('=====================================')
await checkDependencies()
// Ensure output directory exists
const outputDir = path.dirname(config.outputFile)
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true })
}
await generatePDF()
console.log('=====================================')
console.log('π PDF generation complete!')
}
// Run if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch(console.error)
}
export { generatePDF, config }