@juspay/neurolink 5.1.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 +15 -9
- package/README.md +123 -126
- package/dist/agent/direct-tools.d.ts +6 -6
- package/dist/cli/commands/config.d.ts +3 -3
- package/dist/cli/commands/mcp.js +8 -7
- package/dist/cli/factories/command-factory.d.ts +4 -0
- package/dist/cli/factories/command-factory.js +57 -3
- package/dist/cli/index.js +87 -140
- 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 +8 -1
- package/dist/factories/compatibility-factory.js +1 -1
- package/dist/factories/provider-factory.d.ts +72 -0
- package/dist/factories/provider-factory.js +144 -0
- package/dist/factories/provider-registry.d.ts +38 -0
- package/dist/factories/provider-registry.js +107 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +2 -4
- package/dist/lib/agent/direct-tools.d.ts +6 -6
- 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 +8 -1
- package/dist/lib/factories/compatibility-factory.js +1 -1
- package/dist/lib/factories/provider-factory.d.ts +72 -0
- package/dist/lib/factories/provider-factory.js +144 -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 +4 -3
- package/dist/lib/index.js +2 -4
- package/dist/lib/mcp/config.js +28 -3
- 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/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-core-server.js +3 -1
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
- 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 +156 -117
- package/dist/lib/neurolink.js +619 -404
- 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 -43
- package/dist/lib/providers/anthropic.js +82 -306
- package/dist/lib/providers/azure-openai.d.ts +20 -0
- package/dist/lib/providers/azure-openai.js +89 -0
- 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 +32 -25
- package/dist/lib/providers/huggingFace.js +97 -431
- package/dist/lib/providers/index.d.ts +9 -9
- package/dist/lib/providers/index.js +9 -9
- package/dist/lib/providers/mcp-provider.js +4 -0
- 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 -36
- package/dist/lib/providers/ollama.js +297 -520
- package/dist/lib/providers/openAI.d.ts +19 -18
- package/dist/lib/providers/openAI.js +76 -275
- 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 +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 +30 -18
- package/dist/lib/types/universal-provider-options.d.ts +87 -0
- package/dist/lib/types/universal-provider-options.js +53 -0
- package/dist/mcp/config.js +28 -3
- 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/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-core-server.js +3 -1
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
- package/dist/mcp/unified-registry.d.ts +4 -0
- package/dist/mcp/unified-registry.js +42 -9
- package/dist/neurolink.d.ts +156 -117
- package/dist/neurolink.js +619 -404
- 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 -43
- package/dist/providers/anthropic.js +81 -305
- package/dist/providers/azure-openai.d.ts +20 -0
- package/dist/providers/azure-openai.js +89 -0
- 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 +32 -25
- package/dist/providers/huggingFace.js +96 -430
- package/dist/providers/index.d.ts +9 -9
- package/dist/providers/index.js +9 -9
- package/dist/providers/mcp-provider.js +4 -0
- package/dist/providers/mistral.d.ts +42 -0
- package/dist/providers/mistral.js +160 -0
- package/dist/providers/ollama.d.ts +52 -36
- package/dist/providers/ollama.js +297 -519
- package/dist/providers/openAI.d.ts +19 -18
- package/dist/providers/openAI.js +76 -276
- 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 +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 +30 -18
- package/dist/types/universal-provider-options.d.ts +87 -0
- package/dist/types/universal-provider-options.js +53 -0
- package/package.json +15 -10
- package/dist/lib/providers/agent-enhanced-provider.d.ts +0 -93
- package/dist/lib/providers/agent-enhanced-provider.js +0 -605
- package/dist/lib/providers/amazonBedrock.d.ts +0 -28
- package/dist/lib/providers/amazonBedrock.js +0 -364
- package/dist/lib/providers/azureOpenAI.d.ts +0 -42
- package/dist/lib/providers/azureOpenAI.js +0 -347
- package/dist/lib/providers/googleAIStudio.d.ts +0 -42
- package/dist/lib/providers/googleAIStudio.js +0 -364
- package/dist/lib/providers/googleVertexAI.d.ts +0 -34
- package/dist/lib/providers/googleVertexAI.js +0 -547
- package/dist/lib/providers/mistralAI.d.ts +0 -37
- package/dist/lib/providers/mistralAI.js +0 -325
- package/dist/providers/agent-enhanced-provider.d.ts +0 -93
- package/dist/providers/agent-enhanced-provider.js +0 -606
- package/dist/providers/amazonBedrock.d.ts +0 -28
- package/dist/providers/amazonBedrock.js +0 -364
- package/dist/providers/azureOpenAI.d.ts +0 -42
- package/dist/providers/azureOpenAI.js +0 -348
- package/dist/providers/googleAIStudio.d.ts +0 -42
- package/dist/providers/googleAIStudio.js +0 -364
- package/dist/providers/googleVertexAI.d.ts +0 -34
- package/dist/providers/googleVertexAI.js +0 -547
- package/dist/providers/mistralAI.d.ts +0 -37
- package/dist/providers/mistralAI.js +0 -325
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ZodType, ZodTypeDef } from "zod";
|
|
2
|
+
import { type Schema, type LanguageModelV1 } from "ai";
|
|
3
|
+
import type { AIProviderName } from "../core/types.js";
|
|
4
|
+
import type { StreamOptions, StreamResult } from "../types/stream-types.js";
|
|
5
|
+
import { BaseProvider } from "../core/base-provider.js";
|
|
6
|
+
/**
|
|
7
|
+
* Amazon Bedrock Provider v2 - BaseProvider Implementation
|
|
8
|
+
*
|
|
9
|
+
* PHASE 3.3: Simple BaseProvider wrap around existing @ai-sdk/amazon-bedrock implementation
|
|
10
|
+
*
|
|
11
|
+
* Features:
|
|
12
|
+
* - Extends BaseProvider for shared functionality
|
|
13
|
+
* - Preserves existing AWS credential configuration
|
|
14
|
+
* - Maintains inference profile ARN support
|
|
15
|
+
* - Uses pre-initialized Bedrock instance for efficiency
|
|
16
|
+
* - Enhanced error handling with setup guidance
|
|
17
|
+
*/
|
|
18
|
+
export declare class AmazonBedrockProvider extends BaseProvider {
|
|
19
|
+
private bedrock;
|
|
20
|
+
private model;
|
|
21
|
+
constructor(modelName?: string);
|
|
22
|
+
protected getProviderName(): AIProviderName;
|
|
23
|
+
protected getDefaultModel(): string;
|
|
24
|
+
/**
|
|
25
|
+
* Returns the Vercel AI SDK model instance for AWS Bedrock
|
|
26
|
+
*/
|
|
27
|
+
protected getAISDKModel(): LanguageModelV1;
|
|
28
|
+
protected executeStream(options: StreamOptions, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamResult>;
|
|
29
|
+
protected handleProviderError(error: any): Error;
|
|
30
|
+
private validateStreamOptions;
|
|
31
|
+
}
|
|
32
|
+
export default AmazonBedrockProvider;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { createAmazonBedrock } from "@ai-sdk/amazon-bedrock";
|
|
2
|
+
import { streamText } from "ai";
|
|
3
|
+
import { BaseProvider } from "../core/base-provider.js";
|
|
4
|
+
import { logger } from "../utils/logger.js";
|
|
5
|
+
import { TimeoutError, } from "../utils/timeout.js";
|
|
6
|
+
import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
|
|
7
|
+
// Configuration helpers
|
|
8
|
+
const getBedrockModelId = () => {
|
|
9
|
+
return (process.env.BEDROCK_MODEL ||
|
|
10
|
+
process.env.BEDROCK_MODEL_ID ||
|
|
11
|
+
"arn:aws:bedrock:us-east-2:225681119357:inference-profile/us.anthropic.claude-3-7-sonnet-20250219-v1:0");
|
|
12
|
+
};
|
|
13
|
+
const getAWSAccessKeyId = () => {
|
|
14
|
+
const accessKeyId = process.env.AWS_ACCESS_KEY_ID;
|
|
15
|
+
if (!accessKeyId) {
|
|
16
|
+
throw new Error(`❌ AWS Bedrock Provider Configuration Error\n\nMissing required environment variables: AWS_ACCESS_KEY_ID\n\n🔧 Step 1: Get AWS Credentials\n1. Visit: https://console.aws.amazon.com/iam/\n2. Create IAM user with Bedrock permissions\n3. Generate access key\n\n🔧 Step 2: Set Environment Variables\nAdd to your .env file:\nAWS_ACCESS_KEY_ID=your_access_key_here\nAWS_SECRET_ACCESS_KEY=your_secret_key_here\nAWS_REGION=us-east-1\n\n🔧 Step 3: Restart Application\nRestart your application to load the new environment variables.`);
|
|
17
|
+
}
|
|
18
|
+
return accessKeyId;
|
|
19
|
+
};
|
|
20
|
+
const getAWSSecretAccessKey = () => {
|
|
21
|
+
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
|
|
22
|
+
if (!secretAccessKey) {
|
|
23
|
+
throw new Error(`❌ AWS Bedrock Provider Configuration Error\n\nMissing required environment variables: AWS_SECRET_ACCESS_KEY\n\n🔧 Step 1: Get AWS Credentials\n1. Visit: https://console.aws.amazon.com/iam/\n2. Create IAM user with Bedrock permissions\n3. Generate access key\n\n🔧 Step 2: Set Environment Variables\nAdd to your .env file:\nAWS_ACCESS_KEY_ID=your_access_key_here\nAWS_SECRET_ACCESS_KEY=your_secret_key_here\nAWS_REGION=us-east-1\n\n🔧 Step 3: Restart Application\nRestart your application to load the new environment variables.`);
|
|
24
|
+
}
|
|
25
|
+
return secretAccessKey;
|
|
26
|
+
};
|
|
27
|
+
const getAWSRegion = () => {
|
|
28
|
+
return process.env.AWS_REGION || "us-east-1";
|
|
29
|
+
};
|
|
30
|
+
const getAWSSessionToken = () => {
|
|
31
|
+
return process.env.AWS_SESSION_TOKEN;
|
|
32
|
+
};
|
|
33
|
+
const getAppEnvironment = () => {
|
|
34
|
+
return process.env.PUBLIC_APP_ENVIRONMENT || "production";
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Amazon Bedrock Provider v2 - BaseProvider Implementation
|
|
38
|
+
*
|
|
39
|
+
* PHASE 3.3: Simple BaseProvider wrap around existing @ai-sdk/amazon-bedrock implementation
|
|
40
|
+
*
|
|
41
|
+
* Features:
|
|
42
|
+
* - Extends BaseProvider for shared functionality
|
|
43
|
+
* - Preserves existing AWS credential configuration
|
|
44
|
+
* - Maintains inference profile ARN support
|
|
45
|
+
* - Uses pre-initialized Bedrock instance for efficiency
|
|
46
|
+
* - Enhanced error handling with setup guidance
|
|
47
|
+
*/
|
|
48
|
+
export class AmazonBedrockProvider extends BaseProvider {
|
|
49
|
+
bedrock;
|
|
50
|
+
model;
|
|
51
|
+
constructor(modelName) {
|
|
52
|
+
super(modelName, "bedrock");
|
|
53
|
+
// Initialize AWS configuration
|
|
54
|
+
const awsConfig = {
|
|
55
|
+
accessKeyId: getAWSAccessKeyId(),
|
|
56
|
+
secretAccessKey: getAWSSecretAccessKey(),
|
|
57
|
+
region: getAWSRegion(),
|
|
58
|
+
};
|
|
59
|
+
// Add session token for development environment
|
|
60
|
+
if (getAppEnvironment() === "dev") {
|
|
61
|
+
const sessionToken = getAWSSessionToken();
|
|
62
|
+
if (sessionToken) {
|
|
63
|
+
awsConfig.sessionToken = sessionToken;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// Create Bedrock provider instance
|
|
67
|
+
this.bedrock = createAmazonBedrock(awsConfig);
|
|
68
|
+
// Pre-initialize model for efficiency
|
|
69
|
+
this.model = this.bedrock(this.modelName || getBedrockModelId());
|
|
70
|
+
logger.debug("Amazon Bedrock BaseProvider v2 initialized", {
|
|
71
|
+
modelName: this.modelName,
|
|
72
|
+
region: awsConfig.region,
|
|
73
|
+
hasSessionToken: !!awsConfig.sessionToken,
|
|
74
|
+
provider: this.providerName,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
getProviderName() {
|
|
78
|
+
return "bedrock";
|
|
79
|
+
}
|
|
80
|
+
getDefaultModel() {
|
|
81
|
+
return getBedrockModelId();
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Returns the Vercel AI SDK model instance for AWS Bedrock
|
|
85
|
+
*/
|
|
86
|
+
getAISDKModel() {
|
|
87
|
+
return this.model;
|
|
88
|
+
}
|
|
89
|
+
// executeGenerate removed - BaseProvider handles all generation with tools
|
|
90
|
+
async executeStream(options, analysisSchema) {
|
|
91
|
+
try {
|
|
92
|
+
this.validateStreamOptions(options);
|
|
93
|
+
const result = await streamText({
|
|
94
|
+
model: this.model,
|
|
95
|
+
prompt: options.input.text,
|
|
96
|
+
system: options.systemPrompt,
|
|
97
|
+
maxTokens: options.maxTokens || DEFAULT_MAX_TOKENS,
|
|
98
|
+
temperature: options.temperature,
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
stream: (async function* () {
|
|
102
|
+
for await (const chunk of result.textStream) {
|
|
103
|
+
yield { content: chunk };
|
|
104
|
+
}
|
|
105
|
+
})(),
|
|
106
|
+
provider: this.providerName,
|
|
107
|
+
model: this.modelName,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
throw this.handleProviderError(error);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
handleProviderError(error) {
|
|
115
|
+
if (error.name === "TimeoutError") {
|
|
116
|
+
return new TimeoutError(`Amazon Bedrock request timed out. Consider increasing timeout or using a lighter model.`, this.defaultTimeout);
|
|
117
|
+
}
|
|
118
|
+
if (error.message?.includes("InvalidRequestException")) {
|
|
119
|
+
return new Error(`❌ Amazon Bedrock Request Error\n\nThe request was invalid: ${error.message}\n\n🔧 Common Solutions:\n1. Check your model ID format\n2. Verify your request parameters\n3. Ensure your AWS account has Bedrock access`);
|
|
120
|
+
}
|
|
121
|
+
if (error.message?.includes("AccessDeniedException")) {
|
|
122
|
+
return new Error(`❌ Amazon Bedrock Access Denied\n\nYour AWS credentials don't have permission to access Bedrock.\n\n🔧 Required Steps:\n1. Ensure your IAM user has bedrock:InvokeModel permission\n2. Check if Bedrock is available in your region\n3. Verify model access is enabled in Bedrock console`);
|
|
123
|
+
}
|
|
124
|
+
if (error.message?.includes("ValidationException")) {
|
|
125
|
+
return new Error(`❌ Amazon Bedrock Validation Error\n\n${error.message}\n\n🔧 Check:\n1. Model ID format (should be ARN or model identifier)\n2. Request parameters are within limits\n3. Region configuration is correct`);
|
|
126
|
+
}
|
|
127
|
+
return new Error(`❌ Amazon Bedrock Provider Error\n\n${error.message || "Unknown error occurred"}\n\n🔧 Troubleshooting:\n1. Check AWS credentials and permissions\n2. Verify model availability\n3. Check network connectivity`);
|
|
128
|
+
}
|
|
129
|
+
validateStreamOptions(options) {
|
|
130
|
+
if (!options.input?.text?.trim()) {
|
|
131
|
+
throw new Error("Prompt is required for streaming");
|
|
132
|
+
}
|
|
133
|
+
if (options.maxTokens &&
|
|
134
|
+
(options.maxTokens < 1 || options.maxTokens > 4096)) {
|
|
135
|
+
throw new Error("maxTokens must be between 1 and 4096 for Amazon Bedrock");
|
|
136
|
+
}
|
|
137
|
+
if (options.temperature &&
|
|
138
|
+
(options.temperature < 0 || options.temperature > 1)) {
|
|
139
|
+
throw new Error("temperature must be between 0 and 1");
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
export default AmazonBedrockProvider;
|
|
@@ -130,10 +130,13 @@ export function enhanceAnalyticsWithEvaluation(analytics, evaluationResult) {
|
|
|
130
130
|
return {
|
|
131
131
|
...analytics,
|
|
132
132
|
evaluation: {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
133
|
+
// FIX: Use correct field names and ensure minimum score of 1
|
|
134
|
+
relevanceScore: evaluationResult.relevance || evaluationResult.relevanceScore || 1,
|
|
135
|
+
accuracyScore: evaluationResult.accuracy || evaluationResult.accuracyScore || 1,
|
|
136
|
+
completenessScore: evaluationResult.completeness ||
|
|
137
|
+
evaluationResult.completenessScore ||
|
|
138
|
+
1,
|
|
139
|
+
overall: evaluationResult.overall || 1,
|
|
137
140
|
evaluationProvider: evaluationResult.evaluationProvider,
|
|
138
141
|
evaluationTime: evaluationResult.evaluationTime,
|
|
139
142
|
evaluationAttempt: evaluationResult.evaluationAttempt,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ZodType, ZodTypeDef } from "zod";
|
|
2
|
+
import { type Schema } from "ai";
|
|
3
|
+
import type { AIProviderName } from "../core/types.js";
|
|
4
|
+
import type { StreamOptions, StreamResult } from "../types/stream-types.js";
|
|
5
|
+
import { BaseProvider } from "../core/base-provider.js";
|
|
6
|
+
/**
|
|
7
|
+
* Anthropic provider implementation using BaseProvider pattern
|
|
8
|
+
* Migrated from direct API calls to Vercel AI SDK (@ai-sdk/anthropic)
|
|
9
|
+
* Follows exact Google AI interface patterns for compatibility
|
|
10
|
+
*/
|
|
11
|
+
export declare class AnthropicProviderV2 extends BaseProvider {
|
|
12
|
+
constructor(modelName?: string);
|
|
13
|
+
protected getProviderName(): AIProviderName;
|
|
14
|
+
protected getDefaultModel(): string;
|
|
15
|
+
/**
|
|
16
|
+
* Returns the Vercel AI SDK model instance for Anthropic
|
|
17
|
+
*/
|
|
18
|
+
protected getAISDKModel(): any;
|
|
19
|
+
protected handleProviderError(error: any): Error;
|
|
20
|
+
private getApiKey;
|
|
21
|
+
protected executeStream(options: StreamOptions, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamResult>;
|
|
22
|
+
}
|
|
23
|
+
export default AnthropicProviderV2;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
2
|
+
import { streamText } from "ai";
|
|
3
|
+
import { BaseProvider } from "../core/base-provider.js";
|
|
4
|
+
import { logger } from "../utils/logger.js";
|
|
5
|
+
import { createTimeoutController, TimeoutError, } from "../utils/timeout.js";
|
|
6
|
+
import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
|
|
7
|
+
/**
|
|
8
|
+
* Anthropic provider implementation using BaseProvider pattern
|
|
9
|
+
* Migrated from direct API calls to Vercel AI SDK (@ai-sdk/anthropic)
|
|
10
|
+
* Follows exact Google AI interface patterns for compatibility
|
|
11
|
+
*/
|
|
12
|
+
export class AnthropicProviderV2 extends BaseProvider {
|
|
13
|
+
constructor(modelName) {
|
|
14
|
+
super(modelName, "anthropic");
|
|
15
|
+
logger.debug("AnthropicProviderV2 initialized", {
|
|
16
|
+
model: this.modelName,
|
|
17
|
+
provider: this.providerName,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
// ===================
|
|
21
|
+
// ABSTRACT METHOD IMPLEMENTATIONS
|
|
22
|
+
// ===================
|
|
23
|
+
getProviderName() {
|
|
24
|
+
return "anthropic";
|
|
25
|
+
}
|
|
26
|
+
getDefaultModel() {
|
|
27
|
+
return process.env.ANTHROPIC_MODEL || "claude-3-5-sonnet-20241022";
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Returns the Vercel AI SDK model instance for Anthropic
|
|
31
|
+
*/
|
|
32
|
+
getAISDKModel() {
|
|
33
|
+
const apiKey = this.getApiKey();
|
|
34
|
+
const anthropic = createAnthropic({ apiKey });
|
|
35
|
+
return anthropic(this.modelName);
|
|
36
|
+
}
|
|
37
|
+
handleProviderError(error) {
|
|
38
|
+
if (error instanceof TimeoutError) {
|
|
39
|
+
return new Error(`Anthropic request timed out: ${error.message}`);
|
|
40
|
+
}
|
|
41
|
+
if (error?.status === 401) {
|
|
42
|
+
return new Error("Invalid Anthropic API key. Please check your ANTHROPIC_API_KEY environment variable.");
|
|
43
|
+
}
|
|
44
|
+
if (error?.status === 429) {
|
|
45
|
+
return new Error("Anthropic rate limit exceeded. Please try again later.");
|
|
46
|
+
}
|
|
47
|
+
if (error?.status === 400) {
|
|
48
|
+
return new Error(`Anthropic bad request: ${error?.message || "Invalid request parameters"}`);
|
|
49
|
+
}
|
|
50
|
+
return new Error(`Anthropic error: ${error?.message || "Unknown error"}`);
|
|
51
|
+
}
|
|
52
|
+
getApiKey() {
|
|
53
|
+
const apiKey = process.env.ANTHROPIC_API_KEY;
|
|
54
|
+
if (!apiKey) {
|
|
55
|
+
throw new Error(`❌ ANTHROPIC Provider Configuration Error
|
|
56
|
+
|
|
57
|
+
Missing required environment variables: ANTHROPIC_API_KEY
|
|
58
|
+
|
|
59
|
+
🔧 Step 1: Get Credentials
|
|
60
|
+
Get your API key from https://console.anthropic.com/
|
|
61
|
+
|
|
62
|
+
💡 Step 2: Add to your .env file (or export in CLI):
|
|
63
|
+
ANTHROPIC_API_KEY="sk-ant-your-anthropic-api-key"
|
|
64
|
+
# Optional:
|
|
65
|
+
ANTHROPIC_MODEL="claude-3-5-sonnet-20241022"
|
|
66
|
+
ANTHROPIC_BASE_URL="https://api.anthropic.com"
|
|
67
|
+
|
|
68
|
+
🚀 Step 3: Test the setup:
|
|
69
|
+
npx neurolink generate "Hello" --provider anthropic
|
|
70
|
+
|
|
71
|
+
📖 More info: https://docs.neurolink.dev/providers/anthropic`);
|
|
72
|
+
}
|
|
73
|
+
return apiKey;
|
|
74
|
+
}
|
|
75
|
+
// executeGenerate removed - BaseProvider handles all generation with tools
|
|
76
|
+
async executeStream(options, analysisSchema) {
|
|
77
|
+
// Note: StreamOptions validation handled differently than TextGenerationOptions
|
|
78
|
+
const apiKey = this.getApiKey();
|
|
79
|
+
const anthropicClient = createAnthropic({ apiKey });
|
|
80
|
+
const model = anthropicClient(this.modelName);
|
|
81
|
+
const timeout = this.getTimeout(options);
|
|
82
|
+
const timeoutController = createTimeoutController(timeout, this.providerName, "stream");
|
|
83
|
+
try {
|
|
84
|
+
const result = await streamText({
|
|
85
|
+
model,
|
|
86
|
+
prompt: options.input.text,
|
|
87
|
+
system: options.systemPrompt,
|
|
88
|
+
temperature: options.temperature,
|
|
89
|
+
maxTokens: options.maxTokens || DEFAULT_MAX_TOKENS,
|
|
90
|
+
tools: options.tools,
|
|
91
|
+
toolChoice: "auto",
|
|
92
|
+
abortSignal: timeoutController?.controller.signal,
|
|
93
|
+
});
|
|
94
|
+
timeoutController?.cleanup();
|
|
95
|
+
// Transform string stream to content object stream (match Google AI pattern)
|
|
96
|
+
const transformedStream = async function* () {
|
|
97
|
+
for await (const chunk of result.textStream) {
|
|
98
|
+
yield { content: chunk };
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
return {
|
|
102
|
+
stream: transformedStream(),
|
|
103
|
+
provider: this.providerName,
|
|
104
|
+
model: this.modelName,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
timeoutController?.cleanup();
|
|
109
|
+
throw this.handleProviderError(error);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// Export for testing
|
|
114
|
+
export default AnthropicProviderV2;
|
|
@@ -1,48 +1,24 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Anthropic AI Provider (Direct API)
|
|
3
|
-
*
|
|
4
|
-
* Direct integration with Anthropic's Claude models via their native API.
|
|
5
|
-
* Supports Claude 3.5 Sonnet, Claude 3.5 Haiku, and Claude 3 Opus.
|
|
6
|
-
*/
|
|
7
|
-
import type { AIProvider, TextGenerationOptions, EnhancedGenerateResult } from "../core/types.js";
|
|
8
1
|
import type { ZodType, ZodTypeDef } from "zod";
|
|
9
|
-
import type
|
|
2
|
+
import { type Schema, type LanguageModelV1 } from "ai";
|
|
3
|
+
import type { AIProviderName } from "../core/types.js";
|
|
10
4
|
import type { StreamOptions, StreamResult } from "../types/stream-types.js";
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* PRIMARY METHOD: Stream content using AI (recommended for new code)
|
|
23
|
-
* Future-ready for multi-modal capabilities with current text focus
|
|
24
|
-
*/
|
|
25
|
-
stream(optionsOrPrompt: StreamOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamResult>;
|
|
26
|
-
generate(optionsOrPrompt: TextGenerationOptions | string, schema?: any): Promise<any>;
|
|
27
|
-
/**
|
|
28
|
-
* LEGACY METHOD: Use stream() instead for new code
|
|
29
|
-
* @deprecated Use stream() method instead
|
|
30
|
-
*/
|
|
31
|
-
private createAsyncIterable;
|
|
32
|
-
testConnection(): Promise<{
|
|
33
|
-
success: boolean;
|
|
34
|
-
error?: string;
|
|
35
|
-
responseTime?: number;
|
|
36
|
-
}>;
|
|
37
|
-
isConfigured(): boolean;
|
|
38
|
-
getRequiredConfig(): string[];
|
|
39
|
-
getOptionalConfig(): string[];
|
|
40
|
-
getModels(): string[];
|
|
41
|
-
supportsStreaming(): boolean;
|
|
42
|
-
supportsSchema(): boolean;
|
|
43
|
-
getCapabilities(): string[];
|
|
5
|
+
import { BaseProvider } from "../core/base-provider.js";
|
|
6
|
+
/**
|
|
7
|
+
* Anthropic Provider v2 - BaseProvider Implementation
|
|
8
|
+
* Fixed syntax and enhanced with proper error handling
|
|
9
|
+
*/
|
|
10
|
+
export declare class AnthropicProvider extends BaseProvider {
|
|
11
|
+
private model;
|
|
12
|
+
constructor(modelName?: string, sdk?: any);
|
|
13
|
+
protected getProviderName(): AIProviderName;
|
|
14
|
+
protected getDefaultModel(): string;
|
|
44
15
|
/**
|
|
45
|
-
*
|
|
16
|
+
* Returns the Vercel AI SDK model instance for Anthropic
|
|
46
17
|
*/
|
|
47
|
-
|
|
18
|
+
protected getAISDKModel(): LanguageModelV1;
|
|
19
|
+
protected handleProviderError(error: any): Error;
|
|
20
|
+
protected executeStream(options: StreamOptions, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamResult>;
|
|
21
|
+
isAvailable(): Promise<boolean>;
|
|
22
|
+
getModel(): LanguageModelV1;
|
|
48
23
|
}
|
|
24
|
+
export default AnthropicProvider;
|