@illuma-ai/agents 1.1.20 → 1.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (246) hide show
  1. package/dist/cjs/graphs/Graph.cjs +12 -1
  2. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  3. package/dist/cjs/graphs/MultiAgentGraph.cjs +85 -1
  4. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  5. package/dist/cjs/llm/bedrock/index.cjs +14 -0
  6. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  7. package/dist/cjs/run.cjs +20 -9
  8. package/dist/cjs/run.cjs.map +1 -1
  9. package/dist/esm/graphs/Graph.mjs +12 -1
  10. package/dist/esm/graphs/Graph.mjs.map +1 -1
  11. package/dist/esm/graphs/MultiAgentGraph.mjs +85 -1
  12. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  13. package/dist/esm/llm/bedrock/index.mjs +14 -0
  14. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  15. package/dist/esm/run.mjs +20 -9
  16. package/dist/esm/run.mjs.map +1 -1
  17. package/dist/types/graphs/MultiAgentGraph.d.ts +17 -0
  18. package/package.json +1 -1
  19. package/src/graphs/Graph.ts +12 -1
  20. package/src/graphs/MultiAgentGraph.ts +105 -1
  21. package/src/graphs/__tests__/multi-agent-delegate.test.ts +191 -0
  22. package/src/llm/bedrock/index.ts +17 -0
  23. package/src/run.ts +20 -11
  24. package/src/scripts/test-bedrock-handoff-autonomous.ts +231 -0
  25. package/src/agents/AgentContext.js +0 -782
  26. package/src/agents/AgentContext.test.js +0 -421
  27. package/src/agents/__tests__/AgentContext.test.js +0 -678
  28. package/src/agents/__tests__/resolveStructuredOutputMode.test.js +0 -117
  29. package/src/common/enum.js +0 -192
  30. package/src/common/index.js +0 -3
  31. package/src/events.js +0 -166
  32. package/src/graphs/Graph.js +0 -1857
  33. package/src/graphs/MultiAgentGraph.js +0 -1092
  34. package/src/graphs/__tests__/structured-output.integration.test.js +0 -624
  35. package/src/graphs/__tests__/structured-output.test.js +0 -144
  36. package/src/graphs/contextManagement.e2e.test.js +0 -718
  37. package/src/graphs/contextManagement.test.js +0 -485
  38. package/src/graphs/handoffValidation.test.js +0 -276
  39. package/src/graphs/index.js +0 -3
  40. package/src/index.js +0 -28
  41. package/src/instrumentation.js +0 -21
  42. package/src/llm/anthropic/index.js +0 -319
  43. package/src/llm/anthropic/types.js +0 -46
  44. package/src/llm/anthropic/utils/message_inputs.js +0 -627
  45. package/src/llm/anthropic/utils/message_outputs.js +0 -290
  46. package/src/llm/anthropic/utils/output_parsers.js +0 -89
  47. package/src/llm/anthropic/utils/tools.js +0 -25
  48. package/src/llm/bedrock/__tests__/bedrock-caching.test.js +0 -392
  49. package/src/llm/bedrock/index.js +0 -303
  50. package/src/llm/bedrock/types.js +0 -2
  51. package/src/llm/bedrock/utils/index.js +0 -6
  52. package/src/llm/bedrock/utils/message_inputs.js +0 -463
  53. package/src/llm/bedrock/utils/message_outputs.js +0 -269
  54. package/src/llm/fake.js +0 -92
  55. package/src/llm/google/index.js +0 -215
  56. package/src/llm/google/types.js +0 -12
  57. package/src/llm/google/utils/common.js +0 -670
  58. package/src/llm/google/utils/tools.js +0 -111
  59. package/src/llm/google/utils/zod_to_genai_parameters.js +0 -47
  60. package/src/llm/openai/index.js +0 -1033
  61. package/src/llm/openai/types.js +0 -2
  62. package/src/llm/openai/utils/index.js +0 -756
  63. package/src/llm/openai/utils/isReasoningModel.test.js +0 -79
  64. package/src/llm/openrouter/index.js +0 -261
  65. package/src/llm/openrouter/reasoning.test.js +0 -181
  66. package/src/llm/providers.js +0 -36
  67. package/src/llm/text.js +0 -65
  68. package/src/llm/vertexai/index.js +0 -402
  69. package/src/messages/__tests__/tools.test.js +0 -392
  70. package/src/messages/cache.js +0 -404
  71. package/src/messages/cache.test.js +0 -1167
  72. package/src/messages/content.js +0 -48
  73. package/src/messages/content.test.js +0 -314
  74. package/src/messages/core.js +0 -359
  75. package/src/messages/ensureThinkingBlock.test.js +0 -997
  76. package/src/messages/format.js +0 -973
  77. package/src/messages/formatAgentMessages.test.js +0 -2278
  78. package/src/messages/formatAgentMessages.tools.test.js +0 -362
  79. package/src/messages/formatMessage.test.js +0 -608
  80. package/src/messages/ids.js +0 -18
  81. package/src/messages/index.js +0 -9
  82. package/src/messages/labelContentByAgent.test.js +0 -725
  83. package/src/messages/prune.js +0 -438
  84. package/src/messages/reducer.js +0 -60
  85. package/src/messages/shiftIndexTokenCountMap.test.js +0 -63
  86. package/src/messages/summarize.js +0 -146
  87. package/src/messages/summarize.test.js +0 -332
  88. package/src/messages/tools.js +0 -90
  89. package/src/mockStream.js +0 -81
  90. package/src/prompts/collab.js +0 -7
  91. package/src/prompts/index.js +0 -3
  92. package/src/prompts/taskmanager.js +0 -58
  93. package/src/run.js +0 -427
  94. package/src/schemas/index.js +0 -3
  95. package/src/schemas/schema-preparation.test.js +0 -370
  96. package/src/schemas/validate.js +0 -314
  97. package/src/schemas/validate.test.js +0 -264
  98. package/src/scripts/abort.js +0 -127
  99. package/src/scripts/ant_web_search.js +0 -130
  100. package/src/scripts/ant_web_search_edge_case.js +0 -133
  101. package/src/scripts/ant_web_search_error_edge_case.js +0 -119
  102. package/src/scripts/args.js +0 -41
  103. package/src/scripts/bedrock-cache-debug.js +0 -186
  104. package/src/scripts/bedrock-content-aggregation-test.js +0 -195
  105. package/src/scripts/bedrock-merge-test.js +0 -80
  106. package/src/scripts/bedrock-parallel-tools-test.js +0 -150
  107. package/src/scripts/caching.js +0 -106
  108. package/src/scripts/cli.js +0 -152
  109. package/src/scripts/cli2.js +0 -119
  110. package/src/scripts/cli3.js +0 -163
  111. package/src/scripts/cli4.js +0 -165
  112. package/src/scripts/cli5.js +0 -165
  113. package/src/scripts/code_exec.js +0 -171
  114. package/src/scripts/code_exec_files.js +0 -180
  115. package/src/scripts/code_exec_multi_session.js +0 -185
  116. package/src/scripts/code_exec_ptc.js +0 -265
  117. package/src/scripts/code_exec_session.js +0 -217
  118. package/src/scripts/code_exec_simple.js +0 -120
  119. package/src/scripts/content.js +0 -111
  120. package/src/scripts/empty_input.js +0 -125
  121. package/src/scripts/handoff-test.js +0 -96
  122. package/src/scripts/image.js +0 -138
  123. package/src/scripts/memory.js +0 -83
  124. package/src/scripts/multi-agent-chain.js +0 -271
  125. package/src/scripts/multi-agent-conditional.js +0 -185
  126. package/src/scripts/multi-agent-document-review-chain.js +0 -171
  127. package/src/scripts/multi-agent-hybrid-flow.js +0 -264
  128. package/src/scripts/multi-agent-parallel-start.js +0 -214
  129. package/src/scripts/multi-agent-parallel.js +0 -346
  130. package/src/scripts/multi-agent-sequence.js +0 -184
  131. package/src/scripts/multi-agent-supervisor.js +0 -324
  132. package/src/scripts/multi-agent-test.js +0 -147
  133. package/src/scripts/parallel-asymmetric-tools-test.js +0 -202
  134. package/src/scripts/parallel-full-metadata-test.js +0 -176
  135. package/src/scripts/parallel-tools-test.js +0 -256
  136. package/src/scripts/programmatic_exec.js +0 -277
  137. package/src/scripts/programmatic_exec_agent.js +0 -168
  138. package/src/scripts/search.js +0 -118
  139. package/src/scripts/sequential-full-metadata-test.js +0 -143
  140. package/src/scripts/simple.js +0 -174
  141. package/src/scripts/single-agent-metadata-test.js +0 -152
  142. package/src/scripts/stream.js +0 -113
  143. package/src/scripts/test-custom-prompt-key.js +0 -132
  144. package/src/scripts/test-handoff-input.js +0 -143
  145. package/src/scripts/test-handoff-preamble.js +0 -227
  146. package/src/scripts/test-handoff-steering.js +0 -353
  147. package/src/scripts/test-multi-agent-list-handoff.js +0 -318
  148. package/src/scripts/test-parallel-agent-labeling.js +0 -253
  149. package/src/scripts/test-parallel-handoffs.js +0 -229
  150. package/src/scripts/test-thinking-handoff-bedrock.js +0 -132
  151. package/src/scripts/test-thinking-handoff.js +0 -132
  152. package/src/scripts/test-thinking-to-thinking-handoff-bedrock.js +0 -140
  153. package/src/scripts/test-tool-before-handoff-role-order.js +0 -223
  154. package/src/scripts/test-tools-before-handoff.js +0 -187
  155. package/src/scripts/test_code_api.js +0 -263
  156. package/src/scripts/thinking-bedrock.js +0 -128
  157. package/src/scripts/thinking-vertexai.js +0 -130
  158. package/src/scripts/thinking.js +0 -134
  159. package/src/scripts/tool_search.js +0 -114
  160. package/src/scripts/tools.js +0 -125
  161. package/src/specs/agent-handoffs-bedrock.integration.test.js +0 -280
  162. package/src/specs/agent-handoffs.test.js +0 -924
  163. package/src/specs/anthropic.simple.test.js +0 -287
  164. package/src/specs/azure.simple.test.js +0 -381
  165. package/src/specs/cache.simple.test.js +0 -282
  166. package/src/specs/custom-event-await.test.js +0 -148
  167. package/src/specs/deepseek.simple.test.js +0 -189
  168. package/src/specs/emergency-prune.test.js +0 -308
  169. package/src/specs/moonshot.simple.test.js +0 -237
  170. package/src/specs/observability.integration.test.js +0 -1337
  171. package/src/specs/openai.simple.test.js +0 -233
  172. package/src/specs/openrouter.simple.test.js +0 -202
  173. package/src/specs/prune.test.js +0 -733
  174. package/src/specs/reasoning.test.js +0 -144
  175. package/src/specs/spec.utils.js +0 -4
  176. package/src/specs/thinking-handoff.test.js +0 -486
  177. package/src/specs/thinking-prune.test.js +0 -600
  178. package/src/specs/token-distribution-edge-case.test.js +0 -246
  179. package/src/specs/token-memoization.test.js +0 -32
  180. package/src/specs/tokens.test.js +0 -49
  181. package/src/specs/tool-error.test.js +0 -139
  182. package/src/splitStream.js +0 -204
  183. package/src/splitStream.test.js +0 -504
  184. package/src/stream.js +0 -650
  185. package/src/stream.test.js +0 -225
  186. package/src/test/mockTools.js +0 -340
  187. package/src/tools/BrowserTools.js +0 -245
  188. package/src/tools/Calculator.js +0 -38
  189. package/src/tools/Calculator.test.js +0 -225
  190. package/src/tools/CodeExecutor.js +0 -233
  191. package/src/tools/ProgrammaticToolCalling.js +0 -602
  192. package/src/tools/StreamingToolCallBuffer.js +0 -179
  193. package/src/tools/ToolNode.js +0 -930
  194. package/src/tools/ToolSearch.js +0 -904
  195. package/src/tools/__tests__/BrowserTools.test.js +0 -306
  196. package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.js +0 -276
  197. package/src/tools/__tests__/ProgrammaticToolCalling.test.js +0 -807
  198. package/src/tools/__tests__/StreamingToolCallBuffer.test.js +0 -175
  199. package/src/tools/__tests__/ToolApproval.test.js +0 -675
  200. package/src/tools/__tests__/ToolNode.recovery.test.js +0 -200
  201. package/src/tools/__tests__/ToolNode.session.test.js +0 -319
  202. package/src/tools/__tests__/ToolSearch.integration.test.js +0 -125
  203. package/src/tools/__tests__/ToolSearch.test.js +0 -812
  204. package/src/tools/__tests__/handlers.test.js +0 -799
  205. package/src/tools/__tests__/truncation-recovery.integration.test.js +0 -362
  206. package/src/tools/handlers.js +0 -306
  207. package/src/tools/schema.js +0 -25
  208. package/src/tools/search/anthropic.js +0 -34
  209. package/src/tools/search/content.js +0 -116
  210. package/src/tools/search/content.test.js +0 -133
  211. package/src/tools/search/firecrawl.js +0 -173
  212. package/src/tools/search/format.js +0 -198
  213. package/src/tools/search/highlights.js +0 -241
  214. package/src/tools/search/index.js +0 -3
  215. package/src/tools/search/jina-reranker.test.js +0 -106
  216. package/src/tools/search/rerankers.js +0 -165
  217. package/src/tools/search/schema.js +0 -102
  218. package/src/tools/search/search.js +0 -561
  219. package/src/tools/search/serper-scraper.js +0 -126
  220. package/src/tools/search/test.js +0 -129
  221. package/src/tools/search/tool.js +0 -453
  222. package/src/tools/search/types.js +0 -2
  223. package/src/tools/search/utils.js +0 -59
  224. package/src/types/graph.js +0 -24
  225. package/src/types/graph.test.js +0 -192
  226. package/src/types/index.js +0 -7
  227. package/src/types/llm.js +0 -2
  228. package/src/types/messages.js +0 -2
  229. package/src/types/run.js +0 -2
  230. package/src/types/stream.js +0 -2
  231. package/src/types/tools.js +0 -2
  232. package/src/utils/contextAnalytics.js +0 -79
  233. package/src/utils/contextAnalytics.test.js +0 -166
  234. package/src/utils/events.js +0 -26
  235. package/src/utils/graph.js +0 -11
  236. package/src/utils/handlers.js +0 -65
  237. package/src/utils/index.js +0 -10
  238. package/src/utils/llm.js +0 -21
  239. package/src/utils/llmConfig.js +0 -205
  240. package/src/utils/logging.js +0 -37
  241. package/src/utils/misc.js +0 -51
  242. package/src/utils/run.js +0 -69
  243. package/src/utils/schema.js +0 -21
  244. package/src/utils/title.js +0 -119
  245. package/src/utils/tokens.js +0 -92
  246. package/src/utils/toonFormat.js +0 -379
@@ -1,303 +0,0 @@
1
- /**
2
- * Optimized ChatBedrockConverse wrapper that fixes content block merging for
3
- * streaming responses and adds support for latest @langchain/aws features:
4
- *
5
- * - Prompt caching support for Bedrock Converse API (Illuma feature)
6
- * - Application Inference Profiles (PR #9129)
7
- * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+
8
- *
9
- * Bedrock's `@langchain/aws` library does not include an `index` property on content
10
- * blocks (unlike Anthropic/OpenAI), which causes LangChain's `_mergeLists` to append
11
- * each streaming chunk as a separate array entry instead of merging by index.
12
- *
13
- * This wrapper takes full ownership of the stream by directly interfacing with the
14
- * AWS SDK client (`this.client`) and using custom handlers from `./utils/` that
15
- * include `contentBlockIndex` in response_metadata for every delta type. It then
16
- * promotes `contentBlockIndex` to an `index` property on each content block
17
- * (mirroring Anthropic's pattern) and strips it from metadata to avoid
18
- * `_mergeDicts` conflicts.
19
- *
20
- * When multiple content block types are present (e.g. reasoning + text), text deltas
21
- * are promoted from strings to array form with `index` so they merge correctly once
22
- * the accumulated content is already an array.
23
- *
24
- * PROMPT CACHING:
25
- * When promptCache: true is set, this wrapper adds cachePoint markers to the tools array
26
- * to enable Bedrock prompt caching for tool definitions. This allows tool schemas to be
27
- * cached and reused across requests, reducing latency and costs.
28
- *
29
- * CACHE TOKEN EXTRACTION:
30
- * Cache token extraction is handled in `./utils/message_outputs.ts` and added
31
- * to usage_metadata with input_token_details (cacheReadInputTokens/cacheWriteInputTokens).
32
- */
33
- import { ChatBedrockConverse } from '@langchain/aws';
34
- import { ConverseStreamCommand } from '@aws-sdk/client-bedrock-runtime';
35
- import { AIMessageChunk } from '@langchain/core/messages';
36
- import { ChatGenerationChunk } from '@langchain/core/outputs';
37
- import { convertToConverseMessages, handleConverseStreamContentBlockStart, handleConverseStreamContentBlockDelta, handleConverseStreamMetadata, } from './utils';
38
- export class IllumaBedrockConverse extends ChatBedrockConverse {
39
- /** Enable Bedrock prompt caching for tool definitions */
40
- promptCache;
41
- /** Application Inference Profile ARN to use instead of model ID */
42
- applicationInferenceProfile;
43
- /** Service tier for model invocation */
44
- serviceTier;
45
- constructor(fields) {
46
- super(fields);
47
- this.promptCache = fields?.promptCache ?? false;
48
- this.applicationInferenceProfile = fields?.applicationInferenceProfile;
49
- this.serviceTier = fields?.serviceTier;
50
- // Fix: Force supportsToolChoiceValues for Claude models
51
- // The parent constructor checks `model.includes('claude-3')` but this fails when:
52
- // 1. Using applicationInferenceProfile ARNs (arn:aws:bedrock:...)
53
- // 2. Using different naming conventions (claude-4, claude-opus-4, etc.)
54
- // We need to ensure tool_choice is properly set for withStructuredOutput to work
55
- const modelName = (fields?.model ?? '').toLowerCase();
56
- const profileName = (fields?.applicationInferenceProfile ?? '').toLowerCase();
57
- const isClaudeModel = modelName.includes('claude') ||
58
- modelName.includes('anthropic') ||
59
- profileName.includes('claude') ||
60
- profileName.includes('anthropic');
61
- if (isClaudeModel && !this.supportsToolChoiceValues?.length) {
62
- // Claude models support all tool choice values
63
- this.supportsToolChoiceValues = ['auto', 'any', 'tool'];
64
- }
65
- }
66
- static lc_name() {
67
- return 'IllumaBedrockConverse';
68
- }
69
- /**
70
- * Get the model ID to use for API calls.
71
- * Returns applicationInferenceProfile if set, otherwise returns this.model.
72
- */
73
- getModelId() {
74
- return this.applicationInferenceProfile ?? this.model;
75
- }
76
- /**
77
- * Override invocationParams to:
78
- * 1. Add cachePoint to tools when promptCache is enabled
79
- * 2. Add serviceTier support
80
- *
81
- * CACHING STRATEGY: Separate cachePoints for core tools and MCP tools
82
- * - Core tools (web_search, execute_code, etc.) are stable → cache first
83
- * - MCP tools (have '_mcp_' in name) are dynamic → cache separately after
84
- * - This allows core tools to stay cached when MCP selection changes
85
- *
86
- * NOTE: Only Claude models support cachePoint - Nova and other models will reject it.
87
- */
88
- invocationParams(options) {
89
- const params = super.invocationParams(options);
90
- // Add cachePoint to tools array if promptCache is enabled and tools exist
91
- // Only Claude models support cachePoint - check model name
92
- const modelId = this.model.toLowerCase();
93
- const isClaudeModel = modelId.includes('claude') || modelId.includes('anthropic');
94
- if (this.promptCache &&
95
- isClaudeModel &&
96
- params.toolConfig?.tools &&
97
- Array.isArray(params.toolConfig.tools) &&
98
- params.toolConfig.tools.length > 0) {
99
- // Separate core tools from MCP tools
100
- // MCP tools have '_mcp_' in their name (e.g., 'search_emails_mcp_Google-Workspace')
101
- const coreTools = [];
102
- const mcpTools = [];
103
- for (const tool of params.toolConfig.tools) {
104
- // Check if tool has a name property with '_mcp_' pattern
105
- const toolName = tool.toolSpec?.name ?? '';
106
- if (toolName.includes('_mcp_')) {
107
- mcpTools.push(tool);
108
- }
109
- else {
110
- coreTools.push(tool);
111
- }
112
- }
113
- // Build tools array with strategic cachePoints:
114
- // [CoreTool1, CoreTool2, cachePoint] + [MCPTool1, MCPTool2, cachePoint]
115
- const toolsWithCache = [];
116
- // Add core tools with cachePoint (if any)
117
- if (coreTools.length > 0) {
118
- toolsWithCache.push(...coreTools);
119
- toolsWithCache.push({ cachePoint: { type: 'default' } });
120
- }
121
- // Add MCP tools with their own cachePoint (if any)
122
- if (mcpTools.length > 0) {
123
- toolsWithCache.push(...mcpTools);
124
- toolsWithCache.push({ cachePoint: { type: 'default' } });
125
- }
126
- // If no tools at all (shouldn't happen but safety check)
127
- if (toolsWithCache.length === 0) {
128
- toolsWithCache.push({ cachePoint: { type: 'default' } });
129
- }
130
- params.toolConfig.tools = toolsWithCache;
131
- }
132
- // Add serviceTier support
133
- const serviceTierType = options?.serviceTier ?? this.serviceTier;
134
- return {
135
- ...params,
136
- serviceTier: serviceTierType ? { type: serviceTierType } : undefined,
137
- };
138
- }
139
- /**
140
- * Override _generateNonStreaming to use applicationInferenceProfile as modelId.
141
- * Uses the same model-swapping pattern as streaming for consistency.
142
- */
143
- async _generateNonStreaming(messages, options, runManager) {
144
- const originalModel = this.model;
145
- if (this.applicationInferenceProfile != null &&
146
- this.applicationInferenceProfile !== '') {
147
- this.model = this.applicationInferenceProfile;
148
- }
149
- try {
150
- return await super._generateNonStreaming(messages, options, runManager);
151
- }
152
- finally {
153
- this.model = originalModel;
154
- }
155
- }
156
- /**
157
- * Own the stream end-to-end so we have direct access to every
158
- * `contentBlockDelta.contentBlockIndex` from the AWS SDK.
159
- *
160
- * This replaces the parent's implementation which strips contentBlockIndex
161
- * from text and reasoning deltas, making it impossible to merge correctly.
162
- */
163
- async *_streamResponseChunks(messages, options, runManager) {
164
- const { converseMessages, converseSystem } = convertToConverseMessages(messages);
165
- const params = this.invocationParams(options);
166
- let { streamUsage } = this;
167
- if (options.streamUsage !== undefined) {
168
- streamUsage = options.streamUsage;
169
- }
170
- const modelId = this.getModelId();
171
- const command = new ConverseStreamCommand({
172
- modelId,
173
- messages: converseMessages,
174
- system: converseSystem,
175
- ...params,
176
- });
177
- const response = await this.client.send(command, {
178
- abortSignal: options.signal,
179
- });
180
- if (!response.stream) {
181
- return;
182
- }
183
- const seenBlockIndices = new Set();
184
- for await (const event of response.stream) {
185
- if (event.contentBlockStart != null) {
186
- const startChunk = handleConverseStreamContentBlockStart(event.contentBlockStart);
187
- if (startChunk != null) {
188
- const idx = event.contentBlockStart.contentBlockIndex;
189
- if (idx != null) {
190
- seenBlockIndices.add(idx);
191
- }
192
- yield this.enrichChunk(startChunk, seenBlockIndices);
193
- }
194
- }
195
- else if (event.contentBlockDelta != null) {
196
- const deltaChunk = handleConverseStreamContentBlockDelta(event.contentBlockDelta);
197
- const idx = event.contentBlockDelta.contentBlockIndex;
198
- if (idx != null) {
199
- seenBlockIndices.add(idx);
200
- }
201
- yield this.enrichChunk(deltaChunk, seenBlockIndices);
202
- await runManager?.handleLLMNewToken(deltaChunk.text, undefined, undefined, undefined, undefined, { chunk: deltaChunk });
203
- }
204
- else if (event.metadata != null) {
205
- yield handleConverseStreamMetadata(event.metadata, { streamUsage });
206
- }
207
- else if (event.contentBlockStop != null) {
208
- const stopIdx = event.contentBlockStop.contentBlockIndex;
209
- if (stopIdx != null) {
210
- seenBlockIndices.add(stopIdx);
211
- }
212
- }
213
- else {
214
- yield new ChatGenerationChunk({
215
- text: '',
216
- message: new AIMessageChunk({
217
- content: '',
218
- response_metadata: event,
219
- }),
220
- });
221
- }
222
- }
223
- }
224
- /**
225
- * Inject `index` on content blocks for proper merge behaviour, then strip
226
- * `contentBlockIndex` from response_metadata to prevent `_mergeDicts` conflicts.
227
- *
228
- * Text string content is promoted to array form only when the stream contains
229
- * multiple content block indices (e.g. reasoning at index 0, text at index 1),
230
- * ensuring text merges correctly with the already-array accumulated content.
231
- */
232
- enrichChunk(chunk, seenBlockIndices) {
233
- const message = chunk.message;
234
- if (!(message instanceof AIMessageChunk)) {
235
- return chunk;
236
- }
237
- const metadata = message.response_metadata;
238
- const blockIndex = this.extractContentBlockIndex(metadata);
239
- const hasMetadataIndex = blockIndex != null;
240
- let content = message.content;
241
- let contentModified = false;
242
- if (Array.isArray(content) && blockIndex != null) {
243
- content = content.map((block) => typeof block === 'object' && !('index' in block)
244
- ? { ...block, index: blockIndex }
245
- : block);
246
- contentModified = true;
247
- }
248
- else if (typeof content === 'string' &&
249
- content !== '' &&
250
- blockIndex != null &&
251
- seenBlockIndices.size > 1) {
252
- content = [{ type: 'text', text: content, index: blockIndex }];
253
- contentModified = true;
254
- }
255
- if (!contentModified && !hasMetadataIndex) {
256
- return chunk;
257
- }
258
- const cleanedMetadata = hasMetadataIndex
259
- ? this.removeContentBlockIndex(metadata)
260
- : metadata;
261
- return new ChatGenerationChunk({
262
- text: chunk.text,
263
- message: new AIMessageChunk({
264
- ...message,
265
- content,
266
- response_metadata: cleanedMetadata,
267
- }),
268
- generationInfo: chunk.generationInfo,
269
- });
270
- }
271
- /**
272
- * Extract `contentBlockIndex` from the top level of response_metadata.
273
- * Our custom handlers always place it at the top level.
274
- */
275
- extractContentBlockIndex(metadata) {
276
- if ('contentBlockIndex' in metadata &&
277
- typeof metadata.contentBlockIndex === 'number') {
278
- return metadata.contentBlockIndex;
279
- }
280
- return undefined;
281
- }
282
- removeContentBlockIndex(obj) {
283
- if (obj === null || obj === undefined) {
284
- return obj;
285
- }
286
- if (Array.isArray(obj)) {
287
- return obj.map((item) => this.removeContentBlockIndex(item));
288
- }
289
- if (typeof obj === 'object') {
290
- const cleaned = {};
291
- for (const [key, value] of Object.entries(obj)) {
292
- if (key !== 'contentBlockIndex') {
293
- cleaned[key] = this.removeContentBlockIndex(value);
294
- }
295
- }
296
- return cleaned;
297
- }
298
- return obj;
299
- }
300
- }
301
- /** @deprecated Use IllumaBedrockConverse. Alias kept for backward compatibility with existing imports. */
302
- export const CustomChatBedrockConverse = IllumaBedrockConverse;
303
- //# sourceMappingURL=index.js.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=types.js.map
@@ -1,6 +0,0 @@
1
- /**
2
- * Bedrock Converse utility exports.
3
- */
4
- export { convertToConverseMessages, extractImageInfo, langchainReasoningBlockToBedrockReasoningBlock, concatenateLangchainReasoningBlocks, } from './message_inputs';
5
- export { convertConverseMessageToLangChainMessage, handleConverseStreamContentBlockStart, handleConverseStreamContentBlockDelta, handleConverseStreamMetadata, bedrockReasoningBlockToLangchainReasoningBlock, bedrockReasoningDeltaToLangchainPartialReasoningBlock, } from './message_outputs';
6
- //# sourceMappingURL=index.js.map