@juspay/neurolink 5.0.0 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -7
- package/README.md +160 -172
- package/dist/agent/direct-tools.d.ts +6 -6
- package/dist/chat/sse-handler.js +5 -4
- package/dist/chat/websocket-chat-handler.js +9 -9
- package/dist/cli/commands/config.d.ts +3 -3
- package/dist/cli/commands/mcp.js +9 -8
- package/dist/cli/commands/ollama.js +3 -3
- package/dist/cli/factories/command-factory.d.ts +18 -0
- package/dist/cli/factories/command-factory.js +183 -0
- package/dist/cli/index.js +105 -157
- package/dist/cli/utils/interactive-setup.js +2 -2
- package/dist/core/base-provider.d.ts +423 -0
- package/dist/core/base-provider.js +365 -0
- package/dist/core/constants.d.ts +1 -1
- package/dist/core/constants.js +1 -1
- package/dist/core/dynamic-models.d.ts +6 -6
- package/dist/core/evaluation.d.ts +19 -80
- package/dist/core/evaluation.js +185 -484
- package/dist/core/factory.d.ts +3 -3
- package/dist/core/factory.js +31 -91
- package/dist/core/service-registry.d.ts +47 -0
- package/dist/core/service-registry.js +112 -0
- package/dist/core/types.d.ts +49 -49
- package/dist/core/types.js +1 -0
- package/dist/factories/compatibility-factory.d.ts +20 -0
- package/dist/factories/compatibility-factory.js +69 -0
- package/dist/factories/provider-factory.d.ts +72 -0
- package/dist/factories/provider-factory.js +144 -0
- package/dist/factories/provider-generate-factory.d.ts +20 -0
- package/dist/factories/provider-generate-factory.js +87 -0
- package/dist/factories/provider-registry.d.ts +38 -0
- package/dist/factories/provider-registry.js +107 -0
- package/dist/index.d.ts +8 -5
- package/dist/index.js +5 -5
- package/dist/lib/agent/direct-tools.d.ts +6 -6
- package/dist/lib/chat/sse-handler.js +5 -4
- package/dist/lib/chat/websocket-chat-handler.js +9 -9
- package/dist/lib/core/base-provider.d.ts +423 -0
- package/dist/lib/core/base-provider.js +365 -0
- package/dist/lib/core/constants.d.ts +1 -1
- package/dist/lib/core/constants.js +1 -1
- package/dist/lib/core/dynamic-models.d.ts +6 -6
- package/dist/lib/core/evaluation.d.ts +19 -80
- package/dist/lib/core/evaluation.js +185 -484
- package/dist/lib/core/factory.d.ts +3 -3
- package/dist/lib/core/factory.js +30 -91
- package/dist/lib/core/service-registry.d.ts +47 -0
- package/dist/lib/core/service-registry.js +112 -0
- package/dist/lib/core/types.d.ts +49 -49
- package/dist/lib/core/types.js +1 -0
- package/dist/lib/factories/compatibility-factory.d.ts +20 -0
- package/dist/lib/factories/compatibility-factory.js +69 -0
- package/dist/lib/factories/provider-factory.d.ts +72 -0
- package/dist/lib/factories/provider-factory.js +144 -0
- package/dist/lib/factories/provider-generate-factory.d.ts +20 -0
- package/dist/lib/factories/provider-generate-factory.js +87 -0
- package/dist/lib/factories/provider-registry.d.ts +38 -0
- package/dist/lib/factories/provider-registry.js +107 -0
- package/dist/lib/index.d.ts +8 -5
- package/dist/lib/index.js +5 -5
- package/dist/lib/mcp/client.js +5 -5
- package/dist/lib/mcp/config.js +28 -3
- package/dist/lib/mcp/dynamic-orchestrator.js +8 -8
- package/dist/lib/mcp/external-client.js +2 -2
- package/dist/lib/mcp/factory.d.ts +1 -1
- package/dist/lib/mcp/factory.js +1 -1
- package/dist/lib/mcp/function-calling.js +1 -1
- package/dist/lib/mcp/initialize-tools.d.ts +1 -1
- package/dist/lib/mcp/initialize-tools.js +45 -1
- package/dist/lib/mcp/initialize.js +16 -6
- package/dist/lib/mcp/neurolink-mcp-client.js +10 -10
- package/dist/lib/mcp/orchestrator.js +4 -4
- package/dist/lib/mcp/servers/agent/direct-tools-server.d.ts +8 -0
- package/dist/lib/mcp/servers/agent/direct-tools-server.js +109 -0
- package/dist/lib/mcp/servers/ai-providers/ai-analysis-tools.js +10 -10
- package/dist/lib/mcp/servers/ai-providers/ai-core-server.js +8 -6
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.js +16 -16
- package/dist/lib/mcp/unified-registry.d.ts +4 -0
- package/dist/lib/mcp/unified-registry.js +42 -9
- package/dist/lib/neurolink.d.ts +161 -174
- package/dist/lib/neurolink.js +723 -397
- package/dist/lib/providers/amazon-bedrock.d.ts +32 -0
- package/dist/lib/providers/amazon-bedrock.js +143 -0
- package/dist/lib/providers/analytics-helper.js +7 -4
- package/dist/lib/providers/anthropic-baseprovider.d.ts +23 -0
- package/dist/lib/providers/anthropic-baseprovider.js +114 -0
- package/dist/lib/providers/anthropic.d.ts +19 -39
- package/dist/lib/providers/anthropic.js +84 -378
- package/dist/lib/providers/azure-openai.d.ts +20 -0
- package/dist/lib/providers/azure-openai.js +89 -0
- package/dist/lib/providers/function-calling-provider.d.ts +14 -12
- package/dist/lib/providers/function-calling-provider.js +114 -64
- package/dist/lib/providers/google-ai-studio.d.ts +23 -0
- package/dist/lib/providers/google-ai-studio.js +107 -0
- package/dist/lib/providers/google-vertex.d.ts +47 -0
- package/dist/lib/providers/google-vertex.js +205 -0
- package/dist/lib/providers/huggingFace.d.ts +33 -27
- package/dist/lib/providers/huggingFace.js +103 -400
- package/dist/lib/providers/index.d.ts +9 -9
- package/dist/lib/providers/index.js +9 -9
- package/dist/lib/providers/mcp-provider.d.ts +13 -8
- package/dist/lib/providers/mcp-provider.js +63 -18
- package/dist/lib/providers/mistral.d.ts +42 -0
- package/dist/lib/providers/mistral.js +160 -0
- package/dist/lib/providers/ollama.d.ts +52 -35
- package/dist/lib/providers/ollama.js +297 -477
- package/dist/lib/providers/openAI.d.ts +21 -21
- package/dist/lib/providers/openAI.js +81 -245
- package/dist/lib/sdk/tool-extension.d.ts +181 -0
- package/dist/lib/sdk/tool-extension.js +283 -0
- package/dist/lib/sdk/tool-registration.d.ts +95 -0
- package/dist/lib/sdk/tool-registration.js +167 -0
- package/dist/lib/types/generate-types.d.ts +80 -0
- package/dist/lib/types/generate-types.js +1 -0
- package/dist/lib/types/mcp-types.d.ts +116 -0
- package/dist/lib/types/mcp-types.js +5 -0
- package/dist/lib/types/stream-types.d.ts +95 -0
- package/dist/lib/types/stream-types.js +1 -0
- package/dist/lib/types/universal-provider-options.d.ts +87 -0
- package/dist/lib/types/universal-provider-options.js +53 -0
- package/dist/lib/utils/providerUtils-fixed.js +1 -1
- package/dist/lib/utils/streaming-utils.d.ts +14 -2
- package/dist/lib/utils/streaming-utils.js +0 -3
- package/dist/mcp/client.js +5 -5
- package/dist/mcp/config.js +28 -3
- package/dist/mcp/dynamic-orchestrator.js +8 -8
- package/dist/mcp/external-client.js +2 -2
- package/dist/mcp/factory.d.ts +1 -1
- package/dist/mcp/factory.js +1 -1
- package/dist/mcp/function-calling.js +1 -1
- package/dist/mcp/initialize-tools.d.ts +1 -1
- package/dist/mcp/initialize-tools.js +45 -1
- package/dist/mcp/initialize.js +16 -6
- package/dist/mcp/neurolink-mcp-client.js +10 -10
- package/dist/mcp/orchestrator.js +4 -4
- package/dist/mcp/servers/agent/direct-tools-server.d.ts +8 -0
- package/dist/mcp/servers/agent/direct-tools-server.js +109 -0
- package/dist/mcp/servers/ai-providers/ai-analysis-tools.js +10 -10
- package/dist/mcp/servers/ai-providers/ai-core-server.js +8 -6
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.js +16 -16
- package/dist/mcp/unified-registry.d.ts +4 -0
- package/dist/mcp/unified-registry.js +42 -9
- package/dist/neurolink.d.ts +161 -174
- package/dist/neurolink.js +723 -397
- package/dist/providers/amazon-bedrock.d.ts +32 -0
- package/dist/providers/amazon-bedrock.js +143 -0
- package/dist/providers/analytics-helper.js +7 -4
- package/dist/providers/anthropic-baseprovider.d.ts +23 -0
- package/dist/providers/anthropic-baseprovider.js +114 -0
- package/dist/providers/anthropic.d.ts +19 -39
- package/dist/providers/anthropic.js +83 -377
- package/dist/providers/azure-openai.d.ts +20 -0
- package/dist/providers/azure-openai.js +89 -0
- package/dist/providers/function-calling-provider.d.ts +14 -12
- package/dist/providers/function-calling-provider.js +114 -64
- package/dist/providers/google-ai-studio.d.ts +23 -0
- package/dist/providers/google-ai-studio.js +108 -0
- package/dist/providers/google-vertex.d.ts +47 -0
- package/dist/providers/google-vertex.js +205 -0
- package/dist/providers/huggingFace.d.ts +33 -27
- package/dist/providers/huggingFace.js +102 -399
- package/dist/providers/index.d.ts +9 -9
- package/dist/providers/index.js +9 -9
- package/dist/providers/mcp-provider.d.ts +13 -8
- package/dist/providers/mcp-provider.js +63 -18
- package/dist/providers/mistral.d.ts +42 -0
- package/dist/providers/mistral.js +160 -0
- package/dist/providers/ollama.d.ts +52 -35
- package/dist/providers/ollama.js +297 -476
- package/dist/providers/openAI.d.ts +21 -21
- package/dist/providers/openAI.js +81 -246
- package/dist/sdk/tool-extension.d.ts +181 -0
- package/dist/sdk/tool-extension.js +283 -0
- package/dist/sdk/tool-registration.d.ts +95 -0
- package/dist/sdk/tool-registration.js +168 -0
- package/dist/types/generate-types.d.ts +80 -0
- package/dist/types/generate-types.js +1 -0
- package/dist/types/mcp-types.d.ts +116 -0
- package/dist/types/mcp-types.js +5 -0
- package/dist/types/stream-types.d.ts +95 -0
- package/dist/types/stream-types.js +1 -0
- package/dist/types/universal-provider-options.d.ts +87 -0
- package/dist/types/universal-provider-options.js +53 -0
- package/dist/utils/providerUtils-fixed.js +1 -1
- package/dist/utils/streaming-utils.d.ts +14 -2
- package/dist/utils/streaming-utils.js +0 -3
- package/package.json +15 -10
- package/dist/lib/providers/agent-enhanced-provider.d.ts +0 -89
- package/dist/lib/providers/agent-enhanced-provider.js +0 -614
- package/dist/lib/providers/amazonBedrock.d.ts +0 -19
- package/dist/lib/providers/amazonBedrock.js +0 -334
- package/dist/lib/providers/azureOpenAI.d.ts +0 -39
- package/dist/lib/providers/azureOpenAI.js +0 -436
- package/dist/lib/providers/googleAIStudio.d.ts +0 -49
- package/dist/lib/providers/googleAIStudio.js +0 -333
- package/dist/lib/providers/googleVertexAI.d.ts +0 -38
- package/dist/lib/providers/googleVertexAI.js +0 -519
- package/dist/lib/providers/mistralAI.d.ts +0 -34
- package/dist/lib/providers/mistralAI.js +0 -294
- package/dist/providers/agent-enhanced-provider.d.ts +0 -89
- package/dist/providers/agent-enhanced-provider.js +0 -614
- package/dist/providers/amazonBedrock.d.ts +0 -19
- package/dist/providers/amazonBedrock.js +0 -334
- package/dist/providers/azureOpenAI.d.ts +0 -39
- package/dist/providers/azureOpenAI.js +0 -437
- package/dist/providers/googleAIStudio.d.ts +0 -49
- package/dist/providers/googleAIStudio.js +0 -333
- package/dist/providers/googleVertexAI.d.ts +0 -38
- package/dist/providers/googleVertexAI.js +0 -519
- package/dist/providers/mistralAI.d.ts +0 -34
- package/dist/providers/mistralAI.js +0 -294
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import { logger } from "../utils/logger.js";
|
|
2
|
+
import { directAgentTools } from "../agent/direct-tools.js";
|
|
3
|
+
// Dynamic imports to break circular dependency
|
|
4
|
+
// import { evaluateResponse } from "../core/evaluation.js";
|
|
5
|
+
// import { getAvailableFunctionTools } from "../mcp/function-calling.js";
|
|
6
|
+
// Analytics helper will be dynamically imported when needed
|
|
7
|
+
/**
|
|
8
|
+
* Abstract base class for all AI providers
|
|
9
|
+
* Tools are integrated as first-class citizens - always available by default
|
|
10
|
+
*/
|
|
11
|
+
export class BaseProvider {
|
|
12
|
+
modelName;
|
|
13
|
+
providerName;
|
|
14
|
+
defaultTimeout = 30000; // 30 seconds
|
|
15
|
+
// Tools are ALWAYS part of the provider - no flags, no conditions
|
|
16
|
+
directTools = directAgentTools;
|
|
17
|
+
mcpTools; // MCP tools loaded dynamically when available
|
|
18
|
+
sessionId;
|
|
19
|
+
userId;
|
|
20
|
+
sdk; // Reference to NeuroLink SDK instance for custom tools
|
|
21
|
+
constructor(modelName, providerName, sdk) {
|
|
22
|
+
this.modelName = modelName || this.getDefaultModel();
|
|
23
|
+
this.providerName = providerName || this.getProviderName();
|
|
24
|
+
this.sdk = sdk;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Check if this provider supports tool/function calling
|
|
28
|
+
* Override in subclasses to disable tools for specific providers or models
|
|
29
|
+
* @returns true by default, providers can override to return false
|
|
30
|
+
*/
|
|
31
|
+
supportsTools() {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
// ===================
|
|
35
|
+
// PUBLIC API METHODS
|
|
36
|
+
// ===================
|
|
37
|
+
/**
|
|
38
|
+
* Primary streaming method - implements AIProvider interface
|
|
39
|
+
* When tools are involved, falls back to generate() with synthetic streaming
|
|
40
|
+
*/
|
|
41
|
+
async stream(optionsOrPrompt, analysisSchema) {
|
|
42
|
+
const options = this.normalizeStreamOptions(optionsOrPrompt);
|
|
43
|
+
// If tools are not disabled AND provider supports tools, use generate() and create synthetic stream
|
|
44
|
+
if (!options.disableTools && this.supportsTools()) {
|
|
45
|
+
try {
|
|
46
|
+
// Convert stream options to text generation options
|
|
47
|
+
const textOptions = {
|
|
48
|
+
prompt: options.input?.text || "",
|
|
49
|
+
systemPrompt: options.systemPrompt,
|
|
50
|
+
temperature: options.temperature,
|
|
51
|
+
maxTokens: options.maxTokens,
|
|
52
|
+
disableTools: false,
|
|
53
|
+
maxSteps: options.maxSteps || 5,
|
|
54
|
+
provider: options.provider,
|
|
55
|
+
model: options.model,
|
|
56
|
+
};
|
|
57
|
+
const result = await this.generate(textOptions, analysisSchema);
|
|
58
|
+
// Create a synthetic stream from the generate result that simulates progressive delivery
|
|
59
|
+
return {
|
|
60
|
+
stream: (async function* () {
|
|
61
|
+
if (result?.content) {
|
|
62
|
+
// Split content into words for more natural streaming
|
|
63
|
+
const words = result.content.split(/(\s+)/); // Keep whitespace
|
|
64
|
+
let buffer = "";
|
|
65
|
+
for (let i = 0; i < words.length; i++) {
|
|
66
|
+
buffer += words[i];
|
|
67
|
+
// Yield chunks of roughly 5-10 words or at punctuation
|
|
68
|
+
const shouldYield = i === words.length - 1 || // Last word
|
|
69
|
+
buffer.length > 50 || // Buffer getting long
|
|
70
|
+
/[.!?;,]\s*$/.test(buffer); // End of sentence/clause
|
|
71
|
+
if (shouldYield && buffer.trim()) {
|
|
72
|
+
yield { content: buffer };
|
|
73
|
+
buffer = "";
|
|
74
|
+
// Small delay to simulate streaming (1-10ms)
|
|
75
|
+
await new Promise((resolve) => setTimeout(resolve, Math.random() * 9 + 1));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Yield any remaining content
|
|
79
|
+
if (buffer.trim()) {
|
|
80
|
+
yield { content: buffer };
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
})(),
|
|
84
|
+
usage: result?.usage,
|
|
85
|
+
provider: result?.provider,
|
|
86
|
+
model: result?.model,
|
|
87
|
+
toolCalls: result?.toolCalls?.map((call) => ({
|
|
88
|
+
toolName: call.toolName,
|
|
89
|
+
parameters: call.args,
|
|
90
|
+
id: call.toolCallId,
|
|
91
|
+
})),
|
|
92
|
+
toolResults: result?.toolResults,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
logger.error(`Stream with tools failed for ${this.providerName}:`, error);
|
|
97
|
+
throw this.handleProviderError(error);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// Traditional streaming without tools
|
|
101
|
+
try {
|
|
102
|
+
return await this.executeStream(options, analysisSchema);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
logger.error(`Stream failed for ${this.providerName}:`, error);
|
|
106
|
+
throw this.handleProviderError(error);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Text generation method - implements AIProvider interface
|
|
111
|
+
* Tools are always available unless explicitly disabled
|
|
112
|
+
*/
|
|
113
|
+
async generate(optionsOrPrompt, analysisSchema) {
|
|
114
|
+
const options = this.normalizeTextOptions(optionsOrPrompt);
|
|
115
|
+
const startTime = Date.now();
|
|
116
|
+
try {
|
|
117
|
+
// Import generateText dynamically to avoid circular dependencies
|
|
118
|
+
const { generateText } = await import("ai");
|
|
119
|
+
// Get ALL available tools (direct + MCP when available)
|
|
120
|
+
const shouldUseTools = !options.disableTools && this.supportsTools();
|
|
121
|
+
const tools = shouldUseTools ? await this.getAllTools() : {};
|
|
122
|
+
logger.debug(`[BaseProvider.generate] Tools for ${this.providerName}: ${Object.keys(tools).join(", ")}`);
|
|
123
|
+
// EVERY provider uses Vercel AI SDK - no exceptions
|
|
124
|
+
const model = await this.getAISDKModel(); // This method is now REQUIRED
|
|
125
|
+
const result = await generateText({
|
|
126
|
+
model,
|
|
127
|
+
prompt: options.prompt || options.input?.text || "",
|
|
128
|
+
system: options.systemPrompt,
|
|
129
|
+
tools,
|
|
130
|
+
maxSteps: options.maxSteps || 5,
|
|
131
|
+
toolChoice: shouldUseTools ? "auto" : "none",
|
|
132
|
+
temperature: options.temperature,
|
|
133
|
+
maxTokens: options.maxTokens || 8192,
|
|
134
|
+
});
|
|
135
|
+
// Format the result with tool executions included
|
|
136
|
+
const enhancedResult = {
|
|
137
|
+
content: result.text,
|
|
138
|
+
usage: {
|
|
139
|
+
inputTokens: result.usage?.promptTokens || 0,
|
|
140
|
+
outputTokens: result.usage?.completionTokens || 0,
|
|
141
|
+
totalTokens: result.usage?.totalTokens || 0,
|
|
142
|
+
},
|
|
143
|
+
provider: this.providerName,
|
|
144
|
+
model: this.modelName,
|
|
145
|
+
toolCalls: result.toolCalls,
|
|
146
|
+
toolResults: result.toolResults,
|
|
147
|
+
};
|
|
148
|
+
// Enhanced result with analytics and evaluation
|
|
149
|
+
return await this.enhanceResult(enhancedResult, options, startTime);
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
logger.error(`Generate failed for ${this.providerName}:`, error);
|
|
153
|
+
throw this.handleProviderError(error);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Alias for generate method - implements AIProvider interface
|
|
158
|
+
*/
|
|
159
|
+
async gen(optionsOrPrompt, analysisSchema) {
|
|
160
|
+
return this.generate(optionsOrPrompt, analysisSchema);
|
|
161
|
+
}
|
|
162
|
+
// ===================
|
|
163
|
+
// TOOL MANAGEMENT
|
|
164
|
+
// ===================
|
|
165
|
+
/**
|
|
166
|
+
* Get all available tools - direct tools are ALWAYS available
|
|
167
|
+
* MCP tools are added when available (without blocking)
|
|
168
|
+
*/
|
|
169
|
+
async getAllTools() {
|
|
170
|
+
const tools = {
|
|
171
|
+
...this.directTools, // Always include direct tools
|
|
172
|
+
};
|
|
173
|
+
logger.info(`[BaseProvider] getAllTools called, SDK available: ${!!this.sdk}, type: ${typeof this.sdk}`);
|
|
174
|
+
console.log(`[BaseProvider] Direct tools: ${Object.keys(this.directTools).join(", ")}`);
|
|
175
|
+
// Add custom tools from SDK if available
|
|
176
|
+
console.log(`[BaseProvider] Checking SDK: ${!!this.sdk}, has getInMemoryServers: ${this.sdk && typeof this.sdk.getInMemoryServers}`);
|
|
177
|
+
if (this.sdk &&
|
|
178
|
+
typeof this.sdk.getInMemoryServers === "function") {
|
|
179
|
+
console.log(`[BaseProvider] SDK check passed, loading custom tools`);
|
|
180
|
+
try {
|
|
181
|
+
const inMemoryServers = this.sdk.getInMemoryServers();
|
|
182
|
+
console.log(`[BaseProvider] Got servers:`, inMemoryServers.size);
|
|
183
|
+
logger.debug(`[BaseProvider] Loading custom tools from SDK, found ${inMemoryServers.size} servers`);
|
|
184
|
+
if (inMemoryServers && inMemoryServers.size > 0) {
|
|
185
|
+
// Convert in-memory server tools to AI SDK format
|
|
186
|
+
for (const [serverId, serverConfig] of inMemoryServers) {
|
|
187
|
+
const server = serverConfig.server;
|
|
188
|
+
if (server && server.tools) {
|
|
189
|
+
// Handle both Map and object formats
|
|
190
|
+
const toolEntries = server.tools instanceof Map
|
|
191
|
+
? Array.from(server.tools.entries())
|
|
192
|
+
: Object.entries(server.tools || {});
|
|
193
|
+
for (const [toolName, toolInfo] of toolEntries) {
|
|
194
|
+
if (toolInfo && typeof toolInfo.execute === "function") {
|
|
195
|
+
console.log(`[BaseProvider] Converting custom tool: ${toolName}`);
|
|
196
|
+
// Convert to AI SDK tool format
|
|
197
|
+
const { tool: createAISDKTool } = await import("ai");
|
|
198
|
+
const { z } = await import("zod");
|
|
199
|
+
tools[toolName] = createAISDKTool({
|
|
200
|
+
description: toolInfo.description || `Tool ${toolName}`,
|
|
201
|
+
parameters: toolInfo.inputSchema ||
|
|
202
|
+
toolInfo.parameters ||
|
|
203
|
+
z.object({}),
|
|
204
|
+
execute: async (args) => {
|
|
205
|
+
const result = await toolInfo.execute(args);
|
|
206
|
+
// Handle MCP-style results
|
|
207
|
+
if (result &&
|
|
208
|
+
typeof result === "object" &&
|
|
209
|
+
"success" in result) {
|
|
210
|
+
if (result.success) {
|
|
211
|
+
return result.data;
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
throw new Error(result.error || "Tool execution failed");
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return result;
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
logger.debug(`Failed to load custom tools for ${this.providerName}:`, error);
|
|
228
|
+
// Not an error - custom tools are optional
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
// Try to load MCP tools if not already loaded
|
|
232
|
+
if (!this.mcpTools) {
|
|
233
|
+
try {
|
|
234
|
+
const { getAvailableFunctionTools } = await import("../mcp/function-calling.js");
|
|
235
|
+
const result = await getAvailableFunctionTools();
|
|
236
|
+
if (result.tools && result.tools.length > 0) {
|
|
237
|
+
this.mcpTools = {};
|
|
238
|
+
for (const tool of result.tools) {
|
|
239
|
+
const toolName = tool.name || "unknown";
|
|
240
|
+
this.mcpTools[toolName] = tool;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
logger.debug(`MCP tools not available for ${this.providerName}:`, error);
|
|
246
|
+
// Not an error - MCP tools are optional
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
// Add MCP tools if available
|
|
250
|
+
if (this.mcpTools) {
|
|
251
|
+
Object.assign(tools, this.mcpTools);
|
|
252
|
+
}
|
|
253
|
+
console.log(`[BaseProvider] getAllTools returning tools: ${Object.keys(tools).join(", ")}`);
|
|
254
|
+
return tools;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Set session context for MCP tools
|
|
258
|
+
*/
|
|
259
|
+
setSessionContext(sessionId, userId) {
|
|
260
|
+
this.sessionId = sessionId;
|
|
261
|
+
this.userId = userId;
|
|
262
|
+
}
|
|
263
|
+
// ===================
|
|
264
|
+
// TEMPLATE METHODS - COMMON FUNCTIONALITY
|
|
265
|
+
// ===================
|
|
266
|
+
normalizeTextOptions(optionsOrPrompt) {
|
|
267
|
+
if (typeof optionsOrPrompt === "string") {
|
|
268
|
+
return {
|
|
269
|
+
prompt: optionsOrPrompt,
|
|
270
|
+
provider: this.providerName,
|
|
271
|
+
model: this.modelName,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
// Handle both prompt and input.text formats
|
|
275
|
+
const prompt = optionsOrPrompt.prompt || optionsOrPrompt.input?.text || "";
|
|
276
|
+
return {
|
|
277
|
+
...optionsOrPrompt,
|
|
278
|
+
prompt,
|
|
279
|
+
provider: optionsOrPrompt.provider || this.providerName,
|
|
280
|
+
model: optionsOrPrompt.model || this.modelName,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
normalizeStreamOptions(optionsOrPrompt) {
|
|
284
|
+
if (typeof optionsOrPrompt === "string") {
|
|
285
|
+
return {
|
|
286
|
+
input: { text: optionsOrPrompt },
|
|
287
|
+
provider: this.providerName,
|
|
288
|
+
model: this.modelName,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
...optionsOrPrompt,
|
|
293
|
+
provider: optionsOrPrompt.provider || this.providerName,
|
|
294
|
+
model: optionsOrPrompt.model || this.modelName,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
async enhanceResult(result, options, startTime) {
|
|
298
|
+
const responseTime = Date.now() - startTime;
|
|
299
|
+
let enhancedResult = { ...result };
|
|
300
|
+
if (options.enableAnalytics) {
|
|
301
|
+
try {
|
|
302
|
+
logger.debug(`Creating analytics for ${this.providerName}...`);
|
|
303
|
+
const analytics = await this.createAnalytics(result, responseTime, options);
|
|
304
|
+
logger.debug(`Analytics created:`, analytics);
|
|
305
|
+
enhancedResult = { ...enhancedResult, analytics };
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
logger.warn(`Analytics creation failed for ${this.providerName}:`, error);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (options.enableEvaluation) {
|
|
312
|
+
try {
|
|
313
|
+
const evaluation = await this.createEvaluation(result, options);
|
|
314
|
+
enhancedResult = { ...enhancedResult, evaluation };
|
|
315
|
+
}
|
|
316
|
+
catch (error) {
|
|
317
|
+
logger.warn(`Evaluation creation failed for ${this.providerName}:`, error);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return enhancedResult;
|
|
321
|
+
}
|
|
322
|
+
async createAnalytics(result, responseTime, options) {
|
|
323
|
+
const { createAnalytics } = await import("../providers/analytics-helper.js");
|
|
324
|
+
return createAnalytics(this.providerName, this.modelName, result, responseTime, options.context);
|
|
325
|
+
}
|
|
326
|
+
async createEvaluation(result, options) {
|
|
327
|
+
const { evaluateResponse } = await import("../core/evaluation.js");
|
|
328
|
+
return evaluateResponse(result.content, options.prompt);
|
|
329
|
+
}
|
|
330
|
+
validateOptions(options) {
|
|
331
|
+
if (!options.prompt || options.prompt.trim().length === 0) {
|
|
332
|
+
throw new Error("Prompt is required and cannot be empty");
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
getProviderInfo() {
|
|
336
|
+
return {
|
|
337
|
+
provider: this.providerName,
|
|
338
|
+
model: this.modelName,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Get timeout value in milliseconds
|
|
343
|
+
*/
|
|
344
|
+
getTimeout(options) {
|
|
345
|
+
if (!options.timeout) {
|
|
346
|
+
return this.defaultTimeout;
|
|
347
|
+
}
|
|
348
|
+
if (typeof options.timeout === "number") {
|
|
349
|
+
return options.timeout;
|
|
350
|
+
}
|
|
351
|
+
// Parse string timeout (e.g., '30s', '2m', '1h')
|
|
352
|
+
const timeoutStr = options.timeout.toLowerCase();
|
|
353
|
+
const value = parseInt(timeoutStr);
|
|
354
|
+
if (timeoutStr.includes("h")) {
|
|
355
|
+
return value * 60 * 60 * 1000;
|
|
356
|
+
}
|
|
357
|
+
else if (timeoutStr.includes("m")) {
|
|
358
|
+
return value * 60 * 1000;
|
|
359
|
+
}
|
|
360
|
+
else if (timeoutStr.includes("s")) {
|
|
361
|
+
return value * 1000;
|
|
362
|
+
}
|
|
363
|
+
return this.defaultTimeout;
|
|
364
|
+
}
|
|
365
|
+
}
|
package/dist/core/constants.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Central configuration constants for NeuroLink
|
|
3
3
|
* Single source of truth for all default values
|
|
4
4
|
*/
|
|
5
|
-
export declare const DEFAULT_MAX_TOKENS =
|
|
5
|
+
export declare const DEFAULT_MAX_TOKENS = 8192;
|
|
6
6
|
export declare const DEFAULT_TEMPERATURE = 0.7;
|
|
7
7
|
export declare const DEFAULT_TIMEOUT = 30000;
|
|
8
8
|
export declare const DEFAULT_EVALUATION_MAX_TOKENS = 500;
|
package/dist/core/constants.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Single source of truth for all default values
|
|
4
4
|
*/
|
|
5
5
|
// Core AI Generation Defaults
|
|
6
|
-
export const DEFAULT_MAX_TOKENS = 10000
|
|
6
|
+
export const DEFAULT_MAX_TOKENS = 8192; // Changed from 10000 to fix Anthropic error
|
|
7
7
|
export const DEFAULT_TEMPERATURE = 0.7;
|
|
8
8
|
export const DEFAULT_TIMEOUT = 30000;
|
|
9
9
|
// Specialized Use Case Defaults
|
|
@@ -21,8 +21,8 @@ declare const ModelConfigSchema: z.ZodObject<{
|
|
|
21
21
|
releaseDate: z.ZodString;
|
|
22
22
|
}, "strip", z.ZodTypeAny, {
|
|
23
23
|
id: string;
|
|
24
|
-
displayName: string;
|
|
25
24
|
capabilities: string[];
|
|
25
|
+
displayName: string;
|
|
26
26
|
deprecated: boolean;
|
|
27
27
|
pricing: {
|
|
28
28
|
input: number;
|
|
@@ -32,8 +32,8 @@ declare const ModelConfigSchema: z.ZodObject<{
|
|
|
32
32
|
releaseDate: string;
|
|
33
33
|
}, {
|
|
34
34
|
id: string;
|
|
35
|
-
displayName: string;
|
|
36
35
|
capabilities: string[];
|
|
36
|
+
displayName: string;
|
|
37
37
|
deprecated: boolean;
|
|
38
38
|
pricing: {
|
|
39
39
|
input: number;
|
|
@@ -64,8 +64,8 @@ declare const ModelRegistrySchema: z.ZodObject<{
|
|
|
64
64
|
releaseDate: z.ZodString;
|
|
65
65
|
}, "strip", z.ZodTypeAny, {
|
|
66
66
|
id: string;
|
|
67
|
-
displayName: string;
|
|
68
67
|
capabilities: string[];
|
|
68
|
+
displayName: string;
|
|
69
69
|
deprecated: boolean;
|
|
70
70
|
pricing: {
|
|
71
71
|
input: number;
|
|
@@ -75,8 +75,8 @@ declare const ModelRegistrySchema: z.ZodObject<{
|
|
|
75
75
|
releaseDate: string;
|
|
76
76
|
}, {
|
|
77
77
|
id: string;
|
|
78
|
-
displayName: string;
|
|
79
78
|
capabilities: string[];
|
|
79
|
+
displayName: string;
|
|
80
80
|
deprecated: boolean;
|
|
81
81
|
pricing: {
|
|
82
82
|
input: number;
|
|
@@ -92,8 +92,8 @@ declare const ModelRegistrySchema: z.ZodObject<{
|
|
|
92
92
|
lastUpdated: string;
|
|
93
93
|
models: Record<string, Record<string, {
|
|
94
94
|
id: string;
|
|
95
|
-
displayName: string;
|
|
96
95
|
capabilities: string[];
|
|
96
|
+
displayName: string;
|
|
97
97
|
deprecated: boolean;
|
|
98
98
|
pricing: {
|
|
99
99
|
input: number;
|
|
@@ -109,8 +109,8 @@ declare const ModelRegistrySchema: z.ZodObject<{
|
|
|
109
109
|
lastUpdated: string;
|
|
110
110
|
models: Record<string, Record<string, {
|
|
111
111
|
id: string;
|
|
112
|
-
displayName: string;
|
|
113
112
|
capabilities: string[];
|
|
113
|
+
displayName: string;
|
|
114
114
|
deprecated: boolean;
|
|
115
115
|
pricing: {
|
|
116
116
|
input: number;
|
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* NeuroLink Unified Evaluation System
|
|
3
|
-
*
|
|
4
|
-
* Combines Universal Evaluation with Lighthouse-Enhanced capabilities
|
|
5
|
-
* - Domain-aware evaluation with sophisticated context handling
|
|
6
|
-
* - Multi-provider support with fallback strategies
|
|
7
|
-
* - Structured output with Zod schema validation
|
|
8
|
-
* - Tool usage and conversation history analysis
|
|
9
|
-
* - Enterprise-grade reliability and performance
|
|
10
3
|
*/
|
|
11
4
|
import type { EvaluationData } from "./types.js";
|
|
12
|
-
import { z } from "zod";
|
|
13
5
|
export interface UnifiedEvaluationResult extends EvaluationData {
|
|
14
6
|
domainAlignment?: number;
|
|
15
7
|
terminologyAccuracy?: number;
|
|
@@ -24,6 +16,9 @@ export interface UnifiedEvaluationResult extends EvaluationData {
|
|
|
24
16
|
toolsEvaluated: string[];
|
|
25
17
|
conversationTurns: number;
|
|
26
18
|
};
|
|
19
|
+
isOffTopic: boolean;
|
|
20
|
+
alertSeverity: "low" | "medium" | "high" | "none";
|
|
21
|
+
reasoning: string;
|
|
27
22
|
}
|
|
28
23
|
export interface UnifiedEvaluationContext {
|
|
29
24
|
userQuery: string;
|
|
@@ -31,87 +26,31 @@ export interface UnifiedEvaluationContext {
|
|
|
31
26
|
context?: Record<string, any>;
|
|
32
27
|
primaryDomain?: string;
|
|
33
28
|
assistantRole?: string;
|
|
34
|
-
toolsUsed?: string[];
|
|
35
|
-
toolContext?: string;
|
|
36
29
|
conversationHistory?: Array<{
|
|
37
30
|
role: "user" | "assistant";
|
|
38
31
|
content: string;
|
|
39
32
|
timestamp?: string;
|
|
40
33
|
}>;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
34
|
+
toolUsage?: Array<{
|
|
35
|
+
toolName: string;
|
|
36
|
+
input: any;
|
|
37
|
+
output: any;
|
|
38
|
+
executionTime: number;
|
|
39
|
+
}>;
|
|
40
|
+
expectedOutcome?: string;
|
|
41
|
+
evaluationCriteria?: string[];
|
|
45
42
|
}
|
|
46
43
|
/**
|
|
47
|
-
*
|
|
44
|
+
* Get default evaluation when evaluation fails
|
|
48
45
|
*/
|
|
49
|
-
|
|
50
|
-
relevanceScore: z.ZodNumber;
|
|
51
|
-
accuracyScore: z.ZodNumber;
|
|
52
|
-
completenessScore: z.ZodNumber;
|
|
53
|
-
domainAlignment: z.ZodOptional<z.ZodNumber>;
|
|
54
|
-
terminologyAccuracy: z.ZodOptional<z.ZodNumber>;
|
|
55
|
-
toolEffectiveness: z.ZodOptional<z.ZodNumber>;
|
|
56
|
-
isOffTopic: z.ZodBoolean;
|
|
57
|
-
reasoning: z.ZodString;
|
|
58
|
-
suggestedImprovements: z.ZodOptional<z.ZodString>;
|
|
59
|
-
alertSeverity: z.ZodEnum<["low", "medium", "high", "none"]>;
|
|
60
|
-
}, "strip", z.ZodTypeAny, {
|
|
61
|
-
relevanceScore: number;
|
|
62
|
-
accuracyScore: number;
|
|
63
|
-
completenessScore: number;
|
|
64
|
-
isOffTopic: boolean;
|
|
65
|
-
reasoning: string;
|
|
66
|
-
alertSeverity: "low" | "medium" | "high" | "none";
|
|
67
|
-
domainAlignment?: number | undefined;
|
|
68
|
-
terminologyAccuracy?: number | undefined;
|
|
69
|
-
toolEffectiveness?: number | undefined;
|
|
70
|
-
suggestedImprovements?: string | undefined;
|
|
71
|
-
}, {
|
|
72
|
-
relevanceScore: number;
|
|
73
|
-
accuracyScore: number;
|
|
74
|
-
completenessScore: number;
|
|
75
|
-
isOffTopic: boolean;
|
|
76
|
-
reasoning: string;
|
|
77
|
-
alertSeverity: "low" | "medium" | "high" | "none";
|
|
78
|
-
domainAlignment?: number | undefined;
|
|
79
|
-
terminologyAccuracy?: number | undefined;
|
|
80
|
-
toolEffectiveness?: number | undefined;
|
|
81
|
-
suggestedImprovements?: string | undefined;
|
|
82
|
-
}>;
|
|
46
|
+
declare function getDefaultUnifiedEvaluation(reason: string, evaluationTime: number, context: UnifiedEvaluationContext): UnifiedEvaluationResult;
|
|
83
47
|
/**
|
|
84
|
-
*
|
|
48
|
+
* Parse unified evaluation result from text response
|
|
85
49
|
*/
|
|
86
|
-
|
|
50
|
+
declare function parseUnifiedEvaluationResult(response: string, context: UnifiedEvaluationContext): Partial<UnifiedEvaluationResult>;
|
|
87
51
|
/**
|
|
88
|
-
*
|
|
89
|
-
*/
|
|
90
|
-
export declare function getEvaluationModel(): Promise<{
|
|
91
|
-
provider: any;
|
|
92
|
-
config: any;
|
|
93
|
-
} | null>;
|
|
94
|
-
/**
|
|
95
|
-
* Create simple evaluation context (backward compatibility)
|
|
96
|
-
*/
|
|
97
|
-
export declare function createSimpleEvaluationContext(prompt: string, response: string, context?: Record<string, any>): UnifiedEvaluationContext;
|
|
98
|
-
/**
|
|
99
|
-
* Create enhanced evaluation context
|
|
52
|
+
* Main unified evaluation function
|
|
100
53
|
*/
|
|
101
|
-
export declare function
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
toolsUsed?: string[];
|
|
105
|
-
conversationHistory?: Array<{
|
|
106
|
-
role: "user" | "assistant";
|
|
107
|
-
content: string;
|
|
108
|
-
}>;
|
|
109
|
-
sessionId?: string;
|
|
110
|
-
context?: Record<string, any>;
|
|
111
|
-
}): UnifiedEvaluationContext;
|
|
112
|
-
export declare function evaluateResponse(prompt: string, response: string, context?: Record<string, any>, evaluationDomain?: string, toolUsageContext?: string, conversationHistory?: Array<{
|
|
113
|
-
role: string;
|
|
114
|
-
content: string;
|
|
115
|
-
timestamp?: string;
|
|
116
|
-
}>): Promise<UnifiedEvaluationResult>;
|
|
117
|
-
export type EvaluationResult = UnifiedEvaluationResult;
|
|
54
|
+
export declare function generateUnifiedEvaluation(context: UnifiedEvaluationContext): Promise<UnifiedEvaluationResult>;
|
|
55
|
+
export declare function evaluateResponse(responseOrContext: any, contextOrUserQuery?: any, userQuery?: any, providedContexts?: any, options?: any, additionalArgs?: any): Promise<any>;
|
|
56
|
+
export { getDefaultUnifiedEvaluation, parseUnifiedEvaluationResult };
|