1- import z from 'zod/v4'
2-
31// ===== JSON Types =====
42export type JSONValue =
53 | null
@@ -8,25 +6,9 @@ export type JSONValue =
86 | boolean
97 | JSONObject
108 | JSONArray
11- export const jsonValueSchema : z . ZodType < JSONValue > = z . lazy ( ( ) =>
12- z . union ( [
13- z . null ( ) ,
14- z . string ( ) ,
15- z . number ( ) ,
16- z . boolean ( ) ,
17- jsonObjectSchema ,
18- jsonArraySchema ,
19- ] ) ,
20- )
21-
22- export const jsonObjectSchema : z . ZodType < JSONObject > = z . lazy ( ( ) =>
23- z . record ( z . string ( ) , jsonValueSchema ) ,
24- )
9+
2510export type JSONObject = { [ key : string ] : JSONValue }
2611
27- export const jsonArraySchema : z . ZodType < JSONArray > = z . lazy ( ( ) =>
28- z . array ( jsonValueSchema ) ,
29- )
3012export type JSONArray = JSONValue [ ]
3113
3214/**
@@ -50,173 +32,116 @@ export type JsonSchema = {
5032export type JsonObjectSchema = JsonSchema & { type : 'object' }
5133
5234// ===== Data Content Types =====
53- export const dataContentSchema = z . union ( [
54- z . string ( ) ,
55- z . instanceof ( Uint8Array ) ,
56- z . instanceof ( ArrayBuffer ) ,
57- z . custom < Buffer > (
58- // Buffer might not be available in some environments such as CloudFlare:
59- ( value : unknown ) : value is Buffer =>
60- globalThis . Buffer ?. isBuffer ( value ) ?? false ,
61- { message : 'Must be a Buffer' } ,
62- ) ,
63- ] )
64- export type DataContent = z . infer < typeof dataContentSchema >
35+ export type DataContent = string | Uint8Array | ArrayBuffer | Buffer
6536
6637// ===== Provider Metadata Types =====
67- export const providerMetadataSchema = z . record (
68- z . string ( ) ,
69- z . record ( z . string ( ) , jsonValueSchema ) ,
70- )
71-
72- export type ProviderMetadata = z . infer < typeof providerMetadataSchema >
38+ export type ProviderMetadata = Record < string , Record < string , JSONValue > >
7339
7440// ===== Content Part Types =====
75- export const textPartSchema = z . object ( {
76- type : z . literal ( 'text' ) ,
77- text : z . string ( ) ,
78- providerOptions : providerMetadataSchema . optional ( ) ,
79- } )
80- export type TextPart = z . infer < typeof textPartSchema >
81-
82- export const imagePartSchema = z . object ( {
83- type : z . literal ( 'image' ) ,
84- image : z . union ( [ dataContentSchema , z . instanceof ( URL ) ] ) ,
85- mediaType : z . string ( ) . optional ( ) ,
86- providerOptions : providerMetadataSchema . optional ( ) ,
87- } )
88- export type ImagePart = z . infer < typeof imagePartSchema >
89-
90- export const filePartSchema = z . object ( {
91- type : z . literal ( 'file' ) ,
92- data : z . union ( [ dataContentSchema , z . instanceof ( URL ) ] ) ,
93- filename : z . string ( ) . optional ( ) ,
94- mediaType : z . string ( ) ,
95- providerOptions : providerMetadataSchema . optional ( ) ,
96- } )
97- export type FilePart = z . infer < typeof filePartSchema >
98-
99- export const reasoningPartSchema = z . object ( {
100- type : z . literal ( 'reasoning' ) ,
101- text : z . string ( ) ,
102- providerOptions : providerMetadataSchema . optional ( ) ,
103- } )
104- export type ReasoningPart = z . infer < typeof reasoningPartSchema >
105-
106- export const toolCallPartSchema = z . object ( {
107- type : z . literal ( 'tool-call' ) ,
108- toolCallId : z . string ( ) ,
109- toolName : z . string ( ) ,
110- input : z . record ( z . string ( ) , z . unknown ( ) ) ,
111- providerOptions : providerMetadataSchema . optional ( ) ,
112- providerExecuted : z . boolean ( ) . optional ( ) ,
113- } )
114- export type ToolCallPart = z . infer < typeof toolCallPartSchema >
115-
116- export const toolResultOutputSchema = z . discriminatedUnion ( 'type' , [
117- z . object ( {
118- type : z . literal ( 'json' ) ,
119- value : jsonValueSchema ,
120- } ) ,
121- z . object ( {
122- type : z . literal ( 'media' ) ,
123- data : z . string ( ) ,
124- mediaType : z . string ( ) ,
125- } ) ,
126- ] )
127- export type ToolResultOutput = z . infer < typeof toolResultOutputSchema >
128-
129- export const toolResultPartSchema = z . object ( {
130- type : z . literal ( 'tool-result' ) ,
131- toolCallId : z . string ( ) ,
132- toolName : z . string ( ) ,
133- output : toolResultOutputSchema . array ( ) ,
134- providerOptions : providerMetadataSchema . optional ( ) ,
135- } )
136- export type ToolResultPart = z . infer < typeof toolResultPartSchema >
41+ export type TextPart = {
42+ type : 'text'
43+ text : string
44+ providerOptions ?: ProviderMetadata
45+ }
46+
47+ export type ImagePart = {
48+ type : 'image'
49+ image : DataContent
50+ mediaType ?: string
51+ providerOptions ?: ProviderMetadata
52+ }
53+
54+ export type FilePart = {
55+ type : 'file'
56+ data : DataContent
57+ filename ?: string
58+ mediaType : string
59+ providerOptions ?: ProviderMetadata
60+ }
61+
62+ export type ReasoningPart = {
63+ type : 'reasoning'
64+ text : string
65+ providerOptions ?: ProviderMetadata
66+ }
67+
68+ export type ToolCallPart = {
69+ type : 'tool-call'
70+ toolCallId : string
71+ toolName : string
72+ input : Record < string , unknown >
73+ providerOptions ?: ProviderMetadata
74+ providerExecuted ?: boolean
75+ }
76+
77+ export type ToolResultOutput =
78+ | {
79+ type : 'json'
80+ value : JSONValue
81+ }
82+ | {
83+ type : 'media'
84+ data : string
85+ mediaType : string
86+ }
87+
88+ export type ToolResultPart = {
89+ type : 'tool-result'
90+ toolCallId : string
91+ toolName : string
92+ output : ToolResultOutput [ ]
93+ providerOptions ?: ProviderMetadata
94+ }
13795
13896// ===== Message Types =====
139- const auxiliaryDataSchema = z . object ( {
140- providerOptions : providerMetadataSchema . optional ( ) ,
141- timeToLive : z
142- . union ( [ z . literal ( 'agentStep' ) , z . literal ( 'userPrompt' ) ] )
143- . optional ( ) ,
144- keepDuringTruncation : z . boolean ( ) . optional ( ) ,
145- keepLastTags : z . string ( ) . array ( ) . optional ( ) ,
146- } )
147-
148- export const systemMessageSchema = z
149- . object ( {
150- role : z . literal ( 'system' ) ,
151- content : z . string ( ) ,
152- } )
153- . and ( auxiliaryDataSchema )
154- export type SystemMessage = z . infer < typeof systemMessageSchema >
155-
156- export const userMessageSchema = z
157- . object ( {
158- role : z . literal ( 'user' ) ,
159- content : z . union ( [
160- z . string ( ) ,
161- z . union ( [ textPartSchema , imagePartSchema , filePartSchema ] ) . array ( ) ,
162- ] ) ,
163- } )
164- . and ( auxiliaryDataSchema )
165- export type UserMessage = z . infer < typeof userMessageSchema >
166-
167- export const assistantMessageSchema = z
168- . object ( {
169- role : z . literal ( 'assistant' ) ,
170- content : z . union ( [
171- z . string ( ) ,
172- z
173- . union ( [ textPartSchema , reasoningPartSchema , toolCallPartSchema ] )
174- . array ( ) ,
175- ] ) ,
176- } )
177- . and ( auxiliaryDataSchema )
178- export type AssistantMessage = z . infer < typeof assistantMessageSchema >
179-
180- export const toolMessageSchema = z
181- . object ( {
182- role : z . literal ( 'tool' ) ,
183- content : toolResultPartSchema ,
184- } )
185- . and ( auxiliaryDataSchema )
186- export type ToolMessage = z . infer < typeof toolMessageSchema >
187-
188- export const messageSchema = z . union ( [
189- systemMessageSchema ,
190- userMessageSchema ,
191- assistantMessageSchema ,
192- toolMessageSchema ,
193- ] )
194- export type Message = z . infer < typeof messageSchema >
97+ type AuxiliaryData = {
98+ providerOptions ?: ProviderMetadata
99+ timeToLive ?: 'agentStep' | 'userPrompt'
100+ keepDuringTruncation ?: boolean
101+ keepLastTags ?: string [ ]
102+ }
103+
104+ export type SystemMessage = {
105+ role : 'system'
106+ content : string
107+ } & AuxiliaryData
108+
109+ export type UserMessage = {
110+ role : 'user'
111+ content : string | ( TextPart | ImagePart | FilePart ) [ ]
112+ } & AuxiliaryData
113+
114+ export type AssistantMessage = {
115+ role : 'assistant'
116+ content : string | ( TextPart | ReasoningPart | ToolCallPart ) [ ]
117+ } & AuxiliaryData
118+
119+ export type ToolMessage = {
120+ type : 'tool'
121+ content : ToolResultPart
122+ } & AuxiliaryData
123+
124+ export type Message =
125+ | SystemMessage
126+ | UserMessage
127+ | AssistantMessage
128+ | ToolMessage
195129
196130// ===== MCP Server Types =====
197131
198- export const mcpConfigStdioSchema = z . strictObject ( {
199- type : z . literal ( 'stdio' ) . default ( 'stdio' ) ,
200- command : z . string ( ) ,
201- args : z
202- . string ( )
203- . array ( )
204- . default ( ( ) => [ ] ) ,
205- env : z . record ( z . string ( ) , z . string ( ) ) . default ( ( ) => ( { } ) ) ,
206- headers : z . record ( z . string ( ) , z . string ( ) ) . default ( ( ) => ( { } ) ) ,
207- } )
208-
209- export const mcpConfigRemoteSchema = z . strictObject ( {
210- type : z . enum ( [ 'http' , 'sse' ] ) . default ( 'http' ) ,
211- url : z . string ( ) ,
212- params : z . record ( z . string ( ) , z . string ( ) ) . default ( ( ) => ( { } ) ) ,
213- } )
214-
215- export const mcpConfigSchema = z . union ( [
216- mcpConfigRemoteSchema ,
217- mcpConfigStdioSchema ,
218- ] )
219- export type MCPConfig = z . input < typeof mcpConfigSchema >
132+ export type MCPConfig =
133+ | {
134+ type ?: 'stdio'
135+ command : string
136+ args ?: string [ ]
137+ env ?: Record < string , string >
138+ headers ?: Record < string , string >
139+ }
140+ | {
141+ type ?: 'http' | 'sse'
142+ url : string
143+ params ?: Record < string , string >
144+ }
220145
221146// ============================================================================
222147// Logger Interface
0 commit comments