@juspay/neurolink 3.0.0 → 4.0.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 +62 -4
- package/README.md +235 -2
- package/dist/agent/direct-tools.d.ts +6 -6
- package/dist/chat/client-utils.d.ts +92 -0
- package/dist/chat/client-utils.js +298 -0
- package/dist/chat/index.d.ts +27 -0
- package/dist/chat/index.js +41 -0
- package/dist/chat/session-storage.d.ts +77 -0
- package/dist/chat/session-storage.js +233 -0
- package/dist/chat/session.d.ts +95 -0
- package/dist/chat/session.js +257 -0
- package/dist/chat/sse-handler.d.ts +49 -0
- package/dist/chat/sse-handler.js +266 -0
- package/dist/chat/types.d.ts +73 -0
- package/dist/chat/types.js +5 -0
- package/dist/chat/websocket-chat-handler.d.ts +36 -0
- package/dist/chat/websocket-chat-handler.js +262 -0
- package/dist/cli/commands/config.js +12 -12
- package/dist/cli/commands/mcp.js +3 -4
- package/dist/cli/index.d.ts +0 -7
- package/dist/cli/index.js +256 -27
- package/dist/config/configManager.d.ts +60 -0
- package/dist/config/configManager.js +300 -0
- package/dist/config/types.d.ts +136 -0
- package/dist/config/types.js +43 -0
- package/dist/core/analytics.d.ts +23 -0
- package/dist/core/analytics.js +131 -0
- package/dist/core/constants.d.ts +41 -0
- package/dist/core/constants.js +50 -0
- package/dist/core/defaults.d.ts +18 -0
- package/dist/core/defaults.js +29 -0
- package/dist/core/evaluation-config.d.ts +29 -0
- package/dist/core/evaluation-config.js +144 -0
- package/dist/core/evaluation-providers.d.ts +30 -0
- package/dist/core/evaluation-providers.js +187 -0
- package/dist/core/evaluation.d.ts +117 -0
- package/dist/core/evaluation.js +528 -0
- package/dist/core/factory.js +33 -25
- package/dist/core/types.d.ts +165 -6
- package/dist/core/types.js +3 -4
- package/dist/index.d.ts +9 -4
- package/dist/index.js +25 -4
- package/dist/lib/agent/direct-tools.d.ts +6 -6
- package/dist/lib/chat/client-utils.d.ts +92 -0
- package/dist/lib/chat/client-utils.js +298 -0
- package/dist/lib/chat/index.d.ts +27 -0
- package/dist/lib/chat/index.js +41 -0
- package/dist/lib/chat/session-storage.d.ts +77 -0
- package/dist/lib/chat/session-storage.js +233 -0
- package/dist/lib/chat/session.d.ts +95 -0
- package/dist/lib/chat/session.js +257 -0
- package/dist/lib/chat/sse-handler.d.ts +49 -0
- package/dist/lib/chat/sse-handler.js +266 -0
- package/dist/lib/chat/types.d.ts +73 -0
- package/dist/lib/chat/types.js +5 -0
- package/dist/lib/chat/websocket-chat-handler.d.ts +36 -0
- package/dist/lib/chat/websocket-chat-handler.js +262 -0
- package/dist/lib/config/configManager.d.ts +60 -0
- package/dist/lib/config/configManager.js +300 -0
- package/dist/lib/config/types.d.ts +136 -0
- package/dist/lib/config/types.js +43 -0
- package/dist/lib/core/analytics.d.ts +23 -0
- package/dist/lib/core/analytics.js +131 -0
- package/dist/lib/core/constants.d.ts +41 -0
- package/dist/lib/core/constants.js +50 -0
- package/dist/lib/core/defaults.d.ts +18 -0
- package/dist/lib/core/defaults.js +29 -0
- package/dist/lib/core/evaluation-config.d.ts +29 -0
- package/dist/lib/core/evaluation-config.js +144 -0
- package/dist/lib/core/evaluation-providers.d.ts +30 -0
- package/dist/lib/core/evaluation-providers.js +187 -0
- package/dist/lib/core/evaluation.d.ts +117 -0
- package/dist/lib/core/evaluation.js +528 -0
- package/dist/lib/core/factory.js +33 -26
- package/dist/lib/core/types.d.ts +165 -6
- package/dist/lib/core/types.js +3 -4
- package/dist/lib/index.d.ts +9 -4
- package/dist/lib/index.js +25 -4
- package/dist/lib/mcp/contracts/mcpContract.d.ts +118 -0
- package/dist/lib/mcp/contracts/mcpContract.js +5 -0
- package/dist/lib/mcp/function-calling.js +11 -3
- package/dist/lib/mcp/logging.js +5 -0
- package/dist/lib/mcp/neurolink-mcp-client.js +2 -1
- package/dist/lib/mcp/orchestrator.js +18 -9
- package/dist/lib/mcp/registry.d.ts +49 -16
- package/dist/lib/mcp/registry.js +80 -6
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.js +5 -4
- package/dist/lib/mcp/tool-integration.js +1 -1
- package/dist/lib/mcp/tool-registry.d.ts +55 -34
- package/dist/lib/mcp/tool-registry.js +111 -97
- package/dist/lib/mcp/unified-mcp.js +6 -1
- package/dist/lib/mcp/unified-registry.d.ts +12 -4
- package/dist/lib/mcp/unified-registry.js +17 -4
- package/dist/lib/neurolink.d.ts +28 -0
- package/dist/lib/neurolink.js +48 -4
- package/dist/lib/providers/agent-enhanced-provider.d.ts +11 -2
- package/dist/lib/providers/agent-enhanced-provider.js +86 -15
- package/dist/lib/providers/amazonBedrock.d.ts +9 -1
- package/dist/lib/providers/amazonBedrock.js +26 -2
- package/dist/lib/providers/analytics-helper.d.ts +53 -0
- package/dist/lib/providers/analytics-helper.js +151 -0
- package/dist/lib/providers/anthropic.d.ts +11 -1
- package/dist/lib/providers/anthropic.js +29 -4
- package/dist/lib/providers/azureOpenAI.d.ts +3 -1
- package/dist/lib/providers/azureOpenAI.js +28 -4
- package/dist/lib/providers/function-calling-provider.d.ts +9 -1
- package/dist/lib/providers/function-calling-provider.js +14 -1
- package/dist/lib/providers/googleAIStudio.d.ts +15 -1
- package/dist/lib/providers/googleAIStudio.js +32 -2
- package/dist/lib/providers/googleVertexAI.d.ts +9 -1
- package/dist/lib/providers/googleVertexAI.js +31 -2
- package/dist/lib/providers/huggingFace.d.ts +3 -1
- package/dist/lib/providers/huggingFace.js +26 -3
- package/dist/lib/providers/mcp-provider.d.ts +9 -1
- package/dist/lib/providers/mcp-provider.js +12 -0
- package/dist/lib/providers/mistralAI.d.ts +3 -1
- package/dist/lib/providers/mistralAI.js +25 -2
- package/dist/lib/providers/ollama.d.ts +3 -1
- package/dist/lib/providers/ollama.js +27 -4
- package/dist/lib/providers/openAI.d.ts +15 -1
- package/dist/lib/providers/openAI.js +32 -2
- package/dist/lib/proxy/proxy-fetch.js +8 -7
- package/dist/lib/services/streaming/streaming-manager.d.ts +29 -0
- package/dist/lib/services/streaming/streaming-manager.js +244 -0
- package/dist/lib/services/types.d.ts +155 -0
- package/dist/lib/services/types.js +2 -0
- package/dist/lib/services/websocket/websocket-server.d.ts +34 -0
- package/dist/lib/services/websocket/websocket-server.js +304 -0
- package/dist/lib/telemetry/index.d.ts +15 -0
- package/dist/lib/telemetry/index.js +22 -0
- package/dist/lib/telemetry/telemetry-service.d.ts +47 -0
- package/dist/lib/telemetry/telemetry-service.js +259 -0
- package/dist/lib/utils/streaming-utils.d.ts +67 -0
- package/dist/lib/utils/streaming-utils.js +201 -0
- package/dist/mcp/contracts/mcpContract.d.ts +118 -0
- package/dist/mcp/contracts/mcpContract.js +5 -0
- package/dist/mcp/function-calling.js +11 -3
- package/dist/mcp/logging.js +5 -0
- package/dist/mcp/neurolink-mcp-client.js +2 -1
- package/dist/mcp/orchestrator.js +18 -9
- package/dist/mcp/registry.d.ts +49 -16
- package/dist/mcp/registry.js +80 -6
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.js +5 -4
- package/dist/mcp/tool-integration.js +1 -1
- package/dist/mcp/tool-registry.d.ts +55 -34
- package/dist/mcp/tool-registry.js +111 -97
- package/dist/mcp/unified-mcp.js +6 -1
- package/dist/mcp/unified-registry.d.ts +12 -4
- package/dist/mcp/unified-registry.js +17 -4
- package/dist/neurolink.d.ts +28 -0
- package/dist/neurolink.js +48 -4
- package/dist/providers/agent-enhanced-provider.d.ts +11 -2
- package/dist/providers/agent-enhanced-provider.js +86 -15
- package/dist/providers/amazonBedrock.d.ts +9 -1
- package/dist/providers/amazonBedrock.js +26 -2
- package/dist/providers/analytics-helper.d.ts +53 -0
- package/dist/providers/analytics-helper.js +151 -0
- package/dist/providers/anthropic.d.ts +11 -1
- package/dist/providers/anthropic.js +29 -4
- package/dist/providers/azureOpenAI.d.ts +3 -1
- package/dist/providers/azureOpenAI.js +29 -4
- package/dist/providers/function-calling-provider.d.ts +9 -1
- package/dist/providers/function-calling-provider.js +14 -1
- package/dist/providers/googleAIStudio.d.ts +15 -1
- package/dist/providers/googleAIStudio.js +32 -2
- package/dist/providers/googleVertexAI.d.ts +9 -1
- package/dist/providers/googleVertexAI.js +31 -2
- package/dist/providers/huggingFace.d.ts +3 -1
- package/dist/providers/huggingFace.js +26 -3
- package/dist/providers/mcp-provider.d.ts +9 -1
- package/dist/providers/mcp-provider.js +12 -0
- package/dist/providers/mistralAI.d.ts +3 -1
- package/dist/providers/mistralAI.js +25 -2
- package/dist/providers/ollama.d.ts +3 -1
- package/dist/providers/ollama.js +27 -4
- package/dist/providers/openAI.d.ts +15 -1
- package/dist/providers/openAI.js +33 -2
- package/dist/proxy/proxy-fetch.js +8 -7
- package/dist/services/streaming/streaming-manager.d.ts +29 -0
- package/dist/services/streaming/streaming-manager.js +244 -0
- package/dist/services/types.d.ts +155 -0
- package/dist/services/types.js +2 -0
- package/dist/services/websocket/websocket-server.d.ts +34 -0
- package/dist/services/websocket/websocket-server.js +304 -0
- package/dist/telemetry/index.d.ts +15 -0
- package/dist/telemetry/index.js +22 -0
- package/dist/telemetry/telemetry-service.d.ts +47 -0
- package/dist/telemetry/telemetry-service.js +261 -0
- package/dist/utils/streaming-utils.d.ts +67 -0
- package/dist/utils/streaming-utils.js +201 -0
- package/package.json +18 -2
|
@@ -2,6 +2,8 @@ import { HfInference } from "@huggingface/inference";
|
|
|
2
2
|
import { streamText, generateText, Output, } from "ai";
|
|
3
3
|
import { logger } from "../utils/logger.js";
|
|
4
4
|
import { createTimeoutController, TimeoutError, getDefaultTimeout, } from "../utils/timeout.js";
|
|
5
|
+
import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
|
|
6
|
+
import { evaluateResponse } from "../core/evaluation.js";
|
|
5
7
|
// Default system context
|
|
6
8
|
const DEFAULT_SYSTEM_CONTEXT = {
|
|
7
9
|
systemPrompt: "You are a helpful AI assistant.",
|
|
@@ -107,7 +109,7 @@ class HuggingFaceLanguageModel {
|
|
|
107
109
|
inputs: prompt,
|
|
108
110
|
parameters: {
|
|
109
111
|
temperature: options.temperature || 0.7,
|
|
110
|
-
max_new_tokens: options.maxTokens
|
|
112
|
+
max_new_tokens: options.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
111
113
|
return_full_text: false,
|
|
112
114
|
do_sample: (options.temperature || 0.7) > 0,
|
|
113
115
|
},
|
|
@@ -229,7 +231,7 @@ export class HuggingFace {
|
|
|
229
231
|
const options = typeof optionsOrPrompt === "string"
|
|
230
232
|
? { prompt: optionsOrPrompt }
|
|
231
233
|
: optionsOrPrompt;
|
|
232
|
-
const { prompt, temperature = 0.7, maxTokens =
|
|
234
|
+
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout(provider, "stream"), } = options;
|
|
233
235
|
// Use schema from options or fallback parameter
|
|
234
236
|
const finalSchema = schema || analysisSchema;
|
|
235
237
|
logger.debug(`[${functionTag}] Stream request started`, {
|
|
@@ -332,12 +334,13 @@ export class HuggingFace {
|
|
|
332
334
|
async generateText(optionsOrPrompt, analysisSchema) {
|
|
333
335
|
const functionTag = "HuggingFace.generateText";
|
|
334
336
|
const provider = "huggingface";
|
|
337
|
+
const startTime = Date.now();
|
|
335
338
|
try {
|
|
336
339
|
// Parse parameters - support both string and options object
|
|
337
340
|
const options = typeof optionsOrPrompt === "string"
|
|
338
341
|
? { prompt: optionsOrPrompt }
|
|
339
342
|
: optionsOrPrompt;
|
|
340
|
-
const { prompt, temperature = 0.7, maxTokens =
|
|
343
|
+
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout(provider, "generate"), } = options;
|
|
341
344
|
// Use schema from options or fallback parameter
|
|
342
345
|
const finalSchema = schema || analysisSchema;
|
|
343
346
|
logger.debug(`[${functionTag}] Generate request started`, {
|
|
@@ -379,6 +382,20 @@ export class HuggingFace {
|
|
|
379
382
|
responseLength: result.text?.length || 0,
|
|
380
383
|
timeout,
|
|
381
384
|
});
|
|
385
|
+
// Add analytics if enabled
|
|
386
|
+
if (options.enableAnalytics) {
|
|
387
|
+
result.analytics = {
|
|
388
|
+
provider,
|
|
389
|
+
model: this.modelName,
|
|
390
|
+
tokens: result.usage,
|
|
391
|
+
responseTime: Date.now() - startTime,
|
|
392
|
+
context: options.context,
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
// Add evaluation if enabled
|
|
396
|
+
if (options.enableEvaluation) {
|
|
397
|
+
result.evaluation = await evaluateResponse(prompt, result.text, options.context);
|
|
398
|
+
}
|
|
382
399
|
return result;
|
|
383
400
|
}
|
|
384
401
|
finally {
|
|
@@ -407,4 +424,10 @@ export class HuggingFace {
|
|
|
407
424
|
throw err; // Re-throw error to trigger fallback
|
|
408
425
|
}
|
|
409
426
|
}
|
|
427
|
+
async generate(optionsOrPrompt, analysisSchema) {
|
|
428
|
+
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
429
|
+
}
|
|
430
|
+
async gen(optionsOrPrompt, analysisSchema) {
|
|
431
|
+
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
432
|
+
}
|
|
410
433
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* NeuroLink MCP-Aware AI Provider
|
|
3
3
|
* Integrates MCP tools with AI providers following Lighthouse's pattern
|
|
4
4
|
*/
|
|
5
|
-
import type { AIProvider, TextGenerationOptions, StreamTextOptions } from "../core/types.js";
|
|
5
|
+
import type { AIProvider, TextGenerationOptions, StreamTextOptions, EnhancedGenerateTextResult } from "../core/types.js";
|
|
6
6
|
import type { StreamTextResult, ToolSet, Schema, GenerateTextResult } from "ai";
|
|
7
7
|
import type { ZodType, ZodTypeDef } from "zod";
|
|
8
8
|
/**
|
|
@@ -51,6 +51,14 @@ export declare class MCPAwareProvider implements AIProvider {
|
|
|
51
51
|
* Clean up session
|
|
52
52
|
*/
|
|
53
53
|
cleanup(): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Alias for generateText() - CLI-SDK consistency
|
|
56
|
+
*/
|
|
57
|
+
generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateTextResult | null>;
|
|
58
|
+
/**
|
|
59
|
+
* Short alias for generateText() - CLI-SDK consistency
|
|
60
|
+
*/
|
|
61
|
+
gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateTextResult | null>;
|
|
54
62
|
}
|
|
55
63
|
/**
|
|
56
64
|
* Create an MCP-aware provider
|
|
@@ -192,6 +192,18 @@ Please provide a response based on this information.`;
|
|
|
192
192
|
this.mcpInitialized = false;
|
|
193
193
|
}
|
|
194
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Alias for generateText() - CLI-SDK consistency
|
|
197
|
+
*/
|
|
198
|
+
async generate(optionsOrPrompt, analysisSchema) {
|
|
199
|
+
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Short alias for generateText() - CLI-SDK consistency
|
|
203
|
+
*/
|
|
204
|
+
async gen(optionsOrPrompt, analysisSchema) {
|
|
205
|
+
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
206
|
+
}
|
|
195
207
|
}
|
|
196
208
|
/**
|
|
197
209
|
* Create an MCP-aware provider
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ZodType, ZodTypeDef } from "zod";
|
|
2
2
|
import { type StreamTextResult, type ToolSet, type Schema, type GenerateTextResult } from "ai";
|
|
3
|
-
import type { AIProvider, TextGenerationOptions, StreamTextOptions } from "../core/types.js";
|
|
3
|
+
import type { AIProvider, TextGenerationOptions, StreamTextOptions, EnhancedGenerateTextResult } from "../core/types.js";
|
|
4
4
|
export declare class MistralAI implements AIProvider {
|
|
5
5
|
private modelName;
|
|
6
6
|
private client;
|
|
@@ -28,5 +28,7 @@ export declare class MistralAI implements AIProvider {
|
|
|
28
28
|
* @returns Promise resolving to GenerateTextResult or null if operation fails
|
|
29
29
|
*/
|
|
30
30
|
generateText(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<GenerateTextResult<ToolSet, unknown> | null>;
|
|
31
|
+
generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateTextResult | null>;
|
|
32
|
+
gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateTextResult | null>;
|
|
31
33
|
}
|
|
32
34
|
export default MistralAI;
|
|
@@ -2,6 +2,8 @@ import { createMistral } from "@ai-sdk/mistral";
|
|
|
2
2
|
import { streamText, generateText, Output, } from "ai";
|
|
3
3
|
import { logger } from "../utils/logger.js";
|
|
4
4
|
import { createTimeoutController, TimeoutError, getDefaultTimeout, } from "../utils/timeout.js";
|
|
5
|
+
import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
|
|
6
|
+
import { evaluateResponse } from "../core/evaluation.js";
|
|
5
7
|
// Default system context
|
|
6
8
|
const DEFAULT_SYSTEM_CONTEXT = {
|
|
7
9
|
systemPrompt: "You are a helpful AI assistant.",
|
|
@@ -89,7 +91,7 @@ export class MistralAI {
|
|
|
89
91
|
const options = typeof optionsOrPrompt === "string"
|
|
90
92
|
? { prompt: optionsOrPrompt }
|
|
91
93
|
: optionsOrPrompt;
|
|
92
|
-
const { prompt, temperature = 0.7, maxTokens =
|
|
94
|
+
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout(provider, "stream"), } = options;
|
|
93
95
|
// Use schema from options or fallback parameter
|
|
94
96
|
const finalSchema = schema || analysisSchema;
|
|
95
97
|
logger.debug(`[${functionTag}] Stream request started`, {
|
|
@@ -192,12 +194,13 @@ export class MistralAI {
|
|
|
192
194
|
async generateText(optionsOrPrompt, analysisSchema) {
|
|
193
195
|
const functionTag = "MistralAI.generateText";
|
|
194
196
|
const provider = "mistral";
|
|
197
|
+
const startTime = Date.now();
|
|
195
198
|
try {
|
|
196
199
|
// Parse parameters - support both string and options object
|
|
197
200
|
const options = typeof optionsOrPrompt === "string"
|
|
198
201
|
? { prompt: optionsOrPrompt }
|
|
199
202
|
: optionsOrPrompt;
|
|
200
|
-
const { prompt, temperature = 0.7, maxTokens =
|
|
203
|
+
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout(provider, "generate"), } = options;
|
|
201
204
|
// Use schema from options or fallback parameter
|
|
202
205
|
const finalSchema = schema || analysisSchema;
|
|
203
206
|
logger.debug(`[${functionTag}] Generate request started`, {
|
|
@@ -239,6 +242,20 @@ export class MistralAI {
|
|
|
239
242
|
responseLength: result.text?.length || 0,
|
|
240
243
|
timeout,
|
|
241
244
|
});
|
|
245
|
+
// Add analytics if enabled
|
|
246
|
+
if (options.enableAnalytics) {
|
|
247
|
+
result.analytics = {
|
|
248
|
+
provider,
|
|
249
|
+
model: this.modelName,
|
|
250
|
+
tokens: result.usage,
|
|
251
|
+
responseTime: Date.now() - startTime,
|
|
252
|
+
context: options.context,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
// Add evaluation if enabled
|
|
256
|
+
if (options.enableEvaluation) {
|
|
257
|
+
result.evaluation = await evaluateResponse(prompt, result.text, options.context);
|
|
258
|
+
}
|
|
242
259
|
return result;
|
|
243
260
|
}
|
|
244
261
|
finally {
|
|
@@ -267,5 +284,11 @@ export class MistralAI {
|
|
|
267
284
|
throw err; // Re-throw error to trigger fallback
|
|
268
285
|
}
|
|
269
286
|
}
|
|
287
|
+
async generate(optionsOrPrompt, analysisSchema) {
|
|
288
|
+
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
289
|
+
}
|
|
290
|
+
async gen(optionsOrPrompt, analysisSchema) {
|
|
291
|
+
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
292
|
+
}
|
|
270
293
|
}
|
|
271
294
|
export default MistralAI;
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* - Health checking and service validation
|
|
11
11
|
* - Streaming and non-streaming text generation
|
|
12
12
|
*/
|
|
13
|
-
import type { AIProvider, TextGenerationOptions, StreamTextOptions } from "../core/types.js";
|
|
13
|
+
import type { AIProvider, TextGenerationOptions, StreamTextOptions, EnhancedGenerateTextResult } from "../core/types.js";
|
|
14
14
|
import type { GenerateTextResult, StreamTextResult, ToolSet } from "ai";
|
|
15
15
|
import type { ZodType, ZodTypeDef } from "zod";
|
|
16
16
|
import type { Schema } from "ai";
|
|
@@ -48,4 +48,6 @@ export declare class Ollama implements AIProvider {
|
|
|
48
48
|
* Generate streaming text using Ollama local models
|
|
49
49
|
*/
|
|
50
50
|
streamText(optionsOrPrompt: StreamTextOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamTextResult<ToolSet, unknown> | null>;
|
|
51
|
+
generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateTextResult | null>;
|
|
52
|
+
gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateTextResult | null>;
|
|
51
53
|
}
|
package/dist/providers/ollama.js
CHANGED
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
import { streamText, generateText, Output } from "ai";
|
|
14
14
|
import { logger } from "../utils/logger.js";
|
|
15
15
|
import { getDefaultTimeout, TimeoutError } from "../utils/timeout.js";
|
|
16
|
+
import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
|
|
17
|
+
import { evaluateResponse } from "../core/evaluation.js";
|
|
16
18
|
// Default system context
|
|
17
19
|
const DEFAULT_SYSTEM_CONTEXT = {
|
|
18
20
|
systemPrompt: "You are a helpful AI assistant.",
|
|
@@ -106,7 +108,7 @@ class OllamaLanguageModel {
|
|
|
106
108
|
stream: false,
|
|
107
109
|
options: {
|
|
108
110
|
temperature: options.temperature || 0.7,
|
|
109
|
-
num_predict: options.maxTokens
|
|
111
|
+
num_predict: options.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
110
112
|
},
|
|
111
113
|
};
|
|
112
114
|
const controller = new AbortController();
|
|
@@ -176,7 +178,7 @@ class OllamaLanguageModel {
|
|
|
176
178
|
stream: true,
|
|
177
179
|
options: {
|
|
178
180
|
temperature: options.temperature || 0.7,
|
|
179
|
-
num_predict: options.maxTokens
|
|
181
|
+
num_predict: options.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
180
182
|
},
|
|
181
183
|
};
|
|
182
184
|
const controller = new AbortController();
|
|
@@ -391,12 +393,13 @@ export class Ollama {
|
|
|
391
393
|
async generateText(optionsOrPrompt, analysisSchema) {
|
|
392
394
|
const functionTag = "Ollama.generateText";
|
|
393
395
|
const provider = "ollama";
|
|
396
|
+
const startTime = Date.now();
|
|
394
397
|
try {
|
|
395
398
|
// Parse parameters - support both string and options object
|
|
396
399
|
const options = typeof optionsOrPrompt === "string"
|
|
397
400
|
? { prompt: optionsOrPrompt }
|
|
398
401
|
: optionsOrPrompt;
|
|
399
|
-
const { prompt, temperature = 0.7, maxTokens =
|
|
402
|
+
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout, } = options;
|
|
400
403
|
// Use schema from options or fallback parameter
|
|
401
404
|
const finalSchema = schema || analysisSchema;
|
|
402
405
|
// Convert timeout to milliseconds if provided as string
|
|
@@ -437,6 +440,20 @@ export class Ollama {
|
|
|
437
440
|
finishReason: result.finishReason,
|
|
438
441
|
responseLength: result.text?.length || 0,
|
|
439
442
|
});
|
|
443
|
+
// Add analytics if enabled
|
|
444
|
+
if (options.enableAnalytics) {
|
|
445
|
+
result.analytics = {
|
|
446
|
+
provider,
|
|
447
|
+
model: this.modelName,
|
|
448
|
+
tokens: result.usage,
|
|
449
|
+
responseTime: Date.now() - startTime,
|
|
450
|
+
context: options.context,
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
// Add evaluation if enabled
|
|
454
|
+
if (options.enableEvaluation) {
|
|
455
|
+
result.evaluation = await evaluateResponse(prompt, result.text, options.context);
|
|
456
|
+
}
|
|
440
457
|
return result;
|
|
441
458
|
}
|
|
442
459
|
catch (err) {
|
|
@@ -461,7 +478,7 @@ export class Ollama {
|
|
|
461
478
|
const options = typeof optionsOrPrompt === "string"
|
|
462
479
|
? { prompt: optionsOrPrompt }
|
|
463
480
|
: optionsOrPrompt;
|
|
464
|
-
const { prompt, temperature = 0.7, maxTokens =
|
|
481
|
+
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout, } = options;
|
|
465
482
|
// Use schema from options or fallback parameter
|
|
466
483
|
const finalSchema = schema || analysisSchema;
|
|
467
484
|
// Convert timeout to milliseconds if provided as string
|
|
@@ -542,4 +559,10 @@ export class Ollama {
|
|
|
542
559
|
throw err; // Re-throw error to trigger fallback
|
|
543
560
|
}
|
|
544
561
|
}
|
|
562
|
+
async generate(optionsOrPrompt, analysisSchema) {
|
|
563
|
+
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
564
|
+
}
|
|
565
|
+
async gen(optionsOrPrompt, analysisSchema) {
|
|
566
|
+
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
567
|
+
}
|
|
545
568
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ZodType, ZodTypeDef } from "zod";
|
|
2
2
|
import { type StreamTextResult, type ToolSet, type Schema, type GenerateTextResult, type LanguageModelV1 } from "ai";
|
|
3
|
-
import type { AIProvider, TextGenerationOptions, StreamTextOptions } from "../core/types.js";
|
|
3
|
+
import type { AIProvider, TextGenerationOptions, StreamTextOptions, EnhancedGenerateTextResult } from "../core/types.js";
|
|
4
4
|
export declare class OpenAI implements AIProvider {
|
|
5
5
|
private modelName;
|
|
6
6
|
private model;
|
|
@@ -11,4 +11,18 @@ export declare class OpenAI implements AIProvider {
|
|
|
11
11
|
getModel(): LanguageModelV1;
|
|
12
12
|
streamText(optionsOrPrompt: StreamTextOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamTextResult<ToolSet, unknown> | null>;
|
|
13
13
|
generateText(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<GenerateTextResult<ToolSet, unknown> | null>;
|
|
14
|
+
/**
|
|
15
|
+
* Alias for generateText() - CLI-SDK consistency
|
|
16
|
+
* @param optionsOrPrompt - TextGenerationOptions object or prompt string
|
|
17
|
+
* @param analysisSchema - Optional schema for output validation
|
|
18
|
+
* @returns Promise resolving to GenerateTextResult or null
|
|
19
|
+
*/
|
|
20
|
+
generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<EnhancedGenerateTextResult | null>;
|
|
21
|
+
/**
|
|
22
|
+
* Short alias for generateText() - CLI-SDK consistency
|
|
23
|
+
* @param optionsOrPrompt - TextGenerationOptions object or prompt string
|
|
24
|
+
* @param analysisSchema - Optional schema for output validation
|
|
25
|
+
* @returns Promise resolving to GenerateTextResult or null
|
|
26
|
+
*/
|
|
27
|
+
gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<EnhancedGenerateTextResult | null>;
|
|
14
28
|
}
|
package/dist/providers/openAI.js
CHANGED
|
@@ -2,6 +2,9 @@ import { openai } from "@ai-sdk/openai";
|
|
|
2
2
|
import { streamText, generateText, Output, } from "ai";
|
|
3
3
|
import { logger } from "../utils/logger.js";
|
|
4
4
|
import { createTimeoutController, getDefaultTimeout, TimeoutError, } from "../utils/timeout.js";
|
|
5
|
+
import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
|
|
6
|
+
import { evaluateResponse } from "../core/evaluation.js";
|
|
7
|
+
import { createAnalytics } from "../core/analytics.js";
|
|
5
8
|
// Default system context
|
|
6
9
|
const DEFAULT_SYSTEM_CONTEXT = {
|
|
7
10
|
systemPrompt: "You are a helpful AI assistant.",
|
|
@@ -60,7 +63,7 @@ export class OpenAI {
|
|
|
60
63
|
const options = typeof optionsOrPrompt === "string"
|
|
61
64
|
? { prompt: optionsOrPrompt }
|
|
62
65
|
: optionsOrPrompt;
|
|
63
|
-
const { prompt, temperature = 0.7, maxTokens =
|
|
66
|
+
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout(provider, "stream"), } = options;
|
|
64
67
|
// Use schema from options or fallback parameter
|
|
65
68
|
const finalSchema = schema || analysisSchema;
|
|
66
69
|
logger.debug(`[${functionTag}] Stream text started`, {
|
|
@@ -152,12 +155,13 @@ export class OpenAI {
|
|
|
152
155
|
async generateText(optionsOrPrompt, analysisSchema) {
|
|
153
156
|
const functionTag = "OpenAI.generateText";
|
|
154
157
|
const provider = "openai";
|
|
158
|
+
const startTime = Date.now();
|
|
155
159
|
try {
|
|
156
160
|
// Parse parameters - support both string and options object
|
|
157
161
|
const options = typeof optionsOrPrompt === "string"
|
|
158
162
|
? { prompt: optionsOrPrompt }
|
|
159
163
|
: optionsOrPrompt;
|
|
160
|
-
const { prompt, temperature = 0.7, maxTokens =
|
|
164
|
+
const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout(provider, "generate"), } = options;
|
|
161
165
|
// Use schema from options or fallback parameter
|
|
162
166
|
const finalSchema = schema || analysisSchema;
|
|
163
167
|
logger.debug(`[${functionTag}] Generate text started`, {
|
|
@@ -198,6 +202,15 @@ export class OpenAI {
|
|
|
198
202
|
responseLength: result.text?.length || 0,
|
|
199
203
|
timeout,
|
|
200
204
|
});
|
|
205
|
+
// Add analytics if enabled
|
|
206
|
+
if (options.enableAnalytics) {
|
|
207
|
+
const { createAnalytics } = await import("./analytics-helper.js");
|
|
208
|
+
result.analytics = createAnalytics(provider, this.modelName, result, Date.now() - startTime, options.context);
|
|
209
|
+
}
|
|
210
|
+
// Add evaluation if enabled
|
|
211
|
+
if (options.enableEvaluation) {
|
|
212
|
+
result.evaluation = await evaluateResponse(prompt, result.text, options.context, options.evaluationDomain, options.toolUsageContext, options.conversationHistory);
|
|
213
|
+
}
|
|
201
214
|
return result;
|
|
202
215
|
}
|
|
203
216
|
finally {
|
|
@@ -226,4 +239,22 @@ export class OpenAI {
|
|
|
226
239
|
throw err; // Re-throw error to trigger fallback
|
|
227
240
|
}
|
|
228
241
|
}
|
|
242
|
+
/**
|
|
243
|
+
* Alias for generateText() - CLI-SDK consistency
|
|
244
|
+
* @param optionsOrPrompt - TextGenerationOptions object or prompt string
|
|
245
|
+
* @param analysisSchema - Optional schema for output validation
|
|
246
|
+
* @returns Promise resolving to GenerateTextResult or null
|
|
247
|
+
*/
|
|
248
|
+
async generate(optionsOrPrompt, analysisSchema) {
|
|
249
|
+
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Short alias for generateText() - CLI-SDK consistency
|
|
253
|
+
* @param optionsOrPrompt - TextGenerationOptions object or prompt string
|
|
254
|
+
* @param analysisSchema - Optional schema for output validation
|
|
255
|
+
* @returns Promise resolving to GenerateTextResult or null
|
|
256
|
+
*/
|
|
257
|
+
async gen(optionsOrPrompt, analysisSchema) {
|
|
258
|
+
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
259
|
+
}
|
|
229
260
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Proxy-aware fetch implementation for AI SDK providers
|
|
3
3
|
* Implements the proven Vercel AI SDK proxy pattern using undici
|
|
4
4
|
*/
|
|
5
|
+
import { logger } from "../utils/logger.js";
|
|
5
6
|
/**
|
|
6
7
|
* Create a proxy-aware fetch function
|
|
7
8
|
* This implements the community-validated approach for Vercel AI SDK
|
|
@@ -11,12 +12,12 @@ export function createProxyFetch() {
|
|
|
11
12
|
const httpProxy = process.env.HTTP_PROXY || process.env.http_proxy;
|
|
12
13
|
// If no proxy configured, return standard fetch
|
|
13
14
|
if (!httpsProxy && !httpProxy) {
|
|
14
|
-
|
|
15
|
+
logger.debug("[Proxy Fetch] No proxy environment variables found - using standard fetch");
|
|
15
16
|
return fetch;
|
|
16
17
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
logger.debug(`[Proxy Fetch] Configuring proxy with undici ProxyAgent`);
|
|
19
|
+
logger.debug(`[Proxy Fetch] HTTP_PROXY: ${httpProxy || "not set"}`);
|
|
20
|
+
logger.debug(`[Proxy Fetch] HTTPS_PROXY: ${httpsProxy || "not set"}`);
|
|
20
21
|
// Return proxy-aware fetch function
|
|
21
22
|
return async (input, init) => {
|
|
22
23
|
try {
|
|
@@ -30,7 +31,7 @@ export function createProxyFetch() {
|
|
|
30
31
|
: new URL(input.url);
|
|
31
32
|
const proxyUrl = url.protocol === "https:" ? httpsProxy : httpProxy;
|
|
32
33
|
if (proxyUrl) {
|
|
33
|
-
|
|
34
|
+
logger.debug(`[Proxy Fetch] Creating ProxyAgent for ${url.hostname} via ${proxyUrl}`);
|
|
34
35
|
// Create ProxyAgent
|
|
35
36
|
const dispatcher = new ProxyAgent(proxyUrl);
|
|
36
37
|
// Use undici fetch with dispatcher
|
|
@@ -38,12 +39,12 @@ export function createProxyFetch() {
|
|
|
38
39
|
...init,
|
|
39
40
|
dispatcher: dispatcher,
|
|
40
41
|
});
|
|
41
|
-
|
|
42
|
+
logger.debug(`[Proxy Fetch] ✅ Request proxied successfully to ${url.hostname}`);
|
|
42
43
|
return response; // Type assertion to avoid complex type issues
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
46
|
catch (error) {
|
|
46
|
-
|
|
47
|
+
logger.warn(`[Proxy Fetch] Proxy failed (${error.message}), falling back to direct connection`);
|
|
47
48
|
}
|
|
48
49
|
// Fallback to standard fetch
|
|
49
50
|
return fetch(input, init);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { EventEmitter } from "events";
|
|
2
|
+
import type { StreamingSession, StreamingConfig, StreamingPoolConfig, StreamingMetrics, StreamingHealthStatus, BufferConfig } from "../types.js";
|
|
3
|
+
export declare class StreamingManager extends EventEmitter {
|
|
4
|
+
private activeSessions;
|
|
5
|
+
private streamingPools;
|
|
6
|
+
private metrics;
|
|
7
|
+
private healthCheckInterval?;
|
|
8
|
+
private startTime;
|
|
9
|
+
constructor();
|
|
10
|
+
createStreamingSession(config: StreamingConfig): Promise<StreamingSession>;
|
|
11
|
+
terminateStreamingSession(sessionId: string): Promise<void>;
|
|
12
|
+
pauseStreamingSession(sessionId: string): Promise<void>;
|
|
13
|
+
resumeStreamingSession(sessionId: string): Promise<void>;
|
|
14
|
+
optimizeStreamingLatency(sessionId: string): Promise<void>;
|
|
15
|
+
enableStreamingCompression(sessionId: string): Promise<void>;
|
|
16
|
+
configureStreamingBuffering(sessionId: string, bufferConfig: BufferConfig): Promise<void>;
|
|
17
|
+
createStreamingPool(poolId: string, config: StreamingPoolConfig): Promise<void>;
|
|
18
|
+
balanceStreamingLoad(poolId: string): Promise<void>;
|
|
19
|
+
scaleStreamingCapacity(poolId: string, scale: number): Promise<void>;
|
|
20
|
+
getStreamingMetrics(sessionId?: string): StreamingMetrics;
|
|
21
|
+
getStreamingHealthStatus(): StreamingHealthStatus;
|
|
22
|
+
private updateGlobalMetrics;
|
|
23
|
+
private startHealthMonitoring;
|
|
24
|
+
private roundRobinBalance;
|
|
25
|
+
private leastConnectionsBalance;
|
|
26
|
+
private weightedBalance;
|
|
27
|
+
private adaptiveBalance;
|
|
28
|
+
destroy(): void;
|
|
29
|
+
}
|