feat: implement comprehensive type safety system with BrainyTypes API
Major enhancements for type safety and developer experience: - Add BrainyTypes static API for type management and AI-powered suggestions - Implement strict type validation for all 31 NounType categories - Remove dangerous generic add() method that bypassed type safety - Add intelligent type inference with confidence scoring - Provide helpful error messages with typo suggestions using Levenshtein distance - Update all internal code, examples, and documentation to use typed methods - Enhance CLI with new type management commands (types, suggest, validate) Breaking changes: - Remove deprecated add() method - use addNoun() with explicit type parameter - All addNoun() calls now require explicit type as second parameter This release significantly improves type safety across the entire system while maintaining backward compatibility for properly typed method calls. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
d7d2d749b6
commit
6c62bc4e9d
40 changed files with 1704 additions and 497 deletions
71
demo-strict-types.js
Normal file
71
demo-strict-types.js
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
// Demo: Strict Type Enforcement (Now Default!)
|
||||
import { BrainyData, NounType } from './dist/index.js'
|
||||
|
||||
async function demo() {
|
||||
console.log('🚨 Brainy 3.0: Strict Types by Default!\n')
|
||||
console.log('=' .repeat(50) + '\n')
|
||||
|
||||
// Create instance with default config (STRICT MODE)
|
||||
const brain = new BrainyData({
|
||||
storage: { forceMemoryStorage: true }
|
||||
})
|
||||
await brain.init()
|
||||
|
||||
console.log('❌ Test 1: Old API fails by default')
|
||||
console.log('----------------------------------------')
|
||||
try {
|
||||
// This WILL FAIL - no type specified
|
||||
await brain.addNoun('Some data', { metadata: 'stuff' })
|
||||
} catch (error) {
|
||||
console.log('Error (expected):', error.message.split('\n')[0])
|
||||
console.log('✅ Good! Forces you to specify type.\n')
|
||||
}
|
||||
|
||||
console.log('✅ Test 2: New API with explicit types works')
|
||||
console.log('---------------------------------------------')
|
||||
|
||||
const personId = await brain.addNoun(
|
||||
'John Doe',
|
||||
NounType.Person,
|
||||
{ role: 'Engineer' }
|
||||
)
|
||||
console.log(`Added person with ID: ${personId}`)
|
||||
|
||||
const docId = await brain.addNoun(
|
||||
'API Documentation',
|
||||
NounType.Document,
|
||||
{ version: '2.0' }
|
||||
)
|
||||
console.log(`Added document with ID: ${docId}\n`)
|
||||
|
||||
console.log('🔄 Test 3: Compatibility mode (opt-in only)')
|
||||
console.log('--------------------------------------------')
|
||||
|
||||
// Must explicitly enable compatibility mode
|
||||
const compatBrain = new BrainyData({
|
||||
storage: { forceMemoryStorage: true },
|
||||
typeCompatibilityMode: true, // EXPLICIT OPT-IN
|
||||
logging: { verbose: false }
|
||||
})
|
||||
await compatBrain.init()
|
||||
|
||||
// Now old API works (with warnings if verbose: true)
|
||||
const oldApiId = await compatBrain.addNoun(
|
||||
'Old style data',
|
||||
{ someField: 'value' }
|
||||
)
|
||||
console.log(`Compatibility mode allows old API: ${oldApiId}\n`)
|
||||
|
||||
console.log('📊 Summary: Why Strict Mode is Better')
|
||||
console.log('--------------------------------------')
|
||||
console.log('1. Forces explicit types → Better data quality')
|
||||
console.log('2. No ambiguous "content" everywhere')
|
||||
console.log('3. AI works better with typed data')
|
||||
console.log('4. Prevents technical debt')
|
||||
console.log('5. Can always opt-in to compatibility if needed')
|
||||
console.log('\n✨ Brainy 3.0: Type Safety First!')
|
||||
}
|
||||
|
||||
// Bypass version check for demo
|
||||
process.env.BRAINY_SKIP_VERSION_CHECK = 'true'
|
||||
demo().catch(console.error)
|
||||
Loading…
Add table
Add a link
Reference in a new issue