@@ -69,15 +69,30 @@ export interface BedrockConverseAI21ChatCompletionsParams
6969 countPenalty ?: number ;
7070}
7171
72- const getMessageTextContentArray = ( message : Message ) : { text : string } [ ] => {
72+ const getMessageTextContentArray = (
73+ message : Message
74+ ) : Array < { text : string } | { cachePoint : { type : string } } > => {
7375 if ( message . content && typeof message . content === 'object' ) {
74- return message . content
75- . filter ( ( item ) => item . type === 'text' )
76- . map ( ( item ) => {
77- return {
78- text : item . text || '' ,
79- } ;
76+ const filteredContentMessages = message . content . filter (
77+ ( item ) => item . type === 'text'
78+ ) ;
79+ const finalContent : Array <
80+ { text : string } | { cachePoint : { type : string } }
81+ > = [ ] ;
82+ filteredContentMessages . forEach ( ( item ) => {
83+ finalContent . push ( {
84+ text : item . text || '' ,
8085 } ) ;
86+ // push a cache point.
87+ if ( item . cache_control ) {
88+ finalContent . push ( {
89+ cachePoint : {
90+ type : 'default' ,
91+ } ,
92+ } ) ;
93+ }
94+ } ) ;
95+ return finalContent ;
8196 }
8297 return [
8398 {
@@ -162,6 +177,15 @@ const getMessageContent = (message: Message) => {
162177 } ) ;
163178 }
164179 }
180+
181+ if ( item . cache_control ) {
182+ // if content item has `cache_control`, push the cache point to the out array
183+ out . push ( {
184+ cachePoint : {
185+ type : 'default' ,
186+ } ,
187+ } ) ;
188+ }
165189 } ) ;
166190 }
167191
@@ -219,7 +243,10 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
219243 transform : ( params : BedrockChatCompletionsParams ) => {
220244 if ( ! params . messages ) return ;
221245 const systemMessages = params . messages . reduce (
222- ( acc : { text : string } [ ] , msg ) => {
246+ (
247+ acc : Array < { text : string } | { cachePoint : { type : string } } > ,
248+ msg
249+ ) => {
223250 if ( SYSTEM_MESSAGE_ROLES . includes ( msg . role ) )
224251 return acc . concat ( ...getMessageTextContentArray ( msg ) ) ;
225252 return acc ;
@@ -234,17 +261,29 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
234261 tools : {
235262 param : 'toolConfig' ,
236263 transform : ( params : BedrockChatCompletionsParams ) => {
237- const toolConfig = {
238- tools : params . tools ?. map ( ( tool ) => {
239- if ( ! tool . function ) return ;
240- return {
241- toolSpec : {
242- name : tool . function . name ,
243- description : tool . function . description ,
244- inputSchema : { json : tool . function . parameters } ,
264+ const canBeAmazonModel = params . model ?. includes ( 'amazon' ) ;
265+ const tools : Array <
266+ | { toolSpec : { name : string ; description ?: string ; inputSchema : any } }
267+ | { cachePoint : { type : string } }
268+ > = [ ] ;
269+ params . tools ?. forEach ( ( tool ) => {
270+ tools . push ( {
271+ toolSpec : {
272+ name : tool . function . name ,
273+ description : tool . function . description ,
274+ inputSchema : { json : tool . function . parameters } ,
275+ } ,
276+ } ) ;
277+ if ( tool . cache_control && ! canBeAmazonModel ) {
278+ tools . push ( {
279+ cachePoint : {
280+ type : 'default' ,
245281 } ,
246- } ;
247- } ) ,
282+ } ) ;
283+ }
284+ } ) ;
285+ const toolConfig = {
286+ tools : tools ,
248287 } ;
249288 let toolChoice = undefined ;
250289 if ( params . tool_choice ) {
@@ -312,6 +351,11 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
312351 transform : ( params : BedrockChatCompletionsParams ) =>
313352 transformAdditionalModelRequestFields ( params ) ,
314353 } ,
354+ response_format : {
355+ param : 'additionalModelRequestFields' ,
356+ transform : ( params : BedrockChatCompletionsParams ) =>
357+ transformAdditionalModelRequestFields ( params ) ,
358+ } ,
315359} ;
316360
317361type BedrockContentItem = {
@@ -341,6 +385,9 @@ type BedrockContentItem = {
341385 bytes : string ;
342386 } ;
343387 } ;
388+ cachePoint ?: {
389+ type : string ;
390+ } ;
344391} ;
345392
346393interface BedrockChatCompletionResponse {
@@ -358,6 +405,10 @@ interface BedrockChatCompletionResponse {
358405 inputTokens : number ;
359406 outputTokens : number ;
360407 totalTokens : number ;
408+ cacheReadInputTokenCount ?: number ;
409+ cacheReadInputTokens ?: number ;
410+ cacheWriteInputTokenCount ?: number ;
411+ cacheWriteInputTokens ?: number ;
361412 } ;
362413}
363414
@@ -421,6 +472,10 @@ export const BedrockChatCompleteResponseTransform: (
421472 }
422473
423474 if ( 'output' in response ) {
475+ const shouldSendCacheUsage =
476+ response . usage . cacheWriteInputTokens ||
477+ response . usage . cacheReadInputTokens ;
478+
424479 let content : string = '' ;
425480 content = response . output . message . content
426481 . filter ( ( item ) => item . text )
@@ -453,6 +508,10 @@ export const BedrockChatCompleteResponseTransform: (
453508 prompt_tokens : response . usage . inputTokens ,
454509 completion_tokens : response . usage . outputTokens ,
455510 total_tokens : response . usage . totalTokens ,
511+ ...( shouldSendCacheUsage && {
512+ cache_read_input_tokens : response . usage . cacheReadInputTokens ,
513+ cache_creation_input_tokens : response . usage . cacheWriteInputTokens ,
514+ } ) ,
456515 } ,
457516 } ;
458517 const toolCalls = response . output . message . content
@@ -503,6 +562,10 @@ export interface BedrockChatCompleteStreamChunk {
503562 inputTokens : number ;
504563 outputTokens : number ;
505564 totalTokens : number ;
565+ cacheReadInputTokenCount ?: number ;
566+ cacheReadInputTokens ?: number ;
567+ cacheWriteInputTokenCount ?: number ;
568+ cacheWriteInputTokens ?: number ;
506569 } ;
507570}
508571
@@ -534,6 +597,9 @@ export const BedrockChatCompleteStreamChunkTransform: (
534597 }
535598
536599 if ( parsedChunk . usage ) {
600+ const shouldSendCacheUsage =
601+ parsedChunk . usage . cacheWriteInputTokens ||
602+ parsedChunk . usage . cacheReadInputTokens ;
537603 return [
538604 `data: ${ JSON . stringify ( {
539605 id : fallbackId ,
@@ -552,6 +618,11 @@ export const BedrockChatCompleteStreamChunkTransform: (
552618 prompt_tokens : parsedChunk . usage . inputTokens ,
553619 completion_tokens : parsedChunk . usage . outputTokens ,
554620 total_tokens : parsedChunk . usage . totalTokens ,
621+ ...( shouldSendCacheUsage && {
622+ cache_read_input_tokens : parsedChunk . usage . cacheReadInputTokens ,
623+ cache_creation_input_tokens :
624+ parsedChunk . usage . cacheWriteInputTokens ,
625+ } ) ,
555626 } ,
556627 } ) } \n\n`,
557628 `data: [DONE]\n\n` ,
@@ -650,6 +721,9 @@ export const BedrockConverseAnthropicChatCompleteConfig: ProviderConfig = {
650721 transform : ( params : BedrockConverseAnthropicChatCompletionsParams ) =>
651722 transformAnthropicAdditionalModelRequestFields ( params ) ,
652723 } ,
724+ anthropic_beta : {
725+ param : 'anthropic_beta' ,
726+ } ,
653727} ;
654728
655729export const BedrockConverseCohereChatCompleteConfig : ProviderConfig = {
0 commit comments