brainy/scripts/check-type-embeddings.cjs
David Snelling 0d649b8a79 perf: pre-compute type embeddings at build time (zero runtime cost)
Major optimization - all type embeddings now built into package:

Build-time generation:
- Created scripts/buildTypeEmbeddings.ts to generate all type embeddings
- Generates embeddings for 31 NounTypes + 40 VerbTypes at build time
- Stores as base64-encoded binary data in embeddedTypeEmbeddings.ts
- Added check script to rebuild only when needed

Updated all consumers:
- NeuralEntityExtractor: loads pre-computed embeddings (instant)
- BrainyTypes: loads pre-computed embeddings (instant init)
- NaturalLanguageProcessor: loads pre-computed embeddings (instant init)

Build process:
- Added npm run build:types to generate embeddings
- Added npm run build:types:if-needed for conditional rebuild
- Integrated into main build pipeline
- Auto-rebuilds only when types or build script change

Benefits:
- Zero runtime cost - embeddings loaded instantly
- Survives all container restarts
- All 71 types always available (31 nouns + 40 verbs)
- ~100KB memory overhead for permanent performance gain
- Eliminates 5-10 second initialization delay

This completes the type embedding optimization started in v3.32.5
2025-10-09 18:08:57 -07:00

43 lines
1.5 KiB
JavaScript

#!/usr/bin/env node
/**
* Check if type embeddings need rebuilding
* Only rebuild if:
* 1. embeddedTypeEmbeddings.ts doesn't exist
* 2. Type definitions have changed
* 3. Build script has changed
*/
const fs = require('fs');
const path = require('path');
const EMBEDDED_FILE = path.join(__dirname, '../src/neural/embeddedTypeEmbeddings.ts');
const BUILD_SCRIPT = path.join(__dirname, 'buildTypeEmbeddings.ts');
const GRAPH_TYPES = path.join(__dirname, '../src/types/graphTypes.ts');
// Check if embedded type embeddings exist
if (!fs.existsSync(EMBEDDED_FILE)) {
console.log('❌ Embedded type embeddings not found. Building...');
process.exit(1); // Signal need to rebuild
}
// Check if build script is newer than embedded embeddings
const embeddedStats = fs.statSync(EMBEDDED_FILE);
const buildScriptStats = fs.statSync(BUILD_SCRIPT);
if (buildScriptStats.mtime > embeddedStats.mtime) {
console.log('🔄 Build script has changed. Rebuilding type embeddings...');
process.exit(1); // Signal need to rebuild
}
// Check if type definitions are newer than embedded embeddings
if (fs.existsSync(GRAPH_TYPES)) {
const graphTypesStats = fs.statSync(GRAPH_TYPES);
if (graphTypesStats.mtime > embeddedStats.mtime) {
console.log('🔄 Type definitions have changed. Rebuilding type embeddings...');
process.exit(1); // Signal need to rebuild
}
}
console.log('✅ Embedded type embeddings are up-to-date. Skipping rebuild.');
process.exit(0); // No rebuild needed