@juspay/neurolink 4.2.0 → 5.1.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 +47 -2
- package/README.md +51 -60
- package/dist/chat/sse-handler.js +5 -4
- package/dist/chat/websocket-chat-handler.js +9 -9
- package/dist/cli/commands/mcp.js +1 -1
- package/dist/cli/commands/ollama.js +3 -3
- package/dist/cli/factories/command-factory.d.ts +14 -0
- package/dist/cli/factories/command-factory.js +129 -0
- package/dist/cli/index.js +27 -29
- package/dist/cli/utils/interactive-setup.js +2 -2
- package/dist/core/evaluation.d.ts +9 -9
- package/dist/core/evaluation.js +14 -14
- package/dist/core/types.d.ts +41 -48
- 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-generate-factory.d.ts +20 -0
- package/dist/factories/provider-generate-factory.js +87 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +3 -1
- package/dist/lib/chat/sse-handler.js +5 -4
- package/dist/lib/chat/websocket-chat-handler.js +9 -9
- package/dist/lib/core/evaluation.d.ts +9 -9
- package/dist/lib/core/evaluation.js +14 -14
- package/dist/lib/core/types.d.ts +41 -48
- 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-generate-factory.d.ts +20 -0
- package/dist/lib/factories/provider-generate-factory.js +87 -0
- package/dist/lib/index.d.ts +4 -2
- package/dist/lib/index.js +3 -1
- package/dist/lib/mcp/client.js +5 -5
- 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/neurolink-mcp-client.js +10 -10
- package/dist/lib/mcp/orchestrator.js +4 -4
- package/dist/lib/mcp/servers/ai-providers/ai-analysis-tools.js +10 -10
- package/dist/lib/mcp/servers/ai-providers/ai-core-server.js +5 -5
- 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/neurolink.d.ts +21 -73
- package/dist/lib/neurolink.js +230 -119
- package/dist/lib/providers/agent-enhanced-provider.d.ts +12 -8
- package/dist/lib/providers/agent-enhanced-provider.js +87 -96
- package/dist/lib/providers/amazonBedrock.d.ts +17 -8
- package/dist/lib/providers/amazonBedrock.js +60 -30
- package/dist/lib/providers/anthropic.d.ts +14 -10
- package/dist/lib/providers/anthropic.js +84 -154
- package/dist/lib/providers/azureOpenAI.d.ts +9 -6
- package/dist/lib/providers/azureOpenAI.js +70 -159
- 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/googleAIStudio.d.ts +12 -19
- package/dist/lib/providers/googleAIStudio.js +65 -34
- package/dist/lib/providers/googleVertexAI.d.ts +11 -15
- package/dist/lib/providers/googleVertexAI.js +146 -118
- package/dist/lib/providers/huggingFace.d.ts +10 -11
- package/dist/lib/providers/huggingFace.js +61 -24
- package/dist/lib/providers/mcp-provider.d.ts +13 -8
- package/dist/lib/providers/mcp-provider.js +59 -18
- package/dist/lib/providers/mistralAI.d.ts +14 -11
- package/dist/lib/providers/mistralAI.js +60 -29
- package/dist/lib/providers/ollama.d.ts +9 -8
- package/dist/lib/providers/ollama.js +134 -91
- package/dist/lib/providers/openAI.d.ts +11 -12
- package/dist/lib/providers/openAI.js +132 -97
- package/dist/lib/types/generate-types.d.ts +79 -0
- package/dist/lib/types/generate-types.js +1 -0
- package/dist/lib/types/stream-types.d.ts +83 -0
- package/dist/lib/types/stream-types.js +1 -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/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/neurolink-mcp-client.js +10 -10
- package/dist/mcp/orchestrator.js +4 -4
- package/dist/mcp/servers/ai-providers/ai-analysis-tools.js +10 -10
- package/dist/mcp/servers/ai-providers/ai-core-server.js +5 -5
- 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/neurolink.d.ts +21 -73
- package/dist/neurolink.js +230 -119
- package/dist/providers/agent-enhanced-provider.d.ts +12 -8
- package/dist/providers/agent-enhanced-provider.js +87 -95
- package/dist/providers/amazonBedrock.d.ts +17 -8
- package/dist/providers/amazonBedrock.js +60 -30
- package/dist/providers/anthropic.d.ts +14 -10
- package/dist/providers/anthropic.js +84 -154
- package/dist/providers/azureOpenAI.d.ts +9 -6
- package/dist/providers/azureOpenAI.js +70 -159
- package/dist/providers/function-calling-provider.d.ts +14 -12
- package/dist/providers/function-calling-provider.js +114 -64
- package/dist/providers/googleAIStudio.d.ts +12 -19
- package/dist/providers/googleAIStudio.js +65 -34
- package/dist/providers/googleVertexAI.d.ts +11 -15
- package/dist/providers/googleVertexAI.js +146 -118
- package/dist/providers/huggingFace.d.ts +10 -11
- package/dist/providers/huggingFace.js +61 -24
- package/dist/providers/mcp-provider.d.ts +13 -8
- package/dist/providers/mcp-provider.js +59 -18
- package/dist/providers/mistralAI.d.ts +14 -11
- package/dist/providers/mistralAI.js +60 -29
- package/dist/providers/ollama.d.ts +9 -8
- package/dist/providers/ollama.js +133 -90
- package/dist/providers/openAI.d.ts +11 -12
- package/dist/providers/openAI.js +132 -97
- package/dist/types/generate-types.d.ts +79 -0
- package/dist/types/generate-types.js +1 -0
- package/dist/types/stream-types.d.ts +83 -0
- package/dist/types/stream-types.js +1 -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 +2 -3
- package/dist/cli/commands/agent-generate.d.ts +0 -1
- package/dist/cli/commands/agent-generate.js +0 -67
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Agent-Enhanced Provider for NeuroLink CLI
|
|
3
3
|
* Integrates direct tools with AI providers for true agent functionality
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
5
|
+
import { streamText, tool, generateText as aiGenerate } from "ai";
|
|
6
6
|
import { google } from "@ai-sdk/google";
|
|
7
7
|
import { openai } from "@ai-sdk/openai";
|
|
8
8
|
import { anthropic } from "@ai-sdk/anthropic";
|
|
@@ -258,7 +258,37 @@ export class AgentEnhancedProvider {
|
|
|
258
258
|
}
|
|
259
259
|
return { ...directTools, ...mcpTools };
|
|
260
260
|
}
|
|
261
|
-
|
|
261
|
+
/**
|
|
262
|
+
* PRIMARY METHOD: Stream content using AI (recommended for new code)
|
|
263
|
+
* Future-ready for multi-modal capabilities with current text focus
|
|
264
|
+
*/
|
|
265
|
+
async stream(optionsOrPrompt, analysisSchema) {
|
|
266
|
+
const functionTag = "AgentEnhancedProvider.stream";
|
|
267
|
+
const startTime = Date.now();
|
|
268
|
+
// Parse parameters - support both string and options object
|
|
269
|
+
const options = typeof optionsOrPrompt === "string"
|
|
270
|
+
? { input: { text: optionsOrPrompt } }
|
|
271
|
+
: optionsOrPrompt;
|
|
272
|
+
// Validate input
|
|
273
|
+
if (!options?.input?.text ||
|
|
274
|
+
typeof options.input.text !== "string" ||
|
|
275
|
+
options.input.text.trim() === "") {
|
|
276
|
+
throw new Error("Stream options must include input.text as a non-empty string");
|
|
277
|
+
}
|
|
278
|
+
// Convert StreamOptions for internal use
|
|
279
|
+
const convertedOptions = {
|
|
280
|
+
prompt: options.input.text,
|
|
281
|
+
provider: options.provider,
|
|
282
|
+
model: options.model,
|
|
283
|
+
temperature: options.temperature,
|
|
284
|
+
maxTokens: options.maxTokens,
|
|
285
|
+
systemPrompt: options.systemPrompt,
|
|
286
|
+
timeout: options.timeout,
|
|
287
|
+
};
|
|
288
|
+
// Use stream method to get streaming result
|
|
289
|
+
return await this.stream(options);
|
|
290
|
+
}
|
|
291
|
+
async generate(optionsOrPrompt) {
|
|
262
292
|
const startTime = Date.now();
|
|
263
293
|
const options = typeof optionsOrPrompt === "string"
|
|
264
294
|
? { prompt: optionsOrPrompt }
|
|
@@ -284,7 +314,7 @@ export class AgentEnhancedProvider {
|
|
|
284
314
|
}
|
|
285
315
|
}
|
|
286
316
|
// The AI SDK with maxSteps automatically handles tool calling and result integration
|
|
287
|
-
const result = await
|
|
317
|
+
const result = await aiGenerate({
|
|
288
318
|
model: this.model,
|
|
289
319
|
prompt: systemPrompt
|
|
290
320
|
? `System: ${systemPrompt}\n\nUser: ${prompt}`
|
|
@@ -345,9 +375,21 @@ export class AgentEnhancedProvider {
|
|
|
345
375
|
});
|
|
346
376
|
// Return result with the formatted text
|
|
347
377
|
return {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
378
|
+
content: finalText,
|
|
379
|
+
provider: this.getProviderName(),
|
|
380
|
+
model: this.getModelName(),
|
|
381
|
+
usage: result.usage
|
|
382
|
+
? {
|
|
383
|
+
inputTokens: result.usage.promptTokens,
|
|
384
|
+
outputTokens: result.usage.completionTokens,
|
|
385
|
+
totalTokens: result.usage.totalTokens,
|
|
386
|
+
}
|
|
387
|
+
: undefined,
|
|
388
|
+
responseTime: 0,
|
|
389
|
+
toolsUsed: [],
|
|
390
|
+
toolExecutions: [],
|
|
391
|
+
enhancedWithTools: false,
|
|
392
|
+
availableTools: [],
|
|
351
393
|
};
|
|
352
394
|
}
|
|
353
395
|
catch (error) {
|
|
@@ -357,9 +399,21 @@ export class AgentEnhancedProvider {
|
|
|
357
399
|
// Fallback: return raw tool results
|
|
358
400
|
const fallbackText = `Tool execution completed. Raw results: ${JSON.stringify(result.toolResults, null, 2)}`;
|
|
359
401
|
return {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
402
|
+
content: fallbackText,
|
|
403
|
+
provider: this.getProviderName(),
|
|
404
|
+
model: this.getModelName(),
|
|
405
|
+
usage: result.usage
|
|
406
|
+
? {
|
|
407
|
+
inputTokens: result.usage.promptTokens,
|
|
408
|
+
outputTokens: result.usage.completionTokens,
|
|
409
|
+
totalTokens: result.usage.totalTokens,
|
|
410
|
+
}
|
|
411
|
+
: undefined,
|
|
412
|
+
responseTime: 0,
|
|
413
|
+
toolsUsed: [],
|
|
414
|
+
toolExecutions: [],
|
|
415
|
+
enhancedWithTools: false,
|
|
416
|
+
availableTools: [],
|
|
363
417
|
};
|
|
364
418
|
}
|
|
365
419
|
}
|
|
@@ -372,85 +426,26 @@ export class AgentEnhancedProvider {
|
|
|
372
426
|
result.evaluation = await evaluateResponse(prompt, result.text, options.context);
|
|
373
427
|
}
|
|
374
428
|
// Return the full result - the AI SDK has already handled tool execution and integration
|
|
375
|
-
return
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
? { prompt: optionsOrPrompt }
|
|
385
|
-
: optionsOrPrompt;
|
|
386
|
-
const { prompt, temperature = 0.7, maxTokens = 1000, systemPrompt, timeout,
|
|
387
|
-
// Phase 2: Enhanced streaming options
|
|
388
|
-
enableProgressTracking, progressCallback, includeStreamingMetadata, streamingBufferSize, enableStreamingHeaders, customStreamingConfig, } = options;
|
|
389
|
-
// Phase 2.1: Setup streaming enhancements
|
|
390
|
-
const streamId = `agent_stream_${Date.now()}`;
|
|
391
|
-
const streamingConfig = StreamingEnhancer.createStreamingConfig(options);
|
|
392
|
-
if (enableProgressTracking) {
|
|
393
|
-
StreamingMonitor.registerStream(streamId);
|
|
394
|
-
}
|
|
395
|
-
// Get combined tools (direct + MCP) if enabled
|
|
396
|
-
const tools = this.config.enableTools ? await this.getCombinedTools() : {};
|
|
397
|
-
try {
|
|
398
|
-
// Parse timeout if provided
|
|
399
|
-
let abortSignal;
|
|
400
|
-
if (timeout) {
|
|
401
|
-
const timeoutMs = typeof timeout === "string" ? parseTimeout(timeout) : timeout;
|
|
402
|
-
if (timeoutMs !== undefined) {
|
|
403
|
-
abortSignal = AbortSignal.timeout(timeoutMs);
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
const result = await streamText({
|
|
407
|
-
model: this.model,
|
|
408
|
-
prompt: systemPrompt
|
|
409
|
-
? `System: ${systemPrompt}\n\nUser: ${prompt}`
|
|
410
|
-
: prompt,
|
|
411
|
-
tools,
|
|
412
|
-
maxSteps: this.config.maxSteps,
|
|
413
|
-
temperature,
|
|
414
|
-
maxTokens,
|
|
415
|
-
toolChoice: this.shouldForceToolUsage(prompt) ? "required" : "auto",
|
|
416
|
-
abortSignal, // Pass abort signal for timeout support
|
|
417
|
-
});
|
|
418
|
-
// Phase 2.1: Apply streaming enhancements if enabled
|
|
419
|
-
if (streamingConfig.progressTracking && result.textStream) {
|
|
420
|
-
const enhancedCallback = streamingConfig.callback
|
|
421
|
-
? (progress) => {
|
|
422
|
-
StreamingMonitor.updateStream(streamId, progress);
|
|
423
|
-
streamingConfig.callback(progress);
|
|
424
|
-
if (progress.phase === "complete") {
|
|
425
|
-
StreamingMonitor.completeStream(streamId);
|
|
426
|
-
}
|
|
429
|
+
return {
|
|
430
|
+
content: result.text,
|
|
431
|
+
provider: this.getProviderName(),
|
|
432
|
+
model: this.getModelName(),
|
|
433
|
+
usage: result.usage
|
|
434
|
+
? {
|
|
435
|
+
inputTokens: result.usage.promptTokens,
|
|
436
|
+
outputTokens: result.usage.completionTokens,
|
|
437
|
+
totalTokens: result.usage.totalTokens,
|
|
427
438
|
}
|
|
428
|
-
: undefined
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
// Phase 2.1: Add streaming metadata
|
|
436
|
-
streamingMetadata: streamingConfig.metadata
|
|
437
|
-
? {
|
|
438
|
-
streamId,
|
|
439
|
-
provider: this.getProviderName(),
|
|
440
|
-
model: this.getModelName(),
|
|
441
|
-
enabledFeatures: {
|
|
442
|
-
progressTracking: true,
|
|
443
|
-
metadata: streamingConfig.metadata,
|
|
444
|
-
headers: streamingConfig.headers,
|
|
445
|
-
},
|
|
446
|
-
}
|
|
447
|
-
: undefined,
|
|
448
|
-
};
|
|
449
|
-
}
|
|
450
|
-
return result;
|
|
439
|
+
: undefined,
|
|
440
|
+
responseTime: 0,
|
|
441
|
+
toolsUsed: [],
|
|
442
|
+
toolExecutions: [],
|
|
443
|
+
enhancedWithTools: false,
|
|
444
|
+
availableTools: [],
|
|
445
|
+
};
|
|
451
446
|
}
|
|
452
447
|
catch (error) {
|
|
453
|
-
console.error("[AgentEnhancedProvider]
|
|
448
|
+
console.error("[AgentEnhancedProvider] generate error:", error);
|
|
454
449
|
throw error;
|
|
455
450
|
}
|
|
456
451
|
}
|
|
@@ -500,12 +495,12 @@ export class AgentEnhancedProvider {
|
|
|
500
495
|
for (const prompt of testPrompts) {
|
|
501
496
|
try {
|
|
502
497
|
logger.debug(`Testing: "${prompt}"`);
|
|
503
|
-
const result = await this.
|
|
498
|
+
const result = await this.generate(prompt);
|
|
504
499
|
if (!result) {
|
|
505
500
|
results.push({
|
|
506
501
|
prompt,
|
|
507
502
|
success: false,
|
|
508
|
-
error: "No result returned from
|
|
503
|
+
error: "No result returned from generate",
|
|
509
504
|
});
|
|
510
505
|
logger.warn(`❌ No result returned`);
|
|
511
506
|
continue;
|
|
@@ -519,9 +514,9 @@ export class AgentEnhancedProvider {
|
|
|
519
514
|
prompt,
|
|
520
515
|
success,
|
|
521
516
|
toolsCalled,
|
|
522
|
-
response: result.
|
|
517
|
+
response: result.content.substring(0, 100) + "...",
|
|
523
518
|
});
|
|
524
|
-
logger.debug(`✅ Tools called: ${toolsCalled}, Response: ${result.
|
|
519
|
+
logger.debug(`✅ Tools called: ${toolsCalled}, Response: ${result.content.substring(0, 50)}...`);
|
|
525
520
|
}
|
|
526
521
|
catch (error) {
|
|
527
522
|
results.push({
|
|
@@ -565,16 +560,13 @@ export class AgentEnhancedProvider {
|
|
|
565
560
|
return providers;
|
|
566
561
|
}
|
|
567
562
|
/**
|
|
568
|
-
* Alias for
|
|
563
|
+
* Alias for generate() - CLI-SDK consistency
|
|
569
564
|
*/
|
|
570
|
-
async generate(optionsOrPrompt, analysisSchema) {
|
|
571
|
-
return this.generateText(optionsOrPrompt);
|
|
572
|
-
}
|
|
573
565
|
/**
|
|
574
|
-
* Short alias for
|
|
566
|
+
* Short alias for generate() - CLI-SDK consistency
|
|
575
567
|
*/
|
|
576
568
|
async gen(optionsOrPrompt, analysisSchema) {
|
|
577
|
-
return this.
|
|
569
|
+
return this.generate(optionsOrPrompt);
|
|
578
570
|
}
|
|
579
571
|
}
|
|
580
572
|
/**
|
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
import type { ZodType, ZodTypeDef } from "zod";
|
|
2
|
-
import { type
|
|
3
|
-
import type {
|
|
2
|
+
import { type Schema } from "ai";
|
|
3
|
+
import type { GenerateResult } from "../types/generate-types.js";
|
|
4
|
+
import type { StreamOptions, StreamResult } from "../types/stream-types.js";
|
|
5
|
+
import type { AIProvider, TextGenerationOptions, EnhancedGenerateResult } from "../core/types.js";
|
|
4
6
|
export declare class AmazonBedrock implements AIProvider {
|
|
5
7
|
private modelName;
|
|
6
8
|
private model;
|
|
7
9
|
private bedrock;
|
|
8
10
|
constructor(modelName?: string | null);
|
|
9
|
-
streamText(optionsOrPrompt: StreamTextOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamTextResult<ToolSet, unknown> | null>;
|
|
10
|
-
generateText(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<GenerateTextResult<ToolSet, unknown> | null>;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* LEGACY METHOD: Use stream() instead for new code
|
|
13
|
+
* @deprecated Use stream() method instead
|
|
13
14
|
*/
|
|
14
|
-
generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<EnhancedGenerateTextResult | null>;
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
16
|
+
* PRIMARY METHOD: Stream content using AI (recommended for new code)
|
|
17
|
+
* Future-ready for multi-modal capabilities with current text focus
|
|
17
18
|
*/
|
|
18
|
-
|
|
19
|
+
stream(optionsOrPrompt: StreamOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamResult>;
|
|
20
|
+
generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<GenerateResult>;
|
|
21
|
+
/**
|
|
22
|
+
* Alias for generate() - CLI-SDK consistency
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Short alias for generate() - CLI-SDK consistency
|
|
26
|
+
*/
|
|
27
|
+
gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<EnhancedGenerateResult | null>;
|
|
19
28
|
}
|
|
@@ -123,22 +123,38 @@ export class AmazonBedrock {
|
|
|
123
123
|
throw err;
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
/**
|
|
127
|
+
* LEGACY METHOD: Use stream() instead for new code
|
|
128
|
+
* @deprecated Use stream() method instead
|
|
129
|
+
*/
|
|
130
|
+
/**
|
|
131
|
+
* PRIMARY METHOD: Stream content using AI (recommended for new code)
|
|
132
|
+
* Future-ready for multi-modal capabilities with current text focus
|
|
133
|
+
*/
|
|
134
|
+
async stream(optionsOrPrompt, analysisSchema) {
|
|
135
|
+
const functionTag = "AmazonBedrock.stream";
|
|
128
136
|
const provider = "bedrock";
|
|
129
137
|
let chunkCount = 0;
|
|
138
|
+
const startTime = Date.now();
|
|
130
139
|
try {
|
|
131
140
|
// Parse parameters - support both string and options object
|
|
132
141
|
const options = typeof optionsOrPrompt === "string"
|
|
133
|
-
? {
|
|
142
|
+
? { input: { text: optionsOrPrompt } }
|
|
134
143
|
: optionsOrPrompt;
|
|
135
|
-
|
|
144
|
+
// Validate input
|
|
145
|
+
if (!options?.input?.text ||
|
|
146
|
+
typeof options.input.text !== "string" ||
|
|
147
|
+
options.input.text.trim() === "") {
|
|
148
|
+
throw new Error("Stream options must include input.text as a non-empty string");
|
|
149
|
+
}
|
|
150
|
+
// Extract prompt and other parameters
|
|
151
|
+
const { prompt = options.input.text, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout(provider, "stream"), } = options;
|
|
136
152
|
// Use schema from options or fallback parameter
|
|
137
153
|
const finalSchema = schema || analysisSchema;
|
|
138
154
|
logger.debug(`[${functionTag}] Stream request started`, {
|
|
139
155
|
provider,
|
|
140
156
|
modelName: this.modelName,
|
|
141
|
-
promptLength: prompt
|
|
157
|
+
promptLength: prompt?.length || 0,
|
|
142
158
|
temperature,
|
|
143
159
|
maxTokens,
|
|
144
160
|
timeout,
|
|
@@ -159,10 +175,9 @@ export class AmazonBedrock {
|
|
|
159
175
|
const error = event.error;
|
|
160
176
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
161
177
|
const errorStack = error instanceof Error ? error.stack : undefined;
|
|
162
|
-
logger.
|
|
178
|
+
logger.debug(`[${functionTag}] Stream error`, {
|
|
163
179
|
provider,
|
|
164
180
|
modelName: this.modelName,
|
|
165
|
-
region: getAWSRegion(),
|
|
166
181
|
error: errorMessage,
|
|
167
182
|
stack: errorStack,
|
|
168
183
|
promptLength: prompt.length,
|
|
@@ -170,10 +185,9 @@ export class AmazonBedrock {
|
|
|
170
185
|
});
|
|
171
186
|
},
|
|
172
187
|
onFinish: (event) => {
|
|
173
|
-
logger.debug(`[${functionTag}] Stream
|
|
188
|
+
logger.debug(`[${functionTag}] Stream finished`, {
|
|
174
189
|
provider,
|
|
175
190
|
modelName: this.modelName,
|
|
176
|
-
region: getAWSRegion(),
|
|
177
191
|
finishReason: event.finishReason,
|
|
178
192
|
usage: event.usage,
|
|
179
193
|
totalChunks: chunkCount,
|
|
@@ -183,7 +197,7 @@ export class AmazonBedrock {
|
|
|
183
197
|
},
|
|
184
198
|
onChunk: (event) => {
|
|
185
199
|
chunkCount++;
|
|
186
|
-
logger.debug(`[${functionTag}] Stream
|
|
200
|
+
logger.debug(`[${functionTag}] Stream chunk`, {
|
|
187
201
|
provider,
|
|
188
202
|
modelName: this.modelName,
|
|
189
203
|
chunkNumber: chunkCount,
|
|
@@ -197,42 +211,49 @@ export class AmazonBedrock {
|
|
|
197
211
|
schema: finalSchema,
|
|
198
212
|
});
|
|
199
213
|
}
|
|
200
|
-
// Direct streamText call - let the real error bubble up
|
|
201
214
|
const result = streamText(streamOptions);
|
|
202
|
-
logger.debug(`[${functionTag}] Stream
|
|
215
|
+
logger.debug(`[${functionTag}] Stream request completed`, {
|
|
203
216
|
provider,
|
|
204
217
|
modelName: this.modelName,
|
|
205
|
-
promptLength: prompt.length,
|
|
206
218
|
});
|
|
207
|
-
//
|
|
208
|
-
|
|
209
|
-
|
|
219
|
+
// Convert to StreamResult format
|
|
220
|
+
return {
|
|
221
|
+
stream: (async function* () {
|
|
222
|
+
for await (const chunk of result.textStream) {
|
|
223
|
+
yield { content: chunk };
|
|
224
|
+
}
|
|
225
|
+
})(),
|
|
226
|
+
provider: "bedrock",
|
|
227
|
+
model: this.modelName,
|
|
228
|
+
metadata: {
|
|
229
|
+
streamId: `bedrock-${Date.now()}`,
|
|
230
|
+
startTime,
|
|
231
|
+
},
|
|
232
|
+
};
|
|
210
233
|
}
|
|
211
234
|
catch (err) {
|
|
212
235
|
// Log timeout errors specifically
|
|
213
236
|
if (err instanceof TimeoutError) {
|
|
214
|
-
logger.
|
|
237
|
+
logger.debug(`[${functionTag}] Timeout error`, {
|
|
215
238
|
provider,
|
|
216
239
|
modelName: this.modelName,
|
|
217
|
-
region: getAWSRegion(),
|
|
218
240
|
timeout: err.timeout,
|
|
219
241
|
message: err.message,
|
|
220
242
|
});
|
|
221
243
|
}
|
|
222
244
|
else {
|
|
223
|
-
logger.
|
|
245
|
+
logger.debug(`[${functionTag}] Exception`, {
|
|
224
246
|
provider,
|
|
225
247
|
modelName: this.modelName,
|
|
226
|
-
|
|
227
|
-
message: "Error in streaming text",
|
|
248
|
+
message: "Error in streaming content",
|
|
228
249
|
err: String(err),
|
|
229
250
|
});
|
|
230
251
|
}
|
|
231
252
|
throw err; // Re-throw error to trigger fallback
|
|
232
253
|
}
|
|
233
254
|
}
|
|
234
|
-
async
|
|
235
|
-
const functionTag = "AmazonBedrock.
|
|
255
|
+
async generate(optionsOrPrompt, analysisSchema) {
|
|
256
|
+
const functionTag = "AmazonBedrock.generate";
|
|
236
257
|
const provider = "bedrock";
|
|
237
258
|
const startTime = Date.now();
|
|
238
259
|
try {
|
|
@@ -290,7 +311,19 @@ export class AmazonBedrock {
|
|
|
290
311
|
if (options.enableEvaluation) {
|
|
291
312
|
result.evaluation = await evaluateResponse(prompt, result.text, options.context);
|
|
292
313
|
}
|
|
293
|
-
return
|
|
314
|
+
return {
|
|
315
|
+
content: result.text,
|
|
316
|
+
provider: "bedrock",
|
|
317
|
+
model: this.modelName || "claude-3-sonnet",
|
|
318
|
+
usage: result.usage
|
|
319
|
+
? {
|
|
320
|
+
inputTokens: result.usage.promptTokens,
|
|
321
|
+
outputTokens: result.usage.completionTokens,
|
|
322
|
+
totalTokens: result.usage.totalTokens,
|
|
323
|
+
}
|
|
324
|
+
: undefined,
|
|
325
|
+
responseTime: 0,
|
|
326
|
+
};
|
|
294
327
|
}
|
|
295
328
|
finally {
|
|
296
329
|
// Always cleanup timeout
|
|
@@ -320,15 +353,12 @@ export class AmazonBedrock {
|
|
|
320
353
|
}
|
|
321
354
|
}
|
|
322
355
|
/**
|
|
323
|
-
* Alias for
|
|
356
|
+
* Alias for generate() - CLI-SDK consistency
|
|
324
357
|
*/
|
|
325
|
-
async generate(optionsOrPrompt, analysisSchema) {
|
|
326
|
-
return this.generateText(optionsOrPrompt, analysisSchema);
|
|
327
|
-
}
|
|
328
358
|
/**
|
|
329
|
-
* Short alias for
|
|
359
|
+
* Short alias for generate() - CLI-SDK consistency
|
|
330
360
|
*/
|
|
331
361
|
async gen(optionsOrPrompt, analysisSchema) {
|
|
332
|
-
return this.
|
|
362
|
+
return this.generate(optionsOrPrompt, analysisSchema);
|
|
333
363
|
}
|
|
334
364
|
}
|
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
* Direct integration with Anthropic's Claude models via their native API.
|
|
5
5
|
* Supports Claude 3.5 Sonnet, Claude 3.5 Haiku, and Claude 3 Opus.
|
|
6
6
|
*/
|
|
7
|
-
import type { AIProvider, TextGenerationOptions,
|
|
7
|
+
import type { AIProvider, TextGenerationOptions, EnhancedGenerateResult } from "../core/types.js";
|
|
8
8
|
import type { ZodType, ZodTypeDef } from "zod";
|
|
9
9
|
import type { Schema } from "ai";
|
|
10
|
+
import type { StreamOptions, StreamResult } from "../types/stream-types.js";
|
|
10
11
|
import { AIProviderName } from "../core/types.js";
|
|
11
12
|
export declare class AnthropicProvider implements AIProvider {
|
|
12
13
|
readonly name: AIProviderName;
|
|
@@ -17,10 +18,17 @@ export declare class AnthropicProvider implements AIProvider {
|
|
|
17
18
|
private getApiKey;
|
|
18
19
|
private getModel;
|
|
19
20
|
private makeRequest;
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
/**
|
|
22
|
+
* PRIMARY METHOD: Stream content using AI (recommended for new code)
|
|
23
|
+
* Future-ready for multi-modal capabilities with current text focus
|
|
24
|
+
*/
|
|
25
|
+
stream(optionsOrPrompt: StreamOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamResult>;
|
|
26
|
+
generate(optionsOrPrompt: TextGenerationOptions | string, schema?: any): Promise<any>;
|
|
27
|
+
/**
|
|
28
|
+
* LEGACY METHOD: Use stream() instead for new code
|
|
29
|
+
* @deprecated Use stream() method instead
|
|
30
|
+
*/
|
|
22
31
|
private createAsyncIterable;
|
|
23
|
-
generateTextStream(optionsOrPrompt: StreamTextOptions | string): AsyncGenerator<any, void, unknown>;
|
|
24
32
|
testConnection(): Promise<{
|
|
25
33
|
success: boolean;
|
|
26
34
|
error?: string;
|
|
@@ -34,11 +42,7 @@ export declare class AnthropicProvider implements AIProvider {
|
|
|
34
42
|
supportsSchema(): boolean;
|
|
35
43
|
getCapabilities(): string[];
|
|
36
44
|
/**
|
|
37
|
-
*
|
|
38
|
-
*/
|
|
39
|
-
generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<EnhancedGenerateTextResult | null>;
|
|
40
|
-
/**
|
|
41
|
-
* Short alias for generateText() - CLI-SDK consistency
|
|
45
|
+
* Short alias for generate() - CLI-SDK consistency
|
|
42
46
|
*/
|
|
43
|
-
gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<
|
|
47
|
+
gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<EnhancedGenerateResult | null>;
|
|
44
48
|
}
|