CAPT-Memory-Palace / js /simulator.js
Knowurknot's picture
Upload folder using huggingface_hub
3005c74 verified
/**
* CAPT Memory Palace - Simulation Engine
*
* Generates realistic cognitive data for demo mode.
* Also handles real CAPT API connection when available.
*/
const CognitiveSimulator = {
isRunning: false,
intervalId: null,
useRealAPI: false,
currentInput: null,
sweep: 1000,
// Module templates with realistic behaviors
moduleTemplates: [
{ id: 'PULSE', type: 'processing', baseColor: '#8b5cf6', duration: [500, 1500] },
{ id: 'NEDA', type: 'processing', baseColor: '#a855f7', duration: [800, 2000] },
{ id: 'HMC', type: 'memory', baseColor: '#c084fc', duration: [300, 800] },
{ id: 'CIG', type: 'processing', baseColor: '#d946ef', duration: [1000, 2500] },
{ id: 'HDR', type: 'processing', baseColor: '#e879f9', duration: [1200, 3000] },
{ id: 'QIPC', type: 'quorum', baseColor: '#f472b6', duration: [800, 1500] },
{ id: 'ECHO', type: 'memory', baseColor: '#fb7185', duration: [200, 600] },
{ id: 'META', type: 'processing', baseColor: '#f43f5e', duration: [400, 1000] },
{ id: 'IMMU', type: 'validation', baseColor: '#10b981', duration: [300, 700] },
{ id: 'NDS', type: 'output', baseColor: '#06b6d4', duration: [500, 1200] }
],
activeModules: [],
// Demo query presets with realistic processing sequences
demoQueries: [
{
query: "What is consciousness?",
sequence: [
{ module: 'PULSE', duration: 800, status: 'tokenizing' },
{ module: 'NEDA', duration: 1200, status: 'semantic_match' },
{ module: 'ECHO', duration: 600, status: 'memory_retrieval' },
{ module: 'QIPC', duration: 1000, status: 'reasoning' },
{ module: 'NDS', duration: 500, status: 'synthesizing' }
]
},
{
query: "Explain quantum entanglement",
sequence: [
{ module: 'PULSE', duration: 600, status: 'tokenizing' },
{ module: 'NEDA', duration: 1500, status: 'pattern_match' },
{ module: 'HMC', duration: 400, status: 'encoding' },
{ module: 'QIPC', duration: 1200, status: 'physics_reasoning' },
{ module: 'NDS', duration: 700, status: 'generating_explanation' }
]
},
{
query: "Write a poem about AI",
sequence: [
{ module: 'PULSE', duration: 500, status: 'tokenizing' },
{ module: 'NEDA', duration: 800, status: 'creativity_match' },
{ module: 'ECHO', duration: 500, status: 'poetic_patterns' },
{ module: 'QIPC', duration: 900, status: 'lyric_building' },
{ module: 'NDS', duration: 600, status: 'versification' }
]
}
],
/**
* Start simulation
*/
start(onUpdate, options = {}) {
if (this.isRunning) return;
this.isRunning = true;
this.onUpdate = onUpdate;
this.useRealAPI = options.useRealAPI || false;
if (this.useRealAPI) {
this.startRealAPIPolling();
} else {
this.startSimulation();
}
},
/**
* Stop simulation
*/
stop() {
this.isRunning = false;
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
Narrative.reset();
},
/**
* Start simulated cognitive processing
*/
startSimulation() {
// Process a "query" through cognitive pipeline
this.processQuery("What is the meaning of consciousness?");
},
/**
* Process a query through the cognitive pipeline
*/
processQuery(input) {
this.currentInput = input;
this.sweep++;
this.activeModules = [];
// Phase 1: Input processing (PULSE)
setTimeout(() => this.activateModule('PULSE', 0.9), 100);
// Phase 2: Pattern analysis (NEDA)
setTimeout(() => this.activateModule('NEDA', 0.85), 800);
// Phase 3: Memory retrieval (ECHO, HMC)
setTimeout(() => {
this.activateModule('ECHO', 0.7);
this.activateModule('HMC', 0.6);
}, 1500);
// Phase 4: Causal reasoning (CIG)
setTimeout(() => this.activateModule('CIG', 0.8), 2500);
// Phase 5: Concept mapping (HDR)
setTimeout(() => this.activateModule('HDR', 0.75), 3500);
// Phase 6: Competing interpretations (multiple active)
setTimeout(() => {
this.activateModule('QIPC', 0.5);
this.activateModule('META', 0.65);
}, 4500);
// Phase 7: Consensus building
setTimeout(() => {
// Increase QIPC, decrease others
this.activeModules.forEach(m => {
if (m.id === 'QIPC') m.activation = 0.95;
else m.activation *= 0.7;
});
this.emitState();
}, 5500);
// Phase 8: Validation
setTimeout(() => this.activateModule('IMMU', 0.85), 6500);
// Phase 9: Output generation
setTimeout(() => this.activateModule('NDS', 0.9), 7500);
// Complete - restart
setTimeout(() => {
this.emitState();
// Keep cycling
this.processQuery(input);
}, 9000);
},
/**
* Activate a module with realistic activation curve
*/
activateModule(moduleId, maxActivation) {
const template = this.moduleTemplates.find(t => t.id === moduleId);
if (!template) return;
const existing = this.activeModules.find(m => m.id === moduleId);
if (existing) {
existing.activation = maxActivation;
existing.confidence = 0.7 + Math.random() * 0.3;
} else {
this.activeModules.push({
id: moduleId,
type: template.type,
activation: maxActivation,
confidence: 0.7 + Math.random() * 0.3,
color: template.baseColor,
capacity: Math.round(maxActivation * 100)
});
}
this.emitState();
// Gradual decay
const decayTime = template.duration[0] + Math.random() * (template.duration[1] - template.duration[0]);
setTimeout(() => {
if (this.activeModules.length > 0) {
const idx = this.activeModules.findIndex(m => m.id === moduleId);
if (idx >= 0) {
this.activeModules[idx].activation *= 0.5;
if (this.activeModules[idx].activation < 0.1) {
this.activeModules.splice(idx, 1);
}
this.emitState();
}
}
}, decayTime);
},
/**
* Emit current state to callback
*/
emitState() {
if (!this.onUpdate) return;
const state = {
sweep: this.sweep,
modules: this.activeModules,
input: this.currentInput,
timestamp: Date.now()
};
this.onUpdate(state);
},
/**
* Start polling real CAPT API
*/
startRealAPIColling() {
const poll = async () => {
try {
const state = await CAPT_API.getState();
if (state) {
this.onUpdate({
sweep: state.sweep || this.sweep++,
modules: parseCAPTState(state)?.rooms || [],
input: state.input,
timestamp: Date.now()
});
}
} catch (e) {
console.warn('CAPT API polling failed, falling back to simulation');
this.useRealAPI = false;
this.startSimulation();
}
};
poll();
this.intervalId = setInterval(poll, 2000);
},
/**
* Submit a new query
*/
submitQuery(query) {
this.currentInput = query;
this.processQuery(query);
},
/**
* Get current state
*/
getState() {
return {
sweep: this.sweep,
modules: this.activeModules,
input: this.currentInput
};
}
};
// Export
window.CognitiveSimulator = CognitiveSimulator;