@librechat/agents 3.0.80 → 3.0.771
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/graphs/Graph.cjs +5 -19
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +25 -98
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +123 -32
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/messages/core.cjs +1 -1
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/stream.cjs +2 -4
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +5 -9
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +5 -19
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +24 -97
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +123 -32
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/messages/core.mjs +1 -1
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/stream.mjs +2 -4
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +5 -9
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/types/llm/bedrock/index.d.ts +7 -86
- package/dist/types/messages/cache.d.ts +6 -2
- package/dist/types/types/tools.d.ts +0 -2
- package/package.json +3 -6
- package/src/graphs/Graph.ts +5 -23
- package/src/llm/bedrock/index.ts +43 -180
- package/src/messages/cache.test.ts +215 -0
- package/src/messages/cache.ts +172 -43
- package/src/messages/core.ts +1 -1
- package/src/scripts/thinking.ts +18 -39
- package/src/scripts/tools.ts +3 -7
- package/src/stream.ts +2 -4
- package/src/tools/ToolNode.ts +5 -9
- package/src/types/tools.ts +0 -2
- package/dist/types/llm/bedrock/types.d.ts +0 -27
- package/dist/types/llm/bedrock/utils/index.d.ts +0 -5
- package/dist/types/llm/bedrock/utils/message_inputs.d.ts +0 -31
- package/dist/types/llm/bedrock/utils/message_outputs.d.ts +0 -33
- package/src/llm/bedrock/llm.spec.ts +0 -616
- package/src/llm/bedrock/types.ts +0 -51
- package/src/llm/bedrock/utils/index.ts +0 -18
- package/src/llm/bedrock/utils/message_inputs.ts +0 -563
- package/src/llm/bedrock/utils/message_outputs.ts +0 -310
- package/src/scripts/code_exec_multi_session.ts +0 -241
- package/src/scripts/thinking-bedrock.ts +0 -159
package/src/llm/bedrock/index.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts
|
|
3
|
-
* and adds support for latest @langchain/aws features:
|
|
4
|
-
*
|
|
5
|
-
* - Application Inference Profiles (PR #9129)
|
|
6
|
-
* - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+
|
|
7
3
|
*
|
|
8
4
|
* Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,
|
|
9
5
|
* causing LangChain's merge logic to fail with "field[contentBlockIndex] already exists"
|
|
@@ -16,74 +12,15 @@
|
|
|
16
12
|
*/
|
|
17
13
|
|
|
18
14
|
import { ChatBedrockConverse } from '@langchain/aws';
|
|
19
|
-
import { AIMessageChunk } from '@langchain/core/messages';
|
|
20
|
-
import { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';
|
|
21
|
-
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
22
15
|
import type { ChatBedrockConverseInput } from '@langchain/aws';
|
|
16
|
+
import { AIMessageChunk } from '@langchain/core/messages';
|
|
23
17
|
import type { BaseMessage } from '@langchain/core/messages';
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
* Service tier type for Bedrock invocations.
|
|
27
|
-
* Requires AWS SDK >= 3.966.0 to actually work.
|
|
28
|
-
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
|
|
29
|
-
*/
|
|
30
|
-
export type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Extended input interface with additional features:
|
|
34
|
-
* - applicationInferenceProfile: Use an inference profile ARN instead of model ID
|
|
35
|
-
* - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)
|
|
36
|
-
*/
|
|
37
|
-
export interface CustomChatBedrockConverseInput
|
|
38
|
-
extends ChatBedrockConverseInput {
|
|
39
|
-
/**
|
|
40
|
-
* Application Inference Profile ARN to use for the model.
|
|
41
|
-
* For example, "arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx"
|
|
42
|
-
* When provided, this ARN will be used for the actual inference calls instead of the model ID.
|
|
43
|
-
* Must still provide `model` as normal modelId to benefit from all the metadata.
|
|
44
|
-
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html
|
|
45
|
-
*/
|
|
46
|
-
applicationInferenceProfile?: string;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Service tier for model invocation.
|
|
50
|
-
* Specifies the processing tier type used for serving the request.
|
|
51
|
-
* Supported values are 'priority', 'default', 'flex', and 'reserved'.
|
|
52
|
-
*
|
|
53
|
-
* - 'priority': Prioritized processing for lower latency
|
|
54
|
-
* - 'default': Standard processing tier
|
|
55
|
-
* - 'flex': Flexible processing tier with lower cost
|
|
56
|
-
* - 'reserved': Reserved capacity for consistent performance
|
|
57
|
-
*
|
|
58
|
-
* If not provided, AWS uses the default tier.
|
|
59
|
-
* Note: Requires AWS SDK >= 3.966.0 to work.
|
|
60
|
-
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
|
|
61
|
-
*/
|
|
62
|
-
serviceTier?: ServiceTierType;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Extended call options with serviceTier override support.
|
|
67
|
-
*/
|
|
68
|
-
export interface CustomChatBedrockConverseCallOptions {
|
|
69
|
-
serviceTier?: ServiceTierType;
|
|
70
|
-
}
|
|
18
|
+
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
19
|
+
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
71
20
|
|
|
72
21
|
export class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
73
|
-
|
|
74
|
-
* Application Inference Profile ARN to use instead of model ID.
|
|
75
|
-
*/
|
|
76
|
-
applicationInferenceProfile?: string;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Service tier for model invocation.
|
|
80
|
-
*/
|
|
81
|
-
serviceTier?: ServiceTierType;
|
|
82
|
-
|
|
83
|
-
constructor(fields?: CustomChatBedrockConverseInput) {
|
|
22
|
+
constructor(fields?: ChatBedrockConverseInput) {
|
|
84
23
|
super(fields);
|
|
85
|
-
this.applicationInferenceProfile = fields?.applicationInferenceProfile;
|
|
86
|
-
this.serviceTier = fields?.serviceTier;
|
|
87
24
|
}
|
|
88
25
|
|
|
89
26
|
static lc_name(): string {
|
|
@@ -91,126 +28,52 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
|
91
28
|
}
|
|
92
29
|
|
|
93
30
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
|
|
97
|
-
protected getModelId(): string {
|
|
98
|
-
return this.applicationInferenceProfile ?? this.model;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Override invocationParams to add serviceTier support.
|
|
103
|
-
*/
|
|
104
|
-
override invocationParams(
|
|
105
|
-
options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions
|
|
106
|
-
): ReturnType<ChatBedrockConverse['invocationParams']> & {
|
|
107
|
-
serviceTier?: { type: ServiceTierType };
|
|
108
|
-
} {
|
|
109
|
-
const baseParams = super.invocationParams(options);
|
|
110
|
-
|
|
111
|
-
/** Service tier from options or fall back to class-level setting */
|
|
112
|
-
const serviceTierType = options?.serviceTier ?? this.serviceTier;
|
|
113
|
-
|
|
114
|
-
return {
|
|
115
|
-
...baseParams,
|
|
116
|
-
serviceTier: serviceTierType ? { type: serviceTierType } : undefined,
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Override _generateNonStreaming to use applicationInferenceProfile as modelId.
|
|
122
|
-
* Uses the same model-swapping pattern as streaming for consistency.
|
|
31
|
+
* Override _streamResponseChunks to strip contentBlockIndex from response_metadata
|
|
32
|
+
* This prevents LangChain's merge conflicts when the same index is used for
|
|
33
|
+
* different content types (text vs tool calls)
|
|
123
34
|
*/
|
|
124
|
-
|
|
35
|
+
async *_streamResponseChunks(
|
|
125
36
|
messages: BaseMessage[],
|
|
126
|
-
options: this['ParsedCallOptions']
|
|
127
|
-
runManager?: CallbackManagerForLLMRun
|
|
128
|
-
): Promise<ChatResult> {
|
|
129
|
-
// Temporarily swap model for applicationInferenceProfile support
|
|
130
|
-
const originalModel = this.model;
|
|
131
|
-
if (
|
|
132
|
-
this.applicationInferenceProfile != null &&
|
|
133
|
-
this.applicationInferenceProfile !== ''
|
|
134
|
-
) {
|
|
135
|
-
this.model = this.applicationInferenceProfile;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
try {
|
|
139
|
-
return await super._generateNonStreaming(messages, options, runManager);
|
|
140
|
-
} finally {
|
|
141
|
-
// Restore original model
|
|
142
|
-
this.model = originalModel;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Override _streamResponseChunks to:
|
|
148
|
-
* 1. Use applicationInferenceProfile as modelId (by temporarily swapping this.model)
|
|
149
|
-
* 2. Strip contentBlockIndex from response_metadata to prevent merge conflicts
|
|
150
|
-
*
|
|
151
|
-
* Note: We delegate to super._streamResponseChunks() to preserve @langchain/aws's
|
|
152
|
-
* internal chunk handling which correctly preserves array content for reasoning blocks.
|
|
153
|
-
*/
|
|
154
|
-
override async *_streamResponseChunks(
|
|
155
|
-
messages: BaseMessage[],
|
|
156
|
-
options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,
|
|
37
|
+
options: this['ParsedCallOptions'],
|
|
157
38
|
runManager?: CallbackManagerForLLMRun
|
|
158
39
|
): AsyncGenerator<ChatGenerationChunk> {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
40
|
+
const baseStream = super._streamResponseChunks(
|
|
41
|
+
messages,
|
|
42
|
+
options,
|
|
43
|
+
runManager
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
for await (const chunk of baseStream) {
|
|
47
|
+
// Only process if we have response_metadata
|
|
48
|
+
if (
|
|
49
|
+
chunk.message instanceof AIMessageChunk &&
|
|
50
|
+
(chunk.message as Partial<AIMessageChunk>).response_metadata &&
|
|
51
|
+
typeof chunk.message.response_metadata === 'object'
|
|
52
|
+
) {
|
|
53
|
+
// Check if contentBlockIndex exists anywhere in response_metadata (top level or nested)
|
|
54
|
+
const hasContentBlockIndex = this.hasContentBlockIndex(
|
|
55
|
+
chunk.message.response_metadata
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
if (hasContentBlockIndex) {
|
|
59
|
+
const cleanedMetadata = this.removeContentBlockIndex(
|
|
60
|
+
chunk.message.response_metadata
|
|
61
|
+
) as Record<string, unknown>;
|
|
62
|
+
|
|
63
|
+
yield new ChatGenerationChunk({
|
|
64
|
+
text: chunk.text,
|
|
65
|
+
message: new AIMessageChunk({
|
|
66
|
+
...chunk.message,
|
|
67
|
+
response_metadata: cleanedMetadata,
|
|
68
|
+
}),
|
|
69
|
+
generationInfo: chunk.generationInfo,
|
|
70
|
+
});
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
179
73
|
}
|
|
180
|
-
} finally {
|
|
181
|
-
// Restore original model
|
|
182
|
-
this.model = originalModel;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
74
|
|
|
186
|
-
|
|
187
|
-
* Clean a chunk by removing contentBlockIndex from response_metadata.
|
|
188
|
-
*/
|
|
189
|
-
private cleanChunk(chunk: ChatGenerationChunk): ChatGenerationChunk {
|
|
190
|
-
const message = chunk.message;
|
|
191
|
-
if (!(message instanceof AIMessageChunk)) {
|
|
192
|
-
return chunk;
|
|
75
|
+
yield chunk;
|
|
193
76
|
}
|
|
194
|
-
|
|
195
|
-
const metadata = message.response_metadata as Record<string, unknown>;
|
|
196
|
-
const hasContentBlockIndex = this.hasContentBlockIndex(metadata);
|
|
197
|
-
if (!hasContentBlockIndex) {
|
|
198
|
-
return chunk;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const cleanedMetadata = this.removeContentBlockIndex(metadata) as Record<
|
|
202
|
-
string,
|
|
203
|
-
unknown
|
|
204
|
-
>;
|
|
205
|
-
|
|
206
|
-
return new ChatGenerationChunk({
|
|
207
|
-
text: chunk.text,
|
|
208
|
-
message: new AIMessageChunk({
|
|
209
|
-
...message,
|
|
210
|
-
response_metadata: cleanedMetadata,
|
|
211
|
-
}),
|
|
212
|
-
generationInfo: chunk.generationInfo,
|
|
213
|
-
});
|
|
214
77
|
}
|
|
215
78
|
|
|
216
79
|
/**
|
|
@@ -835,6 +835,221 @@ describe('Multi-agent provider interoperability', () => {
|
|
|
835
835
|
});
|
|
836
836
|
});
|
|
837
837
|
|
|
838
|
+
describe('Immutability - addCacheControl does not mutate original messages', () => {
|
|
839
|
+
it('should not mutate original messages when adding cache control to string content', () => {
|
|
840
|
+
const originalMessages: TestMsg[] = [
|
|
841
|
+
{ role: 'user', content: 'Hello' },
|
|
842
|
+
{ role: 'assistant', content: 'Hi there' },
|
|
843
|
+
{ role: 'user', content: 'How are you?' },
|
|
844
|
+
];
|
|
845
|
+
|
|
846
|
+
const originalFirstContent = originalMessages[0].content;
|
|
847
|
+
const originalThirdContent = originalMessages[2].content;
|
|
848
|
+
|
|
849
|
+
const result = addCacheControl(originalMessages as never);
|
|
850
|
+
|
|
851
|
+
expect(originalMessages[0].content).toBe(originalFirstContent);
|
|
852
|
+
expect(originalMessages[2].content).toBe(originalThirdContent);
|
|
853
|
+
expect(typeof originalMessages[0].content).toBe('string');
|
|
854
|
+
expect(typeof originalMessages[2].content).toBe('string');
|
|
855
|
+
|
|
856
|
+
expect(Array.isArray(result[0].content)).toBe(true);
|
|
857
|
+
expect(Array.isArray(result[2].content)).toBe(true);
|
|
858
|
+
});
|
|
859
|
+
|
|
860
|
+
it('should not mutate original messages when adding cache control to array content', () => {
|
|
861
|
+
const originalMessages: TestMsg[] = [
|
|
862
|
+
{
|
|
863
|
+
role: 'user',
|
|
864
|
+
content: [{ type: ContentTypes.TEXT, text: 'Hello' }],
|
|
865
|
+
},
|
|
866
|
+
{ role: 'assistant', content: 'Hi there' },
|
|
867
|
+
{
|
|
868
|
+
role: 'user',
|
|
869
|
+
content: [{ type: ContentTypes.TEXT, text: 'How are you?' }],
|
|
870
|
+
},
|
|
871
|
+
];
|
|
872
|
+
|
|
873
|
+
const originalFirstBlock = {
|
|
874
|
+
...(originalMessages[0].content as MessageContentComplex[])[0],
|
|
875
|
+
};
|
|
876
|
+
const originalThirdBlock = {
|
|
877
|
+
...(originalMessages[2].content as MessageContentComplex[])[0],
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
const result = addCacheControl(originalMessages as never);
|
|
881
|
+
|
|
882
|
+
const firstContent = originalMessages[0].content as MessageContentComplex[];
|
|
883
|
+
const thirdContent = originalMessages[2].content as MessageContentComplex[];
|
|
884
|
+
|
|
885
|
+
expect('cache_control' in firstContent[0]).toBe(false);
|
|
886
|
+
expect('cache_control' in thirdContent[0]).toBe(false);
|
|
887
|
+
expect(firstContent[0]).toEqual(originalFirstBlock);
|
|
888
|
+
expect(thirdContent[0]).toEqual(originalThirdBlock);
|
|
889
|
+
|
|
890
|
+
const resultFirstContent = result[0].content as MessageContentComplex[];
|
|
891
|
+
const resultThirdContent = result[2].content as MessageContentComplex[];
|
|
892
|
+
expect('cache_control' in resultFirstContent[0]).toBe(true);
|
|
893
|
+
expect('cache_control' in resultThirdContent[0]).toBe(true);
|
|
894
|
+
});
|
|
895
|
+
|
|
896
|
+
it('should not mutate original messages when stripping existing cache control', () => {
|
|
897
|
+
const originalMessages: TestMsg[] = [
|
|
898
|
+
{
|
|
899
|
+
role: 'user',
|
|
900
|
+
content: [
|
|
901
|
+
{
|
|
902
|
+
type: ContentTypes.TEXT,
|
|
903
|
+
text: 'Hello',
|
|
904
|
+
cache_control: { type: 'ephemeral' },
|
|
905
|
+
} as MessageContentComplex,
|
|
906
|
+
],
|
|
907
|
+
},
|
|
908
|
+
{ role: 'assistant', content: 'Hi there' },
|
|
909
|
+
{
|
|
910
|
+
role: 'user',
|
|
911
|
+
content: [{ type: ContentTypes.TEXT, text: 'How are you?' }],
|
|
912
|
+
},
|
|
913
|
+
];
|
|
914
|
+
|
|
915
|
+
const originalFirstBlock = (
|
|
916
|
+
originalMessages[0].content as MessageContentComplex[]
|
|
917
|
+
)[0];
|
|
918
|
+
|
|
919
|
+
addCacheControl(originalMessages as never);
|
|
920
|
+
|
|
921
|
+
expect('cache_control' in originalFirstBlock).toBe(true);
|
|
922
|
+
});
|
|
923
|
+
});
|
|
924
|
+
|
|
925
|
+
describe('Immutability - addBedrockCacheControl does not mutate original messages', () => {
|
|
926
|
+
it('should not mutate original messages when adding cache points to string content', () => {
|
|
927
|
+
const originalMessages: TestMsg[] = [
|
|
928
|
+
{ role: 'user', content: 'Hello' },
|
|
929
|
+
{ role: 'assistant', content: 'Hi there' },
|
|
930
|
+
];
|
|
931
|
+
|
|
932
|
+
const originalFirstContent = originalMessages[0].content;
|
|
933
|
+
const originalSecondContent = originalMessages[1].content;
|
|
934
|
+
|
|
935
|
+
const result = addBedrockCacheControl(originalMessages);
|
|
936
|
+
|
|
937
|
+
expect(originalMessages[0].content).toBe(originalFirstContent);
|
|
938
|
+
expect(originalMessages[1].content).toBe(originalSecondContent);
|
|
939
|
+
expect(typeof originalMessages[0].content).toBe('string');
|
|
940
|
+
expect(typeof originalMessages[1].content).toBe('string');
|
|
941
|
+
|
|
942
|
+
expect(Array.isArray(result[0].content)).toBe(true);
|
|
943
|
+
expect(Array.isArray(result[1].content)).toBe(true);
|
|
944
|
+
});
|
|
945
|
+
|
|
946
|
+
it('should not mutate original messages when adding cache points to array content', () => {
|
|
947
|
+
const originalMessages: TestMsg[] = [
|
|
948
|
+
{
|
|
949
|
+
role: 'user',
|
|
950
|
+
content: [{ type: ContentTypes.TEXT, text: 'Hello' }],
|
|
951
|
+
},
|
|
952
|
+
{
|
|
953
|
+
role: 'assistant',
|
|
954
|
+
content: [{ type: ContentTypes.TEXT, text: 'Hi there' }],
|
|
955
|
+
},
|
|
956
|
+
];
|
|
957
|
+
|
|
958
|
+
const originalFirstContentLength = (
|
|
959
|
+
originalMessages[0].content as MessageContentComplex[]
|
|
960
|
+
).length;
|
|
961
|
+
const originalSecondContentLength = (
|
|
962
|
+
originalMessages[1].content as MessageContentComplex[]
|
|
963
|
+
).length;
|
|
964
|
+
|
|
965
|
+
const result = addBedrockCacheControl(originalMessages);
|
|
966
|
+
|
|
967
|
+
const firstContent = originalMessages[0].content as MessageContentComplex[];
|
|
968
|
+
const secondContent = originalMessages[1]
|
|
969
|
+
.content as MessageContentComplex[];
|
|
970
|
+
|
|
971
|
+
expect(firstContent.length).toBe(originalFirstContentLength);
|
|
972
|
+
expect(secondContent.length).toBe(originalSecondContentLength);
|
|
973
|
+
expect(firstContent.some((b) => 'cachePoint' in b)).toBe(false);
|
|
974
|
+
expect(secondContent.some((b) => 'cachePoint' in b)).toBe(false);
|
|
975
|
+
|
|
976
|
+
const resultFirstContent = result[0].content as MessageContentComplex[];
|
|
977
|
+
const resultSecondContent = result[1].content as MessageContentComplex[];
|
|
978
|
+
expect(resultFirstContent.length).toBe(originalFirstContentLength + 1);
|
|
979
|
+
expect(resultSecondContent.length).toBe(originalSecondContentLength + 1);
|
|
980
|
+
expect(resultFirstContent.some((b) => 'cachePoint' in b)).toBe(true);
|
|
981
|
+
expect(resultSecondContent.some((b) => 'cachePoint' in b)).toBe(true);
|
|
982
|
+
});
|
|
983
|
+
|
|
984
|
+
it('should not mutate original messages when stripping existing cache control', () => {
|
|
985
|
+
const originalMessages: TestMsg[] = [
|
|
986
|
+
{
|
|
987
|
+
role: 'user',
|
|
988
|
+
content: [
|
|
989
|
+
{
|
|
990
|
+
type: ContentTypes.TEXT,
|
|
991
|
+
text: 'Hello',
|
|
992
|
+
cache_control: { type: 'ephemeral' },
|
|
993
|
+
} as MessageContentComplex,
|
|
994
|
+
],
|
|
995
|
+
},
|
|
996
|
+
{
|
|
997
|
+
role: 'assistant',
|
|
998
|
+
content: [
|
|
999
|
+
{ type: ContentTypes.TEXT, text: 'Hi there' },
|
|
1000
|
+
{ cachePoint: { type: 'default' } },
|
|
1001
|
+
],
|
|
1002
|
+
},
|
|
1003
|
+
];
|
|
1004
|
+
|
|
1005
|
+
const originalFirstBlock = (
|
|
1006
|
+
originalMessages[0].content as MessageContentComplex[]
|
|
1007
|
+
)[0];
|
|
1008
|
+
const originalSecondContentLength = (
|
|
1009
|
+
originalMessages[1].content as MessageContentComplex[]
|
|
1010
|
+
).length;
|
|
1011
|
+
|
|
1012
|
+
addBedrockCacheControl(originalMessages);
|
|
1013
|
+
|
|
1014
|
+
expect('cache_control' in originalFirstBlock).toBe(true);
|
|
1015
|
+
expect(
|
|
1016
|
+
(originalMessages[1].content as MessageContentComplex[]).length
|
|
1017
|
+
).toBe(originalSecondContentLength);
|
|
1018
|
+
});
|
|
1019
|
+
|
|
1020
|
+
it('should allow different providers to process same messages without cross-contamination', () => {
|
|
1021
|
+
const sharedMessages: TestMsg[] = [
|
|
1022
|
+
{
|
|
1023
|
+
role: 'user',
|
|
1024
|
+
content: [{ type: ContentTypes.TEXT, text: 'Shared message 1' }],
|
|
1025
|
+
},
|
|
1026
|
+
{
|
|
1027
|
+
role: 'assistant',
|
|
1028
|
+
content: [{ type: ContentTypes.TEXT, text: 'Shared response 1' }],
|
|
1029
|
+
},
|
|
1030
|
+
];
|
|
1031
|
+
|
|
1032
|
+
const bedrockResult = addBedrockCacheControl(sharedMessages);
|
|
1033
|
+
|
|
1034
|
+
const anthropicResult = addCacheControl(sharedMessages as never);
|
|
1035
|
+
|
|
1036
|
+
const originalFirstContent = sharedMessages[0]
|
|
1037
|
+
.content as MessageContentComplex[];
|
|
1038
|
+
expect(originalFirstContent.some((b) => 'cachePoint' in b)).toBe(false);
|
|
1039
|
+
expect('cache_control' in originalFirstContent[0]).toBe(false);
|
|
1040
|
+
|
|
1041
|
+
const bedrockFirstContent = bedrockResult[0]
|
|
1042
|
+
.content as MessageContentComplex[];
|
|
1043
|
+
expect(bedrockFirstContent.some((b) => 'cachePoint' in b)).toBe(true);
|
|
1044
|
+
expect('cache_control' in bedrockFirstContent[0]).toBe(false);
|
|
1045
|
+
|
|
1046
|
+
const anthropicFirstContent = anthropicResult[0]
|
|
1047
|
+
.content as MessageContentComplex[];
|
|
1048
|
+
expect(anthropicFirstContent.some((b) => 'cachePoint' in b)).toBe(false);
|
|
1049
|
+
expect('cache_control' in anthropicFirstContent[0]).toBe(true);
|
|
1050
|
+
});
|
|
1051
|
+
});
|
|
1052
|
+
|
|
838
1053
|
describe('Multi-turn cache cleanup', () => {
|
|
839
1054
|
it('strips stale Bedrock cache points from previous turns before applying new ones', () => {
|
|
840
1055
|
const messages: TestMsg[] = [
|