@@ -405,6 +405,135 @@ describe('cache control', () => {
405405 } ) ;
406406 } ) ;
407407
408+ describe ( 'assistant message' , ( ) => {
409+ it ( 'should set cache_control on assistant message text part with part cache control' , async ( ) => {
410+ const result = convertToAnthropicMessagesPrompt ( {
411+ prompt : [
412+ { role : 'user' , content : [ { type : 'text' , text : 'user-content' } ] } ,
413+ {
414+ role : 'assistant' ,
415+ content : [
416+ {
417+ type : 'text' ,
418+ text : 'test' ,
419+ providerMetadata : {
420+ anthropic : {
421+ cacheControl : { type : 'ephemeral' } ,
422+ } ,
423+ } ,
424+ } ,
425+ ] ,
426+ } ,
427+ ] ,
428+ cacheControl : true ,
429+ } ) ;
430+
431+ expect ( result ) . toEqual ( {
432+ messages : [
433+ { role : 'user' , content : [ { type : 'text' , text : 'user-content' } ] } ,
434+ {
435+ role : 'assistant' ,
436+ content : [
437+ {
438+ type : 'text' ,
439+ text : 'test' ,
440+ cache_control : { type : 'ephemeral' } ,
441+ } ,
442+ ] ,
443+ } ,
444+ ] ,
445+ system : undefined ,
446+ } ) ;
447+ } ) ;
448+
449+ it ( 'should set cache_control on assistant tool call part with part cache control' , async ( ) => {
450+ const result = convertToAnthropicMessagesPrompt ( {
451+ prompt : [
452+ { role : 'user' , content : [ { type : 'text' , text : 'user-content' } ] } ,
453+ {
454+ role : 'assistant' ,
455+ content : [
456+ {
457+ type : 'tool-call' ,
458+ toolCallId : 'test-id' ,
459+ toolName : 'test-tool' ,
460+ args : { some : 'arg' } ,
461+ providerMetadata : {
462+ anthropic : {
463+ cacheControl : { type : 'ephemeral' } ,
464+ } ,
465+ } ,
466+ } ,
467+ ] ,
468+ } ,
469+ ] ,
470+ cacheControl : true ,
471+ } ) ;
472+
473+ expect ( result ) . toEqual ( {
474+ messages : [
475+ { role : 'user' , content : [ { type : 'text' , text : 'user-content' } ] } ,
476+ {
477+ role : 'assistant' ,
478+ content : [
479+ {
480+ type : 'tool_use' ,
481+ name : 'test-tool' ,
482+ id : 'test-id' ,
483+ input : { some : 'arg' } ,
484+ cache_control : { type : 'ephemeral' } ,
485+ } ,
486+ ] ,
487+ } ,
488+ ] ,
489+ system : undefined ,
490+ } ) ;
491+ } ) ;
492+
493+ it ( 'should set cache_control on last assistant message part with message cache control' , async ( ) => {
494+ const result = convertToAnthropicMessagesPrompt ( {
495+ prompt : [
496+ { role : 'user' , content : [ { type : 'text' , text : 'user-content' } ] } ,
497+ {
498+ role : 'assistant' ,
499+ content : [
500+ { type : 'text' , text : 'part1' } ,
501+ { type : 'text' , text : 'part2' } ,
502+ ] ,
503+ providerMetadata : {
504+ anthropic : {
505+ cacheControl : { type : 'ephemeral' } ,
506+ } ,
507+ } ,
508+ } ,
509+ ] ,
510+ cacheControl : true ,
511+ } ) ;
512+
513+ expect ( result ) . toEqual ( {
514+ messages : [
515+ { role : 'user' , content : [ { type : 'text' , text : 'user-content' } ] } ,
516+ {
517+ role : 'assistant' ,
518+ content : [
519+ {
520+ type : 'text' ,
521+ text : 'part1' ,
522+ cache_control : undefined ,
523+ } ,
524+ {
525+ type : 'text' ,
526+ text : 'part2' ,
527+ cache_control : { type : 'ephemeral' } ,
528+ } ,
529+ ] ,
530+ } ,
531+ ] ,
532+ system : undefined ,
533+ } ) ;
534+ } ) ;
535+ } ) ;
536+
408537 describe ( 'tool message' , ( ) => {
409538 it ( 'should set cache_control on tool result message part with part cache control' , async ( ) => {
410539 const result = convertToAnthropicMessagesPrompt ( {
0 commit comments