illuma-agents 1.0.2 → 1.0.4

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