@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
|
@@ -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 { AIProviderName } from
|
|
8
|
-
import { logger } from
|
|
7
|
+
import { AIProviderName } from "../core/types.js";
|
|
8
|
+
import { logger } from "../utils/logger.js";
|
|
9
9
|
export class AnthropicProvider {
|
|
10
10
|
name = AIProviderName.ANTHROPIC;
|
|
11
11
|
apiKey;
|
|
@@ -13,14 +13,16 @@ export class AnthropicProvider {
|
|
|
13
13
|
defaultModel;
|
|
14
14
|
constructor() {
|
|
15
15
|
this.apiKey = this.getApiKey();
|
|
16
|
-
this.baseURL =
|
|
17
|
-
|
|
16
|
+
this.baseURL =
|
|
17
|
+
process.env.ANTHROPIC_BASE_URL || "https://api.anthropic.com";
|
|
18
|
+
this.defaultModel =
|
|
19
|
+
process.env.ANTHROPIC_MODEL || "claude-3-5-sonnet-20241022";
|
|
18
20
|
logger.debug(`[AnthropicProvider] Initialized with model: ${this.defaultModel}`);
|
|
19
21
|
}
|
|
20
22
|
getApiKey() {
|
|
21
23
|
const apiKey = process.env.ANTHROPIC_API_KEY;
|
|
22
24
|
if (!apiKey) {
|
|
23
|
-
throw new Error(
|
|
25
|
+
throw new Error("ANTHROPIC_API_KEY environment variable is required");
|
|
24
26
|
}
|
|
25
27
|
return apiKey;
|
|
26
28
|
}
|
|
@@ -30,17 +32,17 @@ export class AnthropicProvider {
|
|
|
30
32
|
async makeRequest(endpoint, body, stream = false) {
|
|
31
33
|
const url = `${this.baseURL}/v1/${endpoint}`;
|
|
32
34
|
const headers = {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
"Content-Type": "application/json",
|
|
36
|
+
"x-api-key": this.apiKey,
|
|
37
|
+
"anthropic-version": "2023-06-01",
|
|
38
|
+
"anthropic-dangerous-direct-browser-access": "true", // Required for browser usage
|
|
37
39
|
};
|
|
38
|
-
logger.debug(`[AnthropicProvider.makeRequest] ${stream ?
|
|
40
|
+
logger.debug(`[AnthropicProvider.makeRequest] ${stream ? "Streaming" : "Non-streaming"} request to ${url}`);
|
|
39
41
|
logger.debug(`[AnthropicProvider.makeRequest] Model: ${body.model}, Max tokens: ${body.max_tokens}`);
|
|
40
42
|
const response = await fetch(url, {
|
|
41
|
-
method:
|
|
43
|
+
method: "POST",
|
|
42
44
|
headers,
|
|
43
|
-
body: JSON.stringify(body)
|
|
45
|
+
body: JSON.stringify(body),
|
|
44
46
|
});
|
|
45
47
|
if (!response.ok) {
|
|
46
48
|
const errorText = await response.text();
|
|
@@ -50,30 +52,30 @@ export class AnthropicProvider {
|
|
|
50
52
|
return response;
|
|
51
53
|
}
|
|
52
54
|
async generateText(optionsOrPrompt, schema) {
|
|
53
|
-
logger.debug(
|
|
55
|
+
logger.debug("[AnthropicProvider.generateText] Starting text generation");
|
|
54
56
|
// Parse parameters with backward compatibility
|
|
55
|
-
const options = typeof optionsOrPrompt ===
|
|
57
|
+
const options = typeof optionsOrPrompt === "string"
|
|
56
58
|
? { prompt: optionsOrPrompt }
|
|
57
59
|
: optionsOrPrompt;
|
|
58
|
-
const { prompt, temperature = 0.7, maxTokens = 500, systemPrompt =
|
|
60
|
+
const { prompt, temperature = 0.7, maxTokens = 500, systemPrompt = "You are Claude, an AI assistant created by Anthropic. You are helpful, harmless, and honest.", } = options;
|
|
59
61
|
logger.debug(`[AnthropicProvider.generateText] Prompt: "${prompt.substring(0, 100)}...", Temperature: ${temperature}, Max tokens: ${maxTokens}`);
|
|
60
62
|
const requestBody = {
|
|
61
63
|
model: this.getModel(),
|
|
62
64
|
max_tokens: maxTokens,
|
|
63
65
|
messages: [
|
|
64
66
|
{
|
|
65
|
-
role:
|
|
66
|
-
content: prompt
|
|
67
|
-
}
|
|
67
|
+
role: "user",
|
|
68
|
+
content: prompt,
|
|
69
|
+
},
|
|
68
70
|
],
|
|
69
71
|
temperature,
|
|
70
|
-
system: systemPrompt
|
|
72
|
+
system: systemPrompt,
|
|
71
73
|
};
|
|
72
74
|
try {
|
|
73
|
-
const response = await this.makeRequest(
|
|
75
|
+
const response = await this.makeRequest("messages", requestBody);
|
|
74
76
|
const data = await response.json();
|
|
75
77
|
logger.debug(`[AnthropicProvider.generateText] Success. Generated ${data.usage.output_tokens} tokens`);
|
|
76
|
-
const content = data.content.map(block => block.text).join(
|
|
78
|
+
const content = data.content.map((block) => block.text).join("");
|
|
77
79
|
return {
|
|
78
80
|
content,
|
|
79
81
|
provider: this.name,
|
|
@@ -81,83 +83,86 @@ export class AnthropicProvider {
|
|
|
81
83
|
usage: {
|
|
82
84
|
promptTokens: data.usage.input_tokens,
|
|
83
85
|
completionTokens: data.usage.output_tokens,
|
|
84
|
-
totalTokens: data.usage.input_tokens + data.usage.output_tokens
|
|
86
|
+
totalTokens: data.usage.input_tokens + data.usage.output_tokens,
|
|
85
87
|
},
|
|
86
|
-
finishReason: data.stop_reason
|
|
88
|
+
finishReason: data.stop_reason,
|
|
87
89
|
};
|
|
88
90
|
}
|
|
89
91
|
catch (error) {
|
|
90
|
-
logger.error(
|
|
92
|
+
logger.error("[AnthropicProvider.generateText] Error:", error);
|
|
91
93
|
throw error;
|
|
92
94
|
}
|
|
93
95
|
}
|
|
94
96
|
async streamText(optionsOrPrompt, schema) {
|
|
95
|
-
logger.debug(
|
|
97
|
+
logger.debug("[AnthropicProvider.streamText] Starting text streaming");
|
|
96
98
|
// Parse parameters with backward compatibility
|
|
97
|
-
const options = typeof optionsOrPrompt ===
|
|
99
|
+
const options = typeof optionsOrPrompt === "string"
|
|
98
100
|
? { prompt: optionsOrPrompt }
|
|
99
101
|
: optionsOrPrompt;
|
|
100
|
-
const { prompt, temperature = 0.7, maxTokens = 500, systemPrompt =
|
|
102
|
+
const { prompt, temperature = 0.7, maxTokens = 500, systemPrompt = "You are Claude, an AI assistant created by Anthropic. You are helpful, harmless, and honest.", } = options;
|
|
101
103
|
logger.debug(`[AnthropicProvider.streamText] Streaming prompt: "${prompt.substring(0, 100)}..."`);
|
|
102
104
|
const requestBody = {
|
|
103
105
|
model: this.getModel(),
|
|
104
106
|
max_tokens: maxTokens,
|
|
105
107
|
messages: [
|
|
106
108
|
{
|
|
107
|
-
role:
|
|
108
|
-
content: prompt
|
|
109
|
-
}
|
|
109
|
+
role: "user",
|
|
110
|
+
content: prompt,
|
|
111
|
+
},
|
|
110
112
|
],
|
|
111
113
|
temperature,
|
|
112
114
|
system: systemPrompt,
|
|
113
|
-
stream: true
|
|
115
|
+
stream: true,
|
|
114
116
|
};
|
|
115
117
|
try {
|
|
116
|
-
const response = await this.makeRequest(
|
|
118
|
+
const response = await this.makeRequest("messages", requestBody, true);
|
|
117
119
|
if (!response.body) {
|
|
118
|
-
throw new Error(
|
|
120
|
+
throw new Error("No response body received");
|
|
119
121
|
}
|
|
120
122
|
// Return a StreamTextResult-like object
|
|
121
123
|
return {
|
|
122
124
|
textStream: this.createAsyncIterable(response.body),
|
|
123
|
-
text:
|
|
125
|
+
text: "",
|
|
124
126
|
usage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 },
|
|
125
|
-
finishReason:
|
|
127
|
+
finishReason: "end_turn",
|
|
126
128
|
};
|
|
127
129
|
}
|
|
128
130
|
catch (error) {
|
|
129
|
-
logger.error(
|
|
131
|
+
logger.error("[AnthropicProvider.streamText] Error:", error);
|
|
130
132
|
throw error;
|
|
131
133
|
}
|
|
132
134
|
}
|
|
133
135
|
async *createAsyncIterable(body) {
|
|
134
136
|
const reader = body.getReader();
|
|
135
137
|
const decoder = new TextDecoder();
|
|
136
|
-
let buffer =
|
|
138
|
+
let buffer = "";
|
|
137
139
|
try {
|
|
138
140
|
while (true) {
|
|
139
141
|
const { done, value } = await reader.read();
|
|
140
|
-
if (done)
|
|
142
|
+
if (done) {
|
|
141
143
|
break;
|
|
144
|
+
}
|
|
142
145
|
buffer += decoder.decode(value, { stream: true });
|
|
143
|
-
const lines = buffer.split(
|
|
144
|
-
buffer = lines.pop() ||
|
|
146
|
+
const lines = buffer.split("\n");
|
|
147
|
+
buffer = lines.pop() || "";
|
|
145
148
|
for (const line of lines) {
|
|
146
|
-
if (line.trim() ===
|
|
149
|
+
if (line.trim() === "") {
|
|
147
150
|
continue;
|
|
148
|
-
|
|
151
|
+
}
|
|
152
|
+
if (line.startsWith("data: ")) {
|
|
149
153
|
const data = line.slice(6);
|
|
150
|
-
if (data.trim() ===
|
|
154
|
+
if (data.trim() === "[DONE]") {
|
|
151
155
|
continue;
|
|
156
|
+
}
|
|
152
157
|
try {
|
|
153
158
|
const chunk = JSON.parse(data);
|
|
154
159
|
// Extract text content from different chunk types
|
|
155
|
-
if (chunk.type ===
|
|
160
|
+
if (chunk.type === "content_block_delta" && chunk.delta?.text) {
|
|
156
161
|
yield chunk.delta.text;
|
|
157
162
|
}
|
|
158
163
|
}
|
|
159
164
|
catch (parseError) {
|
|
160
|
-
logger.warn(
|
|
165
|
+
logger.warn("[AnthropicProvider.createAsyncIterable] Failed to parse chunk:", parseError);
|
|
161
166
|
continue;
|
|
162
167
|
}
|
|
163
168
|
}
|
|
@@ -169,62 +174,65 @@ export class AnthropicProvider {
|
|
|
169
174
|
}
|
|
170
175
|
}
|
|
171
176
|
async *generateTextStream(optionsOrPrompt) {
|
|
172
|
-
logger.debug(
|
|
177
|
+
logger.debug("[AnthropicProvider.generateTextStream] Starting text streaming");
|
|
173
178
|
// Parse parameters with backward compatibility
|
|
174
|
-
const options = typeof optionsOrPrompt ===
|
|
179
|
+
const options = typeof optionsOrPrompt === "string"
|
|
175
180
|
? { prompt: optionsOrPrompt }
|
|
176
181
|
: optionsOrPrompt;
|
|
177
|
-
const { prompt, temperature = 0.7, maxTokens = 500, systemPrompt =
|
|
182
|
+
const { prompt, temperature = 0.7, maxTokens = 500, systemPrompt = "You are Claude, an AI assistant created by Anthropic. You are helpful, harmless, and honest.", } = options;
|
|
178
183
|
logger.debug(`[AnthropicProvider.generateTextStream] Streaming prompt: "${prompt.substring(0, 100)}..."`);
|
|
179
184
|
const requestBody = {
|
|
180
185
|
model: this.getModel(),
|
|
181
186
|
max_tokens: maxTokens,
|
|
182
187
|
messages: [
|
|
183
188
|
{
|
|
184
|
-
role:
|
|
185
|
-
content: prompt
|
|
186
|
-
}
|
|
189
|
+
role: "user",
|
|
190
|
+
content: prompt,
|
|
191
|
+
},
|
|
187
192
|
],
|
|
188
193
|
temperature,
|
|
189
194
|
system: systemPrompt,
|
|
190
|
-
stream: true
|
|
195
|
+
stream: true,
|
|
191
196
|
};
|
|
192
197
|
try {
|
|
193
|
-
const response = await this.makeRequest(
|
|
198
|
+
const response = await this.makeRequest("messages", requestBody, true);
|
|
194
199
|
if (!response.body) {
|
|
195
|
-
throw new Error(
|
|
200
|
+
throw new Error("No response body received");
|
|
196
201
|
}
|
|
197
202
|
const reader = response.body.getReader();
|
|
198
203
|
const decoder = new TextDecoder();
|
|
199
|
-
let buffer =
|
|
204
|
+
let buffer = "";
|
|
200
205
|
try {
|
|
201
206
|
while (true) {
|
|
202
207
|
const { done, value } = await reader.read();
|
|
203
|
-
if (done)
|
|
208
|
+
if (done) {
|
|
204
209
|
break;
|
|
210
|
+
}
|
|
205
211
|
buffer += decoder.decode(value, { stream: true });
|
|
206
|
-
const lines = buffer.split(
|
|
207
|
-
buffer = lines.pop() ||
|
|
212
|
+
const lines = buffer.split("\n");
|
|
213
|
+
buffer = lines.pop() || "";
|
|
208
214
|
for (const line of lines) {
|
|
209
|
-
if (line.trim() ===
|
|
215
|
+
if (line.trim() === "") {
|
|
210
216
|
continue;
|
|
211
|
-
|
|
217
|
+
}
|
|
218
|
+
if (line.startsWith("data: ")) {
|
|
212
219
|
const data = line.slice(6);
|
|
213
|
-
if (data.trim() ===
|
|
220
|
+
if (data.trim() === "[DONE]") {
|
|
214
221
|
continue;
|
|
222
|
+
}
|
|
215
223
|
try {
|
|
216
224
|
const chunk = JSON.parse(data);
|
|
217
225
|
// Extract text content from different chunk types
|
|
218
|
-
if (chunk.type ===
|
|
226
|
+
if (chunk.type === "content_block_delta" && chunk.delta?.text) {
|
|
219
227
|
yield {
|
|
220
228
|
content: chunk.delta.text,
|
|
221
229
|
provider: this.name,
|
|
222
|
-
model: this.getModel()
|
|
230
|
+
model: this.getModel(),
|
|
223
231
|
};
|
|
224
232
|
}
|
|
225
233
|
}
|
|
226
234
|
catch (parseError) {
|
|
227
|
-
logger.warn(
|
|
235
|
+
logger.warn("[AnthropicProvider.generateTextStream] Failed to parse chunk:", parseError);
|
|
228
236
|
continue;
|
|
229
237
|
}
|
|
230
238
|
}
|
|
@@ -234,26 +242,26 @@ export class AnthropicProvider {
|
|
|
234
242
|
finally {
|
|
235
243
|
reader.releaseLock();
|
|
236
244
|
}
|
|
237
|
-
logger.debug(
|
|
245
|
+
logger.debug("[AnthropicProvider.generateTextStream] Streaming completed");
|
|
238
246
|
}
|
|
239
247
|
catch (error) {
|
|
240
|
-
logger.error(
|
|
248
|
+
logger.error("[AnthropicProvider.generateTextStream] Error:", error);
|
|
241
249
|
throw error;
|
|
242
250
|
}
|
|
243
251
|
}
|
|
244
252
|
async testConnection() {
|
|
245
|
-
logger.debug(
|
|
253
|
+
logger.debug("[AnthropicProvider.testConnection] Testing connection to Anthropic API");
|
|
246
254
|
const startTime = Date.now();
|
|
247
255
|
try {
|
|
248
256
|
await this.generateText({
|
|
249
|
-
prompt:
|
|
250
|
-
maxTokens: 5
|
|
257
|
+
prompt: "Hello",
|
|
258
|
+
maxTokens: 5,
|
|
251
259
|
});
|
|
252
260
|
const responseTime = Date.now() - startTime;
|
|
253
261
|
logger.debug(`[AnthropicProvider.testConnection] Connection test successful (${responseTime}ms)`);
|
|
254
262
|
return {
|
|
255
263
|
success: true,
|
|
256
|
-
responseTime
|
|
264
|
+
responseTime,
|
|
257
265
|
};
|
|
258
266
|
}
|
|
259
267
|
catch (error) {
|
|
@@ -261,8 +269,8 @@ export class AnthropicProvider {
|
|
|
261
269
|
logger.error(`[AnthropicProvider.testConnection] Connection test failed (${responseTime}ms):`, error);
|
|
262
270
|
return {
|
|
263
271
|
success: false,
|
|
264
|
-
error: error instanceof Error ? error.message :
|
|
265
|
-
responseTime
|
|
272
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
273
|
+
responseTime,
|
|
266
274
|
};
|
|
267
275
|
}
|
|
268
276
|
}
|
|
@@ -276,18 +284,18 @@ export class AnthropicProvider {
|
|
|
276
284
|
}
|
|
277
285
|
}
|
|
278
286
|
getRequiredConfig() {
|
|
279
|
-
return [
|
|
287
|
+
return ["ANTHROPIC_API_KEY"];
|
|
280
288
|
}
|
|
281
289
|
getOptionalConfig() {
|
|
282
|
-
return [
|
|
290
|
+
return ["ANTHROPIC_MODEL", "ANTHROPIC_BASE_URL"];
|
|
283
291
|
}
|
|
284
292
|
getModels() {
|
|
285
293
|
return [
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
294
|
+
"claude-3-5-sonnet-20241022",
|
|
295
|
+
"claude-3-5-haiku-20241022",
|
|
296
|
+
"claude-3-opus-20240229",
|
|
297
|
+
"claude-3-sonnet-20240229",
|
|
298
|
+
"claude-3-haiku-20240307",
|
|
291
299
|
];
|
|
292
300
|
}
|
|
293
301
|
supportsStreaming() {
|
|
@@ -298,11 +306,11 @@ export class AnthropicProvider {
|
|
|
298
306
|
}
|
|
299
307
|
getCapabilities() {
|
|
300
308
|
return [
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
309
|
+
"text-generation",
|
|
310
|
+
"streaming",
|
|
311
|
+
"conversation",
|
|
312
|
+
"system-prompts",
|
|
313
|
+
"long-context", // Claude models support up to 200k tokens
|
|
306
314
|
];
|
|
307
315
|
}
|
|
308
316
|
}
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* Enterprise-grade OpenAI integration through Microsoft Azure.
|
|
5
5
|
* Supports all OpenAI models with enhanced security and compliance.
|
|
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 AzureOpenAIProvider implements AIProvider {
|
|
10
10
|
readonly name: AIProviderName;
|
|
11
11
|
private apiKey;
|