@juspay/neurolink 9.54.6 → 9.54.7
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 +2 -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 +353 -353
- 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 +82 -0
- package/dist/lib/providers/googleVertex.js +52 -0
- 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 +82 -0
- package/dist/providers/googleVertex.js +52 -0
- 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 -1
|
@@ -2,69 +2,7 @@
|
|
|
2
2
|
* Request Validation Middleware
|
|
3
3
|
* Provides schema-based request validation for server adapters
|
|
4
4
|
*/
|
|
5
|
-
import type { MiddlewareDefinition,
|
|
6
|
-
import { ValidationError as ServerValidationError } from "../errors.js";
|
|
7
|
-
/**
|
|
8
|
-
* Validation configuration
|
|
9
|
-
*/
|
|
10
|
-
type ValidationConfig = {
|
|
11
|
-
/** Schema for validating request body */
|
|
12
|
-
bodySchema?: ValidationSchema;
|
|
13
|
-
/** Schema for validating query parameters */
|
|
14
|
-
querySchema?: ValidationSchema;
|
|
15
|
-
/** Schema for validating path parameters */
|
|
16
|
-
paramsSchema?: ValidationSchema;
|
|
17
|
-
/** Schema for validating headers */
|
|
18
|
-
headersSchema?: ValidationSchema;
|
|
19
|
-
/**
|
|
20
|
-
* Custom validation function
|
|
21
|
-
* Throw ValidationError for invalid requests
|
|
22
|
-
*/
|
|
23
|
-
customValidator?: (ctx: ServerContext) => Promise<void>;
|
|
24
|
-
/** Skip validation for certain paths */
|
|
25
|
-
skipPaths?: string[];
|
|
26
|
-
/** Custom error formatter */
|
|
27
|
-
errorFormatter?: (errors: ServerValidationError[]) => unknown;
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* Simple validation schema
|
|
31
|
-
* Can be extended with JSON Schema or Zod integration
|
|
32
|
-
*/
|
|
33
|
-
type ValidationSchema = {
|
|
34
|
-
/** Required fields */
|
|
35
|
-
required?: string[];
|
|
36
|
-
/** Field type definitions */
|
|
37
|
-
properties?: Record<string, PropertySchema>;
|
|
38
|
-
/** Allow additional properties */
|
|
39
|
-
additionalProperties?: boolean;
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* Property schema definition
|
|
43
|
-
*/
|
|
44
|
-
type PropertySchema = {
|
|
45
|
-
/** Property type */
|
|
46
|
-
type: "string" | "number" | "boolean" | "object" | "array";
|
|
47
|
-
/** Minimum value (for numbers) or length (for strings/arrays) */
|
|
48
|
-
minimum?: number;
|
|
49
|
-
/** Maximum value (for numbers) or length (for strings/arrays) */
|
|
50
|
-
maximum?: number;
|
|
51
|
-
/** Minimum length for strings (alias for minimum) */
|
|
52
|
-
minLength?: number;
|
|
53
|
-
/** Maximum length for strings (alias for maximum) */
|
|
54
|
-
maxLength?: number;
|
|
55
|
-
/** Minimum items for arrays */
|
|
56
|
-
minItems?: number;
|
|
57
|
-
/** Maximum items for arrays */
|
|
58
|
-
maxItems?: number;
|
|
59
|
-
/** Pattern for string validation (regex) */
|
|
60
|
-
pattern?: string;
|
|
61
|
-
/** Enum of allowed values */
|
|
62
|
-
enum?: unknown[];
|
|
63
|
-
/** Default value */
|
|
64
|
-
default?: unknown;
|
|
65
|
-
/** Custom validation function */
|
|
66
|
-
validate?: (value: unknown) => boolean | string;
|
|
67
|
-
};
|
|
5
|
+
import type { ExtendedValidationSchema, MiddlewareDefinition, MiddlewareRequestSchema, PropertySchema, ValidationConfig } from "../../types/index.js";
|
|
68
6
|
/**
|
|
69
7
|
* Re-export ValidationError from errors for convenience
|
|
70
8
|
*/
|
|
@@ -105,7 +43,7 @@ export declare function createFieldValidator(fieldName: string, rules: PropertyS
|
|
|
105
43
|
* });
|
|
106
44
|
* ```
|
|
107
45
|
*/
|
|
108
|
-
export declare function createBodyValidationMiddleware(schema:
|
|
46
|
+
export declare function createBodyValidationMiddleware(schema: MiddlewareRequestSchema): MiddlewareDefinition;
|
|
109
47
|
/**
|
|
110
48
|
* Create query-only validation middleware
|
|
111
49
|
*
|
|
@@ -119,28 +57,12 @@ export declare function createBodyValidationMiddleware(schema: ValidationSchema)
|
|
|
119
57
|
* });
|
|
120
58
|
* ```
|
|
121
59
|
*/
|
|
122
|
-
export declare function createQueryValidationMiddleware(schema:
|
|
60
|
+
export declare function createQueryValidationMiddleware(schema: MiddlewareRequestSchema): MiddlewareDefinition;
|
|
123
61
|
/**
|
|
124
62
|
* Create a combined validation middleware with full config support
|
|
125
63
|
* Alias for createRequestValidationMiddleware for compatibility
|
|
126
64
|
*/
|
|
127
65
|
export declare const createValidationMiddleware: typeof createRequestValidationMiddleware;
|
|
128
|
-
/**
|
|
129
|
-
* Extended property schema for common schemas
|
|
130
|
-
*/
|
|
131
|
-
type ExtendedPropertySchema = PropertySchema & {
|
|
132
|
-
format?: string;
|
|
133
|
-
};
|
|
134
|
-
/**
|
|
135
|
-
* Extended validation schema for common schemas
|
|
136
|
-
*/
|
|
137
|
-
type ExtendedValidationSchema = {
|
|
138
|
-
type?: string;
|
|
139
|
-
format?: string;
|
|
140
|
-
required?: string[];
|
|
141
|
-
properties?: Record<string, ExtendedPropertySchema>;
|
|
142
|
-
additionalProperties?: boolean;
|
|
143
|
-
};
|
|
144
66
|
/**
|
|
145
67
|
* Common validation schemas for reuse
|
|
146
68
|
*/
|
|
@@ -287,6 +287,9 @@ export function createQueryValidationMiddleware(schema) {
|
|
|
287
287
|
* Alias for createRequestValidationMiddleware for compatibility
|
|
288
288
|
*/
|
|
289
289
|
export const createValidationMiddleware = createRequestValidationMiddleware;
|
|
290
|
+
// ============================================
|
|
291
|
+
// Common Schemas
|
|
292
|
+
// ============================================
|
|
290
293
|
/**
|
|
291
294
|
* Common validation schemas for reuse
|
|
292
295
|
*/
|
|
@@ -2,52 +2,7 @@
|
|
|
2
2
|
* OpenAPI 3.1 Specification Generator
|
|
3
3
|
* Generates OpenAPI documentation from NeuroLink server routes
|
|
4
4
|
*/
|
|
5
|
-
import type {
|
|
6
|
-
/**
|
|
7
|
-
* OpenAPI generator configuration
|
|
8
|
-
*/
|
|
9
|
-
type OpenAPIGeneratorConfig = {
|
|
10
|
-
/** API info override */
|
|
11
|
-
info?: {
|
|
12
|
-
title?: string;
|
|
13
|
-
version?: string;
|
|
14
|
-
description?: string;
|
|
15
|
-
};
|
|
16
|
-
/** Server configuration */
|
|
17
|
-
servers?: Array<{
|
|
18
|
-
url: string;
|
|
19
|
-
description?: string;
|
|
20
|
-
}>;
|
|
21
|
-
/** Include security schemes */
|
|
22
|
-
includeSecurity?: boolean;
|
|
23
|
-
/** Base path for all routes */
|
|
24
|
-
basePath?: string;
|
|
25
|
-
/** Additional tags */
|
|
26
|
-
additionalTags?: Array<{
|
|
27
|
-
name: string;
|
|
28
|
-
description: string;
|
|
29
|
-
}>;
|
|
30
|
-
/** Custom schemas to add */
|
|
31
|
-
customSchemas?: Record<string, JsonObject>;
|
|
32
|
-
/** Routes to document in the OpenAPI spec */
|
|
33
|
-
routes?: RouteDefinition[];
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Generated OpenAPI specification
|
|
37
|
-
*/
|
|
38
|
-
type OpenAPISpec = {
|
|
39
|
-
openapi: "3.1.0";
|
|
40
|
-
info: JsonObject;
|
|
41
|
-
servers: JsonObject[];
|
|
42
|
-
tags: JsonObject[];
|
|
43
|
-
paths: Record<string, JsonObject>;
|
|
44
|
-
components: {
|
|
45
|
-
schemas: Record<string, JsonObject>;
|
|
46
|
-
securitySchemes?: Record<string, JsonObject>;
|
|
47
|
-
parameters?: Record<string, JsonObject>;
|
|
48
|
-
};
|
|
49
|
-
security?: JsonObject[];
|
|
50
|
-
};
|
|
5
|
+
import type { OpenAPIGeneratorConfig, OpenAPISpec, RouteDefinition, ServerAdapterConfig } from "../../types/index.js";
|
|
51
6
|
/**
|
|
52
7
|
* OpenAPI specification generator
|
|
53
8
|
* Generates OpenAPI 3.1 compliant documentation from route definitions
|
|
@@ -104,4 +59,3 @@ export declare function generateOpenAPISpec(routes: RouteDefinition[], config?:
|
|
|
104
59
|
* Generate OpenAPI spec from server adapter configuration
|
|
105
60
|
*/
|
|
106
61
|
export declare function generateOpenAPIFromConfig(serverConfig: ServerAdapterConfig, routes?: RouteDefinition[]): OpenAPISpec;
|
|
107
|
-
export {};
|
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
* Agent Routes
|
|
3
3
|
* Endpoints for agent execution and streaming
|
|
4
4
|
*/
|
|
5
|
+
import { SpanStatusCode } from "@opentelemetry/api";
|
|
5
6
|
import { ProviderFactory } from "../../factories/providerFactory.js";
|
|
7
|
+
import { withSpan } from "../../telemetry/withSpan.js";
|
|
8
|
+
import { tracers } from "../../telemetry/tracers.js";
|
|
6
9
|
import { createStreamRedactor } from "../utils/redaction.js";
|
|
7
10
|
import { AgentExecuteRequestSchema, createErrorResponse as createError, EmbedManyRequestSchema, EmbedRequestSchema, validateRequest, } from "../utils/validation.js";
|
|
8
11
|
/**
|
|
@@ -22,43 +25,53 @@ export function createAgentRoutes(basePath = "/api") {
|
|
|
22
25
|
return validation.error;
|
|
23
26
|
}
|
|
24
27
|
const request = validation.data;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
:
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
model: request.model,
|
|
33
|
-
systemPrompt: request.systemPrompt,
|
|
34
|
-
temperature: request.temperature,
|
|
35
|
-
maxTokens: request.maxTokens,
|
|
36
|
-
// Note: tools should be passed as Record<string, Tool> in generate options
|
|
37
|
-
// If request.tools is an array of tool names, we skip them
|
|
38
|
-
context: {
|
|
39
|
-
// When an authenticated user context exists (set by auth middleware),
|
|
40
|
-
// always use its IDs to prevent caller-supplied impersonation.
|
|
41
|
-
sessionId: ctx.user
|
|
42
|
-
? ctx.session?.id
|
|
43
|
-
: (ctx.session?.id ?? request.sessionId),
|
|
44
|
-
userId: ctx.user ? ctx.user.id : request.userId,
|
|
45
|
-
userEmail: ctx.user?.email,
|
|
46
|
-
userRoles: ctx.user?.roles,
|
|
47
|
-
requestId: ctx.requestId,
|
|
28
|
+
return withSpan({
|
|
29
|
+
name: "neurolink.http.execute",
|
|
30
|
+
tracer: tracers.http,
|
|
31
|
+
attributes: {
|
|
32
|
+
"http.route": "/api/agent/execute",
|
|
33
|
+
"ai.provider": request.provider || "default",
|
|
34
|
+
"ai.model": request.model || "default",
|
|
48
35
|
},
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
36
|
+
}, async () => {
|
|
37
|
+
// Normalize input
|
|
38
|
+
const input = typeof request.input === "string"
|
|
39
|
+
? { text: request.input }
|
|
40
|
+
: request.input;
|
|
41
|
+
const result = await ctx.neurolink.generate({
|
|
42
|
+
input,
|
|
43
|
+
provider: request.provider,
|
|
44
|
+
model: request.model,
|
|
45
|
+
systemPrompt: request.systemPrompt,
|
|
46
|
+
temperature: request.temperature,
|
|
47
|
+
maxTokens: request.maxTokens,
|
|
48
|
+
// Note: tools should be passed as Record<string, Tool> in generate options
|
|
49
|
+
// If request.tools is an array of tool names, we skip them
|
|
50
|
+
context: {
|
|
51
|
+
// When an authenticated user context exists (set by auth middleware),
|
|
52
|
+
// always use its IDs to prevent caller-supplied impersonation.
|
|
53
|
+
sessionId: ctx.user
|
|
54
|
+
? ctx.session?.id
|
|
55
|
+
: (ctx.session?.id ?? request.sessionId),
|
|
56
|
+
userId: ctx.user ? ctx.user.id : request.userId,
|
|
57
|
+
userEmail: ctx.user?.email,
|
|
58
|
+
userRoles: ctx.user?.roles,
|
|
59
|
+
requestId: ctx.requestId,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
// Map tool calls from SDK format to API format
|
|
63
|
+
const toolCalls = result.toolCalls?.map((tc) => ({
|
|
64
|
+
name: tc.toolName,
|
|
65
|
+
arguments: tc.args,
|
|
66
|
+
}));
|
|
67
|
+
return {
|
|
68
|
+
content: result.content || "",
|
|
69
|
+
provider: result.provider || request.provider || "unknown",
|
|
70
|
+
model: result.model || request.model || "unknown",
|
|
71
|
+
usage: result.usage,
|
|
72
|
+
toolCalls,
|
|
73
|
+
};
|
|
74
|
+
}); // end withSpan
|
|
62
75
|
},
|
|
63
76
|
description: "Execute agent with prompt",
|
|
64
77
|
tags: ["agent"],
|
|
@@ -99,11 +112,32 @@ export function createAgentRoutes(basePath = "/api") {
|
|
|
99
112
|
});
|
|
100
113
|
// Create redactor (no-op if redaction is not enabled)
|
|
101
114
|
const redactor = createStreamRedactor(ctx.redaction);
|
|
102
|
-
// Wrap stream
|
|
115
|
+
// Wrap stream with a span that stays open for the full consumption
|
|
116
|
+
// lifetime, not just the generator creation.
|
|
103
117
|
async function* redactedStream() {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
118
|
+
const streamSpan = tracers.http.startSpan("neurolink.http.stream", {
|
|
119
|
+
attributes: {
|
|
120
|
+
"http.route": "/api/agent/stream",
|
|
121
|
+
"ai.provider": request.provider || "default",
|
|
122
|
+
"ai.model": request.model || "default",
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
try {
|
|
126
|
+
for await (const chunk of result.stream) {
|
|
127
|
+
yield redactor(chunk);
|
|
128
|
+
}
|
|
129
|
+
streamSpan.setStatus({ code: SpanStatusCode.OK });
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
streamSpan.recordException(err instanceof Error ? err : new Error(String(err)));
|
|
133
|
+
streamSpan.setStatus({
|
|
134
|
+
code: SpanStatusCode.ERROR,
|
|
135
|
+
message: err instanceof Error ? err.message : String(err),
|
|
136
|
+
});
|
|
137
|
+
throw err;
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
streamSpan.end();
|
|
107
141
|
}
|
|
108
142
|
}
|
|
109
143
|
return redactedStream();
|
|
@@ -136,14 +170,24 @@ export function createAgentRoutes(basePath = "/api") {
|
|
|
136
170
|
const request = validation.data;
|
|
137
171
|
try {
|
|
138
172
|
const providerName = request.provider || "openai";
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
173
|
+
return await withSpan({
|
|
174
|
+
name: "neurolink.http.embed",
|
|
175
|
+
tracer: tracers.http,
|
|
176
|
+
attributes: {
|
|
177
|
+
"http.route": "/api/agent/embed",
|
|
178
|
+
"ai.provider": providerName,
|
|
179
|
+
"ai.model": request.model || "default",
|
|
180
|
+
},
|
|
181
|
+
}, async () => {
|
|
182
|
+
const provider = await ProviderFactory.createProvider(providerName, request.model);
|
|
183
|
+
const embedding = await provider.embed(request.text, request.model);
|
|
184
|
+
return {
|
|
185
|
+
embedding,
|
|
186
|
+
provider: providerName,
|
|
187
|
+
model: request.model || "default",
|
|
188
|
+
dimension: embedding.length,
|
|
189
|
+
};
|
|
190
|
+
});
|
|
147
191
|
}
|
|
148
192
|
catch (error) {
|
|
149
193
|
return createError("EXECUTION_FAILED", error instanceof Error
|
|
@@ -165,15 +209,26 @@ export function createAgentRoutes(basePath = "/api") {
|
|
|
165
209
|
const request = validation.data;
|
|
166
210
|
try {
|
|
167
211
|
const providerName = request.provider || "openai";
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
212
|
+
return await withSpan({
|
|
213
|
+
name: "neurolink.http.embedMany",
|
|
214
|
+
tracer: tracers.http,
|
|
215
|
+
attributes: {
|
|
216
|
+
"http.route": "/api/agent/embed-many",
|
|
217
|
+
"ai.provider": providerName,
|
|
218
|
+
"ai.model": request.model || "default",
|
|
219
|
+
"ai.embed.count": request.texts.length,
|
|
220
|
+
},
|
|
221
|
+
}, async () => {
|
|
222
|
+
const provider = await ProviderFactory.createProvider(providerName, request.model);
|
|
223
|
+
const embeddings = await provider.embedMany(request.texts, request.model);
|
|
224
|
+
return {
|
|
225
|
+
embeddings,
|
|
226
|
+
provider: providerName,
|
|
227
|
+
model: request.model || "default",
|
|
228
|
+
count: embeddings.length,
|
|
229
|
+
dimension: embeddings[0]?.length ?? 0,
|
|
230
|
+
};
|
|
231
|
+
});
|
|
177
232
|
}
|
|
178
233
|
catch (error) {
|
|
179
234
|
return createError("EXECUTION_FAILED", error instanceof Error
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* Without a router, models are passed through to the Anthropic provider.
|
|
11
11
|
*/
|
|
12
12
|
import type { ModelRouter } from "../../proxy/modelRouter.js";
|
|
13
|
-
import type { ParsedClaudeRequest, RouteGroup } from "../../types/index.js";
|
|
13
|
+
import type { ParsedClaudeError, ParsedClaudeRequest, RouteGroup } from "../../types/index.js";
|
|
14
14
|
/**
|
|
15
15
|
* Create Claude-compatible proxy routes.
|
|
16
16
|
*
|
|
@@ -23,10 +23,6 @@ import type { ParsedClaudeRequest, RouteGroup } from "../../types/index.js";
|
|
|
23
23
|
*/
|
|
24
24
|
export declare function createClaudeProxyRoutes(modelRouter?: ModelRouter, basePath?: string, accountStrategy?: "round-robin" | "fill-first", passthroughMode?: boolean): RouteGroup;
|
|
25
25
|
export declare function getTransientSameAccountRetryDelayMs(retryNumber: number): number;
|
|
26
|
-
type ParsedClaudeError = {
|
|
27
|
-
errorType?: string;
|
|
28
|
-
message?: string;
|
|
29
|
-
};
|
|
30
26
|
/**
|
|
31
27
|
* Parse a Claude error payload when available.
|
|
32
28
|
*/
|
|
@@ -46,4 +42,3 @@ export declare function buildProxyFallbackOptions(parsed: ParsedClaudeRequest, o
|
|
|
46
42
|
* carry transient HTML responses (e.g. 520 pages) inside `error.message`.
|
|
47
43
|
*/
|
|
48
44
|
export declare function isTransientHttpFailure(status: number, errBody: string): boolean;
|
|
49
|
-
export {};
|
|
@@ -15,6 +15,8 @@ import { join } from "node:path";
|
|
|
15
15
|
import { buildStableClaudeCodeBillingHeader, CLAUDE_CLI_USER_AGENT, CLAUDE_CODE_OAUTH_BETAS, getOrCreateClaudeCodeIdentity, parseClaudeCodeUserId, } from "../../auth/anthropicOAuth.js";
|
|
16
16
|
import { parseQuotaHeaders, saveAccountQuota, } from "../../proxy/accountQuota.js";
|
|
17
17
|
import { buildClaudeError, ClaudeStreamSerializer, generateToolUseId, parseClaudeRequest, serializeClaudeResponse, } from "../../proxy/claudeFormat.js";
|
|
18
|
+
import { tracers } from "../../telemetry/tracers.js";
|
|
19
|
+
import { withSpan } from "../../telemetry/withSpan.js";
|
|
18
20
|
import { ProxyTracer, recordFallbackAttempt } from "../../proxy/proxyTracer.js";
|
|
19
21
|
import { createRawStreamCapture } from "../../proxy/rawStreamCapture.js";
|
|
20
22
|
import { logBodyCapture, logRequest, logRequestAttempt, logStreamError, } from "../../proxy/requestLogger.js";
|
|
@@ -51,7 +53,7 @@ const MAX_TRANSIENT_SAME_ACCOUNT_RETRIES = 2;
|
|
|
51
53
|
const TRANSIENT_SAME_ACCOUNT_RETRY_DELAYS_MS = [250, 1_000];
|
|
52
54
|
/** Maximum upstream 429 attempts per account before rotating to the next account.
|
|
53
55
|
* Total attempts per account = this + 1 (the initial call plus this many retries). */
|
|
54
|
-
const MAX_RATE_LIMIT_SAME_ACCOUNT_RETRIES =
|
|
56
|
+
const MAX_RATE_LIMIT_SAME_ACCOUNT_RETRIES = 10;
|
|
55
57
|
/** Max time to sleep between 429 retries. Caps large upstream retry-after values
|
|
56
58
|
* so we don't hold the client connection open for minutes. */
|
|
57
59
|
const MAX_RATE_LIMIT_RETRY_DELAY_MS = 30_000;
|
|
@@ -62,6 +64,10 @@ const UPSTREAM_FETCH_TIMEOUT_MS = 15 * 60 * 1000; // 15 minutes
|
|
|
62
64
|
const accountRuntimeState = new Map();
|
|
63
65
|
/** Track whether we've run the one-time startup prune. */
|
|
64
66
|
let startupPruneDone = false;
|
|
67
|
+
/** Default cooling period when retries are exhausted and upstream didn't
|
|
68
|
+
* provide a retry-after header. Short enough to recover quickly, long
|
|
69
|
+
* enough to avoid immediately hammering the same account. */
|
|
70
|
+
const DEFAULT_COOLING_PERIOD_MS = 60_000;
|
|
65
71
|
/** Advance the primary account index when the current primary is exhausted
|
|
66
72
|
* (429 retries exhausted or auth failure). This is what makes fill-first work:
|
|
67
73
|
* we stick to one account until it's unusable. Only advances when the exhausted
|
|
@@ -76,6 +82,33 @@ function advancePrimaryIfCurrent(accountKey, enabledCount, primaryAccountKey) {
|
|
|
76
82
|
}
|
|
77
83
|
primaryAccountIndex = (primaryAccountIndex + 1) % enabledCount;
|
|
78
84
|
}
|
|
85
|
+
/** If the configured home primary (index 0) is no longer cooling, reset
|
|
86
|
+
* primaryAccountIndex back to 0 so traffic returns to the preferred account
|
|
87
|
+
* once its rate limit window expires. Called at the start of each request. */
|
|
88
|
+
function maybeResetPrimaryToHome(enabledAccounts) {
|
|
89
|
+
if (enabledAccounts.length <= 1 || primaryAccountIndex === 0) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const homeState = accountRuntimeState.get(enabledAccounts[0].key);
|
|
93
|
+
if (!homeState ||
|
|
94
|
+
!homeState.coolingUntil ||
|
|
95
|
+
Date.now() >= homeState.coolingUntil) {
|
|
96
|
+
// Home account is no longer cooling — reset to it
|
|
97
|
+
primaryAccountIndex = 0;
|
|
98
|
+
if (homeState?.coolingUntil) {
|
|
99
|
+
homeState.coolingUntil = undefined;
|
|
100
|
+
logger.always(`[proxy] home primary account=${enabledAccounts[0].label} cooling expired, resetting primaryAccountIndex to 0`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** Check if an account is currently in its cooling window. */
|
|
105
|
+
function isAccountCooling(accountKey) {
|
|
106
|
+
const state = accountRuntimeState.get(accountKey);
|
|
107
|
+
return !!state?.coolingUntil && Date.now() < state.coolingUntil;
|
|
108
|
+
}
|
|
109
|
+
// ---------------------------------------------------------------------------
|
|
110
|
+
// OAuth polyfill helpers (extracted to reduce block nesting)
|
|
111
|
+
// ---------------------------------------------------------------------------
|
|
79
112
|
const snapshotCache = new Map();
|
|
80
113
|
const SNAPSHOT_CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
|
81
114
|
const SNAPSHOT_STABLE_HEADERS = new Set([
|
|
@@ -2204,6 +2237,32 @@ async function handleAnthropicAuthRetry(args) {
|
|
|
2204
2237
|
}
|
|
2205
2238
|
const retryStatus = retryResp.status;
|
|
2206
2239
|
const retryBody = await retryResp.text();
|
|
2240
|
+
// Capture full response headers and body for all auth-retry errors.
|
|
2241
|
+
// Redact sensitive headers and cap body size before persisting.
|
|
2242
|
+
const retryRespHeaders = {};
|
|
2243
|
+
retryResp.headers.forEach((value, key) => {
|
|
2244
|
+
retryRespHeaders[key] = value;
|
|
2245
|
+
});
|
|
2246
|
+
const safeRetryHeaders = { ...retryRespHeaders };
|
|
2247
|
+
delete safeRetryHeaders["authorization"];
|
|
2248
|
+
delete safeRetryHeaders["x-api-key"];
|
|
2249
|
+
const cappedRetryBody = retryBody.length > 4000
|
|
2250
|
+
? retryBody.slice(0, 4000) + "...[truncated]"
|
|
2251
|
+
: retryBody;
|
|
2252
|
+
tracer?.logUpstreamResponseHeaders(safeRetryHeaders);
|
|
2253
|
+
tracer?.logUpstreamResponseBody(cappedRetryBody);
|
|
2254
|
+
logProxyBody({
|
|
2255
|
+
phase: "upstream_response",
|
|
2256
|
+
headers: safeRetryHeaders,
|
|
2257
|
+
body: cappedRetryBody,
|
|
2258
|
+
bodySize: Buffer.byteLength(retryBody, "utf8"),
|
|
2259
|
+
contentType: retryRespHeaders["content-type"] ?? "application/json",
|
|
2260
|
+
account: account.label,
|
|
2261
|
+
accountType: account.type,
|
|
2262
|
+
attempt: attemptNumber,
|
|
2263
|
+
responseStatus: retryStatus,
|
|
2264
|
+
durationMs: Date.now() - fetchStartMs,
|
|
2265
|
+
});
|
|
2207
2266
|
authRetryError = `retry ${authRetry + 1}/${MAX_AUTH_RETRIES} failed with status ${retryStatus}`;
|
|
2208
2267
|
currentLastError = retryBody;
|
|
2209
2268
|
logger.debug(`[proxy] retry ${authRetry + 1} failed: ${retryStatus} ${retryBody.substring(0, 120)}`);
|
|
@@ -2775,7 +2834,7 @@ async function prepareAnthropicAccountAttempt(args) {
|
|
|
2775
2834
|
};
|
|
2776
2835
|
}
|
|
2777
2836
|
async function fetchAnthropicAccountResponse(args) {
|
|
2778
|
-
const { url, headers, finalBodyStr, account, accountState: _accountState2, enabledAccounts: _enabledAccounts, orderedAccounts: _orderedAccounts, tracer, logAttempt, currentLastError, currentSawRateLimit, currentSawNetworkError, upstreamSpan, } = args;
|
|
2837
|
+
const { url, headers, finalBodyStr, account, accountState: _accountState2, enabledAccounts: _enabledAccounts, orderedAccounts: _orderedAccounts, tracer, logAttempt, logProxyBody, fetchStartMs, attemptNumber, currentLastError, currentSawRateLimit, currentSawNetworkError, upstreamSpan, } = args;
|
|
2779
2838
|
let lastError = currentLastError;
|
|
2780
2839
|
let sawRateLimit = currentSawRateLimit;
|
|
2781
2840
|
let sawNetworkError = currentSawNetworkError;
|
|
@@ -2816,8 +2875,35 @@ async function fetchAnthropicAccountResponse(args) {
|
|
|
2816
2875
|
sawRateLimit = true;
|
|
2817
2876
|
const retryAfterMs = parseRetryAfterMs(response.headers.get("retry-after"));
|
|
2818
2877
|
recordAttemptError(account.label, account.type, 429);
|
|
2878
|
+
// Capture full response headers and body for diagnostics (parity with
|
|
2879
|
+
// handleAnthropicNonOkResponse which does this for all other error statuses).
|
|
2880
|
+
const errRespHeaders = {};
|
|
2881
|
+
response.headers.forEach((value, key) => {
|
|
2882
|
+
errRespHeaders[key] = value;
|
|
2883
|
+
});
|
|
2819
2884
|
lastError = await response.text();
|
|
2820
|
-
|
|
2885
|
+
// Redact sensitive headers and cap body before persisting
|
|
2886
|
+
const safe429Headers = { ...errRespHeaders };
|
|
2887
|
+
delete safe429Headers["authorization"];
|
|
2888
|
+
delete safe429Headers["x-api-key"];
|
|
2889
|
+
const capped429Body = String(lastError).length > 4000
|
|
2890
|
+
? String(lastError).slice(0, 4000) + "...[truncated]"
|
|
2891
|
+
: String(lastError);
|
|
2892
|
+
tracer?.logUpstreamResponseHeaders(safe429Headers);
|
|
2893
|
+
tracer?.logUpstreamResponseBody(capped429Body);
|
|
2894
|
+
logProxyBody({
|
|
2895
|
+
phase: "upstream_response",
|
|
2896
|
+
headers: safe429Headers,
|
|
2897
|
+
body: capped429Body,
|
|
2898
|
+
bodySize: Buffer.byteLength(String(lastError), "utf8"),
|
|
2899
|
+
contentType: errRespHeaders["content-type"] ?? "application/json",
|
|
2900
|
+
account: account.label,
|
|
2901
|
+
accountType: account.type,
|
|
2902
|
+
attempt: attemptNumber,
|
|
2903
|
+
responseStatus: 429,
|
|
2904
|
+
durationMs: Date.now() - fetchStartMs,
|
|
2905
|
+
});
|
|
2906
|
+
logger.always(`[proxy] ← 429 account=${account.label} retry-after=${retryAfterMs}ms (upstream) ratelimit-status=${errRespHeaders["anthropic-ratelimit-unified-status"] ?? "unknown"}`);
|
|
2821
2907
|
logAttempt(429, "rate_limit_error", String(lastError));
|
|
2822
2908
|
tracer?.setError("rate_limit_error", String(lastError).slice(0, 500));
|
|
2823
2909
|
tracer?.recordRetry(account.label, "rate_limit");
|
|
@@ -2866,9 +2952,13 @@ async function handleAnthropicRoutedClaudeRequest(args) {
|
|
|
2866
2952
|
attemptNumber: 0,
|
|
2867
2953
|
};
|
|
2868
2954
|
const acctSelectionSpan = tracer?.startAccountSelection();
|
|
2869
|
-
//
|
|
2870
|
-
|
|
2871
|
-
|
|
2955
|
+
// Try to return to the home primary account if its cooling has expired.
|
|
2956
|
+
maybeResetPrimaryToHome(enabledAccounts);
|
|
2957
|
+
// Skip accounts that are still cooling from a recent 429-exhaustion,
|
|
2958
|
+
// but keep them as last-resort if ALL accounts are cooling.
|
|
2959
|
+
const nonCoolingAccounts = orderedAccounts.filter((a) => !isAccountCooling(a.key));
|
|
2960
|
+
const effectiveAccounts = nonCoolingAccounts.length > 0 ? nonCoolingAccounts : orderedAccounts;
|
|
2961
|
+
accountLoop: for (const account of effectiveAccounts) {
|
|
2872
2962
|
const accountState = getOrCreateRuntimeState(account.key);
|
|
2873
2963
|
let transientSameAccountRetries = 0;
|
|
2874
2964
|
let rateLimitSameAccountRetries = 0;
|
|
@@ -2926,6 +3016,9 @@ async function handleAnthropicRoutedClaudeRequest(args) {
|
|
|
2926
3016
|
orderedAccounts,
|
|
2927
3017
|
tracer,
|
|
2928
3018
|
logAttempt,
|
|
3019
|
+
logProxyBody,
|
|
3020
|
+
fetchStartMs: preparedAttempt.fetchStartMs,
|
|
3021
|
+
attemptNumber: loopState.attemptNumber,
|
|
2929
3022
|
currentLastError: loopState.lastError,
|
|
2930
3023
|
currentSawRateLimit: loopState.sawRateLimit,
|
|
2931
3024
|
currentSawNetworkError: loopState.sawNetworkError,
|
|
@@ -2948,8 +3041,11 @@ async function handleAnthropicRoutedClaudeRequest(args) {
|
|
|
2948
3041
|
// Rate-limit retries exhausted for this account — rotate
|
|
2949
3042
|
if (fetchResult.retrySameAccount &&
|
|
2950
3043
|
fetchResult.retryAfterMs !== undefined) {
|
|
3044
|
+
// Mark account as cooling so subsequent requests don't hammer it
|
|
3045
|
+
const coolingMs = Math.min(fetchResult.retryAfterMs || DEFAULT_COOLING_PERIOD_MS, DEFAULT_COOLING_PERIOD_MS);
|
|
3046
|
+
accountState.coolingUntil = Date.now() + coolingMs;
|
|
2951
3047
|
advancePrimaryIfCurrent(account.key, enabledAccounts.length, orderedAccounts[0]?.key);
|
|
2952
|
-
logger.always(`[proxy] exhausted ${MAX_RATE_LIMIT_SAME_ACCOUNT_RETRIES} rate-limit retries for account=${account.label}; rotating`);
|
|
3048
|
+
logger.always(`[proxy] exhausted ${MAX_RATE_LIMIT_SAME_ACCOUNT_RETRIES} rate-limit retries for account=${account.label}; cooling for ${coolingMs}ms, rotating`);
|
|
2953
3049
|
continue accountLoop;
|
|
2954
3050
|
}
|
|
2955
3051
|
// Transient error retry (network errors, 529 overloaded)
|
|
@@ -3052,6 +3148,10 @@ async function handleAnthropicRoutedClaudeRequest(args) {
|
|
|
3052
3148
|
}
|
|
3053
3149
|
break accountLoop;
|
|
3054
3150
|
}
|
|
3151
|
+
// Clear cooling on success — account is healthy again
|
|
3152
|
+
if (accountState.coolingUntil) {
|
|
3153
|
+
accountState.coolingUntil = undefined;
|
|
3154
|
+
}
|
|
3055
3155
|
const successResult = await handleAnthropicSuccessfulResponse({
|
|
3056
3156
|
ctx,
|
|
3057
3157
|
body,
|
|
@@ -3232,7 +3332,11 @@ export function createClaudeProxyRoutes(modelRouter, basePath = "", accountStrat
|
|
|
3232
3332
|
{
|
|
3233
3333
|
method: "GET",
|
|
3234
3334
|
path: `${basePath}/v1/models`,
|
|
3235
|
-
handler: async (_ctx) => {
|
|
3335
|
+
handler: async (_ctx) => withSpan({
|
|
3336
|
+
name: "neurolink.http.claudeProxy.listModels",
|
|
3337
|
+
tracer: tracers.http,
|
|
3338
|
+
attributes: { "http.route": `${basePath}/v1/models` },
|
|
3339
|
+
}, async () => {
|
|
3236
3340
|
const models = [
|
|
3237
3341
|
"claude-sonnet-4-20250514",
|
|
3238
3342
|
"claude-sonnet-4-5-20250929",
|
|
@@ -3248,7 +3352,7 @@ export function createClaudeProxyRoutes(modelRouter, basePath = "", accountStrat
|
|
|
3248
3352
|
owned_by: "anthropic",
|
|
3249
3353
|
})),
|
|
3250
3354
|
};
|
|
3251
|
-
},
|
|
3355
|
+
}),
|
|
3252
3356
|
description: "List available Claude models",
|
|
3253
3357
|
tags: ["claude-proxy", "models"],
|
|
3254
3358
|
},
|
|
@@ -3258,9 +3362,16 @@ export function createClaudeProxyRoutes(modelRouter, basePath = "", accountStrat
|
|
|
3258
3362
|
{
|
|
3259
3363
|
method: "POST",
|
|
3260
3364
|
path: `${basePath}/v1/messages/count_tokens`,
|
|
3261
|
-
handler: async (ctx) => {
|
|
3365
|
+
handler: async (ctx) => withSpan({
|
|
3366
|
+
name: "neurolink.http.claudeProxy.countTokens",
|
|
3367
|
+
tracer: tracers.http,
|
|
3368
|
+
attributes: {
|
|
3369
|
+
"http.route": `${basePath}/v1/messages/count_tokens`,
|
|
3370
|
+
},
|
|
3371
|
+
}, async (span) => {
|
|
3262
3372
|
const body = ctx.body;
|
|
3263
|
-
if (
|
|
3373
|
+
if (typeof body?.model !== "string" ||
|
|
3374
|
+
!Array.isArray(body?.messages)) {
|
|
3264
3375
|
return buildClaudeError(400, "Missing required fields: model, messages");
|
|
3265
3376
|
}
|
|
3266
3377
|
// Simple estimation using character-to-token heuristic
|
|
@@ -3269,8 +3380,11 @@ export function createClaudeProxyRoutes(modelRouter, basePath = "", accountStrat
|
|
|
3269
3380
|
? m.content
|
|
3270
3381
|
: JSON.stringify(m.content))
|
|
3271
3382
|
.join(" ");
|
|
3272
|
-
|
|
3273
|
-
|
|
3383
|
+
const inputTokens = Math.ceil(text.length / 4);
|
|
3384
|
+
span.setAttribute("ai.model", body.model);
|
|
3385
|
+
span.setAttribute("gen_ai.usage.input_tokens", inputTokens);
|
|
3386
|
+
return { input_tokens: inputTokens };
|
|
3387
|
+
}),
|
|
3274
3388
|
description: "Count tokens for a messages request",
|
|
3275
3389
|
tags: ["claude-proxy", "tokens"],
|
|
3276
3390
|
},
|