@@ -368,37 +368,68 @@ export type ToolPart = Omit<Types.DeepMutable<Schema.Schema.Type<typeof ToolPart
368368 state : ToolState
369369}
370370
371- const Base = z . object ( {
372- id : MessageID . zod ,
373- sessionID : SessionID . zod ,
374- } )
371+ const messageBase = {
372+ id : MessageID ,
373+ sessionID : SessionID ,
374+ }
375375
376- export const User = Base . extend ( {
377- role : z . literal ( "user" ) ,
378- time : z . object ( {
379- created : z . number ( ) ,
376+ export const User = Schema . Struct ( {
377+ ...messageBase ,
378+ role : Schema . Literal ( "user" ) ,
379+ time : Schema . Struct ( {
380+ created : Schema . Number ,
380381 } ) ,
381- format : Format . zod . optional ( ) ,
382- summary : z
383- . object ( {
384- title : z . string ( ) . optional ( ) ,
385- body : z . string ( ) . optional ( ) ,
386- diffs : Snapshot . FileDiff . zod . array ( ) ,
387- } )
388- . optional ( ) ,
389- agent : z . string ( ) ,
390- model : z . object ( {
391- providerID : ProviderID . zod ,
392- modelID : ModelID . zod ,
393- variant : z . string ( ) . optional ( ) ,
382+ format : Schema . optional ( _Format ) ,
383+ summary : Schema . optional (
384+ Schema . Struct ( {
385+ title : Schema . optional ( Schema . String ) ,
386+ body : Schema . optional ( Schema . String ) ,
387+ diffs : Schema . Array ( Snapshot . FileDiff ) ,
388+ } ) ,
389+ ) ,
390+ agent : Schema . String ,
391+ model : Schema . Struct ( {
392+ providerID : ProviderID ,
393+ modelID : ModelID ,
394+ variant : Schema . optional ( Schema . String ) ,
394395 } ) ,
395- system : z . string ( ) . optional ( ) ,
396- tools : z . record ( z . string ( ) , z . boolean ( ) ) . optional ( ) ,
397- } ) . meta ( {
398- ref : "UserMessage" ,
396+ system : Schema . optional ( Schema . String ) ,
397+ tools : Schema . optional ( Schema . Record ( Schema . String , Schema . Boolean ) ) ,
398+ } )
399+ . annotate ( { identifier : "UserMessage" } )
400+ . pipe ( withStatics ( ( s ) => ( { zod : zod ( s ) } ) ) )
401+ export type User = Types . DeepMutable < Schema . Schema . Type < typeof User > >
402+
403+ const _Part = Schema . Union ( [
404+ TextPart ,
405+ SubtaskPart ,
406+ ReasoningPart ,
407+ FilePart ,
408+ ToolPart ,
409+ StepStartPart ,
410+ StepFinishPart ,
411+ SnapshotPart ,
412+ PatchPart ,
413+ AgentPart ,
414+ RetryPart ,
415+ CompactionPart ,
416+ ] ) . annotate ( { discriminator : "type" , identifier : "Part" } )
417+ export const Part = Object . assign ( _Part , {
418+ zod : zod ( _Part ) as unknown as z . ZodType <
419+ | TextPart
420+ | SubtaskPart
421+ | ReasoningPart
422+ | FilePart
423+ | ToolPart
424+ | StepStartPart
425+ | StepFinishPart
426+ | SnapshotPart
427+ | PatchPart
428+ | AgentPart
429+ | RetryPart
430+ | CompactionPart
431+ > ,
399432} )
400- export type User = z . infer < typeof User >
401-
402433export type Part =
403434 | TextPart
404435 | SubtaskPart
@@ -413,28 +444,19 @@ export type Part =
413444 | RetryPart
414445 | CompactionPart
415446
416- // The derived `.zod` on each leaf is typed as `z.ZodType<...>`, but the walker
417- // always emits a `z.ZodObject` at runtime. `z.discriminatedUnion` and
418- // `z.infer` both rely on the ZodObject structural type, so cast here so the
419- // resulting Part behaves like the pre-migration Zod union.
420- export const Part = z
421- . discriminatedUnion ( "type" , [
422- TextPart . zod as unknown as z . ZodObject < any > ,
423- SubtaskPart . zod as unknown as z . ZodObject < any > ,
424- ReasoningPart . zod as unknown as z . ZodObject < any > ,
425- FilePart . zod as unknown as z . ZodObject < any > ,
426- ToolPart . zod as unknown as z . ZodObject < any > ,
427- StepStartPart . zod as unknown as z . ZodObject < any > ,
428- StepFinishPart . zod as unknown as z . ZodObject < any > ,
429- SnapshotPart . zod as unknown as z . ZodObject < any > ,
430- PatchPart . zod as unknown as z . ZodObject < any > ,
431- AgentPart . zod as unknown as z . ZodObject < any > ,
432- RetryPart . zod as unknown as z . ZodObject < any > ,
433- CompactionPart . zod as unknown as z . ZodObject < any > ,
434- ] )
435- . meta ( {
436- ref : "Part" ,
437- } ) as unknown as z . ZodType < Part >
447+ // Errors are still NamedError-based Zod; bridge via ZodOverride so the derived
448+ // Zod + JSON Schema emit the original discriminatedUnion shape. Migrating the
449+ // error classes to Schema.TaggedErrorClass is a separate slice.
450+ const AssistantErrorZod = z . discriminatedUnion ( "name" , [
451+ AuthError . Schema ,
452+ NamedError . Unknown . Schema ,
453+ OutputLengthError . Schema ,
454+ AbortedError . Schema ,
455+ StructuredOutputError . Schema ,
456+ ContextOverflowError . Schema ,
457+ APIError . Schema ,
458+ ] )
459+ type AssistantError = z . infer < typeof AssistantErrorZod >
438460
439461// ── Prompt input schemas ─────────────────────────────────────────────────────
440462//
@@ -508,59 +530,53 @@ export const SubtaskPartInput = Schema.Struct({
508530 . pipe ( withStatics ( ( s ) => ( { zod : zod ( s ) } ) ) )
509531export type SubtaskPartInput = Types . DeepMutable < Schema . Schema . Type < typeof SubtaskPartInput > >
510532
511- export const Assistant = Base . extend ( {
512- role : z . literal ( "assistant" ) ,
513- time : z . object ( {
514- created : z . number ( ) ,
515- completed : z . number ( ) . optional ( ) ,
533+ export const Assistant = Schema . Struct ( {
534+ ...messageBase ,
535+ role : Schema . Literal ( "assistant" ) ,
536+ time : Schema . Struct ( {
537+ created : Schema . Number ,
538+ completed : Schema . optional ( Schema . Number ) ,
516539 } ) ,
517- error : z
518- . discriminatedUnion ( "name" , [
519- AuthError . Schema ,
520- NamedError . Unknown . Schema ,
521- OutputLengthError . Schema ,
522- AbortedError . Schema ,
523- StructuredOutputError . Schema ,
524- ContextOverflowError . Schema ,
525- APIError . Schema ,
526- ] )
527- . optional ( ) ,
528- parentID : MessageID . zod ,
529- modelID : ModelID . zod ,
530- providerID : ProviderID . zod ,
540+ error : Schema . optional ( Schema . Any . annotate ( { [ ZodOverride ] : AssistantErrorZod } ) ) ,
541+ parentID : MessageID ,
542+ modelID : ModelID ,
543+ providerID : ProviderID ,
531544 /**
532545 * @deprecated
533546 */
534- mode : z . string ( ) ,
535- agent : z . string ( ) ,
536- path : z . object ( {
537- cwd : z . string ( ) ,
538- root : z . string ( ) ,
547+ mode : Schema . String ,
548+ agent : Schema . String ,
549+ path : Schema . Struct ( {
550+ cwd : Schema . String ,
551+ root : Schema . String ,
539552 } ) ,
540- summary : z . boolean ( ) . optional ( ) ,
541- cost : z . number ( ) ,
542- tokens : z . object ( {
543- total : z . number ( ) . optional ( ) ,
544- input : z . number ( ) ,
545- output : z . number ( ) ,
546- reasoning : z . number ( ) ,
547- cache : z . object ( {
548- read : z . number ( ) ,
549- write : z . number ( ) ,
553+ summary : Schema . optional ( Schema . Boolean ) ,
554+ cost : Schema . Number ,
555+ tokens : Schema . Struct ( {
556+ total : Schema . optional ( Schema . Number ) ,
557+ input : Schema . Number ,
558+ output : Schema . Number ,
559+ reasoning : Schema . Number ,
560+ cache : Schema . Struct ( {
561+ read : Schema . Number ,
562+ write : Schema . Number ,
550563 } ) ,
551564 } ) ,
552- structured : z . any ( ) . optional ( ) ,
553- variant : z . string ( ) . optional ( ) ,
554- finish : z . string ( ) . optional ( ) ,
555- } ) . meta ( {
556- ref : "AssistantMessage" ,
565+ structured : Schema . optional ( Schema . Any ) ,
566+ variant : Schema . optional ( Schema . String ) ,
567+ finish : Schema . optional ( Schema . String ) ,
557568} )
558- export type Assistant = z . infer < typeof Assistant >
569+ . annotate ( { identifier : "AssistantMessage" } )
570+ . pipe ( withStatics ( ( s ) => ( { zod : zod ( s ) } ) ) )
571+ export type Assistant = Omit < Types . DeepMutable < Schema . Schema . Type < typeof Assistant > > , "error" > & {
572+ error ?: AssistantError
573+ }
559574
560- export const Info = z . discriminatedUnion ( "role" , [ User , Assistant ] ) . meta ( {
561- ref : "Message" ,
575+ const _Info = Schema . Union ( [ User , Assistant ] ) . annotate ( { discriminator : "role" , identifier : "Message" } )
576+ export const Info = Object . assign ( _Info , {
577+ zod : zod ( _Info ) as unknown as z . ZodType < User | Assistant > ,
562578} )
563- export type Info = z . infer < typeof Info >
579+ export type Info = User | Assistant
564580
565581export const Event = {
566582 Updated : SyncEvent . define ( {
@@ -569,7 +585,7 @@ export const Event = {
569585 aggregate : "sessionID" ,
570586 schema : z . object ( {
571587 sessionID : SessionID . zod ,
572- info : Info ,
588+ info : Info . zod ,
573589 } ) ,
574590 } ) ,
575591 Removed : SyncEvent . define ( {
@@ -587,7 +603,7 @@ export const Event = {
587603 aggregate : "sessionID" ,
588604 schema : z . object ( {
589605 sessionID : SessionID . zod ,
590- part : Part ,
606+ part : Part . zod ,
591607 time : z . number ( ) ,
592608 } ) ,
593609 } ) ,
@@ -613,10 +629,10 @@ export const Event = {
613629 } ) ,
614630}
615631
616- export const WithParts = z . object ( {
617- info : Info ,
618- parts : z . array ( Part ) ,
619- } )
632+ export const WithParts = Schema . Struct ( {
633+ info : _Info ,
634+ parts : Schema . Array ( _Part ) ,
635+ } ) . pipe ( withStatics ( ( s ) => ( { zod : zod ( s ) } ) ) )
620636export type WithParts = {
621637 info : Info
622638 parts : Part [ ]
0 commit comments