-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-sync.js
More file actions
653 lines (572 loc) · 18.2 KB
/
session-sync.js
File metadata and controls
653 lines (572 loc) · 18.2 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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#!/usr/bin/env node
/**
* ChittyOS Session Sync Handler
* Manages cross-session state persistence with ChittyAuth integration
* Provides seamless continuity across Claude sessions and MCP projects
*/
import {
readFileSync,
writeFileSync,
existsSync,
mkdirSync,
readdirSync,
} from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { spawn } from "child_process";
import { createHash } from "crypto";
const __dirname = dirname(fileURLToPath(import.meta.url));
class ChittySessionSync {
constructor() {
this.sessionsDir = join(process.env.HOME || ".", ".chitty", "sessions");
this.configFile = join(__dirname, "session-sync-config.json");
this.chittyAuthEndpoint =
process.env.CHITTYAUTH_ENDPOINT || "https://chittyauth-prod.workers.dev";
// Note: Configure for account ending in 121 for production deployment
this.authToken = process.env.CHITTY_AUTH_TOKEN;
this.syncInterval = 15000; // 15 seconds
this.currentSessionId =
process.env.CLAUDE_SESSION_ID || this.generateSessionId();
// Ensure directories exist
if (!existsSync(this.sessionsDir)) {
mkdirSync(this.sessionsDir, { recursive: true });
}
this.config = this.loadConfig();
this.sessionState = this.loadSessionState();
}
/**
* Generate unique session ID
*/
generateSessionId() {
return `session-${Date.now()}`;
}
/**
* Load session sync configuration
*/
loadConfig() {
if (existsSync(this.configFile)) {
try {
return JSON.parse(readFileSync(this.configFile, "utf8"));
} catch (error) {
console.error("Failed to load session config:", error);
}
}
const defaultConfig = {
version: "1.0.0",
session_sync: {
enabled: true,
auto_start: true,
sync_interval: 15000,
max_sessions: 50,
retention_days: 30,
},
chittyauth_integration: {
enabled: true,
validate_sessions: true,
cross_session_sync: true,
session_timeout: 86400000, // 24 hours
},
sync_data: {
mcp_projects: true,
tool_usage: true,
session_context: true,
chittyid_operations: true,
ai_conversations: false, // Privacy-sensitive
file_operations: true,
},
security: {
encrypt_sensitive: true,
hash_sessions: true,
audit_trail: true,
},
};
this.saveConfig(defaultConfig);
return defaultConfig;
}
/**
* Save configuration
*/
saveConfig(config = this.config) {
try {
writeFileSync(this.configFile, JSON.stringify(config, null, 2));
} catch (error) {
console.error("Failed to save config:", error);
}
}
/**
* Load current session state
*/
loadSessionState() {
const sessionFile = join(this.sessionsDir, `${this.currentSessionId}.json`);
if (existsSync(sessionFile)) {
try {
const state = JSON.parse(readFileSync(sessionFile, "utf8"));
console.log(
`[SESSION] Loaded existing session: ${this.currentSessionId}`,
);
return state;
} catch (error) {
console.error("Failed to load session state:", error);
}
}
// Create new session state
const newState = {
session_id: this.currentSessionId,
created_at: new Date().toISOString(),
last_sync: new Date().toISOString(),
chittyauth: {
authenticated: false,
token_hash: null,
validation_count: 0,
},
mcp_projects: [],
tool_usage: {
chittyid: { count: 0, last_used: null },
langchain: { count: 0, last_used: null },
chittycases: { count: 0, last_used: null },
},
context: {
working_directory: process.cwd(),
environment: process.env.NODE_ENV || "development",
platform: process.platform,
user_agent: "chittyos-session-sync/1.0.0",
},
operations: [],
sync_history: [],
};
this.saveSessionState(newState);
console.log(`[SESSION] Created new session: ${this.currentSessionId}`);
return newState;
}
/**
* Save session state
*/
saveSessionState(state = this.sessionState) {
const sessionFile = join(this.sessionsDir, `${this.currentSessionId}.json`);
try {
state.last_sync = new Date().toISOString();
writeFileSync(sessionFile, JSON.stringify(state, null, 2));
} catch (error) {
console.error("Failed to save session state:", error);
}
}
/**
* Authenticate session with ChittyAuth
*/
async authenticateSession() {
if (!this.config.chittyauth_integration.enabled || !this.authToken) {
console.log("[SESSION] ChittyAuth integration disabled or no token");
return false;
}
try {
const response = await fetch(
`${this.chittyAuthEndpoint}/api/v1/sessions/validate`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.authToken}`,
},
body: JSON.stringify({
session_id: this.currentSessionId,
service: "session-sync",
timestamp: new Date().toISOString(),
context: this.sessionState.context,
}),
},
);
if (!response.ok) {
throw new Error(
`ChittyAuth session validation failed: ${response.status}`,
);
}
const result = await response.json();
// Update session state
this.sessionState.chittyauth.authenticated = result.valid;
this.sessionState.chittyauth.validation_count++;
this.sessionState.chittyauth.last_validation = new Date().toISOString();
if (result.valid) {
console.log(
`[SESSION] Authenticated with ChittyAuth: ${this.currentSessionId}`,
);
}
this.saveSessionState();
return result.valid;
} catch (error) {
console.error("[SESSION] ChittyAuth authentication failed:", error);
this.sessionState.chittyauth.authenticated = false;
this.sessionState.chittyauth.last_error = error.message;
this.saveSessionState();
return false;
}
}
/**
* Sync session with remote ChittyAuth storage
*/
async syncWithRemote() {
if (!this.config.chittyauth_integration.enabled || !this.authToken) {
console.log("[SESSION] Remote sync disabled");
return { synced: false, reason: "Integration disabled" };
}
try {
// Upload current session state
const response = await fetch(
`${this.chittyAuthEndpoint}/api/v1/sessions/${this.currentSessionId}/sync`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.authToken}`,
},
body: JSON.stringify({
session_data: this.sessionState,
sync_type: "full_state",
timestamp: new Date().toISOString(),
}),
},
);
if (!response.ok) {
throw new Error(`Session sync failed: ${response.status}`);
}
const result = await response.json();
// Update sync history
this.sessionState.sync_history.push({
timestamp: new Date().toISOString(),
type: "remote_sync",
success: true,
sync_id: result.sync_id,
});
// Keep only last 10 sync records
if (this.sessionState.sync_history.length > 10) {
this.sessionState.sync_history =
this.sessionState.sync_history.slice(-10);
}
this.saveSessionState();
console.log(`[SESSION] Synced with remote storage: ${result.sync_id}`);
return {
synced: true,
sync_id: result.sync_id,
timestamp: new Date().toISOString(),
};
} catch (error) {
console.error("[SESSION] Remote sync failed:", error);
this.sessionState.sync_history.push({
timestamp: new Date().toISOString(),
type: "remote_sync",
success: false,
error: error.message,
});
this.saveSessionState();
return { synced: false, error: error.message };
}
}
/**
* Record tool usage
*/
recordToolUsage(toolName, parameters, result) {
if (!this.config.sync_data.tool_usage) return;
const operation = {
id: `op-${Date.now()}`,
timestamp: new Date().toISOString(),
tool: toolName,
parameters: this.config.security.encrypt_sensitive
? "[ENCRYPTED]"
: parameters,
result_hash: this.hashObject(result),
success: !result.error,
};
// Update tool usage stats
if (this.sessionState.tool_usage[toolName]) {
this.sessionState.tool_usage[toolName].count++;
this.sessionState.tool_usage[toolName].last_used = operation.timestamp;
} else {
this.sessionState.tool_usage[toolName] = {
count: 1,
last_used: operation.timestamp,
};
}
// Add to operations log
this.sessionState.operations.push(operation);
// Keep only last 100 operations
if (this.sessionState.operations.length > 100) {
this.sessionState.operations = this.sessionState.operations.slice(-100);
}
this.saveSessionState();
console.log(`[SESSION] Recorded tool usage: ${toolName}`);
}
/**
* Register MCP project with session
*/
registerMCPProject(projectId, projectData) {
if (!this.config.sync_data.mcp_projects) return;
const existingIndex = this.sessionState.mcp_projects.findIndex(
(p) => p.id === projectId,
);
const projectRecord = {
id: projectId,
name: projectData.name,
registered_at: new Date().toISOString(),
tools_available: projectData.tools_available || [],
last_activity: new Date().toISOString(),
};
if (existingIndex >= 0) {
this.sessionState.mcp_projects[existingIndex] = projectRecord;
} else {
this.sessionState.mcp_projects.push(projectRecord);
}
this.saveSessionState();
console.log(`[SESSION] Registered MCP project: ${projectData.name}`);
}
/**
* Get cross-session project history
*/
getCrossSessionHistory() {
const sessionFiles = readdirSync(this.sessionsDir).filter((f) =>
f.endsWith(".json"),
);
const history = {
total_sessions: sessionFiles.length,
sessions: [],
mcp_projects: new Set(),
tool_usage: {},
};
for (const file of sessionFiles.slice(-10)) {
// Last 10 sessions
try {
const sessionData = JSON.parse(
readFileSync(join(this.sessionsDir, file), "utf8"),
);
history.sessions.push({
session_id: sessionData.session_id,
created_at: sessionData.created_at,
last_sync: sessionData.last_sync,
operations_count: sessionData.operations?.length || 0,
mcp_projects_count: sessionData.mcp_projects?.length || 0,
});
// Aggregate MCP projects
if (sessionData.mcp_projects) {
sessionData.mcp_projects.forEach((p) =>
history.mcp_projects.add(p.name),
);
}
// Aggregate tool usage
if (sessionData.tool_usage) {
Object.keys(sessionData.tool_usage).forEach((tool) => {
history.tool_usage[tool] =
(history.tool_usage[tool] || 0) +
sessionData.tool_usage[tool].count;
});
}
} catch (error) {
console.error(`Failed to read session file ${file}:`, error);
}
}
history.mcp_projects = Array.from(history.mcp_projects);
return history;
}
/**
* Start periodic sync
*/
startPeriodicSync() {
if (!this.config.session_sync.enabled) {
console.log("[SESSION] Periodic sync disabled");
return;
}
console.log(
`[SESSION] Starting periodic sync every ${this.syncInterval}ms`,
);
setInterval(async () => {
try {
await this.syncWithRemote();
} catch (error) {
console.error("[SESSION] Periodic sync error:", error);
}
}, this.syncInterval);
}
/**
* Clean up old sessions
*/
cleanupOldSessions() {
const retentionMs =
this.config.session_sync.retention_days * 24 * 60 * 60 * 1000;
const cutoffDate = new Date(Date.now() - retentionMs);
const sessionFiles = readdirSync(this.sessionsDir).filter((f) =>
f.endsWith(".json"),
);
let cleanedCount = 0;
for (const file of sessionFiles) {
const filePath = join(this.sessionsDir, file);
try {
const sessionData = JSON.parse(readFileSync(filePath, "utf8"));
const createdAt = new Date(sessionData.created_at);
if (createdAt < cutoffDate) {
require("fs").unlinkSync(filePath);
cleanedCount++;
}
} catch (error) {
console.error(`Failed to process session file ${file}:`, error);
}
}
if (cleanedCount > 0) {
console.log(`[SESSION] Cleaned up ${cleanedCount} old sessions`);
}
return cleanedCount;
}
/**
* Hash object for security
*/
hashObject(obj) {
if (!this.config.security.hash_sessions) return null;
return createHash("sha256")
.update(JSON.stringify(obj))
.digest("hex")
.substr(0, 16);
}
/**
* Health check
*/
async healthCheck() {
const history = this.getCrossSessionHistory();
return {
healthy: true,
session_sync_version: this.config.version,
current_session: {
id: this.currentSessionId,
created_at: this.sessionState.created_at,
last_sync: this.sessionState.last_sync,
authenticated: this.sessionState.chittyauth.authenticated,
operations_count: this.sessionState.operations.length,
mcp_projects_count: this.sessionState.mcp_projects.length,
},
cross_session_stats: {
total_sessions: history.total_sessions,
recent_sessions: history.sessions.length,
unique_mcp_projects: history.mcp_projects.length,
total_tool_usage: Object.values(history.tool_usage).reduce(
(a, b) => a + b,
0,
),
},
configuration: {
sync_enabled: this.config.session_sync.enabled,
chittyauth_enabled: this.config.chittyauth_integration.enabled,
sync_interval: this.syncInterval,
retention_days: this.config.session_sync.retention_days,
},
directories: {
sessions_dir: this.sessionsDir,
config_file: this.configFile,
},
timestamp: new Date().toISOString(),
};
}
}
// CLI interface
if (import.meta.url === `file://${process.argv[1]}`) {
const sessionSync = new ChittySessionSync();
const command = process.argv[2];
switch (command) {
case "start":
console.log("[SESSION] Starting session sync...");
sessionSync.authenticateSession().then((authenticated) => {
if (authenticated) {
sessionSync.startPeriodicSync();
} else {
console.log("[SESSION] Running without ChittyAuth authentication");
sessionSync.startPeriodicSync();
}
});
break;
case "auth":
sessionSync.authenticateSession().then((result) => {
console.log(`[SESSION] Authentication result: ${result}`);
});
break;
case "sync":
sessionSync.syncWithRemote().then((result) => {
console.log("[SESSION] Sync result:");
console.log(JSON.stringify(result, null, 2));
});
break;
case "record":
const toolName = process.argv[3];
const params = process.argv[4] || "{}";
const result = process.argv[5] || '{"success": true}';
if (!toolName) {
console.error(
"Tool name required: node session-sync.js record <tool-name> [params] [result]",
);
process.exit(1);
}
try {
sessionSync.recordToolUsage(
toolName,
JSON.parse(params),
JSON.parse(result),
);
console.log(`[SESSION] Recorded usage for tool: ${toolName}`);
} catch (error) {
console.error("Failed to record tool usage:", error);
}
break;
case "mcp":
const projectId = process.argv[3];
const projectName = process.argv[4] || "unnamed-project";
if (!projectId) {
console.error(
"Project ID required: node session-sync.js mcp <project-id> [project-name]",
);
process.exit(1);
}
sessionSync.registerMCPProject(projectId, {
name: projectName,
tools_available: ["chittyid", "langchain", "chittycases"],
});
console.log(`[SESSION] Registered MCP project: ${projectName}`);
break;
case "history":
const history = sessionSync.getCrossSessionHistory();
console.log("Cross-Session History:");
console.log("=====================");
console.log(JSON.stringify(history, null, 2));
break;
case "cleanup":
const cleaned = sessionSync.cleanupOldSessions();
console.log(`[SESSION] Cleanup completed: ${cleaned} sessions removed`);
break;
case "health":
sessionSync.healthCheck().then((result) => {
console.log(JSON.stringify(result, null, 2));
});
break;
default:
console.log(`
ChittyOS Session Sync Commands:
start - Start session sync with ChittyAuth
auth - Authenticate current session
sync - Sync session with remote storage
record <tool> [params] [result] - Record tool usage
mcp <project-id> [name] - Register MCP project
history - Show cross-session history
cleanup - Clean up old sessions
health - Check session sync health
Environment Variables:
CHITTY_AUTH_TOKEN - ChittyAuth authentication token
CHITTYAUTH_ENDPOINT - ChittyAuth service endpoint
CLAUDE_SESSION_ID - Current Claude session ID
Features:
• Cross-session state persistence
• ChittyAuth integration for secure sync
• MCP project tracking across sessions
• Tool usage analytics and history
• Automatic cleanup of old sessions
• Secure session authentication
Example Usage:
node session-sync.js start
node session-sync.js record chittyid '{"command":"gen","type":"person"}' '{"success":true,"id":"01-1-ABC-1234-P-25-1-82"}'
node session-sync.js mcp mcp-demo-123 "chittyid-foundation"
node session-sync.js history
`);
}
}
export { ChittySessionSync };