@juspay/neurolink 9.32.0 → 9.32.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/auth/anthropicOAuth.js +1 -1
- package/dist/cli/commands/proxy.js +18 -5
- package/dist/client/aiSdkAdapter.js +1 -1
- package/dist/client/index.js +137 -501
- package/dist/core/factory.js +0 -1
- package/dist/core/redisConversationMemoryManager.js +1 -1
- package/dist/features/ppt/slideGenerator.js +0 -1
- package/dist/features/ppt/utils.js +0 -1
- package/dist/lib/server/routes/claudeProxyRoutes.js +45 -9
- package/dist/mcp/elicitationProtocol.js +1 -1
- package/dist/mcp/servers/agent/directToolsServer.js +0 -1
- package/dist/providers/azureOpenai.js +1 -1
- package/dist/providers/huggingFace.js +0 -1
- package/dist/providers/openaiCompatible.js +0 -1
- package/dist/sdk/toolRegistration.js +0 -1
- package/dist/server/openapi/generator.js +1 -1
- package/dist/server/routes/claudeProxyRoutes.js +45 -9
- package/dist/types/configTypes.js +0 -5
- package/dist/types/modelTypes.js +0 -1
- package/dist/types/tools.js +0 -1
- package/dist/types/typeAliases.js +0 -1
- package/dist/types/utilities.js +1 -1
- package/dist/types/workflowTypes.js +0 -1
- package/dist/utils/providerRetry.js +0 -1
- package/dist/utils/providerUtils.js +0 -1
- package/package.json +2 -2
- package/dist/client/adapters/providerImageAdapter.js +0 -588
- package/dist/client/adapters/tts/googleTTSHandler.js +0 -344
- package/dist/client/adapters/video/directorPipeline.js +0 -516
- package/dist/client/adapters/video/ffmpegAdapter.js +0 -206
- package/dist/client/adapters/video/frameExtractor.js +0 -143
- package/dist/client/adapters/video/vertexVideoHandler.js +0 -763
- package/dist/client/adapters/video/videoAnalyzer.js +0 -238
- package/dist/client/adapters/video/videoMerger.js +0 -171
- package/dist/client/agent/directTools.js +0 -840
- package/dist/client/auth/AuthProviderFactory.js +0 -111
- package/dist/client/auth/AuthProviderRegistry.js +0 -190
- package/dist/client/auth/RequestContext.js +0 -78
- package/dist/client/auth/accountPool.js +0 -178
- package/dist/client/auth/anthropicOAuth.js +0 -974
- package/dist/client/auth/authContext.js +0 -314
- package/dist/client/auth/errors.js +0 -39
- package/dist/client/auth/index.js +0 -61
- package/dist/client/auth/middleware/AuthMiddleware.js +0 -519
- package/dist/client/auth/middleware/rateLimitByUser.js +0 -554
- package/dist/client/auth/providers/BaseAuthProvider.js +0 -723
- package/dist/client/auth/providers/CognitoProvider.js +0 -304
- package/dist/client/auth/providers/KeycloakProvider.js +0 -393
- package/dist/client/auth/providers/auth0.js +0 -274
- package/dist/client/auth/providers/betterAuth.js +0 -182
- package/dist/client/auth/providers/clerk.js +0 -317
- package/dist/client/auth/providers/custom.js +0 -112
- package/dist/client/auth/providers/firebase.js +0 -226
- package/dist/client/auth/providers/jwt.js +0 -212
- package/dist/client/auth/providers/oauth2.js +0 -303
- package/dist/client/auth/providers/supabase.js +0 -259
- package/dist/client/auth/providers/workos.js +0 -284
- package/dist/client/auth/serverBridge.js +0 -25
- package/dist/client/auth/sessionManager.js +0 -437
- package/dist/client/auth/tokenStore.js +0 -799
- package/dist/client/client/aiSdkAdapter.js +0 -487
- package/dist/client/client/auth.js +0 -473
- package/dist/client/client/errors.js +0 -552
- package/dist/client/client/httpClient.js +0 -837
- package/dist/client/client/index.js +0 -172
- package/dist/client/client/interceptors.js +0 -601
- package/dist/client/client/sseClient.js +0 -545
- package/dist/client/client/streamingClient.js +0 -917
- package/dist/client/client/wsClient.js +0 -369
- package/dist/client/config/configManager.js +0 -303
- package/dist/client/config/conversationMemory.js +0 -86
- package/dist/client/config/taskClassificationConfig.js +0 -148
- package/dist/client/constants/contextWindows.js +0 -295
- package/dist/client/constants/enums.js +0 -853
- package/dist/client/constants/index.js +0 -207
- package/dist/client/constants/performance.js +0 -389
- package/dist/client/constants/retry.js +0 -266
- package/dist/client/constants/timeouts.js +0 -182
- package/dist/client/constants/tokens.js +0 -380
- package/dist/client/constants/videoErrors.js +0 -46
- package/dist/client/context/budgetChecker.js +0 -98
- package/dist/client/context/contextCompactor.js +0 -205
- package/dist/client/context/emergencyTruncation.js +0 -88
- package/dist/client/context/errorDetection.js +0 -171
- package/dist/client/context/errors.js +0 -21
- package/dist/client/context/fileTokenBudget.js +0 -127
- package/dist/client/context/prompts/summarizationPrompt.js +0 -117
- package/dist/client/context/stages/fileReadDeduplicator.js +0 -66
- package/dist/client/context/stages/slidingWindowTruncator.js +0 -190
- package/dist/client/context/stages/structuredSummarizer.js +0 -99
- package/dist/client/context/stages/toolOutputPruner.js +0 -52
- package/dist/client/context/summarizationEngine.js +0 -136
- package/dist/client/context/toolOutputLimits.js +0 -78
- package/dist/client/context/toolPairRepair.js +0 -66
- package/dist/client/core/analytics.js +0 -88
- package/dist/client/core/baseProvider.js +0 -1385
- package/dist/client/core/constants.js +0 -140
- package/dist/client/core/conversationMemoryFactory.js +0 -141
- package/dist/client/core/conversationMemoryInitializer.js +0 -128
- package/dist/client/core/conversationMemoryManager.js +0 -344
- package/dist/client/core/dynamicModels.js +0 -358
- package/dist/client/core/evaluation.js +0 -309
- package/dist/client/core/evaluationProviders.js +0 -248
- package/dist/client/core/factory.js +0 -412
- package/dist/client/core/infrastructure/baseError.js +0 -22
- package/dist/client/core/infrastructure/baseFactory.js +0 -54
- package/dist/client/core/infrastructure/baseRegistry.js +0 -53
- package/dist/client/core/infrastructure/index.js +0 -5
- package/dist/client/core/infrastructure/retry.js +0 -20
- package/dist/client/core/infrastructure/typedEventEmitter.js +0 -23
- package/dist/client/core/modelConfiguration.js +0 -851
- package/dist/client/core/modules/GenerationHandler.js +0 -588
- package/dist/client/core/modules/MessageBuilder.js +0 -273
- package/dist/client/core/modules/StreamHandler.js +0 -185
- package/dist/client/core/modules/TelemetryHandler.js +0 -203
- package/dist/client/core/modules/ToolsManager.js +0 -499
- package/dist/client/core/modules/Utilities.js +0 -331
- package/dist/client/core/redisConversationMemoryManager.js +0 -1435
- package/dist/client/core/streamAnalytics.js +0 -131
- package/dist/client/evaluation/contextBuilder.js +0 -134
- package/dist/client/evaluation/index.js +0 -61
- package/dist/client/evaluation/prompts.js +0 -73
- package/dist/client/evaluation/ragasEvaluator.js +0 -110
- package/dist/client/evaluation/retryManager.js +0 -78
- package/dist/client/evaluation/scoring.js +0 -61
- package/dist/client/factories/providerFactory.js +0 -166
- package/dist/client/factories/providerRegistry.js +0 -166
- package/dist/client/features/ppt/constants.js +0 -896
- package/dist/client/features/ppt/contentPlanner.js +0 -529
- package/dist/client/features/ppt/presentationOrchestrator.js +0 -236
- package/dist/client/features/ppt/slideGenerator.js +0 -532
- package/dist/client/features/ppt/slideRenderers.js +0 -2383
- package/dist/client/features/ppt/slideTypeInference.js +0 -405
- package/dist/client/features/ppt/types.js +0 -13
- package/dist/client/features/ppt/utils.js +0 -443
- package/dist/client/files/fileReferenceRegistry.js +0 -1543
- package/dist/client/files/fileTools.js +0 -450
- package/dist/client/files/streamingReader.js +0 -321
- package/dist/client/files/types.js +0 -23
- package/dist/client/hitl/hitlErrors.js +0 -54
- package/dist/client/hitl/hitlManager.js +0 -460
- package/dist/client/mcp/agentExposure.js +0 -356
- package/dist/client/mcp/auth/index.js +0 -11
- package/dist/client/mcp/auth/oauthClientProvider.js +0 -325
- package/dist/client/mcp/auth/tokenStorage.js +0 -134
- package/dist/client/mcp/batching/index.js +0 -10
- package/dist/client/mcp/batching/requestBatcher.js +0 -441
- package/dist/client/mcp/caching/index.js +0 -10
- package/dist/client/mcp/caching/toolCache.js +0 -433
- package/dist/client/mcp/elicitation/elicitationManager.js +0 -376
- package/dist/client/mcp/elicitation/index.js +0 -11
- package/dist/client/mcp/elicitation/types.js +0 -10
- package/dist/client/mcp/elicitationProtocol.js +0 -375
- package/dist/client/mcp/enhancedToolDiscovery.js +0 -481
- package/dist/client/mcp/externalServerManager.js +0 -1478
- package/dist/client/mcp/factory.js +0 -161
- package/dist/client/mcp/flexibleToolValidator.js +0 -161
- package/dist/client/mcp/httpRateLimiter.js +0 -391
- package/dist/client/mcp/httpRetryHandler.js +0 -178
- package/dist/client/mcp/index.js +0 -74
- package/dist/client/mcp/mcpCircuitBreaker.js +0 -427
- package/dist/client/mcp/mcpClientFactory.js +0 -708
- package/dist/client/mcp/mcpRegistryClient.js +0 -488
- package/dist/client/mcp/mcpServerBase.js +0 -373
- package/dist/client/mcp/multiServerManager.js +0 -579
- package/dist/client/mcp/registry.js +0 -158
- package/dist/client/mcp/routing/index.js +0 -10
- package/dist/client/mcp/routing/toolRouter.js +0 -416
- package/dist/client/mcp/serverCapabilities.js +0 -502
- package/dist/client/mcp/servers/agent/directToolsServer.js +0 -150
- package/dist/client/mcp/toolAnnotations.js +0 -239
- package/dist/client/mcp/toolConverter.js +0 -258
- package/dist/client/mcp/toolDiscoveryService.js +0 -798
- package/dist/client/mcp/toolIntegration.js +0 -334
- package/dist/client/mcp/toolRegistry.js +0 -729
- package/dist/client/memory/hippocampusInitializer.js +0 -19
- package/dist/client/memory/memoryRetrievalTools.js +0 -166
- package/dist/client/middleware/builtin/analytics.js +0 -132
- package/dist/client/middleware/builtin/autoEvaluation.js +0 -203
- package/dist/client/middleware/builtin/guardrails.js +0 -109
- package/dist/client/middleware/builtin/lifecycle.js +0 -168
- package/dist/client/middleware/factory.js +0 -327
- package/dist/client/middleware/registry.js +0 -295
- package/dist/client/middleware/utils/guardrailsUtils.js +0 -396
- package/dist/client/models/anthropicModels.js +0 -527
- package/dist/client/neurolink.js +0 -8233
- package/dist/client/observability/exporterRegistry.js +0 -413
- package/dist/client/observability/exporters/arizeExporter.js +0 -138
- package/dist/client/observability/exporters/baseExporter.js +0 -190
- package/dist/client/observability/exporters/braintrustExporter.js +0 -154
- package/dist/client/observability/exporters/datadogExporter.js +0 -196
- package/dist/client/observability/exporters/laminarExporter.js +0 -302
- package/dist/client/observability/exporters/langfuseExporter.js +0 -209
- package/dist/client/observability/exporters/langsmithExporter.js +0 -143
- package/dist/client/observability/exporters/otelExporter.js +0 -164
- package/dist/client/observability/exporters/posthogExporter.js +0 -287
- package/dist/client/observability/exporters/sentryExporter.js +0 -165
- package/dist/client/observability/index.js +0 -31
- package/dist/client/observability/metricsAggregator.js +0 -556
- package/dist/client/observability/otelBridge.js +0 -131
- package/dist/client/observability/retryPolicy.js +0 -383
- package/dist/client/observability/sampling/samplers.js +0 -216
- package/dist/client/observability/spanProcessor.js +0 -303
- package/dist/client/observability/tokenTracker.js +0 -413
- package/dist/client/observability/types/exporterTypes.js +0 -5
- package/dist/client/observability/types/index.js +0 -4
- package/dist/client/observability/types/spanTypes.js +0 -92
- package/dist/client/observability/utils/safeMetadata.js +0 -25
- package/dist/client/observability/utils/spanSerializer.js +0 -292
- package/dist/client/processors/archive/ArchiveProcessor.js +0 -1308
- package/dist/client/processors/base/BaseFileProcessor.js +0 -614
- package/dist/client/processors/base/types.js +0 -82
- package/dist/client/processors/config/fileTypes.js +0 -520
- package/dist/client/processors/config/index.js +0 -92
- package/dist/client/processors/config/languageMap.js +0 -410
- package/dist/client/processors/config/mimeTypes.js +0 -363
- package/dist/client/processors/config/sizeLimits.js +0 -258
- package/dist/client/processors/document/ExcelProcessor.js +0 -590
- package/dist/client/processors/document/OpenDocumentProcessor.js +0 -212
- package/dist/client/processors/document/PptxProcessor.js +0 -157
- package/dist/client/processors/document/RtfProcessor.js +0 -361
- package/dist/client/processors/document/WordProcessor.js +0 -353
- package/dist/client/processors/errors/FileErrorCode.js +0 -255
- package/dist/client/processors/errors/errorHelpers.js +0 -386
- package/dist/client/processors/errors/errorSerializer.js +0 -507
- package/dist/client/processors/errors/index.js +0 -49
- package/dist/client/processors/markup/SvgProcessor.js +0 -240
- package/dist/client/processors/media/AudioProcessor.js +0 -707
- package/dist/client/processors/media/VideoProcessor.js +0 -1045
- package/dist/client/providers/amazonBedrock.js +0 -1512
- package/dist/client/providers/amazonSagemaker.js +0 -162
- package/dist/client/providers/anthropic.js +0 -831
- package/dist/client/providers/azureOpenai.js +0 -143
- package/dist/client/providers/googleAiStudio.js +0 -1200
- package/dist/client/providers/googleNativeGemini3.js +0 -543
- package/dist/client/providers/googleVertex.js +0 -2936
- package/dist/client/providers/huggingFace.js +0 -315
- package/dist/client/providers/litellm.js +0 -488
- package/dist/client/providers/mistral.js +0 -157
- package/dist/client/providers/ollama.js +0 -1579
- package/dist/client/providers/openAI.js +0 -627
- package/dist/client/providers/openRouter.js +0 -543
- package/dist/client/providers/openaiCompatible.js +0 -290
- package/dist/client/providers/providerTypeUtils.js +0 -46
- package/dist/client/providers/sagemaker/adaptive-semaphore.js +0 -215
- package/dist/client/providers/sagemaker/client.js +0 -472
- package/dist/client/providers/sagemaker/config.js +0 -317
- package/dist/client/providers/sagemaker/detection.js +0 -606
- package/dist/client/providers/sagemaker/error-constants.js +0 -227
- package/dist/client/providers/sagemaker/errors.js +0 -299
- package/dist/client/providers/sagemaker/language-model.js +0 -775
- package/dist/client/providers/sagemaker/parsers.js +0 -634
- package/dist/client/providers/sagemaker/streaming.js +0 -331
- package/dist/client/providers/sagemaker/structured-parser.js +0 -625
- package/dist/client/proxy/accountQuota.js +0 -162
- package/dist/client/proxy/claudeFormat.js +0 -595
- package/dist/client/proxy/modelRouter.js +0 -29
- package/dist/client/proxy/oauthFetch.js +0 -367
- package/dist/client/proxy/proxyFetch.js +0 -586
- package/dist/client/proxy/requestLogger.js +0 -207
- package/dist/client/proxy/tokenRefresh.js +0 -124
- package/dist/client/proxy/usageStats.js +0 -74
- package/dist/client/proxy/utils/noProxyUtils.js +0 -149
- package/dist/client/rag/ChunkerFactory.js +0 -320
- package/dist/client/rag/ChunkerRegistry.js +0 -421
- package/dist/client/rag/chunkers/BaseChunker.js +0 -143
- package/dist/client/rag/chunkers/CharacterChunker.js +0 -28
- package/dist/client/rag/chunkers/HTMLChunker.js +0 -38
- package/dist/client/rag/chunkers/JSONChunker.js +0 -68
- package/dist/client/rag/chunkers/LaTeXChunker.js +0 -63
- package/dist/client/rag/chunkers/MarkdownChunker.js +0 -306
- package/dist/client/rag/chunkers/RecursiveChunker.js +0 -139
- package/dist/client/rag/chunkers/SemanticMarkdownChunker.js +0 -138
- package/dist/client/rag/chunkers/SentenceChunker.js +0 -66
- package/dist/client/rag/chunkers/TokenChunker.js +0 -61
- package/dist/client/rag/chunkers/index.js +0 -15
- package/dist/client/rag/chunking/characterChunker.js +0 -142
- package/dist/client/rag/chunking/chunkerRegistry.js +0 -194
- package/dist/client/rag/chunking/htmlChunker.js +0 -247
- package/dist/client/rag/chunking/index.js +0 -17
- package/dist/client/rag/chunking/jsonChunker.js +0 -281
- package/dist/client/rag/chunking/latexChunker.js +0 -251
- package/dist/client/rag/chunking/markdownChunker.js +0 -373
- package/dist/client/rag/chunking/recursiveChunker.js +0 -148
- package/dist/client/rag/chunking/semanticChunker.js +0 -306
- package/dist/client/rag/chunking/sentenceChunker.js +0 -230
- package/dist/client/rag/chunking/tokenChunker.js +0 -183
- package/dist/client/rag/document/MDocument.js +0 -392
- package/dist/client/rag/document/index.js +0 -5
- package/dist/client/rag/document/loaders.js +0 -500
- package/dist/client/rag/errors/RAGError.js +0 -274
- package/dist/client/rag/errors/index.js +0 -6
- package/dist/client/rag/graphRag/graphRAG.js +0 -401
- package/dist/client/rag/graphRag/index.js +0 -4
- package/dist/client/rag/index.js +0 -141
- package/dist/client/rag/metadata/MetadataExtractorFactory.js +0 -418
- package/dist/client/rag/metadata/MetadataExtractorRegistry.js +0 -362
- package/dist/client/rag/metadata/index.js +0 -9
- package/dist/client/rag/metadata/metadataExtractor.js +0 -280
- package/dist/client/rag/pipeline/RAGPipeline.js +0 -436
- package/dist/client/rag/pipeline/contextAssembly.js +0 -341
- package/dist/client/rag/pipeline/index.js +0 -5
- package/dist/client/rag/ragIntegration.js +0 -321
- package/dist/client/rag/reranker/RerankerFactory.js +0 -430
- package/dist/client/rag/reranker/RerankerRegistry.js +0 -402
- package/dist/client/rag/reranker/index.js +0 -9
- package/dist/client/rag/reranker/reranker.js +0 -277
- package/dist/client/rag/resilience/CircuitBreaker.js +0 -431
- package/dist/client/rag/resilience/RetryHandler.js +0 -304
- package/dist/client/rag/resilience/index.js +0 -7
- package/dist/client/rag/retrieval/hybridSearch.js +0 -335
- package/dist/client/rag/retrieval/index.js +0 -5
- package/dist/client/rag/retrieval/vectorQueryTool.js +0 -307
- package/dist/client/rag/types.js +0 -8
- package/dist/client/sdk/toolRegistration.js +0 -377
- package/dist/client/server/abstract/baseServerAdapter.js +0 -575
- package/dist/client/server/adapters/expressAdapter.js +0 -486
- package/dist/client/server/adapters/fastifyAdapter.js +0 -472
- package/dist/client/server/adapters/honoAdapter.js +0 -632
- package/dist/client/server/adapters/koaAdapter.js +0 -510
- package/dist/client/server/errors.js +0 -486
- package/dist/client/server/factory/serverAdapterFactory.js +0 -160
- package/dist/client/server/index.js +0 -108
- package/dist/client/server/middleware/abortSignal.js +0 -111
- package/dist/client/server/middleware/auth.js +0 -388
- package/dist/client/server/middleware/cache.js +0 -359
- package/dist/client/server/middleware/common.js +0 -281
- package/dist/client/server/middleware/deprecation.js +0 -190
- package/dist/client/server/middleware/mcpBodyAttachment.js +0 -63
- package/dist/client/server/middleware/rateLimit.js +0 -227
- package/dist/client/server/middleware/validation.js +0 -388
- package/dist/client/server/openapi/generator.js +0 -398
- package/dist/client/server/openapi/index.js +0 -36
- package/dist/client/server/openapi/schemas.js +0 -695
- package/dist/client/server/openapi/templates.js +0 -374
- package/dist/client/server/routes/agentRoutes.js +0 -189
- package/dist/client/server/routes/claudeProxyRoutes.js +0 -1600
- package/dist/client/server/routes/healthRoutes.js +0 -187
- package/dist/client/server/routes/index.js +0 -57
- package/dist/client/server/routes/mcpRoutes.js +0 -342
- package/dist/client/server/routes/memoryRoutes.js +0 -350
- package/dist/client/server/routes/openApiRoutes.js +0 -126
- package/dist/client/server/routes/toolRoutes.js +0 -199
- package/dist/client/server/streaming/dataStream.js +0 -486
- package/dist/client/server/streaming/index.js +0 -11
- package/dist/client/server/types.js +0 -67
- package/dist/client/server/utils/redaction.js +0 -334
- package/dist/client/server/utils/validation.js +0 -243
- package/dist/client/server/websocket/WebSocketHandler.js +0 -383
- package/dist/client/server/websocket/index.js +0 -4
- package/dist/client/services/server/ai/observability/instrumentation.js +0 -808
- package/dist/client/telemetry/attributes.js +0 -100
- package/dist/client/telemetry/index.js +0 -26
- package/dist/client/telemetry/telemetryService.js +0 -308
- package/dist/client/telemetry/tracers.js +0 -17
- package/dist/client/telemetry/withSpan.js +0 -34
- package/dist/client/types/actionTypes.js +0 -6
- package/dist/client/types/analytics.js +0 -5
- package/dist/client/types/authTypes.js +0 -9
- package/dist/client/types/circuitBreakerErrors.js +0 -34
- package/dist/client/types/cli.js +0 -21
- package/dist/client/types/clientTypes.js +0 -10
- package/dist/client/types/common.js +0 -51
- package/dist/client/types/configTypes.js +0 -49
- package/dist/client/types/content.js +0 -19
- package/dist/client/types/contextTypes.js +0 -400
- package/dist/client/types/conversation.js +0 -47
- package/dist/client/types/conversationMemoryInterface.js +0 -6
- package/dist/client/types/domainTypes.js +0 -5
- package/dist/client/types/errors.js +0 -167
- package/dist/client/types/evaluation.js +0 -5
- package/dist/client/types/evaluationProviders.js +0 -5
- package/dist/client/types/evaluationTypes.js +0 -1
- package/dist/client/types/externalMcp.js +0 -6
- package/dist/client/types/fileReferenceTypes.js +0 -8
- package/dist/client/types/fileTypes.js +0 -4
- package/dist/client/types/generateTypes.js +0 -1
- package/dist/client/types/guardrails.js +0 -1
- package/dist/client/types/hitlTypes.js +0 -8
- package/dist/client/types/index.js +0 -57
- package/dist/client/types/mcpTypes.js +0 -5
- package/dist/client/types/middlewareTypes.js +0 -1
- package/dist/client/types/modelTypes.js +0 -30
- package/dist/client/types/multimodal.js +0 -135
- package/dist/client/types/observability.js +0 -6
- package/dist/client/types/pptTypes.js +0 -82
- package/dist/client/types/providers.js +0 -111
- package/dist/client/types/proxyTypes.js +0 -16
- package/dist/client/types/ragTypes.js +0 -7
- package/dist/client/types/sdkTypes.js +0 -8
- package/dist/client/types/serviceTypes.js +0 -5
- package/dist/client/types/streamTypes.js +0 -1
- package/dist/client/types/subscriptionTypes.js +0 -9
- package/dist/client/types/taskClassificationTypes.js +0 -5
- package/dist/client/types/tools.js +0 -24
- package/dist/client/types/ttsTypes.js +0 -57
- package/dist/client/types/typeAliases.js +0 -48
- package/dist/client/types/utilities.js +0 -4
- package/dist/client/types/workflowTypes.js +0 -30
- package/dist/client/utils/async/withTimeout.js +0 -98
- package/dist/client/utils/asyncMutex.js +0 -60
- package/dist/client/utils/conversationMemory.js +0 -431
- package/dist/client/utils/csvProcessor.js +0 -846
- package/dist/client/utils/errorHandling.js +0 -936
- package/dist/client/utils/evaluationUtils.js +0 -131
- package/dist/client/utils/factoryProcessing.js +0 -589
- package/dist/client/utils/fileDetector.js +0 -2161
- package/dist/client/utils/imageCache.js +0 -376
- package/dist/client/utils/imageProcessor.js +0 -704
- package/dist/client/utils/logger.js +0 -491
- package/dist/client/utils/mcpDefaults.js +0 -134
- package/dist/client/utils/messageBuilder.js +0 -1653
- package/dist/client/utils/modelAliasResolver.js +0 -54
- package/dist/client/utils/modelDetection.js +0 -80
- package/dist/client/utils/modelRouter.js +0 -292
- package/dist/client/utils/multimodalOptionsBuilder.js +0 -65
- package/dist/client/utils/observabilityHelpers.js +0 -47
- package/dist/client/utils/parameterValidation.js +0 -966
- package/dist/client/utils/pdfProcessor.js +0 -410
- package/dist/client/utils/performance.js +0 -222
- package/dist/client/utils/pricing.js +0 -340
- package/dist/client/utils/promptRedaction.js +0 -62
- package/dist/client/utils/providerConfig.js +0 -1009
- package/dist/client/utils/providerHealth.js +0 -1237
- package/dist/client/utils/providerRetry.js +0 -112
- package/dist/client/utils/providerUtils.js +0 -434
- package/dist/client/utils/rateLimiter.js +0 -200
- package/dist/client/utils/redis.js +0 -368
- package/dist/client/utils/retryHandler.js +0 -269
- package/dist/client/utils/retryability.js +0 -22
- package/dist/client/utils/sanitizers/svg.js +0 -481
- package/dist/client/utils/schemaConversion.js +0 -255
- package/dist/client/utils/taskClassificationUtils.js +0 -149
- package/dist/client/utils/taskClassifier.js +0 -94
- package/dist/client/utils/thinkingConfig.js +0 -104
- package/dist/client/utils/timeout.js +0 -359
- package/dist/client/utils/tokenEstimation.js +0 -142
- package/dist/client/utils/tokenLimits.js +0 -125
- package/dist/client/utils/tokenUtils.js +0 -239
- package/dist/client/utils/toolUtils.js +0 -75
- package/dist/client/utils/transformationUtils.js +0 -554
- package/dist/client/utils/ttsProcessor.js +0 -286
- package/dist/client/utils/typeUtils.js +0 -97
- package/dist/client/utils/videoAnalysisProcessor.js +0 -67
- package/dist/client/workflow/config.js +0 -398
- package/dist/client/workflow/core/ensembleExecutor.js +0 -407
- package/dist/client/workflow/core/judgeScorer.js +0 -544
- package/dist/client/workflow/core/responseConditioner.js +0 -225
- package/dist/client/workflow/core/types/conditionerTypes.js +0 -7
- package/dist/client/workflow/core/types/ensembleTypes.js +0 -7
- package/dist/client/workflow/core/types/index.js +0 -7
- package/dist/client/workflow/core/types/judgeTypes.js +0 -7
- package/dist/client/workflow/core/types/layerTypes.js +0 -7
- package/dist/client/workflow/core/types/registryTypes.js +0 -7
- package/dist/client/workflow/core/workflowRegistry.js +0 -304
- package/dist/client/workflow/core/workflowRunner.js +0 -586
- package/dist/client/workflow/index.js +0 -50
- package/dist/client/workflow/types.js +0 -9
- package/dist/client/workflow/utils/types/index.js +0 -7
- package/dist/client/workflow/utils/workflowMetrics.js +0 -311
- package/dist/client/workflow/utils/workflowValidation.js +0 -420
- package/dist/client/workflow/workflows/adaptiveWorkflow.js +0 -366
- package/dist/client/workflow/workflows/consensusWorkflow.js +0 -192
- package/dist/client/workflow/workflows/fallbackWorkflow.js +0 -225
- package/dist/client/workflow/workflows/multiJudgeWorkflow.js +0 -351
- /package/dist/client/{client/reactHooks.js → reactHooks.js} +0 -0
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Summarization Prompt Builder
|
|
3
|
-
*
|
|
4
|
-
* Builds prompts for summarizing conversation context into a 10-section structure.
|
|
5
|
-
* Supports both initial summarization and incremental merging with existing summaries.
|
|
6
|
-
*/
|
|
7
|
-
const SUMMARY_SECTIONS = [
|
|
8
|
-
"Primary Request and Intent",
|
|
9
|
-
"Key Technical Concepts",
|
|
10
|
-
"Files and Code Sections",
|
|
11
|
-
"Problem Solving",
|
|
12
|
-
"Pending Tasks",
|
|
13
|
-
"Task Evolution",
|
|
14
|
-
"Current Work",
|
|
15
|
-
"Next Step",
|
|
16
|
-
"Required Files",
|
|
17
|
-
"Constraints and Established Rules",
|
|
18
|
-
];
|
|
19
|
-
function buildFileContextSection(filesRead, filesModified) {
|
|
20
|
-
const hasFiles = (filesRead && filesRead.length > 0) ||
|
|
21
|
-
(filesModified && filesModified.length > 0);
|
|
22
|
-
if (!hasFiles) {
|
|
23
|
-
return "";
|
|
24
|
-
}
|
|
25
|
-
let section = "\n\nFile Context:\n";
|
|
26
|
-
if (filesRead && filesRead.length > 0) {
|
|
27
|
-
section += "Files Read:\n";
|
|
28
|
-
for (const file of filesRead) {
|
|
29
|
-
section += `- ${file}\n`;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
if (filesModified && filesModified.length > 0) {
|
|
33
|
-
section += "Files Modified:\n";
|
|
34
|
-
for (const file of filesModified) {
|
|
35
|
-
section += `- ${file}\n`;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return section;
|
|
39
|
-
}
|
|
40
|
-
function buildInitialPrompt(options) {
|
|
41
|
-
const fileContext = buildFileContextSection(options.filesRead, options.filesModified);
|
|
42
|
-
return `You are a context summarization assistant. Your task is to analyze the conversation and create a structured summary following a 10-section format.
|
|
43
|
-
|
|
44
|
-
Create a summary with the following sections:
|
|
45
|
-
|
|
46
|
-
### 1. Primary Request and Intent
|
|
47
|
-
What is the user's main goal or request? What are they trying to accomplish?
|
|
48
|
-
|
|
49
|
-
### 2. Key Technical Concepts
|
|
50
|
-
What technologies, frameworks, patterns, or concepts are central to this conversation?
|
|
51
|
-
|
|
52
|
-
### 3. Files and Code Sections
|
|
53
|
-
What specific files, functions, or code sections have been discussed or modified?
|
|
54
|
-
|
|
55
|
-
### 4. Problem Solving
|
|
56
|
-
What problems were identified? What solutions were attempted or implemented?
|
|
57
|
-
|
|
58
|
-
### 5. Pending Tasks
|
|
59
|
-
What tasks remain incomplete or need follow-up?
|
|
60
|
-
|
|
61
|
-
### 6. Task Evolution
|
|
62
|
-
How has the task changed or evolved during the conversation?
|
|
63
|
-
|
|
64
|
-
### 7. Current Work
|
|
65
|
-
What is being actively worked on right now?
|
|
66
|
-
|
|
67
|
-
### 8. Next Step
|
|
68
|
-
What is the immediate next action to take?
|
|
69
|
-
|
|
70
|
-
### 9. Required Files
|
|
71
|
-
What files will need to be accessed or modified to continue?
|
|
72
|
-
|
|
73
|
-
### 10. Constraints and Established Rules
|
|
74
|
-
What user-imposed constraints, behavioral directives, coding standards, agreed-upon decisions, or established rules must persist across the conversation? Include any "always do X", "never do Y", naming conventions, architectural patterns, or preferences the user has stated.${fileContext}
|
|
75
|
-
|
|
76
|
-
IMPORTANT: Section 10 (Constraints and Established Rules) must ALWAYS be preserved in full. User constraints and established agreements are never "no longer relevant" — they remain in effect for the entire session unless the user explicitly revokes them.
|
|
77
|
-
|
|
78
|
-
Analyze the conversation thoroughly and fill in each section with relevant information. If a section is not applicable, write "N/A" for that section.`;
|
|
79
|
-
}
|
|
80
|
-
function buildIncrementalPrompt(options) {
|
|
81
|
-
const fileContext = buildFileContextSection(options.filesRead, options.filesModified);
|
|
82
|
-
return `You are a context summarization assistant. Your task is to MERGE new conversation content with an existing summary.
|
|
83
|
-
|
|
84
|
-
Existing Summary:
|
|
85
|
-
---
|
|
86
|
-
${options.previousSummary}
|
|
87
|
-
---
|
|
88
|
-
|
|
89
|
-
Update sections with new information while preserving important context from the existing summary. Maintain the same 10-section structure.
|
|
90
|
-
|
|
91
|
-
Instructions:
|
|
92
|
-
1. Review the existing summary above
|
|
93
|
-
2. Analyze the new conversation content
|
|
94
|
-
3. MERGE the new information into the appropriate sections
|
|
95
|
-
4. Update sections with relevant new information
|
|
96
|
-
5. Remove information that is no longer relevant EXCEPT for Section 10 (Constraints and Established Rules) — user constraints and established agreements must ALWAYS be preserved unless the user explicitly revoked them
|
|
97
|
-
6. Keep the summary concise but comprehensive
|
|
98
|
-
7. Maintain the 10-section format
|
|
99
|
-
8. Always carry forward ALL entries from Section 10 of the existing summary, adding any new constraints found in the new content${fileContext}
|
|
100
|
-
|
|
101
|
-
Output the updated summary following the same 10-section structure.`;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Builds a summarization prompt based on the provided options.
|
|
105
|
-
*
|
|
106
|
-
* @param options - Configuration for the prompt builder
|
|
107
|
-
* @returns The constructed prompt string
|
|
108
|
-
*/
|
|
109
|
-
export function buildSummarizationPrompt(options) {
|
|
110
|
-
// For incremental mode, we need a previous summary to merge with
|
|
111
|
-
// If no previous summary is provided, fall back to initial prompt
|
|
112
|
-
if (options.isIncremental && options.previousSummary) {
|
|
113
|
-
return buildIncrementalPrompt(options);
|
|
114
|
-
}
|
|
115
|
-
return buildInitialPrompt(options);
|
|
116
|
-
}
|
|
117
|
-
export { SUMMARY_SECTIONS };
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Stage 2: File Read Deduplication
|
|
3
|
-
*
|
|
4
|
-
* Detect multiple reads of the same file path, keep only the latest,
|
|
5
|
-
* replace earlier reads with a notice.
|
|
6
|
-
*/
|
|
7
|
-
const FILE_READ_PATTERN = /(?:read|reading|read_file|readFile|Read file|cat)\s+['"`]?([^\s'"`\n]+)/i;
|
|
8
|
-
const DEDUP_NOTICE = (filePath) => `[File ${filePath} - refer to latest read below]`;
|
|
9
|
-
const DEDUP_THRESHOLD = 0.3; // Need 30% savings to declare success
|
|
10
|
-
export function deduplicateFileReads(messages) {
|
|
11
|
-
// Track file read positions: filePath -> array of message indices
|
|
12
|
-
const fileReadMap = new Map();
|
|
13
|
-
for (let i = 0; i < messages.length; i++) {
|
|
14
|
-
const msg = messages[i];
|
|
15
|
-
if (msg.role !== "tool_result" && msg.role !== "assistant") {
|
|
16
|
-
continue;
|
|
17
|
-
}
|
|
18
|
-
const match = msg.content.match(FILE_READ_PATTERN);
|
|
19
|
-
if (match) {
|
|
20
|
-
const filePath = match[1];
|
|
21
|
-
const existing = fileReadMap.get(filePath) || [];
|
|
22
|
-
existing.push(i);
|
|
23
|
-
fileReadMap.set(filePath, existing);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
// Find files read multiple times
|
|
27
|
-
const duplicates = new Map();
|
|
28
|
-
for (const [filePath, indices] of fileReadMap) {
|
|
29
|
-
if (indices.length > 1) {
|
|
30
|
-
duplicates.set(filePath, indices);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
if (duplicates.size === 0) {
|
|
34
|
-
return { deduplicated: false, messages, filesDeduped: 0 };
|
|
35
|
-
}
|
|
36
|
-
const result = [...messages];
|
|
37
|
-
let totalOriginalChars = 0;
|
|
38
|
-
let totalReplacedChars = 0;
|
|
39
|
-
for (const [filePath, indices] of duplicates) {
|
|
40
|
-
// Keep the latest (last index), replace all earlier ones
|
|
41
|
-
const latestIndex = indices[indices.length - 1];
|
|
42
|
-
for (const idx of indices) {
|
|
43
|
-
if (idx === latestIndex) {
|
|
44
|
-
continue;
|
|
45
|
-
}
|
|
46
|
-
const original = result[idx];
|
|
47
|
-
totalOriginalChars += original.content.length;
|
|
48
|
-
const notice = DEDUP_NOTICE(filePath);
|
|
49
|
-
totalReplacedChars += notice.length;
|
|
50
|
-
result[idx] = {
|
|
51
|
-
...original,
|
|
52
|
-
content: notice,
|
|
53
|
-
metadata: { ...original.metadata, truncated: true },
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
const savings = totalOriginalChars > 0
|
|
58
|
-
? (totalOriginalChars - totalReplacedChars) / totalOriginalChars
|
|
59
|
-
: 0;
|
|
60
|
-
const deduplicated = savings >= DEDUP_THRESHOLD;
|
|
61
|
-
return {
|
|
62
|
-
deduplicated,
|
|
63
|
-
messages: deduplicated ? result : messages,
|
|
64
|
-
filesDeduped: deduplicated ? duplicates.size : 0,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Stage 4: Sliding Window Truncation
|
|
3
|
-
*
|
|
4
|
-
* Non-destructive fallback: tags oldest messages as truncated
|
|
5
|
-
* instead of deleting them. Always preserves first message pair.
|
|
6
|
-
* Removes messages in pairs to maintain role alternation.
|
|
7
|
-
*
|
|
8
|
-
* Features:
|
|
9
|
-
* - Adaptive truncation (PERF-001): calculates fraction from actual overage
|
|
10
|
-
* instead of fixed 50%, with iterative refinement up to 3 passes.
|
|
11
|
-
* - Small conversation handling (BUG-005): for <= 4 messages, truncates
|
|
12
|
-
* message content proportionally instead of returning no-op.
|
|
13
|
-
*/
|
|
14
|
-
import { estimateTokens, estimateMessagesTokens, truncateToTokenBudget, } from "../../utils/tokenEstimation.js";
|
|
15
|
-
import { logger } from "../../utils/logger.js";
|
|
16
|
-
import { randomUUID } from "crypto";
|
|
17
|
-
const TRUNCATION_MARKER_CONTENT = "[Earlier conversation history was truncated to fit within context limits]";
|
|
18
|
-
function validateRoleAlternation(messages) {
|
|
19
|
-
for (let i = 1; i < messages.length; i++) {
|
|
20
|
-
if (messages[i].role === messages[i - 1].role &&
|
|
21
|
-
messages[i].role !== "system") {
|
|
22
|
-
logger.warn(`[SlidingWindowTruncator] Role alternation broken at index ${i}: consecutive "${messages[i].role}" messages`);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* For conversations with <= 4 messages that exceed token budget,
|
|
28
|
-
* truncate the CONTENT of the longest messages rather than removing messages.
|
|
29
|
-
*
|
|
30
|
-
* Strategy:
|
|
31
|
-
* 1. Calculate each message's proportional share of the token budget
|
|
32
|
-
* 2. Truncate messages that exceed their share using truncateToTokenBudget()
|
|
33
|
-
* 3. Never truncate messages below 200 tokens (preserve minimum context)
|
|
34
|
-
*/
|
|
35
|
-
function truncateSmallConversation(messages, config) {
|
|
36
|
-
// If no target tokens provided, we can't do content truncation
|
|
37
|
-
if (!config?.targetTokens) {
|
|
38
|
-
return { truncated: false, messages, messagesRemoved: 0 };
|
|
39
|
-
}
|
|
40
|
-
const provider = config.provider;
|
|
41
|
-
const targetTokens = config.targetTokens;
|
|
42
|
-
const currentTokens = estimateMessagesTokens(messages, provider);
|
|
43
|
-
if (currentTokens <= targetTokens) {
|
|
44
|
-
return { truncated: false, messages, messagesRemoved: 0 };
|
|
45
|
-
}
|
|
46
|
-
const MINIMUM_MSG_TOKENS = 200;
|
|
47
|
-
const FRAMING_OVERHEAD = 24 + messages.length * 4; // conversation + per-message overhead
|
|
48
|
-
// Available budget for actual content
|
|
49
|
-
const contentBudget = targetTokens - FRAMING_OVERHEAD;
|
|
50
|
-
if (contentBudget <= 0) {
|
|
51
|
-
return { truncated: false, messages, messagesRemoved: 0 };
|
|
52
|
-
}
|
|
53
|
-
// Calculate current content tokens per message
|
|
54
|
-
const msgTokens = messages.map((msg) => estimateTokens(msg.content, provider));
|
|
55
|
-
const totalContentTokens = msgTokens.reduce((sum, t) => sum + t, 0);
|
|
56
|
-
// Each message gets a proportional share of the content budget
|
|
57
|
-
const result = [...messages];
|
|
58
|
-
let totalSaved = 0;
|
|
59
|
-
for (let i = 0; i < result.length; i++) {
|
|
60
|
-
const msg = result[i];
|
|
61
|
-
// Don't truncate system/summary messages
|
|
62
|
-
if (msg.role === "system" || msg.metadata?.isSummary) {
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
const proportionalBudget = Math.floor((msgTokens[i] / totalContentTokens) * contentBudget);
|
|
66
|
-
const msgBudget = Math.max(MINIMUM_MSG_TOKENS, proportionalBudget);
|
|
67
|
-
if (msgTokens[i] > msgBudget) {
|
|
68
|
-
const truncated = truncateToTokenBudget(msg.content, msgBudget, provider);
|
|
69
|
-
if (truncated.truncated) {
|
|
70
|
-
totalSaved += msgTokens[i] - estimateTokens(truncated.text, provider);
|
|
71
|
-
result[i] = {
|
|
72
|
-
...msg,
|
|
73
|
-
content: truncated.text,
|
|
74
|
-
metadata: { ...msg.metadata, truncated: true },
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (totalSaved > 0) {
|
|
80
|
-
const finalTokens = estimateMessagesTokens(result, provider);
|
|
81
|
-
logger.info("[Truncation] Small conversation content truncated", {
|
|
82
|
-
messageCount: messages.length,
|
|
83
|
-
tokensSaved: totalSaved,
|
|
84
|
-
targetTokens,
|
|
85
|
-
finalTokens,
|
|
86
|
-
});
|
|
87
|
-
return {
|
|
88
|
-
truncated: finalTokens <= targetTokens,
|
|
89
|
-
messages: result,
|
|
90
|
-
messagesRemoved: 0, // No messages removed, only content truncated
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
return { truncated: false, messages, messagesRemoved: 0 };
|
|
94
|
-
}
|
|
95
|
-
export function truncateWithSlidingWindow(messages, config) {
|
|
96
|
-
if (messages.length <= 4) {
|
|
97
|
-
// Delegate to content truncation for small conversations (BUG-005)
|
|
98
|
-
return truncateSmallConversation(messages, config);
|
|
99
|
-
}
|
|
100
|
-
// ADAPTIVE MODE: calculate fraction from actual overage (PERF-001)
|
|
101
|
-
let fraction;
|
|
102
|
-
if (config?.currentTokens &&
|
|
103
|
-
config?.targetTokens &&
|
|
104
|
-
config.currentTokens > config.targetTokens) {
|
|
105
|
-
const overageRatio = (config.currentTokens - config.targetTokens) / config.currentTokens;
|
|
106
|
-
const buffer = config?.adaptiveBuffer ?? 0.15;
|
|
107
|
-
// Required fraction = overage ratio + buffer, clamped to [0.1, 0.9]
|
|
108
|
-
fraction = Math.min(0.9, Math.max(0.1, overageRatio + buffer));
|
|
109
|
-
logger.info("[Truncation] Adaptive fraction calculated", {
|
|
110
|
-
currentTokens: config.currentTokens,
|
|
111
|
-
targetTokens: config.targetTokens,
|
|
112
|
-
overageRatio: Math.round(overageRatio * 100),
|
|
113
|
-
fraction: Math.round(fraction * 100),
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
// Fallback to configured or default fraction
|
|
118
|
-
fraction = config?.fraction ?? 0.5;
|
|
119
|
-
}
|
|
120
|
-
// Always preserve first user-assistant pair
|
|
121
|
-
const firstPair = messages.slice(0, 2);
|
|
122
|
-
const remainingMessages = messages.slice(2);
|
|
123
|
-
// ITERATIVE: if first pass isn't enough, increase fraction
|
|
124
|
-
const maxIterations = config?.maxIterations ?? 3;
|
|
125
|
-
let currentFraction = fraction;
|
|
126
|
-
for (let iteration = 0; iteration < maxIterations; iteration++) {
|
|
127
|
-
const removeCount = Math.floor(remainingMessages.length * currentFraction);
|
|
128
|
-
const evenRemoveCount = removeCount - (removeCount % 2);
|
|
129
|
-
if (evenRemoveCount <= 0) {
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
const keptAfterTruncation = remainingMessages.slice(evenRemoveCount);
|
|
133
|
-
// Insert a truncation marker with machine-readable metadata so
|
|
134
|
-
// effectiveHistory.ts can detect it via isTruncationMarker /
|
|
135
|
-
// truncationId and removeTruncationTags can rewind it.
|
|
136
|
-
const truncId = randomUUID();
|
|
137
|
-
const marker = {
|
|
138
|
-
id: `truncation-marker-${truncId}`,
|
|
139
|
-
role: "user",
|
|
140
|
-
content: TRUNCATION_MARKER_CONTENT,
|
|
141
|
-
isTruncationMarker: true,
|
|
142
|
-
truncationId: truncId,
|
|
143
|
-
};
|
|
144
|
-
const candidateMessages = [...firstPair, marker, ...keptAfterTruncation];
|
|
145
|
-
validateRoleAlternation(candidateMessages);
|
|
146
|
-
// If we have token targets, verify the result fits
|
|
147
|
-
if (config?.targetTokens) {
|
|
148
|
-
const candidateTokens = estimateMessagesTokens(candidateMessages, config.provider);
|
|
149
|
-
if (candidateTokens <= config.targetTokens) {
|
|
150
|
-
return {
|
|
151
|
-
truncated: true,
|
|
152
|
-
messages: candidateMessages,
|
|
153
|
-
messagesRemoved: evenRemoveCount,
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
// Not enough -- increase fraction by 25% for next iteration
|
|
157
|
-
currentFraction = Math.min(0.95, currentFraction + 0.25);
|
|
158
|
-
continue;
|
|
159
|
-
}
|
|
160
|
-
// No token targets -- single-pass with calculated fraction
|
|
161
|
-
return {
|
|
162
|
-
truncated: true,
|
|
163
|
-
messages: candidateMessages,
|
|
164
|
-
messagesRemoved: evenRemoveCount,
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
// All iterations exhausted -- return best effort (most aggressive truncation)
|
|
168
|
-
const maxRemove = Math.floor(remainingMessages.length * 0.95);
|
|
169
|
-
const evenMaxRemove = maxRemove - (maxRemove % 2);
|
|
170
|
-
if (evenMaxRemove > 0) {
|
|
171
|
-
const keptMessages = remainingMessages.slice(evenMaxRemove);
|
|
172
|
-
// Insert a truncation marker (see iterative block above)
|
|
173
|
-
const fallbackTruncId = randomUUID();
|
|
174
|
-
const fallbackMarker = {
|
|
175
|
-
id: `truncation-marker-${fallbackTruncId}`,
|
|
176
|
-
role: "user",
|
|
177
|
-
content: TRUNCATION_MARKER_CONTENT,
|
|
178
|
-
isTruncationMarker: true,
|
|
179
|
-
truncationId: fallbackTruncId,
|
|
180
|
-
};
|
|
181
|
-
const fallbackMessages = [...firstPair, fallbackMarker, ...keptMessages];
|
|
182
|
-
validateRoleAlternation(fallbackMessages);
|
|
183
|
-
return {
|
|
184
|
-
truncated: true,
|
|
185
|
-
messages: fallbackMessages,
|
|
186
|
-
messagesRemoved: evenMaxRemove,
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
return { truncated: false, messages, messagesRemoved: 0 };
|
|
190
|
-
}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Stage 3: Structured LLM Summarization
|
|
3
|
-
*
|
|
4
|
-
* Uses the structured 10-section prompt to summarize older messages
|
|
5
|
-
* while preserving recent ones.
|
|
6
|
-
*/
|
|
7
|
-
import { randomUUID } from "crypto";
|
|
8
|
-
import { generateSummary } from "../../utils/conversationMemory.js";
|
|
9
|
-
import { estimateTokens } from "../../utils/tokenEstimation.js";
|
|
10
|
-
import { logger } from "../../utils/logger.js";
|
|
11
|
-
/**
|
|
12
|
-
* Find the split index using token counting — walk backward from the end,
|
|
13
|
-
* accumulating token counts until we've reserved `targetRecentTokens` worth
|
|
14
|
-
* of recent content. Everything before the split index gets summarized.
|
|
15
|
-
*/
|
|
16
|
-
function findSplitIndexByTokens(messages, targetRecentTokens, provider) {
|
|
17
|
-
let recentTokens = 0;
|
|
18
|
-
let splitIndex = messages.length;
|
|
19
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
20
|
-
const content = typeof messages[i].content === "string"
|
|
21
|
-
? messages[i].content
|
|
22
|
-
: JSON.stringify(messages[i].content);
|
|
23
|
-
const msgTokens = estimateTokens(content, provider);
|
|
24
|
-
if (recentTokens + msgTokens > targetRecentTokens) {
|
|
25
|
-
splitIndex = i + 1;
|
|
26
|
-
break;
|
|
27
|
-
}
|
|
28
|
-
recentTokens += msgTokens;
|
|
29
|
-
}
|
|
30
|
-
// Ensure at least one message is summarized
|
|
31
|
-
return Math.max(1, splitIndex);
|
|
32
|
-
}
|
|
33
|
-
export async function summarizeMessages(messages, config) {
|
|
34
|
-
const keepRecentRatio = config?.keepRecentRatio ?? 0.3;
|
|
35
|
-
if (messages.length <= 4) {
|
|
36
|
-
return { summarized: false, messages };
|
|
37
|
-
}
|
|
38
|
-
// Determine split point: prefer token-based when a target budget is available,
|
|
39
|
-
// fall back to message-count-based split for backward compatibility.
|
|
40
|
-
let splitIndex;
|
|
41
|
-
if (config?.targetTokens && config.targetTokens > 0) {
|
|
42
|
-
// Keep `keepRecentRatio` fraction of the target budget as recent context
|
|
43
|
-
const targetRecentTokens = Math.floor(config.targetTokens * keepRecentRatio);
|
|
44
|
-
// NOTE: config.provider is the summarization provider, not the generation
|
|
45
|
-
// provider. Ideally we'd use the generation/budget provider for accurate
|
|
46
|
-
// token estimation, but SummarizeConfig doesn't carry a separate
|
|
47
|
-
// budgetProvider field. This is a known design limitation.
|
|
48
|
-
splitIndex = findSplitIndexByTokens(messages, targetRecentTokens, config.provider);
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
// Legacy: message-count-based split
|
|
52
|
-
const keepCount = Math.max(4, Math.ceil(messages.length * keepRecentRatio));
|
|
53
|
-
splitIndex = messages.length - keepCount;
|
|
54
|
-
}
|
|
55
|
-
// Clamp so at least the last message is always preserved (never summarize everything)
|
|
56
|
-
splitIndex = Math.min(splitIndex, messages.length - 1);
|
|
57
|
-
if (splitIndex <= 0) {
|
|
58
|
-
return { summarized: false, messages };
|
|
59
|
-
}
|
|
60
|
-
const messagesToSummarize = messages.slice(0, splitIndex);
|
|
61
|
-
const recentMessages = messages.slice(splitIndex);
|
|
62
|
-
// Find previous summary if exists
|
|
63
|
-
const previousSummary = messagesToSummarize.find((m) => m.metadata?.isSummary)?.content;
|
|
64
|
-
// Build effective memory config: use provided memoryConfig, or construct from provider/model
|
|
65
|
-
const effectiveMemoryConfig = config?.memoryConfig ? { ...config.memoryConfig } : {};
|
|
66
|
-
// Fill in summarization provider/model from compactor config if not already set
|
|
67
|
-
if (!effectiveMemoryConfig.summarizationProvider && config?.provider) {
|
|
68
|
-
effectiveMemoryConfig.summarizationProvider = config.provider;
|
|
69
|
-
}
|
|
70
|
-
if (!effectiveMemoryConfig.summarizationModel && config?.model) {
|
|
71
|
-
effectiveMemoryConfig.summarizationModel = config.model;
|
|
72
|
-
}
|
|
73
|
-
// Only skip if there's genuinely no provider available
|
|
74
|
-
if (!effectiveMemoryConfig.summarizationProvider &&
|
|
75
|
-
!effectiveMemoryConfig.summarizationModel) {
|
|
76
|
-
logger.debug("[ContextCompactor] Stage 3 skipped: no summarization provider or model available");
|
|
77
|
-
return { summarized: false, messages };
|
|
78
|
-
}
|
|
79
|
-
const summaryText = await generateSummary(messagesToSummarize, effectiveMemoryConfig, "[ContextCompactor]", previousSummary);
|
|
80
|
-
if (!summaryText) {
|
|
81
|
-
return { summarized: false, messages };
|
|
82
|
-
}
|
|
83
|
-
const summaryMessage = {
|
|
84
|
-
id: `summary-${randomUUID()}`,
|
|
85
|
-
role: "user",
|
|
86
|
-
content: `[Previous conversation summary]:\n\n${summaryText}`,
|
|
87
|
-
timestamp: new Date().toISOString(),
|
|
88
|
-
metadata: {
|
|
89
|
-
isSummary: true,
|
|
90
|
-
summarizesFrom: messagesToSummarize[0]?.id,
|
|
91
|
-
summarizesTo: messagesToSummarize[messagesToSummarize.length - 1]?.id,
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
return {
|
|
95
|
-
summarized: true,
|
|
96
|
-
messages: [summaryMessage, ...recentMessages],
|
|
97
|
-
summaryText,
|
|
98
|
-
};
|
|
99
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Stage 1: Tool Output Pruning
|
|
3
|
-
*
|
|
4
|
-
* Walk messages backwards, protect recent tool output tokens,
|
|
5
|
-
* replace older tool results with "[Tool result cleared]".
|
|
6
|
-
*/
|
|
7
|
-
import { estimateTokens } from "../../utils/tokenEstimation.js";
|
|
8
|
-
const CLEARED_PLACEHOLDER = "[Tool result cleared]";
|
|
9
|
-
const DEFAULT_PROTECT_TOKENS = 40_000;
|
|
10
|
-
const DEFAULT_MINIMUM_SAVINGS = 20_000;
|
|
11
|
-
export function pruneToolOutputs(messages, config) {
|
|
12
|
-
const protectTokens = config?.protectTokens ?? DEFAULT_PROTECT_TOKENS;
|
|
13
|
-
const minimumSavings = config?.minimumSavings ?? DEFAULT_MINIMUM_SAVINGS;
|
|
14
|
-
const protectedTools = new Set(config?.protectedTools ?? ["skill"]);
|
|
15
|
-
const provider = config?.provider;
|
|
16
|
-
const result = [...messages];
|
|
17
|
-
let recentToolTokens = 0;
|
|
18
|
-
let totalSaved = 0;
|
|
19
|
-
// Walk backwards to protect recent tool outputs
|
|
20
|
-
for (let i = result.length - 1; i >= 0; i--) {
|
|
21
|
-
const msg = result[i];
|
|
22
|
-
if (msg.role !== "tool_result") {
|
|
23
|
-
continue;
|
|
24
|
-
}
|
|
25
|
-
// Skip protected tools
|
|
26
|
-
if (msg.tool && protectedTools.has(msg.tool)) {
|
|
27
|
-
continue;
|
|
28
|
-
}
|
|
29
|
-
const contentTokens = estimateTokens(msg.content, provider);
|
|
30
|
-
if (recentToolTokens < protectTokens) {
|
|
31
|
-
// This tool output is within the protection window
|
|
32
|
-
recentToolTokens += contentTokens;
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
// This tool output is old enough to prune
|
|
36
|
-
if (msg.content !== CLEARED_PLACEHOLDER) {
|
|
37
|
-
totalSaved +=
|
|
38
|
-
contentTokens - estimateTokens(CLEARED_PLACEHOLDER, provider);
|
|
39
|
-
result[i] = {
|
|
40
|
-
...msg,
|
|
41
|
-
content: CLEARED_PLACEHOLDER,
|
|
42
|
-
metadata: { ...msg.metadata, truncated: true },
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
const pruned = totalSaved >= minimumSavings;
|
|
47
|
-
return {
|
|
48
|
-
pruned,
|
|
49
|
-
messages: pruned ? result : messages,
|
|
50
|
-
tokensSaved: pruned ? totalSaved : 0,
|
|
51
|
-
};
|
|
52
|
-
}
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared Summarization Engine
|
|
3
|
-
*
|
|
4
|
-
* Extracted from ConversationMemoryManager and RedisConversationMemoryManager
|
|
5
|
-
* to eliminate code duplication. Both managers delegate to this engine.
|
|
6
|
-
*/
|
|
7
|
-
import { TokenUtils } from "../constants/tokens.js";
|
|
8
|
-
import { buildContextFromPointer, generateSummary, } from "../utils/conversationMemory.js";
|
|
9
|
-
import { RECENT_MESSAGES_RATIO } from "../config/conversationMemory.js";
|
|
10
|
-
import { logger } from "../utils/logger.js";
|
|
11
|
-
/**
|
|
12
|
-
* Centralized summarization engine for conversation memory.
|
|
13
|
-
* Handles token counting, threshold checking, and summary generation.
|
|
14
|
-
*/
|
|
15
|
-
export class SummarizationEngine {
|
|
16
|
-
/**
|
|
17
|
-
* Check if a session needs summarization and perform it if so.
|
|
18
|
-
* @param session - Session memory to check and potentially summarize
|
|
19
|
-
* @param threshold - Token threshold that triggers summarization
|
|
20
|
-
* @param config - Conversation memory configuration (partial allowed)
|
|
21
|
-
* @param logPrefix - Prefix for log messages
|
|
22
|
-
* @returns True if summarization was performed
|
|
23
|
-
*/
|
|
24
|
-
async checkAndSummarize(session, threshold, config, logPrefix = "[SummarizationEngine]", requestId) {
|
|
25
|
-
const contextMessages = buildContextFromPointer(session, requestId);
|
|
26
|
-
const tokenCount = this.estimateTokens(contextMessages);
|
|
27
|
-
session.lastTokenCount = tokenCount;
|
|
28
|
-
session.lastCountedAt = Date.now();
|
|
29
|
-
logger.info("[Summarization] Check", {
|
|
30
|
-
requestId,
|
|
31
|
-
sessionId: session.sessionId,
|
|
32
|
-
tokenCount,
|
|
33
|
-
threshold,
|
|
34
|
-
willSummarize: tokenCount >= threshold,
|
|
35
|
-
});
|
|
36
|
-
if (tokenCount >= threshold) {
|
|
37
|
-
await this.summarizeSession(session, threshold, config, logPrefix, requestId);
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Perform token-based summarization on a session.
|
|
44
|
-
* Uses pointer-based, non-destructive approach.
|
|
45
|
-
* @param session - Session memory to summarize
|
|
46
|
-
* @param threshold - Token threshold for calculating split point
|
|
47
|
-
* @param config - Conversation memory configuration (partial allowed)
|
|
48
|
-
* @param logPrefix - Prefix for log messages
|
|
49
|
-
*/
|
|
50
|
-
async summarizeSession(session, threshold, config, logPrefix = "[SummarizationEngine]", requestId) {
|
|
51
|
-
const startTime = Date.now();
|
|
52
|
-
const startIndex = session.summarizedUpToMessageId
|
|
53
|
-
? session.messages.findIndex((m) => m.id === session.summarizedUpToMessageId) + 1
|
|
54
|
-
: 0;
|
|
55
|
-
const recentMessages = session.messages.slice(startIndex);
|
|
56
|
-
if (recentMessages.length === 0) {
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
const targetRecentTokens = threshold * RECENT_MESSAGES_RATIO;
|
|
60
|
-
const splitIndex = this.findSplitIndexByTokens(recentMessages, targetRecentTokens);
|
|
61
|
-
const messagesToSummarize = recentMessages.slice(0, splitIndex);
|
|
62
|
-
if (messagesToSummarize.length === 0) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
const recentToKeep = recentMessages.length - messagesToSummarize.length;
|
|
66
|
-
logger.info("[Summarization] Starting", {
|
|
67
|
-
requestId,
|
|
68
|
-
sessionId: session.sessionId,
|
|
69
|
-
messagesToSummarize: messagesToSummarize.length,
|
|
70
|
-
recentToKeep,
|
|
71
|
-
hasPreviousSummary: !!session.summarizedMessage,
|
|
72
|
-
});
|
|
73
|
-
try {
|
|
74
|
-
const summary = await generateSummary(messagesToSummarize, config, logPrefix, session.summarizedMessage, requestId);
|
|
75
|
-
if (!summary) {
|
|
76
|
-
logger.warn(`${logPrefix} Summary generation failed`, {
|
|
77
|
-
requestId,
|
|
78
|
-
sessionId: session.sessionId,
|
|
79
|
-
durationMs: Date.now() - startTime,
|
|
80
|
-
});
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
const lastSummarized = messagesToSummarize[messagesToSummarize.length - 1];
|
|
84
|
-
session.summarizedUpToMessageId = lastSummarized.id;
|
|
85
|
-
session.summarizedMessage = summary;
|
|
86
|
-
logger.info("[Summarization] Complete", {
|
|
87
|
-
requestId,
|
|
88
|
-
sessionId: session.sessionId,
|
|
89
|
-
summaryChars: summary.length,
|
|
90
|
-
newPointerId: lastSummarized.id,
|
|
91
|
-
durationMs: Date.now() - startTime,
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
catch (err) {
|
|
95
|
-
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
96
|
-
logger.error("[Summarization] Error", {
|
|
97
|
-
requestId,
|
|
98
|
-
sessionId: session.sessionId,
|
|
99
|
-
error: errorMessage,
|
|
100
|
-
durationMs: Date.now() - startTime,
|
|
101
|
-
});
|
|
102
|
-
throw err;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Estimate total tokens for a message array.
|
|
107
|
-
* @param messages - Array of chat messages
|
|
108
|
-
* @returns Estimated token count
|
|
109
|
-
*/
|
|
110
|
-
estimateTokens(messages) {
|
|
111
|
-
return messages.reduce((total, msg) => {
|
|
112
|
-
return total + TokenUtils.estimateTokenCount(msg.content);
|
|
113
|
-
}, 0);
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Find split index to keep recent messages within target token count.
|
|
117
|
-
* Works backwards from the most recent message to find the split point.
|
|
118
|
-
* @param messages - Array of messages to analyze
|
|
119
|
-
* @param targetRecentTokens - Target token count for recent messages
|
|
120
|
-
* @returns Index at which to split (messages before this index will be summarized)
|
|
121
|
-
*/
|
|
122
|
-
findSplitIndexByTokens(messages, targetRecentTokens) {
|
|
123
|
-
let recentTokens = 0;
|
|
124
|
-
let splitIndex = messages.length;
|
|
125
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
126
|
-
const msgTokens = TokenUtils.estimateTokenCount(messages[i].content);
|
|
127
|
-
if (recentTokens + msgTokens > targetRecentTokens) {
|
|
128
|
-
splitIndex = i + 1;
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
recentTokens += msgTokens;
|
|
132
|
-
}
|
|
133
|
-
// Ensure at least one message is summarized
|
|
134
|
-
return Math.max(1, splitIndex);
|
|
135
|
-
}
|
|
136
|
-
}
|