@librechat/agents 3.0.776 → 3.1.1

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 (108) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +2 -5
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/graphs/Graph.cjs +20 -5
  4. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  5. package/dist/cjs/graphs/MultiAgentGraph.cjs +26 -17
  6. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  7. package/dist/cjs/llm/bedrock/index.cjs +98 -25
  8. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  9. package/dist/cjs/llm/openai/index.cjs +1 -0
  10. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  11. package/dist/cjs/main.cjs +3 -0
  12. package/dist/cjs/main.cjs.map +1 -1
  13. package/dist/cjs/messages/core.cjs +1 -1
  14. package/dist/cjs/messages/core.cjs.map +1 -1
  15. package/dist/cjs/stream.cjs +4 -2
  16. package/dist/cjs/stream.cjs.map +1 -1
  17. package/dist/cjs/tools/CodeExecutor.cjs +37 -27
  18. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  19. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +21 -17
  20. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  21. package/dist/cjs/tools/ToolNode.cjs +10 -5
  22. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  23. package/dist/cjs/tools/ToolSearch.cjs +37 -30
  24. package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
  25. package/dist/cjs/tools/search/schema.cjs +25 -23
  26. package/dist/cjs/tools/search/schema.cjs.map +1 -1
  27. package/dist/cjs/tools/search/tool.cjs +9 -33
  28. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  29. package/dist/cjs/utils/schema.cjs +27 -0
  30. package/dist/cjs/utils/schema.cjs.map +1 -0
  31. package/dist/cjs/utils/title.cjs +28 -14
  32. package/dist/cjs/utils/title.cjs.map +1 -1
  33. package/dist/esm/agents/AgentContext.mjs +2 -5
  34. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  35. package/dist/esm/graphs/Graph.mjs +20 -5
  36. package/dist/esm/graphs/Graph.mjs.map +1 -1
  37. package/dist/esm/graphs/MultiAgentGraph.mjs +26 -17
  38. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  39. package/dist/esm/llm/bedrock/index.mjs +97 -24
  40. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  41. package/dist/esm/llm/openai/index.mjs +1 -0
  42. package/dist/esm/llm/openai/index.mjs.map +1 -1
  43. package/dist/esm/main.mjs +1 -0
  44. package/dist/esm/main.mjs.map +1 -1
  45. package/dist/esm/messages/core.mjs +1 -1
  46. package/dist/esm/messages/core.mjs.map +1 -1
  47. package/dist/esm/stream.mjs +4 -2
  48. package/dist/esm/stream.mjs.map +1 -1
  49. package/dist/esm/tools/CodeExecutor.mjs +37 -27
  50. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  51. package/dist/esm/tools/ProgrammaticToolCalling.mjs +21 -17
  52. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  53. package/dist/esm/tools/ToolNode.mjs +10 -5
  54. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  55. package/dist/esm/tools/ToolSearch.mjs +37 -30
  56. package/dist/esm/tools/ToolSearch.mjs.map +1 -1
  57. package/dist/esm/tools/search/schema.mjs +25 -23
  58. package/dist/esm/tools/search/schema.mjs.map +1 -1
  59. package/dist/esm/tools/search/tool.mjs +10 -34
  60. package/dist/esm/tools/search/tool.mjs.map +1 -1
  61. package/dist/esm/utils/schema.mjs +24 -0
  62. package/dist/esm/utils/schema.mjs.map +1 -0
  63. package/dist/esm/utils/title.mjs +28 -14
  64. package/dist/esm/utils/title.mjs.map +1 -1
  65. package/dist/types/llm/bedrock/index.d.ts +86 -7
  66. package/dist/types/llm/bedrock/types.d.ts +27 -0
  67. package/dist/types/llm/bedrock/utils/index.d.ts +5 -0
  68. package/dist/types/llm/bedrock/utils/message_inputs.d.ts +31 -0
  69. package/dist/types/llm/bedrock/utils/message_outputs.d.ts +33 -0
  70. package/dist/types/tools/CodeExecutor.d.ts +1 -15
  71. package/dist/types/tools/ProgrammaticToolCalling.d.ts +1 -13
  72. package/dist/types/tools/ToolSearch.d.ts +1 -15
  73. package/dist/types/tools/search/schema.d.ts +25 -7
  74. package/dist/types/tools/search/tool.d.ts +1 -52
  75. package/dist/types/tools/search/types.d.ts +5 -23
  76. package/dist/types/types/tools.d.ts +2 -0
  77. package/dist/types/utils/index.d.ts +1 -0
  78. package/dist/types/utils/schema.d.ts +8 -0
  79. package/package.json +5 -2
  80. package/src/agents/AgentContext.ts +5 -11
  81. package/src/graphs/Graph.ts +23 -5
  82. package/src/graphs/MultiAgentGraph.ts +26 -17
  83. package/src/llm/bedrock/index.ts +180 -43
  84. package/src/llm/bedrock/llm.spec.ts +616 -0
  85. package/src/llm/bedrock/types.ts +51 -0
  86. package/src/llm/bedrock/utils/index.ts +18 -0
  87. package/src/llm/bedrock/utils/message_inputs.ts +563 -0
  88. package/src/llm/bedrock/utils/message_outputs.ts +310 -0
  89. package/src/messages/core.ts +1 -1
  90. package/src/scripts/code_exec_multi_session.ts +241 -0
  91. package/src/scripts/thinking-bedrock.ts +159 -0
  92. package/src/scripts/thinking.ts +39 -18
  93. package/src/scripts/tools.ts +7 -3
  94. package/src/specs/agent-handoffs.test.ts +1 -2
  95. package/src/specs/tool-error.test.ts +7 -2
  96. package/src/stream.ts +4 -2
  97. package/src/test/mockTools.ts +34 -14
  98. package/src/tools/CodeExecutor.ts +48 -31
  99. package/src/tools/ProgrammaticToolCalling.ts +24 -23
  100. package/src/tools/ToolNode.ts +9 -5
  101. package/src/tools/ToolSearch.ts +54 -43
  102. package/src/tools/search/schema.ts +30 -25
  103. package/src/tools/search/tool.ts +23 -16
  104. package/src/tools/search/types.ts +5 -29
  105. package/src/types/tools.ts +2 -0
  106. package/src/utils/index.ts +1 -0
  107. package/src/utils/schema.ts +35 -0
  108. package/src/utils/title.ts +31 -19
@@ -1,4 +1,3 @@
1
- import { z } from 'zod';
2
1
  export declare enum DATE_RANGE {
3
2
  PAST_HOUR = "h",
4
3
  PAST_24_HOURS = "d",
@@ -8,9 +7,28 @@ export declare enum DATE_RANGE {
8
7
  }
9
8
  export declare const DEFAULT_QUERY_DESCRIPTION: string;
10
9
  export declare const DEFAULT_COUNTRY_DESCRIPTION: string;
11
- export declare const querySchema: z.ZodString;
12
- export declare const dateSchema: z.ZodOptional<z.ZodNativeEnum<typeof DATE_RANGE>>;
13
- export declare const countrySchema: z.ZodOptional<z.ZodString>;
14
- export declare const imagesSchema: z.ZodOptional<z.ZodBoolean>;
15
- export declare const videosSchema: z.ZodOptional<z.ZodBoolean>;
16
- export declare const newsSchema: z.ZodOptional<z.ZodBoolean>;
10
+ export declare const querySchema: {
11
+ readonly type: "string";
12
+ readonly description: string;
13
+ };
14
+ export declare const dateSchema: {
15
+ readonly type: "string";
16
+ readonly enum: DATE_RANGE[];
17
+ readonly description: "Date range for search results.";
18
+ };
19
+ export declare const countrySchema: {
20
+ readonly type: "string";
21
+ readonly description: string;
22
+ };
23
+ export declare const imagesSchema: {
24
+ readonly type: "boolean";
25
+ readonly description: "Whether to also run an image search.";
26
+ };
27
+ export declare const videosSchema: {
28
+ readonly type: "boolean";
29
+ readonly description: "Whether to also run a video search.";
30
+ };
31
+ export declare const newsSchema: {
32
+ readonly type: "boolean";
33
+ readonly description: "Whether to also run a news search.";
34
+ };
@@ -1,54 +1,3 @@
1
- import { z } from 'zod';
2
1
  import { DynamicStructuredTool } from '@langchain/core/tools';
3
2
  import type * as t from './types';
4
- import { DATE_RANGE } from './schema';
5
- /**
6
- * Creates a search tool with a schema that dynamically includes the country field
7
- * only when the searchProvider is 'serper'.
8
- *
9
- * Supports multiple scraper providers:
10
- * - Firecrawl (default): Full-featured web scraping with multiple formats
11
- * - Serper: Lightweight scraping using Serper's scrape API
12
- *
13
- * @example
14
- * ```typescript
15
- * // Using Firecrawl scraper (default)
16
- * const searchTool = createSearchTool({
17
- * searchProvider: 'serper',
18
- * scraperProvider: 'firecrawl',
19
- * firecrawlApiKey: 'your-firecrawl-key'
20
- * });
21
- *
22
- * // Using Serper scraper
23
- * const searchTool = createSearchTool({
24
- * searchProvider: 'serper',
25
- * scraperProvider: 'serper',
26
- * serperApiKey: 'your-serper-key'
27
- * });
28
- * ```
29
- *
30
- * @param config - The search tool configuration
31
- * @returns A DynamicStructuredTool with a schema that depends on the searchProvider
32
- */
33
- export declare const createSearchTool: (config?: t.SearchToolConfig) => DynamicStructuredTool<z.ZodObject<{
34
- query: z.ZodString;
35
- date: z.ZodOptional<z.ZodNativeEnum<typeof DATE_RANGE>>;
36
- country?: z.ZodOptional<z.ZodString>;
37
- images: z.ZodOptional<z.ZodBoolean>;
38
- videos: z.ZodOptional<z.ZodBoolean>;
39
- news: z.ZodOptional<z.ZodBoolean>;
40
- }, "strip", z.ZodTypeAny, {
41
- query: string;
42
- videos?: boolean | undefined;
43
- images?: boolean | undefined;
44
- news?: boolean | undefined;
45
- date?: DATE_RANGE | undefined;
46
- country?: unknown;
47
- }, {
48
- query: string;
49
- videos?: boolean | undefined;
50
- images?: boolean | undefined;
51
- news?: boolean | undefined;
52
- date?: DATE_RANGE | undefined;
53
- country?: unknown;
54
- }>>;
3
+ export declare const createSearchTool: (config?: t.SearchToolConfig) => DynamicStructuredTool;
@@ -1,4 +1,3 @@
1
- import { z } from 'zod';
2
1
  import type { Logger as WinstonLogger } from 'winston';
3
2
  import type { RunnableConfig } from '@langchain/core/runnables';
4
3
  import type { BaseReranker } from './rerankers';
@@ -567,25 +566,8 @@ export type ProcessSourcesFields = {
567
566
  proMode: boolean;
568
567
  onGetHighlights: SearchToolConfig['onGetHighlights'];
569
568
  };
570
- export type SearchToolSchema = z.ZodObject<{
571
- query: z.ZodString;
572
- date: z.ZodOptional<z.ZodNativeEnum<typeof DATE_RANGE>>;
573
- country?: z.ZodOptional<z.ZodString>;
574
- images: z.ZodOptional<z.ZodBoolean>;
575
- videos: z.ZodOptional<z.ZodBoolean>;
576
- news: z.ZodOptional<z.ZodBoolean>;
577
- }, 'strip', z.ZodTypeAny, {
578
- query: string;
579
- date?: DATE_RANGE;
580
- country?: unknown;
581
- images?: boolean;
582
- videos?: boolean;
583
- news?: boolean;
584
- }, {
585
- query: string;
586
- date?: DATE_RANGE;
587
- country?: unknown;
588
- images?: boolean;
589
- videos?: boolean;
590
- news?: boolean;
591
- }>;
569
+ export interface SearchToolSchema {
570
+ type: 'object';
571
+ properties: Record<string, unknown>;
572
+ required: string[];
573
+ }
@@ -57,6 +57,8 @@ export type FileRef = {
57
57
  id: string;
58
58
  name: string;
59
59
  path?: string;
60
+ /** Session ID this file belongs to (for multi-session file tracking) */
61
+ session_id?: string;
60
62
  };
61
63
  export type FileRefs = FileRef[];
62
64
  export type ExecuteResult = {
@@ -4,3 +4,4 @@ export * from './misc';
4
4
  export * from './handlers';
5
5
  export * from './run';
6
6
  export * from './tokens';
7
+ export * from './schema';
@@ -0,0 +1,8 @@
1
+ import type { ZodTypeAny } from 'zod';
2
+ /** Checks if a schema is a Zod schema by looking for the _def property */
3
+ export declare function isZodSchema(schema: unknown): schema is ZodTypeAny;
4
+ /**
5
+ * Converts a schema to JSON schema format.
6
+ * Handles both Zod schemas (converts) and JSON schemas (passthrough).
7
+ */
8
+ export declare function toJsonSchema(schema: unknown, name?: string, description?: string): Record<string, unknown>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@librechat/agents",
3
- "version": "3.0.776",
3
+ "version": "3.1.01",
4
4
  "main": "./dist/cjs/main.cjs",
5
5
  "module": "./dist/esm/main.mjs",
6
6
  "types": "./dist/types/index.d.ts",
@@ -51,12 +51,14 @@
51
51
  "image": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/image.ts --provider 'google' --name 'Jo' --location 'New York, NY'",
52
52
  "code_exec_files": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_files.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
53
53
  "code_exec_session": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_session.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
54
+ "code_exec_multi_session": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_multi_session.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
54
55
  "code_exec_simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_simple.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
55
56
  "simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider 'bedrock' --name 'Jo' --location 'New York, NY'",
56
57
  "caching": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/caching.ts --name 'Jo' --location 'New York, NY'",
57
58
  "thinking": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/thinking.ts --name 'Jo' --location 'New York, NY'",
59
+ "thinking:bedrock": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/thinking-bedrock.ts --name 'Jo' --location 'New York, NY'",
58
60
  "memory": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/memory.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
59
- "tool": "node --trace-warnings -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/tools.ts --provider 'openrouter' --name 'Jo' --location 'New York, NY'",
61
+ "tool": "node --trace-warnings -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/tools.ts --provider 'bedrock' --name 'Jo' --location 'New York, NY'",
60
62
  "search": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/search.ts --provider 'bedrock' --name 'Jo' --location 'New York, NY'",
61
63
  "tool_search": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/tool_search.ts",
62
64
  "programmatic_exec": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/programmatic_exec.ts",
@@ -110,6 +112,7 @@
110
112
  }
111
113
  },
112
114
  "dependencies": {
115
+ "@aws-sdk/client-bedrock-runtime": "^3.970.0",
113
116
  "@langchain/anthropic": "^0.3.26",
114
117
  "@langchain/aws": "^0.1.15",
115
118
  "@langchain/core": "^0.3.80",
@@ -1,6 +1,5 @@
1
1
  /* eslint-disable no-console */
2
2
  // src/agents/AgentContext.ts
3
- import { zodToJsonSchema } from 'zod-to-json-schema';
4
3
  import { SystemMessage } from '@langchain/core/messages';
5
4
  import { RunnableLambda } from '@langchain/core/runnables';
6
5
  import type {
@@ -12,6 +11,7 @@ import type { RunnableConfig, Runnable } from '@langchain/core/runnables';
12
11
  import type * as t from '@/types';
13
12
  import type { createPruneMessages } from '@/messages';
14
13
  import { ContentTypes, Providers } from '@/common';
14
+ import { toJsonSchema } from '@/utils/schema';
15
15
 
16
16
  /**
17
17
  * Encapsulates agent-specific state that can vary between agents in a multi-agent system
@@ -479,15 +479,10 @@ export class AgentContext {
479
479
  genericTool.schema != null &&
480
480
  typeof genericTool.schema === 'object'
481
481
  ) {
482
- const schema = genericTool.schema as {
483
- describe: (desc: string) => unknown;
484
- };
485
- const describedSchema = schema.describe(
486
- (genericTool.description as string) || ''
487
- );
488
- const jsonSchema = zodToJsonSchema(
489
- describedSchema as Parameters<typeof zodToJsonSchema>[0],
490
- (genericTool.name as string) || ''
482
+ const jsonSchema = toJsonSchema(
483
+ genericTool.schema,
484
+ (genericTool.name as string | undefined) ?? '',
485
+ (genericTool.description as string | undefined) ?? ''
491
486
  );
492
487
  toolTokens += tokenCounter(
493
488
  new SystemMessage(JSON.stringify(jsonSchema))
@@ -496,7 +491,6 @@ export class AgentContext {
496
491
  }
497
492
  }
498
493
 
499
- // Add tool tokens to existing instruction tokens (which may already include system message tokens)
500
494
  this.instructionTokens += toolTokens;
501
495
  }
502
496
 
@@ -1042,8 +1042,8 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
1042
1042
 
1043
1043
  /**
1044
1044
  * Extract and store code execution session context from artifacts.
1045
- * Only update session_id when files are generated - this ensures we don't
1046
- * lose the original session that contains the files.
1045
+ * Each file is stamped with its source session_id to support multi-session file tracking.
1046
+ * When the same filename appears in a later execution, the newer version replaces the old.
1047
1047
  */
1048
1048
  const toolName = output.name;
1049
1049
  if (
@@ -1060,17 +1060,35 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
1060
1060
  artifact.session_id !== ''
1061
1061
  ) {
1062
1062
  /**
1063
- * Files were generated - update session with the new session_id.
1064
- * The new session_id is the one that contains these files.
1063
+ * Stamp each new file with its source session_id.
1064
+ * This enables files from different executions (parallel or sequential)
1065
+ * to be tracked and passed to subsequent calls.
1065
1066
  */
1067
+ const filesWithSession: t.FileRefs = newFiles.map((file) => ({
1068
+ ...file,
1069
+ session_id: artifact.session_id,
1070
+ }));
1071
+
1066
1072
  const existingSession = this.sessions.get(Constants.EXECUTE_CODE) as
1067
1073
  | t.CodeSessionContext
1068
1074
  | undefined;
1069
1075
  const existingFiles = existingSession?.files ?? [];
1070
1076
 
1077
+ /**
1078
+ * Merge files, preferring latest versions by name.
1079
+ * If a file with the same name exists, replace it with the new version.
1080
+ * This handles cases where files are edited/recreated in subsequent executions.
1081
+ */
1082
+ const newFileNames = new Set(filesWithSession.map((f) => f.name));
1083
+ const filteredExisting = existingFiles.filter(
1084
+ (f) => !newFileNames.has(f.name)
1085
+ );
1086
+
1071
1087
  this.sessions.set(Constants.EXECUTE_CODE, {
1088
+ /** Keep latest session_id for reference/fallback */
1072
1089
  session_id: artifact.session_id,
1073
- files: [...existingFiles, ...newFiles],
1090
+ /** Accumulated files with latest versions preferred */
1091
+ files: [...filteredExisting, ...filesWithSession],
1074
1092
  lastUpdated: Date.now(),
1075
1093
  });
1076
1094
  }
@@ -1,4 +1,3 @@
1
- import { z } from 'zod';
2
1
  import { tool } from '@langchain/core/tools';
3
2
  import { PromptTemplate } from '@langchain/core/prompts';
4
3
  import {
@@ -292,7 +291,8 @@ export class MultiAgentGraph extends StandardGraph {
292
291
 
293
292
  tools.push(
294
293
  tool(
295
- async (input: Record<string, unknown>, config) => {
294
+ async (rawInput, config) => {
295
+ const input = rawInput as Record<string, unknown>;
296
296
  const state = getCurrentTaskInput() as t.BaseGraphState;
297
297
  const toolCallId =
298
298
  (config as ToolRunnableConfig | undefined)?.toolCall?.id ??
@@ -343,13 +343,17 @@ export class MultiAgentGraph extends StandardGraph {
343
343
  {
344
344
  name: toolName,
345
345
  schema: hasHandoffInput
346
- ? z.object({
347
- [promptKey]: z
348
- .string()
349
- .optional()
350
- .describe(handoffInputDescription as string),
351
- })
352
- : z.object({}),
346
+ ? {
347
+ type: 'object',
348
+ properties: {
349
+ [promptKey]: {
350
+ type: 'string',
351
+ description: handoffInputDescription as string,
352
+ },
353
+ },
354
+ required: [],
355
+ }
356
+ : { type: 'object', properties: {}, required: [] },
353
357
  description: toolDescription,
354
358
  }
355
359
  )
@@ -371,7 +375,8 @@ export class MultiAgentGraph extends StandardGraph {
371
375
 
372
376
  tools.push(
373
377
  tool(
374
- async (input: Record<string, unknown>, config) => {
378
+ async (rawInput, config) => {
379
+ const input = rawInput as Record<string, unknown>;
375
380
  const toolCallId =
376
381
  (config as ToolRunnableConfig | undefined)?.toolCall?.id ??
377
382
  'unknown';
@@ -470,13 +475,17 @@ export class MultiAgentGraph extends StandardGraph {
470
475
  {
471
476
  name: toolName,
472
477
  schema: hasHandoffInput
473
- ? z.object({
474
- [promptKey]: z
475
- .string()
476
- .optional()
477
- .describe(handoffInputDescription as string),
478
- })
479
- : z.object({}),
478
+ ? {
479
+ type: 'object',
480
+ properties: {
481
+ [promptKey]: {
482
+ type: 'string',
483
+ description: handoffInputDescription as string,
484
+ },
485
+ },
486
+ required: [],
487
+ }
488
+ : { type: 'object', properties: {}, required: [] },
480
489
  description: toolDescription,
481
490
  }
482
491
  )
@@ -1,5 +1,9 @@
1
1
  /**
2
2
  * Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts
3
+ * and adds support for latest @langchain/aws features:
4
+ *
5
+ * - Application Inference Profiles (PR #9129)
6
+ * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+
3
7
  *
4
8
  * Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,
5
9
  * causing LangChain's merge logic to fail with "field[contentBlockIndex] already exists"
@@ -12,15 +16,74 @@
12
16
  */
13
17
 
14
18
  import { ChatBedrockConverse } from '@langchain/aws';
15
- import type { ChatBedrockConverseInput } from '@langchain/aws';
16
19
  import { AIMessageChunk } from '@langchain/core/messages';
17
- import type { BaseMessage } from '@langchain/core/messages';
18
- import { ChatGenerationChunk } from '@langchain/core/outputs';
20
+ import { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';
19
21
  import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
22
+ import type { ChatBedrockConverseInput } from '@langchain/aws';
23
+ import type { BaseMessage } from '@langchain/core/messages';
24
+
25
+ /**
26
+ * Service tier type for Bedrock invocations.
27
+ * Requires AWS SDK >= 3.966.0 to actually work.
28
+ * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
29
+ */
30
+ export type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';
31
+
32
+ /**
33
+ * Extended input interface with additional features:
34
+ * - applicationInferenceProfile: Use an inference profile ARN instead of model ID
35
+ * - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)
36
+ */
37
+ export interface CustomChatBedrockConverseInput
38
+ extends ChatBedrockConverseInput {
39
+ /**
40
+ * Application Inference Profile ARN to use for the model.
41
+ * For example, "arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx"
42
+ * When provided, this ARN will be used for the actual inference calls instead of the model ID.
43
+ * Must still provide `model` as normal modelId to benefit from all the metadata.
44
+ * @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html
45
+ */
46
+ applicationInferenceProfile?: string;
47
+
48
+ /**
49
+ * Service tier for model invocation.
50
+ * Specifies the processing tier type used for serving the request.
51
+ * Supported values are 'priority', 'default', 'flex', and 'reserved'.
52
+ *
53
+ * - 'priority': Prioritized processing for lower latency
54
+ * - 'default': Standard processing tier
55
+ * - 'flex': Flexible processing tier with lower cost
56
+ * - 'reserved': Reserved capacity for consistent performance
57
+ *
58
+ * If not provided, AWS uses the default tier.
59
+ * Note: Requires AWS SDK >= 3.966.0 to work.
60
+ * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
61
+ */
62
+ serviceTier?: ServiceTierType;
63
+ }
64
+
65
+ /**
66
+ * Extended call options with serviceTier override support.
67
+ */
68
+ export interface CustomChatBedrockConverseCallOptions {
69
+ serviceTier?: ServiceTierType;
70
+ }
20
71
 
21
72
  export class CustomChatBedrockConverse extends ChatBedrockConverse {
22
- constructor(fields?: ChatBedrockConverseInput) {
73
+ /**
74
+ * Application Inference Profile ARN to use instead of model ID.
75
+ */
76
+ applicationInferenceProfile?: string;
77
+
78
+ /**
79
+ * Service tier for model invocation.
80
+ */
81
+ serviceTier?: ServiceTierType;
82
+
83
+ constructor(fields?: CustomChatBedrockConverseInput) {
23
84
  super(fields);
85
+ this.applicationInferenceProfile = fields?.applicationInferenceProfile;
86
+ this.serviceTier = fields?.serviceTier;
24
87
  }
25
88
 
26
89
  static lc_name(): string {
@@ -28,52 +91,126 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
28
91
  }
29
92
 
30
93
  /**
31
- * Override _streamResponseChunks to strip contentBlockIndex from response_metadata
32
- * This prevents LangChain's merge conflicts when the same index is used for
33
- * different content types (text vs tool calls)
94
+ * Get the model ID to use for API calls.
95
+ * Returns applicationInferenceProfile if set, otherwise returns this.model.
96
+ */
97
+ protected getModelId(): string {
98
+ return this.applicationInferenceProfile ?? this.model;
99
+ }
100
+
101
+ /**
102
+ * Override invocationParams to add serviceTier support.
103
+ */
104
+ override invocationParams(
105
+ options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions
106
+ ): ReturnType<ChatBedrockConverse['invocationParams']> & {
107
+ serviceTier?: { type: ServiceTierType };
108
+ } {
109
+ const baseParams = super.invocationParams(options);
110
+
111
+ /** Service tier from options or fall back to class-level setting */
112
+ const serviceTierType = options?.serviceTier ?? this.serviceTier;
113
+
114
+ return {
115
+ ...baseParams,
116
+ serviceTier: serviceTierType ? { type: serviceTierType } : undefined,
117
+ };
118
+ }
119
+
120
+ /**
121
+ * Override _generateNonStreaming to use applicationInferenceProfile as modelId.
122
+ * Uses the same model-swapping pattern as streaming for consistency.
34
123
  */
35
- async *_streamResponseChunks(
124
+ override async _generateNonStreaming(
36
125
  messages: BaseMessage[],
37
- options: this['ParsedCallOptions'],
126
+ options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,
127
+ runManager?: CallbackManagerForLLMRun
128
+ ): Promise<ChatResult> {
129
+ // Temporarily swap model for applicationInferenceProfile support
130
+ const originalModel = this.model;
131
+ if (
132
+ this.applicationInferenceProfile != null &&
133
+ this.applicationInferenceProfile !== ''
134
+ ) {
135
+ this.model = this.applicationInferenceProfile;
136
+ }
137
+
138
+ try {
139
+ return await super._generateNonStreaming(messages, options, runManager);
140
+ } finally {
141
+ // Restore original model
142
+ this.model = originalModel;
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Override _streamResponseChunks to:
148
+ * 1. Use applicationInferenceProfile as modelId (by temporarily swapping this.model)
149
+ * 2. Strip contentBlockIndex from response_metadata to prevent merge conflicts
150
+ *
151
+ * Note: We delegate to super._streamResponseChunks() to preserve @langchain/aws's
152
+ * internal chunk handling which correctly preserves array content for reasoning blocks.
153
+ */
154
+ override async *_streamResponseChunks(
155
+ messages: BaseMessage[],
156
+ options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,
38
157
  runManager?: CallbackManagerForLLMRun
39
158
  ): AsyncGenerator<ChatGenerationChunk> {
40
- const baseStream = super._streamResponseChunks(
41
- messages,
42
- options,
43
- runManager
44
- );
45
-
46
- for await (const chunk of baseStream) {
47
- // Only process if we have response_metadata
48
- if (
49
- chunk.message instanceof AIMessageChunk &&
50
- (chunk.message as Partial<AIMessageChunk>).response_metadata &&
51
- typeof chunk.message.response_metadata === 'object'
52
- ) {
53
- // Check if contentBlockIndex exists anywhere in response_metadata (top level or nested)
54
- const hasContentBlockIndex = this.hasContentBlockIndex(
55
- chunk.message.response_metadata
56
- );
57
-
58
- if (hasContentBlockIndex) {
59
- const cleanedMetadata = this.removeContentBlockIndex(
60
- chunk.message.response_metadata
61
- ) as Record<string, unknown>;
62
-
63
- yield new ChatGenerationChunk({
64
- text: chunk.text,
65
- message: new AIMessageChunk({
66
- ...chunk.message,
67
- response_metadata: cleanedMetadata,
68
- }),
69
- generationInfo: chunk.generationInfo,
70
- });
71
- continue;
72
- }
159
+ // Temporarily swap model for applicationInferenceProfile support
160
+ const originalModel = this.model;
161
+ if (
162
+ this.applicationInferenceProfile != null &&
163
+ this.applicationInferenceProfile !== ''
164
+ ) {
165
+ this.model = this.applicationInferenceProfile;
166
+ }
167
+
168
+ try {
169
+ // Use parent's streaming logic which correctly handles reasoning content
170
+ const baseStream = super._streamResponseChunks(
171
+ messages,
172
+ options,
173
+ runManager
174
+ );
175
+
176
+ for await (const chunk of baseStream) {
177
+ // Clean contentBlockIndex from response_metadata to prevent merge conflicts
178
+ yield this.cleanChunk(chunk);
73
179
  }
180
+ } finally {
181
+ // Restore original model
182
+ this.model = originalModel;
183
+ }
184
+ }
74
185
 
75
- yield chunk;
186
+ /**
187
+ * Clean a chunk by removing contentBlockIndex from response_metadata.
188
+ */
189
+ private cleanChunk(chunk: ChatGenerationChunk): ChatGenerationChunk {
190
+ const message = chunk.message;
191
+ if (!(message instanceof AIMessageChunk)) {
192
+ return chunk;
76
193
  }
194
+
195
+ const metadata = message.response_metadata as Record<string, unknown>;
196
+ const hasContentBlockIndex = this.hasContentBlockIndex(metadata);
197
+ if (!hasContentBlockIndex) {
198
+ return chunk;
199
+ }
200
+
201
+ const cleanedMetadata = this.removeContentBlockIndex(metadata) as Record<
202
+ string,
203
+ unknown
204
+ >;
205
+
206
+ return new ChatGenerationChunk({
207
+ text: chunk.text,
208
+ message: new AIMessageChunk({
209
+ ...message,
210
+ response_metadata: cleanedMetadata,
211
+ }),
212
+ generationInfo: chunk.generationInfo,
213
+ });
77
214
  }
78
215
 
79
216
  /**