213 lines
5.8 KiB
JavaScript
213 lines
5.8 KiB
JavaScript
/**
|
|
* Neural Substrate Integration
|
|
*
|
|
* Integrates agentic-flow's neural embedding features:
|
|
* - Semantic drift detection
|
|
* - Memory physics (hippocampal dynamics)
|
|
* - Embedding state machine
|
|
* - Swarm coordination
|
|
* - Coherence monitoring
|
|
*
|
|
* These features treat embeddings as a synthetic nervous system.
|
|
*/
|
|
/**
|
|
* Lazy-loaded Neural Substrate wrapper
|
|
*
|
|
* Wraps agentic-flow's NeuralSubstrate with graceful fallback
|
|
*/
|
|
export class NeuralEmbeddingService {
|
|
config;
|
|
substrate = null;
|
|
initialized = false;
|
|
available = false;
|
|
constructor(config = {}) {
|
|
this.config = config;
|
|
}
|
|
/**
|
|
* Initialize neural substrate
|
|
*/
|
|
async init() {
|
|
if (this.initialized)
|
|
return this.available;
|
|
try {
|
|
const { getNeuralSubstrate } = await import('agentic-flow/embeddings');
|
|
this.substrate = await getNeuralSubstrate(this.config);
|
|
await this.substrate.init();
|
|
this.available = true;
|
|
}
|
|
catch (error) {
|
|
console.warn('[neural] Neural substrate not available:', error instanceof Error ? error.message : error);
|
|
this.available = false;
|
|
}
|
|
this.initialized = true;
|
|
return this.available;
|
|
}
|
|
/**
|
|
* Check if neural features are available
|
|
*/
|
|
isAvailable() {
|
|
return this.available;
|
|
}
|
|
/**
|
|
* Detect semantic drift from baseline
|
|
*/
|
|
async detectDrift(input) {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.drift.detect(input);
|
|
}
|
|
/**
|
|
* Set baseline for drift detection
|
|
*/
|
|
async setDriftBaseline(context) {
|
|
if (!this.available || !this.substrate)
|
|
return;
|
|
await this.substrate.drift.setBaseline(context);
|
|
}
|
|
/**
|
|
* Store memory with interference detection
|
|
*/
|
|
async storeMemory(id, content) {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.memory.store(id, content);
|
|
}
|
|
/**
|
|
* Recall memories by similarity
|
|
*/
|
|
async recallMemories(query, topK = 5) {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.memory.recall(query, topK);
|
|
}
|
|
/**
|
|
* Consolidate memories (merge similar, forget weak)
|
|
*/
|
|
consolidateMemories() {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.memory.consolidate();
|
|
}
|
|
/**
|
|
* Register agent for state tracking
|
|
*/
|
|
async registerAgent(id, role) {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.states.registerAgent(id, role);
|
|
}
|
|
/**
|
|
* Update agent state based on observation
|
|
*/
|
|
async updateAgentState(agentId, observation) {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.states.updateState(agentId, observation);
|
|
}
|
|
/**
|
|
* Get agent state
|
|
*/
|
|
getAgentState(agentId) {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.states.getAgent(agentId);
|
|
}
|
|
/**
|
|
* Coordinate swarm for task
|
|
*/
|
|
async coordinateSwarm(task) {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.swarm.coordinate(task);
|
|
}
|
|
/**
|
|
* Add agent to swarm
|
|
*/
|
|
async addSwarmAgent(id, role) {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.swarm.addAgent(id, role);
|
|
}
|
|
/**
|
|
* Calibrate coherence monitor
|
|
*/
|
|
async calibrateCoherence(goodOutputs) {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.coherence.calibrate(goodOutputs);
|
|
}
|
|
/**
|
|
* Check output coherence
|
|
*/
|
|
async checkCoherence(output) {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.coherence.check(output);
|
|
}
|
|
/**
|
|
* Process input through full neural substrate
|
|
*/
|
|
async process(input, context) {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.process(input, context);
|
|
}
|
|
/**
|
|
* Get substrate health
|
|
*/
|
|
health() {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.health();
|
|
}
|
|
/**
|
|
* Full consolidation pass
|
|
*/
|
|
consolidate() {
|
|
if (!this.available || !this.substrate)
|
|
return null;
|
|
return this.substrate.consolidate();
|
|
}
|
|
}
|
|
/**
|
|
* Create neural embedding service
|
|
*/
|
|
export function createNeuralService(config = {}) {
|
|
return new NeuralEmbeddingService(config);
|
|
}
|
|
/**
|
|
* Check if neural features are available
|
|
*/
|
|
export async function isNeuralAvailable() {
|
|
try {
|
|
await import('agentic-flow/embeddings');
|
|
return true;
|
|
}
|
|
catch {
|
|
return false;
|
|
}
|
|
}
|
|
/**
|
|
* List available ONNX embedding models
|
|
*/
|
|
export async function listEmbeddingModels() {
|
|
try {
|
|
const { listAvailableModels } = await import('agentic-flow/embeddings');
|
|
return listAvailableModels();
|
|
}
|
|
catch {
|
|
// Return default models if agentic-flow not available
|
|
return [
|
|
{ id: 'all-MiniLM-L6-v2', dimension: 384, size: '23MB', quantized: false, downloaded: false },
|
|
{ id: 'all-mpnet-base-v2', dimension: 768, size: '110MB', quantized: false, downloaded: false },
|
|
];
|
|
}
|
|
}
|
|
/**
|
|
* Download embedding model
|
|
*/
|
|
export async function downloadEmbeddingModel(modelId, targetDir, onProgress) {
|
|
const { downloadModel } = await import('agentic-flow/embeddings');
|
|
return downloadModel(modelId, targetDir ?? '.models', onProgress);
|
|
}
|
|
//# sourceMappingURL=neural-integration.js.map
|