Skip to content

Commit ee21746

Browse files
committed
fix cache control transformations
1 parent b902296 commit ee21746

File tree

1 file changed

+58
-39
lines changed

1 file changed

+58
-39
lines changed

src/providers/bedrock/messages.ts

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import {
88
ToolResultBlockParam,
99
ToolUseBlockParam,
1010
} from '../../types/MessagesRequest';
11-
import {
12-
ContentBlock,
13-
MessagesResponse,
14-
ANTHROPIC_STOP_REASON,
15-
} from '../../types/messagesResponse';
11+
import { ContentBlock, MessagesResponse } from '../../types/messagesResponse';
1612
import { RawContentBlockDeltaEvent } from '../../types/MessagesStreamResponse';
1713
import {
1814
ANTHROPIC_CONTENT_BLOCK_START_EVENT,
@@ -41,15 +37,20 @@ import {
4137
transformToolsConfig as transformToolConfig,
4238
} from './utils/messagesUtils';
4339

44-
const transformTextBlock = (textBlock: TextBlockParam) => {
45-
return {
40+
const appendTextBlock = (
41+
transformedContent: any[],
42+
textBlock: TextBlockParam
43+
) => {
44+
transformedContent.push({
4645
text: textBlock.text,
47-
...(textBlock.cache_control && {
46+
});
47+
if (textBlock.cache_control) {
48+
transformedContent.push({
4849
cachePoint: {
4950
type: 'default',
5051
},
51-
}),
52-
};
52+
});
53+
}
5354
};
5455

5556
const appendImageBlock = (
@@ -64,12 +65,14 @@ const appendImageBlock = (
6465
bytes: imageBlock.source.data,
6566
},
6667
},
67-
...(imageBlock.cache_control && {
68+
});
69+
if (imageBlock.cache_control) {
70+
transformedContent.push({
6871
cachePoint: {
6972
type: 'default',
7073
},
71-
}),
72-
});
74+
});
75+
}
7376
} else if (imageBlock.source.type === 'url') {
7477
transformedContent.push({
7578
image: {
@@ -80,12 +83,14 @@ const appendImageBlock = (
8083
},
8184
},
8285
},
83-
...(imageBlock.cache_control && {
86+
});
87+
if (imageBlock.cache_control) {
88+
transformedContent.push({
8489
cachePoint: {
8590
type: 'default',
8691
},
87-
}),
88-
});
92+
});
93+
}
8994
} else if (imageBlock.source.type === 'file') {
9095
// not supported
9196
}
@@ -103,12 +108,14 @@ const appendDocumentBlock = (
103108
bytes: documentBlock.source.data,
104109
},
105110
},
106-
...(documentBlock.cache_control && {
111+
});
112+
if (documentBlock.cache_control) {
113+
transformedContent.push({
107114
cachePoint: {
108115
type: 'default',
109116
},
110-
}),
111-
});
117+
});
118+
}
112119
} else if (documentBlock.source.type === 'url') {
113120
transformedContent.push({
114121
document: {
@@ -119,12 +126,14 @@ const appendDocumentBlock = (
119126
},
120127
},
121128
},
122-
...(documentBlock.cache_control && {
129+
});
130+
if (documentBlock.cache_control) {
131+
transformedContent.push({
123132
cachePoint: {
124133
type: 'default',
125134
},
126-
}),
127-
});
135+
});
136+
}
128137
}
129138
};
130139

@@ -157,18 +166,20 @@ const appendToolUseBlock = (
157166
transformedContent: any[],
158167
toolUseBlock: ToolUseBlockParam
159168
) => {
160-
return {
169+
transformedContent.push({
161170
toolUse: {
162171
input: toolUseBlock.input,
163172
name: toolUseBlock.name,
164173
toolUseId: toolUseBlock.id,
165174
},
166-
...(toolUseBlock.cache_control && {
175+
});
176+
if (toolUseBlock.cache_control) {
177+
transformedContent.push({
167178
cachePoint: {
168179
type: 'default',
169180
},
170-
}),
171-
};
181+
});
182+
}
172183
};
173184

174185
const appendToolResultBlock = (
@@ -192,18 +203,20 @@ const appendToolResultBlock = (
192203
}
193204
}
194205
}
195-
return {
206+
transformedContent.push({
196207
toolResult: {
197208
toolUseId: toolResultBlock.tool_use_id,
198209
status: toolResultBlock.is_error ? 'error' : 'success',
199210
content: transformedToolResultContent,
200211
},
201-
...(toolResultBlock.cache_control && {
212+
});
213+
if (toolResultBlock.cache_control) {
214+
transformedContent.push({
202215
cachePoint: {
203216
type: 'default',
204217
},
205-
}),
206-
};
218+
});
219+
}
207220
};
208221

209222
export const BedrockConverseMessagesConfig: ProviderConfig = {
@@ -233,7 +246,7 @@ export const BedrockConverseMessagesConfig: ProviderConfig = {
233246
const transformedContent: any[] = [];
234247
for (const content of message.content) {
235248
if (content.type === 'text') {
236-
transformedContent.push(transformTextBlock(content));
249+
appendTextBlock(transformedContent, content);
237250
} else if (content.type === 'image') {
238251
appendImageBlock(transformedContent, content);
239252
} else if (content.type === 'document') {
@@ -287,14 +300,20 @@ export const BedrockConverseMessagesConfig: ProviderConfig = {
287300
},
288301
];
289302
} else if (Array.isArray(system)) {
290-
return system.map((item) => ({
291-
text: item.text,
292-
...(item.cache_control && {
293-
cachePoint: {
294-
type: 'default',
295-
},
296-
}),
297-
}));
303+
const transformedSystem: any[] = [];
304+
system.forEach((item) => {
305+
transformedSystem.push({
306+
text: item.text,
307+
});
308+
if (item.cache_control) {
309+
transformedSystem.push({
310+
cachePoint: {
311+
type: 'default',
312+
},
313+
});
314+
}
315+
});
316+
return transformedSystem;
298317
}
299318
},
300319
},

0 commit comments

Comments
 (0)