- Updated version to `0.10.0` in `README.md`, `cli-package/package.json`, and `src/utils/version.ts`. - Removed `CHANGELOG.md` from the root and `cli-package`, as GitHub auto-generates release notes. - Refactored `brainy-wrapper.js` to simplify utility methods (`isFloat32Array` and `isTypedArray`) by moving them to instance-level definitions. - Standardized Node.js version requirement to `>= 24.3.0` across documentation and configuration files. - Improved `textEncoding.ts` by introducing a unified `Platform` class for cross-environment compatibility (Node.js and browser). - Restructured scripts: - Replaced outdated commands (e.g., `test-cli` -> `test:cli` and `test-all` -> `test:all`) for consistency. - Removed changelog updates from `scripts/create-github-release.js` as part of simplification. - Enhanced `patch-textencoder.js` to also patch `worker.js` for both `TextEncoder` and `TextDecoder`. This release improves compatibility with Node.js 24, simplifies the deployment/changelog process, and refines text encoding and worker functionalities.
7 KiB
Brainy Developer Guide
This document contains detailed information for developers working with Brainy, including building, testing, and publishing instructions.
Table of Contents
- Build System
- Testing
- Publishing
- Development Usage
- Node.js 24 Optimizations
- Development Workflow
- Reporting Issues
- Code Style Guidelines
- Badge Maintenance
Build System
Brainy uses a modern build system that optimizes for both Node.js and browser environments:
-
ES Modules
- Built as ES modules for maximum compatibility
- Works in modern browsers and Node.js environments
- Separate optimized builds for browser and Node.js
-
Environment-Specific Builds
- Node.js Build: Optimized for server environments with full functionality
- Browser Build: Optimized for browser environments with reduced bundle size
- CLI Build: Separate build for command-line interface functionality
- Conditional exports in package.json for automatic environment detection
-
Modular Architecture
- Core functionality and CLI are built separately
- CLI (4MB) is only included when explicitly imported or used from command line
- Reduced bundle size for browser and Node.js applications
-
Environment Detection
- Automatically detects whether it's running in a browser or Node.js
- Loads appropriate dependencies and functionality based on the environment
- Provides consistent API across all environments
-
TypeScript
- Written in TypeScript for type safety and better developer experience
- Generates type definitions for TypeScript users
- Compiled to ES2020 for modern JavaScript environments
-
Build Scripts
npm run build: Builds the core library without CLInpm run build:browser: Builds the browser-optimized versionnpm run build:cli: Builds the CLI version (only needed for CLI usage)npm run prepare:cli: Builds the CLI for command-line usagenpm run demo: Builds both core library and browser versions and starts a demo server- GitHub Actions workflow: Automatically deploys the demo directory to GitHub Pages when pushing to the main branch
Testing
Testing All Environments
Brainy provides a comprehensive test script that verifies the library works correctly in all supported environments ( browser, Node.js, and CLI):
# Test the library in all environments
npm run test:all
This script:
- Builds all packages (main, browser, CLI)
- Runs Node.js tests (worker tests and unified text encoding test)
- Starts a local HTTP server and runs browser tests using Puppeteer (headless browser)
- Runs CLI tests by installing the CLI package locally and testing basic commands
The test results are displayed with color-coded output for better readability.
Testing the CLI Package Locally
Before publishing the CLI package to npm, you can test it locally to ensure it works as expected:
# Test the CLI package locally
npm run test:cli
This script:
- Builds the main package
- Creates a local tarball of the main package
- Builds the CLI package
- Updates the CLI package to use the local main package
- Creates a local tarball of the CLI package
- Installs the CLI package globally for testing
After running this script, you can use the CLI commands as if you had installed the package from npm:
# Test the CLI
brainy --version
brainy init
brainy add "Test data" '{"noun":"Thing"}'
brainy search "test"
When you're done testing, you can uninstall the CLI package:
npm uninstall -g @soulcraft/brainy-cli
Publishing
Publishing the CLI Package
If you need to publish the CLI package to npm, please refer to the CLI Publishing Guide for detailed instructions.
Development Usage
# Run the CLI directly from the source
npm run cli help
# Generate a random graph for testing
npm run cli generate-random-graph --noun-count 20 --verb-count 40
Node.js 24 Optimizations
Brainy takes advantage of several optimizations available in Node.js 24:
-
Improved Worker Threads Performance: The multithreading system has been completely rewritten to leverage Node.js 24's enhanced Worker Threads API, resulting in better performance for compute-intensive operations like embedding generation and vector similarity calculations.
-
Worker Pool Management: A sophisticated worker pool system reuses worker threads to minimize the overhead of creating and destroying threads, leading to more efficient resource utilization.
-
Dynamic Module Imports: Uses the new
node:protocol prefix for importing core modules, which provides better performance and more reliable module resolution. -
ES Modules Optimizations: Takes advantage of Node.js 24's improved ESM implementation for faster module loading and execution.
-
Enhanced Error Handling: Implements more robust error handling patterns available in Node.js 24 for better stability and debugging.
These optimizations are particularly beneficial for:
- Large-scale vector operations
- Batch processing of embeddings
- Real-time data processing pipelines
- High-throughput search operations
Development Workflow
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
Reporting Issues
We use GitHub issues to track bugs and feature requests. Please use the provided issue templates when creating a new issue:
Code Style Guidelines
Brainy follows a specific code style to maintain consistency throughout the codebase:
- No Semicolons: All code in the project should avoid using semicolons wherever possible
- Formatting: The project uses Prettier for code formatting
- Linting: ESLint is configured with specific rules for the project
- TypeScript Configuration: Strict type checking enabled with ES2020 target
- Commit Messages: Use the imperative mood and keep the first line concise
Badge Maintenance
The README badges are automatically updated during the build process:
- npm Version Badge: The npm version badge is automatically updated to match the version in package.json when:
- Running
npm run build(via the prebuild script) - Running
npm versioncommands (patch, minor, major) - Manually running
node scripts/generate-version.js
- Running
This ensures that the badge always reflects the current version in package.json, even before publishing to npm.