@soulcraft/brainy 3.20.1 → 3.20.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/bin/brainy-interactive.js +2 -2
- package/dist/brainy.d.ts +1 -1
- package/dist/mcp/brainyMCPAdapter.d.ts +1 -1
- package/dist/mcp/brainyMCPService.d.ts +1 -1
- package/dist/neural/embeddedPatterns.d.ts +1 -1
- package/dist/neural/embeddedPatterns.js +1 -1
- package/dist/shared/default-augmentations.d.ts +1 -1
- package/dist/types/{brainyDataInterface.js → brainyInterface.js} +1 -1
- package/dist/vfs/VirtualFileSystem.d.ts +1 -0
- package/dist/vfs/VirtualFileSystem.js +108 -69
- package/package.json +1 -1
- package/dist/augmentationFactory.d.ts +0 -86
- package/dist/augmentationFactory.js +0 -342
- package/dist/augmentationRegistry.d.ts +0 -38
- package/dist/augmentationRegistry.js +0 -54
- package/dist/augmentationRegistryLoader.d.ts +0 -146
- package/dist/augmentationRegistryLoader.js +0 -213
- package/dist/augmentations/KnowledgeAugmentation.d.ts +0 -40
- package/dist/augmentations/KnowledgeAugmentation.js +0 -251
- package/dist/augmentations/intelligentVerbScoring.d.ts +0 -158
- package/dist/augmentations/intelligentVerbScoring.js +0 -377
- package/dist/augmentations/marketplace/AugmentationMarketplace.d.ts +0 -168
- package/dist/augmentations/marketplace/AugmentationMarketplace.js +0 -329
- package/dist/augmentations/marketplace/cli.d.ts +0 -47
- package/dist/augmentations/marketplace/cli.js +0 -265
- package/dist/augmentations/memoryAugmentations.d.ts +0 -72
- package/dist/augmentations/memoryAugmentations.js +0 -280
- package/dist/augmentations/serverSearchAugmentations.d.ts +0 -190
- package/dist/augmentations/serverSearchAugmentations.js +0 -586
- package/dist/brainy-unified.d.ts +0 -106
- package/dist/brainy-unified.js +0 -327
- package/dist/brainyData.d.ts +0 -1832
- package/dist/brainyData.js +0 -6443
- package/dist/brainyDataV3.d.ts +0 -186
- package/dist/brainyDataV3.js +0 -337
- package/dist/config/distributedPresets-new.d.ts +0 -118
- package/dist/config/distributedPresets-new.js +0 -318
- package/dist/config/modelPrecisionManager.d.ts +0 -42
- package/dist/config/modelPrecisionManager.js +0 -98
- package/dist/connectors/interfaces/IConnector.d.ts +0 -143
- package/dist/connectors/interfaces/IConnector.js +0 -8
- package/dist/demo.d.ts +0 -106
- package/dist/demo.js +0 -201
- package/dist/embeddings/SingletonModelManager.d.ts +0 -95
- package/dist/embeddings/SingletonModelManager.js +0 -220
- package/dist/embeddings/lightweight-embedder.d.ts +0 -22
- package/dist/embeddings/lightweight-embedder.js +0 -128
- package/dist/embeddings/model-manager.d.ts +0 -39
- package/dist/embeddings/model-manager.js +0 -245
- package/dist/embeddings/universal-memory-manager.d.ts +0 -38
- package/dist/embeddings/universal-memory-manager.js +0 -166
- package/dist/embeddings/worker-embedding.d.ts +0 -7
- package/dist/embeddings/worker-embedding.js +0 -73
- package/dist/embeddings/worker-manager.d.ts +0 -28
- package/dist/embeddings/worker-manager.js +0 -162
- package/dist/examples/basicUsage.d.ts +0 -4
- package/dist/examples/basicUsage.js +0 -121
- package/dist/indices/fieldIndex.d.ts +0 -76
- package/dist/indices/fieldIndex.js +0 -357
- package/dist/mcp/brainyMCPBroadcast.d.ts +0 -82
- package/dist/mcp/brainyMCPBroadcast.js +0 -303
- package/dist/mcp/brainyMCPClient.d.ts +0 -92
- package/dist/mcp/brainyMCPClient.js +0 -258
- package/dist/scripts/precomputePatternEmbeddings.d.ts +0 -19
- package/dist/scripts/precomputePatternEmbeddings.js +0 -100
- package/dist/utils/cacheAutoConfig.d.ts +0 -63
- package/dist/utils/cacheAutoConfig.js +0 -261
- package/dist/utils/hybridModelManager.d.ts +0 -64
- package/dist/utils/hybridModelManager.js +0 -95
- package/dist/utils/statistics.d.ts +0 -28
- package/dist/utils/statistics.js +0 -25
- package/dist/vfs/ConceptSystem.d.ts +0 -203
- package/dist/vfs/ConceptSystem.js +0 -545
- package/dist/vfs/EntityManager.d.ts +0 -75
- package/dist/vfs/EntityManager.js +0 -216
- package/dist/vfs/EventRecorder.d.ts +0 -84
- package/dist/vfs/EventRecorder.js +0 -269
- package/dist/vfs/GitBridge.d.ts +0 -167
- package/dist/vfs/GitBridge.js +0 -537
- package/dist/vfs/KnowledgeAugmentation.d.ts +0 -104
- package/dist/vfs/KnowledgeAugmentation.js +0 -146
- package/dist/vfs/KnowledgeLayer.d.ts +0 -35
- package/dist/vfs/KnowledgeLayer.js +0 -443
- package/dist/vfs/PersistentEntitySystem.d.ts +0 -165
- package/dist/vfs/PersistentEntitySystem.js +0 -503
- package/dist/vfs/SemanticVersioning.d.ts +0 -105
- package/dist/vfs/SemanticVersioning.js +0 -309
- package/dist/vfs/VFSHealthCheck.d.ts +0 -78
- package/dist/vfs/VFSHealthCheck.js +0 -299
- /package/dist/types/{brainyDataInterface.d.ts → brainyInterface.d.ts} +0 -0
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* BrainyMCPBroadcast
|
|
3
|
-
*
|
|
4
|
-
* Enhanced MCP service with real-time WebSocket broadcasting capabilities
|
|
5
|
-
* for multi-agent coordination (Jarvis ↔ Picasso communication)
|
|
6
|
-
*
|
|
7
|
-
* Features:
|
|
8
|
-
* - WebSocket server for real-time push notifications
|
|
9
|
-
* - Subscription management for multiple Claude instances
|
|
10
|
-
* - Message broadcasting to all connected agents
|
|
11
|
-
* - Works both locally and with cloud deployment
|
|
12
|
-
*/
|
|
13
|
-
import { WebSocketServer, WebSocket } from 'ws';
|
|
14
|
-
import { createServer } from 'node:http';
|
|
15
|
-
import { BrainyMCPService } from './brainyMCPService.js';
|
|
16
|
-
import { v4 as uuidv4 } from '../universal/uuid.js';
|
|
17
|
-
export class BrainyMCPBroadcast extends BrainyMCPService {
|
|
18
|
-
constructor(brainyData, options = {}) {
|
|
19
|
-
super(brainyData, options);
|
|
20
|
-
this.agents = new Map();
|
|
21
|
-
this.messageHistory = [];
|
|
22
|
-
this.maxHistorySize = 100;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Start the WebSocket broadcast server
|
|
26
|
-
* @param port Port to listen on (default: 8765)
|
|
27
|
-
* @param isCloud Whether this is a cloud deployment
|
|
28
|
-
*/
|
|
29
|
-
async startBroadcastServer(port = 8765, isCloud = false) {
|
|
30
|
-
return new Promise((resolve, reject) => {
|
|
31
|
-
try {
|
|
32
|
-
// Create HTTP server
|
|
33
|
-
this.httpServer = createServer((req, res) => {
|
|
34
|
-
// Health check endpoint
|
|
35
|
-
if (req.url === '/health') {
|
|
36
|
-
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
37
|
-
res.end(JSON.stringify({
|
|
38
|
-
status: 'healthy',
|
|
39
|
-
agents: Array.from(this.agents.values()).map(a => ({
|
|
40
|
-
id: a.id,
|
|
41
|
-
name: a.name,
|
|
42
|
-
role: a.role,
|
|
43
|
-
connected: true
|
|
44
|
-
})),
|
|
45
|
-
uptime: process.uptime()
|
|
46
|
-
}));
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
res.writeHead(404);
|
|
50
|
-
res.end('Not found');
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
// Create WebSocket server
|
|
54
|
-
this.wsServer = new WebSocketServer({
|
|
55
|
-
server: this.httpServer,
|
|
56
|
-
perMessageDeflate: false // Better performance
|
|
57
|
-
});
|
|
58
|
-
this.wsServer.on('connection', (socket, request) => {
|
|
59
|
-
this.handleNewConnection(socket, request);
|
|
60
|
-
});
|
|
61
|
-
// Start listening
|
|
62
|
-
this.httpServer.listen(port, () => {
|
|
63
|
-
console.log(`🧠 Brain Jar Broadcast Server running on ${isCloud ? 'cloud' : 'local'} port ${port}`);
|
|
64
|
-
console.log(`📡 WebSocket: ws://localhost:${port}`);
|
|
65
|
-
console.log(`🔍 Health: http://localhost:${port}/health`);
|
|
66
|
-
resolve();
|
|
67
|
-
});
|
|
68
|
-
// Heartbeat to keep connections alive
|
|
69
|
-
setInterval(() => {
|
|
70
|
-
this.agents.forEach((agent) => {
|
|
71
|
-
if (Date.now() - agent.lastSeen > 30000) {
|
|
72
|
-
// Remove inactive agents
|
|
73
|
-
this.removeAgent(agent.id);
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
// Send heartbeat
|
|
77
|
-
this.sendToAgent(agent.id, {
|
|
78
|
-
id: uuidv4(),
|
|
79
|
-
from: 'server',
|
|
80
|
-
type: 'heartbeat',
|
|
81
|
-
data: { timestamp: Date.now() },
|
|
82
|
-
timestamp: Date.now()
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}, 15000);
|
|
87
|
-
}
|
|
88
|
-
catch (error) {
|
|
89
|
-
reject(error);
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Handle new WebSocket connection
|
|
95
|
-
*/
|
|
96
|
-
handleNewConnection(socket, request) {
|
|
97
|
-
const agentId = uuidv4();
|
|
98
|
-
// Send welcome message
|
|
99
|
-
socket.send(JSON.stringify({
|
|
100
|
-
id: uuidv4(),
|
|
101
|
-
from: 'server',
|
|
102
|
-
type: 'notification',
|
|
103
|
-
event: 'welcome',
|
|
104
|
-
data: {
|
|
105
|
-
agentId,
|
|
106
|
-
message: 'Connected to Brain Jar Broadcast Server',
|
|
107
|
-
agents: Array.from(this.agents.values()).map(a => ({
|
|
108
|
-
id: a.id,
|
|
109
|
-
name: a.name,
|
|
110
|
-
role: a.role
|
|
111
|
-
}))
|
|
112
|
-
},
|
|
113
|
-
timestamp: Date.now()
|
|
114
|
-
}));
|
|
115
|
-
// Handle messages from this agent
|
|
116
|
-
socket.on('message', (data) => {
|
|
117
|
-
try {
|
|
118
|
-
const message = JSON.parse(data.toString());
|
|
119
|
-
this.handleAgentMessage(agentId, message);
|
|
120
|
-
}
|
|
121
|
-
catch (error) {
|
|
122
|
-
console.error('Invalid message from agent:', error);
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
// Handle disconnection
|
|
126
|
-
socket.on('close', () => {
|
|
127
|
-
this.removeAgent(agentId);
|
|
128
|
-
});
|
|
129
|
-
// Handle errors
|
|
130
|
-
socket.on('error', (error) => {
|
|
131
|
-
console.error(`Agent ${agentId} error:`, error);
|
|
132
|
-
});
|
|
133
|
-
// Store temporary connection until identified
|
|
134
|
-
this.agents.set(agentId, {
|
|
135
|
-
id: agentId,
|
|
136
|
-
name: 'Unknown',
|
|
137
|
-
role: 'Unknown',
|
|
138
|
-
socket,
|
|
139
|
-
lastSeen: Date.now()
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Handle message from an agent
|
|
144
|
-
*/
|
|
145
|
-
handleAgentMessage(agentId, message) {
|
|
146
|
-
const agent = this.agents.get(agentId);
|
|
147
|
-
if (!agent)
|
|
148
|
-
return;
|
|
149
|
-
// Update last seen
|
|
150
|
-
agent.lastSeen = Date.now();
|
|
151
|
-
// Handle identification
|
|
152
|
-
if (message.type === 'identify') {
|
|
153
|
-
agent.name = message.name || agent.name;
|
|
154
|
-
agent.role = message.role || agent.role;
|
|
155
|
-
// Notify all agents about new member
|
|
156
|
-
this.broadcast({
|
|
157
|
-
id: uuidv4(),
|
|
158
|
-
from: 'server',
|
|
159
|
-
type: 'notification',
|
|
160
|
-
event: 'agent_joined',
|
|
161
|
-
data: {
|
|
162
|
-
agent: {
|
|
163
|
-
id: agent.id,
|
|
164
|
-
name: agent.name,
|
|
165
|
-
role: agent.role
|
|
166
|
-
}
|
|
167
|
-
},
|
|
168
|
-
timestamp: Date.now()
|
|
169
|
-
}, agentId); // Exclude the joining agent
|
|
170
|
-
// Send recent history to new agent
|
|
171
|
-
if (this.messageHistory.length > 0) {
|
|
172
|
-
this.sendToAgent(agentId, {
|
|
173
|
-
id: uuidv4(),
|
|
174
|
-
from: 'server',
|
|
175
|
-
type: 'sync',
|
|
176
|
-
data: {
|
|
177
|
-
history: this.messageHistory.slice(-20) // Last 20 messages
|
|
178
|
-
},
|
|
179
|
-
timestamp: Date.now()
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
// Create broadcast message
|
|
185
|
-
const broadcastMsg = {
|
|
186
|
-
id: message.id || uuidv4(),
|
|
187
|
-
from: agent.name,
|
|
188
|
-
to: message.to,
|
|
189
|
-
type: message.type || 'message',
|
|
190
|
-
event: message.event,
|
|
191
|
-
data: message.data,
|
|
192
|
-
timestamp: Date.now()
|
|
193
|
-
};
|
|
194
|
-
// Store in history
|
|
195
|
-
this.addToHistory(broadcastMsg);
|
|
196
|
-
// Broadcast based on recipient
|
|
197
|
-
if (message.to) {
|
|
198
|
-
// Send to specific agent(s)
|
|
199
|
-
const recipients = Array.isArray(message.to) ? message.to : [message.to];
|
|
200
|
-
recipients.forEach((recipientName) => {
|
|
201
|
-
const recipient = Array.from(this.agents.values()).find(a => a.name === recipientName);
|
|
202
|
-
if (recipient) {
|
|
203
|
-
this.sendToAgent(recipient.id, broadcastMsg);
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
else {
|
|
208
|
-
// Broadcast to all agents except sender
|
|
209
|
-
this.broadcast(broadcastMsg, agentId);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Broadcast message to all connected agents
|
|
214
|
-
*/
|
|
215
|
-
broadcast(message, excludeId) {
|
|
216
|
-
const messageStr = JSON.stringify(message);
|
|
217
|
-
this.agents.forEach((agent) => {
|
|
218
|
-
if (agent.id !== excludeId && agent.socket.readyState === WebSocket.OPEN) {
|
|
219
|
-
agent.socket.send(messageStr);
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Send message to specific agent
|
|
225
|
-
*/
|
|
226
|
-
sendToAgent(agentId, message) {
|
|
227
|
-
const agent = this.agents.get(agentId);
|
|
228
|
-
if (agent && agent.socket.readyState === WebSocket.OPEN) {
|
|
229
|
-
agent.socket.send(JSON.stringify(message));
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* Remove agent from connected list
|
|
234
|
-
*/
|
|
235
|
-
removeAgent(agentId) {
|
|
236
|
-
const agent = this.agents.get(agentId);
|
|
237
|
-
if (agent) {
|
|
238
|
-
// Notify others about disconnection
|
|
239
|
-
this.broadcast({
|
|
240
|
-
id: uuidv4(),
|
|
241
|
-
from: 'server',
|
|
242
|
-
type: 'notification',
|
|
243
|
-
event: 'agent_left',
|
|
244
|
-
data: {
|
|
245
|
-
agent: {
|
|
246
|
-
id: agent.id,
|
|
247
|
-
name: agent.name,
|
|
248
|
-
role: agent.role
|
|
249
|
-
}
|
|
250
|
-
},
|
|
251
|
-
timestamp: Date.now()
|
|
252
|
-
});
|
|
253
|
-
this.agents.delete(agentId);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Add message to history
|
|
258
|
-
*/
|
|
259
|
-
addToHistory(message) {
|
|
260
|
-
this.messageHistory.push(message);
|
|
261
|
-
// Trim history if too large
|
|
262
|
-
if (this.messageHistory.length > this.maxHistorySize) {
|
|
263
|
-
this.messageHistory = this.messageHistory.slice(-this.maxHistorySize);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Stop the broadcast server
|
|
268
|
-
*/
|
|
269
|
-
async stopBroadcastServer() {
|
|
270
|
-
// Close all agent connections
|
|
271
|
-
this.agents.forEach(agent => {
|
|
272
|
-
agent.socket.close(1000, 'Server shutting down');
|
|
273
|
-
});
|
|
274
|
-
this.agents.clear();
|
|
275
|
-
// Close WebSocket server
|
|
276
|
-
if (this.wsServer) {
|
|
277
|
-
this.wsServer.close();
|
|
278
|
-
}
|
|
279
|
-
// Close HTTP server
|
|
280
|
-
if (this.httpServer) {
|
|
281
|
-
this.httpServer.close();
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
* Get connected agents
|
|
286
|
-
*/
|
|
287
|
-
getConnectedAgents() {
|
|
288
|
-
return Array.from(this.agents.values()).map(a => ({
|
|
289
|
-
id: a.id,
|
|
290
|
-
name: a.name,
|
|
291
|
-
role: a.role
|
|
292
|
-
}));
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* Get message history
|
|
296
|
-
*/
|
|
297
|
-
getMessageHistory() {
|
|
298
|
-
return [...this.messageHistory];
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
// Export for both environments
|
|
302
|
-
export default BrainyMCPBroadcast;
|
|
303
|
-
//# sourceMappingURL=brainyMCPBroadcast.js.map
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* BrainyMCPClient
|
|
3
|
-
*
|
|
4
|
-
* Client for connecting Claude instances to the Brain Jar Broadcast Server
|
|
5
|
-
* Utilizes Brainy for persistent memory and vector search capabilities
|
|
6
|
-
*/
|
|
7
|
-
interface ClientOptions {
|
|
8
|
-
name: string;
|
|
9
|
-
role: string;
|
|
10
|
-
serverUrl?: string;
|
|
11
|
-
autoReconnect?: boolean;
|
|
12
|
-
useBrainyMemory?: boolean;
|
|
13
|
-
}
|
|
14
|
-
interface Message {
|
|
15
|
-
id: string;
|
|
16
|
-
from: string;
|
|
17
|
-
to?: string | string[];
|
|
18
|
-
type: 'message' | 'notification' | 'sync' | 'heartbeat' | 'identify';
|
|
19
|
-
event?: string;
|
|
20
|
-
data: any;
|
|
21
|
-
timestamp: number;
|
|
22
|
-
}
|
|
23
|
-
export declare class BrainyMCPClient {
|
|
24
|
-
private socket?;
|
|
25
|
-
private options;
|
|
26
|
-
private brainy?;
|
|
27
|
-
private messageHandlers;
|
|
28
|
-
private reconnectTimeout?;
|
|
29
|
-
private isConnected;
|
|
30
|
-
constructor(options: ClientOptions);
|
|
31
|
-
/**
|
|
32
|
-
* Initialize Brainy for persistent memory
|
|
33
|
-
*/
|
|
34
|
-
private initBrainy;
|
|
35
|
-
/**
|
|
36
|
-
* Connect to the broadcast server
|
|
37
|
-
*/
|
|
38
|
-
connect(): Promise<void>;
|
|
39
|
-
/**
|
|
40
|
-
* Handle incoming message
|
|
41
|
-
*/
|
|
42
|
-
private handleMessage;
|
|
43
|
-
/**
|
|
44
|
-
* Send a message
|
|
45
|
-
*/
|
|
46
|
-
send(message: Partial<Message>): void;
|
|
47
|
-
/**
|
|
48
|
-
* Send a message to specific agent(s)
|
|
49
|
-
*/
|
|
50
|
-
sendTo(recipient: string | string[], data: any): void;
|
|
51
|
-
/**
|
|
52
|
-
* Broadcast to all agents
|
|
53
|
-
*/
|
|
54
|
-
broadcast(data: any): void;
|
|
55
|
-
/**
|
|
56
|
-
* Register a message handler
|
|
57
|
-
*/
|
|
58
|
-
on(type: string, handler: (message: Message) => void): void;
|
|
59
|
-
/**
|
|
60
|
-
* Remove a message handler
|
|
61
|
-
*/
|
|
62
|
-
off(type: string): void;
|
|
63
|
-
/**
|
|
64
|
-
* Search historical messages using Brainy's vector search
|
|
65
|
-
*/
|
|
66
|
-
searchMemory(query: string, limit?: number): Promise<any[]>;
|
|
67
|
-
/**
|
|
68
|
-
* Get recent messages from Brainy memory
|
|
69
|
-
*/
|
|
70
|
-
getRecentMessages(limit?: number): Promise<any[]>;
|
|
71
|
-
/**
|
|
72
|
-
* Schedule reconnection attempt
|
|
73
|
-
*/
|
|
74
|
-
private scheduleReconnect;
|
|
75
|
-
/**
|
|
76
|
-
* Disconnect from server
|
|
77
|
-
*/
|
|
78
|
-
disconnect(): void;
|
|
79
|
-
/**
|
|
80
|
-
* Check if connected
|
|
81
|
-
*/
|
|
82
|
-
getIsConnected(): boolean;
|
|
83
|
-
/**
|
|
84
|
-
* Get agent info
|
|
85
|
-
*/
|
|
86
|
-
getAgentInfo(): {
|
|
87
|
-
name: string;
|
|
88
|
-
role: string;
|
|
89
|
-
connected: boolean;
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
export default BrainyMCPClient;
|
|
@@ -1,258 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* BrainyMCPClient
|
|
3
|
-
*
|
|
4
|
-
* Client for connecting Claude instances to the Brain Jar Broadcast Server
|
|
5
|
-
* Utilizes Brainy for persistent memory and vector search capabilities
|
|
6
|
-
*/
|
|
7
|
-
import WebSocket from 'ws';
|
|
8
|
-
import { Brainy } from '../brainy.js';
|
|
9
|
-
import { v4 as uuidv4 } from '../universal/uuid.js';
|
|
10
|
-
export class BrainyMCPClient {
|
|
11
|
-
constructor(options) {
|
|
12
|
-
this.messageHandlers = new Map();
|
|
13
|
-
this.isConnected = false;
|
|
14
|
-
this.options = {
|
|
15
|
-
serverUrl: 'ws://localhost:8765',
|
|
16
|
-
autoReconnect: true,
|
|
17
|
-
useBrainyMemory: true,
|
|
18
|
-
...options
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Initialize Brainy for persistent memory
|
|
23
|
-
*/
|
|
24
|
-
async initBrainy() {
|
|
25
|
-
if (this.options.useBrainyMemory && !this.brainy) {
|
|
26
|
-
this.brainy = new Brainy({
|
|
27
|
-
storage: {
|
|
28
|
-
requestPersistentStorage: true
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
await this.brainy.init();
|
|
32
|
-
console.log(`🧠 Brainy memory initialized for ${this.options.name}`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Connect to the broadcast server
|
|
37
|
-
*/
|
|
38
|
-
async connect() {
|
|
39
|
-
// Initialize Brainy first
|
|
40
|
-
await this.initBrainy();
|
|
41
|
-
return new Promise((resolve, reject) => {
|
|
42
|
-
try {
|
|
43
|
-
this.socket = new WebSocket(this.options.serverUrl);
|
|
44
|
-
this.socket.on('open', () => {
|
|
45
|
-
console.log(`✅ ${this.options.name} connected to Brain Jar Broadcast`);
|
|
46
|
-
this.isConnected = true;
|
|
47
|
-
// Identify ourselves
|
|
48
|
-
this.send({
|
|
49
|
-
type: 'identify',
|
|
50
|
-
data: {
|
|
51
|
-
name: this.options.name,
|
|
52
|
-
role: this.options.role
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
resolve();
|
|
56
|
-
});
|
|
57
|
-
this.socket.on('message', async (data) => {
|
|
58
|
-
try {
|
|
59
|
-
const message = JSON.parse(data.toString());
|
|
60
|
-
await this.handleMessage(message);
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
console.error('Error parsing message:', error);
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
this.socket.on('close', () => {
|
|
67
|
-
console.log(`❌ ${this.options.name} disconnected from Brain Jar`);
|
|
68
|
-
this.isConnected = false;
|
|
69
|
-
if (this.options.autoReconnect) {
|
|
70
|
-
this.scheduleReconnect();
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
this.socket.on('error', (error) => {
|
|
74
|
-
console.error(`Connection error for ${this.options.name}:`, error);
|
|
75
|
-
reject(error);
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
catch (error) {
|
|
79
|
-
reject(error);
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Handle incoming message
|
|
85
|
-
*/
|
|
86
|
-
async handleMessage(message) {
|
|
87
|
-
// Store in Brainy for persistent memory
|
|
88
|
-
if (this.brainy && message.type === 'message') {
|
|
89
|
-
try {
|
|
90
|
-
await this.brainy.add({
|
|
91
|
-
text: `${message.from}: ${JSON.stringify(message.data)}`,
|
|
92
|
-
metadata: {
|
|
93
|
-
messageId: message.id,
|
|
94
|
-
from: message.from,
|
|
95
|
-
to: message.to,
|
|
96
|
-
timestamp: message.timestamp,
|
|
97
|
-
type: message.type,
|
|
98
|
-
event: message.event,
|
|
99
|
-
category: 'Message'
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
catch (error) {
|
|
104
|
-
console.error('Error storing message in Brainy:', error);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
// Handle sync messages (receive history)
|
|
108
|
-
if (message.type === 'sync' && message.data.history) {
|
|
109
|
-
console.log(`📜 ${this.options.name} received ${message.data.history.length} historical messages`);
|
|
110
|
-
// Store history in Brainy
|
|
111
|
-
if (this.brainy) {
|
|
112
|
-
for (const histMsg of message.data.history) {
|
|
113
|
-
await this.brainy.add({
|
|
114
|
-
text: `${histMsg.from}: ${JSON.stringify(histMsg.data)}`,
|
|
115
|
-
metadata: {
|
|
116
|
-
...histMsg,
|
|
117
|
-
category: 'Message'
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
// Call registered handlers
|
|
124
|
-
const handler = this.messageHandlers.get(message.type);
|
|
125
|
-
if (handler) {
|
|
126
|
-
handler(message);
|
|
127
|
-
}
|
|
128
|
-
// Call universal handler
|
|
129
|
-
const universalHandler = this.messageHandlers.get('*');
|
|
130
|
-
if (universalHandler) {
|
|
131
|
-
universalHandler(message);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Send a message
|
|
136
|
-
*/
|
|
137
|
-
send(message) {
|
|
138
|
-
if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
|
|
139
|
-
console.error(`${this.options.name} is not connected`);
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
const fullMessage = {
|
|
143
|
-
id: message.id || uuidv4(),
|
|
144
|
-
from: this.options.name,
|
|
145
|
-
type: message.type || 'message',
|
|
146
|
-
data: message.data || {},
|
|
147
|
-
timestamp: Date.now(),
|
|
148
|
-
...message
|
|
149
|
-
};
|
|
150
|
-
this.socket.send(JSON.stringify(fullMessage));
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Send a message to specific agent(s)
|
|
154
|
-
*/
|
|
155
|
-
sendTo(recipient, data) {
|
|
156
|
-
this.send({
|
|
157
|
-
to: recipient,
|
|
158
|
-
type: 'message',
|
|
159
|
-
data
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Broadcast to all agents
|
|
164
|
-
*/
|
|
165
|
-
broadcast(data) {
|
|
166
|
-
this.send({
|
|
167
|
-
type: 'message',
|
|
168
|
-
data
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Register a message handler
|
|
173
|
-
*/
|
|
174
|
-
on(type, handler) {
|
|
175
|
-
this.messageHandlers.set(type, handler);
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Remove a message handler
|
|
179
|
-
*/
|
|
180
|
-
off(type) {
|
|
181
|
-
this.messageHandlers.delete(type);
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* Search historical messages using Brainy's vector search
|
|
185
|
-
*/
|
|
186
|
-
async searchMemory(query, limit = 10) {
|
|
187
|
-
if (!this.brainy) {
|
|
188
|
-
console.warn('Brainy memory not initialized');
|
|
189
|
-
return [];
|
|
190
|
-
}
|
|
191
|
-
const results = await this.brainy.search(query, limit);
|
|
192
|
-
return results.map(r => ({
|
|
193
|
-
...r.metadata,
|
|
194
|
-
relevance: r.score
|
|
195
|
-
}));
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Get recent messages from Brainy memory
|
|
199
|
-
*/
|
|
200
|
-
async getRecentMessages(limit = 20) {
|
|
201
|
-
if (!this.brainy) {
|
|
202
|
-
console.warn('Brainy memory not initialized');
|
|
203
|
-
return [];
|
|
204
|
-
}
|
|
205
|
-
// Search for recent activity
|
|
206
|
-
const results = await this.brainy.search('recent messages communication', limit);
|
|
207
|
-
return results
|
|
208
|
-
.map(r => r.metadata)
|
|
209
|
-
.sort((a, b) => b.timestamp - a.timestamp);
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Schedule reconnection attempt
|
|
213
|
-
*/
|
|
214
|
-
scheduleReconnect() {
|
|
215
|
-
if (this.reconnectTimeout) {
|
|
216
|
-
clearTimeout(this.reconnectTimeout);
|
|
217
|
-
}
|
|
218
|
-
this.reconnectTimeout = setTimeout(() => {
|
|
219
|
-
console.log(`🔄 ${this.options.name} attempting to reconnect...`);
|
|
220
|
-
this.connect().catch(error => {
|
|
221
|
-
console.error('Reconnection failed:', error);
|
|
222
|
-
this.scheduleReconnect();
|
|
223
|
-
});
|
|
224
|
-
}, 5000);
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* Disconnect from server
|
|
228
|
-
*/
|
|
229
|
-
disconnect() {
|
|
230
|
-
if (this.reconnectTimeout) {
|
|
231
|
-
clearTimeout(this.reconnectTimeout);
|
|
232
|
-
}
|
|
233
|
-
if (this.socket) {
|
|
234
|
-
this.socket.close(1000, 'Client disconnecting');
|
|
235
|
-
this.socket = undefined;
|
|
236
|
-
}
|
|
237
|
-
this.isConnected = false;
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Check if connected
|
|
241
|
-
*/
|
|
242
|
-
getIsConnected() {
|
|
243
|
-
return this.isConnected;
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Get agent info
|
|
247
|
-
*/
|
|
248
|
-
getAgentInfo() {
|
|
249
|
-
return {
|
|
250
|
-
name: this.options.name,
|
|
251
|
-
role: this.options.role,
|
|
252
|
-
connected: this.isConnected
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
// Export for both environments
|
|
257
|
-
export default BrainyMCPClient;
|
|
258
|
-
//# sourceMappingURL=brainyMCPClient.js.map
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* 🧠 Pre-compute Pattern Embeddings Script
|
|
4
|
-
*
|
|
5
|
-
* This script pre-computes embeddings for all patterns and saves them to disk.
|
|
6
|
-
* Run this once after adding new patterns to avoid runtime embedding costs.
|
|
7
|
-
*
|
|
8
|
-
* How it works:
|
|
9
|
-
* 1. Load all patterns from library.json
|
|
10
|
-
* 2. Use Brainy's embedding model to encode each pattern's examples
|
|
11
|
-
* 3. Average the example embeddings to get a robust pattern representation
|
|
12
|
-
* 4. Save embeddings to patterns/embeddings.bin for instant loading
|
|
13
|
-
*
|
|
14
|
-
* Benefits:
|
|
15
|
-
* - Pattern matching becomes pure math (cosine similarity)
|
|
16
|
-
* - No embedding model calls during query processing
|
|
17
|
-
* - Patterns load instantly with pre-computed vectors
|
|
18
|
-
*/
|
|
19
|
-
export {};
|