-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsession-manager.js
More file actions
554 lines (461 loc) · 13.3 KB
/
session-manager.js
File metadata and controls
554 lines (461 loc) · 13.3 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/**
* Claude Flow Session Manager
* Manages persistent sessions, state, and context across Claude Code interactions
*/
import { randomUUID } from 'crypto';
import { readFile, writeFile } from 'fs/promises';
import { existsSync } from 'fs';
import { join } from 'path';
export class SessionManager {
constructor(options = {}) {
this.sessionPath = options.sessionPath || './memory/sessions';
this.currentSession = null;
this.sessionHistory = [];
this.maxHistorySize = options.maxHistorySize || 100;
this.autoSave = options.autoSave !== false;
this.autoSaveInterval = options.autoSaveInterval || 30000; // 30 seconds
this.autoSaveTimer = null;
}
async initialize() {
// Load session history
await this.loadSessionHistory();
// Start auto-save if enabled
if (this.autoSave) {
this.startAutoSave();
}
}
/**
* Create a new session
*/
async createSession(options = {}) {
const sessionId = options.sessionId || randomUUID();
const type = options.type || 'development';
const metadata = options.metadata || {};
const session = {
id: sessionId,
type,
metadata,
created: Date.now(),
lastActive: Date.now(),
state: {
swarms: [],
agents: [],
tasks: [],
memory: {},
metrics: {
tokensUsed: 0,
tasksCompleted: 0,
timeActive: 0,
efficiency: 1.0
}
},
context: {
workingDirectory: process.cwd(),
environment: process.env.NODE_ENV || 'development',
claudeFlowVersion: '2.0.0-alpha.27',
features: await this.detectFeatures()
},
events: [],
checkpoints: []
};
this.currentSession = session;
this.sessionHistory.push({
id: sessionId,
created: session.created,
type: session.type
});
// Save initial session
await this.saveSession();
return { sessionId, created: session.created };
}
/**
* Load an existing session
*/
async loadSession(sessionId) {
const sessionFile = join(this.sessionPath, `${sessionId}.json`);
if (!existsSync(sessionFile)) {
throw new Error(`Session ${sessionId} not found`);
}
const sessionData = await readFile(sessionFile, 'utf8');
this.currentSession = JSON.parse(sessionData);
this.currentSession.lastActive = Date.now();
return this.currentSession;
}
/**
* Save current session
*/
async saveSession() {
if (!this.currentSession) {
return;
}
const sessionFile = join(this.sessionPath, `${this.currentSession.id}.json`);
// Update last active time
this.currentSession.lastActive = Date.now();
// Calculate time active
if (this.currentSession.state.metrics) {
const sessionAge = Date.now() - this.currentSession.created;
this.currentSession.state.metrics.timeActive = sessionAge;
}
await writeFile(
sessionFile,
JSON.stringify(this.currentSession, null, 2),
'utf8'
);
// Update history
await this.saveSessionHistory();
}
/**
* Update session state
*/
async updateState(updates) {
if (!this.currentSession) {
throw new Error('No active session');
}
// Merge updates into current state
this.currentSession.state = {
...this.currentSession.state,
...updates
};
// Add event
this.addEvent('state_updated', { updates });
// Auto-save if enabled
if (this.autoSave) {
await this.saveSession();
}
}
/**
* Add swarm to session
*/
async addSwarm(swarmId, swarmInfo) {
if (!this.currentSession) {
throw new Error('No active session');
}
this.currentSession.state.swarms.push({
id: swarmId,
...swarmInfo,
addedAt: Date.now()
});
this.addEvent('swarm_added', { swarmId, ...swarmInfo });
}
/**
* Add agent to session
*/
async addAgent(agentId, agentInfo) {
if (!this.currentSession) {
throw new Error('No active session');
}
this.currentSession.state.agents.push({
id: agentId,
...agentInfo,
addedAt: Date.now()
});
this.addEvent('agent_added', { agentId, ...agentInfo });
}
/**
* Add task to session
*/
async addTask(taskId, taskInfo) {
if (!this.currentSession) {
throw new Error('No active session');
}
this.currentSession.state.tasks.push({
id: taskId,
...taskInfo,
addedAt: Date.now()
});
this.addEvent('task_added', { taskId, ...taskInfo });
}
/**
* Update memory
*/
async updateMemory(key, value) {
if (!this.currentSession) {
throw new Error('No active session');
}
this.currentSession.state.memory[key] = {
value,
updated: Date.now()
};
this.addEvent('memory_updated', { key, size: JSON.stringify(value).length });
}
/**
* Update metrics
*/
async updateMetrics(metrics) {
if (!this.currentSession) {
throw new Error('No active session');
}
this.currentSession.state.metrics = {
...this.currentSession.state.metrics,
...metrics
};
this.addEvent('metrics_updated', { metrics });
}
/**
* Create checkpoint
*/
async createCheckpoint(name, description) {
if (!this.currentSession) {
throw new Error('No active session');
}
const checkpoint = {
id: randomUUID(),
name,
description,
created: Date.now(),
state: JSON.parse(JSON.stringify(this.currentSession.state))
};
this.currentSession.checkpoints.push(checkpoint);
this.addEvent('checkpoint_created', { checkpointId: checkpoint.id, name });
await this.saveSession();
return { checkpointId: checkpoint.id };
}
/**
* Restore from checkpoint
*/
async restoreCheckpoint(checkpointId) {
if (!this.currentSession) {
throw new Error('No active session');
}
const checkpoint = this.currentSession.checkpoints.find(
cp => cp.id === checkpointId
);
if (!checkpoint) {
throw new Error(`Checkpoint ${checkpointId} not found`);
}
// Restore state
this.currentSession.state = JSON.parse(JSON.stringify(checkpoint.state));
this.addEvent('checkpoint_restored', { checkpointId, name: checkpoint.name });
await this.saveSession();
return { restored: true, checkpoint: checkpoint.name };
}
/**
* Generate session summary
*/
async generateSummary() {
if (!this.currentSession) {
throw new Error('No active session');
}
const session = this.currentSession;
const duration = Date.now() - session.created;
const summary = {
sessionId: session.id,
type: session.type,
duration: this.formatDuration(duration),
created: new Date(session.created).toISOString(),
lastActive: new Date(session.lastActive).toISOString(),
statistics: {
swarms: session.state.swarms.length,
agents: session.state.agents.length,
tasks: session.state.tasks.length,
tasksCompleted: session.state.metrics.tasksCompleted,
tokensUsed: session.state.metrics.tokensUsed,
efficiency: session.state.metrics.efficiency,
events: session.events.length,
checkpoints: session.checkpoints.length
},
topEvents: this.getTopEvents(session.events, 10),
recentActivity: this.getRecentActivity(session.events, 5)
};
return summary;
}
/**
* Export session
*/
async exportSession(format = 'json') {
if (!this.currentSession) {
throw new Error('No active session');
}
const summary = await this.generateSummary();
switch (format) {
case 'json':
return {
session: this.currentSession,
summary
};
case 'markdown':
return this.generateMarkdownReport(this.currentSession, summary);
default:
throw new Error(`Unsupported export format: ${format}`);
}
}
/**
* End session
*/
async endSession() {
if (!this.currentSession) {
throw new Error('No active session');
}
// Generate final summary
const summary = await this.generateSummary();
// Add final event
this.addEvent('session_ended', { summary });
// Save final state
await this.saveSession();
// Clear current session
this.currentSession = null;
// Stop auto-save
if (this.autoSaveTimer) {
clearInterval(this.autoSaveTimer);
this.autoSaveTimer = null;
}
return summary;
}
/**
* Helper methods
*/
addEvent(type, data) {
if (!this.currentSession) return;
this.currentSession.events.push({
id: randomUUID(),
type,
data,
timestamp: Date.now()
});
// Limit event history
if (this.currentSession.events.length > 1000) {
this.currentSession.events = this.currentSession.events.slice(-1000);
}
}
async detectFeatures() {
// Detect available features
return {
mcp: true,
swarms: true,
neural: true,
github: false,
workflows: true
};
}
formatDuration(ms) {
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
if (hours > 0) {
return `${hours}h ${minutes % 60}m`;
} else if (minutes > 0) {
return `${minutes}m ${seconds % 60}s`;
} else {
return `${seconds}s`;
}
}
getTopEvents(events, limit) {
const eventCounts = {};
for (const event of events) {
eventCounts[event.type] = (eventCounts[event.type] || 0) + 1;
}
return Object.entries(eventCounts)
.sort((a, b) => b[1] - a[1])
.slice(0, limit)
.map(([type, count]) => ({ type, count }));
}
getRecentActivity(events, limit) {
return events
.slice(-limit)
.reverse()
.map(event => ({
type: event.type,
timestamp: new Date(event.timestamp).toISOString(),
summary: this.summarizeEvent(event)
}));
}
summarizeEvent(event) {
switch (event.type) {
case 'swarm_added':
return `Added ${event.data.topology} swarm`;
case 'agent_added':
return `Spawned ${event.data.type} agent: ${event.data.name}`;
case 'task_added':
return `Started task: ${event.data.description?.substring(0, 50)}...`;
case 'checkpoint_created':
return `Created checkpoint: ${event.data.name}`;
default:
return event.type.replace(/_/g, ' ');
}
}
generateMarkdownReport(session, summary) {
let report = `# Claude Flow Session Report\n\n`;
report += `## Session Information\n`;
report += `- **ID**: ${summary.sessionId}\n`;
report += `- **Type**: ${summary.type}\n`;
report += `- **Duration**: ${summary.duration}\n`;
report += `- **Created**: ${summary.created}\n`;
report += `- **Last Active**: ${summary.lastActive}\n\n`;
report += `## Statistics\n`;
report += `- **Swarms**: ${summary.statistics.swarms}\n`;
report += `- **Agents**: ${summary.statistics.agents}\n`;
report += `- **Tasks**: ${summary.statistics.tasks}\n`;
report += `- **Tasks Completed**: ${summary.statistics.tasksCompleted}\n`;
report += `- **Tokens Used**: ${summary.statistics.tokensUsed}\n`;
report += `- **Efficiency**: ${(summary.statistics.efficiency * 100).toFixed(1)}%\n\n`;
report += `## Top Events\n`;
for (const event of summary.topEvents) {
report += `- ${event.type}: ${event.count} occurrences\n`;
}
report += `\n`;
report += `## Recent Activity\n`;
for (const activity of summary.recentActivity) {
report += `- [${activity.timestamp}] ${activity.summary}\n`;
}
return report;
}
async loadSessionHistory() {
const historyFile = join(this.sessionPath, 'history.json');
if (existsSync(historyFile)) {
const data = await readFile(historyFile, 'utf8');
this.sessionHistory = JSON.parse(data);
}
}
async saveSessionHistory() {
const historyFile = join(this.sessionPath, 'history.json');
// Limit history size
if (this.sessionHistory.length > this.maxHistorySize) {
this.sessionHistory = this.sessionHistory.slice(-this.maxHistorySize);
}
await writeFile(
historyFile,
JSON.stringify(this.sessionHistory, null, 2),
'utf8'
);
}
startAutoSave() {
this.autoSaveTimer = setInterval(async () => {
if (this.currentSession) {
await this.saveSession();
}
}, this.autoSaveInterval);
}
/**
* Get session list
*/
async listSessions() {
return this.sessionHistory.map(session => ({
id: session.id,
type: session.type,
created: new Date(session.created).toISOString(),
age: this.formatDuration(Date.now() - session.created)
}));
}
/**
* Clean old sessions
*/
async cleanOldSessions(maxAge = 7 * 24 * 60 * 60 * 1000) { // 7 days
const now = Date.now();
const sessionsToKeep = [];
for (const session of this.sessionHistory) {
if (now - session.created < maxAge) {
sessionsToKeep.push(session);
} else {
// Delete old session file
const sessionFile = join(this.sessionPath, `${session.id}.json`);
if (existsSync(sessionFile)) {
const { unlink } = await import('fs/promises');
await unlink(sessionFile);
}
}
}
this.sessionHistory = sessionsToKeep;
await this.saveSessionHistory();
}
}
export default SessionManager;