@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.
Files changed (214) hide show
  1. package/CHANGELOG.md +20 -7
  2. package/README.md +160 -172
  3. package/dist/agent/direct-tools.d.ts +6 -6
  4. package/dist/chat/sse-handler.js +5 -4
  5. package/dist/chat/websocket-chat-handler.js +9 -9
  6. package/dist/cli/commands/config.d.ts +3 -3
  7. package/dist/cli/commands/mcp.js +9 -8
  8. package/dist/cli/commands/ollama.js +3 -3
  9. package/dist/cli/factories/command-factory.d.ts +18 -0
  10. package/dist/cli/factories/command-factory.js +183 -0
  11. package/dist/cli/index.js +105 -157
  12. package/dist/cli/utils/interactive-setup.js +2 -2
  13. package/dist/core/base-provider.d.ts +423 -0
  14. package/dist/core/base-provider.js +365 -0
  15. package/dist/core/constants.d.ts +1 -1
  16. package/dist/core/constants.js +1 -1
  17. package/dist/core/dynamic-models.d.ts +6 -6
  18. package/dist/core/evaluation.d.ts +19 -80
  19. package/dist/core/evaluation.js +185 -484
  20. package/dist/core/factory.d.ts +3 -3
  21. package/dist/core/factory.js +31 -91
  22. package/dist/core/service-registry.d.ts +47 -0
  23. package/dist/core/service-registry.js +112 -0
  24. package/dist/core/types.d.ts +49 -49
  25. package/dist/core/types.js +1 -0
  26. package/dist/factories/compatibility-factory.d.ts +20 -0
  27. package/dist/factories/compatibility-factory.js +69 -0
  28. package/dist/factories/provider-factory.d.ts +72 -0
  29. package/dist/factories/provider-factory.js +144 -0
  30. package/dist/factories/provider-generate-factory.d.ts +20 -0
  31. package/dist/factories/provider-generate-factory.js +87 -0
  32. package/dist/factories/provider-registry.d.ts +38 -0
  33. package/dist/factories/provider-registry.js +107 -0
  34. package/dist/index.d.ts +8 -5
  35. package/dist/index.js +5 -5
  36. package/dist/lib/agent/direct-tools.d.ts +6 -6
  37. package/dist/lib/chat/sse-handler.js +5 -4
  38. package/dist/lib/chat/websocket-chat-handler.js +9 -9
  39. package/dist/lib/core/base-provider.d.ts +423 -0
  40. package/dist/lib/core/base-provider.js +365 -0
  41. package/dist/lib/core/constants.d.ts +1 -1
  42. package/dist/lib/core/constants.js +1 -1
  43. package/dist/lib/core/dynamic-models.d.ts +6 -6
  44. package/dist/lib/core/evaluation.d.ts +19 -80
  45. package/dist/lib/core/evaluation.js +185 -484
  46. package/dist/lib/core/factory.d.ts +3 -3
  47. package/dist/lib/core/factory.js +30 -91
  48. package/dist/lib/core/service-registry.d.ts +47 -0
  49. package/dist/lib/core/service-registry.js +112 -0
  50. package/dist/lib/core/types.d.ts +49 -49
  51. package/dist/lib/core/types.js +1 -0
  52. package/dist/lib/factories/compatibility-factory.d.ts +20 -0
  53. package/dist/lib/factories/compatibility-factory.js +69 -0
  54. package/dist/lib/factories/provider-factory.d.ts +72 -0
  55. package/dist/lib/factories/provider-factory.js +144 -0
  56. package/dist/lib/factories/provider-generate-factory.d.ts +20 -0
  57. package/dist/lib/factories/provider-generate-factory.js +87 -0
  58. package/dist/lib/factories/provider-registry.d.ts +38 -0
  59. package/dist/lib/factories/provider-registry.js +107 -0
  60. package/dist/lib/index.d.ts +8 -5
  61. package/dist/lib/index.js +5 -5
  62. package/dist/lib/mcp/client.js +5 -5
  63. package/dist/lib/mcp/config.js +28 -3
  64. package/dist/lib/mcp/dynamic-orchestrator.js +8 -8
  65. package/dist/lib/mcp/external-client.js +2 -2
  66. package/dist/lib/mcp/factory.d.ts +1 -1
  67. package/dist/lib/mcp/factory.js +1 -1
  68. package/dist/lib/mcp/function-calling.js +1 -1
  69. package/dist/lib/mcp/initialize-tools.d.ts +1 -1
  70. package/dist/lib/mcp/initialize-tools.js +45 -1
  71. package/dist/lib/mcp/initialize.js +16 -6
  72. package/dist/lib/mcp/neurolink-mcp-client.js +10 -10
  73. package/dist/lib/mcp/orchestrator.js +4 -4
  74. package/dist/lib/mcp/servers/agent/direct-tools-server.d.ts +8 -0
  75. package/dist/lib/mcp/servers/agent/direct-tools-server.js +109 -0
  76. package/dist/lib/mcp/servers/ai-providers/ai-analysis-tools.js +10 -10
  77. package/dist/lib/mcp/servers/ai-providers/ai-core-server.js +8 -6
  78. package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
  79. package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.js +16 -16
  80. package/dist/lib/mcp/unified-registry.d.ts +4 -0
  81. package/dist/lib/mcp/unified-registry.js +42 -9
  82. package/dist/lib/neurolink.d.ts +161 -174
  83. package/dist/lib/neurolink.js +723 -397
  84. package/dist/lib/providers/amazon-bedrock.d.ts +32 -0
  85. package/dist/lib/providers/amazon-bedrock.js +143 -0
  86. package/dist/lib/providers/analytics-helper.js +7 -4
  87. package/dist/lib/providers/anthropic-baseprovider.d.ts +23 -0
  88. package/dist/lib/providers/anthropic-baseprovider.js +114 -0
  89. package/dist/lib/providers/anthropic.d.ts +19 -39
  90. package/dist/lib/providers/anthropic.js +84 -378
  91. package/dist/lib/providers/azure-openai.d.ts +20 -0
  92. package/dist/lib/providers/azure-openai.js +89 -0
  93. package/dist/lib/providers/function-calling-provider.d.ts +14 -12
  94. package/dist/lib/providers/function-calling-provider.js +114 -64
  95. package/dist/lib/providers/google-ai-studio.d.ts +23 -0
  96. package/dist/lib/providers/google-ai-studio.js +107 -0
  97. package/dist/lib/providers/google-vertex.d.ts +47 -0
  98. package/dist/lib/providers/google-vertex.js +205 -0
  99. package/dist/lib/providers/huggingFace.d.ts +33 -27
  100. package/dist/lib/providers/huggingFace.js +103 -400
  101. package/dist/lib/providers/index.d.ts +9 -9
  102. package/dist/lib/providers/index.js +9 -9
  103. package/dist/lib/providers/mcp-provider.d.ts +13 -8
  104. package/dist/lib/providers/mcp-provider.js +63 -18
  105. package/dist/lib/providers/mistral.d.ts +42 -0
  106. package/dist/lib/providers/mistral.js +160 -0
  107. package/dist/lib/providers/ollama.d.ts +52 -35
  108. package/dist/lib/providers/ollama.js +297 -477
  109. package/dist/lib/providers/openAI.d.ts +21 -21
  110. package/dist/lib/providers/openAI.js +81 -245
  111. package/dist/lib/sdk/tool-extension.d.ts +181 -0
  112. package/dist/lib/sdk/tool-extension.js +283 -0
  113. package/dist/lib/sdk/tool-registration.d.ts +95 -0
  114. package/dist/lib/sdk/tool-registration.js +167 -0
  115. package/dist/lib/types/generate-types.d.ts +80 -0
  116. package/dist/lib/types/generate-types.js +1 -0
  117. package/dist/lib/types/mcp-types.d.ts +116 -0
  118. package/dist/lib/types/mcp-types.js +5 -0
  119. package/dist/lib/types/stream-types.d.ts +95 -0
  120. package/dist/lib/types/stream-types.js +1 -0
  121. package/dist/lib/types/universal-provider-options.d.ts +87 -0
  122. package/dist/lib/types/universal-provider-options.js +53 -0
  123. package/dist/lib/utils/providerUtils-fixed.js +1 -1
  124. package/dist/lib/utils/streaming-utils.d.ts +14 -2
  125. package/dist/lib/utils/streaming-utils.js +0 -3
  126. package/dist/mcp/client.js +5 -5
  127. package/dist/mcp/config.js +28 -3
  128. package/dist/mcp/dynamic-orchestrator.js +8 -8
  129. package/dist/mcp/external-client.js +2 -2
  130. package/dist/mcp/factory.d.ts +1 -1
  131. package/dist/mcp/factory.js +1 -1
  132. package/dist/mcp/function-calling.js +1 -1
  133. package/dist/mcp/initialize-tools.d.ts +1 -1
  134. package/dist/mcp/initialize-tools.js +45 -1
  135. package/dist/mcp/initialize.js +16 -6
  136. package/dist/mcp/neurolink-mcp-client.js +10 -10
  137. package/dist/mcp/orchestrator.js +4 -4
  138. package/dist/mcp/servers/agent/direct-tools-server.d.ts +8 -0
  139. package/dist/mcp/servers/agent/direct-tools-server.js +109 -0
  140. package/dist/mcp/servers/ai-providers/ai-analysis-tools.js +10 -10
  141. package/dist/mcp/servers/ai-providers/ai-core-server.js +8 -6
  142. package/dist/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
  143. package/dist/mcp/servers/ai-providers/ai-workflow-tools.js +16 -16
  144. package/dist/mcp/unified-registry.d.ts +4 -0
  145. package/dist/mcp/unified-registry.js +42 -9
  146. package/dist/neurolink.d.ts +161 -174
  147. package/dist/neurolink.js +723 -397
  148. package/dist/providers/amazon-bedrock.d.ts +32 -0
  149. package/dist/providers/amazon-bedrock.js +143 -0
  150. package/dist/providers/analytics-helper.js +7 -4
  151. package/dist/providers/anthropic-baseprovider.d.ts +23 -0
  152. package/dist/providers/anthropic-baseprovider.js +114 -0
  153. package/dist/providers/anthropic.d.ts +19 -39
  154. package/dist/providers/anthropic.js +83 -377
  155. package/dist/providers/azure-openai.d.ts +20 -0
  156. package/dist/providers/azure-openai.js +89 -0
  157. package/dist/providers/function-calling-provider.d.ts +14 -12
  158. package/dist/providers/function-calling-provider.js +114 -64
  159. package/dist/providers/google-ai-studio.d.ts +23 -0
  160. package/dist/providers/google-ai-studio.js +108 -0
  161. package/dist/providers/google-vertex.d.ts +47 -0
  162. package/dist/providers/google-vertex.js +205 -0
  163. package/dist/providers/huggingFace.d.ts +33 -27
  164. package/dist/providers/huggingFace.js +102 -399
  165. package/dist/providers/index.d.ts +9 -9
  166. package/dist/providers/index.js +9 -9
  167. package/dist/providers/mcp-provider.d.ts +13 -8
  168. package/dist/providers/mcp-provider.js +63 -18
  169. package/dist/providers/mistral.d.ts +42 -0
  170. package/dist/providers/mistral.js +160 -0
  171. package/dist/providers/ollama.d.ts +52 -35
  172. package/dist/providers/ollama.js +297 -476
  173. package/dist/providers/openAI.d.ts +21 -21
  174. package/dist/providers/openAI.js +81 -246
  175. package/dist/sdk/tool-extension.d.ts +181 -0
  176. package/dist/sdk/tool-extension.js +283 -0
  177. package/dist/sdk/tool-registration.d.ts +95 -0
  178. package/dist/sdk/tool-registration.js +168 -0
  179. package/dist/types/generate-types.d.ts +80 -0
  180. package/dist/types/generate-types.js +1 -0
  181. package/dist/types/mcp-types.d.ts +116 -0
  182. package/dist/types/mcp-types.js +5 -0
  183. package/dist/types/stream-types.d.ts +95 -0
  184. package/dist/types/stream-types.js +1 -0
  185. package/dist/types/universal-provider-options.d.ts +87 -0
  186. package/dist/types/universal-provider-options.js +53 -0
  187. package/dist/utils/providerUtils-fixed.js +1 -1
  188. package/dist/utils/streaming-utils.d.ts +14 -2
  189. package/dist/utils/streaming-utils.js +0 -3
  190. package/package.json +15 -10
  191. package/dist/lib/providers/agent-enhanced-provider.d.ts +0 -89
  192. package/dist/lib/providers/agent-enhanced-provider.js +0 -614
  193. package/dist/lib/providers/amazonBedrock.d.ts +0 -19
  194. package/dist/lib/providers/amazonBedrock.js +0 -334
  195. package/dist/lib/providers/azureOpenAI.d.ts +0 -39
  196. package/dist/lib/providers/azureOpenAI.js +0 -436
  197. package/dist/lib/providers/googleAIStudio.d.ts +0 -49
  198. package/dist/lib/providers/googleAIStudio.js +0 -333
  199. package/dist/lib/providers/googleVertexAI.d.ts +0 -38
  200. package/dist/lib/providers/googleVertexAI.js +0 -519
  201. package/dist/lib/providers/mistralAI.d.ts +0 -34
  202. package/dist/lib/providers/mistralAI.js +0 -294
  203. package/dist/providers/agent-enhanced-provider.d.ts +0 -89
  204. package/dist/providers/agent-enhanced-provider.js +0 -614
  205. package/dist/providers/amazonBedrock.d.ts +0 -19
  206. package/dist/providers/amazonBedrock.js +0 -334
  207. package/dist/providers/azureOpenAI.d.ts +0 -39
  208. package/dist/providers/azureOpenAI.js +0 -437
  209. package/dist/providers/googleAIStudio.d.ts +0 -49
  210. package/dist/providers/googleAIStudio.js +0 -333
  211. package/dist/providers/googleVertexAI.d.ts +0 -38
  212. package/dist/providers/googleVertexAI.js +0 -519
  213. package/dist/providers/mistralAI.d.ts +0 -34
  214. 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
- }