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
|
|
@ -193,7 +193,7 @@ export class APIServerAugmentation extends BaseAugmentation {
|
|||
app.post('/api/add', async (req: any, res: any) => {
|
||||
try {
|
||||
const { content, metadata } = req.body
|
||||
const id = await this.context!.brain.add(content, metadata)
|
||||
const id = await this.context!.brain.addNoun(content, 'Content', metadata)
|
||||
res.json({ success: true, id })
|
||||
} catch (error: any) {
|
||||
res.status(500).json({ success: false, error: error.message })
|
||||
|
|
@ -367,7 +367,7 @@ export class APIServerAugmentation extends BaseAugmentation {
|
|||
break
|
||||
|
||||
case 'add':
|
||||
const id = await this.context!.brain.add(msg.content, msg.metadata)
|
||||
const id = await this.context!.brain.addNoun(msg.content, 'Content', msg.metadata)
|
||||
socket.send(JSON.stringify({
|
||||
type: 'addResult',
|
||||
requestId: msg.requestId,
|
||||
|
|
|
|||
|
|
@ -255,7 +255,7 @@ export function getFieldPatterns(entityType: 'noun' | 'verb', specificType?: str
|
|||
|
||||
/**
|
||||
* Priority fields for different entity types (for AI analysis)
|
||||
* Used by the IntelligentTypeMatcher and neural processing
|
||||
* Used by the BrainyTypes and neural processing
|
||||
*/
|
||||
export const TYPE_PRIORITY_FIELDS: Record<string, string[]> = {
|
||||
[NounType.Person]: [
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
* Universal Display Augmentation - Intelligent Computation Engine
|
||||
*
|
||||
* Leverages existing Brainy AI infrastructure for intelligent field computation:
|
||||
* - IntelligentTypeMatcher for semantic type detection
|
||||
* - BrainyTypes for semantic type detection
|
||||
* - Neural Import patterns for field analysis
|
||||
* - JSON processing utilities for field extraction
|
||||
* - Existing NounType/VerbType taxonomy (31+40 types)
|
||||
|
|
@ -15,7 +15,7 @@ import type {
|
|||
DisplayConfig
|
||||
} from './types.js'
|
||||
import type { VectorDocument, GraphVerb } from '../../coreTypes.js'
|
||||
import { IntelligentTypeMatcher, getTypeMatcher } from '../typeMatching/intelligentTypeMatcher.js'
|
||||
import { BrainyTypes, getBrainyTypes } from '../typeMatching/brainyTypes.js'
|
||||
import { getNounIcon, getVerbIcon } from './iconMappings.js'
|
||||
import {
|
||||
getFieldPatterns,
|
||||
|
|
@ -31,7 +31,7 @@ import { NounType, VerbType } from '../../types/graphTypes.js'
|
|||
* Coordinates AI-powered analysis with fallback heuristics
|
||||
*/
|
||||
export class IntelligentComputationEngine {
|
||||
private typeMatcher: IntelligentTypeMatcher | null = null
|
||||
private typeMatcher: BrainyTypes | null = null
|
||||
private config: DisplayConfig
|
||||
private initialized = false
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ export class IntelligentComputationEngine {
|
|||
|
||||
try {
|
||||
// 🧠 LEVERAGE YOUR EXISTING AI INFRASTRUCTURE
|
||||
this.typeMatcher = await getTypeMatcher()
|
||||
this.typeMatcher = await getBrainyTypes()
|
||||
if (this.typeMatcher) {
|
||||
console.log('🎨 Display computation engine initialized with AI intelligence')
|
||||
} else {
|
||||
|
|
@ -118,7 +118,7 @@ export class IntelligentComputationEngine {
|
|||
}
|
||||
|
||||
/**
|
||||
* AI-powered computation using your existing IntelligentTypeMatcher
|
||||
* AI-powered computation using your existing BrainyTypes
|
||||
* @param data Entity data/metadata
|
||||
* @param entityType Type of entity (noun/verb)
|
||||
* @param options Additional options
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ export interface FieldComputationContext {
|
|||
}
|
||||
|
||||
/**
|
||||
* Type matching result from IntelligentTypeMatcher
|
||||
* Type matching result from BrainyTypes
|
||||
*/
|
||||
export interface TypeMatchResult {
|
||||
type: string
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import { BaseAugmentation, AugmentationContext } from './brainyAugmentation.js'
|
|||
import { NounType, VerbType } from '../types/graphTypes.js'
|
||||
import * as fs from '../universal/fs.js'
|
||||
import * as path from '../universal/path.js'
|
||||
import { IntelligentTypeMatcher, getTypeMatcher } from './typeMatching/intelligentTypeMatcher.js'
|
||||
import { BrainyTypes, getBrainyTypes } from './typeMatching/brainyTypes.js'
|
||||
import { prodLog } from '../utils/logger.js'
|
||||
|
||||
// Neural Import Analysis Types
|
||||
|
|
@ -74,7 +74,7 @@ export class NeuralImportAugmentation extends BaseAugmentation {
|
|||
|
||||
private config: NeuralImportConfig
|
||||
private analysisCache = new Map<string, NeuralAnalysisResult>()
|
||||
private typeMatcher: IntelligentTypeMatcher | null = null
|
||||
private typeMatcher: BrainyTypes | null = null
|
||||
|
||||
constructor(config: Partial<NeuralImportConfig> = {}) {
|
||||
super()
|
||||
|
|
@ -89,7 +89,7 @@ export class NeuralImportAugmentation extends BaseAugmentation {
|
|||
|
||||
protected async onInitialize(): Promise<void> {
|
||||
try {
|
||||
this.typeMatcher = await getTypeMatcher()
|
||||
this.typeMatcher = await getBrainyTypes()
|
||||
this.log('🧠 Neural Import augmentation initialized with intelligent type matching')
|
||||
} catch (error) {
|
||||
this.log('⚠️ Failed to initialize type matcher, falling back to heuristics', 'warn')
|
||||
|
|
@ -460,7 +460,7 @@ export class NeuralImportAugmentation extends BaseAugmentation {
|
|||
private async inferNounType(obj: any): Promise<string> {
|
||||
if (!this.typeMatcher) {
|
||||
// Initialize type matcher if not available
|
||||
this.typeMatcher = await getTypeMatcher()
|
||||
this.typeMatcher = await getBrainyTypes()
|
||||
}
|
||||
|
||||
const result = await this.typeMatcher.matchNounType(obj)
|
||||
|
|
@ -516,7 +516,7 @@ export class NeuralImportAugmentation extends BaseAugmentation {
|
|||
private async inferVerbType(fieldName: string, sourceObj?: any, targetObj?: any): Promise<string> {
|
||||
if (!this.typeMatcher) {
|
||||
// Initialize type matcher if not available
|
||||
this.typeMatcher = await getTypeMatcher()
|
||||
this.typeMatcher = await getBrainyTypes()
|
||||
}
|
||||
|
||||
const result = await this.typeMatcher.matchVerbType(sourceObj, targetObj, fieldName)
|
||||
|
|
|
|||
|
|
@ -265,7 +265,7 @@ export abstract class SynapseAugmentation extends BaseAugmentation {
|
|||
|
||||
// Store original content with neural metadata
|
||||
if (typeof content === 'string') {
|
||||
await this.context.brain.add(content, {
|
||||
await this.context.brain.addNoun(content, 'Content', {
|
||||
...enrichedMetadata,
|
||||
_neuralProcessed: true,
|
||||
_neuralConfidence: neuralResult.data.confidence,
|
||||
|
|
@ -283,10 +283,10 @@ export abstract class SynapseAugmentation extends BaseAugmentation {
|
|||
|
||||
// Fallback to basic storage
|
||||
if (typeof content === 'string') {
|
||||
await this.context.brain.add(content, enrichedMetadata)
|
||||
await this.context.brain.addNoun(content, 'Content', enrichedMetadata)
|
||||
} else {
|
||||
// For structured data, store as JSON
|
||||
await this.context.brain.add(JSON.stringify(content), enrichedMetadata)
|
||||
await this.context.brain.addNoun(JSON.stringify(content), 'Content', enrichedMetadata)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Intelligent Type Matcher - Uses embeddings for semantic type detection
|
||||
* BrainyTypes - Intelligent type detection using semantic embeddings
|
||||
*
|
||||
* This module uses our existing TransformerEmbedding and similarity functions
|
||||
* to intelligently match data to our 31 noun types and 40 verb types.
|
||||
|
|
@ -139,9 +139,9 @@ export interface TypeMatchResult {
|
|||
}
|
||||
|
||||
/**
|
||||
* Intelligent Type Matcher using semantic embeddings
|
||||
* BrainyTypes - Intelligent type detection for nouns and verbs
|
||||
*/
|
||||
export class IntelligentTypeMatcher {
|
||||
export class BrainyTypes {
|
||||
private embedder: TransformerEmbedding
|
||||
private nounEmbeddings: Map<string, Vector> = new Map()
|
||||
private verbEmbeddings: Map<string, Vector> = new Map()
|
||||
|
|
@ -520,15 +520,15 @@ export class IntelligentTypeMatcher {
|
|||
/**
|
||||
* Singleton instance for efficient reuse
|
||||
*/
|
||||
let globalMatcher: IntelligentTypeMatcher | null = null
|
||||
let globalInstance: BrainyTypes | null = null
|
||||
|
||||
/**
|
||||
* Get or create the global type matcher instance
|
||||
* Get or create the global BrainyTypes instance
|
||||
*/
|
||||
export async function getTypeMatcher(): Promise<IntelligentTypeMatcher> {
|
||||
if (!globalMatcher) {
|
||||
globalMatcher = new IntelligentTypeMatcher()
|
||||
await globalMatcher.init()
|
||||
export async function getBrainyTypes(): Promise<BrainyTypes> {
|
||||
if (!globalInstance) {
|
||||
globalInstance = new BrainyTypes()
|
||||
await globalInstance.init()
|
||||
}
|
||||
return globalMatcher
|
||||
return globalInstance
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
* 🎨 Provides intelligent display fields for any noun or verb using AI-powered analysis
|
||||
*
|
||||
* Features:
|
||||
* - ✅ Leverages existing IntelligentTypeMatcher for semantic type detection
|
||||
* - ✅ Leverages existing BrainyTypes for semantic type detection
|
||||
* - ✅ Complete icon coverage for all 31 NounTypes + 40+ VerbTypes
|
||||
* - ✅ Zero performance impact with lazy computation and intelligent caching
|
||||
* - ✅ Perfect isolation - can be disabled, replaced, or configured
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue