@librechat/agents 3.0.79 → 3.0.80

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.
@@ -3,9 +3,6 @@
3
3
  var aws = require('@langchain/aws');
4
4
  var messages = require('@langchain/core/messages');
5
5
  var outputs = require('@langchain/core/outputs');
6
- var clientBedrockRuntime = require('@aws-sdk/client-bedrock-runtime');
7
- var message_inputs = require('./utils/message_inputs.cjs');
8
- var message_outputs = require('./utils/message_outputs.cjs');
9
6
 
10
7
  /**
11
8
  * Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts
@@ -52,7 +49,7 @@ class CustomChatBedrockConverse extends aws.ChatBedrockConverse {
52
49
  */
53
50
  invocationParams(options) {
54
51
  const baseParams = super.invocationParams(options);
55
- // Get serviceTier from options or fall back to class-level setting
52
+ /** Service tier from options or fall back to class-level setting */
56
53
  const serviceTierType = options?.serviceTier ?? this.serviceTier;
57
54
  return {
58
55
  ...baseParams,
@@ -61,86 +58,50 @@ class CustomChatBedrockConverse extends aws.ChatBedrockConverse {
61
58
  }
62
59
  /**
63
60
  * Override _generateNonStreaming to use applicationInferenceProfile as modelId.
61
+ * Uses the same model-swapping pattern as streaming for consistency.
64
62
  */
65
- async _generateNonStreaming(messages, options, _runManager) {
66
- const { converseMessages, converseSystem } = message_inputs.convertToConverseMessages(messages);
67
- const params = this.invocationParams(options);
68
- const command = new clientBedrockRuntime.ConverseCommand({
69
- modelId: this.getModelId(),
70
- messages: converseMessages,
71
- system: converseSystem,
72
- requestMetadata: options.requestMetadata,
73
- ...params,
74
- });
75
- const response = await this.client.send(command, {
76
- abortSignal: options.signal,
77
- });
78
- const { output, ...responseMetadata } = response;
79
- if (!output?.message) {
80
- throw new Error('No message found in Bedrock response.');
63
+ async _generateNonStreaming(messages, options, runManager) {
64
+ // Temporarily swap model for applicationInferenceProfile support
65
+ const originalModel = this.model;
66
+ if (this.applicationInferenceProfile != null &&
67
+ this.applicationInferenceProfile !== '') {
68
+ this.model = this.applicationInferenceProfile;
69
+ }
70
+ try {
71
+ return await super._generateNonStreaming(messages, options, runManager);
72
+ }
73
+ finally {
74
+ // Restore original model
75
+ this.model = originalModel;
81
76
  }
82
- const message = message_outputs.convertConverseMessageToLangChainMessage(output.message, responseMetadata);
83
- return {
84
- generations: [
85
- {
86
- text: typeof message.content === 'string' ? message.content : '',
87
- message,
88
- },
89
- ],
90
- };
91
77
  }
92
78
  /**
93
79
  * Override _streamResponseChunks to:
94
- * 1. Use applicationInferenceProfile as modelId
95
- * 2. Include serviceTier in request
96
- * 3. Strip contentBlockIndex from response_metadata to prevent merge conflicts
80
+ * 1. Use applicationInferenceProfile as modelId (by temporarily swapping this.model)
81
+ * 2. Strip contentBlockIndex from response_metadata to prevent merge conflicts
82
+ *
83
+ * Note: We delegate to super._streamResponseChunks() to preserve @langchain/aws's
84
+ * internal chunk handling which correctly preserves array content for reasoning blocks.
97
85
  */
98
86
  async *_streamResponseChunks(messages, options, runManager) {
99
- const { converseMessages, converseSystem } = message_inputs.convertToConverseMessages(messages);
100
- const params = this.invocationParams(options);
101
- let { streamUsage } = this;
102
- if (options.streamUsage !== undefined) {
103
- streamUsage = options.streamUsage;
87
+ // Temporarily swap model for applicationInferenceProfile support
88
+ const originalModel = this.model;
89
+ if (this.applicationInferenceProfile != null &&
90
+ this.applicationInferenceProfile !== '') {
91
+ this.model = this.applicationInferenceProfile;
104
92
  }
105
- const command = new clientBedrockRuntime.ConverseStreamCommand({
106
- modelId: this.getModelId(),
107
- messages: converseMessages,
108
- system: converseSystem,
109
- requestMetadata: options.requestMetadata,
110
- ...params,
111
- });
112
- const response = await this.client.send(command, {
113
- abortSignal: options.signal,
114
- });
115
- if (response.stream) {
116
- for await (const event of response.stream) {
117
- if (event.contentBlockStart != null) {
118
- const chunk = message_outputs.handleConverseStreamContentBlockStart(event.contentBlockStart);
119
- if (chunk !== undefined) {
120
- const cleanedChunk = this.cleanChunk(chunk);
121
- yield cleanedChunk;
122
- await runManager?.handleLLMNewToken(cleanedChunk.text || '');
123
- }
124
- }
125
- else if (event.contentBlockDelta != null) {
126
- const chunk = message_outputs.handleConverseStreamContentBlockDelta(event.contentBlockDelta);
127
- if (chunk !== undefined) {
128
- const cleanedChunk = this.cleanChunk(chunk);
129
- yield cleanedChunk;
130
- await runManager?.handleLLMNewToken(cleanedChunk.text || '');
131
- }
132
- }
133
- else if (event.metadata != null) {
134
- const chunk = message_outputs.handleConverseStreamMetadata(event.metadata, {
135
- streamUsage,
136
- });
137
- if (chunk !== undefined) {
138
- const cleanedChunk = this.cleanChunk(chunk);
139
- yield cleanedChunk;
140
- }
141
- }
93
+ try {
94
+ // Use parent's streaming logic which correctly handles reasoning content
95
+ const baseStream = super._streamResponseChunks(messages, options, runManager);
96
+ for await (const chunk of baseStream) {
97
+ // Clean contentBlockIndex from response_metadata to prevent merge conflicts
98
+ yield this.cleanChunk(chunk);
142
99
  }
143
100
  }
101
+ finally {
102
+ // Restore original model
103
+ this.model = originalModel;
104
+ }
144
105
  }
145
106
  /**
146
107
  * Clean a chunk by removing contentBlockIndex from response_metadata.
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../../../src/llm/bedrock/index.ts"],"sourcesContent":["/**\n * Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts\n * and adds support for latest @langchain/aws features:\n *\n * - Application Inference Profiles (PR #9129)\n * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+\n *\n * Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,\n * causing LangChain's merge logic to fail with \"field[contentBlockIndex] already exists\"\n * errors. This wrapper simply strips contentBlockIndex from response_metadata to avoid\n * the conflict.\n *\n * The contentBlockIndex field is only used internally by Bedrock's streaming protocol\n * and isn't needed by application logic - the index field on tool_call_chunks serves\n * the purpose of tracking tool call ordering.\n */\n\nimport { ChatBedrockConverse } from '@langchain/aws';\nimport { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { ChatBedrockConverseInput } from '@langchain/aws';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport {\n ConverseCommand,\n ConverseStreamCommand,\n} from '@aws-sdk/client-bedrock-runtime';\nimport {\n convertToConverseMessages,\n convertConverseMessageToLangChainMessage,\n handleConverseStreamContentBlockStart,\n handleConverseStreamContentBlockDelta,\n handleConverseStreamMetadata,\n} from './utils';\n\n/**\n * Service tier type for Bedrock invocations.\n * Requires AWS SDK >= 3.966.0 to actually work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\nexport type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';\n\n/**\n * Extended input interface with additional features:\n * - applicationInferenceProfile: Use an inference profile ARN instead of model ID\n * - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)\n */\nexport interface CustomChatBedrockConverseInput\n extends ChatBedrockConverseInput {\n /**\n * Application Inference Profile ARN to use for the model.\n * For example, \"arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx\"\n * When provided, this ARN will be used for the actual inference calls instead of the model ID.\n * Must still provide `model` as normal modelId to benefit from all the metadata.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n * Specifies the processing tier type used for serving the request.\n * Supported values are 'priority', 'default', 'flex', and 'reserved'.\n *\n * - 'priority': Prioritized processing for lower latency\n * - 'default': Standard processing tier\n * - 'flex': Flexible processing tier with lower cost\n * - 'reserved': Reserved capacity for consistent performance\n *\n * If not provided, AWS uses the default tier.\n * Note: Requires AWS SDK >= 3.966.0 to work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\n serviceTier?: ServiceTierType;\n}\n\n/**\n * Extended call options with serviceTier override support.\n */\nexport interface CustomChatBedrockConverseCallOptions {\n serviceTier?: ServiceTierType;\n}\n\nexport class CustomChatBedrockConverse extends ChatBedrockConverse {\n /**\n * Application Inference Profile ARN to use instead of model ID.\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n */\n serviceTier?: ServiceTierType;\n\n constructor(fields?: CustomChatBedrockConverseInput) {\n super(fields);\n this.applicationInferenceProfile = fields?.applicationInferenceProfile;\n this.serviceTier = fields?.serviceTier;\n }\n\n static lc_name(): string {\n return 'LibreChatBedrockConverse';\n }\n\n /**\n * Get the model ID to use for API calls.\n * Returns applicationInferenceProfile if set, otherwise returns this.model.\n */\n protected getModelId(): string {\n return this.applicationInferenceProfile ?? this.model;\n }\n\n /**\n * Override invocationParams to add serviceTier support.\n */\n override invocationParams(\n options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions\n ): ReturnType<ChatBedrockConverse['invocationParams']> & {\n serviceTier?: { type: ServiceTierType };\n } {\n const baseParams = super.invocationParams(options);\n\n // Get serviceTier from options or fall back to class-level setting\n const serviceTierType = options?.serviceTier ?? this.serviceTier;\n\n return {\n ...baseParams,\n serviceTier: serviceTierType ? { type: serviceTierType } : undefined,\n };\n }\n\n /**\n * Override _generateNonStreaming to use applicationInferenceProfile as modelId.\n */\n override async _generateNonStreaming(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n _runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n const { converseMessages, converseSystem } =\n convertToConverseMessages(messages);\n const params = this.invocationParams(options);\n\n const command = new ConverseCommand({\n modelId: this.getModelId(),\n messages: converseMessages,\n system: converseSystem,\n requestMetadata: options.requestMetadata,\n ...params,\n });\n\n const response = await this.client.send(command, {\n abortSignal: options.signal,\n });\n\n const { output, ...responseMetadata } = response;\n if (!output?.message) {\n throw new Error('No message found in Bedrock response.');\n }\n\n const message = convertConverseMessageToLangChainMessage(\n output.message,\n responseMetadata\n );\n\n return {\n generations: [\n {\n text: typeof message.content === 'string' ? message.content : '',\n message,\n },\n ],\n };\n }\n\n /**\n * Override _streamResponseChunks to:\n * 1. Use applicationInferenceProfile as modelId\n * 2. Include serviceTier in request\n * 3. Strip contentBlockIndex from response_metadata to prevent merge conflicts\n */\n override async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const { converseMessages, converseSystem } =\n convertToConverseMessages(messages);\n const params = this.invocationParams(options);\n\n let { streamUsage } = this;\n if (options.streamUsage !== undefined) {\n streamUsage = options.streamUsage;\n }\n\n const command = new ConverseStreamCommand({\n modelId: this.getModelId(),\n messages: converseMessages,\n system: converseSystem,\n requestMetadata: options.requestMetadata,\n ...params,\n });\n\n const response = await this.client.send(command, {\n abortSignal: options.signal,\n });\n\n if (response.stream) {\n for await (const event of response.stream) {\n if (event.contentBlockStart != null) {\n const chunk = handleConverseStreamContentBlockStart(\n event.contentBlockStart\n ) as ChatGenerationChunk | undefined;\n if (chunk !== undefined) {\n const cleanedChunk = this.cleanChunk(chunk);\n yield cleanedChunk;\n await runManager?.handleLLMNewToken(cleanedChunk.text || '');\n }\n } else if (event.contentBlockDelta != null) {\n const chunk = handleConverseStreamContentBlockDelta(\n event.contentBlockDelta\n ) as ChatGenerationChunk | undefined;\n if (chunk !== undefined) {\n const cleanedChunk = this.cleanChunk(chunk);\n yield cleanedChunk;\n await runManager?.handleLLMNewToken(cleanedChunk.text || '');\n }\n } else if (event.metadata != null) {\n const chunk = handleConverseStreamMetadata(event.metadata, {\n streamUsage,\n }) as ChatGenerationChunk | undefined;\n if (chunk !== undefined) {\n const cleanedChunk = this.cleanChunk(chunk);\n yield cleanedChunk;\n }\n }\n }\n }\n }\n\n /**\n * Clean a chunk by removing contentBlockIndex from response_metadata.\n */\n private cleanChunk(chunk: ChatGenerationChunk): ChatGenerationChunk {\n const message = chunk.message;\n if (!(message instanceof AIMessageChunk)) {\n return chunk;\n }\n\n const metadata = message.response_metadata as Record<string, unknown>;\n const hasContentBlockIndex = this.hasContentBlockIndex(metadata);\n if (!hasContentBlockIndex) {\n return chunk;\n }\n\n const cleanedMetadata = this.removeContentBlockIndex(metadata) as Record<\n string,\n unknown\n >;\n\n return new ChatGenerationChunk({\n text: chunk.text,\n message: new AIMessageChunk({\n ...message,\n response_metadata: cleanedMetadata,\n }),\n generationInfo: chunk.generationInfo,\n });\n }\n\n /**\n * Check if contentBlockIndex exists at any level in the object\n */\n private hasContentBlockIndex(obj: unknown): boolean {\n if (obj === null || obj === undefined || typeof obj !== 'object') {\n return false;\n }\n\n if ('contentBlockIndex' in obj) {\n return true;\n }\n\n for (const value of Object.values(obj)) {\n if (typeof value === 'object' && value !== null) {\n if (this.hasContentBlockIndex(value)) {\n return true;\n }\n }\n }\n\n return false;\n }\n\n /**\n * Recursively remove contentBlockIndex from all levels of an object\n */\n private removeContentBlockIndex(obj: unknown): unknown {\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => this.removeContentBlockIndex(item));\n }\n\n if (typeof obj === 'object') {\n const cleaned: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n if (key !== 'contentBlockIndex') {\n cleaned[key] = this.removeContentBlockIndex(value);\n }\n }\n return cleaned;\n }\n\n return obj;\n }\n}\n\nexport type { ChatBedrockConverseInput };\n"],"names":["ChatBedrockConverse","convertToConverseMessages","ConverseCommand","convertConverseMessageToLangChainMessage","ConverseStreamCommand","handleConverseStreamContentBlockStart","handleConverseStreamContentBlockDelta","handleConverseStreamMetadata","AIMessageChunk","ChatGenerationChunk"],"mappings":";;;;;;;;;AAAA;;;;;;;;;;;;;;;AAeG;AAmEG,MAAO,yBAA0B,SAAQA,uBAAmB,CAAA;AAChE;;AAEG;AACH,IAAA,2BAA2B;AAE3B;;AAEG;AACH,IAAA,WAAW;AAEX,IAAA,WAAA,CAAY,MAAuC,EAAA;QACjD,KAAK,CA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OAAO,GAAG,KAAK,QAAQ,EAAE;YAC3B,MAAM,OAAO,GAA4B,EAAE;AAC3C,YAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;AAC9C,gBAAA,IAAI,GAAG,KAAK,mBAAmB,EAAE;oBAC/B,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,uBAAuB,CAAC,KAAK,CAAC;;;AAGtD,YAAA,OAAO,OAAO;;AAGhB,QAAA,OAAO,GAAG;;AAEb;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../../../../src/llm/bedrock/index.ts"],"sourcesContent":["/**\n * Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts\n * and adds support for latest @langchain/aws features:\n *\n * - Application Inference Profiles (PR #9129)\n * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+\n *\n * Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,\n * causing LangChain's merge logic to fail with \"field[contentBlockIndex] already exists\"\n * errors. This wrapper simply strips contentBlockIndex from response_metadata to avoid\n * the conflict.\n *\n * The contentBlockIndex field is only used internally by Bedrock's streaming protocol\n * and isn't needed by application logic - the index field on tool_call_chunks serves\n * the purpose of tracking tool call ordering.\n */\n\nimport { ChatBedrockConverse } from '@langchain/aws';\nimport { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { ChatBedrockConverseInput } from '@langchain/aws';\nimport type { BaseMessage } from '@langchain/core/messages';\n\n/**\n * Service tier type for Bedrock invocations.\n * Requires AWS SDK >= 3.966.0 to actually work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\nexport type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';\n\n/**\n * Extended input interface with additional features:\n * - applicationInferenceProfile: Use an inference profile ARN instead of model ID\n * - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)\n */\nexport interface CustomChatBedrockConverseInput\n extends ChatBedrockConverseInput {\n /**\n * Application Inference Profile ARN to use for the model.\n * For example, \"arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx\"\n * When provided, this ARN will be used for the actual inference calls instead of the model ID.\n * Must still provide `model` as normal modelId to benefit from all the metadata.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n * Specifies the processing tier type used for serving the request.\n * Supported values are 'priority', 'default', 'flex', and 'reserved'.\n *\n * - 'priority': Prioritized processing for lower latency\n * - 'default': Standard processing tier\n * - 'flex': Flexible processing tier with lower cost\n * - 'reserved': Reserved capacity for consistent performance\n *\n * If not provided, AWS uses the default tier.\n * Note: Requires AWS SDK >= 3.966.0 to work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\n serviceTier?: ServiceTierType;\n}\n\n/**\n * Extended call options with serviceTier override support.\n */\nexport interface CustomChatBedrockConverseCallOptions {\n serviceTier?: ServiceTierType;\n}\n\nexport class CustomChatBedrockConverse extends ChatBedrockConverse {\n /**\n * Application Inference Profile ARN to use instead of model ID.\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n */\n serviceTier?: ServiceTierType;\n\n constructor(fields?: CustomChatBedrockConverseInput) {\n super(fields);\n this.applicationInferenceProfile = fields?.applicationInferenceProfile;\n this.serviceTier = fields?.serviceTier;\n }\n\n static lc_name(): string {\n return 'LibreChatBedrockConverse';\n }\n\n /**\n * Get the model ID to use for API calls.\n * Returns applicationInferenceProfile if set, otherwise returns this.model.\n */\n protected getModelId(): string {\n return this.applicationInferenceProfile ?? this.model;\n }\n\n /**\n * Override invocationParams to add serviceTier support.\n */\n override invocationParams(\n options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions\n ): ReturnType<ChatBedrockConverse['invocationParams']> & {\n serviceTier?: { type: ServiceTierType };\n } {\n const baseParams = super.invocationParams(options);\n\n /** Service tier from options or fall back to class-level setting */\n const serviceTierType = options?.serviceTier ?? this.serviceTier;\n\n return {\n ...baseParams,\n serviceTier: serviceTierType ? { type: serviceTierType } : undefined,\n };\n }\n\n /**\n * Override _generateNonStreaming to use applicationInferenceProfile as modelId.\n * Uses the same model-swapping pattern as streaming for consistency.\n */\n override async _generateNonStreaming(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n // Temporarily swap model for applicationInferenceProfile support\n const originalModel = this.model;\n if (\n this.applicationInferenceProfile != null &&\n this.applicationInferenceProfile !== ''\n ) {\n this.model = this.applicationInferenceProfile;\n }\n\n try {\n return await super._generateNonStreaming(messages, options, runManager);\n } finally {\n // Restore original model\n this.model = originalModel;\n }\n }\n\n /**\n * Override _streamResponseChunks to:\n * 1. Use applicationInferenceProfile as modelId (by temporarily swapping this.model)\n * 2. Strip contentBlockIndex from response_metadata to prevent merge conflicts\n *\n * Note: We delegate to super._streamResponseChunks() to preserve @langchain/aws's\n * internal chunk handling which correctly preserves array content for reasoning blocks.\n */\n override async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n // Temporarily swap model for applicationInferenceProfile support\n const originalModel = this.model;\n if (\n this.applicationInferenceProfile != null &&\n this.applicationInferenceProfile !== ''\n ) {\n this.model = this.applicationInferenceProfile;\n }\n\n try {\n // Use parent's streaming logic which correctly handles reasoning content\n const baseStream = super._streamResponseChunks(\n messages,\n options,\n runManager\n );\n\n for await (const chunk of baseStream) {\n // Clean contentBlockIndex from response_metadata to prevent merge conflicts\n yield this.cleanChunk(chunk);\n }\n } finally {\n // Restore original model\n this.model = originalModel;\n }\n }\n\n /**\n * Clean a chunk by removing contentBlockIndex from response_metadata.\n */\n private cleanChunk(chunk: ChatGenerationChunk): ChatGenerationChunk {\n const message = chunk.message;\n if (!(message instanceof AIMessageChunk)) {\n return chunk;\n }\n\n const metadata = message.response_metadata as Record<string, unknown>;\n const hasContentBlockIndex = this.hasContentBlockIndex(metadata);\n if (!hasContentBlockIndex) {\n return chunk;\n }\n\n const cleanedMetadata = this.removeContentBlockIndex(metadata) as Record<\n string,\n unknown\n >;\n\n return new ChatGenerationChunk({\n text: chunk.text,\n message: new AIMessageChunk({\n ...message,\n response_metadata: cleanedMetadata,\n }),\n generationInfo: chunk.generationInfo,\n });\n }\n\n /**\n * Check if contentBlockIndex exists at any level in the object\n */\n private hasContentBlockIndex(obj: unknown): boolean {\n if (obj === null || obj === undefined || typeof obj !== 'object') {\n return false;\n }\n\n if ('contentBlockIndex' in obj) {\n return true;\n }\n\n for (const value of Object.values(obj)) {\n if (typeof value === 'object' && value !== null) {\n if (this.hasContentBlockIndex(value)) {\n return true;\n }\n }\n }\n\n return false;\n }\n\n /**\n * Recursively remove contentBlockIndex from all levels of an object\n */\n private removeContentBlockIndex(obj: unknown): unknown {\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => this.removeContentBlockIndex(item));\n }\n\n if (typeof obj === 'object') {\n const cleaned: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n if (key !== 'contentBlockIndex') {\n cleaned[key] = this.removeContentBlockIndex(value);\n }\n }\n return cleaned;\n }\n\n return obj;\n }\n}\n\nexport type { ChatBedrockConverseInput };\n"],"names":["ChatBedrockConverse","AIMessageChunk","ChatGenerationChunk"],"mappings":";;;;;;AAAA;;;;;;;;;;;;;;;AAeG;AAwDG,MAAO,yBAA0B,SAAQA,uBAAmB,CAAA;AAChE;;AAEG;AACH,IAAA,2BAA2B;AAE3B;;AAEG;AACH,IAAA,WAAW;AAEX,IAAA,WAAA,CAAY,MAA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A,IAAI,mBAAmB,IAAI,GAAG,EAAE;AAC9B,YAAA,OAAO,IAAI;;QAGb,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE;YACtC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE;AAC/C,gBAAA,IAAI,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,EAAE;AACpC,oBAAA,OAAO,IAAI;;;;AAKjB,QAAA,OAAO,KAAK;;AAGd;;AAEG;AACK,IAAA,uBAAuB,CAAC,GAAY,EAAA;QAC1C,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,EAAE;AACrC,YAAA,OAAO,GAAG;;AAGZ,QAAA,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;AACtB,YAAA,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,CAAC;;AAG9D,QAAA,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC3B,MAAM,OAAO,GAA4B,EAAE;AAC3C,YAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;AAC9C,gBAAA,IAAI,GAAG,KAAK,mBAAmB,EAAE;oBAC/B,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,uBAAuB,CAAC,KAAK,CAAC;;;AAGtD,YAAA,OAAO,OAAO;;AAGhB,QAAA,OAAO,GAAG;;AAEb;;;;"}
@@ -1,9 +1,6 @@
1
1
  import { ChatBedrockConverse } from '@langchain/aws';
2
2
  import { AIMessageChunk } from '@langchain/core/messages';
3
3
  import { ChatGenerationChunk } from '@langchain/core/outputs';
4
- import { ConverseCommand, ConverseStreamCommand } from '@aws-sdk/client-bedrock-runtime';
5
- import { convertToConverseMessages } from './utils/message_inputs.mjs';
6
- import { convertConverseMessageToLangChainMessage, handleConverseStreamContentBlockStart, handleConverseStreamContentBlockDelta, handleConverseStreamMetadata } from './utils/message_outputs.mjs';
7
4
 
8
5
  /**
9
6
  * Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts
@@ -50,7 +47,7 @@ class CustomChatBedrockConverse extends ChatBedrockConverse {
50
47
  */
51
48
  invocationParams(options) {
52
49
  const baseParams = super.invocationParams(options);
53
- // Get serviceTier from options or fall back to class-level setting
50
+ /** Service tier from options or fall back to class-level setting */
54
51
  const serviceTierType = options?.serviceTier ?? this.serviceTier;
55
52
  return {
56
53
  ...baseParams,
@@ -59,86 +56,50 @@ class CustomChatBedrockConverse extends ChatBedrockConverse {
59
56
  }
60
57
  /**
61
58
  * Override _generateNonStreaming to use applicationInferenceProfile as modelId.
59
+ * Uses the same model-swapping pattern as streaming for consistency.
62
60
  */
63
- async _generateNonStreaming(messages, options, _runManager) {
64
- const { converseMessages, converseSystem } = convertToConverseMessages(messages);
65
- const params = this.invocationParams(options);
66
- const command = new ConverseCommand({
67
- modelId: this.getModelId(),
68
- messages: converseMessages,
69
- system: converseSystem,
70
- requestMetadata: options.requestMetadata,
71
- ...params,
72
- });
73
- const response = await this.client.send(command, {
74
- abortSignal: options.signal,
75
- });
76
- const { output, ...responseMetadata } = response;
77
- if (!output?.message) {
78
- throw new Error('No message found in Bedrock response.');
61
+ async _generateNonStreaming(messages, options, runManager) {
62
+ // Temporarily swap model for applicationInferenceProfile support
63
+ const originalModel = this.model;
64
+ if (this.applicationInferenceProfile != null &&
65
+ this.applicationInferenceProfile !== '') {
66
+ this.model = this.applicationInferenceProfile;
67
+ }
68
+ try {
69
+ return await super._generateNonStreaming(messages, options, runManager);
70
+ }
71
+ finally {
72
+ // Restore original model
73
+ this.model = originalModel;
79
74
  }
80
- const message = convertConverseMessageToLangChainMessage(output.message, responseMetadata);
81
- return {
82
- generations: [
83
- {
84
- text: typeof message.content === 'string' ? message.content : '',
85
- message,
86
- },
87
- ],
88
- };
89
75
  }
90
76
  /**
91
77
  * Override _streamResponseChunks to:
92
- * 1. Use applicationInferenceProfile as modelId
93
- * 2. Include serviceTier in request
94
- * 3. Strip contentBlockIndex from response_metadata to prevent merge conflicts
78
+ * 1. Use applicationInferenceProfile as modelId (by temporarily swapping this.model)
79
+ * 2. Strip contentBlockIndex from response_metadata to prevent merge conflicts
80
+ *
81
+ * Note: We delegate to super._streamResponseChunks() to preserve @langchain/aws's
82
+ * internal chunk handling which correctly preserves array content for reasoning blocks.
95
83
  */
96
84
  async *_streamResponseChunks(messages, options, runManager) {
97
- const { converseMessages, converseSystem } = convertToConverseMessages(messages);
98
- const params = this.invocationParams(options);
99
- let { streamUsage } = this;
100
- if (options.streamUsage !== undefined) {
101
- streamUsage = options.streamUsage;
85
+ // Temporarily swap model for applicationInferenceProfile support
86
+ const originalModel = this.model;
87
+ if (this.applicationInferenceProfile != null &&
88
+ this.applicationInferenceProfile !== '') {
89
+ this.model = this.applicationInferenceProfile;
102
90
  }
103
- const command = new ConverseStreamCommand({
104
- modelId: this.getModelId(),
105
- messages: converseMessages,
106
- system: converseSystem,
107
- requestMetadata: options.requestMetadata,
108
- ...params,
109
- });
110
- const response = await this.client.send(command, {
111
- abortSignal: options.signal,
112
- });
113
- if (response.stream) {
114
- for await (const event of response.stream) {
115
- if (event.contentBlockStart != null) {
116
- const chunk = handleConverseStreamContentBlockStart(event.contentBlockStart);
117
- if (chunk !== undefined) {
118
- const cleanedChunk = this.cleanChunk(chunk);
119
- yield cleanedChunk;
120
- await runManager?.handleLLMNewToken(cleanedChunk.text || '');
121
- }
122
- }
123
- else if (event.contentBlockDelta != null) {
124
- const chunk = handleConverseStreamContentBlockDelta(event.contentBlockDelta);
125
- if (chunk !== undefined) {
126
- const cleanedChunk = this.cleanChunk(chunk);
127
- yield cleanedChunk;
128
- await runManager?.handleLLMNewToken(cleanedChunk.text || '');
129
- }
130
- }
131
- else if (event.metadata != null) {
132
- const chunk = handleConverseStreamMetadata(event.metadata, {
133
- streamUsage,
134
- });
135
- if (chunk !== undefined) {
136
- const cleanedChunk = this.cleanChunk(chunk);
137
- yield cleanedChunk;
138
- }
139
- }
91
+ try {
92
+ // Use parent's streaming logic which correctly handles reasoning content
93
+ const baseStream = super._streamResponseChunks(messages, options, runManager);
94
+ for await (const chunk of baseStream) {
95
+ // Clean contentBlockIndex from response_metadata to prevent merge conflicts
96
+ yield this.cleanChunk(chunk);
140
97
  }
141
98
  }
99
+ finally {
100
+ // Restore original model
101
+ this.model = originalModel;
102
+ }
142
103
  }
143
104
  /**
144
105
  * Clean a chunk by removing contentBlockIndex from response_metadata.
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../../../../src/llm/bedrock/index.ts"],"sourcesContent":["/**\n * Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts\n * and adds support for latest @langchain/aws features:\n *\n * - Application Inference Profiles (PR #9129)\n * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+\n *\n * Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,\n * causing LangChain's merge logic to fail with \"field[contentBlockIndex] already exists\"\n * errors. This wrapper simply strips contentBlockIndex from response_metadata to avoid\n * the conflict.\n *\n * The contentBlockIndex field is only used internally by Bedrock's streaming protocol\n * and isn't needed by application logic - the index field on tool_call_chunks serves\n * the purpose of tracking tool call ordering.\n */\n\nimport { ChatBedrockConverse } from '@langchain/aws';\nimport { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { ChatBedrockConverseInput } from '@langchain/aws';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport {\n ConverseCommand,\n ConverseStreamCommand,\n} from '@aws-sdk/client-bedrock-runtime';\nimport {\n convertToConverseMessages,\n convertConverseMessageToLangChainMessage,\n handleConverseStreamContentBlockStart,\n handleConverseStreamContentBlockDelta,\n handleConverseStreamMetadata,\n} from './utils';\n\n/**\n * Service tier type for Bedrock invocations.\n * Requires AWS SDK >= 3.966.0 to actually work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\nexport type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';\n\n/**\n * Extended input interface with additional features:\n * - applicationInferenceProfile: Use an inference profile ARN instead of model ID\n * - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)\n */\nexport interface CustomChatBedrockConverseInput\n extends ChatBedrockConverseInput {\n /**\n * Application Inference Profile ARN to use for the model.\n * For example, \"arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx\"\n * When provided, this ARN will be used for the actual inference calls instead of the model ID.\n * Must still provide `model` as normal modelId to benefit from all the metadata.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n * Specifies the processing tier type used for serving the request.\n * Supported values are 'priority', 'default', 'flex', and 'reserved'.\n *\n * - 'priority': Prioritized processing for lower latency\n * - 'default': Standard processing tier\n * - 'flex': Flexible processing tier with lower cost\n * - 'reserved': Reserved capacity for consistent performance\n *\n * If not provided, AWS uses the default tier.\n * Note: Requires AWS SDK >= 3.966.0 to work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\n serviceTier?: ServiceTierType;\n}\n\n/**\n * Extended call options with serviceTier override support.\n */\nexport interface CustomChatBedrockConverseCallOptions {\n serviceTier?: ServiceTierType;\n}\n\nexport class CustomChatBedrockConverse extends ChatBedrockConverse {\n /**\n * Application Inference Profile ARN to use instead of model ID.\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n */\n serviceTier?: ServiceTierType;\n\n constructor(fields?: CustomChatBedrockConverseInput) {\n super(fields);\n this.applicationInferenceProfile = fields?.applicationInferenceProfile;\n this.serviceTier = fields?.serviceTier;\n }\n\n static lc_name(): string {\n return 'LibreChatBedrockConverse';\n }\n\n /**\n * Get the model ID to use for API calls.\n * Returns applicationInferenceProfile if set, otherwise returns this.model.\n */\n protected getModelId(): string {\n return this.applicationInferenceProfile ?? this.model;\n }\n\n /**\n * Override invocationParams to add serviceTier support.\n */\n override invocationParams(\n options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions\n ): ReturnType<ChatBedrockConverse['invocationParams']> & {\n serviceTier?: { type: ServiceTierType };\n } {\n const baseParams = super.invocationParams(options);\n\n // Get serviceTier from options or fall back to class-level setting\n const serviceTierType = options?.serviceTier ?? this.serviceTier;\n\n return {\n ...baseParams,\n serviceTier: serviceTierType ? { type: serviceTierType } : undefined,\n };\n }\n\n /**\n * Override _generateNonStreaming to use applicationInferenceProfile as modelId.\n */\n override async _generateNonStreaming(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n _runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n const { converseMessages, converseSystem } =\n convertToConverseMessages(messages);\n const params = this.invocationParams(options);\n\n const command = new ConverseCommand({\n modelId: this.getModelId(),\n messages: converseMessages,\n system: converseSystem,\n requestMetadata: options.requestMetadata,\n ...params,\n });\n\n const response = await this.client.send(command, {\n abortSignal: options.signal,\n });\n\n const { output, ...responseMetadata } = response;\n if (!output?.message) {\n throw new Error('No message found in Bedrock response.');\n }\n\n const message = convertConverseMessageToLangChainMessage(\n output.message,\n responseMetadata\n );\n\n return {\n generations: [\n {\n text: typeof message.content === 'string' ? message.content : '',\n message,\n },\n ],\n };\n }\n\n /**\n * Override _streamResponseChunks to:\n * 1. Use applicationInferenceProfile as modelId\n * 2. Include serviceTier in request\n * 3. Strip contentBlockIndex from response_metadata to prevent merge conflicts\n */\n override async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const { converseMessages, converseSystem } =\n convertToConverseMessages(messages);\n const params = this.invocationParams(options);\n\n let { streamUsage } = this;\n if (options.streamUsage !== undefined) {\n streamUsage = options.streamUsage;\n }\n\n const command = new ConverseStreamCommand({\n modelId: this.getModelId(),\n messages: converseMessages,\n system: converseSystem,\n requestMetadata: options.requestMetadata,\n ...params,\n });\n\n const response = await this.client.send(command, {\n abortSignal: options.signal,\n });\n\n if (response.stream) {\n for await (const event of response.stream) {\n if (event.contentBlockStart != null) {\n const chunk = handleConverseStreamContentBlockStart(\n event.contentBlockStart\n ) as ChatGenerationChunk | undefined;\n if (chunk !== undefined) {\n const cleanedChunk = this.cleanChunk(chunk);\n yield cleanedChunk;\n await runManager?.handleLLMNewToken(cleanedChunk.text || '');\n }\n } else if (event.contentBlockDelta != null) {\n const chunk = handleConverseStreamContentBlockDelta(\n event.contentBlockDelta\n ) as ChatGenerationChunk | undefined;\n if (chunk !== undefined) {\n const cleanedChunk = this.cleanChunk(chunk);\n yield cleanedChunk;\n await runManager?.handleLLMNewToken(cleanedChunk.text || '');\n }\n } else if (event.metadata != null) {\n const chunk = handleConverseStreamMetadata(event.metadata, {\n streamUsage,\n }) as ChatGenerationChunk | undefined;\n if (chunk !== undefined) {\n const cleanedChunk = this.cleanChunk(chunk);\n yield cleanedChunk;\n }\n }\n }\n }\n }\n\n /**\n * Clean a chunk by removing contentBlockIndex from response_metadata.\n */\n private cleanChunk(chunk: ChatGenerationChunk): ChatGenerationChunk {\n const message = chunk.message;\n if (!(message instanceof AIMessageChunk)) {\n return chunk;\n }\n\n const metadata = message.response_metadata as Record<string, unknown>;\n const hasContentBlockIndex = this.hasContentBlockIndex(metadata);\n if (!hasContentBlockIndex) {\n return chunk;\n }\n\n const cleanedMetadata = this.removeContentBlockIndex(metadata) as Record<\n string,\n unknown\n >;\n\n return new ChatGenerationChunk({\n text: chunk.text,\n message: new AIMessageChunk({\n ...message,\n response_metadata: cleanedMetadata,\n }),\n generationInfo: chunk.generationInfo,\n });\n }\n\n /**\n * Check if contentBlockIndex exists at any level in the object\n */\n private hasContentBlockIndex(obj: unknown): boolean {\n if (obj === null || obj === undefined || typeof obj !== 'object') {\n return false;\n }\n\n if ('contentBlockIndex' in obj) {\n return true;\n }\n\n for (const value of Object.values(obj)) {\n if (typeof value === 'object' && value !== null) {\n if (this.hasContentBlockIndex(value)) {\n return true;\n }\n }\n }\n\n return false;\n }\n\n /**\n * Recursively remove contentBlockIndex from all levels of an object\n */\n private removeContentBlockIndex(obj: unknown): unknown {\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => this.removeContentBlockIndex(item));\n }\n\n if (typeof obj === 'object') {\n const cleaned: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n if (key !== 'contentBlockIndex') {\n cleaned[key] = this.removeContentBlockIndex(value);\n }\n }\n return cleaned;\n }\n\n return obj;\n }\n}\n\nexport type { ChatBedrockConverseInput };\n"],"names":[],"mappings":";;;;;;;AAAA;;;;;;;;;;;;;;;AAeG;AAmEG,MAAO,yBAA0B,SAAQ,mBAAmB,CAAA;AAChE;;AAEG;AACH,IAAA,2BAA2B;AAE3B;;AAEG;AACH,IAAA,WAAW;AAEX,IAAA,WAAA,CAAY,MAAuC,EAAA;QACjD,KAAK,CAAC,MAAM,CAAC;AACb,QAAA,IAAI,CAAC,2BAA2B,GAAG,MAAM,EAAE,2BAA2B;AACtE,QAAA,IAAI,CAAC,WAAW,GAAG,MAAM,EAAE,WAAW;;AAGxC,IAAA,OAAO,OAAO,GAAA;AACZ,QAAA,OAAO,0BAA0B;;AAGnC;;;AAGG;IACO,UAAU,GAAA;AAClB,QAAA,OAAO,IAAI,CAAC,2BAA2B,IAAI,IAAI,CAAC,KAAK;;AAGvD;;AAEG;AACM,IAAA,gBAAgB,CACvB,OAA0E,EAAA;QAI1E,MAAM,UAAU,GAAG,KAAK,CAAC,gBAAgB,CAAC,OAAO,CAAC;;QAGlD,MAAM,eAAe,GAAG,OAAO,EAAE,WAAW,IAAI,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flicts\n * and adds support for latest @langchain/aws features:\n *\n * - Application Inference Profiles (PR #9129)\n * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+\n *\n * Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,\n * causing LangChain's merge logic to fail with \"field[contentBlockIndex] already exists\"\n * errors. This wrapper simply strips contentBlockIndex from response_metadata to avoid\n * the conflict.\n *\n * The contentBlockIndex field is only used internally by Bedrock's streaming protocol\n * and isn't needed by application logic - the index field on tool_call_chunks serves\n * the purpose of tracking tool call ordering.\n */\n\nimport { ChatBedrockConverse } from '@langchain/aws';\nimport { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { ChatBedrockConverseInput } from '@langchain/aws';\nimport type { BaseMessage } from '@langchain/core/messages';\n\n/**\n * Service tier type for Bedrock invocations.\n * Requires AWS SDK >= 3.966.0 to actually work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\nexport type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';\n\n/**\n * Extended input interface with additional features:\n * - applicationInferenceProfile: Use an inference profile ARN instead of model ID\n * - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)\n */\nexport interface CustomChatBedrockConverseInput\n extends ChatBedrockConverseInput {\n /**\n * Application Inference Profile ARN to use for the model.\n * For example, \"arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx\"\n * When provided, this ARN will be used for the actual inference calls instead of the model ID.\n * Must still provide `model` as normal modelId to benefit from all the metadata.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n * Specifies the processing tier type used for serving the request.\n * Supported values are 'priority', 'default', 'flex', and 'reserved'.\n *\n * - 'priority': Prioritized processing for lower latency\n * - 'default': Standard processing tier\n * - 'flex': Flexible processing tier with lower cost\n * - 'reserved': Reserved capacity for consistent performance\n *\n * If not provided, AWS uses the default tier.\n * Note: Requires AWS SDK >= 3.966.0 to work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\n serviceTier?: ServiceTierType;\n}\n\n/**\n * Extended call options with serviceTier override support.\n */\nexport interface CustomChatBedrockConverseCallOptions {\n serviceTier?: ServiceTierType;\n}\n\nexport class CustomChatBedrockConverse extends ChatBedrockConverse {\n /**\n * Application Inference Profile ARN to use instead of model ID.\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n */\n serviceTier?: ServiceTierType;\n\n constructor(fields?: CustomChatBedrockConverseInput) {\n super(fields);\n this.applicationInferenceProfile = fields?.applicationInferenceProfile;\n this.serviceTier = fields?.serviceTier;\n }\n\n static lc_name(): string {\n return 'LibreChatBedrockConverse';\n }\n\n /**\n * Get the model ID to use for API calls.\n * Returns applicationInferenceProfile if set, otherwise returns this.model.\n */\n protected getModelId(): string {\n return this.applicationInferenceProfile ?? this.model;\n }\n\n /**\n * Override invocationParams to add serviceTier support.\n */\n override invocationParams(\n options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions\n ): ReturnType<ChatBedrockConverse['invocationParams']> & {\n serviceTier?: { type: ServiceTierType };\n } {\n const baseParams = super.invocationParams(options);\n\n /** Service tier from options or fall back to class-level setting */\n const serviceTierType = options?.serviceTier ?? this.serviceTier;\n\n return {\n ...baseParams,\n serviceTier: serviceTierType ? { type: serviceTierType } : undefined,\n };\n }\n\n /**\n * Override _generateNonStreaming to use applicationInferenceProfile as modelId.\n * Uses the same model-swapping pattern as streaming for consistency.\n */\n override async _generateNonStreaming(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n // Temporarily swap model for applicationInferenceProfile support\n const originalModel = this.model;\n if (\n this.applicationInferenceProfile != null &&\n this.applicationInferenceProfile !== ''\n ) {\n this.model = this.applicationInferenceProfile;\n }\n\n try {\n return await super._generateNonStreaming(messages, options, runManager);\n } finally {\n // Restore original model\n this.model = originalModel;\n }\n }\n\n /**\n * Override _streamResponseChunks to:\n * 1. Use applicationInferenceProfile as modelId (by temporarily swapping this.model)\n * 2. Strip contentBlockIndex from response_metadata to prevent merge conflicts\n *\n * Note: We delegate to super._streamResponseChunks() to preserve @langchain/aws's\n * internal chunk handling which correctly preserves array content for reasoning blocks.\n */\n override async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n // Temporarily swap model for applicationInferenceProfile support\n const originalModel = this.model;\n if (\n this.applicationInferenceProfile != null &&\n this.applicationInferenceProfile !== ''\n ) {\n this.model = this.applicationInferenceProfile;\n }\n\n try {\n // Use parent's streaming logic which correctly handles reasoning content\n const baseStream = super._streamResponseChunks(\n messages,\n options,\n runManager\n );\n\n for await (const chunk of baseStream) {\n // Clean contentBlockIndex from response_metadata to prevent merge conflicts\n yield this.cleanChunk(chunk);\n }\n } finally {\n // Restore original model\n this.model = originalModel;\n }\n }\n\n /**\n * Clean a chunk by removing contentBlockIndex from response_metadata.\n */\n private cleanChunk(chunk: ChatGenerationChunk): ChatGenerationChunk {\n const message = chunk.message;\n if (!(message instanceof AIMessageChunk)) {\n return chunk;\n }\n\n const metadata = message.response_metadata as Record<string, unknown>;\n const hasContentBlockIndex = this.hasContentBlockIndex(metadata);\n if (!hasContentBlockIndex) {\n return chunk;\n }\n\n const cleanedMetadata = this.removeContentBlockIndex(metadata) as Record<\n string,\n unknown\n >;\n\n return new ChatGenerationChunk({\n text: chunk.text,\n message: new AIMessageChunk({\n ...message,\n response_metadata: cleanedMetadata,\n }),\n generationInfo: chunk.generationInfo,\n });\n }\n\n /**\n * Check if contentBlockIndex exists at any level in the object\n */\n private hasContentBlockIndex(obj: unknown): boolean {\n if (obj === null || obj === undefined || typeof obj !== 'object') {\n return false;\n }\n\n if ('contentBlockIndex' in obj) {\n return true;\n }\n\n for (const value of Object.values(obj)) {\n if (typeof value === 'object' && value !== null) {\n if (this.hasContentBlockIndex(value)) {\n return true;\n }\n }\n }\n\n return false;\n }\n\n /**\n * Recursively remove contentBlockIndex from all levels of an object\n */\n private removeContentBlockIndex(obj: unknown): unknown {\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => this.removeContentBlockIndex(item));\n }\n\n if (typeof obj === 'object') {\n const cleaned: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n if (key !== 'contentBlockIndex') {\n cleaned[key] = this.removeContentBlockIndex(value);\n }\n }\n return cleaned;\n }\n\n return obj;\n }\n}\n\nexport type { ChatBedrockConverseInput };\n"],"names":[],"mappings":";;;;AAAA;;;;;;;;;;;;;;;AAeG;AAwDG,MAAO,yBAA0B,SAAQ,mBAAmB,CAAA;AAChE;;AAEG;AACH,IAAA,2BAA2B;AAE3B;;AAEG;AACH,IAAA,WAAW;AAEX,IAAA,WAAA,CAAY,MAAuC,EAAA;QACjD,KAAK,CAAC,MAAM,CAAC;AACb,QAAA,IAAI,CAAC,2BAA2B,GAAG,MAAM,EAAE,2BAA2B;AACtE,QAAA,IAAI,CAAC,WAAW,GAAG,MAAM,EAAE,WAAW;;AAGxC,IAAA,OAAO,OAAO,GAAA;AACZ,QAAA,OAAO,0BAA0B;;AAGnC;;;AAGG;IACO,UAAU,GAAA;AAClB,QAAA,OAAO,IAAI,CAAC,2BAA2B,IAAI,IAAI,CAAC,KAAK;;AAGvD;;AAEG;AACM,IAAA,gBAAgB,CACvB,OAA0E,EAAA;QAI1E,MAAM,UAAU,GAAG,KAAK,CAAC,gBAAgB,CAAC,OAAO,CAAC;;QAGlD,MAAM,eAAe,GAAG,OAAO,EAAE,WAAW,IAAI,IAAI,CAAC,WAAW;QAEhE,OAAO;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C,EAAE;AACtB,YAAA,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,CAAC;;AAG9D,QAAA,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC3B,MAAM,OAAO,GAA4B,EAAE;AAC3C,YAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;AAC9C,gBAAA,IAAI,GAAG,KAAK,mBAAmB,EAAE;oBAC/B,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,uBAAuB,CAAC,KAAK,CAAC;;;AAGtD,YAAA,OAAO,OAAO;;AAGhB,QAAA,OAAO,GAAG;;AAEb;;;;"}
@@ -87,13 +87,16 @@ export declare class CustomChatBedrockConverse extends ChatBedrockConverse {
87
87
  };
88
88
  /**
89
89
  * Override _generateNonStreaming to use applicationInferenceProfile as modelId.
90
+ * Uses the same model-swapping pattern as streaming for consistency.
90
91
  */
91
- _generateNonStreaming(messages: BaseMessage[], options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions, _runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
92
+ _generateNonStreaming(messages: BaseMessage[], options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions, runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
92
93
  /**
93
94
  * Override _streamResponseChunks to:
94
- * 1. Use applicationInferenceProfile as modelId
95
- * 2. Include serviceTier in request
96
- * 3. Strip contentBlockIndex from response_metadata to prevent merge conflicts
95
+ * 1. Use applicationInferenceProfile as modelId (by temporarily swapping this.model)
96
+ * 2. Strip contentBlockIndex from response_metadata to prevent merge conflicts
97
+ *
98
+ * Note: We delegate to super._streamResponseChunks() to preserve @langchain/aws's
99
+ * internal chunk handling which correctly preserves array content for reasoning blocks.
97
100
  */
98
101
  _streamResponseChunks(messages: BaseMessage[], options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions, runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
99
102
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@librechat/agents",
3
- "version": "3.0.79",
3
+ "version": "3.0.80",
4
4
  "main": "./dist/cjs/main.cjs",
5
5
  "module": "./dist/esm/main.mjs",
6
6
  "types": "./dist/types/index.d.ts",
@@ -56,6 +56,7 @@
56
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'",
57
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'",
58
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'",
59
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'",
60
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'",
61
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'",
@@ -21,17 +21,6 @@ import { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';
21
21
  import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
22
22
  import type { ChatBedrockConverseInput } from '@langchain/aws';
23
23
  import type { BaseMessage } from '@langchain/core/messages';
24
- import {
25
- ConverseCommand,
26
- ConverseStreamCommand,
27
- } from '@aws-sdk/client-bedrock-runtime';
28
- import {
29
- convertToConverseMessages,
30
- convertConverseMessageToLangChainMessage,
31
- handleConverseStreamContentBlockStart,
32
- handleConverseStreamContentBlockDelta,
33
- handleConverseStreamMetadata,
34
- } from './utils';
35
24
 
36
25
  /**
37
26
  * Service tier type for Bedrock invocations.
@@ -119,7 +108,7 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
119
108
  } {
120
109
  const baseParams = super.invocationParams(options);
121
110
 
122
- // Get serviceTier from options or fall back to class-level setting
111
+ /** Service tier from options or fall back to class-level setting */
123
112
  const serviceTierType = options?.serviceTier ?? this.serviceTier;
124
113
 
125
114
  return {
@@ -130,110 +119,67 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
130
119
 
131
120
  /**
132
121
  * Override _generateNonStreaming to use applicationInferenceProfile as modelId.
122
+ * Uses the same model-swapping pattern as streaming for consistency.
133
123
  */
134
124
  override async _generateNonStreaming(
135
125
  messages: BaseMessage[],
136
126
  options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,
137
- _runManager?: CallbackManagerForLLMRun
127
+ runManager?: CallbackManagerForLLMRun
138
128
  ): Promise<ChatResult> {
139
- const { converseMessages, converseSystem } =
140
- convertToConverseMessages(messages);
141
- const params = this.invocationParams(options);
142
-
143
- const command = new ConverseCommand({
144
- modelId: this.getModelId(),
145
- messages: converseMessages,
146
- system: converseSystem,
147
- requestMetadata: options.requestMetadata,
148
- ...params,
149
- });
150
-
151
- const response = await this.client.send(command, {
152
- abortSignal: options.signal,
153
- });
154
-
155
- const { output, ...responseMetadata } = response;
156
- if (!output?.message) {
157
- throw new Error('No message found in Bedrock response.');
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;
158
136
  }
159
137
 
160
- const message = convertConverseMessageToLangChainMessage(
161
- output.message,
162
- responseMetadata
163
- );
164
-
165
- return {
166
- generations: [
167
- {
168
- text: typeof message.content === 'string' ? message.content : '',
169
- message,
170
- },
171
- ],
172
- };
138
+ try {
139
+ return await super._generateNonStreaming(messages, options, runManager);
140
+ } finally {
141
+ // Restore original model
142
+ this.model = originalModel;
143
+ }
173
144
  }
174
145
 
175
146
  /**
176
147
  * Override _streamResponseChunks to:
177
- * 1. Use applicationInferenceProfile as modelId
178
- * 2. Include serviceTier in request
179
- * 3. Strip contentBlockIndex from response_metadata to prevent merge conflicts
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.
180
153
  */
181
154
  override async *_streamResponseChunks(
182
155
  messages: BaseMessage[],
183
156
  options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,
184
157
  runManager?: CallbackManagerForLLMRun
185
158
  ): AsyncGenerator<ChatGenerationChunk> {
186
- const { converseMessages, converseSystem } =
187
- convertToConverseMessages(messages);
188
- const params = this.invocationParams(options);
189
-
190
- let { streamUsage } = this;
191
- if (options.streamUsage !== undefined) {
192
- streamUsage = options.streamUsage;
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;
193
166
  }
194
167
 
195
- const command = new ConverseStreamCommand({
196
- modelId: this.getModelId(),
197
- messages: converseMessages,
198
- system: converseSystem,
199
- requestMetadata: options.requestMetadata,
200
- ...params,
201
- });
202
-
203
- const response = await this.client.send(command, {
204
- abortSignal: options.signal,
205
- });
206
-
207
- if (response.stream) {
208
- for await (const event of response.stream) {
209
- if (event.contentBlockStart != null) {
210
- const chunk = handleConverseStreamContentBlockStart(
211
- event.contentBlockStart
212
- ) as ChatGenerationChunk | undefined;
213
- if (chunk !== undefined) {
214
- const cleanedChunk = this.cleanChunk(chunk);
215
- yield cleanedChunk;
216
- await runManager?.handleLLMNewToken(cleanedChunk.text || '');
217
- }
218
- } else if (event.contentBlockDelta != null) {
219
- const chunk = handleConverseStreamContentBlockDelta(
220
- event.contentBlockDelta
221
- ) as ChatGenerationChunk | undefined;
222
- if (chunk !== undefined) {
223
- const cleanedChunk = this.cleanChunk(chunk);
224
- yield cleanedChunk;
225
- await runManager?.handleLLMNewToken(cleanedChunk.text || '');
226
- }
227
- } else if (event.metadata != null) {
228
- const chunk = handleConverseStreamMetadata(event.metadata, {
229
- streamUsage,
230
- }) as ChatGenerationChunk | undefined;
231
- if (chunk !== undefined) {
232
- const cleanedChunk = this.cleanChunk(chunk);
233
- yield cleanedChunk;
234
- }
235
- }
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);
236
179
  }
180
+ } finally {
181
+ // Restore original model
182
+ this.model = originalModel;
237
183
  }
238
184
  }
239
185