@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
|
@@ -1,437 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Azure OpenAI Provider
|
|
3
|
-
*
|
|
4
|
-
* Enterprise-grade OpenAI integration through Microsoft Azure.
|
|
5
|
-
* Supports all OpenAI models with enhanced security and compliance.
|
|
6
|
-
*/
|
|
7
|
-
import { AIProviderName } from "../core/types.js";
|
|
8
|
-
import { logger } from "../utils/logger.js";
|
|
9
|
-
import { createTimeoutController, TimeoutError, getDefaultTimeout, } from "../utils/timeout.js";
|
|
10
|
-
import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
|
|
11
|
-
import { evaluateResponse } from "../core/evaluation.js";
|
|
12
|
-
import { createAnalytics } from "../core/analytics.js";
|
|
13
|
-
export class AzureOpenAIProvider {
|
|
14
|
-
name = AIProviderName.AZURE;
|
|
15
|
-
apiKey;
|
|
16
|
-
endpoint;
|
|
17
|
-
deploymentId;
|
|
18
|
-
apiVersion;
|
|
19
|
-
constructor() {
|
|
20
|
-
this.apiKey = this.getApiKey();
|
|
21
|
-
this.endpoint = this.getEndpoint();
|
|
22
|
-
this.deploymentId = this.getDeploymentId();
|
|
23
|
-
this.apiVersion =
|
|
24
|
-
process.env.AZURE_OPENAI_API_VERSION || "2024-02-15-preview";
|
|
25
|
-
logger.debug(`[AzureOpenAIProvider] Initialized with endpoint: ${this.endpoint}, deployment: ${this.deploymentId}`);
|
|
26
|
-
}
|
|
27
|
-
getApiKey() {
|
|
28
|
-
const apiKey = process.env.AZURE_OPENAI_API_KEY;
|
|
29
|
-
if (!apiKey) {
|
|
30
|
-
throw new Error("AZURE_OPENAI_API_KEY environment variable is required");
|
|
31
|
-
}
|
|
32
|
-
return apiKey;
|
|
33
|
-
}
|
|
34
|
-
getEndpoint() {
|
|
35
|
-
const endpoint = process.env.AZURE_OPENAI_ENDPOINT;
|
|
36
|
-
if (!endpoint) {
|
|
37
|
-
throw new Error("AZURE_OPENAI_ENDPOINT environment variable is required");
|
|
38
|
-
}
|
|
39
|
-
return endpoint.replace(/\/$/, ""); // Remove trailing slash
|
|
40
|
-
}
|
|
41
|
-
getDeploymentId() {
|
|
42
|
-
const deploymentId = process.env.AZURE_OPENAI_DEPLOYMENT_ID;
|
|
43
|
-
if (!deploymentId) {
|
|
44
|
-
throw new Error("AZURE_OPENAI_DEPLOYMENT_ID environment variable is required");
|
|
45
|
-
}
|
|
46
|
-
return deploymentId;
|
|
47
|
-
}
|
|
48
|
-
getApiUrl(stream = false) {
|
|
49
|
-
return `${this.endpoint}/openai/deployments/${this.deploymentId}/chat/completions?api-version=${this.apiVersion}`;
|
|
50
|
-
}
|
|
51
|
-
async makeRequest(body, stream = false, signal) {
|
|
52
|
-
const url = this.getApiUrl(stream);
|
|
53
|
-
const headers = {
|
|
54
|
-
"Content-Type": "application/json",
|
|
55
|
-
"api-key": this.apiKey,
|
|
56
|
-
};
|
|
57
|
-
logger.debug(`[AzureOpenAIProvider.makeRequest] ${stream ? "Streaming" : "Non-streaming"} request to deployment: ${this.deploymentId}`);
|
|
58
|
-
logger.debug(`[AzureOpenAIProvider.makeRequest] Max tokens: ${body.max_tokens || "default"}, Temperature: ${body.temperature || "default"}`);
|
|
59
|
-
const response = await fetch(url, {
|
|
60
|
-
method: "POST",
|
|
61
|
-
headers,
|
|
62
|
-
body: JSON.stringify(body),
|
|
63
|
-
signal, // Add abort signal for timeout support
|
|
64
|
-
});
|
|
65
|
-
if (!response.ok) {
|
|
66
|
-
const errorText = await response.text();
|
|
67
|
-
logger.error(`[AzureOpenAIProvider.makeRequest] API error ${response.status}: ${errorText}`);
|
|
68
|
-
throw new Error(`Azure OpenAI API error ${response.status}: ${errorText}`);
|
|
69
|
-
}
|
|
70
|
-
return response;
|
|
71
|
-
}
|
|
72
|
-
async generateText(optionsOrPrompt, schema) {
|
|
73
|
-
const functionTag = "AzureOpenAIProvider.generateText";
|
|
74
|
-
const provider = "azure";
|
|
75
|
-
const startTime = Date.now();
|
|
76
|
-
logger.debug(`[${functionTag}] Starting text generation`);
|
|
77
|
-
// Parse parameters with backward compatibility
|
|
78
|
-
const options = typeof optionsOrPrompt === "string"
|
|
79
|
-
? { prompt: optionsOrPrompt }
|
|
80
|
-
: optionsOrPrompt;
|
|
81
|
-
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = "You are a helpful AI assistant.", timeout = getDefaultTimeout(provider, "generate"), } = options;
|
|
82
|
-
logger.debug(`[${functionTag}] Prompt: "${prompt.substring(0, 100)}...", Temperature: ${temperature}, Max tokens: ${maxTokens}, Timeout: ${timeout}`);
|
|
83
|
-
const messages = [];
|
|
84
|
-
if (systemPrompt) {
|
|
85
|
-
messages.push({
|
|
86
|
-
role: "system",
|
|
87
|
-
content: systemPrompt,
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
messages.push({
|
|
91
|
-
role: "user",
|
|
92
|
-
content: prompt,
|
|
93
|
-
});
|
|
94
|
-
const requestBody = {
|
|
95
|
-
messages,
|
|
96
|
-
temperature,
|
|
97
|
-
max_tokens: maxTokens,
|
|
98
|
-
};
|
|
99
|
-
// Create timeout controller if timeout is specified
|
|
100
|
-
const timeoutController = createTimeoutController(timeout, provider, "generate");
|
|
101
|
-
try {
|
|
102
|
-
const response = await this.makeRequest(requestBody, false, timeoutController?.controller.signal);
|
|
103
|
-
const data = await response.json();
|
|
104
|
-
// Clean up timeout if successful
|
|
105
|
-
timeoutController?.cleanup();
|
|
106
|
-
logger.debug(`[${functionTag}] Success. Generated ${data.usage.completion_tokens} tokens`);
|
|
107
|
-
const content = data.choices[0]?.message?.content || "";
|
|
108
|
-
const result = {
|
|
109
|
-
content,
|
|
110
|
-
provider: this.name,
|
|
111
|
-
model: data.model,
|
|
112
|
-
usage: {
|
|
113
|
-
promptTokens: data.usage.prompt_tokens,
|
|
114
|
-
completionTokens: data.usage.completion_tokens,
|
|
115
|
-
totalTokens: data.usage.total_tokens,
|
|
116
|
-
},
|
|
117
|
-
finishReason: data.choices[0]?.finish_reason || "stop",
|
|
118
|
-
};
|
|
119
|
-
// Add analytics if enabled
|
|
120
|
-
if (options.enableAnalytics) {
|
|
121
|
-
result.analytics = {
|
|
122
|
-
provider: this.name,
|
|
123
|
-
model: data.model,
|
|
124
|
-
tokens: result.usage,
|
|
125
|
-
responseTime: Date.now() - startTime,
|
|
126
|
-
context: options.context,
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
// Add evaluation if enabled
|
|
130
|
-
if (options.enableEvaluation) {
|
|
131
|
-
result.evaluation = await evaluateResponse(options.prompt, content, options.context);
|
|
132
|
-
}
|
|
133
|
-
return result;
|
|
134
|
-
}
|
|
135
|
-
catch (error) {
|
|
136
|
-
// Always cleanup timeout
|
|
137
|
-
timeoutController?.cleanup();
|
|
138
|
-
// Log timeout errors specifically
|
|
139
|
-
if (error instanceof TimeoutError) {
|
|
140
|
-
logger.error(`[${functionTag}] Timeout error`, {
|
|
141
|
-
provider,
|
|
142
|
-
timeout: error.timeout,
|
|
143
|
-
message: error.message,
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
else if (error?.name === "AbortError") {
|
|
147
|
-
// Convert AbortError to TimeoutError
|
|
148
|
-
const timeoutError = new TimeoutError(`${provider} generate operation timed out after ${timeout}`, timeoutController?.timeoutMs || 0, provider, "generate");
|
|
149
|
-
logger.error(`[${functionTag}] Timeout error`, {
|
|
150
|
-
provider,
|
|
151
|
-
timeout: timeoutController?.timeoutMs,
|
|
152
|
-
message: timeoutError.message,
|
|
153
|
-
});
|
|
154
|
-
throw timeoutError;
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
logger.error(`[${functionTag}] Error:`, error);
|
|
158
|
-
}
|
|
159
|
-
throw error;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
async streamText(optionsOrPrompt, schema) {
|
|
163
|
-
const functionTag = "AzureOpenAIProvider.streamText";
|
|
164
|
-
const provider = "azure";
|
|
165
|
-
logger.debug(`[${functionTag}] Starting text streaming`);
|
|
166
|
-
// Parse parameters with backward compatibility
|
|
167
|
-
const options = typeof optionsOrPrompt === "string"
|
|
168
|
-
? { prompt: optionsOrPrompt }
|
|
169
|
-
: optionsOrPrompt;
|
|
170
|
-
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = "You are a helpful AI assistant.", timeout = getDefaultTimeout(provider, "stream"), } = options;
|
|
171
|
-
logger.debug(`[${functionTag}] Streaming prompt: "${prompt.substring(0, 100)}...", Timeout: ${timeout}`);
|
|
172
|
-
const messages = [];
|
|
173
|
-
if (systemPrompt) {
|
|
174
|
-
messages.push({
|
|
175
|
-
role: "system",
|
|
176
|
-
content: systemPrompt,
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
messages.push({
|
|
180
|
-
role: "user",
|
|
181
|
-
content: prompt,
|
|
182
|
-
});
|
|
183
|
-
const requestBody = {
|
|
184
|
-
messages,
|
|
185
|
-
temperature,
|
|
186
|
-
max_tokens: maxTokens,
|
|
187
|
-
stream: true,
|
|
188
|
-
};
|
|
189
|
-
// Create timeout controller if timeout is specified
|
|
190
|
-
const timeoutController = createTimeoutController(timeout, provider, "stream");
|
|
191
|
-
try {
|
|
192
|
-
const response = await this.makeRequest(requestBody, true, timeoutController?.controller.signal);
|
|
193
|
-
if (!response.body) {
|
|
194
|
-
throw new Error("No response body received");
|
|
195
|
-
}
|
|
196
|
-
// Return a StreamTextResult-like object with timeout signal
|
|
197
|
-
return {
|
|
198
|
-
textStream: this.createAsyncIterable(response.body, timeoutController?.controller.signal),
|
|
199
|
-
text: "",
|
|
200
|
-
usage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 },
|
|
201
|
-
finishReason: "stop",
|
|
202
|
-
// Store timeout controller for external cleanup if needed
|
|
203
|
-
_timeoutController: timeoutController,
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
catch (error) {
|
|
207
|
-
// Cleanup timeout on error
|
|
208
|
-
timeoutController?.cleanup();
|
|
209
|
-
// Log timeout errors specifically
|
|
210
|
-
if (error instanceof TimeoutError) {
|
|
211
|
-
logger.error(`[${functionTag}] Timeout error`, {
|
|
212
|
-
provider,
|
|
213
|
-
timeout: error.timeout,
|
|
214
|
-
message: error.message,
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
else if (error?.name === "AbortError") {
|
|
218
|
-
// Convert AbortError to TimeoutError
|
|
219
|
-
const timeoutError = new TimeoutError(`${provider} stream operation timed out after ${timeout}`, timeoutController?.timeoutMs || 0, provider, "stream");
|
|
220
|
-
logger.error(`[${functionTag}] Timeout error`, {
|
|
221
|
-
provider,
|
|
222
|
-
timeout: timeoutController?.timeoutMs,
|
|
223
|
-
message: timeoutError.message,
|
|
224
|
-
});
|
|
225
|
-
throw timeoutError;
|
|
226
|
-
}
|
|
227
|
-
else {
|
|
228
|
-
logger.error(`[${functionTag}] Error:`, error);
|
|
229
|
-
}
|
|
230
|
-
throw error;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
async *createAsyncIterable(body, signal) {
|
|
234
|
-
const reader = body.getReader();
|
|
235
|
-
const decoder = new TextDecoder();
|
|
236
|
-
let buffer = "";
|
|
237
|
-
try {
|
|
238
|
-
while (true) {
|
|
239
|
-
// Check if aborted
|
|
240
|
-
if (signal?.aborted) {
|
|
241
|
-
throw new Error("AbortError");
|
|
242
|
-
}
|
|
243
|
-
const { done, value } = await reader.read();
|
|
244
|
-
if (done) {
|
|
245
|
-
break;
|
|
246
|
-
}
|
|
247
|
-
buffer += decoder.decode(value, { stream: true });
|
|
248
|
-
const lines = buffer.split("\n");
|
|
249
|
-
buffer = lines.pop() || "";
|
|
250
|
-
for (const line of lines) {
|
|
251
|
-
if (line.trim() === "") {
|
|
252
|
-
continue;
|
|
253
|
-
}
|
|
254
|
-
if (line.startsWith("data: ")) {
|
|
255
|
-
const data = line.slice(6);
|
|
256
|
-
if (data.trim() === "[DONE]") {
|
|
257
|
-
continue;
|
|
258
|
-
}
|
|
259
|
-
try {
|
|
260
|
-
const chunk = JSON.parse(data);
|
|
261
|
-
// Extract text content from chunk
|
|
262
|
-
if (chunk.choices?.[0]?.delta?.content) {
|
|
263
|
-
yield chunk.choices[0].delta.content;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
catch (parseError) {
|
|
267
|
-
logger.warn("[AzureOpenAIProvider.createAsyncIterable] Failed to parse chunk:", parseError);
|
|
268
|
-
continue;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
finally {
|
|
275
|
-
reader.releaseLock();
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
async *generateTextStream(optionsOrPrompt) {
|
|
279
|
-
logger.debug("[AzureOpenAIProvider.generateTextStream] Starting text streaming");
|
|
280
|
-
// Parse parameters with backward compatibility
|
|
281
|
-
const options = typeof optionsOrPrompt === "string"
|
|
282
|
-
? { prompt: optionsOrPrompt }
|
|
283
|
-
: optionsOrPrompt;
|
|
284
|
-
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = "You are a helpful AI assistant.", } = options;
|
|
285
|
-
logger.debug(`[AzureOpenAIProvider.generateTextStream] Streaming prompt: "${prompt.substring(0, 100)}..."`);
|
|
286
|
-
const messages = [];
|
|
287
|
-
if (systemPrompt) {
|
|
288
|
-
messages.push({
|
|
289
|
-
role: "system",
|
|
290
|
-
content: systemPrompt,
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
messages.push({
|
|
294
|
-
role: "user",
|
|
295
|
-
content: prompt,
|
|
296
|
-
});
|
|
297
|
-
const requestBody = {
|
|
298
|
-
messages,
|
|
299
|
-
temperature,
|
|
300
|
-
max_tokens: maxTokens,
|
|
301
|
-
stream: true,
|
|
302
|
-
};
|
|
303
|
-
try {
|
|
304
|
-
const response = await this.makeRequest(requestBody, true);
|
|
305
|
-
if (!response.body) {
|
|
306
|
-
throw new Error("No response body received");
|
|
307
|
-
}
|
|
308
|
-
const reader = response.body.getReader();
|
|
309
|
-
const decoder = new TextDecoder();
|
|
310
|
-
let buffer = "";
|
|
311
|
-
try {
|
|
312
|
-
while (true) {
|
|
313
|
-
const { done, value } = await reader.read();
|
|
314
|
-
if (done) {
|
|
315
|
-
break;
|
|
316
|
-
}
|
|
317
|
-
buffer += decoder.decode(value, { stream: true });
|
|
318
|
-
const lines = buffer.split("\n");
|
|
319
|
-
buffer = lines.pop() || "";
|
|
320
|
-
for (const line of lines) {
|
|
321
|
-
if (line.trim() === "") {
|
|
322
|
-
continue;
|
|
323
|
-
}
|
|
324
|
-
if (line.startsWith("data: ")) {
|
|
325
|
-
const data = line.slice(6);
|
|
326
|
-
if (data.trim() === "[DONE]") {
|
|
327
|
-
continue;
|
|
328
|
-
}
|
|
329
|
-
try {
|
|
330
|
-
const chunk = JSON.parse(data);
|
|
331
|
-
// Extract text content from chunk
|
|
332
|
-
if (chunk.choices?.[0]?.delta?.content) {
|
|
333
|
-
yield {
|
|
334
|
-
content: chunk.choices[0].delta.content,
|
|
335
|
-
provider: this.name,
|
|
336
|
-
model: chunk.model || this.deploymentId,
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
catch (parseError) {
|
|
341
|
-
logger.warn("[AzureOpenAIProvider.generateTextStream] Failed to parse chunk:", parseError);
|
|
342
|
-
continue;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
finally {
|
|
349
|
-
reader.releaseLock();
|
|
350
|
-
}
|
|
351
|
-
logger.debug("[AzureOpenAIProvider.generateTextStream] Streaming completed");
|
|
352
|
-
}
|
|
353
|
-
catch (error) {
|
|
354
|
-
logger.error("[AzureOpenAIProvider.generateTextStream] Error:", error);
|
|
355
|
-
throw error;
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
async testConnection() {
|
|
359
|
-
logger.debug("[AzureOpenAIProvider.testConnection] Testing connection to Azure OpenAI");
|
|
360
|
-
const startTime = Date.now();
|
|
361
|
-
try {
|
|
362
|
-
await this.generateText({
|
|
363
|
-
prompt: "Hello",
|
|
364
|
-
maxTokens: 5,
|
|
365
|
-
});
|
|
366
|
-
const responseTime = Date.now() - startTime;
|
|
367
|
-
logger.debug(`[AzureOpenAIProvider.testConnection] Connection test successful (${responseTime}ms)`);
|
|
368
|
-
return {
|
|
369
|
-
success: true,
|
|
370
|
-
responseTime,
|
|
371
|
-
};
|
|
372
|
-
}
|
|
373
|
-
catch (error) {
|
|
374
|
-
const responseTime = Date.now() - startTime;
|
|
375
|
-
logger.error(`[AzureOpenAIProvider.testConnection] Connection test failed (${responseTime}ms):`, error);
|
|
376
|
-
return {
|
|
377
|
-
success: false,
|
|
378
|
-
error: error instanceof Error ? error.message : "Unknown error",
|
|
379
|
-
responseTime,
|
|
380
|
-
};
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
isConfigured() {
|
|
384
|
-
try {
|
|
385
|
-
this.getApiKey();
|
|
386
|
-
this.getEndpoint();
|
|
387
|
-
this.getDeploymentId();
|
|
388
|
-
return true;
|
|
389
|
-
}
|
|
390
|
-
catch {
|
|
391
|
-
return false;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
getRequiredConfig() {
|
|
395
|
-
return [
|
|
396
|
-
"AZURE_OPENAI_API_KEY",
|
|
397
|
-
"AZURE_OPENAI_ENDPOINT",
|
|
398
|
-
"AZURE_OPENAI_DEPLOYMENT_ID",
|
|
399
|
-
];
|
|
400
|
-
}
|
|
401
|
-
getOptionalConfig() {
|
|
402
|
-
return ["AZURE_OPENAI_API_VERSION"];
|
|
403
|
-
}
|
|
404
|
-
getModels() {
|
|
405
|
-
return [
|
|
406
|
-
"gpt-4",
|
|
407
|
-
"gpt-4-turbo",
|
|
408
|
-
"gpt-4-32k",
|
|
409
|
-
"gpt-35-turbo",
|
|
410
|
-
"gpt-35-turbo-16k",
|
|
411
|
-
];
|
|
412
|
-
}
|
|
413
|
-
supportsStreaming() {
|
|
414
|
-
return true;
|
|
415
|
-
}
|
|
416
|
-
supportsSchema() {
|
|
417
|
-
return true; // Azure OpenAI supports JSON mode and function calling
|
|
418
|
-
}
|
|
419
|
-
getCapabilities() {
|
|
420
|
-
return [
|
|
421
|
-
"text-generation",
|
|
422
|
-
"streaming",
|
|
423
|
-
"conversation",
|
|
424
|
-
"system-prompts",
|
|
425
|
-
"json-mode",
|
|
426
|
-
"function-calling",
|
|
427
|
-
"enterprise-security",
|
|
428
|
-
"content-filtering",
|
|
429
|
-
];
|
|
430
|
-
}
|
|
431
|
-
async generate(optionsOrPrompt, analysisSchema) {
|
|
432
|
-
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
433
|
-
}
|
|
434
|
-
async gen(optionsOrPrompt, analysisSchema) {
|
|
435
|
-
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
436
|
-
}
|
|
437
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import type { ZodType, ZodTypeDef } from "zod";
|
|
2
|
-
import { type StreamTextResult, type ToolSet, type Schema, type GenerateTextResult, type LanguageModelV1 } from "ai";
|
|
3
|
-
import type { AIProvider, TextGenerationOptions, StreamTextOptions, EnhancedGenerateTextResult } from "../core/types.js";
|
|
4
|
-
export declare class GoogleAIStudio implements AIProvider {
|
|
5
|
-
private modelName;
|
|
6
|
-
/**
|
|
7
|
-
* Initializes a new instance of GoogleAIStudio
|
|
8
|
-
* @param modelName - Optional model name to override the default from config
|
|
9
|
-
*/
|
|
10
|
-
constructor(modelName?: string | null);
|
|
11
|
-
/**
|
|
12
|
-
* Gets the appropriate model instance
|
|
13
|
-
* Made public to support FunctionCallingProvider integration
|
|
14
|
-
*/
|
|
15
|
-
getModel(): LanguageModelV1;
|
|
16
|
-
/**
|
|
17
|
-
* Expose model property for FunctionCallingProvider
|
|
18
|
-
* This allows the enhanced provider to access the underlying model
|
|
19
|
-
*/
|
|
20
|
-
get model(): LanguageModelV1;
|
|
21
|
-
/**
|
|
22
|
-
* Processes text using streaming approach with enhanced error handling callbacks
|
|
23
|
-
* @param prompt - The input text prompt to analyze
|
|
24
|
-
* @param analysisSchema - Optional Zod schema or Schema object for output validation
|
|
25
|
-
* @returns Promise resolving to StreamTextResult or null if operation fails
|
|
26
|
-
*/
|
|
27
|
-
streamText(optionsOrPrompt: StreamTextOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamTextResult<ToolSet, unknown> | null>;
|
|
28
|
-
/**
|
|
29
|
-
* Processes text using non-streaming approach with optional schema validation
|
|
30
|
-
* @param prompt - The input text prompt to analyze
|
|
31
|
-
* @param analysisSchema - Optional Zod schema or Schema object for output validation
|
|
32
|
-
* @returns Promise resolving to GenerateTextResult or null if operation fails
|
|
33
|
-
*/
|
|
34
|
-
generateText(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<GenerateTextResult<ToolSet, unknown> | null>;
|
|
35
|
-
/**
|
|
36
|
-
* Alias for generateText() - CLI-SDK consistency
|
|
37
|
-
* @param optionsOrPrompt - TextGenerationOptions object or prompt string
|
|
38
|
-
* @param analysisSchema - Optional schema for output validation
|
|
39
|
-
* @returns Promise resolving to GenerateTextResult or null
|
|
40
|
-
*/
|
|
41
|
-
generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<EnhancedGenerateTextResult | null>;
|
|
42
|
-
/**
|
|
43
|
-
* Short alias for generateText() - CLI-SDK consistency
|
|
44
|
-
* @param optionsOrPrompt - TextGenerationOptions object or prompt string
|
|
45
|
-
* @param analysisSchema - Optional schema for output validation
|
|
46
|
-
* @returns Promise resolving to GenerateTextResult or null
|
|
47
|
-
*/
|
|
48
|
-
gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<EnhancedGenerateTextResult | null>;
|
|
49
|
-
}
|