@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
@@ -16,6 +16,7 @@ import type {
16
16
  AnthropicMessageStreamEvent,
17
17
  } from '../types';
18
18
  import type { AnthropicUsageData } from './message_outputs';
19
+ import { isAnthropicServerToolResultType } from '@/messages/anthropicServerTools';
19
20
  import { getAnthropicUsageMetadata } from './message_outputs';
20
21
 
21
22
  type AnthropicContentBlock =
@@ -75,12 +76,17 @@ export async function* convertAnthropicStream(
75
76
  let usageSnapshot: UsageMetadata | undefined;
76
77
  let eventUsage: AnthropicEventUsage | undefined;
77
78
  let stopReason: string | null = null;
79
+ let container: Extract<
80
+ AnthropicMessageStreamEvent,
81
+ { type: 'message_start' }
82
+ >['message']['container'] = null;
78
83
 
79
84
  for await (const data of source) {
80
85
  switch (data.type) {
81
86
  // ── Message lifecycle ──────────────────────────────────
82
87
  case 'message_start': {
83
88
  const { usage, id, model } = data.message;
89
+ container = data.message.container;
84
90
  if (shouldStreamUsage) {
85
91
  eventUsage = {
86
92
  inputTokens: usage.input_tokens,
@@ -106,6 +112,7 @@ export async function* convertAnthropicStream(
106
112
 
107
113
  case 'message_delta': {
108
114
  stopReason = data.delta.stop_reason;
115
+ container = data.delta.container ?? container;
109
116
  if (shouldStreamUsage) {
110
117
  eventUsage = updateEventUsage(eventUsage, data.usage);
111
118
  usageSnapshot = buildUsageSnapshot(eventUsage);
@@ -130,7 +137,10 @@ export async function* convertAnthropicStream(
130
137
  event: 'message-finish' as const,
131
138
  reason: mapStopReason(stopReason),
132
139
  ...(usageSnapshot ? { usage: usageSnapshot } : {}),
133
- responseMetadata: { model_provider: 'anthropic' },
140
+ responseMetadata: {
141
+ model_provider: 'anthropic',
142
+ ...(container != null ? { container } : {}),
143
+ },
134
144
  };
135
145
  yield finishEvent;
136
146
  break;
@@ -295,27 +305,21 @@ function mapBlockToContentBlock(
295
305
  case 'redacted_thinking':
296
306
  return { ...block, index };
297
307
  case 'tool_use':
298
- return {
299
- type: 'tool_call_chunk' as const,
300
- id: block.id,
301
- name: block.name,
302
- args: '',
303
- index,
304
- };
305
308
  case 'server_tool_use':
306
309
  return {
307
- type: 'server_tool_call_chunk' as const,
310
+ type: block.type === 'tool_use' ? 'tool_call_chunk' : 'server_tool_call_chunk',
308
311
  id: block.id,
309
312
  name: block.name,
313
+ ...('caller' in block ? { caller: block.caller } : {}),
310
314
  args: '',
311
315
  index,
312
316
  };
313
- case 'web_search_tool_result':
314
- return { ...block, index };
315
317
  case 'compaction':
316
318
  return { ...block, index };
317
319
  default:
318
- return { type: 'non_standard' as const, value: { ...block }, index };
320
+ return isAnthropicServerToolResultType(block.type)
321
+ ? { ...block, index }
322
+ : { type: 'non_standard' as const, value: { ...block }, index };
319
323
  }
320
324
  }
321
325
 
@@ -463,6 +467,7 @@ function finalizeBlock(accumulated: BlockAccumulator): ContentBlock {
463
467
  id: getStringField(accumulated, 'id'),
464
468
  name: getStringField(accumulated, 'name'),
465
469
  args: parsedArgs,
470
+ ...(accumulated.caller != null ? { caller: accumulated.caller } : {}),
466
471
  } as ContentBlock;
467
472
  }
468
473
 
@@ -58,9 +58,12 @@ import {
58
58
  resolveStreamDelay,
59
59
  isSignalAborted,
60
60
  } from '@/llm/stream/smoother';
61
+ import {
62
+ isBedrockOpenAIModel,
63
+ replaysBedrockReasoning,
64
+ } from './utils/message_inputs';
61
65
  import { modelDisallowsAssistantPrefill } from '@/llm/anthropic/utils/message_inputs';
62
66
  import { linkStreamLimitCanonical } from '@/llm/streamLimits';
63
- import { isBedrockOpenAIModel } from './utils/message_inputs';
64
67
  import { neutralizeHarmonyToolConfig } from './utils/harmony';
65
68
  import { insertBedrockToolCachePoint } from './toolCache';
66
69
 
@@ -271,7 +274,7 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
271
274
  runManager?: CallbackManagerForLLMRun
272
275
  ): Promise<ChatResult> {
273
276
  if (
274
- isBedrockOpenAIModel(this.model) ||
277
+ !replaysBedrockReasoning(this.model) ||
275
278
  modelDisallowsAssistantPrefill(this.model)
276
279
  ) {
277
280
  return this.generateWithLocalConverter(messages, options);
@@ -293,9 +296,8 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
293
296
 
294
297
  /**
295
298
  * Non-streaming Converse through this package's converter, as the stream already uses.
296
- * The parent's converter replays reasoning and sends harmony control tokens as written,
297
- * both of which OpenAI models on Bedrock fail on, and it cannot strip a trailing
298
- * assistant prefill that Claude 4.6 and later reject.
299
+ * The parent's converter cannot omit unsupported reasoning replay, neutralize
300
+ * OpenAI Harmony tokens, or strip unsupported Claude assistant prefill.
299
301
  */
300
302
  private async generateWithLocalConverter(
301
303
  messages: BaseMessage[],
@@ -29,9 +29,9 @@ import type {
29
29
  MessageContentReasoningBlock,
30
30
  } from '../types';
31
31
  import { serializeStructuredValueBounded } from '@/utils/toolContent';
32
- import { neutralizeHarmonyRequest } from './harmony';
33
32
  import { isReasoningContentBlock } from '@/messages/reasoningTypes';
34
33
  import { HARD_MAX_TOOL_RESULT_CHARS } from '@/utils/truncation';
34
+ import { neutralizeHarmonyRequest } from './harmony';
35
35
 
36
36
  /**
37
37
  * Reasoning blocks from other providers, relative to Bedrock. Bedrock's native
@@ -272,8 +272,9 @@ const mimeTypeToDocumentFormat: Partial<Record<string, DocumentFormat>> = {
272
272
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xlsx',
273
273
  };
274
274
 
275
+ /** Decodes natively: `atob` plus a per-character copy takes ~45 ms per MB on the request path. */
275
276
  function base64ToBytes(data: string): Uint8Array {
276
- return Uint8Array.from(atob(data), (char) => char.charCodeAt(0));
277
+ return Buffer.from(data, 'base64');
277
278
  }
278
279
 
279
280
  function getMediaFormat<T extends string>(
@@ -583,9 +584,7 @@ function convertLangChainContentBlockToConverseContentBlock({
583
584
  image: {
584
585
  format,
585
586
  source: {
586
- bytes: Uint8Array.from(atob(imageBlock.data), (c) =>
587
- c.charCodeAt(0)
588
- ),
587
+ bytes: base64ToBytes(imageBlock.data),
589
588
  },
590
589
  },
591
590
  };
@@ -1141,37 +1140,31 @@ export interface ConvertToConverseMessagesOptions {
1141
1140
  model?: string;
1142
1141
  }
1143
1142
 
1144
- /**
1145
- * Whether the model is an identified OpenAI model on Bedrock (`openai.*`, a geo/global
1146
- * inference profile such as `global.openai.*`, or an ARN naming one). Missing model IDs and
1147
- * opaque resource ARNs are not identified.
1148
- */
1143
+ const BEDROCK_NAMED_MODEL_ARN =
1144
+ /^arn:[^:]+:bedrock:[^:]*:[^:]*:(?:foundation-model|inference-profile)\/(.+)$/;
1145
+
1146
+ function bedrockReplayException(
1147
+ model?: string
1148
+ ): 'openai' | 'grok-4.7' | undefined {
1149
+ if (model == null || model === '') return undefined;
1150
+ const normalized = model.toLowerCase();
1151
+ const id = normalized.startsWith('arn:')
1152
+ ? BEDROCK_NAMED_MODEL_ARN.exec(normalized)?.[1]
1153
+ : normalized;
1154
+ if (id == null) return undefined;
1155
+ if (/(?:^|\.)openai\./.test(id)) return 'openai';
1156
+ if (/(?:^|\.)xai\.grok-4\.7(?:$|[-:])/.test(id)) return 'grok-4.7';
1157
+ return undefined;
1158
+ }
1159
+
1160
+ /** Identifies OpenAI models on Bedrock, including named inference profiles. */
1149
1161
  export function isBedrockOpenAIModel(model?: string): boolean {
1150
- if (model == null || model === '') {
1151
- return false;
1152
- }
1153
- let id = model.toLowerCase();
1154
- if (id.startsWith('arn:')) {
1155
- const modelId =
1156
- /^arn:[^:]+:bedrock:[^:]*:[^:]*:(?:foundation-model|inference-profile)\/(.+)$/.exec(
1157
- id
1158
- )?.[1];
1159
- if (modelId == null) {
1160
- return false;
1161
- }
1162
- id = modelId;
1163
- }
1164
- return /(?:^|\.)openai\./.test(id);
1162
+ return bedrockReplayException(model) === 'openai';
1165
1163
  }
1166
1164
 
1167
- /**
1168
- * OpenAI Bedrock models reject prior `reasoningContent`, including on Claude-to-GPT handoffs.
1169
- * Only suppress replay for identified OpenAI targets; other model families retain the existing
1170
- * behavior because their Bedrock reasoning-replay requirements have not been verified here.
1171
- * Missing model IDs and opaque resource ARNs also retain the existing behavior.
1172
- */
1165
+ /** OpenAI and Grok 4.7 reject prior reasoning; opaque resources keep replay. */
1173
1166
  export function replaysBedrockReasoning(model?: string): boolean {
1174
- return !isBedrockOpenAIModel(model);
1167
+ return bedrockReplayException(model) == null;
1175
1168
  }
1176
1169
 
1177
1170
  /**
@@ -1184,8 +1177,8 @@ export function convertToConverseMessages(
1184
1177
  converseMessages: BedrockMessage[];
1185
1178
  converseSystem: BedrockSystemContentBlock[];
1186
1179
  } {
1187
- const openAITarget = isBedrockOpenAIModel(options.model);
1188
- const replayReasoning = !openAITarget;
1180
+ const replayException = bedrockReplayException(options.model);
1181
+ const replayReasoning = replayException == null;
1189
1182
  const converseSystem = messages
1190
1183
  .filter((msg) => msg._getType() === 'system')
1191
1184
  .flatMap((msg) => convertSystemMessageToConverseMessage(msg));
@@ -1251,7 +1244,7 @@ export function convertToConverseMessages(
1251
1244
  );
1252
1245
  }
1253
1246
 
1254
- if (!openAITarget) {
1247
+ if (replayException !== 'openai') {
1255
1248
  return { converseMessages: combinedConverseMessages, converseSystem };
1256
1249
  }
1257
1250
  return {
@@ -78,6 +78,10 @@ type GoogleFunctionCallWithId = FunctionCallPart['functionCall'] & {
78
78
  id?: string;
79
79
  };
80
80
 
81
+ type GoogleFunctionCallPart = FunctionCallPart & {
82
+ functionCall: GoogleFunctionCallWithId;
83
+ };
84
+
81
85
  type GoogleFunctionResponseWithId = {
82
86
  name: string;
83
87
  response: object;
@@ -485,6 +489,75 @@ function _convertLangChainContentToPart(
485
489
  }
486
490
  }
487
491
 
492
+ function convertFallbackContentWithCalls(
493
+ content: MessageContentComplex[],
494
+ isMultimodalModel: boolean,
495
+ functionCalls: GoogleFunctionCallPart[]
496
+ ): Part[] {
497
+ const callsById = new Map<string, number[]>();
498
+ const callNames = new Set<string>();
499
+ for (let i = functionCalls.length - 1; i >= 0; i--) {
500
+ const { id, name } = functionCalls[i].functionCall;
501
+ callNames.add(name);
502
+ if (id == null) continue;
503
+ const indices = callsById.get(id);
504
+ if (indices == null) callsById.set(id, [i]);
505
+ else indices.push(i);
506
+ }
507
+
508
+ const parts: (Part | undefined)[] = [];
509
+ const placed = new Set<number>();
510
+ let slotsByName: Map<string, { indices: number[]; next: number }> | undefined;
511
+ for (const block of content) {
512
+ if ('functionCall' in block && block.functionCall != null) {
513
+ const { id: nativeId, name } =
514
+ block.functionCall as GoogleFunctionCallWithId;
515
+ const id = getGoogleFunctionId(nativeId);
516
+ if (id != null) {
517
+ const index = callsById.get(id)?.pop();
518
+ if (index != null) {
519
+ placed.add(index);
520
+ parts.push(functionCalls[index]);
521
+ }
522
+ } else if (callNames.has(name)) {
523
+ slotsByName ??= new Map();
524
+ let slots = slotsByName.get(name);
525
+ if (slots == null) {
526
+ slots = { indices: [], next: 0 };
527
+ slotsByName.set(name, slots);
528
+ }
529
+ slots.indices.push(parts.length);
530
+ parts.push(undefined);
531
+ }
532
+ continue;
533
+ }
534
+
535
+ const part = _convertLangChainContentToPart(block, isMultimodalModel);
536
+ if (part == null) continue;
537
+ if ('functionCall' in part && part.functionCall != null) {
538
+ const { id, name } = part.functionCall as GoogleFunctionCallWithId;
539
+ if (id != null ? callsById.has(id) : callNames.has(name)) continue;
540
+ }
541
+ parts.push(part);
542
+ }
543
+
544
+ // Reserve exact ID matches before filling any name-only slots.
545
+ for (let i = 0; i < functionCalls.length; i++) {
546
+ if (placed.has(i)) continue;
547
+ const part = functionCalls[i];
548
+ const slots = slotsByName?.get(part.functionCall.name);
549
+ if (slots != null && slots.next < slots.indices.length) {
550
+ parts[slots.indices[slots.next++]] = part;
551
+ } else {
552
+ parts.push(part);
553
+ }
554
+ }
555
+
556
+ return slotsByName == null
557
+ ? (parts as Part[])
558
+ : parts.filter((part) => part != null);
559
+ }
560
+
488
561
  export function convertMessageContentToParts(
489
562
  message: BaseMessage,
490
563
  isMultimodalModel: boolean,
@@ -532,21 +605,7 @@ export function convertMessageContentToParts(
532
605
  const replayParts = restoreGeminiReplayParts(message);
533
606
  if (replayParts != null) return replayParts as Part[];
534
607
 
535
- let functionCalls: FunctionCallPart[] = [];
536
- const messageParts: Part[] = [];
537
-
538
- if (typeof message.content === 'string' && message.content) {
539
- messageParts.push({ text: message.content });
540
- }
541
-
542
- if (Array.isArray(message.content)) {
543
- messageParts.push(
544
- ...(message.content
545
- .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
546
- .filter((p) => p !== undefined) as Part[])
547
- );
548
- }
549
-
608
+ let functionCalls: GoogleFunctionCallPart[] = [];
550
609
  const storedSignatures = message.additional_kwargs[_FUNCTION_CALL_THOUGHT_SIGNATURES_MAP_KEY] as Record<string, string> | undefined;
551
610
  let functionThoughtSignatures = storedSignatures != null && Object.keys(storedSignatures).length > 0 ? storedSignatures : undefined;
552
611
  if (Object.hasOwn(message.additional_kwargs, GEMINI_PARTS) && message.additional_kwargs[GEMINI_REPLAY_UNSUPPORTED] !== true) functionThoughtSignatures = {};
@@ -579,28 +638,21 @@ export function convertMessageContentToParts(
579
638
  });
580
639
  }
581
640
 
582
- const parsedFunctionCallIds = new Set(
583
- functionCalls.flatMap((part) => {
584
- const functionCall = part.functionCall as GoogleFunctionCallWithId;
585
- return functionCall.id != null ? [functionCall.id] : [];
586
- })
587
- );
588
- const parsedFunctionCallNames = new Set(
589
- functionCalls.map((part) => part.functionCall.name)
590
- );
591
- const contentWithoutParsedMirrors = messageParts.filter((part) => {
592
- if (!('functionCall' in part) || part.functionCall == null) {
593
- return true;
594
- }
595
- const functionCall = part.functionCall as GoogleFunctionCallWithId;
596
- return !(
597
- (functionCall.id != null && parsedFunctionCallIds.has(functionCall.id)) ||
598
- (functionCall.id == null &&
599
- parsedFunctionCallNames.has(functionCall.name))
641
+ if (!Array.isArray(message.content)) {
642
+ return typeof message.content === 'string' && message.content
643
+ ? [{ text: message.content }, ...functionCalls]
644
+ : functionCalls;
645
+ }
646
+ if (functionCalls.length > 0) {
647
+ return convertFallbackContentWithCalls(
648
+ message.content,
649
+ isMultimodalModel,
650
+ functionCalls
600
651
  );
601
- });
602
-
603
- return [...contentWithoutParsedMirrors, ...functionCalls];
652
+ }
653
+ return message.content
654
+ .map((block) => _convertLangChainContentToPart(block, isMultimodalModel))
655
+ .filter((part) => part != null);
604
656
  }
605
657
 
606
658
  export function convertBaseMessagesToContent(
@@ -1,8 +1,10 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import { AIMessageChunk, isAIMessage } from '@langchain/core/messages';
3
+ import type { ToolCall } from '@langchain/core/messages/tool';
3
4
  import type { BaseMessage } from '@langchain/core/messages';
4
5
  import type { GeminiPart } from '@langchain/google-common';
5
6
  import type { Part } from '@google/generative-ai';
7
+ import { stripSearchAnchors } from '@/tools/search/citations';
6
8
 
7
9
  export const GEMINI_PARTS = '__gemini_parts__';
8
10
  export const GEMINI_REPLAY_UNSUPPORTED = '__gemini_replay_unsupported__';
@@ -140,43 +142,13 @@ export function captureGeminiReplayParts(
140
142
  delete message.additional_kwargs[GEMINI_REPLAY_UNSUPPORTED];
141
143
  }
142
144
 
143
- /** Returns original parts only when the current message still matches every text hash and call id. */
144
- export function restoreGeminiReplayParts(
145
- message: BaseMessage,
146
- replay: unknown = message.additional_kwargs[GEMINI_PARTS]
145
+ function matchReplayParts(
146
+ replay: readonly GeminiReplayPart[],
147
+ text: string,
148
+ reasoning: string,
149
+ toolCalls: readonly ToolCall[]
147
150
  ): GeminiPart[] | undefined {
148
- if (
149
- message.additional_kwargs[GEMINI_REPLAY_UNSUPPORTED] === true ||
150
- !isAIMessage(message) ||
151
- !isGeminiReplayParts(replay)
152
- )
153
- return undefined;
154
- let text = '';
155
- let contentReasoning: string | undefined;
156
- if (typeof message.content === 'string') text = message.content;
157
- else
158
- for (const block of message.content) {
159
- if (block.type === 'text' && typeof block.text === 'string')
160
- text += block.text;
161
- else if (block.type === 'thinking' && typeof block.thinking === 'string')
162
- contentReasoning = (contentReasoning ?? '') + block.thinking;
163
- else if (
164
- block.type === 'reasoning' &&
165
- typeof block.reasoning === 'string'
166
- )
167
- contentReasoning = (contentReasoning ?? '') + block.reasoning;
168
- else if (!('functionCall' in block)) return undefined;
169
- }
170
- let reasoning = contentReasoning ?? '';
171
- if (typeof message.additional_kwargs.reasoning_content === 'string')
172
- reasoning = message.additional_kwargs.reasoning_content;
173
- if (typeof message.additional_kwargs.reasoning === 'string')
174
- reasoning = message.additional_kwargs.reasoning;
175
- if (contentReasoning != null && contentReasoning !== reasoning)
176
- return undefined;
177
- const calls = new Map(
178
- (message.tool_calls ?? []).map((call) => [call.id, call])
179
- );
151
+ const calls = new Map(toolCalls.map((call) => [call.id, call]));
180
152
  const parts: GeminiPart[] = [];
181
153
  let textOffset = 0;
182
154
  let reasoningOffset = 0;
@@ -220,3 +192,47 @@ export function restoreGeminiReplayParts(
220
192
  ? parts
221
193
  : undefined;
222
194
  }
195
+
196
+ /** Returns original parts only when the current message still matches every text hash and call id. */
197
+ export function restoreGeminiReplayParts(
198
+ message: BaseMessage,
199
+ replay: unknown = message.additional_kwargs[GEMINI_PARTS]
200
+ ): GeminiPart[] | undefined {
201
+ if (
202
+ message.additional_kwargs[GEMINI_REPLAY_UNSUPPORTED] === true ||
203
+ !isAIMessage(message) ||
204
+ !isGeminiReplayParts(replay)
205
+ )
206
+ return undefined;
207
+ let text = '';
208
+ let contentReasoning: string | undefined;
209
+ if (typeof message.content === 'string') text = message.content;
210
+ else
211
+ for (const block of message.content) {
212
+ if (block.type === 'text' && typeof block.text === 'string')
213
+ text += block.text;
214
+ else if (block.type === 'thinking' && typeof block.thinking === 'string')
215
+ contentReasoning = (contentReasoning ?? '') + block.thinking;
216
+ else if (
217
+ block.type === 'reasoning' &&
218
+ typeof block.reasoning === 'string'
219
+ )
220
+ contentReasoning = (contentReasoning ?? '') + block.reasoning;
221
+ else if (!('functionCall' in block)) return undefined;
222
+ }
223
+ let reasoning = contentReasoning ?? '';
224
+ if (typeof message.additional_kwargs.reasoning_content === 'string')
225
+ reasoning = message.additional_kwargs.reasoning_content;
226
+ if (typeof message.additional_kwargs.reasoning === 'string')
227
+ reasoning = message.additional_kwargs.reasoning;
228
+ if (contentReasoning != null && contentReasoning !== reasoning)
229
+ return undefined;
230
+ const toolCalls = message.tool_calls ?? [];
231
+ const parts = matchReplayParts(replay, text, reasoning, toolCalls);
232
+ if (parts != null) return parts;
233
+ /** Citation anchors added to the saved answer are not part of what Gemini signed. */
234
+ const stripped = stripSearchAnchors(text);
235
+ return stripped === text
236
+ ? undefined
237
+ : matchReplayParts(replay, stripped, reasoning, toolCalls);
238
+ }
package/src/llm/init.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { ChatOpenAICallOptions } from '@langchain/openai';
1
2
  import type { Runnable } from '@langchain/core/runnables';
2
3
  import type * as t from '@/types';
3
4
  import {
@@ -101,7 +102,38 @@ export function initializeModel<P extends t.ProviderName>({
101
102
  family === 'openai' ||
102
103
  family === 'google' ||
103
104
  provider === Providers.MOONSHOT;
104
- if (disableToolCalling !== true || !canDisableTools)
105
- return model.bindTools(tools);
106
- return model.bindTools(tools, { tool_choice: 'none' });
105
+ if (disableToolCalling === true && canDisableTools)
106
+ return model.bindTools(tools, { tool_choice: 'none' });
107
+ const options =
108
+ disableToolCalling !== true && isOpenAILike(provider)
109
+ ? getToolCallOptions(clientOptions)
110
+ : undefined;
111
+ if (!options) return model.bindTools(tools);
112
+ return model.bindTools(tools, options);
113
+ }
114
+
115
+ type ToolCallOptions = Pick<
116
+ ChatOpenAICallOptions,
117
+ 'tool_choice' | 'parallel_tool_calls'
118
+ >;
119
+
120
+ /**
121
+ * Reads `tool_choice` and `parallel_tool_calls` from OpenAI-compatible client
122
+ * options. `ChatOpenAI` only honors them as call options, so constructor
123
+ * fields would otherwise be dropped from the request.
124
+ */
125
+ function getToolCallOptions(
126
+ clientOptions: unknown
127
+ ): ToolCallOptions | undefined {
128
+ if (clientOptions == null || typeof clientOptions !== 'object') return;
129
+ const { tool_choice: choice, parallel_tool_calls: parallel } =
130
+ clientOptions as Record<string, unknown>;
131
+ const options: ToolCallOptions = {};
132
+ if (
133
+ typeof choice === 'string' ||
134
+ (choice != null && typeof choice === 'object' && !Array.isArray(choice))
135
+ )
136
+ options.tool_choice = choice as ToolCallOptions['tool_choice'];
137
+ if (typeof parallel === 'boolean') options.parallel_tool_calls = parallel;
138
+ return Object.keys(options).length > 0 ? options : undefined;
107
139
  }