@juspay/neurolink 9.54.6 → 9.54.8
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 +8 -0
- package/dist/action/actionInputs.d.ts +1 -1
- package/dist/adapters/video/directorPipeline.js +6 -0
- package/dist/adapters/video/vertexVideoHandler.js +6 -0
- package/dist/agent/directTools.d.ts +3 -23
- package/dist/auth/AuthProviderFactory.d.ts +1 -3
- package/dist/auth/anthropicOAuth.d.ts +4 -7
- package/dist/auth/anthropicOAuth.js +23 -0
- package/dist/auth/errors.d.ts +1 -1
- package/dist/auth/index.d.ts +11 -0
- package/dist/auth/index.js +14 -0
- package/dist/auth/middleware/AuthMiddleware.d.ts +5 -60
- package/dist/auth/middleware/AuthMiddleware.js +3 -0
- package/dist/auth/middleware/rateLimitByUser.d.ts +4 -93
- package/dist/auth/middleware/rateLimitByUser.js +4 -0
- package/dist/auth/providers/BaseAuthProvider.d.ts +1 -1
- package/dist/auth/providers/CognitoProvider.js +3 -0
- package/dist/auth/providers/KeycloakProvider.js +3 -0
- package/dist/auth/providers/auth0.d.ts +1 -1
- package/dist/auth/sessionManager.d.ts +2 -0
- package/dist/auth/sessionManager.js +53 -11
- package/dist/auth/tokenStore.d.ts +2 -0
- package/dist/auth/tokenStore.js +45 -4
- package/dist/autoresearch/tools.d.ts +1 -16
- package/dist/browser/neurolink.min.js +358 -358
- package/dist/cli/commands/config.d.ts +3 -123
- package/dist/cli/commands/config.js +4 -2
- package/dist/cli/commands/evaluate.d.ts +1 -19
- package/dist/cli/commands/proxy.d.ts +1 -1
- package/dist/cli/commands/proxy.js +3 -0
- package/dist/cli/commands/rag.js +3 -0
- package/dist/cli/commands/setup-anthropic.d.ts +2 -6
- package/dist/cli/commands/setup-anthropic.js +1 -1
- package/dist/cli/commands/setup-azure.d.ts +2 -6
- package/dist/cli/commands/setup-azure.js +1 -1
- package/dist/cli/commands/setup-bedrock.d.ts +2 -6
- package/dist/cli/commands/setup-bedrock.js +1 -1
- package/dist/cli/commands/setup-gcp.d.ts +2 -6
- package/dist/cli/commands/setup-google-ai.d.ts +2 -6
- package/dist/cli/commands/setup-google-ai.js +1 -1
- package/dist/cli/commands/setup-huggingface.d.ts +1 -5
- package/dist/cli/commands/setup-mistral.d.ts +1 -5
- package/dist/cli/commands/setup-openai.d.ts +2 -6
- package/dist/cli/commands/setup-openai.js +1 -1
- package/dist/cli/commands/setup.d.ts +1 -8
- package/dist/cli/commands/task.js +1 -0
- package/dist/cli/commands/voiceServer.d.ts +1 -4
- package/dist/cli/loop/session.js +31 -10
- package/dist/cli/utils/interactiveSetup.d.ts +2 -15
- package/dist/cli/utils/videoFileUtils.d.ts +1 -15
- package/dist/client/aiSdkAdapter.d.ts +1 -1
- package/dist/client/aiSdkAdapter.js +1 -0
- package/dist/client/httpClient.d.ts +1 -0
- package/dist/client/httpClient.js +13 -0
- package/dist/client/sseClient.d.ts +1 -0
- package/dist/client/sseClient.js +29 -0
- package/dist/client/streamingClient.d.ts +2 -0
- package/dist/client/streamingClient.js +19 -0
- package/dist/client/wsClient.d.ts +6 -0
- package/dist/client/wsClient.js +90 -10
- package/dist/context/budgetChecker.js +3 -1
- package/dist/context/contextCompactor.js +163 -143
- package/dist/context/fileSummarizationService.d.ts +1 -9
- package/dist/context/summarizationEngine.js +29 -16
- package/dist/core/baseProvider.js +124 -153
- package/dist/core/infrastructure/baseRegistry.d.ts +1 -7
- package/dist/core/modules/GenerationHandler.d.ts +3 -2
- package/dist/core/modules/GenerationHandler.js +9 -1
- package/dist/core/modules/StreamHandler.js +9 -0
- package/dist/core/modules/ToolsManager.js +18 -2
- package/dist/evaluation/BatchEvaluator.d.ts +1 -97
- package/dist/evaluation/EvaluationAggregator.d.ts +1 -118
- package/dist/evaluation/EvaluatorFactory.d.ts +1 -13
- package/dist/evaluation/EvaluatorRegistry.d.ts +1 -50
- package/dist/evaluation/errors/EvaluationError.d.ts +2 -27
- package/dist/evaluation/hooks/langfuseAdapter.d.ts +1 -39
- package/dist/evaluation/hooks/observabilityHooks.d.ts +3 -55
- package/dist/evaluation/hooks/observabilityHooks.js +3 -0
- package/dist/evaluation/pipeline/strategies/batchStrategy.d.ts +7 -61
- package/dist/evaluation/pipeline/strategies/batchStrategy.js +7 -7
- package/dist/evaluation/ragasEvaluator.js +54 -37
- package/dist/evaluation/reporting/metricsCollector.d.ts +1 -60
- package/dist/evaluation/reporting/reportGenerator.d.ts +1 -17
- package/dist/evaluation/scorers/rule/contentSimilarityScorer.d.ts +1 -29
- package/dist/evaluation/scorers/rule/formatScorer.d.ts +1 -42
- package/dist/evaluation/scorers/rule/keywordCoverageScorer.d.ts +1 -19
- package/dist/evaluation/scorers/rule/lengthScorer.d.ts +1 -33
- package/dist/factories/providerFactory.d.ts +1 -16
- package/dist/factories/providerFactory.js +2 -0
- package/dist/image-gen/ImageGenService.d.ts +3 -0
- package/dist/image-gen/ImageGenService.js +3 -0
- package/dist/lib/action/actionInputs.d.ts +1 -1
- package/dist/lib/adapters/video/directorPipeline.js +6 -0
- package/dist/lib/adapters/video/vertexVideoHandler.js +6 -0
- package/dist/lib/agent/directTools.d.ts +3 -23
- package/dist/lib/auth/AuthProviderFactory.d.ts +1 -3
- package/dist/lib/auth/anthropicOAuth.d.ts +4 -7
- package/dist/lib/auth/anthropicOAuth.js +23 -0
- package/dist/lib/auth/errors.d.ts +1 -1
- package/dist/lib/auth/index.d.ts +11 -0
- package/dist/lib/auth/index.js +14 -0
- package/dist/lib/auth/middleware/AuthMiddleware.d.ts +5 -60
- package/dist/lib/auth/middleware/AuthMiddleware.js +3 -0
- package/dist/lib/auth/middleware/rateLimitByUser.d.ts +4 -93
- package/dist/lib/auth/middleware/rateLimitByUser.js +4 -0
- package/dist/lib/auth/providers/BaseAuthProvider.d.ts +1 -1
- package/dist/lib/auth/providers/CognitoProvider.js +3 -0
- package/dist/lib/auth/providers/KeycloakProvider.js +3 -0
- package/dist/lib/auth/providers/auth0.d.ts +1 -1
- package/dist/lib/auth/sessionManager.d.ts +2 -0
- package/dist/lib/auth/sessionManager.js +53 -11
- package/dist/lib/auth/tokenStore.d.ts +2 -0
- package/dist/lib/auth/tokenStore.js +45 -4
- package/dist/lib/autoresearch/tools.d.ts +1 -16
- package/dist/lib/client/aiSdkAdapter.d.ts +1 -1
- package/dist/lib/client/aiSdkAdapter.js +1 -0
- package/dist/lib/client/httpClient.d.ts +1 -0
- package/dist/lib/client/httpClient.js +13 -0
- package/dist/lib/client/sseClient.d.ts +1 -0
- package/dist/lib/client/sseClient.js +29 -0
- package/dist/lib/client/streamingClient.d.ts +2 -0
- package/dist/lib/client/streamingClient.js +19 -0
- package/dist/lib/client/wsClient.d.ts +6 -0
- package/dist/lib/client/wsClient.js +90 -10
- package/dist/lib/context/budgetChecker.js +3 -1
- package/dist/lib/context/contextCompactor.js +163 -143
- package/dist/lib/context/fileSummarizationService.d.ts +1 -9
- package/dist/lib/context/summarizationEngine.js +29 -16
- package/dist/lib/core/baseProvider.js +124 -153
- package/dist/lib/core/infrastructure/baseRegistry.d.ts +1 -7
- package/dist/lib/core/modules/GenerationHandler.d.ts +3 -2
- package/dist/lib/core/modules/GenerationHandler.js +9 -1
- package/dist/lib/core/modules/StreamHandler.js +9 -0
- package/dist/lib/core/modules/ToolsManager.js +18 -2
- package/dist/lib/evaluation/BatchEvaluator.d.ts +1 -97
- package/dist/lib/evaluation/EvaluationAggregator.d.ts +1 -118
- package/dist/lib/evaluation/EvaluatorFactory.d.ts +1 -13
- package/dist/lib/evaluation/EvaluatorRegistry.d.ts +1 -50
- package/dist/lib/evaluation/errors/EvaluationError.d.ts +2 -27
- package/dist/lib/evaluation/hooks/langfuseAdapter.d.ts +1 -39
- package/dist/lib/evaluation/hooks/observabilityHooks.d.ts +3 -55
- package/dist/lib/evaluation/hooks/observabilityHooks.js +3 -0
- package/dist/lib/evaluation/pipeline/strategies/batchStrategy.d.ts +7 -61
- package/dist/lib/evaluation/pipeline/strategies/batchStrategy.js +7 -7
- package/dist/lib/evaluation/ragasEvaluator.js +54 -37
- package/dist/lib/evaluation/reporting/metricsCollector.d.ts +1 -60
- package/dist/lib/evaluation/reporting/reportGenerator.d.ts +1 -17
- package/dist/lib/evaluation/scorers/rule/contentSimilarityScorer.d.ts +1 -29
- package/dist/lib/evaluation/scorers/rule/formatScorer.d.ts +1 -42
- package/dist/lib/evaluation/scorers/rule/keywordCoverageScorer.d.ts +1 -19
- package/dist/lib/evaluation/scorers/rule/lengthScorer.d.ts +1 -33
- package/dist/lib/factories/providerFactory.d.ts +1 -16
- package/dist/lib/factories/providerFactory.js +2 -0
- package/dist/lib/image-gen/ImageGenService.d.ts +3 -0
- package/dist/lib/image-gen/ImageGenService.js +3 -0
- package/dist/lib/mcp/batching/requestBatcher.js +99 -73
- package/dist/lib/mcp/httpRateLimiter.js +3 -1
- package/dist/lib/mcp/httpRetryHandler.js +3 -1
- package/dist/lib/mcp/mcpClientFactory.js +3 -1
- package/dist/lib/mcp/multiServerManager.d.ts +1 -14
- package/dist/lib/mcp/servers/aiProviders/aiAnalysisTools.js +5 -1
- package/dist/lib/mcp/servers/aiProviders/aiWorkflowTools.js +1 -0
- package/dist/lib/mcp/toolDiscoveryService.js +70 -57
- package/dist/lib/mcp/toolRegistry.js +11 -1
- package/dist/lib/memory/memoryRetrievalTools.js +182 -141
- package/dist/lib/neurolink.js +236 -40
- package/dist/lib/observability/exporterRegistry.d.ts +3 -21
- package/dist/lib/observability/exporters/sentryExporter.js +1 -0
- package/dist/lib/observability/metricsAggregator.d.ts +1 -31
- package/dist/lib/observability/tokenTracker.d.ts +7 -16
- package/dist/lib/observability/tokenTracker.js +6 -4
- package/dist/lib/observability/utils/spanSerializer.d.ts +5 -1
- package/dist/lib/observability/utils/spanSerializer.js +24 -4
- package/dist/lib/processors/base/BaseFileProcessor.js +66 -53
- package/dist/lib/processors/document/ExcelProcessor.d.ts +1 -1
- package/dist/lib/processors/errors/errorHelpers.d.ts +1 -31
- package/dist/lib/processors/errors/errorSerializer.d.ts +1 -45
- package/dist/lib/processors/registry/ProcessorRegistry.js +17 -6
- package/dist/lib/providers/amazonBedrock.js +189 -15
- package/dist/lib/providers/amazonSagemaker.js +25 -11
- package/dist/lib/providers/anthropic.js +13 -0
- package/dist/lib/providers/azureOpenai.js +2 -0
- package/dist/lib/providers/googleAiStudio.js +96 -26
- package/dist/lib/providers/googleVertex.js +66 -27
- package/dist/lib/providers/huggingFace.js +2 -0
- package/dist/lib/providers/litellm.js +2 -0
- package/dist/lib/providers/mistral.js +2 -0
- package/dist/lib/providers/ollama.js +84 -5
- package/dist/lib/providers/openAI.d.ts +2 -0
- package/dist/lib/providers/openAI.js +17 -6
- package/dist/lib/providers/openRouter.js +2 -0
- package/dist/lib/providers/openaiCompatible.js +2 -0
- package/dist/lib/providers/sagemaker/detection.d.ts +1 -33
- package/dist/lib/providers/sagemaker/diagnostics.d.ts +1 -25
- package/dist/lib/providers/sagemaker/language-model.d.ts +1 -1
- package/dist/lib/proxy/proxyConfig.js +4 -0
- package/dist/lib/proxy/proxyEnv.d.ts +1 -17
- package/dist/lib/proxy/proxyTracer.d.ts +1 -36
- package/dist/lib/proxy/proxyTracer.js +9 -0
- package/dist/lib/proxy/quietDetector.d.ts +1 -7
- package/dist/lib/proxy/rawStreamCapture.d.ts +1 -10
- package/dist/lib/proxy/requestLogger.d.ts +1 -21
- package/dist/lib/proxy/sseInterceptor.d.ts +1 -66
- package/dist/lib/proxy/sseInterceptor.js +6 -0
- package/dist/lib/proxy/updateChecker.d.ts +1 -6
- package/dist/lib/proxy/updateState.d.ts +1 -12
- package/dist/lib/rag/chunkers/BaseChunker.js +36 -22
- package/dist/lib/rag/chunking/jsonChunker.d.ts +1 -1
- package/dist/lib/rag/errors/RAGError.d.ts +1 -2
- package/dist/lib/rag/ragIntegration.js +45 -32
- package/dist/lib/rag/reranker/reranker.js +151 -122
- package/dist/lib/rag/retrieval/vectorQueryTool.js +79 -65
- package/dist/lib/sdk/toolRegistration.d.ts +10 -44
- package/dist/lib/sdk/toolRegistration.js +1 -1
- package/dist/lib/server/middleware/abortSignal.d.ts +1 -11
- package/dist/lib/server/middleware/auth.d.ts +1 -21
- package/dist/lib/server/middleware/auth.js +12 -0
- package/dist/lib/server/middleware/common.js +48 -32
- package/dist/lib/server/middleware/deprecation.d.ts +1 -20
- package/dist/lib/server/middleware/rateLimit.d.ts +1 -75
- package/dist/lib/server/middleware/validation.d.ts +3 -81
- package/dist/lib/server/middleware/validation.js +3 -0
- package/dist/lib/server/openapi/generator.d.ts +1 -47
- package/dist/lib/server/routes/agentRoutes.js +112 -57
- package/dist/lib/server/routes/claudeProxyRoutes.d.ts +1 -6
- package/dist/lib/server/routes/claudeProxyRoutes.js +127 -13
- package/dist/lib/server/routes/healthRoutes.js +58 -12
- package/dist/lib/server/routes/index.d.ts +1 -26
- package/dist/lib/server/routes/mcpRoutes.js +40 -7
- package/dist/lib/server/routes/memoryRoutes.js +22 -7
- package/dist/lib/server/routes/openApiRoutes.js +30 -6
- package/dist/lib/server/routes/toolRoutes.js +140 -68
- package/dist/lib/server/streaming/dataStream.d.ts +1 -35
- package/dist/lib/server/streaming/dataStream.js +15 -0
- package/dist/lib/services/server/ai/observability/instrumentation.js +114 -14
- package/dist/lib/session/globalSessionState.d.ts +1 -10
- package/dist/lib/tasks/tools/taskTools.d.ts +2 -2
- package/dist/lib/telemetry/traceContext.d.ts +9 -0
- package/dist/lib/telemetry/traceContext.js +19 -0
- package/dist/lib/telemetry/tracers.d.ts +2 -0
- package/dist/lib/telemetry/tracers.js +2 -0
- package/dist/lib/types/action.d.ts +2 -0
- package/dist/lib/types/artifact.d.ts +7 -0
- package/dist/lib/types/auth.d.ts +125 -18
- package/dist/lib/types/autoresearch.d.ts +12 -0
- package/dist/lib/types/cli.d.ts +415 -0
- package/dist/lib/types/client.d.ts +34 -0
- package/dist/lib/types/common.d.ts +12 -41
- package/dist/lib/types/context.d.ts +5 -0
- package/dist/lib/types/evaluation.d.ts +332 -1
- package/dist/lib/types/file.d.ts +4 -0
- package/dist/lib/types/mcp.d.ts +102 -2
- package/dist/lib/types/middleware.d.ts +116 -0
- package/dist/lib/types/multimodal.d.ts +65 -0
- package/dist/lib/types/observability.d.ts +81 -0
- package/dist/lib/types/processor.d.ts +47 -0
- package/dist/lib/types/providers.d.ts +120 -16
- package/dist/lib/types/proxy.d.ts +321 -1
- package/dist/lib/types/rag.d.ts +22 -0
- package/dist/lib/types/scorer.d.ts +141 -0
- package/dist/lib/types/server.d.ts +99 -0
- package/dist/lib/types/span.d.ts +2 -1
- package/dist/lib/types/span.js +1 -0
- package/dist/lib/types/tools.d.ts +44 -0
- package/dist/lib/types/tts.d.ts +6 -0
- package/dist/lib/types/utilities.d.ts +22 -0
- package/dist/lib/types/workflow.d.ts +18 -0
- package/dist/lib/utils/async/retry.d.ts +2 -8
- package/dist/lib/utils/async/retry.js +9 -9
- package/dist/lib/utils/imageCompressor.d.ts +1 -21
- package/dist/lib/utils/imageCompressor.js +5 -1
- package/dist/lib/utils/messageBuilder.d.ts +1 -1
- package/dist/lib/utils/redis.d.ts +1 -4
- package/dist/lib/utils/toolEndEmitter.d.ts +25 -0
- package/dist/lib/utils/toolEndEmitter.js +65 -0
- package/dist/lib/workflow/config.d.ts +7 -32
- package/dist/lib/workflow/core/ensembleExecutor.js +28 -0
- package/dist/lib/workflow/core/judgeScorer.js +23 -0
- package/dist/lib/workflow/core/responseConditioner.js +17 -0
- package/dist/lib/workflow/core/workflowRunner.d.ts +1 -19
- package/dist/lib/workflow/core/workflowRunner.js +202 -147
- package/dist/mcp/batching/requestBatcher.js +99 -73
- package/dist/mcp/httpRateLimiter.js +3 -1
- package/dist/mcp/httpRetryHandler.js +3 -1
- package/dist/mcp/mcpClientFactory.js +3 -1
- package/dist/mcp/multiServerManager.d.ts +1 -14
- package/dist/mcp/servers/aiProviders/aiAnalysisTools.js +5 -1
- package/dist/mcp/servers/aiProviders/aiWorkflowTools.js +1 -0
- package/dist/mcp/toolDiscoveryService.js +70 -57
- package/dist/mcp/toolRegistry.js +11 -1
- package/dist/memory/memoryRetrievalTools.js +182 -141
- package/dist/neurolink.js +236 -40
- package/dist/observability/exporterRegistry.d.ts +3 -21
- package/dist/observability/exporters/sentryExporter.js +1 -0
- package/dist/observability/metricsAggregator.d.ts +1 -31
- package/dist/observability/tokenTracker.d.ts +7 -16
- package/dist/observability/tokenTracker.js +6 -4
- package/dist/observability/utils/spanSerializer.d.ts +5 -1
- package/dist/observability/utils/spanSerializer.js +24 -4
- package/dist/processors/base/BaseFileProcessor.js +66 -53
- package/dist/processors/document/ExcelProcessor.d.ts +1 -1
- package/dist/processors/errors/errorHelpers.d.ts +1 -31
- package/dist/processors/errors/errorSerializer.d.ts +1 -45
- package/dist/processors/registry/ProcessorRegistry.js +17 -6
- package/dist/providers/amazonBedrock.js +189 -15
- package/dist/providers/amazonSagemaker.js +25 -11
- package/dist/providers/anthropic.js +13 -0
- package/dist/providers/azureOpenai.js +2 -0
- package/dist/providers/googleAiStudio.js +96 -26
- package/dist/providers/googleVertex.js +66 -27
- package/dist/providers/huggingFace.js +2 -0
- package/dist/providers/litellm.js +2 -0
- package/dist/providers/mistral.js +2 -0
- package/dist/providers/ollama.js +84 -5
- package/dist/providers/openAI.d.ts +2 -0
- package/dist/providers/openAI.js +17 -6
- package/dist/providers/openRouter.js +2 -0
- package/dist/providers/openaiCompatible.js +2 -0
- package/dist/providers/sagemaker/detection.d.ts +1 -33
- package/dist/providers/sagemaker/diagnostics.d.ts +1 -25
- package/dist/providers/sagemaker/language-model.d.ts +1 -1
- package/dist/proxy/proxyConfig.js +4 -0
- package/dist/proxy/proxyEnv.d.ts +1 -17
- package/dist/proxy/proxyTracer.d.ts +1 -36
- package/dist/proxy/proxyTracer.js +9 -0
- package/dist/proxy/quietDetector.d.ts +1 -7
- package/dist/proxy/rawStreamCapture.d.ts +1 -10
- package/dist/proxy/requestLogger.d.ts +1 -21
- package/dist/proxy/sseInterceptor.d.ts +1 -66
- package/dist/proxy/sseInterceptor.js +6 -0
- package/dist/proxy/updateChecker.d.ts +1 -6
- package/dist/proxy/updateState.d.ts +1 -12
- package/dist/rag/chunkers/BaseChunker.js +36 -22
- package/dist/rag/chunking/jsonChunker.d.ts +1 -1
- package/dist/rag/errors/RAGError.d.ts +1 -2
- package/dist/rag/ragIntegration.js +45 -32
- package/dist/rag/reranker/reranker.js +151 -122
- package/dist/rag/retrieval/vectorQueryTool.js +79 -65
- package/dist/sdk/toolRegistration.d.ts +10 -44
- package/dist/sdk/toolRegistration.js +1 -1
- package/dist/server/middleware/abortSignal.d.ts +1 -11
- package/dist/server/middleware/auth.d.ts +1 -21
- package/dist/server/middleware/auth.js +12 -0
- package/dist/server/middleware/common.js +48 -32
- package/dist/server/middleware/deprecation.d.ts +1 -20
- package/dist/server/middleware/rateLimit.d.ts +1 -75
- package/dist/server/middleware/validation.d.ts +3 -81
- package/dist/server/middleware/validation.js +3 -0
- package/dist/server/openapi/generator.d.ts +1 -47
- package/dist/server/routes/agentRoutes.js +112 -57
- package/dist/server/routes/claudeProxyRoutes.d.ts +1 -6
- package/dist/server/routes/claudeProxyRoutes.js +127 -13
- package/dist/server/routes/healthRoutes.js +58 -12
- package/dist/server/routes/index.d.ts +1 -26
- package/dist/server/routes/mcpRoutes.js +40 -7
- package/dist/server/routes/memoryRoutes.js +22 -7
- package/dist/server/routes/openApiRoutes.js +30 -6
- package/dist/server/routes/toolRoutes.js +140 -68
- package/dist/server/streaming/dataStream.d.ts +1 -35
- package/dist/server/streaming/dataStream.js +15 -0
- package/dist/services/server/ai/observability/instrumentation.js +114 -14
- package/dist/session/globalSessionState.d.ts +1 -10
- package/dist/tasks/tools/taskTools.d.ts +2 -2
- package/dist/telemetry/traceContext.d.ts +9 -0
- package/dist/telemetry/traceContext.js +18 -0
- package/dist/telemetry/tracers.d.ts +2 -0
- package/dist/telemetry/tracers.js +2 -0
- package/dist/types/action.d.ts +2 -0
- package/dist/types/artifact.d.ts +7 -0
- package/dist/types/auth.d.ts +125 -18
- package/dist/types/autoresearch.d.ts +12 -0
- package/dist/types/cli.d.ts +415 -0
- package/dist/types/client.d.ts +34 -0
- package/dist/types/common.d.ts +12 -41
- package/dist/types/context.d.ts +5 -0
- package/dist/types/evaluation.d.ts +332 -1
- package/dist/types/file.d.ts +4 -0
- package/dist/types/mcp.d.ts +102 -2
- package/dist/types/middleware.d.ts +116 -0
- package/dist/types/multimodal.d.ts +65 -0
- package/dist/types/observability.d.ts +81 -0
- package/dist/types/processor.d.ts +47 -0
- package/dist/types/providers.d.ts +120 -16
- package/dist/types/proxy.d.ts +321 -1
- package/dist/types/rag.d.ts +22 -0
- package/dist/types/scorer.d.ts +141 -0
- package/dist/types/server.d.ts +99 -0
- package/dist/types/span.d.ts +2 -1
- package/dist/types/span.js +1 -0
- package/dist/types/tools.d.ts +44 -0
- package/dist/types/tts.d.ts +6 -0
- package/dist/types/utilities.d.ts +22 -0
- package/dist/types/workflow.d.ts +18 -0
- package/dist/utils/async/retry.d.ts +2 -8
- package/dist/utils/async/retry.js +9 -9
- package/dist/utils/imageCompressor.d.ts +1 -21
- package/dist/utils/imageCompressor.js +5 -1
- package/dist/utils/messageBuilder.d.ts +1 -1
- package/dist/utils/redis.d.ts +1 -4
- package/dist/utils/toolEndEmitter.d.ts +25 -0
- package/dist/utils/toolEndEmitter.js +64 -0
- package/dist/workflow/config.d.ts +4 -29
- package/dist/workflow/core/ensembleExecutor.js +28 -0
- package/dist/workflow/core/judgeScorer.js +23 -0
- package/dist/workflow/core/responseConditioner.js +17 -0
- package/dist/workflow/core/workflowRunner.d.ts +1 -19
- package/dist/workflow/core/workflowRunner.js +202 -147
- package/package.json +2 -8
|
@@ -11,8 +11,11 @@
|
|
|
11
11
|
*
|
|
12
12
|
* @module workflow/core/workflowRunner
|
|
13
13
|
*/
|
|
14
|
+
import { SpanStatusCode } from "@opentelemetry/api";
|
|
14
15
|
import { logger } from "../../utils/logger.js";
|
|
15
16
|
import { SpanSerializer, SpanType, SpanStatus, getMetricsAggregator, } from "../../observability/index.js";
|
|
17
|
+
import { withSpan } from "../../telemetry/withSpan.js";
|
|
18
|
+
import { tracers } from "../../telemetry/tracers.js";
|
|
16
19
|
import { getModelGroups, PLACEHOLDER_MODEL, PLACEHOLDER_PROVIDER, usesModelGroups, } from "../config.js";
|
|
17
20
|
import { validateWorkflow } from "../utils/workflowValidation.js";
|
|
18
21
|
import { executeEnsemble, executeModelGroups } from "./ensembleExecutor.js";
|
|
@@ -45,156 +48,172 @@ import { conditionResponse } from "./responseConditioner.js";
|
|
|
45
48
|
* ```
|
|
46
49
|
*/
|
|
47
50
|
export async function runWorkflow(config, options) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
span
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (options.verbose) {
|
|
72
|
-
logger.debug(`[WorkflowRunner] Received ${ensembleResult.responses.length} model responses`);
|
|
73
|
-
logger.debug(`[WorkflowRunner] Successful: ${ensembleResult.successCount}`);
|
|
74
|
-
}
|
|
75
|
-
// Step 2: Score responses with judge(s)
|
|
76
|
-
const scoreResult = await scoreResponses(config, ensembleResult.responses, options);
|
|
77
|
-
if (options.verbose) {
|
|
78
|
-
logger.debug(`[WorkflowRunner] Scoring complete`);
|
|
79
|
-
logger.debug(`[WorkflowRunner] Scores:`, scoreResult.scores);
|
|
51
|
+
return withSpan({
|
|
52
|
+
name: "neurolink.workflow.run",
|
|
53
|
+
tracer: tracers.sdk,
|
|
54
|
+
attributes: {
|
|
55
|
+
"workflow.name": config.name,
|
|
56
|
+
"workflow.type": config.type,
|
|
57
|
+
"workflow.id": config.id ?? "unknown",
|
|
58
|
+
},
|
|
59
|
+
}, async (otelSpan) => {
|
|
60
|
+
const startTime = Date.now();
|
|
61
|
+
const span = SpanSerializer.createSpan(SpanType.WORKFLOW, "workflow.run", {
|
|
62
|
+
"workflow.operation": "run",
|
|
63
|
+
"workflow.name": config.name,
|
|
64
|
+
"workflow.type": config.type,
|
|
65
|
+
"workflow.id": config.id,
|
|
66
|
+
});
|
|
67
|
+
// Validate configuration
|
|
68
|
+
const validation = validateWorkflow(config);
|
|
69
|
+
if (!validation.valid) {
|
|
70
|
+
span.durationMs = Date.now() - startTime;
|
|
71
|
+
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.ERROR, `Invalid workflow configuration: ${validation.errors.map((err) => err.message).join(", ")}`);
|
|
72
|
+
getMetricsAggregator().recordSpan(endedSpan);
|
|
73
|
+
throw new Error(`Invalid workflow configuration: ${validation.errors.map((err) => err.message).join(", ")}`);
|
|
80
74
|
}
|
|
81
|
-
// Step 3: Select best response
|
|
82
|
-
const bestResponse = selectBestResponse(ensembleResult.responses, scoreResult.scores);
|
|
83
75
|
if (options.verbose) {
|
|
84
|
-
logger.debug(`[WorkflowRunner]
|
|
85
|
-
|
|
86
|
-
logger.debug(`[WorkflowRunner]
|
|
87
|
-
}
|
|
88
|
-
// CRITICAL: Store original content BEFORE any processing
|
|
89
|
-
const originalContent = bestResponse.content;
|
|
90
|
-
// Step 4: Get processed content
|
|
91
|
-
// Priority: Judge-synthesized > Separate conditioning > Original
|
|
92
|
-
let processedContent;
|
|
93
|
-
let conditioningTime = 0;
|
|
94
|
-
const judgeScores = isJudgeScores(scoreResult.scores)
|
|
95
|
-
? scoreResult.scores
|
|
96
|
-
: convertToJudgeScores(scoreResult.scores);
|
|
97
|
-
if (judgeScores.synthesizedResponse) {
|
|
98
|
-
// Judge already synthesized improved response
|
|
99
|
-
processedContent = judgeScores.synthesizedResponse;
|
|
100
|
-
logger.debug(`[WorkflowRunner] Using judge-synthesized response`);
|
|
76
|
+
logger.debug(`[WorkflowRunner] Starting workflow: ${config.name}`);
|
|
77
|
+
logger.debug(`[WorkflowRunner] Type: ${config.type}`);
|
|
78
|
+
logger.debug(`[WorkflowRunner] Uses layer-based execution: ${usesModelGroups(config)}`);
|
|
101
79
|
}
|
|
102
|
-
|
|
103
|
-
//
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
//
|
|
123
|
-
originalContent
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
80
|
+
try {
|
|
81
|
+
// Step 1: Execute models (layer-based or flat)
|
|
82
|
+
const ensembleResult = await executeModels(config, options);
|
|
83
|
+
if (options.verbose) {
|
|
84
|
+
logger.debug(`[WorkflowRunner] Received ${ensembleResult.responses.length} model responses`);
|
|
85
|
+
logger.debug(`[WorkflowRunner] Successful: ${ensembleResult.successCount}`);
|
|
86
|
+
}
|
|
87
|
+
// Step 2: Score responses with judge(s)
|
|
88
|
+
const scoreResult = await scoreResponses(config, ensembleResult.responses, options);
|
|
89
|
+
if (options.verbose) {
|
|
90
|
+
logger.debug(`[WorkflowRunner] Scoring complete`);
|
|
91
|
+
logger.debug(`[WorkflowRunner] Scores:`, scoreResult.scores);
|
|
92
|
+
}
|
|
93
|
+
// Step 3: Select best response
|
|
94
|
+
const bestResponse = selectBestResponse(ensembleResult.responses, scoreResult.scores);
|
|
95
|
+
if (options.verbose) {
|
|
96
|
+
logger.debug(`[WorkflowRunner] Best response: ${bestResponse.model}`);
|
|
97
|
+
const bestScore = extractScore(scoreResult.scores, bestResponse, ensembleResult.responses);
|
|
98
|
+
logger.debug(`[WorkflowRunner] Best score: ${bestScore}`);
|
|
99
|
+
}
|
|
100
|
+
// CRITICAL: Store original content BEFORE any processing
|
|
101
|
+
const originalContent = bestResponse.content;
|
|
102
|
+
// Step 4: Get processed content
|
|
103
|
+
// Priority: Judge-synthesized > Separate conditioning > Original
|
|
104
|
+
let processedContent;
|
|
105
|
+
let conditioningTime = 0;
|
|
106
|
+
const judgeScores = isJudgeScores(scoreResult.scores)
|
|
107
|
+
? scoreResult.scores
|
|
108
|
+
: convertToJudgeScores(scoreResult.scores);
|
|
109
|
+
if (judgeScores.synthesizedResponse) {
|
|
110
|
+
// Judge already synthesized improved response
|
|
111
|
+
processedContent = judgeScores.synthesizedResponse;
|
|
112
|
+
logger.debug(`[WorkflowRunner] Using judge-synthesized response`);
|
|
113
|
+
}
|
|
114
|
+
else if (config.conditioning) {
|
|
115
|
+
// Fall back to separate conditioning if configured
|
|
116
|
+
const conditionedContent = await conditionFinalResponse(bestResponse, scoreResult.scores, config, options, ensembleResult.responses);
|
|
117
|
+
processedContent = conditionedContent.content;
|
|
118
|
+
conditioningTime = conditionedContent.conditioningTime;
|
|
119
|
+
logger.debug(`[WorkflowRunner] Using separate conditioning`);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
// No processing, use original
|
|
123
|
+
processedContent = originalContent;
|
|
124
|
+
logger.debug(`[WorkflowRunner] No conditioning applied`);
|
|
125
|
+
}
|
|
126
|
+
// Step 5: Calculate execution metrics
|
|
127
|
+
const executionTime = Date.now() - startTime;
|
|
128
|
+
const ensembleTime = ensembleResult.totalTime;
|
|
129
|
+
const judgeTime = scoreResult.judgeTime;
|
|
130
|
+
// Step 6: Assemble complete result
|
|
131
|
+
const result = {
|
|
132
|
+
// Primary output (processed version)
|
|
133
|
+
content: processedContent,
|
|
134
|
+
// IMPORTANT: Store original unmodified response separately
|
|
135
|
+
originalContent: originalContent,
|
|
136
|
+
// Evaluation metrics (0-100 scale)
|
|
137
|
+
score: extractScore(scoreResult.scores, bestResponse, ensembleResult.responses),
|
|
138
|
+
reasoning: extractReasoning(scoreResult.scores),
|
|
139
|
+
// Ensemble data
|
|
140
|
+
ensembleResponses: ensembleResult.responses,
|
|
141
|
+
// Judge data
|
|
142
|
+
judgeScores: judgeScores,
|
|
143
|
+
selectedResponse: bestResponse,
|
|
144
|
+
// Quality metrics
|
|
145
|
+
confidence: extractConfidence(scoreResult.scores),
|
|
146
|
+
consensus: extractConsensus(scoreResult.scores),
|
|
147
|
+
// Performance metrics
|
|
148
|
+
totalTime: executionTime,
|
|
149
|
+
ensembleTime,
|
|
150
|
+
judgeTime,
|
|
151
|
+
conditioningTime: conditioningTime,
|
|
152
|
+
// Workflow metadata
|
|
153
|
+
workflow: config.id,
|
|
154
|
+
workflowName: config.name,
|
|
155
|
+
workflowVersion: config.version,
|
|
156
|
+
// Resource usage
|
|
157
|
+
usage: {
|
|
158
|
+
totalInputTokens: calculateInputTokens(ensembleResult.responses),
|
|
159
|
+
totalOutputTokens: calculateOutputTokens(ensembleResult.responses),
|
|
160
|
+
totalTokens: calculateTotalTokens(ensembleResult.responses),
|
|
161
|
+
byModel: [], // TODO: Populate per-model breakdown
|
|
162
|
+
},
|
|
163
|
+
// Additional metadata
|
|
164
|
+
metadata: options.metadata,
|
|
165
|
+
timestamp: new Date().toISOString(),
|
|
166
|
+
};
|
|
167
|
+
if (options.verbose) {
|
|
168
|
+
logger.debug(`[WorkflowRunner] Workflow complete in ${executionTime}ms`);
|
|
169
|
+
logger.debug(`[WorkflowRunner] Total tokens: ${result.usage?.totalTokens || 0}`);
|
|
170
|
+
}
|
|
171
|
+
span.durationMs = executionTime;
|
|
172
|
+
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.OK);
|
|
173
|
+
getMetricsAggregator().recordSpan(endedSpan);
|
|
174
|
+
return result;
|
|
158
175
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
176
|
+
catch (error) {
|
|
177
|
+
const executionTime = Date.now() - startTime;
|
|
178
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
179
|
+
if (options.verbose) {
|
|
180
|
+
logger.error(`[WorkflowRunner] Workflow failed:`, errorMessage);
|
|
181
|
+
}
|
|
182
|
+
span.durationMs = executionTime;
|
|
183
|
+
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.ERROR, errorMessage);
|
|
184
|
+
getMetricsAggregator().recordSpan(endedSpan);
|
|
185
|
+
// Mark outer OTel span as ERROR since we return instead of rethrowing
|
|
186
|
+
otelSpan.recordException(error instanceof Error ? error : new Error(errorMessage));
|
|
187
|
+
otelSpan.setStatus({
|
|
188
|
+
code: SpanStatusCode.ERROR,
|
|
189
|
+
message: errorMessage,
|
|
190
|
+
});
|
|
191
|
+
// Return error result with dummy data
|
|
192
|
+
const dummyResponse = {
|
|
193
|
+
provider: PLACEHOLDER_PROVIDER,
|
|
194
|
+
model: PLACEHOLDER_MODEL,
|
|
195
|
+
content: "",
|
|
196
|
+
responseTime: 0,
|
|
197
|
+
status: "failure",
|
|
198
|
+
error: errorMessage,
|
|
199
|
+
timestamp: new Date().toISOString(),
|
|
200
|
+
};
|
|
201
|
+
return {
|
|
202
|
+
content: "",
|
|
203
|
+
score: 0,
|
|
204
|
+
reasoning: `Workflow execution failed: ${errorMessage}`,
|
|
205
|
+
ensembleResponses: [dummyResponse],
|
|
206
|
+
confidence: 0,
|
|
207
|
+
totalTime: executionTime,
|
|
208
|
+
ensembleTime: 0,
|
|
209
|
+
workflow: config.id,
|
|
210
|
+
workflowName: config.name,
|
|
211
|
+
workflowVersion: config.version,
|
|
212
|
+
metadata: options.metadata,
|
|
213
|
+
timestamp: new Date().toISOString(),
|
|
214
|
+
};
|
|
169
215
|
}
|
|
170
|
-
|
|
171
|
-
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.ERROR, errorMessage);
|
|
172
|
-
getMetricsAggregator().recordSpan(endedSpan);
|
|
173
|
-
// Return error result with dummy data
|
|
174
|
-
const dummyResponse = {
|
|
175
|
-
provider: PLACEHOLDER_PROVIDER,
|
|
176
|
-
model: PLACEHOLDER_MODEL,
|
|
177
|
-
content: "",
|
|
178
|
-
responseTime: 0,
|
|
179
|
-
status: "failure",
|
|
180
|
-
error: errorMessage,
|
|
181
|
-
timestamp: new Date().toISOString(),
|
|
182
|
-
};
|
|
183
|
-
return {
|
|
184
|
-
content: "",
|
|
185
|
-
score: 0,
|
|
186
|
-
reasoning: `Workflow execution failed: ${errorMessage}`,
|
|
187
|
-
ensembleResponses: [dummyResponse],
|
|
188
|
-
confidence: 0,
|
|
189
|
-
totalTime: executionTime,
|
|
190
|
-
ensembleTime: 0,
|
|
191
|
-
workflow: config.id,
|
|
192
|
-
workflowName: config.name,
|
|
193
|
-
workflowVersion: config.version,
|
|
194
|
-
metadata: options.metadata,
|
|
195
|
-
timestamp: new Date().toISOString(),
|
|
196
|
-
};
|
|
197
|
-
}
|
|
216
|
+
}); // end withSpan
|
|
198
217
|
}
|
|
199
218
|
/**
|
|
200
219
|
* Execute models using layer-based or flat execution
|
|
@@ -459,6 +478,18 @@ function calculateTotalTokens(responses) {
|
|
|
459
478
|
* ```
|
|
460
479
|
*/
|
|
461
480
|
export async function* runWorkflowWithStreaming(config, options) {
|
|
481
|
+
// Wrap the generator in an active OTel span so Pipeline A captures the
|
|
482
|
+
// streaming workflow end-to-end and Pipeline B (below) inherits its traceId.
|
|
483
|
+
const generator = tracers.workflow.startActiveSpan("neurolink.workflow.run.streaming", {
|
|
484
|
+
attributes: {
|
|
485
|
+
"workflow.name": config.name,
|
|
486
|
+
"workflow.type": config.type,
|
|
487
|
+
"workflow.id": config.id ?? "unknown",
|
|
488
|
+
},
|
|
489
|
+
}, (otelSpan) => runWorkflowStreamingInner(config, options, otelSpan));
|
|
490
|
+
yield* generator;
|
|
491
|
+
}
|
|
492
|
+
async function* runWorkflowStreamingInner(config, options, otelSpan) {
|
|
462
493
|
const startTime = Date.now();
|
|
463
494
|
const span = SpanSerializer.createSpan(SpanType.WORKFLOW, "workflow.run.streaming", {
|
|
464
495
|
"workflow.operation": "run.streaming",
|
|
@@ -469,14 +500,21 @@ export async function* runWorkflowWithStreaming(config, options) {
|
|
|
469
500
|
// Validate configuration
|
|
470
501
|
const validation = validateWorkflow(config);
|
|
471
502
|
if (!validation.valid) {
|
|
503
|
+
const errMsg = `Invalid workflow configuration: ${validation.errors.map((err) => err.message).join(", ")}`;
|
|
472
504
|
span.durationMs = Date.now() - startTime;
|
|
473
|
-
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.ERROR,
|
|
505
|
+
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.ERROR, errMsg);
|
|
474
506
|
getMetricsAggregator().recordSpan(endedSpan);
|
|
475
|
-
|
|
507
|
+
const err = new Error(errMsg);
|
|
508
|
+
otelSpan.recordException(err);
|
|
509
|
+
otelSpan.setStatus({ code: SpanStatusCode.ERROR, message: errMsg });
|
|
510
|
+
otelSpan.end();
|
|
511
|
+
throw err;
|
|
476
512
|
}
|
|
477
513
|
if (options.verbose) {
|
|
478
514
|
logger.debug(`[WorkflowRunner] Starting streaming workflow: ${config.name}`);
|
|
479
515
|
}
|
|
516
|
+
// eslint-disable-next-line no-useless-assignment -- read in finally block
|
|
517
|
+
let spanEnded = false;
|
|
480
518
|
try {
|
|
481
519
|
// Step 1: Execute models
|
|
482
520
|
const ensembleResult = await executeModels(config, options);
|
|
@@ -571,17 +609,34 @@ export async function* runWorkflowWithStreaming(config, options) {
|
|
|
571
609
|
span.durationMs = executionTime;
|
|
572
610
|
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.OK);
|
|
573
611
|
getMetricsAggregator().recordSpan(endedSpan);
|
|
612
|
+
spanEnded = true;
|
|
613
|
+
otelSpan.setStatus({ code: SpanStatusCode.OK });
|
|
614
|
+
otelSpan.end();
|
|
574
615
|
}
|
|
575
616
|
catch (error) {
|
|
617
|
+
spanEnded = true;
|
|
576
618
|
const executionTime = Date.now() - startTime;
|
|
577
619
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
578
620
|
span.durationMs = executionTime;
|
|
579
621
|
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.ERROR, errorMessage);
|
|
580
622
|
getMetricsAggregator().recordSpan(endedSpan);
|
|
623
|
+
if (error instanceof Error) {
|
|
624
|
+
otelSpan.recordException(error);
|
|
625
|
+
}
|
|
626
|
+
otelSpan.setStatus({ code: SpanStatusCode.ERROR, message: errorMessage });
|
|
627
|
+
otelSpan.end();
|
|
581
628
|
logger.error(`[WorkflowRunner] Streaming workflow failed`, {
|
|
582
629
|
error: errorMessage,
|
|
583
630
|
});
|
|
584
631
|
throw error;
|
|
585
632
|
}
|
|
633
|
+
finally {
|
|
634
|
+
// Guard against span leak when the consumer breaks out of the async
|
|
635
|
+
// generator early (neither try-success nor catch fires in that case).
|
|
636
|
+
if (!spanEnded) {
|
|
637
|
+
otelSpan.setStatus({ code: SpanStatusCode.OK });
|
|
638
|
+
otelSpan.end();
|
|
639
|
+
}
|
|
640
|
+
}
|
|
586
641
|
}
|
|
587
642
|
//# sourceMappingURL=workflowRunner.js.map
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
import { EventEmitter } from "events";
|
|
11
11
|
import { logger } from "../../utils/logger.js";
|
|
12
12
|
import { ErrorFactory } from "../../utils/errorHandling.js";
|
|
13
|
+
import { withSpan } from "../../telemetry/withSpan.js";
|
|
14
|
+
import { tracers } from "../../telemetry/tracers.js";
|
|
13
15
|
/**
|
|
14
16
|
* Request Batcher - Efficient batch processing for MCP tool calls
|
|
15
17
|
*
|
|
@@ -217,85 +219,109 @@ export class RequestBatcher extends EventEmitter {
|
|
|
217
219
|
const startTime = Date.now();
|
|
218
220
|
this.activeBatches++;
|
|
219
221
|
this.emit("batchStarted", { batchId, size: batchRequests.length });
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
timeoutHandle = setTimeout(() => reject(ErrorFactory.toolTimeout("batchExecution", timeoutMs)), timeoutMs);
|
|
237
|
-
});
|
|
238
|
-
const results = await Promise.race([
|
|
239
|
-
executorPromise,
|
|
240
|
-
timeoutPromise,
|
|
241
|
-
]).finally(() => {
|
|
242
|
-
if (timeoutHandle) {
|
|
243
|
-
clearTimeout(timeoutHandle);
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
// Process results
|
|
247
|
-
const batchResults = [];
|
|
248
|
-
for (let i = 0; i < batchRequests.length; i++) {
|
|
249
|
-
const request = batchRequests[i];
|
|
250
|
-
const result = results[i];
|
|
251
|
-
const executionTime = Date.now() - startTime;
|
|
252
|
-
if (!result) {
|
|
253
|
-
const noResultError = ErrorFactory.toolExecutionFailed(request.tool, new Error(`Batch executor returned no result for request ${i}`));
|
|
254
|
-
request.reject(noResultError);
|
|
255
|
-
batchResults.push({
|
|
256
|
-
id: request.id,
|
|
257
|
-
success: false,
|
|
258
|
-
error: noResultError,
|
|
259
|
-
executionTime,
|
|
222
|
+
await withSpan({
|
|
223
|
+
name: "neurolink.mcp.batch.execute",
|
|
224
|
+
tracer: tracers.mcp,
|
|
225
|
+
attributes: {
|
|
226
|
+
"mcp.batch.id": batchId,
|
|
227
|
+
"mcp.batch.size": batchRequests.length,
|
|
228
|
+
"mcp.batch.active_batches": this.activeBatches,
|
|
229
|
+
},
|
|
230
|
+
}, async (span) => {
|
|
231
|
+
let successCount = 0;
|
|
232
|
+
let errorCount = 0;
|
|
233
|
+
try {
|
|
234
|
+
// Guard against missing executor
|
|
235
|
+
if (!this.executor) {
|
|
236
|
+
throw ErrorFactory.missingConfiguration("batchExecutor", {
|
|
237
|
+
hint: "Call setExecutor() before executing batches",
|
|
260
238
|
});
|
|
261
|
-
continue;
|
|
262
239
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
240
|
+
// Execute the batch with a timeout to prevent indefinite hangs
|
|
241
|
+
const executorPromise = this.executor(batchRequests.map((r) => ({
|
|
242
|
+
tool: r.tool,
|
|
243
|
+
args: r.args,
|
|
244
|
+
serverId: r.serverId,
|
|
245
|
+
})));
|
|
246
|
+
const timeoutMs = Math.max(5000, Number(process.env.MCP_TOOL_TIMEOUT) || 60000);
|
|
247
|
+
let timeoutHandle;
|
|
248
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
249
|
+
timeoutHandle = setTimeout(() => reject(ErrorFactory.toolTimeout("batchExecution", timeoutMs)), timeoutMs);
|
|
250
|
+
});
|
|
251
|
+
// Suppress unhandled rejection if executorPromise rejects after timeout wins
|
|
252
|
+
void executorPromise.catch((_e) => {
|
|
253
|
+
// Intentionally swallowed — timeout already handled the failure
|
|
254
|
+
});
|
|
255
|
+
const results = await Promise.race([
|
|
256
|
+
executorPromise,
|
|
257
|
+
timeoutPromise,
|
|
258
|
+
]).finally(() => {
|
|
259
|
+
if (timeoutHandle) {
|
|
260
|
+
clearTimeout(timeoutHandle);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
// Process results
|
|
264
|
+
const batchResults = [];
|
|
265
|
+
for (let i = 0; i < batchRequests.length; i++) {
|
|
266
|
+
const request = batchRequests[i];
|
|
267
|
+
const result = results[i];
|
|
268
|
+
const executionTime = Date.now() - startTime;
|
|
269
|
+
if (!result) {
|
|
270
|
+
const noResultError = ErrorFactory.toolExecutionFailed(request.tool, new Error(`Batch executor returned no result for request ${i}`));
|
|
271
|
+
request.reject(noResultError);
|
|
272
|
+
batchResults.push({
|
|
273
|
+
id: request.id,
|
|
274
|
+
success: false,
|
|
275
|
+
error: noResultError,
|
|
276
|
+
executionTime,
|
|
277
|
+
});
|
|
278
|
+
errorCount++;
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
if (result.success) {
|
|
282
|
+
request.resolve(result.result);
|
|
283
|
+
batchResults.push({
|
|
284
|
+
id: request.id,
|
|
285
|
+
success: true,
|
|
286
|
+
result: result.result,
|
|
287
|
+
executionTime,
|
|
288
|
+
});
|
|
289
|
+
successCount++;
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
const error = result.error ??
|
|
293
|
+
ErrorFactory.toolExecutionFailed(request.tool, new Error("Unknown batch execution error"));
|
|
294
|
+
request.reject(error);
|
|
295
|
+
batchResults.push({
|
|
296
|
+
id: request.id,
|
|
297
|
+
success: false,
|
|
298
|
+
error,
|
|
299
|
+
executionTime,
|
|
300
|
+
});
|
|
301
|
+
errorCount++;
|
|
302
|
+
}
|
|
271
303
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
304
|
+
span.setAttribute("mcp.batch.success_count", successCount);
|
|
305
|
+
span.setAttribute("mcp.batch.error_count", errorCount);
|
|
306
|
+
this.emit("batchCompleted", { batchId, results: batchResults });
|
|
307
|
+
}
|
|
308
|
+
catch (error) {
|
|
309
|
+
// Batch-level failure - reject all requests
|
|
310
|
+
const batchError = error instanceof Error
|
|
311
|
+
? error
|
|
312
|
+
: ErrorFactory.toolExecutionFailed("batch", new Error(String(error)));
|
|
313
|
+
for (const request of batchRequests) {
|
|
314
|
+
request.reject(batchError);
|
|
282
315
|
}
|
|
316
|
+
this.emit("batchFailed", { batchId, error: batchError });
|
|
317
|
+
throw batchError;
|
|
283
318
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
catch (error) {
|
|
287
|
-
// Batch-level failure - reject all requests
|
|
288
|
-
const batchError = error instanceof Error
|
|
289
|
-
? error
|
|
290
|
-
: ErrorFactory.toolExecutionFailed("batch", new Error(String(error)));
|
|
291
|
-
for (const request of batchRequests) {
|
|
292
|
-
request.reject(batchError);
|
|
319
|
+
finally {
|
|
320
|
+
this.activeBatches--;
|
|
293
321
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
this.activeBatches--;
|
|
298
|
-
}
|
|
322
|
+
}).catch((error) => {
|
|
323
|
+
logger.error("Batch span execution failed:", error);
|
|
324
|
+
});
|
|
299
325
|
// Schedule next batch if there are more pending requests
|
|
300
326
|
if (this.pending.size > 0) {
|
|
301
327
|
this.clearFlushTimer();
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { mcpLogger } from "../utils/logger.js";
|
|
7
7
|
import { SpanSerializer, SpanType, SpanStatus, getMetricsAggregator, } from "../observability/index.js";
|
|
8
|
+
import { getActiveTraceContext } from "../telemetry/traceContext.js";
|
|
8
9
|
/**
|
|
9
10
|
* Default rate limit configuration
|
|
10
11
|
* Provides sensible defaults for most MCP HTTP transport use cases
|
|
@@ -72,12 +73,13 @@ export class HTTPRateLimiter {
|
|
|
72
73
|
* @throws Error if the wait queue is too long
|
|
73
74
|
*/
|
|
74
75
|
async acquire() {
|
|
76
|
+
const { traceId, parentSpanId } = getActiveTraceContext();
|
|
75
77
|
const span = SpanSerializer.createSpan(SpanType.MCP_TRANSPORT, "mcp.rateLimit", {
|
|
76
78
|
"mcp.transport": "http",
|
|
77
79
|
"mcp.operation": "rateLimit",
|
|
78
80
|
"mcp.rateLimit.tokensAvailable": this.tokens,
|
|
79
81
|
"mcp.rateLimit.maxBurst": this.config.maxBurst,
|
|
80
|
-
});
|
|
82
|
+
}, parentSpanId, traceId);
|
|
81
83
|
const startTime = Date.now();
|
|
82
84
|
try {
|
|
83
85
|
// First, try to acquire without waiting
|
|
@@ -8,6 +8,7 @@ import { isAbortError } from "../utils/errorHandling.js";
|
|
|
8
8
|
import { calculateBackoffDelay } from "../utils/retryHandler.js";
|
|
9
9
|
import { logger } from "../utils/logger.js";
|
|
10
10
|
import { SpanSerializer, SpanType, SpanStatus, getMetricsAggregator, } from "../observability/index.js";
|
|
11
|
+
import { getActiveTraceContext } from "../telemetry/traceContext.js";
|
|
11
12
|
/**
|
|
12
13
|
* Default HTTP retry configuration
|
|
13
14
|
*/
|
|
@@ -130,11 +131,12 @@ export async function withHTTPRetry(operation, config = {}) {
|
|
|
130
131
|
...DEFAULT_HTTP_RETRY_CONFIG,
|
|
131
132
|
...config,
|
|
132
133
|
};
|
|
134
|
+
const { traceId, parentSpanId } = getActiveTraceContext();
|
|
133
135
|
const span = SpanSerializer.createSpan(SpanType.MCP_TRANSPORT, "mcp.retry", {
|
|
134
136
|
"mcp.transport": "http",
|
|
135
137
|
"mcp.operation": "retry",
|
|
136
138
|
"mcp.maxAttempts": mergedConfig.maxAttempts,
|
|
137
|
-
});
|
|
139
|
+
}, parentSpanId, traceId);
|
|
138
140
|
const startTime = Date.now();
|
|
139
141
|
let lastError;
|
|
140
142
|
let actualAttempts = 0;
|