distill-mcp 0.6.0-beta

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 (440) hide show
  1. package/bin/cli.js +133 -0
  2. package/dist/analytics/session-tracker.d.ts +74 -0
  3. package/dist/analytics/session-tracker.d.ts.map +1 -0
  4. package/dist/analytics/session-tracker.js +123 -0
  5. package/dist/ast/benchmark.test.d.ts +7 -0
  6. package/dist/ast/benchmark.test.d.ts.map +1 -0
  7. package/dist/ast/benchmark.test.js +175 -0
  8. package/dist/ast/go/index.d.ts +9 -0
  9. package/dist/ast/go/index.d.ts.map +1 -0
  10. package/dist/ast/go/index.js +8 -0
  11. package/dist/ast/go/parser.d.ts +31 -0
  12. package/dist/ast/go/parser.d.ts.map +1 -0
  13. package/dist/ast/go/parser.js +428 -0
  14. package/dist/ast/go/parser.test.d.ts +5 -0
  15. package/dist/ast/go/parser.test.d.ts.map +1 -0
  16. package/dist/ast/go/parser.test.js +241 -0
  17. package/dist/ast/go/queries.d.ts +51 -0
  18. package/dist/ast/go/queries.d.ts.map +1 -0
  19. package/dist/ast/go/queries.js +114 -0
  20. package/dist/ast/go/utils.d.ts +66 -0
  21. package/dist/ast/go/utils.d.ts.map +1 -0
  22. package/dist/ast/go/utils.js +140 -0
  23. package/dist/ast/index.d.ts +39 -0
  24. package/dist/ast/index.d.ts.map +1 -0
  25. package/dist/ast/index.js +245 -0
  26. package/dist/ast/php/index.d.ts +9 -0
  27. package/dist/ast/php/index.d.ts.map +1 -0
  28. package/dist/ast/php/index.js +8 -0
  29. package/dist/ast/php/parser.d.ts +31 -0
  30. package/dist/ast/php/parser.d.ts.map +1 -0
  31. package/dist/ast/php/parser.js +388 -0
  32. package/dist/ast/php/parser.test.d.ts +5 -0
  33. package/dist/ast/php/parser.test.d.ts.map +1 -0
  34. package/dist/ast/php/parser.test.js +328 -0
  35. package/dist/ast/php/queries.d.ts +61 -0
  36. package/dist/ast/php/queries.d.ts.map +1 -0
  37. package/dist/ast/php/queries.js +117 -0
  38. package/dist/ast/php/utils.d.ts +83 -0
  39. package/dist/ast/php/utils.d.ts.map +1 -0
  40. package/dist/ast/php/utils.js +246 -0
  41. package/dist/ast/python/index.d.ts +9 -0
  42. package/dist/ast/python/index.d.ts.map +1 -0
  43. package/dist/ast/python/index.js +8 -0
  44. package/dist/ast/python/parser.d.ts +32 -0
  45. package/dist/ast/python/parser.d.ts.map +1 -0
  46. package/dist/ast/python/parser.js +422 -0
  47. package/dist/ast/python/parser.test.d.ts +5 -0
  48. package/dist/ast/python/parser.test.d.ts.map +1 -0
  49. package/dist/ast/python/parser.test.js +186 -0
  50. package/dist/ast/python/queries.d.ts +73 -0
  51. package/dist/ast/python/queries.d.ts.map +1 -0
  52. package/dist/ast/python/queries.js +137 -0
  53. package/dist/ast/python/utils.d.ts +63 -0
  54. package/dist/ast/python/utils.d.ts.map +1 -0
  55. package/dist/ast/python/utils.js +159 -0
  56. package/dist/ast/quick-scan.d.ts +40 -0
  57. package/dist/ast/quick-scan.d.ts.map +1 -0
  58. package/dist/ast/quick-scan.js +287 -0
  59. package/dist/ast/rust/index.d.ts +9 -0
  60. package/dist/ast/rust/index.d.ts.map +1 -0
  61. package/dist/ast/rust/index.js +8 -0
  62. package/dist/ast/rust/parser.d.ts +31 -0
  63. package/dist/ast/rust/parser.d.ts.map +1 -0
  64. package/dist/ast/rust/parser.js +416 -0
  65. package/dist/ast/rust/parser.test.d.ts +5 -0
  66. package/dist/ast/rust/parser.test.d.ts.map +1 -0
  67. package/dist/ast/rust/parser.test.js +329 -0
  68. package/dist/ast/rust/queries.d.ts +66 -0
  69. package/dist/ast/rust/queries.d.ts.map +1 -0
  70. package/dist/ast/rust/queries.js +132 -0
  71. package/dist/ast/rust/utils.d.ts +91 -0
  72. package/dist/ast/rust/utils.d.ts.map +1 -0
  73. package/dist/ast/rust/utils.js +254 -0
  74. package/dist/ast/swift/index.d.ts +10 -0
  75. package/dist/ast/swift/index.d.ts.map +1 -0
  76. package/dist/ast/swift/index.js +8 -0
  77. package/dist/ast/swift/parser.d.ts +31 -0
  78. package/dist/ast/swift/parser.d.ts.map +1 -0
  79. package/dist/ast/swift/parser.js +554 -0
  80. package/dist/ast/swift/parser.test.d.ts +5 -0
  81. package/dist/ast/swift/parser.test.d.ts.map +1 -0
  82. package/dist/ast/swift/parser.test.js +398 -0
  83. package/dist/ast/swift/queries.d.ts +71 -0
  84. package/dist/ast/swift/queries.d.ts.map +1 -0
  85. package/dist/ast/swift/queries.js +137 -0
  86. package/dist/ast/swift/utils.d.ts +94 -0
  87. package/dist/ast/swift/utils.d.ts.map +1 -0
  88. package/dist/ast/swift/utils.js +411 -0
  89. package/dist/ast/types.d.ts +96 -0
  90. package/dist/ast/types.d.ts.map +1 -0
  91. package/dist/ast/types.js +21 -0
  92. package/dist/ast/typescript.d.ts +24 -0
  93. package/dist/ast/typescript.d.ts.map +1 -0
  94. package/dist/ast/typescript.js +357 -0
  95. package/dist/cache/file-hash.d.ts +33 -0
  96. package/dist/cache/file-hash.d.ts.map +1 -0
  97. package/dist/cache/file-hash.js +59 -0
  98. package/dist/cache/index.d.ts +9 -0
  99. package/dist/cache/index.d.ts.map +1 -0
  100. package/dist/cache/index.js +8 -0
  101. package/dist/cache/smart-cache.d.ts +68 -0
  102. package/dist/cache/smart-cache.d.ts.map +1 -0
  103. package/dist/cache/smart-cache.js +266 -0
  104. package/dist/cache/types.d.ts +102 -0
  105. package/dist/cache/types.d.ts.map +1 -0
  106. package/dist/cache/types.js +6 -0
  107. package/dist/cli/analyze.d.ts +43 -0
  108. package/dist/cli/analyze.d.ts.map +1 -0
  109. package/dist/cli/analyze.js +250 -0
  110. package/dist/cli/doctor.d.ts +2 -0
  111. package/dist/cli/doctor.d.ts.map +1 -0
  112. package/dist/cli/doctor.js +127 -0
  113. package/dist/cli/hooks.d.ts +14 -0
  114. package/dist/cli/hooks.d.ts.map +1 -0
  115. package/dist/cli/hooks.js +229 -0
  116. package/dist/cli/index.d.ts +5 -0
  117. package/dist/cli/index.d.ts.map +1 -0
  118. package/dist/cli/index.js +4 -0
  119. package/dist/cli/setup.d.ts +10 -0
  120. package/dist/cli/setup.d.ts.map +1 -0
  121. package/dist/cli/setup.js +117 -0
  122. package/dist/cli/utils.d.ts +30 -0
  123. package/dist/cli/utils.d.ts.map +1 -0
  124. package/dist/cli/utils.js +116 -0
  125. package/dist/compressors/config.d.ts +9 -0
  126. package/dist/compressors/config.d.ts.map +1 -0
  127. package/dist/compressors/config.js +183 -0
  128. package/dist/compressors/conversation.d.ts +109 -0
  129. package/dist/compressors/conversation.d.ts.map +1 -0
  130. package/dist/compressors/conversation.js +404 -0
  131. package/dist/compressors/diff.d.ts +35 -0
  132. package/dist/compressors/diff.d.ts.map +1 -0
  133. package/dist/compressors/diff.js +389 -0
  134. package/dist/compressors/generic.d.ts +9 -0
  135. package/dist/compressors/generic.d.ts.map +1 -0
  136. package/dist/compressors/generic.js +188 -0
  137. package/dist/compressors/index.d.ts +31 -0
  138. package/dist/compressors/index.d.ts.map +1 -0
  139. package/dist/compressors/index.js +82 -0
  140. package/dist/compressors/logs.d.ts +9 -0
  141. package/dist/compressors/logs.d.ts.map +1 -0
  142. package/dist/compressors/logs.js +245 -0
  143. package/dist/compressors/multifile.d.ts +106 -0
  144. package/dist/compressors/multifile.d.ts.map +1 -0
  145. package/dist/compressors/multifile.js +498 -0
  146. package/dist/compressors/semantic.d.ts +33 -0
  147. package/dist/compressors/semantic.d.ts.map +1 -0
  148. package/dist/compressors/semantic.js +233 -0
  149. package/dist/compressors/stacktrace.d.ts +9 -0
  150. package/dist/compressors/stacktrace.d.ts.map +1 -0
  151. package/dist/compressors/stacktrace.js +259 -0
  152. package/dist/compressors/types.d.ts +146 -0
  153. package/dist/compressors/types.d.ts.map +1 -0
  154. package/dist/compressors/types.js +6 -0
  155. package/dist/config/output-config.d.ts +56 -0
  156. package/dist/config/output-config.d.ts.map +1 -0
  157. package/dist/config/output-config.js +78 -0
  158. package/dist/index.d.ts +21 -0
  159. package/dist/index.d.ts.map +1 -0
  160. package/dist/index.js +27 -0
  161. package/dist/middleware/chain.d.ts +49 -0
  162. package/dist/middleware/chain.d.ts.map +1 -0
  163. package/dist/middleware/chain.js +126 -0
  164. package/dist/middleware/index.d.ts +4 -0
  165. package/dist/middleware/index.d.ts.map +1 -0
  166. package/dist/middleware/index.js +3 -0
  167. package/dist/middleware/logging.d.ts +8 -0
  168. package/dist/middleware/logging.d.ts.map +1 -0
  169. package/dist/middleware/logging.js +71 -0
  170. package/dist/middleware/types.d.ts +58 -0
  171. package/dist/middleware/types.d.ts.map +1 -0
  172. package/dist/middleware/types.js +7 -0
  173. package/dist/parsers/eslint.d.ts +8 -0
  174. package/dist/parsers/eslint.d.ts.map +1 -0
  175. package/dist/parsers/eslint.js +132 -0
  176. package/dist/parsers/generic.d.ts +8 -0
  177. package/dist/parsers/generic.d.ts.map +1 -0
  178. package/dist/parsers/generic.js +234 -0
  179. package/dist/parsers/index.d.ts +34 -0
  180. package/dist/parsers/index.d.ts.map +1 -0
  181. package/dist/parsers/index.js +216 -0
  182. package/dist/parsers/types.d.ts +84 -0
  183. package/dist/parsers/types.d.ts.map +1 -0
  184. package/dist/parsers/types.js +6 -0
  185. package/dist/parsers/typescript.d.ts +8 -0
  186. package/dist/parsers/typescript.d.ts.map +1 -0
  187. package/dist/parsers/typescript.js +107 -0
  188. package/dist/pipelines/definitions.d.ts +50 -0
  189. package/dist/pipelines/definitions.d.ts.map +1 -0
  190. package/dist/pipelines/definitions.js +206 -0
  191. package/dist/sandbox/executor.d.ts +12 -0
  192. package/dist/sandbox/executor.d.ts.map +1 -0
  193. package/dist/sandbox/executor.js +191 -0
  194. package/dist/sandbox/index.d.ts +11 -0
  195. package/dist/sandbox/index.d.ts.map +1 -0
  196. package/dist/sandbox/index.js +9 -0
  197. package/dist/sandbox/sandbox.test.d.ts +7 -0
  198. package/dist/sandbox/sandbox.test.d.ts.map +1 -0
  199. package/dist/sandbox/sandbox.test.js +202 -0
  200. package/dist/sandbox/sdk/analyze.d.ts +36 -0
  201. package/dist/sandbox/sdk/analyze.d.ts.map +1 -0
  202. package/dist/sandbox/sdk/analyze.js +413 -0
  203. package/dist/sandbox/sdk/analyze.test.d.ts +7 -0
  204. package/dist/sandbox/sdk/analyze.test.d.ts.map +1 -0
  205. package/dist/sandbox/sdk/analyze.test.js +191 -0
  206. package/dist/sandbox/sdk/code.d.ts +20 -0
  207. package/dist/sandbox/sdk/code.d.ts.map +1 -0
  208. package/dist/sandbox/sdk/code.js +104 -0
  209. package/dist/sandbox/sdk/compress.d.ts +23 -0
  210. package/dist/sandbox/sdk/compress.d.ts.map +1 -0
  211. package/dist/sandbox/sdk/compress.js +107 -0
  212. package/dist/sandbox/sdk/conversation.d.ts +148 -0
  213. package/dist/sandbox/sdk/conversation.d.ts.map +1 -0
  214. package/dist/sandbox/sdk/conversation.js +177 -0
  215. package/dist/sandbox/sdk/files.d.ts +29 -0
  216. package/dist/sandbox/sdk/files.d.ts.map +1 -0
  217. package/dist/sandbox/sdk/files.js +41 -0
  218. package/dist/sandbox/sdk/git.d.ts +37 -0
  219. package/dist/sandbox/sdk/git.d.ts.map +1 -0
  220. package/dist/sandbox/sdk/git.js +313 -0
  221. package/dist/sandbox/sdk/git.test.d.ts +8 -0
  222. package/dist/sandbox/sdk/git.test.d.ts.map +1 -0
  223. package/dist/sandbox/sdk/git.test.js +160 -0
  224. package/dist/sandbox/sdk/index.d.ts +16 -0
  225. package/dist/sandbox/sdk/index.d.ts.map +1 -0
  226. package/dist/sandbox/sdk/index.js +15 -0
  227. package/dist/sandbox/sdk/multifile.d.ts +63 -0
  228. package/dist/sandbox/sdk/multifile.d.ts.map +1 -0
  229. package/dist/sandbox/sdk/multifile.js +130 -0
  230. package/dist/sandbox/sdk/pipeline.d.ts +16 -0
  231. package/dist/sandbox/sdk/pipeline.d.ts.map +1 -0
  232. package/dist/sandbox/sdk/pipeline.js +454 -0
  233. package/dist/sandbox/sdk/pipeline.test.d.ts +7 -0
  234. package/dist/sandbox/sdk/pipeline.test.d.ts.map +1 -0
  235. package/dist/sandbox/sdk/pipeline.test.js +197 -0
  236. package/dist/sandbox/sdk/search.d.ts +36 -0
  237. package/dist/sandbox/sdk/search.d.ts.map +1 -0
  238. package/dist/sandbox/sdk/search.js +338 -0
  239. package/dist/sandbox/sdk/search.test.d.ts +7 -0
  240. package/dist/sandbox/sdk/search.test.d.ts.map +1 -0
  241. package/dist/sandbox/sdk/search.test.js +183 -0
  242. package/dist/sandbox/sdk/utils.d.ts +18 -0
  243. package/dist/sandbox/sdk/utils.d.ts.map +1 -0
  244. package/dist/sandbox/sdk/utils.js +24 -0
  245. package/dist/sandbox/security/code-analyzer.d.ts +15 -0
  246. package/dist/sandbox/security/code-analyzer.d.ts.map +1 -0
  247. package/dist/sandbox/security/code-analyzer.js +87 -0
  248. package/dist/sandbox/security/index.d.ts +6 -0
  249. package/dist/sandbox/security/index.d.ts.map +1 -0
  250. package/dist/sandbox/security/index.js +5 -0
  251. package/dist/sandbox/security/path-validator.d.ts +23 -0
  252. package/dist/sandbox/security/path-validator.d.ts.map +1 -0
  253. package/dist/sandbox/security/path-validator.js +113 -0
  254. package/dist/sandbox/types.d.ts +577 -0
  255. package/dist/sandbox/types.d.ts.map +1 -0
  256. package/dist/sandbox/types.js +14 -0
  257. package/dist/server.d.ts +36 -0
  258. package/dist/server.d.ts.map +1 -0
  259. package/dist/server.js +133 -0
  260. package/dist/summarizers/build-logs.d.ts +11 -0
  261. package/dist/summarizers/build-logs.d.ts.map +1 -0
  262. package/dist/summarizers/build-logs.js +234 -0
  263. package/dist/summarizers/generic.d.ts +11 -0
  264. package/dist/summarizers/generic.d.ts.map +1 -0
  265. package/dist/summarizers/generic.js +93 -0
  266. package/dist/summarizers/index.d.ts +20 -0
  267. package/dist/summarizers/index.d.ts.map +1 -0
  268. package/dist/summarizers/index.js +43 -0
  269. package/dist/summarizers/server-logs.d.ts +11 -0
  270. package/dist/summarizers/server-logs.d.ts.map +1 -0
  271. package/dist/summarizers/server-logs.js +215 -0
  272. package/dist/summarizers/test-logs.d.ts +11 -0
  273. package/dist/summarizers/test-logs.d.ts.map +1 -0
  274. package/dist/summarizers/test-logs.js +258 -0
  275. package/dist/summarizers/types.d.ts +146 -0
  276. package/dist/summarizers/types.d.ts.map +1 -0
  277. package/dist/summarizers/types.js +21 -0
  278. package/dist/tools/analyze-build-output.d.ts +30 -0
  279. package/dist/tools/analyze-build-output.d.ts.map +1 -0
  280. package/dist/tools/analyze-build-output.js +45 -0
  281. package/dist/tools/analyze-context.d.ts +23 -0
  282. package/dist/tools/analyze-context.d.ts.map +1 -0
  283. package/dist/tools/analyze-context.js +78 -0
  284. package/dist/tools/auto-optimize.d.ts +9 -0
  285. package/dist/tools/auto-optimize.d.ts.map +1 -0
  286. package/dist/tools/auto-optimize.js +191 -0
  287. package/dist/tools/code-execute.d.ts +9 -0
  288. package/dist/tools/code-execute.d.ts.map +1 -0
  289. package/dist/tools/code-execute.js +84 -0
  290. package/dist/tools/code-skeleton.d.ts +33 -0
  291. package/dist/tools/code-skeleton.d.ts.map +1 -0
  292. package/dist/tools/code-skeleton.js +206 -0
  293. package/dist/tools/compress-context.d.ts +33 -0
  294. package/dist/tools/compress-context.d.ts.map +1 -0
  295. package/dist/tools/compress-context.js +64 -0
  296. package/dist/tools/context-budget.d.ts +43 -0
  297. package/dist/tools/context-budget.d.ts.map +1 -0
  298. package/dist/tools/context-budget.js +260 -0
  299. package/dist/tools/context-budget.test.d.ts +5 -0
  300. package/dist/tools/context-budget.test.d.ts.map +1 -0
  301. package/dist/tools/context-budget.test.js +219 -0
  302. package/dist/tools/conversation-compress.d.ts +46 -0
  303. package/dist/tools/conversation-compress.d.ts.map +1 -0
  304. package/dist/tools/conversation-compress.js +78 -0
  305. package/dist/tools/conversation-memory.d.ts +75 -0
  306. package/dist/tools/conversation-memory.d.ts.map +1 -0
  307. package/dist/tools/conversation-memory.js +289 -0
  308. package/dist/tools/deduplicate-errors.d.ts +30 -0
  309. package/dist/tools/deduplicate-errors.d.ts.map +1 -0
  310. package/dist/tools/deduplicate-errors.js +72 -0
  311. package/dist/tools/detect-retry-loop.d.ts +40 -0
  312. package/dist/tools/detect-retry-loop.d.ts.map +1 -0
  313. package/dist/tools/detect-retry-loop.js +212 -0
  314. package/dist/tools/diff-compress.d.ts +40 -0
  315. package/dist/tools/diff-compress.d.ts.map +1 -0
  316. package/dist/tools/diff-compress.js +94 -0
  317. package/dist/tools/discover-tools.d.ts +11 -0
  318. package/dist/tools/discover-tools.d.ts.map +1 -0
  319. package/dist/tools/discover-tools.js +163 -0
  320. package/dist/tools/dynamic-loader.d.ts +131 -0
  321. package/dist/tools/dynamic-loader.d.ts.map +1 -0
  322. package/dist/tools/dynamic-loader.js +378 -0
  323. package/dist/tools/dynamic-loader.test.d.ts +10 -0
  324. package/dist/tools/dynamic-loader.test.d.ts.map +1 -0
  325. package/dist/tools/dynamic-loader.test.js +164 -0
  326. package/dist/tools/lazy-mcp.d.ts +31 -0
  327. package/dist/tools/lazy-mcp.d.ts.map +1 -0
  328. package/dist/tools/lazy-mcp.js +151 -0
  329. package/dist/tools/lazy-mcp.test.d.ts +10 -0
  330. package/dist/tools/lazy-mcp.test.d.ts.map +1 -0
  331. package/dist/tools/lazy-mcp.test.js +172 -0
  332. package/dist/tools/multifile-compress.d.ts +36 -0
  333. package/dist/tools/multifile-compress.d.ts.map +1 -0
  334. package/dist/tools/multifile-compress.js +223 -0
  335. package/dist/tools/optimization-tips.d.ts +18 -0
  336. package/dist/tools/optimization-tips.d.ts.map +1 -0
  337. package/dist/tools/optimization-tips.js +133 -0
  338. package/dist/tools/registry.d.ts +70 -0
  339. package/dist/tools/registry.d.ts.map +1 -0
  340. package/dist/tools/registry.js +169 -0
  341. package/dist/tools/semantic-compress.d.ts +39 -0
  342. package/dist/tools/semantic-compress.d.ts.map +1 -0
  343. package/dist/tools/semantic-compress.js +113 -0
  344. package/dist/tools/semantic-compress.test.d.ts +5 -0
  345. package/dist/tools/semantic-compress.test.d.ts.map +1 -0
  346. package/dist/tools/semantic-compress.test.js +182 -0
  347. package/dist/tools/session-stats.d.ts +34 -0
  348. package/dist/tools/session-stats.d.ts.map +1 -0
  349. package/dist/tools/session-stats.js +194 -0
  350. package/dist/tools/set-output-config.d.ts +38 -0
  351. package/dist/tools/set-output-config.d.ts.map +1 -0
  352. package/dist/tools/set-output-config.js +122 -0
  353. package/dist/tools/smart-cache-tool.d.ts +38 -0
  354. package/dist/tools/smart-cache-tool.d.ts.map +1 -0
  355. package/dist/tools/smart-cache-tool.js +224 -0
  356. package/dist/tools/smart-file-read.d.ts +52 -0
  357. package/dist/tools/smart-file-read.d.ts.map +1 -0
  358. package/dist/tools/smart-file-read.js +481 -0
  359. package/dist/tools/smart-pipeline.d.ts +40 -0
  360. package/dist/tools/smart-pipeline.d.ts.map +1 -0
  361. package/dist/tools/smart-pipeline.js +295 -0
  362. package/dist/tools/summarize-logs.d.ts +36 -0
  363. package/dist/tools/summarize-logs.d.ts.map +1 -0
  364. package/dist/tools/summarize-logs.js +184 -0
  365. package/dist/tools/token-budget.test.d.ts +11 -0
  366. package/dist/tools/token-budget.test.d.ts.map +1 -0
  367. package/dist/tools/token-budget.test.js +275 -0
  368. package/dist/utils/bm25.d.ts +86 -0
  369. package/dist/utils/bm25.d.ts.map +1 -0
  370. package/dist/utils/bm25.js +153 -0
  371. package/dist/utils/bm25.test.d.ts +5 -0
  372. package/dist/utils/bm25.test.d.ts.map +1 -0
  373. package/dist/utils/bm25.test.js +156 -0
  374. package/dist/utils/command-normalizer.d.ts +39 -0
  375. package/dist/utils/command-normalizer.d.ts.map +1 -0
  376. package/dist/utils/command-normalizer.js +90 -0
  377. package/dist/utils/content-detector.d.ts +27 -0
  378. package/dist/utils/content-detector.d.ts.map +1 -0
  379. package/dist/utils/content-detector.js +127 -0
  380. package/dist/utils/embeddings.d.ts +54 -0
  381. package/dist/utils/embeddings.d.ts.map +1 -0
  382. package/dist/utils/embeddings.js +97 -0
  383. package/dist/utils/embeddings.test.d.ts +8 -0
  384. package/dist/utils/embeddings.test.d.ts.map +1 -0
  385. package/dist/utils/embeddings.test.js +96 -0
  386. package/dist/utils/error-normalizer.d.ts +39 -0
  387. package/dist/utils/error-normalizer.d.ts.map +1 -0
  388. package/dist/utils/error-normalizer.js +233 -0
  389. package/dist/utils/hybrid-search.d.ts +79 -0
  390. package/dist/utils/hybrid-search.d.ts.map +1 -0
  391. package/dist/utils/hybrid-search.js +146 -0
  392. package/dist/utils/hybrid-search.test.d.ts +5 -0
  393. package/dist/utils/hybrid-search.test.d.ts.map +1 -0
  394. package/dist/utils/hybrid-search.test.js +172 -0
  395. package/dist/utils/index.d.ts +13 -0
  396. package/dist/utils/index.d.ts.map +1 -0
  397. package/dist/utils/index.js +12 -0
  398. package/dist/utils/language-detector.d.ts +27 -0
  399. package/dist/utils/language-detector.d.ts.map +1 -0
  400. package/dist/utils/language-detector.js +94 -0
  401. package/dist/utils/log-parser.d.ts +46 -0
  402. package/dist/utils/log-parser.d.ts.map +1 -0
  403. package/dist/utils/log-parser.js +287 -0
  404. package/dist/utils/output-estimator.d.ts +54 -0
  405. package/dist/utils/output-estimator.d.ts.map +1 -0
  406. package/dist/utils/output-estimator.js +119 -0
  407. package/dist/utils/output-estimator.test.d.ts +5 -0
  408. package/dist/utils/output-estimator.test.d.ts.map +1 -0
  409. package/dist/utils/output-estimator.test.js +115 -0
  410. package/dist/utils/output-similarity.d.ts +48 -0
  411. package/dist/utils/output-similarity.d.ts.map +1 -0
  412. package/dist/utils/output-similarity.js +140 -0
  413. package/dist/utils/project-detector.d.ts +16 -0
  414. package/dist/utils/project-detector.d.ts.map +1 -0
  415. package/dist/utils/project-detector.js +119 -0
  416. package/dist/utils/segment-scorer.d.ts +99 -0
  417. package/dist/utils/segment-scorer.d.ts.map +1 -0
  418. package/dist/utils/segment-scorer.js +148 -0
  419. package/dist/utils/signature-grouper.d.ts +58 -0
  420. package/dist/utils/signature-grouper.d.ts.map +1 -0
  421. package/dist/utils/signature-grouper.js +185 -0
  422. package/dist/utils/tfidf.d.ts +45 -0
  423. package/dist/utils/tfidf.d.ts.map +1 -0
  424. package/dist/utils/tfidf.js +204 -0
  425. package/dist/utils/tfidf.test.d.ts +5 -0
  426. package/dist/utils/tfidf.test.d.ts.map +1 -0
  427. package/dist/utils/tfidf.test.js +115 -0
  428. package/dist/utils/token-counter.d.ts +35 -0
  429. package/dist/utils/token-counter.d.ts.map +1 -0
  430. package/dist/utils/token-counter.js +83 -0
  431. package/dist/utils/toon-serializer.d.ts +120 -0
  432. package/dist/utils/toon-serializer.d.ts.map +1 -0
  433. package/dist/utils/toon-serializer.js +472 -0
  434. package/dist/utils/toon-serializer.test.d.ts +7 -0
  435. package/dist/utils/toon-serializer.test.d.ts.map +1 -0
  436. package/dist/utils/toon-serializer.test.js +290 -0
  437. package/package.json +63 -0
  438. package/scripts/install.ps1 +133 -0
  439. package/scripts/install.sh +183 -0
  440. package/scripts/pre-commit-hook.sh +86 -0
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Code Skeleton Tool
3
+ *
4
+ * Extracts code skeleton (signatures only) from source files.
5
+ * Provides configurable depth levels and token statistics.
6
+ */
7
+ import { z } from "zod";
8
+ import * as fs from "fs/promises";
9
+ import * as path from "path";
10
+ import { parseFile, hasParserSupport } from "../ast/index.js";
11
+ import { detectLanguageFromPath } from "../utils/language-detector.js";
12
+ import { countTokens } from "../utils/token-counter.js";
13
+ // Minimal schema - includeTypes/includeComments rarely changed
14
+ export const codeSkeletonSchema = {
15
+ type: "object",
16
+ properties: {
17
+ filePath: { type: "string" },
18
+ depth: { enum: [1, 2, 3] },
19
+ },
20
+ required: ["filePath"],
21
+ };
22
+ // Schema Zod
23
+ const inputSchema = z.object({
24
+ filePath: z.string(),
25
+ includeTypes: z.boolean().optional().default(true),
26
+ includeComments: z.boolean().optional().default(true),
27
+ depth: z.number().min(1).max(3).optional().default(1),
28
+ });
29
+ /**
30
+ * Format file structure as skeleton based on depth level
31
+ *
32
+ * Depth levels:
33
+ * - 1: Signatures only (minimal)
34
+ * - 2: Signatures + inline doc preview
35
+ * - 3: Full signatures with complete docs
36
+ */
37
+ function formatSkeletonByDepth(structure, depth, includeTypes, includeComments, language) {
38
+ const lines = [];
39
+ // Types/Interfaces (if includeTypes)
40
+ if (includeTypes && structure.types?.length) {
41
+ for (const t of structure.types) {
42
+ const exported = t.isExported ? "export " : "";
43
+ if (depth >= 2 && includeComments && t.documentation) {
44
+ const doc = t.documentation.split("\n")[0];
45
+ if (depth === 3) {
46
+ lines.push(`/** ${t.documentation} */`);
47
+ }
48
+ else {
49
+ lines.push(`/** ${doc} */`);
50
+ }
51
+ }
52
+ lines.push(`${exported}${t.signature || `type ${t.name}`}`);
53
+ }
54
+ if (structure.types.length > 0)
55
+ lines.push("");
56
+ }
57
+ // Functions (top-level only, not methods)
58
+ const topLevelFunctions = structure.functions?.filter((f) => !f.parent) || [];
59
+ if (topLevelFunctions.length > 0) {
60
+ for (const fn of topLevelFunctions) {
61
+ const exported = fn.isExported ? "export " : "";
62
+ const asyncMod = fn.isAsync ? "async " : "";
63
+ const sig = fn.signature || `function ${fn.name}()`;
64
+ if (depth === 1) {
65
+ lines.push(`${exported}${asyncMod}${sig}`);
66
+ }
67
+ else if (depth >= 2) {
68
+ if (includeComments && fn.documentation) {
69
+ const doc = fn.documentation.split("\n")[0];
70
+ if (depth === 3) {
71
+ lines.push(`/** ${fn.documentation} */`);
72
+ lines.push(`${exported}${asyncMod}${sig}`);
73
+ }
74
+ else {
75
+ lines.push(`${exported}${asyncMod}${sig} // ${doc}`);
76
+ }
77
+ }
78
+ else {
79
+ lines.push(`${exported}${asyncMod}${sig}`);
80
+ }
81
+ }
82
+ }
83
+ lines.push("");
84
+ }
85
+ // Classes with methods
86
+ if (structure.classes?.length) {
87
+ for (const cls of structure.classes) {
88
+ const exported = cls.isExported ? "export " : "";
89
+ if (depth >= 2 && includeComments && cls.documentation) {
90
+ const doc = cls.documentation.split("\n")[0];
91
+ if (depth === 3) {
92
+ lines.push(`/** ${cls.documentation} */`);
93
+ }
94
+ else {
95
+ lines.push(`/** ${doc} */`);
96
+ }
97
+ }
98
+ lines.push(`${exported}class ${cls.name} {`);
99
+ // Methods
100
+ const methods = structure.functions?.filter((f) => f.parent === cls.name) || [];
101
+ for (const m of methods) {
102
+ const asyncMod = m.isAsync ? "async " : "";
103
+ const sig = m.signature || `${m.name}()`;
104
+ if (depth === 1) {
105
+ lines.push(` ${asyncMod}${sig}`);
106
+ }
107
+ else if (depth >= 2) {
108
+ if (includeComments && m.documentation) {
109
+ const doc = m.documentation.split("\n")[0];
110
+ if (depth === 3) {
111
+ lines.push(` /** ${m.documentation} */`);
112
+ lines.push(` ${asyncMod}${sig}`);
113
+ }
114
+ else {
115
+ lines.push(` ${asyncMod}${sig} // ${doc}`);
116
+ }
117
+ }
118
+ else {
119
+ lines.push(` ${asyncMod}${sig}`);
120
+ }
121
+ }
122
+ }
123
+ lines.push(`}`);
124
+ lines.push("");
125
+ }
126
+ }
127
+ // Exported variables (constants, etc.)
128
+ const exportedVars = structure.variables?.filter((v) => v.isExported) || [];
129
+ if (exportedVars.length > 0) {
130
+ for (const v of exportedVars) {
131
+ if (depth >= 2 && includeComments && v.documentation) {
132
+ const doc = v.documentation.split("\n")[0];
133
+ lines.push(`// ${doc}`);
134
+ }
135
+ lines.push(`export ${v.signature || `const ${v.name}`}`);
136
+ }
137
+ lines.push("");
138
+ }
139
+ return lines.join("\n").trim();
140
+ }
141
+ /**
142
+ * Execute code-skeleton tool
143
+ */
144
+ export async function executeCodeSkeleton(args) {
145
+ const input = inputSchema.parse(args);
146
+ const workingDir = process.cwd();
147
+ // Resolve path
148
+ const resolvedPath = path.isAbsolute(input.filePath)
149
+ ? path.normalize(input.filePath)
150
+ : path.resolve(workingDir, input.filePath);
151
+ // Read file
152
+ let content;
153
+ try {
154
+ content = await fs.readFile(resolvedPath, "utf-8");
155
+ }
156
+ catch {
157
+ return {
158
+ content: [{ type: "text", text: `File not found: ${resolvedPath}` }],
159
+ };
160
+ }
161
+ // Detect language
162
+ const language = detectLanguageFromPath(resolvedPath);
163
+ if (!hasParserSupport(language)) {
164
+ return {
165
+ content: [
166
+ {
167
+ type: "text",
168
+ text: `Language '${language}' not supported for skeleton extraction. Supported: typescript, javascript, python, go, rust, php, swift`,
169
+ },
170
+ ],
171
+ };
172
+ }
173
+ // Parse file
174
+ const structure = parseFile(content, language);
175
+ // Generate skeleton
176
+ const skeleton = formatSkeletonByDepth(structure, input.depth, input.includeTypes, input.includeComments, language);
177
+ // Calculate statistics
178
+ const originalTokens = countTokens(content);
179
+ const skeletonTokens = countTokens(skeleton);
180
+ const savings = originalTokens > 0
181
+ ? Math.round((1 - skeletonTokens / originalTokens) * 100)
182
+ : 0;
183
+ // Format output
184
+ const depthLabels = ["", "signatures", "signatures+docs", "full"];
185
+ const output = [
186
+ `## Code Skeleton: ${input.filePath}`,
187
+ "",
188
+ `**Language:** ${language} | **Depth:** ${input.depth} (${depthLabels[input.depth]})`,
189
+ `**Options:** types=${input.includeTypes}, comments=${input.includeComments}`,
190
+ `**Tokens:** ${skeletonTokens} (was ${originalTokens}) | **Savings:** ${savings}%`,
191
+ "",
192
+ "```" + language,
193
+ skeleton,
194
+ "```",
195
+ ].join("\n");
196
+ return { content: [{ type: "text", text: output }] };
197
+ }
198
+ /**
199
+ * Code Skeleton Tool Definition
200
+ */
201
+ export const codeSkeletonTool = {
202
+ name: "code_skeleton",
203
+ description: "Extract signatures only (no bodies). Depth: 1=minimal, 2=+docs, 3=full.",
204
+ inputSchema: codeSkeletonSchema,
205
+ execute: executeCodeSkeleton,
206
+ };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Compress Context Tool
3
+ *
4
+ * Compresses verbose text content (logs, stack traces, configs)
5
+ * while preserving essential information.
6
+ */
7
+ import type { ToolDefinition } from "./registry.js";
8
+ export declare const compressContextSchema: {
9
+ type: "object";
10
+ properties: {
11
+ content: {
12
+ type: string;
13
+ };
14
+ contentType: {
15
+ enum: string[];
16
+ };
17
+ targetRatio: {
18
+ type: string;
19
+ };
20
+ detail: {
21
+ enum: string[];
22
+ };
23
+ };
24
+ required: string[];
25
+ };
26
+ export declare function executeCompressContext(args: unknown): Promise<{
27
+ content: Array<{
28
+ type: "text";
29
+ text: string;
30
+ }>;
31
+ }>;
32
+ export declare const compressContextTool: ToolDefinition;
33
+ //# sourceMappingURL=compress-context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compress-context.d.ts","sourceRoot":"","sources":["../../src/tools/compress-context.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAGpD,eAAO,MAAM,qBAAqB;;;;;;;;;;;;;;;;;CASjC,CAAC;AA+BF,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,OAAO,GACZ,OAAO,CAAC;IAAE,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAA;CAAE,CAAC,CAkC7D;AAED,eAAO,MAAM,mBAAmB,EAAE,cAKjC,CAAC"}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Compress Context Tool
3
+ *
4
+ * Compresses verbose text content (logs, stack traces, configs)
5
+ * while preserving essential information.
6
+ */
7
+ import { z } from "zod";
8
+ import { compressContent, analyzeContent } from "../compressors/index.js";
9
+ // Minimal schema - preservePatterns rarely used
10
+ export const compressContextSchema = {
11
+ type: "object",
12
+ properties: {
13
+ content: { type: "string" },
14
+ contentType: { enum: ["logs", "stacktrace", "config", "code", "generic"] },
15
+ targetRatio: { type: "number" },
16
+ detail: { enum: ["minimal", "normal", "detailed"] },
17
+ },
18
+ required: ["content"],
19
+ };
20
+ const inputSchema = z.object({
21
+ content: z.string(),
22
+ contentType: z.enum(["logs", "stacktrace", "config", "code", "generic"]).optional(),
23
+ targetRatio: z.number().min(0.01).max(1).optional(),
24
+ preservePatterns: z.array(z.string()).optional(),
25
+ detail: z.enum(["minimal", "normal", "detailed"]).optional().default("normal"),
26
+ });
27
+ /**
28
+ * Format compression result as markdown
29
+ */
30
+ function formatResult(compressed, stats, contentType, _omittedInfo) {
31
+ // Minimal header to save tokens
32
+ const header = `[${contentType}] ${stats.originalTokens}→${stats.compressedTokens} tokens (-${stats.reductionPercent}%)`;
33
+ return `${header}\n${compressed}`;
34
+ }
35
+ export async function executeCompressContext(args) {
36
+ const input = inputSchema.parse(args);
37
+ // Convert preserve patterns to RegExp
38
+ const preservePatterns = input.preservePatterns?.map((p) => new RegExp(p));
39
+ // Analyze content if type not provided
40
+ const analysis = analyzeContent(input.content);
41
+ const contentType = (input.contentType ?? analysis.detectedType);
42
+ // Compress
43
+ const result = compressContent(input.content, {
44
+ contentType,
45
+ detail: input.detail,
46
+ targetRatio: input.targetRatio,
47
+ preservePatterns,
48
+ });
49
+ // Update session state with token savings
50
+ const tokensSaved = result.stats.originalTokens - result.stats.compressedTokens;
51
+ if (tokensSaved > 0) {
52
+ }
53
+ // Format output
54
+ const formatted = formatResult(result.compressed, result.stats, contentType, result.omittedInfo);
55
+ return {
56
+ content: [{ type: "text", text: formatted }],
57
+ };
58
+ }
59
+ export const compressContextTool = {
60
+ name: "compress_context",
61
+ description: "Compress verbose text (logs, stack traces, configs). 40-90% reduction.",
62
+ inputSchema: compressContextSchema,
63
+ execute: executeCompressContext,
64
+ };
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Context Budget Tool
3
+ *
4
+ * Proactive token budget management - analyze content BEFORE sending to LLM
5
+ * to estimate costs, check budget constraints, and get optimization recommendations.
6
+ */
7
+ import type { ToolDefinition } from "./registry.js";
8
+ /**
9
+ * JSON Schema for MCP tool registration
10
+ */
11
+ export declare const contextBudgetSchema: {
12
+ type: "object";
13
+ properties: {
14
+ content: {
15
+ type: string;
16
+ };
17
+ model: {
18
+ enum: string[];
19
+ };
20
+ budgetTokens: {
21
+ type: string;
22
+ };
23
+ includeEstimatedOutput: {
24
+ type: string;
25
+ };
26
+ };
27
+ required: string[];
28
+ };
29
+ /**
30
+ * Execute the context-budget tool
31
+ */
32
+ export declare function executeContextBudget(args: unknown): Promise<{
33
+ content: Array<{
34
+ type: "text";
35
+ text: string;
36
+ }>;
37
+ isError?: boolean;
38
+ }>;
39
+ /**
40
+ * Tool definition for MCP registration
41
+ */
42
+ export declare const contextBudgetTool: ToolDefinition;
43
+ //# sourceMappingURL=context-budget.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-budget.d.ts","sourceRoot":"","sources":["../../src/tools/context-budget.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAapD;;GAEG;AAEH,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;CAS/B,CAAC;AAwPF;;GAEG;AACH,wBAAsB,oBAAoB,CACxC,IAAI,EAAE,OAAO,GACZ,OAAO,CAAC;IACT,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC/C,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC,CAqFD;AAED;;GAEG;AACH,eAAO,MAAM,iBAAiB,EAAE,cAK/B,CAAC"}
@@ -0,0 +1,260 @@
1
+ /**
2
+ * Context Budget Tool
3
+ *
4
+ * Proactive token budget management - analyze content BEFORE sending to LLM
5
+ * to estimate costs, check budget constraints, and get optimization recommendations.
6
+ */
7
+ import { z } from "zod";
8
+ import { countTokens } from "../utils/token-counter.js";
9
+ import { detectContentType } from "../utils/content-detector.js";
10
+ import { estimateOutputTokens } from "../utils/output-estimator.js";
11
+ import { ANTHROPIC_MODELS, DEFAULT_MODEL, calculateCost, formatCost, calculateContextUsage, } from "@ctxopt/shared";
12
+ /**
13
+ * JSON Schema for MCP tool registration
14
+ */
15
+ // Minimal schema
16
+ export const contextBudgetSchema = {
17
+ type: "object",
18
+ properties: {
19
+ content: { type: "string" },
20
+ model: { enum: ["claude-opus-4-20250514", "claude-sonnet-4-20250514", "claude-3-5-haiku-20241022"] },
21
+ budgetTokens: { type: "number" },
22
+ includeEstimatedOutput: { type: "boolean" },
23
+ },
24
+ required: ["content"],
25
+ };
26
+ /**
27
+ * Zod schema for runtime validation
28
+ */
29
+ const inputSchema = z.object({
30
+ content: z.string().min(1, "Content is required"),
31
+ model: z
32
+ .enum([
33
+ "claude-opus-4-20250514",
34
+ "claude-sonnet-4-20250514",
35
+ "claude-3-5-haiku-20241022",
36
+ ])
37
+ .optional()
38
+ .default(DEFAULT_MODEL),
39
+ budgetTokens: z.number().min(50).optional(),
40
+ includeEstimatedOutput: z.boolean().optional().default(true),
41
+ });
42
+ /**
43
+ * Analyze content for optimization opportunities
44
+ */
45
+ function analyzeContent(content, tokenCount) {
46
+ const lines = content.split("\n");
47
+ const nonEmptyLines = lines.filter((l) => l.trim().length > 0);
48
+ const uniqueLines = new Set(nonEmptyLines);
49
+ const duplicateRatio = nonEmptyLines.length > 0
50
+ ? 1 - uniqueLines.size / nonEmptyLines.length
51
+ : 0;
52
+ return {
53
+ hasCode: /```|\bfunction\b|\bclass\b|\bconst\b|\blet\b|\bvar\b|\bimport\b|\bexport\b|def\s+\w+|func\s+\w+|fn\s+\w+/.test(content),
54
+ hasLogs: /\[(INFO|DEBUG|WARN|ERROR|FATAL|TRACE)\]|\b(INFO|DEBUG|WARN|ERROR|FATAL)\b:|\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}/.test(content),
55
+ hasErrors: /\b(error|exception|failed|failure|stack\s*trace|traceback|panic)\b/i.test(content) || /at\s+\w+\s*\(/.test(content),
56
+ hasDuplicates: duplicateRatio > 0.2, // >20% duplicates
57
+ isLongProse: tokenCount > 2000 && !/```/.test(content),
58
+ tokenCount,
59
+ };
60
+ }
61
+ /**
62
+ * Generate optimization recommendations based on content analysis
63
+ */
64
+ function generateRecommendations(analysis) {
65
+ const recommendations = [];
66
+ // Rule 1: Code content -> smart_file_read
67
+ if (analysis.hasCode && analysis.tokenCount > 500) {
68
+ recommendations.push({
69
+ action: "Extract only relevant code sections",
70
+ tool: "smart_file_read",
71
+ expectedSavings: 60,
72
+ description: "Use smart_file_read to extract specific functions/classes instead of full file content. Typical savings: 50-70%.",
73
+ });
74
+ }
75
+ // Rule 2: Logs -> summarize_logs
76
+ if (analysis.hasLogs) {
77
+ recommendations.push({
78
+ action: "Summarize log output",
79
+ tool: "summarize_logs",
80
+ expectedSavings: 85,
81
+ description: "Use summarize_logs to extract key events, errors, and patterns. Typical savings: 80-90%.",
82
+ });
83
+ }
84
+ // Rule 3: Errors + duplicates -> deduplicate_errors
85
+ if (analysis.hasErrors && analysis.hasDuplicates) {
86
+ recommendations.push({
87
+ action: "Deduplicate repeated errors",
88
+ tool: "deduplicate_errors",
89
+ expectedSavings: 80,
90
+ description: "Use deduplicate_errors to group identical error messages. Typical savings: 80-95%.",
91
+ });
92
+ }
93
+ else if (analysis.hasErrors) {
94
+ // Rule 4: Errors alone -> auto_optimize
95
+ recommendations.push({
96
+ action: "Compress error output",
97
+ tool: "auto_optimize",
98
+ expectedSavings: 70,
99
+ description: "Use auto_optimize to intelligently compress error output. Typical savings: 60-90%.",
100
+ });
101
+ }
102
+ // Rule 5: Long prose -> semantic_compress
103
+ if (analysis.isLongProse) {
104
+ recommendations.push({
105
+ action: "Apply semantic compression",
106
+ tool: "semantic_compress",
107
+ expectedSavings: 50,
108
+ description: "Use semantic_compress to preserve key information while reducing verbosity. Typical savings: 40-60%.",
109
+ });
110
+ }
111
+ // Rule 6: Duplicates without errors -> compress_context
112
+ if (analysis.hasDuplicates && !analysis.hasErrors) {
113
+ recommendations.push({
114
+ action: "Remove redundant content",
115
+ tool: "compress_context",
116
+ expectedSavings: 45,
117
+ description: "Use compress_context to eliminate duplicate lines and normalize whitespace. Typical savings: 40-60%.",
118
+ });
119
+ }
120
+ // Rule 7: Large generic content -> auto_optimize (fallback)
121
+ if (analysis.tokenCount > 3000 && recommendations.length === 0) {
122
+ recommendations.push({
123
+ action: "Auto-optimize content",
124
+ tool: "auto_optimize",
125
+ expectedSavings: 50,
126
+ description: "Use auto_optimize to automatically detect content type and apply best compression strategy.",
127
+ });
128
+ }
129
+ return recommendations;
130
+ }
131
+ /**
132
+ * Format the analysis result as markdown
133
+ */
134
+ function formatOutput(result, model) {
135
+ const modelInfo = ANTHROPIC_MODELS[model];
136
+ const modelName = modelInfo?.name || model;
137
+ const parts = [];
138
+ parts.push("## Context Budget Analysis\n");
139
+ // Token summary table
140
+ parts.push("### Token Estimate\n");
141
+ parts.push("| Metric | Value |");
142
+ parts.push("|--------|-------|");
143
+ parts.push(`| Input Tokens | ${result.inputTokens.toLocaleString()} |`);
144
+ parts.push(`| Estimated Output | ${result.estimatedOutputTokens.toLocaleString()} |`);
145
+ parts.push(`| **Total Estimated** | **${result.totalEstimatedTokens.toLocaleString()}** |`);
146
+ parts.push(`| Estimated Cost | ${formatCost(result.estimatedCostUSD * 1_000_000)} |`);
147
+ parts.push(`| Context Usage | ${result.contextUsagePercent}% |`);
148
+ parts.push(`| Model | ${modelName} |\n`);
149
+ // Budget status
150
+ if (result.budgetRemaining !== null) {
151
+ const withinBudget = result.withinBudget;
152
+ const status = withinBudget ? "Within Budget" : "**OVER BUDGET**";
153
+ const icon = withinBudget ? "" : "";
154
+ const remaining = withinBudget
155
+ ? `${result.budgetRemaining.toLocaleString()} tokens remaining`
156
+ : `${Math.abs(result.budgetRemaining).toLocaleString()} tokens over limit`;
157
+ parts.push(`### Budget Status: ${icon} ${status}`);
158
+ parts.push(`${remaining}\n`);
159
+ }
160
+ // Recommendations
161
+ if (result.recommendations.length > 0) {
162
+ parts.push("### Optimization Recommendations\n");
163
+ for (const rec of result.recommendations) {
164
+ parts.push(`**${rec.action}** (~${rec.expectedSavings}% savings)`);
165
+ parts.push(`- Tool: \`${rec.tool}\``);
166
+ parts.push(`- ${rec.description}\n`);
167
+ }
168
+ parts.push(`**Potential Savings:** Up to ${result.potentialSavingsPercent}% reduction possible\n`);
169
+ }
170
+ else {
171
+ parts.push("### Status\n");
172
+ parts.push("Content is already well-optimized. No immediate recommendations.\n");
173
+ }
174
+ // Quick action hint
175
+ if (result.autoOptimizeAvailable) {
176
+ parts.push("---");
177
+ parts.push("*Tip: Use `auto_optimize` to automatically apply the best compression strategy.*");
178
+ }
179
+ return parts.join("\n");
180
+ }
181
+ /**
182
+ * Execute the context-budget tool
183
+ */
184
+ export async function executeContextBudget(args) {
185
+ // Validate input
186
+ const parseResult = inputSchema.safeParse(args);
187
+ if (!parseResult.success) {
188
+ return {
189
+ content: [
190
+ {
191
+ type: "text",
192
+ text: `Invalid input: ${parseResult.error.errors.map((e) => e.message).join(", ")}`,
193
+ },
194
+ ],
195
+ isError: true,
196
+ };
197
+ }
198
+ const { content, model, budgetTokens, includeEstimatedOutput } = parseResult.data;
199
+ // Count input tokens
200
+ const inputTokens = countTokens(content);
201
+ // Detect content type
202
+ const contentType = detectContentType(content);
203
+ // Analyze content for recommendations
204
+ const analysis = analyzeContent(content, inputTokens);
205
+ // Estimate output tokens
206
+ const outputEstimate = includeEstimatedOutput
207
+ ? estimateOutputTokens(content, inputTokens, contentType)
208
+ : { estimated: 0, confidence: "high", reasoning: "Disabled" };
209
+ const estimatedOutputTokens = outputEstimate.estimated;
210
+ const totalEstimatedTokens = inputTokens + estimatedOutputTokens;
211
+ // Calculate cost
212
+ const costs = calculateCost(model, inputTokens, estimatedOutputTokens);
213
+ const estimatedCostUSD = costs.totalCostMicros / 1_000_000;
214
+ // Calculate context usage
215
+ const contextUsagePercent = calculateContextUsage(totalEstimatedTokens, model);
216
+ // Budget check
217
+ const withinBudget = budgetTokens
218
+ ? totalEstimatedTokens <= budgetTokens
219
+ : true;
220
+ const budgetRemaining = budgetTokens
221
+ ? budgetTokens - totalEstimatedTokens
222
+ : null;
223
+ // Generate recommendations
224
+ const recommendations = generateRecommendations(analysis);
225
+ // Calculate potential savings (max of all recommendations)
226
+ const potentialSavingsPercent = recommendations.length > 0
227
+ ? Math.min(95, Math.max(...recommendations.map((r) => r.expectedSavings)))
228
+ : 0;
229
+ // Determine if auto_optimize can help
230
+ const autoOptimizeAvailable = inputTokens > 500 &&
231
+ (analysis.hasLogs ||
232
+ analysis.hasErrors ||
233
+ analysis.hasDuplicates ||
234
+ analysis.isLongProse);
235
+ const result = {
236
+ inputTokens,
237
+ estimatedOutputTokens,
238
+ totalEstimatedTokens,
239
+ estimatedCostUSD,
240
+ contextUsagePercent,
241
+ withinBudget,
242
+ budgetRemaining,
243
+ recommendations,
244
+ autoOptimizeAvailable,
245
+ potentialSavingsPercent,
246
+ };
247
+ const formatted = formatOutput(result, model);
248
+ return {
249
+ content: [{ type: "text", text: formatted }],
250
+ };
251
+ }
252
+ /**
253
+ * Tool definition for MCP registration
254
+ */
255
+ export const contextBudgetTool = {
256
+ name: "context_budget",
257
+ description: "Pre-flight token/cost estimation with optimization recommendations.",
258
+ inputSchema: contextBudgetSchema,
259
+ execute: executeContextBudget,
260
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Context Budget Tool Tests
3
+ */
4
+ export {};
5
+ //# sourceMappingURL=context-budget.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-budget.test.d.ts","sourceRoot":"","sources":["../../src/tools/context-budget.test.ts"],"names":[],"mappings":"AAAA;;GAEG"}