@juspay/neurolink 7.29.1 → 7.29.3
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 +12 -0
- package/dist/cli/commands/config.d.ts +86 -86
- package/dist/cli/commands/mcp.js +64 -9
- package/dist/cli/commands/models.js +25 -21
- package/dist/cli/commands/ollama.js +2 -2
- package/dist/cli/factories/commandFactory.d.ts +9 -0
- package/dist/cli/factories/commandFactory.js +177 -83
- package/dist/cli/factories/ollamaCommandFactory.js +3 -1
- package/dist/cli/factories/sagemakerCommandFactory.js +3 -2
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +19 -11
- package/dist/cli/utils/envManager.js +5 -5
- package/dist/cli/utils/ollamaUtils.d.ts +12 -0
- package/dist/cli/utils/ollamaUtils.js +58 -42
- package/dist/config/configManager.js +5 -2
- package/dist/core/analytics.d.ts +2 -24
- package/dist/core/analytics.js +12 -17
- package/dist/core/baseProvider.d.ts +30 -1
- package/dist/core/baseProvider.js +180 -198
- package/dist/core/dynamicModels.d.ts +4 -4
- package/dist/core/dynamicModels.js +7 -7
- package/dist/core/evaluation.d.ts +9 -9
- package/dist/core/evaluation.js +117 -65
- package/dist/core/evaluationProviders.d.ts +18 -2
- package/dist/core/evaluationProviders.js +15 -13
- package/dist/core/factory.js +77 -4
- package/dist/core/modelConfiguration.d.ts +63 -0
- package/dist/core/modelConfiguration.js +354 -290
- package/dist/core/streamAnalytics.d.ts +10 -5
- package/dist/core/streamAnalytics.js +10 -10
- package/dist/core/types.d.ts +19 -109
- package/dist/core/types.js +13 -0
- package/dist/factories/providerFactory.js +4 -1
- package/dist/factories/providerRegistry.js +2 -2
- package/dist/index.d.ts +2 -1
- package/dist/lib/config/configManager.js +5 -2
- package/dist/lib/core/analytics.d.ts +2 -24
- package/dist/lib/core/analytics.js +12 -17
- package/dist/lib/core/baseProvider.d.ts +30 -1
- package/dist/lib/core/baseProvider.js +180 -198
- package/dist/lib/core/dynamicModels.js +7 -7
- package/dist/lib/core/evaluation.d.ts +9 -9
- package/dist/lib/core/evaluation.js +117 -65
- package/dist/lib/core/evaluationProviders.d.ts +18 -2
- package/dist/lib/core/evaluationProviders.js +15 -13
- package/dist/lib/core/factory.js +77 -4
- package/dist/lib/core/modelConfiguration.d.ts +63 -0
- package/dist/lib/core/modelConfiguration.js +354 -290
- package/dist/lib/core/streamAnalytics.d.ts +10 -5
- package/dist/lib/core/streamAnalytics.js +10 -10
- package/dist/lib/core/types.d.ts +19 -109
- package/dist/lib/core/types.js +13 -0
- package/dist/lib/factories/providerFactory.js +4 -1
- package/dist/lib/factories/providerRegistry.js +2 -2
- package/dist/lib/index.d.ts +2 -1
- package/dist/lib/mcp/externalServerManager.js +14 -6
- package/dist/lib/mcp/factory.js +1 -1
- package/dist/lib/mcp/flexibleToolValidator.d.ts +50 -0
- package/dist/lib/mcp/flexibleToolValidator.js +161 -0
- package/dist/lib/mcp/index.d.ts +1 -1
- package/dist/lib/mcp/index.js +1 -1
- package/dist/lib/mcp/mcpCircuitBreaker.js +5 -1
- package/dist/lib/mcp/mcpClientFactory.js +3 -0
- package/dist/lib/mcp/registry.d.ts +3 -3
- package/dist/lib/mcp/registry.js +3 -3
- package/dist/lib/mcp/servers/aiProviders/aiAnalysisTools.js +5 -5
- package/dist/lib/mcp/servers/aiProviders/aiWorkflowTools.js +6 -6
- package/dist/lib/mcp/servers/utilities/utilityServer.js +1 -1
- package/dist/lib/mcp/toolDiscoveryService.js +8 -2
- package/dist/lib/mcp/toolRegistry.d.ts +2 -2
- package/dist/lib/mcp/toolRegistry.js +29 -54
- package/dist/lib/middleware/builtin/analytics.js +4 -4
- package/dist/lib/middleware/builtin/guardrails.js +2 -2
- package/dist/lib/middleware/registry.js +11 -2
- package/dist/lib/models/modelRegistry.d.ts +1 -1
- package/dist/lib/models/modelRegistry.js +3 -3
- package/dist/lib/models/modelResolver.d.ts +1 -1
- package/dist/lib/models/modelResolver.js +2 -2
- package/dist/lib/neurolink.d.ts +118 -0
- package/dist/lib/neurolink.js +814 -952
- package/dist/lib/providers/amazonBedrock.d.ts +47 -6
- package/dist/lib/providers/amazonBedrock.js +282 -23
- package/dist/lib/providers/amazonSagemaker.d.ts +1 -1
- package/dist/lib/providers/amazonSagemaker.js +12 -3
- package/dist/lib/providers/anthropic.d.ts +1 -1
- package/dist/lib/providers/anthropic.js +7 -6
- package/dist/lib/providers/anthropicBaseProvider.d.ts +1 -1
- package/dist/lib/providers/anthropicBaseProvider.js +4 -3
- package/dist/lib/providers/aws/credentialProvider.d.ts +58 -0
- package/dist/lib/providers/aws/credentialProvider.js +267 -0
- package/dist/lib/providers/aws/credentialTester.d.ts +49 -0
- package/dist/lib/providers/aws/credentialTester.js +394 -0
- package/dist/lib/providers/azureOpenai.d.ts +1 -1
- package/dist/lib/providers/azureOpenai.js +1 -1
- package/dist/lib/providers/googleAiStudio.d.ts +1 -1
- package/dist/lib/providers/googleAiStudio.js +2 -2
- package/dist/lib/providers/googleVertex.d.ts +40 -0
- package/dist/lib/providers/googleVertex.js +330 -274
- package/dist/lib/providers/huggingFace.js +1 -1
- package/dist/lib/providers/mistral.d.ts +1 -1
- package/dist/lib/providers/mistral.js +2 -2
- package/dist/lib/providers/ollama.d.ts +4 -0
- package/dist/lib/providers/ollama.js +38 -18
- package/dist/lib/providers/openAI.d.ts +1 -1
- package/dist/lib/providers/openAI.js +2 -2
- package/dist/lib/providers/sagemaker/adaptive-semaphore.js +7 -4
- package/dist/lib/providers/sagemaker/client.js +13 -3
- package/dist/lib/providers/sagemaker/config.js +5 -1
- package/dist/lib/providers/sagemaker/detection.js +19 -9
- package/dist/lib/providers/sagemaker/errors.d.ts +8 -1
- package/dist/lib/providers/sagemaker/errors.js +103 -20
- package/dist/lib/providers/sagemaker/language-model.d.ts +3 -3
- package/dist/lib/providers/sagemaker/language-model.js +4 -4
- package/dist/lib/providers/sagemaker/parsers.js +14 -6
- package/dist/lib/providers/sagemaker/streaming.js +14 -3
- package/dist/lib/providers/sagemaker/types.d.ts +1 -1
- package/dist/lib/proxy/awsProxyIntegration.d.ts +23 -0
- package/dist/lib/proxy/awsProxyIntegration.js +285 -0
- package/dist/lib/proxy/proxyFetch.d.ts +9 -5
- package/dist/lib/proxy/proxyFetch.js +232 -98
- package/dist/lib/proxy/utils/noProxyUtils.d.ts +39 -0
- package/dist/lib/proxy/utils/noProxyUtils.js +149 -0
- package/dist/lib/sdk/toolRegistration.d.ts +1 -1
- package/dist/lib/types/cli.d.ts +80 -8
- package/dist/lib/types/contextTypes.js +2 -2
- package/dist/lib/types/generateTypes.d.ts +4 -6
- package/dist/lib/types/providers.d.ts +124 -19
- package/dist/lib/types/providers.js +6 -6
- package/dist/lib/types/streamTypes.d.ts +4 -6
- package/dist/lib/types/typeAliases.d.ts +1 -1
- package/dist/lib/utils/analyticsUtils.d.ts +33 -0
- package/dist/lib/utils/analyticsUtils.js +76 -0
- package/dist/lib/utils/errorHandling.js +4 -1
- package/dist/lib/utils/evaluationUtils.d.ts +27 -0
- package/dist/lib/utils/evaluationUtils.js +131 -0
- package/dist/lib/utils/optionsUtils.js +10 -1
- package/dist/lib/utils/performance.d.ts +1 -1
- package/dist/lib/utils/performance.js +15 -3
- package/dist/lib/utils/providerConfig.d.ts +1 -0
- package/dist/lib/utils/providerConfig.js +2 -1
- package/dist/lib/utils/providerHealth.d.ts +48 -0
- package/dist/lib/utils/providerHealth.js +221 -158
- package/dist/lib/utils/providerUtils.js +2 -2
- package/dist/lib/utils/timeout.js +8 -3
- package/dist/mcp/externalServerManager.js +14 -6
- package/dist/mcp/factory.js +1 -1
- package/dist/mcp/flexibleToolValidator.d.ts +50 -0
- package/dist/mcp/flexibleToolValidator.js +161 -0
- package/dist/mcp/index.d.ts +1 -1
- package/dist/mcp/index.js +1 -1
- package/dist/mcp/mcpCircuitBreaker.js +5 -1
- package/dist/mcp/mcpClientFactory.js +3 -0
- package/dist/mcp/registry.d.ts +3 -3
- package/dist/mcp/registry.js +3 -3
- package/dist/mcp/servers/aiProviders/aiAnalysisTools.js +5 -5
- package/dist/mcp/servers/aiProviders/aiWorkflowTools.js +6 -6
- package/dist/mcp/servers/utilities/utilityServer.js +1 -1
- package/dist/mcp/toolDiscoveryService.js +8 -2
- package/dist/mcp/toolRegistry.d.ts +2 -2
- package/dist/mcp/toolRegistry.js +29 -54
- package/dist/middleware/builtin/analytics.js +4 -4
- package/dist/middleware/builtin/guardrails.js +2 -2
- package/dist/middleware/registry.js +11 -2
- package/dist/models/modelRegistry.d.ts +1 -1
- package/dist/models/modelRegistry.js +3 -3
- package/dist/models/modelResolver.d.ts +1 -1
- package/dist/models/modelResolver.js +2 -2
- package/dist/neurolink.d.ts +118 -0
- package/dist/neurolink.js +814 -952
- package/dist/providers/amazonBedrock.d.ts +47 -6
- package/dist/providers/amazonBedrock.js +282 -23
- package/dist/providers/amazonSagemaker.d.ts +1 -1
- package/dist/providers/amazonSagemaker.js +12 -3
- package/dist/providers/anthropic.d.ts +1 -1
- package/dist/providers/anthropic.js +7 -6
- package/dist/providers/anthropicBaseProvider.d.ts +1 -1
- package/dist/providers/anthropicBaseProvider.js +4 -3
- package/dist/providers/aws/credentialProvider.d.ts +58 -0
- package/dist/providers/aws/credentialProvider.js +267 -0
- package/dist/providers/aws/credentialTester.d.ts +49 -0
- package/dist/providers/aws/credentialTester.js +394 -0
- package/dist/providers/azureOpenai.d.ts +1 -1
- package/dist/providers/azureOpenai.js +1 -1
- package/dist/providers/googleAiStudio.d.ts +1 -1
- package/dist/providers/googleAiStudio.js +2 -2
- package/dist/providers/googleVertex.d.ts +40 -0
- package/dist/providers/googleVertex.js +330 -274
- package/dist/providers/huggingFace.js +1 -1
- package/dist/providers/mistral.d.ts +1 -1
- package/dist/providers/mistral.js +2 -2
- package/dist/providers/ollama.d.ts +4 -0
- package/dist/providers/ollama.js +38 -18
- package/dist/providers/openAI.d.ts +1 -1
- package/dist/providers/openAI.js +2 -2
- package/dist/providers/sagemaker/adaptive-semaphore.js +7 -4
- package/dist/providers/sagemaker/client.js +13 -3
- package/dist/providers/sagemaker/config.js +5 -1
- package/dist/providers/sagemaker/detection.js +19 -9
- package/dist/providers/sagemaker/errors.d.ts +8 -1
- package/dist/providers/sagemaker/errors.js +103 -20
- package/dist/providers/sagemaker/language-model.d.ts +3 -3
- package/dist/providers/sagemaker/language-model.js +4 -4
- package/dist/providers/sagemaker/parsers.js +14 -6
- package/dist/providers/sagemaker/streaming.js +14 -3
- package/dist/providers/sagemaker/types.d.ts +1 -1
- package/dist/proxy/awsProxyIntegration.d.ts +23 -0
- package/dist/proxy/awsProxyIntegration.js +285 -0
- package/dist/proxy/proxyFetch.d.ts +9 -5
- package/dist/proxy/proxyFetch.js +232 -98
- package/dist/proxy/utils/noProxyUtils.d.ts +39 -0
- package/dist/proxy/utils/noProxyUtils.js +149 -0
- package/dist/sdk/toolRegistration.d.ts +1 -1
- package/dist/types/cli.d.ts +80 -8
- package/dist/types/contextTypes.js +2 -2
- package/dist/types/generateTypes.d.ts +4 -6
- package/dist/types/providers.d.ts +124 -19
- package/dist/types/providers.js +6 -6
- package/dist/types/streamTypes.d.ts +4 -6
- package/dist/types/typeAliases.d.ts +1 -1
- package/dist/utils/analyticsUtils.d.ts +33 -0
- package/dist/utils/analyticsUtils.js +76 -0
- package/dist/utils/errorHandling.js +4 -1
- package/dist/utils/evaluationUtils.d.ts +27 -0
- package/dist/utils/evaluationUtils.js +131 -0
- package/dist/utils/optionsUtils.js +10 -1
- package/dist/utils/performance.d.ts +1 -1
- package/dist/utils/performance.js +15 -3
- package/dist/utils/providerConfig.d.ts +1 -0
- package/dist/utils/providerConfig.js +2 -1
- package/dist/utils/providerHealth.d.ts +48 -0
- package/dist/utils/providerHealth.js +221 -158
- package/dist/utils/providerUtils.js +2 -2
- package/dist/utils/timeout.js +8 -3
- package/package.json +5 -1
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* NeuroLink
|
|
2
|
+
* NeuroLink Evaluation System
|
|
3
3
|
*/
|
|
4
4
|
import { logger } from "../utils/logger.js";
|
|
5
5
|
import { AIProviderFactory } from "./factory.js";
|
|
6
6
|
import { z } from "zod";
|
|
7
7
|
import { ProviderRegistry } from "../factories/providerRegistry.js";
|
|
8
8
|
import { modelConfig } from "./modelConfiguration.js";
|
|
9
|
+
import { normalizeEvaluationData } from "../utils/evaluationUtils.js";
|
|
9
10
|
// Zod schema for validation
|
|
10
|
-
const
|
|
11
|
+
const EvaluationSchema = z.object({
|
|
11
12
|
relevance: z.number().min(1).max(10),
|
|
12
13
|
accuracy: z.number().min(1).max(10),
|
|
13
14
|
completeness: z.number().min(1).max(10),
|
|
@@ -19,8 +20,8 @@ const UnifiedEvaluationSchema = z.object({
|
|
|
19
20
|
/**
|
|
20
21
|
* Get default evaluation when evaluation fails
|
|
21
22
|
*/
|
|
22
|
-
function
|
|
23
|
-
const functionTag = "
|
|
23
|
+
function getDefaultEvaluation(reason, evaluationTime, context) {
|
|
24
|
+
const functionTag = "getDefaultEvaluation";
|
|
24
25
|
logger.debug(`[${functionTag}] Creating default evaluation`, {
|
|
25
26
|
reason,
|
|
26
27
|
evaluationTime,
|
|
@@ -61,11 +62,14 @@ function getDefaultUnifiedEvaluation(reason, evaluationTime, context) {
|
|
|
61
62
|
/**
|
|
62
63
|
* Parse unified evaluation result from text response
|
|
63
64
|
*/
|
|
64
|
-
function
|
|
65
|
-
const functionTag = "
|
|
65
|
+
function parseEvaluationResult(response, context) {
|
|
66
|
+
const functionTag = "parseEvaluationResult";
|
|
66
67
|
try {
|
|
67
68
|
logger.debug(`[${functionTag}] Parsing evaluation response`, {
|
|
68
69
|
responseLength: response.length,
|
|
70
|
+
domain: context.primaryDomain,
|
|
71
|
+
hasToolUsage: !!context.toolUsage?.length,
|
|
72
|
+
hasConversationHistory: !!context.conversationHistory?.length,
|
|
69
73
|
});
|
|
70
74
|
// Try JSON parsing first
|
|
71
75
|
const jsonMatch = response.match(/\{[^}]*\}/s);
|
|
@@ -74,8 +78,11 @@ function parseUnifiedEvaluationResult(response, context) {
|
|
|
74
78
|
const parsed = JSON.parse(jsonMatch[0]);
|
|
75
79
|
return parsed;
|
|
76
80
|
}
|
|
77
|
-
catch (
|
|
78
|
-
logger.debug(`[${functionTag}] JSON parsing failed, trying regex
|
|
81
|
+
catch (jsonError) {
|
|
82
|
+
logger.debug(`[${functionTag}] JSON parsing failed, trying regex`, {
|
|
83
|
+
error: jsonError instanceof Error ? jsonError.message : String(jsonError),
|
|
84
|
+
jsonContent: jsonMatch[0].substring(0, 100), // First 100 chars for debugging
|
|
85
|
+
});
|
|
79
86
|
}
|
|
80
87
|
}
|
|
81
88
|
// Fallback to regex parsing
|
|
@@ -115,14 +122,49 @@ function parseUnifiedEvaluationResult(response, context) {
|
|
|
115
122
|
}
|
|
116
123
|
}
|
|
117
124
|
}
|
|
118
|
-
// Ensure minimum valid scores
|
|
119
|
-
|
|
125
|
+
// Ensure minimum valid scores and validate with schema
|
|
126
|
+
// Use context to enhance evaluation data
|
|
127
|
+
const evaluationData = {
|
|
120
128
|
relevance: result.relevance || 1,
|
|
121
129
|
accuracy: result.accuracy || 1,
|
|
122
130
|
completeness: result.completeness || 1,
|
|
123
131
|
overall: result.overall || 1,
|
|
124
|
-
|
|
132
|
+
domainAlignment: result.domainAlignment || (context.primaryDomain ? 5 : undefined), // Default to 5 if domain-specific
|
|
133
|
+
terminologyAccuracy: result.terminologyAccuracy || (context.primaryDomain ? 5 : undefined),
|
|
134
|
+
toolEffectiveness: result.toolEffectiveness || (context.toolUsage?.length ? 5 : undefined), // Default to 5 if tools were used
|
|
125
135
|
};
|
|
136
|
+
// Validate against schema
|
|
137
|
+
try {
|
|
138
|
+
const validated = EvaluationSchema.parse(evaluationData);
|
|
139
|
+
// Enhance reasoning with context information
|
|
140
|
+
let enhancedReasoning = result.reasoning || "No detailed reasoning provided";
|
|
141
|
+
if (context.primaryDomain) {
|
|
142
|
+
enhancedReasoning += ` (Domain: ${context.primaryDomain})`;
|
|
143
|
+
}
|
|
144
|
+
if (context.toolUsage?.length) {
|
|
145
|
+
enhancedReasoning += ` (Tools used: ${context.toolUsage.map((t) => t.toolName).join(", ")})`;
|
|
146
|
+
}
|
|
147
|
+
if (context.conversationHistory?.length) {
|
|
148
|
+
enhancedReasoning += ` (Conversation turns: ${context.conversationHistory.length})`;
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
...validated,
|
|
152
|
+
reasoning: enhancedReasoning,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
catch (validationError) {
|
|
156
|
+
logger.warn(`[${functionTag}] Schema validation failed, using fallback`, {
|
|
157
|
+
validationError,
|
|
158
|
+
originalData: evaluationData,
|
|
159
|
+
});
|
|
160
|
+
return {
|
|
161
|
+
relevance: Math.max(1, Math.min(10, result.relevance || 1)),
|
|
162
|
+
accuracy: Math.max(1, Math.min(10, result.accuracy || 1)),
|
|
163
|
+
completeness: Math.max(1, Math.min(10, result.completeness || 1)),
|
|
164
|
+
overall: Math.max(1, Math.min(10, result.overall || 1)),
|
|
165
|
+
reasoning: result.reasoning || "No detailed reasoning provided",
|
|
166
|
+
};
|
|
167
|
+
}
|
|
126
168
|
}
|
|
127
169
|
catch (error) {
|
|
128
170
|
logger.error(`[${functionTag}] Failed to parse evaluation result`, {
|
|
@@ -140,8 +182,8 @@ function parseUnifiedEvaluationResult(response, context) {
|
|
|
140
182
|
/**
|
|
141
183
|
* Main unified evaluation function
|
|
142
184
|
*/
|
|
143
|
-
export async function
|
|
144
|
-
const functionTag = "
|
|
185
|
+
export async function generateEvaluation(context) {
|
|
186
|
+
const functionTag = "generateEvaluation";
|
|
145
187
|
const startTime = Date.now();
|
|
146
188
|
logger.debug(`[${functionTag}] Starting evaluation`, {
|
|
147
189
|
hasUserQuery: !!context.userQuery,
|
|
@@ -161,7 +203,7 @@ export async function generateUnifiedEvaluation(context) {
|
|
|
161
203
|
const provider = await AIProviderFactory.createProvider(evaluationProvider, evaluationModel);
|
|
162
204
|
if (!provider) {
|
|
163
205
|
logger.debug(`[${functionTag}] No evaluation provider available, returning defaults`);
|
|
164
|
-
return
|
|
206
|
+
return getDefaultEvaluation("no-provider", Date.now() - startTime, context);
|
|
165
207
|
}
|
|
166
208
|
// Create evaluation prompt
|
|
167
209
|
const prompt = `
|
|
@@ -187,68 +229,78 @@ Reasoning: [Provide a detailed explanation of your evaluation, explaining why yo
|
|
|
187
229
|
const result = await provider.generate(prompt);
|
|
188
230
|
if (!result) {
|
|
189
231
|
logger.debug(`[${functionTag}] No response from provider`);
|
|
190
|
-
return
|
|
232
|
+
return getDefaultEvaluation("no-response", Date.now() - startTime, context);
|
|
191
233
|
}
|
|
192
234
|
// Extract text from result
|
|
193
235
|
const response = typeof result === "string" ? result : result?.content || String(result);
|
|
194
236
|
// Parse evaluation result
|
|
195
|
-
const parsed =
|
|
196
|
-
// Validate and enhance result
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
fallbackUsed: false,
|
|
206
|
-
costEstimate: 0.001, // Rough estimate
|
|
207
|
-
},
|
|
237
|
+
const parsed = parseEvaluationResult(response, context);
|
|
238
|
+
// Validate and enhance result using schema
|
|
239
|
+
const baseResult = {
|
|
240
|
+
relevance: parsed.relevance || 1,
|
|
241
|
+
accuracy: parsed.accuracy || 1,
|
|
242
|
+
completeness: parsed.completeness || 1,
|
|
243
|
+
overall: parsed.overall || 1,
|
|
244
|
+
domainAlignment: parsed.domainAlignment,
|
|
245
|
+
terminologyAccuracy: parsed.terminologyAccuracy,
|
|
246
|
+
toolEffectiveness: parsed.toolEffectiveness,
|
|
208
247
|
};
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
248
|
+
// Validate against schema before finalizing
|
|
249
|
+
try {
|
|
250
|
+
const validatedScores = EvaluationSchema.parse(baseResult);
|
|
251
|
+
const validatedResult = normalizeEvaluationData({
|
|
252
|
+
...parsed,
|
|
253
|
+
...validatedScores,
|
|
254
|
+
evaluationModel: `${evaluationProvider}/${evaluationModel}`,
|
|
255
|
+
evaluationTime: Date.now() - startTime,
|
|
256
|
+
evaluationProvider,
|
|
257
|
+
evaluationAttempt: 1,
|
|
258
|
+
evaluationConfig: {
|
|
259
|
+
mode: "standard",
|
|
260
|
+
fallbackUsed: false,
|
|
261
|
+
costEstimate: 0.001, // Rough estimate
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
logger.debug(`[${functionTag}] Schema validation passed`, {
|
|
265
|
+
validatedScores,
|
|
266
|
+
});
|
|
267
|
+
return validatedResult;
|
|
268
|
+
}
|
|
269
|
+
catch (validationError) {
|
|
270
|
+
logger.warn(`[${functionTag}] Schema validation failed in main evaluation`, {
|
|
271
|
+
validationError,
|
|
272
|
+
baseResult,
|
|
273
|
+
});
|
|
274
|
+
// Fallback with clamped values using normalizer
|
|
275
|
+
const validatedResult = normalizeEvaluationData({
|
|
276
|
+
...parsed,
|
|
277
|
+
relevance: parsed.relevance || 1,
|
|
278
|
+
accuracy: parsed.accuracy || 1,
|
|
279
|
+
completeness: parsed.completeness || 1,
|
|
280
|
+
overall: parsed.overall || 1,
|
|
281
|
+
evaluationModel: `${evaluationProvider}/${evaluationModel}`,
|
|
282
|
+
evaluationTime: Date.now() - startTime,
|
|
283
|
+
evaluationProvider,
|
|
284
|
+
evaluationAttempt: 1,
|
|
285
|
+
evaluationConfig: {
|
|
286
|
+
mode: "standard",
|
|
287
|
+
fallbackUsed: false,
|
|
288
|
+
costEstimate: 0.001, // Rough estimate
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
return validatedResult;
|
|
292
|
+
}
|
|
217
293
|
}
|
|
218
294
|
catch (error) {
|
|
219
295
|
logger.error(`[${functionTag}] Evaluation failed`, {
|
|
220
296
|
error: error instanceof Error ? error.message : String(error),
|
|
221
297
|
});
|
|
222
|
-
return
|
|
298
|
+
return getDefaultEvaluation(error instanceof Error ? error.message : "unknown-error", Date.now() - startTime, context);
|
|
223
299
|
}
|
|
224
300
|
}
|
|
225
|
-
//
|
|
226
|
-
export async function evaluateResponse(
|
|
227
|
-
|
|
228
|
-
let aiResponse;
|
|
229
|
-
let context;
|
|
230
|
-
if (typeof responseOrContext === "string") {
|
|
231
|
-
// Normal call: evaluateResponse(response, context, ...)
|
|
232
|
-
aiResponse = responseOrContext;
|
|
233
|
-
context = contextOrUserQuery;
|
|
234
|
-
}
|
|
235
|
-
else {
|
|
236
|
-
// Provider call pattern: evaluateResponse(contextObject, userQuery, ...)
|
|
237
|
-
context = responseOrContext;
|
|
238
|
-
aiResponse =
|
|
239
|
-
context?.aiResponse ||
|
|
240
|
-
context?.response ||
|
|
241
|
-
String(contextOrUserQuery || "");
|
|
242
|
-
}
|
|
243
|
-
const evalContext = {
|
|
244
|
-
userQuery: (typeof userQuery === "string" ? userQuery : "") ||
|
|
245
|
-
context?.userQuery ||
|
|
246
|
-
(typeof contextOrUserQuery === "string" ? contextOrUserQuery : "") ||
|
|
247
|
-
"Generated response",
|
|
248
|
-
aiResponse,
|
|
249
|
-
context: context,
|
|
250
|
-
};
|
|
251
|
-
return generateUnifiedEvaluation(evalContext);
|
|
301
|
+
// Simplified evaluation function
|
|
302
|
+
export async function evaluateResponse(context) {
|
|
303
|
+
return generateEvaluation(context);
|
|
252
304
|
}
|
|
253
305
|
// Export additional utilities
|
|
254
|
-
export {
|
|
306
|
+
export { getDefaultEvaluation, parseEvaluationResult };
|
|
@@ -1,4 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
interface ProviderModelConfig {
|
|
2
|
+
provider: string;
|
|
3
|
+
models: string[];
|
|
4
|
+
costPerToken?: number | {
|
|
5
|
+
input: number;
|
|
6
|
+
output: number;
|
|
7
|
+
};
|
|
8
|
+
requiresApiKey?: string[];
|
|
9
|
+
performance?: {
|
|
10
|
+
averageLatency?: number;
|
|
11
|
+
reliability?: number;
|
|
12
|
+
speed?: number;
|
|
13
|
+
quality?: number;
|
|
14
|
+
cost?: number;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
2
17
|
/**
|
|
3
18
|
* Dynamic provider configurations for evaluation
|
|
4
19
|
* Now uses configurable system instead of hardcoded values
|
|
@@ -22,7 +37,7 @@ export declare function sortProvidersByPreference(providers: ProviderModelConfig
|
|
|
22
37
|
* Estimate cost for a specific provider and token usage
|
|
23
38
|
* Now uses the configurable system
|
|
24
39
|
*/
|
|
25
|
-
export declare function estimateProviderCost(providerName: string,
|
|
40
|
+
export declare function estimateProviderCost(providerName: string, input: number, output: number): number;
|
|
26
41
|
/**
|
|
27
42
|
* Check if a provider is available (has required API keys)
|
|
28
43
|
* Now uses the configurable system
|
|
@@ -58,3 +73,4 @@ export declare function getProviderPerformanceAnalytics(): Record<string, {
|
|
|
58
73
|
* @param providerName - (Optional) The name of the provider to reset. If omitted, resets all providers.
|
|
59
74
|
*/
|
|
60
75
|
export declare function resetProviderMetrics(providerName?: string): void;
|
|
76
|
+
export {};
|
|
@@ -12,7 +12,7 @@ const providerMetrics = new Map();
|
|
|
12
12
|
function convertToLegacyFormat(config) {
|
|
13
13
|
return {
|
|
14
14
|
provider: config.provider,
|
|
15
|
-
models: config.models,
|
|
15
|
+
models: Object.keys(config.models || {}),
|
|
16
16
|
costPerToken: config.defaultCost,
|
|
17
17
|
requiresApiKey: config.requiredEnvVars,
|
|
18
18
|
performance: config.performance,
|
|
@@ -57,25 +57,27 @@ export function getAvailableProviders() {
|
|
|
57
57
|
*/
|
|
58
58
|
export function sortProvidersByPreference(providers, preferCheap = true) {
|
|
59
59
|
return providers.sort((a, b) => {
|
|
60
|
+
const aPerf = a.performance || { cost: 0, speed: 0, quality: 0 };
|
|
61
|
+
const bPerf = b.performance || { cost: 0, speed: 0, quality: 0 };
|
|
60
62
|
if (preferCheap) {
|
|
61
63
|
// Cost > Speed > Quality for cheap preference
|
|
62
|
-
if (
|
|
63
|
-
return
|
|
64
|
+
if ((aPerf.cost || 0) !== (bPerf.cost || 0)) {
|
|
65
|
+
return (bPerf.cost || 0) - (aPerf.cost || 0);
|
|
64
66
|
}
|
|
65
|
-
if (
|
|
66
|
-
return
|
|
67
|
+
if ((aPerf.speed || 0) !== (bPerf.speed || 0)) {
|
|
68
|
+
return (bPerf.speed || 0) - (aPerf.speed || 0);
|
|
67
69
|
}
|
|
68
|
-
return
|
|
70
|
+
return (bPerf.quality || 0) - (aPerf.quality || 0);
|
|
69
71
|
}
|
|
70
72
|
else {
|
|
71
73
|
// Quality > Speed > Cost for quality preference
|
|
72
|
-
if (
|
|
73
|
-
return
|
|
74
|
+
if ((aPerf.quality || 0) !== (bPerf.quality || 0)) {
|
|
75
|
+
return (bPerf.quality || 0) - (aPerf.quality || 0);
|
|
74
76
|
}
|
|
75
|
-
if (
|
|
76
|
-
return
|
|
77
|
+
if ((aPerf.speed || 0) !== (bPerf.speed || 0)) {
|
|
78
|
+
return (bPerf.speed || 0) - (aPerf.speed || 0);
|
|
77
79
|
}
|
|
78
|
-
return
|
|
80
|
+
return (bPerf.cost || 0) - (aPerf.cost || 0);
|
|
79
81
|
}
|
|
80
82
|
});
|
|
81
83
|
}
|
|
@@ -83,12 +85,12 @@ export function sortProvidersByPreference(providers, preferCheap = true) {
|
|
|
83
85
|
* Estimate cost for a specific provider and token usage
|
|
84
86
|
* Now uses the configurable system
|
|
85
87
|
*/
|
|
86
|
-
export function estimateProviderCost(providerName,
|
|
88
|
+
export function estimateProviderCost(providerName, input, output) {
|
|
87
89
|
const costInfo = modelConfig.getCostInfo(providerName);
|
|
88
90
|
if (!costInfo) {
|
|
89
91
|
return 0;
|
|
90
92
|
}
|
|
91
|
-
return
|
|
93
|
+
return input * costInfo.input + output * costInfo.output;
|
|
92
94
|
}
|
|
93
95
|
/**
|
|
94
96
|
* Check if a provider is available (has required API keys)
|
package/dist/lib/core/factory.js
CHANGED
|
@@ -55,12 +55,18 @@ export class AIProviderFactory {
|
|
|
55
55
|
* @returns AIProvider instance
|
|
56
56
|
*/
|
|
57
57
|
static async createProvider(providerName, modelName, enableMCP = true, sdk) {
|
|
58
|
-
const functionTag = "
|
|
58
|
+
const functionTag = "AIProviderFactory.createProvider";
|
|
59
59
|
// Providers are registered via ProviderFactory.initialize() on first use
|
|
60
60
|
logger.debug(`[${functionTag}] Provider creation started`, {
|
|
61
61
|
providerName,
|
|
62
62
|
modelName: modelName || "default",
|
|
63
63
|
enableMCP,
|
|
64
|
+
environmentVariables: {
|
|
65
|
+
BEDROCK_MODEL: process.env.BEDROCK_MODEL || "not set",
|
|
66
|
+
BEDROCK_MODEL_ID: process.env.BEDROCK_MODEL_ID || "not set",
|
|
67
|
+
VERTEX_MODEL: process.env.VERTEX_MODEL || "not set",
|
|
68
|
+
OPENAI_MODEL: process.env.OPENAI_MODEL || "not set",
|
|
69
|
+
},
|
|
64
70
|
});
|
|
65
71
|
try {
|
|
66
72
|
// DYNAMIC MODEL PROVIDER STATUS (2025): Enhanced with timeout handling
|
|
@@ -73,12 +79,57 @@ export class AIProviderFactory {
|
|
|
73
79
|
//
|
|
74
80
|
// The dynamic model provider now provides reliable functionality without hanging
|
|
75
81
|
let resolvedModelName = modelName;
|
|
76
|
-
//
|
|
82
|
+
// PRIORITY 1: Check environment variables BEFORE dynamic resolution
|
|
77
83
|
if (!modelName || modelName === "default") {
|
|
84
|
+
logger.debug(`[${functionTag}] Checking environment variables for provider: ${providerName}`);
|
|
85
|
+
// Check for provider-specific environment variables first
|
|
86
|
+
if (providerName.toLowerCase().includes("bedrock")) {
|
|
87
|
+
const envModel = process.env.BEDROCK_MODEL || process.env.BEDROCK_MODEL_ID;
|
|
88
|
+
if (envModel) {
|
|
89
|
+
resolvedModelName = envModel;
|
|
90
|
+
logger.debug(`[${functionTag}] Environment variable found for Bedrock`, {
|
|
91
|
+
envVariable: process.env.BEDROCK_MODEL
|
|
92
|
+
? "BEDROCK_MODEL"
|
|
93
|
+
: "BEDROCK_MODEL_ID",
|
|
94
|
+
resolvedModel: envModel,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
logger.debug(`[${functionTag}] No Bedrock environment variables found (BEDROCK_MODEL, BEDROCK_MODEL_ID)`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
else if (providerName.toLowerCase().includes("vertex")) {
|
|
102
|
+
const envModel = process.env.VERTEX_MODEL;
|
|
103
|
+
if (envModel) {
|
|
104
|
+
resolvedModelName = envModel;
|
|
105
|
+
logger.debug(`[${functionTag}] Environment variable found for Vertex`, {
|
|
106
|
+
envVariable: "VERTEX_MODEL",
|
|
107
|
+
resolvedModel: envModel,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
logger.debug(`[${functionTag}] No Vertex environment variables found (VERTEX_MODEL)`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
logger.debug(`[${functionTag}] Provider ${providerName} - no environment variable check implemented`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
logger.debug(`[${functionTag}] Skipping environment variable check - explicit model provided: ${modelName}`);
|
|
120
|
+
}
|
|
121
|
+
// PRIORITY 2: Enable dynamic model resolution only if no env var found
|
|
122
|
+
if ((!resolvedModelName || resolvedModelName === "default") &&
|
|
123
|
+
(!modelName || modelName === "default")) {
|
|
124
|
+
logger.debug(`[${functionTag}] Attempting dynamic model resolution`, {
|
|
125
|
+
currentResolvedModel: resolvedModelName || "none",
|
|
126
|
+
reason: "No environment variable found and no explicit model provided",
|
|
127
|
+
});
|
|
78
128
|
try {
|
|
79
129
|
const normalizedProvider = this.normalizeProviderName(providerName);
|
|
80
130
|
// Initialize with timeout protection - won't hang anymore
|
|
81
131
|
if (dynamicModelProvider.needsRefresh()) {
|
|
132
|
+
logger.debug(`[${functionTag}] Dynamic model provider needs refresh - initializing`);
|
|
82
133
|
await this.initializeDynamicProviderWithTimeout();
|
|
83
134
|
}
|
|
84
135
|
const dynamicModel = dynamicModelProvider.resolveModel(normalizedProvider, modelName || undefined);
|
|
@@ -92,6 +143,12 @@ export class AIProviderFactory {
|
|
|
92
143
|
pricing: dynamicModel.pricing.input,
|
|
93
144
|
});
|
|
94
145
|
}
|
|
146
|
+
else {
|
|
147
|
+
logger.debug(`[${functionTag}] Dynamic model resolution returned null`, {
|
|
148
|
+
provider: normalizedProvider,
|
|
149
|
+
requestedModel: modelName || "default",
|
|
150
|
+
});
|
|
151
|
+
}
|
|
95
152
|
}
|
|
96
153
|
catch (resolveError) {
|
|
97
154
|
logger.debug(`[${functionTag}] Dynamic model resolution failed, using static fallback`, {
|
|
@@ -102,6 +159,12 @@ export class AIProviderFactory {
|
|
|
102
159
|
// Continue with static model name - no functionality loss
|
|
103
160
|
}
|
|
104
161
|
}
|
|
162
|
+
else {
|
|
163
|
+
logger.debug(`[${functionTag}] Skipping dynamic model resolution`, {
|
|
164
|
+
resolvedModelName: resolvedModelName || "none",
|
|
165
|
+
reason: "Model already resolved from environment variables or explicit parameter",
|
|
166
|
+
});
|
|
167
|
+
}
|
|
105
168
|
// CRITICAL FIX: Initialize providers before using them
|
|
106
169
|
await ProviderRegistry.registerAllProviders();
|
|
107
170
|
// PURE FACTORY PATTERN: No switch statements - use ProviderFactory exclusively
|
|
@@ -109,8 +172,18 @@ export class AIProviderFactory {
|
|
|
109
172
|
const finalModelName = resolvedModelName === "default" || resolvedModelName === null
|
|
110
173
|
? undefined
|
|
111
174
|
: resolvedModelName;
|
|
175
|
+
logger.debug(`[${functionTag}] Final provider configuration`, {
|
|
176
|
+
originalProviderName: providerName,
|
|
177
|
+
normalizedProviderName: normalizedName,
|
|
178
|
+
originalModelName: modelName || "not provided",
|
|
179
|
+
resolvedModelName: resolvedModelName || "not resolved",
|
|
180
|
+
finalModelName: finalModelName || "using provider default",
|
|
181
|
+
});
|
|
112
182
|
// Create provider with enhanced SDK
|
|
113
183
|
const provider = await ProviderFactory.createProvider(normalizedName, finalModelName, sdk);
|
|
184
|
+
// Summary logging in format expected by debugging tools
|
|
185
|
+
logger.debug(`[AIProviderFactory] Provider creation completed { providerName: '${normalizedName}', modelName: '${finalModelName}' }`);
|
|
186
|
+
logger.debug(`[AIProviderFactory] Resolved model: ${finalModelName}`);
|
|
114
187
|
logger.debug(componentIdentifier, "Pure factory pattern provider created", {
|
|
115
188
|
providerName: normalizedName,
|
|
116
189
|
modelName: finalModelName,
|
|
@@ -153,7 +226,7 @@ export class AIProviderFactory {
|
|
|
153
226
|
* @returns AIProvider instance
|
|
154
227
|
*/
|
|
155
228
|
static async createProviderWithModel(provider, model) {
|
|
156
|
-
const functionTag = "
|
|
229
|
+
const functionTag = "AIProviderFactory.createProviderWithModel";
|
|
157
230
|
logger.debug(`[${functionTag}] Provider model creation started`, {
|
|
158
231
|
provider,
|
|
159
232
|
model,
|
|
@@ -214,7 +287,7 @@ export class AIProviderFactory {
|
|
|
214
287
|
* @returns Object with primary and fallback providers
|
|
215
288
|
*/
|
|
216
289
|
static async createProviderWithFallback(primaryProvider, fallbackProvider, modelName, enableMCP = true) {
|
|
217
|
-
const functionTag = "
|
|
290
|
+
const functionTag = "AIProviderFactory.createProviderWithFallback";
|
|
218
291
|
logger.debug(`[${functionTag}] Fallback provider setup started`, {
|
|
219
292
|
primaryProvider,
|
|
220
293
|
fallbackProvider,
|
|
@@ -132,6 +132,50 @@ export declare class ModelConfigurationManager {
|
|
|
132
132
|
private lastUpdated;
|
|
133
133
|
private constructor();
|
|
134
134
|
static getInstance(): ModelConfigurationManager;
|
|
135
|
+
/**
|
|
136
|
+
* Create Google AI provider configuration
|
|
137
|
+
*/
|
|
138
|
+
private createGoogleAIConfig;
|
|
139
|
+
/**
|
|
140
|
+
* Create Google Vertex AI provider configuration
|
|
141
|
+
*/
|
|
142
|
+
private createVertexConfig;
|
|
143
|
+
/**
|
|
144
|
+
* Create OpenAI provider configuration
|
|
145
|
+
*/
|
|
146
|
+
private createOpenAIConfig;
|
|
147
|
+
/**
|
|
148
|
+
* Create all provider configurations - centralized approach
|
|
149
|
+
*/
|
|
150
|
+
private createAllProviderConfigs;
|
|
151
|
+
/**
|
|
152
|
+
* Create Anthropic provider configuration
|
|
153
|
+
*/
|
|
154
|
+
private createAnthropicConfig;
|
|
155
|
+
/**
|
|
156
|
+
* Create Vertex alternative provider configuration
|
|
157
|
+
*/
|
|
158
|
+
private createVertexAlternativeConfig;
|
|
159
|
+
/**
|
|
160
|
+
* Create Bedrock provider configuration
|
|
161
|
+
*/
|
|
162
|
+
private createBedrockConfig;
|
|
163
|
+
/**
|
|
164
|
+
* Create Azure provider configuration
|
|
165
|
+
*/
|
|
166
|
+
private createAzureConfig;
|
|
167
|
+
/**
|
|
168
|
+
* Create Ollama provider configuration
|
|
169
|
+
*/
|
|
170
|
+
private createOllamaConfig;
|
|
171
|
+
/**
|
|
172
|
+
* Create HuggingFace provider configuration
|
|
173
|
+
*/
|
|
174
|
+
private createHuggingFaceConfig;
|
|
175
|
+
/**
|
|
176
|
+
* Create Mistral provider configuration
|
|
177
|
+
*/
|
|
178
|
+
private createMistralConfig;
|
|
135
179
|
/**
|
|
136
180
|
* Load default configurations (replaces hardcoded values)
|
|
137
181
|
*/
|
|
@@ -174,6 +218,25 @@ export declare class ModelConfigurationManager {
|
|
|
174
218
|
* Update provider configuration (runtime updates)
|
|
175
219
|
*/
|
|
176
220
|
updateProviderConfig(provider: string, config: ProviderConfig): void;
|
|
221
|
+
/**
|
|
222
|
+
* Parse YAML content with basic support
|
|
223
|
+
* For full YAML support, would need a proper YAML library
|
|
224
|
+
*/
|
|
225
|
+
private parseYAMLContent;
|
|
226
|
+
/**
|
|
227
|
+
* Convert simple YAML to JSON object
|
|
228
|
+
* Handles basic key-value pairs only
|
|
229
|
+
*/
|
|
230
|
+
private convertSimpleYAMLToJSON;
|
|
231
|
+
/**
|
|
232
|
+
* Process a single YAML line
|
|
233
|
+
* Returns key-value pair if valid, null otherwise
|
|
234
|
+
*/
|
|
235
|
+
private processYAMLLine;
|
|
236
|
+
/**
|
|
237
|
+
* Convert YAML value to appropriate JavaScript type
|
|
238
|
+
*/
|
|
239
|
+
private convertYAMLValue;
|
|
177
240
|
/**
|
|
178
241
|
* Load configurations from external source
|
|
179
242
|
*/
|