illuma-agents 1.0.2

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 (437) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/common/enum.cjs +163 -0
  3. package/dist/cjs/common/enum.cjs.map +1 -0
  4. package/dist/cjs/events.cjs +143 -0
  5. package/dist/cjs/events.cjs.map +1 -0
  6. package/dist/cjs/graphs/Graph.cjs +581 -0
  7. package/dist/cjs/graphs/Graph.cjs.map +1 -0
  8. package/dist/cjs/instrumentation.cjs +21 -0
  9. package/dist/cjs/instrumentation.cjs.map +1 -0
  10. package/dist/cjs/llm/anthropic/index.cjs +292 -0
  11. package/dist/cjs/llm/anthropic/index.cjs.map +1 -0
  12. package/dist/cjs/llm/anthropic/types.cjs +50 -0
  13. package/dist/cjs/llm/anthropic/types.cjs.map +1 -0
  14. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +553 -0
  15. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -0
  16. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +218 -0
  17. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -0
  18. package/dist/cjs/llm/anthropic/utils/tools.cjs +29 -0
  19. package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -0
  20. package/dist/cjs/llm/fake.cjs +97 -0
  21. package/dist/cjs/llm/fake.cjs.map +1 -0
  22. package/dist/cjs/llm/google/index.cjs +147 -0
  23. package/dist/cjs/llm/google/index.cjs.map +1 -0
  24. package/dist/cjs/llm/google/utils/common.cjs +490 -0
  25. package/dist/cjs/llm/google/utils/common.cjs.map +1 -0
  26. package/dist/cjs/llm/ollama/index.cjs +70 -0
  27. package/dist/cjs/llm/ollama/index.cjs.map +1 -0
  28. package/dist/cjs/llm/ollama/utils.cjs +158 -0
  29. package/dist/cjs/llm/ollama/utils.cjs.map +1 -0
  30. package/dist/cjs/llm/openai/index.cjs +613 -0
  31. package/dist/cjs/llm/openai/index.cjs.map +1 -0
  32. package/dist/cjs/llm/openai/utils/index.cjs +677 -0
  33. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -0
  34. package/dist/cjs/llm/openrouter/index.cjs +29 -0
  35. package/dist/cjs/llm/openrouter/index.cjs.map +1 -0
  36. package/dist/cjs/llm/providers.cjs +47 -0
  37. package/dist/cjs/llm/providers.cjs.map +1 -0
  38. package/dist/cjs/llm/text.cjs +69 -0
  39. package/dist/cjs/llm/text.cjs.map +1 -0
  40. package/dist/cjs/llm/vertexai/index.cjs +330 -0
  41. package/dist/cjs/llm/vertexai/index.cjs.map +1 -0
  42. package/dist/cjs/main.cjs +127 -0
  43. package/dist/cjs/main.cjs.map +1 -0
  44. package/dist/cjs/messages/core.cjs +359 -0
  45. package/dist/cjs/messages/core.cjs.map +1 -0
  46. package/dist/cjs/messages/format.cjs +455 -0
  47. package/dist/cjs/messages/format.cjs.map +1 -0
  48. package/dist/cjs/messages/ids.cjs +23 -0
  49. package/dist/cjs/messages/ids.cjs.map +1 -0
  50. package/dist/cjs/messages/prune.cjs +398 -0
  51. package/dist/cjs/messages/prune.cjs.map +1 -0
  52. package/dist/cjs/run.cjs +264 -0
  53. package/dist/cjs/run.cjs.map +1 -0
  54. package/dist/cjs/splitStream.cjs +210 -0
  55. package/dist/cjs/splitStream.cjs.map +1 -0
  56. package/dist/cjs/stream.cjs +504 -0
  57. package/dist/cjs/stream.cjs.map +1 -0
  58. package/dist/cjs/tools/CodeExecutor.cjs +192 -0
  59. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -0
  60. package/dist/cjs/tools/ToolNode.cjs +125 -0
  61. package/dist/cjs/tools/ToolNode.cjs.map +1 -0
  62. package/dist/cjs/tools/handlers.cjs +250 -0
  63. package/dist/cjs/tools/handlers.cjs.map +1 -0
  64. package/dist/cjs/tools/search/anthropic.cjs +40 -0
  65. package/dist/cjs/tools/search/anthropic.cjs.map +1 -0
  66. package/dist/cjs/tools/search/content.cjs +140 -0
  67. package/dist/cjs/tools/search/content.cjs.map +1 -0
  68. package/dist/cjs/tools/search/firecrawl.cjs +179 -0
  69. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -0
  70. package/dist/cjs/tools/search/format.cjs +203 -0
  71. package/dist/cjs/tools/search/format.cjs.map +1 -0
  72. package/dist/cjs/tools/search/highlights.cjs +245 -0
  73. package/dist/cjs/tools/search/highlights.cjs.map +1 -0
  74. package/dist/cjs/tools/search/rerankers.cjs +174 -0
  75. package/dist/cjs/tools/search/rerankers.cjs.map +1 -0
  76. package/dist/cjs/tools/search/schema.cjs +70 -0
  77. package/dist/cjs/tools/search/schema.cjs.map +1 -0
  78. package/dist/cjs/tools/search/search.cjs +561 -0
  79. package/dist/cjs/tools/search/search.cjs.map +1 -0
  80. package/dist/cjs/tools/search/serper-scraper.cjs +132 -0
  81. package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -0
  82. package/dist/cjs/tools/search/tool.cjs +331 -0
  83. package/dist/cjs/tools/search/tool.cjs.map +1 -0
  84. package/dist/cjs/tools/search/utils.cjs +66 -0
  85. package/dist/cjs/tools/search/utils.cjs.map +1 -0
  86. package/dist/cjs/utils/graph.cjs +16 -0
  87. package/dist/cjs/utils/graph.cjs.map +1 -0
  88. package/dist/cjs/utils/llm.cjs +28 -0
  89. package/dist/cjs/utils/llm.cjs.map +1 -0
  90. package/dist/cjs/utils/misc.cjs +56 -0
  91. package/dist/cjs/utils/misc.cjs.map +1 -0
  92. package/dist/cjs/utils/run.cjs +69 -0
  93. package/dist/cjs/utils/run.cjs.map +1 -0
  94. package/dist/cjs/utils/title.cjs +111 -0
  95. package/dist/cjs/utils/title.cjs.map +1 -0
  96. package/dist/cjs/utils/tokens.cjs +65 -0
  97. package/dist/cjs/utils/tokens.cjs.map +1 -0
  98. package/dist/esm/common/enum.mjs +163 -0
  99. package/dist/esm/common/enum.mjs.map +1 -0
  100. package/dist/esm/events.mjs +135 -0
  101. package/dist/esm/events.mjs.map +1 -0
  102. package/dist/esm/graphs/Graph.mjs +578 -0
  103. package/dist/esm/graphs/Graph.mjs.map +1 -0
  104. package/dist/esm/instrumentation.mjs +19 -0
  105. package/dist/esm/instrumentation.mjs.map +1 -0
  106. package/dist/esm/llm/anthropic/index.mjs +290 -0
  107. package/dist/esm/llm/anthropic/index.mjs.map +1 -0
  108. package/dist/esm/llm/anthropic/types.mjs +48 -0
  109. package/dist/esm/llm/anthropic/types.mjs.map +1 -0
  110. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +550 -0
  111. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -0
  112. package/dist/esm/llm/anthropic/utils/message_outputs.mjs +216 -0
  113. package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -0
  114. package/dist/esm/llm/anthropic/utils/tools.mjs +27 -0
  115. package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -0
  116. package/dist/esm/llm/fake.mjs +94 -0
  117. package/dist/esm/llm/fake.mjs.map +1 -0
  118. package/dist/esm/llm/google/index.mjs +145 -0
  119. package/dist/esm/llm/google/index.mjs.map +1 -0
  120. package/dist/esm/llm/google/utils/common.mjs +484 -0
  121. package/dist/esm/llm/google/utils/common.mjs.map +1 -0
  122. package/dist/esm/llm/ollama/index.mjs +68 -0
  123. package/dist/esm/llm/ollama/index.mjs.map +1 -0
  124. package/dist/esm/llm/ollama/utils.mjs +155 -0
  125. package/dist/esm/llm/ollama/utils.mjs.map +1 -0
  126. package/dist/esm/llm/openai/index.mjs +604 -0
  127. package/dist/esm/llm/openai/index.mjs.map +1 -0
  128. package/dist/esm/llm/openai/utils/index.mjs +671 -0
  129. package/dist/esm/llm/openai/utils/index.mjs.map +1 -0
  130. package/dist/esm/llm/openrouter/index.mjs +27 -0
  131. package/dist/esm/llm/openrouter/index.mjs.map +1 -0
  132. package/dist/esm/llm/providers.mjs +43 -0
  133. package/dist/esm/llm/providers.mjs.map +1 -0
  134. package/dist/esm/llm/text.mjs +67 -0
  135. package/dist/esm/llm/text.mjs.map +1 -0
  136. package/dist/esm/llm/vertexai/index.mjs +328 -0
  137. package/dist/esm/llm/vertexai/index.mjs.map +1 -0
  138. package/dist/esm/main.mjs +20 -0
  139. package/dist/esm/main.mjs.map +1 -0
  140. package/dist/esm/messages/core.mjs +351 -0
  141. package/dist/esm/messages/core.mjs.map +1 -0
  142. package/dist/esm/messages/format.mjs +447 -0
  143. package/dist/esm/messages/format.mjs.map +1 -0
  144. package/dist/esm/messages/ids.mjs +21 -0
  145. package/dist/esm/messages/ids.mjs.map +1 -0
  146. package/dist/esm/messages/prune.mjs +393 -0
  147. package/dist/esm/messages/prune.mjs.map +1 -0
  148. package/dist/esm/run.mjs +261 -0
  149. package/dist/esm/run.mjs.map +1 -0
  150. package/dist/esm/splitStream.mjs +207 -0
  151. package/dist/esm/splitStream.mjs.map +1 -0
  152. package/dist/esm/stream.mjs +500 -0
  153. package/dist/esm/stream.mjs.map +1 -0
  154. package/dist/esm/tools/CodeExecutor.mjs +188 -0
  155. package/dist/esm/tools/CodeExecutor.mjs.map +1 -0
  156. package/dist/esm/tools/ToolNode.mjs +122 -0
  157. package/dist/esm/tools/ToolNode.mjs.map +1 -0
  158. package/dist/esm/tools/handlers.mjs +245 -0
  159. package/dist/esm/tools/handlers.mjs.map +1 -0
  160. package/dist/esm/tools/search/anthropic.mjs +37 -0
  161. package/dist/esm/tools/search/anthropic.mjs.map +1 -0
  162. package/dist/esm/tools/search/content.mjs +119 -0
  163. package/dist/esm/tools/search/content.mjs.map +1 -0
  164. package/dist/esm/tools/search/firecrawl.mjs +176 -0
  165. package/dist/esm/tools/search/firecrawl.mjs.map +1 -0
  166. package/dist/esm/tools/search/format.mjs +201 -0
  167. package/dist/esm/tools/search/format.mjs.map +1 -0
  168. package/dist/esm/tools/search/highlights.mjs +243 -0
  169. package/dist/esm/tools/search/highlights.mjs.map +1 -0
  170. package/dist/esm/tools/search/rerankers.mjs +168 -0
  171. package/dist/esm/tools/search/rerankers.mjs.map +1 -0
  172. package/dist/esm/tools/search/schema.mjs +61 -0
  173. package/dist/esm/tools/search/schema.mjs.map +1 -0
  174. package/dist/esm/tools/search/search.mjs +558 -0
  175. package/dist/esm/tools/search/search.mjs.map +1 -0
  176. package/dist/esm/tools/search/serper-scraper.mjs +129 -0
  177. package/dist/esm/tools/search/serper-scraper.mjs.map +1 -0
  178. package/dist/esm/tools/search/tool.mjs +329 -0
  179. package/dist/esm/tools/search/tool.mjs.map +1 -0
  180. package/dist/esm/tools/search/utils.mjs +61 -0
  181. package/dist/esm/tools/search/utils.mjs.map +1 -0
  182. package/dist/esm/utils/graph.mjs +13 -0
  183. package/dist/esm/utils/graph.mjs.map +1 -0
  184. package/dist/esm/utils/llm.mjs +25 -0
  185. package/dist/esm/utils/llm.mjs.map +1 -0
  186. package/dist/esm/utils/misc.mjs +53 -0
  187. package/dist/esm/utils/misc.mjs.map +1 -0
  188. package/dist/esm/utils/run.mjs +66 -0
  189. package/dist/esm/utils/run.mjs.map +1 -0
  190. package/dist/esm/utils/title.mjs +108 -0
  191. package/dist/esm/utils/title.mjs.map +1 -0
  192. package/dist/esm/utils/tokens.mjs +62 -0
  193. package/dist/esm/utils/tokens.mjs.map +1 -0
  194. package/dist/types/common/enum.d.ts +128 -0
  195. package/dist/types/common/index.d.ts +1 -0
  196. package/dist/types/events.d.ts +29 -0
  197. package/dist/types/graphs/Graph.d.ts +122 -0
  198. package/dist/types/graphs/index.d.ts +1 -0
  199. package/dist/types/index.d.ts +13 -0
  200. package/dist/types/instrumentation.d.ts +1 -0
  201. package/dist/types/llm/anthropic/index.d.ts +39 -0
  202. package/dist/types/llm/anthropic/types.d.ts +37 -0
  203. package/dist/types/llm/anthropic/utils/message_inputs.d.ts +14 -0
  204. package/dist/types/llm/anthropic/utils/message_outputs.d.ts +14 -0
  205. package/dist/types/llm/anthropic/utils/output_parsers.d.ts +22 -0
  206. package/dist/types/llm/anthropic/utils/tools.d.ts +3 -0
  207. package/dist/types/llm/fake.d.ts +31 -0
  208. package/dist/types/llm/google/index.d.ts +14 -0
  209. package/dist/types/llm/google/types.d.ts +32 -0
  210. package/dist/types/llm/google/utils/common.d.ts +19 -0
  211. package/dist/types/llm/google/utils/tools.d.ts +10 -0
  212. package/dist/types/llm/google/utils/zod_to_genai_parameters.d.ts +14 -0
  213. package/dist/types/llm/ollama/index.d.ts +8 -0
  214. package/dist/types/llm/ollama/utils.d.ts +7 -0
  215. package/dist/types/llm/openai/index.d.ts +103 -0
  216. package/dist/types/llm/openai/types.d.ts +10 -0
  217. package/dist/types/llm/openai/utils/index.d.ts +20 -0
  218. package/dist/types/llm/openrouter/index.d.ts +12 -0
  219. package/dist/types/llm/providers.d.ts +5 -0
  220. package/dist/types/llm/text.d.ts +21 -0
  221. package/dist/types/llm/vertexai/index.d.ts +293 -0
  222. package/dist/types/messages/core.d.ts +14 -0
  223. package/dist/types/messages/format.d.ts +113 -0
  224. package/dist/types/messages/ids.d.ts +3 -0
  225. package/dist/types/messages/index.d.ts +4 -0
  226. package/dist/types/messages/prune.d.ts +51 -0
  227. package/dist/types/mockStream.d.ts +32 -0
  228. package/dist/types/prompts/collab.d.ts +1 -0
  229. package/dist/types/prompts/index.d.ts +2 -0
  230. package/dist/types/prompts/taskmanager.d.ts +41 -0
  231. package/dist/types/run.d.ts +30 -0
  232. package/dist/types/scripts/abort.d.ts +1 -0
  233. package/dist/types/scripts/ant_web_search.d.ts +1 -0
  234. package/dist/types/scripts/args.d.ts +7 -0
  235. package/dist/types/scripts/caching.d.ts +1 -0
  236. package/dist/types/scripts/cli.d.ts +1 -0
  237. package/dist/types/scripts/cli2.d.ts +1 -0
  238. package/dist/types/scripts/cli3.d.ts +1 -0
  239. package/dist/types/scripts/cli4.d.ts +1 -0
  240. package/dist/types/scripts/cli5.d.ts +1 -0
  241. package/dist/types/scripts/code_exec.d.ts +1 -0
  242. package/dist/types/scripts/code_exec_files.d.ts +1 -0
  243. package/dist/types/scripts/code_exec_simple.d.ts +1 -0
  244. package/dist/types/scripts/content.d.ts +1 -0
  245. package/dist/types/scripts/empty_input.d.ts +1 -0
  246. package/dist/types/scripts/image.d.ts +1 -0
  247. package/dist/types/scripts/memory.d.ts +1 -0
  248. package/dist/types/scripts/search.d.ts +1 -0
  249. package/dist/types/scripts/simple.d.ts +1 -0
  250. package/dist/types/scripts/stream.d.ts +1 -0
  251. package/dist/types/scripts/thinking.d.ts +1 -0
  252. package/dist/types/scripts/tools.d.ts +1 -0
  253. package/dist/types/specs/spec.utils.d.ts +1 -0
  254. package/dist/types/splitStream.d.ts +37 -0
  255. package/dist/types/stream.d.ts +14 -0
  256. package/dist/types/tools/CodeExecutor.d.ts +23 -0
  257. package/dist/types/tools/ToolNode.d.ts +22 -0
  258. package/dist/types/tools/example.d.ts +78 -0
  259. package/dist/types/tools/handlers.d.ts +19 -0
  260. package/dist/types/tools/search/anthropic.d.ts +16 -0
  261. package/dist/types/tools/search/content.d.ts +4 -0
  262. package/dist/types/tools/search/firecrawl.d.ts +54 -0
  263. package/dist/types/tools/search/format.d.ts +5 -0
  264. package/dist/types/tools/search/highlights.d.ts +13 -0
  265. package/dist/types/tools/search/index.d.ts +2 -0
  266. package/dist/types/tools/search/rerankers.d.ts +38 -0
  267. package/dist/types/tools/search/schema.d.ts +16 -0
  268. package/dist/types/tools/search/search.d.ts +8 -0
  269. package/dist/types/tools/search/serper-scraper.d.ts +59 -0
  270. package/dist/types/tools/search/test.d.ts +1 -0
  271. package/dist/types/tools/search/tool.d.ts +54 -0
  272. package/dist/types/tools/search/types.d.ts +591 -0
  273. package/dist/types/tools/search/utils.d.ts +10 -0
  274. package/dist/types/types/graph.d.ts +138 -0
  275. package/dist/types/types/index.d.ts +5 -0
  276. package/dist/types/types/llm.d.ts +102 -0
  277. package/dist/types/types/run.d.ts +74 -0
  278. package/dist/types/types/stream.d.ts +293 -0
  279. package/dist/types/types/tools.d.ts +61 -0
  280. package/dist/types/utils/graph.d.ts +2 -0
  281. package/dist/types/utils/index.d.ts +5 -0
  282. package/dist/types/utils/llm.d.ts +3 -0
  283. package/dist/types/utils/llmConfig.d.ts +3 -0
  284. package/dist/types/utils/logging.d.ts +1 -0
  285. package/dist/types/utils/misc.d.ts +7 -0
  286. package/dist/types/utils/run.d.ts +27 -0
  287. package/dist/types/utils/title.d.ts +4 -0
  288. package/dist/types/utils/tokens.d.ts +3 -0
  289. package/package.json +145 -0
  290. package/src/common/enum.ts +176 -0
  291. package/src/common/index.ts +2 -0
  292. package/src/events.ts +191 -0
  293. package/src/graphs/Graph.ts +846 -0
  294. package/src/graphs/index.ts +1 -0
  295. package/src/index.ts +24 -0
  296. package/src/instrumentation.ts +22 -0
  297. package/src/llm/anthropic/Jacob_Lee_Resume_2023.pdf +0 -0
  298. package/src/llm/anthropic/index.ts +413 -0
  299. package/src/llm/anthropic/llm.spec.ts +1442 -0
  300. package/src/llm/anthropic/types.ts +140 -0
  301. package/src/llm/anthropic/utils/message_inputs.ts +660 -0
  302. package/src/llm/anthropic/utils/message_outputs.ts +289 -0
  303. package/src/llm/anthropic/utils/output_parsers.ts +133 -0
  304. package/src/llm/anthropic/utils/tools.ts +29 -0
  305. package/src/llm/fake.ts +133 -0
  306. package/src/llm/google/index.ts +222 -0
  307. package/src/llm/google/types.ts +43 -0
  308. package/src/llm/google/utils/common.ts +660 -0
  309. package/src/llm/google/utils/tools.ts +160 -0
  310. package/src/llm/google/utils/zod_to_genai_parameters.ts +88 -0
  311. package/src/llm/ollama/index.ts +92 -0
  312. package/src/llm/ollama/utils.ts +193 -0
  313. package/src/llm/openai/index.ts +853 -0
  314. package/src/llm/openai/types.ts +24 -0
  315. package/src/llm/openai/utils/index.ts +918 -0
  316. package/src/llm/openai/utils/isReasoningModel.test.ts +90 -0
  317. package/src/llm/openrouter/index.ts +60 -0
  318. package/src/llm/providers.ts +57 -0
  319. package/src/llm/text.ts +94 -0
  320. package/src/llm/vertexai/index.ts +360 -0
  321. package/src/messages/core.ts +463 -0
  322. package/src/messages/format.ts +625 -0
  323. package/src/messages/formatAgentMessages.test.ts +917 -0
  324. package/src/messages/formatAgentMessages.tools.test.ts +400 -0
  325. package/src/messages/formatMessage.test.ts +693 -0
  326. package/src/messages/ids.ts +26 -0
  327. package/src/messages/index.ts +4 -0
  328. package/src/messages/prune.ts +567 -0
  329. package/src/messages/shiftIndexTokenCountMap.test.ts +81 -0
  330. package/src/mockStream.ts +99 -0
  331. package/src/prompts/collab.ts +6 -0
  332. package/src/prompts/index.ts +2 -0
  333. package/src/prompts/taskmanager.ts +61 -0
  334. package/src/proto/CollabGraph.ts +269 -0
  335. package/src/proto/TaskManager.ts +243 -0
  336. package/src/proto/collab.ts +200 -0
  337. package/src/proto/collab_design.ts +184 -0
  338. package/src/proto/collab_design_v2.ts +224 -0
  339. package/src/proto/collab_design_v3.ts +255 -0
  340. package/src/proto/collab_design_v4.ts +220 -0
  341. package/src/proto/collab_design_v5.ts +251 -0
  342. package/src/proto/collab_graph.ts +181 -0
  343. package/src/proto/collab_original.ts +123 -0
  344. package/src/proto/example.ts +93 -0
  345. package/src/proto/example_new.ts +68 -0
  346. package/src/proto/example_old.ts +201 -0
  347. package/src/proto/example_test.ts +152 -0
  348. package/src/proto/example_test_anthropic.ts +100 -0
  349. package/src/proto/log_stream.ts +202 -0
  350. package/src/proto/main_collab_community_event.ts +133 -0
  351. package/src/proto/main_collab_design_v2.ts +96 -0
  352. package/src/proto/main_collab_design_v4.ts +100 -0
  353. package/src/proto/main_collab_design_v5.ts +135 -0
  354. package/src/proto/main_collab_global_analysis.ts +122 -0
  355. package/src/proto/main_collab_hackathon_event.ts +153 -0
  356. package/src/proto/main_collab_space_mission.ts +153 -0
  357. package/src/proto/main_philosophy.ts +210 -0
  358. package/src/proto/original_script.ts +126 -0
  359. package/src/proto/standard.ts +100 -0
  360. package/src/proto/stream.ts +56 -0
  361. package/src/proto/tasks.ts +118 -0
  362. package/src/proto/tools/global_analysis_tools.ts +86 -0
  363. package/src/proto/tools/space_mission_tools.ts +60 -0
  364. package/src/proto/vertexai.ts +54 -0
  365. package/src/run.ts +381 -0
  366. package/src/scripts/abort.ts +138 -0
  367. package/src/scripts/ant_web_search.ts +158 -0
  368. package/src/scripts/args.ts +48 -0
  369. package/src/scripts/caching.ts +124 -0
  370. package/src/scripts/cli.ts +167 -0
  371. package/src/scripts/cli2.ts +125 -0
  372. package/src/scripts/cli3.ts +178 -0
  373. package/src/scripts/cli4.ts +184 -0
  374. package/src/scripts/cli5.ts +184 -0
  375. package/src/scripts/code_exec.ts +214 -0
  376. package/src/scripts/code_exec_files.ts +193 -0
  377. package/src/scripts/code_exec_simple.ts +129 -0
  378. package/src/scripts/content.ts +120 -0
  379. package/src/scripts/empty_input.ts +137 -0
  380. package/src/scripts/image.ts +178 -0
  381. package/src/scripts/memory.ts +97 -0
  382. package/src/scripts/search.ts +150 -0
  383. package/src/scripts/simple.ts +225 -0
  384. package/src/scripts/stream.ts +122 -0
  385. package/src/scripts/thinking.ts +150 -0
  386. package/src/scripts/tools.ts +155 -0
  387. package/src/specs/anthropic.simple.test.ts +317 -0
  388. package/src/specs/azure.simple.test.ts +316 -0
  389. package/src/specs/openai.simple.test.ts +316 -0
  390. package/src/specs/prune.test.ts +763 -0
  391. package/src/specs/reasoning.test.ts +165 -0
  392. package/src/specs/spec.utils.ts +3 -0
  393. package/src/specs/thinking-prune.test.ts +703 -0
  394. package/src/specs/token-distribution-edge-case.test.ts +316 -0
  395. package/src/specs/tool-error.test.ts +193 -0
  396. package/src/splitStream.test.ts +691 -0
  397. package/src/splitStream.ts +234 -0
  398. package/src/stream.test.ts +94 -0
  399. package/src/stream.ts +651 -0
  400. package/src/tools/CodeExecutor.ts +220 -0
  401. package/src/tools/ToolNode.ts +170 -0
  402. package/src/tools/example.ts +129 -0
  403. package/src/tools/handlers.ts +336 -0
  404. package/src/tools/search/anthropic.ts +51 -0
  405. package/src/tools/search/content.test.ts +173 -0
  406. package/src/tools/search/content.ts +147 -0
  407. package/src/tools/search/firecrawl.ts +210 -0
  408. package/src/tools/search/format.ts +250 -0
  409. package/src/tools/search/highlights.ts +320 -0
  410. package/src/tools/search/index.ts +2 -0
  411. package/src/tools/search/jina-reranker.test.ts +126 -0
  412. package/src/tools/search/output.md +2775 -0
  413. package/src/tools/search/rerankers.ts +242 -0
  414. package/src/tools/search/schema.ts +63 -0
  415. package/src/tools/search/search.ts +759 -0
  416. package/src/tools/search/serper-scraper.ts +155 -0
  417. package/src/tools/search/test.html +884 -0
  418. package/src/tools/search/test.md +643 -0
  419. package/src/tools/search/test.ts +159 -0
  420. package/src/tools/search/tool.ts +471 -0
  421. package/src/tools/search/types.ts +687 -0
  422. package/src/tools/search/utils.ts +79 -0
  423. package/src/types/graph.ts +185 -0
  424. package/src/types/index.ts +6 -0
  425. package/src/types/llm.ts +140 -0
  426. package/src/types/run.ts +89 -0
  427. package/src/types/stream.ts +400 -0
  428. package/src/types/tools.ts +80 -0
  429. package/src/utils/graph.ts +11 -0
  430. package/src/utils/index.ts +5 -0
  431. package/src/utils/llm.ts +27 -0
  432. package/src/utils/llmConfig.ts +183 -0
  433. package/src/utils/logging.ts +48 -0
  434. package/src/utils/misc.ts +57 -0
  435. package/src/utils/run.ts +101 -0
  436. package/src/utils/title.ts +165 -0
  437. package/src/utils/tokens.ts +70 -0
@@ -0,0 +1,853 @@
1
+ import { AzureOpenAI as AzureOpenAIClient } from 'openai';
2
+ import { AIMessageChunk } from '@langchain/core/messages';
3
+ import { ChatXAI as OriginalChatXAI } from '@langchain/xai';
4
+ import { ChatGenerationChunk } from '@langchain/core/outputs';
5
+ import { ToolDefinition } from '@langchain/core/language_models/base';
6
+ import { isLangChainTool } from '@langchain/core/utils/function_calling';
7
+ import { ChatDeepSeek as OriginalChatDeepSeek } from '@langchain/deepseek';
8
+ import { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
9
+ import {
10
+ getEndpoint,
11
+ OpenAIClient,
12
+ formatToOpenAITool,
13
+ ChatOpenAI as OriginalChatOpenAI,
14
+ AzureChatOpenAI as OriginalAzureChatOpenAI,
15
+ } from '@langchain/openai';
16
+ import type {
17
+ OpenAIChatCallOptions,
18
+ OpenAIRoleEnum,
19
+ HeaderValue,
20
+ HeadersLike,
21
+ } from './types';
22
+ import type { BindToolsInput } from '@langchain/core/language_models/chat_models';
23
+ import type { BaseMessage, UsageMetadata } from '@langchain/core/messages';
24
+ import type { ChatXAIInput } from '@langchain/xai';
25
+ import type * as t from '@langchain/openai';
26
+ import {
27
+ isReasoningModel,
28
+ _convertMessagesToOpenAIParams,
29
+ _convertMessagesToOpenAIResponsesParams,
30
+ _convertOpenAIResponsesDeltaToBaseMessageChunk,
31
+ type ResponseReturnStreamEvents,
32
+ } from './utils';
33
+
34
+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
35
+ const iife = <T>(fn: () => T) => fn();
36
+
37
+ export function isHeaders(headers: unknown): headers is Headers {
38
+ return (
39
+ typeof Headers !== 'undefined' &&
40
+ headers !== null &&
41
+ typeof headers === 'object' &&
42
+ Object.prototype.toString.call(headers) === '[object Headers]'
43
+ );
44
+ }
45
+
46
+ export function normalizeHeaders(
47
+ headers: HeadersLike
48
+ ): Record<string, HeaderValue | readonly HeaderValue[]> {
49
+ const output = iife(() => {
50
+ // If headers is a Headers instance
51
+ if (isHeaders(headers)) {
52
+ return headers;
53
+ }
54
+ // If headers is an array of [key, value] pairs
55
+ else if (Array.isArray(headers)) {
56
+ return new Headers(headers);
57
+ }
58
+ // If headers is a NullableHeaders-like object (has 'values' property that is a Headers)
59
+ else if (
60
+ typeof headers === 'object' &&
61
+ headers !== null &&
62
+ 'values' in headers &&
63
+ isHeaders(headers.values)
64
+ ) {
65
+ return headers.values;
66
+ }
67
+ // If headers is a plain object
68
+ else if (typeof headers === 'object' && headers !== null) {
69
+ const entries: [string, string][] = Object.entries(headers)
70
+ .filter(([, v]) => typeof v === 'string')
71
+ .map(([k, v]) => [k, v as string]);
72
+ return new Headers(entries);
73
+ }
74
+ return new Headers();
75
+ });
76
+
77
+ return Object.fromEntries(output.entries());
78
+ }
79
+
80
+ type OpenAICompletionParam =
81
+ OpenAIClient.Chat.Completions.ChatCompletionMessageParam;
82
+
83
+ type OpenAICoreRequestOptions = OpenAIClient.RequestOptions;
84
+
85
+ function createAbortHandler(controller: AbortController): () => void {
86
+ return function (): void {
87
+ controller.abort();
88
+ };
89
+ }
90
+ /**
91
+ * Formats a tool in either OpenAI format, or LangChain structured tool format
92
+ * into an OpenAI tool format. If the tool is already in OpenAI format, return without
93
+ * any changes. If it is in LangChain structured tool format, convert it to OpenAI tool format
94
+ * using OpenAI's `zodFunction` util, falling back to `convertToOpenAIFunction` if the parameters
95
+ * returned from the `zodFunction` util are not defined.
96
+ *
97
+ * @param {BindToolsInput} tool The tool to convert to an OpenAI tool.
98
+ * @param {Object} [fields] Additional fields to add to the OpenAI tool.
99
+ * @returns {ToolDefinition} The inputted tool in OpenAI tool format.
100
+ */
101
+ export function _convertToOpenAITool(
102
+ tool: BindToolsInput,
103
+ fields?: {
104
+ /**
105
+ * If `true`, model output is guaranteed to exactly match the JSON Schema
106
+ * provided in the function definition.
107
+ */
108
+ strict?: boolean;
109
+ }
110
+ ): OpenAIClient.ChatCompletionTool {
111
+ let toolDef: OpenAIClient.ChatCompletionTool | undefined;
112
+
113
+ if (isLangChainTool(tool)) {
114
+ toolDef = formatToOpenAITool(tool);
115
+ } else {
116
+ toolDef = tool as ToolDefinition;
117
+ }
118
+
119
+ if (fields?.strict !== undefined) {
120
+ toolDef.function.strict = fields.strict;
121
+ }
122
+
123
+ return toolDef;
124
+ }
125
+ export class CustomOpenAIClient extends OpenAIClient {
126
+ abortHandler?: () => void;
127
+ async fetchWithTimeout(
128
+ url: RequestInfo,
129
+ init: RequestInit | undefined,
130
+ ms: number,
131
+ controller: AbortController
132
+ ): Promise<Response> {
133
+ const { signal, ...options } = init || {};
134
+ const handler = createAbortHandler(controller);
135
+ this.abortHandler = handler;
136
+ if (signal) signal.addEventListener('abort', handler, { once: true });
137
+
138
+ const timeout = setTimeout(() => handler, ms);
139
+
140
+ const fetchOptions = {
141
+ signal: controller.signal as AbortSignal,
142
+ ...options,
143
+ };
144
+ if (fetchOptions.method != null) {
145
+ // Custom methods like 'patch' need to be uppercased
146
+ // See https://github.com/nodejs/undici/issues/2294
147
+ fetchOptions.method = fetchOptions.method.toUpperCase();
148
+ }
149
+
150
+ return (
151
+ // use undefined this binding; fetch errors if bound to something else in browser/cloudflare
152
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
153
+ /** @ts-ignore */
154
+ this.fetch.call(undefined, url, fetchOptions).finally(() => {
155
+ clearTimeout(timeout);
156
+ })
157
+ );
158
+ }
159
+ }
160
+ export class CustomAzureOpenAIClient extends AzureOpenAIClient {
161
+ abortHandler?: () => void;
162
+ async fetchWithTimeout(
163
+ url: RequestInfo,
164
+ init: RequestInit | undefined,
165
+ ms: number,
166
+ controller: AbortController
167
+ ): Promise<Response> {
168
+ const { signal, ...options } = init || {};
169
+ const handler = createAbortHandler(controller);
170
+ this.abortHandler = handler;
171
+ if (signal) signal.addEventListener('abort', handler, { once: true });
172
+
173
+ const timeout = setTimeout(() => handler, ms);
174
+
175
+ const fetchOptions = {
176
+ signal: controller.signal as AbortSignal,
177
+ ...options,
178
+ };
179
+ if (fetchOptions.method != null) {
180
+ // Custom methods like 'patch' need to be uppercased
181
+ // See https://github.com/nodejs/undici/issues/2294
182
+ fetchOptions.method = fetchOptions.method.toUpperCase();
183
+ }
184
+
185
+ return (
186
+ // use undefined this binding; fetch errors if bound to something else in browser/cloudflare
187
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
188
+ /** @ts-ignore */
189
+ this.fetch.call(undefined, url, fetchOptions).finally(() => {
190
+ clearTimeout(timeout);
191
+ })
192
+ );
193
+ }
194
+ }
195
+
196
+ /** @ts-expect-error We are intentionally overriding `getReasoningParams` */
197
+ export class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
198
+ public get exposedClient(): CustomOpenAIClient {
199
+ return this.client;
200
+ }
201
+ static lc_name(): string {
202
+ return 'IllumaOpenAI';
203
+ }
204
+ protected _getClientOptions(
205
+ options?: OpenAICoreRequestOptions
206
+ ): OpenAICoreRequestOptions {
207
+ if (!(this.client as OpenAIClient | undefined)) {
208
+ const openAIEndpointConfig: t.OpenAIEndpointConfig = {
209
+ baseURL: this.clientConfig.baseURL,
210
+ };
211
+
212
+ const endpoint = getEndpoint(openAIEndpointConfig);
213
+ const params = {
214
+ ...this.clientConfig,
215
+ baseURL: endpoint,
216
+ timeout: this.timeout,
217
+ maxRetries: 0,
218
+ };
219
+ if (params.baseURL == null) {
220
+ delete params.baseURL;
221
+ }
222
+
223
+ this.client = new CustomOpenAIClient(params);
224
+ }
225
+ const requestOptions = {
226
+ ...this.clientConfig,
227
+ ...options,
228
+ } as OpenAICoreRequestOptions;
229
+ return requestOptions;
230
+ }
231
+
232
+ /**
233
+ * Returns backwards compatible reasoning parameters from constructor params and call options
234
+ * @internal
235
+ */
236
+ getReasoningParams(
237
+ options?: this['ParsedCallOptions']
238
+ ): OpenAIClient.Reasoning | undefined {
239
+ // apply options in reverse order of importance -- newer options supersede older options
240
+ let reasoning: OpenAIClient.Reasoning | undefined;
241
+ if (this.reasoning !== undefined) {
242
+ reasoning = {
243
+ ...reasoning,
244
+ ...this.reasoning,
245
+ };
246
+ }
247
+ if (options?.reasoning !== undefined) {
248
+ reasoning = {
249
+ ...reasoning,
250
+ ...options.reasoning,
251
+ };
252
+ }
253
+
254
+ return reasoning;
255
+ }
256
+
257
+ protected _getReasoningParams(
258
+ options?: this['ParsedCallOptions']
259
+ ): OpenAIClient.Reasoning | undefined {
260
+ return this.getReasoningParams(options);
261
+ }
262
+
263
+ async *_streamResponseChunks(
264
+ messages: BaseMessage[],
265
+ options: this['ParsedCallOptions'],
266
+ runManager?: CallbackManagerForLLMRun
267
+ ): AsyncGenerator<ChatGenerationChunk> {
268
+ if (!this._useResponseApi(options)) {
269
+ return yield* this._streamResponseChunks2(messages, options, runManager);
270
+ }
271
+ const streamIterable = await this.responseApiWithRetry(
272
+ {
273
+ ...this.invocationParams<'responses'>(options, { streaming: true }),
274
+ input: _convertMessagesToOpenAIResponsesParams(
275
+ messages,
276
+ this.model,
277
+ this.zdrEnabled
278
+ ),
279
+ stream: true,
280
+ },
281
+ options
282
+ );
283
+
284
+ for await (const data of streamIterable) {
285
+ const chunk = _convertOpenAIResponsesDeltaToBaseMessageChunk(
286
+ data as ResponseReturnStreamEvents
287
+ );
288
+ if (chunk == null) continue;
289
+ yield chunk;
290
+ await runManager?.handleLLMNewToken(
291
+ chunk.text || '',
292
+ undefined,
293
+ undefined,
294
+ undefined,
295
+ undefined,
296
+ { chunk }
297
+ );
298
+ }
299
+
300
+ return;
301
+ }
302
+
303
+ async *_streamResponseChunks2(
304
+ messages: BaseMessage[],
305
+ options: this['ParsedCallOptions'],
306
+ runManager?: CallbackManagerForLLMRun
307
+ ): AsyncGenerator<ChatGenerationChunk> {
308
+ const messagesMapped: OpenAICompletionParam[] =
309
+ _convertMessagesToOpenAIParams(messages, this.model);
310
+
311
+ const params = {
312
+ ...this.invocationParams(options, {
313
+ streaming: true,
314
+ }),
315
+ messages: messagesMapped,
316
+ stream: true as const,
317
+ };
318
+ let defaultRole: OpenAIRoleEnum | undefined;
319
+
320
+ const streamIterable = await this.completionWithRetry(params, options);
321
+ let usage: OpenAIClient.Completions.CompletionUsage | undefined;
322
+ for await (const data of streamIterable) {
323
+ const choice = data.choices[0] as
324
+ | Partial<OpenAIClient.Chat.Completions.ChatCompletionChunk.Choice>
325
+ | undefined;
326
+ if (data.usage) {
327
+ usage = data.usage;
328
+ }
329
+ if (!choice) {
330
+ continue;
331
+ }
332
+
333
+ const { delta } = choice;
334
+ if (!delta) {
335
+ continue;
336
+ }
337
+ const chunk = this._convertOpenAIDeltaToBaseMessageChunk(
338
+ delta,
339
+ data,
340
+ defaultRole
341
+ );
342
+ if ('reasoning_content' in delta) {
343
+ chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
344
+ } else if ('reasoning' in delta) {
345
+ chunk.additional_kwargs.reasoning_content = delta.reasoning;
346
+ }
347
+ if ('provider_specific_fields' in delta) {
348
+ chunk.additional_kwargs.provider_specific_fields =
349
+ delta.provider_specific_fields;
350
+ }
351
+ defaultRole = delta.role ?? defaultRole;
352
+ const newTokenIndices = {
353
+ prompt: options.promptIndex ?? 0,
354
+ completion: choice.index ?? 0,
355
+ };
356
+ if (typeof chunk.content !== 'string') {
357
+ // eslint-disable-next-line no-console
358
+ console.log(
359
+ '[WARNING]: Received non-string content from OpenAI. This is currently not supported.'
360
+ );
361
+ continue;
362
+ }
363
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
364
+ const generationInfo: Record<string, any> = { ...newTokenIndices };
365
+ if (choice.finish_reason != null) {
366
+ generationInfo.finish_reason = choice.finish_reason;
367
+ // Only include system fingerprint in the last chunk for now
368
+ // to avoid concatenation issues
369
+ generationInfo.system_fingerprint = data.system_fingerprint;
370
+ generationInfo.model_name = data.model;
371
+ generationInfo.service_tier = data.service_tier;
372
+ }
373
+ if (this.logprobs == true) {
374
+ generationInfo.logprobs = choice.logprobs;
375
+ }
376
+ const generationChunk = new ChatGenerationChunk({
377
+ message: chunk,
378
+ text: chunk.content,
379
+ generationInfo,
380
+ });
381
+ yield generationChunk;
382
+ await runManager?.handleLLMNewToken(
383
+ generationChunk.text || '',
384
+ newTokenIndices,
385
+ undefined,
386
+ undefined,
387
+ undefined,
388
+ { chunk: generationChunk }
389
+ );
390
+ }
391
+ if (usage) {
392
+ const inputTokenDetails = {
393
+ ...(usage.prompt_tokens_details?.audio_tokens != null && {
394
+ audio: usage.prompt_tokens_details.audio_tokens,
395
+ }),
396
+ ...(usage.prompt_tokens_details?.cached_tokens != null && {
397
+ cache_read: usage.prompt_tokens_details.cached_tokens,
398
+ }),
399
+ };
400
+ const outputTokenDetails = {
401
+ ...(usage.completion_tokens_details?.audio_tokens != null && {
402
+ audio: usage.completion_tokens_details.audio_tokens,
403
+ }),
404
+ ...(usage.completion_tokens_details?.reasoning_tokens != null && {
405
+ reasoning: usage.completion_tokens_details.reasoning_tokens,
406
+ }),
407
+ };
408
+ const generationChunk = new ChatGenerationChunk({
409
+ message: new AIMessageChunk({
410
+ content: '',
411
+ response_metadata: {
412
+ usage: { ...usage },
413
+ },
414
+ usage_metadata: {
415
+ input_tokens: usage.prompt_tokens,
416
+ output_tokens: usage.completion_tokens,
417
+ total_tokens: usage.total_tokens,
418
+ ...(Object.keys(inputTokenDetails).length > 0 && {
419
+ input_token_details: inputTokenDetails,
420
+ }),
421
+ ...(Object.keys(outputTokenDetails).length > 0 && {
422
+ output_token_details: outputTokenDetails,
423
+ }),
424
+ },
425
+ }),
426
+ text: '',
427
+ });
428
+ yield generationChunk;
429
+ }
430
+ if (options.signal?.aborted === true) {
431
+ throw new Error('AbortError');
432
+ }
433
+ }
434
+ }
435
+
436
+ /** @ts-expect-error We are intentionally overriding `getReasoningParams` */
437
+ export class AzureChatOpenAI extends OriginalAzureChatOpenAI {
438
+ public get exposedClient(): CustomOpenAIClient {
439
+ return this.client;
440
+ }
441
+ static lc_name(): 'IllumaAzureOpenAI' {
442
+ return 'IllumaAzureOpenAI';
443
+ }
444
+ /**
445
+ * Returns backwards compatible reasoning parameters from constructor params and call options
446
+ * @internal
447
+ */
448
+ getReasoningParams(
449
+ options?: this['ParsedCallOptions']
450
+ ): OpenAIClient.Reasoning | undefined {
451
+ if (!isReasoningModel(this.model)) {
452
+ return;
453
+ }
454
+
455
+ // apply options in reverse order of importance -- newer options supersede older options
456
+ let reasoning: OpenAIClient.Reasoning | undefined;
457
+ if (this.reasoning !== undefined) {
458
+ reasoning = {
459
+ ...reasoning,
460
+ ...this.reasoning,
461
+ };
462
+ }
463
+ if (options?.reasoning !== undefined) {
464
+ reasoning = {
465
+ ...reasoning,
466
+ ...options.reasoning,
467
+ };
468
+ }
469
+
470
+ return reasoning;
471
+ }
472
+
473
+ protected _getReasoningParams(
474
+ options?: this['ParsedCallOptions']
475
+ ): OpenAIClient.Reasoning | undefined {
476
+ return this.getReasoningParams(options);
477
+ }
478
+
479
+ protected _getClientOptions(
480
+ options: OpenAICoreRequestOptions | undefined
481
+ ): OpenAICoreRequestOptions {
482
+ if (!(this.client as unknown as AzureOpenAIClient | undefined)) {
483
+ const openAIEndpointConfig: t.OpenAIEndpointConfig = {
484
+ azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
485
+ azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
486
+ azureOpenAIApiKey: this.azureOpenAIApiKey,
487
+ azureOpenAIBasePath: this.azureOpenAIBasePath,
488
+ azureADTokenProvider: this.azureADTokenProvider,
489
+ baseURL: this.clientConfig.baseURL,
490
+ };
491
+
492
+ const endpoint = getEndpoint(openAIEndpointConfig);
493
+
494
+ const params = {
495
+ ...this.clientConfig,
496
+ baseURL: endpoint,
497
+ timeout: this.timeout,
498
+ maxRetries: 0,
499
+ };
500
+
501
+ if (!this.azureADTokenProvider) {
502
+ params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
503
+ }
504
+
505
+ if (params.baseURL == null) {
506
+ delete params.baseURL;
507
+ }
508
+
509
+ const defaultHeaders = normalizeHeaders(params.defaultHeaders);
510
+ params.defaultHeaders = {
511
+ ...params.defaultHeaders,
512
+ 'User-Agent':
513
+ defaultHeaders['User-Agent'] != null
514
+ ? `${defaultHeaders['User-Agent']}: illuma-azure-openai-v2`
515
+ : 'illuma-azure-openai-v2',
516
+ };
517
+
518
+ this.client = new CustomAzureOpenAIClient({
519
+ apiVersion: this.azureOpenAIApiVersion,
520
+ azureADTokenProvider: this.azureADTokenProvider,
521
+ ...(params as t.AzureOpenAIInput),
522
+ }) as unknown as CustomOpenAIClient;
523
+ }
524
+
525
+ const requestOptions = {
526
+ ...this.clientConfig,
527
+ ...options,
528
+ } as OpenAICoreRequestOptions;
529
+ if (this.azureOpenAIApiKey != null) {
530
+ requestOptions.headers = {
531
+ 'api-key': this.azureOpenAIApiKey,
532
+ ...requestOptions.headers,
533
+ };
534
+ requestOptions.query = {
535
+ 'api-version': this.azureOpenAIApiVersion,
536
+ ...requestOptions.query,
537
+ };
538
+ }
539
+ return requestOptions;
540
+ }
541
+ async *_streamResponseChunks(
542
+ messages: BaseMessage[],
543
+ options: this['ParsedCallOptions'],
544
+ runManager?: CallbackManagerForLLMRun
545
+ ): AsyncGenerator<ChatGenerationChunk> {
546
+ if (!this._useResponseApi(options)) {
547
+ return yield* super._streamResponseChunks(messages, options, runManager);
548
+ }
549
+ const streamIterable = await this.responseApiWithRetry(
550
+ {
551
+ ...this.invocationParams<'responses'>(options, { streaming: true }),
552
+ input: _convertMessagesToOpenAIResponsesParams(
553
+ messages,
554
+ this.model,
555
+ this.zdrEnabled
556
+ ),
557
+ stream: true,
558
+ },
559
+ options
560
+ );
561
+
562
+ for await (const data of streamIterable) {
563
+ const chunk = _convertOpenAIResponsesDeltaToBaseMessageChunk(
564
+ data as ResponseReturnStreamEvents
565
+ );
566
+ if (chunk == null) continue;
567
+ yield chunk;
568
+ await runManager?.handleLLMNewToken(
569
+ chunk.text || '',
570
+ undefined,
571
+ undefined,
572
+ undefined,
573
+ undefined,
574
+ { chunk }
575
+ );
576
+ }
577
+
578
+ return;
579
+ }
580
+ }
581
+ export class ChatDeepSeek extends OriginalChatDeepSeek {
582
+ public get exposedClient(): CustomOpenAIClient {
583
+ return this.client;
584
+ }
585
+ static lc_name(): 'IllumaDeepSeek' {
586
+ return 'IllumaDeepSeek';
587
+ }
588
+ protected _getClientOptions(
589
+ options?: OpenAICoreRequestOptions
590
+ ): OpenAICoreRequestOptions {
591
+ if (!(this.client as OpenAIClient | undefined)) {
592
+ const openAIEndpointConfig: t.OpenAIEndpointConfig = {
593
+ baseURL: this.clientConfig.baseURL,
594
+ };
595
+
596
+ const endpoint = getEndpoint(openAIEndpointConfig);
597
+ const params = {
598
+ ...this.clientConfig,
599
+ baseURL: endpoint,
600
+ timeout: this.timeout,
601
+ maxRetries: 0,
602
+ };
603
+ if (params.baseURL == null) {
604
+ delete params.baseURL;
605
+ }
606
+
607
+ this.client = new CustomOpenAIClient(params);
608
+ }
609
+ const requestOptions = {
610
+ ...this.clientConfig,
611
+ ...options,
612
+ } as OpenAICoreRequestOptions;
613
+ return requestOptions;
614
+ }
615
+ }
616
+
617
+ /** xAI-specific usage metadata type */
618
+ export interface XAIUsageMetadata
619
+ extends OpenAIClient.Completions.CompletionUsage {
620
+ prompt_tokens_details?: {
621
+ audio_tokens?: number;
622
+ cached_tokens?: number;
623
+ text_tokens?: number;
624
+ image_tokens?: number;
625
+ };
626
+ completion_tokens_details?: {
627
+ audio_tokens?: number;
628
+ reasoning_tokens?: number;
629
+ accepted_prediction_tokens?: number;
630
+ rejected_prediction_tokens?: number;
631
+ };
632
+ num_sources_used?: number;
633
+ }
634
+
635
+ export class ChatXAI extends OriginalChatXAI {
636
+ constructor(
637
+ fields?: Partial<ChatXAIInput> & {
638
+ configuration?: { baseURL?: string };
639
+ clientConfig?: { baseURL?: string };
640
+ }
641
+ ) {
642
+ super(fields);
643
+ const customBaseURL =
644
+ fields?.configuration?.baseURL ?? fields?.clientConfig?.baseURL;
645
+ if (customBaseURL != null && customBaseURL) {
646
+ this.clientConfig = {
647
+ ...this.clientConfig,
648
+ baseURL: customBaseURL,
649
+ };
650
+ // Reset the client to force recreation with new config
651
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
652
+ this.client = undefined as any;
653
+ }
654
+ }
655
+
656
+ static lc_name(): 'IllumaXAI' {
657
+ return 'IllumaXAI';
658
+ }
659
+
660
+ public get exposedClient(): CustomOpenAIClient {
661
+ return this.client;
662
+ }
663
+
664
+ protected _getClientOptions(
665
+ options?: OpenAICoreRequestOptions
666
+ ): OpenAICoreRequestOptions {
667
+ if (!(this.client as OpenAIClient | undefined)) {
668
+ const openAIEndpointConfig: t.OpenAIEndpointConfig = {
669
+ baseURL: this.clientConfig.baseURL,
670
+ };
671
+
672
+ const endpoint = getEndpoint(openAIEndpointConfig);
673
+ const params = {
674
+ ...this.clientConfig,
675
+ baseURL: endpoint,
676
+ timeout: this.timeout,
677
+ maxRetries: 0,
678
+ };
679
+ if (params.baseURL == null) {
680
+ delete params.baseURL;
681
+ }
682
+
683
+ this.client = new CustomOpenAIClient(params);
684
+ }
685
+ const requestOptions = {
686
+ ...this.clientConfig,
687
+ ...options,
688
+ } as OpenAICoreRequestOptions;
689
+ return requestOptions;
690
+ }
691
+
692
+ async *_streamResponseChunks(
693
+ messages: BaseMessage[],
694
+ options: this['ParsedCallOptions'],
695
+ runManager?: CallbackManagerForLLMRun
696
+ ): AsyncGenerator<ChatGenerationChunk> {
697
+ const messagesMapped: OpenAICompletionParam[] =
698
+ _convertMessagesToOpenAIParams(messages, this.model);
699
+
700
+ const params = {
701
+ ...this.invocationParams(options, {
702
+ streaming: true,
703
+ }),
704
+ messages: messagesMapped,
705
+ stream: true as const,
706
+ };
707
+ let defaultRole: OpenAIRoleEnum | undefined;
708
+
709
+ const streamIterable = await this.completionWithRetry(params, options);
710
+ let usage: OpenAIClient.Completions.CompletionUsage | undefined;
711
+ for await (const data of streamIterable) {
712
+ const choice = data.choices[0] as
713
+ | Partial<OpenAIClient.Chat.Completions.ChatCompletionChunk.Choice>
714
+ | undefined;
715
+ if (data.usage) {
716
+ usage = data.usage;
717
+ }
718
+ if (!choice) {
719
+ continue;
720
+ }
721
+
722
+ const { delta } = choice;
723
+ if (!delta) {
724
+ continue;
725
+ }
726
+ const chunk = this._convertOpenAIDeltaToBaseMessageChunk(
727
+ delta,
728
+ data,
729
+ defaultRole
730
+ );
731
+ if (chunk.usage_metadata != null) {
732
+ chunk.usage_metadata = {
733
+ input_tokens:
734
+ (chunk.usage_metadata as Partial<UsageMetadata>).input_tokens ?? 0,
735
+ output_tokens:
736
+ (chunk.usage_metadata as Partial<UsageMetadata>).output_tokens ?? 0,
737
+ total_tokens:
738
+ (chunk.usage_metadata as Partial<UsageMetadata>).total_tokens ?? 0,
739
+ };
740
+ }
741
+ if ('reasoning_content' in delta) {
742
+ chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
743
+ }
744
+ defaultRole = delta.role ?? defaultRole;
745
+ const newTokenIndices = {
746
+ prompt: (options as OpenAIChatCallOptions).promptIndex ?? 0,
747
+ completion: choice.index ?? 0,
748
+ };
749
+ if (typeof chunk.content !== 'string') {
750
+ // eslint-disable-next-line no-console
751
+ console.log(
752
+ '[WARNING]: Received non-string content from OpenAI. This is currently not supported.'
753
+ );
754
+ continue;
755
+ }
756
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
757
+ const generationInfo: Record<string, any> = { ...newTokenIndices };
758
+ if (choice.finish_reason != null) {
759
+ generationInfo.finish_reason = choice.finish_reason;
760
+ // Only include system fingerprint in the last chunk for now
761
+ // to avoid concatenation issues
762
+ generationInfo.system_fingerprint = data.system_fingerprint;
763
+ generationInfo.model_name = data.model;
764
+ generationInfo.service_tier = data.service_tier;
765
+ }
766
+ if (this.logprobs == true) {
767
+ generationInfo.logprobs = choice.logprobs;
768
+ }
769
+ const generationChunk = new ChatGenerationChunk({
770
+ message: chunk,
771
+ text: chunk.content,
772
+ generationInfo,
773
+ });
774
+ yield generationChunk;
775
+ await runManager?.handleLLMNewToken(
776
+ generationChunk.text || '',
777
+ newTokenIndices,
778
+ undefined,
779
+ undefined,
780
+ undefined,
781
+ { chunk: generationChunk }
782
+ );
783
+ }
784
+ if (usage) {
785
+ // Type assertion for xAI-specific usage structure
786
+ const xaiUsage = usage as XAIUsageMetadata;
787
+ const inputTokenDetails = {
788
+ // Standard OpenAI fields
789
+ ...(usage.prompt_tokens_details?.audio_tokens != null && {
790
+ audio: usage.prompt_tokens_details.audio_tokens,
791
+ }),
792
+ ...(usage.prompt_tokens_details?.cached_tokens != null && {
793
+ cache_read: usage.prompt_tokens_details.cached_tokens,
794
+ }),
795
+ // Add xAI-specific prompt token details if they exist
796
+ ...(xaiUsage.prompt_tokens_details?.text_tokens != null && {
797
+ text: xaiUsage.prompt_tokens_details.text_tokens,
798
+ }),
799
+ ...(xaiUsage.prompt_tokens_details?.image_tokens != null && {
800
+ image: xaiUsage.prompt_tokens_details.image_tokens,
801
+ }),
802
+ };
803
+ const outputTokenDetails = {
804
+ // Standard OpenAI fields
805
+ ...(usage.completion_tokens_details?.audio_tokens != null && {
806
+ audio: usage.completion_tokens_details.audio_tokens,
807
+ }),
808
+ ...(usage.completion_tokens_details?.reasoning_tokens != null && {
809
+ reasoning: usage.completion_tokens_details.reasoning_tokens,
810
+ }),
811
+ // Add xAI-specific completion token details if they exist
812
+ ...(xaiUsage.completion_tokens_details?.accepted_prediction_tokens !=
813
+ null && {
814
+ accepted_prediction:
815
+ xaiUsage.completion_tokens_details.accepted_prediction_tokens,
816
+ }),
817
+ ...(xaiUsage.completion_tokens_details?.rejected_prediction_tokens !=
818
+ null && {
819
+ rejected_prediction:
820
+ xaiUsage.completion_tokens_details.rejected_prediction_tokens,
821
+ }),
822
+ };
823
+ const generationChunk = new ChatGenerationChunk({
824
+ message: new AIMessageChunk({
825
+ content: '',
826
+ response_metadata: {
827
+ usage: { ...usage },
828
+ // Include xAI-specific metadata if it exists
829
+ ...(xaiUsage.num_sources_used != null && {
830
+ num_sources_used: xaiUsage.num_sources_used,
831
+ }),
832
+ },
833
+ usage_metadata: {
834
+ input_tokens: usage.prompt_tokens,
835
+ output_tokens: usage.completion_tokens,
836
+ total_tokens: usage.total_tokens,
837
+ ...(Object.keys(inputTokenDetails).length > 0 && {
838
+ input_token_details: inputTokenDetails,
839
+ }),
840
+ ...(Object.keys(outputTokenDetails).length > 0 && {
841
+ output_token_details: outputTokenDetails,
842
+ }),
843
+ },
844
+ }),
845
+ text: '',
846
+ });
847
+ yield generationChunk;
848
+ }
849
+ if (options.signal?.aborted === true) {
850
+ throw new Error('AbortError');
851
+ }
852
+ }
853
+ }