File size: 7,425 Bytes
192cba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3005c74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192cba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
 * 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;