@librechat/agents 4.2.12 → 4.2.14

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 (357) hide show
  1. package/dist/cjs/graphs/Graph.cjs +56 -6
  2. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  3. package/dist/cjs/harness/providerText.cjs +1 -0
  4. package/dist/cjs/harness/providerText.cjs.map +1 -1
  5. package/dist/cjs/llm/anthropic/index.cjs +4 -3
  6. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  7. package/dist/cjs/llm/anthropic/utils/container.cjs +37 -0
  8. package/dist/cjs/llm/anthropic/utils/container.cjs.map +1 -0
  9. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +25 -23
  10. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
  11. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +15 -21
  12. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
  13. package/dist/cjs/llm/anthropic/utils/stream_events.cjs +16 -14
  14. package/dist/cjs/llm/anthropic/utils/stream_events.cjs.map +1 -1
  15. package/dist/cjs/llm/bedrock/index.cjs +1 -1
  16. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  17. package/dist/cjs/llm/bedrock/toolCache.cjs +1 -1
  18. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +19 -13
  19. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -1
  20. package/dist/cjs/llm/google/utils/common.cjs +59 -13
  21. package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
  22. package/dist/cjs/llm/google/utils/signatures.cjs +22 -14
  23. package/dist/cjs/llm/google/utils/signatures.cjs.map +1 -1
  24. package/dist/cjs/llm/init.cjs +12 -2
  25. package/dist/cjs/llm/init.cjs.map +1 -1
  26. package/dist/cjs/llm/invoke.cjs +122 -13
  27. package/dist/cjs/llm/invoke.cjs.map +1 -1
  28. package/dist/cjs/llm/openai/index.cjs +9 -3
  29. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  30. package/dist/cjs/llm/openai/typedContent.cjs +94 -0
  31. package/dist/cjs/llm/openai/typedContent.cjs.map +1 -0
  32. package/dist/cjs/llm/openrouter/toolCache.cjs +1 -1
  33. package/dist/cjs/llm/prepareProviderRequest.cjs +81 -4
  34. package/dist/cjs/llm/prepareProviderRequest.cjs.map +1 -1
  35. package/dist/cjs/llm/providerTextBatch.cjs +128 -0
  36. package/dist/cjs/llm/providerTextBatch.cjs.map +1 -0
  37. package/dist/cjs/llm/providerTextBoundary.cjs +81 -12
  38. package/dist/cjs/llm/providerTextBoundary.cjs.map +1 -1
  39. package/dist/cjs/main.cjs +8 -3
  40. package/dist/cjs/messages/alternation.cjs +1 -1
  41. package/dist/cjs/messages/anthropicServerTools.cjs +20 -0
  42. package/dist/cjs/messages/anthropicServerTools.cjs.map +1 -0
  43. package/dist/cjs/messages/budget.cjs +1 -1
  44. package/dist/cjs/messages/core.cjs +3 -2
  45. package/dist/cjs/messages/core.cjs.map +1 -1
  46. package/dist/cjs/messages/format.cjs +2 -2
  47. package/dist/cjs/messages/index.cjs +1 -1
  48. package/dist/cjs/messages/prune.cjs +1 -1
  49. package/dist/cjs/messages/recency.cjs +1 -1
  50. package/dist/cjs/messages/toolResultTypes.cjs +21 -45
  51. package/dist/cjs/messages/toolResultTypes.cjs.map +1 -1
  52. package/dist/cjs/protection/providerText.cjs +60 -6
  53. package/dist/cjs/protection/providerText.cjs.map +1 -1
  54. package/dist/cjs/protection/providerTextAdmission.cjs +5 -0
  55. package/dist/cjs/protection/providerTextAdmission.cjs.map +1 -1
  56. package/dist/cjs/protection/toolResult.cjs +119 -0
  57. package/dist/cjs/protection/toolResult.cjs.map +1 -0
  58. package/dist/cjs/run.cjs +14 -3
  59. package/dist/cjs/run.cjs.map +1 -1
  60. package/dist/cjs/runtime/client.cjs +4 -0
  61. package/dist/cjs/runtime/client.cjs.map +1 -1
  62. package/dist/cjs/stream.cjs +10 -2
  63. package/dist/cjs/stream.cjs.map +1 -1
  64. package/dist/cjs/summarization/node.cjs +1 -1
  65. package/dist/cjs/tools/ToolNode.cjs +46 -13
  66. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  67. package/dist/cjs/tools/handlers.cjs +47 -1
  68. package/dist/cjs/tools/handlers.cjs.map +1 -1
  69. package/dist/cjs/tools/search/citations.cjs +194 -0
  70. package/dist/cjs/tools/search/citations.cjs.map +1 -0
  71. package/dist/cjs/tools/search/content.cjs +163 -42
  72. package/dist/cjs/tools/search/content.cjs.map +1 -1
  73. package/dist/cjs/tools/search/firecrawl.cjs +4 -9
  74. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
  75. package/dist/cjs/tools/search/google.cjs +68 -0
  76. package/dist/cjs/tools/search/google.cjs.map +1 -0
  77. package/dist/cjs/tools/search/openai.cjs +80 -0
  78. package/dist/cjs/tools/search/openai.cjs.map +1 -0
  79. package/dist/cjs/tools/subagent/SubagentExecutor.cjs +4 -0
  80. package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
  81. package/dist/cjs/utils/tokens.cjs +12 -2
  82. package/dist/cjs/utils/tokens.cjs.map +1 -1
  83. package/dist/esm/graphs/Graph.mjs +57 -7
  84. package/dist/esm/graphs/Graph.mjs.map +1 -1
  85. package/dist/esm/harness/providerText.mjs +1 -0
  86. package/dist/esm/harness/providerText.mjs.map +1 -1
  87. package/dist/esm/llm/anthropic/index.mjs +4 -3
  88. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  89. package/dist/esm/llm/anthropic/utils/container.mjs +37 -0
  90. package/dist/esm/llm/anthropic/utils/container.mjs.map +1 -0
  91. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +25 -23
  92. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
  93. package/dist/esm/llm/anthropic/utils/message_outputs.mjs +15 -21
  94. package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
  95. package/dist/esm/llm/anthropic/utils/stream_events.mjs +16 -14
  96. package/dist/esm/llm/anthropic/utils/stream_events.mjs.map +1 -1
  97. package/dist/esm/llm/bedrock/index.mjs +2 -2
  98. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  99. package/dist/esm/llm/bedrock/toolCache.mjs +1 -1
  100. package/dist/esm/llm/bedrock/utils/message_inputs.mjs +19 -14
  101. package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -1
  102. package/dist/esm/llm/google/utils/common.mjs +59 -13
  103. package/dist/esm/llm/google/utils/common.mjs.map +1 -1
  104. package/dist/esm/llm/google/utils/signatures.mjs +22 -14
  105. package/dist/esm/llm/google/utils/signatures.mjs.map +1 -1
  106. package/dist/esm/llm/init.mjs +12 -2
  107. package/dist/esm/llm/init.mjs.map +1 -1
  108. package/dist/esm/llm/invoke.mjs +124 -15
  109. package/dist/esm/llm/invoke.mjs.map +1 -1
  110. package/dist/esm/llm/openai/index.mjs +9 -3
  111. package/dist/esm/llm/openai/index.mjs.map +1 -1
  112. package/dist/esm/llm/openai/typedContent.mjs +93 -0
  113. package/dist/esm/llm/openai/typedContent.mjs.map +1 -0
  114. package/dist/esm/llm/openrouter/toolCache.mjs +1 -1
  115. package/dist/esm/llm/prepareProviderRequest.mjs +81 -4
  116. package/dist/esm/llm/prepareProviderRequest.mjs.map +1 -1
  117. package/dist/esm/llm/providerTextBatch.mjs +128 -0
  118. package/dist/esm/llm/providerTextBatch.mjs.map +1 -0
  119. package/dist/esm/llm/providerTextBoundary.mjs +81 -12
  120. package/dist/esm/llm/providerTextBoundary.mjs.map +1 -1
  121. package/dist/esm/main.mjs +7 -6
  122. package/dist/esm/messages/alternation.mjs +1 -1
  123. package/dist/esm/messages/anthropicServerTools.mjs +18 -0
  124. package/dist/esm/messages/anthropicServerTools.mjs.map +1 -0
  125. package/dist/esm/messages/budget.mjs +1 -1
  126. package/dist/esm/messages/core.mjs +3 -2
  127. package/dist/esm/messages/core.mjs.map +1 -1
  128. package/dist/esm/messages/format.mjs +2 -2
  129. package/dist/esm/messages/index.mjs +1 -1
  130. package/dist/esm/messages/prune.mjs +1 -1
  131. package/dist/esm/messages/recency.mjs +1 -1
  132. package/dist/esm/messages/toolResultTypes.mjs +21 -45
  133. package/dist/esm/messages/toolResultTypes.mjs.map +1 -1
  134. package/dist/esm/protection/providerText.mjs +58 -7
  135. package/dist/esm/protection/providerText.mjs.map +1 -1
  136. package/dist/esm/protection/providerTextAdmission.mjs +5 -0
  137. package/dist/esm/protection/providerTextAdmission.mjs.map +1 -1
  138. package/dist/esm/protection/toolResult.mjs +114 -0
  139. package/dist/esm/protection/toolResult.mjs.map +1 -0
  140. package/dist/esm/run.mjs +14 -3
  141. package/dist/esm/run.mjs.map +1 -1
  142. package/dist/esm/runtime/client.mjs +4 -0
  143. package/dist/esm/runtime/client.mjs.map +1 -1
  144. package/dist/esm/stream.mjs +10 -2
  145. package/dist/esm/stream.mjs.map +1 -1
  146. package/dist/esm/summarization/node.mjs +1 -1
  147. package/dist/esm/tools/ToolNode.mjs +46 -13
  148. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  149. package/dist/esm/tools/handlers.mjs +47 -2
  150. package/dist/esm/tools/handlers.mjs.map +1 -1
  151. package/dist/esm/tools/search/citations.mjs +190 -0
  152. package/dist/esm/tools/search/citations.mjs.map +1 -0
  153. package/dist/esm/tools/search/content.mjs +163 -42
  154. package/dist/esm/tools/search/content.mjs.map +1 -1
  155. package/dist/esm/tools/search/firecrawl.mjs +4 -9
  156. package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
  157. package/dist/esm/tools/search/google.mjs +67 -0
  158. package/dist/esm/tools/search/google.mjs.map +1 -0
  159. package/dist/esm/tools/search/openai.mjs +80 -0
  160. package/dist/esm/tools/search/openai.mjs.map +1 -0
  161. package/dist/esm/tools/subagent/SubagentExecutor.mjs +4 -0
  162. package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
  163. package/dist/esm/utils/tokens.mjs +12 -2
  164. package/dist/esm/utils/tokens.mjs.map +1 -1
  165. package/dist/types/common/index.d.ts +2 -2
  166. package/dist/types/decisions/breaker.d.ts +2 -2
  167. package/dist/types/decisions/core.d.ts +13 -13
  168. package/dist/types/decisions/dialect.d.ts +1 -1
  169. package/dist/types/decisions/http.d.ts +2 -2
  170. package/dist/types/decisions/index.d.ts +5 -5
  171. package/dist/types/decisions/node.d.ts +2 -2
  172. package/dist/types/decisions/presets.d.ts +2 -2
  173. package/dist/types/decisions/probe.d.ts +1 -1
  174. package/dist/types/decisions/questions.d.ts +1 -1
  175. package/dist/types/decisions/structuredChat.d.ts +2 -2
  176. package/dist/types/decisions/transport.d.ts +1 -1
  177. package/dist/types/eventActor/EventActorExecutor.d.ts +1 -1
  178. package/dist/types/eventActor/index.d.ts +2 -2
  179. package/dist/types/graphs/Graph.d.ts +18 -2
  180. package/dist/types/graphs/MultiAgentGraph.d.ts +1 -1
  181. package/dist/types/graphs/applyGraphRuntimeConfig.d.ts +1 -1
  182. package/dist/types/graphs/graphFactory.d.ts +1 -1
  183. package/dist/types/graphs/index.d.ts +5 -5
  184. package/dist/types/harness/fileBindings.d.ts +2 -2
  185. package/dist/types/harness/fileService.d.ts +2 -2
  186. package/dist/types/harness/fileStream.d.ts +1 -1
  187. package/dist/types/harness/index.d.ts +15 -15
  188. package/dist/types/harness/model.d.ts +1 -1
  189. package/dist/types/harness/modelService.d.ts +2 -2
  190. package/dist/types/harness/modelStreamCallbacks.d.ts +1 -1
  191. package/dist/types/harness/providerText.d.ts +1 -1
  192. package/dist/types/hitl/index.d.ts +3 -3
  193. package/dist/types/hooks/HookRegistry.d.ts +1 -1
  194. package/dist/types/hooks/createToolPolicyHook.d.ts +1 -1
  195. package/dist/types/hooks/createWorkspacePolicyHook.d.ts +1 -1
  196. package/dist/types/hooks/executeHooks.d.ts +2 -2
  197. package/dist/types/hooks/index.d.ts +11 -11
  198. package/dist/types/index.d.ts +62 -60
  199. package/dist/types/langchain/index.d.ts +8 -8
  200. package/dist/types/llm/anthropic/utils/container.d.ts +4 -0
  201. package/dist/types/llm/anthropic/utils/message_inputs.d.ts +1 -1
  202. package/dist/types/llm/anthropic/utils/message_outputs.d.ts +1 -1
  203. package/dist/types/llm/anthropic/utils/stream_events.d.ts +1 -1
  204. package/dist/types/llm/bedrock/index.d.ts +2 -3
  205. package/dist/types/llm/bedrock/utils/harmony.d.ts +1 -1
  206. package/dist/types/llm/bedrock/utils/index.d.ts +2 -2
  207. package/dist/types/llm/bedrock/utils/message_inputs.d.ts +3 -12
  208. package/dist/types/llm/bedrock/utils/message_outputs.d.ts +1 -1
  209. package/dist/types/llm/google/index.d.ts +2 -2
  210. package/dist/types/llm/google/utils/common.d.ts +1 -1
  211. package/dist/types/llm/google/utils/tools.d.ts +1 -1
  212. package/dist/types/llm/openai/index.d.ts +1 -1
  213. package/dist/types/llm/openai/typedContent.d.ts +14 -0
  214. package/dist/types/llm/providerRegistry.d.ts +1 -1
  215. package/dist/types/llm/providerTextBatch.d.ts +26 -0
  216. package/dist/types/llm/providerTextBoundary.d.ts +7 -1
  217. package/dist/types/llm/providers.d.ts +1 -1
  218. package/dist/types/messages/anthropicServerTools.d.ts +13 -0
  219. package/dist/types/messages/contextPruning.d.ts +1 -1
  220. package/dist/types/messages/core.d.ts +4 -4
  221. package/dist/types/messages/format.d.ts +1 -1
  222. package/dist/types/messages/index.d.ts +21 -21
  223. package/dist/types/messages/prune.d.ts +1 -1
  224. package/dist/types/openai/index.d.ts +2 -2
  225. package/dist/types/openai/toolProjection.d.ts +1 -1
  226. package/dist/types/prompts/index.d.ts +2 -2
  227. package/dist/types/protection/providerText.d.ts +32 -2
  228. package/dist/types/protection/providerTextAdmission.d.ts +1 -0
  229. package/dist/types/protection/toolResult.d.ts +69 -0
  230. package/dist/types/provider-registration.d.ts +1 -1
  231. package/dist/types/rerank/bm25.d.ts +1 -1
  232. package/dist/types/rerank/core.d.ts +17 -17
  233. package/dist/types/rerank/crossEncoder.d.ts +3 -3
  234. package/dist/types/rerank/fallback.d.ts +1 -1
  235. package/dist/types/rerank/index.d.ts +2 -2
  236. package/dist/types/rerank/outcome.d.ts +2 -2
  237. package/dist/types/rerank/pin.d.ts +2 -2
  238. package/dist/types/rerank/search.d.ts +1 -1
  239. package/dist/types/rerank/systemone.d.ts +4 -4
  240. package/dist/types/rerank/types.d.ts +1 -1
  241. package/dist/types/run.d.ts +1 -0
  242. package/dist/types/runtime/admission.d.ts +1 -1
  243. package/dist/types/runtime/checkpoint.d.ts +1 -1
  244. package/dist/types/runtime/client.d.ts +2 -2
  245. package/dist/types/runtime/files.d.ts +3 -3
  246. package/dist/types/runtime/hostTools.d.ts +2 -2
  247. package/dist/types/runtime/index.d.ts +12 -12
  248. package/dist/types/runtime/model.d.ts +5 -5
  249. package/dist/types/runtime/policy.d.ts +3 -3
  250. package/dist/types/runtime/recovery/client.d.ts +3 -3
  251. package/dist/types/runtime/recovery/log.d.ts +2 -2
  252. package/dist/types/runtime/recovery/redis.d.ts +1 -1
  253. package/dist/types/runtime/recovery/server.d.ts +3 -3
  254. package/dist/types/runtime/resume.d.ts +1 -1
  255. package/dist/types/runtime/server.d.ts +3 -3
  256. package/dist/types/runtime/snapshot.d.ts +1 -1
  257. package/dist/types/runtime/types.d.ts +2 -2
  258. package/dist/types/session/AgentSession.d.ts +2 -2
  259. package/dist/types/session/JsonlSessionStore.d.ts +1 -1
  260. package/dist/types/session/deriveMessages.d.ts +1 -1
  261. package/dist/types/session/handlers.d.ts +1 -1
  262. package/dist/types/session/index.d.ts +7 -7
  263. package/dist/types/session/messageSerialization.d.ts +1 -1
  264. package/dist/types/session/sessionProjection.d.ts +3 -3
  265. package/dist/types/summarization/index.d.ts +2 -2
  266. package/dist/types/tools/BashProgrammaticToolCalling.d.ts +1 -1
  267. package/dist/types/tools/GitHubCompare.d.ts +6 -6
  268. package/dist/types/tools/ProgrammaticToolCalling.d.ts +1 -1
  269. package/dist/types/tools/ReadFile.d.ts +6 -6
  270. package/dist/types/tools/SkillTool.d.ts +6 -6
  271. package/dist/types/tools/ToolNode.d.ts +5 -1
  272. package/dist/types/tools/cloudflare/index.d.ts +4 -4
  273. package/dist/types/tools/handlers.d.ts +13 -0
  274. package/dist/types/tools/local/FileCheckpointer.d.ts +1 -1
  275. package/dist/types/tools/local/LocalExecutionEngine.d.ts +1 -1
  276. package/dist/types/tools/local/attachments.d.ts +1 -1
  277. package/dist/types/tools/local/index.d.ts +12 -12
  278. package/dist/types/tools/search/anthropic.d.ts +1 -1
  279. package/dist/types/tools/search/citations.d.ts +88 -0
  280. package/dist/types/tools/search/content.d.ts +38 -1
  281. package/dist/types/tools/search/crw-scraper.d.ts +1 -1
  282. package/dist/types/tools/search/crw-search.d.ts +1 -1
  283. package/dist/types/tools/search/firecrawl.d.ts +4 -2
  284. package/dist/types/tools/search/format.d.ts +1 -1
  285. package/dist/types/tools/search/google.d.ts +11 -0
  286. package/dist/types/tools/search/highlights.d.ts +1 -1
  287. package/dist/types/tools/search/index.d.ts +3 -3
  288. package/dist/types/tools/search/keenable-scraper.d.ts +1 -1
  289. package/dist/types/tools/search/keenable-search.d.ts +1 -1
  290. package/dist/types/tools/search/metrics.d.ts +1 -1
  291. package/dist/types/tools/search/openai.d.ts +3 -0
  292. package/dist/types/tools/search/rerankers.d.ts +1 -1
  293. package/dist/types/tools/search/schema.d.ts +6 -6
  294. package/dist/types/tools/search/search.d.ts +1 -1
  295. package/dist/types/tools/search/serper-scraper.d.ts +1 -1
  296. package/dist/types/tools/search/tavily-scraper.d.ts +1 -1
  297. package/dist/types/tools/search/tavily-search.d.ts +1 -1
  298. package/dist/types/tools/search/tool.d.ts +3 -3
  299. package/dist/types/tools/search/types.d.ts +26 -2
  300. package/dist/types/tools/search/utils.d.ts +3 -3
  301. package/dist/types/tools/subagent/SubagentExecutionRegistry.d.ts +1 -1
  302. package/dist/types/tools/subagent/SubagentExecutor.d.ts +6 -3
  303. package/dist/types/tools/subagent/diagnostics.d.ts +1 -1
  304. package/dist/types/tools/subagent/index.d.ts +7 -7
  305. package/dist/types/types/attachments.d.ts +1 -1
  306. package/dist/types/types/graph.d.ts +4 -1
  307. package/dist/types/types/index.d.ts +14 -14
  308. package/dist/types/types/llm.d.ts +2 -2
  309. package/dist/types/types/run.d.ts +3 -1
  310. package/dist/types/types/stream.d.ts +12 -0
  311. package/dist/types/types/subagentTasks.d.ts +2 -2
  312. package/dist/types/types/tools.d.ts +6 -3
  313. package/dist/types/utils/index.d.ts +10 -10
  314. package/dist/types/utils/tokens.d.ts +2 -0
  315. package/package.json +3 -2
  316. package/src/graphs/Graph.ts +125 -3
  317. package/src/harness/providerText.ts +1 -0
  318. package/src/index.ts +10 -0
  319. package/src/llm/anthropic/index.ts +11 -3
  320. package/src/llm/anthropic/utils/container.ts +63 -0
  321. package/src/llm/anthropic/utils/message_inputs.ts +57 -51
  322. package/src/llm/anthropic/utils/message_outputs.ts +12 -39
  323. package/src/llm/anthropic/utils/stream_events.ts +17 -12
  324. package/src/llm/bedrock/index.ts +7 -5
  325. package/src/llm/bedrock/utils/message_inputs.ts +28 -35
  326. package/src/llm/google/utils/common.ts +88 -36
  327. package/src/llm/google/utils/signatures.ts +52 -36
  328. package/src/llm/init.ts +35 -3
  329. package/src/llm/invoke.ts +160 -16
  330. package/src/llm/openai/index.ts +18 -5
  331. package/src/llm/openai/typedContent.ts +110 -0
  332. package/src/llm/prepareProviderRequest.ts +154 -4
  333. package/src/llm/providerTextBatch.ts +158 -0
  334. package/src/llm/providerTextBoundary.ts +119 -13
  335. package/src/messages/anthropicServerTools.ts +24 -0
  336. package/src/messages/core.ts +12 -11
  337. package/src/messages/toolResultTypes.ts +80 -88
  338. package/src/protection/providerText.ts +222 -46
  339. package/src/protection/providerTextAdmission.ts +6 -0
  340. package/src/protection/toolResult.ts +272 -0
  341. package/src/run.ts +37 -5
  342. package/src/runtime/client.ts +6 -0
  343. package/src/stream.ts +23 -2
  344. package/src/tools/ToolNode.ts +112 -17
  345. package/src/tools/handlers.ts +110 -5
  346. package/src/tools/search/citations.ts +375 -0
  347. package/src/tools/search/content.ts +282 -62
  348. package/src/tools/search/firecrawl.ts +13 -16
  349. package/src/tools/search/google.ts +122 -0
  350. package/src/tools/search/openai.ts +118 -0
  351. package/src/tools/search/types.ts +21 -0
  352. package/src/tools/subagent/SubagentExecutor.ts +6 -0
  353. package/src/types/graph.ts +3 -0
  354. package/src/types/run.ts +3 -0
  355. package/src/types/stream.ts +9 -0
  356. package/src/types/tools.ts +3 -0
  357. package/src/utils/tokens.ts +37 -2
@@ -37,6 +37,7 @@ import type {
37
37
  GraphFactoryRequest,
38
38
  } from '@/graphs/graphFactory';
39
39
  import type { OverflowRecoveryPlan } from '@/llm/contextOverflowRecovery';
40
+ import type { GoogleGroundingMetadata } from '@/tools/search/types';
40
41
  import type { FallbackErrorContext } from '@/llm/invoke';
41
42
  import type { HandoffRouting } from './handoff';
42
43
  import type { HookRegistry } from '@/hooks';
@@ -163,6 +164,10 @@ import {
163
164
  getToolContentCharLength,
164
165
  serializeToolContentBounded,
165
166
  } from '@/utils/toolContent';
167
+ import {
168
+ createToolResultGate,
169
+ ToolResultProtectionError,
170
+ } from '@/protection/toolResult';
166
171
  import {
167
172
  appendCallbacks,
168
173
  findCallback,
@@ -180,6 +185,7 @@ import {
180
185
  isThinkingEnabled,
181
186
  getConfiguredCompletionTokens,
182
187
  } from '@/llm/request';
188
+ import { handleNativeSearchCitations, handleToolCalls } from '@/tools/handlers';
183
189
  import { createToolHistoryPreparation } from '@/messages/toolHistoryProjection';
184
190
  import { ToolNode as CustomToolNode, toolsCondition } from '@/tools/ToolNode';
185
191
  import { shouldTraceToolNodeForLangfuse } from '@/langfuseToolOutputTracing';
@@ -199,11 +205,11 @@ import { isRunStepResumeState } from '@/tools/runStepResume';
199
205
  import { resolveLocalToolsForBinding } from '@/tools/local';
200
206
  import { createSummarizeNode } from '@/summarization/node';
201
207
  import { getTruncationStopReason } from '@/llm/truncation';
208
+ import { SearchCitations } from '@/tools/search/citations';
202
209
  import { createSchemaOnlyTools } from '@/tools/schema';
203
210
  import { AgentContext } from '@/agents/AgentContext';
204
211
  import { createFakeStreamingLLM } from '@/llm/fake';
205
212
  import { handoffStateAnnotation } from './handoff';
206
- import { handleToolCalls } from '@/tools/handlers';
207
213
  import { resolveMaxSeals } from '@/llm/preempt';
208
214
  import { initializeModel } from '@/llm/init';
209
215
  import { HandlerRegistry } from '@/events';
@@ -843,6 +849,8 @@ export abstract class Graph<
843
849
  callerSignal?: AbortSignal;
844
850
  /** Set of invoked tool call IDs from non-message run steps completed mid-run, if any */
845
851
  invokedToolIds?: Set<string>;
852
+ /** Provider-native web search sources and the anchors still owed to their citations. */
853
+ searchCitations = new SearchCitations();
846
854
  /** Explicit host policy. Never inferred from missing ToolNode claims. */
847
855
  clientDelegatedToolNames?: ReadonlySet<string>;
848
856
  handlerRegistry: HandlerRegistry | undefined;
@@ -951,6 +959,7 @@ export abstract class Graph<
951
959
  this.messageStepHasToolCalls = new Map();
952
960
  this.prelimMessageIdsByStepKey = new Map();
953
961
  this.invokedToolIds = undefined;
962
+ this.searchCitations = new SearchCitations();
954
963
  this.handlerRegistry = undefined;
955
964
  this.parentToolHandlerRegistry = undefined;
956
965
  this.hookRegistry = undefined;
@@ -1440,6 +1449,8 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
1440
1449
  streamLimits: ResolvedStreamLimits;
1441
1450
  /** Default-off mandatory provider prose release. */
1442
1451
  providerTextProtection?: t.StandardGraphInput['providerTextProtection'];
1452
+ /** Default-off host inspection of selected tools' result text. */
1453
+ toolResultProtection?: t.StandardGraphInput['toolResultProtection'];
1443
1454
  /** Host lookup for attachments carried by reference; read while a provider request is prepared. */
1444
1455
  resolveAttachment?: t.StandardGraphInput['resolveAttachment'];
1445
1456
  /**
@@ -1622,6 +1633,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
1622
1633
  preemption,
1623
1634
  streamLimits,
1624
1635
  providerTextProtection,
1636
+ toolResultProtection,
1625
1637
  resolveAttachment,
1626
1638
  toolExecution,
1627
1639
  clientDelegatedToolNames,
@@ -1654,7 +1666,31 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
1654
1666
  this.streamLimits = resolveStreamLimits(streamLimits);
1655
1667
  if (providerTextProtection != null)
1656
1668
  validateProviderTextProtection(providerTextProtection);
1669
+ if (providerTextProtection?.batching != null) {
1670
+ const agent = agents[0];
1671
+ if (
1672
+ this.isMultiAgentGraph() ||
1673
+ agents.length !== 1 ||
1674
+ preemption != null ||
1675
+ (clientDelegatedToolNames?.length ?? 0) > 0 ||
1676
+ (agent.tools?.length ?? 0) > 0 ||
1677
+ (agent.graphTools?.length ?? 0) > 0 ||
1678
+ (agent.toolDefinitions?.length ?? 0) > 0 ||
1679
+ (agent.toolRegistry?.size ?? 0) > 0 ||
1680
+ (agent.subagentConfigs?.length ?? 0) > 0 ||
1681
+ agent.summarizationEnabled === true ||
1682
+ agent.summarizeOnly === true ||
1683
+ agent.initialSummary != null
1684
+ ) {
1685
+ throw new ProviderTextProtectionError('unsupported');
1686
+ }
1687
+ }
1657
1688
  this.providerTextProtection = providerTextProtection;
1689
+ const toolResultGate =
1690
+ toolResultProtection == null
1691
+ ? undefined
1692
+ : createToolResultGate(toolResultProtection);
1693
+ this.toolResultProtection = toolResultProtection;
1658
1694
  this.resolveAttachment = resolveAttachment;
1659
1695
  this.toolExecution = toolExecution;
1660
1696
  if (
@@ -1667,6 +1703,13 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
1667
1703
  if (clientDelegatedToolNames.some((name) => !name.trim())) {
1668
1704
  throw new Error('Client delegated tool names must be nonempty');
1669
1705
  }
1706
+ if (
1707
+ clientDelegatedToolNames.some(
1708
+ (name) => toolResultGate?.toolNames.has(name) === true
1709
+ )
1710
+ ) {
1711
+ throw new ToolResultProtectionError('unsupported');
1712
+ }
1670
1713
  this.clientDelegatedToolNames = new Set(clientDelegatedToolNames);
1671
1714
  }
1672
1715
 
@@ -1835,6 +1878,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
1835
1878
  new Map()
1836
1879
  );
1837
1880
  this.invokedToolIds = resetIfNotEmpty(this.invokedToolIds, undefined);
1881
+ this.searchCitations = new SearchCitations();
1838
1882
  this.resetPreemptTurnState();
1839
1883
  this.resetPreemptTotals();
1840
1884
  const hasScopedCheckpoint =
@@ -2337,6 +2381,15 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
2337
2381
  return false;
2338
2382
  }
2339
2383
 
2384
+ const anchors = this.searchCitations.release(stepId);
2385
+ if (anchors != null && this.config != null) {
2386
+ await this.dispatchMessageDelta(
2387
+ stepId,
2388
+ { content: [{ type: ContentTypes.TEXT, text: anchors.text }] },
2389
+ anchors.metadata ?? options?.metadata
2390
+ );
2391
+ }
2392
+
2340
2393
  const closedAt = options?.at ?? Date.now();
2341
2394
  runStep.status = status;
2342
2395
  if (status === 'completed') {
@@ -2465,6 +2518,54 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
2465
2518
  await this.closeRunStep(stepId, 'completed', { metadata, at: closeAt });
2466
2519
  }
2467
2520
 
2521
+ /** Gemini grounding and OpenAI url_citation arrive after the text they cite. */
2522
+ private tracksSearchText(metadata?: Record<string, unknown>): boolean {
2523
+ if (metadata == null) {
2524
+ return false;
2525
+ }
2526
+ try {
2527
+ const { provider } = this.getAgentContext(metadata);
2528
+ return (
2529
+ isGoogleLike(provider) ||
2530
+ provider === Providers.OPENAI ||
2531
+ provider === Providers.AZURE
2532
+ );
2533
+ } catch (_e) {
2534
+ return false;
2535
+ }
2536
+ }
2537
+
2538
+ /** Keeps a Gemini chunk's grounding for the lane's model call. */
2539
+ groundSearchCitations(
2540
+ metadata: Record<string, unknown> | undefined,
2541
+ grounding: GoogleGroundingMetadata
2542
+ ): void {
2543
+ this.searchCitations.ground(
2544
+ this.getStepAgentKey(metadata),
2545
+ grounding,
2546
+ metadata
2547
+ );
2548
+ }
2549
+
2550
+ /**
2551
+ * Turns the web search citations a finished agent model call carried into
2552
+ * a `web_search` result and anchors in its text. Model calls outside an
2553
+ * agent node (summaries, tool internals) never own the lane's text.
2554
+ */
2555
+ async settleSearchCitations(
2556
+ metadata?: Record<string, unknown>
2557
+ ): Promise<void> {
2558
+ const node = metadata?.langgraph_node;
2559
+ if (typeof node !== 'string' || !node.startsWith(AGENT)) {
2560
+ return;
2561
+ }
2562
+ const call = this.searchCitations.take(this.getStepAgentKey(metadata));
2563
+ if (call == null) {
2564
+ return;
2565
+ }
2566
+ await handleNativeSearchCitations({ graph: this, call, metadata });
2567
+ }
2568
+
2468
2569
  /**
2469
2570
  * Closes the tracked open MESSAGE_CREATION step for the event's agent lane.
2470
2571
  * Fires on every model end, so the empty-map check short-circuits ahead of
@@ -3031,6 +3132,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
3031
3132
  maxContextTokens: agentContext?.maxContextTokens,
3032
3133
  maxToolResultChars: agentContext?.maxToolResultChars,
3033
3134
  toolOutputRegistry: this.getOrCreateToolOutputRegistry(),
3135
+ toolResultProtection: this.toolResultProtection,
3034
3136
  fileCheckpointer: this.getOrCreateFileCheckpointer(),
3035
3137
  getBreakerSignal: (): AbortSignal => this.breakerAbort.signal,
3036
3138
  getRunScope: (): RunBreakerScope => this.runScope,
@@ -3114,6 +3216,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
3114
3216
  maxContextTokens: agentContext?.maxContextTokens,
3115
3217
  maxToolResultChars: agentContext?.maxToolResultChars,
3116
3218
  toolOutputRegistry: this.getOrCreateToolOutputRegistry(),
3219
+ toolResultProtection: this.toolResultProtection,
3117
3220
  fileCheckpointer: this.getOrCreateFileCheckpointer(),
3118
3221
  getBreakerSignal: (): AbortSignal => this.breakerAbort.signal,
3119
3222
  getRunScope: (): RunBreakerScope => this.runScope,
@@ -5449,6 +5552,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
5449
5552
  onResolutionFailure: this.onSubagentResolutionFailure,
5450
5553
  streamLimits: this.streamLimits,
5451
5554
  providerTextProtection: this.providerTextProtection,
5555
+ toolResultProtection: this.toolResultProtection,
5452
5556
  resolveAttachment: this.resolveAttachment,
5453
5557
  humanInTheLoop: this.humanInTheLoop,
5454
5558
  checkpointer: this.compileOptions?.checkpointer,
@@ -5929,6 +6033,12 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
5929
6033
 
5930
6034
  createWorkflow(): t.CompiledStateWorkflow {
5931
6035
  this.hasCompiledCheckpointer = this.compileOptions?.checkpointer != null;
6036
+ if (
6037
+ this.hasCompiledCheckpointer &&
6038
+ this.providerTextProtection?.batching != null
6039
+ ) {
6040
+ throw new ProviderTextProtectionError('unsupported');
6041
+ }
5932
6042
  const agentNode = this.createAgentNode(this.defaultAgentId);
5933
6043
  const StateAnnotation = Annotation.Root({
5934
6044
  messages: Annotation<BaseMessage[]>({
@@ -6296,11 +6406,23 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
6296
6406
  if (!this.config) {
6297
6407
  throw new Error('No config provided');
6298
6408
  }
6409
+ const content = Array.isArray(delta.content)
6410
+ ? (this.searchCitations.anchor(id, delta.content, metadata) ??
6411
+ delta.content)
6412
+ : delta.content;
6299
6413
  const messageDelta: t.MessageDeltaEvent = {
6300
6414
  id,
6301
- delta,
6415
+ delta: content === delta.content ? delta : { ...delta, content },
6302
6416
  };
6303
- if (hasTextDeltaContent(delta.content)) {
6417
+ if (content != null && this.tracksSearchText(metadata)) {
6418
+ this.searchCitations.track(
6419
+ this.getStepAgentKey(metadata),
6420
+ id,
6421
+ content,
6422
+ metadata
6423
+ );
6424
+ }
6425
+ if (hasTextDeltaContent(content)) {
6304
6426
  this.messageStepHasTextDeltas.add(id);
6305
6427
  }
6306
6428
  const handler = this.handlerRegistry?.getHandler(
@@ -66,6 +66,7 @@ export function createHostProviderTextService(options: {
66
66
  const identity = boundIdentity(options.identity);
67
67
  const policy = options.policy;
68
68
  validateProviderTextProtection(policy);
69
+ if (policy.batching != null) throw new ProviderTextProtectionError('unsupported');
69
70
  const { admission, inspectionTimeoutMs } = options;
70
71
  if (inspectionTimeoutMs !== undefined)
71
72
  validateProviderTextTimeout(inspectionTimeoutMs);
package/src/index.ts CHANGED
@@ -125,16 +125,26 @@ export * from './rerank';
125
125
 
126
126
  export {
127
127
  PROVIDER_TEXT_PROTECTION_VERSION,
128
+ PROVIDER_TEXT_BATCHING_VERSION,
128
129
  ProviderTextProtectionError,
129
130
  serializeProviderTextProtectionError,
130
131
  deserializeProviderTextProtectionError,
131
132
  } from './protection/providerText';
132
133
  export type {
134
+ ProviderTextBatching,
133
135
  ProviderTextProtection,
134
136
  ProviderTextProtectionResult,
135
137
  ProviderTextProtectionErrorCode,
136
138
  ProviderTextProtectionFailure,
137
139
  } from './protection/providerText';
140
+ export {
141
+ TOOL_RESULT_PROTECTION_VERSION,
142
+ ToolResultProtectionError,
143
+ } from './protection/toolResult';
144
+ export type {
145
+ ToolResultProtection,
146
+ ToolResultProtectionResult,
147
+ } from './protection/toolResult';
138
148
 
139
149
  export {
140
150
  GEMINI_PARTS,
@@ -35,6 +35,7 @@ import {
35
35
  import { _makeMessageChunkFromAnthropicEvent } from './utils/message_outputs';
36
36
  import { convertAnthropicStream } from './utils/stream_events';
37
37
  import { getCurrentClaudeDefaultMaxTokens } from './defaults';
38
+ import { withCurrentTurnContainer } from './utils/container';
38
39
  import { handleToolChoice } from './utils/tools';
39
40
 
40
41
  type StreamTokenType = 'string' | 'input' | 'content';
@@ -642,7 +643,9 @@ export class CustomAnthropic extends ChatAnthropicMessages {
642
643
  options: this['ParsedCallOptions'],
643
644
  _runManager?: CallbackManagerForLLMRun
644
645
  ): AsyncGenerator<ChatModelStreamEvent> {
645
- const params = this.invocationParams(options);
646
+ const params = this.invocationParams(
647
+ withCurrentTurnContainer(messages, options)
648
+ );
646
649
  const formattedMessages = _convertMessagesToAnthropicPayload(messages);
647
650
  const payload = stripUnsupportedAssistantPrefill({
648
651
  ...params,
@@ -666,7 +669,9 @@ export class CustomAnthropic extends ChatAnthropicMessages {
666
669
  runManager?: CallbackManagerForLLMRun
667
670
  ): AsyncGenerator<ChatGenerationChunk> {
668
671
  this.resetTokenEvents();
669
- const params = this.invocationParams(options);
672
+ const params = this.invocationParams(
673
+ withCurrentTurnContainer(messages, options)
674
+ );
670
675
  const formattedMessages = _convertMessagesToAnthropicPayload(messages);
671
676
  const payload = stripUnsupportedAssistantPrefill({
672
677
  ...params,
@@ -740,7 +745,10 @@ export class CustomAnthropic extends ChatAnthropicMessages {
740
745
  if (
741
746
  !tokenType ||
742
747
  tokenType === 'input' ||
743
- (token === '' && (chunk.usage_metadata != null || chunk.id != null))
748
+ (token === '' &&
749
+ (chunk.usage_metadata != null ||
750
+ chunk.id != null ||
751
+ chunk.additional_kwargs.container != null))
744
752
  ) {
745
753
  yield {
746
754
  text: '',
@@ -0,0 +1,63 @@
1
+ import type { BaseMessage } from '@langchain/core/messages';
2
+ import type { InjectedMessage } from '@/types/tools';
3
+
4
+ const IN_TURN_SOURCES: ReadonlySet<string> = new Set<
5
+ NonNullable<InjectedMessage['source']> | 'routing' | 'handoff'
6
+ >(['skill', 'hook', 'system', 'steer', 'routing', 'handoff']);
7
+
8
+ function getContainerId(container: unknown): string | undefined {
9
+ if (
10
+ typeof container !== 'object' ||
11
+ container === null ||
12
+ !('id' in container)
13
+ ) {
14
+ return undefined;
15
+ }
16
+ return typeof container.id === 'string' && container.id !== ''
17
+ ? container.id
18
+ : undefined;
19
+ }
20
+
21
+ export function withCurrentTurnContainer<T extends { container?: string }>(
22
+ messages: readonly BaseMessage[],
23
+ options: T
24
+ ): T {
25
+ if (options.container != null) {
26
+ return options;
27
+ }
28
+ for (let i = messages.length - 1; i >= 0; i--) {
29
+ const message = messages[i];
30
+ const kwargs = message.additional_kwargs;
31
+ const type = message.getType();
32
+ if (type === 'human') {
33
+ if (
34
+ kwargs.injected !== true &&
35
+ !(
36
+ typeof kwargs.source === 'string' &&
37
+ IN_TURN_SOURCES.has(kwargs.source)
38
+ )
39
+ ) {
40
+ break;
41
+ }
42
+ }
43
+ if (type !== 'ai') {
44
+ continue;
45
+ }
46
+ const metadata = message.response_metadata as {
47
+ model_provider?: string;
48
+ container?: { id?: string };
49
+ };
50
+ if (
51
+ metadata.model_provider != null &&
52
+ metadata.model_provider !== 'anthropic'
53
+ ) {
54
+ continue;
55
+ }
56
+ const container =
57
+ getContainerId(kwargs.container) ?? getContainerId(metadata.container);
58
+ if (container != null) {
59
+ return { ...options, container };
60
+ }
61
+ }
62
+ return options;
63
+ }
@@ -28,7 +28,6 @@ import {
28
28
  AnthropicThinkingBlockParam,
29
29
  AnthropicRedactedThinkingBlockParam,
30
30
  AnthropicServerToolUseBlockParam,
31
- AnthropicWebSearchToolResultBlockParam,
32
31
  isAnthropicImageBlockParam,
33
32
  AnthropicSearchResultBlockParam,
34
33
  AnthropicCompactionBlockParam,
@@ -38,6 +37,7 @@ import {
38
37
  cloneToolMessageWithContent,
39
38
  serializeStructuredValueBounded,
40
39
  } from '@/utils/toolContent';
40
+ import { isAnthropicServerToolResultType } from '@/messages/anthropicServerTools';
41
41
  import { isReasoningContentBlock } from '@/messages/reasoningTypes';
42
42
  import { HARD_MAX_TOOL_RESULT_CHARS } from '@/utils/truncation';
43
43
  import { Constants } from '@/common';
@@ -167,9 +167,10 @@ function hoistToolResultCacheControl(
167
167
  return { content, cacheControl: undefined };
168
168
  }
169
169
  let cacheControl: unknown;
170
- const stripped = content.map((block) => {
170
+ const stripped = content.map((part) => {
171
+ const block = part as MessageContentComplex | null;
171
172
  if (typeof block !== 'object' || block == null || isProxy(block)) {
172
- return block;
173
+ return part;
173
174
  }
174
175
  try {
175
176
  const descriptors = Object.getOwnPropertyDescriptors(block);
@@ -210,7 +211,7 @@ function unwrapToolMessageResultContent(content: ToolMessage['content']): {
210
211
  return { content, cacheControl: undefined, isError: undefined };
211
212
  }
212
213
 
213
- const block = content[0];
214
+ const block = content[0] as MessageContentComplex | null;
214
215
  if (typeof block !== 'object' || block == null || isProxy(block)) {
215
216
  return { content, cacheControl: undefined, isError: undefined };
216
217
  }
@@ -650,7 +651,6 @@ function _formatContent(message: BaseMessage) {
650
651
  'tool_result',
651
652
  'input_json_delta',
652
653
  'server_tool_use',
653
- 'web_search_tool_result',
654
654
  'web_search_result',
655
655
  ];
656
656
  const textTypes = ['text', 'text_delta'];
@@ -694,46 +694,60 @@ function _formatContent(message: BaseMessage) {
694
694
  const corrected: AnthropicServerToolUseBlockParam = {
695
695
  type: 'server_tool_use',
696
696
  id: rawPart.id as string,
697
- name: (rawPart.name ?? 'web_search') as 'web_search',
697
+ name: (rawPart.name ??
698
+ 'web_search') as AnthropicServerToolUseBlockParam['name'],
698
699
  input: coerceAnthropicToolUseInput(rawPart.input ?? rawPart.args),
700
+ ...(rawPart.caller != null
701
+ ? {
702
+ caller:
703
+ rawPart.caller as AnthropicServerToolUseBlockParam['caller'],
704
+ }
705
+ : {}),
706
+ ...(rawPart.cache_control != null
707
+ ? {
708
+ cache_control:
709
+ rawPart.cache_control as AnthropicServerToolUseBlockParam['cache_control'],
710
+ }
711
+ : {}),
699
712
  };
700
713
  return corrected;
701
714
  }
702
715
 
703
- /**
704
- * Normalize web_search_tool_result blocks into a clean shape.
705
- * Same rationale as above — the block may carry extra properties from
706
- * streaming (input, index, etc.) that the API rejects. Rebuild cleanly.
707
- */
716
+ const isServerResult = isAnthropicServerToolResultType(contentPart.type);
708
717
  if (
718
+ isServerResult ||
719
+ ((contentPart.type === 'text' || contentPart.type === 'tool_result') &&
709
720
  'tool_use_id' in contentPart &&
710
- typeof (contentPart as Record<string, unknown>).tool_use_id ===
711
- 'string' &&
712
- (
713
- (contentPart as Record<string, unknown>).tool_use_id as string
714
- ).startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) &&
715
- 'content' in contentPart
721
+ typeof contentPart.tool_use_id === 'string' &&
722
+ contentPart.tool_use_id.startsWith(
723
+ Constants.ANTHROPIC_SERVER_TOOL_PREFIX
724
+ ))
716
725
  ) {
717
- const rawPart = contentPart as Record<string, unknown>;
718
- const content = rawPart.content;
719
- const isValidContent =
720
- Array.isArray(content) ||
721
- (content != null &&
722
- typeof content === 'object' &&
723
- 'type' in content &&
724
- (content as Record<string, unknown>).type ===
725
- 'web_search_tool_result_error');
726
-
727
- if (isValidContent) {
728
- const corrected: AnthropicWebSearchToolResultBlockParam = {
729
- type: 'web_search_tool_result',
730
- tool_use_id: rawPart.tool_use_id as string,
731
- content:
732
- content as AnthropicWebSearchToolResultBlockParam['content'],
733
- };
734
- return corrected;
726
+ const { tool_use_id, content, caller, cache_control } =
727
+ contentPart as Record<string, unknown>;
728
+ const type = isServerResult
729
+ ? contentPart.type
730
+ : 'web_search_tool_result';
731
+ if (
732
+ typeof tool_use_id !== 'string' ||
733
+ !tool_use_id.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) ||
734
+ content == null ||
735
+ typeof content !== 'object' ||
736
+ (type === 'web_search_tool_result'
737
+ ? !Array.isArray(content) &&
738
+ (!('type' in content) ||
739
+ content.type !== 'web_search_tool_result_error')
740
+ : Array.isArray(content))
741
+ ) {
742
+ return null;
735
743
  }
736
- return null;
744
+ return {
745
+ type,
746
+ tool_use_id,
747
+ content,
748
+ ...(caller != null ? { caller } : {}),
749
+ ...(cache_control != null ? { cache_control } : {}),
750
+ };
737
751
  }
738
752
 
739
753
  /**
@@ -749,22 +763,14 @@ function _formatContent(message: BaseMessage) {
749
763
  typeof contentPart.name === 'string' &&
750
764
  'args' in contentPart
751
765
  ) {
752
- let args = contentPart.args;
753
- if (typeof args === 'string') {
754
- try {
755
- args = JSON.parse(args);
756
- } catch {
757
- args = {};
758
- }
759
- }
760
- if (args == null || typeof args !== 'object' || Array.isArray(args)) {
761
- args = {};
762
- }
763
- return _convertLangChainToolCallToAnthropic({
764
- id: contentPart.id,
766
+ const caller = 'caller' in contentPart ? contentPart.caller : undefined;
767
+ return {
768
+ type: 'tool_use' as const,
769
+ id: normalizeAnthropicToolCallId(contentPart.id),
765
770
  name: contentPart.name,
766
- args: args as Record<string, unknown>,
767
- });
771
+ input: coerceAnthropicToolUseInput(contentPart.args),
772
+ ...(caller != null ? { caller } : {}),
773
+ };
768
774
  }
769
775
 
770
776
  /**
@@ -6,6 +6,7 @@ import type { ChatGeneration } from '@langchain/core/outputs';
6
6
  import type Anthropic from '@anthropic-ai/sdk';
7
7
  import type { AnthropicMessageResponse } from '../types';
8
8
  import type { MessageContentComplex } from '@/types';
9
+ import { isAnthropicServerToolResultType } from '@/messages/anthropicServerTools';
9
10
  import { toLangChainContent } from '@/messages/langchain';
10
11
  import { extractToolCalls } from './output_parsers';
11
12
 
@@ -52,6 +53,8 @@ function _isAnthropicCompactionBlock(
52
53
  );
53
54
  }
54
55
 
56
+ const OTHER_REPLAYABLE_BLOCK_TYPES: ReadonlySet<string> = new Set(['tool_use', 'document', 'server_tool_use']);
57
+
55
58
  export function _makeMessageChunkFromAnthropicEvent(
56
59
  data: Anthropic.Beta.Messages.BetaRawMessageStreamEvent,
57
60
  fields: {
@@ -115,47 +118,17 @@ export function _makeMessageChunkFromAnthropicEvent(
115
118
  };
116
119
  } else if (
117
120
  data.type === 'content_block_start' &&
118
- [
119
- 'tool_use',
120
- 'document',
121
- 'server_tool_use',
122
- 'web_search_tool_result',
123
- ].includes(data.content_block.type)
121
+ (OTHER_REPLAYABLE_BLOCK_TYPES.has(data.content_block.type) ||
122
+ isAnthropicServerToolResultType(data.content_block.type))
124
123
  ) {
125
- const contentBlock = data.content_block;
126
- let toolCallChunks: ToolCallChunk[];
127
- if (contentBlock.type === 'tool_use') {
128
- toolCallChunks = [
129
- {
130
- id: contentBlock.id,
131
- index: data.index,
132
- name: contentBlock.name,
133
- args: '',
134
- },
135
- ];
136
- } else if (contentBlock.type === 'server_tool_use') {
137
- // Handle anthropic built-in server tool use (like web search)
138
- toolCallChunks = [
139
- {
140
- id: contentBlock.id,
141
- index: data.index,
142
- name: contentBlock.name,
143
- args: '',
144
- },
145
- ];
146
- } else {
147
- toolCallChunks = [];
148
- }
124
+ const block = data.content_block;
125
+ const isToolUse =
126
+ block.type === 'tool_use' || block.type === 'server_tool_use';
127
+ const toolCallChunks: ToolCallChunk[] = isToolUse
128
+ ? [{ id: block.id, index: data.index, name: block.name, args: '' }]
129
+ : [];
149
130
  const content = [
150
- {
151
- index: data.index,
152
- ...data.content_block,
153
- input:
154
- contentBlock.type === 'server_tool_use' ||
155
- contentBlock.type === 'tool_use'
156
- ? ''
157
- : undefined,
158
- },
131
+ { ...block, index: data.index, ...(isToolUse ? { input: '' } : {}) },
159
132
  ];
160
133
  return {
161
134
  chunk: new AIMessageChunk({