@@ -136,12 +136,17 @@ const transformAndAppendThinkingMessageItem = (
136
136
} ;
137
137
138
138
const getMessageContent = ( message : Message ) => {
139
- if ( ! message . content && ! message . tool_calls ) return [ ] ;
139
+ if ( ! message . content && ! message . tool_calls && ! message . tool_call_id )
140
+ return [ ] ;
140
141
if ( message . role === 'tool' ) {
142
+ const toolResultContent = getMessageTextContentArray ( message ) ;
141
143
return [
142
144
{
143
145
toolResult : {
144
- content : getMessageTextContentArray ( message ) ,
146
+ ...( toolResultContent . length &&
147
+ ( toolResultContent [ 0 ] as { text : string } ) ?. text
148
+ ? { content : toolResultContent }
149
+ : { content : [ ] } ) , // Bedrock allows empty array but does not allow empty string in content.
145
150
toolUseId : message . tool_call_id ,
146
151
} ,
147
152
} ,
@@ -151,11 +156,11 @@ const getMessageContent = (message: Message) => {
151
156
const inputContent : ContentType [ ] | string | undefined =
152
157
message . content_blocks ?? message . content ;
153
158
// if message is a string, return a single element array with the text
154
- if ( typeof inputContent === 'string' ) {
159
+ if ( typeof inputContent === 'string' && inputContent . trim ( ) ) {
155
160
out . push ( {
156
161
text : inputContent ,
157
162
} ) ;
158
- } else if ( inputContent ) {
163
+ } else if ( inputContent && Array . isArray ( inputContent ) ) {
159
164
inputContent . forEach ( ( item ) => {
160
165
if ( item . type === 'text' ) {
161
166
out . push ( {
@@ -280,7 +285,9 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
280
285
required : false ,
281
286
transform : ( params : BedrockChatCompletionsParams ) => {
282
287
if ( ! params . messages ) return ;
283
- const systemMessages = params . messages . reduce (
288
+ const systemMessages : Array <
289
+ { text : string } | { cachePoint : { type : string } }
290
+ > = params . messages . reduce (
284
291
(
285
292
acc : Array < { text : string } | { cachePoint : { type : string } } > ,
286
293
msg
@@ -321,7 +328,7 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
321
328
}
322
329
} ) ;
323
330
const toolConfig = {
324
- tools : tools ,
331
+ tools,
325
332
} ;
326
333
let toolChoice = undefined ;
327
334
if ( params . tool_choice ) {
@@ -551,7 +558,7 @@ export const BedrockChatCompleteResponseTransform: (
551
558
usage : {
552
559
prompt_tokens : response . usage . inputTokens ,
553
560
completion_tokens : response . usage . outputTokens ,
554
- total_tokens : response . usage . totalTokens ,
561
+ total_tokens : response . usage . totalTokens , // contains the cache usage as well
555
562
...( shouldSendCacheUsage && {
556
563
cache_read_input_tokens : response . usage . cacheReadInputTokens ,
557
564
cache_creation_input_tokens : response . usage . cacheWriteInputTokens ,
@@ -866,7 +873,7 @@ export const BedrockCohereChatCompleteConfig: ProviderConfig = {
866
873
required : true ,
867
874
transform : ( params : Params ) => {
868
875
let prompt : string = '' ;
869
- if ( ! ! params . messages ) {
876
+ if ( params . messages ) {
870
877
let messages : Message [ ] = params . messages ;
871
878
messages . forEach ( ( msg , index ) => {
872
879
if ( index === 0 && SYSTEM_MESSAGE_ROLES . includes ( msg . role ) ) {
@@ -1068,7 +1075,7 @@ export const BedrockAI21ChatCompleteConfig: ProviderConfig = {
1068
1075
required : true ,
1069
1076
transform : ( params : Params ) => {
1070
1077
let prompt : string = '' ;
1071
- if ( ! ! params . messages ) {
1078
+ if ( params . messages ) {
1072
1079
let messages : Message [ ] = params . messages ;
1073
1080
messages . forEach ( ( msg , index ) => {
1074
1081
if ( index === 0 && SYSTEM_MESSAGE_ROLES . includes ( msg . role ) ) {
0 commit comments