@juspay/neurolink 1.6.0 → 1.9.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 +193 -7
- package/README.md +100 -17
- package/dist/agent/direct-tools.d.ts +1203 -0
- package/dist/agent/direct-tools.js +387 -0
- package/dist/cli/commands/agent-generate.d.ts +2 -0
- package/dist/cli/commands/agent-generate.js +70 -0
- package/dist/cli/commands/config.d.ts +6 -6
- package/dist/cli/commands/config.js +326 -273
- package/dist/cli/commands/mcp.d.ts +2 -1
- package/dist/cli/commands/mcp.js +874 -146
- package/dist/cli/commands/ollama.d.ts +1 -1
- package/dist/cli/commands/ollama.js +153 -143
- package/dist/cli/index.js +589 -323
- package/dist/cli/utils/complete-setup.d.ts +19 -0
- package/dist/cli/utils/complete-setup.js +81 -0
- package/dist/cli/utils/env-manager.d.ts +44 -0
- package/dist/cli/utils/env-manager.js +226 -0
- package/dist/cli/utils/interactive-setup.d.ts +48 -0
- package/dist/cli/utils/interactive-setup.js +302 -0
- package/dist/core/dynamic-models.d.ts +208 -0
- package/dist/core/dynamic-models.js +250 -0
- package/dist/core/factory.d.ts +13 -6
- package/dist/core/factory.js +176 -61
- package/dist/core/types.d.ts +4 -2
- package/dist/core/types.js +4 -4
- package/dist/index.d.ts +16 -16
- package/dist/index.js +16 -16
- package/dist/lib/agent/direct-tools.d.ts +1203 -0
- package/dist/lib/agent/direct-tools.js +387 -0
- package/dist/lib/core/dynamic-models.d.ts +208 -0
- package/dist/lib/core/dynamic-models.js +250 -0
- package/dist/lib/core/factory.d.ts +13 -6
- package/dist/lib/core/factory.js +176 -61
- package/dist/lib/core/types.d.ts +4 -2
- package/dist/lib/core/types.js +4 -4
- package/dist/lib/index.d.ts +16 -16
- package/dist/lib/index.js +16 -16
- package/dist/lib/mcp/auto-discovery.d.ts +120 -0
- package/dist/lib/mcp/auto-discovery.js +793 -0
- package/dist/lib/mcp/client.d.ts +66 -0
- package/dist/lib/mcp/client.js +245 -0
- package/dist/lib/mcp/config.d.ts +31 -0
- package/dist/lib/mcp/config.js +74 -0
- package/dist/lib/mcp/context-manager.d.ts +4 -4
- package/dist/lib/mcp/context-manager.js +24 -18
- package/dist/lib/mcp/factory.d.ts +28 -11
- package/dist/lib/mcp/factory.js +36 -29
- package/dist/lib/mcp/function-calling.d.ts +51 -0
- package/dist/lib/mcp/function-calling.js +510 -0
- package/dist/lib/mcp/index.d.ts +190 -0
- package/dist/lib/mcp/index.js +156 -0
- package/dist/lib/mcp/initialize-tools.d.ts +28 -0
- package/dist/lib/mcp/initialize-tools.js +209 -0
- package/dist/lib/mcp/initialize.d.ts +17 -0
- package/dist/lib/mcp/initialize.js +51 -0
- package/dist/lib/mcp/logging.d.ts +71 -0
- package/dist/lib/mcp/logging.js +183 -0
- package/dist/lib/mcp/manager.d.ts +67 -0
- package/dist/lib/mcp/manager.js +176 -0
- package/dist/lib/mcp/neurolink-mcp-client.d.ts +96 -0
- package/dist/lib/mcp/neurolink-mcp-client.js +417 -0
- package/dist/lib/mcp/orchestrator.d.ts +3 -3
- package/dist/lib/mcp/orchestrator.js +46 -43
- package/dist/lib/mcp/registry.d.ts +2 -2
- package/dist/lib/mcp/registry.js +42 -33
- package/dist/lib/mcp/servers/ai-providers/ai-analysis-tools.d.ts +1 -1
- package/dist/lib/mcp/servers/ai-providers/ai-analysis-tools.js +204 -65
- package/dist/lib/mcp/servers/ai-providers/ai-core-server.js +142 -102
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.d.ts +6 -6
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.js +197 -142
- package/dist/lib/mcp/servers/utilities/utility-server.d.ts +8 -0
- package/dist/lib/mcp/servers/utilities/utility-server.js +326 -0
- package/dist/lib/mcp/tool-integration.d.ts +67 -0
- package/dist/lib/mcp/tool-integration.js +179 -0
- package/dist/lib/mcp/unified-registry.d.ts +269 -0
- package/dist/lib/mcp/unified-registry.js +1411 -0
- package/dist/lib/neurolink.d.ts +68 -6
- package/dist/lib/neurolink.js +304 -42
- package/dist/lib/providers/agent-enhanced-provider.d.ts +59 -0
- package/dist/lib/providers/agent-enhanced-provider.js +242 -0
- package/dist/lib/providers/amazonBedrock.d.ts +3 -3
- package/dist/lib/providers/amazonBedrock.js +54 -50
- package/dist/lib/providers/anthropic.d.ts +2 -2
- package/dist/lib/providers/anthropic.js +92 -84
- package/dist/lib/providers/azureOpenAI.d.ts +2 -2
- package/dist/lib/providers/azureOpenAI.js +97 -86
- package/dist/lib/providers/function-calling-provider.d.ts +70 -0
- package/dist/lib/providers/function-calling-provider.js +359 -0
- package/dist/lib/providers/googleAIStudio.d.ts +10 -5
- package/dist/lib/providers/googleAIStudio.js +60 -38
- package/dist/lib/providers/googleVertexAI.d.ts +3 -3
- package/dist/lib/providers/googleVertexAI.js +96 -86
- package/dist/lib/providers/huggingFace.d.ts +3 -3
- package/dist/lib/providers/huggingFace.js +70 -63
- package/dist/lib/providers/index.d.ts +11 -11
- package/dist/lib/providers/index.js +18 -18
- package/dist/lib/providers/mcp-provider.d.ts +62 -0
- package/dist/lib/providers/mcp-provider.js +183 -0
- package/dist/lib/providers/mistralAI.d.ts +3 -3
- package/dist/lib/providers/mistralAI.js +42 -36
- package/dist/lib/providers/ollama.d.ts +4 -4
- package/dist/lib/providers/ollama.js +113 -98
- package/dist/lib/providers/openAI.d.ts +7 -3
- package/dist/lib/providers/openAI.js +45 -33
- package/dist/lib/utils/logger.js +2 -2
- package/dist/lib/utils/providerUtils.js +53 -31
- package/dist/mcp/auto-discovery.d.ts +120 -0
- package/dist/mcp/auto-discovery.js +794 -0
- package/dist/mcp/client.d.ts +66 -0
- package/dist/mcp/client.js +245 -0
- package/dist/mcp/config.d.ts +31 -0
- package/dist/mcp/config.js +74 -0
- package/dist/mcp/context-manager.d.ts +4 -4
- package/dist/mcp/context-manager.js +24 -18
- package/dist/mcp/factory.d.ts +28 -11
- package/dist/mcp/factory.js +36 -29
- package/dist/mcp/function-calling.d.ts +51 -0
- package/dist/mcp/function-calling.js +510 -0
- package/dist/mcp/index.d.ts +190 -0
- package/dist/mcp/index.js +156 -0
- package/dist/mcp/initialize-tools.d.ts +28 -0
- package/dist/mcp/initialize-tools.js +210 -0
- package/dist/mcp/initialize.d.ts +17 -0
- package/dist/mcp/initialize.js +51 -0
- package/dist/mcp/logging.d.ts +71 -0
- package/dist/mcp/logging.js +183 -0
- package/dist/mcp/manager.d.ts +67 -0
- package/dist/mcp/manager.js +176 -0
- package/dist/mcp/neurolink-mcp-client.d.ts +96 -0
- package/dist/mcp/neurolink-mcp-client.js +417 -0
- package/dist/mcp/orchestrator.d.ts +3 -3
- package/dist/mcp/orchestrator.js +46 -43
- package/dist/mcp/registry.d.ts +2 -2
- package/dist/mcp/registry.js +42 -33
- package/dist/mcp/servers/ai-providers/ai-analysis-tools.d.ts +1 -1
- package/dist/mcp/servers/ai-providers/ai-analysis-tools.js +204 -65
- package/dist/mcp/servers/ai-providers/ai-core-server.js +142 -102
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.d.ts +6 -6
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.js +197 -142
- package/dist/mcp/servers/utilities/utility-server.d.ts +8 -0
- package/dist/mcp/servers/utilities/utility-server.js +326 -0
- package/dist/mcp/tool-integration.d.ts +67 -0
- package/dist/mcp/tool-integration.js +179 -0
- package/dist/mcp/unified-registry.d.ts +269 -0
- package/dist/mcp/unified-registry.js +1411 -0
- package/dist/neurolink.d.ts +68 -6
- package/dist/neurolink.js +304 -42
- package/dist/providers/agent-enhanced-provider.d.ts +59 -0
- package/dist/providers/agent-enhanced-provider.js +242 -0
- package/dist/providers/amazonBedrock.d.ts +3 -3
- package/dist/providers/amazonBedrock.js +54 -50
- package/dist/providers/anthropic.d.ts +2 -2
- package/dist/providers/anthropic.js +92 -84
- package/dist/providers/azureOpenAI.d.ts +2 -2
- package/dist/providers/azureOpenAI.js +97 -86
- package/dist/providers/function-calling-provider.d.ts +70 -0
- package/dist/providers/function-calling-provider.js +359 -0
- package/dist/providers/googleAIStudio.d.ts +10 -5
- package/dist/providers/googleAIStudio.js +60 -38
- package/dist/providers/googleVertexAI.d.ts +3 -3
- package/dist/providers/googleVertexAI.js +96 -86
- package/dist/providers/huggingFace.d.ts +3 -3
- package/dist/providers/huggingFace.js +70 -63
- package/dist/providers/index.d.ts +11 -11
- package/dist/providers/index.js +18 -18
- package/dist/providers/mcp-provider.d.ts +62 -0
- package/dist/providers/mcp-provider.js +183 -0
- package/dist/providers/mistralAI.d.ts +3 -3
- package/dist/providers/mistralAI.js +42 -36
- package/dist/providers/ollama.d.ts +4 -4
- package/dist/providers/ollama.js +113 -98
- package/dist/providers/openAI.d.ts +7 -3
- package/dist/providers/openAI.js +45 -33
- package/dist/utils/logger.js +2 -2
- package/dist/utils/providerUtils.js +53 -31
- package/package.json +175 -161
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent-Enhanced Provider for NeuroLink CLI
|
|
3
|
+
* Integrates direct tools with AI providers for true agent functionality
|
|
4
|
+
*/
|
|
5
|
+
import { generateText, streamText, } from "ai";
|
|
6
|
+
import { google } from "@ai-sdk/google";
|
|
7
|
+
import { openai } from "@ai-sdk/openai";
|
|
8
|
+
import { anthropic } from "@ai-sdk/anthropic";
|
|
9
|
+
import { directAgentTools, getToolsForCategory, } from "../agent/direct-tools.js";
|
|
10
|
+
/**
|
|
11
|
+
* Agent-Enhanced Provider Class
|
|
12
|
+
* Provides AI generation with tool calling capabilities
|
|
13
|
+
*/
|
|
14
|
+
export class AgentEnhancedProvider {
|
|
15
|
+
config;
|
|
16
|
+
model;
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.config = {
|
|
19
|
+
maxSteps: 5,
|
|
20
|
+
toolCategory: "all",
|
|
21
|
+
enableTools: true,
|
|
22
|
+
...config,
|
|
23
|
+
};
|
|
24
|
+
// Initialize the AI model based on provider
|
|
25
|
+
this.model = this.createModel();
|
|
26
|
+
}
|
|
27
|
+
createModel() {
|
|
28
|
+
const { provider, model } = this.config;
|
|
29
|
+
switch (provider) {
|
|
30
|
+
case "google-ai":
|
|
31
|
+
return google(model || process.env.GOOGLE_AI_MODEL || "gemini-2.0-flash-exp");
|
|
32
|
+
case "openai":
|
|
33
|
+
return openai(model || process.env.OPENAI_MODEL || "gpt-4o");
|
|
34
|
+
case "anthropic":
|
|
35
|
+
return anthropic(model || process.env.ANTHROPIC_MODEL || "claude-3-5-sonnet-20241022");
|
|
36
|
+
default:
|
|
37
|
+
throw new Error(`Unsupported provider: ${provider}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async generateText(optionsOrPrompt) {
|
|
41
|
+
const options = typeof optionsOrPrompt === "string"
|
|
42
|
+
? { prompt: optionsOrPrompt }
|
|
43
|
+
: optionsOrPrompt;
|
|
44
|
+
const { prompt, temperature = 0.7, maxTokens = 500, systemPrompt, schema, } = options;
|
|
45
|
+
// Get tools if enabled
|
|
46
|
+
const tools = this.config.enableTools
|
|
47
|
+
? getToolsForCategory(this.config.toolCategory)
|
|
48
|
+
: {};
|
|
49
|
+
try {
|
|
50
|
+
const result = await generateText({
|
|
51
|
+
model: this.model,
|
|
52
|
+
prompt: systemPrompt
|
|
53
|
+
? `System: ${systemPrompt}\n\nUser: ${prompt}`
|
|
54
|
+
: prompt,
|
|
55
|
+
tools,
|
|
56
|
+
maxSteps: this.config.maxSteps,
|
|
57
|
+
temperature,
|
|
58
|
+
maxTokens,
|
|
59
|
+
// Force tool usage for specific patterns
|
|
60
|
+
toolChoice: this.shouldForceToolUsage(prompt) ? "required" : "auto",
|
|
61
|
+
});
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
console.error("[AgentEnhancedProvider] generateText error:", error);
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async streamText(optionsOrPrompt) {
|
|
70
|
+
const options = typeof optionsOrPrompt === "string"
|
|
71
|
+
? { prompt: optionsOrPrompt }
|
|
72
|
+
: optionsOrPrompt;
|
|
73
|
+
const { prompt, temperature = 0.7, maxTokens = 500, systemPrompt, } = options;
|
|
74
|
+
// Get tools if enabled
|
|
75
|
+
const tools = this.config.enableTools
|
|
76
|
+
? getToolsForCategory(this.config.toolCategory)
|
|
77
|
+
: {};
|
|
78
|
+
try {
|
|
79
|
+
const result = await streamText({
|
|
80
|
+
model: this.model,
|
|
81
|
+
prompt: systemPrompt
|
|
82
|
+
? `System: ${systemPrompt}\n\nUser: ${prompt}`
|
|
83
|
+
: prompt,
|
|
84
|
+
tools,
|
|
85
|
+
maxSteps: this.config.maxSteps,
|
|
86
|
+
temperature,
|
|
87
|
+
maxTokens,
|
|
88
|
+
toolChoice: this.shouldForceToolUsage(prompt) ? "required" : "auto",
|
|
89
|
+
});
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
console.error("[AgentEnhancedProvider] streamText error:", error);
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Determine if we should force tool usage based on prompt patterns
|
|
99
|
+
*/
|
|
100
|
+
shouldForceToolUsage(prompt) {
|
|
101
|
+
const forceToolPatterns = [
|
|
102
|
+
/what time is it/i,
|
|
103
|
+
/current time/i,
|
|
104
|
+
/list files/i,
|
|
105
|
+
/read file/i,
|
|
106
|
+
/directory/i,
|
|
107
|
+
/calculate/i,
|
|
108
|
+
/math/i,
|
|
109
|
+
/search for/i,
|
|
110
|
+
/find files/i,
|
|
111
|
+
];
|
|
112
|
+
return forceToolPatterns.some((pattern) => pattern.test(prompt));
|
|
113
|
+
}
|
|
114
|
+
getCapabilities() {
|
|
115
|
+
return [
|
|
116
|
+
"text-generation",
|
|
117
|
+
"streaming",
|
|
118
|
+
"tool-calling",
|
|
119
|
+
"agent-functionality",
|
|
120
|
+
];
|
|
121
|
+
}
|
|
122
|
+
getProviderName() {
|
|
123
|
+
return `agent-${this.config.provider}`;
|
|
124
|
+
}
|
|
125
|
+
getModelName() {
|
|
126
|
+
return this.config.model || `default-${this.config.provider}-model`;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Test agent functionality
|
|
130
|
+
*/
|
|
131
|
+
async testAgentCapabilities() {
|
|
132
|
+
const testPrompts = [
|
|
133
|
+
"What time is it right now?",
|
|
134
|
+
"List files in current directory",
|
|
135
|
+
"Calculate 15 * 7",
|
|
136
|
+
"What is the square root of 144?",
|
|
137
|
+
];
|
|
138
|
+
const results = [];
|
|
139
|
+
let successCount = 0;
|
|
140
|
+
for (const prompt of testPrompts) {
|
|
141
|
+
try {
|
|
142
|
+
console.log(`Testing: "${prompt}"`);
|
|
143
|
+
const result = await this.generateText(prompt);
|
|
144
|
+
if (!result) {
|
|
145
|
+
results.push({
|
|
146
|
+
prompt,
|
|
147
|
+
success: false,
|
|
148
|
+
error: "No result returned from generateText",
|
|
149
|
+
});
|
|
150
|
+
console.log(`❌ No result returned`);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const toolsCalled = result.toolCalls?.length || 0;
|
|
154
|
+
const success = toolsCalled > 0;
|
|
155
|
+
if (success) {
|
|
156
|
+
successCount++;
|
|
157
|
+
}
|
|
158
|
+
results.push({
|
|
159
|
+
prompt,
|
|
160
|
+
success,
|
|
161
|
+
toolsCalled,
|
|
162
|
+
response: result.text.substring(0, 100) + "...",
|
|
163
|
+
});
|
|
164
|
+
console.log(`✅ Tools called: ${toolsCalled}, Response: ${result.text.substring(0, 50)}...`);
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
results.push({
|
|
168
|
+
prompt,
|
|
169
|
+
success: false,
|
|
170
|
+
error: error instanceof Error ? error.message : String(error),
|
|
171
|
+
});
|
|
172
|
+
console.log(`❌ Error: ${error}`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
success: successCount > 0,
|
|
177
|
+
results,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Create agent-enhanced provider factory
|
|
182
|
+
*/
|
|
183
|
+
static createAgent(config) {
|
|
184
|
+
return new AgentEnhancedProvider(config);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Create multiple agent providers for comparison
|
|
188
|
+
*/
|
|
189
|
+
static createMultiProviderAgents() {
|
|
190
|
+
const providers = {};
|
|
191
|
+
// Only create providers that have API keys configured
|
|
192
|
+
if (process.env.GOOGLE_AI_API_KEY) {
|
|
193
|
+
providers["google-ai"] = new AgentEnhancedProvider({
|
|
194
|
+
provider: "google-ai",
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
if (process.env.OPENAI_API_KEY) {
|
|
198
|
+
providers["openai"] = new AgentEnhancedProvider({ provider: "openai" });
|
|
199
|
+
}
|
|
200
|
+
if (process.env.ANTHROPIC_API_KEY) {
|
|
201
|
+
providers["anthropic"] = new AgentEnhancedProvider({
|
|
202
|
+
provider: "anthropic",
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return providers;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Helper function to create agent provider
|
|
210
|
+
*/
|
|
211
|
+
export function createAgentProvider(provider, options) {
|
|
212
|
+
return new AgentEnhancedProvider({
|
|
213
|
+
provider,
|
|
214
|
+
...options,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Test all available agent providers
|
|
219
|
+
*/
|
|
220
|
+
export async function testAllAgentProviders() {
|
|
221
|
+
console.log("🧪 Testing All Agent Providers\n");
|
|
222
|
+
const providers = AgentEnhancedProvider.createMultiProviderAgents();
|
|
223
|
+
if (Object.keys(providers).length === 0) {
|
|
224
|
+
console.log("❌ No API keys found. Please configure at least one provider.");
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
for (const [name, provider] of Object.entries(providers)) {
|
|
228
|
+
console.log(`\n🔬 Testing ${name.toUpperCase()} Agent Provider:`);
|
|
229
|
+
try {
|
|
230
|
+
const testResult = await provider.testAgentCapabilities();
|
|
231
|
+
if (testResult.success) {
|
|
232
|
+
console.log(`✅ ${name} agent provider working correctly`);
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
console.log(`❌ ${name} agent provider failed tests`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
console.log(`❌ ${name} provider error:`, error);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ZodType, ZodTypeDef } from
|
|
2
|
-
import { type StreamTextResult, type ToolSet, type Schema, type GenerateTextResult } from
|
|
3
|
-
import type { AIProvider, TextGenerationOptions, StreamTextOptions } from
|
|
1
|
+
import type { ZodType, ZodTypeDef } from "zod";
|
|
2
|
+
import { type StreamTextResult, type ToolSet, type Schema, type GenerateTextResult } from "ai";
|
|
3
|
+
import type { AIProvider, TextGenerationOptions, StreamTextOptions } from "../core/types.js";
|
|
4
4
|
export declare class AmazonBedrock implements AIProvider {
|
|
5
5
|
private modelName;
|
|
6
6
|
private model;
|
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import { createAmazonBedrock } from
|
|
2
|
-
import { streamText, generateText, Output } from
|
|
3
|
-
import { logger } from
|
|
1
|
+
import { createAmazonBedrock } from "@ai-sdk/amazon-bedrock";
|
|
2
|
+
import { streamText, generateText, Output, } from "ai";
|
|
3
|
+
import { logger } from "../utils/logger.js";
|
|
4
4
|
// Default system context
|
|
5
5
|
const DEFAULT_SYSTEM_CONTEXT = {
|
|
6
|
-
systemPrompt:
|
|
6
|
+
systemPrompt: "You are a helpful AI assistant.",
|
|
7
7
|
};
|
|
8
8
|
// Configuration helpers
|
|
9
9
|
const getBedrockModelId = () => {
|
|
10
|
-
return process.env.BEDROCK_MODEL ||
|
|
10
|
+
return (process.env.BEDROCK_MODEL ||
|
|
11
11
|
process.env.BEDROCK_MODEL_ID ||
|
|
12
|
-
|
|
12
|
+
"arn:aws:bedrock:us-east-2:225681119357:inference-profile/us.anthropic.claude-3-7-sonnet-20250219-v1:0");
|
|
13
13
|
};
|
|
14
14
|
const getAWSAccessKeyId = () => {
|
|
15
15
|
const keyId = process.env.AWS_ACCESS_KEY_ID;
|
|
16
16
|
if (!keyId) {
|
|
17
|
-
throw new Error(
|
|
17
|
+
throw new Error("AWS_ACCESS_KEY_ID environment variable is not set");
|
|
18
18
|
}
|
|
19
19
|
return keyId;
|
|
20
20
|
};
|
|
21
21
|
const getAWSSecretAccessKey = () => {
|
|
22
22
|
const secretKey = process.env.AWS_SECRET_ACCESS_KEY;
|
|
23
23
|
if (!secretKey) {
|
|
24
|
-
throw new Error(
|
|
24
|
+
throw new Error("AWS_SECRET_ACCESS_KEY environment variable is not set");
|
|
25
25
|
}
|
|
26
26
|
return secretKey;
|
|
27
27
|
};
|
|
28
28
|
const getAWSRegion = () => {
|
|
29
|
-
return process.env.AWS_REGION ||
|
|
29
|
+
return process.env.AWS_REGION || "us-east-2";
|
|
30
30
|
};
|
|
31
31
|
const getAWSSessionToken = () => {
|
|
32
32
|
return process.env.AWS_SESSION_TOKEN;
|
|
33
33
|
};
|
|
34
34
|
const getAppEnvironment = () => {
|
|
35
|
-
return process.env.PUBLIC_APP_ENVIRONMENT ||
|
|
35
|
+
return process.env.PUBLIC_APP_ENVIRONMENT || "dev";
|
|
36
36
|
};
|
|
37
37
|
// Amazon Bedrock class with enhanced error handling using createAmazonBedrock
|
|
38
38
|
export class AmazonBedrock {
|
|
@@ -40,95 +40,95 @@ export class AmazonBedrock {
|
|
|
40
40
|
model;
|
|
41
41
|
bedrock;
|
|
42
42
|
constructor(modelName) {
|
|
43
|
-
const functionTag =
|
|
43
|
+
const functionTag = "AmazonBedrock.constructor";
|
|
44
44
|
this.modelName = modelName || getBedrockModelId();
|
|
45
45
|
try {
|
|
46
46
|
logger.debug(`[${functionTag}] Function called`, {
|
|
47
47
|
modelName: this.modelName,
|
|
48
48
|
envBedrockModel: process.env.BEDROCK_MODEL,
|
|
49
49
|
envBedrockModelId: process.env.BEDROCK_MODEL_ID,
|
|
50
|
-
fallbackModel:
|
|
50
|
+
fallbackModel: "arn:aws:bedrock:us-east-2:225681119357:inference-profile/us.anthropic.claude-3-7-sonnet-20250219-v1:0",
|
|
51
51
|
});
|
|
52
52
|
// Configure AWS credentials for custom Bedrock instance
|
|
53
53
|
const awsConfig = {
|
|
54
54
|
accessKeyId: getAWSAccessKeyId(),
|
|
55
55
|
secretAccessKey: getAWSSecretAccessKey(),
|
|
56
|
-
region: getAWSRegion()
|
|
56
|
+
region: getAWSRegion(),
|
|
57
57
|
};
|
|
58
58
|
logger.debug(`[${functionTag}] AWS config validation`, {
|
|
59
59
|
hasAccessKeyId: !!awsConfig.accessKeyId,
|
|
60
60
|
hasSecretAccessKey: !!awsConfig.secretAccessKey,
|
|
61
|
-
region: awsConfig.region ||
|
|
61
|
+
region: awsConfig.region || "MISSING",
|
|
62
62
|
accessKeyIdLength: awsConfig.accessKeyId?.length || 0,
|
|
63
|
-
hasSessionToken: !!process.env.AWS_SESSION_TOKEN
|
|
63
|
+
hasSessionToken: !!process.env.AWS_SESSION_TOKEN,
|
|
64
64
|
});
|
|
65
65
|
// Add session token for development environment
|
|
66
|
-
if (getAppEnvironment() ===
|
|
66
|
+
if (getAppEnvironment() === "dev") {
|
|
67
67
|
const sessionToken = getAWSSessionToken();
|
|
68
68
|
if (sessionToken) {
|
|
69
69
|
awsConfig.sessionToken = sessionToken;
|
|
70
70
|
logger.debug(`[${functionTag}] Session token added`, {
|
|
71
|
-
environment:
|
|
71
|
+
environment: "dev",
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
74
|
else {
|
|
75
75
|
logger.warn(`[${functionTag}] Session token missing`, {
|
|
76
|
-
environment:
|
|
76
|
+
environment: "dev",
|
|
77
77
|
});
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
logger.debug(`[${functionTag}] AWS config created`, {
|
|
81
81
|
region: awsConfig.region,
|
|
82
|
-
hasSessionToken: !!awsConfig.sessionToken
|
|
82
|
+
hasSessionToken: !!awsConfig.sessionToken,
|
|
83
83
|
});
|
|
84
84
|
logger.debug(`[${functionTag}] Bedrock provider creating`, {
|
|
85
|
-
modelName: this.modelName
|
|
85
|
+
modelName: this.modelName,
|
|
86
86
|
});
|
|
87
87
|
// Create custom Bedrock provider instance with environment-based configuration
|
|
88
88
|
this.bedrock = createAmazonBedrock(awsConfig);
|
|
89
89
|
logger.debug(`[${functionTag}] Bedrock provider initialized`, {
|
|
90
|
-
modelName: this.modelName
|
|
90
|
+
modelName: this.modelName,
|
|
91
91
|
});
|
|
92
92
|
logger.debug(`[${functionTag}] Model instance creating`, {
|
|
93
|
-
modelName: this.modelName
|
|
93
|
+
modelName: this.modelName,
|
|
94
94
|
});
|
|
95
95
|
this.model = this.bedrock(this.modelName);
|
|
96
96
|
logger.debug(`[${functionTag}] Model instance created`, {
|
|
97
|
-
modelName: this.modelName
|
|
97
|
+
modelName: this.modelName,
|
|
98
98
|
});
|
|
99
99
|
logger.debug(`[${functionTag}] Function result`, {
|
|
100
100
|
modelName: this.modelName,
|
|
101
101
|
region: awsConfig.region,
|
|
102
102
|
hasSessionToken: !!awsConfig.sessionToken,
|
|
103
|
-
success: true
|
|
103
|
+
success: true,
|
|
104
104
|
});
|
|
105
105
|
logger.debug(`[${functionTag}] Initialization completed`, {
|
|
106
106
|
modelName: this.modelName,
|
|
107
107
|
region: awsConfig.region,
|
|
108
|
-
hasSessionToken: !!awsConfig.sessionToken
|
|
108
|
+
hasSessionToken: !!awsConfig.sessionToken,
|
|
109
109
|
});
|
|
110
110
|
}
|
|
111
111
|
catch (err) {
|
|
112
112
|
logger.error(`[${functionTag}] Initialization failed`, {
|
|
113
|
-
message:
|
|
113
|
+
message: "Error in initializing Amazon Bedrock",
|
|
114
114
|
modelName: this.modelName,
|
|
115
115
|
region: getAWSRegion(),
|
|
116
116
|
error: err instanceof Error ? err.message : String(err),
|
|
117
|
-
stack: err instanceof Error ? err.stack : undefined
|
|
117
|
+
stack: err instanceof Error ? err.stack : undefined,
|
|
118
118
|
});
|
|
119
119
|
throw err;
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
async streamText(optionsOrPrompt, analysisSchema) {
|
|
123
|
-
const functionTag =
|
|
124
|
-
const provider =
|
|
123
|
+
const functionTag = "AmazonBedrock.streamText";
|
|
124
|
+
const provider = "bedrock";
|
|
125
125
|
let chunkCount = 0;
|
|
126
126
|
try {
|
|
127
127
|
// Parse parameters - support both string and options object
|
|
128
|
-
const options = typeof optionsOrPrompt ===
|
|
128
|
+
const options = typeof optionsOrPrompt === "string"
|
|
129
129
|
? { prompt: optionsOrPrompt }
|
|
130
130
|
: optionsOrPrompt;
|
|
131
|
-
const { prompt, temperature = 0.7, maxTokens = 500, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema } = options;
|
|
131
|
+
const { prompt, temperature = 0.7, maxTokens = 500, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, } = options;
|
|
132
132
|
// Use schema from options or fallback parameter
|
|
133
133
|
const finalSchema = schema || analysisSchema;
|
|
134
134
|
logger.debug(`[${functionTag}] Stream request started`, {
|
|
@@ -136,7 +136,7 @@ export class AmazonBedrock {
|
|
|
136
136
|
modelName: this.modelName,
|
|
137
137
|
promptLength: prompt.length,
|
|
138
138
|
temperature,
|
|
139
|
-
maxTokens
|
|
139
|
+
maxTokens,
|
|
140
140
|
});
|
|
141
141
|
const streamOptions = {
|
|
142
142
|
model: this.model,
|
|
@@ -155,7 +155,7 @@ export class AmazonBedrock {
|
|
|
155
155
|
error: errorMessage,
|
|
156
156
|
stack: errorStack,
|
|
157
157
|
promptLength: prompt.length,
|
|
158
|
-
chunkCount
|
|
158
|
+
chunkCount,
|
|
159
159
|
});
|
|
160
160
|
},
|
|
161
161
|
onFinish: (event) => {
|
|
@@ -167,7 +167,7 @@ export class AmazonBedrock {
|
|
|
167
167
|
usage: event.usage,
|
|
168
168
|
totalChunks: chunkCount,
|
|
169
169
|
promptLength: prompt.length,
|
|
170
|
-
responseLength: event.text?.length || 0
|
|
170
|
+
responseLength: event.text?.length || 0,
|
|
171
171
|
});
|
|
172
172
|
},
|
|
173
173
|
onChunk: (event) => {
|
|
@@ -177,19 +177,21 @@ export class AmazonBedrock {
|
|
|
177
177
|
modelName: this.modelName,
|
|
178
178
|
chunkNumber: chunkCount,
|
|
179
179
|
chunkLength: event.chunk.text?.length || 0,
|
|
180
|
-
chunkType: event.chunk.type
|
|
180
|
+
chunkType: event.chunk.type,
|
|
181
181
|
});
|
|
182
|
-
}
|
|
182
|
+
},
|
|
183
183
|
};
|
|
184
184
|
if (finalSchema) {
|
|
185
|
-
streamOptions.experimental_output = Output.object({
|
|
185
|
+
streamOptions.experimental_output = Output.object({
|
|
186
|
+
schema: finalSchema,
|
|
187
|
+
});
|
|
186
188
|
}
|
|
187
189
|
// Direct streamText call - let the real error bubble up
|
|
188
190
|
const result = streamText(streamOptions);
|
|
189
191
|
logger.debug(`[${functionTag}] Stream text call successful`, {
|
|
190
192
|
provider,
|
|
191
193
|
modelName: this.modelName,
|
|
192
|
-
promptLength: prompt.length
|
|
194
|
+
promptLength: prompt.length,
|
|
193
195
|
});
|
|
194
196
|
return result;
|
|
195
197
|
}
|
|
@@ -198,21 +200,21 @@ export class AmazonBedrock {
|
|
|
198
200
|
provider,
|
|
199
201
|
modelName: this.modelName,
|
|
200
202
|
region: getAWSRegion(),
|
|
201
|
-
message:
|
|
202
|
-
err: String(err)
|
|
203
|
+
message: "Error in streaming text",
|
|
204
|
+
err: String(err),
|
|
203
205
|
});
|
|
204
206
|
throw err; // Re-throw error to trigger fallback
|
|
205
207
|
}
|
|
206
208
|
}
|
|
207
209
|
async generateText(optionsOrPrompt, analysisSchema) {
|
|
208
|
-
const functionTag =
|
|
209
|
-
const provider =
|
|
210
|
+
const functionTag = "AmazonBedrock.generateText";
|
|
211
|
+
const provider = "bedrock";
|
|
210
212
|
try {
|
|
211
213
|
// Parse parameters - support both string and options object
|
|
212
|
-
const options = typeof optionsOrPrompt ===
|
|
214
|
+
const options = typeof optionsOrPrompt === "string"
|
|
213
215
|
? { prompt: optionsOrPrompt }
|
|
214
216
|
: optionsOrPrompt;
|
|
215
|
-
const { prompt, temperature = 0.7, maxTokens = 500, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema } = options;
|
|
217
|
+
const { prompt, temperature = 0.7, maxTokens = 500, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, } = options;
|
|
216
218
|
// Use schema from options or fallback parameter
|
|
217
219
|
const finalSchema = schema || analysisSchema;
|
|
218
220
|
logger.debug(`[${functionTag}] Generate text started`, {
|
|
@@ -221,17 +223,19 @@ export class AmazonBedrock {
|
|
|
221
223
|
region: getAWSRegion(),
|
|
222
224
|
promptLength: prompt.length,
|
|
223
225
|
temperature,
|
|
224
|
-
maxTokens
|
|
226
|
+
maxTokens,
|
|
225
227
|
});
|
|
226
228
|
const generateOptions = {
|
|
227
229
|
model: this.model,
|
|
228
230
|
prompt: prompt,
|
|
229
231
|
system: systemPrompt,
|
|
230
232
|
temperature,
|
|
231
|
-
maxTokens
|
|
233
|
+
maxTokens,
|
|
232
234
|
};
|
|
233
235
|
if (finalSchema) {
|
|
234
|
-
generateOptions.experimental_output = Output.object({
|
|
236
|
+
generateOptions.experimental_output = Output.object({
|
|
237
|
+
schema: finalSchema,
|
|
238
|
+
});
|
|
235
239
|
}
|
|
236
240
|
const result = await generateText(generateOptions);
|
|
237
241
|
logger.debug(`[${functionTag}] Generate text completed`, {
|
|
@@ -239,7 +243,7 @@ export class AmazonBedrock {
|
|
|
239
243
|
modelName: this.modelName,
|
|
240
244
|
usage: result.usage,
|
|
241
245
|
finishReason: result.finishReason,
|
|
242
|
-
responseLength: result.text?.length || 0
|
|
246
|
+
responseLength: result.text?.length || 0,
|
|
243
247
|
});
|
|
244
248
|
return result;
|
|
245
249
|
}
|
|
@@ -247,8 +251,8 @@ export class AmazonBedrock {
|
|
|
247
251
|
logger.error(`[${functionTag}] Exception`, {
|
|
248
252
|
provider,
|
|
249
253
|
modelName: this.modelName,
|
|
250
|
-
message:
|
|
251
|
-
err: String(err)
|
|
254
|
+
message: "Error in generating text",
|
|
255
|
+
err: String(err),
|
|
252
256
|
});
|
|
253
257
|
throw err; // Re-throw error to trigger fallback instead of returning null
|
|
254
258
|
}
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* Direct integration with Anthropic's Claude models via their native API.
|
|
5
5
|
* Supports Claude 3.5 Sonnet, Claude 3.5 Haiku, and Claude 3 Opus.
|
|
6
6
|
*/
|
|
7
|
-
import type { AIProvider, TextGenerationOptions, StreamTextOptions } from
|
|
8
|
-
import { AIProviderName } from
|
|
7
|
+
import type { AIProvider, TextGenerationOptions, StreamTextOptions } from "../core/types.js";
|
|
8
|
+
import { AIProviderName } from "../core/types.js";
|
|
9
9
|
export declare class AnthropicProvider implements AIProvider {
|
|
10
10
|
readonly name: AIProviderName;
|
|
11
11
|
private apiKey;
|