#!/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 `

Brainy System Architecture

Environment Detection & Adaptation Browser Node.js Serverless Container Server Cloud Brainy Data API add() | search() | addVerb() | get() | delete() | backup() | restore() Augmentation Pipeline SENSE MEMORY COGNITION CONDUIT PERCEPTION DIALOG Vector Processing TensorFlow Embedding HNSW Index Adaptive Storage Hot Cache Warm Cache Cold Storage OPFS FileSystem S3/Cloud Memory Cross-Platform Synchronization WebSocket Conduit WebRTC Conduit MCP Protocol
`; } function generateDataFlowDiagram() { return `

Data Processing Flow

Raw Data Text/JSON Images/Media Embedding TensorFlow.js GPU Accel Vector Index HNSW Algorithm Graph Build Noun-Verb Relations Storage Multi-tier Adaptive Query Processing Semantic Search Graph Traversal Result Ranking Performance Multithreading Caching Scalability Auto-tuning Load Balancing Synchronization Real-time Cross-platform Intelligence Adaptive Learning Pattern Analysis
`; } function generateHNSWDiagram() { return `

HNSW (Hierarchical Navigable Small World) Index Structure

Layer 2 Layer 1 Layer 0 (Base Layer) Search Path (Logarithmic Complexity) HNSW Benefits β€’ Fast similarity search (log complexity) β€’ Memory efficient indexing β€’ Scalable to millions of vectors β€’ Configurable precision/performance
`; } // Retro logo-inspired diagram for title page function generateBrainyLogo() { return `
BRAINY ARCHITECTURE
`; } // HTML template with Apple-inspired structure const htmlTemplate = ` Brainy Architecture Documentation ${appleInspiredCSS} {{CONTENT}} ` // Generate comprehensive content combining README and architecture documentation function generateComprehensiveContent() { const readmeContent = fs.readFileSync(config.readmeFile, 'utf8') const architectureContent = fs.readFileSync(config.architectureFile, 'utf8') // Extract key sections from README const overviewMatch = readmeContent.match(/## ✨ Overview([\s\S]*?)(?=\n## |$)/) const featuresMatch = readmeContent.match(/### πŸš€ Key Features([\s\S]*?)(?=\n## |\n### |$)/) const howItWorksMatch = readmeContent.match(/## 🧩 How It Works([\s\S]*?)(?=\n## |$)/) const pipelineMatch = readmeContent.match(/## πŸš€ The Brainy Pipeline([\s\S]*?)(?=\n## |$)/) const dataModelMatch = readmeContent.match(/## Data Model([\s\S]*?)(?=\n## |$)/) const scalingMatch = readmeContent.match(/## πŸ“ˆ Scaling Strategy([\s\S]*?)(?=\n## |$)/) const performanceMatch = readmeContent.match(/### Performance Tuning([\s\S]*?)(?=\n## |\n### |$)/) const content = ` ${generateBrainyLogo()} # Brainy Architecture Documentation
A Comprehensive Guide to the Advanced AI Graph Database Platform

Table of Contents

1. Executive Summary 2. System Overview & Key Features 3. Core Architecture & Design Principles 4. Advanced Data Processing Pipeline 5. Vector Search & Graph Database Technology 6. Storage Architecture & Environment Adaptation 7. Performance & Scalability Features 8. Cross-Platform Integration & Synchronization 9. Technical Implementation Details 10. Architecture Diagrams & Visual Guide

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 }