@juspay/neurolink 7.33.2 → 7.33.4
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 +8 -0
- package/dist/cli/commands/config.d.ts +3 -4
- package/dist/cli/commands/config.js +2 -3
- package/dist/constants/index.d.ts +192 -0
- package/dist/constants/index.js +195 -0
- package/dist/constants/performance.d.ts +366 -0
- package/dist/constants/performance.js +389 -0
- package/dist/constants/retry.d.ts +224 -0
- package/dist/constants/retry.js +266 -0
- package/dist/constants/timeouts.d.ts +225 -0
- package/dist/constants/timeouts.js +182 -0
- package/dist/constants/tokens.d.ts +234 -0
- package/dist/constants/tokens.js +314 -0
- package/dist/core/baseProvider.js +26 -1
- package/dist/core/constants.d.ts +12 -3
- package/dist/core/constants.js +22 -6
- package/dist/core/factory.js +19 -0
- package/dist/core/types.d.ts +268 -0
- package/dist/core/types.js +153 -0
- package/dist/factories/providerRegistry.js +2 -0
- package/dist/lib/constants/index.d.ts +192 -0
- package/dist/lib/constants/index.js +195 -0
- package/dist/lib/constants/performance.d.ts +366 -0
- package/dist/lib/constants/performance.js +389 -0
- package/dist/lib/constants/retry.d.ts +224 -0
- package/dist/lib/constants/retry.js +266 -0
- package/dist/lib/constants/timeouts.d.ts +225 -0
- package/dist/lib/constants/timeouts.js +182 -0
- package/dist/lib/constants/tokens.d.ts +234 -0
- package/dist/lib/constants/tokens.js +314 -0
- package/dist/lib/core/baseProvider.js +26 -1
- package/dist/lib/core/constants.d.ts +12 -3
- package/dist/lib/core/constants.js +22 -6
- package/dist/lib/core/factory.js +19 -0
- package/dist/lib/core/types.d.ts +268 -0
- package/dist/lib/core/types.js +153 -0
- package/dist/lib/factories/providerRegistry.js +2 -0
- package/dist/lib/mcp/servers/aiProviders/aiWorkflowTools.js +2 -2
- package/dist/lib/models/modelRegistry.d.ts +1 -1
- package/dist/lib/models/modelRegistry.js +63 -37
- package/dist/lib/neurolink.js +35 -34
- package/dist/lib/providers/amazonBedrock.js +2 -2
- package/dist/lib/providers/anthropic.js +3 -12
- package/dist/lib/providers/anthropicBaseProvider.js +1 -2
- package/dist/lib/providers/azureOpenai.d.ts +1 -1
- package/dist/lib/providers/azureOpenai.js +51 -9
- package/dist/lib/providers/googleAiStudio.js +3 -3
- package/dist/lib/providers/googleVertex.js +2 -2
- package/dist/lib/providers/huggingFace.js +1 -2
- package/dist/lib/providers/litellm.js +1 -2
- package/dist/lib/providers/mistral.js +2 -2
- package/dist/lib/providers/ollama.js +7 -8
- package/dist/lib/providers/openAI.js +2 -2
- package/dist/lib/providers/openaiCompatible.js +5 -2
- package/dist/lib/providers/sagemaker/language-model.d.ts +5 -0
- package/dist/lib/providers/sagemaker/language-model.js +9 -1
- package/dist/lib/utils/providerConfig.d.ts +25 -0
- package/dist/lib/utils/providerConfig.js +24 -3
- package/dist/lib/utils/providerHealth.d.ts +1 -1
- package/dist/lib/utils/providerHealth.js +47 -36
- package/dist/lib/utils/providerSetupMessages.js +7 -6
- package/dist/lib/utils/providerUtils.js +16 -24
- package/dist/lib/utils/tokenLimits.d.ts +2 -2
- package/dist/lib/utils/tokenLimits.js +10 -3
- package/dist/mcp/servers/aiProviders/aiWorkflowTools.js +2 -2
- package/dist/models/modelRegistry.d.ts +1 -1
- package/dist/models/modelRegistry.js +63 -37
- package/dist/neurolink.js +35 -34
- package/dist/providers/amazonBedrock.js +2 -2
- package/dist/providers/anthropic.js +3 -12
- package/dist/providers/anthropicBaseProvider.js +1 -2
- package/dist/providers/azureOpenai.d.ts +1 -1
- package/dist/providers/azureOpenai.js +51 -9
- package/dist/providers/googleAiStudio.js +3 -3
- package/dist/providers/googleVertex.js +2 -2
- package/dist/providers/huggingFace.js +1 -2
- package/dist/providers/litellm.js +1 -2
- package/dist/providers/mistral.js +2 -2
- package/dist/providers/ollama.js +7 -8
- package/dist/providers/openAI.js +2 -2
- package/dist/providers/openaiCompatible.js +5 -2
- package/dist/providers/sagemaker/language-model.d.ts +5 -0
- package/dist/providers/sagemaker/language-model.js +9 -1
- package/dist/utils/providerConfig.d.ts +25 -0
- package/dist/utils/providerConfig.js +24 -3
- package/dist/utils/providerHealth.d.ts +1 -1
- package/dist/utils/providerHealth.js +47 -36
- package/dist/utils/providerSetupMessages.js +7 -6
- package/dist/utils/providerUtils.js +16 -24
- package/dist/utils/tokenLimits.d.ts +2 -2
- package/dist/utils/tokenLimits.js +10 -3
- package/package.json +1 -1
@@ -0,0 +1,266 @@
|
|
1
|
+
/**
|
2
|
+
* Retry and Circuit Breaker Constants for NeuroLink
|
3
|
+
*
|
4
|
+
* Centralized retry configuration to replace magic numbers throughout the codebase.
|
5
|
+
* Includes retry attempts, delays, backoff strategies, and circuit breaker settings.
|
6
|
+
*
|
7
|
+
* @fileoverview Retry and resilience constants for robust error handling
|
8
|
+
* @author NeuroLink Team
|
9
|
+
* @version 1.0.0
|
10
|
+
*/
|
11
|
+
/**
|
12
|
+
* Retry attempt configuration
|
13
|
+
* Balanced for reliability vs performance across different operation types
|
14
|
+
*/
|
15
|
+
export const RETRY_ATTEMPTS = {
|
16
|
+
/** Default retry attempts for most operations */
|
17
|
+
DEFAULT: 3, // 3 attempts - Balance reliability vs speed
|
18
|
+
/** Critical operations that must succeed */
|
19
|
+
CRITICAL: 5, // 5 attempts - High-importance operations
|
20
|
+
/** Quick operations that should fail fast */
|
21
|
+
QUICK: 2, // 2 attempts - Fast operations, minimal retry
|
22
|
+
/** Network operations prone to transient failures */
|
23
|
+
NETWORK: 4, // 4 attempts - Network operations
|
24
|
+
/** Authentication operations */
|
25
|
+
AUTH: 2, // 2 attempts - Auth should fail fast for security
|
26
|
+
/** Database operations */
|
27
|
+
DATABASE: 3, // 3 attempts - Standard DB operations
|
28
|
+
/** File I/O operations */
|
29
|
+
FILE_IO: 2, // 2 attempts - File operations
|
30
|
+
};
|
31
|
+
/**
|
32
|
+
* Retry delay configuration
|
33
|
+
* Configured to prevent overwhelming services while providing quick recovery
|
34
|
+
*/
|
35
|
+
export const RETRY_DELAYS = {
|
36
|
+
/** Base delay for exponential backoff */
|
37
|
+
BASE_MS: 1000, // 1s - Starting delay
|
38
|
+
/** Minimum delay between retries */
|
39
|
+
MIN_MS: 500, // 500ms - Minimum wait time
|
40
|
+
/** Maximum delay between retries */
|
41
|
+
MAX_MS: 30000, // 30s - Maximum wait time
|
42
|
+
/** Quick retry delay for fast operations */
|
43
|
+
QUICK_MS: 200, // 200ms - Fast retry for quick operations
|
44
|
+
/** Network operation base delay */
|
45
|
+
NETWORK_BASE_MS: 2000, // 2s - Network-specific base delay
|
46
|
+
/** Authentication delay (security consideration) */
|
47
|
+
AUTH_MS: 3000, // 3s - Auth retry delay for security
|
48
|
+
};
|
49
|
+
/**
|
50
|
+
* Exponential backoff configuration
|
51
|
+
* Controls how delays increase with each retry attempt
|
52
|
+
*/
|
53
|
+
export const BACKOFF_CONFIG = {
|
54
|
+
/** Standard exponential multiplier */
|
55
|
+
MULTIPLIER: 2, // 2x - Standard exponential backoff
|
56
|
+
/** Conservative multiplier for sensitive operations */
|
57
|
+
CONSERVATIVE_MULTIPLIER: 1.5, // 1.5x - Gentler backoff
|
58
|
+
/** Aggressive multiplier for operations that should back off quickly */
|
59
|
+
AGGRESSIVE_MULTIPLIER: 3, // 3x - Rapid backoff
|
60
|
+
/** Jitter factor to prevent thundering herd */
|
61
|
+
JITTER_FACTOR: 0.1, // 10% - Random jitter
|
62
|
+
/** Maximum jitter amount in milliseconds */
|
63
|
+
MAX_JITTER_MS: 1000, // 1s - Cap on random jitter
|
64
|
+
};
|
65
|
+
/**
|
66
|
+
* Circuit breaker configuration
|
67
|
+
* Prevents cascading failures and provides automatic recovery
|
68
|
+
*/
|
69
|
+
export const CIRCUIT_BREAKER = {
|
70
|
+
/** Failure threshold before opening circuit */
|
71
|
+
FAILURE_THRESHOLD: 5, // 5 failures - Balance sensitivity vs stability
|
72
|
+
/** Success threshold for closing circuit from half-open */
|
73
|
+
SUCCESS_THRESHOLD: 3, // 3 successes - Confirm recovery
|
74
|
+
/** Minimum calls before calculating failure rate */
|
75
|
+
MIN_CALLS: 10, // 10 calls - Statistical significance
|
76
|
+
/** Failure rate threshold (0.0 to 1.0) */
|
77
|
+
FAILURE_RATE_THRESHOLD: 0.5, // 50% - Failure rate to open circuit
|
78
|
+
/** Monitoring window for failure tracking */
|
79
|
+
MONITORING_WINDOW_MS: 300000, // 5m - Sliding window for statistics
|
80
|
+
/** Half-open max calls for testing recovery */
|
81
|
+
HALF_OPEN_MAX_CALLS: 3, // 3 calls - Limited test calls
|
82
|
+
};
|
83
|
+
/**
|
84
|
+
* Provider-specific retry configuration
|
85
|
+
* Different providers may need different retry strategies
|
86
|
+
*/
|
87
|
+
export const PROVIDER_RETRY = {
|
88
|
+
/** OpenAI retry configuration */
|
89
|
+
OPENAI: {
|
90
|
+
maxAttempts: RETRY_ATTEMPTS.DEFAULT,
|
91
|
+
baseDelay: RETRY_DELAYS.BASE_MS,
|
92
|
+
maxDelay: RETRY_DELAYS.MAX_MS,
|
93
|
+
multiplier: BACKOFF_CONFIG.MULTIPLIER,
|
94
|
+
},
|
95
|
+
/** Anthropic retry configuration */
|
96
|
+
ANTHROPIC: {
|
97
|
+
maxAttempts: RETRY_ATTEMPTS.DEFAULT,
|
98
|
+
baseDelay: RETRY_DELAYS.BASE_MS,
|
99
|
+
maxDelay: RETRY_DELAYS.MAX_MS,
|
100
|
+
multiplier: BACKOFF_CONFIG.CONSERVATIVE_MULTIPLIER, // More conservative
|
101
|
+
},
|
102
|
+
/** Google (Vertex/Google AI) retry configuration */
|
103
|
+
GOOGLE: {
|
104
|
+
maxAttempts: RETRY_ATTEMPTS.NETWORK,
|
105
|
+
baseDelay: RETRY_DELAYS.NETWORK_BASE_MS,
|
106
|
+
maxDelay: RETRY_DELAYS.MAX_MS,
|
107
|
+
multiplier: BACKOFF_CONFIG.MULTIPLIER,
|
108
|
+
},
|
109
|
+
/** AWS Bedrock retry configuration */
|
110
|
+
BEDROCK: {
|
111
|
+
maxAttempts: RETRY_ATTEMPTS.CRITICAL,
|
112
|
+
baseDelay: RETRY_DELAYS.BASE_MS,
|
113
|
+
maxDelay: RETRY_DELAYS.MAX_MS,
|
114
|
+
multiplier: BACKOFF_CONFIG.CONSERVATIVE_MULTIPLIER,
|
115
|
+
},
|
116
|
+
/** Azure OpenAI retry configuration */
|
117
|
+
AZURE: {
|
118
|
+
maxAttempts: RETRY_ATTEMPTS.DEFAULT,
|
119
|
+
baseDelay: RETRY_DELAYS.BASE_MS,
|
120
|
+
maxDelay: RETRY_DELAYS.MAX_MS,
|
121
|
+
multiplier: BACKOFF_CONFIG.MULTIPLIER,
|
122
|
+
},
|
123
|
+
/** Ollama retry configuration (local service) */
|
124
|
+
OLLAMA: {
|
125
|
+
maxAttempts: RETRY_ATTEMPTS.QUICK,
|
126
|
+
baseDelay: RETRY_DELAYS.QUICK_MS,
|
127
|
+
maxDelay: 5000, // 5s max for local service
|
128
|
+
multiplier: BACKOFF_CONFIG.CONSERVATIVE_MULTIPLIER,
|
129
|
+
},
|
130
|
+
};
|
131
|
+
/**
|
132
|
+
* Operation-specific retry configuration
|
133
|
+
* Different operations may require different retry strategies
|
134
|
+
*/
|
135
|
+
export const OPERATION_RETRY = {
|
136
|
+
/** Tool execution retry config */
|
137
|
+
TOOL_EXECUTION: {
|
138
|
+
maxAttempts: RETRY_ATTEMPTS.DEFAULT,
|
139
|
+
baseDelay: RETRY_DELAYS.BASE_MS,
|
140
|
+
circuitBreaker: true,
|
141
|
+
},
|
142
|
+
/** MCP operation retry config */
|
143
|
+
MCP_OPERATION: {
|
144
|
+
maxAttempts: RETRY_ATTEMPTS.QUICK,
|
145
|
+
baseDelay: RETRY_DELAYS.QUICK_MS,
|
146
|
+
circuitBreaker: false, // MCP operations are usually fast
|
147
|
+
},
|
148
|
+
/** Network request retry config */
|
149
|
+
NETWORK_REQUEST: {
|
150
|
+
maxAttempts: RETRY_ATTEMPTS.NETWORK,
|
151
|
+
baseDelay: RETRY_DELAYS.NETWORK_BASE_MS,
|
152
|
+
circuitBreaker: true,
|
153
|
+
},
|
154
|
+
/** Database operation retry config */
|
155
|
+
DATABASE_OPERATION: {
|
156
|
+
maxAttempts: RETRY_ATTEMPTS.DATABASE,
|
157
|
+
baseDelay: RETRY_DELAYS.BASE_MS,
|
158
|
+
circuitBreaker: true,
|
159
|
+
},
|
160
|
+
/** Authentication retry config */
|
161
|
+
AUTHENTICATION: {
|
162
|
+
maxAttempts: RETRY_ATTEMPTS.AUTH,
|
163
|
+
baseDelay: RETRY_DELAYS.AUTH_MS,
|
164
|
+
circuitBreaker: false, // Don't circuit break auth for security
|
165
|
+
},
|
166
|
+
};
|
167
|
+
/**
|
168
|
+
* Retry utility functions
|
169
|
+
*/
|
170
|
+
export const RetryUtils = {
|
171
|
+
/**
|
172
|
+
* Calculate exponential backoff delay
|
173
|
+
* @param attempt - Current attempt number (0-based)
|
174
|
+
* @param baseDelay - Base delay in milliseconds
|
175
|
+
* @param multiplier - Exponential multiplier
|
176
|
+
* @param maxDelay - Maximum delay cap
|
177
|
+
* @param jitter - Whether to add random jitter
|
178
|
+
* @returns Calculated delay in milliseconds
|
179
|
+
*/
|
180
|
+
calculateBackoffDelay: (attempt, baseDelay = RETRY_DELAYS.BASE_MS, multiplier = BACKOFF_CONFIG.MULTIPLIER, maxDelay = RETRY_DELAYS.MAX_MS, jitter = true) => {
|
181
|
+
const safeAttempt = Math.max(0, attempt);
|
182
|
+
const base = Math.max(baseDelay, RETRY_DELAYS.MIN_MS);
|
183
|
+
const delay = Math.min(base * Math.pow(multiplier, safeAttempt), maxDelay);
|
184
|
+
if (!jitter) {
|
185
|
+
return delay;
|
186
|
+
}
|
187
|
+
const jitterCap = Math.min(BACKOFF_CONFIG.MAX_JITTER_MS, delay * BACKOFF_CONFIG.JITTER_FACTOR);
|
188
|
+
return Math.min(maxDelay, Math.floor(delay + Math.random() * jitterCap));
|
189
|
+
},
|
190
|
+
/**
|
191
|
+
* Check if an error is retriable
|
192
|
+
* @param error - The error to check
|
193
|
+
* @returns True if the error should be retried
|
194
|
+
*/
|
195
|
+
isRetriableError: (error) => {
|
196
|
+
if (!error) {
|
197
|
+
return false;
|
198
|
+
}
|
199
|
+
const err = error;
|
200
|
+
// Network/system codes
|
201
|
+
const code = typeof err?.code === "string" ? err.code : undefined;
|
202
|
+
if (code &&
|
203
|
+
["ENOTFOUND", "ECONNRESET", "ETIMEDOUT", "ECONNREFUSED"].includes(code)) {
|
204
|
+
return true;
|
205
|
+
}
|
206
|
+
// HTTP status
|
207
|
+
const status = typeof err?.status === "number"
|
208
|
+
? err.status
|
209
|
+
: typeof err?.statusCode === "number"
|
210
|
+
? err.statusCode
|
211
|
+
: undefined;
|
212
|
+
if (typeof status === "number" &&
|
213
|
+
(status >= 500 || status === 429 || status === 408)) {
|
214
|
+
return true;
|
215
|
+
}
|
216
|
+
// Messages
|
217
|
+
const msg = typeof err?.message === "string" ? err.message.toLowerCase() : "";
|
218
|
+
if (msg.includes("rate limit") ||
|
219
|
+
msg.includes("quota") ||
|
220
|
+
msg.includes("throttle")) {
|
221
|
+
return true;
|
222
|
+
}
|
223
|
+
if (msg.includes("timeout")) {
|
224
|
+
return true;
|
225
|
+
}
|
226
|
+
return false;
|
227
|
+
},
|
228
|
+
/**
|
229
|
+
* Get retry configuration for a specific provider
|
230
|
+
* @param provider - Provider name
|
231
|
+
* @returns Retry configuration object
|
232
|
+
*/
|
233
|
+
getProviderRetryConfig: (provider) => {
|
234
|
+
const normalizedProvider = provider.toLowerCase().replace(/[-_]/g, "");
|
235
|
+
switch (normalizedProvider) {
|
236
|
+
case "openai":
|
237
|
+
return PROVIDER_RETRY.OPENAI;
|
238
|
+
case "anthropic":
|
239
|
+
return PROVIDER_RETRY.ANTHROPIC;
|
240
|
+
case "googleai":
|
241
|
+
case "vertex":
|
242
|
+
return PROVIDER_RETRY.GOOGLE;
|
243
|
+
case "bedrock":
|
244
|
+
return PROVIDER_RETRY.BEDROCK;
|
245
|
+
case "azure":
|
246
|
+
return PROVIDER_RETRY.AZURE;
|
247
|
+
case "ollama":
|
248
|
+
return PROVIDER_RETRY.OLLAMA;
|
249
|
+
default:
|
250
|
+
return {
|
251
|
+
maxAttempts: RETRY_ATTEMPTS.DEFAULT,
|
252
|
+
baseDelay: RETRY_DELAYS.BASE_MS,
|
253
|
+
maxDelay: RETRY_DELAYS.MAX_MS,
|
254
|
+
multiplier: BACKOFF_CONFIG.MULTIPLIER,
|
255
|
+
};
|
256
|
+
}
|
257
|
+
},
|
258
|
+
};
|
259
|
+
// Legacy compatibility exports
|
260
|
+
export const DEFAULT_RETRY_ATTEMPTS = RETRY_ATTEMPTS.DEFAULT;
|
261
|
+
export const DEFAULT_INITIAL_DELAY = RETRY_DELAYS.BASE_MS;
|
262
|
+
export const DEFAULT_MAX_DELAY = RETRY_DELAYS.MAX_MS;
|
263
|
+
export const DEFAULT_BACKOFF_MULTIPLIER = BACKOFF_CONFIG.MULTIPLIER;
|
264
|
+
export const CIRCUIT_BREAKER_FAILURE_THRESHOLD = CIRCUIT_BREAKER.FAILURE_THRESHOLD;
|
265
|
+
// Single source of truth: re-export from timeouts
|
266
|
+
export { CIRCUIT_BREAKER_RESET_MS } from "./timeouts.js";
|
@@ -0,0 +1,225 @@
|
|
1
|
+
/**
|
2
|
+
* Timeout Constants for NeuroLink
|
3
|
+
*
|
4
|
+
* Centralized timeout configuration to replace magic numbers throughout the codebase.
|
5
|
+
* Each timeout value includes business justification and use case documentation.
|
6
|
+
*
|
7
|
+
* @fileoverview Timeout constants organized by functional area
|
8
|
+
* @author NeuroLink Team
|
9
|
+
* @version 1.0.0
|
10
|
+
*/
|
11
|
+
/**
|
12
|
+
* Tool execution timeout constants
|
13
|
+
* These values balance reliability vs performance for AI tool operations
|
14
|
+
*/
|
15
|
+
export declare const TOOL_TIMEOUTS: {
|
16
|
+
/** Default timeout for AI tool execution - handles most complex operations */
|
17
|
+
readonly EXECUTION_DEFAULT_MS: 30000;
|
18
|
+
/** Fast timeout for simple tools that should complete quickly */
|
19
|
+
readonly EXECUTION_FAST_MS: 5000;
|
20
|
+
/** Extended timeout for complex analysis operations */
|
21
|
+
readonly EXECUTION_COMPLEX_MS: 60000;
|
22
|
+
/** Ultra-long timeout for batch operations */
|
23
|
+
readonly EXECUTION_BATCH_MS: 120000;
|
24
|
+
};
|
25
|
+
/**
|
26
|
+
* Provider connectivity and health check timeouts
|
27
|
+
* Tuned for quick health checks while allowing for network latency
|
28
|
+
*/
|
29
|
+
export declare const PROVIDER_TIMEOUTS: {
|
30
|
+
/** Quick provider health check - should fail fast */
|
31
|
+
readonly TEST_MS: 5000;
|
32
|
+
/** Provider connection establishment timeout */
|
33
|
+
readonly CONNECTION_MS: 10000;
|
34
|
+
/** Authentication validation timeout */
|
35
|
+
readonly AUTH_MS: 3000;
|
36
|
+
/** Model availability check timeout */
|
37
|
+
readonly MODEL_CHECK_MS: 8000;
|
38
|
+
/** Provider capability discovery timeout */
|
39
|
+
readonly CAPABILITY_DISCOVERY_MS: 15000;
|
40
|
+
};
|
41
|
+
/**
|
42
|
+
* MCP (Model Context Protocol) system timeouts
|
43
|
+
* Balanced for reliable MCP server communication
|
44
|
+
*/
|
45
|
+
export declare const MCP_TIMEOUTS: {
|
46
|
+
/** MCP server initialization timeout */
|
47
|
+
readonly INITIALIZATION_MS: 3000;
|
48
|
+
/** Tool discovery timeout per server */
|
49
|
+
readonly TOOL_DISCOVERY_MS: 10000;
|
50
|
+
/** MCP client connection timeout */
|
51
|
+
readonly CLIENT_CONNECTION_MS: 5000;
|
52
|
+
/** External MCP server startup timeout */
|
53
|
+
readonly EXTERNAL_SERVER_STARTUP_MS: 15000;
|
54
|
+
/** MCP server shutdown timeout */
|
55
|
+
readonly SHUTDOWN_MS: 5000;
|
56
|
+
};
|
57
|
+
/**
|
58
|
+
* Circuit breaker timeout configuration
|
59
|
+
* Prevents cascading failures while allowing recovery
|
60
|
+
*/
|
61
|
+
export declare const CIRCUIT_BREAKER_TIMEOUTS: {
|
62
|
+
/** Circuit breaker reset timeout - how long to wait before retry */
|
63
|
+
readonly RESET_MS: 60000;
|
64
|
+
/** Half-open state timeout - trial period for recovery */
|
65
|
+
readonly HALF_OPEN_MS: 30000;
|
66
|
+
/** Monitoring window for failure rate calculation */
|
67
|
+
readonly MONITORING_WINDOW_MS: 300000;
|
68
|
+
};
|
69
|
+
/**
|
70
|
+
* Network operation timeouts
|
71
|
+
* Configured for various network conditions and use cases
|
72
|
+
*/
|
73
|
+
export declare const NETWORK_TIMEOUTS: {
|
74
|
+
/** Standard network request timeout */
|
75
|
+
readonly REQUEST_MS: 15000;
|
76
|
+
/** Quick connectivity check timeout */
|
77
|
+
readonly QUICK_CHECK_MS: 2000;
|
78
|
+
/** File download/upload timeout per MB */
|
79
|
+
readonly TRANSFER_PER_MB_MS: 1000;
|
80
|
+
/** DNS resolution timeout */
|
81
|
+
readonly DNS_RESOLUTION_MS: 3000;
|
82
|
+
/** WebSocket connection timeout */
|
83
|
+
readonly WEBSOCKET_CONNECTION_MS: 10000;
|
84
|
+
};
|
85
|
+
/**
|
86
|
+
* System operation timeouts
|
87
|
+
* For application lifecycle and resource management
|
88
|
+
*/
|
89
|
+
export declare const SYSTEM_TIMEOUTS: {
|
90
|
+
/** Application graceful shutdown timeout */
|
91
|
+
readonly GRACEFUL_SHUTDOWN_MS: 30000;
|
92
|
+
/** Resource cleanup timeout */
|
93
|
+
readonly CLEANUP_OPERATION_MS: 5000;
|
94
|
+
/** Cache flush timeout */
|
95
|
+
readonly CACHE_FLUSH_MS: 3000;
|
96
|
+
/** Health check interval */
|
97
|
+
readonly HEALTH_CHECK_INTERVAL_MS: 30000;
|
98
|
+
/** Metrics collection timeout */
|
99
|
+
readonly METRICS_COLLECTION_MS: 1000;
|
100
|
+
};
|
101
|
+
/**
|
102
|
+
* Development and testing timeouts
|
103
|
+
* Separate timeouts for development vs production environments
|
104
|
+
*/
|
105
|
+
export declare const DEV_TIMEOUTS: {
|
106
|
+
/** Extended timeout for debugging */
|
107
|
+
readonly DEBUG_EXTENDED_MS: 300000;
|
108
|
+
/** Unit test timeout */
|
109
|
+
readonly UNIT_TEST_MS: 10000;
|
110
|
+
/** Integration test timeout */
|
111
|
+
readonly INTEGRATION_TEST_MS: 60000;
|
112
|
+
/** Mock service response delay */
|
113
|
+
readonly MOCK_DELAY_MS: 100;
|
114
|
+
};
|
115
|
+
/**
|
116
|
+
* Combined timeout constants for easy access
|
117
|
+
* Exports all timeout categories in a single object
|
118
|
+
*/
|
119
|
+
export declare const TIMEOUTS: {
|
120
|
+
readonly TOOL: {
|
121
|
+
/** Default timeout for AI tool execution - handles most complex operations */
|
122
|
+
readonly EXECUTION_DEFAULT_MS: 30000;
|
123
|
+
/** Fast timeout for simple tools that should complete quickly */
|
124
|
+
readonly EXECUTION_FAST_MS: 5000;
|
125
|
+
/** Extended timeout for complex analysis operations */
|
126
|
+
readonly EXECUTION_COMPLEX_MS: 60000;
|
127
|
+
/** Ultra-long timeout for batch operations */
|
128
|
+
readonly EXECUTION_BATCH_MS: 120000;
|
129
|
+
};
|
130
|
+
readonly PROVIDER: {
|
131
|
+
/** Quick provider health check - should fail fast */
|
132
|
+
readonly TEST_MS: 5000;
|
133
|
+
/** Provider connection establishment timeout */
|
134
|
+
readonly CONNECTION_MS: 10000;
|
135
|
+
/** Authentication validation timeout */
|
136
|
+
readonly AUTH_MS: 3000;
|
137
|
+
/** Model availability check timeout */
|
138
|
+
readonly MODEL_CHECK_MS: 8000;
|
139
|
+
/** Provider capability discovery timeout */
|
140
|
+
readonly CAPABILITY_DISCOVERY_MS: 15000;
|
141
|
+
};
|
142
|
+
readonly MCP: {
|
143
|
+
/** MCP server initialization timeout */
|
144
|
+
readonly INITIALIZATION_MS: 3000;
|
145
|
+
/** Tool discovery timeout per server */
|
146
|
+
readonly TOOL_DISCOVERY_MS: 10000;
|
147
|
+
/** MCP client connection timeout */
|
148
|
+
readonly CLIENT_CONNECTION_MS: 5000;
|
149
|
+
/** External MCP server startup timeout */
|
150
|
+
readonly EXTERNAL_SERVER_STARTUP_MS: 15000;
|
151
|
+
/** MCP server shutdown timeout */
|
152
|
+
readonly SHUTDOWN_MS: 5000;
|
153
|
+
};
|
154
|
+
readonly CIRCUIT_BREAKER: {
|
155
|
+
/** Circuit breaker reset timeout - how long to wait before retry */
|
156
|
+
readonly RESET_MS: 60000;
|
157
|
+
/** Half-open state timeout - trial period for recovery */
|
158
|
+
readonly HALF_OPEN_MS: 30000;
|
159
|
+
/** Monitoring window for failure rate calculation */
|
160
|
+
readonly MONITORING_WINDOW_MS: 300000;
|
161
|
+
};
|
162
|
+
readonly NETWORK: {
|
163
|
+
/** Standard network request timeout */
|
164
|
+
readonly REQUEST_MS: 15000;
|
165
|
+
/** Quick connectivity check timeout */
|
166
|
+
readonly QUICK_CHECK_MS: 2000;
|
167
|
+
/** File download/upload timeout per MB */
|
168
|
+
readonly TRANSFER_PER_MB_MS: 1000;
|
169
|
+
/** DNS resolution timeout */
|
170
|
+
readonly DNS_RESOLUTION_MS: 3000;
|
171
|
+
/** WebSocket connection timeout */
|
172
|
+
readonly WEBSOCKET_CONNECTION_MS: 10000;
|
173
|
+
};
|
174
|
+
readonly SYSTEM: {
|
175
|
+
/** Application graceful shutdown timeout */
|
176
|
+
readonly GRACEFUL_SHUTDOWN_MS: 30000;
|
177
|
+
/** Resource cleanup timeout */
|
178
|
+
readonly CLEANUP_OPERATION_MS: 5000;
|
179
|
+
/** Cache flush timeout */
|
180
|
+
readonly CACHE_FLUSH_MS: 3000;
|
181
|
+
/** Health check interval */
|
182
|
+
readonly HEALTH_CHECK_INTERVAL_MS: 30000;
|
183
|
+
/** Metrics collection timeout */
|
184
|
+
readonly METRICS_COLLECTION_MS: 1000;
|
185
|
+
};
|
186
|
+
readonly DEV: {
|
187
|
+
/** Extended timeout for debugging */
|
188
|
+
readonly DEBUG_EXTENDED_MS: 300000;
|
189
|
+
/** Unit test timeout */
|
190
|
+
readonly UNIT_TEST_MS: 10000;
|
191
|
+
/** Integration test timeout */
|
192
|
+
readonly INTEGRATION_TEST_MS: 60000;
|
193
|
+
/** Mock service response delay */
|
194
|
+
readonly MOCK_DELAY_MS: 100;
|
195
|
+
};
|
196
|
+
};
|
197
|
+
/**
|
198
|
+
* Timeout utility functions
|
199
|
+
*/
|
200
|
+
export declare const TimeoutUtils: {
|
201
|
+
/**
|
202
|
+
* Get environment-adjusted timeout
|
203
|
+
* @param baseTimeout - Base timeout value in milliseconds
|
204
|
+
* @param environment - Target environment (development gets 2x timeout)
|
205
|
+
* @returns Adjusted timeout value
|
206
|
+
*/
|
207
|
+
readonly getEnvironmentTimeout: (baseTimeout: number, environment?: string | undefined) => number;
|
208
|
+
/**
|
209
|
+
* Get timeout with jitter to prevent thundering herd
|
210
|
+
* @param baseTimeout - Base timeout value
|
211
|
+
* @param jitterFactor - Jitter factor (0.1 = 10% random variation)
|
212
|
+
* @returns Timeout with random jitter applied
|
213
|
+
*/
|
214
|
+
readonly getTimeoutWithJitter: (baseTimeout: number, jitterFactor?: number) => number;
|
215
|
+
/**
|
216
|
+
* Parse timeout string to milliseconds
|
217
|
+
* @param timeout - Timeout string like "30s", "2m", "1h" or number
|
218
|
+
* @returns Timeout in milliseconds
|
219
|
+
*/
|
220
|
+
readonly parseTimeout: (timeout: string | number) => number;
|
221
|
+
};
|
222
|
+
export declare const DEFAULT_TIMEOUT: 30000;
|
223
|
+
export declare const PROVIDER_TEST_TIMEOUT: 5000;
|
224
|
+
export declare const MCP_INIT_TIMEOUT: 3000;
|
225
|
+
export declare const CIRCUIT_BREAKER_RESET_MS: 60000;
|
@@ -0,0 +1,182 @@
|
|
1
|
+
/**
|
2
|
+
* Timeout Constants for NeuroLink
|
3
|
+
*
|
4
|
+
* Centralized timeout configuration to replace magic numbers throughout the codebase.
|
5
|
+
* Each timeout value includes business justification and use case documentation.
|
6
|
+
*
|
7
|
+
* @fileoverview Timeout constants organized by functional area
|
8
|
+
* @author NeuroLink Team
|
9
|
+
* @version 1.0.0
|
10
|
+
*/
|
11
|
+
/**
|
12
|
+
* Tool execution timeout constants
|
13
|
+
* These values balance reliability vs performance for AI tool operations
|
14
|
+
*/
|
15
|
+
export const TOOL_TIMEOUTS = {
|
16
|
+
/** Default timeout for AI tool execution - handles most complex operations */
|
17
|
+
EXECUTION_DEFAULT_MS: 30000, // 30s - Complex AI tool operations requiring LLM calls
|
18
|
+
/** Fast timeout for simple tools that should complete quickly */
|
19
|
+
EXECUTION_FAST_MS: 5000, // 5s - Simple tool operations (calculations, lookups)
|
20
|
+
/** Extended timeout for complex analysis operations */
|
21
|
+
EXECUTION_COMPLEX_MS: 60000, // 60s - Complex analysis, multiple API calls
|
22
|
+
/** Ultra-long timeout for batch operations */
|
23
|
+
EXECUTION_BATCH_MS: 120000, // 2m - Batch processing, large data operations
|
24
|
+
};
|
25
|
+
/**
|
26
|
+
* Provider connectivity and health check timeouts
|
27
|
+
* Tuned for quick health checks while allowing for network latency
|
28
|
+
*/
|
29
|
+
export const PROVIDER_TIMEOUTS = {
|
30
|
+
/** Quick provider health check - should fail fast */
|
31
|
+
TEST_MS: 5000, // 5s - Provider health check ping
|
32
|
+
/** Provider connection establishment timeout */
|
33
|
+
CONNECTION_MS: 10000, // 10s - Initial connection setup
|
34
|
+
/** Authentication validation timeout */
|
35
|
+
AUTH_MS: 3000, // 3s - API key validation
|
36
|
+
/** Model availability check timeout */
|
37
|
+
MODEL_CHECK_MS: 8000, // 8s - Model existence/availability check
|
38
|
+
/** Provider capability discovery timeout */
|
39
|
+
CAPABILITY_DISCOVERY_MS: 15000, // 15s - Feature discovery and validation
|
40
|
+
};
|
41
|
+
/**
|
42
|
+
* MCP (Model Context Protocol) system timeouts
|
43
|
+
* Balanced for reliable MCP server communication
|
44
|
+
*/
|
45
|
+
export const MCP_TIMEOUTS = {
|
46
|
+
/** MCP server initialization timeout */
|
47
|
+
INITIALIZATION_MS: 3000, // 3s - MCP server startup
|
48
|
+
/** Tool discovery timeout per server */
|
49
|
+
TOOL_DISCOVERY_MS: 10000, // 10s - Tool enumeration from MCP server
|
50
|
+
/** MCP client connection timeout */
|
51
|
+
CLIENT_CONNECTION_MS: 5000, // 5s - Client connection establishment
|
52
|
+
/** External MCP server startup timeout */
|
53
|
+
EXTERNAL_SERVER_STARTUP_MS: 15000, // 15s - External process startup
|
54
|
+
/** MCP server shutdown timeout */
|
55
|
+
SHUTDOWN_MS: 5000, // 5s - Graceful server shutdown
|
56
|
+
};
|
57
|
+
/**
|
58
|
+
* Circuit breaker timeout configuration
|
59
|
+
* Prevents cascading failures while allowing recovery
|
60
|
+
*/
|
61
|
+
export const CIRCUIT_BREAKER_TIMEOUTS = {
|
62
|
+
/** Circuit breaker reset timeout - how long to wait before retry */
|
63
|
+
RESET_MS: 60000, // 60s - Circuit breaker reset time
|
64
|
+
/** Half-open state timeout - trial period for recovery */
|
65
|
+
HALF_OPEN_MS: 30000, // 30s - Half-open state timeout
|
66
|
+
/** Monitoring window for failure rate calculation */
|
67
|
+
MONITORING_WINDOW_MS: 300000, // 5m - Sliding window for failure tracking
|
68
|
+
};
|
69
|
+
/**
|
70
|
+
* Network operation timeouts
|
71
|
+
* Configured for various network conditions and use cases
|
72
|
+
*/
|
73
|
+
export const NETWORK_TIMEOUTS = {
|
74
|
+
/** Standard network request timeout */
|
75
|
+
REQUEST_MS: 15000, // 15s - Standard HTTP requests
|
76
|
+
/** Quick connectivity check timeout */
|
77
|
+
QUICK_CHECK_MS: 2000, // 2s - Fast network availability check
|
78
|
+
/** File download/upload timeout per MB */
|
79
|
+
TRANSFER_PER_MB_MS: 1000, // 1s per MB - File transfer operations
|
80
|
+
/** DNS resolution timeout */
|
81
|
+
DNS_RESOLUTION_MS: 3000, // 3s - DNS lookup timeout
|
82
|
+
/** WebSocket connection timeout */
|
83
|
+
WEBSOCKET_CONNECTION_MS: 10000, // 10s - WebSocket handshake
|
84
|
+
};
|
85
|
+
/**
|
86
|
+
* System operation timeouts
|
87
|
+
* For application lifecycle and resource management
|
88
|
+
*/
|
89
|
+
export const SYSTEM_TIMEOUTS = {
|
90
|
+
/** Application graceful shutdown timeout */
|
91
|
+
GRACEFUL_SHUTDOWN_MS: 30000, // 30s - Complete application shutdown
|
92
|
+
/** Resource cleanup timeout */
|
93
|
+
CLEANUP_OPERATION_MS: 5000, // 5s - Memory/resource cleanup
|
94
|
+
/** Cache flush timeout */
|
95
|
+
CACHE_FLUSH_MS: 3000, // 3s - Cache clearing operations
|
96
|
+
/** Health check interval */
|
97
|
+
HEALTH_CHECK_INTERVAL_MS: 30000, // 30s - Periodic health monitoring
|
98
|
+
/** Metrics collection timeout */
|
99
|
+
METRICS_COLLECTION_MS: 1000, // 1s - Performance metrics gathering
|
100
|
+
};
|
101
|
+
/**
|
102
|
+
* Development and testing timeouts
|
103
|
+
* Separate timeouts for development vs production environments
|
104
|
+
*/
|
105
|
+
export const DEV_TIMEOUTS = {
|
106
|
+
/** Extended timeout for debugging */
|
107
|
+
DEBUG_EXTENDED_MS: 300000, // 5m - Long timeout for debugging sessions
|
108
|
+
/** Unit test timeout */
|
109
|
+
UNIT_TEST_MS: 10000, // 10s - Individual test timeout
|
110
|
+
/** Integration test timeout */
|
111
|
+
INTEGRATION_TEST_MS: 60000, // 60s - Full integration test
|
112
|
+
/** Mock service response delay */
|
113
|
+
MOCK_DELAY_MS: 100, // 100ms - Simulated service delay
|
114
|
+
};
|
115
|
+
/**
|
116
|
+
* Combined timeout constants for easy access
|
117
|
+
* Exports all timeout categories in a single object
|
118
|
+
*/
|
119
|
+
export const TIMEOUTS = {
|
120
|
+
TOOL: TOOL_TIMEOUTS,
|
121
|
+
PROVIDER: PROVIDER_TIMEOUTS,
|
122
|
+
MCP: MCP_TIMEOUTS,
|
123
|
+
CIRCUIT_BREAKER: CIRCUIT_BREAKER_TIMEOUTS,
|
124
|
+
NETWORK: NETWORK_TIMEOUTS,
|
125
|
+
SYSTEM: SYSTEM_TIMEOUTS,
|
126
|
+
DEV: DEV_TIMEOUTS,
|
127
|
+
};
|
128
|
+
/**
|
129
|
+
* Timeout utility functions
|
130
|
+
*/
|
131
|
+
export const TimeoutUtils = {
|
132
|
+
/**
|
133
|
+
* Get environment-adjusted timeout
|
134
|
+
* @param baseTimeout - Base timeout value in milliseconds
|
135
|
+
* @param environment - Target environment (development gets 2x timeout)
|
136
|
+
* @returns Adjusted timeout value
|
137
|
+
*/
|
138
|
+
getEnvironmentTimeout: (baseTimeout, environment = process.env.NODE_ENV) => {
|
139
|
+
return environment === "development" ? baseTimeout * 2 : baseTimeout;
|
140
|
+
},
|
141
|
+
/**
|
142
|
+
* Get timeout with jitter to prevent thundering herd
|
143
|
+
* @param baseTimeout - Base timeout value
|
144
|
+
* @param jitterFactor - Jitter factor (0.1 = 10% random variation)
|
145
|
+
* @returns Timeout with random jitter applied
|
146
|
+
*/
|
147
|
+
getTimeoutWithJitter: (baseTimeout, jitterFactor = 0.1) => {
|
148
|
+
const jitter = Math.random() * baseTimeout * jitterFactor;
|
149
|
+
return Math.floor(baseTimeout + jitter);
|
150
|
+
},
|
151
|
+
/**
|
152
|
+
* Parse timeout string to milliseconds
|
153
|
+
* @param timeout - Timeout string like "30s", "2m", "1h" or number
|
154
|
+
* @returns Timeout in milliseconds
|
155
|
+
*/
|
156
|
+
parseTimeout: (timeout) => {
|
157
|
+
if (typeof timeout === "number") {
|
158
|
+
return timeout;
|
159
|
+
}
|
160
|
+
const match = timeout.match(/^(\d+(?:\.\d+)?)(s|m|h)?$/);
|
161
|
+
if (!match) {
|
162
|
+
throw new Error(`Invalid timeout format: ${timeout}`);
|
163
|
+
}
|
164
|
+
const value = parseFloat(match[1]);
|
165
|
+
const unit = match[2] || "s";
|
166
|
+
switch (unit) {
|
167
|
+
case "s":
|
168
|
+
return value * 1000;
|
169
|
+
case "m":
|
170
|
+
return value * 60 * 1000;
|
171
|
+
case "h":
|
172
|
+
return value * 60 * 60 * 1000;
|
173
|
+
default:
|
174
|
+
return value;
|
175
|
+
}
|
176
|
+
},
|
177
|
+
};
|
178
|
+
// Legacy compatibility - maintain backward compatibility
|
179
|
+
export const DEFAULT_TIMEOUT = TOOL_TIMEOUTS.EXECUTION_DEFAULT_MS;
|
180
|
+
export const PROVIDER_TEST_TIMEOUT = PROVIDER_TIMEOUTS.TEST_MS;
|
181
|
+
export const MCP_INIT_TIMEOUT = MCP_TIMEOUTS.INITIALIZATION_MS;
|
182
|
+
export const CIRCUIT_BREAKER_RESET_MS = CIRCUIT_BREAKER_TIMEOUTS.RESET_MS;
|