@@ -155,6 +155,54 @@ describe("session.message-v2.toModelMessage", () => {
155155 expect ( await MessageV2 . toModelMessages ( input , model ) ) . toStrictEqual ( [ ] )
156156 } )
157157
158+ test ( "filters out user messages with only empty text parts" , async ( ) => {
159+ const messageID = "m-user"
160+
161+ const input : MessageV2 . WithParts [ ] = [
162+ {
163+ info : userInfo ( messageID ) ,
164+ parts : [
165+ {
166+ ...basePart ( messageID , "p1" ) ,
167+ type : "text" ,
168+ text : "" ,
169+ } ,
170+ ] as MessageV2 . Part [ ] ,
171+ } ,
172+ ]
173+
174+ expect ( await MessageV2 . toModelMessages ( input , model ) ) . toStrictEqual ( [ ] )
175+ } )
176+
177+ test ( "filters empty user text parts while keeping non-empty parts" , async ( ) => {
178+ const messageID = "m-user"
179+
180+ const input : MessageV2 . WithParts [ ] = [
181+ {
182+ info : userInfo ( messageID ) ,
183+ parts : [
184+ {
185+ ...basePart ( messageID , "p1" ) ,
186+ type : "text" ,
187+ text : "" ,
188+ } ,
189+ {
190+ ...basePart ( messageID , "p2" ) ,
191+ type : "text" ,
192+ text : "hello" ,
193+ } ,
194+ ] as MessageV2 . Part [ ] ,
195+ } ,
196+ ]
197+
198+ expect ( await MessageV2 . toModelMessages ( input , model ) ) . toStrictEqual ( [
199+ {
200+ role : "user" ,
201+ content : [ { type : "text" , text : "hello" } ] ,
202+ } ,
203+ ] )
204+ } )
205+
158206 test ( "includes synthetic text parts" , async ( ) => {
159207 const messageID = "m-user"
160208
@@ -443,6 +491,108 @@ describe("session.message-v2.toModelMessage", () => {
443491 } )
444492 } )
445493
494+ test ( "moves bedrock pdf tool-result media into a separate user message" , async ( ) => {
495+ const bedrockModel : Provider . Model = {
496+ ...model ,
497+ id : ModelID . make ( "amazon-bedrock/anthropic.claude-sonnet-4-6" ) ,
498+ providerID : ProviderID . make ( "amazon-bedrock" ) ,
499+ api : {
500+ id : "anthropic.claude-sonnet-4-6" ,
501+ url : "https://bedrock-runtime.us-east-1.amazonaws.com" ,
502+ npm : "@ai-sdk/amazon-bedrock" ,
503+ } ,
504+ capabilities : {
505+ ...model . capabilities ,
506+ attachment : true ,
507+ input : {
508+ ...model . capabilities . input ,
509+ image : true ,
510+ pdf : true ,
511+ } ,
512+ } ,
513+ }
514+ const pdf = Buffer . from ( "%PDF-1.4\n" ) . toString ( "base64" )
515+ const userID = "m-user-bedrock-pdf"
516+ const assistantID = "m-assistant-bedrock-pdf"
517+ const input : MessageV2 . WithParts [ ] = [
518+ {
519+ info : userInfo ( userID ) ,
520+ parts : [
521+ {
522+ ...basePart ( userID , "u1-bedrock-pdf" ) ,
523+ type : "text" ,
524+ text : "run tool" ,
525+ } ,
526+ ] as MessageV2 . Part [ ] ,
527+ } ,
528+ {
529+ info : assistantInfo ( assistantID , userID ) ,
530+ parts : [
531+ {
532+ ...basePart ( assistantID , "a1-bedrock-pdf" ) ,
533+ type : "tool" ,
534+ callID : "call-bedrock-pdf-1" ,
535+ tool : "read" ,
536+ state : {
537+ status : "completed" ,
538+ input : { filePath : "/tmp/example.pdf" } ,
539+ output : "PDF read successfully" ,
540+ title : "Read" ,
541+ metadata : { } ,
542+ time : { start : 0 , end : 1 } ,
543+ attachments : [
544+ {
545+ ...basePart ( assistantID , "file-bedrock-pdf-1" ) ,
546+ type : "file" ,
547+ mime : "application/pdf" ,
548+ filename : "example.pdf" ,
549+ url : `data:application/pdf;base64,${ pdf } ` ,
550+ } ,
551+ ] ,
552+ } ,
553+ } ,
554+ ] as MessageV2 . Part [ ] ,
555+ } ,
556+ ]
557+
558+ expect ( await MessageV2 . toModelMessages ( input , bedrockModel ) ) . toStrictEqual ( [
559+ {
560+ role : "user" ,
561+ content : [ { type : "text" , text : "run tool" } ] ,
562+ } ,
563+ {
564+ role : "assistant" ,
565+ content : [
566+ {
567+ type : "tool-call" ,
568+ toolCallId : "call-bedrock-pdf-1" ,
569+ toolName : "read" ,
570+ input : { filePath : "/tmp/example.pdf" } ,
571+ providerExecuted : undefined ,
572+ } ,
573+ ] ,
574+ } ,
575+ {
576+ role : "tool" ,
577+ content : [
578+ {
579+ type : "tool-result" ,
580+ toolCallId : "call-bedrock-pdf-1" ,
581+ toolName : "read" ,
582+ output : { type : "text" , value : "PDF read successfully" } ,
583+ } ,
584+ ] ,
585+ } ,
586+ {
587+ role : "user" ,
588+ content : [
589+ { type : "text" , text : "Attached media from tool result:" } ,
590+ { type : "file" , mediaType : "application/pdf" , filename : "example.pdf" , data : `data:application/pdf;base64,${ pdf } ` } ,
591+ ] ,
592+ } ,
593+ ] )
594+ } )
595+
446596 test ( "omits provider metadata when assistant model differs" , async ( ) => {
447597 const userID = "m-user"
448598 const assistantID = "m-assistant"
@@ -1134,8 +1284,9 @@ describe("session.message-v2.toModelMessage", () => {
11341284 expect ( ( result [ 1 ] . content as any [ ] ) . find ( ( p ) => p . type === "text" ) . text ) . toBe ( "the answer" )
11351285 } )
11361286
1137- test ( "substitutes space for empty text when reasoning signature is under 'bedrock' namespace" , async ( ) => {
1138- // AWS Bedrock hosts Anthropic Claude but stores signatures under metadata.bedrock
1287+ test ( "leaves empty text alone when reasoning signature is under 'bedrock' namespace" , async ( ) => {
1288+ // Bedrock signed reasoning is preserved as reasoning metadata, but unlike the
1289+ // direct Anthropic path we do not preserve empty text separators for Bedrock.
11391290 const assistantID = "m-assistant-bedrock"
11401291 const input : MessageV2 . WithParts [ ] = [
11411292 {
@@ -1157,7 +1308,7 @@ describe("session.message-v2.toModelMessage", () => {
11571308
11581309 expect ( result ) . toHaveLength ( 1 )
11591310 const texts = ( result [ 0 ] . content as any [ ] ) . filter ( ( p ) => p . type === "text" )
1160- expect ( texts . map ( ( t ) => t . text ) ) . toStrictEqual ( [ " " , "answer" ] )
1311+ expect ( texts . map ( ( t ) => t . text ) ) . toStrictEqual ( [ "" , "answer" ] )
11611312 } )
11621313
11631314 test ( "leaves empty text alone when reasoning has no Anthropic signature" , async ( ) => {
0 commit comments