@juspay/neurolink 3.0.1 → 4.1.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 +66 -6
- package/README.md +318 -27
- package/dist/agent/direct-tools.d.ts +6 -6
- package/dist/chat/client-utils.d.ts +92 -0
- package/dist/chat/client-utils.js +298 -0
- package/dist/chat/index.d.ts +27 -0
- package/dist/chat/index.js +41 -0
- package/dist/chat/session-storage.d.ts +77 -0
- package/dist/chat/session-storage.js +233 -0
- package/dist/chat/session.d.ts +95 -0
- package/dist/chat/session.js +257 -0
- package/dist/chat/sse-handler.d.ts +49 -0
- package/dist/chat/sse-handler.js +266 -0
- package/dist/chat/types.d.ts +73 -0
- package/dist/chat/types.js +5 -0
- package/dist/chat/websocket-chat-handler.d.ts +36 -0
- package/dist/chat/websocket-chat-handler.js +262 -0
- package/dist/cli/commands/config.js +12 -12
- package/dist/cli/commands/mcp.js +3 -4
- package/dist/cli/index.d.ts +0 -7
- package/dist/cli/index.js +247 -28
- package/dist/config/configManager.d.ts +60 -0
- package/dist/config/configManager.js +300 -0
- package/dist/config/types.d.ts +136 -0
- package/dist/config/types.js +43 -0
- package/dist/core/analytics.d.ts +23 -0
- package/dist/core/analytics.js +131 -0
- package/dist/core/constants.d.ts +41 -0
- package/dist/core/constants.js +50 -0
- package/dist/core/defaults.d.ts +18 -0
- package/dist/core/defaults.js +29 -0
- package/dist/core/evaluation-config.d.ts +29 -0
- package/dist/core/evaluation-config.js +144 -0
- package/dist/core/evaluation-providers.d.ts +30 -0
- package/dist/core/evaluation-providers.js +187 -0
- package/dist/core/evaluation.d.ts +117 -0
- package/dist/core/evaluation.js +528 -0
- package/dist/core/factory.js +33 -25
- package/dist/core/types.d.ts +165 -6
- package/dist/core/types.js +3 -4
- package/dist/index.d.ts +9 -4
- package/dist/index.js +25 -4
- package/dist/lib/agent/direct-tools.d.ts +6 -6
- package/dist/lib/chat/client-utils.d.ts +92 -0
- package/dist/lib/chat/client-utils.js +298 -0
- package/dist/lib/chat/index.d.ts +27 -0
- package/dist/lib/chat/index.js +41 -0
- package/dist/lib/chat/session-storage.d.ts +77 -0
- package/dist/lib/chat/session-storage.js +233 -0
- package/dist/lib/chat/session.d.ts +95 -0
- package/dist/lib/chat/session.js +257 -0
- package/dist/lib/chat/sse-handler.d.ts +49 -0
- package/dist/lib/chat/sse-handler.js +266 -0
- package/dist/lib/chat/types.d.ts +73 -0
- package/dist/lib/chat/types.js +5 -0
- package/dist/lib/chat/websocket-chat-handler.d.ts +36 -0
- package/dist/lib/chat/websocket-chat-handler.js +262 -0
- package/dist/lib/config/configManager.d.ts +60 -0
- package/dist/lib/config/configManager.js +300 -0
- package/dist/lib/config/types.d.ts +136 -0
- package/dist/lib/config/types.js +43 -0
- package/dist/lib/core/analytics.d.ts +23 -0
- package/dist/lib/core/analytics.js +131 -0
- package/dist/lib/core/constants.d.ts +41 -0
- package/dist/lib/core/constants.js +50 -0
- package/dist/lib/core/defaults.d.ts +18 -0
- package/dist/lib/core/defaults.js +29 -0
- package/dist/lib/core/evaluation-config.d.ts +29 -0
- package/dist/lib/core/evaluation-config.js +144 -0
- package/dist/lib/core/evaluation-providers.d.ts +30 -0
- package/dist/lib/core/evaluation-providers.js +187 -0
- package/dist/lib/core/evaluation.d.ts +117 -0
- package/dist/lib/core/evaluation.js +528 -0
- package/dist/lib/core/factory.js +33 -26
- package/dist/lib/core/types.d.ts +165 -6
- package/dist/lib/core/types.js +3 -4
- package/dist/lib/index.d.ts +9 -4
- package/dist/lib/index.js +25 -4
- package/dist/lib/mcp/contracts/mcpContract.d.ts +118 -0
- package/dist/lib/mcp/contracts/mcpContract.js +5 -0
- package/dist/lib/mcp/dynamic-chain-executor.d.ts +201 -0
- package/dist/lib/mcp/dynamic-chain-executor.js +489 -0
- package/dist/lib/mcp/dynamic-orchestrator.d.ts +109 -0
- package/dist/lib/mcp/dynamic-orchestrator.js +351 -0
- package/dist/lib/mcp/error-manager.d.ts +254 -0
- package/dist/lib/mcp/error-manager.js +501 -0
- package/dist/lib/mcp/error-recovery.d.ts +158 -0
- package/dist/lib/mcp/error-recovery.js +405 -0
- package/dist/lib/mcp/function-calling.js +11 -3
- package/dist/lib/mcp/health-monitor.d.ts +256 -0
- package/dist/lib/mcp/health-monitor.js +621 -0
- package/dist/lib/mcp/logging.js +5 -0
- package/dist/lib/mcp/neurolink-mcp-client.js +2 -1
- package/dist/lib/mcp/orchestrator.d.ts +136 -5
- package/dist/lib/mcp/orchestrator.js +332 -16
- package/dist/lib/mcp/registry.d.ts +71 -16
- package/dist/lib/mcp/registry.js +104 -6
- package/dist/lib/mcp/semaphore-manager.d.ts +137 -0
- package/dist/lib/mcp/semaphore-manager.js +329 -0
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.js +5 -4
- package/dist/lib/mcp/session-manager.d.ts +186 -0
- package/dist/lib/mcp/session-manager.js +400 -0
- package/dist/lib/mcp/session-persistence.d.ts +93 -0
- package/dist/lib/mcp/session-persistence.js +298 -0
- package/dist/lib/mcp/tool-integration.js +1 -1
- package/dist/lib/mcp/tool-registry.d.ts +55 -34
- package/dist/lib/mcp/tool-registry.js +111 -97
- package/dist/lib/mcp/transport-manager.d.ts +153 -0
- package/dist/lib/mcp/transport-manager.js +330 -0
- package/dist/lib/mcp/unified-mcp.js +6 -1
- package/dist/lib/mcp/unified-registry.d.ts +54 -5
- package/dist/lib/mcp/unified-registry.js +139 -6
- package/dist/lib/neurolink.d.ts +101 -0
- package/dist/lib/neurolink.js +147 -1
- package/dist/lib/providers/agent-enhanced-provider.d.ts +11 -2
- package/dist/lib/providers/agent-enhanced-provider.js +86 -15
- package/dist/lib/providers/amazonBedrock.d.ts +9 -1
- package/dist/lib/providers/amazonBedrock.js +26 -2
- package/dist/lib/providers/analytics-helper.d.ts +53 -0
- package/dist/lib/providers/analytics-helper.js +151 -0
- package/dist/lib/providers/anthropic.d.ts +11 -1
- package/dist/lib/providers/anthropic.js +29 -4
- package/dist/lib/providers/azureOpenAI.d.ts +3 -1
- package/dist/lib/providers/azureOpenAI.js +28 -4
- package/dist/lib/providers/function-calling-provider.d.ts +9 -1
- package/dist/lib/providers/function-calling-provider.js +14 -1
- package/dist/lib/providers/googleAIStudio.d.ts +15 -1
- package/dist/lib/providers/googleAIStudio.js +32 -2
- package/dist/lib/providers/googleVertexAI.d.ts +9 -1
- package/dist/lib/providers/googleVertexAI.js +31 -2
- package/dist/lib/providers/huggingFace.d.ts +3 -1
- package/dist/lib/providers/huggingFace.js +26 -3
- package/dist/lib/providers/mcp-provider.d.ts +9 -1
- package/dist/lib/providers/mcp-provider.js +12 -0
- package/dist/lib/providers/mistralAI.d.ts +3 -1
- package/dist/lib/providers/mistralAI.js +25 -2
- package/dist/lib/providers/ollama.d.ts +3 -1
- package/dist/lib/providers/ollama.js +27 -4
- package/dist/lib/providers/openAI.d.ts +15 -1
- package/dist/lib/providers/openAI.js +32 -2
- package/dist/lib/proxy/proxy-fetch.js +8 -7
- package/dist/lib/services/streaming/streaming-manager.d.ts +29 -0
- package/dist/lib/services/streaming/streaming-manager.js +244 -0
- package/dist/lib/services/types.d.ts +155 -0
- package/dist/lib/services/types.js +2 -0
- package/dist/lib/services/websocket/websocket-server.d.ts +34 -0
- package/dist/lib/services/websocket/websocket-server.js +304 -0
- package/dist/lib/telemetry/index.d.ts +15 -0
- package/dist/lib/telemetry/index.js +22 -0
- package/dist/lib/telemetry/telemetry-service.d.ts +47 -0
- package/dist/lib/telemetry/telemetry-service.js +259 -0
- package/dist/lib/utils/streaming-utils.d.ts +67 -0
- package/dist/lib/utils/streaming-utils.js +201 -0
- package/dist/mcp/contracts/mcpContract.d.ts +118 -0
- package/dist/mcp/contracts/mcpContract.js +5 -0
- package/dist/mcp/dynamic-chain-executor.d.ts +201 -0
- package/dist/mcp/dynamic-chain-executor.js +489 -0
- package/dist/mcp/dynamic-orchestrator.d.ts +109 -0
- package/dist/mcp/dynamic-orchestrator.js +351 -0
- package/dist/mcp/error-manager.d.ts +254 -0
- package/dist/mcp/error-manager.js +501 -0
- package/dist/mcp/error-recovery.d.ts +158 -0
- package/dist/mcp/error-recovery.js +405 -0
- package/dist/mcp/function-calling.js +11 -3
- package/dist/mcp/health-monitor.d.ts +256 -0
- package/dist/mcp/health-monitor.js +621 -0
- package/dist/mcp/logging.js +5 -0
- package/dist/mcp/neurolink-mcp-client.js +2 -1
- package/dist/mcp/orchestrator.d.ts +136 -5
- package/dist/mcp/orchestrator.js +332 -16
- package/dist/mcp/plugins/core/neurolink-mcp.json +15 -15
- package/dist/mcp/registry.d.ts +71 -16
- package/dist/mcp/registry.js +104 -6
- package/dist/mcp/semaphore-manager.d.ts +137 -0
- package/dist/mcp/semaphore-manager.js +329 -0
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.js +5 -4
- package/dist/mcp/session-manager.d.ts +186 -0
- package/dist/mcp/session-manager.js +400 -0
- package/dist/mcp/session-persistence.d.ts +93 -0
- package/dist/mcp/session-persistence.js +299 -0
- package/dist/mcp/tool-integration.js +1 -1
- package/dist/mcp/tool-registry.d.ts +55 -34
- package/dist/mcp/tool-registry.js +111 -97
- package/dist/mcp/transport-manager.d.ts +153 -0
- package/dist/mcp/transport-manager.js +331 -0
- package/dist/mcp/unified-mcp.js +6 -1
- package/dist/mcp/unified-registry.d.ts +54 -5
- package/dist/mcp/unified-registry.js +139 -6
- package/dist/neurolink.d.ts +101 -0
- package/dist/neurolink.js +147 -1
- package/dist/providers/agent-enhanced-provider.d.ts +11 -2
- package/dist/providers/agent-enhanced-provider.js +86 -15
- package/dist/providers/amazonBedrock.d.ts +9 -1
- package/dist/providers/amazonBedrock.js +26 -2
- package/dist/providers/analytics-helper.d.ts +53 -0
- package/dist/providers/analytics-helper.js +151 -0
- package/dist/providers/anthropic.d.ts +11 -1
- package/dist/providers/anthropic.js +29 -4
- package/dist/providers/azureOpenAI.d.ts +3 -1
- package/dist/providers/azureOpenAI.js +29 -4
- package/dist/providers/function-calling-provider.d.ts +9 -1
- package/dist/providers/function-calling-provider.js +14 -1
- package/dist/providers/googleAIStudio.d.ts +15 -1
- package/dist/providers/googleAIStudio.js +32 -2
- package/dist/providers/googleVertexAI.d.ts +9 -1
- package/dist/providers/googleVertexAI.js +31 -2
- package/dist/providers/huggingFace.d.ts +3 -1
- package/dist/providers/huggingFace.js +26 -3
- package/dist/providers/mcp-provider.d.ts +9 -1
- package/dist/providers/mcp-provider.js +12 -0
- package/dist/providers/mistralAI.d.ts +3 -1
- package/dist/providers/mistralAI.js +25 -2
- package/dist/providers/ollama.d.ts +3 -1
- package/dist/providers/ollama.js +27 -4
- package/dist/providers/openAI.d.ts +15 -1
- package/dist/providers/openAI.js +33 -2
- package/dist/proxy/proxy-fetch.js +8 -7
- package/dist/services/streaming/streaming-manager.d.ts +29 -0
- package/dist/services/streaming/streaming-manager.js +244 -0
- package/dist/services/types.d.ts +155 -0
- package/dist/services/types.js +2 -0
- package/dist/services/websocket/websocket-server.d.ts +34 -0
- package/dist/services/websocket/websocket-server.js +304 -0
- package/dist/telemetry/index.d.ts +15 -0
- package/dist/telemetry/index.js +22 -0
- package/dist/telemetry/telemetry-service.d.ts +47 -0
- package/dist/telemetry/telemetry-service.js +261 -0
- package/dist/utils/streaming-utils.d.ts +67 -0
- package/dist/utils/streaming-utils.js +201 -0
- package/package.json +245 -228
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NeuroLink Config Manager with Backup/Restore System
|
|
3
|
+
* Industry standard configuration management with safety mechanisms
|
|
4
|
+
*/
|
|
5
|
+
import { promises as fs } from "fs";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import crypto from "crypto";
|
|
8
|
+
const { readFile, writeFile, readdir, mkdir, unlink, access } = fs;
|
|
9
|
+
/**
|
|
10
|
+
* Enhanced Config Manager with automatic backup/restore capabilities
|
|
11
|
+
*/
|
|
12
|
+
export class NeuroLinkConfigManager {
|
|
13
|
+
configPath = ".neurolink.config";
|
|
14
|
+
backupDir = ".neurolink.backups";
|
|
15
|
+
config = null;
|
|
16
|
+
configCache = new Map();
|
|
17
|
+
/**
|
|
18
|
+
* Load configuration with caching
|
|
19
|
+
*/
|
|
20
|
+
async loadConfig() {
|
|
21
|
+
if (!this.config) {
|
|
22
|
+
this.config = await this.readConfigFile();
|
|
23
|
+
}
|
|
24
|
+
return this.config;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Update configuration with automatic backup
|
|
28
|
+
*/
|
|
29
|
+
async updateConfig(updates, options = {}) {
|
|
30
|
+
const { createBackup = true, validate = true, merge = true, reason = "update", silent = false, } = options;
|
|
31
|
+
// ALWAYS create backup before updating (unless explicitly disabled)
|
|
32
|
+
if (createBackup) {
|
|
33
|
+
await this.createBackup(reason);
|
|
34
|
+
if (!silent) {
|
|
35
|
+
console.log("💾 Backup created before config update");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const existing = await this.loadConfig();
|
|
39
|
+
// Merge or replace based on options
|
|
40
|
+
this.config = merge
|
|
41
|
+
? { ...existing, ...updates, lastUpdated: Date.now() }
|
|
42
|
+
: { ...updates, lastUpdated: Date.now() };
|
|
43
|
+
// Validate config if requested
|
|
44
|
+
if (validate) {
|
|
45
|
+
const validation = await this.validateConfig(this.config);
|
|
46
|
+
if (!validation.valid) {
|
|
47
|
+
throw new Error(`Config validation failed: ${validation.errors.join(", ")}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
await this.persistConfig(this.config);
|
|
52
|
+
if (!silent) {
|
|
53
|
+
console.log("✅ Configuration updated successfully");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
// Auto-restore on failure
|
|
58
|
+
if (createBackup) {
|
|
59
|
+
await this.restoreLatestBackup();
|
|
60
|
+
if (!silent) {
|
|
61
|
+
console.log("🔄 Auto-restored from backup due to error");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
throw new Error(`Config update failed, restored from backup: ${error.message}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Create a backup with metadata
|
|
69
|
+
*/
|
|
70
|
+
async createBackup(reason = "manual") {
|
|
71
|
+
await this.ensureBackupDirectory();
|
|
72
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
73
|
+
const backupFilename = `neurolink-config-${timestamp}.js`;
|
|
74
|
+
const backupPath = path.join(this.backupDir, backupFilename);
|
|
75
|
+
const currentConfig = await this.loadConfig();
|
|
76
|
+
const configHash = this.generateConfigHash(currentConfig);
|
|
77
|
+
const backupMetadata = {
|
|
78
|
+
reason,
|
|
79
|
+
timestamp: Date.now(),
|
|
80
|
+
version: currentConfig.configVersion || "unknown",
|
|
81
|
+
originalPath: this.configPath,
|
|
82
|
+
hash: configHash,
|
|
83
|
+
size: JSON.stringify(currentConfig).length,
|
|
84
|
+
createdBy: "NeuroLinkConfigManager",
|
|
85
|
+
};
|
|
86
|
+
const backupContent = `// NeuroLink Config Backup - ${reason}
|
|
87
|
+
// Created: ${new Date().toISOString()}
|
|
88
|
+
// Reason: ${reason}
|
|
89
|
+
// Hash: ${configHash}
|
|
90
|
+
export const metadata = ${JSON.stringify(backupMetadata, null, 2)};
|
|
91
|
+
export default ${JSON.stringify(currentConfig, null, 2)};`;
|
|
92
|
+
await writeFile(backupPath, backupContent, "utf-8");
|
|
93
|
+
return backupPath;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* List all available backups
|
|
97
|
+
*/
|
|
98
|
+
async listBackups() {
|
|
99
|
+
await this.ensureBackupDirectory();
|
|
100
|
+
try {
|
|
101
|
+
const files = await readdir(this.backupDir);
|
|
102
|
+
const backupFiles = files.filter((f) => f.startsWith("neurolink-config-") && f.endsWith(".js"));
|
|
103
|
+
const backups = [];
|
|
104
|
+
for (const file of backupFiles) {
|
|
105
|
+
try {
|
|
106
|
+
const filePath = path.join(this.backupDir, file);
|
|
107
|
+
const content = await readFile(filePath, "utf-8");
|
|
108
|
+
const metadata = this.extractMetadataFromBackup(content);
|
|
109
|
+
const config = this.extractConfigFromBackup(content);
|
|
110
|
+
backups.push({
|
|
111
|
+
filename: file,
|
|
112
|
+
path: filePath,
|
|
113
|
+
metadata,
|
|
114
|
+
config,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
console.warn(`Failed to read backup ${file}:`, error.message);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return backups.sort((a, b) => b.metadata.timestamp - a.metadata.timestamp);
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
console.warn("Failed to list backups:", error.message);
|
|
125
|
+
return [];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Restore from specific backup
|
|
130
|
+
*/
|
|
131
|
+
async restoreFromBackup(backupFilename) {
|
|
132
|
+
const backupPath = path.join(this.backupDir, backupFilename);
|
|
133
|
+
// Create backup of current config before restore
|
|
134
|
+
await this.createBackup("pre-restore");
|
|
135
|
+
try {
|
|
136
|
+
const content = await readFile(backupPath, "utf-8");
|
|
137
|
+
const restoredConfig = this.extractConfigFromBackup(content);
|
|
138
|
+
// Validate restored config
|
|
139
|
+
const validation = await this.validateConfig(restoredConfig);
|
|
140
|
+
if (!validation.valid) {
|
|
141
|
+
throw new Error(`Backup config is invalid: ${validation.errors.join(", ")}`);
|
|
142
|
+
}
|
|
143
|
+
this.config = restoredConfig;
|
|
144
|
+
await this.persistConfig(this.config);
|
|
145
|
+
console.log(`✅ Config restored from backup: ${backupFilename}`);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
throw new Error(`Failed to restore from backup ${backupFilename}: ${error.message}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Restore from latest backup
|
|
153
|
+
*/
|
|
154
|
+
async restoreLatestBackup() {
|
|
155
|
+
const backups = await this.listBackups();
|
|
156
|
+
if (backups.length === 0) {
|
|
157
|
+
throw new Error("No backups available for restore");
|
|
158
|
+
}
|
|
159
|
+
await this.restoreFromBackup(backups[0].filename);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Clean up old backups
|
|
163
|
+
*/
|
|
164
|
+
async cleanupOldBackups(keepCount = 10) {
|
|
165
|
+
const backups = await this.listBackups();
|
|
166
|
+
const toDelete = backups.slice(keepCount);
|
|
167
|
+
for (const backup of toDelete) {
|
|
168
|
+
try {
|
|
169
|
+
await unlink(backup.path);
|
|
170
|
+
console.log(`🗑️ Deleted old backup: ${backup.filename}`);
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
console.warn(`Failed to delete backup ${backup.filename}:`, error.message);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Update provider status
|
|
179
|
+
*/
|
|
180
|
+
async updateProviderStatus(providerId, status) {
|
|
181
|
+
const config = await this.loadConfig();
|
|
182
|
+
if (!config.providers) {
|
|
183
|
+
config.providers = {};
|
|
184
|
+
}
|
|
185
|
+
config.providers[providerId] = {
|
|
186
|
+
...config.providers[providerId],
|
|
187
|
+
...status,
|
|
188
|
+
lastCheck: Date.now(),
|
|
189
|
+
};
|
|
190
|
+
await this.updateConfig({ providers: config.providers }, { reason: `provider-${providerId}-update` });
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Validate configuration
|
|
194
|
+
*/
|
|
195
|
+
async validateConfig(config) {
|
|
196
|
+
const result = {
|
|
197
|
+
valid: true,
|
|
198
|
+
errors: [],
|
|
199
|
+
warnings: [],
|
|
200
|
+
suggestions: [],
|
|
201
|
+
};
|
|
202
|
+
// Basic validation
|
|
203
|
+
if (!config || typeof config !== "object") {
|
|
204
|
+
result.errors.push("Config must be a valid object");
|
|
205
|
+
result.valid = false;
|
|
206
|
+
return result;
|
|
207
|
+
}
|
|
208
|
+
// Version validation
|
|
209
|
+
if (config.configVersion && typeof config.configVersion !== "string") {
|
|
210
|
+
result.errors.push("configVersion must be a string");
|
|
211
|
+
result.valid = false;
|
|
212
|
+
}
|
|
213
|
+
// Provider validation
|
|
214
|
+
if (config.providers) {
|
|
215
|
+
if (typeof config.providers !== "object") {
|
|
216
|
+
result.errors.push("providers must be an object");
|
|
217
|
+
result.valid = false;
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
// Check for default provider
|
|
221
|
+
if (!config.providers.defaultProvider) {
|
|
222
|
+
result.warnings.push("No default provider specified");
|
|
223
|
+
result.suggestions.push('Consider setting providers.defaultProvider to "googleAi"');
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
// Performance validation
|
|
228
|
+
if (config.performance?.cache?.ttlMs) {
|
|
229
|
+
if (config.performance.cache.ttlMs < 1000) {
|
|
230
|
+
result.warnings.push("Cache TTL is very low (< 1 second)");
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return result;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Generate default configuration
|
|
237
|
+
*/
|
|
238
|
+
async generateDefaultConfig() {
|
|
239
|
+
const { DEFAULT_CONFIG } = await import("./types.js");
|
|
240
|
+
return {
|
|
241
|
+
...DEFAULT_CONFIG,
|
|
242
|
+
lastUpdated: Date.now(),
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
// Private helper methods
|
|
246
|
+
async ensureBackupDirectory() {
|
|
247
|
+
try {
|
|
248
|
+
await mkdir(this.backupDir, { recursive: true });
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
console.warn("Failed to create backup directory:", error.message);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
async readConfigFile() {
|
|
255
|
+
try {
|
|
256
|
+
// Check if config file exists
|
|
257
|
+
await access(this.configPath);
|
|
258
|
+
const content = await readFile(this.configPath, "utf-8");
|
|
259
|
+
// Parse as JavaScript module
|
|
260
|
+
const configMatch = content.match(/export default ([\s\S]+);?$/);
|
|
261
|
+
if (configMatch) {
|
|
262
|
+
const configJson = configMatch[1].trim();
|
|
263
|
+
return JSON.parse(configJson);
|
|
264
|
+
}
|
|
265
|
+
throw new Error("Invalid config file format");
|
|
266
|
+
}
|
|
267
|
+
catch (error) {
|
|
268
|
+
console.log("Config file not found, generating default...");
|
|
269
|
+
return await this.generateDefaultConfig();
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
async persistConfig(config) {
|
|
273
|
+
const configContent = `export default ${JSON.stringify(config, null, 2)};`;
|
|
274
|
+
await writeFile(this.configPath, configContent, "utf-8");
|
|
275
|
+
// Clear cache after persisting
|
|
276
|
+
this.config = config;
|
|
277
|
+
}
|
|
278
|
+
generateConfigHash(config) {
|
|
279
|
+
const configString = JSON.stringify(config, Object.keys(config).sort());
|
|
280
|
+
return crypto
|
|
281
|
+
.createHash("sha256")
|
|
282
|
+
.update(configString)
|
|
283
|
+
.digest("hex")
|
|
284
|
+
.substring(0, 8);
|
|
285
|
+
}
|
|
286
|
+
extractMetadataFromBackup(content) {
|
|
287
|
+
const metadataMatch = content.match(/export const metadata = ([\s\S]+?);/);
|
|
288
|
+
if (metadataMatch) {
|
|
289
|
+
return JSON.parse(metadataMatch[1]);
|
|
290
|
+
}
|
|
291
|
+
throw new Error("No metadata found in backup file");
|
|
292
|
+
}
|
|
293
|
+
extractConfigFromBackup(content) {
|
|
294
|
+
const configMatch = content.match(/export default ([\s\S]+);$/);
|
|
295
|
+
if (configMatch) {
|
|
296
|
+
return JSON.parse(configMatch[1]);
|
|
297
|
+
}
|
|
298
|
+
throw new Error("No config found in backup file");
|
|
299
|
+
}
|
|
300
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NeuroLink Configuration Types
|
|
3
|
+
* Industry standard camelCase interfaces for config management
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Main NeuroLink configuration interface
|
|
7
|
+
*/
|
|
8
|
+
export interface NeuroLinkConfig {
|
|
9
|
+
providers?: Record<string, ProviderConfig>;
|
|
10
|
+
performance?: PerformanceConfig;
|
|
11
|
+
analytics?: AnalyticsConfig;
|
|
12
|
+
lastUpdated?: number;
|
|
13
|
+
configVersion?: string;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Provider-specific configuration
|
|
18
|
+
*/
|
|
19
|
+
export interface ProviderConfig {
|
|
20
|
+
model?: string;
|
|
21
|
+
available?: boolean;
|
|
22
|
+
lastCheck?: number;
|
|
23
|
+
reason?: string;
|
|
24
|
+
apiKey?: string;
|
|
25
|
+
endpoint?: string;
|
|
26
|
+
maxTokens?: number;
|
|
27
|
+
temperature?: number;
|
|
28
|
+
timeout?: number;
|
|
29
|
+
costPerToken?: number;
|
|
30
|
+
features?: string[];
|
|
31
|
+
[key: string]: unknown;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Performance and caching configuration
|
|
35
|
+
*/
|
|
36
|
+
export interface PerformanceConfig {
|
|
37
|
+
cache?: CacheConfig;
|
|
38
|
+
fallback?: FallbackConfig;
|
|
39
|
+
timeoutMs?: number;
|
|
40
|
+
maxConcurrency?: number;
|
|
41
|
+
retryConfig?: RetryConfig;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Cache configuration
|
|
45
|
+
*/
|
|
46
|
+
export interface CacheConfig {
|
|
47
|
+
enabled?: boolean;
|
|
48
|
+
ttlMs?: number;
|
|
49
|
+
strategy?: "memory" | "writeThrough" | "cacheAside";
|
|
50
|
+
maxSize?: number;
|
|
51
|
+
persistToDisk?: boolean;
|
|
52
|
+
diskPath?: string;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Fallback configuration
|
|
56
|
+
*/
|
|
57
|
+
export interface FallbackConfig {
|
|
58
|
+
enabled?: boolean;
|
|
59
|
+
maxAttempts?: number;
|
|
60
|
+
delayMs?: number;
|
|
61
|
+
circuitBreaker?: boolean;
|
|
62
|
+
commonResponses?: Record<string, string>;
|
|
63
|
+
localFallbackPath?: string;
|
|
64
|
+
degradedMode?: boolean;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Retry configuration
|
|
68
|
+
*/
|
|
69
|
+
export interface RetryConfig {
|
|
70
|
+
enabled?: boolean;
|
|
71
|
+
maxAttempts?: number;
|
|
72
|
+
baseDelayMs?: number;
|
|
73
|
+
maxDelayMs?: number;
|
|
74
|
+
exponentialBackoff?: boolean;
|
|
75
|
+
retryConditions?: string[];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Analytics configuration
|
|
79
|
+
*/
|
|
80
|
+
export interface AnalyticsConfig {
|
|
81
|
+
enabled?: boolean;
|
|
82
|
+
trackTokens?: boolean;
|
|
83
|
+
trackCosts?: boolean;
|
|
84
|
+
trackPerformance?: boolean;
|
|
85
|
+
trackErrors?: boolean;
|
|
86
|
+
exportFormat?: "json" | "csv" | "prometheus";
|
|
87
|
+
exportPath?: string;
|
|
88
|
+
retention?: {
|
|
89
|
+
days?: number;
|
|
90
|
+
maxEntries?: number;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Backup metadata information
|
|
95
|
+
*/
|
|
96
|
+
export interface BackupInfo {
|
|
97
|
+
filename: string;
|
|
98
|
+
path: string;
|
|
99
|
+
metadata: BackupMetadata;
|
|
100
|
+
config: NeuroLinkConfig;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Backup metadata
|
|
104
|
+
*/
|
|
105
|
+
export interface BackupMetadata {
|
|
106
|
+
reason: string;
|
|
107
|
+
timestamp: number;
|
|
108
|
+
version: string;
|
|
109
|
+
originalPath: string;
|
|
110
|
+
hash?: string;
|
|
111
|
+
size?: number;
|
|
112
|
+
createdBy?: string;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Configuration validation result
|
|
116
|
+
*/
|
|
117
|
+
export interface ConfigValidationResult {
|
|
118
|
+
valid: boolean;
|
|
119
|
+
errors: string[];
|
|
120
|
+
warnings: string[];
|
|
121
|
+
suggestions: string[];
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Configuration update options
|
|
125
|
+
*/
|
|
126
|
+
export interface ConfigUpdateOptions {
|
|
127
|
+
createBackup?: boolean;
|
|
128
|
+
validate?: boolean;
|
|
129
|
+
merge?: boolean;
|
|
130
|
+
reason?: string;
|
|
131
|
+
silent?: boolean;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Default configuration values
|
|
135
|
+
*/
|
|
136
|
+
export declare const DEFAULT_CONFIG: NeuroLinkConfig;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NeuroLink Configuration Types
|
|
3
|
+
* Industry standard camelCase interfaces for config management
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Default configuration values
|
|
7
|
+
*/
|
|
8
|
+
export const DEFAULT_CONFIG = {
|
|
9
|
+
providers: {
|
|
10
|
+
googleAi: {
|
|
11
|
+
model: "gemini-2.5-pro",
|
|
12
|
+
available: true,
|
|
13
|
+
features: ["streaming", "functionCalling"],
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
performance: {
|
|
17
|
+
cache: {
|
|
18
|
+
enabled: true,
|
|
19
|
+
ttlMs: 300000, // 5 minutes
|
|
20
|
+
strategy: "memory",
|
|
21
|
+
maxSize: 1000,
|
|
22
|
+
},
|
|
23
|
+
fallback: {
|
|
24
|
+
enabled: true,
|
|
25
|
+
maxAttempts: 3,
|
|
26
|
+
delayMs: 1000,
|
|
27
|
+
circuitBreaker: true,
|
|
28
|
+
},
|
|
29
|
+
timeoutMs: 30000, // 30 seconds
|
|
30
|
+
maxConcurrency: 5,
|
|
31
|
+
},
|
|
32
|
+
analytics: {
|
|
33
|
+
enabled: true,
|
|
34
|
+
trackTokens: true,
|
|
35
|
+
trackCosts: true,
|
|
36
|
+
trackPerformance: true,
|
|
37
|
+
retention: {
|
|
38
|
+
days: 30,
|
|
39
|
+
maxEntries: 10000,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
configVersion: "3.0.1",
|
|
43
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NeuroLink Analytics System
|
|
3
|
+
*
|
|
4
|
+
* Provides lightweight analytics tracking for AI provider usage,
|
|
5
|
+
* including tokens, costs, performance metrics, and custom context.
|
|
6
|
+
*/
|
|
7
|
+
export interface AnalyticsData {
|
|
8
|
+
provider: string;
|
|
9
|
+
model: string;
|
|
10
|
+
tokens: {
|
|
11
|
+
input: number;
|
|
12
|
+
output: number;
|
|
13
|
+
total: number;
|
|
14
|
+
};
|
|
15
|
+
cost?: number;
|
|
16
|
+
responseTime: number;
|
|
17
|
+
context?: Record<string, any>;
|
|
18
|
+
timestamp: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create analytics data structure from AI response
|
|
22
|
+
*/
|
|
23
|
+
export declare function createAnalytics(provider: string, model: string, result: any, responseTime: number, context?: Record<string, any>): AnalyticsData;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NeuroLink Analytics System
|
|
3
|
+
*
|
|
4
|
+
* Provides lightweight analytics tracking for AI provider usage,
|
|
5
|
+
* including tokens, costs, performance metrics, and custom context.
|
|
6
|
+
*/
|
|
7
|
+
import { logger } from "../utils/logger.js";
|
|
8
|
+
/**
|
|
9
|
+
* Create analytics data structure from AI response
|
|
10
|
+
*/
|
|
11
|
+
export function createAnalytics(provider, model, result, responseTime, context) {
|
|
12
|
+
const functionTag = "createAnalytics";
|
|
13
|
+
try {
|
|
14
|
+
// Extract token usage from different result formats
|
|
15
|
+
const tokens = extractTokenUsage(result);
|
|
16
|
+
// Estimate cost based on provider and tokens
|
|
17
|
+
const cost = estimateCost(provider, model, tokens);
|
|
18
|
+
const analytics = {
|
|
19
|
+
provider,
|
|
20
|
+
model,
|
|
21
|
+
tokens,
|
|
22
|
+
cost,
|
|
23
|
+
responseTime,
|
|
24
|
+
context,
|
|
25
|
+
timestamp: new Date().toISOString(),
|
|
26
|
+
};
|
|
27
|
+
logger.debug(`[${functionTag}] Analytics created`, {
|
|
28
|
+
provider,
|
|
29
|
+
model,
|
|
30
|
+
tokens: tokens.total,
|
|
31
|
+
responseTime,
|
|
32
|
+
cost,
|
|
33
|
+
});
|
|
34
|
+
return analytics;
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
logger.error(`[${functionTag}] Failed to create analytics`, { error });
|
|
38
|
+
// Return minimal analytics on error
|
|
39
|
+
return {
|
|
40
|
+
provider,
|
|
41
|
+
model,
|
|
42
|
+
tokens: { input: 0, output: 0, total: 0 },
|
|
43
|
+
responseTime,
|
|
44
|
+
context,
|
|
45
|
+
timestamp: new Date().toISOString(),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Extract token usage from various AI result formats
|
|
51
|
+
*/
|
|
52
|
+
function extractTokenUsage(result) {
|
|
53
|
+
// Handle different response formats
|
|
54
|
+
if (result.usage) {
|
|
55
|
+
const usage = result.usage;
|
|
56
|
+
// Standard format
|
|
57
|
+
if (usage.promptTokens !== undefined &&
|
|
58
|
+
usage.completionTokens !== undefined) {
|
|
59
|
+
return {
|
|
60
|
+
input: usage.promptTokens || 0,
|
|
61
|
+
output: usage.completionTokens || 0,
|
|
62
|
+
total: usage.totalTokens || usage.promptTokens + usage.completionTokens,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
// Alternative formats
|
|
66
|
+
if (usage.input_tokens !== undefined && usage.output_tokens !== undefined) {
|
|
67
|
+
return {
|
|
68
|
+
input: usage.input_tokens || 0,
|
|
69
|
+
output: usage.output_tokens || 0,
|
|
70
|
+
total: usage.total_tokens || usage.input_tokens + usage.output_tokens,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
// Generic tokens field
|
|
74
|
+
if (typeof usage.tokens === "number") {
|
|
75
|
+
return {
|
|
76
|
+
input: 0,
|
|
77
|
+
output: 0,
|
|
78
|
+
total: usage.tokens,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Fallback: estimate from text length
|
|
83
|
+
const textLength = result.text?.length || result.content?.length || 0;
|
|
84
|
+
const estimatedTokens = Math.ceil(textLength / 4); // ~4 chars per token
|
|
85
|
+
return {
|
|
86
|
+
input: 0,
|
|
87
|
+
output: estimatedTokens,
|
|
88
|
+
total: estimatedTokens,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Estimate cost based on provider, model, and token usage
|
|
93
|
+
*/
|
|
94
|
+
function estimateCost(provider, model, tokens) {
|
|
95
|
+
try {
|
|
96
|
+
// Cost per 1K tokens (USD) - approximate rates as of 2024
|
|
97
|
+
const costMap = {
|
|
98
|
+
openai: {
|
|
99
|
+
"gpt-4": { input: 0.03, output: 0.06 },
|
|
100
|
+
"gpt-4-turbo": { input: 0.01, output: 0.03 },
|
|
101
|
+
"gpt-3.5-turbo": { input: 0.0015, output: 0.002 },
|
|
102
|
+
},
|
|
103
|
+
anthropic: {
|
|
104
|
+
"claude-3-opus": { input: 0.015, output: 0.075 },
|
|
105
|
+
"claude-3-sonnet": { input: 0.003, output: 0.015 },
|
|
106
|
+
"claude-3-haiku": { input: 0.00025, output: 0.00125 },
|
|
107
|
+
},
|
|
108
|
+
"google-ai": {
|
|
109
|
+
"gemini-pro": { input: 0.00035, output: 0.00105 },
|
|
110
|
+
"gemini-2.5-flash": { input: 0.000075, output: 0.0003 },
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
const providerCosts = costMap[provider.toLowerCase()];
|
|
114
|
+
if (!providerCosts) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
// Find best matching model
|
|
118
|
+
const modelKey = Object.keys(providerCosts).find((key) => model.toLowerCase().includes(key) || key.includes(model.toLowerCase()));
|
|
119
|
+
if (!modelKey) {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
const rates = providerCosts[modelKey];
|
|
123
|
+
const inputCost = (tokens.input / 1000) * rates.input;
|
|
124
|
+
const outputCost = (tokens.output / 1000) * rates.output;
|
|
125
|
+
return Math.round((inputCost + outputCost) * 100000) / 100000; // Round to 5 decimal places
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
logger.debug("Cost estimation failed", { provider, model, error });
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Central configuration constants for NeuroLink
|
|
3
|
+
* Single source of truth for all default values
|
|
4
|
+
*/
|
|
5
|
+
export declare const DEFAULT_MAX_TOKENS = 10000;
|
|
6
|
+
export declare const DEFAULT_TEMPERATURE = 0.7;
|
|
7
|
+
export declare const DEFAULT_TIMEOUT = 30000;
|
|
8
|
+
export declare const DEFAULT_EVALUATION_MAX_TOKENS = 500;
|
|
9
|
+
export declare const DEFAULT_ANALYSIS_MAX_TOKENS = 800;
|
|
10
|
+
export declare const DEFAULT_DOCUMENTATION_MAX_TOKENS = 12000;
|
|
11
|
+
export declare const PROVIDER_CONFIG: {
|
|
12
|
+
evaluation: {
|
|
13
|
+
maxTokens: number;
|
|
14
|
+
model: string;
|
|
15
|
+
temperature: number;
|
|
16
|
+
};
|
|
17
|
+
analysis: {
|
|
18
|
+
maxTokens: number;
|
|
19
|
+
temperature: number;
|
|
20
|
+
};
|
|
21
|
+
documentation: {
|
|
22
|
+
maxTokens: number;
|
|
23
|
+
temperature: number;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export declare const CLI_LIMITS: {
|
|
27
|
+
maxTokens: {
|
|
28
|
+
min: number;
|
|
29
|
+
max: number;
|
|
30
|
+
default: number;
|
|
31
|
+
};
|
|
32
|
+
temperature: {
|
|
33
|
+
min: number;
|
|
34
|
+
max: number;
|
|
35
|
+
default: number;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
export declare const ENV_DEFAULTS: {
|
|
39
|
+
maxTokens: number;
|
|
40
|
+
temperature: number;
|
|
41
|
+
};
|