@juspay/neurolink 9.5.2 → 9.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (356) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +29 -25
  3. package/dist/agent/directTools.d.ts +5 -5
  4. package/dist/cli/commands/config.d.ts +9 -9
  5. package/dist/cli/commands/serve.d.ts +37 -0
  6. package/dist/cli/commands/serve.js +302 -229
  7. package/dist/cli/commands/setup-anthropic.d.ts +2 -2
  8. package/dist/cli/commands/setup-azure.d.ts +2 -2
  9. package/dist/cli/commands/setup-bedrock.d.ts +2 -2
  10. package/dist/cli/commands/setup-gcp.d.ts +2 -2
  11. package/dist/cli/commands/setup-google-ai.d.ts +2 -2
  12. package/dist/cli/commands/setup-huggingface.d.ts +2 -2
  13. package/dist/cli/commands/setup-mistral.d.ts +2 -2
  14. package/dist/cli/commands/setup-openai.d.ts +2 -2
  15. package/dist/cli/commands/setup.d.ts +2 -2
  16. package/dist/cli/factories/commandFactory.js +16 -2
  17. package/dist/cli/loop/optionsSchema.d.ts +2 -2
  18. package/dist/cli/loop/session.d.ts +4 -0
  19. package/dist/cli/loop/session.js +49 -4
  20. package/dist/cli/utils/interactiveSetup.d.ts +4 -4
  21. package/dist/config/conversationMemory.d.ts +2 -0
  22. package/dist/config/conversationMemory.js +5 -5
  23. package/dist/constants/contextWindows.d.ts +46 -0
  24. package/dist/constants/contextWindows.js +156 -0
  25. package/dist/context/budgetChecker.d.ts +18 -0
  26. package/dist/context/budgetChecker.js +71 -0
  27. package/dist/context/contextCompactor.d.ts +22 -0
  28. package/dist/context/contextCompactor.js +106 -0
  29. package/dist/context/effectiveHistory.d.ts +52 -0
  30. package/dist/context/effectiveHistory.js +105 -0
  31. package/dist/context/errorDetection.d.ts +14 -0
  32. package/dist/context/errorDetection.js +124 -0
  33. package/dist/context/fileSummarizationService.d.ts +54 -0
  34. package/dist/context/fileSummarizationService.js +255 -0
  35. package/dist/context/fileSummarizer.d.ts +56 -0
  36. package/dist/context/fileSummarizer.js +145 -0
  37. package/dist/context/fileTokenBudget.d.ts +53 -0
  38. package/dist/context/fileTokenBudget.js +127 -0
  39. package/dist/context/prompts/summarizationPrompt.d.ts +17 -0
  40. package/dist/context/prompts/summarizationPrompt.js +110 -0
  41. package/dist/context/stages/fileReadDeduplicator.d.ts +10 -0
  42. package/dist/context/stages/fileReadDeduplicator.js +66 -0
  43. package/dist/context/stages/slidingWindowTruncator.d.ts +11 -0
  44. package/dist/context/stages/slidingWindowTruncator.js +42 -0
  45. package/dist/context/stages/structuredSummarizer.d.ts +10 -0
  46. package/dist/context/stages/structuredSummarizer.js +49 -0
  47. package/dist/context/stages/toolOutputPruner.d.ts +10 -0
  48. package/dist/context/stages/toolOutputPruner.js +52 -0
  49. package/dist/context/summarizationEngine.d.ts +45 -0
  50. package/dist/context/summarizationEngine.js +110 -0
  51. package/dist/context/toolOutputLimits.d.ts +17 -0
  52. package/dist/context/toolOutputLimits.js +84 -0
  53. package/dist/context/toolPairRepair.d.ts +16 -0
  54. package/dist/context/toolPairRepair.js +66 -0
  55. package/dist/core/conversationMemoryManager.d.ts +5 -15
  56. package/dist/core/conversationMemoryManager.js +15 -75
  57. package/dist/core/modules/MessageBuilder.d.ts +1 -1
  58. package/dist/core/modules/MessageBuilder.js +2 -0
  59. package/dist/core/modules/TelemetryHandler.d.ts +2 -3
  60. package/dist/core/modules/TelemetryHandler.js +3 -3
  61. package/dist/core/modules/ToolsManager.d.ts +2 -2
  62. package/dist/core/redisConversationMemoryManager.d.ts +8 -14
  63. package/dist/core/redisConversationMemoryManager.js +69 -78
  64. package/dist/factories/providerFactory.d.ts +2 -2
  65. package/dist/files/fileReferenceRegistry.d.ts +276 -0
  66. package/dist/files/fileReferenceRegistry.js +1543 -0
  67. package/dist/files/fileTools.d.ts +423 -0
  68. package/dist/files/fileTools.js +449 -0
  69. package/dist/files/index.d.ts +14 -0
  70. package/dist/files/index.js +13 -0
  71. package/dist/files/streamingReader.d.ts +93 -0
  72. package/dist/files/streamingReader.js +321 -0
  73. package/dist/files/types.d.ts +23 -0
  74. package/dist/files/types.js +23 -0
  75. package/dist/image-gen/imageGenTools.d.ts +2 -2
  76. package/dist/image-gen/types.d.ts +12 -12
  77. package/dist/lib/agent/directTools.d.ts +7 -7
  78. package/dist/lib/config/conversationMemory.d.ts +2 -0
  79. package/dist/lib/config/conversationMemory.js +5 -5
  80. package/dist/lib/constants/contextWindows.d.ts +46 -0
  81. package/dist/lib/constants/contextWindows.js +157 -0
  82. package/dist/lib/context/budgetChecker.d.ts +18 -0
  83. package/dist/lib/context/budgetChecker.js +72 -0
  84. package/dist/lib/context/contextCompactor.d.ts +22 -0
  85. package/dist/lib/context/contextCompactor.js +107 -0
  86. package/dist/lib/context/effectiveHistory.d.ts +52 -0
  87. package/dist/lib/context/effectiveHistory.js +106 -0
  88. package/dist/lib/context/errorDetection.d.ts +14 -0
  89. package/dist/lib/context/errorDetection.js +125 -0
  90. package/dist/lib/context/fileSummarizationService.d.ts +54 -0
  91. package/dist/lib/context/fileSummarizationService.js +256 -0
  92. package/dist/lib/context/fileSummarizer.d.ts +56 -0
  93. package/dist/lib/context/fileSummarizer.js +146 -0
  94. package/dist/lib/context/fileTokenBudget.d.ts +53 -0
  95. package/dist/lib/context/fileTokenBudget.js +128 -0
  96. package/dist/lib/context/prompts/summarizationPrompt.d.ts +17 -0
  97. package/dist/lib/context/prompts/summarizationPrompt.js +111 -0
  98. package/dist/lib/context/stages/fileReadDeduplicator.d.ts +10 -0
  99. package/dist/lib/context/stages/fileReadDeduplicator.js +67 -0
  100. package/dist/lib/context/stages/slidingWindowTruncator.d.ts +11 -0
  101. package/dist/lib/context/stages/slidingWindowTruncator.js +43 -0
  102. package/dist/lib/context/stages/structuredSummarizer.d.ts +10 -0
  103. package/dist/lib/context/stages/structuredSummarizer.js +50 -0
  104. package/dist/lib/context/stages/toolOutputPruner.d.ts +10 -0
  105. package/dist/lib/context/stages/toolOutputPruner.js +53 -0
  106. package/dist/lib/context/summarizationEngine.d.ts +45 -0
  107. package/dist/lib/context/summarizationEngine.js +111 -0
  108. package/dist/lib/context/toolOutputLimits.d.ts +17 -0
  109. package/dist/lib/context/toolOutputLimits.js +85 -0
  110. package/dist/lib/context/toolPairRepair.d.ts +16 -0
  111. package/dist/lib/context/toolPairRepair.js +67 -0
  112. package/dist/lib/core/conversationMemoryManager.d.ts +5 -15
  113. package/dist/lib/core/conversationMemoryManager.js +15 -75
  114. package/dist/lib/core/modules/MessageBuilder.d.ts +1 -1
  115. package/dist/lib/core/modules/MessageBuilder.js +2 -0
  116. package/dist/lib/core/modules/TelemetryHandler.d.ts +2 -3
  117. package/dist/lib/core/modules/TelemetryHandler.js +3 -3
  118. package/dist/lib/core/modules/ToolsManager.d.ts +2 -2
  119. package/dist/lib/core/redisConversationMemoryManager.d.ts +8 -14
  120. package/dist/lib/core/redisConversationMemoryManager.js +69 -78
  121. package/dist/lib/factories/providerFactory.d.ts +2 -2
  122. package/dist/lib/files/fileReferenceRegistry.d.ts +276 -0
  123. package/dist/lib/files/fileReferenceRegistry.js +1544 -0
  124. package/dist/lib/files/fileTools.d.ts +423 -0
  125. package/dist/lib/files/fileTools.js +450 -0
  126. package/dist/lib/files/index.d.ts +14 -0
  127. package/dist/lib/files/index.js +14 -0
  128. package/dist/lib/files/streamingReader.d.ts +93 -0
  129. package/dist/lib/files/streamingReader.js +322 -0
  130. package/dist/lib/files/types.d.ts +23 -0
  131. package/dist/lib/files/types.js +24 -0
  132. package/dist/lib/image-gen/imageGenTools.d.ts +2 -2
  133. package/dist/lib/image-gen/types.d.ts +12 -12
  134. package/dist/lib/memory/mem0Initializer.d.ts +2 -2
  135. package/dist/lib/neurolink.d.ts +61 -2
  136. package/dist/lib/neurolink.js +619 -307
  137. package/dist/lib/processors/archive/ArchiveProcessor.d.ts +327 -0
  138. package/dist/lib/processors/archive/ArchiveProcessor.js +1309 -0
  139. package/dist/lib/processors/archive/index.d.ts +33 -0
  140. package/dist/lib/processors/archive/index.js +43 -0
  141. package/dist/lib/processors/base/types.d.ts +70 -64
  142. package/dist/lib/processors/base/types.js +6 -0
  143. package/dist/lib/processors/cli/fileProcessorCli.d.ts +8 -8
  144. package/dist/lib/processors/cli/fileProcessorCli.js +5 -5
  145. package/dist/lib/processors/config/mimeTypes.js +25 -0
  146. package/dist/lib/processors/config/sizeLimits.d.ts +52 -40
  147. package/dist/lib/processors/config/sizeLimits.js +56 -44
  148. package/dist/lib/processors/document/ExcelProcessor.d.ts +14 -0
  149. package/dist/lib/processors/document/ExcelProcessor.js +72 -1
  150. package/dist/lib/processors/document/PptxProcessor.d.ts +63 -0
  151. package/dist/lib/processors/document/PptxProcessor.js +158 -0
  152. package/dist/lib/processors/document/index.d.ts +1 -0
  153. package/dist/lib/processors/document/index.js +6 -0
  154. package/dist/lib/processors/errors/FileErrorCode.d.ts +2 -2
  155. package/dist/lib/processors/errors/errorHelpers.d.ts +2 -2
  156. package/dist/lib/processors/errors/errorSerializer.d.ts +4 -4
  157. package/dist/lib/processors/index.d.ts +8 -2
  158. package/dist/lib/processors/index.js +5 -2
  159. package/dist/lib/processors/integration/FileProcessorIntegration.d.ts +8 -8
  160. package/dist/lib/processors/integration/FileProcessorIntegration.js +7 -7
  161. package/dist/lib/processors/media/AudioProcessor.d.ts +328 -0
  162. package/dist/lib/processors/media/AudioProcessor.js +708 -0
  163. package/dist/lib/processors/media/VideoProcessor.d.ts +350 -0
  164. package/dist/lib/processors/media/VideoProcessor.js +992 -0
  165. package/dist/lib/processors/media/index.d.ts +27 -0
  166. package/dist/lib/processors/media/index.js +37 -0
  167. package/dist/lib/processors/registry/ProcessorRegistry.d.ts +19 -5
  168. package/dist/lib/processors/registry/ProcessorRegistry.js +103 -8
  169. package/dist/lib/processors/registry/index.d.ts +1 -1
  170. package/dist/lib/processors/registry/index.js +1 -1
  171. package/dist/lib/processors/registry/types.d.ts +2 -2
  172. package/dist/lib/providers/googleAiStudio.d.ts +34 -0
  173. package/dist/lib/providers/googleAiStudio.js +267 -397
  174. package/dist/lib/providers/googleVertex.d.ts +55 -1
  175. package/dist/lib/providers/googleVertex.js +452 -719
  176. package/dist/lib/providers/sagemaker/detection.d.ts +6 -6
  177. package/dist/lib/providers/sagemaker/diagnostics.d.ts +4 -4
  178. package/dist/lib/providers/sagemaker/parsers.d.ts +4 -4
  179. package/dist/lib/rag/chunkers/RecursiveChunker.js +2 -2
  180. package/dist/lib/rag/document/loaders.d.ts +6 -71
  181. package/dist/lib/rag/document/loaders.js +5 -5
  182. package/dist/lib/rag/graphRag/graphRAG.js +26 -9
  183. package/dist/lib/rag/metadata/MetadataExtractorFactory.d.ts +5 -55
  184. package/dist/lib/rag/metadata/metadataExtractor.js +6 -3
  185. package/dist/lib/rag/pipeline/RAGPipeline.d.ts +8 -126
  186. package/dist/lib/rag/pipeline/RAGPipeline.js +11 -11
  187. package/dist/lib/rag/pipeline/contextAssembly.d.ts +3 -42
  188. package/dist/lib/rag/pipeline/contextAssembly.js +6 -3
  189. package/dist/lib/rag/reranker/RerankerFactory.d.ts +5 -60
  190. package/dist/lib/rag/resilience/CircuitBreaker.d.ts +3 -33
  191. package/dist/lib/rag/resilience/RetryHandler.d.ts +2 -21
  192. package/dist/lib/rag/retrieval/hybridSearch.d.ts +3 -41
  193. package/dist/lib/rag/retrieval/vectorQueryTool.d.ts +2 -13
  194. package/dist/lib/rag/retrieval/vectorQueryTool.js +4 -3
  195. package/dist/lib/rag/types.d.ts +3 -3
  196. package/dist/lib/sdk/toolRegistration.d.ts +2 -2
  197. package/dist/lib/server/middleware/cache.d.ts +2 -2
  198. package/dist/lib/server/middleware/rateLimit.d.ts +2 -2
  199. package/dist/lib/server/routes/mcpRoutes.js +277 -249
  200. package/dist/lib/server/routes/memoryRoutes.js +287 -281
  201. package/dist/lib/server/utils/validation.d.ts +10 -10
  202. package/dist/lib/session/globalSessionState.d.ts +2 -2
  203. package/dist/lib/telemetry/telemetryService.d.ts +2 -2
  204. package/dist/lib/types/common.d.ts +39 -0
  205. package/dist/lib/types/contextTypes.d.ts +255 -0
  206. package/dist/lib/types/contextTypes.js +0 -2
  207. package/dist/lib/types/conversation.d.ts +62 -0
  208. package/dist/lib/types/conversationMemoryInterface.d.ts +27 -0
  209. package/dist/lib/types/conversationMemoryInterface.js +7 -0
  210. package/dist/lib/types/fileReferenceTypes.d.ts +222 -0
  211. package/dist/lib/types/fileReferenceTypes.js +9 -0
  212. package/dist/lib/types/fileTypes.d.ts +26 -3
  213. package/dist/lib/types/generateTypes.d.ts +22 -1
  214. package/dist/lib/types/index.d.ts +4 -5
  215. package/dist/lib/types/index.js +8 -10
  216. package/dist/lib/types/modelTypes.d.ts +2 -2
  217. package/dist/lib/types/processorTypes.d.ts +597 -0
  218. package/dist/lib/types/processorTypes.js +91 -0
  219. package/dist/lib/types/ragTypes.d.ts +481 -0
  220. package/dist/lib/types/ragTypes.js +8 -0
  221. package/dist/lib/types/sdkTypes.d.ts +17 -18
  222. package/dist/lib/types/streamTypes.d.ts +11 -1
  223. package/dist/lib/utils/async/retry.d.ts +2 -2
  224. package/dist/lib/utils/async/withTimeout.js +3 -1
  225. package/dist/lib/utils/conversationMemory.d.ts +12 -6
  226. package/dist/lib/utils/conversationMemory.js +76 -36
  227. package/dist/lib/utils/fileDetector.d.ts +62 -0
  228. package/dist/lib/utils/fileDetector.js +1014 -14
  229. package/dist/lib/utils/json/safeParse.d.ts +2 -2
  230. package/dist/lib/utils/messageBuilder.js +806 -153
  231. package/dist/lib/utils/modelChoices.d.ts +2 -2
  232. package/dist/lib/utils/multimodalOptionsBuilder.d.ts +2 -1
  233. package/dist/lib/utils/multimodalOptionsBuilder.js +1 -0
  234. package/dist/lib/utils/rateLimiter.d.ts +2 -2
  235. package/dist/lib/utils/sanitizers/filename.d.ts +4 -4
  236. package/dist/lib/utils/sanitizers/svg.d.ts +2 -2
  237. package/dist/lib/utils/thinkingConfig.d.ts +6 -6
  238. package/dist/lib/utils/tokenEstimation.d.ts +68 -0
  239. package/dist/lib/utils/tokenEstimation.js +113 -0
  240. package/dist/lib/utils/tokenUtils.d.ts +4 -4
  241. package/dist/lib/utils/ttsProcessor.d.ts +2 -2
  242. package/dist/lib/workflow/config.d.ts +150 -150
  243. package/dist/memory/mem0Initializer.d.ts +2 -2
  244. package/dist/neurolink.d.ts +61 -2
  245. package/dist/neurolink.js +619 -307
  246. package/dist/processors/archive/ArchiveProcessor.d.ts +327 -0
  247. package/dist/processors/archive/ArchiveProcessor.js +1308 -0
  248. package/dist/processors/archive/index.d.ts +33 -0
  249. package/dist/processors/archive/index.js +42 -0
  250. package/dist/processors/base/types.d.ts +70 -64
  251. package/dist/processors/base/types.js +6 -0
  252. package/dist/processors/cli/fileProcessorCli.d.ts +8 -8
  253. package/dist/processors/cli/fileProcessorCli.js +5 -5
  254. package/dist/processors/config/mimeTypes.js +25 -0
  255. package/dist/processors/config/sizeLimits.d.ts +52 -40
  256. package/dist/processors/config/sizeLimits.js +56 -44
  257. package/dist/processors/document/ExcelProcessor.d.ts +14 -0
  258. package/dist/processors/document/ExcelProcessor.js +72 -1
  259. package/dist/processors/document/PptxProcessor.d.ts +63 -0
  260. package/dist/processors/document/PptxProcessor.js +157 -0
  261. package/dist/processors/document/index.d.ts +1 -0
  262. package/dist/processors/document/index.js +6 -0
  263. package/dist/processors/errors/FileErrorCode.d.ts +2 -2
  264. package/dist/processors/errors/errorHelpers.d.ts +2 -2
  265. package/dist/processors/errors/errorSerializer.d.ts +4 -4
  266. package/dist/processors/index.d.ts +8 -2
  267. package/dist/processors/index.js +5 -2
  268. package/dist/processors/integration/FileProcessorIntegration.d.ts +8 -8
  269. package/dist/processors/integration/FileProcessorIntegration.js +7 -7
  270. package/dist/processors/media/AudioProcessor.d.ts +328 -0
  271. package/dist/processors/media/AudioProcessor.js +707 -0
  272. package/dist/processors/media/VideoProcessor.d.ts +350 -0
  273. package/dist/processors/media/VideoProcessor.js +991 -0
  274. package/dist/processors/media/ffprobe-static.d.ts +4 -0
  275. package/dist/processors/media/index.d.ts +27 -0
  276. package/dist/processors/media/index.js +36 -0
  277. package/dist/processors/registry/ProcessorRegistry.d.ts +19 -5
  278. package/dist/processors/registry/ProcessorRegistry.js +103 -8
  279. package/dist/processors/registry/index.d.ts +1 -1
  280. package/dist/processors/registry/index.js +1 -1
  281. package/dist/processors/registry/types.d.ts +2 -2
  282. package/dist/providers/googleAiStudio.d.ts +34 -0
  283. package/dist/providers/googleAiStudio.js +267 -397
  284. package/dist/providers/googleVertex.d.ts +55 -1
  285. package/dist/providers/googleVertex.js +452 -719
  286. package/dist/providers/sagemaker/detection.d.ts +6 -6
  287. package/dist/providers/sagemaker/diagnostics.d.ts +4 -4
  288. package/dist/providers/sagemaker/parsers.d.ts +4 -4
  289. package/dist/rag/chunkers/RecursiveChunker.js +2 -2
  290. package/dist/rag/document/loaders.d.ts +6 -71
  291. package/dist/rag/document/loaders.js +5 -5
  292. package/dist/rag/graphRag/graphRAG.js +26 -9
  293. package/dist/rag/metadata/MetadataExtractorFactory.d.ts +5 -55
  294. package/dist/rag/metadata/metadataExtractor.js +6 -3
  295. package/dist/rag/pipeline/RAGPipeline.d.ts +8 -126
  296. package/dist/rag/pipeline/RAGPipeline.js +11 -11
  297. package/dist/rag/pipeline/contextAssembly.d.ts +3 -42
  298. package/dist/rag/pipeline/contextAssembly.js +6 -3
  299. package/dist/rag/reranker/RerankerFactory.d.ts +5 -60
  300. package/dist/rag/resilience/CircuitBreaker.d.ts +3 -33
  301. package/dist/rag/resilience/RetryHandler.d.ts +2 -21
  302. package/dist/rag/retrieval/hybridSearch.d.ts +3 -41
  303. package/dist/rag/retrieval/vectorQueryTool.d.ts +2 -13
  304. package/dist/rag/retrieval/vectorQueryTool.js +4 -3
  305. package/dist/rag/types.d.ts +3 -3
  306. package/dist/sdk/toolRegistration.d.ts +2 -2
  307. package/dist/server/middleware/cache.d.ts +2 -2
  308. package/dist/server/middleware/rateLimit.d.ts +2 -2
  309. package/dist/server/routes/mcpRoutes.js +277 -249
  310. package/dist/server/routes/memoryRoutes.js +287 -281
  311. package/dist/server/utils/validation.d.ts +4 -4
  312. package/dist/session/globalSessionState.d.ts +2 -2
  313. package/dist/telemetry/telemetryService.d.ts +2 -2
  314. package/dist/types/common.d.ts +39 -0
  315. package/dist/types/contextTypes.d.ts +255 -0
  316. package/dist/types/contextTypes.js +0 -2
  317. package/dist/types/conversation.d.ts +62 -0
  318. package/dist/types/conversationMemoryInterface.d.ts +27 -0
  319. package/dist/types/conversationMemoryInterface.js +6 -0
  320. package/dist/types/fileReferenceTypes.d.ts +222 -0
  321. package/dist/types/fileReferenceTypes.js +8 -0
  322. package/dist/types/fileTypes.d.ts +26 -3
  323. package/dist/types/generateTypes.d.ts +22 -1
  324. package/dist/types/index.d.ts +4 -5
  325. package/dist/types/index.js +8 -10
  326. package/dist/types/processorTypes.d.ts +597 -0
  327. package/dist/types/processorTypes.js +90 -0
  328. package/dist/types/ragTypes.d.ts +481 -0
  329. package/dist/types/ragTypes.js +7 -0
  330. package/dist/types/sdkTypes.d.ts +17 -18
  331. package/dist/types/streamTypes.d.ts +11 -1
  332. package/dist/utils/async/retry.d.ts +2 -2
  333. package/dist/utils/async/withTimeout.js +3 -1
  334. package/dist/utils/conversationMemory.d.ts +12 -6
  335. package/dist/utils/conversationMemory.js +76 -36
  336. package/dist/utils/fileDetector.d.ts +62 -0
  337. package/dist/utils/fileDetector.js +1014 -14
  338. package/dist/utils/json/safeParse.d.ts +2 -2
  339. package/dist/utils/messageBuilder.js +806 -153
  340. package/dist/utils/modelChoices.d.ts +2 -2
  341. package/dist/utils/multimodalOptionsBuilder.d.ts +2 -1
  342. package/dist/utils/multimodalOptionsBuilder.js +1 -0
  343. package/dist/utils/rateLimiter.d.ts +2 -2
  344. package/dist/utils/sanitizers/filename.d.ts +4 -4
  345. package/dist/utils/sanitizers/svg.d.ts +2 -2
  346. package/dist/utils/thinkingConfig.d.ts +6 -6
  347. package/dist/utils/tokenEstimation.d.ts +68 -0
  348. package/dist/utils/tokenEstimation.js +112 -0
  349. package/dist/utils/tokenUtils.d.ts +4 -4
  350. package/dist/utils/ttsProcessor.d.ts +2 -2
  351. package/dist/workflow/config.d.ts +104 -104
  352. package/package.json +18 -6
  353. package/dist/lib/utils/conversationMemoryUtils.d.ts +0 -25
  354. package/dist/lib/utils/conversationMemoryUtils.js +0 -138
  355. package/dist/utils/conversationMemoryUtils.d.ts +0 -25
  356. package/dist/utils/conversationMemoryUtils.js +0 -137
@@ -8,9 +8,9 @@
8
8
  *
9
9
  * Follows the same UX patterns as setup-openai and setup-gcp
10
10
  */
11
- interface GoogleAISetupArgv {
11
+ type GoogleAISetupArgv = {
12
12
  check?: boolean;
13
13
  nonInteractive?: boolean;
14
- }
14
+ };
15
15
  export declare function handleGoogleAISetup(argv: GoogleAISetupArgv): Promise<void>;
16
16
  export {};
@@ -1,8 +1,8 @@
1
1
  import type { Arguments, CommandBuilder } from "yargs";
2
- interface SetupHuggingFaceArgs {
2
+ type SetupHuggingFaceArgs = {
3
3
  check?: boolean;
4
4
  "non-interactive"?: boolean;
5
- }
5
+ };
6
6
  export declare const handleHuggingFaceSetup: (argv: Arguments<SetupHuggingFaceArgs>) => Promise<void>;
7
7
  export declare const setupHuggingFaceBuilder: CommandBuilder;
8
8
  export {};
@@ -1,8 +1,8 @@
1
1
  import type { Arguments, CommandBuilder } from "yargs";
2
- interface SetupMistralArgs {
2
+ type SetupMistralArgs = {
3
3
  check?: boolean;
4
4
  "non-interactive"?: boolean;
5
- }
5
+ };
6
6
  export declare const handleMistralSetup: (argv: Arguments<SetupMistralArgs>) => Promise<void>;
7
7
  export declare const setupMistralBuilder: CommandBuilder;
8
8
  export {};
@@ -8,9 +8,9 @@
8
8
  *
9
9
  * Follows the same UX patterns as setup-gcp and setup-bedrock
10
10
  */
11
- interface OpenAISetupArgv {
11
+ type OpenAISetupArgv = {
12
12
  check?: boolean;
13
13
  nonInteractive?: boolean;
14
- }
14
+ };
15
15
  export declare function handleOpenAISetup(argv: OpenAISetupArgv): Promise<void>;
16
16
  export {};
@@ -5,13 +5,13 @@
5
5
  * Provides a beautiful welcome experience for new users and guided
6
6
  * provider selection, while delegating to existing setup commands.
7
7
  */
8
- interface SetupArgs {
8
+ type SetupArgs = {
9
9
  provider?: string;
10
10
  list?: boolean;
11
11
  status?: boolean;
12
12
  interactive?: boolean;
13
13
  help?: boolean;
14
- }
14
+ };
15
15
  /**
16
16
  * Main setup command handler
17
17
  */
@@ -6,7 +6,7 @@ import { ModelResolver } from "../../lib/models/modelResolver.js";
6
6
  import { globalSession } from "../../lib/session/globalSessionState.js";
7
7
  // Use TokenUsage from standard types - no local interface needed
8
8
  import { ContextFactory, } from "../../lib/types/contextTypes.js";
9
- import { checkRedisAvailability } from "../../lib/utils/conversationMemoryUtils.js";
9
+ import { checkRedisAvailability } from "../../lib/utils/conversationMemory.js";
10
10
  import { normalizeEvaluationData } from "../../lib/utils/evaluationUtils.js";
11
11
  import { logger } from "../../lib/utils/logger.js";
12
12
  import { createThinkingConfigFromRecord } from "../../lib/utils/thinkingConfig.js";
@@ -1152,6 +1152,16 @@ export class CLICommandFactory {
1152
1152
  description: "List available conversations and exit",
1153
1153
  alias: "l",
1154
1154
  },
1155
+ "compact-threshold": {
1156
+ describe: "Context compaction trigger threshold (0.0-1.0)",
1157
+ type: "number",
1158
+ default: 0.8,
1159
+ },
1160
+ "disable-compaction": {
1161
+ describe: "Disable automatic context compaction",
1162
+ type: "boolean",
1163
+ default: false,
1164
+ },
1155
1165
  })
1156
1166
  .example("$0 loop", "Start interactive session with conversation selection")
1157
1167
  .example("$0 loop --new", "Force start new conversation")
@@ -1165,7 +1175,7 @@ export class CLICommandFactory {
1165
1175
  return;
1166
1176
  }
1167
1177
  let conversationMemoryConfig;
1168
- const { enableConversationMemory, maxSessions, maxTurnsPerSession, autoRedis, listConversations, } = argv;
1178
+ const { enableConversationMemory, maxSessions, maxTurnsPerSession, autoRedis, listConversations, compactThreshold, disableCompaction, } = argv;
1169
1179
  if (enableConversationMemory) {
1170
1180
  let storageType = "memory";
1171
1181
  if (autoRedis) {
@@ -1188,6 +1198,10 @@ export class CLICommandFactory {
1188
1198
  enabled: true,
1189
1199
  maxSessions: maxSessions,
1190
1200
  maxTurnsPerSession: maxTurnsPerSession,
1201
+ contextCompaction: {
1202
+ enabled: !disableCompaction,
1203
+ threshold: compactThreshold,
1204
+ },
1191
1205
  };
1192
1206
  }
1193
1207
  // Handle --list-conversations option
@@ -1,8 +1,8 @@
1
- import type { TextGenerationOptions } from "../../lib/types/generateTypes.js";
2
1
  import type { OptionSchema } from "../../lib/types/cli.js";
2
+ import type { TextGenerationOptions } from "../../lib/types/generateTypes.js";
3
3
  /**
4
4
  * Master schema for all text generation options.
5
5
  * This object provides metadata for validation and help text in the CLI loop.
6
6
  * It is derived from the main TextGenerationOptions interface to ensure consistency.
7
7
  */
8
- export declare const textGenerationOptionsSchema: Record<keyof Omit<TextGenerationOptions, "prompt" | "input" | "schema" | "tools" | "context" | "conversationHistory" | "conversationMessages" | "conversationMemoryConfig" | "originalPrompt" | "middleware" | "expectedOutcome" | "evaluationCriteria" | "region" | "csvOptions" | "tts" | "thinkingConfig">, OptionSchema>;
8
+ export declare const textGenerationOptionsSchema: Record<keyof Omit<TextGenerationOptions, "prompt" | "input" | "schema" | "tools" | "context" | "conversationHistory" | "conversationMessages" | "conversationMemoryConfig" | "originalPrompt" | "middleware" | "expectedOutcome" | "evaluationCriteria" | "region" | "csvOptions" | "tts" | "thinkingConfig" | "fileRegistry">, OptionSchema>;
@@ -21,6 +21,10 @@ export declare class LoopSession {
21
21
  * Handle conversation selection logic when no direct resume is specified
22
22
  */
23
23
  private handleConversationSelection;
24
+ /**
25
+ * Check context budget and warn if approaching limits.
26
+ */
27
+ private checkContextBudgetWarning;
24
28
  /**
25
29
  * Clean up session resources and connections
26
30
  */
@@ -1,12 +1,13 @@
1
1
  import chalk from "chalk";
2
2
  import readline from "readline";
3
- import { logger } from "../../lib/utils/logger.js";
3
+ import { checkContextBudget } from "../../lib/context/budgetChecker.js";
4
+ import { NeuroLink } from "../../lib/neurolink.js";
4
5
  import { globalSession } from "../../lib/session/globalSessionState.js";
5
- import { textGenerationOptionsSchema } from "./optionsSchema.js";
6
+ import { logger } from "../../lib/utils/logger.js";
7
+ import { displayConversationPreview, displaySessionMessage, getConversationPreview, loadCommandHistory, parseValue, restoreSessionVariables, saveCommandToHistory, verifyConversationContext, } from "../../lib/utils/loopUtils.js";
6
8
  import { handleError } from "../errorHandler.js";
7
9
  import { ConversationSelector } from "./conversationSelector.js";
8
- import { NeuroLink } from "../../lib/neurolink.js";
9
- import { displaySessionMessage, verifyConversationContext, getConversationPreview, loadCommandHistory, saveCommandToHistory, displayConversationPreview, parseValue, restoreSessionVariables, } from "../../lib/utils/loopUtils.js";
10
+ import { textGenerationOptionsSchema } from "./optionsSchema.js";
10
11
  // Banner Art
11
12
  const NEUROLINK_BANNER = `
12
13
  ▗▖ ▗▖▗▄▄▄▖▗▖ ▗▖▗▄▄▖ ▗▄▖ ▗▖ ▗▄▄▄▖▗▖ ▗▖▗▖ ▗▖
@@ -120,6 +121,8 @@ export class LoopSession {
120
121
  })
121
122
  .exitProcess(false)
122
123
  .parse(processedCommand);
124
+ // Check context budget after each generation command
125
+ await this.checkContextBudgetWarning();
123
126
  }
124
127
  catch (error) {
125
128
  // Handle command execution errors gracefully
@@ -205,6 +208,48 @@ export class LoopSession {
205
208
  this.sessionId = globalSession.setLoopSession(this.conversationMemoryConfig);
206
209
  }
207
210
  }
211
+ /**
212
+ * Check context budget and warn if approaching limits.
213
+ */
214
+ async checkContextBudgetWarning() {
215
+ const compactionConfig = this.conversationMemoryConfig?.contextCompaction;
216
+ if (!compactionConfig?.enabled) {
217
+ return;
218
+ }
219
+ try {
220
+ const provider = globalSession.getSessionVariable("provider") || "openai";
221
+ const model = globalSession.getSessionVariable("model");
222
+ const neurolinkInstance = globalSession.getOrCreateNeuroLink();
223
+ if (!neurolinkInstance?.conversationMemory || !this.sessionId) {
224
+ return;
225
+ }
226
+ const messages = await neurolinkInstance.conversationMemory.buildContextMessages(this.sessionId);
227
+ if (!messages || messages.length === 0) {
228
+ return;
229
+ }
230
+ const budgetResult = checkContextBudget({
231
+ provider,
232
+ model,
233
+ conversationMessages: messages.map((m) => ({
234
+ role: m.role,
235
+ content: m.content,
236
+ })),
237
+ });
238
+ const usagePercent = budgetResult.usageRatio * 100;
239
+ if (budgetResult.shouldCompact) {
240
+ logger.always(chalk.yellow(`\n Context usage: ${usagePercent.toFixed(0)}% of window (${budgetResult.estimatedInputTokens.toLocaleString()} / ${budgetResult.availableInputTokens.toLocaleString()} tokens)`));
241
+ logger.always(chalk.yellow(` Auto-compaction will trigger to preserve conversation quality.\n`));
242
+ }
243
+ else if (usagePercent > 60) {
244
+ logger.always(chalk.gray(` Context: ${usagePercent.toFixed(0)}% used`));
245
+ }
246
+ }
247
+ catch (error) {
248
+ logger.debug("Context budget check failed", {
249
+ error: error instanceof Error ? error.message : String(error),
250
+ });
251
+ }
252
+ }
208
253
  /**
209
254
  * Clean up session resources and connections
210
255
  */
@@ -5,7 +5,7 @@
5
5
  * credential collection, testing, and environment file management.
6
6
  */
7
7
  import { AIProviderName } from "../../lib/constants/enums.js";
8
- export interface ProviderConfig {
8
+ export type ProviderConfig = {
9
9
  id: AIProviderName;
10
10
  name: string;
11
11
  description: string;
@@ -16,9 +16,9 @@ export interface ProviderConfig {
16
16
  default?: string;
17
17
  optional?: boolean;
18
18
  }>;
19
- }
19
+ };
20
20
  export declare const PROVIDER_CONFIGS: ProviderConfig[];
21
- export interface SetupResult {
21
+ export type SetupResult = {
22
22
  selectedProviders: AIProviderName[];
23
23
  credentials: Record<string, string>;
24
24
  envFileBackup?: string;
@@ -28,7 +28,7 @@ export interface SetupResult {
28
28
  error?: string;
29
29
  responseTime?: number;
30
30
  }>;
31
- }
31
+ };
32
32
  /**
33
33
  * Run the interactive setup wizard
34
34
  */
@@ -5,6 +5,8 @@
5
5
  import type { ConversationMemoryConfig } from "../types/conversation.js";
6
6
  /**
7
7
  * Default maximum number of turns per session
8
+ * @deprecated Use tokenThreshold-based memory management instead.
9
+ * This constant is kept for backward compatibility with legacy turn-based memory.
8
10
  */
9
11
  export declare const DEFAULT_MAX_TURNS_PER_SESSION = 50;
10
12
  /**
@@ -4,6 +4,8 @@
4
4
  */
5
5
  /**
6
6
  * Default maximum number of turns per session
7
+ * @deprecated Use tokenThreshold-based memory management instead.
8
+ * This constant is kept for backward compatibility with legacy turn-based memory.
7
9
  */
8
10
  export const DEFAULT_MAX_TURNS_PER_SESSION = 50;
9
11
  /**
@@ -77,10 +79,8 @@ export function getConversationMemoryDefaults() {
77
79
  : undefined,
78
80
  summarizationProvider: process.env.NEUROLINK_SUMMARIZATION_PROVIDER || "vertex",
79
81
  summarizationModel: process.env.NEUROLINK_SUMMARIZATION_MODEL || "gemini-2.5-flash",
80
- // Deprecated (for backward compatibility)
81
- maxTurnsPerSession: Number(process.env.NEUROLINK_MEMORY_MAX_TURNS_PER_SESSION) ||
82
- DEFAULT_MAX_TURNS_PER_SESSION,
83
- summarizationThresholdTurns: Number(process.env.NEUROLINK_SUMMARIZATION_THRESHOLD_TURNS) || 20,
84
- summarizationTargetTurns: Number(process.env.NEUROLINK_SUMMARIZATION_TARGET_TURNS) || 10,
82
+ // @deprecated Turn-based fields are superseded by tokenThreshold-based memory.
83
+ // Kept for backward compatibility; will be removed in a future major version.
84
+ maxTurnsPerSession: DEFAULT_MAX_TURNS_PER_SESSION,
85
85
  };
86
86
  }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Context Window Registry
3
+ *
4
+ * Accurate per-provider, per-model context window sizes (INPUT token limits).
5
+ * These are distinct from OUTPUT token limits in tokens.ts.
6
+ *
7
+ * Sources:
8
+ * - Anthropic: https://docs.anthropic.com/en/docs/about-claude/models
9
+ * - OpenAI: https://platform.openai.com/docs/models
10
+ * - Google: https://ai.google.dev/gemini-api/docs/models
11
+ * - Others: Provider documentation as of Feb 2026
12
+ */
13
+ /** Default context window when provider/model is unknown */
14
+ export declare const DEFAULT_CONTEXT_WINDOW = 128000;
15
+ /** Maximum output reserve when maxTokens not specified */
16
+ export declare const MAX_DEFAULT_OUTPUT_RESERVE = 64000;
17
+ /** Default output reserve ratio (35% of context) */
18
+ export declare const DEFAULT_OUTPUT_RESERVE_RATIO = 0.35;
19
+ /**
20
+ * Per-provider, per-model context window sizes.
21
+ * The "_default" key is the fallback for unknown models within a provider.
22
+ */
23
+ export declare const MODEL_CONTEXT_WINDOWS: Record<string, Record<string, number>>;
24
+ /**
25
+ * Resolve context window size for a provider/model combination.
26
+ *
27
+ * Priority:
28
+ * 1. Exact model match under provider
29
+ * 2. Provider's _default
30
+ * 3. Global DEFAULT_CONTEXT_WINDOW
31
+ */
32
+ export declare function getContextWindowSize(provider: string, model?: string): number;
33
+ /**
34
+ * Calculate output token reserve for a given context window.
35
+ *
36
+ * @param contextWindow - Total context window size
37
+ * @param maxTokens - Explicit maxTokens from user config (if set)
38
+ * @returns Number of tokens reserved for output
39
+ */
40
+ export declare function getOutputReserve(contextWindow: number, maxTokens?: number): number;
41
+ /**
42
+ * Calculate available input tokens for a given provider/model.
43
+ *
44
+ * available = contextWindow - outputReserve
45
+ */
46
+ export declare function getAvailableInputTokens(provider: string, model?: string, maxTokens?: number): number;
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Context Window Registry
3
+ *
4
+ * Accurate per-provider, per-model context window sizes (INPUT token limits).
5
+ * These are distinct from OUTPUT token limits in tokens.ts.
6
+ *
7
+ * Sources:
8
+ * - Anthropic: https://docs.anthropic.com/en/docs/about-claude/models
9
+ * - OpenAI: https://platform.openai.com/docs/models
10
+ * - Google: https://ai.google.dev/gemini-api/docs/models
11
+ * - Others: Provider documentation as of Feb 2026
12
+ */
13
+ /** Default context window when provider/model is unknown */
14
+ export const DEFAULT_CONTEXT_WINDOW = 128_000;
15
+ /** Maximum output reserve when maxTokens not specified */
16
+ export const MAX_DEFAULT_OUTPUT_RESERVE = 64_000;
17
+ /** Default output reserve ratio (35% of context) */
18
+ export const DEFAULT_OUTPUT_RESERVE_RATIO = 0.35;
19
+ /**
20
+ * Per-provider, per-model context window sizes.
21
+ * The "_default" key is the fallback for unknown models within a provider.
22
+ */
23
+ export const MODEL_CONTEXT_WINDOWS = {
24
+ anthropic: {
25
+ _default: 200_000,
26
+ "claude-opus-4-20250514": 200_000,
27
+ "claude-sonnet-4-20250514": 200_000,
28
+ "claude-3-7-sonnet-20250219": 200_000,
29
+ "claude-3-5-sonnet-20241022": 200_000,
30
+ "claude-3-5-haiku-20241022": 200_000,
31
+ "claude-3-opus-20240229": 200_000,
32
+ "claude-3-sonnet-20240229": 200_000,
33
+ "claude-3-haiku-20240307": 200_000,
34
+ },
35
+ openai: {
36
+ _default: 128_000,
37
+ "gpt-4o": 128_000,
38
+ "gpt-4o-mini": 128_000,
39
+ "gpt-4-turbo": 128_000,
40
+ "gpt-4": 8_192,
41
+ "gpt-3.5-turbo": 16_385,
42
+ o1: 200_000,
43
+ "o1-mini": 128_000,
44
+ "o1-pro": 200_000,
45
+ o3: 200_000,
46
+ "o3-mini": 200_000,
47
+ "o4-mini": 200_000,
48
+ "gpt-4.1": 1_047_576,
49
+ "gpt-4.1-mini": 1_047_576,
50
+ "gpt-4.1-nano": 1_047_576,
51
+ "gpt-5": 1_047_576,
52
+ },
53
+ "google-ai": {
54
+ _default: 1_048_576,
55
+ "gemini-2.5-pro": 1_048_576,
56
+ "gemini-2.5-flash": 1_048_576,
57
+ "gemini-2.0-flash": 1_048_576,
58
+ "gemini-1.5-pro": 2_097_152,
59
+ "gemini-1.5-flash": 1_048_576,
60
+ "gemini-3-flash-preview": 1_048_576,
61
+ "gemini-3-pro-preview": 1_048_576,
62
+ },
63
+ vertex: {
64
+ _default: 1_048_576,
65
+ "gemini-2.5-pro": 1_048_576,
66
+ "gemini-2.5-flash": 1_048_576,
67
+ "gemini-2.0-flash": 1_048_576,
68
+ "gemini-1.5-pro": 2_097_152,
69
+ "gemini-1.5-flash": 1_048_576,
70
+ },
71
+ bedrock: {
72
+ _default: 200_000,
73
+ "anthropic.claude-3-5-sonnet-20241022-v2:0": 200_000,
74
+ "anthropic.claude-3-5-haiku-20241022-v1:0": 200_000,
75
+ "anthropic.claude-3-opus-20240229-v1:0": 200_000,
76
+ "anthropic.claude-3-sonnet-20240229-v1:0": 200_000,
77
+ "anthropic.claude-3-haiku-20240307-v1:0": 200_000,
78
+ "amazon.nova-pro-v1:0": 300_000,
79
+ "amazon.nova-lite-v1:0": 300_000,
80
+ },
81
+ azure: {
82
+ _default: 128_000,
83
+ "gpt-4o": 128_000,
84
+ "gpt-4o-mini": 128_000,
85
+ "gpt-4-turbo": 128_000,
86
+ "gpt-4": 8_192,
87
+ },
88
+ mistral: {
89
+ _default: 128_000,
90
+ "mistral-large-latest": 128_000,
91
+ "mistral-medium-latest": 32_000,
92
+ "mistral-small-latest": 128_000,
93
+ "codestral-latest": 256_000,
94
+ },
95
+ ollama: {
96
+ _default: 128_000,
97
+ },
98
+ litellm: {
99
+ _default: 128_000,
100
+ },
101
+ huggingface: {
102
+ _default: 32_000,
103
+ },
104
+ sagemaker: {
105
+ _default: 128_000,
106
+ },
107
+ };
108
+ /**
109
+ * Resolve context window size for a provider/model combination.
110
+ *
111
+ * Priority:
112
+ * 1. Exact model match under provider
113
+ * 2. Provider's _default
114
+ * 3. Global DEFAULT_CONTEXT_WINDOW
115
+ */
116
+ export function getContextWindowSize(provider, model) {
117
+ const providerWindows = MODEL_CONTEXT_WINDOWS[provider];
118
+ if (!providerWindows) {
119
+ return DEFAULT_CONTEXT_WINDOW;
120
+ }
121
+ if (model && providerWindows[model] !== undefined) {
122
+ return providerWindows[model];
123
+ }
124
+ // Try partial match (model name may be a prefix)
125
+ if (model) {
126
+ for (const [key, value] of Object.entries(providerWindows)) {
127
+ if (key !== "_default" && model.startsWith(key)) {
128
+ return value;
129
+ }
130
+ }
131
+ }
132
+ return providerWindows._default ?? DEFAULT_CONTEXT_WINDOW;
133
+ }
134
+ /**
135
+ * Calculate output token reserve for a given context window.
136
+ *
137
+ * @param contextWindow - Total context window size
138
+ * @param maxTokens - Explicit maxTokens from user config (if set)
139
+ * @returns Number of tokens reserved for output
140
+ */
141
+ export function getOutputReserve(contextWindow, maxTokens) {
142
+ if (maxTokens !== undefined && maxTokens > 0) {
143
+ return maxTokens;
144
+ }
145
+ return Math.min(MAX_DEFAULT_OUTPUT_RESERVE, Math.ceil(contextWindow * DEFAULT_OUTPUT_RESERVE_RATIO));
146
+ }
147
+ /**
148
+ * Calculate available input tokens for a given provider/model.
149
+ *
150
+ * available = contextWindow - outputReserve
151
+ */
152
+ export function getAvailableInputTokens(provider, model, maxTokens) {
153
+ const contextWindow = getContextWindowSize(provider, model);
154
+ const outputReserve = getOutputReserve(contextWindow, maxTokens);
155
+ return contextWindow - outputReserve;
156
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Context Budget Checker
3
+ *
4
+ * Pre-generation validation that estimates total input token cost
5
+ * and compares against the model's available input space.
6
+ *
7
+ * This runs BEFORE every LLM call to prevent context overflow.
8
+ */
9
+ import type { BudgetCheckResult, BudgetCheckParams } from "../types/contextTypes.js";
10
+ export type { BudgetCheckResult, BudgetCheckParams, } from "../types/contextTypes.js";
11
+ /**
12
+ * Check whether a request fits within the model's context budget.
13
+ *
14
+ * Estimates total input tokens from: system prompt + tool definitions +
15
+ * conversation history + current prompt + file attachments, and compares
16
+ * against available input space.
17
+ */
18
+ export declare function checkContextBudget(params: BudgetCheckParams): BudgetCheckResult;
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Context Budget Checker
3
+ *
4
+ * Pre-generation validation that estimates total input token cost
5
+ * and compares against the model's available input space.
6
+ *
7
+ * This runs BEFORE every LLM call to prevent context overflow.
8
+ */
9
+ import { getAvailableInputTokens } from "../constants/contextWindows.js";
10
+ import { estimateMessagesTokens, estimateTokens, TOKENS_PER_MESSAGE, } from "../utils/tokenEstimation.js";
11
+ /** Default compaction threshold (80% of available input) */
12
+ const DEFAULT_COMPACTION_THRESHOLD = 0.8;
13
+ /** Estimated tokens per tool definition */
14
+ const TOKENS_PER_TOOL_DEFINITION = 200;
15
+ /**
16
+ * Check whether a request fits within the model's context budget.
17
+ *
18
+ * Estimates total input tokens from: system prompt + tool definitions +
19
+ * conversation history + current prompt + file attachments, and compares
20
+ * against available input space.
21
+ */
22
+ export function checkContextBudget(params) {
23
+ const { provider, model, maxTokens, systemPrompt, conversationMessages, currentPrompt, toolDefinitions, fileAttachments, compactionThreshold = DEFAULT_COMPACTION_THRESHOLD, } = params;
24
+ const availableInputTokens = getAvailableInputTokens(provider, model, maxTokens);
25
+ // Estimate each category
26
+ const systemPromptTokens = systemPrompt
27
+ ? estimateTokens(systemPrompt, provider) + TOKENS_PER_MESSAGE
28
+ : 0;
29
+ const conversationHistoryTokens = conversationMessages?.length
30
+ ? estimateMessagesTokens(conversationMessages, provider)
31
+ : 0;
32
+ const currentPromptTokens = currentPrompt
33
+ ? estimateTokens(currentPrompt, provider) + TOKENS_PER_MESSAGE
34
+ : 0;
35
+ const toolDefinitionTokens = toolDefinitions?.length
36
+ ? toolDefinitions.reduce((sum, tool) => {
37
+ try {
38
+ const serialized = JSON.stringify(tool);
39
+ return sum + Math.ceil(serialized.length / 4);
40
+ }
41
+ catch {
42
+ return sum + TOKENS_PER_TOOL_DEFINITION;
43
+ }
44
+ }, 0)
45
+ : 0;
46
+ const fileAttachmentTokens = fileAttachments?.length
47
+ ? fileAttachments.reduce((sum, file) => sum + estimateTokens(file.content, provider), 0)
48
+ : 0;
49
+ const estimatedInputTokens = systemPromptTokens +
50
+ conversationHistoryTokens +
51
+ currentPromptTokens +
52
+ toolDefinitionTokens +
53
+ fileAttachmentTokens;
54
+ const usageRatio = availableInputTokens > 0 ? estimatedInputTokens / availableInputTokens : 1;
55
+ const withinBudget = estimatedInputTokens <= availableInputTokens;
56
+ const shouldCompact = usageRatio >= compactionThreshold;
57
+ return {
58
+ withinBudget,
59
+ estimatedInputTokens,
60
+ availableInputTokens,
61
+ usageRatio,
62
+ shouldCompact,
63
+ breakdown: {
64
+ systemPrompt: systemPromptTokens,
65
+ conversationHistory: conversationHistoryTokens,
66
+ currentPrompt: currentPromptTokens,
67
+ toolDefinitions: toolDefinitionTokens,
68
+ fileAttachments: fileAttachmentTokens,
69
+ },
70
+ };
71
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * ContextCompactor
3
+ *
4
+ * Orchestrates multi-stage context reduction:
5
+ *
6
+ * Stage 1: Tool Output Pruning (cheapest -- no LLM call)
7
+ * Stage 2: File Read Deduplication (cheap -- no LLM call)
8
+ * Stage 3: LLM Summarization (expensive -- requires LLM call)
9
+ * Stage 4: Sliding Window Truncation (fallback -- no LLM call)
10
+ */
11
+ import type { ChatMessage, ConversationMemoryConfig } from "../types/conversation.js";
12
+ import type { CompactionResult, CompactionConfig } from "../types/contextTypes.js";
13
+ export type { CompactionResult, CompactionConfig, } from "../types/contextTypes.js";
14
+ export type CompactionStage = "prune" | "deduplicate" | "summarize" | "truncate";
15
+ export declare class ContextCompactor {
16
+ private config;
17
+ constructor(config?: CompactionConfig);
18
+ /**
19
+ * Run the multi-stage compaction pipeline until messages fit within budget.
20
+ */
21
+ compact(messages: ChatMessage[], targetTokens: number, memoryConfig?: Partial<ConversationMemoryConfig>): Promise<CompactionResult>;
22
+ }