@@ -31,10 +31,13 @@ import {
3131 isObjectSchema ,
3232 isVoidSchema ,
3333 getNullableInner ,
34+ getSessionEventVariantSchemas ,
35+ getSharedSessionEventEnvelopeProperties ,
3436 REPO_ROOT ,
3537 type ApiSchema ,
3638 type DefinitionCollections ,
3739 type RpcMethod ,
40+ type SessionEventEnvelopeProperty ,
3841} from "./utils.js" ;
3942
4043const execFileAsync = promisify ( execFile ) ;
@@ -317,12 +320,6 @@ interface EventVariant {
317320 dataDescription ?: string ;
318321}
319322
320- interface EventEnvelopeProperty {
321- name : string ;
322- schema : JSONSchema7 ;
323- required : boolean ;
324- }
325-
326323let generatedEnums = new Map < string , { enumName : string ; values : string [ ] } > ( ) ;
327324
328325/** Schema definitions available during session event generation (for $ref resolution). */
@@ -369,63 +366,6 @@ function extractEventVariants(schema: JSONSchema7): EventVariant[] {
369366 } ) ;
370367}
371368
372- function getSessionEventVariantSchemas (
373- schema : JSONSchema7 ,
374- definitionCollections : DefinitionCollections = collectDefinitionCollections ( schema as Record < string , unknown > )
375- ) : JSONSchema7 [ ] {
376- const sessionEvent =
377- resolveSchema ( { $ref : "#/definitions/SessionEvent" } , definitionCollections ) ??
378- resolveSchema ( { $ref : "#/$defs/SessionEvent" } , definitionCollections ) ;
379- if ( ! sessionEvent ?. anyOf ) throw new Error ( "Schema must have SessionEvent definition with anyOf" ) ;
380-
381- return sessionEvent . anyOf . map ( ( variant ) => {
382- const resolvedVariant =
383- resolveObjectSchema ( variant as JSONSchema7 , definitionCollections ) ??
384- resolveSchema ( variant as JSONSchema7 , definitionCollections ) ??
385- ( variant as JSONSchema7 ) ;
386- if ( typeof resolvedVariant !== "object" || ! resolvedVariant . properties ) throw new Error ( "Invalid variant" ) ;
387- return resolvedVariant ;
388- } ) ;
389- }
390-
391- function getSharedEventEnvelopeProperties ( schema : JSONSchema7 ) : EventEnvelopeProperty [ ] {
392- const variants = getSessionEventVariantSchemas ( schema , sessionDefinitions ) ;
393- const firstVariant = variants [ 0 ] ;
394- const firstProperties = firstVariant . properties ?? { } ;
395-
396- return Object . entries ( firstProperties )
397- . filter ( ( [ name ] ) => name !== "type" && name !== "data" )
398- . map ( ( [ name ] ) => {
399- const propertySchemas = variants
400- . map ( ( variant ) => variant . properties ?. [ name ] )
401- . filter ( ( propSchema ) : propSchema is JSONSchema7 => typeof propSchema === "object" && propSchema !== null ) ;
402-
403- if ( propertySchemas . length !== variants . length ) return undefined ;
404-
405- return {
406- name,
407- schema : selectEnvelopePropertySchema ( propertySchemas ) ,
408- required : variants . every ( ( variant ) => ( variant . required ?? [ ] ) . includes ( name ) ) ,
409- } ;
410- } )
411- . filter ( ( property ) : property is EventEnvelopeProperty => property !== undefined ) ;
412- }
413-
414- function selectEnvelopePropertySchema ( propertySchemas : JSONSchema7 [ ] ) : JSONSchema7 {
415- // Some variants further constrain a shared envelope property, e.g. ephemeral const true.
416- // Generate the base property from the least restrictive schema that has useful metadata.
417- return (
418- propertySchemas . find ( ( schema ) => ! isConstOrEnumSchema ( schema ) && schema . description ) ??
419- propertySchemas . find ( ( schema ) => ! isConstOrEnumSchema ( schema ) ) ??
420- propertySchemas . find ( ( schema ) => schema . description ) ??
421- propertySchemas [ 0 ]
422- ) ;
423- }
424-
425- function isConstOrEnumSchema ( schema : JSONSchema7 ) : boolean {
426- return "const" in schema || ( Array . isArray ( schema . enum ) && schema . enum . length > 0 ) ;
427- }
428-
429369/**
430370 * Find a discriminator property shared by all variants in an anyOf.
431371 */
@@ -732,7 +672,7 @@ function generateDataClass(variant: EventVariant, knownTypes: Map<string, string
732672}
733673
734674function emitSessionEventEnvelopeProperty (
735- property : EventEnvelopeProperty ,
675+ property : SessionEventEnvelopeProperty ,
736676 knownTypes : Map < string , string > ,
737677 nestedClasses : Map < string , string > ,
738678 enumOutput : string [ ]
@@ -767,7 +707,7 @@ function generateSessionEventsCode(schema: JSONSchema7): string {
767707 const knownTypes = new Map < string , string > ( ) ;
768708 const nestedClasses = new Map < string , string > ( ) ;
769709 const enumOutput : string [ ] = [ ] ;
770- const envelopeProperties = getSharedEventEnvelopeProperties ( schema ) ;
710+ const envelopeProperties = getSharedSessionEventEnvelopeProperties ( schema , sessionDefinitions ) ;
771711
772712 const lines : string [ ] = [ ] ;
773713 lines . push ( `${ COPYRIGHT }
0 commit comments