@illuma-ai/agents 1.5.1 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (319) hide show
  1. package/README.md +0 -62
  2. package/dist/cjs/agents/AgentContext.cjs +160 -259
  3. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  4. package/dist/cjs/common/enum.cjs +12 -12
  5. package/dist/cjs/common/enum.cjs.map +1 -1
  6. package/dist/cjs/graphs/Graph.cjs +30 -13
  7. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  8. package/dist/cjs/graphs/MultiAgentGraph.cjs +1 -1
  9. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  10. package/dist/cjs/graphs/phases/memoryFlushPhase.cjs +1 -1
  11. package/dist/cjs/graphs/phases/memoryFlushPhase.cjs.map +1 -1
  12. package/dist/cjs/hooks/HookRegistry.cjs +1 -1
  13. package/dist/cjs/hooks/HookRegistry.cjs.map +1 -1
  14. package/dist/cjs/hooks/matchers.cjs +2 -2
  15. package/dist/cjs/hooks/matchers.cjs.map +1 -1
  16. package/dist/cjs/hooks/types.cjs +1 -1
  17. package/dist/cjs/hooks/types.cjs.map +1 -1
  18. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +1 -5
  19. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
  20. package/dist/cjs/llm/bedrock/index.cjs +33 -61
  21. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  22. package/dist/cjs/llm/openai/index.cjs +1 -1
  23. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  24. package/dist/cjs/llm/openai/utils/index.cjs +10 -27
  25. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
  26. package/dist/cjs/main.cjs +3 -84
  27. package/dist/cjs/main.cjs.map +1 -1
  28. package/dist/cjs/memory/citations.cjs +4 -4
  29. package/dist/cjs/memory/citations.cjs.map +1 -1
  30. package/dist/cjs/memory/constants.cjs +17 -17
  31. package/dist/cjs/memory/constants.cjs.map +1 -1
  32. package/dist/cjs/memory/mmr.cjs +1 -1
  33. package/dist/cjs/memory/mmr.cjs.map +1 -1
  34. package/dist/cjs/memory/paths.cjs +1 -1
  35. package/dist/cjs/memory/paths.cjs.map +1 -1
  36. package/dist/cjs/memory/recallTracking.cjs +3 -3
  37. package/dist/cjs/memory/recallTracking.cjs.map +1 -1
  38. package/dist/cjs/memory/temporalDecay.cjs +2 -2
  39. package/dist/cjs/memory/temporalDecay.cjs.map +1 -1
  40. package/dist/cjs/messages/cache.cjs +0 -89
  41. package/dist/cjs/messages/cache.cjs.map +1 -1
  42. package/dist/cjs/messages/format.cjs +13 -71
  43. package/dist/cjs/messages/format.cjs.map +1 -1
  44. package/dist/cjs/tools/BashExecutor.cjs +11 -21
  45. package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
  46. package/dist/cjs/tools/CodeExecutor.cjs +13 -41
  47. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  48. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +11 -16
  49. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  50. package/dist/cjs/tools/ToolNode.cjs +78 -13
  51. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  52. package/dist/cjs/tools/memory/memoryAppendTool.cjs +1 -1
  53. package/dist/cjs/tools/memory/memoryAppendTool.cjs.map +1 -1
  54. package/dist/cjs/tools/memory/memoryGetTool.cjs +2 -2
  55. package/dist/cjs/tools/memory/memoryGetTool.cjs.map +1 -1
  56. package/dist/cjs/tools/memory/memorySearchTool.cjs +3 -3
  57. package/dist/cjs/tools/memory/memorySearchTool.cjs.map +1 -1
  58. package/dist/cjs/tools/memory/shared.cjs +1 -1
  59. package/dist/cjs/tools/memory/shared.cjs.map +1 -1
  60. package/dist/cjs/tools/search/search.cjs +3 -11
  61. package/dist/cjs/tools/search/search.cjs.map +1 -1
  62. package/dist/cjs/tools/search/tool.cjs +4 -28
  63. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  64. package/dist/cjs/tools/search/utils.cjs +3 -10
  65. package/dist/cjs/tools/search/utils.cjs.map +1 -1
  66. package/dist/cjs/tools/subagent/SubagentExecutor.cjs +48 -0
  67. package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
  68. package/dist/cjs/types/graph.cjs.map +1 -1
  69. package/dist/esm/agents/AgentContext.mjs +160 -259
  70. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  71. package/dist/esm/common/enum.mjs +12 -12
  72. package/dist/esm/common/enum.mjs.map +1 -1
  73. package/dist/esm/graphs/Graph.mjs +30 -13
  74. package/dist/esm/graphs/Graph.mjs.map +1 -1
  75. package/dist/esm/graphs/MultiAgentGraph.mjs +1 -1
  76. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  77. package/dist/esm/graphs/phases/memoryFlushPhase.mjs +1 -1
  78. package/dist/esm/graphs/phases/memoryFlushPhase.mjs.map +1 -1
  79. package/dist/esm/hooks/HookRegistry.mjs +1 -1
  80. package/dist/esm/hooks/HookRegistry.mjs.map +1 -1
  81. package/dist/esm/hooks/matchers.mjs +2 -2
  82. package/dist/esm/hooks/matchers.mjs.map +1 -1
  83. package/dist/esm/hooks/types.mjs +1 -1
  84. package/dist/esm/hooks/types.mjs.map +1 -1
  85. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +1 -5
  86. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
  87. package/dist/esm/llm/bedrock/index.mjs +34 -61
  88. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  89. package/dist/esm/llm/openai/index.mjs +1 -1
  90. package/dist/esm/llm/openai/index.mjs.map +1 -1
  91. package/dist/esm/llm/openai/utils/index.mjs +10 -27
  92. package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
  93. package/dist/esm/main.mjs +1 -5
  94. package/dist/esm/main.mjs.map +1 -1
  95. package/dist/esm/memory/citations.mjs +4 -4
  96. package/dist/esm/memory/citations.mjs.map +1 -1
  97. package/dist/esm/memory/constants.mjs +17 -17
  98. package/dist/esm/memory/constants.mjs.map +1 -1
  99. package/dist/esm/memory/mmr.mjs +1 -1
  100. package/dist/esm/memory/mmr.mjs.map +1 -1
  101. package/dist/esm/memory/paths.mjs +1 -1
  102. package/dist/esm/memory/paths.mjs.map +1 -1
  103. package/dist/esm/memory/recallTracking.mjs +3 -3
  104. package/dist/esm/memory/recallTracking.mjs.map +1 -1
  105. package/dist/esm/memory/temporalDecay.mjs +2 -2
  106. package/dist/esm/memory/temporalDecay.mjs.map +1 -1
  107. package/dist/esm/messages/cache.mjs +0 -89
  108. package/dist/esm/messages/cache.mjs.map +1 -1
  109. package/dist/esm/messages/format.mjs +13 -71
  110. package/dist/esm/messages/format.mjs.map +1 -1
  111. package/dist/esm/tools/BashExecutor.mjs +12 -22
  112. package/dist/esm/tools/BashExecutor.mjs.map +1 -1
  113. package/dist/esm/tools/CodeExecutor.mjs +14 -41
  114. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  115. package/dist/esm/tools/ProgrammaticToolCalling.mjs +12 -17
  116. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  117. package/dist/esm/tools/ToolNode.mjs +78 -13
  118. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  119. package/dist/esm/tools/memory/memoryAppendTool.mjs +1 -1
  120. package/dist/esm/tools/memory/memoryAppendTool.mjs.map +1 -1
  121. package/dist/esm/tools/memory/memoryGetTool.mjs +2 -2
  122. package/dist/esm/tools/memory/memoryGetTool.mjs.map +1 -1
  123. package/dist/esm/tools/memory/memorySearchTool.mjs +3 -3
  124. package/dist/esm/tools/memory/memorySearchTool.mjs.map +1 -1
  125. package/dist/esm/tools/memory/shared.mjs +1 -1
  126. package/dist/esm/tools/memory/shared.mjs.map +1 -1
  127. package/dist/esm/tools/search/search.mjs +3 -11
  128. package/dist/esm/tools/search/search.mjs.map +1 -1
  129. package/dist/esm/tools/search/tool.mjs +4 -28
  130. package/dist/esm/tools/search/tool.mjs.map +1 -1
  131. package/dist/esm/tools/search/utils.mjs +3 -10
  132. package/dist/esm/tools/search/utils.mjs.map +1 -1
  133. package/dist/esm/tools/subagent/SubagentExecutor.mjs +48 -0
  134. package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
  135. package/dist/esm/types/graph.mjs.map +1 -1
  136. package/dist/types/agents/AgentContext.d.ts +25 -95
  137. package/dist/types/common/enum.d.ts +12 -12
  138. package/dist/types/graphs/Graph.d.ts +2 -2
  139. package/dist/types/graphs/phases/memoryFlushPhase.d.ts +2 -2
  140. package/dist/types/hooks/HookRegistry.d.ts +1 -1
  141. package/dist/types/hooks/matchers.d.ts +2 -2
  142. package/dist/types/hooks/types.d.ts +1 -1
  143. package/dist/types/index.d.ts +0 -1
  144. package/dist/types/llm/bedrock/index.d.ts +1 -54
  145. package/dist/types/llm/openai/index.d.ts +1 -1
  146. package/dist/types/memory/citations.d.ts +4 -4
  147. package/dist/types/memory/constants.d.ts +17 -17
  148. package/dist/types/memory/mmr.d.ts +3 -3
  149. package/dist/types/memory/paths.d.ts +1 -1
  150. package/dist/types/memory/temporalDecay.d.ts +2 -2
  151. package/dist/types/memory/types.d.ts +3 -3
  152. package/dist/types/messages/format.d.ts +2 -5
  153. package/dist/types/tools/CodeExecutor.d.ts +0 -6
  154. package/dist/types/tools/ToolNode.d.ts +3 -3
  155. package/dist/types/tools/memory/shared.d.ts +1 -1
  156. package/dist/types/tools/search/test.d.ts +1 -0
  157. package/dist/types/tools/search/types.d.ts +5 -99
  158. package/dist/types/tools/search/utils.d.ts +2 -2
  159. package/dist/types/tools/subagent/SubagentExecutor.d.ts +29 -0
  160. package/dist/types/types/graph.d.ts +30 -34
  161. package/dist/types/types/index.d.ts +0 -1
  162. package/dist/types/types/messages.d.ts +1 -1
  163. package/dist/types/types/run.d.ts +1 -3
  164. package/dist/types/types/tools.d.ts +5 -14
  165. package/package.json +1 -61
  166. package/src/agents/AgentContext.test.ts +176 -0
  167. package/src/agents/AgentContext.ts +179 -305
  168. package/src/agents/__tests__/AgentContext.test.ts +0 -632
  169. package/src/common/__tests__/enum.test.ts +1 -1
  170. package/src/common/enum.ts +12 -12
  171. package/src/graphs/Graph.ts +32 -13
  172. package/src/graphs/MultiAgentGraph.ts +1 -1
  173. package/src/graphs/gapFeatures.test.ts +1 -1
  174. package/src/graphs/phases/__tests__/memoryFlushPhase.test.ts +1 -1
  175. package/src/graphs/phases/memoryFlushPhase.ts +2 -2
  176. package/src/hooks/HookRegistry.ts +1 -1
  177. package/src/hooks/index.ts +1 -1
  178. package/src/hooks/matchers.ts +2 -2
  179. package/src/hooks/types.ts +1 -1
  180. package/src/index.ts +0 -6
  181. package/src/llm/anthropic/utils/message_inputs.ts +1 -10
  182. package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +18 -166
  183. package/src/llm/bedrock/index.ts +41 -116
  184. package/src/llm/openai/index.ts +2 -2
  185. package/src/llm/openai/utils/index.ts +14 -31
  186. package/src/memory/citations.ts +4 -4
  187. package/src/memory/constants.ts +17 -17
  188. package/src/memory/mmr.ts +3 -3
  189. package/src/memory/paths.ts +1 -1
  190. package/src/memory/recallTracking.ts +3 -3
  191. package/src/memory/temporalDecay.ts +2 -2
  192. package/src/memory/types.ts +3 -3
  193. package/src/messages/cache.test.ts +24 -62
  194. package/src/messages/cache.ts +0 -112
  195. package/src/messages/ensureThinkingBlock.test.ts +1 -1
  196. package/src/messages/format.ts +13 -92
  197. package/src/messages/formatAgentMessages.test.ts +1 -1
  198. package/src/scripts/subagent-configurable-inheritance.ts +263 -0
  199. package/src/scripts/subagent-event-driven-debug.ts +2 -2
  200. package/src/specs/anthropic.simple.test.ts +0 -61
  201. package/src/specs/prune.orphans.test.ts +1 -1
  202. package/src/tools/BashExecutor.ts +13 -37
  203. package/src/tools/CodeExecutor.ts +14 -59
  204. package/src/tools/ProgrammaticToolCalling.ts +14 -29
  205. package/src/tools/ToolNode.ts +75 -14
  206. package/src/tools/__tests__/CodeExecutor.test.ts +3 -3
  207. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +0 -60
  208. package/src/tools/__tests__/SubagentExecutor.test.ts +157 -0
  209. package/src/tools/memory/memoryAppendTool.ts +1 -1
  210. package/src/tools/memory/memoryGetTool.ts +2 -2
  211. package/src/tools/memory/memorySearchTool.ts +3 -3
  212. package/src/tools/memory/shared.ts +1 -1
  213. package/src/tools/search/output.md +2775 -0
  214. package/src/tools/search/search.ts +2 -12
  215. package/src/tools/search/test.html +884 -0
  216. package/src/tools/search/test.md +643 -0
  217. package/src/tools/search/test.ts +159 -0
  218. package/src/tools/search/tool.ts +2 -36
  219. package/src/tools/search/types.ts +8 -133
  220. package/src/tools/search/utils.ts +5 -13
  221. package/src/tools/subagent/SubagentExecutor.ts +78 -0
  222. package/src/types/graph.ts +27 -34
  223. package/src/types/index.ts +0 -1
  224. package/src/types/messages.ts +1 -1
  225. package/src/types/run.ts +1 -3
  226. package/src/types/tools.ts +5 -14
  227. package/dist/cjs/langchain/google-common.cjs +0 -3
  228. package/dist/cjs/langchain/google-common.cjs.map +0 -1
  229. package/dist/cjs/langchain/index.cjs +0 -86
  230. package/dist/cjs/langchain/index.cjs.map +0 -1
  231. package/dist/cjs/langchain/language_models/chat_models.cjs +0 -3
  232. package/dist/cjs/langchain/language_models/chat_models.cjs.map +0 -1
  233. package/dist/cjs/langchain/messages/tool.cjs +0 -3
  234. package/dist/cjs/langchain/messages/tool.cjs.map +0 -1
  235. package/dist/cjs/langchain/messages.cjs +0 -51
  236. package/dist/cjs/langchain/messages.cjs.map +0 -1
  237. package/dist/cjs/langchain/openai.cjs +0 -3
  238. package/dist/cjs/langchain/openai.cjs.map +0 -1
  239. package/dist/cjs/langchain/prompts.cjs +0 -11
  240. package/dist/cjs/langchain/prompts.cjs.map +0 -1
  241. package/dist/cjs/langchain/runnables.cjs +0 -19
  242. package/dist/cjs/langchain/runnables.cjs.map +0 -1
  243. package/dist/cjs/langchain/tools.cjs +0 -23
  244. package/dist/cjs/langchain/tools.cjs.map +0 -1
  245. package/dist/cjs/langchain/utils/env.cjs +0 -11
  246. package/dist/cjs/langchain/utils/env.cjs.map +0 -1
  247. package/dist/cjs/llm/bedrock/cacheSupport.cjs +0 -55
  248. package/dist/cjs/llm/bedrock/cacheSupport.cjs.map +0 -1
  249. package/dist/cjs/tools/search/tavily-scraper.cjs +0 -189
  250. package/dist/cjs/tools/search/tavily-scraper.cjs.map +0 -1
  251. package/dist/cjs/tools/search/tavily-search.cjs +0 -372
  252. package/dist/cjs/tools/search/tavily-search.cjs.map +0 -1
  253. package/dist/cjs/types/agent-cache.cjs +0 -54
  254. package/dist/cjs/types/agent-cache.cjs.map +0 -1
  255. package/dist/esm/langchain/google-common.mjs +0 -2
  256. package/dist/esm/langchain/google-common.mjs.map +0 -1
  257. package/dist/esm/langchain/index.mjs +0 -5
  258. package/dist/esm/langchain/index.mjs.map +0 -1
  259. package/dist/esm/langchain/language_models/chat_models.mjs +0 -2
  260. package/dist/esm/langchain/language_models/chat_models.mjs.map +0 -1
  261. package/dist/esm/langchain/messages/tool.mjs +0 -2
  262. package/dist/esm/langchain/messages/tool.mjs.map +0 -1
  263. package/dist/esm/langchain/messages.mjs +0 -2
  264. package/dist/esm/langchain/messages.mjs.map +0 -1
  265. package/dist/esm/langchain/openai.mjs +0 -2
  266. package/dist/esm/langchain/openai.mjs.map +0 -1
  267. package/dist/esm/langchain/prompts.mjs +0 -2
  268. package/dist/esm/langchain/prompts.mjs.map +0 -1
  269. package/dist/esm/langchain/runnables.mjs +0 -2
  270. package/dist/esm/langchain/runnables.mjs.map +0 -1
  271. package/dist/esm/langchain/tools.mjs +0 -2
  272. package/dist/esm/langchain/tools.mjs.map +0 -1
  273. package/dist/esm/langchain/utils/env.mjs +0 -2
  274. package/dist/esm/langchain/utils/env.mjs.map +0 -1
  275. package/dist/esm/llm/bedrock/cacheSupport.mjs +0 -52
  276. package/dist/esm/llm/bedrock/cacheSupport.mjs.map +0 -1
  277. package/dist/esm/tools/search/tavily-scraper.mjs +0 -186
  278. package/dist/esm/tools/search/tavily-scraper.mjs.map +0 -1
  279. package/dist/esm/tools/search/tavily-search.mjs +0 -370
  280. package/dist/esm/tools/search/tavily-search.mjs.map +0 -1
  281. package/dist/esm/types/agent-cache.mjs +0 -52
  282. package/dist/esm/types/agent-cache.mjs.map +0 -1
  283. package/dist/types/langchain/google-common.d.ts +0 -1
  284. package/dist/types/langchain/index.d.ts +0 -8
  285. package/dist/types/langchain/language_models/chat_models.d.ts +0 -1
  286. package/dist/types/langchain/messages/tool.d.ts +0 -1
  287. package/dist/types/langchain/messages.d.ts +0 -2
  288. package/dist/types/langchain/openai.d.ts +0 -1
  289. package/dist/types/langchain/prompts.d.ts +0 -1
  290. package/dist/types/langchain/runnables.d.ts +0 -2
  291. package/dist/types/langchain/tools.d.ts +0 -2
  292. package/dist/types/langchain/utils/env.d.ts +0 -1
  293. package/dist/types/llm/bedrock/cacheSupport.d.ts +0 -35
  294. package/dist/types/tools/search/tavily-scraper.d.ts +0 -19
  295. package/dist/types/tools/search/tavily-search.d.ts +0 -4
  296. package/dist/types/tools/subagent/types.d.ts +0 -84
  297. package/dist/types/types/agent-cache.d.ts +0 -71
  298. package/src/agents/__tests__/AgentContext.cacheTtl.live.test.ts +0 -259
  299. package/src/agents/__tests__/AgentContext.crossAgentTier1.live.test.ts +0 -266
  300. package/src/agents/__tests__/AgentContext.crossUserCache.live.test.ts +0 -342
  301. package/src/langchain/google-common.ts +0 -1
  302. package/src/langchain/index.ts +0 -8
  303. package/src/langchain/language_models/chat_models.ts +0 -1
  304. package/src/langchain/messages/tool.ts +0 -5
  305. package/src/langchain/messages.ts +0 -21
  306. package/src/langchain/openai.ts +0 -1
  307. package/src/langchain/prompts.ts +0 -1
  308. package/src/langchain/runnables.ts +0 -7
  309. package/src/langchain/tools.ts +0 -8
  310. package/src/langchain/utils/env.ts +0 -1
  311. package/src/llm/anthropic/utils/server-tool-inputs.test.ts +0 -436
  312. package/src/llm/bedrock/cacheSupport.test.ts +0 -99
  313. package/src/llm/bedrock/cacheSupport.ts +0 -53
  314. package/src/tools/search/tavily-scraper.ts +0 -235
  315. package/src/tools/search/tavily-search.ts +0 -424
  316. package/src/tools/search/tavily.test.ts +0 -965
  317. package/src/tools/subagent/types.test.ts +0 -70
  318. package/src/tools/subagent/types.ts +0 -115
  319. package/src/types/agent-cache.ts +0 -74
package/README.md CHANGED
@@ -398,68 +398,6 @@ Caching is **automatic** for Anthropic and Bedrock providers:
398
398
  }
399
399
  ```
400
400
 
401
- ### Multi-block system caching (cross-tenant prefix sharing)
402
-
403
- For multi-agent platforms where a stable prefix is shared across many
404
- agents, use `system_cache_blocks` to emit additional cachePoint markers.
405
- A cacheable block placed BEFORE `instructions` is cached at provider
406
- level (Anthropic workspace, Bedrock account) and shared across every
407
- agent that emits the same byte-identical prefix:
408
-
409
- ```typescript
410
- {
411
- system_cache_blocks: [
412
- { text: PLATFORM_WIDE_RULES, ttl: '5m' }, // shared across all agents
413
- ],
414
- instructions: agentSpecificInstructions, // per-agent stable
415
- additional_instructions: perRequestContext, // never cached
416
- }
417
- ```
418
-
419
- This produces (Bedrock):
420
-
421
- ```
422
- [PLATFORM_WIDE_RULES]
423
- [cachePoint: 5m] ← cross-agent share point
424
- [agentSpecificInstructions]
425
- [cachePoint: 5m] ← per-agent cache
426
- [perRequestContext] ← uncached
427
- ```
428
-
429
- Capped at `MAX_SYSTEM_CACHE_BLOCKS` (2 entries) to stay within Bedrock's
430
- 4-cachePoint per-request budget. See
431
- [`docs/prompt-cache-architecture.md`](docs/prompt-cache-architecture.md)
432
- for the full architecture.
433
-
434
- ### Tool array cachePoint strategies (Bedrock)
435
-
436
- ```typescript
437
- new IllumaBedrockConverse({
438
- promptCache: true,
439
- toolCacheStrategy: 'split', // 'single' (default) | 'split' | 'none'
440
- toolGroupSelector: (toolName) =>
441
- toolName.includes('_mcp_') ? 'mcp' : 'core',
442
- });
443
- ```
444
-
445
- - `'single'`: one cachePoint at the end of the tools array (default)
446
- - `'split'`: separate cachePoints per group (use when groups have
447
- different volatility)
448
- - `'none'`: no cachePoint on tools
449
-
450
- ### Extending the Bedrock model allowlist
451
-
452
- ```typescript
453
- new IllumaBedrockConverse({
454
- model: 'mycorp.future-model-v1:0',
455
- promptCache: true,
456
- bedrockCacheModelPatterns: [/^mycorp\.future-/i],
457
- });
458
- ```
459
-
460
- Useful when AWS adds a new cache-supporting model family before the
461
- library's allowlist updates.
462
-
463
401
  ---
464
402
 
465
403
  ## Structured Output Modes
@@ -5,8 +5,6 @@ var runnables = require('@langchain/core/runnables');
5
5
  var schema$1 = require('../tools/schema.cjs');
6
6
  var _enum = require('../common/enum.cjs');
7
7
  require('../tools/approval/constants.cjs');
8
- var agentCache = require('../types/agent-cache.cjs');
9
- var cacheSupport = require('../llm/bedrock/cacheSupport.cjs');
10
8
  var schema = require('../utils/schema.cjs');
11
9
 
12
10
  /* eslint-disable no-console */
@@ -19,7 +17,7 @@ class AgentContext {
19
17
  * Create an AgentContext from configuration with token accounting initialization
20
18
  */
21
19
  static fromConfig(agentConfig, tokenCounter, indexTokenCountMap) {
22
- const { agentId, name, description, provider, clientOptions, tools, toolMap, toolEnd, toolRegistry, toolDefinitions, system_cache_blocks, instructions, instructions_cache_ttl, additional_instructions, streamBuffer, maxContextTokens, reasoningKey, useLegacyContent, dynamicContext, structuredOutput: structuredOutputCamel, structured_output: structuredOutputSnake, discoveredTools, summarizeCallback, persistedSummary, summarizationConfig, fileManifest, } = agentConfig;
20
+ const { agentId, name, description, provider, clientOptions, tools, toolMap, toolEnd, toolRegistry, toolDefinitions, instructions, additional_instructions, streamBuffer, maxContextTokens, reasoningKey, useLegacyContent, dynamicContext, structuredOutput: structuredOutputCamel, structured_output: structuredOutputSnake, discoveredTools, summarizeCallback, persistedSummary, summarizationConfig, fileManifest, system_cache_blocks, instructions_cache_ttl, } = agentConfig;
23
21
  // Normalize structured output: support both camelCase and snake_case inputs
24
22
  // Priority: structuredOutput (camelCase) > structured_output (snake_case with enabled check)
25
23
  let structuredOutput;
@@ -49,9 +47,7 @@ class AgentContext {
49
47
  toolMap,
50
48
  toolRegistry,
51
49
  toolDefinitions,
52
- systemCacheBlocks: system_cache_blocks,
53
50
  instructions,
54
- instructionsCacheTtl: instructions_cache_ttl,
55
51
  additionalInstructions: additional_instructions,
56
52
  reasoningKey,
57
53
  toolEnd,
@@ -65,9 +61,11 @@ class AgentContext {
65
61
  persistedSummary,
66
62
  summarizationConfig,
67
63
  fileManifest,
64
+ systemCacheBlocks: system_cache_blocks,
65
+ instructionsCacheTtl: instructions_cache_ttl,
68
66
  });
69
67
  /**
70
- * Track subagent inputs on the context. `_sourceInputs`
68
+ * Track upstream-aligned subagent inputs on the context. `_sourceInputs`
71
69
  * preserves the original AgentInputs so SubagentExecutor can self-spawn
72
70
  * (`SubagentConfig.self === true`) without separate config; the other
73
71
  * two flow straight from agentConfig.
@@ -145,18 +143,9 @@ class AgentContext {
145
143
  toolDefinitions;
146
144
  /** Set of tool names discovered via tool search (to be loaded) */
147
145
  discoveredToolNames = new Set();
148
- /**
149
- * Cacheable system content blocks emitted before `instructions`. Each
150
- * gets its own cache marker (cachePoint on Bedrock, cache_control on
151
- * Anthropic). Earlier entries = wider cache key (best for cross-tenant
152
- * sharing). See `src/types/agent-cache.ts`.
153
- */
154
- systemCacheBlocks;
155
- /** Stable/cacheable instructions for this agent (gets trailing marker). */
146
+ /** Instructions for this agent */
156
147
  instructions;
157
- /** TTL for the trailing instructions cache marker. Defaults to '5m'. */
158
- instructionsCacheTtl;
159
- /** Dynamic system tail (per-user / per-message — never cached). */
148
+ /** Additional instructions for this agent */
160
149
  additionalInstructions;
161
150
  /**
162
151
  * Dynamic context that changes per-request (e.g., current time, user info).
@@ -218,13 +207,22 @@ class AgentContext {
218
207
  summarizationConfig;
219
208
  /** Lightweight file manifest for file-aware compaction (IDs and names only, no content) */
220
209
  fileManifest;
210
+ /**
211
+ * Workspace-shared system-message tiers. When set, each entry becomes a
212
+ * separate text block in the SystemMessage with its own cachePoint /
213
+ * cache_control marker, BEFORE the per-agent `instructions` block.
214
+ * See {@link t.AgentInputs.system_cache_blocks} for full semantics.
215
+ */
216
+ systemCacheBlocks;
217
+ /** TTL hint for the per-agent instructions cache marker. Defaults to '5m'. */
218
+ instructionsCacheTtl;
221
219
  /** Original AgentInputs used to create this context — used for self-spawn subagent resolution. */
222
220
  _sourceInputs;
223
221
  /** Subagent configurations for hierarchical delegation. */
224
222
  subagentConfigs;
225
223
  /** Maximum subagent nesting depth. */
226
224
  maxSubagentDepth;
227
- constructor({ agentId, name, description, provider, clientOptions, maxContextTokens, streamBuffer, tokenCounter, tools, toolMap, toolRegistry, toolDefinitions, systemCacheBlocks, instructions, instructionsCacheTtl, additionalInstructions, dynamicContext, reasoningKey, toolEnd, instructionTokens, useLegacyContent, structuredOutput, discoveredTools, summarizeCallback, persistedSummary, summarizationConfig, fileManifest, }) {
225
+ constructor({ agentId, name, description, provider, clientOptions, maxContextTokens, streamBuffer, tokenCounter, tools, toolMap, toolRegistry, toolDefinitions, instructions, additionalInstructions, dynamicContext, reasoningKey, toolEnd, instructionTokens, useLegacyContent, structuredOutput, discoveredTools, summarizeCallback, persistedSummary, summarizationConfig, fileManifest, systemCacheBlocks, instructionsCacheTtl, }) {
228
226
  this.agentId = agentId;
229
227
  this.name = name;
230
228
  this.description = description;
@@ -237,16 +235,7 @@ class AgentContext {
237
235
  this.toolMap = toolMap;
238
236
  this.toolRegistry = toolRegistry;
239
237
  this.toolDefinitions = toolDefinitions;
240
- if (systemCacheBlocks &&
241
- systemCacheBlocks.length > agentCache.MAX_SYSTEM_CACHE_BLOCKS) {
242
- throw new Error(`system_cache_blocks supports at most ${agentCache.MAX_SYSTEM_CACHE_BLOCKS} entries ` +
243
- `(received ${systemCacheBlocks.length}); excess entries would exceed ` +
244
- `Bedrock's 4-cachePoint per-request budget once the tools array and ` +
245
- `trailing 'instructions' marker are counted. See src/types/agent-cache.ts.`);
246
- }
247
- this.systemCacheBlocks = systemCacheBlocks;
248
238
  this.instructions = instructions;
249
- this.instructionsCacheTtl = instructionsCacheTtl;
250
239
  this.additionalInstructions = additionalInstructions;
251
240
  this.dynamicContext = dynamicContext;
252
241
  this.structuredOutput = structuredOutput;
@@ -254,6 +243,12 @@ class AgentContext {
254
243
  this.persistedSummary = persistedSummary;
255
244
  this.summarizationConfig = summarizationConfig;
256
245
  this.fileManifest = fileManifest;
246
+ if (systemCacheBlocks && systemCacheBlocks.length > 0) {
247
+ this.systemCacheBlocks = systemCacheBlocks;
248
+ }
249
+ if (instructionsCacheTtl) {
250
+ this.instructionsCacheTtl = instructionsCacheTtl;
251
+ }
257
252
  if (reasoningKey) {
258
253
  this.reasoningKey = reasoningKey;
259
254
  }
@@ -437,11 +432,8 @@ class AgentContext {
437
432
  return this.cachedSystemRunnable;
438
433
  }
439
434
  // Stale or first access - rebuild
440
- this.cachedSystemRunnable = this.buildSystemRunnable({
441
- systemCacheBlocks: this.systemCacheBlocks ?? [],
442
- stableInstructions: this.buildStableInstructionsString(),
443
- dynamicInstructions: this.buildDynamicInstructionsString(),
444
- });
435
+ const instructionsString = this.buildInstructionsString();
436
+ this.cachedSystemRunnable = this.buildSystemRunnable(instructionsString);
445
437
  this.systemRunnableStale = false;
446
438
  return this.cachedSystemRunnable;
447
439
  }
@@ -451,26 +443,16 @@ class AgentContext {
451
443
  */
452
444
  initializeSystemRunnable() {
453
445
  if (this.systemRunnableStale || this.cachedSystemRunnable === undefined) {
454
- this.cachedSystemRunnable = this.buildSystemRunnable({
455
- systemCacheBlocks: this.systemCacheBlocks ?? [],
456
- stableInstructions: this.buildStableInstructionsString(),
457
- dynamicInstructions: this.buildDynamicInstructionsString(),
458
- });
446
+ const instructionsString = this.buildInstructionsString();
447
+ this.cachedSystemRunnable = this.buildSystemRunnable(instructionsString);
459
448
  this.systemRunnableStale = false;
460
449
  }
461
450
  }
462
451
  /**
463
- * Builds the cacheable instructions string (without creating SystemMessage).
464
- * Includes agent identity preamble, the agent's static instructions, and
465
- * programmatic-only tool documentation. This is the part of the system
466
- * message that stays byte-stable across turns and across users for the
467
- * same agent — the prompt cache prefix.
468
- *
469
- * Per-user/per-message dynamic context belongs in
470
- * `buildDynamicInstructionsString()` so it does not invalidate the cache
471
- * marker. (See `feedback_cache_stability_invariant` for the rule.)
452
+ * Builds the raw instructions string (without creating SystemMessage).
453
+ * Includes agent identity preamble and handoff context when available.
472
454
  */
473
- buildStableInstructionsString() {
455
+ buildInstructionsString() {
474
456
  const parts = [];
475
457
  /** Build agent identity and handoff context preamble */
476
458
  const identityPreamble = this.buildIdentityPreamble();
@@ -481,6 +463,11 @@ class AgentContext {
481
463
  if (this.instructions != null && this.instructions !== '') {
482
464
  parts.push(this.instructions);
483
465
  }
466
+ /** Add additional instructions */
467
+ if (this.additionalInstructions != null &&
468
+ this.additionalInstructions !== '') {
469
+ parts.push(this.additionalInstructions);
470
+ }
484
471
  /** Add programmatic tools documentation */
485
472
  const programmaticToolsDoc = this.buildProgrammaticOnlyToolsInstructions();
486
473
  if (programmaticToolsDoc) {
@@ -488,23 +475,6 @@ class AgentContext {
488
475
  }
489
476
  return parts.join('\n\n');
490
477
  }
491
- /**
492
- * Builds the dynamic system-tail string (without creating SystemMessage).
493
- * Keep this out of prompt-cache-marked content so volatile per-call
494
- * context does not invalidate the stable prefix.
495
- *
496
- * `additional_instructions` is treated as dynamic (per-user/per-message
497
- * memory, runtime context, etc.) and intentionally excluded from the
498
- * cacheable prefix.
499
- */
500
- buildDynamicInstructionsString() {
501
- const parts = [];
502
- if (this.additionalInstructions != null &&
503
- this.additionalInstructions !== '') {
504
- parts.push(this.additionalInstructions);
505
- }
506
- return parts.join('\n\n');
507
- }
508
478
  /**
509
479
  * Builds the agent identity preamble including handoff context if present.
510
480
  * This helps the agent understand its role in the multi-agent workflow.
@@ -542,136 +512,107 @@ class AgentContext {
542
512
  return lines.join('\n');
543
513
  }
544
514
  /**
545
- * True when Anthropic prompt caching is enabled for this agent.
546
- * Used by `buildSystemRunnable` to decide whether to emit a cache_control
547
- * marker on the stable instructions prefix.
515
+ * Build system runnable from pre-built instructions string.
516
+ * Only called when content has actually changed.
548
517
  */
549
- hasAnthropicPromptCache() {
550
- if (this.provider !== _enum.Providers.ANTHROPIC) {
551
- return false;
552
- }
553
- const anthropicOptions = this.clientOptions;
554
- return anthropicOptions?.promptCache === true;
555
- }
556
- /**
557
- * True when Bedrock prompt caching is enabled for this agent AND the
558
- * configured model supports `cachePoint` blocks. Only Claude (and Nova)
559
- * models on Bedrock honour cachePoint — Llama / Titan reject it.
560
- *
561
- * Used by `buildSystemRunnable` to inline a `cachePoint` block right
562
- * after the stable system text so the system prefix is cached at the
563
- * AWS account level (cross-user, cross-conversation).
564
- */
565
- hasBedrockPromptCache() {
566
- if (this.provider !== _enum.Providers.BEDROCK) {
567
- return false;
568
- }
569
- const bedrockOptions = this.clientOptions;
570
- if (bedrockOptions?.promptCache !== true) {
571
- return false;
572
- }
573
- /* Allowlist-based check (see src/llm/bedrock/cacheSupport.ts). The
574
- * `bedrockCacheModelPatterns` clientOption lets consumers add new
575
- * model patterns without forking the library — useful when AWS adds
576
- * a new family before the next library release. */
577
- return cacheSupport.isBedrockCacheSupported(bedrockOptions.model, bedrockOptions.bedrockCacheModelPatterns);
578
- }
579
- /**
580
- * Build system runnable from cacheable blocks + the trailing
581
- * `instructions` block + optional dynamic tail.
582
- *
583
- * ┌──────────────────────────────────────────┐
584
- * │ system_cache_blocks[0].text │ ← consumer-defined block 0
585
- * ├──── cache marker (TTL = blocks[0].ttl) ──┤
586
- * │ system_cache_blocks[1].text │ ← consumer-defined block 1
587
- * ├──── cache marker (TTL = blocks[1].ttl) ──┤
588
- * │ instructions │ ← per-agent stable
589
- * ├──── cache marker (TTL = instructionsCacheTtl) ┤
590
- * │ additional_instructions │ ← dynamic (uncached)
591
- * └──────────────────────────────────────────┘
592
- *
593
- * If `system_cache_blocks` is empty, behavior reduces to the 2-tier
594
- * (instructions + dynamic) path used by simpler consumers.
595
- *
596
- * Provider-specific cache marker:
597
- * - Anthropic: `cache_control: { type: 'ephemeral', ttl?: '1h'|'5m' }` on
598
- * each cacheable text block. Up to 4 cache breakpoints per workspace.
599
- * - Bedrock (Claude/Nova): a `{ cachePoint: { type: 'default', ttl?: '1h'|'5m' } }`
600
- * block inserted after each cacheable section. Up to 4 cachePoints per
601
- * request, of which the tools array can consume up to 2.
602
- *
603
- * Cache key composition: every byte from message start to a given cache
604
- * marker forms that marker's cache key. Earlier blocks = wider cache key
605
- * = more cross-tenant share. Place the most stable content first.
606
- */
607
- buildSystemRunnable({ systemCacheBlocks, stableInstructions, dynamicInstructions, }) {
608
- const hasAnyCacheBlocks = systemCacheBlocks.length > 0;
609
- if (!hasAnyCacheBlocks && !stableInstructions && !dynamicInstructions) {
518
+ buildSystemRunnable(instructionsString) {
519
+ if (!instructionsString) {
610
520
  // Remove previous tokens if we had a system message before
611
521
  this.instructionTokens -= this.systemMessageTokens;
612
522
  this.systemMessageTokens = 0;
613
523
  return undefined;
614
524
  }
615
- const useAnthropicCache = this.hasAnthropicPromptCache();
616
- const useBedrockCache = this.hasBedrockPromptCache();
617
- const instructionsTtl = this.instructionsCacheTtl ?? '5m';
618
- let finalInstructions;
619
- if (useAnthropicCache) {
620
- const content = [];
621
- // Emit each system_cache_blocks entry with its own cache_control.
622
- for (const block of systemCacheBlocks) {
623
- if (!block.text)
624
- continue;
625
- content.push({
626
- type: 'text',
627
- text: block.text,
628
- cache_control: { type: 'ephemeral', ttl: block.ttl ?? '5m' },
629
- });
630
- }
631
- // Trailing stable instructions block (also cacheable).
632
- if (stableInstructions) {
633
- content.push({
634
- type: 'text',
635
- text: stableInstructions,
636
- cache_control: { type: 'ephemeral', ttl: instructionsTtl },
637
- });
638
- }
639
- if (dynamicInstructions) {
640
- // Dynamic tail: NO cache_control so it doesn't shift the cache prefix.
641
- content.push({ type: 'text', text: dynamicInstructions });
642
- }
643
- finalInstructions = { content };
644
- }
645
- else if (useBedrockCache && (hasAnyCacheBlocks || stableInstructions)) {
646
- const content = [];
647
- // Emit each system_cache_blocks entry as text + cachePoint pair.
648
- for (const block of systemCacheBlocks) {
649
- if (!block.text)
650
- continue;
651
- content.push({ type: 'text', text: block.text });
652
- content.push({
653
- cachePoint: { type: 'default', ttl: block.ttl ?? '5m' },
654
- });
655
- }
656
- if (stableInstructions) {
657
- content.push({ type: 'text', text: stableInstructions });
658
- content.push({
659
- cachePoint: { type: 'default', ttl: instructionsTtl },
660
- });
661
- }
662
- if (dynamicInstructions) {
663
- content.push({ type: 'text', text: dynamicInstructions });
525
+ let finalInstructions = instructionsString;
526
+ /**
527
+ * Tiered system-message assembly. When `systemCacheBlocks` is set, build
528
+ * a multi-block SystemMessage so each tier has its own cache breakpoint:
529
+ *
530
+ * [tier1_block_1][tier1_cachePoint]...[tier1_block_N][tier1_cachePoint]
531
+ * [instructions][instructions_cachePoint]
532
+ *
533
+ * Forward-prefix-hash means agents in the same workspace whose tier-1
534
+ * bytes are identical share the platform cache entry; only the per-agent
535
+ * `instructions` block invalidates per-agent.
536
+ *
537
+ * - Bedrock: emit `cachePoint: { type: 'default' }` blocks (handled by
538
+ * `convertSystemMessageToConverseMessage`).
539
+ * - Anthropic: emit `cache_control: { type: 'ephemeral' }` on each text
540
+ * block. (TTL hints are dropped for Anthropic — the SDK currently
541
+ * only supports `'ephemeral'`. Bedrock cachePoint has no TTL knob.)
542
+ */
543
+ const hasTieredCache = Array.isArray(this.systemCacheBlocks) &&
544
+ this.systemCacheBlocks.length > 0;
545
+ const isBedrock = this.provider === _enum.Providers.BEDROCK;
546
+ const isAnthropic = this.provider === _enum.Providers.ANTHROPIC;
547
+ const anthropicCacheEnabled = isAnthropic &&
548
+ this.clientOptions
549
+ ?.promptCache === true;
550
+ /**
551
+ * Bedrock cachePoint is Claude-only — Nova/Llama/Titan reject it.
552
+ * Mirrors the model check in `IllumaBedrockConverse.invocationParams`
553
+ * (src/llm/bedrock/index.ts:186-189).
554
+ */
555
+ const bedrockCacheEnabled = isBedrock &&
556
+ (() => {
557
+ const opts = this.clientOptions;
558
+ const modelId = (opts?.model ?? '').toLowerCase();
559
+ const isClaudeModel = modelId.includes('claude') || modelId.includes('anthropic');
560
+ return opts?.promptCache === true && isClaudeModel;
561
+ })();
562
+ if (hasTieredCache && (bedrockCacheEnabled || anthropicCacheEnabled)) {
563
+ /**
564
+ * Anthropic / Bedrock cap cache breakpoints at 4 per request. The
565
+ * lib already emits one for tools and up to two for messages, so we
566
+ * have at most 1 left for the system block. We spend it on Tier 1
567
+ * (the workspace-shared bytes) per-agent Tier 2 caching is still
568
+ * achieved implicitly via the tools breakpoint that follows, since
569
+ * cache lookups are forward-prefix-hash.
570
+ *
571
+ * Tier 1 may itself be emitted as multiple text blocks (one per
572
+ * `systemCacheBlocks` entry); only the LAST gets the cache marker,
573
+ * the rest are plain text inside the same cached prefix.
574
+ */
575
+ const contentBlocks = [];
576
+ const tier1Blocks = this.systemCacheBlocks.filter((b) => b?.text);
577
+ tier1Blocks.forEach((block, idx) => {
578
+ const isLast = idx === tier1Blocks.length - 1;
579
+ if (bedrockCacheEnabled) {
580
+ contentBlocks.push({ type: 'text', text: block.text });
581
+ if (isLast) {
582
+ contentBlocks.push({ cachePoint: { type: 'default' } });
583
+ }
584
+ }
585
+ else if (isLast) {
586
+ contentBlocks.push({
587
+ type: 'text',
588
+ text: block.text,
589
+ cache_control: { type: 'ephemeral' },
590
+ });
591
+ }
592
+ else {
593
+ contentBlocks.push({ type: 'text', text: block.text });
594
+ }
595
+ });
596
+ if (instructionsString) {
597
+ // No cache marker on the trailing per-agent block — tools'
598
+ // breakpoint covers it.
599
+ contentBlocks.push({ type: 'text', text: instructionsString });
664
600
  }
665
- finalInstructions = { content };
601
+ finalInstructions = {
602
+ content: contentBlocks,
603
+ };
666
604
  }
667
- else {
668
- finalInstructions = [
669
- ...systemCacheBlocks.map((b) => b.text),
670
- stableInstructions,
671
- dynamicInstructions,
672
- ]
673
- .filter((part) => part !== '')
674
- .join('\n\n');
605
+ else if (anthropicCacheEnabled) {
606
+ // Legacy single-block Anthropic caching (preserved for back-compat).
607
+ finalInstructions = {
608
+ content: [
609
+ {
610
+ type: 'text',
611
+ text: instructionsString,
612
+ cache_control: { type: 'ephemeral' },
613
+ },
614
+ ],
615
+ };
675
616
  }
676
617
  const systemMessage = new messages.SystemMessage(finalInstructions);
677
618
  // Update token counts (subtract old, add new)
@@ -740,45 +681,6 @@ class AgentContext {
740
681
  this.indexTokenCountMap = { ...baseTokenMap };
741
682
  }
742
683
  }
743
- /** Active tool definitions for token accounting (excludes deferred-and-undiscovered entries
744
- * and definitions whose `allowed_callers` exclude `'direct'`). Mirrors the gate
745
- * `getEventDrivenToolsForBinding` applies so accounting and binding stay aligned. */
746
- getActiveToolDefinitions() {
747
- if (!this.toolDefinitions) {
748
- return [];
749
- }
750
- return this.toolDefinitions.filter((def) => {
751
- const allowedCallers = def.allowed_callers ?? ['direct'];
752
- if (!allowedCallers.includes('direct')) {
753
- return false;
754
- }
755
- return (def.defer_loading !== true || this.discoveredToolNames.has(def.name));
756
- });
757
- }
758
- /**
759
- * Single source of truth for "which entries of `this.tools` should be
760
- * treated as actually bound". Callers:
761
- * - `getToolsForBinding` (non-event-driven branch)
762
- * - `getEventDrivenToolsForBinding` (appends instance tools alongside
763
- * schema-only definitions)
764
- * - `calculateInstructionTokens` (counts schema bytes for accounting)
765
- *
766
- * In event-driven mode (`toolDefinitions` present) instance tools are
767
- * appended unfiltered; outside event-driven mode they pass through
768
- * `filterToolsForBinding`. Centralizing the decision here prevents the
769
- * accounting/binding paths from drifting apart, which was the root
770
- * cause of the original miscount (Fixes #121).
771
- */
772
- getEffectiveInstanceTools() {
773
- if (!this.tools) {
774
- return undefined;
775
- }
776
- const isEventDriven = (this.toolDefinitions?.length ?? 0) > 0;
777
- if (isEventDriven || !this.toolRegistry) {
778
- return this.tools;
779
- }
780
- return this.filterToolsForBinding(this.tools);
781
- }
782
684
  /**
783
685
  * Calculate tool tokens and add to instruction tokens
784
686
  * Note: System message tokens are calculated during systemRunnable creation
@@ -792,16 +694,9 @@ class AgentContext {
792
694
  const countedToolNames = new Set();
793
695
  // Reset per-tool breakdown
794
696
  this.toolsDetail = [];
795
- /* Use `getEffectiveInstanceTools()` so accounting reflects exactly the
796
- * subset that `getToolsForBinding` would emit — preventing the
797
- * worst-case-ceiling miscount that triggered spurious `empty_messages`
798
- * preflight rejections at low `maxContextTokens`. Deferred and
799
- * non-`'direct'` `toolDefinitions` are excluded by
800
- * `getActiveToolDefinitions()` below. */
801
- const effectiveInstanceTools = this.getEffectiveInstanceTools();
802
697
  // Count tokens for bound tools (StructuredTool instances with .schema)
803
- if (effectiveInstanceTools && effectiveInstanceTools.length > 0) {
804
- for (const tool of effectiveInstanceTools) {
698
+ if (this.tools && this.tools.length > 0) {
699
+ for (const tool of this.tools) {
805
700
  const genericTool = tool;
806
701
  if (genericTool.schema != null &&
807
702
  typeof genericTool.schema === 'object') {
@@ -820,25 +715,23 @@ class AgentContext {
820
715
  // Count tokens for tool definitions (MCP / event-driven tools).
821
716
  // These are sent to the provider API as tool schemas alongside bound tools.
822
717
  // Both can be populated simultaneously (graph tools + MCP tools).
823
- // Use `getActiveToolDefinitions()` so programmatic-only definitions
824
- // (e.g. `allowed_callers: ['code_execution']`) and deferred-and-
825
- // -undiscovered ones don't inflate `toolSchemaTokens` while never
826
- // being bound to the model.
827
- for (const def of this.getActiveToolDefinitions()) {
828
- if (countedToolNames.has(def.name)) {
829
- continue; // Already counted via this.tools
718
+ if (this.toolDefinitions && this.toolDefinitions.length > 0) {
719
+ for (const def of this.toolDefinitions) {
720
+ if (countedToolNames.has(def.name)) {
721
+ continue; // Already counted via this.tools
722
+ }
723
+ const schema = {
724
+ name: def.name,
725
+ description: def.description ?? '',
726
+ parameters: def.parameters ?? {},
727
+ };
728
+ const defTokens = tokenCounter(new messages.SystemMessage(JSON.stringify(schema)));
729
+ this.toolsDetail.push({
730
+ name: def.name || 'unknown',
731
+ tokens: defTokens,
732
+ });
733
+ toolTokens += defTokens;
830
734
  }
831
- const schema = {
832
- name: def.name,
833
- description: def.description ?? '',
834
- parameters: def.parameters ?? {},
835
- };
836
- const defTokens = tokenCounter(new messages.SystemMessage(JSON.stringify(schema)));
837
- this.toolsDetail.push({
838
- name: def.name || 'unknown',
839
- tokens: defTokens,
840
- });
841
- toolTokens += defTokens;
842
735
  }
843
736
  // Store total tool tokens for breakdown reporting
844
737
  this.toolTokensTotal = toolTokens;
@@ -970,7 +863,9 @@ class AgentContext {
970
863
  return this.getEventDrivenToolsForBinding();
971
864
  }
972
865
  /** Traditional mode: filter actual tool instances */
973
- const filtered = this.getEffectiveInstanceTools();
866
+ const filtered = !this.tools || !this.toolRegistry
867
+ ? this.tools
868
+ : this.filterToolsForBinding(this.tools);
974
869
  if (this.graphTools && this.graphTools.length > 0) {
975
870
  return [...(filtered ?? []), ...this.graphTools];
976
871
  }
@@ -981,16 +876,22 @@ class AgentContext {
981
876
  if (!this.toolDefinitions) {
982
877
  return this.graphTools ?? [];
983
878
  }
984
- /* Reuse `getActiveToolDefinitions()` so the binding gate matches the
985
- * accounting filter exactly (single source of truth). */
986
- const schemaTools = schema$1.createSchemaOnlyTools(this.getActiveToolDefinitions());
879
+ const defsToInclude = this.toolDefinitions.filter((def) => {
880
+ const allowedCallers = def.allowed_callers ?? ['direct'];
881
+ if (!allowedCallers.includes('direct')) {
882
+ return false;
883
+ }
884
+ if (def.defer_loading === true &&
885
+ !this.discoveredToolNames.has(def.name)) {
886
+ return false;
887
+ }
888
+ return true;
889
+ });
890
+ const schemaTools = schema$1.createSchemaOnlyTools(defsToInclude);
987
891
  const allTools = [...schemaTools];
988
892
  if (this.graphTools && this.graphTools.length > 0) {
989
893
  allTools.push(...this.graphTools);
990
894
  }
991
- /* In event-driven mode, instance tools are appended UNFILTERED (matching
992
- * `getEffectiveInstanceTools()`'s event-driven branch). Deferred /
993
- * non-direct logic is represented in `toolDefinitions`, not here. */
994
895
  if (this.tools && this.tools.length > 0) {
995
896
  allTools.push(...this.tools);
996
897
  }