illuma-agents 1.0.2

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 (437) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/common/enum.cjs +163 -0
  3. package/dist/cjs/common/enum.cjs.map +1 -0
  4. package/dist/cjs/events.cjs +143 -0
  5. package/dist/cjs/events.cjs.map +1 -0
  6. package/dist/cjs/graphs/Graph.cjs +581 -0
  7. package/dist/cjs/graphs/Graph.cjs.map +1 -0
  8. package/dist/cjs/instrumentation.cjs +21 -0
  9. package/dist/cjs/instrumentation.cjs.map +1 -0
  10. package/dist/cjs/llm/anthropic/index.cjs +292 -0
  11. package/dist/cjs/llm/anthropic/index.cjs.map +1 -0
  12. package/dist/cjs/llm/anthropic/types.cjs +50 -0
  13. package/dist/cjs/llm/anthropic/types.cjs.map +1 -0
  14. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +553 -0
  15. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -0
  16. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +218 -0
  17. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -0
  18. package/dist/cjs/llm/anthropic/utils/tools.cjs +29 -0
  19. package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -0
  20. package/dist/cjs/llm/fake.cjs +97 -0
  21. package/dist/cjs/llm/fake.cjs.map +1 -0
  22. package/dist/cjs/llm/google/index.cjs +147 -0
  23. package/dist/cjs/llm/google/index.cjs.map +1 -0
  24. package/dist/cjs/llm/google/utils/common.cjs +490 -0
  25. package/dist/cjs/llm/google/utils/common.cjs.map +1 -0
  26. package/dist/cjs/llm/ollama/index.cjs +70 -0
  27. package/dist/cjs/llm/ollama/index.cjs.map +1 -0
  28. package/dist/cjs/llm/ollama/utils.cjs +158 -0
  29. package/dist/cjs/llm/ollama/utils.cjs.map +1 -0
  30. package/dist/cjs/llm/openai/index.cjs +613 -0
  31. package/dist/cjs/llm/openai/index.cjs.map +1 -0
  32. package/dist/cjs/llm/openai/utils/index.cjs +677 -0
  33. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -0
  34. package/dist/cjs/llm/openrouter/index.cjs +29 -0
  35. package/dist/cjs/llm/openrouter/index.cjs.map +1 -0
  36. package/dist/cjs/llm/providers.cjs +47 -0
  37. package/dist/cjs/llm/providers.cjs.map +1 -0
  38. package/dist/cjs/llm/text.cjs +69 -0
  39. package/dist/cjs/llm/text.cjs.map +1 -0
  40. package/dist/cjs/llm/vertexai/index.cjs +330 -0
  41. package/dist/cjs/llm/vertexai/index.cjs.map +1 -0
  42. package/dist/cjs/main.cjs +127 -0
  43. package/dist/cjs/main.cjs.map +1 -0
  44. package/dist/cjs/messages/core.cjs +359 -0
  45. package/dist/cjs/messages/core.cjs.map +1 -0
  46. package/dist/cjs/messages/format.cjs +455 -0
  47. package/dist/cjs/messages/format.cjs.map +1 -0
  48. package/dist/cjs/messages/ids.cjs +23 -0
  49. package/dist/cjs/messages/ids.cjs.map +1 -0
  50. package/dist/cjs/messages/prune.cjs +398 -0
  51. package/dist/cjs/messages/prune.cjs.map +1 -0
  52. package/dist/cjs/run.cjs +264 -0
  53. package/dist/cjs/run.cjs.map +1 -0
  54. package/dist/cjs/splitStream.cjs +210 -0
  55. package/dist/cjs/splitStream.cjs.map +1 -0
  56. package/dist/cjs/stream.cjs +504 -0
  57. package/dist/cjs/stream.cjs.map +1 -0
  58. package/dist/cjs/tools/CodeExecutor.cjs +192 -0
  59. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -0
  60. package/dist/cjs/tools/ToolNode.cjs +125 -0
  61. package/dist/cjs/tools/ToolNode.cjs.map +1 -0
  62. package/dist/cjs/tools/handlers.cjs +250 -0
  63. package/dist/cjs/tools/handlers.cjs.map +1 -0
  64. package/dist/cjs/tools/search/anthropic.cjs +40 -0
  65. package/dist/cjs/tools/search/anthropic.cjs.map +1 -0
  66. package/dist/cjs/tools/search/content.cjs +140 -0
  67. package/dist/cjs/tools/search/content.cjs.map +1 -0
  68. package/dist/cjs/tools/search/firecrawl.cjs +179 -0
  69. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -0
  70. package/dist/cjs/tools/search/format.cjs +203 -0
  71. package/dist/cjs/tools/search/format.cjs.map +1 -0
  72. package/dist/cjs/tools/search/highlights.cjs +245 -0
  73. package/dist/cjs/tools/search/highlights.cjs.map +1 -0
  74. package/dist/cjs/tools/search/rerankers.cjs +174 -0
  75. package/dist/cjs/tools/search/rerankers.cjs.map +1 -0
  76. package/dist/cjs/tools/search/schema.cjs +70 -0
  77. package/dist/cjs/tools/search/schema.cjs.map +1 -0
  78. package/dist/cjs/tools/search/search.cjs +561 -0
  79. package/dist/cjs/tools/search/search.cjs.map +1 -0
  80. package/dist/cjs/tools/search/serper-scraper.cjs +132 -0
  81. package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -0
  82. package/dist/cjs/tools/search/tool.cjs +331 -0
  83. package/dist/cjs/tools/search/tool.cjs.map +1 -0
  84. package/dist/cjs/tools/search/utils.cjs +66 -0
  85. package/dist/cjs/tools/search/utils.cjs.map +1 -0
  86. package/dist/cjs/utils/graph.cjs +16 -0
  87. package/dist/cjs/utils/graph.cjs.map +1 -0
  88. package/dist/cjs/utils/llm.cjs +28 -0
  89. package/dist/cjs/utils/llm.cjs.map +1 -0
  90. package/dist/cjs/utils/misc.cjs +56 -0
  91. package/dist/cjs/utils/misc.cjs.map +1 -0
  92. package/dist/cjs/utils/run.cjs +69 -0
  93. package/dist/cjs/utils/run.cjs.map +1 -0
  94. package/dist/cjs/utils/title.cjs +111 -0
  95. package/dist/cjs/utils/title.cjs.map +1 -0
  96. package/dist/cjs/utils/tokens.cjs +65 -0
  97. package/dist/cjs/utils/tokens.cjs.map +1 -0
  98. package/dist/esm/common/enum.mjs +163 -0
  99. package/dist/esm/common/enum.mjs.map +1 -0
  100. package/dist/esm/events.mjs +135 -0
  101. package/dist/esm/events.mjs.map +1 -0
  102. package/dist/esm/graphs/Graph.mjs +578 -0
  103. package/dist/esm/graphs/Graph.mjs.map +1 -0
  104. package/dist/esm/instrumentation.mjs +19 -0
  105. package/dist/esm/instrumentation.mjs.map +1 -0
  106. package/dist/esm/llm/anthropic/index.mjs +290 -0
  107. package/dist/esm/llm/anthropic/index.mjs.map +1 -0
  108. package/dist/esm/llm/anthropic/types.mjs +48 -0
  109. package/dist/esm/llm/anthropic/types.mjs.map +1 -0
  110. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +550 -0
  111. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -0
  112. package/dist/esm/llm/anthropic/utils/message_outputs.mjs +216 -0
  113. package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -0
  114. package/dist/esm/llm/anthropic/utils/tools.mjs +27 -0
  115. package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -0
  116. package/dist/esm/llm/fake.mjs +94 -0
  117. package/dist/esm/llm/fake.mjs.map +1 -0
  118. package/dist/esm/llm/google/index.mjs +145 -0
  119. package/dist/esm/llm/google/index.mjs.map +1 -0
  120. package/dist/esm/llm/google/utils/common.mjs +484 -0
  121. package/dist/esm/llm/google/utils/common.mjs.map +1 -0
  122. package/dist/esm/llm/ollama/index.mjs +68 -0
  123. package/dist/esm/llm/ollama/index.mjs.map +1 -0
  124. package/dist/esm/llm/ollama/utils.mjs +155 -0
  125. package/dist/esm/llm/ollama/utils.mjs.map +1 -0
  126. package/dist/esm/llm/openai/index.mjs +604 -0
  127. package/dist/esm/llm/openai/index.mjs.map +1 -0
  128. package/dist/esm/llm/openai/utils/index.mjs +671 -0
  129. package/dist/esm/llm/openai/utils/index.mjs.map +1 -0
  130. package/dist/esm/llm/openrouter/index.mjs +27 -0
  131. package/dist/esm/llm/openrouter/index.mjs.map +1 -0
  132. package/dist/esm/llm/providers.mjs +43 -0
  133. package/dist/esm/llm/providers.mjs.map +1 -0
  134. package/dist/esm/llm/text.mjs +67 -0
  135. package/dist/esm/llm/text.mjs.map +1 -0
  136. package/dist/esm/llm/vertexai/index.mjs +328 -0
  137. package/dist/esm/llm/vertexai/index.mjs.map +1 -0
  138. package/dist/esm/main.mjs +20 -0
  139. package/dist/esm/main.mjs.map +1 -0
  140. package/dist/esm/messages/core.mjs +351 -0
  141. package/dist/esm/messages/core.mjs.map +1 -0
  142. package/dist/esm/messages/format.mjs +447 -0
  143. package/dist/esm/messages/format.mjs.map +1 -0
  144. package/dist/esm/messages/ids.mjs +21 -0
  145. package/dist/esm/messages/ids.mjs.map +1 -0
  146. package/dist/esm/messages/prune.mjs +393 -0
  147. package/dist/esm/messages/prune.mjs.map +1 -0
  148. package/dist/esm/run.mjs +261 -0
  149. package/dist/esm/run.mjs.map +1 -0
  150. package/dist/esm/splitStream.mjs +207 -0
  151. package/dist/esm/splitStream.mjs.map +1 -0
  152. package/dist/esm/stream.mjs +500 -0
  153. package/dist/esm/stream.mjs.map +1 -0
  154. package/dist/esm/tools/CodeExecutor.mjs +188 -0
  155. package/dist/esm/tools/CodeExecutor.mjs.map +1 -0
  156. package/dist/esm/tools/ToolNode.mjs +122 -0
  157. package/dist/esm/tools/ToolNode.mjs.map +1 -0
  158. package/dist/esm/tools/handlers.mjs +245 -0
  159. package/dist/esm/tools/handlers.mjs.map +1 -0
  160. package/dist/esm/tools/search/anthropic.mjs +37 -0
  161. package/dist/esm/tools/search/anthropic.mjs.map +1 -0
  162. package/dist/esm/tools/search/content.mjs +119 -0
  163. package/dist/esm/tools/search/content.mjs.map +1 -0
  164. package/dist/esm/tools/search/firecrawl.mjs +176 -0
  165. package/dist/esm/tools/search/firecrawl.mjs.map +1 -0
  166. package/dist/esm/tools/search/format.mjs +201 -0
  167. package/dist/esm/tools/search/format.mjs.map +1 -0
  168. package/dist/esm/tools/search/highlights.mjs +243 -0
  169. package/dist/esm/tools/search/highlights.mjs.map +1 -0
  170. package/dist/esm/tools/search/rerankers.mjs +168 -0
  171. package/dist/esm/tools/search/rerankers.mjs.map +1 -0
  172. package/dist/esm/tools/search/schema.mjs +61 -0
  173. package/dist/esm/tools/search/schema.mjs.map +1 -0
  174. package/dist/esm/tools/search/search.mjs +558 -0
  175. package/dist/esm/tools/search/search.mjs.map +1 -0
  176. package/dist/esm/tools/search/serper-scraper.mjs +129 -0
  177. package/dist/esm/tools/search/serper-scraper.mjs.map +1 -0
  178. package/dist/esm/tools/search/tool.mjs +329 -0
  179. package/dist/esm/tools/search/tool.mjs.map +1 -0
  180. package/dist/esm/tools/search/utils.mjs +61 -0
  181. package/dist/esm/tools/search/utils.mjs.map +1 -0
  182. package/dist/esm/utils/graph.mjs +13 -0
  183. package/dist/esm/utils/graph.mjs.map +1 -0
  184. package/dist/esm/utils/llm.mjs +25 -0
  185. package/dist/esm/utils/llm.mjs.map +1 -0
  186. package/dist/esm/utils/misc.mjs +53 -0
  187. package/dist/esm/utils/misc.mjs.map +1 -0
  188. package/dist/esm/utils/run.mjs +66 -0
  189. package/dist/esm/utils/run.mjs.map +1 -0
  190. package/dist/esm/utils/title.mjs +108 -0
  191. package/dist/esm/utils/title.mjs.map +1 -0
  192. package/dist/esm/utils/tokens.mjs +62 -0
  193. package/dist/esm/utils/tokens.mjs.map +1 -0
  194. package/dist/types/common/enum.d.ts +128 -0
  195. package/dist/types/common/index.d.ts +1 -0
  196. package/dist/types/events.d.ts +29 -0
  197. package/dist/types/graphs/Graph.d.ts +122 -0
  198. package/dist/types/graphs/index.d.ts +1 -0
  199. package/dist/types/index.d.ts +13 -0
  200. package/dist/types/instrumentation.d.ts +1 -0
  201. package/dist/types/llm/anthropic/index.d.ts +39 -0
  202. package/dist/types/llm/anthropic/types.d.ts +37 -0
  203. package/dist/types/llm/anthropic/utils/message_inputs.d.ts +14 -0
  204. package/dist/types/llm/anthropic/utils/message_outputs.d.ts +14 -0
  205. package/dist/types/llm/anthropic/utils/output_parsers.d.ts +22 -0
  206. package/dist/types/llm/anthropic/utils/tools.d.ts +3 -0
  207. package/dist/types/llm/fake.d.ts +31 -0
  208. package/dist/types/llm/google/index.d.ts +14 -0
  209. package/dist/types/llm/google/types.d.ts +32 -0
  210. package/dist/types/llm/google/utils/common.d.ts +19 -0
  211. package/dist/types/llm/google/utils/tools.d.ts +10 -0
  212. package/dist/types/llm/google/utils/zod_to_genai_parameters.d.ts +14 -0
  213. package/dist/types/llm/ollama/index.d.ts +8 -0
  214. package/dist/types/llm/ollama/utils.d.ts +7 -0
  215. package/dist/types/llm/openai/index.d.ts +103 -0
  216. package/dist/types/llm/openai/types.d.ts +10 -0
  217. package/dist/types/llm/openai/utils/index.d.ts +20 -0
  218. package/dist/types/llm/openrouter/index.d.ts +12 -0
  219. package/dist/types/llm/providers.d.ts +5 -0
  220. package/dist/types/llm/text.d.ts +21 -0
  221. package/dist/types/llm/vertexai/index.d.ts +293 -0
  222. package/dist/types/messages/core.d.ts +14 -0
  223. package/dist/types/messages/format.d.ts +113 -0
  224. package/dist/types/messages/ids.d.ts +3 -0
  225. package/dist/types/messages/index.d.ts +4 -0
  226. package/dist/types/messages/prune.d.ts +51 -0
  227. package/dist/types/mockStream.d.ts +32 -0
  228. package/dist/types/prompts/collab.d.ts +1 -0
  229. package/dist/types/prompts/index.d.ts +2 -0
  230. package/dist/types/prompts/taskmanager.d.ts +41 -0
  231. package/dist/types/run.d.ts +30 -0
  232. package/dist/types/scripts/abort.d.ts +1 -0
  233. package/dist/types/scripts/ant_web_search.d.ts +1 -0
  234. package/dist/types/scripts/args.d.ts +7 -0
  235. package/dist/types/scripts/caching.d.ts +1 -0
  236. package/dist/types/scripts/cli.d.ts +1 -0
  237. package/dist/types/scripts/cli2.d.ts +1 -0
  238. package/dist/types/scripts/cli3.d.ts +1 -0
  239. package/dist/types/scripts/cli4.d.ts +1 -0
  240. package/dist/types/scripts/cli5.d.ts +1 -0
  241. package/dist/types/scripts/code_exec.d.ts +1 -0
  242. package/dist/types/scripts/code_exec_files.d.ts +1 -0
  243. package/dist/types/scripts/code_exec_simple.d.ts +1 -0
  244. package/dist/types/scripts/content.d.ts +1 -0
  245. package/dist/types/scripts/empty_input.d.ts +1 -0
  246. package/dist/types/scripts/image.d.ts +1 -0
  247. package/dist/types/scripts/memory.d.ts +1 -0
  248. package/dist/types/scripts/search.d.ts +1 -0
  249. package/dist/types/scripts/simple.d.ts +1 -0
  250. package/dist/types/scripts/stream.d.ts +1 -0
  251. package/dist/types/scripts/thinking.d.ts +1 -0
  252. package/dist/types/scripts/tools.d.ts +1 -0
  253. package/dist/types/specs/spec.utils.d.ts +1 -0
  254. package/dist/types/splitStream.d.ts +37 -0
  255. package/dist/types/stream.d.ts +14 -0
  256. package/dist/types/tools/CodeExecutor.d.ts +23 -0
  257. package/dist/types/tools/ToolNode.d.ts +22 -0
  258. package/dist/types/tools/example.d.ts +78 -0
  259. package/dist/types/tools/handlers.d.ts +19 -0
  260. package/dist/types/tools/search/anthropic.d.ts +16 -0
  261. package/dist/types/tools/search/content.d.ts +4 -0
  262. package/dist/types/tools/search/firecrawl.d.ts +54 -0
  263. package/dist/types/tools/search/format.d.ts +5 -0
  264. package/dist/types/tools/search/highlights.d.ts +13 -0
  265. package/dist/types/tools/search/index.d.ts +2 -0
  266. package/dist/types/tools/search/rerankers.d.ts +38 -0
  267. package/dist/types/tools/search/schema.d.ts +16 -0
  268. package/dist/types/tools/search/search.d.ts +8 -0
  269. package/dist/types/tools/search/serper-scraper.d.ts +59 -0
  270. package/dist/types/tools/search/test.d.ts +1 -0
  271. package/dist/types/tools/search/tool.d.ts +54 -0
  272. package/dist/types/tools/search/types.d.ts +591 -0
  273. package/dist/types/tools/search/utils.d.ts +10 -0
  274. package/dist/types/types/graph.d.ts +138 -0
  275. package/dist/types/types/index.d.ts +5 -0
  276. package/dist/types/types/llm.d.ts +102 -0
  277. package/dist/types/types/run.d.ts +74 -0
  278. package/dist/types/types/stream.d.ts +293 -0
  279. package/dist/types/types/tools.d.ts +61 -0
  280. package/dist/types/utils/graph.d.ts +2 -0
  281. package/dist/types/utils/index.d.ts +5 -0
  282. package/dist/types/utils/llm.d.ts +3 -0
  283. package/dist/types/utils/llmConfig.d.ts +3 -0
  284. package/dist/types/utils/logging.d.ts +1 -0
  285. package/dist/types/utils/misc.d.ts +7 -0
  286. package/dist/types/utils/run.d.ts +27 -0
  287. package/dist/types/utils/title.d.ts +4 -0
  288. package/dist/types/utils/tokens.d.ts +3 -0
  289. package/package.json +145 -0
  290. package/src/common/enum.ts +176 -0
  291. package/src/common/index.ts +2 -0
  292. package/src/events.ts +191 -0
  293. package/src/graphs/Graph.ts +846 -0
  294. package/src/graphs/index.ts +1 -0
  295. package/src/index.ts +24 -0
  296. package/src/instrumentation.ts +22 -0
  297. package/src/llm/anthropic/Jacob_Lee_Resume_2023.pdf +0 -0
  298. package/src/llm/anthropic/index.ts +413 -0
  299. package/src/llm/anthropic/llm.spec.ts +1442 -0
  300. package/src/llm/anthropic/types.ts +140 -0
  301. package/src/llm/anthropic/utils/message_inputs.ts +660 -0
  302. package/src/llm/anthropic/utils/message_outputs.ts +289 -0
  303. package/src/llm/anthropic/utils/output_parsers.ts +133 -0
  304. package/src/llm/anthropic/utils/tools.ts +29 -0
  305. package/src/llm/fake.ts +133 -0
  306. package/src/llm/google/index.ts +222 -0
  307. package/src/llm/google/types.ts +43 -0
  308. package/src/llm/google/utils/common.ts +660 -0
  309. package/src/llm/google/utils/tools.ts +160 -0
  310. package/src/llm/google/utils/zod_to_genai_parameters.ts +88 -0
  311. package/src/llm/ollama/index.ts +92 -0
  312. package/src/llm/ollama/utils.ts +193 -0
  313. package/src/llm/openai/index.ts +853 -0
  314. package/src/llm/openai/types.ts +24 -0
  315. package/src/llm/openai/utils/index.ts +918 -0
  316. package/src/llm/openai/utils/isReasoningModel.test.ts +90 -0
  317. package/src/llm/openrouter/index.ts +60 -0
  318. package/src/llm/providers.ts +57 -0
  319. package/src/llm/text.ts +94 -0
  320. package/src/llm/vertexai/index.ts +360 -0
  321. package/src/messages/core.ts +463 -0
  322. package/src/messages/format.ts +625 -0
  323. package/src/messages/formatAgentMessages.test.ts +917 -0
  324. package/src/messages/formatAgentMessages.tools.test.ts +400 -0
  325. package/src/messages/formatMessage.test.ts +693 -0
  326. package/src/messages/ids.ts +26 -0
  327. package/src/messages/index.ts +4 -0
  328. package/src/messages/prune.ts +567 -0
  329. package/src/messages/shiftIndexTokenCountMap.test.ts +81 -0
  330. package/src/mockStream.ts +99 -0
  331. package/src/prompts/collab.ts +6 -0
  332. package/src/prompts/index.ts +2 -0
  333. package/src/prompts/taskmanager.ts +61 -0
  334. package/src/proto/CollabGraph.ts +269 -0
  335. package/src/proto/TaskManager.ts +243 -0
  336. package/src/proto/collab.ts +200 -0
  337. package/src/proto/collab_design.ts +184 -0
  338. package/src/proto/collab_design_v2.ts +224 -0
  339. package/src/proto/collab_design_v3.ts +255 -0
  340. package/src/proto/collab_design_v4.ts +220 -0
  341. package/src/proto/collab_design_v5.ts +251 -0
  342. package/src/proto/collab_graph.ts +181 -0
  343. package/src/proto/collab_original.ts +123 -0
  344. package/src/proto/example.ts +93 -0
  345. package/src/proto/example_new.ts +68 -0
  346. package/src/proto/example_old.ts +201 -0
  347. package/src/proto/example_test.ts +152 -0
  348. package/src/proto/example_test_anthropic.ts +100 -0
  349. package/src/proto/log_stream.ts +202 -0
  350. package/src/proto/main_collab_community_event.ts +133 -0
  351. package/src/proto/main_collab_design_v2.ts +96 -0
  352. package/src/proto/main_collab_design_v4.ts +100 -0
  353. package/src/proto/main_collab_design_v5.ts +135 -0
  354. package/src/proto/main_collab_global_analysis.ts +122 -0
  355. package/src/proto/main_collab_hackathon_event.ts +153 -0
  356. package/src/proto/main_collab_space_mission.ts +153 -0
  357. package/src/proto/main_philosophy.ts +210 -0
  358. package/src/proto/original_script.ts +126 -0
  359. package/src/proto/standard.ts +100 -0
  360. package/src/proto/stream.ts +56 -0
  361. package/src/proto/tasks.ts +118 -0
  362. package/src/proto/tools/global_analysis_tools.ts +86 -0
  363. package/src/proto/tools/space_mission_tools.ts +60 -0
  364. package/src/proto/vertexai.ts +54 -0
  365. package/src/run.ts +381 -0
  366. package/src/scripts/abort.ts +138 -0
  367. package/src/scripts/ant_web_search.ts +158 -0
  368. package/src/scripts/args.ts +48 -0
  369. package/src/scripts/caching.ts +124 -0
  370. package/src/scripts/cli.ts +167 -0
  371. package/src/scripts/cli2.ts +125 -0
  372. package/src/scripts/cli3.ts +178 -0
  373. package/src/scripts/cli4.ts +184 -0
  374. package/src/scripts/cli5.ts +184 -0
  375. package/src/scripts/code_exec.ts +214 -0
  376. package/src/scripts/code_exec_files.ts +193 -0
  377. package/src/scripts/code_exec_simple.ts +129 -0
  378. package/src/scripts/content.ts +120 -0
  379. package/src/scripts/empty_input.ts +137 -0
  380. package/src/scripts/image.ts +178 -0
  381. package/src/scripts/memory.ts +97 -0
  382. package/src/scripts/search.ts +150 -0
  383. package/src/scripts/simple.ts +225 -0
  384. package/src/scripts/stream.ts +122 -0
  385. package/src/scripts/thinking.ts +150 -0
  386. package/src/scripts/tools.ts +155 -0
  387. package/src/specs/anthropic.simple.test.ts +317 -0
  388. package/src/specs/azure.simple.test.ts +316 -0
  389. package/src/specs/openai.simple.test.ts +316 -0
  390. package/src/specs/prune.test.ts +763 -0
  391. package/src/specs/reasoning.test.ts +165 -0
  392. package/src/specs/spec.utils.ts +3 -0
  393. package/src/specs/thinking-prune.test.ts +703 -0
  394. package/src/specs/token-distribution-edge-case.test.ts +316 -0
  395. package/src/specs/tool-error.test.ts +193 -0
  396. package/src/splitStream.test.ts +691 -0
  397. package/src/splitStream.ts +234 -0
  398. package/src/stream.test.ts +94 -0
  399. package/src/stream.ts +651 -0
  400. package/src/tools/CodeExecutor.ts +220 -0
  401. package/src/tools/ToolNode.ts +170 -0
  402. package/src/tools/example.ts +129 -0
  403. package/src/tools/handlers.ts +336 -0
  404. package/src/tools/search/anthropic.ts +51 -0
  405. package/src/tools/search/content.test.ts +173 -0
  406. package/src/tools/search/content.ts +147 -0
  407. package/src/tools/search/firecrawl.ts +210 -0
  408. package/src/tools/search/format.ts +250 -0
  409. package/src/tools/search/highlights.ts +320 -0
  410. package/src/tools/search/index.ts +2 -0
  411. package/src/tools/search/jina-reranker.test.ts +126 -0
  412. package/src/tools/search/output.md +2775 -0
  413. package/src/tools/search/rerankers.ts +242 -0
  414. package/src/tools/search/schema.ts +63 -0
  415. package/src/tools/search/search.ts +759 -0
  416. package/src/tools/search/serper-scraper.ts +155 -0
  417. package/src/tools/search/test.html +884 -0
  418. package/src/tools/search/test.md +643 -0
  419. package/src/tools/search/test.ts +159 -0
  420. package/src/tools/search/tool.ts +471 -0
  421. package/src/tools/search/types.ts +687 -0
  422. package/src/tools/search/utils.ts +79 -0
  423. package/src/types/graph.ts +185 -0
  424. package/src/types/index.ts +6 -0
  425. package/src/types/llm.ts +140 -0
  426. package/src/types/run.ts +89 -0
  427. package/src/types/stream.ts +400 -0
  428. package/src/types/tools.ts +80 -0
  429. package/src/utils/graph.ts +11 -0
  430. package/src/utils/index.ts +5 -0
  431. package/src/utils/llm.ts +27 -0
  432. package/src/utils/llmConfig.ts +183 -0
  433. package/src/utils/logging.ts +48 -0
  434. package/src/utils/misc.ts +57 -0
  435. package/src/utils/run.ts +101 -0
  436. package/src/utils/title.ts +165 -0
  437. package/src/utils/tokens.ts +70 -0
@@ -0,0 +1,703 @@
1
+ // src/specs/thinking-prune.test.ts
2
+ import { HumanMessage, AIMessage, SystemMessage, BaseMessage, ToolMessage } from '@langchain/core/messages';
3
+ import type * as t from '@/types';
4
+ import { createPruneMessages } from '@/messages/prune';
5
+
6
+ // Create a simple token counter for testing
7
+ const createTestTokenCounter = (): t.TokenCounter => {
8
+ return (message: BaseMessage): number => {
9
+ // Use type assertion to help TypeScript understand the type
10
+ const content = message.content as string | Array<t.MessageContentComplex | string> | undefined;
11
+
12
+ // Handle string content
13
+ if (typeof content === 'string') {
14
+ return content.length;
15
+ }
16
+
17
+ // Handle array content
18
+ if (Array.isArray(content)) {
19
+ let totalLength = 0;
20
+
21
+ for (const item of content) {
22
+ if (typeof item === 'string') {
23
+ totalLength += item.length;
24
+ } else if (typeof item === 'object') {
25
+ if (item.type === 'thinking' && typeof item.thinking === 'string') {
26
+ totalLength += item.thinking.length;
27
+ } else if ('text' in item && typeof item.text === 'string') {
28
+ totalLength += item.text.length;
29
+ } else if ('input' in item && typeof item.input === 'string') {
30
+ totalLength += item.input.length;
31
+ }
32
+ }
33
+ }
34
+
35
+ return totalLength;
36
+ }
37
+
38
+ // Default case - if content is null, undefined, or any other type
39
+ return 0;
40
+ };
41
+ };
42
+
43
+ describe('Prune Messages with Thinking Mode Tests', () => {
44
+ jest.setTimeout(30000);
45
+
46
+ it('should preserve thinking blocks when pruning with thinking mode enabled', () => {
47
+ // Create a token counter
48
+ const tokenCounter = createTestTokenCounter();
49
+
50
+ // Create messages based on the example provided
51
+ const userMessage = new HumanMessage({
52
+ content: [
53
+ {
54
+ type: 'text',
55
+ text: '/home/danny/Illuma/gistfile1.txt\n\nread it 200 lines at a time\n\nthere are 5000 lines\n\ndo not stop until done',
56
+ },
57
+ ],
58
+ });
59
+
60
+ const assistantMessageWithThinking = new AIMessage({
61
+ content: [
62
+ {
63
+ type: 'thinking',
64
+ thinking: 'The user is asking me to read a file located at `/home/danny/Illuma/gistfile1.txt` in chunks of 200 lines at a time, mentioning that the file has 5000 lines total. They want me to continue reading through the entire file without stopping.\n\nI\'ll need to use the text editor tool to view the file in chunks of 200 lines each. Since the file has 5000 lines, I\'ll need to view it in 25 chunks (5000 ÷ 200 = 25).\n\nI\'ll need to make multiple calls to the text editor with the `view` command, specifying different line ranges for each call.\n\nLet me plan out the approach:\n1. Start with lines 1-200\n2. Then 201-400\n3. Then 401-600\n4. And so on until I reach 4801-5000\n\nFor each call, I\'ll use the `view` command with the specific line range in the `view_range` parameter. I\'ll continue until I\'ve shown all 5000 lines as requested.',
65
+ },
66
+ {
67
+ type: 'text',
68
+ text: 'I\'ll read the file for you in chunks of 200 lines at a time until all 5000 lines are displayed.\n\nLet me start with the first 200 lines:',
69
+ },
70
+ {
71
+ type: 'tool_use',
72
+ id: 'toolu_01YApWuFsEQCuBFDgYXmcmeZ',
73
+ name: 'text_editor_mcp_textEditor',
74
+ input: '{"command": "view", "path": "/home/danny/Illuma/gistfile1.txt", "description": "Viewing lines 1-200 of the file", "view_range": [1,200]}',
75
+ },
76
+ ],
77
+ });
78
+
79
+ const toolResponseMessage1 = new ToolMessage({
80
+ content: [
81
+ {
82
+ type: 'text',
83
+ text: '{"success":true,"message":"File content (truncated):"',
84
+ },
85
+ ],
86
+ tool_call_id: 'toolu_01YApWuFsEQCuBFDgYXmcmeZ',
87
+ name: 'text_editor_mcp_textEditor',
88
+ });
89
+
90
+ const assistantMessage2 = new AIMessage({
91
+ content: [
92
+ {
93
+ type: 'text',
94
+ text: 'It looks like the file content is clipped in the response. I\'ll continue with the next chunks:',
95
+ },
96
+ {
97
+ type: 'tool_use',
98
+ id: 'toolu_01VnyMQ4CvEd6zLDxxtTd6d4',
99
+ name: 'text_editor_mcp_textEditor',
100
+ input: '{"command": "view", "path": "/home/danny/Illuma/gistfile1.txt", "description": "Viewing lines 201-400 of the file", "view_range": [201,400]}',
101
+ },
102
+ ],
103
+ });
104
+
105
+ const toolResponseMessage2 = new ToolMessage({
106
+ content: [
107
+ {
108
+ type: 'text',
109
+ text: '{"success":true,"message":"File content (truncated):"',
110
+ },
111
+ ],
112
+ tool_call_id: 'toolu_01VnyMQ4CvEd6zLDxxtTd6d4',
113
+ name: 'text_editor_mcp_textEditor',
114
+ });
115
+
116
+ const assistantMessage3 = new AIMessage({
117
+ content: [
118
+ {
119
+ type: 'text',
120
+ text: 'Continuing with the next chunk:',
121
+ },
122
+ {
123
+ type: 'tool_use',
124
+ id: 'toolu_01TZKs4nnBc58BYXKz1Mw4fp',
125
+ name: 'text_editor_mcp_textEditor',
126
+ input: '{"command": "view", "path": "/home/danny/Illuma/gistfile1.txt", "description": "Viewing lines 401-600 of the file", "view_range": [401,600]}',
127
+ },
128
+ ],
129
+ });
130
+
131
+ const toolResponseMessage3 = new ToolMessage({
132
+ content: [
133
+ {
134
+ type: 'text',
135
+ text: '{"success":true,"message":"File content (truncated):"',
136
+ },
137
+ ],
138
+ tool_call_id: 'toolu_01TZKs4nnBc58BYXKz1Mw4fp',
139
+ name: 'text_editor_mcp_textEditor',
140
+ });
141
+
142
+ const assistantMessage4 = new AIMessage({
143
+ content: [
144
+ {
145
+ type: 'text',
146
+ text: 'Let\'s continue with the next chunk:',
147
+ },
148
+ {
149
+ type: 'tool_use',
150
+ id: 'toolu_01TZgBacNxjx1QNUpJg9hca5',
151
+ name: 'text_editor_mcp_textEditor',
152
+ input: '{"command": "view", "path": "/home/danny/Illuma/gistfile1.txt", "description": "Viewing lines 601-800 of the file", "view_range": [601,800]}',
153
+ },
154
+ ],
155
+ });
156
+
157
+ const toolResponseMessage4 = new ToolMessage({
158
+ content: [
159
+ {
160
+ type: 'text',
161
+ text: '{"success":true,"message":"File content (truncated):"',
162
+ },
163
+ ],
164
+ tool_call_id: 'toolu_01TZgBacNxjx1QNUpJg9hca5',
165
+ name: 'text_editor_mcp_textEditor',
166
+ });
167
+
168
+ const messages = [
169
+ userMessage,
170
+ assistantMessageWithThinking,
171
+ toolResponseMessage1,
172
+ assistantMessage2,
173
+ toolResponseMessage2,
174
+ assistantMessage3,
175
+ toolResponseMessage3,
176
+ assistantMessage4,
177
+ toolResponseMessage4,
178
+ ];
179
+
180
+ // Create indexTokenCountMap based on the example provided
181
+ const indexTokenCountMap = {
182
+ '0': 617, // userMessage
183
+ '1': 52, // assistantMessageWithThinking
184
+ '2': 4995, // toolResponseMessage1
185
+ '3': 307, // assistantMessage2
186
+ '4': 9359, // toolResponseMessage2
187
+ '5': 178, // assistantMessage3
188
+ '6': 5463, // toolResponseMessage3
189
+ '7': 125, // assistantMessage4
190
+ '8': 4264, // toolResponseMessage4
191
+ };
192
+
193
+ // Create pruneMessages function with thinking mode enabled
194
+ const pruneMessages = createPruneMessages({
195
+ maxTokens: 19800,
196
+ startIndex: 0,
197
+ tokenCounter,
198
+ indexTokenCountMap: { ...indexTokenCountMap },
199
+ thinkingEnabled: true,
200
+ });
201
+
202
+ // Prune messages
203
+ const result = pruneMessages({
204
+ messages,
205
+ usageMetadata: {
206
+ input_tokens: 25254,
207
+ output_tokens: 106,
208
+ total_tokens: 25360,
209
+ input_token_details: {
210
+ cache_read: 0,
211
+ cache_creation: 0,
212
+ },
213
+ },
214
+ startType: 'human',
215
+ });
216
+
217
+ // Verify that the first assistant message in the pruned context has a thinking block
218
+ expect(result.context.length).toBeGreaterThan(0);
219
+
220
+ // Find the first assistant message in the pruned context
221
+ const firstAssistantIndex = result.context.findIndex(msg => msg.getType() === 'ai');
222
+ expect(firstAssistantIndex).toBe(0);
223
+
224
+ const firstAssistantMsg = result.context[firstAssistantIndex];
225
+ expect(Array.isArray(firstAssistantMsg.content)).toBe(true);
226
+
227
+ // Verify that the first assistant message has a thinking block
228
+ const hasThinkingBlock = (firstAssistantMsg.content as t.MessageContentComplex[]).some((item: t.MessageContentComplex) =>
229
+ typeof item === 'object' && item.type === 'thinking');
230
+ expect(hasThinkingBlock).toBe(true);
231
+
232
+ // Verify that the thinking block is from the original assistant message
233
+ const thinkingBlock = (firstAssistantMsg.content as t.MessageContentComplex[]).find((item: t.MessageContentComplex) =>
234
+ typeof item === 'object' && item.type === 'thinking');
235
+ expect(thinkingBlock).toBeDefined();
236
+ expect((thinkingBlock as t.ThinkingContentText).thinking).toContain('The user is asking me to read a file');
237
+ });
238
+
239
+ it('should handle token recalculation when inserting thinking blocks', () => {
240
+ // Create a token counter
241
+ const tokenCounter = createTestTokenCounter();
242
+
243
+ // Create a message with thinking block
244
+ const assistantMessageWithThinking = new AIMessage({
245
+ content: [
246
+ {
247
+ type: 'thinking',
248
+ thinking: 'This is a thinking block',
249
+ },
250
+ {
251
+ type: 'text',
252
+ text: 'Response with thinking',
253
+ },
254
+ ],
255
+ });
256
+
257
+ // Create a message without thinking block
258
+ const assistantMessageWithoutThinking = new AIMessage({
259
+ content: [
260
+ {
261
+ type: 'text',
262
+ text: 'Response without thinking',
263
+ },
264
+ ],
265
+ });
266
+
267
+ const messages = [
268
+ new SystemMessage('System instruction'),
269
+ new HumanMessage('Hello'),
270
+ assistantMessageWithThinking,
271
+ new HumanMessage('Next message'),
272
+ assistantMessageWithoutThinking,
273
+ ];
274
+
275
+ // Calculate token counts for each message
276
+ const indexTokenCountMap: Record<string, number> = {};
277
+ for (let i = 0; i < messages.length; i++) {
278
+ indexTokenCountMap[i] = tokenCounter(messages[i]);
279
+ }
280
+
281
+ // Create pruneMessages function with thinking mode enabled
282
+ const pruneMessages = createPruneMessages({
283
+ maxTokens: 50, // Set a low token limit to force pruning
284
+ startIndex: 0,
285
+ tokenCounter,
286
+ indexTokenCountMap: { ...indexTokenCountMap },
287
+ thinkingEnabled: true,
288
+ });
289
+
290
+ // Prune messages
291
+ const result = pruneMessages({ messages });
292
+
293
+ // Verify that the pruned context has fewer messages than the original
294
+ expect(result.context.length).toBeLessThan(messages.length);
295
+ });
296
+
297
+ it('should not modify messages when under token limit', () => {
298
+ // Create a token counter
299
+ const tokenCounter = createTestTokenCounter();
300
+
301
+ // Create a message with thinking block
302
+ const assistantMessageWithThinking = new AIMessage({
303
+ content: [
304
+ {
305
+ type: 'thinking',
306
+ thinking: 'This is a thinking block',
307
+ },
308
+ {
309
+ type: 'text',
310
+ text: 'Response with thinking',
311
+ },
312
+ ],
313
+ });
314
+
315
+ const messages = [
316
+ new SystemMessage('System instruction'),
317
+ new HumanMessage('Hello'),
318
+ assistantMessageWithThinking,
319
+ ];
320
+
321
+ // Calculate token counts for each message
322
+ const indexTokenCountMap: Record<string, number> = {};
323
+ for (let i = 0; i < messages.length; i++) {
324
+ indexTokenCountMap[i] = tokenCounter(messages[i]);
325
+ }
326
+
327
+ // Create pruneMessages function with thinking mode enabled
328
+ const pruneMessages = createPruneMessages({
329
+ maxTokens: 1000, // Set a high token limit to avoid pruning
330
+ startIndex: 0,
331
+ tokenCounter,
332
+ indexTokenCountMap: { ...indexTokenCountMap },
333
+ thinkingEnabled: true,
334
+ });
335
+
336
+ // Prune messages
337
+ const result = pruneMessages({ messages });
338
+
339
+ // Verify that all messages are preserved
340
+ expect(result.context.length).toBe(messages.length);
341
+ expect(result.context).toEqual(messages);
342
+ });
343
+
344
+ it('should handle the case when no thinking blocks are found', () => {
345
+ // Create a token counter
346
+ const tokenCounter = createTestTokenCounter();
347
+
348
+ // Create messages without thinking blocks
349
+ const messages = [
350
+ new SystemMessage('System instruction'),
351
+ new HumanMessage('Hello'),
352
+ new AIMessage('Response without thinking'),
353
+ new HumanMessage('Next message'),
354
+ new AIMessage('Another response without thinking'),
355
+ ];
356
+
357
+ // Calculate token counts for each message
358
+ const indexTokenCountMap: Record<string, number> = {};
359
+ for (let i = 0; i < messages.length; i++) {
360
+ indexTokenCountMap[i] = tokenCounter(messages[i]);
361
+ }
362
+
363
+ // Create pruneMessages function with thinking mode enabled
364
+ const pruneMessages = createPruneMessages({
365
+ maxTokens: 50, // Set a low token limit to force pruning
366
+ startIndex: 0,
367
+ tokenCounter,
368
+ indexTokenCountMap: { ...indexTokenCountMap },
369
+ thinkingEnabled: true,
370
+ });
371
+
372
+ // Prune messages
373
+ const result = pruneMessages({ messages });
374
+
375
+ // Verify that the pruned context has fewer messages than the original
376
+ expect(result.context.length).toBeLessThan(messages.length);
377
+
378
+ // The function should not throw an error even though no thinking blocks are found
379
+ expect(() => pruneMessages({ messages })).not.toThrow();
380
+ });
381
+
382
+ it('should preserve AI <--> tool message correspondences when pruning', () => {
383
+ // Create a token counter
384
+ const tokenCounter = createTestTokenCounter();
385
+
386
+ // Create messages with tool calls
387
+ const assistantMessageWithToolCall = new AIMessage({
388
+ content: [
389
+ {
390
+ type: 'text',
391
+ text: 'Let me check that file:',
392
+ },
393
+ {
394
+ type: 'tool_use',
395
+ id: 'tool123',
396
+ name: 'text_editor_mcp_textEditor',
397
+ input: '{"command": "view", "path": "/path/to/file.txt"}',
398
+ },
399
+ ],
400
+ });
401
+
402
+ const toolResponseMessage = new ToolMessage({
403
+ content: [
404
+ {
405
+ type: 'text',
406
+ text: '{"success":true,"message":"File content"}',
407
+ },
408
+ ],
409
+ tool_call_id: 'tool123',
410
+ name: 'text_editor_mcp_textEditor',
411
+ });
412
+
413
+ const assistantMessageWithThinking = new AIMessage({
414
+ content: [
415
+ {
416
+ type: 'thinking',
417
+ thinking: 'This is a thinking block',
418
+ },
419
+ {
420
+ type: 'text',
421
+ text: 'Response with thinking',
422
+ },
423
+ ],
424
+ });
425
+
426
+ const messages = [
427
+ new SystemMessage('System instruction'),
428
+ new HumanMessage('Hello'),
429
+ assistantMessageWithToolCall,
430
+ toolResponseMessage,
431
+ new HumanMessage('Next message'),
432
+ assistantMessageWithThinking,
433
+ ];
434
+
435
+ // Calculate token counts for each message
436
+ const indexTokenCountMap: Record<string, number> = {};
437
+ for (let i = 0; i < messages.length; i++) {
438
+ indexTokenCountMap[i] = tokenCounter(messages[i]);
439
+ }
440
+
441
+ // Create pruneMessages function with thinking mode enabled and a low token limit
442
+ const pruneMessages = createPruneMessages({
443
+ maxTokens: 100, // Set a low token limit to force pruning
444
+ startIndex: 0,
445
+ tokenCounter,
446
+ indexTokenCountMap: { ...indexTokenCountMap },
447
+ thinkingEnabled: true,
448
+ });
449
+
450
+ // Prune messages
451
+ const result = pruneMessages({ messages });
452
+
453
+ // Find assistant message with tool call and its corresponding tool message in the pruned context
454
+ const assistantIndex = result.context.findIndex(msg =>
455
+ msg.getType() === 'ai' &&
456
+ Array.isArray(msg.content) &&
457
+ msg.content.some(item => typeof item === 'object' && item.type === 'tool_use' && item.id === 'tool123')
458
+ );
459
+
460
+ // If the assistant message with tool call is in the context, its corresponding tool message should also be there
461
+ if (assistantIndex !== -1) {
462
+ const toolIndex = result.context.findIndex(msg =>
463
+ msg.getType() === 'tool' &&
464
+ 'tool_call_id' in msg &&
465
+ msg.tool_call_id === 'tool123'
466
+ );
467
+
468
+ expect(toolIndex).not.toBe(-1);
469
+ }
470
+
471
+ // If the tool message is in the context, its corresponding assistant message should also be there
472
+ const toolIndex = result.context.findIndex(msg =>
473
+ msg.getType() === 'tool' &&
474
+ 'tool_call_id' in msg &&
475
+ msg.tool_call_id === 'tool123'
476
+ );
477
+
478
+ if (toolIndex !== -1) {
479
+ const assistantWithToolIndex = result.context.findIndex(msg =>
480
+ msg.getType() === 'ai' &&
481
+ Array.isArray(msg.content) &&
482
+ msg.content.some(item => typeof item === 'object' && item.type === 'tool_use' && item.id === 'tool123')
483
+ );
484
+
485
+ expect(assistantWithToolIndex).not.toBe(-1);
486
+ }
487
+ });
488
+
489
+ it('should ensure an assistant message with thinking appears in the latest sequence of assistant/tool messages', () => {
490
+ // Create a token counter
491
+ const tokenCounter = createTestTokenCounter();
492
+
493
+ // Create messages with the latest message being an assistant message with thinking
494
+ const assistantMessageWithThinking = new AIMessage({
495
+ content: [
496
+ {
497
+ type: 'thinking',
498
+ thinking: 'This is a thinking block',
499
+ },
500
+ {
501
+ type: 'text',
502
+ text: 'Response with thinking',
503
+ },
504
+ ],
505
+ });
506
+
507
+ // Create an assistant message with tool use
508
+ const assistantMessageWithToolUse = new AIMessage({
509
+ content: [
510
+ {
511
+ type: 'text',
512
+ text: 'Let me check that file:',
513
+ },
514
+ {
515
+ type: 'tool_use',
516
+ id: 'tool123',
517
+ name: 'text_editor_mcp_textEditor',
518
+ input: '{"command": "view", "path": "/path/to/file.txt"}',
519
+ },
520
+ ],
521
+ });
522
+
523
+ // Create a tool response message
524
+ const toolResponseMessage = new ToolMessage({
525
+ content: [
526
+ {
527
+ type: 'text',
528
+ text: '{"success":true,"message":"File content"}',
529
+ },
530
+ ],
531
+ tool_call_id: 'tool123',
532
+ name: 'text_editor_mcp_textEditor',
533
+ });
534
+
535
+ // Test case without system message
536
+ const messagesWithoutSystem = [
537
+ new HumanMessage('Hello'),
538
+ assistantMessageWithToolUse,
539
+ toolResponseMessage,
540
+ new HumanMessage('Next message'),
541
+ assistantMessageWithThinking, // Latest message is an assistant message with thinking
542
+ ];
543
+
544
+ // Calculate token counts for each message
545
+ const indexTokenCountMapWithoutSystem: Record<string, number> = {};
546
+ for (let i = 0; i < messagesWithoutSystem.length; i++) {
547
+ indexTokenCountMapWithoutSystem[i] = tokenCounter(messagesWithoutSystem[i]);
548
+ }
549
+
550
+ // Create pruneMessages function with thinking mode enabled
551
+ const pruneMessagesWithoutSystem = createPruneMessages({
552
+ maxTokens: 100, // Set a token limit to force some pruning
553
+ startIndex: 0,
554
+ tokenCounter,
555
+ indexTokenCountMap: { ...indexTokenCountMapWithoutSystem },
556
+ thinkingEnabled: true,
557
+ });
558
+
559
+ // Prune messages
560
+ const resultWithoutSystem = pruneMessagesWithoutSystem({ messages: messagesWithoutSystem });
561
+
562
+ // Verify that the pruned context contains at least one message
563
+ expect(resultWithoutSystem.context.length).toBeGreaterThan(0);
564
+
565
+ // Find all assistant messages in the latest sequence (after the last human message)
566
+ const lastHumanIndex = resultWithoutSystem.context.map(msg => msg.getType()).lastIndexOf('human');
567
+ const assistantMessagesAfterLastHuman = resultWithoutSystem.context.slice(lastHumanIndex + 1)
568
+ .filter(msg => msg.getType() === 'ai');
569
+
570
+ // Verify that at least one assistant message exists in the latest sequence
571
+ expect(assistantMessagesAfterLastHuman.length).toBeGreaterThan(0);
572
+
573
+ // Verify that at least one of these assistant messages has a thinking block
574
+ const hasThinkingBlock = assistantMessagesAfterLastHuman.some(msg => {
575
+ const content = msg.content as t.MessageContentComplex[];
576
+ return Array.isArray(content) && content.some(item =>
577
+ typeof item === 'object' && item.type === 'thinking');
578
+ });
579
+ expect(hasThinkingBlock).toBe(true);
580
+
581
+ // Test case with system message
582
+ const messagesWithSystem = [
583
+ new SystemMessage('System instruction'),
584
+ new HumanMessage('Hello'),
585
+ assistantMessageWithToolUse,
586
+ toolResponseMessage,
587
+ new HumanMessage('Next message'),
588
+ assistantMessageWithThinking, // Latest message is an assistant message with thinking
589
+ ];
590
+
591
+ // Calculate token counts for each message
592
+ const indexTokenCountMapWithSystem: Record<string, number> = {};
593
+ for (let i = 0; i < messagesWithSystem.length; i++) {
594
+ indexTokenCountMapWithSystem[i] = tokenCounter(messagesWithSystem[i]);
595
+ }
596
+
597
+ // Create pruneMessages function with thinking mode enabled
598
+ const pruneMessagesWithSystem = createPruneMessages({
599
+ maxTokens: 120, // Set a token limit to force some pruning but keep system message
600
+ startIndex: 0,
601
+ tokenCounter,
602
+ indexTokenCountMap: { ...indexTokenCountMapWithSystem },
603
+ thinkingEnabled: true,
604
+ });
605
+
606
+ // Prune messages
607
+ const resultWithSystem = pruneMessagesWithSystem({ messages: messagesWithSystem });
608
+
609
+ // Verify that the system message remains first
610
+ expect(resultWithSystem.context.length).toBeGreaterThan(1);
611
+ expect(resultWithSystem.context[0].getType()).toBe('system');
612
+
613
+ // Find all assistant messages in the latest sequence (after the last human message)
614
+ const lastHumanIndexWithSystem = resultWithSystem.context.map(msg => msg.getType()).lastIndexOf('human');
615
+ const assistantMessagesAfterLastHumanWithSystem = resultWithSystem.context.slice(lastHumanIndexWithSystem + 1)
616
+ .filter(msg => msg.getType() === 'ai');
617
+
618
+ // Verify that at least one assistant message exists in the latest sequence
619
+ expect(assistantMessagesAfterLastHumanWithSystem.length).toBeGreaterThan(0);
620
+
621
+ // Verify that at least one of these assistant messages has a thinking block
622
+ const hasThinkingBlockWithSystem = assistantMessagesAfterLastHumanWithSystem.some(msg => {
623
+ const content = msg.content as t.MessageContentComplex[];
624
+ return Array.isArray(content) && content.some(item =>
625
+ typeof item === 'object' && item.type === 'thinking');
626
+ });
627
+ expect(hasThinkingBlockWithSystem).toBe(true);
628
+ });
629
+
630
+ it('should look for thinking blocks starting from the most recent messages', () => {
631
+ // Create a token counter
632
+ const tokenCounter = createTestTokenCounter();
633
+
634
+ // Create messages with multiple thinking blocks
635
+ const olderAssistantMessageWithThinking = new AIMessage({
636
+ content: [
637
+ {
638
+ type: 'thinking',
639
+ thinking: 'This is an older thinking block',
640
+ },
641
+ {
642
+ type: 'text',
643
+ text: 'Older response with thinking',
644
+ },
645
+ ],
646
+ });
647
+
648
+ const newerAssistantMessageWithThinking = new AIMessage({
649
+ content: [
650
+ {
651
+ type: 'thinking',
652
+ thinking: 'This is a newer thinking block',
653
+ },
654
+ {
655
+ type: 'text',
656
+ text: 'Newer response with thinking',
657
+ },
658
+ ],
659
+ });
660
+
661
+ const messages = [
662
+ new SystemMessage('System instruction'),
663
+ new HumanMessage('Hello'),
664
+ olderAssistantMessageWithThinking,
665
+ new HumanMessage('Next message'),
666
+ newerAssistantMessageWithThinking,
667
+ ];
668
+
669
+ // Calculate token counts for each message
670
+ const indexTokenCountMap: Record<string, number> = {};
671
+ for (let i = 0; i < messages.length; i++) {
672
+ indexTokenCountMap[i] = tokenCounter(messages[i]);
673
+ }
674
+
675
+ // Create pruneMessages function with thinking mode enabled
676
+ // Set a token limit that will force pruning of the older assistant message
677
+ const pruneMessages = createPruneMessages({
678
+ maxTokens: 80,
679
+ startIndex: 0,
680
+ tokenCounter,
681
+ indexTokenCountMap: { ...indexTokenCountMap },
682
+ thinkingEnabled: true,
683
+ });
684
+
685
+ // Prune messages
686
+ const result = pruneMessages({ messages });
687
+
688
+ // Find the first assistant message in the pruned context
689
+ const firstAssistantIndex = result.context.findIndex(msg => msg.getType() === 'ai');
690
+ expect(firstAssistantIndex).not.toBe(-1);
691
+
692
+ const firstAssistantMsg = result.context[firstAssistantIndex];
693
+ expect(Array.isArray(firstAssistantMsg.content)).toBe(true);
694
+
695
+ // Verify that the first assistant message has a thinking block
696
+ const thinkingBlock = (firstAssistantMsg.content as t.MessageContentComplex[]).find(item =>
697
+ typeof item === 'object' && item.type === 'thinking');
698
+ expect(thinkingBlock).toBeDefined();
699
+
700
+ // Verify that it's the newer thinking block
701
+ expect((thinkingBlock as t.ThinkingContentText).thinking).toContain('newer thinking block');
702
+ });
703
+ });