@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
@@ -0,0 +1,158 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import { performance } from 'node:perf_hooks';
3
+ import { AIMessageChunk } from '@langchain/core/messages';
4
+ import type { ChatGenerationChunk } from '@langchain/core/outputs';
5
+ import type {
6
+ ProviderTextAttempt,
7
+ ProviderTextBatching,
8
+ } from '@/protection/providerText';
9
+ import { ProviderTextProtectionError } from '@/protection/providerText';
10
+
11
+ /** A single provider read can overlap inspection or downstream publication. */
12
+ export class ProviderTextBatch {
13
+ private parts: string[] = [];
14
+ private freshBytes = 0;
15
+ private bytes = 0;
16
+ private stagedText = 0;
17
+ private stagedControl = 0;
18
+ private reading = false;
19
+ private next?: IteratorResult<ChatGenerationChunk>;
20
+ private timer?: ReturnType<typeof setTimeout>;
21
+ private deadline = Infinity;
22
+ private wake?: () => void;
23
+ private closed = false;
24
+
25
+ constructor(
26
+ private readonly attempt: ProviderTextAttempt,
27
+ private readonly options: ProviderTextBatching,
28
+ private readonly admit: (chunk: ChatGenerationChunk) => number
29
+ ) {}
30
+
31
+ private prefetch(source: AsyncGenerator<ChatGenerationChunk>): void {
32
+ if (this.reading || this.next != null) return;
33
+ this.reading = true;
34
+ void this.attempt
35
+ .wait(source.next())
36
+ .then((next) => {
37
+ this.reading = false;
38
+ if (this.closed) return;
39
+ this.attempt.check();
40
+ if (next.done !== true) {
41
+ const { content } = next.value.message;
42
+ if (
43
+ typeof content !== 'string' ||
44
+ !(next.value.message instanceof AIMessageChunk)
45
+ ) {
46
+ throw new ProviderTextProtectionError('unsupported');
47
+ }
48
+ this.stagedControl = this.admit(next.value);
49
+ // Raw fragments, their joined copy, and detached prefix/tail during a split.
50
+ this.stagedText = content === '' ? 0 : content.length * 8 + 256;
51
+ this.attempt.retain(this.stagedText + this.stagedControl);
52
+ }
53
+ this.next = next;
54
+ this.wake?.();
55
+ })
56
+ .catch((error: unknown) => {
57
+ if (this.closed) return;
58
+ this.attempt.stop(
59
+ error instanceof ProviderTextProtectionError
60
+ ? error
61
+ : new ProviderTextProtectionError('unavailable')
62
+ );
63
+ this.wake?.();
64
+ });
65
+ }
66
+
67
+ async read(
68
+ source: AsyncGenerator<ChatGenerationChunk>
69
+ ): Promise<IteratorResult<ChatGenerationChunk> | 'flush'> {
70
+ this.attempt.check();
71
+ this.prefetch(source);
72
+ for (;;) {
73
+ this.attempt.check();
74
+ if (
75
+ this.freshBytes >= this.options.batchBytes ||
76
+ performance.now() >= this.deadline
77
+ )
78
+ return 'flush';
79
+ if (this.next != null) {
80
+ const next = this.next;
81
+ this.next = undefined;
82
+ return next;
83
+ }
84
+ // Only one waiter, so a timer shared across a batch does not accumulate race listeners.
85
+ const signal = this.attempt.signal;
86
+ const ready = Promise.withResolvers<void>();
87
+ this.wake = (): void => ready.resolve();
88
+ signal.addEventListener('abort', this.wake, { once: true });
89
+ try {
90
+ if (!signal.aborted) await ready.promise;
91
+ } finally {
92
+ signal.removeEventListener('abort', this.wake);
93
+ this.wake = undefined;
94
+ }
95
+ }
96
+ }
97
+
98
+ append(text: string): void {
99
+ const bytes = this.stagedText;
100
+ this.stagedText = 0;
101
+ this.attempt.releaseWindow(this.stagedControl);
102
+ this.stagedControl = 0;
103
+ if (text === '') return;
104
+ this.parts.push(text);
105
+ this.freshBytes += text.length * 2;
106
+ this.bytes += bytes;
107
+ if (this.timer == null) {
108
+ this.deadline = performance.now() + this.options.flushMs;
109
+ this.timer = setTimeout(() => {
110
+ this.deadline = 0;
111
+ this.wake?.();
112
+ }, this.options.flushMs);
113
+ this.timer.unref();
114
+ }
115
+ }
116
+
117
+ async flush(final: boolean): Promise<string> {
118
+ this.resetTimer();
119
+ this.freshBytes = 0;
120
+ const text = this.parts.length === 1 ? this.parts[0] : this.parts.join('');
121
+ if (text === '') return '';
122
+ const approved = await this.attempt.inspect(text);
123
+ if (approved !== text)
124
+ throw new ProviderTextProtectionError('incompatible');
125
+ if (final) return text;
126
+ let end = Math.max(0, text.length - (this.options.maxMatchChars - 1));
127
+ if (end > 0) {
128
+ const previous = text.charCodeAt(end - 1);
129
+ if (previous >= 0xd800 && previous <= 0xdbff) end--;
130
+ }
131
+ if (end === 0) return '';
132
+ const tail = end === text.length ? '' : detach(text.slice(end));
133
+ const prefix = tail === '' ? text : detach(text.slice(0, end));
134
+ const retained = tail === '' ? 0 : tail.length * 8 + 256;
135
+ this.parts = tail === '' ? [] : [tail];
136
+ this.attempt.releaseWindow(this.bytes - retained);
137
+ this.bytes = retained;
138
+ return prefix;
139
+ }
140
+
141
+ private resetTimer(): void {
142
+ clearTimeout(this.timer);
143
+ this.timer = undefined;
144
+ this.deadline = Infinity;
145
+ }
146
+
147
+ close(): void {
148
+ this.closed = true;
149
+ this.resetTimer();
150
+ this.parts = [];
151
+ this.next = undefined;
152
+ }
153
+ }
154
+
155
+ /** Avoid keeping an unchecked suffix alive through a sliced, published prefix. */
156
+ function detach(text: string): string {
157
+ return Buffer.from(text, 'utf16le').toString('utf16le');
158
+ }
@@ -21,6 +21,7 @@ import {
21
21
  import { isProviderTextInput } from '@/protection/providerTextInput';
22
22
  import { createProviderTextChunk } from '@/llm/providerTextChunk';
23
23
  import { preserveModelInvocationContext } from '@/llm/invocation';
24
+ import { ProviderTextBatch } from '@/llm/providerTextBatch';
24
25
  import { getChatModelClass } from '@/llm/providers';
25
26
  import { Providers } from '@/common';
26
27
 
@@ -136,6 +137,52 @@ function validateMetadata(metadata: AIMessageChunk['response_metadata'], google
136
137
  }
137
138
  }
138
139
 
140
+ /** The staged frame may contain provider controls as well as raw prose. */
141
+ function batchControls(generation: ChatGenerationChunk): number {
142
+ const message = generation.message as AIMessageChunk;
143
+ if (
144
+ (message.tool_calls?.length ?? 0) > 0 ||
145
+ (message.tool_call_chunks?.length ?? 0) > 0 ||
146
+ (message.invalid_tool_calls?.length ?? 0) > 0 ||
147
+ Object.hasOwn(message.response_metadata, 'output') ||
148
+ generation.text !== message.content
149
+ ) throw new ProviderTextProtectionError('unsupported');
150
+
151
+ for (const key of Object.keys(message.additional_kwargs)) {
152
+ if (!Object.hasOwn(additionalControls, key) || additionalControls[key]?.(message.additional_kwargs[key]) !== true) {
153
+ throw new ProviderTextProtectionError('unsupported');
154
+ }
155
+ }
156
+ validateMetadata(message.response_metadata);
157
+ validateMetadata(generation.generationInfo ?? {});
158
+ if (!numericMetadata(message.usage_metadata)) throw new ProviderTextProtectionError('unsupported');
159
+
160
+ let bytes = 256 + ((message.id?.length ?? 0) + (message.name?.length ?? 0)) * 2;
161
+ for (const record of [message.additional_kwargs, message.response_metadata, generation.generationInfo]) {
162
+ if (record == null) continue;
163
+ for (const [key, value] of Object.entries(record)) {
164
+ bytes += key.length * 2 + metadataBytes(value);
165
+ }
166
+ }
167
+ return bytes + metadataBytes(message.usage_metadata);
168
+ }
169
+
170
+ /** Called only for already validated scalar or bounded numeric metadata. */
171
+ function metadataBytes(value: unknown): number {
172
+ if (typeof value === 'string') return value.length * 2 + 16;
173
+ if (typeof value !== 'object' || value == null) return 16;
174
+ let bytes = 64;
175
+ for (const [key, entry] of Object.entries(value)) bytes += key.length * 2 + metadataBytes(entry);
176
+ return bytes;
177
+ }
178
+
179
+ function batchChunk(text: string): ChatGenerationChunk | undefined {
180
+ return text === '' ? undefined : new ChatGenerationChunk({
181
+ text,
182
+ message: new AIMessageChunk({ content: text }),
183
+ });
184
+ }
185
+
139
186
  class Candidate {
140
187
  private block?: TextBlock;
141
188
  private stringContent = false;
@@ -144,10 +191,23 @@ class Candidate {
144
191
 
145
192
  constructor(private readonly attempt: ProviderTextAttempt, private readonly provider?: ProviderName) { this.responses = new ResponsesTextProjection(attempt); }
146
193
 
147
- strip(generation: ChatGenerationChunk): ChatGenerationChunk {
194
+ strip(generation: ChatGenerationChunk): ChatGenerationChunk;
195
+ strip(generation: ChatGenerationChunk, batch: ProviderTextBatch | undefined): ChatGenerationChunk | undefined;
196
+ strip(generation: ChatGenerationChunk, batch?: ProviderTextBatch): ChatGenerationChunk | undefined {
148
197
  const message = generation.message;
149
198
  if (!(message instanceof AIMessageChunk))
150
199
  throw new ProviderTextProtectionError('unsupported');
200
+ if (batch != null) {
201
+ if (typeof message.content !== 'string') throw new ProviderTextProtectionError('unsupported');
202
+ batch.append(message.content);
203
+ if (message.id == null && message.name == null && message.usage_metadata == null &&
204
+ Object.keys(message.additional_kwargs).length === 0 &&
205
+ Object.keys(message.response_metadata).length === 0 &&
206
+ Object.keys(generation.generationInfo ?? {}).length === 0) return undefined;
207
+ return new ChatGenerationChunk({
208
+ text: '', message: copyMessage(message, ''), generationInfo: generation.generationInfo,
209
+ });
210
+ }
151
211
  this.attempt.observeChunk();
152
212
  for (const key of Object.keys(message.additional_kwargs)) {
153
213
  if (additionalKeys.has(key)) continue;
@@ -303,17 +363,25 @@ function usesInternalStreaming(
303
363
  return false;
304
364
  }
305
365
 
366
+ interface BatchInvocation {
367
+ getAttempt: (signal?: AbortSignal) => ProviderTextAttempt;
368
+ dispatchStart?: (attempt: ProviderTextAttempt) => void | Promise<void>;
369
+ }
370
+
306
371
  /** Clone only known runnable shells. Never mutate a shared provider or its callback configuration. */
307
372
  export function withProviderTextBoundary(
308
373
  model: ChatModel,
309
374
  policy: ProviderTextProtection,
310
- provider?: ProviderName
375
+ provider?: ProviderName,
376
+ batchInvocation?: BatchInvocation
311
377
  ): ChatModel {
312
378
  const protectedModel = clone(model);
313
379
  if (model instanceof RunnableBinding) {
314
380
  assertBindingShell(model);
315
381
  Object.defineProperty(protectedModel, 'bound', {
316
- value: withProviderTextBoundary(model.bound as ChatModel, policy, provider),
382
+ value: withProviderTextBoundary(
383
+ model.bound as ChatModel, policy, provider, batchInvocation
384
+ ),
317
385
  });
318
386
  return protectedModel;
319
387
  }
@@ -330,7 +398,8 @@ export function withProviderTextBoundary(
330
398
  value: withProviderTextBoundary(
331
399
  steps[steps.length - 1] as ChatModel,
332
400
  policy,
333
- provider
401
+ provider,
402
+ batchInvocation
334
403
  ),
335
404
  });
336
405
  return protectedModel;
@@ -354,16 +423,27 @@ export function withProviderTextBoundary(
354
423
  if (model.disableStreaming && usesInternalStreaming(model)) {
355
424
  throw new ProviderTextProtectionError('unsupported');
356
425
  }
426
+ if (policy.batching != null && provider !== Providers.OPENAI) {
427
+ throw new ProviderTextProtectionError('unsupported');
428
+ }
357
429
  const protectedChat = clone(model);
358
430
  protectedChat._streamResponseChunks = async function* (
359
431
  messages,
360
432
  options,
361
433
  runManager
362
434
  ): AsyncGenerator<ChatGenerationChunk> {
363
- const attempt = new ProviderTextAttempt(policy, options.signal);
435
+ const attempt =
436
+ batchInvocation?.getAttempt(options.signal) ??
437
+ new ProviderTextAttempt(policy, options.signal);
364
438
  const candidate = new Candidate(attempt, provider);
439
+ const batch = policy.batching == null ? undefined : new ProviderTextBatch(attempt, policy.batching, batchControls);
440
+ let completed = false;
365
441
  let source: AsyncGenerator<ChatGenerationChunk> | undefined;
366
442
  try {
443
+ if (batch != null) {
444
+ const ready = batchInvocation?.dispatchStart?.(attempt);
445
+ if (ready != null) await ready;
446
+ }
367
447
  source = model._streamResponseChunks(
368
448
  messages,
369
449
  preserveModelInvocationContext(
@@ -374,26 +454,35 @@ export function withProviderTextBoundary(
374
454
  undefined
375
455
  );
376
456
  for (;;) {
377
- const next = await attempt.wait(source.next());
457
+ const next = batch == null ? await attempt.wait(source.next()) : await batch.read(source);
458
+ attempt.check();
459
+ if (next !== 'flush' && next.done === true) break;
460
+ const chunk = next === 'flush'
461
+ ? batchChunk(await batch!.flush(false))
462
+ : candidate.strip(next.value, batch);
463
+ if (chunk == null) continue;
378
464
  attempt.check();
379
- if (next.done === true) break;
380
- const chunk = candidate.strip(next.value);
465
+ if (batch != null && chunk.text !== '') attempt.markTextPublished();
381
466
  yield chunk;
382
- await runManager?.handleLLMNewToken(
383
- '',
467
+ if (batch != null) attempt.check();
468
+ const notification = runManager?.handleLLMNewToken(
469
+ chunk.text,
384
470
  undefined,
385
471
  undefined,
386
472
  undefined,
387
473
  undefined,
388
474
  { chunk }
389
475
  );
476
+ if (batch != null && notification != null) await attempt.wait(notification, false);
477
+ else await notification;
390
478
  }
391
- const canonical = await candidate.canonical();
479
+ const canonical = batch == null ? await candidate.canonical() : batchChunk(await batch.flush(true));
392
480
  attempt.check();
393
481
  if (canonical != null) {
482
+ if (batch != null) attempt.markTextPublished();
394
483
  yield canonical;
395
484
  attempt.check();
396
- await runManager?.handleLLMNewToken(
485
+ const notification = runManager?.handleLLMNewToken(
397
486
  canonical.text,
398
487
  undefined,
399
488
  undefined,
@@ -401,8 +490,23 @@ export function withProviderTextBoundary(
401
490
  undefined,
402
491
  { chunk: canonical }
403
492
  );
493
+ if (batch != null && notification != null) await attempt.wait(notification, false);
494
+ else await notification;
404
495
  }
496
+ completed = true;
497
+ } catch (error) {
498
+ if (batch == null) throw error;
499
+ attempt.check();
500
+ const safe = error instanceof ProviderTextProtectionError
501
+ ? error
502
+ : new ProviderTextProtectionError('unavailable');
503
+ attempt.stop(safe);
504
+ throw safe;
405
505
  } finally {
506
+ if (batch != null) {
507
+ batch.close();
508
+ if (!completed) attempt.stop(new ProviderTextProtectionError('cancelled'));
509
+ }
406
510
  try {
407
511
  if (source != null) void attempt.wait(source.return(undefined)).catch(() => {});
408
512
  } finally {
@@ -413,7 +517,9 @@ export function withProviderTextBoundary(
413
517
  protectedChat._streamChatModelEvents =
414
518
  BaseChatModel.prototype._streamChatModelEvents;
415
519
  protectedChat._generate = async function (messages, options, runManager): Promise<ChatResult> {
416
- if (usesInternalStreaming(model, options)) throw new ProviderTextProtectionError('unsupported');
520
+ if (policy.batching != null || usesInternalStreaming(model, options)) {
521
+ throw new ProviderTextProtectionError('unsupported');
522
+ }
417
523
  const attempt = new ProviderTextAttempt(policy, options.signal);
418
524
  try {
419
525
  const result = await attempt.wait(
@@ -0,0 +1,24 @@
1
+ export const ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE = {
2
+ advisor_tool_result: ['advisor'],
3
+ bash_code_execution_tool_result: ['bash_code_execution'],
4
+ code_execution_tool_result: ['code_execution'],
5
+ text_editor_code_execution_tool_result: ['text_editor_code_execution'],
6
+ tool_search_tool_result: ['tool_search_tool_regex', 'tool_search_tool_bm25'],
7
+ web_fetch_tool_result: ['web_fetch'],
8
+ web_search_tool_result: ['web_search'],
9
+ } as const;
10
+
11
+ type AnthropicServerToolResultType =
12
+ keyof typeof ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE;
13
+
14
+ export const ANTHROPIC_SERVER_TOOL_RESULT_TYPES = Object.keys(
15
+ ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE
16
+ ) as AnthropicServerToolResultType[];
17
+
18
+ export function isAnthropicServerToolResultType(
19
+ type: string | undefined
20
+ ): type is AnthropicServerToolResultType {
21
+ return (
22
+ type != null && Object.hasOwn(ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE, type)
23
+ );
24
+ }
@@ -9,13 +9,13 @@ import {
9
9
  } from '@langchain/core/messages';
10
10
  import type { ContentBlock as LangChainContentBlock } from '@langchain/core/messages';
11
11
  import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
12
- import type { ProviderMessageProvenancePart } from './provenance';
13
- import type * as t from '@/types';
14
12
  import type {
15
13
  ResponsesReplayPosition,
16
14
  ToolHistoryPreparation,
17
15
  OrderedToolHistoryProjection,
18
16
  } from './toolHistoryProjection';
17
+ import type { ProviderMessageProvenancePart } from './provenance';
18
+ import type * as t from '@/types';
19
19
  import {
20
20
  cloneToolMessageWithContent,
21
21
  compactToolContent,
@@ -27,6 +27,13 @@ import {
27
27
  isComputerCallOutputMessage,
28
28
  serializeToolContentBounded,
29
29
  } from '@/utils/toolContent';
30
+ import {
31
+ OPENAI_RESPONSES_REPLAY_POSITIONS_KEY,
32
+ createToolHistoryPreparation,
33
+ getGeneratedImageMimeType,
34
+ getResponsesHistorySource,
35
+ isResponsesReplayPosition,
36
+ } from './toolHistoryProjection';
30
37
  import {
31
38
  getProviderMessageProvenance,
32
39
  getProviderSourceMessageIds,
@@ -34,18 +41,12 @@ import {
34
41
  setProviderMessageProvenance,
35
42
  stampSyntheticProviderMessage,
36
43
  } from './provenance';
44
+ import { ANTHROPIC_SERVER_TOOL_RESULT_TYPES } from './anthropicServerTools';
37
45
  import { HARD_MAX_TOOL_RESULT_CHARS } from '@/utils/truncation';
38
- import { stripAnthropicCacheControl } from './cache';
39
46
  import { isReasoningContentBlock } from './reasoningTypes';
47
+ import { stripAnthropicCacheControl } from './cache';
40
48
  import { ContentTypes, Providers } from '@/common';
41
49
  import { toLangChainContent } from './langchain';
42
- import {
43
- OPENAI_RESPONSES_REPLAY_POSITIONS_KEY,
44
- createToolHistoryPreparation,
45
- getGeneratedImageMimeType,
46
- getResponsesHistorySource,
47
- isResponsesReplayPosition,
48
- } from './toolHistoryProjection';
49
50
 
50
51
  type ReasoningSummary = { summary?: Array<{ text?: string }> };
51
52
  type ReasoningDetail = { type?: string; text?: string };
@@ -91,7 +92,7 @@ const allowedTypesByProvider: Record<string, string[]> = {
91
92
  'thinking',
92
93
  'redacted_thinking',
93
94
  'server_tool_use',
94
- 'web_search_tool_result',
95
+ ...ANTHROPIC_SERVER_TOOL_RESULT_TYPES,
95
96
  'web_search_result',
96
97
  ],
97
98
  [Providers.BEDROCK]: [..._allowedTypes, 'reasoning_content'],