@juspay/neurolink 3.0.0 → 4.0.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 +62 -4
- package/README.md +235 -2
- 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 +256 -27
- 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/function-calling.js +11 -3
- package/dist/lib/mcp/logging.js +5 -0
- package/dist/lib/mcp/neurolink-mcp-client.js +2 -1
- package/dist/lib/mcp/orchestrator.js +18 -9
- package/dist/lib/mcp/registry.d.ts +49 -16
- package/dist/lib/mcp/registry.js +80 -6
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.js +5 -4
- 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/unified-mcp.js +6 -1
- package/dist/lib/mcp/unified-registry.d.ts +12 -4
- package/dist/lib/mcp/unified-registry.js +17 -4
- package/dist/lib/neurolink.d.ts +28 -0
- package/dist/lib/neurolink.js +48 -4
- 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/function-calling.js +11 -3
- package/dist/mcp/logging.js +5 -0
- package/dist/mcp/neurolink-mcp-client.js +2 -1
- package/dist/mcp/orchestrator.js +18 -9
- package/dist/mcp/registry.d.ts +49 -16
- package/dist/mcp/registry.js +80 -6
- 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/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/unified-mcp.js +6 -1
- package/dist/mcp/unified-registry.d.ts +12 -4
- package/dist/mcp/unified-registry.js +17 -4
- package/dist/neurolink.d.ts +28 -0
- package/dist/neurolink.js +48 -4
- 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 +18 -2
package/dist/neurolink.d.ts
CHANGED
|
@@ -8,20 +8,36 @@ import type { AIProviderName } from "./core/types.js";
|
|
|
8
8
|
export interface TextGenerationOptions {
|
|
9
9
|
prompt: string;
|
|
10
10
|
provider?: "openai" | "bedrock" | "vertex" | "anthropic" | "azure" | "google-ai" | "huggingface" | "ollama" | "mistral" | "auto";
|
|
11
|
+
model?: string;
|
|
11
12
|
temperature?: number;
|
|
12
13
|
maxTokens?: number;
|
|
13
14
|
systemPrompt?: string;
|
|
14
15
|
schema?: any;
|
|
15
16
|
timeout?: number | string;
|
|
16
17
|
disableTools?: boolean;
|
|
18
|
+
enableAnalytics?: boolean;
|
|
19
|
+
enableEvaluation?: boolean;
|
|
20
|
+
context?: Record<string, any>;
|
|
21
|
+
evaluationDomain?: string;
|
|
22
|
+
toolUsageContext?: string;
|
|
23
|
+
conversationHistory?: Array<{
|
|
24
|
+
role: string;
|
|
25
|
+
content: string;
|
|
26
|
+
}>;
|
|
17
27
|
}
|
|
18
28
|
export interface StreamTextOptions {
|
|
19
29
|
prompt: string;
|
|
20
30
|
provider?: "openai" | "bedrock" | "vertex" | "anthropic" | "azure" | "google-ai" | "huggingface" | "ollama" | "mistral" | "auto";
|
|
31
|
+
model?: string;
|
|
21
32
|
temperature?: number;
|
|
22
33
|
maxTokens?: number;
|
|
23
34
|
systemPrompt?: string;
|
|
35
|
+
schema?: any;
|
|
24
36
|
timeout?: number | string;
|
|
37
|
+
disableTools?: boolean;
|
|
38
|
+
enableAnalytics?: boolean;
|
|
39
|
+
enableEvaluation?: boolean;
|
|
40
|
+
context?: Record<string, any>;
|
|
25
41
|
}
|
|
26
42
|
export interface TextGenerationResult {
|
|
27
43
|
content: string;
|
|
@@ -113,4 +129,16 @@ export declare class NeuroLink {
|
|
|
113
129
|
hasServer: boolean;
|
|
114
130
|
}[];
|
|
115
131
|
}>;
|
|
132
|
+
/**
|
|
133
|
+
* Alias for generateText() - CLI-SDK consistency
|
|
134
|
+
* @param options - Text generation options
|
|
135
|
+
* @returns Promise resolving to text generation result
|
|
136
|
+
*/
|
|
137
|
+
generate(options: TextGenerationOptions): Promise<TextGenerationResult>;
|
|
138
|
+
/**
|
|
139
|
+
* Short alias for generateText() - CLI-SDK consistency
|
|
140
|
+
* @param options - Text generation options
|
|
141
|
+
* @returns Promise resolving to text generation result
|
|
142
|
+
*/
|
|
143
|
+
gen(options: TextGenerationOptions): Promise<TextGenerationResult>;
|
|
116
144
|
}
|
package/dist/neurolink.js
CHANGED
|
@@ -127,7 +127,7 @@ export class NeuroLink {
|
|
|
127
127
|
// Create tool-aware system prompt
|
|
128
128
|
const enhancedSystemPrompt = this.createToolAwareSystemPrompt(options.systemPrompt, availableTools);
|
|
129
129
|
// Create provider with MCP enabled using best provider function
|
|
130
|
-
const provider = await AIProviderFactory.createBestProvider(providerName,
|
|
130
|
+
const provider = await AIProviderFactory.createBestProvider(providerName, options.model, true);
|
|
131
131
|
// Generate text with automatic tool detection
|
|
132
132
|
const result = await provider.generateText({
|
|
133
133
|
prompt: options.prompt,
|
|
@@ -135,6 +135,14 @@ export class NeuroLink {
|
|
|
135
135
|
maxTokens: options.maxTokens,
|
|
136
136
|
systemPrompt: enhancedSystemPrompt,
|
|
137
137
|
timeout: options.timeout,
|
|
138
|
+
// NEW: Pass enhancement options
|
|
139
|
+
enableAnalytics: options.enableAnalytics,
|
|
140
|
+
enableEvaluation: options.enableEvaluation,
|
|
141
|
+
context: options.context,
|
|
142
|
+
// NEW: Lighthouse-compatible domain-aware evaluation
|
|
143
|
+
evaluationDomain: options.evaluationDomain,
|
|
144
|
+
toolUsageContext: options.toolUsageContext,
|
|
145
|
+
conversationHistory: options.conversationHistory,
|
|
138
146
|
}, options.schema);
|
|
139
147
|
if (!result) {
|
|
140
148
|
throw new Error("No response received from AI provider");
|
|
@@ -166,6 +174,9 @@ export class NeuroLink {
|
|
|
166
174
|
toolsUsed: metadata.toolsUsed || [],
|
|
167
175
|
enhancedWithTools: metadata.enhancedWithTools || false,
|
|
168
176
|
availableTools: availableTools.length > 0 ? availableTools : undefined,
|
|
177
|
+
// NEW: Preserve enhancement data from provider
|
|
178
|
+
...(result.analytics && { analytics: result.analytics }),
|
|
179
|
+
...(result.evaluation && { evaluation: result.evaluation }),
|
|
169
180
|
};
|
|
170
181
|
}
|
|
171
182
|
catch (error) {
|
|
@@ -217,13 +228,22 @@ export class NeuroLink {
|
|
|
217
228
|
logger.debug(`[${functionTag}] Attempting provider`, {
|
|
218
229
|
provider: providerName,
|
|
219
230
|
});
|
|
220
|
-
const provider = await AIProviderFactory.createProvider(providerName);
|
|
231
|
+
const provider = await AIProviderFactory.createProvider(providerName, options.model);
|
|
221
232
|
const result = await provider.generateText({
|
|
222
233
|
prompt: options.prompt,
|
|
234
|
+
model: options.model,
|
|
223
235
|
temperature: options.temperature,
|
|
224
236
|
maxTokens: options.maxTokens,
|
|
225
237
|
systemPrompt: options.systemPrompt,
|
|
226
238
|
timeout: options.timeout,
|
|
239
|
+
// NEW: Pass enhancement options
|
|
240
|
+
enableAnalytics: options.enableAnalytics,
|
|
241
|
+
enableEvaluation: options.enableEvaluation,
|
|
242
|
+
context: options.context,
|
|
243
|
+
// NEW: Lighthouse-compatible domain-aware evaluation
|
|
244
|
+
evaluationDomain: options.evaluationDomain,
|
|
245
|
+
toolUsageContext: options.toolUsageContext,
|
|
246
|
+
conversationHistory: options.conversationHistory,
|
|
227
247
|
}, options.schema);
|
|
228
248
|
if (!result) {
|
|
229
249
|
throw new Error("No response received from AI provider");
|
|
@@ -249,6 +269,9 @@ export class NeuroLink {
|
|
|
249
269
|
provider: providerName,
|
|
250
270
|
usage: result.usage,
|
|
251
271
|
responseTime,
|
|
272
|
+
// NEW: Preserve enhancement data from provider
|
|
273
|
+
...(result.analytics && { analytics: result.analytics }),
|
|
274
|
+
...(result.evaluation && { evaluation: result.evaluation }),
|
|
252
275
|
};
|
|
253
276
|
}
|
|
254
277
|
catch (error) {
|
|
@@ -343,9 +366,10 @@ Note: Tool integration is currently in development. Please provide helpful respo
|
|
|
343
366
|
logger.debug(`[${functionTag}] Attempting provider`, {
|
|
344
367
|
provider: providerName,
|
|
345
368
|
});
|
|
346
|
-
const provider = await AIProviderFactory.createProvider(providerName);
|
|
369
|
+
const provider = await AIProviderFactory.createProvider(providerName, options.model);
|
|
347
370
|
const result = await provider.streamText({
|
|
348
371
|
prompt: options.prompt,
|
|
372
|
+
model: options.model,
|
|
349
373
|
temperature: options.temperature,
|
|
350
374
|
maxTokens: options.maxTokens,
|
|
351
375
|
systemPrompt: options.systemPrompt,
|
|
@@ -407,7 +431,11 @@ Note: Tool integration is currently in development. Please provide helpful respo
|
|
|
407
431
|
async testProvider(providerName, testPrompt = "test") {
|
|
408
432
|
try {
|
|
409
433
|
const provider = await AIProviderFactory.createProvider(providerName);
|
|
410
|
-
await provider.generateText(
|
|
434
|
+
await provider.generateText({
|
|
435
|
+
prompt: testPrompt,
|
|
436
|
+
enableAnalytics: false,
|
|
437
|
+
enableEvaluation: false,
|
|
438
|
+
});
|
|
411
439
|
return true;
|
|
412
440
|
}
|
|
413
441
|
catch (error) {
|
|
@@ -444,4 +472,20 @@ Note: Tool integration is currently in development. Please provide helpful respo
|
|
|
444
472
|
})),
|
|
445
473
|
};
|
|
446
474
|
}
|
|
475
|
+
/**
|
|
476
|
+
* Alias for generateText() - CLI-SDK consistency
|
|
477
|
+
* @param options - Text generation options
|
|
478
|
+
* @returns Promise resolving to text generation result
|
|
479
|
+
*/
|
|
480
|
+
async generate(options) {
|
|
481
|
+
return this.generateText(options);
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Short alias for generateText() - CLI-SDK consistency
|
|
485
|
+
* @param options - Text generation options
|
|
486
|
+
* @returns Promise resolving to text generation result
|
|
487
|
+
*/
|
|
488
|
+
async gen(options) {
|
|
489
|
+
return this.generateText(options);
|
|
490
|
+
}
|
|
447
491
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Integrates direct tools with AI providers for true agent functionality
|
|
4
4
|
*/
|
|
5
5
|
import { type GenerateTextResult, type StreamTextResult, type ToolSet } from "ai";
|
|
6
|
-
import type { AIProvider, TextGenerationOptions,
|
|
6
|
+
import type { AIProvider, TextGenerationOptions, EnhancedGenerateTextResult, EnhancedStreamTextOptions } from "../core/types.js";
|
|
7
7
|
/**
|
|
8
8
|
* Agent configuration options
|
|
9
9
|
*/
|
|
@@ -30,6 +30,7 @@ interface AgentConfig {
|
|
|
30
30
|
export declare class AgentEnhancedProvider implements AIProvider {
|
|
31
31
|
private config;
|
|
32
32
|
private model;
|
|
33
|
+
private resolvedModelName;
|
|
33
34
|
private mcpSystem;
|
|
34
35
|
private mcpInitialized;
|
|
35
36
|
private mcpInitializing;
|
|
@@ -45,7 +46,7 @@ export declare class AgentEnhancedProvider implements AIProvider {
|
|
|
45
46
|
*/
|
|
46
47
|
private getCombinedTools;
|
|
47
48
|
generateText(optionsOrPrompt: TextGenerationOptions | string): Promise<GenerateTextResult<ToolSet, unknown> | null>;
|
|
48
|
-
streamText(optionsOrPrompt:
|
|
49
|
+
streamText(optionsOrPrompt: EnhancedStreamTextOptions | string): Promise<StreamTextResult<ToolSet, unknown> | null>;
|
|
49
50
|
/**
|
|
50
51
|
* Determine if we should force tool usage based on prompt patterns
|
|
51
52
|
*/
|
|
@@ -68,6 +69,14 @@ export declare class AgentEnhancedProvider implements AIProvider {
|
|
|
68
69
|
* Create multiple agent providers for comparison
|
|
69
70
|
*/
|
|
70
71
|
static createMultiProviderAgents(): Record<string, AgentEnhancedProvider>;
|
|
72
|
+
/**
|
|
73
|
+
* Alias for generateText() - CLI-SDK consistency
|
|
74
|
+
*/
|
|
75
|
+
generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateTextResult | null>;
|
|
76
|
+
/**
|
|
77
|
+
* Short alias for generateText() - CLI-SDK consistency
|
|
78
|
+
*/
|
|
79
|
+
gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateTextResult | null>;
|
|
71
80
|
}
|
|
72
81
|
/**
|
|
73
82
|
* Helper function to create agent provider
|
|
@@ -7,9 +7,13 @@ import { google } from "@ai-sdk/google";
|
|
|
7
7
|
import { openai } from "@ai-sdk/openai";
|
|
8
8
|
import { anthropic } from "@ai-sdk/anthropic";
|
|
9
9
|
import { directAgentTools, getToolsForCategory, } from "../agent/direct-tools.js";
|
|
10
|
+
import { StreamingEnhancer, StreamingMonitor, } from "../utils/streaming-utils.js";
|
|
10
11
|
import { UnifiedMCPSystem } from "../mcp/unified-mcp.js";
|
|
11
12
|
import { mcpLogger } from "../mcp/logging.js";
|
|
12
13
|
import { parseTimeout } from "../utils/timeout.js";
|
|
14
|
+
import { evaluateResponse } from "../core/evaluation.js";
|
|
15
|
+
import { createAnalytics } from "../core/analytics.js";
|
|
16
|
+
import { logger } from "../utils/logger.js";
|
|
13
17
|
/**
|
|
14
18
|
* Agent-Enhanced Provider Class
|
|
15
19
|
* Provides AI generation with tool calling capabilities
|
|
@@ -18,6 +22,7 @@ import { parseTimeout } from "../utils/timeout.js";
|
|
|
18
22
|
export class AgentEnhancedProvider {
|
|
19
23
|
config;
|
|
20
24
|
model;
|
|
25
|
+
resolvedModelName = "default";
|
|
21
26
|
mcpSystem = null;
|
|
22
27
|
mcpInitialized = false;
|
|
23
28
|
mcpInitializing = false;
|
|
@@ -35,7 +40,7 @@ export class AgentEnhancedProvider {
|
|
|
35
40
|
},
|
|
36
41
|
...config,
|
|
37
42
|
};
|
|
38
|
-
// Initialize the AI model based on provider
|
|
43
|
+
// Initialize the AI model based on provider and store resolved model name
|
|
39
44
|
this.model = this.createModel();
|
|
40
45
|
// Initialize MCP registry if enabled
|
|
41
46
|
if (this.config.enableMCP) {
|
|
@@ -46,11 +51,16 @@ export class AgentEnhancedProvider {
|
|
|
46
51
|
const { provider, model } = this.config;
|
|
47
52
|
switch (provider) {
|
|
48
53
|
case "google-ai":
|
|
49
|
-
|
|
54
|
+
this.resolvedModelName =
|
|
55
|
+
model || process.env.GOOGLE_AI_MODEL || "gemini-2.5-flash";
|
|
56
|
+
return google(this.resolvedModelName);
|
|
50
57
|
case "openai":
|
|
51
|
-
|
|
58
|
+
this.resolvedModelName = model || process.env.OPENAI_MODEL || "gpt-4o";
|
|
59
|
+
return openai(this.resolvedModelName);
|
|
52
60
|
case "anthropic":
|
|
53
|
-
|
|
61
|
+
this.resolvedModelName =
|
|
62
|
+
model || process.env.ANTHROPIC_MODEL || "claude-3-5-sonnet-20241022";
|
|
63
|
+
return anthropic(this.resolvedModelName);
|
|
54
64
|
default:
|
|
55
65
|
throw new Error(`Unsupported provider: ${provider}`);
|
|
56
66
|
}
|
|
@@ -249,6 +259,7 @@ export class AgentEnhancedProvider {
|
|
|
249
259
|
return { ...directTools, ...mcpTools };
|
|
250
260
|
}
|
|
251
261
|
async generateText(optionsOrPrompt) {
|
|
262
|
+
const startTime = Date.now();
|
|
252
263
|
const options = typeof optionsOrPrompt === "string"
|
|
253
264
|
? { prompt: optionsOrPrompt }
|
|
254
265
|
: optionsOrPrompt;
|
|
@@ -352,6 +363,14 @@ export class AgentEnhancedProvider {
|
|
|
352
363
|
};
|
|
353
364
|
}
|
|
354
365
|
}
|
|
366
|
+
// Add analytics if enabled
|
|
367
|
+
if (options.enableAnalytics) {
|
|
368
|
+
result.analytics = createAnalytics(this.config.provider, this.resolvedModelName, result, Date.now() - startTime, options.context);
|
|
369
|
+
}
|
|
370
|
+
// Add evaluation if enabled
|
|
371
|
+
if (options.enableEvaluation) {
|
|
372
|
+
result.evaluation = await evaluateResponse(prompt, result.text, options.context);
|
|
373
|
+
}
|
|
355
374
|
// Return the full result - the AI SDK has already handled tool execution and integration
|
|
356
375
|
return result;
|
|
357
376
|
}
|
|
@@ -364,7 +383,15 @@ export class AgentEnhancedProvider {
|
|
|
364
383
|
const options = typeof optionsOrPrompt === "string"
|
|
365
384
|
? { prompt: optionsOrPrompt }
|
|
366
385
|
: optionsOrPrompt;
|
|
367
|
-
const { prompt, temperature = 0.7, maxTokens = 1000, systemPrompt, timeout,
|
|
386
|
+
const { prompt, temperature = 0.7, maxTokens = 1000, systemPrompt, timeout,
|
|
387
|
+
// Phase 2: Enhanced streaming options
|
|
388
|
+
enableProgressTracking, progressCallback, includeStreamingMetadata, streamingBufferSize, enableStreamingHeaders, customStreamingConfig, } = options;
|
|
389
|
+
// Phase 2.1: Setup streaming enhancements
|
|
390
|
+
const streamId = `agent_stream_${Date.now()}`;
|
|
391
|
+
const streamingConfig = StreamingEnhancer.createStreamingConfig(options);
|
|
392
|
+
if (enableProgressTracking) {
|
|
393
|
+
StreamingMonitor.registerStream(streamId);
|
|
394
|
+
}
|
|
368
395
|
// Get combined tools (direct + MCP) if enabled
|
|
369
396
|
const tools = this.config.enableTools ? await this.getCombinedTools() : {};
|
|
370
397
|
try {
|
|
@@ -388,6 +415,38 @@ export class AgentEnhancedProvider {
|
|
|
388
415
|
toolChoice: this.shouldForceToolUsage(prompt) ? "required" : "auto",
|
|
389
416
|
abortSignal, // Pass abort signal for timeout support
|
|
390
417
|
});
|
|
418
|
+
// Phase 2.1: Apply streaming enhancements if enabled
|
|
419
|
+
if (streamingConfig.progressTracking && result.textStream) {
|
|
420
|
+
const enhancedCallback = streamingConfig.callback
|
|
421
|
+
? (progress) => {
|
|
422
|
+
StreamingMonitor.updateStream(streamId, progress);
|
|
423
|
+
streamingConfig.callback(progress);
|
|
424
|
+
if (progress.phase === "complete") {
|
|
425
|
+
StreamingMonitor.completeStream(streamId);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
: undefined;
|
|
429
|
+
// Enhance the stream with progress tracking
|
|
430
|
+
const enhancedStream = StreamingEnhancer.addProgressTracking(result.textStream, enhancedCallback, { streamId, bufferSize: streamingConfig.bufferSize });
|
|
431
|
+
// Return enhanced result with tracking
|
|
432
|
+
return {
|
|
433
|
+
...result,
|
|
434
|
+
textStream: enhancedStream,
|
|
435
|
+
// Phase 2.1: Add streaming metadata
|
|
436
|
+
streamingMetadata: streamingConfig.metadata
|
|
437
|
+
? {
|
|
438
|
+
streamId,
|
|
439
|
+
provider: this.getProviderName(),
|
|
440
|
+
model: this.getModelName(),
|
|
441
|
+
enabledFeatures: {
|
|
442
|
+
progressTracking: true,
|
|
443
|
+
metadata: streamingConfig.metadata,
|
|
444
|
+
headers: streamingConfig.headers,
|
|
445
|
+
},
|
|
446
|
+
}
|
|
447
|
+
: undefined,
|
|
448
|
+
};
|
|
449
|
+
}
|
|
391
450
|
return result;
|
|
392
451
|
}
|
|
393
452
|
catch (error) {
|
|
@@ -440,7 +499,7 @@ export class AgentEnhancedProvider {
|
|
|
440
499
|
let successCount = 0;
|
|
441
500
|
for (const prompt of testPrompts) {
|
|
442
501
|
try {
|
|
443
|
-
|
|
502
|
+
logger.debug(`Testing: "${prompt}"`);
|
|
444
503
|
const result = await this.generateText(prompt);
|
|
445
504
|
if (!result) {
|
|
446
505
|
results.push({
|
|
@@ -448,7 +507,7 @@ export class AgentEnhancedProvider {
|
|
|
448
507
|
success: false,
|
|
449
508
|
error: "No result returned from generateText",
|
|
450
509
|
});
|
|
451
|
-
|
|
510
|
+
logger.warn(`❌ No result returned`);
|
|
452
511
|
continue;
|
|
453
512
|
}
|
|
454
513
|
const toolsCalled = result.toolCalls?.length || 0;
|
|
@@ -462,7 +521,7 @@ export class AgentEnhancedProvider {
|
|
|
462
521
|
toolsCalled,
|
|
463
522
|
response: result.text.substring(0, 100) + "...",
|
|
464
523
|
});
|
|
465
|
-
|
|
524
|
+
logger.debug(`✅ Tools called: ${toolsCalled}, Response: ${result.text.substring(0, 50)}...`);
|
|
466
525
|
}
|
|
467
526
|
catch (error) {
|
|
468
527
|
results.push({
|
|
@@ -470,7 +529,7 @@ export class AgentEnhancedProvider {
|
|
|
470
529
|
success: false,
|
|
471
530
|
error: error instanceof Error ? error.message : String(error),
|
|
472
531
|
});
|
|
473
|
-
|
|
532
|
+
logger.error(`❌ Error: ${error}`);
|
|
474
533
|
}
|
|
475
534
|
}
|
|
476
535
|
return {
|
|
@@ -505,6 +564,18 @@ export class AgentEnhancedProvider {
|
|
|
505
564
|
}
|
|
506
565
|
return providers;
|
|
507
566
|
}
|
|
567
|
+
/**
|
|
568
|
+
* Alias for generateText() - CLI-SDK consistency
|
|
569
|
+
*/
|
|
570
|
+
async generate(optionsOrPrompt, analysisSchema) {
|
|
571
|
+
return this.generateText(optionsOrPrompt);
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Short alias for generateText() - CLI-SDK consistency
|
|
575
|
+
*/
|
|
576
|
+
async gen(optionsOrPrompt, analysisSchema) {
|
|
577
|
+
return this.generateText(optionsOrPrompt);
|
|
578
|
+
}
|
|
508
579
|
}
|
|
509
580
|
/**
|
|
510
581
|
* Helper function to create agent provider
|
|
@@ -519,25 +590,25 @@ export function createAgentProvider(provider, options) {
|
|
|
519
590
|
* Test all available agent providers
|
|
520
591
|
*/
|
|
521
592
|
export async function testAllAgentProviders() {
|
|
522
|
-
|
|
593
|
+
logger.info("🧪 Testing All Agent Providers\n");
|
|
523
594
|
const providers = AgentEnhancedProvider.createMultiProviderAgents();
|
|
524
595
|
if (Object.keys(providers).length === 0) {
|
|
525
|
-
|
|
596
|
+
logger.warn("❌ No API keys found. Please configure at least one provider.");
|
|
526
597
|
return;
|
|
527
598
|
}
|
|
528
599
|
for (const [name, provider] of Object.entries(providers)) {
|
|
529
|
-
|
|
600
|
+
logger.info(`\n🔬 Testing ${name.toUpperCase()} Agent Provider:`);
|
|
530
601
|
try {
|
|
531
602
|
const testResult = await provider.testAgentCapabilities();
|
|
532
603
|
if (testResult.success) {
|
|
533
|
-
|
|
604
|
+
logger.info(`✅ ${name} agent provider working correctly`);
|
|
534
605
|
}
|
|
535
606
|
else {
|
|
536
|
-
|
|
607
|
+
logger.warn(`❌ ${name} agent provider failed tests`);
|
|
537
608
|
}
|
|
538
609
|
}
|
|
539
610
|
catch (error) {
|
|
540
|
-
|
|
611
|
+
logger.error(`❌ ${name} provider error:`, error);
|
|
541
612
|
}
|
|
542
613
|
}
|
|
543
614
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ZodType, ZodTypeDef } from "zod";
|
|
2
2
|
import { type StreamTextResult, type ToolSet, type Schema, type GenerateTextResult } from "ai";
|
|
3
|
-
import type { AIProvider, TextGenerationOptions, StreamTextOptions } from "../core/types.js";
|
|
3
|
+
import type { AIProvider, TextGenerationOptions, StreamTextOptions, EnhancedGenerateTextResult } from "../core/types.js";
|
|
4
4
|
export declare class AmazonBedrock implements AIProvider {
|
|
5
5
|
private modelName;
|
|
6
6
|
private model;
|
|
@@ -8,4 +8,12 @@ export declare class AmazonBedrock implements AIProvider {
|
|
|
8
8
|
constructor(modelName?: string | null);
|
|
9
9
|
streamText(optionsOrPrompt: StreamTextOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamTextResult<ToolSet, unknown> | null>;
|
|
10
10
|
generateText(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<GenerateTextResult<ToolSet, unknown> | null>;
|
|
11
|
+
/**
|
|
12
|
+
* Alias for generateText() - CLI-SDK consistency
|
|
13
|
+
*/
|
|
14
|
+
generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<EnhancedGenerateTextResult | null>;
|
|
15
|
+
/**
|
|
16
|
+
* Short alias for generateText() - CLI-SDK consistency
|
|
17
|
+
*/
|
|
18
|
+
gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<EnhancedGenerateTextResult | null>;
|
|
11
19
|
}
|
|
@@ -2,6 +2,9 @@ import { createAmazonBedrock } from "@ai-sdk/amazon-bedrock";
|
|
|
2
2
|
import { streamText, generateText, Output, } from "ai";
|
|
3
3
|
import { logger } from "../utils/logger.js";
|
|
4
4
|
import { createTimeoutController, TimeoutError, getDefaultTimeout, } from "../utils/timeout.js";
|
|
5
|
+
import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
|
|
6
|
+
import { evaluateResponse } from "../core/evaluation.js";
|
|
7
|
+
import { createAnalytics } from "../core/analytics.js";
|
|
5
8
|
// Default system context
|
|
6
9
|
const DEFAULT_SYSTEM_CONTEXT = {
|
|
7
10
|
systemPrompt: "You are a helpful AI assistant.",
|
|
@@ -129,7 +132,7 @@ export class AmazonBedrock {
|
|
|
129
132
|
const options = typeof optionsOrPrompt === "string"
|
|
130
133
|
? { prompt: optionsOrPrompt }
|
|
131
134
|
: optionsOrPrompt;
|
|
132
|
-
const { prompt, temperature = 0.7, maxTokens =
|
|
135
|
+
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout(provider, "stream"), } = options;
|
|
133
136
|
// Use schema from options or fallback parameter
|
|
134
137
|
const finalSchema = schema || analysisSchema;
|
|
135
138
|
logger.debug(`[${functionTag}] Stream request started`, {
|
|
@@ -231,12 +234,13 @@ export class AmazonBedrock {
|
|
|
231
234
|
async generateText(optionsOrPrompt, analysisSchema) {
|
|
232
235
|
const functionTag = "AmazonBedrock.generateText";
|
|
233
236
|
const provider = "bedrock";
|
|
237
|
+
const startTime = Date.now();
|
|
234
238
|
try {
|
|
235
239
|
// Parse parameters - support both string and options object
|
|
236
240
|
const options = typeof optionsOrPrompt === "string"
|
|
237
241
|
? { prompt: optionsOrPrompt }
|
|
238
242
|
: optionsOrPrompt;
|
|
239
|
-
const { prompt, temperature = 0.7, maxTokens =
|
|
243
|
+
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout(provider, "generate"), } = options;
|
|
240
244
|
// Use schema from options or fallback parameter
|
|
241
245
|
const finalSchema = schema || analysisSchema;
|
|
242
246
|
logger.debug(`[${functionTag}] Generate text started`, {
|
|
@@ -278,6 +282,14 @@ export class AmazonBedrock {
|
|
|
278
282
|
responseLength: result.text?.length || 0,
|
|
279
283
|
timeout,
|
|
280
284
|
});
|
|
285
|
+
// Add analytics if enabled
|
|
286
|
+
if (options.enableAnalytics) {
|
|
287
|
+
result.analytics = createAnalytics(provider, this.modelName, result, Date.now() - startTime, options.context);
|
|
288
|
+
}
|
|
289
|
+
// Add evaluation if enabled
|
|
290
|
+
if (options.enableEvaluation) {
|
|
291
|
+
result.evaluation = await evaluateResponse(prompt, result.text, options.context);
|
|
292
|
+
}
|
|
281
293
|
return result;
|
|
282
294
|
}
|
|
283
295
|
finally {
|
|
@@ -307,4 +319,16 @@ export class AmazonBedrock {
|
|
|
307
319
|
throw err; // Re-throw error to trigger fallback instead of returning null
|
|
308
320
|
}
|
|
309
321
|
}
|
|
322
|
+
/**
|
|
323
|
+
* Alias for generateText() - CLI-SDK consistency
|
|
324
|
+
*/
|
|
325
|
+
async generate(optionsOrPrompt, analysisSchema) {
|
|
326
|
+
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Short alias for generateText() - CLI-SDK consistency
|
|
330
|
+
*/
|
|
331
|
+
async gen(optionsOrPrompt, analysisSchema) {
|
|
332
|
+
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
333
|
+
}
|
|
310
334
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enhanced Analytics Helper for All Providers
|
|
3
|
+
* Ensures consistent analytics data format across providers
|
|
4
|
+
* Integrates with Universal Evaluation System
|
|
5
|
+
*/
|
|
6
|
+
export interface AnalyticsData {
|
|
7
|
+
provider: string;
|
|
8
|
+
model: string;
|
|
9
|
+
tokens: {
|
|
10
|
+
input: number;
|
|
11
|
+
output: number;
|
|
12
|
+
total: number;
|
|
13
|
+
};
|
|
14
|
+
cost?: number;
|
|
15
|
+
responseTime: number;
|
|
16
|
+
timestamp: string;
|
|
17
|
+
context?: Record<string, any>;
|
|
18
|
+
evaluation?: {
|
|
19
|
+
relevanceScore: number;
|
|
20
|
+
accuracyScore: number;
|
|
21
|
+
completenessScore: number;
|
|
22
|
+
overall: number;
|
|
23
|
+
evaluationProvider?: string;
|
|
24
|
+
evaluationTime?: number;
|
|
25
|
+
evaluationAttempt?: number;
|
|
26
|
+
};
|
|
27
|
+
costDetails?: {
|
|
28
|
+
inputCost: number;
|
|
29
|
+
outputCost: number;
|
|
30
|
+
evaluationCost?: number;
|
|
31
|
+
totalCost: number;
|
|
32
|
+
currency: string;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Create standardized analytics data from provider response
|
|
37
|
+
*/
|
|
38
|
+
export declare function createAnalytics(provider: string, model: string, result: any, responseTime: number, context?: Record<string, any>): AnalyticsData;
|
|
39
|
+
/**
|
|
40
|
+
* Create enhanced analytics data with accurate cost calculations (async version)
|
|
41
|
+
*/
|
|
42
|
+
export declare function createEnhancedAnalytics(provider: string, model: string, result: any, responseTime: number, context?: Record<string, any>): Promise<AnalyticsData>;
|
|
43
|
+
/**
|
|
44
|
+
* Calculate enhanced cost details using provider configurations
|
|
45
|
+
*/
|
|
46
|
+
export declare function calculateEnhancedCost(provider: string, inputTokens: number, outputTokens: number): Promise<{
|
|
47
|
+
costDetails: any;
|
|
48
|
+
estimatedCost: number;
|
|
49
|
+
}>;
|
|
50
|
+
/**
|
|
51
|
+
* Enhance analytics with evaluation data
|
|
52
|
+
*/
|
|
53
|
+
export declare function enhanceAnalyticsWithEvaluation(analytics: AnalyticsData, evaluationResult: any): AnalyticsData;
|