137 lines
5.0 KiB
TypeScript
137 lines
5.0 KiB
TypeScript
/**
|
|
* LearningBridge - Connects AutoMemoryBridge to NeuralLearningSystem
|
|
*
|
|
* When insights are recorded via AutoMemoryBridge, this module triggers
|
|
* neural learning trajectories so the system continuously improves from
|
|
* its own discoveries. The NeuralLearningSystem dependency is optional:
|
|
* when unavailable, all operations degrade gracefully to no-ops.
|
|
*
|
|
* @module @claude-flow/memory/learning-bridge
|
|
*/
|
|
import { EventEmitter } from 'node:events';
|
|
import type { IMemoryBackend, SONAMode } from './types.js';
|
|
import type { MemoryInsight } from './auto-memory-bridge.js';
|
|
/**
|
|
* Factory function that returns a neural system instance.
|
|
* Used for dependency injection so tests can supply a mock.
|
|
*/
|
|
export type NeuralLoader = () => Promise<any>;
|
|
/** Configuration for the LearningBridge */
|
|
export interface LearningBridgeConfig {
|
|
/** SONA operating mode (default: 'balanced') */
|
|
sonaMode?: SONAMode;
|
|
/** Per-hour confidence decay rate (default: 0.005) */
|
|
confidenceDecayRate?: number;
|
|
/** Confidence boost per access (default: 0.03) */
|
|
accessBoostAmount?: number;
|
|
/** Maximum confidence value (default: 1.0) */
|
|
maxConfidence?: number;
|
|
/** Minimum confidence floor (default: 0.1) */
|
|
minConfidence?: number;
|
|
/** EWC regularization strength (default: 2000) */
|
|
ewcLambda?: number;
|
|
/** Min active trajectories before consolidation runs (default: 10) */
|
|
consolidationThreshold?: number;
|
|
/** Enable the bridge (default: true). When false all methods are no-ops */
|
|
enabled?: boolean;
|
|
/**
|
|
* Optional factory for the neural learning system.
|
|
* When provided, this replaces the default dynamic import of @claude-flow/neural.
|
|
* Primarily used for testing.
|
|
*/
|
|
neuralLoader?: NeuralLoader;
|
|
}
|
|
/** Aggregated learning statistics */
|
|
export interface LearningStats {
|
|
totalTrajectories: number;
|
|
completedTrajectories: number;
|
|
activeTrajectories: number;
|
|
totalConsolidations: number;
|
|
totalDecays: number;
|
|
avgConfidenceBoost: number;
|
|
neuralAvailable: boolean;
|
|
}
|
|
/** Result returned by consolidate() */
|
|
export interface ConsolidateResult {
|
|
trajectoriesCompleted: number;
|
|
patternsLearned: number;
|
|
entriesUpdated: number;
|
|
durationMs: number;
|
|
}
|
|
/** A single pattern match returned by findSimilarPatterns() */
|
|
export interface PatternMatch {
|
|
content: string;
|
|
similarity: number;
|
|
category: string;
|
|
confidence: number;
|
|
}
|
|
/**
|
|
* Connects AutoMemoryBridge insights to the NeuralLearningSystem.
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* const bridge = new LearningBridge(memoryBackend);
|
|
* await bridge.onInsightRecorded(insight, entryId);
|
|
* await bridge.onInsightAccessed(entryId);
|
|
* const result = await bridge.consolidate();
|
|
* ```
|
|
*/
|
|
export declare class LearningBridge extends EventEmitter {
|
|
private neural;
|
|
private backend;
|
|
private config;
|
|
private activeTrajectories;
|
|
private stats;
|
|
private destroyed;
|
|
private neuralInitPromise;
|
|
constructor(backend: IMemoryBackend, config?: LearningBridgeConfig);
|
|
/**
|
|
* Notify the bridge that an insight has been recorded in AgentDB.
|
|
* Creates a learning trajectory so the neural system can track the
|
|
* insight's lifecycle.
|
|
*/
|
|
onInsightRecorded(insight: MemoryInsight, entryId: string): Promise<void>;
|
|
/**
|
|
* Notify the bridge that an insight entry was accessed.
|
|
* Boosts confidence in the backend and records a step in the
|
|
* trajectory if one exists.
|
|
*/
|
|
onInsightAccessed(entryId: string): Promise<void>;
|
|
/**
|
|
* Consolidate active trajectories by completing them in the neural system.
|
|
* Only runs when there are enough active trajectories to justify the cost.
|
|
*/
|
|
consolidate(): Promise<ConsolidateResult>;
|
|
/**
|
|
* Apply time-based confidence decay to entries in the given namespace.
|
|
* Entries not accessed for more than one hour see their confidence reduced
|
|
* proportionally to the hours elapsed, down to minConfidence.
|
|
*
|
|
* @returns number of entries whose confidence was lowered
|
|
*/
|
|
decayConfidences(namespace: string): Promise<number>;
|
|
/**
|
|
* Find patterns similar to the given content using the neural system.
|
|
* Returns an empty array when the neural system is unavailable.
|
|
*/
|
|
findSimilarPatterns(content: string, k?: number): Promise<PatternMatch[]>;
|
|
/** Return aggregated learning statistics */
|
|
getStats(): LearningStats;
|
|
/** Tear down the bridge. Subsequent method calls become no-ops. */
|
|
destroy(): void;
|
|
/**
|
|
* Lazily attempt to load and initialize the NeuralLearningSystem.
|
|
* The promise is cached so that repeated calls do not re-attempt
|
|
* after a failure.
|
|
*/
|
|
private initNeural;
|
|
private loadNeural;
|
|
/**
|
|
* Create a deterministic hash-based embedding for content.
|
|
* This is a lightweight stand-in for a real embedding model,
|
|
* suitable for pattern matching within the neural trajectory system.
|
|
*/
|
|
private createHashEmbedding;
|
|
}
|
|
export default LearningBridge;
|
|
//# sourceMappingURL=learning-bridge.d.ts.map
|