@@ -54,7 +54,7 @@ import {
5454 getProviderName ,
5555 type ModelOrMetadata ,
5656} from "./shared.js" ;
57- import { pick } from "convex-helpers" ;
57+ import { omit , pick } from "convex-helpers" ;
5858export type AIMessageWithoutId = Omit < AIMessage , "id" > ;
5959
6060export type SerializeUrlsAndUint8Arrays < T > = T extends URL
@@ -102,9 +102,7 @@ export function fromModelMessage(message: ModelMessage): Message {
102102 return {
103103 role : message . role ,
104104 content,
105- ...( message . providerOptions
106- ? { providerOptions : message . providerOptions }
107- : { } ) ,
105+ ...pick ( message , [ "providerOptions" ] ) ,
108106 } as SerializedMessage ;
109107}
110108
@@ -121,9 +119,7 @@ export async function serializeOrThrow(
121119 return {
122120 role : message . role ,
123121 content,
124- ...( message . providerOptions
125- ? { providerOptions : message . providerOptions }
126- : { } ) ,
122+ ...pick ( message , [ "providerOptions" ] ) ,
127123 } as SerializedMessage ;
128124}
129125
@@ -613,20 +609,20 @@ export function toModelMessageContent(
613609 mediaType : getMimeOrMediaType ( part ) ! ,
614610 ...metadata ,
615611 } satisfies FilePart ;
616- case "tool-call" : {
617- const input = "input" in part ? part . input : part . args ;
612+ case "tool-call" :
618613 return {
619- type : part . type ,
620- input : input ?? null ,
621- toolCallId : part . toolCallId ,
622- toolName : part . toolName ,
623- providerExecuted : part . providerExecuted ,
624- ...metadata ,
614+ input : ( "input" in part ? part . input : part . args ) ?? null ,
615+ ...omit ( part as Infer < typeof vToolCallPart > , [ "args" ] ) ,
625616 } satisfies ToolCallPart ;
626- }
627- case "tool-result" : {
628- return normalizeToolResult ( part , metadata ) ;
629- }
617+ case "tool-result" :
618+ return {
619+ input : ( part as Infer < typeof vToolResultPart > ) . args ,
620+ output : normalizeToolOutput (
621+ ( part as Infer < typeof vToolResultPart > ) . result ,
622+ ) ,
623+ ...omit ( part as Infer < typeof vToolResultPart > , [ "result" , "args" ] ) ,
624+ ...metadata ,
625+ } satisfies ToolResultPart & { input : unknown } ;
630626 case "reasoning" :
631627 return {
632628 type : part . type ,
@@ -684,16 +680,13 @@ function normalizeToolResult(
684680 providerOptions ?: ProviderOptions ;
685681 providerMetadata ?: ProviderMetadata ;
686682 } ,
687- ) : ToolResultPart & Infer < typeof vToolResultPart > {
683+ ) : ToolResultPart & Infer < typeof vToolResultPart > & { input : unknown } {
688684 return {
689- type : part . type ,
690- output :
691- part . output ??
692- normalizeToolOutput ( "result" in part ? part . result : undefined ) ,
693- toolCallId : part . toolCallId ,
694- toolName : part . toolName ,
685+ input : ( part as Infer < typeof vToolResultPart > ) . args ,
686+ output : normalizeToolOutput ( "result" in part ? part . result : undefined ) ,
687+ ...omit ( part as Infer < typeof vToolResultPart > , [ "result" , "args" ] ) ,
695688 ...metadata ,
696- } satisfies ToolResultPart ;
689+ } satisfies ToolResultPart & { input : unknown } ;
697690}
698691
699692/**
@@ -804,16 +797,24 @@ export function toModelMessageDataOrUrl(
804797 return urlOrString ;
805798}
806799
807- export function toUIFilePart ( part : ImagePart | FilePart ) : FileUIPart {
800+ export function toUIFilePart (
801+ part :
802+ | ImagePart
803+ | FilePart
804+ | Infer < typeof vImagePart >
805+ | Infer < typeof vFilePart > ,
806+ ) : FileUIPart {
808807 const dataOrUrl = part . type === "image" ? part . image : part . data ;
809808 const url =
810809 dataOrUrl instanceof ArrayBuffer
811810 ? convertUint8ArrayToBase64 ( new Uint8Array ( dataOrUrl ) )
812811 : dataOrUrl . toString ( ) ;
813812
813+ const mediaType = getMimeOrMediaType ( part ) ;
814+
814815 return {
815816 type : "file" ,
816- mediaType : part . mediaType ! ,
817+ mediaType : mediaType ! ,
817818 filename : part . type === "file" ? part . filename : undefined ,
818819 url,
819820 providerMetadata : part . providerOptions ,
0 commit comments