@librechat/agents 3.0.0-rc1 → 3.0.0-rc11

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 (187) hide show
  1. package/dist/cjs/common/enum.cjs +1 -0
  2. package/dist/cjs/common/enum.cjs.map +1 -1
  3. package/dist/cjs/graphs/Graph.cjs +7 -2
  4. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  5. package/dist/cjs/graphs/MultiAgentGraph.cjs +229 -44
  6. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  7. package/dist/cjs/llm/anthropic/index.cjs +21 -2
  8. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  9. package/dist/cjs/llm/google/index.cjs +3 -0
  10. package/dist/cjs/llm/google/index.cjs.map +1 -1
  11. package/dist/cjs/llm/google/utils/common.cjs +13 -0
  12. package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
  13. package/dist/cjs/llm/ollama/index.cjs +3 -0
  14. package/dist/cjs/llm/ollama/index.cjs.map +1 -1
  15. package/dist/cjs/llm/openai/index.cjs +53 -1
  16. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  17. package/dist/cjs/llm/openai/utils/index.cjs +6 -1
  18. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
  19. package/dist/cjs/llm/openrouter/index.cjs +5 -1
  20. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  21. package/dist/cjs/llm/vertexai/index.cjs +1 -1
  22. package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
  23. package/dist/cjs/main.cjs +3 -1
  24. package/dist/cjs/main.cjs.map +1 -1
  25. package/dist/cjs/messages/core.cjs +5 -1
  26. package/dist/cjs/messages/core.cjs.map +1 -1
  27. package/dist/cjs/messages/format.cjs +52 -34
  28. package/dist/cjs/messages/format.cjs.map +1 -1
  29. package/dist/cjs/messages/prune.cjs +28 -0
  30. package/dist/cjs/messages/prune.cjs.map +1 -1
  31. package/dist/cjs/run.cjs +28 -15
  32. package/dist/cjs/run.cjs.map +1 -1
  33. package/dist/cjs/stream.cjs +1 -1
  34. package/dist/cjs/stream.cjs.map +1 -1
  35. package/dist/cjs/tools/ToolNode.cjs +2 -0
  36. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  37. package/dist/cjs/tools/search/firecrawl.cjs +3 -1
  38. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
  39. package/dist/cjs/tools/search/rerankers.cjs +8 -6
  40. package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
  41. package/dist/cjs/tools/search/search.cjs +5 -5
  42. package/dist/cjs/tools/search/search.cjs.map +1 -1
  43. package/dist/cjs/tools/search/serper-scraper.cjs +132 -0
  44. package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -0
  45. package/dist/cjs/tools/search/tool.cjs +46 -9
  46. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  47. package/dist/cjs/utils/handlers.cjs +70 -0
  48. package/dist/cjs/utils/handlers.cjs.map +1 -0
  49. package/dist/esm/common/enum.mjs +1 -0
  50. package/dist/esm/common/enum.mjs.map +1 -1
  51. package/dist/esm/graphs/Graph.mjs +7 -2
  52. package/dist/esm/graphs/Graph.mjs.map +1 -1
  53. package/dist/esm/graphs/MultiAgentGraph.mjs +230 -45
  54. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  55. package/dist/esm/llm/anthropic/index.mjs +21 -2
  56. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  57. package/dist/esm/llm/google/index.mjs +3 -0
  58. package/dist/esm/llm/google/index.mjs.map +1 -1
  59. package/dist/esm/llm/google/utils/common.mjs +13 -0
  60. package/dist/esm/llm/google/utils/common.mjs.map +1 -1
  61. package/dist/esm/llm/ollama/index.mjs +3 -0
  62. package/dist/esm/llm/ollama/index.mjs.map +1 -1
  63. package/dist/esm/llm/openai/index.mjs +53 -1
  64. package/dist/esm/llm/openai/index.mjs.map +1 -1
  65. package/dist/esm/llm/openai/utils/index.mjs +6 -1
  66. package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
  67. package/dist/esm/llm/openrouter/index.mjs +5 -1
  68. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  69. package/dist/esm/llm/vertexai/index.mjs +1 -1
  70. package/dist/esm/llm/vertexai/index.mjs.map +1 -1
  71. package/dist/esm/main.mjs +2 -1
  72. package/dist/esm/main.mjs.map +1 -1
  73. package/dist/esm/messages/core.mjs +5 -1
  74. package/dist/esm/messages/core.mjs.map +1 -1
  75. package/dist/esm/messages/format.mjs +52 -34
  76. package/dist/esm/messages/format.mjs.map +1 -1
  77. package/dist/esm/messages/prune.mjs +28 -0
  78. package/dist/esm/messages/prune.mjs.map +1 -1
  79. package/dist/esm/run.mjs +28 -15
  80. package/dist/esm/run.mjs.map +1 -1
  81. package/dist/esm/stream.mjs +1 -1
  82. package/dist/esm/stream.mjs.map +1 -1
  83. package/dist/esm/tools/ToolNode.mjs +2 -0
  84. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  85. package/dist/esm/tools/search/firecrawl.mjs +3 -1
  86. package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
  87. package/dist/esm/tools/search/rerankers.mjs +8 -6
  88. package/dist/esm/tools/search/rerankers.mjs.map +1 -1
  89. package/dist/esm/tools/search/search.mjs +5 -5
  90. package/dist/esm/tools/search/search.mjs.map +1 -1
  91. package/dist/esm/tools/search/serper-scraper.mjs +129 -0
  92. package/dist/esm/tools/search/serper-scraper.mjs.map +1 -0
  93. package/dist/esm/tools/search/tool.mjs +46 -9
  94. package/dist/esm/tools/search/tool.mjs.map +1 -1
  95. package/dist/esm/utils/handlers.mjs +68 -0
  96. package/dist/esm/utils/handlers.mjs.map +1 -0
  97. package/dist/types/common/enum.d.ts +2 -1
  98. package/dist/types/graphs/MultiAgentGraph.d.ts +12 -2
  99. package/dist/types/llm/anthropic/index.d.ts +3 -0
  100. package/dist/types/llm/google/index.d.ts +1 -0
  101. package/dist/types/llm/ollama/index.d.ts +1 -0
  102. package/dist/types/llm/openai/index.d.ts +14 -0
  103. package/dist/types/llm/openrouter/index.d.ts +4 -2
  104. package/dist/types/llm/vertexai/index.d.ts +1 -1
  105. package/dist/types/messages/format.d.ts +23 -20
  106. package/dist/types/run.d.ts +1 -1
  107. package/dist/types/tools/search/firecrawl.d.ts +2 -1
  108. package/dist/types/tools/search/rerankers.d.ts +4 -1
  109. package/dist/types/tools/search/search.d.ts +1 -2
  110. package/dist/types/tools/search/serper-scraper.d.ts +59 -0
  111. package/dist/types/tools/search/tool.d.ts +25 -4
  112. package/dist/types/tools/search/types.d.ts +31 -1
  113. package/dist/types/types/graph.d.ts +38 -4
  114. package/dist/types/types/llm.d.ts +1 -0
  115. package/dist/types/types/run.d.ts +5 -1
  116. package/dist/types/utils/handlers.d.ts +34 -0
  117. package/dist/types/utils/index.d.ts +1 -0
  118. package/package.json +11 -3
  119. package/src/common/enum.ts +1 -0
  120. package/src/graphs/Graph.ts +8 -2
  121. package/src/graphs/MultiAgentGraph.ts +267 -50
  122. package/src/llm/anthropic/index.ts +23 -2
  123. package/src/llm/google/index.ts +4 -0
  124. package/src/llm/google/utils/common.ts +14 -0
  125. package/src/llm/ollama/index.ts +3 -0
  126. package/src/llm/openai/index.ts +60 -1
  127. package/src/llm/openai/utils/index.ts +7 -1
  128. package/src/llm/openrouter/index.ts +15 -6
  129. package/src/llm/vertexai/index.ts +2 -2
  130. package/src/messages/core.ts +5 -2
  131. package/src/messages/format.ts +67 -39
  132. package/src/messages/formatMessage.test.ts +418 -2
  133. package/src/messages/prune.ts +51 -0
  134. package/src/run.ts +38 -27
  135. package/src/scripts/multi-agent-chain.ts +278 -0
  136. package/src/scripts/multi-agent-document-review-chain.ts +197 -0
  137. package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
  138. package/src/scripts/multi-agent-parallel.ts +27 -23
  139. package/src/scripts/multi-agent-supervisor.ts +362 -0
  140. package/src/scripts/search.ts +5 -1
  141. package/src/scripts/test-custom-prompt-key.ts +145 -0
  142. package/src/scripts/test-handoff-input.ts +170 -0
  143. package/src/scripts/test-multi-agent-list-handoff.ts +261 -0
  144. package/src/scripts/test-tools-before-handoff.ts +233 -0
  145. package/src/scripts/tools.ts +4 -1
  146. package/src/stream.ts +4 -1
  147. package/src/tools/search/firecrawl.ts +5 -2
  148. package/src/tools/search/jina-reranker.test.ts +126 -0
  149. package/src/tools/search/rerankers.ts +11 -5
  150. package/src/tools/search/search.ts +6 -8
  151. package/src/tools/search/serper-scraper.ts +155 -0
  152. package/src/tools/search/tool.ts +49 -8
  153. package/src/tools/search/types.ts +46 -0
  154. package/src/types/graph.ts +51 -5
  155. package/src/types/llm.ts +1 -0
  156. package/src/types/run.ts +6 -1
  157. package/src/utils/handlers.ts +107 -0
  158. package/src/utils/index.ts +2 -1
  159. package/src/utils/llmConfig.ts +35 -1
  160. package/dist/types/scripts/abort.d.ts +0 -1
  161. package/dist/types/scripts/ant_web_search.d.ts +0 -1
  162. package/dist/types/scripts/args.d.ts +0 -7
  163. package/dist/types/scripts/caching.d.ts +0 -1
  164. package/dist/types/scripts/cli.d.ts +0 -1
  165. package/dist/types/scripts/cli2.d.ts +0 -1
  166. package/dist/types/scripts/cli3.d.ts +0 -1
  167. package/dist/types/scripts/cli4.d.ts +0 -1
  168. package/dist/types/scripts/cli5.d.ts +0 -1
  169. package/dist/types/scripts/code_exec.d.ts +0 -1
  170. package/dist/types/scripts/code_exec_files.d.ts +0 -1
  171. package/dist/types/scripts/code_exec_simple.d.ts +0 -1
  172. package/dist/types/scripts/content.d.ts +0 -1
  173. package/dist/types/scripts/empty_input.d.ts +0 -1
  174. package/dist/types/scripts/handoff-test.d.ts +0 -1
  175. package/dist/types/scripts/image.d.ts +0 -1
  176. package/dist/types/scripts/memory.d.ts +0 -1
  177. package/dist/types/scripts/multi-agent-conditional.d.ts +0 -1
  178. package/dist/types/scripts/multi-agent-parallel.d.ts +0 -1
  179. package/dist/types/scripts/multi-agent-sequence.d.ts +0 -1
  180. package/dist/types/scripts/multi-agent-test.d.ts +0 -1
  181. package/dist/types/scripts/search.d.ts +0 -1
  182. package/dist/types/scripts/simple.d.ts +0 -1
  183. package/dist/types/scripts/stream.d.ts +0 -1
  184. package/dist/types/scripts/thinking.d.ts +0 -1
  185. package/dist/types/scripts/tools.d.ts +0 -1
  186. package/dist/types/specs/spec.utils.d.ts +0 -1
  187. package/src/scripts/multi-agent-example-output.md +0 -110
@@ -30,6 +30,7 @@ import {
30
30
  _convertOpenAIResponsesDeltaToBaseMessageChunk,
31
31
  type ResponseReturnStreamEvents,
32
32
  } from './utils';
33
+ import { sleep } from '@/utils';
33
34
 
34
35
  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
35
36
  const iife = <T>(fn: () => T) => fn();
@@ -195,9 +196,23 @@ export class CustomAzureOpenAIClient extends AzureOpenAIClient {
195
196
 
196
197
  /** @ts-expect-error We are intentionally overriding `getReasoningParams` */
197
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
+
198
210
  public get exposedClient(): CustomOpenAIClient {
199
211
  return this.client;
200
212
  }
213
+ static lc_name(): string {
214
+ return 'LibreChatOpenAI';
215
+ }
201
216
  protected _getClientOptions(
202
217
  options?: OpenAICoreRequestOptions
203
218
  ): OpenAICoreRequestOptions {
@@ -233,7 +248,8 @@ export class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
233
248
  getReasoningParams(
234
249
  options?: this['ParsedCallOptions']
235
250
  ): OpenAIClient.Reasoning | undefined {
236
- if (!isReasoningModel(this.model)) {
251
+ const lc_name = (this.constructor as typeof ChatOpenAI).lc_name();
252
+ if (lc_name === 'LibreChatOpenAI' && !isReasoningModel(this.model)) {
237
253
  return;
238
254
  }
239
255
 
@@ -288,6 +304,9 @@ export class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
288
304
  );
289
305
  if (chunk == null) continue;
290
306
  yield chunk;
307
+ if (this._lc_stream_delay != null) {
308
+ await sleep(this._lc_stream_delay);
309
+ }
291
310
  await runManager?.handleLLMNewToken(
292
311
  chunk.text || '',
293
312
  undefined,
@@ -345,6 +364,10 @@ export class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
345
364
  } else if ('reasoning' in delta) {
346
365
  chunk.additional_kwargs.reasoning_content = delta.reasoning;
347
366
  }
367
+ if ('provider_specific_fields' in delta) {
368
+ chunk.additional_kwargs.provider_specific_fields =
369
+ delta.provider_specific_fields;
370
+ }
348
371
  defaultRole = delta.role ?? defaultRole;
349
372
  const newTokenIndices = {
350
373
  prompt: options.promptIndex ?? 0,
@@ -376,6 +399,9 @@ export class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
376
399
  generationInfo,
377
400
  });
378
401
  yield generationChunk;
402
+ if (this._lc_stream_delay != null) {
403
+ await sleep(this._lc_stream_delay);
404
+ }
379
405
  await runManager?.handleLLMNewToken(
380
406
  generationChunk.text || '',
381
407
  newTokenIndices,
@@ -423,6 +449,9 @@ export class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
423
449
  text: '',
424
450
  });
425
451
  yield generationChunk;
452
+ if (this._lc_stream_delay != null) {
453
+ await sleep(this._lc_stream_delay);
454
+ }
426
455
  }
427
456
  if (options.signal?.aborted === true) {
428
457
  throw new Error('AbortError');
@@ -432,9 +461,19 @@ export class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
432
461
 
433
462
  /** @ts-expect-error We are intentionally overriding `getReasoningParams` */
434
463
  export class AzureChatOpenAI extends OriginalAzureChatOpenAI {
464
+ _lc_stream_delay?: number;
465
+
466
+ constructor(fields?: t.AzureOpenAIInput & { _lc_stream_delay?: number }) {
467
+ super(fields);
468
+ this._lc_stream_delay = fields?._lc_stream_delay;
469
+ }
470
+
435
471
  public get exposedClient(): CustomOpenAIClient {
436
472
  return this.client;
437
473
  }
474
+ static lc_name(): 'LibreChatAzureOpenAI' {
475
+ return 'LibreChatAzureOpenAI';
476
+ }
438
477
  /**
439
478
  * Returns backwards compatible reasoning parameters from constructor params and call options
440
479
  * @internal
@@ -559,6 +598,9 @@ export class AzureChatOpenAI extends OriginalAzureChatOpenAI {
559
598
  );
560
599
  if (chunk == null) continue;
561
600
  yield chunk;
601
+ if (this._lc_stream_delay != null) {
602
+ await sleep(this._lc_stream_delay);
603
+ }
562
604
  await runManager?.handleLLMNewToken(
563
605
  chunk.text || '',
564
606
  undefined,
@@ -576,6 +618,9 @@ export class ChatDeepSeek extends OriginalChatDeepSeek {
576
618
  public get exposedClient(): CustomOpenAIClient {
577
619
  return this.client;
578
620
  }
621
+ static lc_name(): 'LibreChatDeepSeek' {
622
+ return 'LibreChatDeepSeek';
623
+ }
579
624
  protected _getClientOptions(
580
625
  options?: OpenAICoreRequestOptions
581
626
  ): OpenAICoreRequestOptions {
@@ -624,13 +669,17 @@ export interface XAIUsageMetadata
624
669
  }
625
670
 
626
671
  export class ChatXAI extends OriginalChatXAI {
672
+ _lc_stream_delay?: number;
673
+
627
674
  constructor(
628
675
  fields?: Partial<ChatXAIInput> & {
629
676
  configuration?: { baseURL?: string };
630
677
  clientConfig?: { baseURL?: string };
678
+ _lc_stream_delay?: number;
631
679
  }
632
680
  ) {
633
681
  super(fields);
682
+ this._lc_stream_delay = fields?._lc_stream_delay;
634
683
  const customBaseURL =
635
684
  fields?.configuration?.baseURL ?? fields?.clientConfig?.baseURL;
636
685
  if (customBaseURL != null && customBaseURL) {
@@ -644,6 +693,10 @@ export class ChatXAI extends OriginalChatXAI {
644
693
  }
645
694
  }
646
695
 
696
+ static lc_name(): 'LibreChatXAI' {
697
+ return 'LibreChatXAI';
698
+ }
699
+
647
700
  public get exposedClient(): CustomOpenAIClient {
648
701
  return this.client;
649
702
  }
@@ -759,6 +812,9 @@ export class ChatXAI extends OriginalChatXAI {
759
812
  generationInfo,
760
813
  });
761
814
  yield generationChunk;
815
+ if (this._lc_stream_delay != null) {
816
+ await sleep(this._lc_stream_delay);
817
+ }
762
818
  await runManager?.handleLLMNewToken(
763
819
  generationChunk.text || '',
764
820
  newTokenIndices,
@@ -832,6 +888,9 @@ export class ChatXAI extends OriginalChatXAI {
832
888
  text: '',
833
889
  });
834
890
  yield generationChunk;
891
+ if (this._lc_stream_delay != null) {
892
+ await sleep(this._lc_stream_delay);
893
+ }
835
894
  }
836
895
  if (options.signal?.aborted === true) {
837
896
  throw new Error('AbortError');
@@ -298,10 +298,16 @@ export function _convertMessagesToOpenAIParams(
298
298
  role = 'developer';
299
299
  }
300
300
 
301
+ let hasAnthropicThinkingBlock: boolean = false;
302
+
301
303
  const content =
302
304
  typeof message.content === 'string'
303
305
  ? message.content
304
306
  : message.content.map((m) => {
307
+ if ('type' in m && m.type === 'thinking') {
308
+ hasAnthropicThinkingBlock = true;
309
+ return m;
310
+ }
305
311
  if (isDataContentBlock(m)) {
306
312
  return convertToProviderContentBlock(
307
313
  m,
@@ -326,7 +332,7 @@ export function _convertMessagesToOpenAIParams(
326
332
  completionParam.tool_calls = message.tool_calls.map(
327
333
  convertLangChainToolCallToOpenAI
328
334
  );
329
- completionParam.content = '';
335
+ completionParam.content = hasAnthropicThinkingBlock ? content : '';
330
336
  } else {
331
337
  if (message.additional_kwargs.tool_calls != null) {
332
338
  completionParam.tool_calls = message.additional_kwargs.tool_calls;
@@ -1,27 +1,36 @@
1
- import type { ChatOpenAICallOptions, OpenAIClient } from '@langchain/openai';
1
+ import { ChatOpenAI } from '@/llm/openai';
2
2
  import type {
3
- AIMessageChunk,
4
- HumanMessageChunk,
5
- SystemMessageChunk,
6
3
  FunctionMessageChunk,
4
+ SystemMessageChunk,
5
+ HumanMessageChunk,
7
6
  ToolMessageChunk,
8
7
  ChatMessageChunk,
8
+ AIMessageChunk,
9
9
  } from '@langchain/core/messages';
10
- import { ChatOpenAI } from '@/llm/openai';
10
+ import type {
11
+ ChatOpenAICallOptions,
12
+ OpenAIChatInput,
13
+ OpenAIClient,
14
+ } from '@langchain/openai';
11
15
 
12
16
  export interface ChatOpenRouterCallOptions extends ChatOpenAICallOptions {
13
17
  include_reasoning?: boolean;
18
+ modelKwargs?: OpenAIChatInput['modelKwargs'];
14
19
  }
15
20
  export class ChatOpenRouter extends ChatOpenAI {
16
21
  constructor(_fields: Partial<ChatOpenRouterCallOptions>) {
17
- const { include_reasoning, ...fields } = _fields;
22
+ const { include_reasoning, modelKwargs = {}, ...fields } = _fields;
18
23
  super({
19
24
  ...fields,
20
25
  modelKwargs: {
26
+ ...modelKwargs,
21
27
  include_reasoning,
22
28
  },
23
29
  });
24
30
  }
31
+ static lc_name(): 'LibreChatOpenRouter' {
32
+ return 'LibreChatOpenRouter';
33
+ }
25
34
  protected override _convertOpenAIDeltaToBaseMessageChunk(
26
35
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
36
  delta: Record<string, any>,
@@ -313,8 +313,8 @@ export class ChatVertexAI extends ChatGoogle {
313
313
  lc_namespace = ['langchain', 'chat_models', 'vertexai'];
314
314
  dynamicThinkingBudget = false;
315
315
 
316
- static lc_name(): 'ChatVertexAI' {
317
- return 'ChatVertexAI';
316
+ static lc_name(): 'LibreChatVertexAI' {
317
+ return 'LibreChatVertexAI';
318
318
  }
319
319
 
320
320
  constructor(fields?: VertexAIClientOptions) {
@@ -321,12 +321,15 @@ export function convertMessagesToContent(
321
321
  const id = (message as ToolMessage).tool_call_id;
322
322
  const output = (message as ToolMessage).content;
323
323
  const tool_call = toolCallMap.get(id);
324
-
324
+ if (currentAIMessageIndex === -1) {
325
+ processedContent.push({ type: 'text', text: '' });
326
+ currentAIMessageIndex = processedContent.length - 1;
327
+ }
328
+ const contentPart = processedContent[currentAIMessageIndex];
325
329
  processedContent.push({
326
330
  type: 'tool_call',
327
331
  tool_call: Object.assign({}, tool_call, { output }),
328
332
  });
329
- const contentPart = processedContent[currentAIMessageIndex];
330
333
  const tool_call_ids = contentPart.tool_call_ids || [];
331
334
  tool_call_ids.push(id);
332
335
  contentPart.tool_call_ids = tool_call_ids;
@@ -17,28 +17,28 @@ import type {
17
17
  } from '@/types';
18
18
  import { Providers, ContentTypes } from '@/common';
19
19
 
20
- interface VisionMessageParams {
20
+ interface MediaMessageParams {
21
21
  message: {
22
22
  role: string;
23
23
  content: string;
24
24
  name?: string;
25
25
  [key: string]: any;
26
26
  };
27
- image_urls: MessageContentImageUrl[];
27
+ mediaParts: MessageContentComplex[];
28
28
  endpoint?: Providers;
29
29
  }
30
30
 
31
31
  /**
32
- * Formats a message to OpenAI Vision API payload format.
32
+ * Formats a message with media content (images, documents, videos, audios) to API payload format.
33
33
  *
34
- * @param {VisionMessageParams} params - The parameters for formatting.
35
- * @returns {Object} - The formatted message.
34
+ * @param params - The parameters for formatting.
35
+ * @returns - The formatted message.
36
36
  */
37
- export const formatVisionMessage = ({
37
+ export const formatMediaMessage = ({
38
38
  message,
39
- image_urls,
40
39
  endpoint,
41
- }: VisionMessageParams): {
40
+ mediaParts,
41
+ }: MediaMessageParams): {
42
42
  role: string;
43
43
  content: MessageContentComplex[];
44
44
  name?: string;
@@ -57,7 +57,7 @@ export const formatVisionMessage = ({
57
57
 
58
58
  if (endpoint === Providers.ANTHROPIC) {
59
59
  result.content = [
60
- ...image_urls,
60
+ ...mediaParts,
61
61
  { type: ContentTypes.TEXT, text: message.content },
62
62
  ] as MessageContentComplex[];
63
63
  return result;
@@ -65,7 +65,7 @@ export const formatVisionMessage = ({
65
65
 
66
66
  result.content = [
67
67
  { type: ContentTypes.TEXT, text: message.content },
68
- ...image_urls,
68
+ ...mediaParts,
69
69
  ] as MessageContentComplex[];
70
70
 
71
71
  return result;
@@ -78,6 +78,9 @@ interface MessageInput {
78
78
  text?: string;
79
79
  content?: string | MessageContentComplex[];
80
80
  image_urls?: MessageContentImageUrl[];
81
+ documents?: MessageContentComplex[];
82
+ videos?: MessageContentComplex[];
83
+ audios?: MessageContentComplex[];
81
84
  lc_id?: string[];
82
85
  [key: string]: any;
83
86
  }
@@ -100,14 +103,14 @@ interface FormattedMessage {
100
103
  /**
101
104
  * Formats a message to OpenAI payload format based on the provided options.
102
105
  *
103
- * @param {FormatMessageParams} params - The parameters for formatting.
104
- * @returns {FormattedMessage | HumanMessage | AIMessage | SystemMessage} - The formatted message.
106
+ * @param params - The parameters for formatting.
107
+ * @returns - The formatted message.
105
108
  */
106
109
  export const formatMessage = ({
107
110
  message,
108
111
  userName,
109
- assistantName,
110
112
  endpoint,
113
+ assistantName,
111
114
  langChain = false,
112
115
  }: FormatMessageParams):
113
116
  | FormattedMessage
@@ -135,21 +138,7 @@ export const formatMessage = ({
135
138
  content,
136
139
  };
137
140
 
138
- const { image_urls } = message;
139
- if (Array.isArray(image_urls) && image_urls.length > 0 && role === 'user') {
140
- return formatVisionMessage({
141
- message: {
142
- ...formattedMessage,
143
- content:
144
- typeof formattedMessage.content === 'string'
145
- ? formattedMessage.content
146
- : '',
147
- },
148
- image_urls,
149
- endpoint,
150
- });
151
- }
152
-
141
+ // Set name fields first
153
142
  if (_name != null && _name) {
154
143
  formattedMessage.name = _name;
155
144
  }
@@ -179,6 +168,45 @@ export const formatMessage = ({
179
168
  }
180
169
  }
181
170
 
171
+ const { image_urls, documents, videos, audios } = message;
172
+ const mediaParts: MessageContentComplex[] = [];
173
+
174
+ if (Array.isArray(documents) && documents.length > 0) {
175
+ mediaParts.push(...documents);
176
+ }
177
+
178
+ if (Array.isArray(videos) && videos.length > 0) {
179
+ mediaParts.push(...videos);
180
+ }
181
+
182
+ if (Array.isArray(audios) && audios.length > 0) {
183
+ mediaParts.push(...audios);
184
+ }
185
+
186
+ if (Array.isArray(image_urls) && image_urls.length > 0) {
187
+ mediaParts.push(...image_urls);
188
+ }
189
+
190
+ if (mediaParts.length > 0 && role === 'user') {
191
+ const mediaMessage = formatMediaMessage({
192
+ message: {
193
+ ...formattedMessage,
194
+ content:
195
+ typeof formattedMessage.content === 'string'
196
+ ? formattedMessage.content
197
+ : '',
198
+ },
199
+ mediaParts,
200
+ endpoint,
201
+ });
202
+
203
+ if (!langChain) {
204
+ return mediaMessage;
205
+ }
206
+
207
+ return new HumanMessage(mediaMessage);
208
+ }
209
+
182
210
  if (!langChain) {
183
211
  return formattedMessage;
184
212
  }
@@ -195,9 +223,9 @@ export const formatMessage = ({
195
223
  /**
196
224
  * Formats an array of messages for LangChain.
197
225
  *
198
- * @param {Array<MessageInput>} messages - The array of messages to format.
199
- * @param {Omit<FormatMessageParams, 'message' | 'langChain'>} formatOptions - The options for formatting each message.
200
- * @returns {Array<HumanMessage | AIMessage | SystemMessage>} - The array of formatted LangChain messages.
226
+ * @param messages - The array of messages to format.
227
+ * @param formatOptions - The options for formatting each message.
228
+ * @returns - The array of formatted LangChain messages.
201
229
  */
202
230
  export const formatLangChainMessages = (
203
231
  messages: Array<MessageInput>,
@@ -228,8 +256,8 @@ interface LangChainMessage {
228
256
  /**
229
257
  * Formats a LangChain message object by merging properties from `lc_kwargs` or `kwargs` and `additional_kwargs`.
230
258
  *
231
- * @param {LangChainMessage} message - The message object to format.
232
- * @returns {Record<string, any>} The formatted LangChain message.
259
+ * @param message - The message object to format.
260
+ * @returns - The formatted LangChain message.
233
261
  */
234
262
  export const formatFromLangChain = (
235
263
  message: LangChainMessage
@@ -357,10 +385,10 @@ function formatAssistantMessage(
357
385
  /**
358
386
  * Formats an array of messages for LangChain, handling tool calls and creating ToolMessage instances.
359
387
  *
360
- * @param {TPayload} payload - The array of messages to format.
361
- * @param {Record<number, number>} [indexTokenCountMap] - Optional map of message indices to token counts.
362
- * @param {Set<string>} [tools] - Optional set of tool names that are allowed in the request.
363
- * @returns {Object} - Object containing formatted messages and updated indexTokenCountMap if provided.
388
+ * @param payload - The array of messages to format.
389
+ * @param indexTokenCountMap - Optional map of message indices to token counts.
390
+ * @param tools - Optional set of tool names that are allowed in the request.
391
+ * @returns - Object containing formatted messages and updated indexTokenCountMap if provided.
364
392
  */
365
393
  export const formatAgentMessages = (
366
394
  payload: TPayload,
@@ -539,8 +567,8 @@ export const formatAgentMessages = (
539
567
 
540
568
  /**
541
569
  * Formats an array of messages for LangChain, making sure all content fields are strings
542
- * @param {Array<HumanMessage | AIMessage | SystemMessage | ToolMessage>} payload - The array of messages to format.
543
- * @returns {Array<HumanMessage | AIMessage | SystemMessage | ToolMessage>} - The array of formatted LangChain messages, including ToolMessages for tool calls.
570
+ * @param payload - The array of messages to format.
571
+ * @returns - The array of formatted LangChain messages, including ToolMessages for tool calls.
544
572
  */
545
573
  export const formatContentStrings = (
546
574
  payload: Array<BaseMessage>