illuma-agents 1.0.16 → 1.0.18

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 (114) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +3 -1
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/common/enum.cjs +18 -0
  4. package/dist/cjs/common/enum.cjs.map +1 -1
  5. package/dist/cjs/graphs/Graph.cjs +79 -32
  6. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  7. package/dist/cjs/llm/bedrock/index.cjs +5 -3
  8. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  9. package/dist/cjs/llm/openai/index.cjs +1 -0
  10. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  11. package/dist/cjs/llm/openrouter/index.cjs +10 -1
  12. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  13. package/dist/cjs/llm/vertexai/index.cjs +7 -8
  14. package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
  15. package/dist/cjs/main.cjs +15 -0
  16. package/dist/cjs/main.cjs.map +1 -1
  17. package/dist/cjs/messages/cache.cjs +11 -6
  18. package/dist/cjs/messages/cache.cjs.map +1 -1
  19. package/dist/cjs/messages/core.cjs +16 -8
  20. package/dist/cjs/messages/core.cjs.map +1 -1
  21. package/dist/cjs/messages/format.cjs +9 -2
  22. package/dist/cjs/messages/format.cjs.map +1 -1
  23. package/dist/cjs/messages/tools.cjs +17 -10
  24. package/dist/cjs/messages/tools.cjs.map +1 -1
  25. package/dist/cjs/stream.cjs +30 -16
  26. package/dist/cjs/stream.cjs.map +1 -1
  27. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +209 -47
  28. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  29. package/dist/cjs/tools/ToolNode.cjs +73 -3
  30. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  31. package/dist/cjs/tools/handlers.cjs +1 -0
  32. package/dist/cjs/tools/handlers.cjs.map +1 -1
  33. package/dist/cjs/tools/search/search.cjs.map +1 -1
  34. package/dist/cjs/tools/search/tool.cjs +3 -1
  35. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  36. package/dist/cjs/utils/contextAnalytics.cjs +66 -0
  37. package/dist/cjs/utils/contextAnalytics.cjs.map +1 -0
  38. package/dist/cjs/utils/run.cjs.map +1 -1
  39. package/dist/cjs/utils/toonFormat.cjs +388 -0
  40. package/dist/cjs/utils/toonFormat.cjs.map +1 -0
  41. package/dist/esm/agents/AgentContext.mjs +3 -1
  42. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  43. package/dist/esm/common/enum.mjs +19 -1
  44. package/dist/esm/common/enum.mjs.map +1 -1
  45. package/dist/esm/graphs/Graph.mjs +81 -34
  46. package/dist/esm/graphs/Graph.mjs.map +1 -1
  47. package/dist/esm/llm/bedrock/index.mjs +5 -3
  48. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  49. package/dist/esm/llm/openai/index.mjs +1 -0
  50. package/dist/esm/llm/openai/index.mjs.map +1 -1
  51. package/dist/esm/llm/openrouter/index.mjs +10 -1
  52. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  53. package/dist/esm/llm/vertexai/index.mjs +7 -8
  54. package/dist/esm/llm/vertexai/index.mjs.map +1 -1
  55. package/dist/esm/main.mjs +4 -2
  56. package/dist/esm/main.mjs.map +1 -1
  57. package/dist/esm/messages/cache.mjs +11 -6
  58. package/dist/esm/messages/cache.mjs.map +1 -1
  59. package/dist/esm/messages/core.mjs +18 -10
  60. package/dist/esm/messages/core.mjs.map +1 -1
  61. package/dist/esm/messages/format.mjs +10 -3
  62. package/dist/esm/messages/format.mjs.map +1 -1
  63. package/dist/esm/messages/tools.mjs +19 -12
  64. package/dist/esm/messages/tools.mjs.map +1 -1
  65. package/dist/esm/stream.mjs +30 -16
  66. package/dist/esm/stream.mjs.map +1 -1
  67. package/dist/esm/tools/ProgrammaticToolCalling.mjs +208 -48
  68. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  69. package/dist/esm/tools/ToolNode.mjs +73 -3
  70. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  71. package/dist/esm/tools/handlers.mjs +1 -0
  72. package/dist/esm/tools/handlers.mjs.map +1 -1
  73. package/dist/esm/tools/search/search.mjs.map +1 -1
  74. package/dist/esm/tools/search/tool.mjs +3 -1
  75. package/dist/esm/tools/search/tool.mjs.map +1 -1
  76. package/dist/esm/utils/contextAnalytics.mjs +64 -0
  77. package/dist/esm/utils/contextAnalytics.mjs.map +1 -0
  78. package/dist/esm/utils/run.mjs.map +1 -1
  79. package/dist/esm/utils/toonFormat.mjs +381 -0
  80. package/dist/esm/utils/toonFormat.mjs.map +1 -0
  81. package/dist/types/common/enum.d.ts +17 -0
  82. package/dist/types/graphs/Graph.d.ts +8 -0
  83. package/dist/types/tools/ProgrammaticToolCalling.d.ts +19 -0
  84. package/dist/types/types/tools.d.ts +3 -1
  85. package/dist/types/utils/contextAnalytics.d.ts +37 -0
  86. package/dist/types/utils/index.d.ts +2 -0
  87. package/dist/types/utils/toonFormat.d.ts +111 -0
  88. package/package.json +3 -2
  89. package/src/agents/AgentContext.ts +28 -20
  90. package/src/common/enum.ts +18 -0
  91. package/src/graphs/Graph.ts +152 -62
  92. package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +495 -473
  93. package/src/llm/bedrock/index.ts +47 -35
  94. package/src/llm/openrouter/index.ts +11 -1
  95. package/src/llm/vertexai/index.ts +9 -10
  96. package/src/messages/cache.ts +104 -55
  97. package/src/messages/core.ts +29 -19
  98. package/src/messages/format.ts +14 -3
  99. package/src/messages/tools.ts +20 -13
  100. package/src/scripts/simple.ts +1 -1
  101. package/src/specs/emergency-prune.test.ts +407 -355
  102. package/src/stream.ts +28 -20
  103. package/src/tools/ProgrammaticToolCalling.ts +246 -52
  104. package/src/tools/ToolNode.ts +78 -5
  105. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +155 -0
  106. package/src/tools/search/jina-reranker.test.ts +32 -28
  107. package/src/tools/search/search.ts +3 -1
  108. package/src/tools/search/tool.ts +16 -7
  109. package/src/types/tools.ts +3 -1
  110. package/src/utils/contextAnalytics.ts +103 -0
  111. package/src/utils/index.ts +2 -0
  112. package/src/utils/llmConfig.ts +8 -1
  113. package/src/utils/run.ts +5 -4
  114. package/src/utils/toonFormat.ts +475 -0
@@ -1,473 +1,495 @@
1
- /**
2
- * Unit tests for Bedrock Prompt Caching functionality
3
- *
4
- * Tests cover:
5
- * 1. CustomChatBedrockConverse - Tool caching with cachePoint
6
- * 2. AgentContext - System message caching with cachePoint for Bedrock
7
- */
8
-
9
- import { CustomChatBedrockConverse } from '../index';
10
- import { AgentContext } from '@/agents/AgentContext';
11
- import { Providers } from '@/common';
12
- import type * as t from '@/types';
13
-
14
- describe('Bedrock Prompt Caching', () => {
15
- describe('CustomChatBedrockConverse - Tool Caching', () => {
16
- describe('invocationParams with promptCache enabled', () => {
17
- it('should add cachePoint to tools array when promptCache is true', () => {
18
- const model = new CustomChatBedrockConverse({
19
- model: 'anthropic.claude-3-haiku-20240307-v1:0',
20
- region: 'us-east-1',
21
- promptCache: true,
22
- });
23
-
24
- const mockTools = [
25
- {
26
- toolSpec: {
27
- name: 'get_weather',
28
- description: 'Get weather for a location',
29
- inputSchema: { json: { type: 'object', properties: {} } },
30
- },
31
- },
32
- {
33
- toolSpec: {
34
- name: 'search_web',
35
- description: 'Search the web',
36
- inputSchema: { json: { type: 'object', properties: {} } },
37
- },
38
- },
39
- ];
40
-
41
- const params = model.invocationParams({
42
- tools: mockTools,
43
- } as any);
44
-
45
- // Should have tools + cachePoint
46
- expect(params.toolConfig?.tools).toHaveLength(3);
47
- expect(params.toolConfig?.tools?.[2]).toEqual({
48
- cachePoint: { type: 'default' },
49
- });
50
- });
51
-
52
- it('should NOT add cachePoint when promptCache is false', () => {
53
- const model = new CustomChatBedrockConverse({
54
- model: 'anthropic.claude-3-haiku-20240307-v1:0',
55
- region: 'us-east-1',
56
- promptCache: false,
57
- });
58
-
59
- const mockTools = [
60
- {
61
- toolSpec: {
62
- name: 'get_weather',
63
- description: 'Get weather',
64
- inputSchema: { json: { type: 'object', properties: {} } },
65
- },
66
- },
67
- ];
68
-
69
- const params = model.invocationParams({
70
- tools: mockTools,
71
- } as any);
72
-
73
- // Should only have original tools, no cachePoint
74
- expect(params.toolConfig?.tools).toHaveLength(1);
75
- expect(params.toolConfig?.tools?.[0]).toEqual(mockTools[0]);
76
- });
77
-
78
- it('should NOT add cachePoint when promptCache is undefined (default)', () => {
79
- const model = new CustomChatBedrockConverse({
80
- model: 'anthropic.claude-3-haiku-20240307-v1:0',
81
- region: 'us-east-1',
82
- });
83
-
84
- const mockTools = [
85
- {
86
- toolSpec: {
87
- name: 'get_weather',
88
- description: 'Get weather',
89
- inputSchema: { json: { type: 'object', properties: {} } },
90
- },
91
- },
92
- ];
93
-
94
- const params = model.invocationParams({
95
- tools: mockTools,
96
- } as any);
97
-
98
- // Should only have original tools
99
- expect(params.toolConfig?.tools).toHaveLength(1);
100
- });
101
-
102
- it('should NOT add cachePoint when no tools are provided', () => {
103
- const model = new CustomChatBedrockConverse({
104
- model: 'anthropic.claude-3-haiku-20240307-v1:0',
105
- region: 'us-east-1',
106
- promptCache: true,
107
- });
108
-
109
- const params = model.invocationParams({} as any);
110
-
111
- // toolConfig should be undefined or have no tools
112
- expect(params.toolConfig?.tools).toBeUndefined();
113
- });
114
-
115
- it('should NOT add cachePoint when tools array is empty', () => {
116
- const model = new CustomChatBedrockConverse({
117
- model: 'anthropic.claude-3-haiku-20240307-v1:0',
118
- region: 'us-east-1',
119
- promptCache: true,
120
- });
121
-
122
- const params = model.invocationParams({
123
- tools: [],
124
- } as any);
125
-
126
- // Empty tools array results in undefined toolConfig
127
- expect(params.toolConfig).toBeUndefined();
128
- });
129
-
130
- it('should preserve other invocationParams properties', () => {
131
- const model = new CustomChatBedrockConverse({
132
- model: 'anthropic.claude-3-haiku-20240307-v1:0',
133
- region: 'us-east-1',
134
- promptCache: true,
135
- temperature: 0.7,
136
- maxTokens: 1000,
137
- });
138
-
139
- const mockTools = [
140
- {
141
- toolSpec: {
142
- name: 'test_tool',
143
- description: 'Test',
144
- inputSchema: { json: { type: 'object', properties: {} } },
145
- },
146
- },
147
- ];
148
-
149
- const params = model.invocationParams({
150
- tools: mockTools,
151
- } as any);
152
-
153
- // Check that other params are preserved
154
- expect(params.inferenceConfig?.temperature).toBe(0.7);
155
- expect(params.inferenceConfig?.maxTokens).toBe(1000);
156
- // And cachePoint is still added
157
- expect(params.toolConfig?.tools).toHaveLength(2);
158
- });
159
- });
160
-
161
- describe('promptCache property', () => {
162
- it('should store promptCache value from constructor', () => {
163
- const modelWithCache = new CustomChatBedrockConverse({
164
- model: 'test-model',
165
- region: 'us-east-1',
166
- promptCache: true,
167
- });
168
-
169
- const modelWithoutCache = new CustomChatBedrockConverse({
170
- model: 'test-model',
171
- region: 'us-east-1',
172
- promptCache: false,
173
- });
174
-
175
- expect(modelWithCache.promptCache).toBe(true);
176
- expect(modelWithoutCache.promptCache).toBe(false);
177
- });
178
-
179
- it('should default promptCache to false when not provided', () => {
180
- const model = new CustomChatBedrockConverse({
181
- model: 'test-model',
182
- region: 'us-east-1',
183
- });
184
-
185
- expect(model.promptCache).toBe(false);
186
- });
187
- });
188
- });
189
-
190
- describe('AgentContext - System Message Caching', () => {
191
- const createBedrockContext = (options: {
192
- instructions?: string;
193
- promptCache?: boolean;
194
- additionalInstructions?: string;
195
- }): AgentContext => {
196
- const clientOptions: t.BedrockAnthropicInput = {
197
- model: 'anthropic.claude-3-haiku-20240307-v1:0',
198
- region: 'us-east-1',
199
- promptCache: options.promptCache,
200
- };
201
-
202
- return AgentContext.fromConfig({
203
- agentId: 'test-bedrock-agent',
204
- provider: Providers.BEDROCK,
205
- instructions: options.instructions,
206
- additional_instructions: options.additionalInstructions,
207
- clientOptions,
208
- });
209
- };
210
-
211
- const createAnthropicContext = (options: {
212
- instructions?: string;
213
- promptCachingEnabled?: boolean;
214
- }): AgentContext => {
215
- const clientOptions: t.AnthropicClientOptions = {
216
- clientOptions: {
217
- defaultHeaders: options.promptCachingEnabled
218
- ? { 'anthropic-beta': 'prompt-caching-2024-07-31' }
219
- : undefined,
220
- },
221
- };
222
-
223
- return AgentContext.fromConfig({
224
- agentId: 'test-anthropic-agent',
225
- provider: Providers.ANTHROPIC,
226
- instructions: options.instructions,
227
- clientOptions,
228
- });
229
- };
230
-
231
- describe('Bedrock system message with promptCache: true', () => {
232
- it('should add cachePoint to system message content', async () => {
233
- const ctx = createBedrockContext({
234
- instructions: 'You are a helpful assistant.',
235
- promptCache: true,
236
- });
237
-
238
- const systemRunnable = ctx.systemRunnable;
239
- expect(systemRunnable).toBeDefined();
240
-
241
- // Invoke the runnable to get the messages
242
- const result = await systemRunnable!.invoke([]);
243
- const systemMessage = result[0];
244
-
245
- // Check content structure has cachePoint
246
- expect(systemMessage.content).toBeInstanceOf(Array);
247
- const content = systemMessage.content as Array<Record<string, unknown>>;
248
-
249
- expect(content).toHaveLength(2);
250
- expect(content[0]).toEqual({
251
- type: 'text',
252
- text: 'You are a helpful assistant.',
253
- });
254
- expect(content[1]).toEqual({
255
- cachePoint: { type: 'default' },
256
- });
257
- });
258
-
259
- it('should include combined instructions and additional_instructions with cachePoint', async () => {
260
- const ctx = createBedrockContext({
261
- instructions: 'Base instructions.',
262
- additionalInstructions: 'Additional context.',
263
- promptCache: true,
264
- });
265
-
266
- const result = await ctx.systemRunnable!.invoke([]);
267
- const systemMessage = result[0];
268
- const content = systemMessage.content as Array<Record<string, unknown>>;
269
-
270
- expect(content).toHaveLength(2);
271
- expect((content[0] as { text: string }).text).toContain('Base instructions.');
272
- expect((content[0] as { text: string }).text).toContain('Additional context.');
273
- expect(content[1]).toEqual({
274
- cachePoint: { type: 'default' },
275
- });
276
- });
277
- });
278
-
279
- describe('Bedrock system message with promptCache: false', () => {
280
- it('should NOT add cachePoint when promptCache is false', async () => {
281
- const ctx = createBedrockContext({
282
- instructions: 'You are a helpful assistant.',
283
- promptCache: false,
284
- });
285
-
286
- const result = await ctx.systemRunnable!.invoke([]);
287
- const systemMessage = result[0];
288
-
289
- // Content should be plain string, not array with cachePoint
290
- expect(typeof systemMessage.content).toBe('string');
291
- expect(systemMessage.content).toBe('You are a helpful assistant.');
292
- });
293
-
294
- it('should NOT add cachePoint when promptCache is undefined', async () => {
295
- const ctx = createBedrockContext({
296
- instructions: 'You are a helpful assistant.',
297
- promptCache: undefined,
298
- });
299
-
300
- const result = await ctx.systemRunnable!.invoke([]);
301
- const systemMessage = result[0];
302
-
303
- expect(typeof systemMessage.content).toBe('string');
304
- });
305
- });
306
-
307
- describe('Anthropic system message caching (for comparison)', () => {
308
- it('should add cache_control for Anthropic with prompt-caching beta', async () => {
309
- const ctx = createAnthropicContext({
310
- instructions: 'You are a helpful assistant.',
311
- promptCachingEnabled: true,
312
- });
313
-
314
- const result = await ctx.systemRunnable!.invoke([]);
315
- const systemMessage = result[0];
316
- const content = systemMessage.content as Array<Record<string, unknown>>;
317
-
318
- expect(content).toHaveLength(1);
319
- expect(content[0]).toEqual({
320
- type: 'text',
321
- text: 'You are a helpful assistant.',
322
- cache_control: { type: 'ephemeral' },
323
- });
324
- });
325
-
326
- it('should NOT add cache_control for Anthropic without beta header', async () => {
327
- const ctx = createAnthropicContext({
328
- instructions: 'You are a helpful assistant.',
329
- promptCachingEnabled: false,
330
- });
331
-
332
- const result = await ctx.systemRunnable!.invoke([]);
333
- const systemMessage = result[0];
334
-
335
- expect(typeof systemMessage.content).toBe('string');
336
- });
337
- });
338
-
339
- describe('Provider-specific caching behavior', () => {
340
- it('should use cachePoint format for Bedrock, not cache_control', async () => {
341
- const bedrockCtx = createBedrockContext({
342
- instructions: 'Test',
343
- promptCache: true,
344
- });
345
-
346
- const result = await bedrockCtx.systemRunnable!.invoke([]);
347
- const content = result[0].content as Array<Record<string, unknown>>;
348
-
349
- // Bedrock uses cachePoint, NOT cache_control
350
- expect(content.some((c) => 'cachePoint' in c)).toBe(true);
351
- expect(content.some((c) => 'cache_control' in c)).toBe(false);
352
- });
353
-
354
- it('should use cache_control format for Anthropic, not cachePoint', async () => {
355
- const anthropicCtx = createAnthropicContext({
356
- instructions: 'Test',
357
- promptCachingEnabled: true,
358
- });
359
-
360
- const result = await anthropicCtx.systemRunnable!.invoke([]);
361
- const content = result[0].content as Array<Record<string, unknown>>;
362
-
363
- // Anthropic uses cache_control, NOT cachePoint
364
- expect(content.some((c) => 'cache_control' in c)).toBe(true);
365
- expect(content.some((c) => 'cachePoint' in c)).toBe(false);
366
- });
367
-
368
- it('should not add any caching for OpenAI provider', async () => {
369
- const openaiCtx = AgentContext.fromConfig({
370
- agentId: 'test-openai-agent',
371
- provider: Providers.OPENAI,
372
- instructions: 'Test instructions',
373
- });
374
-
375
- const result = await openaiCtx.systemRunnable!.invoke([]);
376
- const systemMessage = result[0];
377
-
378
- // OpenAI should have plain string content
379
- expect(typeof systemMessage.content).toBe('string');
380
- expect(systemMessage.content).toBe('Test instructions');
381
- });
382
- });
383
- });
384
-
385
- describe('Integration scenarios', () => {
386
- it('should handle multi-turn caching scenario', async () => {
387
- // Simulate: Query 1 with tools → Query 2 with same tools
388
- // Both should have cachePoint, enabling cache reuse
389
-
390
- const model = new CustomChatBedrockConverse({
391
- model: 'anthropic.claude-3-haiku-20240307-v1:0',
392
- region: 'us-east-1',
393
- promptCache: true,
394
- });
395
-
396
- const tools = [
397
- {
398
- toolSpec: {
399
- name: 'execute_code',
400
- description: 'Execute Python code',
401
- inputSchema: { json: { type: 'object', properties: {} } },
402
- },
403
- },
404
- {
405
- toolSpec: {
406
- name: 'file_search',
407
- description: 'Search files',
408
- inputSchema: { json: { type: 'object', properties: {} } },
409
- },
410
- },
411
- ];
412
-
413
- // Query 1
414
- const params1 = model.invocationParams({ tools } as any);
415
-
416
- // Query 2 (same tools)
417
- const params2 = model.invocationParams({ tools } as any);
418
-
419
- // Both should have identical tool configs with cachePoint
420
- expect(params1.toolConfig?.tools).toEqual(params2.toolConfig?.tools);
421
- expect(params1.toolConfig?.tools).toHaveLength(3); // 2 tools + cachePoint
422
- });
423
-
424
- it('should handle dynamic tool selection scenario', async () => {
425
- // Simulate: Query 1 with [A, B] → Query 2 with [A, B, C]
426
- // Tool set changed, so cache won't match (expected behavior)
427
-
428
- const model = new CustomChatBedrockConverse({
429
- model: 'anthropic.claude-3-haiku-20240307-v1:0',
430
- region: 'us-east-1',
431
- promptCache: true,
432
- });
433
-
434
- const toolA = {
435
- toolSpec: {
436
- name: 'tool_a',
437
- description: 'Tool A',
438
- inputSchema: { json: { type: 'object', properties: {} } },
439
- },
440
- };
441
-
442
- const toolB = {
443
- toolSpec: {
444
- name: 'tool_b',
445
- description: 'Tool B',
446
- inputSchema: { json: { type: 'object', properties: {} } },
447
- },
448
- };
449
-
450
- const toolC = {
451
- toolSpec: {
452
- name: 'tool_c',
453
- description: 'Tool C',
454
- inputSchema: { json: { type: 'object', properties: {} } },
455
- },
456
- };
457
-
458
- // Query 1: [A, B]
459
- const params1 = model.invocationParams({ tools: [toolA, toolB] } as any);
460
-
461
- // Query 2: [A, B, C] - different tool set
462
- const params2 = model.invocationParams({ tools: [toolA, toolB, toolC] } as any);
463
-
464
- // Different lengths (cache prefix won't match beyond common prefix)
465
- expect(params1.toolConfig?.tools).toHaveLength(3); // 2 tools + cachePoint
466
- expect(params2.toolConfig?.tools).toHaveLength(4); // 3 tools + cachePoint
467
-
468
- // But both have cachePoint at the end
469
- expect(params1.toolConfig?.tools?.[2]).toEqual({ cachePoint: { type: 'default' } });
470
- expect(params2.toolConfig?.tools?.[3]).toEqual({ cachePoint: { type: 'default' } });
471
- });
472
- });
473
- });
1
+ /**
2
+ * Unit tests for Bedrock Prompt Caching functionality
3
+ *
4
+ * Tests cover:
5
+ * 1. CustomChatBedrockConverse - Tool caching with cachePoint
6
+ * 2. AgentContext - System message caching with cachePoint for Bedrock
7
+ */
8
+
9
+ import { CustomChatBedrockConverse } from '../index';
10
+ import { AgentContext } from '@/agents/AgentContext';
11
+ import { Providers } from '@/common';
12
+ import type * as t from '@/types';
13
+
14
+ type InvocationParamsOptions = Parameters<
15
+ CustomChatBedrockConverse['invocationParams']
16
+ >[0];
17
+
18
+ describe('Bedrock Prompt Caching', () => {
19
+ describe('CustomChatBedrockConverse - Tool Caching', () => {
20
+ describe('invocationParams with promptCache enabled', () => {
21
+ it('should add cachePoint to tools array when promptCache is true', () => {
22
+ const model = new CustomChatBedrockConverse({
23
+ model: 'anthropic.claude-3-haiku-20240307-v1:0',
24
+ region: 'us-east-1',
25
+ promptCache: true,
26
+ });
27
+
28
+ const mockTools = [
29
+ {
30
+ toolSpec: {
31
+ name: 'get_weather',
32
+ description: 'Get weather for a location',
33
+ inputSchema: { json: { type: 'object', properties: {} } },
34
+ },
35
+ },
36
+ {
37
+ toolSpec: {
38
+ name: 'search_web',
39
+ description: 'Search the web',
40
+ inputSchema: { json: { type: 'object', properties: {} } },
41
+ },
42
+ },
43
+ ];
44
+
45
+ const params = model.invocationParams({
46
+ tools: mockTools,
47
+ } satisfies InvocationParamsOptions);
48
+
49
+ // Should have tools + cachePoint
50
+ expect(params.toolConfig?.tools).toHaveLength(3);
51
+ expect(params.toolConfig?.tools?.[2]).toEqual({
52
+ cachePoint: { type: 'default' },
53
+ });
54
+ });
55
+
56
+ it('should NOT add cachePoint when promptCache is false', () => {
57
+ const model = new CustomChatBedrockConverse({
58
+ model: 'anthropic.claude-3-haiku-20240307-v1:0',
59
+ region: 'us-east-1',
60
+ promptCache: false,
61
+ });
62
+
63
+ const mockTools = [
64
+ {
65
+ toolSpec: {
66
+ name: 'get_weather',
67
+ description: 'Get weather',
68
+ inputSchema: { json: { type: 'object', properties: {} } },
69
+ },
70
+ },
71
+ ];
72
+
73
+ const params = model.invocationParams({
74
+ tools: mockTools,
75
+ } satisfies InvocationParamsOptions);
76
+
77
+ // Should only have original tools, no cachePoint
78
+ expect(params.toolConfig?.tools).toHaveLength(1);
79
+ expect(params.toolConfig?.tools?.[0]).toEqual(mockTools[0]);
80
+ });
81
+
82
+ it('should NOT add cachePoint when promptCache is undefined (default)', () => {
83
+ const model = new CustomChatBedrockConverse({
84
+ model: 'anthropic.claude-3-haiku-20240307-v1:0',
85
+ region: 'us-east-1',
86
+ });
87
+
88
+ const mockTools = [
89
+ {
90
+ toolSpec: {
91
+ name: 'get_weather',
92
+ description: 'Get weather',
93
+ inputSchema: { json: { type: 'object', properties: {} } },
94
+ },
95
+ },
96
+ ];
97
+
98
+ const params = model.invocationParams({
99
+ tools: mockTools,
100
+ } satisfies InvocationParamsOptions);
101
+
102
+ // Should only have original tools
103
+ expect(params.toolConfig?.tools).toHaveLength(1);
104
+ });
105
+
106
+ it('should NOT add cachePoint when no tools are provided', () => {
107
+ const model = new CustomChatBedrockConverse({
108
+ model: 'anthropic.claude-3-haiku-20240307-v1:0',
109
+ region: 'us-east-1',
110
+ promptCache: true,
111
+ });
112
+
113
+ const params = model.invocationParams(
114
+ {} satisfies InvocationParamsOptions
115
+ );
116
+
117
+ // toolConfig should be undefined or have no tools
118
+ expect(params.toolConfig?.tools).toBeUndefined();
119
+ });
120
+
121
+ it('should NOT add cachePoint when tools array is empty', () => {
122
+ const model = new CustomChatBedrockConverse({
123
+ model: 'anthropic.claude-3-haiku-20240307-v1:0',
124
+ region: 'us-east-1',
125
+ promptCache: true,
126
+ });
127
+
128
+ const params = model.invocationParams({
129
+ tools: [],
130
+ } satisfies InvocationParamsOptions);
131
+
132
+ // Empty tools array results in undefined toolConfig
133
+ expect(params.toolConfig).toBeUndefined();
134
+ });
135
+
136
+ it('should preserve other invocationParams properties', () => {
137
+ const model = new CustomChatBedrockConverse({
138
+ model: 'anthropic.claude-3-haiku-20240307-v1:0',
139
+ region: 'us-east-1',
140
+ promptCache: true,
141
+ temperature: 0.7,
142
+ maxTokens: 1000,
143
+ });
144
+
145
+ const mockTools = [
146
+ {
147
+ toolSpec: {
148
+ name: 'test_tool',
149
+ description: 'Test',
150
+ inputSchema: { json: { type: 'object', properties: {} } },
151
+ },
152
+ },
153
+ ];
154
+
155
+ const params = model.invocationParams({
156
+ tools: mockTools,
157
+ } satisfies InvocationParamsOptions);
158
+
159
+ // Check that other params are preserved
160
+ expect(params.inferenceConfig?.temperature).toBe(0.7);
161
+ expect(params.inferenceConfig?.maxTokens).toBe(1000);
162
+ // And cachePoint is still added
163
+ expect(params.toolConfig?.tools).toHaveLength(2);
164
+ });
165
+ });
166
+
167
+ describe('promptCache property', () => {
168
+ it('should store promptCache value from constructor', () => {
169
+ const modelWithCache = new CustomChatBedrockConverse({
170
+ model: 'test-model',
171
+ region: 'us-east-1',
172
+ promptCache: true,
173
+ });
174
+
175
+ const modelWithoutCache = new CustomChatBedrockConverse({
176
+ model: 'test-model',
177
+ region: 'us-east-1',
178
+ promptCache: false,
179
+ });
180
+
181
+ expect(modelWithCache.promptCache).toBe(true);
182
+ expect(modelWithoutCache.promptCache).toBe(false);
183
+ });
184
+
185
+ it('should default promptCache to false when not provided', () => {
186
+ const model = new CustomChatBedrockConverse({
187
+ model: 'test-model',
188
+ region: 'us-east-1',
189
+ });
190
+
191
+ expect(model.promptCache).toBe(false);
192
+ });
193
+ });
194
+ });
195
+
196
+ describe('AgentContext - System Message Caching', () => {
197
+ const createBedrockContext = (options: {
198
+ instructions?: string;
199
+ promptCache?: boolean;
200
+ additionalInstructions?: string;
201
+ }): AgentContext => {
202
+ const clientOptions: t.BedrockAnthropicInput = {
203
+ model: 'anthropic.claude-3-haiku-20240307-v1:0',
204
+ region: 'us-east-1',
205
+ promptCache: options.promptCache,
206
+ };
207
+
208
+ return AgentContext.fromConfig({
209
+ agentId: 'test-bedrock-agent',
210
+ provider: Providers.BEDROCK,
211
+ instructions: options.instructions,
212
+ additional_instructions: options.additionalInstructions,
213
+ clientOptions,
214
+ });
215
+ };
216
+
217
+ const createAnthropicContext = (options: {
218
+ instructions?: string;
219
+ promptCachingEnabled?: boolean;
220
+ }): AgentContext => {
221
+ const clientOptions: t.AnthropicClientOptions = {
222
+ clientOptions: {
223
+ defaultHeaders:
224
+ options.promptCachingEnabled === true
225
+ ? { 'anthropic-beta': 'prompt-caching-2024-07-31' }
226
+ : undefined,
227
+ },
228
+ };
229
+ return AgentContext.fromConfig({
230
+ agentId: 'test-anthropic-agent',
231
+ provider: Providers.ANTHROPIC,
232
+ instructions: options.instructions,
233
+ clientOptions,
234
+ });
235
+ };
236
+
237
+ describe('Bedrock system message with promptCache: true', () => {
238
+ it('should add cachePoint to system message content', async () => {
239
+ const ctx = createBedrockContext({
240
+ instructions: 'You are a helpful assistant.',
241
+ promptCache: true,
242
+ });
243
+
244
+ const systemRunnable = ctx.systemRunnable;
245
+ expect(systemRunnable).toBeDefined();
246
+
247
+ // Invoke the runnable to get the messages
248
+ const result = await systemRunnable!.invoke([]);
249
+ const systemMessage = result[0];
250
+
251
+ // Check content structure has cachePoint
252
+ expect(systemMessage.content).toBeInstanceOf(Array);
253
+ const content = systemMessage.content as Array<Record<string, unknown>>;
254
+
255
+ expect(content).toHaveLength(2);
256
+ expect(content[0]).toEqual({
257
+ type: 'text',
258
+ text: 'You are a helpful assistant.',
259
+ });
260
+ expect(content[1]).toEqual({
261
+ cachePoint: { type: 'default' },
262
+ });
263
+ });
264
+
265
+ it('should include combined instructions and additional_instructions with cachePoint', async () => {
266
+ const ctx = createBedrockContext({
267
+ instructions: 'Base instructions.',
268
+ additionalInstructions: 'Additional context.',
269
+ promptCache: true,
270
+ });
271
+
272
+ const result = await ctx.systemRunnable!.invoke([]);
273
+ const systemMessage = result[0];
274
+ const content = systemMessage.content as Array<Record<string, unknown>>;
275
+
276
+ expect(content).toHaveLength(2);
277
+ expect((content[0] as { text: string }).text).toContain(
278
+ 'Base instructions.'
279
+ );
280
+ expect((content[0] as { text: string }).text).toContain(
281
+ 'Additional context.'
282
+ );
283
+ expect(content[1]).toEqual({
284
+ cachePoint: { type: 'default' },
285
+ });
286
+ });
287
+ });
288
+
289
+ describe('Bedrock system message with promptCache: false', () => {
290
+ it('should NOT add cachePoint when promptCache is false', async () => {
291
+ const ctx = createBedrockContext({
292
+ instructions: 'You are a helpful assistant.',
293
+ promptCache: false,
294
+ });
295
+
296
+ const result = await ctx.systemRunnable!.invoke([]);
297
+ const systemMessage = result[0];
298
+
299
+ // Content should be plain string, not array with cachePoint
300
+ expect(typeof systemMessage.content).toBe('string');
301
+ expect(systemMessage.content).toBe('You are a helpful assistant.');
302
+ });
303
+
304
+ it('should NOT add cachePoint when promptCache is undefined', async () => {
305
+ const ctx = createBedrockContext({
306
+ instructions: 'You are a helpful assistant.',
307
+ promptCache: undefined,
308
+ });
309
+
310
+ const result = await ctx.systemRunnable!.invoke([]);
311
+ const systemMessage = result[0];
312
+
313
+ expect(typeof systemMessage.content).toBe('string');
314
+ });
315
+ });
316
+
317
+ describe('Anthropic system message caching (for comparison)', () => {
318
+ it('should add cache_control for Anthropic with prompt-caching beta', async () => {
319
+ const ctx = createAnthropicContext({
320
+ instructions: 'You are a helpful assistant.',
321
+ promptCachingEnabled: true,
322
+ });
323
+
324
+ const result = await ctx.systemRunnable!.invoke([]);
325
+ const systemMessage = result[0];
326
+ const content = systemMessage.content as Array<Record<string, unknown>>;
327
+
328
+ expect(content).toHaveLength(1);
329
+ expect(content[0]).toEqual({
330
+ type: 'text',
331
+ text: 'You are a helpful assistant.',
332
+ cache_control: { type: 'ephemeral' },
333
+ });
334
+ });
335
+
336
+ it('should NOT add cache_control for Anthropic without beta header', async () => {
337
+ const ctx = createAnthropicContext({
338
+ instructions: 'You are a helpful assistant.',
339
+ promptCachingEnabled: false,
340
+ });
341
+
342
+ const result = await ctx.systemRunnable!.invoke([]);
343
+ const systemMessage = result[0];
344
+
345
+ expect(typeof systemMessage.content).toBe('string');
346
+ });
347
+ });
348
+
349
+ describe('Provider-specific caching behavior', () => {
350
+ it('should use cachePoint format for Bedrock, not cache_control', async () => {
351
+ const bedrockCtx = createBedrockContext({
352
+ instructions: 'Test',
353
+ promptCache: true,
354
+ });
355
+
356
+ const result = await bedrockCtx.systemRunnable!.invoke([]);
357
+ const content = result[0].content as Array<Record<string, unknown>>;
358
+
359
+ // Bedrock uses cachePoint, NOT cache_control
360
+ expect(content.some((c) => 'cachePoint' in c)).toBe(true);
361
+ expect(content.some((c) => 'cache_control' in c)).toBe(false);
362
+ });
363
+
364
+ it('should use cache_control format for Anthropic, not cachePoint', async () => {
365
+ const anthropicCtx = createAnthropicContext({
366
+ instructions: 'Test',
367
+ promptCachingEnabled: true,
368
+ });
369
+
370
+ const result = await anthropicCtx.systemRunnable!.invoke([]);
371
+ const content = result[0].content as Array<Record<string, unknown>>;
372
+
373
+ // Anthropic uses cache_control, NOT cachePoint
374
+ expect(content.some((c) => 'cache_control' in c)).toBe(true);
375
+ expect(content.some((c) => 'cachePoint' in c)).toBe(false);
376
+ });
377
+
378
+ it('should not add any caching for OpenAI provider', async () => {
379
+ const openaiCtx = AgentContext.fromConfig({
380
+ agentId: 'test-openai-agent',
381
+ provider: Providers.OPENAI,
382
+ instructions: 'Test instructions',
383
+ });
384
+
385
+ const result = await openaiCtx.systemRunnable!.invoke([]);
386
+ const systemMessage = result[0];
387
+
388
+ // OpenAI should have plain string content
389
+ expect(typeof systemMessage.content).toBe('string');
390
+ expect(systemMessage.content).toBe('Test instructions');
391
+ });
392
+ });
393
+ });
394
+
395
+ describe('Integration scenarios', () => {
396
+ it('should handle multi-turn caching scenario', async () => {
397
+ // Simulate: Query 1 with tools → Query 2 with same tools
398
+ // Both should have cachePoint, enabling cache reuse
399
+
400
+ const model = new CustomChatBedrockConverse({
401
+ model: 'anthropic.claude-3-haiku-20240307-v1:0',
402
+ region: 'us-east-1',
403
+ promptCache: true,
404
+ });
405
+
406
+ const tools = [
407
+ {
408
+ toolSpec: {
409
+ name: 'execute_code',
410
+ description: 'Execute Python code',
411
+ inputSchema: { json: { type: 'object', properties: {} } },
412
+ },
413
+ },
414
+ {
415
+ toolSpec: {
416
+ name: 'file_search',
417
+ description: 'Search files',
418
+ inputSchema: { json: { type: 'object', properties: {} } },
419
+ },
420
+ },
421
+ ];
422
+
423
+ // Query 1
424
+ const params1 = model.invocationParams({
425
+ tools,
426
+ } satisfies InvocationParamsOptions);
427
+
428
+ // Query 2 (same tools)
429
+ const params2 = model.invocationParams({
430
+ tools,
431
+ } satisfies InvocationParamsOptions);
432
+
433
+ // Both should have identical tool configs with cachePoint
434
+ expect(params1.toolConfig?.tools).toEqual(params2.toolConfig?.tools);
435
+ expect(params1.toolConfig?.tools).toHaveLength(3); // 2 tools + cachePoint
436
+ });
437
+
438
+ it('should handle dynamic tool selection scenario', async () => {
439
+ // Simulate: Query 1 with [A, B] → Query 2 with [A, B, C]
440
+ // Tool set changed, so cache won't match (expected behavior)
441
+
442
+ const model = new CustomChatBedrockConverse({
443
+ model: 'anthropic.claude-3-haiku-20240307-v1:0',
444
+ region: 'us-east-1',
445
+ promptCache: true,
446
+ });
447
+
448
+ const toolA = {
449
+ toolSpec: {
450
+ name: 'tool_a',
451
+ description: 'Tool A',
452
+ inputSchema: { json: { type: 'object', properties: {} } },
453
+ },
454
+ };
455
+
456
+ const toolB = {
457
+ toolSpec: {
458
+ name: 'tool_b',
459
+ description: 'Tool B',
460
+ inputSchema: { json: { type: 'object', properties: {} } },
461
+ },
462
+ };
463
+
464
+ const toolC = {
465
+ toolSpec: {
466
+ name: 'tool_c',
467
+ description: 'Tool C',
468
+ inputSchema: { json: { type: 'object', properties: {} } },
469
+ },
470
+ };
471
+
472
+ // Query 1: [A, B]
473
+ const params1 = model.invocationParams({
474
+ tools: [toolA, toolB],
475
+ } satisfies InvocationParamsOptions);
476
+
477
+ // Query 2: [A, B, C] - different tool set
478
+ const params2 = model.invocationParams({
479
+ tools: [toolA, toolB, toolC],
480
+ } satisfies InvocationParamsOptions);
481
+
482
+ // Different lengths (cache prefix won't match beyond common prefix)
483
+ expect(params1.toolConfig?.tools).toHaveLength(3); // 2 tools + cachePoint
484
+ expect(params2.toolConfig?.tools).toHaveLength(4); // 3 tools + cachePoint
485
+
486
+ // But both have cachePoint at the end
487
+ expect(params1.toolConfig?.tools?.[2]).toEqual({
488
+ cachePoint: { type: 'default' },
489
+ });
490
+ expect(params2.toolConfig?.tools?.[3]).toEqual({
491
+ cachePoint: { type: 'default' },
492
+ });
493
+ });
494
+ });
495
+ });