@juspay/neurolink 6.1.0 → 6.2.0
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 +10 -6
- package/dist/cli/commands/config.d.ts +13 -13
- package/dist/cli/index.js +3 -9
- package/dist/core/analytics.d.ts +11 -1
- package/dist/core/analytics.js +2 -2
- package/dist/core/base-provider.js +4 -18
- package/dist/core/dynamic-models.d.ts +8 -8
- package/dist/core/factory.js +3 -11
- package/dist/index.d.ts +2 -4
- package/dist/index.js +2 -11
- package/dist/lib/core/analytics.d.ts +11 -1
- package/dist/lib/core/analytics.js +2 -2
- package/dist/lib/core/base-provider.js +4 -18
- package/dist/lib/core/dynamic-models.d.ts +8 -8
- package/dist/lib/core/factory.js +3 -11
- package/dist/lib/index.d.ts +2 -4
- package/dist/lib/index.js +2 -11
- package/dist/lib/mcp/factory.d.ts +1 -1
- package/dist/lib/mcp/index.d.ts +7 -19
- package/dist/lib/mcp/index.js +15 -56
- package/dist/lib/neurolink.d.ts +1 -9
- package/dist/lib/neurolink.js +21 -70
- package/dist/lib/providers/amazon-bedrock.js +5 -16
- package/dist/lib/providers/anthropic-baseprovider.js +3 -21
- package/dist/lib/providers/anthropic.js +4 -7
- package/dist/lib/providers/azure-openai.js +4 -2
- package/dist/lib/providers/google-vertex.js +5 -9
- package/dist/lib/providers/huggingFace.js +5 -10
- package/dist/lib/providers/mistral.js +5 -8
- package/dist/lib/providers/openAI.js +4 -7
- package/dist/lib/utils/providerConfig.d.ts +117 -0
- package/dist/lib/utils/providerConfig.js +353 -0
- package/dist/lib/utils/providerUtils.d.ts +2 -1
- package/dist/lib/utils/providerUtils.js +53 -36
- package/dist/lib/utils/timeout.d.ts +72 -1
- package/dist/lib/utils/timeout.js +203 -2
- package/dist/mcp/factory.d.ts +1 -1
- package/dist/mcp/index.d.ts +7 -19
- package/dist/mcp/index.js +15 -56
- package/dist/neurolink.d.ts +1 -9
- package/dist/neurolink.js +21 -70
- package/dist/providers/amazon-bedrock.js +5 -16
- package/dist/providers/anthropic-baseprovider.js +3 -21
- package/dist/providers/anthropic.js +4 -7
- package/dist/providers/azure-openai.js +4 -2
- package/dist/providers/google-vertex.js +5 -9
- package/dist/providers/huggingFace.js +5 -10
- package/dist/providers/mistral.js +5 -8
- package/dist/providers/openAI.js +4 -7
- package/dist/utils/providerConfig.d.ts +117 -0
- package/dist/utils/providerConfig.js +353 -0
- package/dist/utils/providerUtils.d.ts +2 -1
- package/dist/utils/providerUtils.js +53 -36
- package/dist/utils/timeout.d.ts +72 -1
- package/dist/utils/timeout.js +203 -2
- package/package.json +1 -1
- package/dist/chat/client-utils.d.ts +0 -95
- package/dist/chat/client-utils.js +0 -315
- package/dist/chat/index.d.ts +0 -24
- package/dist/chat/index.js +0 -33
- package/dist/chat/session-storage.d.ts +0 -77
- package/dist/chat/session-storage.js +0 -233
- package/dist/chat/session.d.ts +0 -96
- package/dist/chat/session.js +0 -257
- package/dist/chat/sse-handler.d.ts +0 -49
- package/dist/chat/sse-handler.js +0 -259
- package/dist/chat/types.d.ts +0 -74
- package/dist/chat/types.js +0 -5
- package/dist/chat/websocket-chat-handler.d.ts +0 -37
- package/dist/chat/websocket-chat-handler.js +0 -262
- package/dist/cli/commands/mcp.d.ts +0 -20
- package/dist/cli/commands/mcp.js +0 -1272
- package/dist/core/defaults.d.ts +0 -19
- package/dist/core/defaults.js +0 -29
- package/dist/core/evaluation-config.d.ts +0 -29
- package/dist/core/evaluation-config.js +0 -144
- package/dist/factories/compatibility-factory.d.ts +0 -35
- package/dist/factories/compatibility-factory.js +0 -71
- package/dist/factories/provider-generate-factory.d.ts +0 -20
- package/dist/factories/provider-generate-factory.js +0 -93
- package/dist/lib/chat/client-utils.d.ts +0 -95
- package/dist/lib/chat/client-utils.js +0 -315
- package/dist/lib/chat/index.d.ts +0 -24
- package/dist/lib/chat/index.js +0 -33
- package/dist/lib/chat/session-storage.d.ts +0 -77
- package/dist/lib/chat/session-storage.js +0 -233
- package/dist/lib/chat/session.d.ts +0 -96
- package/dist/lib/chat/session.js +0 -257
- package/dist/lib/chat/sse-handler.d.ts +0 -49
- package/dist/lib/chat/sse-handler.js +0 -259
- package/dist/lib/chat/types.d.ts +0 -74
- package/dist/lib/chat/types.js +0 -5
- package/dist/lib/chat/websocket-chat-handler.d.ts +0 -37
- package/dist/lib/chat/websocket-chat-handler.js +0 -262
- package/dist/lib/core/defaults.d.ts +0 -19
- package/dist/lib/core/defaults.js +0 -29
- package/dist/lib/core/evaluation-config.d.ts +0 -29
- package/dist/lib/core/evaluation-config.js +0 -144
- package/dist/lib/factories/compatibility-factory.d.ts +0 -35
- package/dist/lib/factories/compatibility-factory.js +0 -71
- package/dist/lib/factories/provider-generate-factory.d.ts +0 -20
- package/dist/lib/factories/provider-generate-factory.js +0 -93
- package/dist/lib/mcp/adapters/plugin-bridge.d.ts +0 -40
- package/dist/lib/mcp/adapters/plugin-bridge.js +0 -89
- package/dist/lib/mcp/auto-discovery.d.ts +0 -62
- package/dist/lib/mcp/auto-discovery.js +0 -149
- package/dist/lib/mcp/client.d.ts +0 -68
- package/dist/lib/mcp/client.js +0 -248
- package/dist/lib/mcp/config.d.ts +0 -31
- package/dist/lib/mcp/config.js +0 -99
- package/dist/lib/mcp/context-manager.d.ts +0 -171
- package/dist/lib/mcp/context-manager.js +0 -362
- package/dist/lib/mcp/contracts/mcp-contract.d.ts +0 -169
- package/dist/lib/mcp/contracts/mcp-contract.js +0 -58
- package/dist/lib/mcp/core/plugin-manager.d.ts +0 -46
- package/dist/lib/mcp/core/plugin-manager.js +0 -110
- package/dist/lib/mcp/demo/plugin-demo.d.ts +0 -20
- package/dist/lib/mcp/demo/plugin-demo.js +0 -118
- package/dist/lib/mcp/dynamic-chain-executor.d.ts +0 -225
- package/dist/lib/mcp/dynamic-chain-executor.js +0 -489
- package/dist/lib/mcp/dynamic-orchestrator.d.ts +0 -115
- package/dist/lib/mcp/dynamic-orchestrator.js +0 -351
- package/dist/lib/mcp/ecosystem.d.ts +0 -75
- package/dist/lib/mcp/ecosystem.js +0 -161
- package/dist/lib/mcp/error-manager.d.ts +0 -254
- package/dist/lib/mcp/error-manager.js +0 -501
- package/dist/lib/mcp/error-recovery.d.ts +0 -159
- package/dist/lib/mcp/error-recovery.js +0 -405
- package/dist/lib/mcp/external-client.d.ts +0 -88
- package/dist/lib/mcp/external-client.js +0 -331
- package/dist/lib/mcp/external-manager.d.ts +0 -112
- package/dist/lib/mcp/external-manager.js +0 -308
- package/dist/lib/mcp/function-calling.d.ts +0 -65
- package/dist/lib/mcp/function-calling.js +0 -642
- package/dist/lib/mcp/health-monitor.d.ts +0 -257
- package/dist/lib/mcp/health-monitor.js +0 -630
- package/dist/lib/mcp/initialize-tools.d.ts +0 -29
- package/dist/lib/mcp/initialize-tools.js +0 -261
- package/dist/lib/mcp/initialize.d.ts +0 -18
- package/dist/lib/mcp/initialize.js +0 -62
- package/dist/lib/mcp/manager.d.ts +0 -68
- package/dist/lib/mcp/manager.js +0 -176
- package/dist/lib/mcp/neurolink-mcp-client.d.ts +0 -97
- package/dist/lib/mcp/neurolink-mcp-client.js +0 -462
- package/dist/lib/mcp/orchestrator.d.ts +0 -302
- package/dist/lib/mcp/orchestrator.js +0 -703
- package/dist/lib/mcp/plugin-manager.d.ts +0 -98
- package/dist/lib/mcp/plugin-manager.js +0 -296
- package/dist/lib/mcp/plugins/core/filesystem-mcp.d.ts +0 -36
- package/dist/lib/mcp/plugins/core/filesystem-mcp.js +0 -142
- package/dist/lib/mcp/plugins/filesystem-mcp.d.ts +0 -37
- package/dist/lib/mcp/plugins/filesystem-mcp.js +0 -54
- package/dist/lib/mcp/security-manager.d.ts +0 -87
- package/dist/lib/mcp/security-manager.js +0 -344
- package/dist/lib/mcp/semaphore-manager.d.ts +0 -137
- package/dist/lib/mcp/semaphore-manager.js +0 -329
- package/dist/lib/mcp/session-manager.d.ts +0 -187
- package/dist/lib/mcp/session-manager.js +0 -400
- package/dist/lib/mcp/session-persistence.d.ts +0 -93
- package/dist/lib/mcp/session-persistence.js +0 -301
- package/dist/lib/mcp/tool-integration.d.ts +0 -58
- package/dist/lib/mcp/tool-integration.js +0 -203
- package/dist/lib/mcp/transport-manager.d.ts +0 -154
- package/dist/lib/mcp/transport-manager.js +0 -334
- package/dist/lib/mcp/unified-mcp.d.ts +0 -133
- package/dist/lib/mcp/unified-mcp.js +0 -251
- package/dist/lib/mcp/unified-registry.d.ts +0 -165
- package/dist/lib/mcp/unified-registry.js +0 -538
- package/dist/lib/providers/analytics-helper.d.ts +0 -38
- package/dist/lib/providers/analytics-helper.js +0 -216
- package/dist/lib/providers/function-calling-provider.d.ts +0 -142
- package/dist/lib/providers/function-calling-provider.js +0 -630
- package/dist/lib/providers/mcp-provider.d.ts +0 -75
- package/dist/lib/providers/mcp-provider.js +0 -283
- package/dist/lib/providers/timeout-wrapper.d.ts +0 -40
- package/dist/lib/providers/timeout-wrapper.js +0 -100
- package/dist/lib/sdk/tool-extension.d.ts +0 -181
- package/dist/lib/sdk/tool-extension.js +0 -284
- package/dist/lib/services/streaming/streaming-manager.d.ts +0 -29
- package/dist/lib/services/streaming/streaming-manager.js +0 -245
- package/dist/lib/services/types.d.ts +0 -156
- package/dist/lib/services/types.js +0 -2
- package/dist/lib/services/websocket/websocket-server.d.ts +0 -34
- package/dist/lib/services/websocket/websocket-server.js +0 -305
- package/dist/lib/utils/provider-validation.d.ts +0 -36
- package/dist/lib/utils/provider-validation.js +0 -625
- package/dist/lib/utils/providerUtils-fixed.d.ts +0 -8
- package/dist/lib/utils/providerUtils-fixed.js +0 -94
- package/dist/lib/utils/streaming-utils.d.ts +0 -79
- package/dist/lib/utils/streaming-utils.js +0 -198
- package/dist/lib/utils/timeout-manager.d.ts +0 -75
- package/dist/lib/utils/timeout-manager.js +0 -244
- package/dist/mcp/adapters/plugin-bridge.d.ts +0 -40
- package/dist/mcp/adapters/plugin-bridge.js +0 -89
- package/dist/mcp/auto-discovery.d.ts +0 -62
- package/dist/mcp/auto-discovery.js +0 -149
- package/dist/mcp/client.d.ts +0 -68
- package/dist/mcp/client.js +0 -248
- package/dist/mcp/config.d.ts +0 -31
- package/dist/mcp/config.js +0 -99
- package/dist/mcp/context-manager.d.ts +0 -171
- package/dist/mcp/context-manager.js +0 -362
- package/dist/mcp/contracts/mcp-contract.d.ts +0 -169
- package/dist/mcp/contracts/mcp-contract.js +0 -58
- package/dist/mcp/core/plugin-manager.d.ts +0 -46
- package/dist/mcp/core/plugin-manager.js +0 -110
- package/dist/mcp/demo/plugin-demo.d.ts +0 -20
- package/dist/mcp/demo/plugin-demo.js +0 -118
- package/dist/mcp/dynamic-chain-executor.d.ts +0 -225
- package/dist/mcp/dynamic-chain-executor.js +0 -489
- package/dist/mcp/dynamic-orchestrator.d.ts +0 -115
- package/dist/mcp/dynamic-orchestrator.js +0 -351
- package/dist/mcp/ecosystem.d.ts +0 -75
- package/dist/mcp/ecosystem.js +0 -162
- package/dist/mcp/error-manager.d.ts +0 -254
- package/dist/mcp/error-manager.js +0 -501
- package/dist/mcp/error-recovery.d.ts +0 -159
- package/dist/mcp/error-recovery.js +0 -405
- package/dist/mcp/external-client.d.ts +0 -88
- package/dist/mcp/external-client.js +0 -331
- package/dist/mcp/external-manager.d.ts +0 -112
- package/dist/mcp/external-manager.js +0 -308
- package/dist/mcp/function-calling.d.ts +0 -65
- package/dist/mcp/function-calling.js +0 -642
- package/dist/mcp/health-monitor.d.ts +0 -257
- package/dist/mcp/health-monitor.js +0 -630
- package/dist/mcp/initialize-tools.d.ts +0 -29
- package/dist/mcp/initialize-tools.js +0 -262
- package/dist/mcp/initialize.d.ts +0 -18
- package/dist/mcp/initialize.js +0 -62
- package/dist/mcp/manager.d.ts +0 -68
- package/dist/mcp/manager.js +0 -176
- package/dist/mcp/neurolink-mcp-client.d.ts +0 -97
- package/dist/mcp/neurolink-mcp-client.js +0 -462
- package/dist/mcp/orchestrator.d.ts +0 -302
- package/dist/mcp/orchestrator.js +0 -703
- package/dist/mcp/plugin-manager.d.ts +0 -98
- package/dist/mcp/plugin-manager.js +0 -297
- package/dist/mcp/plugins/core/filesystem-mcp.d.ts +0 -36
- package/dist/mcp/plugins/core/filesystem-mcp.js +0 -142
- package/dist/mcp/plugins/core/neurolink-mcp.json +0 -17
- package/dist/mcp/plugins/filesystem-mcp.d.ts +0 -37
- package/dist/mcp/plugins/filesystem-mcp.js +0 -54
- package/dist/mcp/security-manager.d.ts +0 -87
- package/dist/mcp/security-manager.js +0 -344
- package/dist/mcp/semaphore-manager.d.ts +0 -137
- package/dist/mcp/semaphore-manager.js +0 -329
- package/dist/mcp/session-manager.d.ts +0 -187
- package/dist/mcp/session-manager.js +0 -400
- package/dist/mcp/session-persistence.d.ts +0 -93
- package/dist/mcp/session-persistence.js +0 -302
- package/dist/mcp/tool-integration.d.ts +0 -58
- package/dist/mcp/tool-integration.js +0 -203
- package/dist/mcp/transport-manager.d.ts +0 -154
- package/dist/mcp/transport-manager.js +0 -335
- package/dist/mcp/unified-mcp.d.ts +0 -133
- package/dist/mcp/unified-mcp.js +0 -251
- package/dist/mcp/unified-registry.d.ts +0 -165
- package/dist/mcp/unified-registry.js +0 -539
- package/dist/providers/analytics-helper.d.ts +0 -38
- package/dist/providers/analytics-helper.js +0 -216
- package/dist/providers/function-calling-provider.d.ts +0 -142
- package/dist/providers/function-calling-provider.js +0 -630
- package/dist/providers/mcp-provider.d.ts +0 -75
- package/dist/providers/mcp-provider.js +0 -283
- package/dist/providers/timeout-wrapper.d.ts +0 -40
- package/dist/providers/timeout-wrapper.js +0 -100
- package/dist/sdk/tool-extension.d.ts +0 -181
- package/dist/sdk/tool-extension.js +0 -284
- package/dist/services/streaming/streaming-manager.d.ts +0 -29
- package/dist/services/streaming/streaming-manager.js +0 -245
- package/dist/services/types.d.ts +0 -156
- package/dist/services/types.js +0 -2
- package/dist/services/websocket/websocket-server.d.ts +0 -34
- package/dist/services/websocket/websocket-server.js +0 -306
- package/dist/utils/provider-validation.d.ts +0 -36
- package/dist/utils/provider-validation.js +0 -625
- package/dist/utils/providerUtils-fixed.d.ts +0 -8
- package/dist/utils/providerUtils-fixed.js +0 -94
- package/dist/utils/streaming-utils.d.ts +0 -79
- package/dist/utils/streaming-utils.js +0 -198
- package/dist/utils/timeout-manager.d.ts +0 -75
- package/dist/utils/timeout-manager.js +0 -244
|
@@ -1,302 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* NeuroLink Session Persistence Layer
|
|
3
|
-
* Provides file-based persistence for sessions with atomic writes and recovery
|
|
4
|
-
*/
|
|
5
|
-
import fs from "fs/promises";
|
|
6
|
-
import path from "path";
|
|
7
|
-
import crypto from "crypto";
|
|
8
|
-
import { existsSync } from "fs";
|
|
9
|
-
/**
|
|
10
|
-
* Session Persistence Manager
|
|
11
|
-
* Handles saving and loading sessions to/from disk with recovery mechanisms
|
|
12
|
-
*/
|
|
13
|
-
export class SessionPersistence {
|
|
14
|
-
directory;
|
|
15
|
-
snapshotInterval;
|
|
16
|
-
retentionPeriod;
|
|
17
|
-
enableChecksum;
|
|
18
|
-
maxRetries;
|
|
19
|
-
snapshotTimer = null;
|
|
20
|
-
sessionMap;
|
|
21
|
-
constructor(sessionMap, options = {}) {
|
|
22
|
-
this.sessionMap = sessionMap;
|
|
23
|
-
this.directory = options.directory || ".neurolink/sessions";
|
|
24
|
-
this.snapshotInterval = options.snapshotInterval || 30000; // 30 seconds
|
|
25
|
-
this.retentionPeriod = options.retentionPeriod || 86400000; // 24 hours
|
|
26
|
-
this.enableChecksum = options.enableChecksum ?? true;
|
|
27
|
-
this.maxRetries = options.maxRetries || 3;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Initialize persistence layer
|
|
31
|
-
*/
|
|
32
|
-
async initialize() {
|
|
33
|
-
// Create sessions directory if it doesn't exist
|
|
34
|
-
await this.ensureDirectory();
|
|
35
|
-
// Load existing sessions
|
|
36
|
-
await this.loadSessions();
|
|
37
|
-
// Start snapshot timer
|
|
38
|
-
this.startSnapshotTimer();
|
|
39
|
-
if (process.env.NEUROLINK_DEBUG === "true") {
|
|
40
|
-
console.log(`[SessionPersistence] Initialized with directory: ${this.directory}`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Ensure sessions directory exists
|
|
45
|
-
*/
|
|
46
|
-
async ensureDirectory() {
|
|
47
|
-
await fs.mkdir(this.directory, { recursive: true });
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Calculate checksum for data integrity
|
|
51
|
-
*/
|
|
52
|
-
calculateChecksum(data) {
|
|
53
|
-
return crypto.createHash("sha256").update(data).digest("hex");
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Serialize session to JSON with metadata
|
|
57
|
-
*/
|
|
58
|
-
serializeSession(session) {
|
|
59
|
-
// Convert Map to array for JSON serialization
|
|
60
|
-
const sessionData = {
|
|
61
|
-
...session,
|
|
62
|
-
state: Array.from(session.state.entries()),
|
|
63
|
-
};
|
|
64
|
-
const fileData = {
|
|
65
|
-
session: sessionData,
|
|
66
|
-
checksum: "",
|
|
67
|
-
version: "1.0",
|
|
68
|
-
lastSaved: Date.now(),
|
|
69
|
-
};
|
|
70
|
-
if (this.enableChecksum) {
|
|
71
|
-
// First serialization without checksum
|
|
72
|
-
const tempJsonData = JSON.stringify(fileData, null, 2);
|
|
73
|
-
// Calculate checksum based on the first serialization
|
|
74
|
-
fileData.checksum = this.calculateChecksum(tempJsonData);
|
|
75
|
-
}
|
|
76
|
-
// Final serialization with checksum included
|
|
77
|
-
return JSON.stringify(fileData, null, 2);
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Deserialize session from JSON
|
|
81
|
-
*/
|
|
82
|
-
deserializeSession(data) {
|
|
83
|
-
try {
|
|
84
|
-
const fileData = JSON.parse(data);
|
|
85
|
-
// Verify checksum if enabled
|
|
86
|
-
if (this.enableChecksum && fileData.checksum) {
|
|
87
|
-
const tempData = { ...fileData, checksum: "" };
|
|
88
|
-
const expectedChecksum = this.calculateChecksum(JSON.stringify(tempData, null, 2));
|
|
89
|
-
if (fileData.checksum !== expectedChecksum) {
|
|
90
|
-
console.error("[SessionPersistence] Checksum mismatch, session corrupted");
|
|
91
|
-
return null;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
// Convert state array back to Map
|
|
95
|
-
const serializedSession = fileData.session;
|
|
96
|
-
const session = {
|
|
97
|
-
...serializedSession,
|
|
98
|
-
state: new Map(serializedSession.state),
|
|
99
|
-
};
|
|
100
|
-
return session;
|
|
101
|
-
}
|
|
102
|
-
catch (error) {
|
|
103
|
-
console.error("[SessionPersistence] Failed to deserialize session:", error);
|
|
104
|
-
return null;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Save single session with atomic write
|
|
109
|
-
*/
|
|
110
|
-
async saveSession(sessionId, session) {
|
|
111
|
-
const filename = `${sessionId}.json`;
|
|
112
|
-
const filepath = path.join(this.directory, filename);
|
|
113
|
-
const tempPath = `${filepath}.tmp`;
|
|
114
|
-
let retries = 0;
|
|
115
|
-
while (retries < this.maxRetries) {
|
|
116
|
-
try {
|
|
117
|
-
// Serialize session
|
|
118
|
-
const data = this.serializeSession(session);
|
|
119
|
-
// Write to temporary file first (atomic write pattern)
|
|
120
|
-
await fs.writeFile(tempPath, data, "utf8");
|
|
121
|
-
// Rename temp file to final location (atomic operation)
|
|
122
|
-
await fs.rename(tempPath, filepath);
|
|
123
|
-
if (process.env.NEUROLINK_DEBUG === "true") {
|
|
124
|
-
console.log(`[SessionPersistence] Saved session ${sessionId}`);
|
|
125
|
-
}
|
|
126
|
-
return true;
|
|
127
|
-
}
|
|
128
|
-
catch (error) {
|
|
129
|
-
retries++;
|
|
130
|
-
console.error(`[SessionPersistence] Failed to save session (attempt ${retries}):`, error);
|
|
131
|
-
// Clean up temp file if it exists
|
|
132
|
-
try {
|
|
133
|
-
await fs.unlink(tempPath);
|
|
134
|
-
}
|
|
135
|
-
catch {
|
|
136
|
-
// Ignore errors when cleaning up temp file
|
|
137
|
-
}
|
|
138
|
-
if (retries < this.maxRetries) {
|
|
139
|
-
// Exponential backoff
|
|
140
|
-
await new Promise((resolve) => setTimeout(resolve, Math.pow(2, retries) * 100));
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Load single session from disk
|
|
148
|
-
*/
|
|
149
|
-
async loadSession(sessionId) {
|
|
150
|
-
const filename = `${sessionId}.json`;
|
|
151
|
-
const filepath = path.join(this.directory, filename);
|
|
152
|
-
try {
|
|
153
|
-
const data = await fs.readFile(filepath, "utf8");
|
|
154
|
-
return this.deserializeSession(data);
|
|
155
|
-
}
|
|
156
|
-
catch (error) {
|
|
157
|
-
if (error.code !== "ENOENT") {
|
|
158
|
-
console.error(`[SessionPersistence] Failed to load session ${sessionId}:`, error);
|
|
159
|
-
}
|
|
160
|
-
return null;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Load all sessions from disk on startup
|
|
165
|
-
*/
|
|
166
|
-
async loadSessions() {
|
|
167
|
-
try {
|
|
168
|
-
const files = await fs.readdir(this.directory);
|
|
169
|
-
const sessionFiles = files.filter((f) => f.endsWith(".json"));
|
|
170
|
-
let loaded = 0;
|
|
171
|
-
let expired = 0;
|
|
172
|
-
for (const file of sessionFiles) {
|
|
173
|
-
const sessionId = file.replace(".json", "");
|
|
174
|
-
const session = await this.loadSession(sessionId);
|
|
175
|
-
if (session) {
|
|
176
|
-
// Check if session is expired
|
|
177
|
-
if (session.expiresAt < Date.now()) {
|
|
178
|
-
expired++;
|
|
179
|
-
// Clean up expired session file
|
|
180
|
-
await this.deleteSession(sessionId);
|
|
181
|
-
}
|
|
182
|
-
else {
|
|
183
|
-
// Restore to memory
|
|
184
|
-
this.sessionMap.set(sessionId, session);
|
|
185
|
-
loaded++;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
console.log(`[SessionPersistence] Loaded ${loaded} sessions, cleaned ${expired} expired sessions`);
|
|
190
|
-
}
|
|
191
|
-
catch (error) {
|
|
192
|
-
console.error("[SessionPersistence] Failed to load sessions:", error);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Save all active sessions (snapshot)
|
|
197
|
-
*/
|
|
198
|
-
async saveAllSessions() {
|
|
199
|
-
const sessions = Array.from(this.sessionMap.entries());
|
|
200
|
-
// Filter out expired sessions and create save promises
|
|
201
|
-
const savePromises = sessions
|
|
202
|
-
.filter(([_, session]) => session.expiresAt >= Date.now())
|
|
203
|
-
.map(async ([sessionId, session]) => {
|
|
204
|
-
return (await this.saveSession(sessionId, session)) ? 1 : 0;
|
|
205
|
-
});
|
|
206
|
-
// Execute all save operations concurrently
|
|
207
|
-
const results = await Promise.all(savePromises);
|
|
208
|
-
const saved = results.reduce((sum, result) => sum + result, 0);
|
|
209
|
-
if (process.env.NEUROLINK_DEBUG === "true") {
|
|
210
|
-
console.log(`[SessionPersistence] Snapshot: saved ${saved}/${sessions.length} sessions`);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Delete session file
|
|
215
|
-
*/
|
|
216
|
-
async deleteSession(sessionId) {
|
|
217
|
-
const filename = `${sessionId}.json`;
|
|
218
|
-
const filepath = path.join(this.directory, filename);
|
|
219
|
-
try {
|
|
220
|
-
await fs.unlink(filepath);
|
|
221
|
-
// Keep memory and disk state in sync
|
|
222
|
-
this.sessionMap.delete(sessionId);
|
|
223
|
-
}
|
|
224
|
-
catch (error) {
|
|
225
|
-
if (error.code !== "ENOENT") {
|
|
226
|
-
console.error(`[SessionPersistence] Failed to delete session ${sessionId}:`, error);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Clean up old session files
|
|
232
|
-
*/
|
|
233
|
-
async cleanupOldSessions() {
|
|
234
|
-
try {
|
|
235
|
-
const files = await fs.readdir(this.directory);
|
|
236
|
-
const now = Date.now();
|
|
237
|
-
let cleaned = 0;
|
|
238
|
-
for (const file of files) {
|
|
239
|
-
if (!file.endsWith(".json")) {
|
|
240
|
-
continue;
|
|
241
|
-
}
|
|
242
|
-
const filepath = path.join(this.directory, file);
|
|
243
|
-
const stats = await fs.stat(filepath);
|
|
244
|
-
// Remove files older than retention period
|
|
245
|
-
if (now - stats.mtimeMs > this.retentionPeriod) {
|
|
246
|
-
await fs.unlink(filepath);
|
|
247
|
-
cleaned++;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
if (cleaned > 0) {
|
|
251
|
-
console.log(`[SessionPersistence] Cleaned up ${cleaned} old session files`);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
catch (error) {
|
|
255
|
-
console.error("[SessionPersistence] Cleanup failed:", error);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* Start periodic snapshot timer
|
|
260
|
-
*/
|
|
261
|
-
startSnapshotTimer() {
|
|
262
|
-
if (this.snapshotTimer) {
|
|
263
|
-
clearInterval(this.snapshotTimer);
|
|
264
|
-
}
|
|
265
|
-
this.snapshotTimer = setInterval(async () => {
|
|
266
|
-
await this.saveAllSessions();
|
|
267
|
-
await this.cleanupOldSessions();
|
|
268
|
-
}, this.snapshotInterval);
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Stop snapshot timer
|
|
272
|
-
*/
|
|
273
|
-
stopSnapshotTimer() {
|
|
274
|
-
if (this.snapshotTimer) {
|
|
275
|
-
clearInterval(this.snapshotTimer);
|
|
276
|
-
this.snapshotTimer = null;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Shutdown persistence layer
|
|
281
|
-
*/
|
|
282
|
-
async shutdown() {
|
|
283
|
-
this.stopSnapshotTimer();
|
|
284
|
-
// Final snapshot before shutdown
|
|
285
|
-
await this.saveAllSessions();
|
|
286
|
-
console.log("[SessionPersistence] Shutdown complete");
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Default session persistence instance
|
|
291
|
-
*/
|
|
292
|
-
export let defaultSessionPersistence = null;
|
|
293
|
-
/**
|
|
294
|
-
* Initialize default session persistence
|
|
295
|
-
*/
|
|
296
|
-
export async function initializeSessionPersistence(sessionMap, options) {
|
|
297
|
-
if (!defaultSessionPersistence) {
|
|
298
|
-
defaultSessionPersistence = new SessionPersistence(sessionMap, options);
|
|
299
|
-
await defaultSessionPersistence.initialize();
|
|
300
|
-
}
|
|
301
|
-
return defaultSessionPersistence;
|
|
302
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MCP Tool Integration Layer
|
|
3
|
-
* Connects MCP tools to NeuroLink AI providers following Lighthouse patterns
|
|
4
|
-
*/
|
|
5
|
-
import type { Unknown } from "../types/common.js";
|
|
6
|
-
import type { NeuroLinkExecutionContext, ToolResult } from "./factory.js";
|
|
7
|
-
/**
|
|
8
|
-
* Tool Integration System
|
|
9
|
-
* Provides natural language tool discovery and execution
|
|
10
|
-
*/
|
|
11
|
-
export declare class MCPToolIntegration {
|
|
12
|
-
private registry;
|
|
13
|
-
private context;
|
|
14
|
-
constructor(context?: Partial<NeuroLinkExecutionContext>);
|
|
15
|
-
/**
|
|
16
|
-
* Initialize tools from all active servers
|
|
17
|
-
*/
|
|
18
|
-
private initializeTools;
|
|
19
|
-
/**
|
|
20
|
-
* Get all available tools
|
|
21
|
-
*/
|
|
22
|
-
getAvailableTools(): Promise<import("./tool-registry.js").ToolInfo[]>;
|
|
23
|
-
/**
|
|
24
|
-
* Find tools that match a query
|
|
25
|
-
*/
|
|
26
|
-
findTools(query: string): Promise<Array<{
|
|
27
|
-
name: string;
|
|
28
|
-
description: string;
|
|
29
|
-
serverId: string;
|
|
30
|
-
relevance: number;
|
|
31
|
-
}>>;
|
|
32
|
-
/**
|
|
33
|
-
* Execute a tool by name
|
|
34
|
-
*/
|
|
35
|
-
executeTool(toolName: string, params: Unknown, serverId?: string): Promise<ToolResult>;
|
|
36
|
-
/**
|
|
37
|
-
* Enhance AI prompt with tool context
|
|
38
|
-
*/
|
|
39
|
-
createToolAwarePrompt(userPrompt: string): Promise<string>;
|
|
40
|
-
/**
|
|
41
|
-
* Analyze AI response for tool usage requests
|
|
42
|
-
*/
|
|
43
|
-
analyzeForToolUsage(aiResponse: string): Array<{
|
|
44
|
-
toolName: string;
|
|
45
|
-
params: Unknown;
|
|
46
|
-
confidence: number;
|
|
47
|
-
}>;
|
|
48
|
-
/**
|
|
49
|
-
* Update context for tool execution
|
|
50
|
-
*/
|
|
51
|
-
updateContext(updates: Partial<NeuroLinkExecutionContext>): void;
|
|
52
|
-
}
|
|
53
|
-
export declare function getToolIntegration(context?: Partial<NeuroLinkExecutionContext>): MCPToolIntegration;
|
|
54
|
-
/**
|
|
55
|
-
* Initialize MCP tools for a session
|
|
56
|
-
* Following Lighthouse's initialization pattern
|
|
57
|
-
*/
|
|
58
|
-
export declare function initializeMCPTools(sessionId: string, context?: Partial<NeuroLinkExecutionContext>): Promise<MCPToolIntegration>;
|
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MCP Tool Integration Layer
|
|
3
|
-
* Connects MCP tools to NeuroLink AI providers following Lighthouse patterns
|
|
4
|
-
*/
|
|
5
|
-
import { mcpConfig } from "./config.js";
|
|
6
|
-
import { logger } from "../utils/logger.js";
|
|
7
|
-
import { MCPToolRegistry } from "./tool-registry.js";
|
|
8
|
-
/**
|
|
9
|
-
* Tool Integration System
|
|
10
|
-
* Provides natural language tool discovery and execution
|
|
11
|
-
*/
|
|
12
|
-
export class MCPToolIntegration {
|
|
13
|
-
registry;
|
|
14
|
-
context;
|
|
15
|
-
constructor(context) {
|
|
16
|
-
this.registry = new MCPToolRegistry();
|
|
17
|
-
this.context = {
|
|
18
|
-
sessionId: context?.sessionId || `session-${Date.now()}`,
|
|
19
|
-
userId: context?.userId || "anonymous",
|
|
20
|
-
aiProvider: context?.aiProvider || "unknown",
|
|
21
|
-
...context,
|
|
22
|
-
// Ensure secureFS is properly typed
|
|
23
|
-
secureFS: context?.secureFS || {
|
|
24
|
-
readFile: async () => {
|
|
25
|
-
throw new Error("secureFS not configured");
|
|
26
|
-
},
|
|
27
|
-
writeFile: async () => {
|
|
28
|
-
throw new Error("secureFS not configured");
|
|
29
|
-
},
|
|
30
|
-
readdir: async () => {
|
|
31
|
-
throw new Error("secureFS not configured");
|
|
32
|
-
},
|
|
33
|
-
stat: async () => {
|
|
34
|
-
throw new Error("secureFS not configured");
|
|
35
|
-
},
|
|
36
|
-
mkdir: async () => {
|
|
37
|
-
throw new Error("secureFS not configured");
|
|
38
|
-
},
|
|
39
|
-
exists: async () => false,
|
|
40
|
-
rmdir: async () => {
|
|
41
|
-
throw new Error("secureFS not configured");
|
|
42
|
-
},
|
|
43
|
-
unlink: async () => {
|
|
44
|
-
throw new Error("secureFS not configured");
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
// Initialize with all active servers
|
|
49
|
-
this.initializeTools();
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Initialize tools from all active servers
|
|
53
|
-
*/
|
|
54
|
-
async initializeTools() {
|
|
55
|
-
const servers = await mcpConfig.getServers();
|
|
56
|
-
for (const server of servers) {
|
|
57
|
-
await this.registry.registerServer(server.id ||
|
|
58
|
-
server.name ||
|
|
59
|
-
"unknown", server);
|
|
60
|
-
}
|
|
61
|
-
const tools = await this.registry.listTools();
|
|
62
|
-
logger.debug("[Tool Integration] Initialized with servers:", {
|
|
63
|
-
serverCount: servers.length,
|
|
64
|
-
toolCount: tools.length,
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Get all available tools
|
|
69
|
-
*/
|
|
70
|
-
getAvailableTools() {
|
|
71
|
-
return this.registry.listTools();
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Find tools that match a query
|
|
75
|
-
*/
|
|
76
|
-
async findTools(query) {
|
|
77
|
-
const allTools = await this.registry.listTools();
|
|
78
|
-
const queryLower = query.toLowerCase();
|
|
79
|
-
const results = [];
|
|
80
|
-
for (const toolInfo of allTools) {
|
|
81
|
-
let relevance = 0;
|
|
82
|
-
// Check tool name
|
|
83
|
-
if (toolInfo.name.toLowerCase().includes(queryLower)) {
|
|
84
|
-
relevance += 10;
|
|
85
|
-
}
|
|
86
|
-
// Check tool description
|
|
87
|
-
if (toolInfo.description?.toLowerCase().includes(queryLower)) {
|
|
88
|
-
relevance += 5;
|
|
89
|
-
}
|
|
90
|
-
// Check category if present (category might not exist on ToolInfo)
|
|
91
|
-
// if (toolInfo.category?.toLowerCase().includes(queryLower)) {
|
|
92
|
-
// relevance += 3;
|
|
93
|
-
// }
|
|
94
|
-
if (relevance > 0) {
|
|
95
|
-
results.push({
|
|
96
|
-
name: toolInfo.name,
|
|
97
|
-
description: toolInfo.description || "No description available",
|
|
98
|
-
serverId: toolInfo.serverId || "unknown",
|
|
99
|
-
relevance,
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
// Sort by relevance
|
|
104
|
-
return results.sort((a, b) => b.relevance - a.relevance);
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Execute a tool by name
|
|
108
|
-
*/
|
|
109
|
-
async executeTool(toolName, params, serverId) {
|
|
110
|
-
try {
|
|
111
|
-
// If serverId is provided, use qualified name
|
|
112
|
-
const qualifiedName = serverId ? `${serverId}.${toolName}` : toolName;
|
|
113
|
-
const result = await this.registry.executeTool(qualifiedName, params, this.context);
|
|
114
|
-
return result;
|
|
115
|
-
}
|
|
116
|
-
catch (error) {
|
|
117
|
-
logger.error("[Tool Integration] Tool execution failed:", error);
|
|
118
|
-
return {
|
|
119
|
-
success: false,
|
|
120
|
-
error: error instanceof Error ? error.message : String(error),
|
|
121
|
-
metadata: {
|
|
122
|
-
toolName,
|
|
123
|
-
serverId,
|
|
124
|
-
timestamp: Date.now(),
|
|
125
|
-
},
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Enhance AI prompt with tool context
|
|
131
|
-
*/
|
|
132
|
-
async createToolAwarePrompt(userPrompt) {
|
|
133
|
-
const tools = await this.getAvailableTools();
|
|
134
|
-
const toolDescriptions = tools
|
|
135
|
-
.map((t) => `- ${t.name}: ${t.description}`)
|
|
136
|
-
.join("\n");
|
|
137
|
-
return `${userPrompt}
|
|
138
|
-
|
|
139
|
-
Available tools:
|
|
140
|
-
${toolDescriptions}
|
|
141
|
-
|
|
142
|
-
You can use these tools to provide more accurate and real-time information.`;
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Analyze AI response for tool usage requests
|
|
146
|
-
*/
|
|
147
|
-
analyzeForToolUsage(aiResponse) {
|
|
148
|
-
const toolRequests = [];
|
|
149
|
-
// Simple pattern matching for tool requests
|
|
150
|
-
// This can be enhanced with more sophisticated NLP
|
|
151
|
-
const toolPattern = /<tool[^>]*>([^<]+)<\/tool>/g;
|
|
152
|
-
let match;
|
|
153
|
-
while ((match = toolPattern.exec(aiResponse)) !== null) {
|
|
154
|
-
try {
|
|
155
|
-
const toolData = JSON.parse(match[1]);
|
|
156
|
-
toolRequests.push({
|
|
157
|
-
toolName: toolData.name,
|
|
158
|
-
params: toolData.params || {},
|
|
159
|
-
confidence: 1.0,
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
catch (error) {
|
|
163
|
-
logger.warn("[Tool Integration] Failed to parse tool request:", match[1]);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return toolRequests;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Update context for tool execution
|
|
170
|
-
*/
|
|
171
|
-
updateContext(updates) {
|
|
172
|
-
this.context = {
|
|
173
|
-
...this.context,
|
|
174
|
-
...updates,
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Create a global tool integration instance
|
|
180
|
-
* This can be initialized once and reused
|
|
181
|
-
*/
|
|
182
|
-
let globalToolIntegration = null;
|
|
183
|
-
export function getToolIntegration(context) {
|
|
184
|
-
if (!globalToolIntegration) {
|
|
185
|
-
globalToolIntegration = new MCPToolIntegration(context);
|
|
186
|
-
}
|
|
187
|
-
else if (context) {
|
|
188
|
-
globalToolIntegration.updateContext(context);
|
|
189
|
-
}
|
|
190
|
-
return globalToolIntegration;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Initialize MCP tools for a session
|
|
194
|
-
* Following Lighthouse's initialization pattern
|
|
195
|
-
*/
|
|
196
|
-
export async function initializeMCPTools(sessionId, context) {
|
|
197
|
-
const integration = getToolIntegration({
|
|
198
|
-
sessionId,
|
|
199
|
-
...context,
|
|
200
|
-
});
|
|
201
|
-
logger.debug("[Tool Integration] MCP tools initialized for session:", sessionId);
|
|
202
|
-
return integration;
|
|
203
|
-
}
|