@illuma-ai/agents 1.1.21 → 1.1.22

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