@illuma-ai/agents 1.0.81

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (558) hide show
  1. package/README.md +485 -0
  2. package/dist/cjs/agents/AgentContext.cjs +734 -0
  3. package/dist/cjs/agents/AgentContext.cjs.map +1 -0
  4. package/dist/cjs/common/enum.cjs +190 -0
  5. package/dist/cjs/common/enum.cjs.map +1 -0
  6. package/dist/cjs/events.cjs +172 -0
  7. package/dist/cjs/events.cjs.map +1 -0
  8. package/dist/cjs/graphs/Graph.cjs +1615 -0
  9. package/dist/cjs/graphs/Graph.cjs.map +1 -0
  10. package/dist/cjs/graphs/MultiAgentGraph.cjs +890 -0
  11. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -0
  12. package/dist/cjs/instrumentation.cjs +21 -0
  13. package/dist/cjs/instrumentation.cjs.map +1 -0
  14. package/dist/cjs/llm/anthropic/index.cjs +292 -0
  15. package/dist/cjs/llm/anthropic/index.cjs.map +1 -0
  16. package/dist/cjs/llm/anthropic/types.cjs +50 -0
  17. package/dist/cjs/llm/anthropic/types.cjs.map +1 -0
  18. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +630 -0
  19. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -0
  20. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +218 -0
  21. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -0
  22. package/dist/cjs/llm/anthropic/utils/tools.cjs +29 -0
  23. package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -0
  24. package/dist/cjs/llm/bedrock/index.cjs +282 -0
  25. package/dist/cjs/llm/bedrock/index.cjs.map +1 -0
  26. package/dist/cjs/llm/fake.cjs +97 -0
  27. package/dist/cjs/llm/fake.cjs.map +1 -0
  28. package/dist/cjs/llm/google/index.cjs +216 -0
  29. package/dist/cjs/llm/google/index.cjs.map +1 -0
  30. package/dist/cjs/llm/google/utils/common.cjs +647 -0
  31. package/dist/cjs/llm/google/utils/common.cjs.map +1 -0
  32. package/dist/cjs/llm/openai/index.cjs +1028 -0
  33. package/dist/cjs/llm/openai/index.cjs.map +1 -0
  34. package/dist/cjs/llm/openai/utils/index.cjs +765 -0
  35. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -0
  36. package/dist/cjs/llm/openrouter/index.cjs +212 -0
  37. package/dist/cjs/llm/openrouter/index.cjs.map +1 -0
  38. package/dist/cjs/llm/providers.cjs +43 -0
  39. package/dist/cjs/llm/providers.cjs.map +1 -0
  40. package/dist/cjs/llm/text.cjs +69 -0
  41. package/dist/cjs/llm/text.cjs.map +1 -0
  42. package/dist/cjs/llm/vertexai/index.cjs +329 -0
  43. package/dist/cjs/llm/vertexai/index.cjs.map +1 -0
  44. package/dist/cjs/main.cjs +240 -0
  45. package/dist/cjs/main.cjs.map +1 -0
  46. package/dist/cjs/messages/cache.cjs +387 -0
  47. package/dist/cjs/messages/cache.cjs.map +1 -0
  48. package/dist/cjs/messages/content.cjs +53 -0
  49. package/dist/cjs/messages/content.cjs.map +1 -0
  50. package/dist/cjs/messages/core.cjs +367 -0
  51. package/dist/cjs/messages/core.cjs.map +1 -0
  52. package/dist/cjs/messages/format.cjs +761 -0
  53. package/dist/cjs/messages/format.cjs.map +1 -0
  54. package/dist/cjs/messages/ids.cjs +23 -0
  55. package/dist/cjs/messages/ids.cjs.map +1 -0
  56. package/dist/cjs/messages/prune.cjs +398 -0
  57. package/dist/cjs/messages/prune.cjs.map +1 -0
  58. package/dist/cjs/messages/tools.cjs +96 -0
  59. package/dist/cjs/messages/tools.cjs.map +1 -0
  60. package/dist/cjs/run.cjs +328 -0
  61. package/dist/cjs/run.cjs.map +1 -0
  62. package/dist/cjs/schemas/validate.cjs +324 -0
  63. package/dist/cjs/schemas/validate.cjs.map +1 -0
  64. package/dist/cjs/splitStream.cjs +210 -0
  65. package/dist/cjs/splitStream.cjs.map +1 -0
  66. package/dist/cjs/stream.cjs +620 -0
  67. package/dist/cjs/stream.cjs.map +1 -0
  68. package/dist/cjs/tools/BrowserTools.cjs +248 -0
  69. package/dist/cjs/tools/BrowserTools.cjs.map +1 -0
  70. package/dist/cjs/tools/Calculator.cjs +66 -0
  71. package/dist/cjs/tools/Calculator.cjs.map +1 -0
  72. package/dist/cjs/tools/CodeExecutor.cjs +234 -0
  73. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -0
  74. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +636 -0
  75. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -0
  76. package/dist/cjs/tools/ToolNode.cjs +548 -0
  77. package/dist/cjs/tools/ToolNode.cjs.map +1 -0
  78. package/dist/cjs/tools/ToolSearch.cjs +909 -0
  79. package/dist/cjs/tools/ToolSearch.cjs.map +1 -0
  80. package/dist/cjs/tools/handlers.cjs +255 -0
  81. package/dist/cjs/tools/handlers.cjs.map +1 -0
  82. package/dist/cjs/tools/schema.cjs +31 -0
  83. package/dist/cjs/tools/schema.cjs.map +1 -0
  84. package/dist/cjs/tools/search/anthropic.cjs +40 -0
  85. package/dist/cjs/tools/search/anthropic.cjs.map +1 -0
  86. package/dist/cjs/tools/search/content.cjs +140 -0
  87. package/dist/cjs/tools/search/content.cjs.map +1 -0
  88. package/dist/cjs/tools/search/firecrawl.cjs +179 -0
  89. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -0
  90. package/dist/cjs/tools/search/format.cjs +203 -0
  91. package/dist/cjs/tools/search/format.cjs.map +1 -0
  92. package/dist/cjs/tools/search/highlights.cjs +245 -0
  93. package/dist/cjs/tools/search/highlights.cjs.map +1 -0
  94. package/dist/cjs/tools/search/rerankers.cjs +174 -0
  95. package/dist/cjs/tools/search/rerankers.cjs.map +1 -0
  96. package/dist/cjs/tools/search/schema.cjs +117 -0
  97. package/dist/cjs/tools/search/schema.cjs.map +1 -0
  98. package/dist/cjs/tools/search/search.cjs +566 -0
  99. package/dist/cjs/tools/search/search.cjs.map +1 -0
  100. package/dist/cjs/tools/search/serper-scraper.cjs +132 -0
  101. package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -0
  102. package/dist/cjs/tools/search/tool.cjs +456 -0
  103. package/dist/cjs/tools/search/tool.cjs.map +1 -0
  104. package/dist/cjs/tools/search/utils.cjs +66 -0
  105. package/dist/cjs/tools/search/utils.cjs.map +1 -0
  106. package/dist/cjs/types/graph.cjs +29 -0
  107. package/dist/cjs/types/graph.cjs.map +1 -0
  108. package/dist/cjs/utils/contextAnalytics.cjs +66 -0
  109. package/dist/cjs/utils/contextAnalytics.cjs.map +1 -0
  110. package/dist/cjs/utils/events.cjs +31 -0
  111. package/dist/cjs/utils/events.cjs.map +1 -0
  112. package/dist/cjs/utils/graph.cjs +16 -0
  113. package/dist/cjs/utils/graph.cjs.map +1 -0
  114. package/dist/cjs/utils/handlers.cjs +70 -0
  115. package/dist/cjs/utils/handlers.cjs.map +1 -0
  116. package/dist/cjs/utils/llm.cjs +27 -0
  117. package/dist/cjs/utils/llm.cjs.map +1 -0
  118. package/dist/cjs/utils/misc.cjs +56 -0
  119. package/dist/cjs/utils/misc.cjs.map +1 -0
  120. package/dist/cjs/utils/run.cjs +73 -0
  121. package/dist/cjs/utils/run.cjs.map +1 -0
  122. package/dist/cjs/utils/schema.cjs +27 -0
  123. package/dist/cjs/utils/schema.cjs.map +1 -0
  124. package/dist/cjs/utils/title.cjs +125 -0
  125. package/dist/cjs/utils/title.cjs.map +1 -0
  126. package/dist/cjs/utils/tokens.cjs +125 -0
  127. package/dist/cjs/utils/tokens.cjs.map +1 -0
  128. package/dist/cjs/utils/toonFormat.cjs +388 -0
  129. package/dist/cjs/utils/toonFormat.cjs.map +1 -0
  130. package/dist/esm/agents/AgentContext.mjs +732 -0
  131. package/dist/esm/agents/AgentContext.mjs.map +1 -0
  132. package/dist/esm/common/enum.mjs +190 -0
  133. package/dist/esm/common/enum.mjs.map +1 -0
  134. package/dist/esm/events.mjs +164 -0
  135. package/dist/esm/events.mjs.map +1 -0
  136. package/dist/esm/graphs/Graph.mjs +1612 -0
  137. package/dist/esm/graphs/Graph.mjs.map +1 -0
  138. package/dist/esm/graphs/MultiAgentGraph.mjs +888 -0
  139. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -0
  140. package/dist/esm/instrumentation.mjs +19 -0
  141. package/dist/esm/instrumentation.mjs.map +1 -0
  142. package/dist/esm/llm/anthropic/index.mjs +290 -0
  143. package/dist/esm/llm/anthropic/index.mjs.map +1 -0
  144. package/dist/esm/llm/anthropic/types.mjs +48 -0
  145. package/dist/esm/llm/anthropic/types.mjs.map +1 -0
  146. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +627 -0
  147. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -0
  148. package/dist/esm/llm/anthropic/utils/message_outputs.mjs +216 -0
  149. package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -0
  150. package/dist/esm/llm/anthropic/utils/tools.mjs +27 -0
  151. package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -0
  152. package/dist/esm/llm/bedrock/index.mjs +280 -0
  153. package/dist/esm/llm/bedrock/index.mjs.map +1 -0
  154. package/dist/esm/llm/fake.mjs +94 -0
  155. package/dist/esm/llm/fake.mjs.map +1 -0
  156. package/dist/esm/llm/google/index.mjs +214 -0
  157. package/dist/esm/llm/google/index.mjs.map +1 -0
  158. package/dist/esm/llm/google/utils/common.mjs +638 -0
  159. package/dist/esm/llm/google/utils/common.mjs.map +1 -0
  160. package/dist/esm/llm/openai/index.mjs +1018 -0
  161. package/dist/esm/llm/openai/index.mjs.map +1 -0
  162. package/dist/esm/llm/openai/utils/index.mjs +759 -0
  163. package/dist/esm/llm/openai/utils/index.mjs.map +1 -0
  164. package/dist/esm/llm/openrouter/index.mjs +210 -0
  165. package/dist/esm/llm/openrouter/index.mjs.map +1 -0
  166. package/dist/esm/llm/providers.mjs +39 -0
  167. package/dist/esm/llm/providers.mjs.map +1 -0
  168. package/dist/esm/llm/text.mjs +67 -0
  169. package/dist/esm/llm/text.mjs.map +1 -0
  170. package/dist/esm/llm/vertexai/index.mjs +327 -0
  171. package/dist/esm/llm/vertexai/index.mjs.map +1 -0
  172. package/dist/esm/main.mjs +37 -0
  173. package/dist/esm/main.mjs.map +1 -0
  174. package/dist/esm/messages/cache.mjs +382 -0
  175. package/dist/esm/messages/cache.mjs.map +1 -0
  176. package/dist/esm/messages/content.mjs +51 -0
  177. package/dist/esm/messages/content.mjs.map +1 -0
  178. package/dist/esm/messages/core.mjs +359 -0
  179. package/dist/esm/messages/core.mjs.map +1 -0
  180. package/dist/esm/messages/format.mjs +752 -0
  181. package/dist/esm/messages/format.mjs.map +1 -0
  182. package/dist/esm/messages/ids.mjs +21 -0
  183. package/dist/esm/messages/ids.mjs.map +1 -0
  184. package/dist/esm/messages/prune.mjs +393 -0
  185. package/dist/esm/messages/prune.mjs.map +1 -0
  186. package/dist/esm/messages/tools.mjs +93 -0
  187. package/dist/esm/messages/tools.mjs.map +1 -0
  188. package/dist/esm/run.mjs +325 -0
  189. package/dist/esm/run.mjs.map +1 -0
  190. package/dist/esm/schemas/validate.mjs +317 -0
  191. package/dist/esm/schemas/validate.mjs.map +1 -0
  192. package/dist/esm/splitStream.mjs +207 -0
  193. package/dist/esm/splitStream.mjs.map +1 -0
  194. package/dist/esm/stream.mjs +616 -0
  195. package/dist/esm/stream.mjs.map +1 -0
  196. package/dist/esm/tools/BrowserTools.mjs +244 -0
  197. package/dist/esm/tools/BrowserTools.mjs.map +1 -0
  198. package/dist/esm/tools/Calculator.mjs +41 -0
  199. package/dist/esm/tools/Calculator.mjs.map +1 -0
  200. package/dist/esm/tools/CodeExecutor.mjs +226 -0
  201. package/dist/esm/tools/CodeExecutor.mjs.map +1 -0
  202. package/dist/esm/tools/ProgrammaticToolCalling.mjs +622 -0
  203. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -0
  204. package/dist/esm/tools/ToolNode.mjs +545 -0
  205. package/dist/esm/tools/ToolNode.mjs.map +1 -0
  206. package/dist/esm/tools/ToolSearch.mjs +870 -0
  207. package/dist/esm/tools/ToolSearch.mjs.map +1 -0
  208. package/dist/esm/tools/handlers.mjs +250 -0
  209. package/dist/esm/tools/handlers.mjs.map +1 -0
  210. package/dist/esm/tools/schema.mjs +28 -0
  211. package/dist/esm/tools/schema.mjs.map +1 -0
  212. package/dist/esm/tools/search/anthropic.mjs +37 -0
  213. package/dist/esm/tools/search/anthropic.mjs.map +1 -0
  214. package/dist/esm/tools/search/content.mjs +119 -0
  215. package/dist/esm/tools/search/content.mjs.map +1 -0
  216. package/dist/esm/tools/search/firecrawl.mjs +176 -0
  217. package/dist/esm/tools/search/firecrawl.mjs.map +1 -0
  218. package/dist/esm/tools/search/format.mjs +201 -0
  219. package/dist/esm/tools/search/format.mjs.map +1 -0
  220. package/dist/esm/tools/search/highlights.mjs +243 -0
  221. package/dist/esm/tools/search/highlights.mjs.map +1 -0
  222. package/dist/esm/tools/search/rerankers.mjs +168 -0
  223. package/dist/esm/tools/search/rerankers.mjs.map +1 -0
  224. package/dist/esm/tools/search/schema.mjs +104 -0
  225. package/dist/esm/tools/search/schema.mjs.map +1 -0
  226. package/dist/esm/tools/search/search.mjs +563 -0
  227. package/dist/esm/tools/search/search.mjs.map +1 -0
  228. package/dist/esm/tools/search/serper-scraper.mjs +129 -0
  229. package/dist/esm/tools/search/serper-scraper.mjs.map +1 -0
  230. package/dist/esm/tools/search/tool.mjs +454 -0
  231. package/dist/esm/tools/search/tool.mjs.map +1 -0
  232. package/dist/esm/tools/search/utils.mjs +61 -0
  233. package/dist/esm/tools/search/utils.mjs.map +1 -0
  234. package/dist/esm/types/graph.mjs +26 -0
  235. package/dist/esm/types/graph.mjs.map +1 -0
  236. package/dist/esm/utils/contextAnalytics.mjs +64 -0
  237. package/dist/esm/utils/contextAnalytics.mjs.map +1 -0
  238. package/dist/esm/utils/events.mjs +29 -0
  239. package/dist/esm/utils/events.mjs.map +1 -0
  240. package/dist/esm/utils/graph.mjs +13 -0
  241. package/dist/esm/utils/graph.mjs.map +1 -0
  242. package/dist/esm/utils/handlers.mjs +68 -0
  243. package/dist/esm/utils/handlers.mjs.map +1 -0
  244. package/dist/esm/utils/llm.mjs +24 -0
  245. package/dist/esm/utils/llm.mjs.map +1 -0
  246. package/dist/esm/utils/misc.mjs +53 -0
  247. package/dist/esm/utils/misc.mjs.map +1 -0
  248. package/dist/esm/utils/run.mjs +70 -0
  249. package/dist/esm/utils/run.mjs.map +1 -0
  250. package/dist/esm/utils/schema.mjs +24 -0
  251. package/dist/esm/utils/schema.mjs.map +1 -0
  252. package/dist/esm/utils/title.mjs +122 -0
  253. package/dist/esm/utils/title.mjs.map +1 -0
  254. package/dist/esm/utils/tokens.mjs +121 -0
  255. package/dist/esm/utils/tokens.mjs.map +1 -0
  256. package/dist/esm/utils/toonFormat.mjs +381 -0
  257. package/dist/esm/utils/toonFormat.mjs.map +1 -0
  258. package/dist/types/agents/AgentContext.d.ts +293 -0
  259. package/dist/types/common/enum.d.ts +155 -0
  260. package/dist/types/common/index.d.ts +1 -0
  261. package/dist/types/events.d.ts +31 -0
  262. package/dist/types/graphs/Graph.d.ts +216 -0
  263. package/dist/types/graphs/MultiAgentGraph.d.ts +104 -0
  264. package/dist/types/graphs/index.d.ts +2 -0
  265. package/dist/types/index.d.ts +21 -0
  266. package/dist/types/instrumentation.d.ts +1 -0
  267. package/dist/types/llm/anthropic/index.d.ts +39 -0
  268. package/dist/types/llm/anthropic/types.d.ts +37 -0
  269. package/dist/types/llm/anthropic/utils/message_inputs.d.ts +14 -0
  270. package/dist/types/llm/anthropic/utils/message_outputs.d.ts +14 -0
  271. package/dist/types/llm/anthropic/utils/output_parsers.d.ts +22 -0
  272. package/dist/types/llm/anthropic/utils/tools.d.ts +3 -0
  273. package/dist/types/llm/bedrock/index.d.ts +141 -0
  274. package/dist/types/llm/bedrock/types.d.ts +27 -0
  275. package/dist/types/llm/bedrock/utils/index.d.ts +5 -0
  276. package/dist/types/llm/bedrock/utils/message_inputs.d.ts +31 -0
  277. package/dist/types/llm/bedrock/utils/message_outputs.d.ts +33 -0
  278. package/dist/types/llm/fake.d.ts +31 -0
  279. package/dist/types/llm/google/index.d.ts +24 -0
  280. package/dist/types/llm/google/types.d.ts +42 -0
  281. package/dist/types/llm/google/utils/common.d.ts +34 -0
  282. package/dist/types/llm/google/utils/tools.d.ts +10 -0
  283. package/dist/types/llm/google/utils/zod_to_genai_parameters.d.ts +14 -0
  284. package/dist/types/llm/openai/index.d.ts +127 -0
  285. package/dist/types/llm/openai/types.d.ts +10 -0
  286. package/dist/types/llm/openai/utils/index.d.ts +29 -0
  287. package/dist/types/llm/openrouter/index.d.ts +15 -0
  288. package/dist/types/llm/providers.d.ts +5 -0
  289. package/dist/types/llm/text.d.ts +21 -0
  290. package/dist/types/llm/vertexai/index.d.ts +293 -0
  291. package/dist/types/messages/cache.d.ts +54 -0
  292. package/dist/types/messages/content.d.ts +7 -0
  293. package/dist/types/messages/core.d.ts +14 -0
  294. package/dist/types/messages/format.d.ts +137 -0
  295. package/dist/types/messages/ids.d.ts +3 -0
  296. package/dist/types/messages/index.d.ts +7 -0
  297. package/dist/types/messages/prune.d.ts +52 -0
  298. package/dist/types/messages/reducer.d.ts +9 -0
  299. package/dist/types/messages/tools.d.ts +17 -0
  300. package/dist/types/mockStream.d.ts +32 -0
  301. package/dist/types/prompts/collab.d.ts +1 -0
  302. package/dist/types/prompts/index.d.ts +2 -0
  303. package/dist/types/prompts/taskmanager.d.ts +41 -0
  304. package/dist/types/run.d.ts +41 -0
  305. package/dist/types/schemas/index.d.ts +1 -0
  306. package/dist/types/schemas/validate.d.ts +59 -0
  307. package/dist/types/splitStream.d.ts +37 -0
  308. package/dist/types/stream.d.ts +15 -0
  309. package/dist/types/test/mockTools.d.ts +28 -0
  310. package/dist/types/tools/BrowserTools.d.ts +87 -0
  311. package/dist/types/tools/Calculator.d.ts +34 -0
  312. package/dist/types/tools/CodeExecutor.d.ts +57 -0
  313. package/dist/types/tools/ProgrammaticToolCalling.d.ts +138 -0
  314. package/dist/types/tools/ToolNode.d.ts +51 -0
  315. package/dist/types/tools/ToolSearch.d.ts +219 -0
  316. package/dist/types/tools/handlers.d.ts +22 -0
  317. package/dist/types/tools/schema.d.ts +12 -0
  318. package/dist/types/tools/search/anthropic.d.ts +16 -0
  319. package/dist/types/tools/search/content.d.ts +4 -0
  320. package/dist/types/tools/search/firecrawl.d.ts +54 -0
  321. package/dist/types/tools/search/format.d.ts +5 -0
  322. package/dist/types/tools/search/highlights.d.ts +13 -0
  323. package/dist/types/tools/search/index.d.ts +3 -0
  324. package/dist/types/tools/search/rerankers.d.ts +38 -0
  325. package/dist/types/tools/search/schema.d.ts +103 -0
  326. package/dist/types/tools/search/search.d.ts +8 -0
  327. package/dist/types/tools/search/serper-scraper.d.ts +59 -0
  328. package/dist/types/tools/search/test.d.ts +1 -0
  329. package/dist/types/tools/search/tool.d.ts +3 -0
  330. package/dist/types/tools/search/types.d.ts +575 -0
  331. package/dist/types/tools/search/utils.d.ts +10 -0
  332. package/dist/types/types/graph.d.ts +399 -0
  333. package/dist/types/types/index.d.ts +5 -0
  334. package/dist/types/types/llm.d.ts +105 -0
  335. package/dist/types/types/messages.d.ts +4 -0
  336. package/dist/types/types/run.d.ts +112 -0
  337. package/dist/types/types/stream.d.ts +308 -0
  338. package/dist/types/types/tools.d.ts +296 -0
  339. package/dist/types/utils/contextAnalytics.d.ts +37 -0
  340. package/dist/types/utils/events.d.ts +6 -0
  341. package/dist/types/utils/graph.d.ts +2 -0
  342. package/dist/types/utils/handlers.d.ts +34 -0
  343. package/dist/types/utils/index.d.ts +9 -0
  344. package/dist/types/utils/llm.d.ts +3 -0
  345. package/dist/types/utils/llmConfig.d.ts +3 -0
  346. package/dist/types/utils/logging.d.ts +1 -0
  347. package/dist/types/utils/misc.d.ts +7 -0
  348. package/dist/types/utils/run.d.ts +27 -0
  349. package/dist/types/utils/schema.d.ts +8 -0
  350. package/dist/types/utils/title.d.ts +4 -0
  351. package/dist/types/utils/tokens.d.ts +28 -0
  352. package/dist/types/utils/toonFormat.d.ts +111 -0
  353. package/package.json +190 -0
  354. package/src/agents/AgentContext.test.ts +458 -0
  355. package/src/agents/AgentContext.ts +972 -0
  356. package/src/agents/__tests__/AgentContext.test.ts +805 -0
  357. package/src/agents/__tests__/resolveStructuredOutputMode.test.ts +137 -0
  358. package/src/common/enum.ts +203 -0
  359. package/src/common/index.ts +2 -0
  360. package/src/events.ts +223 -0
  361. package/src/graphs/Graph.ts +2228 -0
  362. package/src/graphs/MultiAgentGraph.ts +1063 -0
  363. package/src/graphs/__tests__/structured-output.integration.test.ts +809 -0
  364. package/src/graphs/__tests__/structured-output.test.ts +183 -0
  365. package/src/graphs/index.ts +2 -0
  366. package/src/index.ts +34 -0
  367. package/src/instrumentation.ts +22 -0
  368. package/src/llm/anthropic/Jacob_Lee_Resume_2023.pdf +0 -0
  369. package/src/llm/anthropic/index.ts +413 -0
  370. package/src/llm/anthropic/llm.spec.ts +1442 -0
  371. package/src/llm/anthropic/types.ts +140 -0
  372. package/src/llm/anthropic/utils/message_inputs.ts +757 -0
  373. package/src/llm/anthropic/utils/message_outputs.ts +289 -0
  374. package/src/llm/anthropic/utils/output_parsers.ts +133 -0
  375. package/src/llm/anthropic/utils/tools.ts +29 -0
  376. package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +495 -0
  377. package/src/llm/bedrock/index.ts +411 -0
  378. package/src/llm/bedrock/llm.spec.ts +616 -0
  379. package/src/llm/bedrock/types.ts +51 -0
  380. package/src/llm/bedrock/utils/index.ts +18 -0
  381. package/src/llm/bedrock/utils/message_inputs.ts +563 -0
  382. package/src/llm/bedrock/utils/message_outputs.ts +310 -0
  383. package/src/llm/fake.ts +133 -0
  384. package/src/llm/google/data/gettysburg10.wav +0 -0
  385. package/src/llm/google/data/hotdog.jpg +0 -0
  386. package/src/llm/google/index.ts +337 -0
  387. package/src/llm/google/llm.spec.ts +934 -0
  388. package/src/llm/google/types.ts +56 -0
  389. package/src/llm/google/utils/common.ts +873 -0
  390. package/src/llm/google/utils/tools.ts +160 -0
  391. package/src/llm/google/utils/zod_to_genai_parameters.ts +86 -0
  392. package/src/llm/openai/index.ts +1366 -0
  393. package/src/llm/openai/types.ts +24 -0
  394. package/src/llm/openai/utils/index.ts +1035 -0
  395. package/src/llm/openai/utils/isReasoningModel.test.ts +90 -0
  396. package/src/llm/openrouter/index.ts +291 -0
  397. package/src/llm/providers.ts +52 -0
  398. package/src/llm/text.ts +94 -0
  399. package/src/llm/vertexai/index.ts +359 -0
  400. package/src/messages/__tests__/tools.test.ts +473 -0
  401. package/src/messages/cache.test.ts +1261 -0
  402. package/src/messages/cache.ts +518 -0
  403. package/src/messages/content.test.ts +362 -0
  404. package/src/messages/content.ts +63 -0
  405. package/src/messages/core.ts +473 -0
  406. package/src/messages/ensureThinkingBlock.test.ts +468 -0
  407. package/src/messages/format.ts +1029 -0
  408. package/src/messages/formatAgentMessages.test.ts +1513 -0
  409. package/src/messages/formatAgentMessages.tools.test.ts +419 -0
  410. package/src/messages/formatMessage.test.ts +693 -0
  411. package/src/messages/ids.ts +26 -0
  412. package/src/messages/index.ts +7 -0
  413. package/src/messages/labelContentByAgent.test.ts +887 -0
  414. package/src/messages/prune.ts +568 -0
  415. package/src/messages/reducer.ts +80 -0
  416. package/src/messages/shiftIndexTokenCountMap.test.ts +81 -0
  417. package/src/messages/tools.ts +108 -0
  418. package/src/mockStream.ts +99 -0
  419. package/src/prompts/collab.ts +6 -0
  420. package/src/prompts/index.ts +2 -0
  421. package/src/prompts/taskmanager.ts +61 -0
  422. package/src/run.ts +467 -0
  423. package/src/schemas/index.ts +2 -0
  424. package/src/schemas/schema-preparation.test.ts +500 -0
  425. package/src/schemas/validate.test.ts +358 -0
  426. package/src/schemas/validate.ts +454 -0
  427. package/src/scripts/abort.ts +157 -0
  428. package/src/scripts/ant_web_search.ts +158 -0
  429. package/src/scripts/ant_web_search_edge_case.ts +162 -0
  430. package/src/scripts/ant_web_search_error_edge_case.ts +148 -0
  431. package/src/scripts/args.ts +48 -0
  432. package/src/scripts/caching.ts +132 -0
  433. package/src/scripts/cli.ts +172 -0
  434. package/src/scripts/cli2.ts +133 -0
  435. package/src/scripts/cli3.ts +184 -0
  436. package/src/scripts/cli4.ts +191 -0
  437. package/src/scripts/cli5.ts +191 -0
  438. package/src/scripts/code_exec.ts +213 -0
  439. package/src/scripts/code_exec_files.ts +236 -0
  440. package/src/scripts/code_exec_multi_session.ts +241 -0
  441. package/src/scripts/code_exec_ptc.ts +334 -0
  442. package/src/scripts/code_exec_session.ts +282 -0
  443. package/src/scripts/code_exec_simple.ts +147 -0
  444. package/src/scripts/content.ts +138 -0
  445. package/src/scripts/empty_input.ts +137 -0
  446. package/src/scripts/handoff-test.ts +135 -0
  447. package/src/scripts/image.ts +178 -0
  448. package/src/scripts/memory.ts +97 -0
  449. package/src/scripts/multi-agent-chain.ts +331 -0
  450. package/src/scripts/multi-agent-conditional.ts +221 -0
  451. package/src/scripts/multi-agent-document-review-chain.ts +197 -0
  452. package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
  453. package/src/scripts/multi-agent-parallel-start.ts +265 -0
  454. package/src/scripts/multi-agent-parallel.ts +394 -0
  455. package/src/scripts/multi-agent-sequence.ts +217 -0
  456. package/src/scripts/multi-agent-supervisor.ts +365 -0
  457. package/src/scripts/multi-agent-test.ts +186 -0
  458. package/src/scripts/parallel-asymmetric-tools-test.ts +274 -0
  459. package/src/scripts/parallel-full-metadata-test.ts +240 -0
  460. package/src/scripts/parallel-tools-test.ts +340 -0
  461. package/src/scripts/programmatic_exec.ts +396 -0
  462. package/src/scripts/programmatic_exec_agent.ts +231 -0
  463. package/src/scripts/search.ts +146 -0
  464. package/src/scripts/sequential-full-metadata-test.ts +197 -0
  465. package/src/scripts/simple.ts +225 -0
  466. package/src/scripts/single-agent-metadata-test.ts +198 -0
  467. package/src/scripts/stream.ts +140 -0
  468. package/src/scripts/test-custom-prompt-key.ts +145 -0
  469. package/src/scripts/test-handoff-input.ts +170 -0
  470. package/src/scripts/test-handoff-preamble.ts +277 -0
  471. package/src/scripts/test-multi-agent-list-handoff.ts +417 -0
  472. package/src/scripts/test-parallel-agent-labeling.ts +325 -0
  473. package/src/scripts/test-parallel-handoffs.ts +291 -0
  474. package/src/scripts/test-thinking-handoff-bedrock.ts +153 -0
  475. package/src/scripts/test-thinking-handoff.ts +155 -0
  476. package/src/scripts/test-tools-before-handoff.ts +226 -0
  477. package/src/scripts/test_code_api.ts +361 -0
  478. package/src/scripts/thinking-bedrock.ts +159 -0
  479. package/src/scripts/thinking.ts +171 -0
  480. package/src/scripts/tool_search.ts +162 -0
  481. package/src/scripts/tools.ts +177 -0
  482. package/src/specs/agent-handoffs.test.ts +888 -0
  483. package/src/specs/anthropic.simple.test.ts +387 -0
  484. package/src/specs/azure.simple.test.ts +364 -0
  485. package/src/specs/cache.simple.test.ts +396 -0
  486. package/src/specs/deepseek.simple.test.ts +283 -0
  487. package/src/specs/emergency-prune.test.ts +407 -0
  488. package/src/specs/moonshot.simple.test.ts +358 -0
  489. package/src/specs/openai.simple.test.ts +311 -0
  490. package/src/specs/openrouter.simple.test.ts +107 -0
  491. package/src/specs/prune.test.ts +901 -0
  492. package/src/specs/reasoning.test.ts +201 -0
  493. package/src/specs/spec.utils.ts +3 -0
  494. package/src/specs/thinking-handoff.test.ts +620 -0
  495. package/src/specs/thinking-prune.test.ts +703 -0
  496. package/src/specs/token-distribution-edge-case.test.ts +316 -0
  497. package/src/specs/token-memoization.test.ts +32 -0
  498. package/src/specs/tool-error.test.ts +198 -0
  499. package/src/splitStream.test.ts +691 -0
  500. package/src/splitStream.ts +234 -0
  501. package/src/stream.test.ts +94 -0
  502. package/src/stream.ts +801 -0
  503. package/src/test/mockTools.ts +386 -0
  504. package/src/tools/BrowserTools.ts +393 -0
  505. package/src/tools/Calculator.test.ts +278 -0
  506. package/src/tools/Calculator.ts +46 -0
  507. package/src/tools/CodeExecutor.ts +270 -0
  508. package/src/tools/ProgrammaticToolCalling.ts +785 -0
  509. package/src/tools/ToolNode.ts +674 -0
  510. package/src/tools/ToolSearch.ts +1095 -0
  511. package/src/tools/__tests__/BrowserTools.test.ts +265 -0
  512. package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +319 -0
  513. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +1006 -0
  514. package/src/tools/__tests__/ToolSearch.integration.test.ts +162 -0
  515. package/src/tools/__tests__/ToolSearch.test.ts +1003 -0
  516. package/src/tools/handlers.ts +363 -0
  517. package/src/tools/schema.ts +37 -0
  518. package/src/tools/search/anthropic.ts +51 -0
  519. package/src/tools/search/content.test.ts +173 -0
  520. package/src/tools/search/content.ts +147 -0
  521. package/src/tools/search/firecrawl.ts +210 -0
  522. package/src/tools/search/format.ts +250 -0
  523. package/src/tools/search/highlights.ts +320 -0
  524. package/src/tools/search/index.ts +3 -0
  525. package/src/tools/search/jina-reranker.test.ts +130 -0
  526. package/src/tools/search/output.md +2775 -0
  527. package/src/tools/search/rerankers.ts +242 -0
  528. package/src/tools/search/schema.ts +113 -0
  529. package/src/tools/search/search.ts +768 -0
  530. package/src/tools/search/serper-scraper.ts +155 -0
  531. package/src/tools/search/test.html +884 -0
  532. package/src/tools/search/test.md +643 -0
  533. package/src/tools/search/test.ts +159 -0
  534. package/src/tools/search/tool.ts +657 -0
  535. package/src/tools/search/types.ts +665 -0
  536. package/src/tools/search/utils.ts +79 -0
  537. package/src/types/graph.test.ts +218 -0
  538. package/src/types/graph.ts +533 -0
  539. package/src/types/index.ts +6 -0
  540. package/src/types/llm.ts +140 -0
  541. package/src/types/messages.ts +4 -0
  542. package/src/types/run.ts +128 -0
  543. package/src/types/stream.ts +417 -0
  544. package/src/types/tools.ts +355 -0
  545. package/src/utils/contextAnalytics.ts +103 -0
  546. package/src/utils/events.ts +32 -0
  547. package/src/utils/graph.ts +11 -0
  548. package/src/utils/handlers.ts +107 -0
  549. package/src/utils/index.ts +9 -0
  550. package/src/utils/llm.ts +26 -0
  551. package/src/utils/llmConfig.ts +208 -0
  552. package/src/utils/logging.ts +48 -0
  553. package/src/utils/misc.ts +57 -0
  554. package/src/utils/run.ts +106 -0
  555. package/src/utils/schema.ts +35 -0
  556. package/src/utils/title.ts +177 -0
  557. package/src/utils/tokens.ts +142 -0
  558. package/src/utils/toonFormat.ts +475 -0
@@ -0,0 +1,1028 @@
1
+ 'use strict';
2
+
3
+ var openai$1 = require('openai');
4
+ var xai = require('@langchain/xai');
5
+ var outputs = require('@langchain/core/outputs');
6
+ var messages = require('@langchain/core/messages');
7
+ require('@langchain/core/utils/function_calling');
8
+ var deepseek = require('@langchain/deepseek');
9
+ var openai = require('@langchain/openai');
10
+ var index = require('./utils/index.cjs');
11
+ require('../../common/enum.cjs');
12
+ require('nanoid');
13
+ require('../../messages/core.cjs');
14
+ require('../../utils/toonFormat.cjs');
15
+ var run = require('../../utils/run.cjs');
16
+ require('js-tiktoken');
17
+ require('zod-to-json-schema');
18
+
19
+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
20
+ const iife = (fn) => fn();
21
+ function isHeaders(headers) {
22
+ return (typeof Headers !== 'undefined' &&
23
+ headers !== null &&
24
+ typeof headers === 'object' &&
25
+ Object.prototype.toString.call(headers) === '[object Headers]');
26
+ }
27
+ function normalizeHeaders(headers) {
28
+ const output = iife(() => {
29
+ // If headers is a Headers instance
30
+ if (isHeaders(headers)) {
31
+ return headers;
32
+ }
33
+ // If headers is an array of [key, value] pairs
34
+ else if (Array.isArray(headers)) {
35
+ return new Headers(headers);
36
+ }
37
+ // If headers is a NullableHeaders-like object (has 'values' property that is a Headers)
38
+ else if (typeof headers === 'object' &&
39
+ headers !== null &&
40
+ 'values' in headers &&
41
+ isHeaders(headers.values)) {
42
+ return headers.values;
43
+ }
44
+ // If headers is a plain object
45
+ else if (typeof headers === 'object' && headers !== null) {
46
+ const entries = Object.entries(headers)
47
+ .filter(([, v]) => typeof v === 'string')
48
+ .map(([k, v]) => [k, v]);
49
+ return new Headers(entries);
50
+ }
51
+ return new Headers();
52
+ });
53
+ return Object.fromEntries(output.entries());
54
+ }
55
+ function createAbortHandler(controller) {
56
+ return function () {
57
+ controller.abort();
58
+ };
59
+ }
60
+ class CustomOpenAIClient extends openai.OpenAIClient {
61
+ abortHandler;
62
+ async fetchWithTimeout(url, init, ms, controller) {
63
+ const { signal, ...options } = init || {};
64
+ const handler = createAbortHandler(controller);
65
+ this.abortHandler = handler;
66
+ if (signal)
67
+ signal.addEventListener('abort', handler, { once: true });
68
+ const timeout = setTimeout(() => handler, ms);
69
+ const fetchOptions = {
70
+ signal: controller.signal,
71
+ ...options,
72
+ };
73
+ if (fetchOptions.method != null) {
74
+ // Custom methods like 'patch' need to be uppercased
75
+ // See https://github.com/nodejs/undici/issues/2294
76
+ fetchOptions.method = fetchOptions.method.toUpperCase();
77
+ }
78
+ return (
79
+ // use undefined this binding; fetch errors if bound to something else in browser/cloudflare
80
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
81
+ /** @ts-ignore */
82
+ this.fetch.call(undefined, url, fetchOptions).finally(() => {
83
+ clearTimeout(timeout);
84
+ }));
85
+ }
86
+ }
87
+ class CustomAzureOpenAIClient extends openai$1.AzureOpenAI {
88
+ abortHandler;
89
+ async fetchWithTimeout(url, init, ms, controller) {
90
+ const { signal, ...options } = init || {};
91
+ const handler = createAbortHandler(controller);
92
+ this.abortHandler = handler;
93
+ if (signal)
94
+ signal.addEventListener('abort', handler, { once: true });
95
+ const timeout = setTimeout(() => handler, ms);
96
+ const fetchOptions = {
97
+ signal: controller.signal,
98
+ ...options,
99
+ };
100
+ if (fetchOptions.method != null) {
101
+ // Custom methods like 'patch' need to be uppercased
102
+ // See https://github.com/nodejs/undici/issues/2294
103
+ fetchOptions.method = fetchOptions.method.toUpperCase();
104
+ }
105
+ return (
106
+ // use undefined this binding; fetch errors if bound to something else in browser/cloudflare
107
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
108
+ /** @ts-ignore */
109
+ this.fetch.call(undefined, url, fetchOptions).finally(() => {
110
+ clearTimeout(timeout);
111
+ }));
112
+ }
113
+ }
114
+ /** @ts-expect-error We are intentionally overriding `getReasoningParams` */
115
+ class ChatOpenAI extends openai.ChatOpenAI {
116
+ _lc_stream_delay;
117
+ constructor(fields) {
118
+ super(fields);
119
+ this._lc_stream_delay = fields?._lc_stream_delay;
120
+ }
121
+ get exposedClient() {
122
+ return this.client;
123
+ }
124
+ static lc_name() {
125
+ return 'IllumaOpenAI';
126
+ }
127
+ _getClientOptions(options) {
128
+ if (!this.client) {
129
+ const openAIEndpointConfig = {
130
+ baseURL: this.clientConfig.baseURL,
131
+ };
132
+ const endpoint = openai.getEndpoint(openAIEndpointConfig);
133
+ const params = {
134
+ ...this.clientConfig,
135
+ baseURL: endpoint,
136
+ timeout: this.timeout,
137
+ maxRetries: 0,
138
+ };
139
+ if (params.baseURL == null) {
140
+ delete params.baseURL;
141
+ }
142
+ this.client = new CustomOpenAIClient(params);
143
+ }
144
+ const requestOptions = {
145
+ ...this.clientConfig,
146
+ ...options,
147
+ };
148
+ return requestOptions;
149
+ }
150
+ /**
151
+ * Returns backwards compatible reasoning parameters from constructor params and call options
152
+ * @internal
153
+ */
154
+ getReasoningParams(options) {
155
+ // apply options in reverse order of importance -- newer options supersede older options
156
+ let reasoning;
157
+ if (this.reasoning !== undefined) {
158
+ reasoning = {
159
+ ...reasoning,
160
+ ...this.reasoning,
161
+ };
162
+ }
163
+ if (options?.reasoning !== undefined) {
164
+ reasoning = {
165
+ ...reasoning,
166
+ ...options.reasoning,
167
+ };
168
+ }
169
+ return reasoning;
170
+ }
171
+ _getReasoningParams(options) {
172
+ return this.getReasoningParams(options);
173
+ }
174
+ async *_streamResponseChunks(messages, options, runManager) {
175
+ if (!this._useResponseApi(options)) {
176
+ return yield* this._streamResponseChunks2(messages, options, runManager);
177
+ }
178
+ const streamIterable = await this.responseApiWithRetry({
179
+ ...this.invocationParams(options, { streaming: true }),
180
+ input: index._convertMessagesToOpenAIResponsesParams(messages, this.model, this.zdrEnabled),
181
+ stream: true,
182
+ }, options);
183
+ for await (const data of streamIterable) {
184
+ const chunk = index._convertOpenAIResponsesDeltaToBaseMessageChunk(data);
185
+ if (chunk == null)
186
+ continue;
187
+ yield chunk;
188
+ if (this._lc_stream_delay != null) {
189
+ await run.sleep(this._lc_stream_delay);
190
+ }
191
+ await runManager?.handleLLMNewToken(chunk.text || '', undefined, undefined, undefined, undefined, { chunk });
192
+ }
193
+ return;
194
+ }
195
+ async *_streamResponseChunks2(messages$1, options, runManager) {
196
+ const messagesMapped = index._convertMessagesToOpenAIParams(messages$1, this.model);
197
+ const params = {
198
+ ...this.invocationParams(options, {
199
+ streaming: true,
200
+ }),
201
+ messages: messagesMapped,
202
+ stream: true,
203
+ };
204
+ let defaultRole;
205
+ const streamIterable = await this.completionWithRetry(params, options);
206
+ let usage;
207
+ for await (const data of streamIterable) {
208
+ const choice = data.choices[0];
209
+ if (data.usage) {
210
+ usage = data.usage;
211
+ }
212
+ if (!choice) {
213
+ continue;
214
+ }
215
+ const { delta } = choice;
216
+ if (!delta) {
217
+ continue;
218
+ }
219
+ const chunk = this._convertOpenAIDeltaToBaseMessageChunk(delta, data, defaultRole);
220
+ if ('reasoning_content' in delta) {
221
+ chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
222
+ }
223
+ else if ('reasoning' in delta) {
224
+ chunk.additional_kwargs.reasoning_content = delta.reasoning;
225
+ }
226
+ if ('provider_specific_fields' in delta) {
227
+ chunk.additional_kwargs.provider_specific_fields =
228
+ delta.provider_specific_fields;
229
+ }
230
+ defaultRole = delta.role ?? defaultRole;
231
+ const newTokenIndices = {
232
+ prompt: options.promptIndex ?? 0,
233
+ completion: choice.index ?? 0,
234
+ };
235
+ if (typeof chunk.content !== 'string') {
236
+ // eslint-disable-next-line no-console
237
+ console.log('[WARNING]: Received non-string content from OpenAI. This is currently not supported.');
238
+ continue;
239
+ }
240
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
241
+ const generationInfo = { ...newTokenIndices };
242
+ if (choice.finish_reason != null) {
243
+ generationInfo.finish_reason = choice.finish_reason;
244
+ // Only include system fingerprint in the last chunk for now
245
+ // to avoid concatenation issues
246
+ generationInfo.system_fingerprint = data.system_fingerprint;
247
+ generationInfo.model_name = data.model;
248
+ generationInfo.service_tier = data.service_tier;
249
+ }
250
+ if (this.logprobs == true) {
251
+ generationInfo.logprobs = choice.logprobs;
252
+ }
253
+ const generationChunk = new outputs.ChatGenerationChunk({
254
+ message: chunk,
255
+ text: chunk.content,
256
+ generationInfo,
257
+ });
258
+ yield generationChunk;
259
+ if (this._lc_stream_delay != null) {
260
+ await run.sleep(this._lc_stream_delay);
261
+ }
262
+ await runManager?.handleLLMNewToken(generationChunk.text || '', newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
263
+ }
264
+ if (usage) {
265
+ const inputTokenDetails = {
266
+ ...(usage.prompt_tokens_details?.audio_tokens != null && {
267
+ audio: usage.prompt_tokens_details.audio_tokens,
268
+ }),
269
+ ...(usage.prompt_tokens_details?.cached_tokens != null && {
270
+ cache_read: usage.prompt_tokens_details.cached_tokens,
271
+ }),
272
+ };
273
+ const outputTokenDetails = {
274
+ ...(usage.completion_tokens_details?.audio_tokens != null && {
275
+ audio: usage.completion_tokens_details.audio_tokens,
276
+ }),
277
+ ...(usage.completion_tokens_details?.reasoning_tokens != null && {
278
+ reasoning: usage.completion_tokens_details.reasoning_tokens,
279
+ }),
280
+ };
281
+ const generationChunk = new outputs.ChatGenerationChunk({
282
+ message: new messages.AIMessageChunk({
283
+ content: '',
284
+ response_metadata: {
285
+ usage: { ...usage },
286
+ },
287
+ usage_metadata: {
288
+ input_tokens: usage.prompt_tokens,
289
+ output_tokens: usage.completion_tokens,
290
+ total_tokens: usage.total_tokens,
291
+ ...(Object.keys(inputTokenDetails).length > 0 && {
292
+ input_token_details: inputTokenDetails,
293
+ }),
294
+ ...(Object.keys(outputTokenDetails).length > 0 && {
295
+ output_token_details: outputTokenDetails,
296
+ }),
297
+ },
298
+ }),
299
+ text: '',
300
+ });
301
+ yield generationChunk;
302
+ if (this._lc_stream_delay != null) {
303
+ await run.sleep(this._lc_stream_delay);
304
+ }
305
+ }
306
+ if (options.signal?.aborted === true) {
307
+ throw new Error('AbortError');
308
+ }
309
+ }
310
+ }
311
+ /** @ts-expect-error We are intentionally overriding `getReasoningParams` */
312
+ class AzureChatOpenAI extends openai.AzureChatOpenAI {
313
+ _lc_stream_delay;
314
+ constructor(fields) {
315
+ super(fields);
316
+ this._lc_stream_delay = fields?._lc_stream_delay;
317
+ }
318
+ get exposedClient() {
319
+ return this.client;
320
+ }
321
+ static lc_name() {
322
+ return 'IllumaAzureOpenAI';
323
+ }
324
+ /**
325
+ * Returns backwards compatible reasoning parameters from constructor params and call options
326
+ * @internal
327
+ */
328
+ getReasoningParams(options) {
329
+ if (!index.isReasoningModel(this.model)) {
330
+ return;
331
+ }
332
+ // apply options in reverse order of importance -- newer options supersede older options
333
+ let reasoning;
334
+ if (this.reasoning !== undefined) {
335
+ reasoning = {
336
+ ...reasoning,
337
+ ...this.reasoning,
338
+ };
339
+ }
340
+ if (options?.reasoning !== undefined) {
341
+ reasoning = {
342
+ ...reasoning,
343
+ ...options.reasoning,
344
+ };
345
+ }
346
+ return reasoning;
347
+ }
348
+ _getReasoningParams(options) {
349
+ return this.getReasoningParams(options);
350
+ }
351
+ _getClientOptions(options) {
352
+ if (!this.client) {
353
+ const openAIEndpointConfig = {
354
+ azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
355
+ azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
356
+ azureOpenAIApiKey: this.azureOpenAIApiKey,
357
+ azureOpenAIBasePath: this.azureOpenAIBasePath,
358
+ azureADTokenProvider: this.azureADTokenProvider,
359
+ baseURL: this.clientConfig.baseURL,
360
+ };
361
+ const endpoint = openai.getEndpoint(openAIEndpointConfig);
362
+ const params = {
363
+ ...this.clientConfig,
364
+ baseURL: endpoint,
365
+ timeout: this.timeout,
366
+ maxRetries: 0,
367
+ };
368
+ if (!this.azureADTokenProvider) {
369
+ params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
370
+ }
371
+ if (params.baseURL == null) {
372
+ delete params.baseURL;
373
+ }
374
+ const defaultHeaders = normalizeHeaders(params.defaultHeaders);
375
+ params.defaultHeaders = {
376
+ ...params.defaultHeaders,
377
+ 'User-Agent': defaultHeaders['User-Agent'] != null
378
+ ? `${defaultHeaders['User-Agent']}: illuma-azure-openai-v2`
379
+ : 'illuma-azure-openai-v2',
380
+ };
381
+ this.client = new CustomAzureOpenAIClient({
382
+ apiVersion: this.azureOpenAIApiVersion,
383
+ azureADTokenProvider: this.azureADTokenProvider,
384
+ ...params,
385
+ });
386
+ }
387
+ const requestOptions = {
388
+ ...this.clientConfig,
389
+ ...options,
390
+ };
391
+ if (this.azureOpenAIApiKey != null) {
392
+ requestOptions.headers = {
393
+ 'api-key': this.azureOpenAIApiKey,
394
+ ...requestOptions.headers,
395
+ };
396
+ requestOptions.query = {
397
+ 'api-version': this.azureOpenAIApiVersion,
398
+ ...requestOptions.query,
399
+ };
400
+ }
401
+ return requestOptions;
402
+ }
403
+ async *_streamResponseChunks(messages, options, runManager) {
404
+ if (!this._useResponseApi(options)) {
405
+ return yield* super._streamResponseChunks(messages, options, runManager);
406
+ }
407
+ const streamIterable = await this.responseApiWithRetry({
408
+ ...this.invocationParams(options, { streaming: true }),
409
+ input: index._convertMessagesToOpenAIResponsesParams(messages, this.model, this.zdrEnabled),
410
+ stream: true,
411
+ }, options);
412
+ for await (const data of streamIterable) {
413
+ const chunk = index._convertOpenAIResponsesDeltaToBaseMessageChunk(data);
414
+ if (chunk == null)
415
+ continue;
416
+ yield chunk;
417
+ if (this._lc_stream_delay != null) {
418
+ await run.sleep(this._lc_stream_delay);
419
+ }
420
+ await runManager?.handleLLMNewToken(chunk.text || '', undefined, undefined, undefined, undefined, { chunk });
421
+ }
422
+ return;
423
+ }
424
+ }
425
+ class ChatDeepSeek extends deepseek.ChatDeepSeek {
426
+ get exposedClient() {
427
+ return this.client;
428
+ }
429
+ static lc_name() {
430
+ return 'IllumaDeepSeek';
431
+ }
432
+ _convertMessages(messages) {
433
+ return index._convertMessagesToOpenAIParams(messages, this.model, {
434
+ includeReasoningContent: true,
435
+ });
436
+ }
437
+ async _generate(messages, options, runManager) {
438
+ const params = this.invocationParams(options);
439
+ if (params.stream === true) {
440
+ return super._generate(messages, options ?? {}, runManager);
441
+ }
442
+ const messagesMapped = this._convertMessages(messages);
443
+ const data = await this.completionWithRetry({
444
+ ...params,
445
+ stream: false,
446
+ messages: messagesMapped,
447
+ }, {
448
+ signal: options?.signal,
449
+ ...options?.options,
450
+ });
451
+ const { completion_tokens, prompt_tokens, total_tokens } = data.usage ?? {};
452
+ const generations = [];
453
+ for (const part of data.choices ?? []) {
454
+ const text = part.message.content ?? '';
455
+ const generation = {
456
+ text: typeof text === 'string' ? text : '',
457
+ message: this._convertResponseToMessage(part, data),
458
+ };
459
+ generation.generationInfo = {
460
+ ...(part.finish_reason != null
461
+ ? { finish_reason: part.finish_reason }
462
+ : {}),
463
+ ...(part.logprobs ? { logprobs: part.logprobs } : {}),
464
+ };
465
+ generations.push(generation);
466
+ }
467
+ return {
468
+ generations,
469
+ llmOutput: {
470
+ tokenUsage: {
471
+ completionTokens: completion_tokens,
472
+ promptTokens: prompt_tokens,
473
+ totalTokens: total_tokens,
474
+ },
475
+ },
476
+ };
477
+ }
478
+ _convertResponseToMessage(choice, data) {
479
+ const { message } = choice;
480
+ const rawToolCalls = message.tool_calls;
481
+ const toolCalls = rawToolCalls?.map((tc) => ({
482
+ id: tc.id,
483
+ name: tc.function.name,
484
+ args: JSON.parse(tc.function.arguments || '{}'),
485
+ type: 'tool_call',
486
+ }));
487
+ const additional_kwargs = {};
488
+ if (rawToolCalls) {
489
+ additional_kwargs.tool_calls = rawToolCalls;
490
+ }
491
+ if ('reasoning_content' in message &&
492
+ message.reasoning_content != null &&
493
+ message.reasoning_content !== '') {
494
+ additional_kwargs.reasoning_content = message.reasoning_content;
495
+ }
496
+ return new messages.AIMessage({
497
+ content: message.content ?? '',
498
+ tool_calls: toolCalls,
499
+ additional_kwargs,
500
+ usage_metadata: data.usage
501
+ ? {
502
+ input_tokens: data.usage.prompt_tokens,
503
+ output_tokens: data.usage.completion_tokens,
504
+ total_tokens: data.usage.total_tokens,
505
+ }
506
+ : undefined,
507
+ response_metadata: {
508
+ model_name: data.model,
509
+ system_fingerprint: data.system_fingerprint,
510
+ finish_reason: choice.finish_reason,
511
+ },
512
+ });
513
+ }
514
+ _getClientOptions(options) {
515
+ if (!this.client) {
516
+ const openAIEndpointConfig = {
517
+ baseURL: this.clientConfig.baseURL,
518
+ };
519
+ const endpoint = openai.getEndpoint(openAIEndpointConfig);
520
+ const params = {
521
+ ...this.clientConfig,
522
+ baseURL: endpoint,
523
+ timeout: this.timeout,
524
+ maxRetries: 0,
525
+ };
526
+ if (params.baseURL == null) {
527
+ delete params.baseURL;
528
+ }
529
+ this.client = new CustomOpenAIClient(params);
530
+ }
531
+ const requestOptions = {
532
+ ...this.clientConfig,
533
+ ...options,
534
+ };
535
+ return requestOptions;
536
+ }
537
+ async *_streamResponseChunks(messages$1, options, runManager) {
538
+ const messagesMapped = index._convertMessagesToOpenAIParams(messages$1, this.model, {
539
+ includeReasoningContent: true,
540
+ });
541
+ const params = {
542
+ ...this.invocationParams(options, {
543
+ streaming: true,
544
+ }),
545
+ messages: messagesMapped,
546
+ stream: true,
547
+ };
548
+ let defaultRole;
549
+ const streamIterable = await this.completionWithRetry(params, options);
550
+ let usage;
551
+ for await (const data of streamIterable) {
552
+ const choice = data.choices[0];
553
+ if (data.usage) {
554
+ usage = data.usage;
555
+ }
556
+ if (!choice) {
557
+ continue;
558
+ }
559
+ const { delta } = choice;
560
+ if (!delta) {
561
+ continue;
562
+ }
563
+ const chunk = this._convertOpenAIDeltaToBaseMessageChunk(delta, data, defaultRole);
564
+ if ('reasoning_content' in delta) {
565
+ chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
566
+ }
567
+ defaultRole = delta.role ?? defaultRole;
568
+ const newTokenIndices = {
569
+ prompt: options.promptIndex ?? 0,
570
+ completion: choice.index ?? 0,
571
+ };
572
+ if (typeof chunk.content !== 'string') {
573
+ // eslint-disable-next-line no-console
574
+ console.log('[WARNING]: Received non-string content from OpenAI. This is currently not supported.');
575
+ continue;
576
+ }
577
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
578
+ const generationInfo = { ...newTokenIndices };
579
+ if (choice.finish_reason != null) {
580
+ generationInfo.finish_reason = choice.finish_reason;
581
+ generationInfo.system_fingerprint = data.system_fingerprint;
582
+ generationInfo.model_name = data.model;
583
+ generationInfo.service_tier = data.service_tier;
584
+ }
585
+ if (this.logprobs == true) {
586
+ generationInfo.logprobs = choice.logprobs;
587
+ }
588
+ const generationChunk = new outputs.ChatGenerationChunk({
589
+ message: chunk,
590
+ text: chunk.content,
591
+ generationInfo,
592
+ });
593
+ yield generationChunk;
594
+ await runManager?.handleLLMNewToken(generationChunk.text || '', newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
595
+ }
596
+ if (usage) {
597
+ const inputTokenDetails = {
598
+ ...(usage.prompt_tokens_details?.audio_tokens != null && {
599
+ audio: usage.prompt_tokens_details.audio_tokens,
600
+ }),
601
+ ...(usage.prompt_tokens_details?.cached_tokens != null && {
602
+ cache_read: usage.prompt_tokens_details.cached_tokens,
603
+ }),
604
+ };
605
+ const outputTokenDetails = {
606
+ ...(usage.completion_tokens_details?.audio_tokens != null && {
607
+ audio: usage.completion_tokens_details.audio_tokens,
608
+ }),
609
+ ...(usage.completion_tokens_details?.reasoning_tokens != null && {
610
+ reasoning: usage.completion_tokens_details.reasoning_tokens,
611
+ }),
612
+ };
613
+ const generationChunk = new outputs.ChatGenerationChunk({
614
+ message: new messages.AIMessageChunk({
615
+ content: '',
616
+ response_metadata: {
617
+ usage: { ...usage },
618
+ },
619
+ usage_metadata: {
620
+ input_tokens: usage.prompt_tokens,
621
+ output_tokens: usage.completion_tokens,
622
+ total_tokens: usage.total_tokens,
623
+ ...(Object.keys(inputTokenDetails).length > 0 && {
624
+ input_token_details: inputTokenDetails,
625
+ }),
626
+ ...(Object.keys(outputTokenDetails).length > 0 && {
627
+ output_token_details: outputTokenDetails,
628
+ }),
629
+ },
630
+ }),
631
+ text: '',
632
+ });
633
+ yield generationChunk;
634
+ }
635
+ if (options.signal?.aborted === true) {
636
+ throw new Error('AbortError');
637
+ }
638
+ }
639
+ }
640
+ class ChatMoonshot extends ChatOpenAI {
641
+ static lc_name() {
642
+ return 'LibreChatMoonshot';
643
+ }
644
+ _convertMessages(messages) {
645
+ return index._convertMessagesToOpenAIParams(messages, this.model, {
646
+ includeReasoningContent: true,
647
+ });
648
+ }
649
+ async _generate(messages, options, runManager) {
650
+ const params = this.invocationParams(options);
651
+ if (params.stream === true) {
652
+ return super._generate(messages, options, runManager);
653
+ }
654
+ const messagesMapped = this._convertMessages(messages);
655
+ const data = await this.completionWithRetry({
656
+ ...params,
657
+ stream: false,
658
+ messages: messagesMapped,
659
+ }, {
660
+ signal: options.signal,
661
+ ...options.options,
662
+ });
663
+ const { completion_tokens, prompt_tokens, total_tokens } = data.usage ?? {};
664
+ const generations = [];
665
+ for (const part of data.choices ?? []) {
666
+ const text = part.message.content ?? '';
667
+ const generation = {
668
+ text: typeof text === 'string' ? text : '',
669
+ message: this._convertResponseToMessage(part, data),
670
+ };
671
+ generation.generationInfo = {
672
+ ...(part.finish_reason ? { finish_reason: part.finish_reason } : {}),
673
+ ...(part.logprobs ? { logprobs: part.logprobs } : {}),
674
+ };
675
+ generations.push(generation);
676
+ }
677
+ return {
678
+ generations,
679
+ llmOutput: {
680
+ tokenUsage: {
681
+ completionTokens: completion_tokens,
682
+ promptTokens: prompt_tokens,
683
+ totalTokens: total_tokens,
684
+ },
685
+ },
686
+ };
687
+ }
688
+ _convertResponseToMessage(choice, data) {
689
+ const { message } = choice;
690
+ const rawToolCalls = message.tool_calls;
691
+ const toolCalls = rawToolCalls?.map((tc) => ({
692
+ id: tc.id,
693
+ name: tc.function.name,
694
+ args: JSON.parse(tc.function.arguments || '{}'),
695
+ type: 'tool_call',
696
+ }));
697
+ const additional_kwargs = {};
698
+ if (rawToolCalls) {
699
+ additional_kwargs.tool_calls = rawToolCalls;
700
+ }
701
+ if ('reasoning_content' in message &&
702
+ message.reasoning_content != null &&
703
+ message.reasoning_content !== '') {
704
+ additional_kwargs.reasoning_content = message.reasoning_content;
705
+ }
706
+ return new messages.AIMessage({
707
+ content: message.content ?? '',
708
+ tool_calls: toolCalls,
709
+ additional_kwargs,
710
+ usage_metadata: data.usage
711
+ ? {
712
+ input_tokens: data.usage.prompt_tokens,
713
+ output_tokens: data.usage.completion_tokens,
714
+ total_tokens: data.usage.total_tokens,
715
+ }
716
+ : undefined,
717
+ response_metadata: {
718
+ model_name: data.model,
719
+ system_fingerprint: data.system_fingerprint,
720
+ finish_reason: choice.finish_reason,
721
+ },
722
+ });
723
+ }
724
+ async *_streamResponseChunks(messages$1, options, runManager) {
725
+ const messagesMapped = index._convertMessagesToOpenAIParams(messages$1, this.model, {
726
+ includeReasoningContent: true,
727
+ });
728
+ const params = {
729
+ ...this.invocationParams(options, {
730
+ streaming: true,
731
+ }),
732
+ messages: messagesMapped,
733
+ stream: true,
734
+ };
735
+ let defaultRole;
736
+ const streamIterable = await this.completionWithRetry(params, options);
737
+ let usage;
738
+ for await (const data of streamIterable) {
739
+ const choice = data.choices[0];
740
+ if (data.usage) {
741
+ usage = data.usage;
742
+ }
743
+ if (!choice) {
744
+ continue;
745
+ }
746
+ const { delta } = choice;
747
+ if (!delta) {
748
+ continue;
749
+ }
750
+ const chunk = this._convertOpenAIDeltaToBaseMessageChunk(delta, data, defaultRole);
751
+ if ('reasoning_content' in delta) {
752
+ chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
753
+ }
754
+ defaultRole = delta.role ?? defaultRole;
755
+ const newTokenIndices = {
756
+ prompt: options.promptIndex ?? 0,
757
+ completion: choice.index ?? 0,
758
+ };
759
+ if (typeof chunk.content !== 'string') {
760
+ // eslint-disable-next-line no-console
761
+ console.log('[WARNING]: Received non-string content from OpenAI. This is currently not supported.');
762
+ continue;
763
+ }
764
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
765
+ const generationInfo = { ...newTokenIndices };
766
+ if (choice.finish_reason != null) {
767
+ generationInfo.finish_reason = choice.finish_reason;
768
+ generationInfo.system_fingerprint = data.system_fingerprint;
769
+ generationInfo.model_name = data.model;
770
+ generationInfo.service_tier = data.service_tier;
771
+ }
772
+ if (this.logprobs == true) {
773
+ generationInfo.logprobs = choice.logprobs;
774
+ }
775
+ const generationChunk = new outputs.ChatGenerationChunk({
776
+ message: chunk,
777
+ text: chunk.content,
778
+ generationInfo,
779
+ });
780
+ yield generationChunk;
781
+ if (this._lc_stream_delay != null) {
782
+ await run.sleep(this._lc_stream_delay);
783
+ }
784
+ await runManager?.handleLLMNewToken(generationChunk.text || '', newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
785
+ }
786
+ if (usage) {
787
+ const inputTokenDetails = {
788
+ ...(usage.prompt_tokens_details?.audio_tokens != null && {
789
+ audio: usage.prompt_tokens_details.audio_tokens,
790
+ }),
791
+ ...(usage.prompt_tokens_details?.cached_tokens != null && {
792
+ cache_read: usage.prompt_tokens_details.cached_tokens,
793
+ }),
794
+ };
795
+ const outputTokenDetails = {
796
+ ...(usage.completion_tokens_details?.audio_tokens != null && {
797
+ audio: usage.completion_tokens_details.audio_tokens,
798
+ }),
799
+ ...(usage.completion_tokens_details?.reasoning_tokens != null && {
800
+ reasoning: usage.completion_tokens_details.reasoning_tokens,
801
+ }),
802
+ };
803
+ const generationChunk = new outputs.ChatGenerationChunk({
804
+ message: new messages.AIMessageChunk({
805
+ content: '',
806
+ response_metadata: {
807
+ usage: { ...usage },
808
+ },
809
+ usage_metadata: {
810
+ input_tokens: usage.prompt_tokens,
811
+ output_tokens: usage.completion_tokens,
812
+ total_tokens: usage.total_tokens,
813
+ ...(Object.keys(inputTokenDetails).length > 0 && {
814
+ input_token_details: inputTokenDetails,
815
+ }),
816
+ ...(Object.keys(outputTokenDetails).length > 0 && {
817
+ output_token_details: outputTokenDetails,
818
+ }),
819
+ },
820
+ }),
821
+ text: '',
822
+ });
823
+ yield generationChunk;
824
+ if (this._lc_stream_delay != null) {
825
+ await run.sleep(this._lc_stream_delay);
826
+ }
827
+ }
828
+ if (options.signal?.aborted === true) {
829
+ throw new Error('AbortError');
830
+ }
831
+ }
832
+ }
833
+ class ChatXAI extends xai.ChatXAI {
834
+ _lc_stream_delay;
835
+ constructor(fields) {
836
+ super(fields);
837
+ this._lc_stream_delay = fields?._lc_stream_delay;
838
+ const customBaseURL = fields?.configuration?.baseURL ?? fields?.clientConfig?.baseURL;
839
+ if (customBaseURL != null && customBaseURL) {
840
+ this.clientConfig = {
841
+ ...this.clientConfig,
842
+ baseURL: customBaseURL,
843
+ };
844
+ // Reset the client to force recreation with new config
845
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
846
+ this.client = undefined;
847
+ }
848
+ }
849
+ static lc_name() {
850
+ return 'IllumaXAI';
851
+ }
852
+ get exposedClient() {
853
+ return this.client;
854
+ }
855
+ _getClientOptions(options) {
856
+ if (!this.client) {
857
+ const openAIEndpointConfig = {
858
+ baseURL: this.clientConfig.baseURL,
859
+ };
860
+ const endpoint = openai.getEndpoint(openAIEndpointConfig);
861
+ const params = {
862
+ ...this.clientConfig,
863
+ baseURL: endpoint,
864
+ timeout: this.timeout,
865
+ maxRetries: 0,
866
+ };
867
+ if (params.baseURL == null) {
868
+ delete params.baseURL;
869
+ }
870
+ this.client = new CustomOpenAIClient(params);
871
+ }
872
+ const requestOptions = {
873
+ ...this.clientConfig,
874
+ ...options,
875
+ };
876
+ return requestOptions;
877
+ }
878
+ async *_streamResponseChunks(messages$1, options, runManager) {
879
+ const messagesMapped = index._convertMessagesToOpenAIParams(messages$1, this.model);
880
+ const params = {
881
+ ...this.invocationParams(options, {
882
+ streaming: true,
883
+ }),
884
+ messages: messagesMapped,
885
+ stream: true,
886
+ };
887
+ let defaultRole;
888
+ const streamIterable = await this.completionWithRetry(params, options);
889
+ let usage;
890
+ for await (const data of streamIterable) {
891
+ const choice = data.choices[0];
892
+ if (data.usage) {
893
+ usage = data.usage;
894
+ }
895
+ if (!choice) {
896
+ continue;
897
+ }
898
+ const { delta } = choice;
899
+ if (!delta) {
900
+ continue;
901
+ }
902
+ const chunk = this._convertOpenAIDeltaToBaseMessageChunk(delta, data, defaultRole);
903
+ if (chunk.usage_metadata != null) {
904
+ chunk.usage_metadata = {
905
+ input_tokens: chunk.usage_metadata.input_tokens ?? 0,
906
+ output_tokens: chunk.usage_metadata.output_tokens ?? 0,
907
+ total_tokens: chunk.usage_metadata.total_tokens ?? 0,
908
+ };
909
+ }
910
+ if ('reasoning_content' in delta) {
911
+ chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
912
+ }
913
+ defaultRole = delta.role ?? defaultRole;
914
+ const newTokenIndices = {
915
+ prompt: options.promptIndex ?? 0,
916
+ completion: choice.index ?? 0,
917
+ };
918
+ if (typeof chunk.content !== 'string') {
919
+ // eslint-disable-next-line no-console
920
+ console.log('[WARNING]: Received non-string content from OpenAI. This is currently not supported.');
921
+ continue;
922
+ }
923
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
924
+ const generationInfo = { ...newTokenIndices };
925
+ if (choice.finish_reason != null) {
926
+ generationInfo.finish_reason = choice.finish_reason;
927
+ // Only include system fingerprint in the last chunk for now
928
+ // to avoid concatenation issues
929
+ generationInfo.system_fingerprint = data.system_fingerprint;
930
+ generationInfo.model_name = data.model;
931
+ generationInfo.service_tier = data.service_tier;
932
+ }
933
+ if (this.logprobs == true) {
934
+ generationInfo.logprobs = choice.logprobs;
935
+ }
936
+ const generationChunk = new outputs.ChatGenerationChunk({
937
+ message: chunk,
938
+ text: chunk.content,
939
+ generationInfo,
940
+ });
941
+ yield generationChunk;
942
+ if (this._lc_stream_delay != null) {
943
+ await run.sleep(this._lc_stream_delay);
944
+ }
945
+ await runManager?.handleLLMNewToken(generationChunk.text || '', newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
946
+ }
947
+ if (usage) {
948
+ // Type assertion for xAI-specific usage structure
949
+ const xaiUsage = usage;
950
+ const inputTokenDetails = {
951
+ // Standard OpenAI fields
952
+ ...(usage.prompt_tokens_details?.audio_tokens != null && {
953
+ audio: usage.prompt_tokens_details.audio_tokens,
954
+ }),
955
+ ...(usage.prompt_tokens_details?.cached_tokens != null && {
956
+ cache_read: usage.prompt_tokens_details.cached_tokens,
957
+ }),
958
+ // Add xAI-specific prompt token details if they exist
959
+ ...(xaiUsage.prompt_tokens_details?.text_tokens != null && {
960
+ text: xaiUsage.prompt_tokens_details.text_tokens,
961
+ }),
962
+ ...(xaiUsage.prompt_tokens_details?.image_tokens != null && {
963
+ image: xaiUsage.prompt_tokens_details.image_tokens,
964
+ }),
965
+ };
966
+ const outputTokenDetails = {
967
+ // Standard OpenAI fields
968
+ ...(usage.completion_tokens_details?.audio_tokens != null && {
969
+ audio: usage.completion_tokens_details.audio_tokens,
970
+ }),
971
+ ...(usage.completion_tokens_details?.reasoning_tokens != null && {
972
+ reasoning: usage.completion_tokens_details.reasoning_tokens,
973
+ }),
974
+ // Add xAI-specific completion token details if they exist
975
+ ...(xaiUsage.completion_tokens_details?.accepted_prediction_tokens !=
976
+ null && {
977
+ accepted_prediction: xaiUsage.completion_tokens_details.accepted_prediction_tokens,
978
+ }),
979
+ ...(xaiUsage.completion_tokens_details?.rejected_prediction_tokens !=
980
+ null && {
981
+ rejected_prediction: xaiUsage.completion_tokens_details.rejected_prediction_tokens,
982
+ }),
983
+ };
984
+ const generationChunk = new outputs.ChatGenerationChunk({
985
+ message: new messages.AIMessageChunk({
986
+ content: '',
987
+ response_metadata: {
988
+ usage: { ...usage },
989
+ // Include xAI-specific metadata if it exists
990
+ ...(xaiUsage.num_sources_used != null && {
991
+ num_sources_used: xaiUsage.num_sources_used,
992
+ }),
993
+ },
994
+ usage_metadata: {
995
+ input_tokens: usage.prompt_tokens,
996
+ output_tokens: usage.completion_tokens,
997
+ total_tokens: usage.total_tokens,
998
+ ...(Object.keys(inputTokenDetails).length > 0 && {
999
+ input_token_details: inputTokenDetails,
1000
+ }),
1001
+ ...(Object.keys(outputTokenDetails).length > 0 && {
1002
+ output_token_details: outputTokenDetails,
1003
+ }),
1004
+ },
1005
+ }),
1006
+ text: '',
1007
+ });
1008
+ yield generationChunk;
1009
+ if (this._lc_stream_delay != null) {
1010
+ await run.sleep(this._lc_stream_delay);
1011
+ }
1012
+ }
1013
+ if (options.signal?.aborted === true) {
1014
+ throw new Error('AbortError');
1015
+ }
1016
+ }
1017
+ }
1018
+
1019
+ exports.AzureChatOpenAI = AzureChatOpenAI;
1020
+ exports.ChatDeepSeek = ChatDeepSeek;
1021
+ exports.ChatMoonshot = ChatMoonshot;
1022
+ exports.ChatOpenAI = ChatOpenAI;
1023
+ exports.ChatXAI = ChatXAI;
1024
+ exports.CustomAzureOpenAIClient = CustomAzureOpenAIClient;
1025
+ exports.CustomOpenAIClient = CustomOpenAIClient;
1026
+ exports.isHeaders = isHeaders;
1027
+ exports.normalizeHeaders = normalizeHeaders;
1028
+ //# sourceMappingURL=index.cjs.map