@@ -8,11 +8,7 @@ import {
8
8
ToolResultBlockParam ,
9
9
ToolUseBlockParam ,
10
10
} from '../../types/MessagesRequest' ;
11
- import {
12
- ContentBlock ,
13
- MessagesResponse ,
14
- ANTHROPIC_STOP_REASON ,
15
- } from '../../types/messagesResponse' ;
11
+ import { ContentBlock , MessagesResponse } from '../../types/messagesResponse' ;
16
12
import { RawContentBlockDeltaEvent } from '../../types/MessagesStreamResponse' ;
17
13
import {
18
14
ANTHROPIC_CONTENT_BLOCK_START_EVENT ,
@@ -41,15 +37,20 @@ import {
41
37
transformToolsConfig as transformToolConfig ,
42
38
} from './utils/messagesUtils' ;
43
39
44
- const transformTextBlock = ( textBlock : TextBlockParam ) => {
45
- return {
40
+ const appendTextBlock = (
41
+ transformedContent : any [ ] ,
42
+ textBlock : TextBlockParam
43
+ ) => {
44
+ transformedContent . push ( {
46
45
text : textBlock . text ,
47
- ...( textBlock . cache_control && {
46
+ } ) ;
47
+ if ( textBlock . cache_control ) {
48
+ transformedContent . push ( {
48
49
cachePoint : {
49
50
type : 'default' ,
50
51
} ,
51
- } ) ,
52
- } ;
52
+ } ) ;
53
+ }
53
54
} ;
54
55
55
56
const appendImageBlock = (
@@ -64,12 +65,14 @@ const appendImageBlock = (
64
65
bytes : imageBlock . source . data ,
65
66
} ,
66
67
} ,
67
- ...( imageBlock . cache_control && {
68
+ } ) ;
69
+ if ( imageBlock . cache_control ) {
70
+ transformedContent . push ( {
68
71
cachePoint : {
69
72
type : 'default' ,
70
73
} ,
71
- } ) ,
72
- } ) ;
74
+ } ) ;
75
+ }
73
76
} else if ( imageBlock . source . type === 'url' ) {
74
77
transformedContent . push ( {
75
78
image : {
@@ -80,12 +83,14 @@ const appendImageBlock = (
80
83
} ,
81
84
} ,
82
85
} ,
83
- ...( imageBlock . cache_control && {
86
+ } ) ;
87
+ if ( imageBlock . cache_control ) {
88
+ transformedContent . push ( {
84
89
cachePoint : {
85
90
type : 'default' ,
86
91
} ,
87
- } ) ,
88
- } ) ;
92
+ } ) ;
93
+ }
89
94
} else if ( imageBlock . source . type === 'file' ) {
90
95
// not supported
91
96
}
@@ -103,12 +108,14 @@ const appendDocumentBlock = (
103
108
bytes : documentBlock . source . data ,
104
109
} ,
105
110
} ,
106
- ...( documentBlock . cache_control && {
111
+ } ) ;
112
+ if ( documentBlock . cache_control ) {
113
+ transformedContent . push ( {
107
114
cachePoint : {
108
115
type : 'default' ,
109
116
} ,
110
- } ) ,
111
- } ) ;
117
+ } ) ;
118
+ }
112
119
} else if ( documentBlock . source . type === 'url' ) {
113
120
transformedContent . push ( {
114
121
document : {
@@ -119,12 +126,14 @@ const appendDocumentBlock = (
119
126
} ,
120
127
} ,
121
128
} ,
122
- ...( documentBlock . cache_control && {
129
+ } ) ;
130
+ if ( documentBlock . cache_control ) {
131
+ transformedContent . push ( {
123
132
cachePoint : {
124
133
type : 'default' ,
125
134
} ,
126
- } ) ,
127
- } ) ;
135
+ } ) ;
136
+ }
128
137
}
129
138
} ;
130
139
@@ -157,18 +166,20 @@ const appendToolUseBlock = (
157
166
transformedContent : any [ ] ,
158
167
toolUseBlock : ToolUseBlockParam
159
168
) => {
160
- return {
169
+ transformedContent . push ( {
161
170
toolUse : {
162
171
input : toolUseBlock . input ,
163
172
name : toolUseBlock . name ,
164
173
toolUseId : toolUseBlock . id ,
165
174
} ,
166
- ...( toolUseBlock . cache_control && {
175
+ } ) ;
176
+ if ( toolUseBlock . cache_control ) {
177
+ transformedContent . push ( {
167
178
cachePoint : {
168
179
type : 'default' ,
169
180
} ,
170
- } ) ,
171
- } ;
181
+ } ) ;
182
+ }
172
183
} ;
173
184
174
185
const appendToolResultBlock = (
@@ -192,18 +203,20 @@ const appendToolResultBlock = (
192
203
}
193
204
}
194
205
}
195
- return {
206
+ transformedContent . push ( {
196
207
toolResult : {
197
208
toolUseId : toolResultBlock . tool_use_id ,
198
209
status : toolResultBlock . is_error ? 'error' : 'success' ,
199
210
content : transformedToolResultContent ,
200
211
} ,
201
- ...( toolResultBlock . cache_control && {
212
+ } ) ;
213
+ if ( toolResultBlock . cache_control ) {
214
+ transformedContent . push ( {
202
215
cachePoint : {
203
216
type : 'default' ,
204
217
} ,
205
- } ) ,
206
- } ;
218
+ } ) ;
219
+ }
207
220
} ;
208
221
209
222
export const BedrockConverseMessagesConfig : ProviderConfig = {
@@ -233,7 +246,7 @@ export const BedrockConverseMessagesConfig: ProviderConfig = {
233
246
const transformedContent : any [ ] = [ ] ;
234
247
for ( const content of message . content ) {
235
248
if ( content . type === 'text' ) {
236
- transformedContent . push ( transformTextBlock ( content ) ) ;
249
+ appendTextBlock ( transformedContent , content ) ;
237
250
} else if ( content . type === 'image' ) {
238
251
appendImageBlock ( transformedContent , content ) ;
239
252
} else if ( content . type === 'document' ) {
@@ -287,14 +300,20 @@ export const BedrockConverseMessagesConfig: ProviderConfig = {
287
300
} ,
288
301
] ;
289
302
} 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 ;
298
317
}
299
318
} ,
300
319
} ,
0 commit comments