|
| 1 | +# Copilot Instructions for HPCC-JS-WASM |
| 2 | +*Last updated: July 2025 | For AI assistants working with hpcc-systems/hpcc-js-wasm* |
| 3 | + |
| 4 | +This document provides guidance for AI assistants working with the HPCC-JS-WASM repository. |
| 5 | + |
| 6 | +## Repository Overview |
| 7 | + |
| 8 | +This is a **monorepo** that provides WebAssembly (WASM) versions of popular C++ libraries for use in Node.js, web browsers, and JavaScript applications. It uses **Lerna** for monorepo management and **npm workspaces**. |
| 9 | + |
| 10 | +### Packages Provided |
| 11 | + |
| 12 | +1. **@hpcc-js/wasm-base91** - Base91 encoding/decoding library |
| 13 | +2. **@hpcc-js/wasm-duckdb** - DuckDB embedded database |
| 14 | +3. **@hpcc-js/wasm-expat** - Expat XML parser |
| 15 | +4. **@hpcc-js/wasm-graphviz** - Graphviz graph visualization library |
| 16 | +5. **@hpcc-js/wasm-graphviz-cli** - Command-line interface for Graphviz |
| 17 | +6. **@hpcc-js/wasm-llama** - Llama.cpp AI model library |
| 18 | +7. **@hpcc-js/wasm-zstd** - Zstandard compression library |
| 19 | +8. **@hpcc-js/wasm** - Meta package for backward compatibility |
| 20 | + |
| 21 | +## Architecture |
| 22 | + |
| 23 | +### File Structure |
| 24 | +``` |
| 25 | +├── packages/ # Individual WASM packages |
| 26 | +│ ├── base91/ # Base91 package |
| 27 | +│ ├── duckdb/ # DuckDB package |
| 28 | +│ ├── expat/ # Expat package |
| 29 | +│ ├── graphviz/ # Graphviz package |
| 30 | +│ ├── graphviz-cli/ # Graphviz CLI package |
| 31 | +│ ├── llama/ # Llama package |
| 32 | +│ ├── wasm/ # Meta package |
| 33 | +│ └── zstd/ # Zstd package |
| 34 | +├── src-cpp/ # Shared C++ source code |
| 35 | +├── scripts/ # Build and utility scripts |
| 36 | +├── docs/ # Documentation (VitePress) |
| 37 | +└── .vscode/ # VSCode configuration |
| 38 | +``` |
| 39 | + |
| 40 | +### Package Structure Pattern |
| 41 | +Each package typically contains: |
| 42 | +``` |
| 43 | +packages/[name]/ |
| 44 | +├── src/ # TypeScript source |
| 45 | +├── src-cpp/ # C++ source code |
| 46 | +├── tests/ # Test files |
| 47 | +├── package.json # Package configuration |
| 48 | +└── vitest.config.ts # Test configuration |
| 49 | +``` |
| 50 | + |
| 51 | +## Development Workflow |
| 52 | + |
| 53 | +### Prerequisites for Full Development |
| 54 | +```bash |
| 55 | +# Install dependencies |
| 56 | +npm ci |
| 57 | + |
| 58 | +# Install build dependencies (requires system tools) |
| 59 | +npm run install-build-deps # Installs emsdk, vcpkg, playwright |
| 60 | + |
| 61 | +# Build C++ to WASM (requires emscripten) |
| 62 | +npm run build-cpp |
| 63 | + |
| 64 | +# Build TypeScript packages |
| 65 | +npm run build-ws |
| 66 | +``` |
| 67 | + |
| 68 | +### Quick Development (TypeScript only) |
| 69 | +```bash |
| 70 | +# Install dependencies |
| 71 | +npm ci |
| 72 | + |
| 73 | +# Build only TypeScript (without C++ compilation) |
| 74 | +npm run build-ws |
| 75 | + |
| 76 | +# Run linting |
| 77 | +npm run lint |
| 78 | + |
| 79 | +# Note: Tests may fail without WASM builds |
| 80 | +``` |
| 81 | + |
| 82 | +### Testing |
| 83 | +```bash |
| 84 | +# Run all tests (requires full build) |
| 85 | +npm run test |
| 86 | + |
| 87 | +# Run specific package tests |
| 88 | +cd packages/graphviz && npm test |
| 89 | + |
| 90 | +# Run browser tests |
| 91 | +npm run test-browser |
| 92 | + |
| 93 | +# Run node tests |
| 94 | +npm run test-node |
| 95 | +``` |
| 96 | + |
| 97 | +## Common Patterns |
| 98 | + |
| 99 | +### WASM Library Loading Pattern |
| 100 | +All WASM libraries follow this pattern: |
| 101 | +```typescript |
| 102 | +import { LibraryName } from "@hpcc-js/wasm-libraryname"; |
| 103 | + |
| 104 | +// Async loading required |
| 105 | +const library = await LibraryName.load(); |
| 106 | + |
| 107 | +// Use library methods |
| 108 | +const result = library.someMethod(input); |
| 109 | +``` |
| 110 | + |
| 111 | +### Package Export Structure |
| 112 | +```typescript |
| 113 | +// src/index.ts - Main entry point |
| 114 | +export * from "./libraryname.ts"; |
| 115 | + |
| 116 | +// src/libraryname.ts - Main implementation |
| 117 | +export class LibraryName { |
| 118 | + static async load(): Promise<LibraryName> { ... } |
| 119 | + someMethod(input: string): string { ... } |
| 120 | + version(): string { ... } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### Testing Patterns |
| 125 | +```typescript |
| 126 | +import { describe, it, expect } from "vitest"; |
| 127 | +import { LibraryName } from "@hpcc-js/wasm-libraryname"; |
| 128 | + |
| 129 | +describe("LibraryName", () => { |
| 130 | + it("basic functionality", async () => { |
| 131 | + const lib = await LibraryName.load(); |
| 132 | + const result = lib.someMethod("test"); |
| 133 | + expect(result).toBeDefined(); |
| 134 | + }); |
| 135 | +}); |
| 136 | +``` |
| 137 | + |
| 138 | +## Key Technologies |
| 139 | + |
| 140 | +- **TypeScript** - Primary language for package implementations |
| 141 | +- **C++** - Source libraries compiled to WASM |
| 142 | +- **Emscripten** - C++ to WASM compilation toolchain |
| 143 | +- **Lerna** - Monorepo management |
| 144 | +- **Vitest** - Testing framework (with browser support) |
| 145 | +- **ESBuild** - TypeScript bundling |
| 146 | +- **VitePress** - Documentation generation |
| 147 | + |
| 148 | +## Build System |
| 149 | + |
| 150 | +### C++ Compilation Chain |
| 151 | +1. **vcpkg** - C++ package manager for dependencies |
| 152 | +2. **emscripten** - Compiles C++ to WASM |
| 153 | +3. **IDL files** - WebIDL bindings for C++ classes |
| 154 | +4. **Custom esbuild plugins** - Handle WASM imports |
| 155 | + |
| 156 | +### TypeScript Build |
| 157 | +1. **tsc** - Type generation |
| 158 | +2. **esbuild** - Bundling (ESM, CJS, UMD formats) |
| 159 | +3. **Custom plugins** - WASM asset handling |
| 160 | + |
| 161 | +## Common Issues and Solutions |
| 162 | + |
| 163 | +### Build Failures |
| 164 | +- **Missing WASM files**: Run `npm run build-cpp` first |
| 165 | +- **Type errors**: Ensure all packages are built with `npm run build-ws` |
| 166 | +- **Test failures in fresh clone**: Expected - requires full build process |
| 167 | + |
| 168 | +### Package Resolution Issues |
| 169 | +- Packages depend on each other being built |
| 170 | +- Use relative imports for local development |
| 171 | +- Run `lerna run build` to build all packages |
| 172 | + |
| 173 | +### WASM Loading Issues |
| 174 | +- WASM files must be accessible at runtime |
| 175 | +- Browser requires proper MIME types for .wasm files |
| 176 | +- Node.js needs appropriate file system access |
| 177 | + |
| 178 | +## Debugging |
| 179 | + |
| 180 | +### VSCode Configuration |
| 181 | +- Launch configurations provided for browser and Node.js debugging |
| 182 | +- Tasks configured for watch mode development |
| 183 | +- CMake integration for C++ development |
| 184 | + |
| 185 | +### Log Levels |
| 186 | +Most libraries support debug output: |
| 187 | +```typescript |
| 188 | +// Enable debug logging |
| 189 | +const lib = await LibraryName.load({ debug: true }); |
| 190 | +``` |
| 191 | + |
| 192 | +## Documentation |
| 193 | + |
| 194 | +- **API Docs**: Generated with TypeDoc at https://hpcc-systems.github.io/hpcc-js-wasm/ |
| 195 | +- **Examples**: See `/examples` directory and live demos in root: |
| 196 | + - `hw-graphviz.html` - Graphviz graph rendering |
| 197 | + - `hw-zstd.html` - Compression examples |
| 198 | + - `hw-base91.html` - Encoding demos |
| 199 | + - `index.html` - Package showcase |
| 200 | +- **Package READMEs**: Each package has specific documentation |
| 201 | + |
| 202 | +## AI Assistant Guidelines |
| 203 | + |
| 204 | +### When Making Changes |
| 205 | +1. **Understand the monorepo structure** - changes may affect multiple packages |
| 206 | +2. **Follow existing patterns** - each package follows similar structure |
| 207 | +3. **Test incrementally** - build and test after each significant change |
| 208 | +4. **Consider WASM implications** - changes to C++ require full rebuild |
| 209 | +5. **Update documentation** - maintain TypeDoc comments and READMEs |
| 210 | + |
| 211 | +### Safe Operations (No Full Build Required) |
| 212 | +- TypeScript code changes in `src/` directories |
| 213 | +- Test file modifications |
| 214 | +- Documentation updates |
| 215 | +- Package.json script modifications |
| 216 | + |
| 217 | +### Operations Requiring Full Build |
| 218 | +- C++ source code changes in `src-cpp/` directories |
| 219 | +- WebIDL (.idl) file changes |
| 220 | +- CMakeLists.txt modifications |
| 221 | +- New WASM library additions |
| 222 | + |
| 223 | +### Before Submitting Changes |
| 224 | +1. Run `npm run lint` to check code style |
| 225 | +2. Run `npm run build-ws` to ensure TypeScript builds |
| 226 | +3. If C++ was modified, run full build cycle |
| 227 | +4. Test affected packages individually |
| 228 | +5. Update relevant documentation |
0 commit comments