@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,46 +0,0 @@
1
- export function isAnthropicImageBlockParam(block) {
2
- if (block == null) {
3
- return false;
4
- }
5
- if (typeof block !== 'object') {
6
- return false;
7
- }
8
- if (!('type' in block) || block.type !== 'image') {
9
- return false;
10
- }
11
- if (!('source' in block) || typeof block.source !== 'object') {
12
- return false;
13
- }
14
- if (block.source == null) {
15
- return false;
16
- }
17
- if (!('type' in block.source)) {
18
- return false;
19
- }
20
- if (block.source.type === 'base64') {
21
- if (!('media_type' in block.source)) {
22
- return false;
23
- }
24
- if (typeof block.source.media_type !== 'string') {
25
- return false;
26
- }
27
- if (!('data' in block.source)) {
28
- return false;
29
- }
30
- if (typeof block.source.data !== 'string') {
31
- return false;
32
- }
33
- return true;
34
- }
35
- if (block.source.type === 'url') {
36
- if (!('url' in block.source)) {
37
- return false;
38
- }
39
- if (typeof block.source.url !== 'string') {
40
- return false;
41
- }
42
- return true;
43
- }
44
- return false;
45
- }
46
- //# sourceMappingURL=types.js.map
@@ -1,627 +0,0 @@
1
- /* eslint-disable @typescript-eslint/explicit-function-return-type */
2
- /* eslint-disable no-console */
3
- /**
4
- * This util file contains functions for converting LangChain messages to Anthropic messages.
5
- */
6
- import { HumanMessage, isAIMessage, isDataContentBlock, convertToProviderContentBlock, parseBase64DataUrl, } from '@langchain/core/messages';
7
- import { isAnthropicImageBlockParam, } from '../types';
8
- function _formatImage(imageUrl) {
9
- const parsed = parseBase64DataUrl({ dataUrl: imageUrl });
10
- if (parsed) {
11
- return {
12
- type: 'base64',
13
- media_type: parsed.mime_type,
14
- data: parsed.data,
15
- };
16
- }
17
- let parsedUrl;
18
- try {
19
- parsedUrl = new URL(imageUrl);
20
- }
21
- catch {
22
- throw new Error([
23
- `Malformed image URL: ${JSON.stringify(imageUrl)}. Content blocks of type 'image_url' must be a valid http, https, or base64-encoded data URL.`,
24
- 'Example: data:image/png;base64,/9j/4AAQSk...',
25
- 'Example: https://example.com/image.jpg',
26
- ].join('\n\n'));
27
- }
28
- if (parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:') {
29
- return {
30
- type: 'url',
31
- url: imageUrl,
32
- };
33
- }
34
- throw new Error([
35
- `Invalid image URL protocol: ${JSON.stringify(parsedUrl.protocol)}. Anthropic only supports images as http, https, or base64-encoded data URLs on 'image_url' content blocks.`,
36
- 'Example: data:image/png;base64,/9j/4AAQSk...',
37
- 'Example: https://example.com/image.jpg',
38
- ].join('\n\n'));
39
- }
40
- function _ensureMessageContents(messages) {
41
- // Merge runs of human/tool messages into single human messages with content blocks.
42
- const updatedMsgs = [];
43
- for (const message of messages) {
44
- if (message._getType() === 'tool') {
45
- if (typeof message.content === 'string') {
46
- const previousMessage = updatedMsgs[updatedMsgs.length - 1];
47
- if (previousMessage._getType() === 'human' &&
48
- Array.isArray(previousMessage.content) &&
49
- 'type' in previousMessage.content[0] &&
50
- previousMessage.content[0].type === 'tool_result') {
51
- // If the previous message was a tool result, we merge this tool message into it.
52
- previousMessage.content.push({
53
- type: 'tool_result',
54
- content: message.content,
55
- tool_use_id: message.tool_call_id,
56
- });
57
- }
58
- else {
59
- // If not, we create a new human message with the tool result.
60
- updatedMsgs.push(new HumanMessage({
61
- content: [
62
- {
63
- type: 'tool_result',
64
- content: message.content,
65
- tool_use_id: message.tool_call_id,
66
- },
67
- ],
68
- }));
69
- }
70
- }
71
- else {
72
- updatedMsgs.push(new HumanMessage({
73
- content: [
74
- {
75
- type: 'tool_result',
76
- // rare case: message.content could be undefined
77
- ...(message.content != null
78
- ? { content: _formatContent(message) }
79
- : {}),
80
- tool_use_id: message.tool_call_id,
81
- },
82
- ],
83
- }));
84
- }
85
- }
86
- else {
87
- updatedMsgs.push(message);
88
- }
89
- }
90
- return updatedMsgs;
91
- }
92
- export function _convertLangChainToolCallToAnthropic(toolCall) {
93
- if (toolCall.id === undefined) {
94
- throw new Error('Anthropic requires all tool calls to have an "id".');
95
- }
96
- return {
97
- type: 'tool_use',
98
- id: toolCall.id,
99
- name: toolCall.name,
100
- input: toolCall.args,
101
- };
102
- }
103
- const standardContentBlockConverter = {
104
- providerName: 'anthropic',
105
- fromStandardTextBlock(block) {
106
- return {
107
- type: 'text',
108
- text: block.text,
109
- ...('citations' in (block.metadata ?? {})
110
- ? { citations: block.metadata.citations }
111
- : {}),
112
- ...('cache_control' in (block.metadata ?? {})
113
- ? { cache_control: block.metadata.cache_control }
114
- : {}),
115
- };
116
- },
117
- fromStandardImageBlock(block) {
118
- if (block.source_type === 'url') {
119
- const data = parseBase64DataUrl({
120
- dataUrl: block.url,
121
- asTypedArray: false,
122
- });
123
- if (data) {
124
- return {
125
- type: 'image',
126
- source: {
127
- type: 'base64',
128
- data: data.data,
129
- media_type: data.mime_type,
130
- },
131
- ...('cache_control' in (block.metadata ?? {})
132
- ? { cache_control: block.metadata.cache_control }
133
- : {}),
134
- };
135
- }
136
- else {
137
- return {
138
- type: 'image',
139
- source: {
140
- type: 'url',
141
- url: block.url,
142
- media_type: block.mime_type ?? '',
143
- },
144
- ...('cache_control' in (block.metadata ?? {})
145
- ? { cache_control: block.metadata.cache_control }
146
- : {}),
147
- };
148
- }
149
- }
150
- else {
151
- if (block.source_type === 'base64') {
152
- return {
153
- type: 'image',
154
- source: {
155
- type: 'base64',
156
- data: block.data,
157
- media_type: block.mime_type ?? '',
158
- },
159
- ...('cache_control' in (block.metadata ?? {})
160
- ? { cache_control: block.metadata.cache_control }
161
- : {}),
162
- };
163
- }
164
- else {
165
- throw new Error(`Unsupported image source type: ${block.source_type}`);
166
- }
167
- }
168
- },
169
- fromStandardFileBlock(block) {
170
- const mime_type = (block.mime_type ?? '').split(';')[0];
171
- if (block.source_type === 'url') {
172
- if (mime_type === 'application/pdf' || mime_type === '') {
173
- return {
174
- type: 'document',
175
- source: {
176
- type: 'url',
177
- url: block.url,
178
- media_type: block.mime_type ?? '',
179
- },
180
- ...('cache_control' in (block.metadata ?? {})
181
- ? { cache_control: block.metadata.cache_control }
182
- : {}),
183
- ...('citations' in (block.metadata ?? {})
184
- ? { citations: block.metadata.citations }
185
- : {}),
186
- ...('context' in (block.metadata ?? {})
187
- ? { context: block.metadata.context }
188
- : {}),
189
- ...('title' in (block.metadata ?? {})
190
- ? { title: block.metadata.title }
191
- : {}),
192
- };
193
- }
194
- throw new Error(`Unsupported file mime type for file url source: ${block.mime_type}`);
195
- }
196
- else if (block.source_type === 'text') {
197
- if (mime_type === 'text/plain' || mime_type === '') {
198
- return {
199
- type: 'document',
200
- source: {
201
- type: 'text',
202
- data: block.text,
203
- media_type: block.mime_type ?? '',
204
- },
205
- ...('cache_control' in (block.metadata ?? {})
206
- ? { cache_control: block.metadata.cache_control }
207
- : {}),
208
- ...('citations' in (block.metadata ?? {})
209
- ? { citations: block.metadata.citations }
210
- : {}),
211
- ...('context' in (block.metadata ?? {})
212
- ? { context: block.metadata.context }
213
- : {}),
214
- ...('title' in (block.metadata ?? {})
215
- ? { title: block.metadata.title }
216
- : {}),
217
- };
218
- }
219
- else {
220
- throw new Error(`Unsupported file mime type for file text source: ${block.mime_type}`);
221
- }
222
- }
223
- else if (block.source_type === 'base64') {
224
- if (mime_type === 'application/pdf' || mime_type === '') {
225
- return {
226
- type: 'document',
227
- source: {
228
- type: 'base64',
229
- data: block.data,
230
- media_type: 'application/pdf',
231
- },
232
- ...('cache_control' in (block.metadata ?? {})
233
- ? { cache_control: block.metadata.cache_control }
234
- : {}),
235
- ...('citations' in (block.metadata ?? {})
236
- ? { citations: block.metadata.citations }
237
- : {}),
238
- ...('context' in (block.metadata ?? {})
239
- ? { context: block.metadata.context }
240
- : {}),
241
- ...('title' in (block.metadata ?? {})
242
- ? { title: block.metadata.title }
243
- : {}),
244
- };
245
- }
246
- else if (['image/jpeg', 'image/png', 'image/gif', 'image/webp'].includes(mime_type)) {
247
- return {
248
- type: 'document',
249
- source: {
250
- type: 'content',
251
- content: [
252
- {
253
- type: 'image',
254
- source: {
255
- type: 'base64',
256
- data: block.data,
257
- media_type: mime_type,
258
- },
259
- },
260
- ],
261
- },
262
- ...('cache_control' in (block.metadata ?? {})
263
- ? { cache_control: block.metadata.cache_control }
264
- : {}),
265
- ...('citations' in (block.metadata ?? {})
266
- ? { citations: block.metadata.citations }
267
- : {}),
268
- ...('context' in (block.metadata ?? {})
269
- ? { context: block.metadata.context }
270
- : {}),
271
- ...('title' in (block.metadata ?? {})
272
- ? { title: block.metadata.title }
273
- : {}),
274
- };
275
- }
276
- else {
277
- throw new Error(`Unsupported file mime type for file base64 source: ${block.mime_type}`);
278
- }
279
- }
280
- else {
281
- throw new Error(`Unsupported file source type: ${block.source_type}`);
282
- }
283
- },
284
- };
285
- function _formatContent(message) {
286
- const toolTypes = [
287
- 'tool_use',
288
- 'tool_result',
289
- 'input_json_delta',
290
- 'server_tool_use',
291
- 'web_search_tool_result',
292
- 'web_search_result',
293
- ];
294
- const textTypes = ['text', 'text_delta'];
295
- const { content } = message;
296
- if (typeof content === 'string') {
297
- return content;
298
- }
299
- else {
300
- const contentBlocks = content.map((contentPart) => {
301
- /**
302
- * Handle malformed blocks that have server tool fields mixed with text type.
303
- * These can occur when server_tool_use blocks get mislabeled during aggregation.
304
- * Correct their type ONLY if we can confirm it's a server tool by checking the ID prefix.
305
- * Anthropic needs both server_tool_use and web_search_tool_result blocks for citations to work.
306
- */
307
- if ('id' in contentPart &&
308
- 'name' in contentPart &&
309
- 'input' in contentPart &&
310
- contentPart.type === 'text') {
311
- const rawPart = contentPart;
312
- const id = rawPart.id;
313
- // Only correct if this is definitely a server tool (ID starts with 'srvtoolu_')
314
- if (id && id.startsWith('srvtoolu_')) {
315
- let input = rawPart.input;
316
- // Ensure input is an object
317
- if (typeof input === 'string') {
318
- try {
319
- input = JSON.parse(input);
320
- }
321
- catch {
322
- input = {};
323
- }
324
- }
325
- const corrected = {
326
- type: 'server_tool_use',
327
- id,
328
- name: 'web_search',
329
- input: input,
330
- };
331
- return corrected;
332
- }
333
- // If it's not a server tool, skip it (return null to filter it out)
334
- return null;
335
- }
336
- /**
337
- * Handle malformed web_search_tool_result blocks marked as text.
338
- * These have tool_use_id and nested content - fix their type instead of filtering.
339
- * Only correct if we can confirm it's a web search result by checking the tool_use_id prefix.
340
- *
341
- * Handles both success results (array content) and error results (object with error_code).
342
- */
343
- if ('tool_use_id' in contentPart &&
344
- 'content' in contentPart &&
345
- contentPart.type === 'text') {
346
- const rawPart = contentPart;
347
- const toolUseId = rawPart.tool_use_id;
348
- const content = rawPart.content;
349
- // Only correct if this is definitely a server tool result (tool_use_id starts with 'srvtoolu_')
350
- if (toolUseId && toolUseId.startsWith('srvtoolu_')) {
351
- // Verify content is either an array (success) or error object
352
- const isValidContent = Array.isArray(content) ||
353
- (content != null &&
354
- typeof content === 'object' &&
355
- 'type' in content &&
356
- content.type ===
357
- 'web_search_tool_result_error');
358
- if (isValidContent) {
359
- const corrected = {
360
- type: 'web_search_tool_result',
361
- tool_use_id: toolUseId,
362
- content: content,
363
- };
364
- return corrected;
365
- }
366
- }
367
- // If it's not a recognized server tool result format, skip it (return null to filter it out)
368
- return null;
369
- }
370
- if (isDataContentBlock(contentPart)) {
371
- return convertToProviderContentBlock(contentPart, standardContentBlockConverter);
372
- }
373
- const cacheControl = 'cache_control' in contentPart ? contentPart.cache_control : undefined;
374
- if (contentPart.type === 'image_url') {
375
- let source;
376
- if (typeof contentPart.image_url === 'string') {
377
- source = _formatImage(contentPart.image_url);
378
- }
379
- else {
380
- source = _formatImage(contentPart.image_url.url);
381
- }
382
- return {
383
- type: 'image', // Explicitly setting the type as "image"
384
- source,
385
- ...(cacheControl ? { cache_control: cacheControl } : {}),
386
- };
387
- }
388
- else if (isAnthropicImageBlockParam(contentPart)) {
389
- return contentPart;
390
- }
391
- else if (contentPart.type === 'document') {
392
- // PDF
393
- return {
394
- ...contentPart,
395
- ...(cacheControl ? { cache_control: cacheControl } : {}),
396
- };
397
- }
398
- else if (contentPart.type === 'thinking') {
399
- const block = {
400
- type: 'thinking', // Explicitly setting the type as "thinking"
401
- thinking: contentPart.thinking,
402
- signature: contentPart.signature,
403
- ...(cacheControl ? { cache_control: cacheControl } : {}),
404
- };
405
- return block;
406
- }
407
- else if (contentPart.type === 'redacted_thinking') {
408
- const block = {
409
- type: 'redacted_thinking', // Explicitly setting the type as "redacted_thinking"
410
- data: contentPart.data,
411
- ...(cacheControl ? { cache_control: cacheControl } : {}),
412
- };
413
- return block;
414
- }
415
- else if (contentPart.type === 'search_result') {
416
- const block = {
417
- type: 'search_result',
418
- title: contentPart.title,
419
- source: contentPart.source,
420
- ...('cache_control' in contentPart && contentPart.cache_control
421
- ? { cache_control: contentPart.cache_control }
422
- : {}),
423
- ...('citations' in contentPart && contentPart.citations
424
- ? { citations: contentPart.citations }
425
- : {}),
426
- content: contentPart.content,
427
- };
428
- return block;
429
- }
430
- else if (contentPart.type === 'compaction') {
431
- const block = {
432
- type: 'compaction',
433
- content: contentPart.content,
434
- ...(cacheControl ? { cache_control: cacheControl } : {}),
435
- };
436
- return block;
437
- }
438
- else if (textTypes.find((t) => t === contentPart.type) &&
439
- 'text' in contentPart) {
440
- // Assuming contentPart is of type MessageContentText here
441
- return {
442
- type: 'text', // Explicitly setting the type as "text"
443
- text: contentPart.text,
444
- ...(cacheControl ? { cache_control: cacheControl } : {}),
445
- ...('citations' in contentPart && contentPart.citations
446
- ? { citations: contentPart.citations }
447
- : {}),
448
- };
449
- }
450
- else if (toolTypes.find((t) => t === contentPart.type)) {
451
- const contentPartCopy = { ...contentPart };
452
- if ('index' in contentPartCopy) {
453
- // Anthropic does not support passing the index field here, so we remove it.
454
- delete contentPartCopy.index;
455
- }
456
- if (contentPartCopy.type === 'input_json_delta') {
457
- // `input_json_delta` type only represents yielding partial tool inputs
458
- // and is not a valid type for Anthropic messages.
459
- contentPartCopy.type = 'tool_use';
460
- }
461
- if ('input' in contentPartCopy) {
462
- // Anthropic tool use inputs should be valid objects, when applicable.
463
- if (typeof contentPartCopy.input === 'string') {
464
- try {
465
- contentPartCopy.input = JSON.parse(contentPartCopy.input);
466
- }
467
- catch {
468
- contentPartCopy.input = {};
469
- }
470
- }
471
- }
472
- /**
473
- * For multi-turn conversations with citations, we must preserve ALL blocks
474
- * including server_tool_use, web_search_tool_result, and web_search_result.
475
- * Citations reference search results by index, so filtering changes indices and breaks references.
476
- *
477
- * The ToolNode already handles skipping server tool invocations via the srvtoolu_ prefix check.
478
- */
479
- // TODO: Fix when SDK types are fixed
480
- return {
481
- ...contentPartCopy,
482
- ...(cacheControl ? { cache_control: cacheControl } : {}),
483
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
484
- };
485
- }
486
- else if ('functionCall' in contentPart &&
487
- contentPart.functionCall &&
488
- typeof contentPart.functionCall === 'object' &&
489
- isAIMessage(message)) {
490
- const correspondingToolCall = message.tool_calls?.find((toolCall) => toolCall.name === contentPart.functionCall.name);
491
- if (!correspondingToolCall) {
492
- throw new Error(`Could not find tool call for function call ${contentPart.functionCall.name}`);
493
- }
494
- // Google GenAI models include a `functionCall` object inside content. We should ignore it as Anthropic will not support it.
495
- return {
496
- id: correspondingToolCall.id,
497
- type: 'tool_use',
498
- name: correspondingToolCall.name,
499
- input: contentPart.functionCall.args,
500
- };
501
- }
502
- else {
503
- console.error('Unsupported content part:', JSON.stringify(contentPart, null, 2));
504
- throw new Error('Unsupported message content format');
505
- }
506
- });
507
- return contentBlocks.filter((block) => block !== null);
508
- }
509
- }
510
- /**
511
- * Formats messages as a prompt for the model.
512
- * Used in LangSmith, export is important here.
513
- * @param messages The base messages to format as a prompt.
514
- * @returns The formatted prompt.
515
- */
516
- export function _convertMessagesToAnthropicPayload(messages) {
517
- const mergedMessages = _ensureMessageContents(messages);
518
- let system;
519
- if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {
520
- system = messages[0].content;
521
- }
522
- const conversationMessages = system !== undefined ? mergedMessages.slice(1) : mergedMessages;
523
- const formattedMessages = conversationMessages.map((message) => {
524
- let role;
525
- if (message._getType() === 'human') {
526
- role = 'user';
527
- }
528
- else if (message._getType() === 'ai') {
529
- role = 'assistant';
530
- }
531
- else if (message._getType() === 'tool') {
532
- role = 'user';
533
- }
534
- else if (message._getType() === 'system') {
535
- throw new Error('System messages are only permitted as the first passed message.');
536
- }
537
- else {
538
- throw new Error(`Message type "${message._getType()}" is not supported.`);
539
- }
540
- if (isAIMessage(message) && !!message.tool_calls?.length) {
541
- if (typeof message.content === 'string') {
542
- if (message.content === '') {
543
- return {
544
- role,
545
- content: message.tool_calls.map(_convertLangChainToolCallToAnthropic),
546
- };
547
- }
548
- else {
549
- return {
550
- role,
551
- content: [
552
- { type: 'text', text: message.content },
553
- ...message.tool_calls.map(_convertLangChainToolCallToAnthropic),
554
- ],
555
- };
556
- }
557
- }
558
- else {
559
- const { content } = message;
560
- const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) => !!content.find((contentPart) => (contentPart.type === 'tool_use' ||
561
- contentPart.type === 'input_json_delta' ||
562
- contentPart.type === 'server_tool_use') &&
563
- contentPart.id === toolCall.id));
564
- if (hasMismatchedToolCalls) {
565
- console.warn('The "tool_calls" field on a message is only respected if content is a string.');
566
- }
567
- return {
568
- role,
569
- content: _formatContent(message),
570
- };
571
- }
572
- }
573
- else {
574
- return {
575
- role,
576
- content: _formatContent(message),
577
- };
578
- }
579
- });
580
- return {
581
- messages: mergeMessages(formattedMessages),
582
- system,
583
- };
584
- }
585
- function mergeMessages(messages) {
586
- if (!messages || messages.length <= 1) {
587
- return messages;
588
- }
589
- const result = [];
590
- let currentMessage = messages[0];
591
- const normalizeContent = (content) => {
592
- if (typeof content === 'string') {
593
- return [{ type: 'text', text: content }];
594
- }
595
- return content;
596
- };
597
- const isToolResultMessage = (msg) => {
598
- if (msg.role !== 'user')
599
- return false;
600
- if (typeof msg.content === 'string') {
601
- return false;
602
- }
603
- return (Array.isArray(msg.content) &&
604
- msg.content.every((item) => item.type === 'tool_result'));
605
- };
606
- for (let i = 1; i < messages.length; i += 1) {
607
- const nextMessage = messages[i];
608
- if (isToolResultMessage(currentMessage) &&
609
- isToolResultMessage(nextMessage)) {
610
- // Merge the messages by combining their content arrays
611
- currentMessage = {
612
- ...currentMessage,
613
- content: [
614
- ...normalizeContent(currentMessage.content),
615
- ...normalizeContent(nextMessage.content),
616
- ],
617
- };
618
- }
619
- else {
620
- result.push(currentMessage);
621
- currentMessage = nextMessage;
622
- }
623
- }
624
- result.push(currentMessage);
625
- return result;
626
- }
627
- //# sourceMappingURL=message_inputs.js.map