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.
- package/dist/cjs/agents/AgentContext.cjs +3 -1
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +18 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +79 -32
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +5 -3
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +1 -0
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/index.cjs +10 -1
- package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
- package/dist/cjs/llm/vertexai/index.cjs +7 -8
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +15 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +11 -6
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/messages/core.cjs +16 -8
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +9 -2
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/tools.cjs +17 -10
- package/dist/cjs/messages/tools.cjs.map +1 -1
- package/dist/cjs/stream.cjs +30 -16
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +209 -47
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +73 -3
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +1 -0
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/tools/search/search.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +3 -1
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/utils/contextAnalytics.cjs +66 -0
- package/dist/cjs/utils/contextAnalytics.cjs.map +1 -0
- package/dist/cjs/utils/run.cjs.map +1 -1
- package/dist/cjs/utils/toonFormat.cjs +388 -0
- package/dist/cjs/utils/toonFormat.cjs.map +1 -0
- package/dist/esm/agents/AgentContext.mjs +3 -1
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +19 -1
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +81 -34
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +5 -3
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +1 -0
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openrouter/index.mjs +10 -1
- package/dist/esm/llm/openrouter/index.mjs.map +1 -1
- package/dist/esm/llm/vertexai/index.mjs +7 -8
- package/dist/esm/llm/vertexai/index.mjs.map +1 -1
- package/dist/esm/main.mjs +4 -2
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +11 -6
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/messages/core.mjs +18 -10
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +10 -3
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/tools.mjs +19 -12
- package/dist/esm/messages/tools.mjs.map +1 -1
- package/dist/esm/stream.mjs +30 -16
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +208 -48
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +73 -3
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +1 -0
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/tools/search/search.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +3 -1
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/utils/contextAnalytics.mjs +64 -0
- package/dist/esm/utils/contextAnalytics.mjs.map +1 -0
- package/dist/esm/utils/run.mjs.map +1 -1
- package/dist/esm/utils/toonFormat.mjs +381 -0
- package/dist/esm/utils/toonFormat.mjs.map +1 -0
- package/dist/types/common/enum.d.ts +17 -0
- package/dist/types/graphs/Graph.d.ts +8 -0
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +19 -0
- package/dist/types/types/tools.d.ts +3 -1
- package/dist/types/utils/contextAnalytics.d.ts +37 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/toonFormat.d.ts +111 -0
- package/package.json +3 -2
- package/src/agents/AgentContext.ts +28 -20
- package/src/common/enum.ts +18 -0
- package/src/graphs/Graph.ts +152 -62
- package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +495 -473
- package/src/llm/bedrock/index.ts +47 -35
- package/src/llm/openrouter/index.ts +11 -1
- package/src/llm/vertexai/index.ts +9 -10
- package/src/messages/cache.ts +104 -55
- package/src/messages/core.ts +29 -19
- package/src/messages/format.ts +14 -3
- package/src/messages/tools.ts +20 -13
- package/src/scripts/simple.ts +1 -1
- package/src/specs/emergency-prune.test.ts +407 -355
- package/src/stream.ts +28 -20
- package/src/tools/ProgrammaticToolCalling.ts +246 -52
- package/src/tools/ToolNode.ts +78 -5
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +155 -0
- package/src/tools/search/jina-reranker.test.ts +32 -28
- package/src/tools/search/search.ts +3 -1
- package/src/tools/search/tool.ts +16 -7
- package/src/types/tools.ts +3 -1
- package/src/utils/contextAnalytics.ts +103 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/llmConfig.ts +8 -1
- package/src/utils/run.ts +5 -4
- 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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
const
|
|
170
|
-
model: 'test-model',
|
|
171
|
-
region: 'us-east-1',
|
|
172
|
-
promptCache:
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
const
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
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
|
+
});
|