@illuma-ai/agents 1.0.81

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 (558) hide show
  1. package/README.md +485 -0
  2. package/dist/cjs/agents/AgentContext.cjs +734 -0
  3. package/dist/cjs/agents/AgentContext.cjs.map +1 -0
  4. package/dist/cjs/common/enum.cjs +190 -0
  5. package/dist/cjs/common/enum.cjs.map +1 -0
  6. package/dist/cjs/events.cjs +172 -0
  7. package/dist/cjs/events.cjs.map +1 -0
  8. package/dist/cjs/graphs/Graph.cjs +1615 -0
  9. package/dist/cjs/graphs/Graph.cjs.map +1 -0
  10. package/dist/cjs/graphs/MultiAgentGraph.cjs +890 -0
  11. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -0
  12. package/dist/cjs/instrumentation.cjs +21 -0
  13. package/dist/cjs/instrumentation.cjs.map +1 -0
  14. package/dist/cjs/llm/anthropic/index.cjs +292 -0
  15. package/dist/cjs/llm/anthropic/index.cjs.map +1 -0
  16. package/dist/cjs/llm/anthropic/types.cjs +50 -0
  17. package/dist/cjs/llm/anthropic/types.cjs.map +1 -0
  18. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +630 -0
  19. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -0
  20. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +218 -0
  21. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -0
  22. package/dist/cjs/llm/anthropic/utils/tools.cjs +29 -0
  23. package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -0
  24. package/dist/cjs/llm/bedrock/index.cjs +282 -0
  25. package/dist/cjs/llm/bedrock/index.cjs.map +1 -0
  26. package/dist/cjs/llm/fake.cjs +97 -0
  27. package/dist/cjs/llm/fake.cjs.map +1 -0
  28. package/dist/cjs/llm/google/index.cjs +216 -0
  29. package/dist/cjs/llm/google/index.cjs.map +1 -0
  30. package/dist/cjs/llm/google/utils/common.cjs +647 -0
  31. package/dist/cjs/llm/google/utils/common.cjs.map +1 -0
  32. package/dist/cjs/llm/openai/index.cjs +1028 -0
  33. package/dist/cjs/llm/openai/index.cjs.map +1 -0
  34. package/dist/cjs/llm/openai/utils/index.cjs +765 -0
  35. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -0
  36. package/dist/cjs/llm/openrouter/index.cjs +212 -0
  37. package/dist/cjs/llm/openrouter/index.cjs.map +1 -0
  38. package/dist/cjs/llm/providers.cjs +43 -0
  39. package/dist/cjs/llm/providers.cjs.map +1 -0
  40. package/dist/cjs/llm/text.cjs +69 -0
  41. package/dist/cjs/llm/text.cjs.map +1 -0
  42. package/dist/cjs/llm/vertexai/index.cjs +329 -0
  43. package/dist/cjs/llm/vertexai/index.cjs.map +1 -0
  44. package/dist/cjs/main.cjs +240 -0
  45. package/dist/cjs/main.cjs.map +1 -0
  46. package/dist/cjs/messages/cache.cjs +387 -0
  47. package/dist/cjs/messages/cache.cjs.map +1 -0
  48. package/dist/cjs/messages/content.cjs +53 -0
  49. package/dist/cjs/messages/content.cjs.map +1 -0
  50. package/dist/cjs/messages/core.cjs +367 -0
  51. package/dist/cjs/messages/core.cjs.map +1 -0
  52. package/dist/cjs/messages/format.cjs +761 -0
  53. package/dist/cjs/messages/format.cjs.map +1 -0
  54. package/dist/cjs/messages/ids.cjs +23 -0
  55. package/dist/cjs/messages/ids.cjs.map +1 -0
  56. package/dist/cjs/messages/prune.cjs +398 -0
  57. package/dist/cjs/messages/prune.cjs.map +1 -0
  58. package/dist/cjs/messages/tools.cjs +96 -0
  59. package/dist/cjs/messages/tools.cjs.map +1 -0
  60. package/dist/cjs/run.cjs +328 -0
  61. package/dist/cjs/run.cjs.map +1 -0
  62. package/dist/cjs/schemas/validate.cjs +324 -0
  63. package/dist/cjs/schemas/validate.cjs.map +1 -0
  64. package/dist/cjs/splitStream.cjs +210 -0
  65. package/dist/cjs/splitStream.cjs.map +1 -0
  66. package/dist/cjs/stream.cjs +620 -0
  67. package/dist/cjs/stream.cjs.map +1 -0
  68. package/dist/cjs/tools/BrowserTools.cjs +248 -0
  69. package/dist/cjs/tools/BrowserTools.cjs.map +1 -0
  70. package/dist/cjs/tools/Calculator.cjs +66 -0
  71. package/dist/cjs/tools/Calculator.cjs.map +1 -0
  72. package/dist/cjs/tools/CodeExecutor.cjs +234 -0
  73. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -0
  74. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +636 -0
  75. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -0
  76. package/dist/cjs/tools/ToolNode.cjs +548 -0
  77. package/dist/cjs/tools/ToolNode.cjs.map +1 -0
  78. package/dist/cjs/tools/ToolSearch.cjs +909 -0
  79. package/dist/cjs/tools/ToolSearch.cjs.map +1 -0
  80. package/dist/cjs/tools/handlers.cjs +255 -0
  81. package/dist/cjs/tools/handlers.cjs.map +1 -0
  82. package/dist/cjs/tools/schema.cjs +31 -0
  83. package/dist/cjs/tools/schema.cjs.map +1 -0
  84. package/dist/cjs/tools/search/anthropic.cjs +40 -0
  85. package/dist/cjs/tools/search/anthropic.cjs.map +1 -0
  86. package/dist/cjs/tools/search/content.cjs +140 -0
  87. package/dist/cjs/tools/search/content.cjs.map +1 -0
  88. package/dist/cjs/tools/search/firecrawl.cjs +179 -0
  89. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -0
  90. package/dist/cjs/tools/search/format.cjs +203 -0
  91. package/dist/cjs/tools/search/format.cjs.map +1 -0
  92. package/dist/cjs/tools/search/highlights.cjs +245 -0
  93. package/dist/cjs/tools/search/highlights.cjs.map +1 -0
  94. package/dist/cjs/tools/search/rerankers.cjs +174 -0
  95. package/dist/cjs/tools/search/rerankers.cjs.map +1 -0
  96. package/dist/cjs/tools/search/schema.cjs +117 -0
  97. package/dist/cjs/tools/search/schema.cjs.map +1 -0
  98. package/dist/cjs/tools/search/search.cjs +566 -0
  99. package/dist/cjs/tools/search/search.cjs.map +1 -0
  100. package/dist/cjs/tools/search/serper-scraper.cjs +132 -0
  101. package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -0
  102. package/dist/cjs/tools/search/tool.cjs +456 -0
  103. package/dist/cjs/tools/search/tool.cjs.map +1 -0
  104. package/dist/cjs/tools/search/utils.cjs +66 -0
  105. package/dist/cjs/tools/search/utils.cjs.map +1 -0
  106. package/dist/cjs/types/graph.cjs +29 -0
  107. package/dist/cjs/types/graph.cjs.map +1 -0
  108. package/dist/cjs/utils/contextAnalytics.cjs +66 -0
  109. package/dist/cjs/utils/contextAnalytics.cjs.map +1 -0
  110. package/dist/cjs/utils/events.cjs +31 -0
  111. package/dist/cjs/utils/events.cjs.map +1 -0
  112. package/dist/cjs/utils/graph.cjs +16 -0
  113. package/dist/cjs/utils/graph.cjs.map +1 -0
  114. package/dist/cjs/utils/handlers.cjs +70 -0
  115. package/dist/cjs/utils/handlers.cjs.map +1 -0
  116. package/dist/cjs/utils/llm.cjs +27 -0
  117. package/dist/cjs/utils/llm.cjs.map +1 -0
  118. package/dist/cjs/utils/misc.cjs +56 -0
  119. package/dist/cjs/utils/misc.cjs.map +1 -0
  120. package/dist/cjs/utils/run.cjs +73 -0
  121. package/dist/cjs/utils/run.cjs.map +1 -0
  122. package/dist/cjs/utils/schema.cjs +27 -0
  123. package/dist/cjs/utils/schema.cjs.map +1 -0
  124. package/dist/cjs/utils/title.cjs +125 -0
  125. package/dist/cjs/utils/title.cjs.map +1 -0
  126. package/dist/cjs/utils/tokens.cjs +125 -0
  127. package/dist/cjs/utils/tokens.cjs.map +1 -0
  128. package/dist/cjs/utils/toonFormat.cjs +388 -0
  129. package/dist/cjs/utils/toonFormat.cjs.map +1 -0
  130. package/dist/esm/agents/AgentContext.mjs +732 -0
  131. package/dist/esm/agents/AgentContext.mjs.map +1 -0
  132. package/dist/esm/common/enum.mjs +190 -0
  133. package/dist/esm/common/enum.mjs.map +1 -0
  134. package/dist/esm/events.mjs +164 -0
  135. package/dist/esm/events.mjs.map +1 -0
  136. package/dist/esm/graphs/Graph.mjs +1612 -0
  137. package/dist/esm/graphs/Graph.mjs.map +1 -0
  138. package/dist/esm/graphs/MultiAgentGraph.mjs +888 -0
  139. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -0
  140. package/dist/esm/instrumentation.mjs +19 -0
  141. package/dist/esm/instrumentation.mjs.map +1 -0
  142. package/dist/esm/llm/anthropic/index.mjs +290 -0
  143. package/dist/esm/llm/anthropic/index.mjs.map +1 -0
  144. package/dist/esm/llm/anthropic/types.mjs +48 -0
  145. package/dist/esm/llm/anthropic/types.mjs.map +1 -0
  146. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +627 -0
  147. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -0
  148. package/dist/esm/llm/anthropic/utils/message_outputs.mjs +216 -0
  149. package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -0
  150. package/dist/esm/llm/anthropic/utils/tools.mjs +27 -0
  151. package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -0
  152. package/dist/esm/llm/bedrock/index.mjs +280 -0
  153. package/dist/esm/llm/bedrock/index.mjs.map +1 -0
  154. package/dist/esm/llm/fake.mjs +94 -0
  155. package/dist/esm/llm/fake.mjs.map +1 -0
  156. package/dist/esm/llm/google/index.mjs +214 -0
  157. package/dist/esm/llm/google/index.mjs.map +1 -0
  158. package/dist/esm/llm/google/utils/common.mjs +638 -0
  159. package/dist/esm/llm/google/utils/common.mjs.map +1 -0
  160. package/dist/esm/llm/openai/index.mjs +1018 -0
  161. package/dist/esm/llm/openai/index.mjs.map +1 -0
  162. package/dist/esm/llm/openai/utils/index.mjs +759 -0
  163. package/dist/esm/llm/openai/utils/index.mjs.map +1 -0
  164. package/dist/esm/llm/openrouter/index.mjs +210 -0
  165. package/dist/esm/llm/openrouter/index.mjs.map +1 -0
  166. package/dist/esm/llm/providers.mjs +39 -0
  167. package/dist/esm/llm/providers.mjs.map +1 -0
  168. package/dist/esm/llm/text.mjs +67 -0
  169. package/dist/esm/llm/text.mjs.map +1 -0
  170. package/dist/esm/llm/vertexai/index.mjs +327 -0
  171. package/dist/esm/llm/vertexai/index.mjs.map +1 -0
  172. package/dist/esm/main.mjs +37 -0
  173. package/dist/esm/main.mjs.map +1 -0
  174. package/dist/esm/messages/cache.mjs +382 -0
  175. package/dist/esm/messages/cache.mjs.map +1 -0
  176. package/dist/esm/messages/content.mjs +51 -0
  177. package/dist/esm/messages/content.mjs.map +1 -0
  178. package/dist/esm/messages/core.mjs +359 -0
  179. package/dist/esm/messages/core.mjs.map +1 -0
  180. package/dist/esm/messages/format.mjs +752 -0
  181. package/dist/esm/messages/format.mjs.map +1 -0
  182. package/dist/esm/messages/ids.mjs +21 -0
  183. package/dist/esm/messages/ids.mjs.map +1 -0
  184. package/dist/esm/messages/prune.mjs +393 -0
  185. package/dist/esm/messages/prune.mjs.map +1 -0
  186. package/dist/esm/messages/tools.mjs +93 -0
  187. package/dist/esm/messages/tools.mjs.map +1 -0
  188. package/dist/esm/run.mjs +325 -0
  189. package/dist/esm/run.mjs.map +1 -0
  190. package/dist/esm/schemas/validate.mjs +317 -0
  191. package/dist/esm/schemas/validate.mjs.map +1 -0
  192. package/dist/esm/splitStream.mjs +207 -0
  193. package/dist/esm/splitStream.mjs.map +1 -0
  194. package/dist/esm/stream.mjs +616 -0
  195. package/dist/esm/stream.mjs.map +1 -0
  196. package/dist/esm/tools/BrowserTools.mjs +244 -0
  197. package/dist/esm/tools/BrowserTools.mjs.map +1 -0
  198. package/dist/esm/tools/Calculator.mjs +41 -0
  199. package/dist/esm/tools/Calculator.mjs.map +1 -0
  200. package/dist/esm/tools/CodeExecutor.mjs +226 -0
  201. package/dist/esm/tools/CodeExecutor.mjs.map +1 -0
  202. package/dist/esm/tools/ProgrammaticToolCalling.mjs +622 -0
  203. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -0
  204. package/dist/esm/tools/ToolNode.mjs +545 -0
  205. package/dist/esm/tools/ToolNode.mjs.map +1 -0
  206. package/dist/esm/tools/ToolSearch.mjs +870 -0
  207. package/dist/esm/tools/ToolSearch.mjs.map +1 -0
  208. package/dist/esm/tools/handlers.mjs +250 -0
  209. package/dist/esm/tools/handlers.mjs.map +1 -0
  210. package/dist/esm/tools/schema.mjs +28 -0
  211. package/dist/esm/tools/schema.mjs.map +1 -0
  212. package/dist/esm/tools/search/anthropic.mjs +37 -0
  213. package/dist/esm/tools/search/anthropic.mjs.map +1 -0
  214. package/dist/esm/tools/search/content.mjs +119 -0
  215. package/dist/esm/tools/search/content.mjs.map +1 -0
  216. package/dist/esm/tools/search/firecrawl.mjs +176 -0
  217. package/dist/esm/tools/search/firecrawl.mjs.map +1 -0
  218. package/dist/esm/tools/search/format.mjs +201 -0
  219. package/dist/esm/tools/search/format.mjs.map +1 -0
  220. package/dist/esm/tools/search/highlights.mjs +243 -0
  221. package/dist/esm/tools/search/highlights.mjs.map +1 -0
  222. package/dist/esm/tools/search/rerankers.mjs +168 -0
  223. package/dist/esm/tools/search/rerankers.mjs.map +1 -0
  224. package/dist/esm/tools/search/schema.mjs +104 -0
  225. package/dist/esm/tools/search/schema.mjs.map +1 -0
  226. package/dist/esm/tools/search/search.mjs +563 -0
  227. package/dist/esm/tools/search/search.mjs.map +1 -0
  228. package/dist/esm/tools/search/serper-scraper.mjs +129 -0
  229. package/dist/esm/tools/search/serper-scraper.mjs.map +1 -0
  230. package/dist/esm/tools/search/tool.mjs +454 -0
  231. package/dist/esm/tools/search/tool.mjs.map +1 -0
  232. package/dist/esm/tools/search/utils.mjs +61 -0
  233. package/dist/esm/tools/search/utils.mjs.map +1 -0
  234. package/dist/esm/types/graph.mjs +26 -0
  235. package/dist/esm/types/graph.mjs.map +1 -0
  236. package/dist/esm/utils/contextAnalytics.mjs +64 -0
  237. package/dist/esm/utils/contextAnalytics.mjs.map +1 -0
  238. package/dist/esm/utils/events.mjs +29 -0
  239. package/dist/esm/utils/events.mjs.map +1 -0
  240. package/dist/esm/utils/graph.mjs +13 -0
  241. package/dist/esm/utils/graph.mjs.map +1 -0
  242. package/dist/esm/utils/handlers.mjs +68 -0
  243. package/dist/esm/utils/handlers.mjs.map +1 -0
  244. package/dist/esm/utils/llm.mjs +24 -0
  245. package/dist/esm/utils/llm.mjs.map +1 -0
  246. package/dist/esm/utils/misc.mjs +53 -0
  247. package/dist/esm/utils/misc.mjs.map +1 -0
  248. package/dist/esm/utils/run.mjs +70 -0
  249. package/dist/esm/utils/run.mjs.map +1 -0
  250. package/dist/esm/utils/schema.mjs +24 -0
  251. package/dist/esm/utils/schema.mjs.map +1 -0
  252. package/dist/esm/utils/title.mjs +122 -0
  253. package/dist/esm/utils/title.mjs.map +1 -0
  254. package/dist/esm/utils/tokens.mjs +121 -0
  255. package/dist/esm/utils/tokens.mjs.map +1 -0
  256. package/dist/esm/utils/toonFormat.mjs +381 -0
  257. package/dist/esm/utils/toonFormat.mjs.map +1 -0
  258. package/dist/types/agents/AgentContext.d.ts +293 -0
  259. package/dist/types/common/enum.d.ts +155 -0
  260. package/dist/types/common/index.d.ts +1 -0
  261. package/dist/types/events.d.ts +31 -0
  262. package/dist/types/graphs/Graph.d.ts +216 -0
  263. package/dist/types/graphs/MultiAgentGraph.d.ts +104 -0
  264. package/dist/types/graphs/index.d.ts +2 -0
  265. package/dist/types/index.d.ts +21 -0
  266. package/dist/types/instrumentation.d.ts +1 -0
  267. package/dist/types/llm/anthropic/index.d.ts +39 -0
  268. package/dist/types/llm/anthropic/types.d.ts +37 -0
  269. package/dist/types/llm/anthropic/utils/message_inputs.d.ts +14 -0
  270. package/dist/types/llm/anthropic/utils/message_outputs.d.ts +14 -0
  271. package/dist/types/llm/anthropic/utils/output_parsers.d.ts +22 -0
  272. package/dist/types/llm/anthropic/utils/tools.d.ts +3 -0
  273. package/dist/types/llm/bedrock/index.d.ts +141 -0
  274. package/dist/types/llm/bedrock/types.d.ts +27 -0
  275. package/dist/types/llm/bedrock/utils/index.d.ts +5 -0
  276. package/dist/types/llm/bedrock/utils/message_inputs.d.ts +31 -0
  277. package/dist/types/llm/bedrock/utils/message_outputs.d.ts +33 -0
  278. package/dist/types/llm/fake.d.ts +31 -0
  279. package/dist/types/llm/google/index.d.ts +24 -0
  280. package/dist/types/llm/google/types.d.ts +42 -0
  281. package/dist/types/llm/google/utils/common.d.ts +34 -0
  282. package/dist/types/llm/google/utils/tools.d.ts +10 -0
  283. package/dist/types/llm/google/utils/zod_to_genai_parameters.d.ts +14 -0
  284. package/dist/types/llm/openai/index.d.ts +127 -0
  285. package/dist/types/llm/openai/types.d.ts +10 -0
  286. package/dist/types/llm/openai/utils/index.d.ts +29 -0
  287. package/dist/types/llm/openrouter/index.d.ts +15 -0
  288. package/dist/types/llm/providers.d.ts +5 -0
  289. package/dist/types/llm/text.d.ts +21 -0
  290. package/dist/types/llm/vertexai/index.d.ts +293 -0
  291. package/dist/types/messages/cache.d.ts +54 -0
  292. package/dist/types/messages/content.d.ts +7 -0
  293. package/dist/types/messages/core.d.ts +14 -0
  294. package/dist/types/messages/format.d.ts +137 -0
  295. package/dist/types/messages/ids.d.ts +3 -0
  296. package/dist/types/messages/index.d.ts +7 -0
  297. package/dist/types/messages/prune.d.ts +52 -0
  298. package/dist/types/messages/reducer.d.ts +9 -0
  299. package/dist/types/messages/tools.d.ts +17 -0
  300. package/dist/types/mockStream.d.ts +32 -0
  301. package/dist/types/prompts/collab.d.ts +1 -0
  302. package/dist/types/prompts/index.d.ts +2 -0
  303. package/dist/types/prompts/taskmanager.d.ts +41 -0
  304. package/dist/types/run.d.ts +41 -0
  305. package/dist/types/schemas/index.d.ts +1 -0
  306. package/dist/types/schemas/validate.d.ts +59 -0
  307. package/dist/types/splitStream.d.ts +37 -0
  308. package/dist/types/stream.d.ts +15 -0
  309. package/dist/types/test/mockTools.d.ts +28 -0
  310. package/dist/types/tools/BrowserTools.d.ts +87 -0
  311. package/dist/types/tools/Calculator.d.ts +34 -0
  312. package/dist/types/tools/CodeExecutor.d.ts +57 -0
  313. package/dist/types/tools/ProgrammaticToolCalling.d.ts +138 -0
  314. package/dist/types/tools/ToolNode.d.ts +51 -0
  315. package/dist/types/tools/ToolSearch.d.ts +219 -0
  316. package/dist/types/tools/handlers.d.ts +22 -0
  317. package/dist/types/tools/schema.d.ts +12 -0
  318. package/dist/types/tools/search/anthropic.d.ts +16 -0
  319. package/dist/types/tools/search/content.d.ts +4 -0
  320. package/dist/types/tools/search/firecrawl.d.ts +54 -0
  321. package/dist/types/tools/search/format.d.ts +5 -0
  322. package/dist/types/tools/search/highlights.d.ts +13 -0
  323. package/dist/types/tools/search/index.d.ts +3 -0
  324. package/dist/types/tools/search/rerankers.d.ts +38 -0
  325. package/dist/types/tools/search/schema.d.ts +103 -0
  326. package/dist/types/tools/search/search.d.ts +8 -0
  327. package/dist/types/tools/search/serper-scraper.d.ts +59 -0
  328. package/dist/types/tools/search/test.d.ts +1 -0
  329. package/dist/types/tools/search/tool.d.ts +3 -0
  330. package/dist/types/tools/search/types.d.ts +575 -0
  331. package/dist/types/tools/search/utils.d.ts +10 -0
  332. package/dist/types/types/graph.d.ts +399 -0
  333. package/dist/types/types/index.d.ts +5 -0
  334. package/dist/types/types/llm.d.ts +105 -0
  335. package/dist/types/types/messages.d.ts +4 -0
  336. package/dist/types/types/run.d.ts +112 -0
  337. package/dist/types/types/stream.d.ts +308 -0
  338. package/dist/types/types/tools.d.ts +296 -0
  339. package/dist/types/utils/contextAnalytics.d.ts +37 -0
  340. package/dist/types/utils/events.d.ts +6 -0
  341. package/dist/types/utils/graph.d.ts +2 -0
  342. package/dist/types/utils/handlers.d.ts +34 -0
  343. package/dist/types/utils/index.d.ts +9 -0
  344. package/dist/types/utils/llm.d.ts +3 -0
  345. package/dist/types/utils/llmConfig.d.ts +3 -0
  346. package/dist/types/utils/logging.d.ts +1 -0
  347. package/dist/types/utils/misc.d.ts +7 -0
  348. package/dist/types/utils/run.d.ts +27 -0
  349. package/dist/types/utils/schema.d.ts +8 -0
  350. package/dist/types/utils/title.d.ts +4 -0
  351. package/dist/types/utils/tokens.d.ts +28 -0
  352. package/dist/types/utils/toonFormat.d.ts +111 -0
  353. package/package.json +190 -0
  354. package/src/agents/AgentContext.test.ts +458 -0
  355. package/src/agents/AgentContext.ts +972 -0
  356. package/src/agents/__tests__/AgentContext.test.ts +805 -0
  357. package/src/agents/__tests__/resolveStructuredOutputMode.test.ts +137 -0
  358. package/src/common/enum.ts +203 -0
  359. package/src/common/index.ts +2 -0
  360. package/src/events.ts +223 -0
  361. package/src/graphs/Graph.ts +2228 -0
  362. package/src/graphs/MultiAgentGraph.ts +1063 -0
  363. package/src/graphs/__tests__/structured-output.integration.test.ts +809 -0
  364. package/src/graphs/__tests__/structured-output.test.ts +183 -0
  365. package/src/graphs/index.ts +2 -0
  366. package/src/index.ts +34 -0
  367. package/src/instrumentation.ts +22 -0
  368. package/src/llm/anthropic/Jacob_Lee_Resume_2023.pdf +0 -0
  369. package/src/llm/anthropic/index.ts +413 -0
  370. package/src/llm/anthropic/llm.spec.ts +1442 -0
  371. package/src/llm/anthropic/types.ts +140 -0
  372. package/src/llm/anthropic/utils/message_inputs.ts +757 -0
  373. package/src/llm/anthropic/utils/message_outputs.ts +289 -0
  374. package/src/llm/anthropic/utils/output_parsers.ts +133 -0
  375. package/src/llm/anthropic/utils/tools.ts +29 -0
  376. package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +495 -0
  377. package/src/llm/bedrock/index.ts +411 -0
  378. package/src/llm/bedrock/llm.spec.ts +616 -0
  379. package/src/llm/bedrock/types.ts +51 -0
  380. package/src/llm/bedrock/utils/index.ts +18 -0
  381. package/src/llm/bedrock/utils/message_inputs.ts +563 -0
  382. package/src/llm/bedrock/utils/message_outputs.ts +310 -0
  383. package/src/llm/fake.ts +133 -0
  384. package/src/llm/google/data/gettysburg10.wav +0 -0
  385. package/src/llm/google/data/hotdog.jpg +0 -0
  386. package/src/llm/google/index.ts +337 -0
  387. package/src/llm/google/llm.spec.ts +934 -0
  388. package/src/llm/google/types.ts +56 -0
  389. package/src/llm/google/utils/common.ts +873 -0
  390. package/src/llm/google/utils/tools.ts +160 -0
  391. package/src/llm/google/utils/zod_to_genai_parameters.ts +86 -0
  392. package/src/llm/openai/index.ts +1366 -0
  393. package/src/llm/openai/types.ts +24 -0
  394. package/src/llm/openai/utils/index.ts +1035 -0
  395. package/src/llm/openai/utils/isReasoningModel.test.ts +90 -0
  396. package/src/llm/openrouter/index.ts +291 -0
  397. package/src/llm/providers.ts +52 -0
  398. package/src/llm/text.ts +94 -0
  399. package/src/llm/vertexai/index.ts +359 -0
  400. package/src/messages/__tests__/tools.test.ts +473 -0
  401. package/src/messages/cache.test.ts +1261 -0
  402. package/src/messages/cache.ts +518 -0
  403. package/src/messages/content.test.ts +362 -0
  404. package/src/messages/content.ts +63 -0
  405. package/src/messages/core.ts +473 -0
  406. package/src/messages/ensureThinkingBlock.test.ts +468 -0
  407. package/src/messages/format.ts +1029 -0
  408. package/src/messages/formatAgentMessages.test.ts +1513 -0
  409. package/src/messages/formatAgentMessages.tools.test.ts +419 -0
  410. package/src/messages/formatMessage.test.ts +693 -0
  411. package/src/messages/ids.ts +26 -0
  412. package/src/messages/index.ts +7 -0
  413. package/src/messages/labelContentByAgent.test.ts +887 -0
  414. package/src/messages/prune.ts +568 -0
  415. package/src/messages/reducer.ts +80 -0
  416. package/src/messages/shiftIndexTokenCountMap.test.ts +81 -0
  417. package/src/messages/tools.ts +108 -0
  418. package/src/mockStream.ts +99 -0
  419. package/src/prompts/collab.ts +6 -0
  420. package/src/prompts/index.ts +2 -0
  421. package/src/prompts/taskmanager.ts +61 -0
  422. package/src/run.ts +467 -0
  423. package/src/schemas/index.ts +2 -0
  424. package/src/schemas/schema-preparation.test.ts +500 -0
  425. package/src/schemas/validate.test.ts +358 -0
  426. package/src/schemas/validate.ts +454 -0
  427. package/src/scripts/abort.ts +157 -0
  428. package/src/scripts/ant_web_search.ts +158 -0
  429. package/src/scripts/ant_web_search_edge_case.ts +162 -0
  430. package/src/scripts/ant_web_search_error_edge_case.ts +148 -0
  431. package/src/scripts/args.ts +48 -0
  432. package/src/scripts/caching.ts +132 -0
  433. package/src/scripts/cli.ts +172 -0
  434. package/src/scripts/cli2.ts +133 -0
  435. package/src/scripts/cli3.ts +184 -0
  436. package/src/scripts/cli4.ts +191 -0
  437. package/src/scripts/cli5.ts +191 -0
  438. package/src/scripts/code_exec.ts +213 -0
  439. package/src/scripts/code_exec_files.ts +236 -0
  440. package/src/scripts/code_exec_multi_session.ts +241 -0
  441. package/src/scripts/code_exec_ptc.ts +334 -0
  442. package/src/scripts/code_exec_session.ts +282 -0
  443. package/src/scripts/code_exec_simple.ts +147 -0
  444. package/src/scripts/content.ts +138 -0
  445. package/src/scripts/empty_input.ts +137 -0
  446. package/src/scripts/handoff-test.ts +135 -0
  447. package/src/scripts/image.ts +178 -0
  448. package/src/scripts/memory.ts +97 -0
  449. package/src/scripts/multi-agent-chain.ts +331 -0
  450. package/src/scripts/multi-agent-conditional.ts +221 -0
  451. package/src/scripts/multi-agent-document-review-chain.ts +197 -0
  452. package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
  453. package/src/scripts/multi-agent-parallel-start.ts +265 -0
  454. package/src/scripts/multi-agent-parallel.ts +394 -0
  455. package/src/scripts/multi-agent-sequence.ts +217 -0
  456. package/src/scripts/multi-agent-supervisor.ts +365 -0
  457. package/src/scripts/multi-agent-test.ts +186 -0
  458. package/src/scripts/parallel-asymmetric-tools-test.ts +274 -0
  459. package/src/scripts/parallel-full-metadata-test.ts +240 -0
  460. package/src/scripts/parallel-tools-test.ts +340 -0
  461. package/src/scripts/programmatic_exec.ts +396 -0
  462. package/src/scripts/programmatic_exec_agent.ts +231 -0
  463. package/src/scripts/search.ts +146 -0
  464. package/src/scripts/sequential-full-metadata-test.ts +197 -0
  465. package/src/scripts/simple.ts +225 -0
  466. package/src/scripts/single-agent-metadata-test.ts +198 -0
  467. package/src/scripts/stream.ts +140 -0
  468. package/src/scripts/test-custom-prompt-key.ts +145 -0
  469. package/src/scripts/test-handoff-input.ts +170 -0
  470. package/src/scripts/test-handoff-preamble.ts +277 -0
  471. package/src/scripts/test-multi-agent-list-handoff.ts +417 -0
  472. package/src/scripts/test-parallel-agent-labeling.ts +325 -0
  473. package/src/scripts/test-parallel-handoffs.ts +291 -0
  474. package/src/scripts/test-thinking-handoff-bedrock.ts +153 -0
  475. package/src/scripts/test-thinking-handoff.ts +155 -0
  476. package/src/scripts/test-tools-before-handoff.ts +226 -0
  477. package/src/scripts/test_code_api.ts +361 -0
  478. package/src/scripts/thinking-bedrock.ts +159 -0
  479. package/src/scripts/thinking.ts +171 -0
  480. package/src/scripts/tool_search.ts +162 -0
  481. package/src/scripts/tools.ts +177 -0
  482. package/src/specs/agent-handoffs.test.ts +888 -0
  483. package/src/specs/anthropic.simple.test.ts +387 -0
  484. package/src/specs/azure.simple.test.ts +364 -0
  485. package/src/specs/cache.simple.test.ts +396 -0
  486. package/src/specs/deepseek.simple.test.ts +283 -0
  487. package/src/specs/emergency-prune.test.ts +407 -0
  488. package/src/specs/moonshot.simple.test.ts +358 -0
  489. package/src/specs/openai.simple.test.ts +311 -0
  490. package/src/specs/openrouter.simple.test.ts +107 -0
  491. package/src/specs/prune.test.ts +901 -0
  492. package/src/specs/reasoning.test.ts +201 -0
  493. package/src/specs/spec.utils.ts +3 -0
  494. package/src/specs/thinking-handoff.test.ts +620 -0
  495. package/src/specs/thinking-prune.test.ts +703 -0
  496. package/src/specs/token-distribution-edge-case.test.ts +316 -0
  497. package/src/specs/token-memoization.test.ts +32 -0
  498. package/src/specs/tool-error.test.ts +198 -0
  499. package/src/splitStream.test.ts +691 -0
  500. package/src/splitStream.ts +234 -0
  501. package/src/stream.test.ts +94 -0
  502. package/src/stream.ts +801 -0
  503. package/src/test/mockTools.ts +386 -0
  504. package/src/tools/BrowserTools.ts +393 -0
  505. package/src/tools/Calculator.test.ts +278 -0
  506. package/src/tools/Calculator.ts +46 -0
  507. package/src/tools/CodeExecutor.ts +270 -0
  508. package/src/tools/ProgrammaticToolCalling.ts +785 -0
  509. package/src/tools/ToolNode.ts +674 -0
  510. package/src/tools/ToolSearch.ts +1095 -0
  511. package/src/tools/__tests__/BrowserTools.test.ts +265 -0
  512. package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +319 -0
  513. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +1006 -0
  514. package/src/tools/__tests__/ToolSearch.integration.test.ts +162 -0
  515. package/src/tools/__tests__/ToolSearch.test.ts +1003 -0
  516. package/src/tools/handlers.ts +363 -0
  517. package/src/tools/schema.ts +37 -0
  518. package/src/tools/search/anthropic.ts +51 -0
  519. package/src/tools/search/content.test.ts +173 -0
  520. package/src/tools/search/content.ts +147 -0
  521. package/src/tools/search/firecrawl.ts +210 -0
  522. package/src/tools/search/format.ts +250 -0
  523. package/src/tools/search/highlights.ts +320 -0
  524. package/src/tools/search/index.ts +3 -0
  525. package/src/tools/search/jina-reranker.test.ts +130 -0
  526. package/src/tools/search/output.md +2775 -0
  527. package/src/tools/search/rerankers.ts +242 -0
  528. package/src/tools/search/schema.ts +113 -0
  529. package/src/tools/search/search.ts +768 -0
  530. package/src/tools/search/serper-scraper.ts +155 -0
  531. package/src/tools/search/test.html +884 -0
  532. package/src/tools/search/test.md +643 -0
  533. package/src/tools/search/test.ts +159 -0
  534. package/src/tools/search/tool.ts +657 -0
  535. package/src/tools/search/types.ts +665 -0
  536. package/src/tools/search/utils.ts +79 -0
  537. package/src/types/graph.test.ts +218 -0
  538. package/src/types/graph.ts +533 -0
  539. package/src/types/index.ts +6 -0
  540. package/src/types/llm.ts +140 -0
  541. package/src/types/messages.ts +4 -0
  542. package/src/types/run.ts +128 -0
  543. package/src/types/stream.ts +417 -0
  544. package/src/types/tools.ts +355 -0
  545. package/src/utils/contextAnalytics.ts +103 -0
  546. package/src/utils/events.ts +32 -0
  547. package/src/utils/graph.ts +11 -0
  548. package/src/utils/handlers.ts +107 -0
  549. package/src/utils/index.ts +9 -0
  550. package/src/utils/llm.ts +26 -0
  551. package/src/utils/llmConfig.ts +208 -0
  552. package/src/utils/logging.ts +48 -0
  553. package/src/utils/misc.ts +57 -0
  554. package/src/utils/run.ts +106 -0
  555. package/src/utils/schema.ts +35 -0
  556. package/src/utils/title.ts +177 -0
  557. package/src/utils/tokens.ts +142 -0
  558. package/src/utils/toonFormat.ts +475 -0
@@ -0,0 +1,972 @@
1
+ /* eslint-disable no-console */
2
+ // src/agents/AgentContext.ts
3
+ import { SystemMessage } from '@langchain/core/messages';
4
+ import { RunnableLambda } from '@langchain/core/runnables';
5
+ import type {
6
+ UsageMetadata,
7
+ BaseMessage,
8
+ BaseMessageFields,
9
+ } from '@langchain/core/messages';
10
+ import type { RunnableConfig, Runnable } from '@langchain/core/runnables';
11
+ import type * as t from '@/types';
12
+ import type { createPruneMessages } from '@/messages';
13
+ import { createSchemaOnlyTools } from '@/tools/schema';
14
+ import { ContentTypes, Providers } from '@/common';
15
+ import { toJsonSchema } from '@/utils/schema';
16
+
17
+ /**
18
+ * Encapsulates agent-specific state that can vary between agents in a multi-agent system
19
+ */
20
+ export class AgentContext {
21
+ /**
22
+ * Create an AgentContext from configuration with token accounting initialization
23
+ */
24
+ static fromConfig(
25
+ agentConfig: t.AgentInputs,
26
+ tokenCounter?: t.TokenCounter,
27
+ indexTokenCountMap?: Record<string, number>
28
+ ): AgentContext {
29
+ const {
30
+ agentId,
31
+ name,
32
+ provider,
33
+ clientOptions,
34
+ tools,
35
+ toolMap,
36
+ toolEnd,
37
+ toolRegistry,
38
+ toolDefinitions,
39
+ instructions,
40
+ additional_instructions,
41
+ streamBuffer,
42
+ maxContextTokens,
43
+ reasoningKey,
44
+ useLegacyContent,
45
+ dynamicContext,
46
+ structuredOutput: structuredOutputCamel,
47
+
48
+ structured_output: structuredOutputSnake,
49
+ discoveredTools,
50
+ summarizeCallback,
51
+ } = agentConfig;
52
+
53
+ // Normalize structured output: support both camelCase and snake_case inputs
54
+ // Priority: structuredOutput (camelCase) > structured_output (snake_case with enabled check)
55
+ let structuredOutput: t.StructuredOutputConfig | undefined;
56
+ if (structuredOutputCamel) {
57
+ structuredOutput = structuredOutputCamel;
58
+ } else if (structuredOutputSnake?.enabled && structuredOutputSnake.schema) {
59
+ // Convert snake_case input to StructuredOutputConfig
60
+ structuredOutput = {
61
+ schema: structuredOutputSnake.schema,
62
+ name: structuredOutputSnake.name,
63
+ description: structuredOutputSnake.description,
64
+ mode: structuredOutputSnake.mode,
65
+ strict: structuredOutputSnake.strict,
66
+ };
67
+ }
68
+
69
+ const agentContext = new AgentContext({
70
+ agentId,
71
+ name: name ?? agentId,
72
+ provider,
73
+ clientOptions,
74
+ maxContextTokens,
75
+ streamBuffer,
76
+ tools,
77
+ toolMap,
78
+ toolRegistry,
79
+ toolDefinitions,
80
+ instructions,
81
+ additionalInstructions: additional_instructions,
82
+ reasoningKey,
83
+ toolEnd,
84
+ instructionTokens: 0,
85
+ tokenCounter,
86
+ useLegacyContent,
87
+ dynamicContext,
88
+ structuredOutput,
89
+ discoveredTools,
90
+ });
91
+
92
+ if (summarizeCallback) {
93
+ agentContext.summarizeCallback = summarizeCallback;
94
+ }
95
+
96
+ if (tokenCounter) {
97
+ // Initialize system runnable BEFORE async tool token calculation
98
+ // This ensures system message tokens are in instructionTokens before
99
+ // updateTokenMapWithInstructions is called
100
+ agentContext.initializeSystemRunnable();
101
+
102
+ const tokenMap = indexTokenCountMap || {};
103
+ agentContext.indexTokenCountMap = tokenMap;
104
+ agentContext.tokenCalculationPromise = agentContext
105
+ .calculateInstructionTokens(tokenCounter)
106
+ .then(() => {
107
+ // Update token map with instruction tokens (includes system + tool tokens)
108
+ agentContext.updateTokenMapWithInstructions(tokenMap);
109
+ })
110
+ .catch((err) => {
111
+ console.error('Error calculating instruction tokens:', err);
112
+ });
113
+ } else if (indexTokenCountMap) {
114
+ agentContext.indexTokenCountMap = indexTokenCountMap;
115
+ }
116
+
117
+ return agentContext;
118
+ }
119
+
120
+ /** Agent identifier */
121
+ agentId: string;
122
+ /** Human-readable name for this agent (used in handoff context). Falls back to agentId if not provided. */
123
+ name?: string;
124
+ /** Provider for this specific agent */
125
+ provider: Providers;
126
+ /** Client options for this agent */
127
+ clientOptions?: t.ClientOptions;
128
+ /** Token count map indexed by message position */
129
+ indexTokenCountMap: Record<string, number | undefined> = {};
130
+ /** Maximum context tokens for this agent */
131
+ maxContextTokens?: number;
132
+ /** Current usage metadata for this agent */
133
+ currentUsage?: Partial<UsageMetadata>;
134
+ /** Prune messages function configured for this agent */
135
+ pruneMessages?: ReturnType<typeof createPruneMessages>;
136
+ /** Token counter function for this agent */
137
+ tokenCounter?: t.TokenCounter;
138
+ /** Instructions/system message token count */
139
+ instructionTokens: number = 0;
140
+ /** The amount of time that should pass before another consecutive API call */
141
+ streamBuffer?: number;
142
+ /** Last stream call timestamp for rate limiting */
143
+ lastStreamCall?: number;
144
+ /** Tools available to this agent */
145
+ tools?: t.GraphTools;
146
+ /** Tool map for this agent */
147
+ toolMap?: t.ToolMap;
148
+ /**
149
+ * Tool definitions registry (includes deferred and programmatic tool metadata).
150
+ * Used for tool search and programmatic tool calling.
151
+ */
152
+ toolRegistry?: t.LCToolRegistry;
153
+ /**
154
+ * Serializable tool definitions for event-driven execution.
155
+ * When provided, ToolNode operates in event-driven mode.
156
+ */
157
+ toolDefinitions?: t.LCTool[];
158
+ /** Set of tool names discovered via tool search (to be loaded) */
159
+ discoveredToolNames: Set<string> = new Set();
160
+ /** Instructions for this agent */
161
+ instructions?: string;
162
+ /** Additional instructions for this agent */
163
+ additionalInstructions?: string;
164
+ /**
165
+ * Dynamic context that changes per-request (e.g., current time, user info).
166
+ * This is NOT included in the system message to preserve cache.
167
+ * Instead, it's injected as a user message at the start of the conversation.
168
+ */
169
+ dynamicContext?: string;
170
+ /** Reasoning key for this agent */
171
+ reasoningKey: 'reasoning_content' | 'reasoning' = 'reasoning_content';
172
+ /** Last token for reasoning detection */
173
+ lastToken?: string;
174
+ /** Token type switch state */
175
+ tokenTypeSwitch?: 'reasoning' | 'content';
176
+ /** Current token type being processed */
177
+ currentTokenType: ContentTypes.TEXT | ContentTypes.THINK | 'think_and_text' =
178
+ ContentTypes.TEXT;
179
+ /** Whether tools should end the workflow */
180
+ toolEnd: boolean = false;
181
+ /** Cached system runnable (created lazily) */
182
+ private cachedSystemRunnable?: Runnable<
183
+ BaseMessage[],
184
+ (BaseMessage | SystemMessage)[],
185
+ RunnableConfig<Record<string, unknown>>
186
+ >;
187
+ /** Whether system runnable needs rebuild (set when discovered tools change) */
188
+ private systemRunnableStale: boolean = true;
189
+ /** Cached system message token count (separate from tool tokens) */
190
+ private systemMessageTokens: number = 0;
191
+ /** Promise for token calculation initialization */
192
+ tokenCalculationPromise?: Promise<void>;
193
+ /** Format content blocks as strings (for legacy compatibility) */
194
+ useLegacyContent: boolean = false;
195
+ /** Detailed per-tool token breakdown for admin tracking */
196
+ private toolsDetail: Array<{ name: string; tokens: number }> = [];
197
+ /** Total tool tokens (sum of all toolsDetail) */
198
+ private toolTokensTotal: number = 0;
199
+ /** Per-prompt token breakdown for detailed admin reporting */
200
+ private promptBreakdown: {
201
+ branding: number;
202
+ toolRouting: number;
203
+ agentInstructions: number;
204
+ mcpInstructions: number;
205
+ artifacts: number;
206
+ memory: number;
207
+ } = {
208
+ branding: 0,
209
+ toolRouting: 0,
210
+ agentInstructions: 0,
211
+ mcpInstructions: 0,
212
+ artifacts: 0,
213
+ memory: 0,
214
+ };
215
+ /**
216
+ * Handoff context when this agent receives control via handoff.
217
+ * Contains source and parallel execution info for system message context.
218
+ */
219
+ handoffContext?: {
220
+ /** Source agent that transferred control */
221
+ sourceAgentName: string;
222
+ /** Names of sibling agents executing in parallel (empty if sequential) */
223
+ parallelSiblings: string[];
224
+ };
225
+ /**
226
+ * Structured output configuration.
227
+ * When set, the agent will return a validated JSON response
228
+ * instead of streaming text.
229
+ */
230
+ structuredOutput?: t.StructuredOutputConfig;
231
+ /**
232
+ * Optional callback for summarizing messages that were pruned from context.
233
+ * When provided, discarded messages are summarized instead of silently dropped.
234
+ */
235
+ summarizeCallback?: (messagesToRefine: BaseMessage[]) => Promise<string | undefined>;
236
+
237
+ constructor({
238
+ agentId,
239
+ name,
240
+ provider,
241
+ clientOptions,
242
+ maxContextTokens,
243
+ streamBuffer,
244
+ tokenCounter,
245
+ tools,
246
+ toolMap,
247
+ toolRegistry,
248
+ toolDefinitions,
249
+ instructions,
250
+ additionalInstructions,
251
+ dynamicContext,
252
+ reasoningKey,
253
+ toolEnd,
254
+ instructionTokens,
255
+ useLegacyContent,
256
+ structuredOutput,
257
+ discoveredTools,
258
+ }: {
259
+ agentId: string;
260
+ name?: string;
261
+ provider: Providers;
262
+ clientOptions?: t.ClientOptions;
263
+ maxContextTokens?: number;
264
+ streamBuffer?: number;
265
+ tokenCounter?: t.TokenCounter;
266
+ tools?: t.GraphTools;
267
+ toolMap?: t.ToolMap;
268
+ toolRegistry?: t.LCToolRegistry;
269
+ toolDefinitions?: t.LCTool[];
270
+ instructions?: string;
271
+ additionalInstructions?: string;
272
+ dynamicContext?: string;
273
+ reasoningKey?: 'reasoning_content' | 'reasoning';
274
+ toolEnd?: boolean;
275
+ instructionTokens?: number;
276
+ useLegacyContent?: boolean;
277
+ structuredOutput?: t.StructuredOutputConfig;
278
+ discoveredTools?: string[];
279
+ }) {
280
+ this.agentId = agentId;
281
+ this.name = name;
282
+ this.provider = provider;
283
+ this.clientOptions = clientOptions;
284
+ this.maxContextTokens = maxContextTokens;
285
+ this.streamBuffer = streamBuffer;
286
+ this.tokenCounter = tokenCounter;
287
+ this.tools = tools;
288
+ this.toolMap = toolMap;
289
+ this.toolRegistry = toolRegistry;
290
+ this.toolDefinitions = toolDefinitions;
291
+ this.instructions = instructions;
292
+ this.additionalInstructions = additionalInstructions;
293
+ this.dynamicContext = dynamicContext;
294
+ this.structuredOutput = structuredOutput;
295
+ if (reasoningKey) {
296
+ this.reasoningKey = reasoningKey;
297
+ }
298
+ if (toolEnd !== undefined) {
299
+ this.toolEnd = toolEnd;
300
+ }
301
+ if (instructionTokens !== undefined) {
302
+ this.instructionTokens = instructionTokens;
303
+ }
304
+
305
+ this.useLegacyContent = useLegacyContent ?? false;
306
+
307
+ if (discoveredTools && discoveredTools.length > 0) {
308
+ for (const toolName of discoveredTools) {
309
+ this.discoveredToolNames.add(toolName);
310
+ }
311
+ }
312
+ }
313
+
314
+ /**
315
+ * Checks if structured output mode is enabled for this agent.
316
+ * When enabled, the agent will use model.invoke() instead of streaming
317
+ * and return a validated JSON response.
318
+ */
319
+ get isStructuredOutputMode(): boolean {
320
+ return (
321
+ this.structuredOutput != null && this.structuredOutput.schema != null
322
+ );
323
+ }
324
+
325
+ /**
326
+ * Gets the structured output schema with normalized defaults.
327
+ * Returns undefined if structured output is not configured.
328
+ */
329
+ getStructuredOutputSchema(): Record<string, unknown> | undefined {
330
+ if (!this.structuredOutput?.schema) {
331
+ return undefined;
332
+ }
333
+
334
+ const schema = { ...this.structuredOutput.schema };
335
+
336
+ // Ensure type is set
337
+ if (!schema.type && schema.properties) {
338
+ schema.type = 'object';
339
+ }
340
+
341
+ // Add title from config name
342
+ if (this.structuredOutput.name && !schema.title) {
343
+ schema.title = this.structuredOutput.name;
344
+ }
345
+
346
+ // Add description from config
347
+ if (this.structuredOutput.description && !schema.description) {
348
+ schema.description = this.structuredOutput.description;
349
+ }
350
+
351
+ // Enable strict mode by default
352
+ if (this.structuredOutput.strict !== false && schema.type === 'object') {
353
+ schema.additionalProperties = schema.additionalProperties ?? false;
354
+ }
355
+
356
+ return schema;
357
+ }
358
+
359
+ /**
360
+ * Resolves the structured output mode to a concrete method based on provider capabilities.
361
+ *
362
+ * Resolution logic:
363
+ * - 'native' or 'auto' + supported provider → native constrained decoding
364
+ * - 'native' + unsupported provider → fallback to 'functionCalling' with warning
365
+ * - 'provider' → LangChain's jsonMode (existing behavior)
366
+ * - 'tool' → function calling trick (existing behavior)
367
+ * - 'auto' + unsupported provider → 'functionCalling'
368
+ *
369
+ * @returns The resolved method for withStructuredOutput, or 'native' for direct API usage
370
+ */
371
+ resolveStructuredOutputMode(): {
372
+ method: t.ResolvedStructuredOutputMethod;
373
+ warnings: string[];
374
+ } {
375
+ const mode = this.structuredOutput?.mode ?? 'auto';
376
+ const warnings: string[] = [];
377
+
378
+ // Providers that support native constrained decoding via LangChain
379
+ const nativeProviders = new Set([
380
+ Providers.ANTHROPIC,
381
+ Providers.OPENAI,
382
+ Providers.AZURE,
383
+ ]);
384
+
385
+ // Providers where LangChain supports jsonMode
386
+ const jsonModeProviders = new Set([
387
+ Providers.ANTHROPIC,
388
+ Providers.OPENAI,
389
+ Providers.AZURE,
390
+ ]);
391
+
392
+ switch (mode) {
393
+ case 'native': {
394
+ if (nativeProviders.has(this.provider)) {
395
+ if (this.provider === Providers.ANTHROPIC) {
396
+ return { method: 'jsonSchema', warnings };
397
+ }
398
+ // OpenAI/Azure
399
+ return { method: 'jsonSchema', warnings };
400
+ }
401
+ // Fallback for unsupported providers
402
+ warnings.push(
403
+ `Native structured output is not supported for provider '${this.provider}'. Falling back to function calling.`
404
+ );
405
+ return { method: 'functionCalling', warnings };
406
+ }
407
+
408
+ case 'auto': {
409
+ if (nativeProviders.has(this.provider)) {
410
+ if (this.provider === Providers.ANTHROPIC) {
411
+ return { method: 'jsonSchema', warnings };
412
+ }
413
+ // OpenAI/Azure
414
+ return { method: 'jsonSchema', warnings };
415
+ }
416
+ // Default to function calling for all other providers
417
+ return { method: undefined, warnings };
418
+ }
419
+
420
+ case 'provider': {
421
+ if (this.provider === Providers.BEDROCK) {
422
+ // Bedrock doesn't support jsonMode, fall back to functionCalling
423
+ return { method: 'functionCalling', warnings };
424
+ }
425
+ if (jsonModeProviders.has(this.provider)) {
426
+ return { method: 'jsonMode', warnings };
427
+ }
428
+ return { method: 'jsonMode', warnings };
429
+ }
430
+
431
+ case 'tool': {
432
+ return { method: 'functionCalling', warnings };
433
+ }
434
+
435
+ default: {
436
+ return { method: undefined, warnings };
437
+ }
438
+ }
439
+ }
440
+
441
+ /**
442
+ * Builds instructions text for tools that are ONLY callable via programmatic code execution.
443
+ * These tools cannot be called directly by the LLM but are available through the
444
+ * run_tools_with_code tool.
445
+ *
446
+ * Includes:
447
+ * - Code_execution-only tools that are NOT deferred
448
+ * - Code_execution-only tools that ARE deferred but have been discovered via tool search
449
+ */
450
+ private buildProgrammaticOnlyToolsInstructions(): string {
451
+ if (!this.toolRegistry) return '';
452
+
453
+ const programmaticOnlyTools: t.LCTool[] = [];
454
+ for (const [name, toolDef] of this.toolRegistry) {
455
+ const allowedCallers = toolDef.allowed_callers ?? ['direct'];
456
+ const isCodeExecutionOnly =
457
+ allowedCallers.includes('code_execution') &&
458
+ !allowedCallers.includes('direct');
459
+
460
+ if (!isCodeExecutionOnly) continue;
461
+
462
+ // Include if: not deferred OR deferred but discovered
463
+ const isDeferred = toolDef.defer_loading === true;
464
+ const isDiscovered = this.discoveredToolNames.has(name);
465
+ if (!isDeferred || isDiscovered) {
466
+ programmaticOnlyTools.push(toolDef);
467
+ }
468
+ }
469
+
470
+ if (programmaticOnlyTools.length === 0) return '';
471
+
472
+ const toolDescriptions = programmaticOnlyTools
473
+ .map((tool) => {
474
+ let desc = `- **${tool.name}**`;
475
+ if (tool.description != null && tool.description !== '') {
476
+ desc += `: ${tool.description}`;
477
+ }
478
+ if (tool.parameters) {
479
+ desc += `\n Parameters: ${JSON.stringify(tool.parameters, null, 2).replace(/\n/g, '\n ')}`;
480
+ }
481
+ return desc;
482
+ })
483
+ .join('\n\n');
484
+
485
+ return (
486
+ '\n\n## Programmatic-Only Tools\n\n' +
487
+ 'The following tools are available exclusively through the `run_tools_with_code` tool. ' +
488
+ 'You cannot call these tools directly; instead, use `run_tools_with_code` with Python code that invokes them.\n\n' +
489
+ toolDescriptions
490
+ );
491
+ }
492
+
493
+ /**
494
+ * Gets the system runnable, creating it lazily if needed.
495
+ * Includes instructions, additional instructions, and programmatic-only tools documentation.
496
+ * Only rebuilds when marked stale (via markToolsAsDiscovered).
497
+ */
498
+ get systemRunnable():
499
+ | Runnable<
500
+ BaseMessage[],
501
+ (BaseMessage | SystemMessage)[],
502
+ RunnableConfig<Record<string, unknown>>
503
+ >
504
+ | undefined {
505
+ // Return cached if not stale
506
+ if (!this.systemRunnableStale && this.cachedSystemRunnable !== undefined) {
507
+ return this.cachedSystemRunnable;
508
+ }
509
+
510
+ // Stale or first access - rebuild
511
+ const instructionsString = this.buildInstructionsString();
512
+ this.cachedSystemRunnable = this.buildSystemRunnable(instructionsString);
513
+ this.systemRunnableStale = false;
514
+ return this.cachedSystemRunnable;
515
+ }
516
+
517
+ /**
518
+ * Explicitly initializes the system runnable.
519
+ * Call this before async token calculation to ensure system message tokens are counted first.
520
+ */
521
+ initializeSystemRunnable(): void {
522
+ if (this.systemRunnableStale || this.cachedSystemRunnable === undefined) {
523
+ const instructionsString = this.buildInstructionsString();
524
+ this.cachedSystemRunnable = this.buildSystemRunnable(instructionsString);
525
+ this.systemRunnableStale = false;
526
+ }
527
+ }
528
+
529
+ /**
530
+ * Builds the raw instructions string (without creating SystemMessage).
531
+ * Includes agent identity preamble and handoff context when available.
532
+ */
533
+ private buildInstructionsString(): string {
534
+ const parts: string[] = [];
535
+
536
+ /** Build agent identity and handoff context preamble */
537
+ const identityPreamble = this.buildIdentityPreamble();
538
+ if (identityPreamble) {
539
+ parts.push(identityPreamble);
540
+ }
541
+
542
+ /** Add main instructions */
543
+ if (this.instructions != null && this.instructions !== '') {
544
+ parts.push(this.instructions);
545
+ }
546
+
547
+ /** Add additional instructions */
548
+ if (
549
+ this.additionalInstructions != null &&
550
+ this.additionalInstructions !== ''
551
+ ) {
552
+ parts.push(this.additionalInstructions);
553
+ }
554
+
555
+ /** Add programmatic tools documentation */
556
+ const programmaticToolsDoc = this.buildProgrammaticOnlyToolsInstructions();
557
+ if (programmaticToolsDoc) {
558
+ parts.push(programmaticToolsDoc);
559
+ }
560
+
561
+ return parts.join('\n\n');
562
+ }
563
+
564
+ /**
565
+ * Builds the agent identity preamble including handoff context if present.
566
+ * This helps the agent understand its role in the multi-agent workflow.
567
+ */
568
+ private buildIdentityPreamble(): string {
569
+ if (!this.handoffContext) return '';
570
+
571
+ const displayName = this.name ?? this.agentId;
572
+ const { sourceAgentName, parallelSiblings } = this.handoffContext;
573
+ const isParallel = parallelSiblings.length > 0;
574
+
575
+ const lines: string[] = [];
576
+ lines.push('## Multi-Agent Workflow');
577
+ lines.push(
578
+ `You are "${displayName}", transferred from "${sourceAgentName}".`
579
+ );
580
+
581
+ if (isParallel) {
582
+ lines.push(`Running in parallel with: ${parallelSiblings.join(', ')}.`);
583
+ }
584
+
585
+ lines.push(
586
+ 'Execute only tasks relevant to your role. Routing is already handled if requested, unless you can route further.'
587
+ );
588
+
589
+ return lines.join('\n');
590
+ }
591
+
592
+ /**
593
+ * Build system runnable from pre-built instructions string.
594
+ * Only called when content has actually changed.
595
+ */
596
+ private buildSystemRunnable(
597
+ instructionsString: string
598
+ ):
599
+ | Runnable<
600
+ BaseMessage[],
601
+ (BaseMessage | SystemMessage)[],
602
+ RunnableConfig<Record<string, unknown>>
603
+ >
604
+ | undefined {
605
+ if (!instructionsString) {
606
+ // Remove previous tokens if we had a system message before
607
+ this.instructionTokens -= this.systemMessageTokens;
608
+ this.systemMessageTokens = 0;
609
+ return undefined;
610
+ }
611
+
612
+ let finalInstructions: string | BaseMessageFields = instructionsString;
613
+
614
+ // Handle Anthropic prompt caching (Direct API)
615
+ if (this.provider === Providers.ANTHROPIC) {
616
+ const anthropicOptions = this.clientOptions as
617
+ | t.AnthropicClientOptions
618
+ | undefined;
619
+ if (anthropicOptions?.promptCache === true) {
620
+ finalInstructions = {
621
+ content: [
622
+ {
623
+ type: 'text',
624
+ text: instructionsString,
625
+ cache_control: { type: 'ephemeral' },
626
+ },
627
+ ],
628
+ };
629
+ }
630
+ }
631
+
632
+ // Handle Bedrock prompt caching (Converse API)
633
+ // Adds cachePoint block after text content for system message caching
634
+ // NOTE: Both Claude and Nova models support cachePoint in system and messages
635
+ // (Nova does NOT support cachePoint in tools - that check is in bedrock/index.ts)
636
+ if (this.provider === Providers.BEDROCK) {
637
+ const bedrockOptions = this.clientOptions as
638
+ | t.BedrockAnthropicInput
639
+ | undefined;
640
+ const modelId = bedrockOptions?.model?.toLowerCase() ?? '';
641
+ const supportsCaching =
642
+ modelId.includes('claude') ||
643
+ modelId.includes('anthropic') ||
644
+ modelId.includes('nova');
645
+
646
+ if (bedrockOptions?.promptCache === true && supportsCaching) {
647
+ finalInstructions = {
648
+ content: [
649
+ {
650
+ type: 'text',
651
+ text: instructionsString,
652
+ },
653
+ {
654
+ cachePoint: { type: 'default' },
655
+ },
656
+ ],
657
+ };
658
+ }
659
+ }
660
+
661
+ const systemMessage = new SystemMessage(finalInstructions);
662
+
663
+ // Update token counts (subtract old, add new)
664
+ if (this.tokenCounter) {
665
+ this.instructionTokens -= this.systemMessageTokens;
666
+ this.systemMessageTokens = this.tokenCounter(systemMessage);
667
+ this.instructionTokens += this.systemMessageTokens;
668
+ }
669
+
670
+ return RunnableLambda.from((messages: BaseMessage[]) => {
671
+ return [systemMessage, ...messages];
672
+ }).withConfig({ runName: 'prompt' });
673
+ }
674
+
675
+ /**
676
+ * Reset context for a new run
677
+ */
678
+ reset(): void {
679
+ this.instructionTokens = 0;
680
+ this.systemMessageTokens = 0;
681
+ this.toolsDetail = [];
682
+ this.toolTokensTotal = 0;
683
+ this.cachedSystemRunnable = undefined;
684
+ this.systemRunnableStale = true;
685
+ this.lastToken = undefined;
686
+ this.indexTokenCountMap = {};
687
+ this.currentUsage = undefined;
688
+ this.pruneMessages = undefined;
689
+ this.lastStreamCall = undefined;
690
+ this.tokenTypeSwitch = undefined;
691
+ this.currentTokenType = ContentTypes.TEXT;
692
+ this.discoveredToolNames.clear();
693
+ this.handoffContext = undefined;
694
+ }
695
+
696
+ /**
697
+ * Update the token count map with instruction tokens
698
+ */
699
+ updateTokenMapWithInstructions(baseTokenMap: Record<string, number>): void {
700
+ if (this.instructionTokens > 0) {
701
+ // Shift all indices by the instruction token count
702
+ const shiftedMap: Record<string, number> = {};
703
+ for (const [key, value] of Object.entries(baseTokenMap)) {
704
+ const index = parseInt(key, 10);
705
+ if (!isNaN(index)) {
706
+ shiftedMap[String(index)] =
707
+ value + (index === 0 ? this.instructionTokens : 0);
708
+ }
709
+ }
710
+ this.indexTokenCountMap = shiftedMap;
711
+ } else {
712
+ this.indexTokenCountMap = { ...baseTokenMap };
713
+ }
714
+ }
715
+
716
+ /**
717
+ * Calculate tool tokens and add to instruction tokens
718
+ * Note: System message tokens are calculated during systemRunnable creation
719
+ * Also tracks per-tool token breakdown for admin reporting
720
+ */
721
+ async calculateInstructionTokens(
722
+ tokenCounter: t.TokenCounter
723
+ ): Promise<void> {
724
+ let toolTokens = 0;
725
+ this.toolsDetail = []; // Reset per-tool breakdown
726
+
727
+ if (this.tools && this.tools.length > 0) {
728
+ for (const tool of this.tools) {
729
+ const genericTool = tool as Record<string, unknown>;
730
+ if (
731
+ genericTool.schema != null &&
732
+ typeof genericTool.schema === 'object'
733
+ ) {
734
+ const jsonSchema = toJsonSchema(
735
+ genericTool.schema,
736
+ (genericTool.name as string | undefined) ?? '',
737
+ (genericTool.description as string | undefined) ?? ''
738
+ );
739
+ const toolName = (genericTool.name as string) || 'unknown';
740
+ const tokens = tokenCounter(
741
+ new SystemMessage(JSON.stringify(jsonSchema))
742
+ );
743
+
744
+ // Track per-tool breakdown
745
+ this.toolsDetail.push({ name: toolName, tokens });
746
+ toolTokens += tokens;
747
+ }
748
+ }
749
+ }
750
+
751
+ // Store total tool tokens for breakdown reporting
752
+ this.toolTokensTotal = toolTokens;
753
+
754
+ // Add tool tokens to existing instruction tokens (which may already include system message tokens)
755
+ this.instructionTokens += toolTokens;
756
+ }
757
+
758
+ /**
759
+ * Set the per-prompt token breakdown for detailed admin tracking.
760
+ * Called by the client after assembling all prompt components.
761
+ * @param breakdown - Object with token counts per prompt component
762
+ */
763
+ setPromptBreakdown(breakdown: {
764
+ branding?: number;
765
+ toolRouting?: number;
766
+ agentInstructions?: number;
767
+ mcpInstructions?: number;
768
+ artifacts?: number;
769
+ memory?: number;
770
+ }): void {
771
+ if (breakdown.branding !== undefined)
772
+ this.promptBreakdown.branding = breakdown.branding;
773
+ if (breakdown.toolRouting !== undefined)
774
+ this.promptBreakdown.toolRouting = breakdown.toolRouting;
775
+ if (breakdown.agentInstructions !== undefined)
776
+ this.promptBreakdown.agentInstructions = breakdown.agentInstructions;
777
+ if (breakdown.mcpInstructions !== undefined)
778
+ this.promptBreakdown.mcpInstructions = breakdown.mcpInstructions;
779
+ if (breakdown.artifacts !== undefined)
780
+ this.promptBreakdown.artifacts = breakdown.artifacts;
781
+ if (breakdown.memory !== undefined)
782
+ this.promptBreakdown.memory = breakdown.memory;
783
+ }
784
+
785
+ /**
786
+ * Get a detailed breakdown of context tokens for admin reporting.
787
+ * This provides visibility into what's consuming the input token budget.
788
+ * @returns ContextBreakdown object with per-component token counts
789
+ */
790
+ getContextBreakdown(): {
791
+ instructions: number;
792
+ artifacts: number;
793
+ tools: number;
794
+ toolCount: number;
795
+ toolContext: number;
796
+ total: number;
797
+ toolsDetail: Array<{ name: string; tokens: number }>;
798
+ toolContextDetail: Array<{ name: string; tokens: number }>;
799
+ prompts?: {
800
+ branding: number;
801
+ toolRouting: number;
802
+ agentInstructions: number;
803
+ mcpInstructions: number;
804
+ artifacts: number;
805
+ memory: number;
806
+ };
807
+ } {
808
+ // Calculate sum of prompt components
809
+ const promptsTotal =
810
+ this.promptBreakdown.branding +
811
+ this.promptBreakdown.toolRouting +
812
+ this.promptBreakdown.agentInstructions +
813
+ this.promptBreakdown.mcpInstructions +
814
+ this.promptBreakdown.artifacts +
815
+ this.promptBreakdown.memory;
816
+
817
+ return {
818
+ // System message tokens (instructions + additional_instructions)
819
+ instructions: this.systemMessageTokens,
820
+ // Artifacts prompt tokens
821
+ artifacts: this.promptBreakdown.artifacts,
822
+ // Total tool definition tokens
823
+ tools: this.toolTokensTotal,
824
+ // Number of tools
825
+ toolCount: this.toolsDetail.length,
826
+ // Tool context/usage instructions (currently embedded in system message)
827
+ toolContext: 0,
828
+ // Total tracked context tokens
829
+ total: this.instructionTokens,
830
+ // Per-tool token breakdown
831
+ toolsDetail: [...this.toolsDetail],
832
+ // Tool context detail (currently not tracked separately)
833
+ toolContextDetail: [],
834
+ // Per-prompt breakdown (only include if any prompts were tracked)
835
+ prompts: promptsTotal > 0 ? { ...this.promptBreakdown } : undefined,
836
+ };
837
+ }
838
+
839
+ /**
840
+ * Gets the tool registry for deferred tools (for tool search).
841
+ * @param onlyDeferred If true, only returns tools with defer_loading=true
842
+ * @returns LCToolRegistry with tool definitions
843
+ */
844
+ getDeferredToolRegistry(onlyDeferred: boolean = true): t.LCToolRegistry {
845
+ const registry: t.LCToolRegistry = new Map();
846
+
847
+ if (!this.toolRegistry) {
848
+ return registry;
849
+ }
850
+
851
+ for (const [name, toolDef] of this.toolRegistry) {
852
+ if (!onlyDeferred || toolDef.defer_loading === true) {
853
+ registry.set(name, toolDef);
854
+ }
855
+ }
856
+
857
+ return registry;
858
+ }
859
+
860
+ /**
861
+ * Sets the handoff context for this agent.
862
+ * Call this when the agent receives control via handoff from another agent.
863
+ * Marks system runnable as stale to include handoff context in system message.
864
+ * @param sourceAgentName - Name of the agent that transferred control
865
+ * @param parallelSiblings - Names of other agents executing in parallel with this one
866
+ */
867
+ setHandoffContext(sourceAgentName: string, parallelSiblings: string[]): void {
868
+ this.handoffContext = { sourceAgentName, parallelSiblings };
869
+ this.systemRunnableStale = true;
870
+ }
871
+
872
+ /**
873
+ * Clears any handoff context.
874
+ * Call this when resetting the agent or when handoff context is no longer relevant.
875
+ */
876
+ clearHandoffContext(): void {
877
+ if (this.handoffContext) {
878
+ this.handoffContext = undefined;
879
+ this.systemRunnableStale = true;
880
+ }
881
+ }
882
+
883
+ /**
884
+ * Marks tools as discovered via tool search.
885
+ * Discovered tools will be included in the next model binding.
886
+ * Only marks system runnable stale if NEW tools were actually added.
887
+ * @param toolNames - Array of discovered tool names
888
+ * @returns true if any new tools were discovered
889
+ */
890
+ markToolsAsDiscovered(toolNames: string[]): boolean {
891
+ let hasNewDiscoveries = false;
892
+ for (const name of toolNames) {
893
+ if (!this.discoveredToolNames.has(name)) {
894
+ this.discoveredToolNames.add(name);
895
+ hasNewDiscoveries = true;
896
+ }
897
+ }
898
+ if (hasNewDiscoveries) {
899
+ this.systemRunnableStale = true;
900
+ }
901
+ return hasNewDiscoveries;
902
+ }
903
+
904
+ /**
905
+ * Gets tools that should be bound to the LLM.
906
+ * In event-driven mode (toolDefinitions present, tools empty), creates schema-only tools.
907
+ * Otherwise filters tool instances based on:
908
+ * 1. Non-deferred tools with allowed_callers: ['direct']
909
+ * 2. Discovered tools (from tool search)
910
+ * @returns Array of tools to bind to model
911
+ */
912
+ getToolsForBinding(): t.GraphTools | undefined {
913
+ /** Event-driven mode: create schema-only tools from definitions */
914
+ if (this.toolDefinitions && this.toolDefinitions.length > 0) {
915
+ return this.getEventDrivenToolsForBinding();
916
+ }
917
+
918
+ /** Traditional mode: filter actual tool instances */
919
+ if (!this.tools || !this.toolRegistry) {
920
+ return this.tools;
921
+ }
922
+
923
+ return this.filterToolsForBinding(this.tools);
924
+ }
925
+
926
+ /** Creates schema-only tools from toolDefinitions for event-driven mode */
927
+ private getEventDrivenToolsForBinding(): t.GraphTools {
928
+ if (!this.toolDefinitions) {
929
+ return [];
930
+ }
931
+
932
+ const defsToInclude = this.toolDefinitions.filter((def) => {
933
+ const allowedCallers = def.allowed_callers ?? ['direct'];
934
+ if (!allowedCallers.includes('direct')) {
935
+ return false;
936
+ }
937
+ if (
938
+ def.defer_loading === true &&
939
+ !this.discoveredToolNames.has(def.name)
940
+ ) {
941
+ return false;
942
+ }
943
+ return true;
944
+ });
945
+
946
+ return createSchemaOnlyTools(defsToInclude) as t.GraphTools;
947
+ }
948
+
949
+ /** Filters tool instances for binding based on registry config */
950
+ private filterToolsForBinding(tools: t.GraphTools): t.GraphTools {
951
+ return tools.filter((tool) => {
952
+ if (!('name' in tool)) {
953
+ return true;
954
+ }
955
+
956
+ const toolDef = this.toolRegistry?.get(tool.name);
957
+ if (!toolDef) {
958
+ return true;
959
+ }
960
+
961
+ if (this.discoveredToolNames.has(tool.name)) {
962
+ const allowedCallers = toolDef.allowed_callers ?? ['direct'];
963
+ return allowedCallers.includes('direct');
964
+ }
965
+
966
+ const allowedCallers = toolDef.allowed_callers ?? ['direct'];
967
+ return (
968
+ allowedCallers.includes('direct') && toolDef.defer_loading !== true
969
+ );
970
+ });
971
+ }
972
+ }