@juspay/neurolink 6.1.0 → 6.2.1
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 +16 -6
- package/dist/agent/direct-tools.js +6 -5
- package/dist/cli/commands/config.d.ts +13 -13
- package/dist/cli/index.js +3 -9
- package/dist/config/configManager.js +11 -10
- package/dist/core/analytics.d.ts +11 -1
- package/dist/core/analytics.js +2 -2
- package/dist/core/base-provider.js +4 -18
- package/dist/core/dynamic-models.d.ts +8 -8
- package/dist/core/factory.js +3 -11
- package/dist/index.d.ts +2 -4
- package/dist/index.js +2 -11
- package/dist/lib/agent/direct-tools.js +6 -5
- package/dist/lib/config/configManager.js +11 -10
- package/dist/lib/core/analytics.d.ts +11 -1
- package/dist/lib/core/analytics.js +2 -2
- package/dist/lib/core/base-provider.js +4 -18
- package/dist/lib/core/dynamic-models.d.ts +8 -8
- package/dist/lib/core/factory.js +3 -11
- package/dist/lib/index.d.ts +2 -4
- package/dist/lib/index.js +2 -11
- package/dist/lib/mcp/factory.d.ts +1 -1
- package/dist/lib/mcp/index.d.ts +9 -21
- package/dist/lib/mcp/index.js +16 -57
- package/dist/lib/mcp/registry.js +1 -1
- package/dist/lib/mcp/servers/ai-providers/ai-analysis-tools.js +2 -1
- package/dist/lib/mcp/tool-registry.js +1 -1
- package/dist/lib/neurolink.d.ts +1 -9
- package/dist/lib/neurolink.js +22 -71
- package/dist/lib/providers/amazon-bedrock.js +5 -16
- package/dist/lib/providers/anthropic-baseprovider.js +3 -21
- package/dist/lib/providers/anthropic.js +4 -7
- package/dist/lib/providers/azure-openai.js +6 -3
- package/dist/lib/providers/google-vertex.js +5 -9
- package/dist/lib/providers/huggingFace.js +5 -10
- package/dist/lib/providers/mistral.js +5 -8
- package/dist/lib/providers/openAI.js +4 -7
- package/dist/lib/telemetry/index.js +2 -1
- package/dist/lib/utils/logger.d.ts +62 -2
- package/dist/lib/utils/logger.js +174 -9
- package/dist/lib/utils/providerConfig.d.ts +117 -0
- package/dist/lib/utils/providerConfig.js +353 -0
- package/dist/lib/utils/providerUtils.d.ts +2 -1
- package/dist/lib/utils/providerUtils.js +53 -36
- package/dist/lib/utils/timeout.d.ts +72 -1
- package/dist/lib/utils/timeout.js +203 -2
- package/dist/mcp/factory.d.ts +1 -1
- package/dist/mcp/index.d.ts +9 -21
- package/dist/mcp/index.js +16 -57
- package/dist/mcp/registry.js +1 -1
- package/dist/mcp/servers/ai-providers/ai-analysis-tools.js +2 -1
- package/dist/mcp/tool-registry.js +1 -1
- package/dist/neurolink.d.ts +1 -9
- package/dist/neurolink.js +22 -71
- package/dist/providers/amazon-bedrock.js +5 -16
- package/dist/providers/anthropic-baseprovider.js +3 -21
- package/dist/providers/anthropic.js +4 -7
- package/dist/providers/azure-openai.js +6 -3
- package/dist/providers/google-vertex.js +5 -9
- package/dist/providers/huggingFace.js +5 -10
- package/dist/providers/mistral.js +5 -8
- package/dist/providers/openAI.js +4 -7
- package/dist/telemetry/index.js +2 -1
- package/dist/utils/logger.d.ts +62 -2
- package/dist/utils/logger.js +174 -9
- package/dist/utils/providerConfig.d.ts +117 -0
- package/dist/utils/providerConfig.js +353 -0
- package/dist/utils/providerUtils.d.ts +2 -1
- package/dist/utils/providerUtils.js +53 -36
- package/dist/utils/timeout.d.ts +72 -1
- package/dist/utils/timeout.js +203 -2
- package/package.json +1 -1
- package/dist/chat/client-utils.d.ts +0 -95
- package/dist/chat/client-utils.js +0 -315
- package/dist/chat/index.d.ts +0 -24
- package/dist/chat/index.js +0 -33
- package/dist/chat/session-storage.d.ts +0 -77
- package/dist/chat/session-storage.js +0 -233
- package/dist/chat/session.d.ts +0 -96
- package/dist/chat/session.js +0 -257
- package/dist/chat/sse-handler.d.ts +0 -49
- package/dist/chat/sse-handler.js +0 -259
- package/dist/chat/types.d.ts +0 -74
- package/dist/chat/types.js +0 -5
- package/dist/chat/websocket-chat-handler.d.ts +0 -37
- package/dist/chat/websocket-chat-handler.js +0 -262
- package/dist/cli/commands/mcp.d.ts +0 -20
- package/dist/cli/commands/mcp.js +0 -1272
- package/dist/core/defaults.d.ts +0 -19
- package/dist/core/defaults.js +0 -29
- package/dist/core/evaluation-config.d.ts +0 -29
- package/dist/core/evaluation-config.js +0 -144
- package/dist/factories/compatibility-factory.d.ts +0 -35
- package/dist/factories/compatibility-factory.js +0 -71
- package/dist/factories/provider-generate-factory.d.ts +0 -20
- package/dist/factories/provider-generate-factory.js +0 -93
- package/dist/lib/chat/client-utils.d.ts +0 -95
- package/dist/lib/chat/client-utils.js +0 -315
- package/dist/lib/chat/index.d.ts +0 -24
- package/dist/lib/chat/index.js +0 -33
- package/dist/lib/chat/session-storage.d.ts +0 -77
- package/dist/lib/chat/session-storage.js +0 -233
- package/dist/lib/chat/session.d.ts +0 -96
- package/dist/lib/chat/session.js +0 -257
- package/dist/lib/chat/sse-handler.d.ts +0 -49
- package/dist/lib/chat/sse-handler.js +0 -259
- package/dist/lib/chat/types.d.ts +0 -74
- package/dist/lib/chat/types.js +0 -5
- package/dist/lib/chat/websocket-chat-handler.d.ts +0 -37
- package/dist/lib/chat/websocket-chat-handler.js +0 -262
- package/dist/lib/core/defaults.d.ts +0 -19
- package/dist/lib/core/defaults.js +0 -29
- package/dist/lib/core/evaluation-config.d.ts +0 -29
- package/dist/lib/core/evaluation-config.js +0 -144
- package/dist/lib/factories/compatibility-factory.d.ts +0 -35
- package/dist/lib/factories/compatibility-factory.js +0 -71
- package/dist/lib/factories/provider-generate-factory.d.ts +0 -20
- package/dist/lib/factories/provider-generate-factory.js +0 -93
- package/dist/lib/mcp/adapters/plugin-bridge.d.ts +0 -40
- package/dist/lib/mcp/adapters/plugin-bridge.js +0 -89
- package/dist/lib/mcp/auto-discovery.d.ts +0 -62
- package/dist/lib/mcp/auto-discovery.js +0 -149
- package/dist/lib/mcp/client.d.ts +0 -68
- package/dist/lib/mcp/client.js +0 -248
- package/dist/lib/mcp/config.d.ts +0 -31
- package/dist/lib/mcp/config.js +0 -99
- package/dist/lib/mcp/context-manager.d.ts +0 -171
- package/dist/lib/mcp/context-manager.js +0 -362
- package/dist/lib/mcp/contracts/mcp-contract.d.ts +0 -169
- package/dist/lib/mcp/contracts/mcp-contract.js +0 -58
- package/dist/lib/mcp/core/plugin-manager.d.ts +0 -46
- package/dist/lib/mcp/core/plugin-manager.js +0 -110
- package/dist/lib/mcp/demo/plugin-demo.d.ts +0 -20
- package/dist/lib/mcp/demo/plugin-demo.js +0 -118
- package/dist/lib/mcp/dynamic-chain-executor.d.ts +0 -225
- package/dist/lib/mcp/dynamic-chain-executor.js +0 -489
- package/dist/lib/mcp/dynamic-orchestrator.d.ts +0 -115
- package/dist/lib/mcp/dynamic-orchestrator.js +0 -351
- package/dist/lib/mcp/ecosystem.d.ts +0 -75
- package/dist/lib/mcp/ecosystem.js +0 -161
- package/dist/lib/mcp/error-manager.d.ts +0 -254
- package/dist/lib/mcp/error-manager.js +0 -501
- package/dist/lib/mcp/error-recovery.d.ts +0 -159
- package/dist/lib/mcp/error-recovery.js +0 -405
- package/dist/lib/mcp/external-client.d.ts +0 -88
- package/dist/lib/mcp/external-client.js +0 -331
- package/dist/lib/mcp/external-manager.d.ts +0 -112
- package/dist/lib/mcp/external-manager.js +0 -308
- package/dist/lib/mcp/function-calling.d.ts +0 -65
- package/dist/lib/mcp/function-calling.js +0 -642
- package/dist/lib/mcp/health-monitor.d.ts +0 -257
- package/dist/lib/mcp/health-monitor.js +0 -630
- package/dist/lib/mcp/initialize-tools.d.ts +0 -29
- package/dist/lib/mcp/initialize-tools.js +0 -261
- package/dist/lib/mcp/initialize.d.ts +0 -18
- package/dist/lib/mcp/initialize.js +0 -62
- package/dist/lib/mcp/logging.d.ts +0 -38
- package/dist/lib/mcp/logging.js +0 -100
- package/dist/lib/mcp/manager.d.ts +0 -68
- package/dist/lib/mcp/manager.js +0 -176
- package/dist/lib/mcp/neurolink-mcp-client.d.ts +0 -97
- package/dist/lib/mcp/neurolink-mcp-client.js +0 -462
- package/dist/lib/mcp/orchestrator.d.ts +0 -302
- package/dist/lib/mcp/orchestrator.js +0 -703
- package/dist/lib/mcp/plugin-manager.d.ts +0 -98
- package/dist/lib/mcp/plugin-manager.js +0 -296
- package/dist/lib/mcp/plugins/core/filesystem-mcp.d.ts +0 -36
- package/dist/lib/mcp/plugins/core/filesystem-mcp.js +0 -142
- package/dist/lib/mcp/plugins/filesystem-mcp.d.ts +0 -37
- package/dist/lib/mcp/plugins/filesystem-mcp.js +0 -54
- package/dist/lib/mcp/security-manager.d.ts +0 -87
- package/dist/lib/mcp/security-manager.js +0 -344
- package/dist/lib/mcp/semaphore-manager.d.ts +0 -137
- package/dist/lib/mcp/semaphore-manager.js +0 -329
- package/dist/lib/mcp/session-manager.d.ts +0 -187
- package/dist/lib/mcp/session-manager.js +0 -400
- package/dist/lib/mcp/session-persistence.d.ts +0 -93
- package/dist/lib/mcp/session-persistence.js +0 -301
- package/dist/lib/mcp/tool-integration.d.ts +0 -58
- package/dist/lib/mcp/tool-integration.js +0 -203
- package/dist/lib/mcp/transport-manager.d.ts +0 -154
- package/dist/lib/mcp/transport-manager.js +0 -334
- package/dist/lib/mcp/unified-mcp.d.ts +0 -133
- package/dist/lib/mcp/unified-mcp.js +0 -251
- package/dist/lib/mcp/unified-registry.d.ts +0 -165
- package/dist/lib/mcp/unified-registry.js +0 -538
- package/dist/lib/providers/analytics-helper.d.ts +0 -38
- package/dist/lib/providers/analytics-helper.js +0 -216
- package/dist/lib/providers/function-calling-provider.d.ts +0 -142
- package/dist/lib/providers/function-calling-provider.js +0 -630
- package/dist/lib/providers/mcp-provider.d.ts +0 -75
- package/dist/lib/providers/mcp-provider.js +0 -283
- package/dist/lib/providers/timeout-wrapper.d.ts +0 -40
- package/dist/lib/providers/timeout-wrapper.js +0 -100
- package/dist/lib/sdk/tool-extension.d.ts +0 -181
- package/dist/lib/sdk/tool-extension.js +0 -284
- package/dist/lib/services/streaming/streaming-manager.d.ts +0 -29
- package/dist/lib/services/streaming/streaming-manager.js +0 -245
- package/dist/lib/services/types.d.ts +0 -156
- package/dist/lib/services/types.js +0 -2
- package/dist/lib/services/websocket/websocket-server.d.ts +0 -34
- package/dist/lib/services/websocket/websocket-server.js +0 -305
- package/dist/lib/utils/provider-validation.d.ts +0 -36
- package/dist/lib/utils/provider-validation.js +0 -625
- package/dist/lib/utils/providerUtils-fixed.d.ts +0 -8
- package/dist/lib/utils/providerUtils-fixed.js +0 -94
- package/dist/lib/utils/streaming-utils.d.ts +0 -79
- package/dist/lib/utils/streaming-utils.js +0 -198
- package/dist/lib/utils/timeout-manager.d.ts +0 -75
- package/dist/lib/utils/timeout-manager.js +0 -244
- package/dist/mcp/adapters/plugin-bridge.d.ts +0 -40
- package/dist/mcp/adapters/plugin-bridge.js +0 -89
- package/dist/mcp/auto-discovery.d.ts +0 -62
- package/dist/mcp/auto-discovery.js +0 -149
- package/dist/mcp/client.d.ts +0 -68
- package/dist/mcp/client.js +0 -248
- package/dist/mcp/config.d.ts +0 -31
- package/dist/mcp/config.js +0 -99
- package/dist/mcp/context-manager.d.ts +0 -171
- package/dist/mcp/context-manager.js +0 -362
- package/dist/mcp/contracts/mcp-contract.d.ts +0 -169
- package/dist/mcp/contracts/mcp-contract.js +0 -58
- package/dist/mcp/core/plugin-manager.d.ts +0 -46
- package/dist/mcp/core/plugin-manager.js +0 -110
- package/dist/mcp/demo/plugin-demo.d.ts +0 -20
- package/dist/mcp/demo/plugin-demo.js +0 -118
- package/dist/mcp/dynamic-chain-executor.d.ts +0 -225
- package/dist/mcp/dynamic-chain-executor.js +0 -489
- package/dist/mcp/dynamic-orchestrator.d.ts +0 -115
- package/dist/mcp/dynamic-orchestrator.js +0 -351
- package/dist/mcp/ecosystem.d.ts +0 -75
- package/dist/mcp/ecosystem.js +0 -162
- package/dist/mcp/error-manager.d.ts +0 -254
- package/dist/mcp/error-manager.js +0 -501
- package/dist/mcp/error-recovery.d.ts +0 -159
- package/dist/mcp/error-recovery.js +0 -405
- package/dist/mcp/external-client.d.ts +0 -88
- package/dist/mcp/external-client.js +0 -331
- package/dist/mcp/external-manager.d.ts +0 -112
- package/dist/mcp/external-manager.js +0 -308
- package/dist/mcp/function-calling.d.ts +0 -65
- package/dist/mcp/function-calling.js +0 -642
- package/dist/mcp/health-monitor.d.ts +0 -257
- package/dist/mcp/health-monitor.js +0 -630
- package/dist/mcp/initialize-tools.d.ts +0 -29
- package/dist/mcp/initialize-tools.js +0 -262
- package/dist/mcp/initialize.d.ts +0 -18
- package/dist/mcp/initialize.js +0 -62
- package/dist/mcp/logging.d.ts +0 -38
- package/dist/mcp/logging.js +0 -100
- package/dist/mcp/manager.d.ts +0 -68
- package/dist/mcp/manager.js +0 -176
- package/dist/mcp/neurolink-mcp-client.d.ts +0 -97
- package/dist/mcp/neurolink-mcp-client.js +0 -462
- package/dist/mcp/orchestrator.d.ts +0 -302
- package/dist/mcp/orchestrator.js +0 -703
- package/dist/mcp/plugin-manager.d.ts +0 -98
- package/dist/mcp/plugin-manager.js +0 -297
- package/dist/mcp/plugins/core/filesystem-mcp.d.ts +0 -36
- package/dist/mcp/plugins/core/filesystem-mcp.js +0 -142
- package/dist/mcp/plugins/core/neurolink-mcp.json +0 -17
- package/dist/mcp/plugins/filesystem-mcp.d.ts +0 -37
- package/dist/mcp/plugins/filesystem-mcp.js +0 -54
- package/dist/mcp/security-manager.d.ts +0 -87
- package/dist/mcp/security-manager.js +0 -344
- package/dist/mcp/semaphore-manager.d.ts +0 -137
- package/dist/mcp/semaphore-manager.js +0 -329
- package/dist/mcp/session-manager.d.ts +0 -187
- package/dist/mcp/session-manager.js +0 -400
- package/dist/mcp/session-persistence.d.ts +0 -93
- package/dist/mcp/session-persistence.js +0 -302
- package/dist/mcp/tool-integration.d.ts +0 -58
- package/dist/mcp/tool-integration.js +0 -203
- package/dist/mcp/transport-manager.d.ts +0 -154
- package/dist/mcp/transport-manager.js +0 -335
- package/dist/mcp/unified-mcp.d.ts +0 -133
- package/dist/mcp/unified-mcp.js +0 -251
- package/dist/mcp/unified-registry.d.ts +0 -165
- package/dist/mcp/unified-registry.js +0 -539
- package/dist/providers/analytics-helper.d.ts +0 -38
- package/dist/providers/analytics-helper.js +0 -216
- package/dist/providers/function-calling-provider.d.ts +0 -142
- package/dist/providers/function-calling-provider.js +0 -630
- package/dist/providers/mcp-provider.d.ts +0 -75
- package/dist/providers/mcp-provider.js +0 -283
- package/dist/providers/timeout-wrapper.d.ts +0 -40
- package/dist/providers/timeout-wrapper.js +0 -100
- package/dist/sdk/tool-extension.d.ts +0 -181
- package/dist/sdk/tool-extension.js +0 -284
- package/dist/services/streaming/streaming-manager.d.ts +0 -29
- package/dist/services/streaming/streaming-manager.js +0 -245
- package/dist/services/types.d.ts +0 -156
- package/dist/services/types.js +0 -2
- package/dist/services/websocket/websocket-server.d.ts +0 -34
- package/dist/services/websocket/websocket-server.js +0 -306
- package/dist/utils/provider-validation.d.ts +0 -36
- package/dist/utils/provider-validation.js +0 -625
- package/dist/utils/providerUtils-fixed.d.ts +0 -8
- package/dist/utils/providerUtils-fixed.js +0 -94
- package/dist/utils/streaming-utils.d.ts +0 -79
- package/dist/utils/streaming-utils.js +0 -198
- package/dist/utils/timeout-manager.d.ts +0 -75
- package/dist/utils/timeout-manager.js +0 -244
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Dynamic AI Tool Chain Executor
|
|
3
|
-
* Allows AI to dynamically decide tool execution sequences based on context and results
|
|
4
|
-
*/
|
|
5
|
-
import type { MCPOrchestrator } from "./orchestrator.js";
|
|
6
|
-
import type { MCPRegistry } from "./registry.js";
|
|
7
|
-
import type { NeuroLinkExecutionContext } from "./factory.js";
|
|
8
|
-
import type { JsonValue, UnknownRecord } from "../types/common.js";
|
|
9
|
-
import { ErrorManager } from "./error-manager.js";
|
|
10
|
-
/**
|
|
11
|
-
* Tool input schema structure
|
|
12
|
-
*/
|
|
13
|
-
export interface ToolInputSchema {
|
|
14
|
-
type?: string;
|
|
15
|
-
properties?: Record<string, unknown>;
|
|
16
|
-
required?: string[];
|
|
17
|
-
[key: string]: unknown;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Tool execution parameters
|
|
21
|
-
*/
|
|
22
|
-
export interface ToolExecutionParameters extends Record<string, JsonValue> {
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Context evolution entry tracking data changes
|
|
26
|
-
*/
|
|
27
|
-
export interface ContextEvolutionEntry {
|
|
28
|
-
step: string;
|
|
29
|
-
timestamp: number;
|
|
30
|
-
dataKeys: string[];
|
|
31
|
-
[key: string]: JsonValue;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* AI model planning response structure
|
|
35
|
-
*/
|
|
36
|
-
export interface AIPlanningResponse {
|
|
37
|
-
toolName: string | null;
|
|
38
|
-
parameters?: ToolExecutionParameters;
|
|
39
|
-
reasoning?: string;
|
|
40
|
-
confidence?: number;
|
|
41
|
-
expectedOutcome?: string;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* AI model evaluation response structure
|
|
45
|
-
*/
|
|
46
|
-
export interface AIEvaluationResponse {
|
|
47
|
-
goalAchieved: boolean;
|
|
48
|
-
confidence: number;
|
|
49
|
-
nextAction: "continue" | "retry" | "abort" | "complete";
|
|
50
|
-
reasoning: string;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Available tool descriptor
|
|
54
|
-
*/
|
|
55
|
-
export interface AvailableToolDescriptor {
|
|
56
|
-
name: string;
|
|
57
|
-
description: string;
|
|
58
|
-
inputSchema: ToolInputSchema;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Tool execution result with metadata
|
|
62
|
-
*/
|
|
63
|
-
export interface ToolExecutionResult {
|
|
64
|
-
toolName: string;
|
|
65
|
-
success: boolean;
|
|
66
|
-
result?: JsonValue;
|
|
67
|
-
error?: Error;
|
|
68
|
-
timestamp: number;
|
|
69
|
-
executionTime: number;
|
|
70
|
-
context?: UnknownRecord;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Chain execution step
|
|
74
|
-
*/
|
|
75
|
-
export interface ChainStep {
|
|
76
|
-
stepId: string;
|
|
77
|
-
toolName: string;
|
|
78
|
-
parameters: ToolExecutionParameters;
|
|
79
|
-
reasoning: string;
|
|
80
|
-
confidence: number;
|
|
81
|
-
expectedOutcome: string;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Chain execution context
|
|
85
|
-
*/
|
|
86
|
-
export interface ChainExecutionContext {
|
|
87
|
-
goal: string;
|
|
88
|
-
currentStep: number;
|
|
89
|
-
totalSteps?: number;
|
|
90
|
-
executionHistory: ToolExecutionResult[];
|
|
91
|
-
accumulatedContext: UnknownRecord;
|
|
92
|
-
userContext?: NeuroLinkExecutionContext;
|
|
93
|
-
maxSteps: number;
|
|
94
|
-
aiModel?: string;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Chain execution result
|
|
98
|
-
*/
|
|
99
|
-
export interface ChainExecutionResult {
|
|
100
|
-
success: boolean;
|
|
101
|
-
goal: string;
|
|
102
|
-
totalSteps: number;
|
|
103
|
-
executionTime: number;
|
|
104
|
-
results: ToolExecutionResult[];
|
|
105
|
-
finalResult?: JsonValue;
|
|
106
|
-
reasoning: string;
|
|
107
|
-
error?: Error;
|
|
108
|
-
metadata: {
|
|
109
|
-
toolsUsed: string[];
|
|
110
|
-
averageConfidence: number;
|
|
111
|
-
contextEvolution: ContextEvolutionEntry[];
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* AI Tool Chain Planner interface
|
|
116
|
-
*/
|
|
117
|
-
export interface AIChainPlanner {
|
|
118
|
-
name: string;
|
|
119
|
-
planNextStep(goal: string, availableTools: AvailableToolDescriptor[], executionHistory: ToolExecutionResult[], accumulatedContext: UnknownRecord): Promise<ChainStep | null>;
|
|
120
|
-
evaluateResult(step: ChainStep, result: ToolExecutionResult, goal: string): Promise<AIEvaluationResponse>;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Simple AI Chain Planner using heuristics
|
|
124
|
-
*/
|
|
125
|
-
export declare class HeuristicChainPlanner implements AIChainPlanner {
|
|
126
|
-
name: string;
|
|
127
|
-
planNextStep(goal: string, availableTools: AvailableToolDescriptor[], executionHistory: ToolExecutionResult[], accumulatedContext: UnknownRecord): Promise<ChainStep | null>;
|
|
128
|
-
evaluateResult(step: ChainStep, result: ToolExecutionResult, goal: string): Promise<AIEvaluationResponse>;
|
|
129
|
-
private generateParameters;
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Advanced AI Chain Planner using actual AI model
|
|
133
|
-
*/
|
|
134
|
-
export declare class AIModelChainPlanner implements AIChainPlanner {
|
|
135
|
-
name: string;
|
|
136
|
-
private aiModel;
|
|
137
|
-
constructor(aiModel?: string);
|
|
138
|
-
planNextStep(goal: string, availableTools: AvailableToolDescriptor[], executionHistory: ToolExecutionResult[], accumulatedContext: UnknownRecord): Promise<ChainStep | null>;
|
|
139
|
-
evaluateResult(step: ChainStep, result: ToolExecutionResult, goal: string): Promise<AIEvaluationResponse>;
|
|
140
|
-
private buildPlanningPrompt;
|
|
141
|
-
private buildEvaluationPrompt;
|
|
142
|
-
private callAIModel;
|
|
143
|
-
private parseAIResponse;
|
|
144
|
-
private parseEvaluationResponse;
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Dynamic Chain Executor
|
|
148
|
-
*/
|
|
149
|
-
export declare class DynamicChainExecutor {
|
|
150
|
-
private orchestrator;
|
|
151
|
-
private registry;
|
|
152
|
-
private errorManager;
|
|
153
|
-
private planner;
|
|
154
|
-
constructor(orchestrator: MCPOrchestrator, registry: MCPRegistry, errorManager: ErrorManager, planner?: AIChainPlanner);
|
|
155
|
-
/**
|
|
156
|
-
* Execute dynamic tool chain to achieve a goal
|
|
157
|
-
*
|
|
158
|
-
* @param goal The goal to achieve
|
|
159
|
-
* @param initialContext Initial context data
|
|
160
|
-
* @param userContext User execution context
|
|
161
|
-
* @param options Execution options
|
|
162
|
-
* @returns Chain execution result
|
|
163
|
-
*/
|
|
164
|
-
executeChain(goal: string, initialContext?: UnknownRecord, userContext?: NeuroLinkExecutionContext, options?: {
|
|
165
|
-
maxSteps?: number;
|
|
166
|
-
aiModel?: string;
|
|
167
|
-
timeout?: number;
|
|
168
|
-
}): Promise<ChainExecutionResult>;
|
|
169
|
-
/**
|
|
170
|
-
* Execute chain steps iteratively
|
|
171
|
-
*
|
|
172
|
-
* @private
|
|
173
|
-
*/
|
|
174
|
-
private executeChainSteps;
|
|
175
|
-
/**
|
|
176
|
-
* Execute a single step
|
|
177
|
-
*
|
|
178
|
-
* @private
|
|
179
|
-
*/
|
|
180
|
-
private executeStep;
|
|
181
|
-
/**
|
|
182
|
-
* Update accumulated context with new result
|
|
183
|
-
*
|
|
184
|
-
* @private
|
|
185
|
-
*/
|
|
186
|
-
private updateAccumulatedContext;
|
|
187
|
-
/**
|
|
188
|
-
* Calculate average confidence across execution history
|
|
189
|
-
*
|
|
190
|
-
* @private
|
|
191
|
-
*/
|
|
192
|
-
private calculateAverageConfidence;
|
|
193
|
-
/**
|
|
194
|
-
* Track context evolution through execution
|
|
195
|
-
*
|
|
196
|
-
* @private
|
|
197
|
-
*/
|
|
198
|
-
private trackContextEvolution;
|
|
199
|
-
/**
|
|
200
|
-
* Set AI planner
|
|
201
|
-
*
|
|
202
|
-
* @param planner AI chain planner instance
|
|
203
|
-
*/
|
|
204
|
-
setPlanner(planner: AIChainPlanner): void;
|
|
205
|
-
/**
|
|
206
|
-
* Get current planner
|
|
207
|
-
*
|
|
208
|
-
* @returns Current AI chain planner
|
|
209
|
-
*/
|
|
210
|
-
getPlanner(): AIChainPlanner;
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Default dynamic chain executor instance
|
|
214
|
-
*/
|
|
215
|
-
export declare let defaultDynamicChainExecutor: DynamicChainExecutor | null;
|
|
216
|
-
/**
|
|
217
|
-
* Initialize default dynamic chain executor
|
|
218
|
-
*
|
|
219
|
-
* @param orchestrator MCP orchestrator
|
|
220
|
-
* @param registry Tool registry
|
|
221
|
-
* @param errorManager Error manager
|
|
222
|
-
* @param planner Optional AI planner
|
|
223
|
-
* @returns Dynamic chain executor instance
|
|
224
|
-
*/
|
|
225
|
-
export declare function initializeDynamicChainExecutor(orchestrator: MCPOrchestrator, registry: MCPRegistry, errorManager: ErrorManager, planner?: AIChainPlanner): DynamicChainExecutor;
|
|
@@ -1,489 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Dynamic AI Tool Chain Executor
|
|
3
|
-
* Allows AI to dynamically decide tool execution sequences based on context and results
|
|
4
|
-
*/
|
|
5
|
-
import { ErrorManager, ErrorCategory, ErrorSeverity } from "./error-manager.js";
|
|
6
|
-
/**
|
|
7
|
-
* Simple AI Chain Planner using heuristics
|
|
8
|
-
*/
|
|
9
|
-
export class HeuristicChainPlanner {
|
|
10
|
-
name = "heuristic";
|
|
11
|
-
async planNextStep(goal, availableTools, executionHistory, accumulatedContext) {
|
|
12
|
-
// Simple heuristic-based planning
|
|
13
|
-
const usedTools = new Set(executionHistory.map((h) => h.toolName));
|
|
14
|
-
const availableUnused = availableTools.filter((t) => !usedTools.has(t.name));
|
|
15
|
-
if (availableUnused.length === 0) {
|
|
16
|
-
return null; // No more tools to try
|
|
17
|
-
}
|
|
18
|
-
// Select tool based on goal keywords and context
|
|
19
|
-
const goalLower = goal.toLowerCase();
|
|
20
|
-
let selectedTool = availableUnused[0];
|
|
21
|
-
let confidence = 0.3;
|
|
22
|
-
// Keyword-based tool selection
|
|
23
|
-
for (const tool of availableUnused) {
|
|
24
|
-
const toolDesc = tool.description.toLowerCase();
|
|
25
|
-
let toolConfidence = 0.3;
|
|
26
|
-
// Check for keyword matches
|
|
27
|
-
if (goalLower.includes("fetch") && toolDesc.includes("fetch")) {
|
|
28
|
-
toolConfidence += 0.4;
|
|
29
|
-
}
|
|
30
|
-
if (goalLower.includes("process") && toolDesc.includes("process")) {
|
|
31
|
-
toolConfidence += 0.4;
|
|
32
|
-
}
|
|
33
|
-
if (goalLower.includes("analyze") && toolDesc.includes("analy")) {
|
|
34
|
-
toolConfidence += 0.4;
|
|
35
|
-
}
|
|
36
|
-
if (goalLower.includes("data") && toolDesc.includes("data")) {
|
|
37
|
-
toolConfidence += 0.3;
|
|
38
|
-
}
|
|
39
|
-
if (toolConfidence > confidence) {
|
|
40
|
-
confidence = toolConfidence;
|
|
41
|
-
selectedTool = tool;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
// Generate parameters based on context and previous results
|
|
45
|
-
const parameters = this.generateParameters(selectedTool, accumulatedContext, executionHistory);
|
|
46
|
-
return {
|
|
47
|
-
stepId: `step-${Date.now()}`,
|
|
48
|
-
toolName: selectedTool.name,
|
|
49
|
-
parameters,
|
|
50
|
-
reasoning: `Selected ${selectedTool.name} based on goal keywords and available context`,
|
|
51
|
-
confidence: Math.min(confidence, 0.8),
|
|
52
|
-
expectedOutcome: `Execute ${selectedTool.name} to progress towards: ${goal}`,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
async evaluateResult(step, result, goal) {
|
|
56
|
-
if (!result.success) {
|
|
57
|
-
return {
|
|
58
|
-
goalAchieved: false,
|
|
59
|
-
confidence: 0.1,
|
|
60
|
-
nextAction: "retry",
|
|
61
|
-
reasoning: `Tool ${step.toolName} failed: ${result.error?.message}"`,
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
// Simple goal completion heuristic
|
|
65
|
-
const goalKeywords = goal.toLowerCase().split(" ");
|
|
66
|
-
const resultString = JSON.stringify(result.result || {}).toLowerCase();
|
|
67
|
-
const matchedKeywords = goalKeywords.filter((keyword) => resultString.includes(keyword) || keyword.length < 3);
|
|
68
|
-
const completionRatio = matchedKeywords.length / goalKeywords.length;
|
|
69
|
-
const goalAchieved = completionRatio > 0.6;
|
|
70
|
-
return {
|
|
71
|
-
goalAchieved,
|
|
72
|
-
confidence: completionRatio,
|
|
73
|
-
nextAction: goalAchieved ? "complete" : "continue",
|
|
74
|
-
reasoning: goalAchieved
|
|
75
|
-
? `Goal appears achieved based on result content`
|
|
76
|
-
: `Goal not yet achieved (${Math.round(completionRatio * 100)}% match), continuing`,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
generateParameters(tool, context, history) {
|
|
80
|
-
const params = {};
|
|
81
|
-
// Extract useful data from previous results
|
|
82
|
-
const lastResult = history[history.length - 1];
|
|
83
|
-
if (lastResult?.result) {
|
|
84
|
-
// Pass relevant data from previous step
|
|
85
|
-
if (typeof lastResult.result === "object" && lastResult.result !== null) {
|
|
86
|
-
Object.assign(params, lastResult.result);
|
|
87
|
-
}
|
|
88
|
-
else if (typeof lastResult.result === "string") {
|
|
89
|
-
params.input = lastResult.result;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
// Add context data
|
|
93
|
-
Object.assign(params, context);
|
|
94
|
-
return params;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Advanced AI Chain Planner using actual AI model
|
|
99
|
-
*/
|
|
100
|
-
export class AIModelChainPlanner {
|
|
101
|
-
name = "ai-model";
|
|
102
|
-
aiModel;
|
|
103
|
-
constructor(aiModel = "gpt-4") {
|
|
104
|
-
this.aiModel = aiModel;
|
|
105
|
-
}
|
|
106
|
-
async planNextStep(goal, availableTools, executionHistory, accumulatedContext) {
|
|
107
|
-
const prompt = this.buildPlanningPrompt(goal, availableTools, executionHistory, accumulatedContext);
|
|
108
|
-
try {
|
|
109
|
-
// This would integrate with actual AI service
|
|
110
|
-
// For now, return a structured response format
|
|
111
|
-
const aiResponse = await this.callAIModel(prompt);
|
|
112
|
-
return this.parseAIResponse(aiResponse);
|
|
113
|
-
}
|
|
114
|
-
catch (error) {
|
|
115
|
-
// Fallback to heuristic planning
|
|
116
|
-
console.warn("AI planning failed, falling back to heuristic", error);
|
|
117
|
-
const fallback = new HeuristicChainPlanner();
|
|
118
|
-
return fallback.planNextStep(goal, availableTools, executionHistory, accumulatedContext);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
async evaluateResult(step, result, goal) {
|
|
122
|
-
const prompt = this.buildEvaluationPrompt(step, result, goal);
|
|
123
|
-
try {
|
|
124
|
-
const aiResponse = await this.callAIModel(prompt);
|
|
125
|
-
return this.parseEvaluationResponse(aiResponse);
|
|
126
|
-
}
|
|
127
|
-
catch (error) {
|
|
128
|
-
// Fallback to heuristic evaluation
|
|
129
|
-
const fallback = new HeuristicChainPlanner();
|
|
130
|
-
return fallback.evaluateResult(step, result, goal);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
buildPlanningPrompt(goal, availableTools, executionHistory, accumulatedContext) {
|
|
134
|
-
return `
|
|
135
|
-
You are an AI tool chain planner. Your job is to select the next tool to execute towards achieving a goal.
|
|
136
|
-
|
|
137
|
-
GOAL: ${goal}
|
|
138
|
-
|
|
139
|
-
AVAILABLE TOOLS:
|
|
140
|
-
${availableTools.map((t) => `- ${t.name}: ${t.description}`).join("\n")}
|
|
141
|
-
|
|
142
|
-
EXECUTION HISTORY:
|
|
143
|
-
${executionHistory.map((h) => `${h.toolName}: ${h.success ? "SUCCESS" : "FAILED"} - ${JSON.stringify(h.result || h.error?.message)}`).join("\n")}
|
|
144
|
-
|
|
145
|
-
ACCUMULATED CONTEXT:
|
|
146
|
-
${JSON.stringify(accumulatedContext, null, 2)}
|
|
147
|
-
|
|
148
|
-
Select the next tool to execute, or return null if the goal is achieved or no progress can be made.
|
|
149
|
-
|
|
150
|
-
Respond in JSON format:
|
|
151
|
-
{
|
|
152
|
-
"toolName": "tool-name",
|
|
153
|
-
"parameters": { /* tool parameters */ },
|
|
154
|
-
"reasoning": "why this tool was selected",
|
|
155
|
-
"confidence": 0.8,
|
|
156
|
-
"expectedOutcome": "what this step should achieve"
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
If no tool should be executed, return: {"toolName": null}
|
|
160
|
-
`;
|
|
161
|
-
}
|
|
162
|
-
buildEvaluationPrompt(step, result, goal) {
|
|
163
|
-
return `
|
|
164
|
-
Evaluate whether the goal has been achieved after executing a tool.
|
|
165
|
-
|
|
166
|
-
GOAL: ${goal}
|
|
167
|
-
|
|
168
|
-
EXECUTED STEP:
|
|
169
|
-
Tool: ${step.toolName}
|
|
170
|
-
Reasoning: ${step.reasoning}
|
|
171
|
-
Expected: ${step.expectedOutcome}
|
|
172
|
-
|
|
173
|
-
ACTUAL RESULT:
|
|
174
|
-
Success: ${result.success}
|
|
175
|
-
Result: ${JSON.stringify(result.result || result.error?.message)}
|
|
176
|
-
|
|
177
|
-
Respond in JSON format:
|
|
178
|
-
{
|
|
179
|
-
"goalAchieved": true/false,
|
|
180
|
-
"confidence": 0.8,
|
|
181
|
-
"nextAction": "continue"|"retry"|"abort"|"complete",
|
|
182
|
-
"reasoning": "explanation of evaluation"
|
|
183
|
-
}
|
|
184
|
-
`;
|
|
185
|
-
}
|
|
186
|
-
async callAIModel(prompt) {
|
|
187
|
-
// Mock AI response for now
|
|
188
|
-
// In real implementation, this would call the actual AI service
|
|
189
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
190
|
-
// Return mock structured response
|
|
191
|
-
if (prompt.includes("Select the next tool")) {
|
|
192
|
-
return JSON.stringify({
|
|
193
|
-
toolName: "mock-tool",
|
|
194
|
-
parameters: { input: "mock" },
|
|
195
|
-
reasoning: "Mock AI selection",
|
|
196
|
-
confidence: 0.7,
|
|
197
|
-
expectedOutcome: "Mock execution",
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
return JSON.stringify({
|
|
202
|
-
goalAchieved: false,
|
|
203
|
-
confidence: 0.6,
|
|
204
|
-
nextAction: "continue",
|
|
205
|
-
reasoning: "Mock AI evaluation",
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
parseAIResponse(response) {
|
|
210
|
-
try {
|
|
211
|
-
const parsed = JSON.parse(response);
|
|
212
|
-
if (!parsed.toolName) {
|
|
213
|
-
return null;
|
|
214
|
-
}
|
|
215
|
-
return {
|
|
216
|
-
stepId: `ai-step-${Date.now()}`,
|
|
217
|
-
toolName: parsed.toolName,
|
|
218
|
-
parameters: parsed.parameters || {},
|
|
219
|
-
reasoning: parsed.reasoning || "AI selected tool",
|
|
220
|
-
confidence: parsed.confidence || 0.5,
|
|
221
|
-
expectedOutcome: parsed.expectedOutcome || "Execute tool",
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
catch (error) {
|
|
225
|
-
console.warn("Failed to parse AI response", error);
|
|
226
|
-
return null;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
parseEvaluationResponse(response) {
|
|
230
|
-
try {
|
|
231
|
-
const parsed = JSON.parse(response);
|
|
232
|
-
return {
|
|
233
|
-
goalAchieved: parsed.goalAchieved || false,
|
|
234
|
-
confidence: parsed.confidence || 0.5,
|
|
235
|
-
nextAction: parsed.nextAction || "continue",
|
|
236
|
-
reasoning: parsed.reasoning || "AI evaluation",
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
catch (error) {
|
|
240
|
-
return {
|
|
241
|
-
goalAchieved: false,
|
|
242
|
-
confidence: 0.3,
|
|
243
|
-
nextAction: "continue",
|
|
244
|
-
reasoning: "Failed to parse AI evaluation",
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Dynamic Chain Executor
|
|
251
|
-
*/
|
|
252
|
-
export class DynamicChainExecutor {
|
|
253
|
-
orchestrator;
|
|
254
|
-
registry;
|
|
255
|
-
errorManager;
|
|
256
|
-
planner;
|
|
257
|
-
constructor(orchestrator, registry, errorManager, planner) {
|
|
258
|
-
this.orchestrator = orchestrator;
|
|
259
|
-
this.registry = registry;
|
|
260
|
-
this.errorManager = errorManager;
|
|
261
|
-
this.planner = planner || new HeuristicChainPlanner();
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Execute dynamic tool chain to achieve a goal
|
|
265
|
-
*
|
|
266
|
-
* @param goal The goal to achieve
|
|
267
|
-
* @param initialContext Initial context data
|
|
268
|
-
* @param userContext User execution context
|
|
269
|
-
* @param options Execution options
|
|
270
|
-
* @returns Chain execution result
|
|
271
|
-
*/
|
|
272
|
-
async executeChain(goal, initialContext = {}, userContext, options = {}) {
|
|
273
|
-
const startTime = Date.now();
|
|
274
|
-
const maxSteps = options.maxSteps || 10;
|
|
275
|
-
const executionContext = {
|
|
276
|
-
goal,
|
|
277
|
-
currentStep: 0,
|
|
278
|
-
executionHistory: [],
|
|
279
|
-
accumulatedContext: { ...initialContext },
|
|
280
|
-
userContext,
|
|
281
|
-
maxSteps,
|
|
282
|
-
aiModel: options.aiModel,
|
|
283
|
-
};
|
|
284
|
-
try {
|
|
285
|
-
const result = await this.executeChainSteps(executionContext);
|
|
286
|
-
return {
|
|
287
|
-
success: true,
|
|
288
|
-
goal,
|
|
289
|
-
totalSteps: result.executionHistory.length,
|
|
290
|
-
executionTime: Date.now() - startTime,
|
|
291
|
-
results: result.executionHistory,
|
|
292
|
-
finalResult: result.executionHistory[result.executionHistory.length - 1]?.result,
|
|
293
|
-
reasoning: "Chain execution completed",
|
|
294
|
-
metadata: {
|
|
295
|
-
toolsUsed: [
|
|
296
|
-
...new Set(result.executionHistory.map((h) => h.toolName)),
|
|
297
|
-
],
|
|
298
|
-
averageConfidence: this.calculateAverageConfidence(result.executionHistory),
|
|
299
|
-
contextEvolution: this.trackContextEvolution(result.executionHistory),
|
|
300
|
-
},
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
catch (error) {
|
|
304
|
-
await this.errorManager.recordError(error, {
|
|
305
|
-
category: ErrorCategory.TOOL_ERROR,
|
|
306
|
-
severity: ErrorSeverity.HIGH,
|
|
307
|
-
toolName: "dynamic-chain-executor",
|
|
308
|
-
executionContext: userContext,
|
|
309
|
-
});
|
|
310
|
-
return {
|
|
311
|
-
success: false,
|
|
312
|
-
goal,
|
|
313
|
-
totalSteps: executionContext.executionHistory.length,
|
|
314
|
-
executionTime: Date.now() - startTime,
|
|
315
|
-
results: executionContext.executionHistory,
|
|
316
|
-
error: error instanceof Error ? error : new Error(String(error)),
|
|
317
|
-
reasoning: `Chain execution failed: ${error}`,
|
|
318
|
-
metadata: {
|
|
319
|
-
toolsUsed: [
|
|
320
|
-
...new Set(executionContext.executionHistory.map((h) => h.toolName)),
|
|
321
|
-
],
|
|
322
|
-
averageConfidence: this.calculateAverageConfidence(executionContext.executionHistory),
|
|
323
|
-
contextEvolution: this.trackContextEvolution(executionContext.executionHistory),
|
|
324
|
-
},
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Execute chain steps iteratively
|
|
330
|
-
*
|
|
331
|
-
* @private
|
|
332
|
-
*/
|
|
333
|
-
async executeChainSteps(context) {
|
|
334
|
-
const availableTools = await this.registry.listTools();
|
|
335
|
-
while (context.currentStep < context.maxSteps) {
|
|
336
|
-
// Plan next step
|
|
337
|
-
const nextStep = await this.planner.planNextStep(context.goal, availableTools.map((tool) => ({
|
|
338
|
-
name: tool.name,
|
|
339
|
-
description: tool.description || "No description available",
|
|
340
|
-
inputSchema: tool.inputSchema || {},
|
|
341
|
-
})), context.executionHistory, context.accumulatedContext);
|
|
342
|
-
if (!nextStep) {
|
|
343
|
-
// No more steps to execute
|
|
344
|
-
break;
|
|
345
|
-
}
|
|
346
|
-
// Execute the planned step
|
|
347
|
-
const stepResult = await this.executeStep(nextStep, context);
|
|
348
|
-
context.executionHistory.push(stepResult);
|
|
349
|
-
// Update accumulated context
|
|
350
|
-
if (stepResult.success && stepResult.result) {
|
|
351
|
-
this.updateAccumulatedContext(context.accumulatedContext, stepResult.result);
|
|
352
|
-
}
|
|
353
|
-
// Evaluate if goal is achieved
|
|
354
|
-
const evaluation = await this.planner.evaluateResult(nextStep, stepResult, context.goal);
|
|
355
|
-
if (evaluation.goalAchieved || evaluation.nextAction === "complete") {
|
|
356
|
-
break;
|
|
357
|
-
}
|
|
358
|
-
else if (evaluation.nextAction === "abort") {
|
|
359
|
-
throw new Error(`Chain execution aborted: ${evaluation.reasoning}`);
|
|
360
|
-
}
|
|
361
|
-
else if (evaluation.nextAction === "retry" && context.currentStep > 0) {
|
|
362
|
-
// Retry logic could be implemented here
|
|
363
|
-
console.warn(`Step retry suggested: ${evaluation.reasoning}`);
|
|
364
|
-
}
|
|
365
|
-
context.currentStep++;
|
|
366
|
-
}
|
|
367
|
-
return context;
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Execute a single step
|
|
371
|
-
*
|
|
372
|
-
* @private
|
|
373
|
-
*/
|
|
374
|
-
async executeStep(step, context) {
|
|
375
|
-
const startTime = Date.now();
|
|
376
|
-
try {
|
|
377
|
-
const result = await this.orchestrator.executeTool(step.toolName, step.parameters, context.userContext);
|
|
378
|
-
return {
|
|
379
|
-
toolName: step.toolName,
|
|
380
|
-
success: true,
|
|
381
|
-
result: result,
|
|
382
|
-
timestamp: Date.now(),
|
|
383
|
-
executionTime: Date.now() - startTime,
|
|
384
|
-
context: {
|
|
385
|
-
stepId: step.stepId,
|
|
386
|
-
reasoning: step.reasoning,
|
|
387
|
-
confidence: step.confidence,
|
|
388
|
-
},
|
|
389
|
-
};
|
|
390
|
-
}
|
|
391
|
-
catch (error) {
|
|
392
|
-
return {
|
|
393
|
-
toolName: step.toolName,
|
|
394
|
-
success: false,
|
|
395
|
-
error: error instanceof Error ? error : new Error(String(error)),
|
|
396
|
-
timestamp: Date.now(),
|
|
397
|
-
executionTime: Date.now() - startTime,
|
|
398
|
-
context: {
|
|
399
|
-
stepId: step.stepId,
|
|
400
|
-
reasoning: step.reasoning,
|
|
401
|
-
confidence: step.confidence,
|
|
402
|
-
},
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
/**
|
|
407
|
-
* Update accumulated context with new result
|
|
408
|
-
*
|
|
409
|
-
* @private
|
|
410
|
-
*/
|
|
411
|
-
updateAccumulatedContext(context, result) {
|
|
412
|
-
if (typeof result === "object" && result !== null) {
|
|
413
|
-
Object.assign(context, result);
|
|
414
|
-
}
|
|
415
|
-
else {
|
|
416
|
-
context.lastResult = result;
|
|
417
|
-
}
|
|
418
|
-
context.lastUpdated = Date.now();
|
|
419
|
-
}
|
|
420
|
-
/**
|
|
421
|
-
* Calculate average confidence across execution history
|
|
422
|
-
*
|
|
423
|
-
* @private
|
|
424
|
-
*/
|
|
425
|
-
calculateAverageConfidence(history) {
|
|
426
|
-
if (history.length === 0) {
|
|
427
|
-
return 0;
|
|
428
|
-
}
|
|
429
|
-
const confidences = history
|
|
430
|
-
.map((h) => h.context?.confidence || 0.5)
|
|
431
|
-
.filter((c) => typeof c === "number");
|
|
432
|
-
return confidences.length > 0
|
|
433
|
-
? confidences.reduce((sum, c) => sum + c, 0) / confidences.length
|
|
434
|
-
: 0.5;
|
|
435
|
-
}
|
|
436
|
-
/**
|
|
437
|
-
* Track context evolution through execution
|
|
438
|
-
*
|
|
439
|
-
* @private
|
|
440
|
-
*/
|
|
441
|
-
trackContextEvolution(history) {
|
|
442
|
-
const evolution = [];
|
|
443
|
-
history.forEach((result) => {
|
|
444
|
-
if (result.success && result.result) {
|
|
445
|
-
evolution.push({
|
|
446
|
-
step: result.toolName,
|
|
447
|
-
timestamp: result.timestamp,
|
|
448
|
-
dataKeys: typeof result.result === "object" && result.result !== null
|
|
449
|
-
? Object.keys(result.result)
|
|
450
|
-
: ["primitive"],
|
|
451
|
-
});
|
|
452
|
-
}
|
|
453
|
-
});
|
|
454
|
-
return evolution;
|
|
455
|
-
}
|
|
456
|
-
/**
|
|
457
|
-
* Set AI planner
|
|
458
|
-
*
|
|
459
|
-
* @param planner AI chain planner instance
|
|
460
|
-
*/
|
|
461
|
-
setPlanner(planner) {
|
|
462
|
-
this.planner = planner;
|
|
463
|
-
}
|
|
464
|
-
/**
|
|
465
|
-
* Get current planner
|
|
466
|
-
*
|
|
467
|
-
* @returns Current AI chain planner
|
|
468
|
-
*/
|
|
469
|
-
getPlanner() {
|
|
470
|
-
return this.planner;
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
/**
|
|
474
|
-
* Default dynamic chain executor instance
|
|
475
|
-
*/
|
|
476
|
-
export let defaultDynamicChainExecutor = null;
|
|
477
|
-
/**
|
|
478
|
-
* Initialize default dynamic chain executor
|
|
479
|
-
*
|
|
480
|
-
* @param orchestrator MCP orchestrator
|
|
481
|
-
* @param registry Tool registry
|
|
482
|
-
* @param errorManager Error manager
|
|
483
|
-
* @param planner Optional AI planner
|
|
484
|
-
* @returns Dynamic chain executor instance
|
|
485
|
-
*/
|
|
486
|
-
export function initializeDynamicChainExecutor(orchestrator, registry, errorManager, planner) {
|
|
487
|
-
defaultDynamicChainExecutor = new DynamicChainExecutor(orchestrator, registry, errorManager, planner);
|
|
488
|
-
return defaultDynamicChainExecutor;
|
|
489
|
-
}
|