@trenchwork/coder 1.3.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 (560) hide show
  1. package/LICENSE +16 -0
  2. package/README.md +173 -0
  3. package/agents/trenchwork-code.rules.json +199 -0
  4. package/dist/bin/deepseek.d.ts +3 -0
  5. package/dist/bin/deepseek.d.ts.map +1 -0
  6. package/dist/bin/deepseek.js +23 -0
  7. package/dist/bin/deepseek.js.map +1 -0
  8. package/dist/capabilities/baseCapability.d.ts +72 -0
  9. package/dist/capabilities/baseCapability.d.ts.map +1 -0
  10. package/dist/capabilities/baseCapability.js +183 -0
  11. package/dist/capabilities/baseCapability.js.map +1 -0
  12. package/dist/capabilities/bashCapability.d.ts +13 -0
  13. package/dist/capabilities/bashCapability.d.ts.map +1 -0
  14. package/dist/capabilities/bashCapability.js +24 -0
  15. package/dist/capabilities/bashCapability.js.map +1 -0
  16. package/dist/capabilities/editCapability.d.ts +17 -0
  17. package/dist/capabilities/editCapability.d.ts.map +1 -0
  18. package/dist/capabilities/editCapability.js +27 -0
  19. package/dist/capabilities/editCapability.js.map +1 -0
  20. package/dist/capabilities/enhancedGitCapability.d.ts +7 -0
  21. package/dist/capabilities/enhancedGitCapability.d.ts.map +1 -0
  22. package/dist/capabilities/enhancedGitCapability.js +220 -0
  23. package/dist/capabilities/enhancedGitCapability.js.map +1 -0
  24. package/dist/capabilities/filesystemCapability.d.ts +13 -0
  25. package/dist/capabilities/filesystemCapability.d.ts.map +1 -0
  26. package/dist/capabilities/filesystemCapability.js +24 -0
  27. package/dist/capabilities/filesystemCapability.js.map +1 -0
  28. package/dist/capabilities/gitHistoryCapability.d.ts +6 -0
  29. package/dist/capabilities/gitHistoryCapability.d.ts.map +1 -0
  30. package/dist/capabilities/gitHistoryCapability.js +184 -0
  31. package/dist/capabilities/gitHistoryCapability.js.map +1 -0
  32. package/dist/capabilities/hitlCapability.d.ts +18 -0
  33. package/dist/capabilities/hitlCapability.d.ts.map +1 -0
  34. package/dist/capabilities/hitlCapability.js +29 -0
  35. package/dist/capabilities/hitlCapability.js.map +1 -0
  36. package/dist/capabilities/index.d.ts +11 -0
  37. package/dist/capabilities/index.d.ts.map +1 -0
  38. package/dist/capabilities/index.js +16 -0
  39. package/dist/capabilities/index.js.map +1 -0
  40. package/dist/capabilities/memoryCapability.d.ts +10 -0
  41. package/dist/capabilities/memoryCapability.d.ts.map +1 -0
  42. package/dist/capabilities/memoryCapability.js +22 -0
  43. package/dist/capabilities/memoryCapability.js.map +1 -0
  44. package/dist/capabilities/notebookCapability.d.ts +6 -0
  45. package/dist/capabilities/notebookCapability.d.ts.map +1 -0
  46. package/dist/capabilities/notebookCapability.js +17 -0
  47. package/dist/capabilities/notebookCapability.js.map +1 -0
  48. package/dist/capabilities/searchCapability.d.ts +19 -0
  49. package/dist/capabilities/searchCapability.d.ts.map +1 -0
  50. package/dist/capabilities/searchCapability.js +29 -0
  51. package/dist/capabilities/searchCapability.js.map +1 -0
  52. package/dist/capabilities/skillCapability.d.ts +6 -0
  53. package/dist/capabilities/skillCapability.d.ts.map +1 -0
  54. package/dist/capabilities/skillCapability.js +17 -0
  55. package/dist/capabilities/skillCapability.js.map +1 -0
  56. package/dist/capabilities/todoCapability.d.ts +11 -0
  57. package/dist/capabilities/todoCapability.d.ts.map +1 -0
  58. package/dist/capabilities/todoCapability.js +22 -0
  59. package/dist/capabilities/todoCapability.js.map +1 -0
  60. package/dist/capabilities/toolManifest.d.ts +3 -0
  61. package/dist/capabilities/toolManifest.d.ts.map +1 -0
  62. package/dist/capabilities/toolManifest.js +163 -0
  63. package/dist/capabilities/toolManifest.js.map +1 -0
  64. package/dist/capabilities/toolRegistry.d.ts +25 -0
  65. package/dist/capabilities/toolRegistry.d.ts.map +1 -0
  66. package/dist/capabilities/toolRegistry.js +150 -0
  67. package/dist/capabilities/toolRegistry.js.map +1 -0
  68. package/dist/capabilities/unifiedCodingCapability.d.ts +62 -0
  69. package/dist/capabilities/unifiedCodingCapability.d.ts.map +1 -0
  70. package/dist/capabilities/unifiedCodingCapability.js +790 -0
  71. package/dist/capabilities/unifiedCodingCapability.js.map +1 -0
  72. package/dist/capabilities/webCapability.d.ts +23 -0
  73. package/dist/capabilities/webCapability.d.ts.map +1 -0
  74. package/dist/capabilities/webCapability.js +33 -0
  75. package/dist/capabilities/webCapability.js.map +1 -0
  76. package/dist/config.d.ts +25 -0
  77. package/dist/config.d.ts.map +1 -0
  78. package/dist/config.js +181 -0
  79. package/dist/config.js.map +1 -0
  80. package/dist/contracts/agent-profiles.schema.json +25 -0
  81. package/dist/contracts/agent-schemas.json +158 -0
  82. package/dist/contracts/models.schema.json +9 -0
  83. package/dist/contracts/module-schema.json +367 -0
  84. package/dist/contracts/schemas/agent-profile.schema.json +157 -0
  85. package/dist/contracts/schemas/agent-rules.schema.json +238 -0
  86. package/dist/contracts/schemas/agent-schemas.schema.json +528 -0
  87. package/dist/contracts/schemas/agent.schema.json +90 -0
  88. package/dist/contracts/schemas/tool-selection.schema.json +174 -0
  89. package/dist/contracts/tools.schema.json +42 -0
  90. package/dist/contracts/unified-schema.json +40 -0
  91. package/dist/contracts/v1/agent.d.ts +225 -0
  92. package/dist/contracts/v1/agent.d.ts.map +1 -0
  93. package/dist/contracts/v1/agent.js +8 -0
  94. package/dist/contracts/v1/agent.js.map +1 -0
  95. package/dist/contracts/v1/agentProfileManifest.d.ts +60 -0
  96. package/dist/contracts/v1/agentProfileManifest.d.ts.map +1 -0
  97. package/dist/contracts/v1/agentProfileManifest.js +9 -0
  98. package/dist/contracts/v1/agentProfileManifest.js.map +1 -0
  99. package/dist/contracts/v1/agentRules.d.ts +60 -0
  100. package/dist/contracts/v1/agentRules.d.ts.map +1 -0
  101. package/dist/contracts/v1/agentRules.js +10 -0
  102. package/dist/contracts/v1/agentRules.js.map +1 -0
  103. package/dist/contracts/v1/provider.d.ts +149 -0
  104. package/dist/contracts/v1/provider.d.ts.map +1 -0
  105. package/dist/contracts/v1/provider.js +7 -0
  106. package/dist/contracts/v1/provider.js.map +1 -0
  107. package/dist/contracts/v1/tool.d.ts +136 -0
  108. package/dist/contracts/v1/tool.d.ts.map +1 -0
  109. package/dist/contracts/v1/tool.js +7 -0
  110. package/dist/contracts/v1/tool.js.map +1 -0
  111. package/dist/contracts/v1/toolAccess.d.ts +43 -0
  112. package/dist/contracts/v1/toolAccess.d.ts.map +1 -0
  113. package/dist/contracts/v1/toolAccess.js +9 -0
  114. package/dist/contracts/v1/toolAccess.js.map +1 -0
  115. package/dist/core/adversarial.d.ts +38 -0
  116. package/dist/core/adversarial.d.ts.map +1 -0
  117. package/dist/core/adversarial.js +106 -0
  118. package/dist/core/adversarial.js.map +1 -0
  119. package/dist/core/adversarialCorrection.d.ts +22 -0
  120. package/dist/core/adversarialCorrection.d.ts.map +1 -0
  121. package/dist/core/adversarialCorrection.js +25 -0
  122. package/dist/core/adversarialCorrection.js.map +1 -0
  123. package/dist/core/agent.d.ts +331 -0
  124. package/dist/core/agent.d.ts.map +1 -0
  125. package/dist/core/agent.js +1637 -0
  126. package/dist/core/agent.js.map +1 -0
  127. package/dist/core/agentProfileManifest.d.ts +3 -0
  128. package/dist/core/agentProfileManifest.d.ts.map +1 -0
  129. package/dist/core/agentProfileManifest.js +188 -0
  130. package/dist/core/agentProfileManifest.js.map +1 -0
  131. package/dist/core/agentProfiles.d.ts +22 -0
  132. package/dist/core/agentProfiles.d.ts.map +1 -0
  133. package/dist/core/agentProfiles.js +35 -0
  134. package/dist/core/agentProfiles.js.map +1 -0
  135. package/dist/core/agentRegistry.d.ts +111 -0
  136. package/dist/core/agentRegistry.d.ts.map +1 -0
  137. package/dist/core/agentRegistry.js +229 -0
  138. package/dist/core/agentRegistry.js.map +1 -0
  139. package/dist/core/agentRulebook.d.ts +11 -0
  140. package/dist/core/agentRulebook.d.ts.map +1 -0
  141. package/dist/core/agentRulebook.js +136 -0
  142. package/dist/core/agentRulebook.js.map +1 -0
  143. package/dist/core/agentSchemaLoader.d.ts +131 -0
  144. package/dist/core/agentSchemaLoader.d.ts.map +1 -0
  145. package/dist/core/agentSchemaLoader.js +235 -0
  146. package/dist/core/agentSchemaLoader.js.map +1 -0
  147. package/dist/core/aiErrorFixer.d.ts +57 -0
  148. package/dist/core/aiErrorFixer.d.ts.map +1 -0
  149. package/dist/core/aiErrorFixer.js +214 -0
  150. package/dist/core/aiErrorFixer.js.map +1 -0
  151. package/dist/core/bashCommandGuidance.d.ts +16 -0
  152. package/dist/core/bashCommandGuidance.d.ts.map +1 -0
  153. package/dist/core/bashCommandGuidance.js +40 -0
  154. package/dist/core/bashCommandGuidance.js.map +1 -0
  155. package/dist/core/compactionNote.d.ts +13 -0
  156. package/dist/core/compactionNote.d.ts.map +1 -0
  157. package/dist/core/compactionNote.js +13 -0
  158. package/dist/core/compactionNote.js.map +1 -0
  159. package/dist/core/constants.d.ts +31 -0
  160. package/dist/core/constants.d.ts.map +1 -0
  161. package/dist/core/constants.js +62 -0
  162. package/dist/core/constants.js.map +1 -0
  163. package/dist/core/contextManager.d.ts +271 -0
  164. package/dist/core/contextManager.d.ts.map +1 -0
  165. package/dist/core/contextManager.js +1076 -0
  166. package/dist/core/contextManager.js.map +1 -0
  167. package/dist/core/contextUsage.d.ts +28 -0
  168. package/dist/core/contextUsage.d.ts.map +1 -0
  169. package/dist/core/contextUsage.js +62 -0
  170. package/dist/core/contextUsage.js.map +1 -0
  171. package/dist/core/contextWindow.d.ts +42 -0
  172. package/dist/core/contextWindow.d.ts.map +1 -0
  173. package/dist/core/contextWindow.js +127 -0
  174. package/dist/core/contextWindow.js.map +1 -0
  175. package/dist/core/customCommands.d.ts +19 -0
  176. package/dist/core/customCommands.d.ts.map +1 -0
  177. package/dist/core/customCommands.js +85 -0
  178. package/dist/core/customCommands.js.map +1 -0
  179. package/dist/core/diffPanel.d.ts +30 -0
  180. package/dist/core/diffPanel.d.ts.map +1 -0
  181. package/dist/core/diffPanel.js +48 -0
  182. package/dist/core/diffPanel.js.map +1 -0
  183. package/dist/core/errorClassification.d.ts +44 -0
  184. package/dist/core/errorClassification.d.ts.map +1 -0
  185. package/dist/core/errorClassification.js +333 -0
  186. package/dist/core/errorClassification.js.map +1 -0
  187. package/dist/core/errors/apiKeyErrors.d.ts +11 -0
  188. package/dist/core/errors/apiKeyErrors.d.ts.map +1 -0
  189. package/dist/core/errors/apiKeyErrors.js +159 -0
  190. package/dist/core/errors/apiKeyErrors.js.map +1 -0
  191. package/dist/core/errors/errorTypes.d.ts +111 -0
  192. package/dist/core/errors/errorTypes.d.ts.map +1 -0
  193. package/dist/core/errors/errorTypes.js +345 -0
  194. package/dist/core/errors/errorTypes.js.map +1 -0
  195. package/dist/core/errors/index.d.ts +50 -0
  196. package/dist/core/errors/index.d.ts.map +1 -0
  197. package/dist/core/errors/index.js +156 -0
  198. package/dist/core/errors/index.js.map +1 -0
  199. package/dist/core/errors/networkErrors.d.ts +14 -0
  200. package/dist/core/errors/networkErrors.d.ts.map +1 -0
  201. package/dist/core/errors/networkErrors.js +53 -0
  202. package/dist/core/errors/networkErrors.js.map +1 -0
  203. package/dist/core/errors/safetyValidator.d.ts +109 -0
  204. package/dist/core/errors/safetyValidator.d.ts.map +1 -0
  205. package/dist/core/errors/safetyValidator.js +272 -0
  206. package/dist/core/errors/safetyValidator.js.map +1 -0
  207. package/dist/core/errors.d.ts +4 -0
  208. package/dist/core/errors.d.ts.map +1 -0
  209. package/dist/core/errors.js +33 -0
  210. package/dist/core/errors.js.map +1 -0
  211. package/dist/core/failureRegistry.d.ts +30 -0
  212. package/dist/core/failureRegistry.d.ts.map +1 -0
  213. package/dist/core/failureRegistry.js +74 -0
  214. package/dist/core/failureRegistry.js.map +1 -0
  215. package/dist/core/fileMentions.d.ts +40 -0
  216. package/dist/core/fileMentions.d.ts.map +1 -0
  217. package/dist/core/fileMentions.js +136 -0
  218. package/dist/core/fileMentions.js.map +1 -0
  219. package/dist/core/finalResponseFormatter.d.ts +10 -0
  220. package/dist/core/finalResponseFormatter.d.ts.map +1 -0
  221. package/dist/core/finalResponseFormatter.js +14 -0
  222. package/dist/core/finalResponseFormatter.js.map +1 -0
  223. package/dist/core/guardrails.d.ts +146 -0
  224. package/dist/core/guardrails.d.ts.map +1 -0
  225. package/dist/core/guardrails.js +361 -0
  226. package/dist/core/guardrails.js.map +1 -0
  227. package/dist/core/hitl.d.ts +119 -0
  228. package/dist/core/hitl.d.ts.map +1 -0
  229. package/dist/core/hitl.js +396 -0
  230. package/dist/core/hitl.js.map +1 -0
  231. package/dist/core/hooks.d.ts +95 -0
  232. package/dist/core/hooks.d.ts.map +1 -0
  233. package/dist/core/hooks.js +236 -0
  234. package/dist/core/hooks.js.map +1 -0
  235. package/dist/core/hostedAuth.d.ts +88 -0
  236. package/dist/core/hostedAuth.d.ts.map +1 -0
  237. package/dist/core/hostedAuth.js +219 -0
  238. package/dist/core/hostedAuth.js.map +1 -0
  239. package/dist/core/inputProtection.d.ts +122 -0
  240. package/dist/core/inputProtection.d.ts.map +1 -0
  241. package/dist/core/inputProtection.js +422 -0
  242. package/dist/core/inputProtection.js.map +1 -0
  243. package/dist/core/modelDiscovery.d.ts +102 -0
  244. package/dist/core/modelDiscovery.d.ts.map +1 -0
  245. package/dist/core/modelDiscovery.js +416 -0
  246. package/dist/core/modelDiscovery.js.map +1 -0
  247. package/dist/core/multilinePasteHandler.d.ts +35 -0
  248. package/dist/core/multilinePasteHandler.d.ts.map +1 -0
  249. package/dist/core/multilinePasteHandler.js +81 -0
  250. package/dist/core/multilinePasteHandler.js.map +1 -0
  251. package/dist/core/permissionMode.d.ts +40 -0
  252. package/dist/core/permissionMode.d.ts.map +1 -0
  253. package/dist/core/permissionMode.js +86 -0
  254. package/dist/core/permissionMode.js.map +1 -0
  255. package/dist/core/postWriteDiagnostics.d.ts +32 -0
  256. package/dist/core/postWriteDiagnostics.d.ts.map +1 -0
  257. package/dist/core/postWriteDiagnostics.js +127 -0
  258. package/dist/core/postWriteDiagnostics.js.map +1 -0
  259. package/dist/core/preferences.d.ts +66 -0
  260. package/dist/core/preferences.d.ts.map +1 -0
  261. package/dist/core/preferences.js +310 -0
  262. package/dist/core/preferences.js.map +1 -0
  263. package/dist/core/quota.d.ts +61 -0
  264. package/dist/core/quota.d.ts.map +1 -0
  265. package/dist/core/quota.js +104 -0
  266. package/dist/core/quota.js.map +1 -0
  267. package/dist/core/quotaErrors.d.ts +42 -0
  268. package/dist/core/quotaErrors.d.ts.map +1 -0
  269. package/dist/core/quotaErrors.js +86 -0
  270. package/dist/core/quotaErrors.js.map +1 -0
  271. package/dist/core/refusalDetection.d.ts +2 -0
  272. package/dist/core/refusalDetection.d.ts.map +1 -0
  273. package/dist/core/refusalDetection.js +51 -0
  274. package/dist/core/refusalDetection.js.map +1 -0
  275. package/dist/core/relativeTime.d.ts +8 -0
  276. package/dist/core/relativeTime.d.ts.map +1 -0
  277. package/dist/core/relativeTime.js +29 -0
  278. package/dist/core/relativeTime.js.map +1 -0
  279. package/dist/core/resultVerification.d.ts +48 -0
  280. package/dist/core/resultVerification.d.ts.map +1 -0
  281. package/dist/core/resultVerification.js +127 -0
  282. package/dist/core/resultVerification.js.map +1 -0
  283. package/dist/core/rewind.d.ts +14 -0
  284. package/dist/core/rewind.d.ts.map +1 -0
  285. package/dist/core/rewind.js +25 -0
  286. package/dist/core/rewind.js.map +1 -0
  287. package/dist/core/schemaValidator.d.ts +49 -0
  288. package/dist/core/schemaValidator.d.ts.map +1 -0
  289. package/dist/core/schemaValidator.js +234 -0
  290. package/dist/core/schemaValidator.js.map +1 -0
  291. package/dist/core/secretStore.d.ts +59 -0
  292. package/dist/core/secretStore.d.ts.map +1 -0
  293. package/dist/core/secretStore.js +278 -0
  294. package/dist/core/secretStore.js.map +1 -0
  295. package/dist/core/sessionStorage.d.ts +10 -0
  296. package/dist/core/sessionStorage.d.ts.map +1 -0
  297. package/dist/core/sessionStorage.js +46 -0
  298. package/dist/core/sessionStorage.js.map +1 -0
  299. package/dist/core/sessionStore.d.ts +35 -0
  300. package/dist/core/sessionStore.d.ts.map +1 -0
  301. package/dist/core/sessionStore.js +190 -0
  302. package/dist/core/sessionStore.js.map +1 -0
  303. package/dist/core/shutdown.d.ts +34 -0
  304. package/dist/core/shutdown.d.ts.map +1 -0
  305. package/dist/core/shutdown.js +186 -0
  306. package/dist/core/shutdown.js.map +1 -0
  307. package/dist/core/slashCommands.d.ts +38 -0
  308. package/dist/core/slashCommands.d.ts.map +1 -0
  309. package/dist/core/slashCommands.js +72 -0
  310. package/dist/core/slashCommands.js.map +1 -0
  311. package/dist/core/subAgentNote.d.ts +15 -0
  312. package/dist/core/subAgentNote.d.ts.map +1 -0
  313. package/dist/core/subAgentNote.js +16 -0
  314. package/dist/core/subAgentNote.js.map +1 -0
  315. package/dist/core/sudoPasswordManager.d.ts +52 -0
  316. package/dist/core/sudoPasswordManager.d.ts.map +1 -0
  317. package/dist/core/sudoPasswordManager.js +115 -0
  318. package/dist/core/sudoPasswordManager.js.map +1 -0
  319. package/dist/core/taskCompletionDetector.d.ts +117 -0
  320. package/dist/core/taskCompletionDetector.d.ts.map +1 -0
  321. package/dist/core/taskCompletionDetector.js +532 -0
  322. package/dist/core/taskCompletionDetector.js.map +1 -0
  323. package/dist/core/testFailureMonitor.d.ts +67 -0
  324. package/dist/core/testFailureMonitor.d.ts.map +1 -0
  325. package/dist/core/testFailureMonitor.js +262 -0
  326. package/dist/core/testFailureMonitor.js.map +1 -0
  327. package/dist/core/thinkingVerbs.d.ts +31 -0
  328. package/dist/core/thinkingVerbs.d.ts.map +1 -0
  329. package/dist/core/thinkingVerbs.js +58 -0
  330. package/dist/core/thinkingVerbs.js.map +1 -0
  331. package/dist/core/toolPreconditions.d.ts +34 -0
  332. package/dist/core/toolPreconditions.d.ts.map +1 -0
  333. package/dist/core/toolPreconditions.js +242 -0
  334. package/dist/core/toolPreconditions.js.map +1 -0
  335. package/dist/core/toolRuntime.d.ts +193 -0
  336. package/dist/core/toolRuntime.d.ts.map +1 -0
  337. package/dist/core/toolRuntime.js +526 -0
  338. package/dist/core/toolRuntime.js.map +1 -0
  339. package/dist/core/turnGovernor.d.ts +63 -0
  340. package/dist/core/turnGovernor.d.ts.map +1 -0
  341. package/dist/core/turnGovernor.js +94 -0
  342. package/dist/core/turnGovernor.js.map +1 -0
  343. package/dist/core/types/utilityTypes.d.ts +183 -0
  344. package/dist/core/types/utilityTypes.d.ts.map +1 -0
  345. package/dist/core/types/utilityTypes.js +273 -0
  346. package/dist/core/types/utilityTypes.js.map +1 -0
  347. package/dist/core/types.d.ts +334 -0
  348. package/dist/core/types.d.ts.map +1 -0
  349. package/dist/core/types.js +76 -0
  350. package/dist/core/types.js.map +1 -0
  351. package/dist/core/updateChecker.d.ts +148 -0
  352. package/dist/core/updateChecker.d.ts.map +1 -0
  353. package/dist/core/updateChecker.js +605 -0
  354. package/dist/core/updateChecker.js.map +1 -0
  355. package/dist/core/usage.d.ts +28 -0
  356. package/dist/core/usage.d.ts.map +1 -0
  357. package/dist/core/usage.js +77 -0
  358. package/dist/core/usage.js.map +1 -0
  359. package/dist/headless/interactiveShell.d.ts +47 -0
  360. package/dist/headless/interactiveShell.d.ts.map +1 -0
  361. package/dist/headless/interactiveShell.js +2495 -0
  362. package/dist/headless/interactiveShell.js.map +1 -0
  363. package/dist/leanAgent.d.ts +73 -0
  364. package/dist/leanAgent.d.ts.map +1 -0
  365. package/dist/leanAgent.js +177 -0
  366. package/dist/leanAgent.js.map +1 -0
  367. package/dist/plugins/providers/deepseek/index.d.ts +12 -0
  368. package/dist/plugins/providers/deepseek/index.d.ts.map +1 -0
  369. package/dist/plugins/providers/deepseek/index.js +123 -0
  370. package/dist/plugins/providers/deepseek/index.js.map +1 -0
  371. package/dist/plugins/providers/index.d.ts +2 -0
  372. package/dist/plugins/providers/index.d.ts.map +1 -0
  373. package/dist/plugins/providers/index.js +10 -0
  374. package/dist/plugins/providers/index.js.map +1 -0
  375. package/dist/providers/baseProvider.d.ts +140 -0
  376. package/dist/providers/baseProvider.d.ts.map +1 -0
  377. package/dist/providers/baseProvider.js +230 -0
  378. package/dist/providers/baseProvider.js.map +1 -0
  379. package/dist/providers/openaiChatCompletionsProvider.d.ts +70 -0
  380. package/dist/providers/openaiChatCompletionsProvider.d.ts.map +1 -0
  381. package/dist/providers/openaiChatCompletionsProvider.js +971 -0
  382. package/dist/providers/openaiChatCompletionsProvider.js.map +1 -0
  383. package/dist/providers/providerFactory.d.ts +22 -0
  384. package/dist/providers/providerFactory.d.ts.map +1 -0
  385. package/dist/providers/providerFactory.js +25 -0
  386. package/dist/providers/providerFactory.js.map +1 -0
  387. package/dist/providers/resilientProvider.d.ts +96 -0
  388. package/dist/providers/resilientProvider.d.ts.map +1 -0
  389. package/dist/providers/resilientProvider.js +251 -0
  390. package/dist/providers/resilientProvider.js.map +1 -0
  391. package/dist/runtime/agentController.d.ts +137 -0
  392. package/dist/runtime/agentController.d.ts.map +1 -0
  393. package/dist/runtime/agentController.js +784 -0
  394. package/dist/runtime/agentController.js.map +1 -0
  395. package/dist/runtime/agentHost.d.ts +61 -0
  396. package/dist/runtime/agentHost.d.ts.map +1 -0
  397. package/dist/runtime/agentHost.js +158 -0
  398. package/dist/runtime/agentHost.js.map +1 -0
  399. package/dist/runtime/agentSession.d.ts +49 -0
  400. package/dist/runtime/agentSession.d.ts.map +1 -0
  401. package/dist/runtime/agentSession.js +218 -0
  402. package/dist/runtime/agentSession.js.map +1 -0
  403. package/dist/runtime/agentSpawningWiring.d.ts +32 -0
  404. package/dist/runtime/agentSpawningWiring.d.ts.map +1 -0
  405. package/dist/runtime/agentSpawningWiring.js +114 -0
  406. package/dist/runtime/agentSpawningWiring.js.map +1 -0
  407. package/dist/runtime/node.d.ts +7 -0
  408. package/dist/runtime/node.d.ts.map +1 -0
  409. package/dist/runtime/node.js +50 -0
  410. package/dist/runtime/node.js.map +1 -0
  411. package/dist/runtime/universal.d.ts +18 -0
  412. package/dist/runtime/universal.d.ts.map +1 -0
  413. package/dist/runtime/universal.js +21 -0
  414. package/dist/runtime/universal.js.map +1 -0
  415. package/dist/shell/liveStatus.d.ts +27 -0
  416. package/dist/shell/liveStatus.d.ts.map +1 -0
  417. package/dist/shell/liveStatus.js +53 -0
  418. package/dist/shell/liveStatus.js.map +1 -0
  419. package/dist/shell/systemPrompt.d.ts +12 -0
  420. package/dist/shell/systemPrompt.d.ts.map +1 -0
  421. package/dist/shell/systemPrompt.js +16 -0
  422. package/dist/shell/systemPrompt.js.map +1 -0
  423. package/dist/shell/toolPresentation.d.ts +54 -0
  424. package/dist/shell/toolPresentation.d.ts.map +1 -0
  425. package/dist/shell/toolPresentation.js +334 -0
  426. package/dist/shell/toolPresentation.js.map +1 -0
  427. package/dist/tools/bashTools.d.ts +11 -0
  428. package/dist/tools/bashTools.d.ts.map +1 -0
  429. package/dist/tools/bashTools.js +785 -0
  430. package/dist/tools/bashTools.js.map +1 -0
  431. package/dist/tools/diffUtils.d.ts +43 -0
  432. package/dist/tools/diffUtils.d.ts.map +1 -0
  433. package/dist/tools/diffUtils.js +607 -0
  434. package/dist/tools/diffUtils.js.map +1 -0
  435. package/dist/tools/editTools.d.ts +29 -0
  436. package/dist/tools/editTools.d.ts.map +1 -0
  437. package/dist/tools/editTools.js +792 -0
  438. package/dist/tools/editTools.js.map +1 -0
  439. package/dist/tools/fileChangeTracker.d.ts +47 -0
  440. package/dist/tools/fileChangeTracker.d.ts.map +1 -0
  441. package/dist/tools/fileChangeTracker.js +154 -0
  442. package/dist/tools/fileChangeTracker.js.map +1 -0
  443. package/dist/tools/fileReadTracker.d.ts +69 -0
  444. package/dist/tools/fileReadTracker.d.ts.map +1 -0
  445. package/dist/tools/fileReadTracker.js +213 -0
  446. package/dist/tools/fileReadTracker.js.map +1 -0
  447. package/dist/tools/fileTools.d.ts +3 -0
  448. package/dist/tools/fileTools.d.ts.map +1 -0
  449. package/dist/tools/fileTools.js +389 -0
  450. package/dist/tools/fileTools.js.map +1 -0
  451. package/dist/tools/grepTools.d.ts +3 -0
  452. package/dist/tools/grepTools.d.ts.map +1 -0
  453. package/dist/tools/grepTools.js +137 -0
  454. package/dist/tools/grepTools.js.map +1 -0
  455. package/dist/tools/hitlTools.d.ts +7 -0
  456. package/dist/tools/hitlTools.d.ts.map +1 -0
  457. package/dist/tools/hitlTools.js +185 -0
  458. package/dist/tools/hitlTools.js.map +1 -0
  459. package/dist/tools/memoryTools.d.ts +27 -0
  460. package/dist/tools/memoryTools.d.ts.map +1 -0
  461. package/dist/tools/memoryTools.js +197 -0
  462. package/dist/tools/memoryTools.js.map +1 -0
  463. package/dist/tools/notebookTools.d.ts +20 -0
  464. package/dist/tools/notebookTools.d.ts.map +1 -0
  465. package/dist/tools/notebookTools.js +140 -0
  466. package/dist/tools/notebookTools.js.map +1 -0
  467. package/dist/tools/searchTools.d.ts +12 -0
  468. package/dist/tools/searchTools.d.ts.map +1 -0
  469. package/dist/tools/searchTools.js +414 -0
  470. package/dist/tools/searchTools.js.map +1 -0
  471. package/dist/tools/skillTools.d.ts +24 -0
  472. package/dist/tools/skillTools.d.ts.map +1 -0
  473. package/dist/tools/skillTools.js +140 -0
  474. package/dist/tools/skillTools.js.map +1 -0
  475. package/dist/tools/todoTools.d.ts +23 -0
  476. package/dist/tools/todoTools.d.ts.map +1 -0
  477. package/dist/tools/todoTools.js +120 -0
  478. package/dist/tools/todoTools.js.map +1 -0
  479. package/dist/tools/webTools.d.ts +26 -0
  480. package/dist/tools/webTools.d.ts.map +1 -0
  481. package/dist/tools/webTools.js +467 -0
  482. package/dist/tools/webTools.js.map +1 -0
  483. package/dist/ui/ink/App.d.ts +53 -0
  484. package/dist/ui/ink/App.d.ts.map +1 -0
  485. package/dist/ui/ink/App.js +13 -0
  486. package/dist/ui/ink/App.js.map +1 -0
  487. package/dist/ui/ink/ChatStatic.d.ts +30 -0
  488. package/dist/ui/ink/ChatStatic.d.ts.map +1 -0
  489. package/dist/ui/ink/ChatStatic.js +83 -0
  490. package/dist/ui/ink/ChatStatic.js.map +1 -0
  491. package/dist/ui/ink/InkPromptController.d.ts +321 -0
  492. package/dist/ui/ink/InkPromptController.d.ts.map +1 -0
  493. package/dist/ui/ink/InkPromptController.js +667 -0
  494. package/dist/ui/ink/InkPromptController.js.map +1 -0
  495. package/dist/ui/ink/Menu.d.ts +21 -0
  496. package/dist/ui/ink/Menu.d.ts.map +1 -0
  497. package/dist/ui/ink/Menu.js +61 -0
  498. package/dist/ui/ink/Menu.js.map +1 -0
  499. package/dist/ui/ink/Prompt.d.ts +47 -0
  500. package/dist/ui/ink/Prompt.d.ts.map +1 -0
  501. package/dist/ui/ink/Prompt.js +571 -0
  502. package/dist/ui/ink/Prompt.js.map +1 -0
  503. package/dist/ui/ink/StatusLine.d.ts +35 -0
  504. package/dist/ui/ink/StatusLine.d.ts.map +1 -0
  505. package/dist/ui/ink/StatusLine.js +66 -0
  506. package/dist/ui/ink/StatusLine.js.map +1 -0
  507. package/dist/ui/ink/pasteBuffer.d.ts +44 -0
  508. package/dist/ui/ink/pasteBuffer.d.ts.map +1 -0
  509. package/dist/ui/ink/pasteBuffer.js +73 -0
  510. package/dist/ui/ink/pasteBuffer.js.map +1 -0
  511. package/dist/ui/theme.d.ts +351 -0
  512. package/dist/ui/theme.d.ts.map +1 -0
  513. package/dist/ui/theme.js +435 -0
  514. package/dist/ui/theme.js.map +1 -0
  515. package/dist/utils/analytics.d.ts +2 -0
  516. package/dist/utils/analytics.d.ts.map +1 -0
  517. package/dist/utils/analytics.js +51 -0
  518. package/dist/utils/analytics.js.map +1 -0
  519. package/dist/utils/asyncUtils.d.ts +95 -0
  520. package/dist/utils/asyncUtils.d.ts.map +1 -0
  521. package/dist/utils/asyncUtils.js +286 -0
  522. package/dist/utils/asyncUtils.js.map +1 -0
  523. package/dist/utils/debugLogger.d.ts +6 -0
  524. package/dist/utils/debugLogger.d.ts.map +1 -0
  525. package/dist/utils/debugLogger.js +39 -0
  526. package/dist/utils/debugLogger.js.map +1 -0
  527. package/dist/utils/errorUtils.d.ts +12 -0
  528. package/dist/utils/errorUtils.d.ts.map +1 -0
  529. package/dist/utils/errorUtils.js +83 -0
  530. package/dist/utils/errorUtils.js.map +1 -0
  531. package/dist/utils/frontmatter.d.ts +10 -0
  532. package/dist/utils/frontmatter.d.ts.map +1 -0
  533. package/dist/utils/frontmatter.js +78 -0
  534. package/dist/utils/frontmatter.js.map +1 -0
  535. package/dist/utils/packageInfo.d.ts +14 -0
  536. package/dist/utils/packageInfo.d.ts.map +1 -0
  537. package/dist/utils/packageInfo.js +45 -0
  538. package/dist/utils/packageInfo.js.map +1 -0
  539. package/dist/utils/planFormatter.d.ts +34 -0
  540. package/dist/utils/planFormatter.d.ts.map +1 -0
  541. package/dist/utils/planFormatter.js +141 -0
  542. package/dist/utils/planFormatter.js.map +1 -0
  543. package/dist/utils/securityUtils.d.ts +132 -0
  544. package/dist/utils/securityUtils.d.ts.map +1 -0
  545. package/dist/utils/securityUtils.js +324 -0
  546. package/dist/utils/securityUtils.js.map +1 -0
  547. package/dist/utils/statusReporter.d.ts +6 -0
  548. package/dist/utils/statusReporter.d.ts.map +1 -0
  549. package/dist/utils/statusReporter.js +26 -0
  550. package/dist/utils/statusReporter.js.map +1 -0
  551. package/dist/workspace.d.ts +8 -0
  552. package/dist/workspace.d.ts.map +1 -0
  553. package/dist/workspace.js +135 -0
  554. package/dist/workspace.js.map +1 -0
  555. package/dist/workspace.validator.d.ts +49 -0
  556. package/dist/workspace.validator.d.ts.map +1 -0
  557. package/dist/workspace.validator.js +215 -0
  558. package/dist/workspace.validator.js.map +1 -0
  559. package/package.json +116 -0
  560. package/scripts/postinstall.cjs +56 -0
@@ -0,0 +1,1076 @@
1
+ /**
2
+ * ContextManager - Manages conversation context to prevent token limit leaks
3
+ *
4
+ * Responsibilities:
5
+ * - Truncate tool outputs intelligently
6
+ * - Prune old conversation history with LLM summarization
7
+ * - Track and estimate token usage
8
+ * - Keep conversation within budget based on model context windows
9
+ * - Proactively shrink context before hitting limits
10
+ */
11
+ import { calculateContextThresholds } from './contextWindow.js';
12
+ /**
13
+ * Summarization prompt template
14
+ */
15
+ export const SUMMARIZATION_PROMPT = `Create a compact but reliable summary of the earlier conversation.
16
+
17
+ Keep:
18
+ - Decisions, preferences, and open questions
19
+ - File paths, function/class names, APIs, and error messages with fixes
20
+ - What was completed vs. still pending (tests, TODOs)
21
+
22
+ Format:
23
+ ## Key Context
24
+ - ...
25
+ ## Work Completed
26
+ - ...
27
+ ## Open Items
28
+ - ...
29
+
30
+ Conversation:
31
+ {conversation}`;
32
+ /**
33
+ * Pre-defined AI Flow Patterns for intelligent context management
34
+ */
35
+ export const DEFAULT_AI_FLOW_PATTERNS = [
36
+ {
37
+ patternId: 'read_edit_workflow',
38
+ description: 'Standard file modification workflow',
39
+ toolSequence: ['read', 'edit'],
40
+ contextImpact: 1500,
41
+ compactionOpportunity: true,
42
+ preservationPriority: 8,
43
+ },
44
+ {
45
+ patternId: 'analysis_phase',
46
+ description: 'Code analysis and exploration phase',
47
+ toolSequence: ['read', 'grep', 'glob', 'search'],
48
+ contextImpact: 3000,
49
+ compactionOpportunity: true,
50
+ preservationPriority: 6,
51
+ },
52
+ {
53
+ patternId: 'implementation_phase',
54
+ description: 'Active code implementation phase',
55
+ toolSequence: ['edit', 'write'],
56
+ contextImpact: 2000,
57
+ compactionOpportunity: false, // Preserve implementation context
58
+ preservationPriority: 9,
59
+ },
60
+ {
61
+ patternId: 'validation_phase',
62
+ description: 'Code validation and testing phase',
63
+ toolSequence: ['run_tests', 'run_build', 'run_repo_checks'],
64
+ contextImpact: 1000,
65
+ compactionOpportunity: true,
66
+ preservationPriority: 5,
67
+ },
68
+ {
69
+ patternId: 'parallel_execution',
70
+ description: 'Efficient parallel tool usage',
71
+ toolSequence: ['read', 'read', 'read'], // Multiple parallel reads
72
+ contextImpact: 2500,
73
+ compactionOpportunity: true,
74
+ preservationPriority: 7,
75
+ },
76
+ {
77
+ patternId: 'git_workflow',
78
+ description: 'Git operations workflow',
79
+ toolSequence: ['git_smart_commit', 'git_sync', 'git_create_pr'],
80
+ contextImpact: 1200,
81
+ compactionOpportunity: true,
82
+ preservationPriority: 6,
83
+ },
84
+ ];
85
+ export class ContextManager {
86
+ config;
87
+ sessionStartTime = Date.now();
88
+ toolCallHistory = [];
89
+ constructor(config = {}) {
90
+ this.config = {
91
+ maxTokens: 130000, // Leave room below 131072 limit
92
+ targetTokens: 100000, // Target to trigger pruning
93
+ maxToolOutputLength: 10000, // 10k chars max per tool output
94
+ preserveRecentMessages: 10, // Keep last 10 user/assistant exchanges
95
+ estimatedCharsPerToken: 4,
96
+ ...config,
97
+ };
98
+ }
99
+ /**
100
+ * Record a tool call for context-aware summarization
101
+ */
102
+ recordToolCall(toolName) {
103
+ this.toolCallHistory.push(toolName);
104
+ // Keep only recent history
105
+ if (this.toolCallHistory.length > 50) {
106
+ this.toolCallHistory.shift();
107
+ }
108
+ }
109
+ /**
110
+ * Truncate tool output intelligently using the smart summarizer
111
+ */
112
+ truncateToolOutput(output, toolName, _args) {
113
+ const originalLength = output.length;
114
+ // First check if we even need to truncate
115
+ if (originalLength <= this.config.maxToolOutputLength) {
116
+ return {
117
+ content: output,
118
+ wasTruncated: false,
119
+ originalLength,
120
+ truncatedLength: originalLength,
121
+ };
122
+ }
123
+ // Intelligent truncation based on tool type
124
+ const truncated = this.intelligentTruncate(output, toolName);
125
+ const truncatedLength = truncated.length;
126
+ return {
127
+ content: truncated,
128
+ wasTruncated: true,
129
+ originalLength,
130
+ truncatedLength,
131
+ };
132
+ }
133
+ /**
134
+ * Intelligent truncation based on tool type
135
+ */
136
+ intelligentTruncate(output, toolName) {
137
+ const maxLength = this.config.maxToolOutputLength;
138
+ // For file reads, show beginning and end
139
+ if (toolName === 'Read' || toolName === 'read_file') {
140
+ return this.truncateFileOutput(output, maxLength);
141
+ }
142
+ // For search results, keep first N results
143
+ if (toolName === 'Grep' || toolName === 'grep_search' || toolName === 'Glob') {
144
+ return this.truncateSearchOutput(output, maxLength);
145
+ }
146
+ // For bash/command output, keep end (usually most relevant)
147
+ if (toolName === 'Bash' || toolName === 'bash' || toolName === 'execute_bash') {
148
+ return this.truncateBashOutput(output, maxLength);
149
+ }
150
+ // Default: show beginning with truncation notice
151
+ return this.truncateDefault(output, maxLength);
152
+ }
153
+ truncateFileOutput(output, maxLength) {
154
+ const lines = output.split('\n');
155
+ if (lines.length <= 100) {
156
+ // For small files, just truncate text
157
+ return this.truncateDefault(output, maxLength);
158
+ }
159
+ // Show first 50 and last 50 lines
160
+ const keepLines = Math.floor(maxLength / 100); // Rough estimate
161
+ const headLines = lines.slice(0, keepLines);
162
+ const tailLines = lines.slice(-keepLines);
163
+ const truncatedCount = lines.length - (keepLines * 2);
164
+ return [
165
+ ...headLines,
166
+ `\n... [${truncatedCount} lines truncated for context management] ...\n`,
167
+ ...tailLines,
168
+ ].join('\n');
169
+ }
170
+ truncateSearchOutput(output, maxLength) {
171
+ const lines = output.split('\n');
172
+ const keepLines = Math.floor(maxLength / 80); // Rough average line length
173
+ if (lines.length <= keepLines) {
174
+ return output;
175
+ }
176
+ const truncatedCount = lines.length - keepLines;
177
+ return [
178
+ ...lines.slice(0, keepLines),
179
+ `\n... [${truncatedCount} more results truncated for context management] ...`,
180
+ ].join('\n');
181
+ }
182
+ truncateBashOutput(output, maxLength) {
183
+ if (output.length <= maxLength) {
184
+ return output;
185
+ }
186
+ // For command output, the end is usually most important (errors, final status)
187
+ const keepChars = Math.floor(maxLength * 0.8); // 80% at end
188
+ const prefixChars = maxLength - keepChars - 100; // Small prefix
189
+ const prefix = output.slice(0, prefixChars);
190
+ const suffix = output.slice(-keepChars);
191
+ const truncatedChars = output.length - prefixChars - keepChars;
192
+ return `${prefix}\n\n... [${truncatedChars} characters truncated for context management] ...\n\n${suffix}`;
193
+ }
194
+ truncateDefault(output, maxLength) {
195
+ if (output.length <= maxLength) {
196
+ return output;
197
+ }
198
+ const truncatedChars = output.length - maxLength + 100; // Account for notice
199
+ return `${output.slice(0, maxLength - 100)}\n\n... [${truncatedChars} characters truncated for context management] ...`;
200
+ }
201
+ /**
202
+ * Estimate tokens in a message
203
+ */
204
+ estimateTokens(message) {
205
+ let charCount = 0;
206
+ if (message.content) {
207
+ charCount += message.content.length;
208
+ }
209
+ if (message.role === 'assistant' && message.toolCalls) {
210
+ // Tool calls add overhead
211
+ for (const call of message.toolCalls) {
212
+ charCount += call.name.length;
213
+ charCount += JSON.stringify(call.arguments).length;
214
+ }
215
+ }
216
+ return Math.ceil(charCount / this.config.estimatedCharsPerToken);
217
+ }
218
+ /**
219
+ * Detect context overflow risk from recent tool usage patterns
220
+ */
221
+ detectContextOverflowRisk(toolCalls) {
222
+ const recentTools = toolCalls.slice(-10); // Last 10 tools
223
+ // Check for broad search patterns without limits
224
+ const broadSearches = recentTools.filter(tool => tool.includes('Glob') && !tool.includes('head_limit'));
225
+ // Check for multiple large file reads
226
+ const fileReads = recentTools.filter(tool => tool.includes('Read') || tool.includes('read_file'));
227
+ // Check for redundant context_snapshot calls
228
+ const contextSnapshots = recentTools.filter(tool => tool.includes('context_snapshot'));
229
+ // Risk threshold: 2+ broad searches OR 5+ file reads OR 1+ context_snapshot
230
+ return broadSearches.length >= 2 ||
231
+ fileReads.length >= 5 ||
232
+ contextSnapshots.length >= 1;
233
+ }
234
+ /**
235
+ * Estimate total tokens in conversation
236
+ */
237
+ estimateTotalTokens(messages) {
238
+ return messages.reduce((sum, msg) => sum + this.estimateTokens(msg), 0);
239
+ }
240
+ /**
241
+ * Prune old messages when approaching limit
242
+ *
243
+ * Synchronously removes old messages to stay within budget.
244
+ * If LLM summarization is available and enabled, this method will be async.
245
+ */
246
+ pruneMessages(messages) {
247
+ const totalTokens = this.estimateTotalTokens(messages);
248
+ // Only prune if we're above target
249
+ if (totalTokens < this.config.targetTokens) {
250
+ return { pruned: messages, removed: 0 };
251
+ }
252
+ // Always keep system message (first)
253
+ const firstMessage = messages[0];
254
+ const systemMessage = firstMessage?.role === 'system' ? firstMessage : null;
255
+ const conversationMessages = systemMessage ? messages.slice(1) : messages;
256
+ // Group messages into "turns" to maintain tool call/result pairing
257
+ // A turn is: [user] or [assistant + all its tool results]
258
+ const turns = [];
259
+ let currentTurn = [];
260
+ for (const msg of conversationMessages) {
261
+ if (msg.role === 'user') {
262
+ if (currentTurn.length > 0) {
263
+ turns.push(currentTurn);
264
+ }
265
+ currentTurn = [msg];
266
+ }
267
+ else if (msg.role === 'assistant') {
268
+ if (currentTurn.length > 0) {
269
+ turns.push(currentTurn);
270
+ }
271
+ currentTurn = [msg];
272
+ }
273
+ else if (msg.role === 'tool') {
274
+ // Tool results belong with the current assistant turn
275
+ currentTurn.push(msg);
276
+ }
277
+ }
278
+ if (currentTurn.length > 0) {
279
+ turns.push(currentTurn);
280
+ }
281
+ // Keep recent turns based on preserveRecentMessages (count user turns)
282
+ const recentTurns = [];
283
+ let exchangeCount = 0;
284
+ for (let i = turns.length - 1; i >= 0; i--) {
285
+ const turn = turns[i];
286
+ if (!turn || turn.length === 0)
287
+ continue;
288
+ recentTurns.unshift(turn);
289
+ // Count user messages as exchanges
290
+ if (turn[0]?.role === 'user') {
291
+ exchangeCount++;
292
+ if (exchangeCount >= this.config.preserveRecentMessages) {
293
+ break;
294
+ }
295
+ }
296
+ }
297
+ // IMPORTANT: Ensure we don't start with orphaned tool messages
298
+ // The first kept turn must start with user or assistant (not tool)
299
+ let startIndex = 0;
300
+ while (startIndex < recentTurns.length) {
301
+ const firstTurn = recentTurns[startIndex];
302
+ if (firstTurn && firstTurn.length > 0 && firstTurn[0]?.role === 'tool') {
303
+ startIndex++;
304
+ continue;
305
+ }
306
+ // Also check for assistant turns with missing tool results
307
+ if (firstTurn && firstTurn[0]?.role === 'assistant') {
308
+ const assistantMsg = firstTurn[0];
309
+ if (assistantMsg.toolCalls && assistantMsg.toolCalls.length > 0) {
310
+ // PERF: Pre-compute tool call IDs once, use direct Set lookup
311
+ const toolCallIds = assistantMsg.toolCalls.map(tc => tc.id);
312
+ const presentToolResultIds = new Set(firstTurn.filter(m => m.role === 'tool').map(m => m.toolCallId));
313
+ // If NOT all tool calls have results, skip this turn
314
+ // PERF: Direct has() calls instead of spread + every()
315
+ let allPresent = true;
316
+ for (const id of toolCallIds) {
317
+ if (!presentToolResultIds.has(id)) {
318
+ allPresent = false;
319
+ break;
320
+ }
321
+ }
322
+ if (!allPresent) {
323
+ startIndex++;
324
+ continue;
325
+ }
326
+ }
327
+ }
328
+ break;
329
+ }
330
+ const validTurns = recentTurns.slice(startIndex);
331
+ // Flatten turns back to messages
332
+ const recentMessages = validTurns.flat();
333
+ // Build pruned message list
334
+ const pruned = [];
335
+ if (systemMessage) {
336
+ pruned.push(systemMessage);
337
+ }
338
+ // Add a context summary message if we removed messages
339
+ const removedCount = conversationMessages.length - recentMessages.length;
340
+ if (removedCount > 0) {
341
+ pruned.push({
342
+ role: 'system',
343
+ content: `[Context Manager: Removed ${removedCount} old messages to stay within token budget. Recent conversation history preserved.]`,
344
+ });
345
+ }
346
+ pruned.push(...recentMessages);
347
+ return {
348
+ pruned,
349
+ removed: removedCount,
350
+ };
351
+ }
352
+ /**
353
+ * Prune messages with LLM-based summarization
354
+ *
355
+ * This is an async version that uses the LLM to create intelligent summaries
356
+ * instead of just removing old messages. Should be called BEFORE generation.
357
+ */
358
+ async pruneMessagesWithSummary(messages, options) {
359
+ const totalTokens = this.estimateTotalTokens(messages);
360
+ // Only prune if we're above target
361
+ if (!options?.force && totalTokens < this.config.targetTokens) {
362
+ return { pruned: messages, removed: 0, summarized: false };
363
+ }
364
+ // If no summarization callback or disabled, fall back to simple pruning
365
+ if (!this.config.summarizationCallback || !this.config.useLLMSummarization) {
366
+ const result = this.pruneMessages(messages);
367
+ return { ...result, summarized: false };
368
+ }
369
+ // Partition messages
370
+ const firstMessage = messages[0];
371
+ const systemMessage = firstMessage?.role === 'system' ? firstMessage : null;
372
+ const conversationMessages = systemMessage ? messages.slice(1) : messages;
373
+ // Group messages into "turns" to maintain tool call/result pairing
374
+ const turns = [];
375
+ let currentTurn = [];
376
+ for (const msg of conversationMessages) {
377
+ if (msg.role === 'user') {
378
+ if (currentTurn.length > 0) {
379
+ turns.push(currentTurn);
380
+ }
381
+ currentTurn = [msg];
382
+ }
383
+ else if (msg.role === 'assistant') {
384
+ if (currentTurn.length > 0) {
385
+ turns.push(currentTurn);
386
+ }
387
+ currentTurn = [msg];
388
+ }
389
+ else if (msg.role === 'tool') {
390
+ currentTurn.push(msg);
391
+ }
392
+ }
393
+ if (currentTurn.length > 0) {
394
+ turns.push(currentTurn);
395
+ }
396
+ // Keep recent turns based on preserveRecentMessages
397
+ const recentTurns = [];
398
+ let exchangeCount = 0;
399
+ for (let i = turns.length - 1; i >= 0; i--) {
400
+ const turn = turns[i];
401
+ if (!turn || turn.length === 0)
402
+ continue;
403
+ recentTurns.unshift(turn);
404
+ if (turn[0]?.role === 'user') {
405
+ exchangeCount++;
406
+ if (exchangeCount >= this.config.preserveRecentMessages) {
407
+ break;
408
+ }
409
+ }
410
+ }
411
+ // Ensure we don't start with orphaned tool messages
412
+ let startIndex = 0;
413
+ while (startIndex < recentTurns.length) {
414
+ const firstTurn = recentTurns[startIndex];
415
+ if (firstTurn && firstTurn.length > 0 && firstTurn[0]?.role === 'tool') {
416
+ startIndex++;
417
+ continue;
418
+ }
419
+ if (firstTurn && firstTurn[0]?.role === 'assistant') {
420
+ const assistantMsg = firstTurn[0];
421
+ if (assistantMsg.toolCalls && assistantMsg.toolCalls.length > 0) {
422
+ // PERF: Pre-compute tool call IDs once, use direct Set lookup
423
+ const toolCallIds = assistantMsg.toolCalls.map(tc => tc.id);
424
+ const presentToolResultIds = new Set(firstTurn.filter(m => m.role === 'tool').map(m => m.toolCallId));
425
+ // PERF: Direct has() calls instead of spread + every()
426
+ let allPresent = true;
427
+ for (const id of toolCallIds) {
428
+ if (!presentToolResultIds.has(id)) {
429
+ allPresent = false;
430
+ break;
431
+ }
432
+ }
433
+ if (!allPresent) {
434
+ startIndex++;
435
+ continue;
436
+ }
437
+ }
438
+ }
439
+ break;
440
+ }
441
+ const validTurns = recentTurns.slice(startIndex);
442
+ const recentMessages = validTurns.flat();
443
+ // Determine which turns to summarize. validTurns starts at
444
+ // (turns.length - keepTurnCount); everything before it must be summarized.
445
+ // The old `- startIndex` here excluded the `startIndex` turns peeled off the
446
+ // front of recentTurns from BOTH sets, silently dropping their content.
447
+ const keepTurnCount = validTurns.length;
448
+ const summarizeTurns = turns.slice(0, turns.length - keepTurnCount);
449
+ const toSummarize = summarizeTurns.flat();
450
+ // If nothing to summarize, return as-is
451
+ if (toSummarize.length === 0) {
452
+ return { pruned: messages, removed: 0, summarized: false };
453
+ }
454
+ try {
455
+ // Call the LLM to summarize old messages
456
+ const summary = await this.config.summarizationCallback(toSummarize);
457
+ // Build pruned message list with summary
458
+ const pruned = [];
459
+ if (systemMessage) {
460
+ pruned.push(systemMessage);
461
+ }
462
+ // Add intelligent summary
463
+ pruned.push({
464
+ role: 'system',
465
+ content: [
466
+ '=== Context Summary (Auto-generated) ===',
467
+ summary.trim(),
468
+ '',
469
+ `[Summarized ${toSummarize.length} earlier messages. Recent ${recentMessages.length} messages preserved below.]`,
470
+ ].join('\n'),
471
+ });
472
+ pruned.push(...recentMessages);
473
+ return {
474
+ pruned,
475
+ removed: toSummarize.length,
476
+ summarized: true,
477
+ };
478
+ }
479
+ catch (error) {
480
+ // If summarization fails, fall back to simple pruning
481
+ const result = this.pruneMessages(messages);
482
+ return { ...result, summarized: false };
483
+ }
484
+ }
485
+ /**
486
+ * Check if we're approaching the limit
487
+ */
488
+ isApproachingLimit(messages) {
489
+ const totalTokens = this.estimateTotalTokens(messages);
490
+ return totalTokens >= this.config.targetTokens;
491
+ }
492
+ /**
493
+ * Get warning level for current context usage
494
+ * Returns: null (no warning), 'info' (<70%), 'warning' (70-90%), 'danger' (>90%)
495
+ */
496
+ getWarningLevel(messages) {
497
+ const totalTokens = this.estimateTotalTokens(messages);
498
+ const percentage = (totalTokens / this.config.maxTokens) * 100;
499
+ if (percentage > 90) {
500
+ return 'danger';
501
+ }
502
+ else if (percentage > 70) {
503
+ return 'warning';
504
+ }
505
+ else if (percentage > 50) {
506
+ return 'info';
507
+ }
508
+ return null;
509
+ }
510
+ /**
511
+ * Get a human-readable warning message
512
+ */
513
+ getWarningMessage(messages) {
514
+ const stats = this.getStats(messages);
515
+ const warningLevel = this.getWarningLevel(messages);
516
+ if (warningLevel === 'danger') {
517
+ return `⚠️ Context usage critical (${stats.percentage}%). Consider starting a new session or the next request may fail.`;
518
+ }
519
+ else if (warningLevel === 'warning') {
520
+ return `Context usage high (${stats.percentage}%). Automatic cleanup will occur soon.`;
521
+ }
522
+ return null;
523
+ }
524
+ /**
525
+ * Get context stats
526
+ */
527
+ getStats(messages) {
528
+ const totalTokens = this.estimateTotalTokens(messages);
529
+ const percentage = Math.round((totalTokens / this.config.maxTokens) * 100);
530
+ return {
531
+ totalTokens,
532
+ percentage,
533
+ isOverLimit: totalTokens >= this.config.maxTokens,
534
+ isApproachingLimit: totalTokens >= this.config.targetTokens,
535
+ };
536
+ }
537
+ /**
538
+ * Update configuration
539
+ */
540
+ updateConfig(config) {
541
+ this.config = { ...this.config, ...config };
542
+ }
543
+ // ============================================================================
544
+ // INTELLIGENT COMPACTION SYSTEM
545
+ // Automatically detects optimal points for conversation compaction
546
+ // ============================================================================
547
+ /**
548
+ * Default patterns that indicate task boundaries
549
+ */
550
+ static DEFAULT_TASK_BOUNDARY_PATTERNS = [
551
+ // Completion indicators
552
+ /\b(done|completed|finished|fixed|resolved|implemented|added|created|updated)\b/i,
553
+ /\b(all\s+(?:tests?\s+)?pass(?:ing|ed)?)\b/i,
554
+ /\b(successfully|works?\s+(?:now|correctly))\b/i,
555
+ // Transition indicators
556
+ /\b(next|now\s+(?:let's|we\s+can)|moving\s+on)\b/i,
557
+ /\b(that's\s+(?:it|all|done))\b/i,
558
+ // Acknowledgment patterns
559
+ /^(?:great|perfect|thanks|thank\s+you|got\s+it|understood)\b/i,
560
+ ];
561
+ /**
562
+ * Patterns indicating topic/task shifts
563
+ */
564
+ static TOPIC_SHIFT_PATTERNS = [
565
+ /\b(different|another|new|separate|unrelated)\s+(?:task|thing|topic|issue|question)\b/i,
566
+ /\b(can\s+you|could\s+you|please|now|let's)\s+(?:also|help|do|make|create|fix|add)\b/i,
567
+ /\b(switching|changing|moving)\s+to\b/i,
568
+ /\b(forget|ignore|never\s*mind)\s+(?:that|the|about)\b/i,
569
+ /^(?:ok|okay|alright|anyway|so)\s*[,.]?\s*(?:can|could|now|let|please)/i,
570
+ ];
571
+ /**
572
+ * Patterns indicating user pivots (abandoning current direction)
573
+ */
574
+ static USER_PIVOT_PATTERNS = [
575
+ /\b(actually|wait|hold\s+on|stop|cancel|scratch\s+that)\b/i,
576
+ /\b(let's\s+(?:try|do)\s+(?:something|it)\s+(?:else|differently))\b/i,
577
+ /\b(go\s+back|revert|undo|start\s+over)\b/i,
578
+ /\b(wrong|not\s+(?:what|right)|that's\s+not)\b/i,
579
+ ];
580
+ /**
581
+ * Analyze the conversation to detect intelligent compaction points
582
+ */
583
+ analyzeCompactionPoints(messages) {
584
+ const signals = [];
585
+ const totalTokens = this.estimateTotalTokens(messages);
586
+ const tokenPercentage = totalTokens / this.config.maxTokens;
587
+ const compactionThreshold = this.config.compactionThreshold ?? 0.5;
588
+ const minConfidence = this.config.minSignalConfidence ?? 0.6;
589
+ // Don't analyze if below threshold
590
+ if (tokenPercentage < compactionThreshold) {
591
+ return {
592
+ shouldCompact: false,
593
+ signals: [],
594
+ recommendedCompactionPoint: null,
595
+ urgency: 'none',
596
+ preserveFromIndex: 0,
597
+ };
598
+ }
599
+ // Analyze each message for compaction signals
600
+ for (let i = 0; i < messages.length; i++) {
601
+ const msg = messages[i];
602
+ if (!msg)
603
+ continue;
604
+ // Detect task boundaries
605
+ const taskBoundary = this.detectTaskBoundary(msg, i, messages);
606
+ if (taskBoundary && taskBoundary.confidence >= minConfidence) {
607
+ signals.push(taskBoundary);
608
+ }
609
+ // Detect topic shifts
610
+ const topicShift = this.detectTopicShift(msg, i, messages);
611
+ if (topicShift && topicShift.confidence >= minConfidence) {
612
+ signals.push(topicShift);
613
+ }
614
+ // Detect user pivots
615
+ const userPivot = this.detectUserPivot(msg, i);
616
+ if (userPivot && userPivot.confidence >= minConfidence) {
617
+ signals.push(userPivot);
618
+ }
619
+ // Detect context saturation (tool output heavy regions)
620
+ const saturation = this.detectContextSaturation(msg, i, messages);
621
+ if (saturation && saturation.confidence >= minConfidence) {
622
+ signals.push(saturation);
623
+ }
624
+ // Detect milestones
625
+ const milestone = this.detectMilestone(msg, i, messages);
626
+ if (milestone && milestone.confidence >= minConfidence) {
627
+ signals.push(milestone);
628
+ }
629
+ }
630
+ // Determine urgency based on token percentage
631
+ const urgency = this.calculateUrgency(tokenPercentage);
632
+ // Find the best compaction point
633
+ const recommendedPoint = this.findBestCompactionPoint(signals, messages, urgency);
634
+ // Calculate preserve index (everything after this should be kept)
635
+ const preserveFromIndex = recommendedPoint !== null
636
+ ? this.findSafePreservePoint(recommendedPoint, messages)
637
+ : messages.length;
638
+ return {
639
+ shouldCompact: signals.length > 0 && urgency !== 'none',
640
+ signals,
641
+ recommendedCompactionPoint: recommendedPoint,
642
+ urgency,
643
+ preserveFromIndex,
644
+ };
645
+ }
646
+ /**
647
+ * Detect task boundary signals
648
+ */
649
+ detectTaskBoundary(msg, index, messages) {
650
+ if (msg.role !== 'user' && msg.role !== 'assistant')
651
+ return null;
652
+ const content = msg.content || '';
653
+ const patterns = this.config.taskBoundaryPatterns
654
+ ? this.config.taskBoundaryPatterns.map(p => new RegExp(p, 'i'))
655
+ : ContextManager.DEFAULT_TASK_BOUNDARY_PATTERNS;
656
+ let matchCount = 0;
657
+ const reasons = [];
658
+ for (const pattern of patterns) {
659
+ if (pattern.test(content)) {
660
+ matchCount++;
661
+ reasons.push(pattern.source.slice(0, 30));
662
+ }
663
+ }
664
+ if (matchCount === 0)
665
+ return null;
666
+ // Higher confidence if followed by a new user message with different intent
667
+ let confidence = Math.min(0.4 + matchCount * 0.2, 0.9);
668
+ // Boost confidence if this looks like a conclusion
669
+ if (msg.role === 'assistant' && this.looksLikeConclusion(content)) {
670
+ confidence = Math.min(confidence + 0.2, 0.95);
671
+ }
672
+ // Boost if next user message starts a new topic
673
+ const nextUserMsg = messages.slice(index + 1).find(m => m.role === 'user');
674
+ if (nextUserMsg && this.isNewTopic(content, nextUserMsg.content || '')) {
675
+ confidence = Math.min(confidence + 0.15, 0.95);
676
+ }
677
+ return {
678
+ type: 'task_boundary',
679
+ confidence,
680
+ messageIndex: index,
681
+ reason: `Task completion detected: ${reasons.slice(0, 2).join(', ')}`,
682
+ };
683
+ }
684
+ /**
685
+ * Detect topic shift signals
686
+ */
687
+ detectTopicShift(msg, index, messages) {
688
+ if (msg.role !== 'user')
689
+ return null;
690
+ const content = msg.content || '';
691
+ const sensitivity = this.config.topicShiftSensitivity ?? 0.7;
692
+ // Check explicit shift patterns
693
+ for (const pattern of ContextManager.TOPIC_SHIFT_PATTERNS) {
694
+ if (pattern.test(content)) {
695
+ return {
696
+ type: 'topic_shift',
697
+ confidence: 0.7 + sensitivity * 0.2,
698
+ messageIndex: index,
699
+ reason: 'Explicit topic shift language detected',
700
+ };
701
+ }
702
+ }
703
+ // Check semantic shift from previous context
704
+ const prevMessages = messages.slice(Math.max(0, index - 5), index);
705
+ const prevContent = prevMessages
706
+ .filter(m => m.role === 'user' || m.role === 'assistant')
707
+ .map(m => m.content || '')
708
+ .join(' ');
709
+ if (prevContent && this.isNewTopic(prevContent, content)) {
710
+ return {
711
+ type: 'topic_shift',
712
+ confidence: 0.6 + sensitivity * 0.2,
713
+ messageIndex: index,
714
+ reason: 'Semantic topic shift detected',
715
+ };
716
+ }
717
+ return null;
718
+ }
719
+ /**
720
+ * Detect user pivot signals (abandoning current direction)
721
+ */
722
+ detectUserPivot(msg, index) {
723
+ if (msg.role !== 'user')
724
+ return null;
725
+ const content = msg.content || '';
726
+ for (const pattern of ContextManager.USER_PIVOT_PATTERNS) {
727
+ if (pattern.test(content)) {
728
+ return {
729
+ type: 'user_pivot',
730
+ confidence: 0.85,
731
+ messageIndex: index,
732
+ reason: 'User pivot/direction change detected',
733
+ };
734
+ }
735
+ }
736
+ return null;
737
+ }
738
+ /**
739
+ * Detect context saturation (heavy tool output regions)
740
+ */
741
+ detectContextSaturation(msg, index, messages) {
742
+ if (msg.role !== 'tool')
743
+ return null;
744
+ // Look at the surrounding region
745
+ const windowStart = Math.max(0, index - 10);
746
+ const windowEnd = Math.min(messages.length, index + 5);
747
+ const window = messages.slice(windowStart, windowEnd);
748
+ // Count tool messages and their sizes
749
+ let toolCount = 0;
750
+ let totalToolSize = 0;
751
+ for (const m of window) {
752
+ if (m.role === 'tool') {
753
+ toolCount++;
754
+ totalToolSize += (m.content || '').length;
755
+ }
756
+ }
757
+ // High saturation if many tool outputs with large content
758
+ if (toolCount >= 5 && totalToolSize > 20000) {
759
+ // Find the last tool message in this cluster as compaction point
760
+ let lastToolIndex = index;
761
+ for (let i = index + 1; i < windowEnd; i++) {
762
+ if (messages[i]?.role === 'tool') {
763
+ lastToolIndex = i;
764
+ }
765
+ else if (messages[i]?.role === 'user') {
766
+ break; // Stop at next user message
767
+ }
768
+ }
769
+ return {
770
+ type: 'context_saturation',
771
+ confidence: Math.min(0.5 + toolCount * 0.05, 0.85),
772
+ messageIndex: lastToolIndex,
773
+ reason: `Heavy tool output region (${toolCount} tools, ${Math.round(totalToolSize / 1000)}k chars)`,
774
+ };
775
+ }
776
+ return null;
777
+ }
778
+ /**
779
+ * Detect milestone signals (significant accomplishments)
780
+ */
781
+ detectMilestone(msg, index, _messages) {
782
+ if (msg.role !== 'assistant')
783
+ return null;
784
+ const content = msg.content || '';
785
+ // Look for milestone indicators
786
+ const milestonePatterns = [
787
+ /\b(commit(?:ted)?|pushed|deployed|merged|released)\b/i,
788
+ /\b(all\s+tests?\s+pass(?:ing|ed)?)\b/i,
789
+ /\b(build\s+(?:succeed|success|pass))\b/i,
790
+ /\b(feature\s+(?:complete|done|ready))\b/i,
791
+ /\b(pr\s+(?:created|opened|merged))\b/i,
792
+ ];
793
+ for (const pattern of milestonePatterns) {
794
+ if (pattern.test(content)) {
795
+ return {
796
+ type: 'milestone',
797
+ confidence: 0.9,
798
+ messageIndex: index,
799
+ reason: 'Significant milestone achieved',
800
+ };
801
+ }
802
+ }
803
+ return null;
804
+ }
805
+ /**
806
+ * Check if content looks like a task conclusion
807
+ */
808
+ looksLikeConclusion(content) {
809
+ const conclusionPatterns = [
810
+ /\b(let\s+me\s+know|feel\s+free|if\s+you\s+(?:need|have|want))\b/i,
811
+ /\b(anything\s+else|other\s+questions?)\b/i,
812
+ /\b(should\s+be\s+(?:good|working|ready|done))\b/i,
813
+ /\b(that\s+should|this\s+(?:should|will))\s+(?:fix|solve|work)/i,
814
+ ];
815
+ return conclusionPatterns.some(p => p.test(content));
816
+ }
817
+ /**
818
+ * Check if two contents represent different topics (simple heuristic)
819
+ */
820
+ isNewTopic(prevContent, newContent) {
821
+ // Extract key terms (simple tokenization)
822
+ const extractTerms = (text) => {
823
+ const words = text.toLowerCase()
824
+ .replace(/[^a-z0-9\s]/g, ' ')
825
+ .split(/\s+/)
826
+ .filter(w => w.length > 3);
827
+ return new Set(words);
828
+ };
829
+ const prevTerms = extractTerms(prevContent);
830
+ const newTerms = extractTerms(newContent);
831
+ if (prevTerms.size === 0 || newTerms.size === 0)
832
+ return false;
833
+ // Calculate overlap
834
+ let overlap = 0;
835
+ for (const term of newTerms) {
836
+ if (prevTerms.has(term))
837
+ overlap++;
838
+ }
839
+ const overlapRatio = overlap / Math.min(prevTerms.size, newTerms.size);
840
+ // Low overlap suggests new topic
841
+ return overlapRatio < 0.2;
842
+ }
843
+ /**
844
+ * Calculate urgency level based on token percentage
845
+ */
846
+ calculateUrgency(tokenPercentage) {
847
+ if (tokenPercentage >= 0.9)
848
+ return 'critical';
849
+ if (tokenPercentage >= 0.75)
850
+ return 'high';
851
+ if (tokenPercentage >= 0.6)
852
+ return 'medium';
853
+ if (tokenPercentage >= 0.5)
854
+ return 'low';
855
+ return 'none';
856
+ }
857
+ /**
858
+ * Find the best compaction point from signals
859
+ */
860
+ findBestCompactionPoint(signals, messages, urgency) {
861
+ if (signals.length === 0)
862
+ return null;
863
+ // Score each signal based on type priority and confidence
864
+ const typePriority = {
865
+ milestone: 1.0,
866
+ task_boundary: 0.9,
867
+ user_pivot: 0.85,
868
+ ai_flow_pattern: 0.82, // AI flow patterns like thinking/tool use cycles
869
+ topic_shift: 0.8,
870
+ context_saturation: 0.7,
871
+ };
872
+ // Urgency affects how far back we're willing to compact
873
+ const urgencyDepth = {
874
+ none: 0,
875
+ low: 0.3, // Compact only recent 30%
876
+ medium: 0.5,
877
+ high: 0.7,
878
+ critical: 0.9,
879
+ };
880
+ const maxDepth = urgencyDepth[urgency] ?? 0.5;
881
+ const minIndex = Math.floor(messages.length * (1 - maxDepth));
882
+ // Find highest scoring signal within allowed depth
883
+ let bestSignal = null;
884
+ let bestScore = 0;
885
+ for (const signal of signals) {
886
+ if (signal.messageIndex < minIndex)
887
+ continue;
888
+ const score = signal.confidence * typePriority[signal.type];
889
+ if (score > bestScore) {
890
+ bestScore = score;
891
+ bestSignal = signal;
892
+ }
893
+ }
894
+ return bestSignal?.messageIndex ?? null;
895
+ }
896
+ /**
897
+ * Find a safe preservation point that doesn't break tool call chains
898
+ */
899
+ findSafePreservePoint(compactionPoint, messages) {
900
+ // Start from compaction point and move forward to find a safe break
901
+ for (let i = compactionPoint + 1; i < messages.length; i++) {
902
+ const msg = messages[i];
903
+ if (!msg)
904
+ continue;
905
+ // Safe if it's a user message
906
+ if (msg.role === 'user') {
907
+ return i;
908
+ }
909
+ // Safe if it's an assistant without pending tool calls
910
+ if (msg.role === 'assistant' && !msg.toolCalls?.length) {
911
+ return i;
912
+ }
913
+ }
914
+ // If no safe point found, keep more messages
915
+ return Math.min(compactionPoint + 1, messages.length);
916
+ }
917
+ /**
918
+ * Perform intelligent compaction based on analysis
919
+ * This method analyzes the conversation and compacts at the optimal point
920
+ */
921
+ async intelligentCompact(messages) {
922
+ // Analyze for compaction points
923
+ const analysis = this.analyzeCompactionPoints(messages);
924
+ // If no compaction needed or no good point found
925
+ if (!analysis.shouldCompact || analysis.recommendedCompactionPoint === null) {
926
+ return {
927
+ compacted: messages,
928
+ analysis,
929
+ summarized: false,
930
+ };
931
+ }
932
+ // Separate messages to summarize and preserve
933
+ const firstMessage = messages[0];
934
+ const systemMessage = firstMessage?.role === 'system' ? firstMessage : null;
935
+ const startIndex = systemMessage ? 1 : 0;
936
+ const toSummarize = messages.slice(startIndex, analysis.preserveFromIndex);
937
+ const toPreserve = messages.slice(analysis.preserveFromIndex);
938
+ // If nothing to summarize, return as-is
939
+ if (toSummarize.length === 0) {
940
+ return {
941
+ compacted: messages,
942
+ analysis,
943
+ summarized: false,
944
+ };
945
+ }
946
+ // Build result
947
+ const compacted = [];
948
+ if (systemMessage) {
949
+ compacted.push(systemMessage);
950
+ }
951
+ // Try LLM summarization if available
952
+ if (this.config.summarizationCallback && this.config.useLLMSummarization !== false) {
953
+ try {
954
+ const summary = await this.config.summarizationCallback(toSummarize);
955
+ compacted.push({
956
+ role: 'system',
957
+ content: [
958
+ '=== Intelligent Context Summary ===',
959
+ `Compaction triggered: ${analysis.signals[0]?.reason || 'Context optimization'}`,
960
+ '',
961
+ summary.trim(),
962
+ '',
963
+ `[Summarized ${toSummarize.length} messages. ${toPreserve.length} recent messages preserved.]`,
964
+ ].join('\n'),
965
+ });
966
+ compacted.push(...toPreserve);
967
+ return {
968
+ compacted,
969
+ analysis,
970
+ summarized: true,
971
+ };
972
+ }
973
+ catch {
974
+ // Fall through to simple compaction
975
+ }
976
+ }
977
+ // Simple compaction without LLM
978
+ compacted.push({
979
+ role: 'system',
980
+ content: `[Context Manager: Intelligently compacted ${toSummarize.length} messages at "${analysis.signals[0]?.reason || 'optimal point'}". ${toPreserve.length} recent messages preserved.]`,
981
+ });
982
+ compacted.push(...toPreserve);
983
+ return {
984
+ compacted,
985
+ analysis,
986
+ summarized: false,
987
+ };
988
+ }
989
+ /**
990
+ * Check if intelligent compaction should be triggered
991
+ * Call this before generation to proactively manage context
992
+ */
993
+ shouldTriggerCompaction(messages) {
994
+ if (this.config.enableIntelligentCompaction === false) {
995
+ return { shouldCompact: false, urgency: 'none', reason: null };
996
+ }
997
+ const analysis = this.analyzeCompactionPoints(messages);
998
+ if (!analysis.shouldCompact) {
999
+ return { shouldCompact: false, urgency: analysis.urgency, reason: null };
1000
+ }
1001
+ const topSignal = analysis.signals
1002
+ .sort((a, b) => b.confidence - a.confidence)[0];
1003
+ return {
1004
+ shouldCompact: true,
1005
+ urgency: analysis.urgency,
1006
+ reason: topSignal?.reason || 'Context optimization recommended',
1007
+ };
1008
+ }
1009
+ }
1010
+ /**
1011
+ * Create a default context manager instance with model-aware limits
1012
+ */
1013
+ export function createDefaultContextManager(overrides, model) {
1014
+ // Get model-specific thresholds
1015
+ const thresholds = calculateContextThresholds(model);
1016
+ return new ContextManager({
1017
+ maxTokens: thresholds.maxTokens,
1018
+ targetTokens: thresholds.targetTokens, // Start pruning at 60%
1019
+ warningTokens: thresholds.warningTokens, // Warn at 50%
1020
+ criticalTokens: thresholds.criticalTokens, // Critical at 75%
1021
+ maxToolOutputLength: 5000, // 5k chars max per tool (reduced for safety)
1022
+ preserveRecentMessages: 5, // Keep last 5 exchanges
1023
+ estimatedCharsPerToken: 3.5, // More aggressive estimate (accounts for special tokens, JSON overhead)
1024
+ useLLMSummarization: true, // Enable LLM summarization by default
1025
+ // Intelligent compaction defaults
1026
+ enableIntelligentCompaction: true,
1027
+ compactionThreshold: 0.5, // Start analyzing at 50% context usage
1028
+ minSignalConfidence: 0.6, // Require 60% confidence for compaction signals
1029
+ topicShiftSensitivity: 0.7, // Moderately sensitive to topic changes
1030
+ model,
1031
+ ...overrides,
1032
+ });
1033
+ }
1034
+ /**
1035
+ * Format conversation messages into readable text for summarization
1036
+ */
1037
+ export function formatMessagesForSummary(messages) {
1038
+ const lines = [];
1039
+ for (const msg of messages) {
1040
+ if (msg.role === 'user') {
1041
+ lines.push(`USER: ${msg.content}`);
1042
+ }
1043
+ else if (msg.role === 'assistant') {
1044
+ let content = msg.content || '';
1045
+ if (msg.toolCalls && msg.toolCalls.length > 0) {
1046
+ const toolNames = msg.toolCalls.map(tc => tc.name);
1047
+ content += ` [Called tools: ${toolNames.join(', ')}]`;
1048
+ }
1049
+ lines.push(`ASSISTANT: ${content}`);
1050
+ }
1051
+ else if (msg.role === 'tool') {
1052
+ // Truncate long tool outputs for summarization
1053
+ const output = msg.content.length > 500
1054
+ ? `${msg.content.slice(0, 500)}...`
1055
+ : msg.content;
1056
+ lines.push(`TOOL (${msg.name}): ${output}`);
1057
+ }
1058
+ // Skip system messages in summary input
1059
+ }
1060
+ return lines.join('\n\n');
1061
+ }
1062
+ /**
1063
+ * Create a summarization callback using the given provider
1064
+ */
1065
+ export function createSummarizationCallback(provider) {
1066
+ return async (messages) => {
1067
+ // Format messages into readable conversation
1068
+ const conversationText = formatMessagesForSummary(messages);
1069
+ // Create summarization prompt
1070
+ const prompt = SUMMARIZATION_PROMPT.replace('{conversation}', conversationText);
1071
+ // Call provider to generate summary (no tools needed)
1072
+ const response = await provider.generate([{ role: 'user', content: prompt }], []);
1073
+ return response.content || '';
1074
+ };
1075
+ }
1076
+ //# sourceMappingURL=contextManager.js.map