@@ -435,6 +435,40 @@ export namespace MessageV2 {
435435
436436 export function toModelMessages ( input : WithParts [ ] , model : Provider . Model ) : ModelMessage [ ] {
437437 const result : UIMessage [ ] = [ ]
438+ const toolNames = new Set < string > ( )
439+
440+ const toModelOutput = ( output : unknown ) => {
441+ if ( typeof output === "string" ) {
442+ return { type : "text" , value : output }
443+ }
444+
445+ if ( typeof output === "object" ) {
446+ const outputObject = output as {
447+ text : string
448+ attachments ?: Array < { mime : string ; url : string } >
449+ }
450+ const attachments = ( outputObject . attachments ?? [ ] ) . filter ( ( attachment ) => {
451+ return attachment . url . startsWith ( "data:" ) && attachment . url . includes ( "," )
452+ } )
453+
454+ return {
455+ type : "content" ,
456+ value : [
457+ { type : "text" , text : outputObject . text } ,
458+ ...attachments . map ( ( attachment ) => ( {
459+ type : "media" ,
460+ mediaType : attachment . mime ,
461+ data : iife ( ( ) => {
462+ const commaIndex = attachment . url . indexOf ( "," )
463+ return commaIndex === - 1 ? attachment . url : attachment . url . slice ( commaIndex + 1 )
464+ } ) ,
465+ } ) ) ,
466+ ] ,
467+ }
468+ }
469+
470+ return { type : "json" , value : output as never }
471+ }
438472
439473 for ( const msg of input ) {
440474 if ( msg . parts . length === 0 ) continue
@@ -505,31 +539,24 @@ export namespace MessageV2 {
505539 type : "step-start" ,
506540 } )
507541 if ( part . type === "tool" ) {
542+ toolNames . add ( part . tool )
508543 if ( part . state . status === "completed" ) {
509- if ( part . state . attachments ?. length ) {
510- result . push ( {
511- id : Identifier . ascending ( "message" ) ,
512- role : "user" ,
513- parts : [
514- {
515- type : "text" ,
516- text : `The tool ${ part . tool } returned the following attachments:` ,
517- } ,
518- ...part . state . attachments . map ( ( attachment ) => ( {
519- type : "file" as const ,
520- url : attachment . url ,
521- mediaType : attachment . mime ,
522- filename : attachment . filename ,
523- } ) ) ,
524- ] ,
525- } )
526- }
544+ const outputText = part . state . time . compacted ? "[Old tool result content cleared]" : part . state . output
545+ const attachments = part . state . time . compacted ? [ ] : ( part . state . attachments ?? [ ] )
546+ const output =
547+ attachments . length > 0
548+ ? {
549+ text : outputText ,
550+ attachments,
551+ }
552+ : outputText
553+
527554 assistantMessage . parts . push ( {
528555 type : ( "tool-" + part . tool ) as `tool-${string } `,
529556 state : "output-available" ,
530557 toolCallId : part . callID ,
531558 input : part . state . input ,
532- output : part . state . time . compacted ? "[Old tool result content cleared]" : part . state . output ,
559+ output,
533560 ...( differentModel ? { } : { callProviderMetadata : part . metadata } ) ,
534561 } )
535562 }
@@ -568,7 +595,15 @@ export namespace MessageV2 {
568595 }
569596 }
570597
571- return convertToModelMessages ( result . filter ( ( msg ) => msg . parts . some ( ( part ) => part . type !== "step-start" ) ) )
598+ const tools = Object . fromEntries ( Array . from ( toolNames ) . map ( ( toolName ) => [ toolName , { toModelOutput } ] ) )
599+
600+ return convertToModelMessages (
601+ result . filter ( ( msg ) => msg . parts . some ( ( part ) => part . type !== "step-start" ) ) ,
602+ {
603+ //@ts -expect-error (convertToModelMessages expects a ToolSet but only actually needs tools[name]?.toModelOutput)
604+ tools,
605+ } ,
606+ )
572607 }
573608
574609 export const stream = fn ( Identifier . schema ( "session" ) , async function * ( sessionID ) {
0 commit comments