@@ -54,7 +54,7 @@ import {
54
54
getProviderName ,
55
55
type ModelOrMetadata ,
56
56
} from "./shared.js" ;
57
- import { pick } from "convex-helpers" ;
57
+ import { omit , pick } from "convex-helpers" ;
58
58
export type AIMessageWithoutId = Omit < AIMessage , "id" > ;
59
59
60
60
export type SerializeUrlsAndUint8Arrays < T > = T extends URL
@@ -102,9 +102,7 @@ export function fromModelMessage(message: ModelMessage): Message {
102
102
return {
103
103
role : message . role ,
104
104
content,
105
- ...( message . providerOptions
106
- ? { providerOptions : message . providerOptions }
107
- : { } ) ,
105
+ ...pick ( message , [ "providerOptions" ] ) ,
108
106
} as SerializedMessage ;
109
107
}
110
108
@@ -121,9 +119,7 @@ export async function serializeOrThrow(
121
119
return {
122
120
role : message . role ,
123
121
content,
124
- ...( message . providerOptions
125
- ? { providerOptions : message . providerOptions }
126
- : { } ) ,
122
+ ...pick ( message , [ "providerOptions" ] ) ,
127
123
} as SerializedMessage ;
128
124
}
129
125
@@ -613,20 +609,20 @@ export function deserializeContent(
613
609
mediaType : getMimeOrMediaType ( part ) ! ,
614
610
...metadata ,
615
611
} satisfies FilePart ;
616
- case "tool-call" : {
617
- const input = "input" in part ? part . input : part . args ;
612
+ case "tool-call" :
618
613
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" ] ) ,
625
616
} 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 } ;
630
626
case "reasoning" :
631
627
return {
632
628
type : part . type ,
@@ -684,16 +680,13 @@ function normalizeToolResult(
684
680
providerOptions ?: ProviderOptions ;
685
681
providerMetadata ?: ProviderMetadata ;
686
682
} ,
687
- ) : ToolResultPart & Infer < typeof vToolResultPart > {
683
+ ) : ToolResultPart & Infer < typeof vToolResultPart > & { input : unknown } {
688
684
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" ] ) ,
695
688
...metadata ,
696
- } satisfies ToolResultPart ;
689
+ } satisfies ToolResultPart & { input : unknown } ;
697
690
}
698
691
699
692
/**
@@ -804,16 +797,24 @@ export function deserializeUrl(
804
797
return urlOrString ;
805
798
}
806
799
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 {
808
807
const dataOrUrl = part . type === "image" ? part . image : part . data ;
809
808
const url =
810
809
dataOrUrl instanceof ArrayBuffer
811
810
? convertUint8ArrayToBase64 ( new Uint8Array ( dataOrUrl ) )
812
811
: dataOrUrl . toString ( ) ;
813
812
813
+ const mediaType = getMimeOrMediaType ( part ) ;
814
+
814
815
return {
815
816
type : "file" ,
816
- mediaType : part . mediaType ! ,
817
+ mediaType : mediaType ! ,
817
818
filename : part . type === "file" ? part . filename : undefined ,
818
819
url,
819
820
providerMetadata : part . providerOptions ,
0 commit comments