File size: 5,389 Bytes
529090e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { WebSocketServer, WebSocket } from 'ws';
import { Server } from 'http';
import { MCPMessage } from '@widget-tdc/mcp-types';
import { mcpRegistry } from './mcpRegistry.js';
import { exec } from 'child_process';

export class MCPWebSocketServer {
  private wss: WebSocketServer;
  private clients: Map<string, WebSocket> = new Map();

  constructor(server: Server) {
    this.wss = new WebSocketServer({ server, path: '/mcp/ws' });
    this.setupWebSocketServer();
  }

  private setupWebSocketServer(): void {
    this.wss.on('connection', (ws: WebSocket) => {
      const clientId = Math.random().toString(36).substring(7);
      this.clients.set(clientId, ws);

      console.log(`MCP WebSocket client connected: ${clientId}`);

      ws.on('message', async (data: Buffer) => {
        try {
          const rawMessage = data.toString();

          // Handle NEXUS commands
          if (rawMessage.startsWith('NEXUS_COMMAND:')) {
            const commandCode = rawMessage.replace('NEXUS_COMMAND:', '');
            const result = this.executeNexusCommand(commandCode);

            // Send result back as NEXUS_RESPONSE
            ws.send(JSON.stringify({
              event: 'NEXUS_RESPONSE',
              data: result
            }));
            return;
          }

          const message: MCPMessage = JSON.parse(rawMessage);

          // Route the message
          const result = await mcpRegistry.route(message);

          // Send response back to client
          ws.send(JSON.stringify({
            success: true,
            messageId: message.id,
            result,
          }));

          // Broadcast to other clients if needed
          this.broadcast(message, clientId);
        } catch (error: any) {
          ws.send(JSON.stringify({
            success: false,
            error: error.message,
          }));
        }
      });

      ws.on('close', () => {
        this.clients.delete(clientId);
        console.log(`MCP WebSocket client disconnected: ${clientId}`);
      });

      // Send welcome message
      ws.send(JSON.stringify({
        type: 'welcome',
        clientId,
        availableTools: mcpRegistry.getRegisteredTools(),
      }));
    });
  }

  private broadcast(message: MCPMessage, excludeClientId?: string): void {
    const data = JSON.stringify({
      type: 'broadcast',
      message,
    });

    this.clients.forEach((client, clientId) => {
      if (clientId !== excludeClientId && client.readyState === WebSocket.OPEN) {
        client.send(data);
      }
    });
  }

  public sendToAll(message: any): void {
    const data = JSON.stringify(message);
    this.clients.forEach((client) => {
      if (client.readyState === WebSocket.OPEN) {
        client.send(data);
      }
    });
  }

  /**
   * Emit autonomous decision event to all connected clients
   */
  public emitAutonomousDecision(decision: {
    queryId: string;
    selectedSource: string;
    confidence: number;
    alternatives: string[];
    reasoning: string;
    latency: number;
  }): void {
    this.sendToAll({
      type: 'autonomous:decision',
      timestamp: new Date().toISOString(),
      data: decision
    });
  }

  /**
   * Emit source health update
   */
  public emitSourceHealth(sourceName: string, health: {
    healthy: boolean;
    score: number;
    latency?: number;
  }): void {
    this.sendToAll({
      type: 'autonomous:health',
      timestamp: new Date().toISOString(),
      data: {
        source: sourceName,
        ...health
      }
    });
  }

  /**
   * Emit learning progress update
   */
  public emitLearningProgress(progress: {
    patternsLearned: number;
    decisionsMade: number;
    averageConfidence: number;
  }): void {
    this.sendToAll({
      type: 'autonomous:learning',
      timestamp: new Date().toISOString(),
      data: progress
    });
  }

  private executeNexusCommand(commandIntent: string): string {
    console.log(`⚡ NEXUS EXECUTING: ${commandIntent}`);

    // Simple translation of "AI Intent" to system commands
    if (commandIntent.includes('KILL_CHROME')) {
      exec('taskkill /F /IM chrome.exe', (error, _stdout, _stderr) => {
        if (error) console.log('Error killing Chrome:', error);
      });
      return "Target neutralized: Google Chrome processes terminated.";
    }
    if (commandIntent.includes('OPEN_STEAM')) {
      exec('start steam://', (error, _stdout, _stderr) => {
        if (error) console.log('Error opening Steam:', error);
      });
      return "Launching entertainment subsystem...";
    }
    if (commandIntent.includes('FLUSH_DNS')) {
      exec('ipconfig /flushdns', (error, _stdout, _stderr) => {
        if (error) console.log('Error flushing DNS:', error);
      });
      return "Network cache cleared.";
    }
    if (commandIntent.includes('KILL_NODE')) {
      exec('taskkill /F /IM node.exe', (error, _stdout, _stderr) => {
        if (error) console.log('Error killing Node processes:', error);
      });
      return "All Node.js processes terminated.";
    }
    if (commandIntent.includes('RESTART_EXPLORER')) {
      exec('taskkill /F /IM explorer.exe && start explorer.exe', (error, _stdout, _stderr) => {
        if (error) console.log('Error restarting Explorer:', error);
      });
      return "Windows Explorer restarted.";
    }

    return `Command '${commandIntent}' not recognized in safety protocols.`;
  }
}