@@ -21,6 +21,17 @@ const __dirname = path.dirname(__filename);
2121/** Root of the copilot-sdk repo */
2222export const REPO_ROOT = path . resolve ( __dirname , "../.." ) ;
2323
24+ /** Event types to exclude from generation (internal/legacy types) */
25+ export const EXCLUDED_EVENT_TYPES = new Set ( [ "session.import_legacy" ] ) ;
26+
27+ export interface JSONSchema7WithDefs extends JSONSchema7 {
28+ $defs ?: Record < string , JSONSchema7Definition > ;
29+ }
30+
31+ export type SchemaWithSharedDefinitions < T extends JSONSchema7 = JSONSchema7 > = T & {
32+ definitions : Record < string , JSONSchema7Definition > ;
33+ $defs : Record < string , JSONSchema7Definition > ;
34+ } ;
2435// ── Schema paths ────────────────────────────────────────────────────────────
2536
2637export async function getSessionEventsSchemaPath ( ) : Promise < string > {
@@ -51,16 +62,7 @@ export async function getApiSchemaPath(cliArg?: string): Promise<string> {
5162export function postProcessSchema ( schema : JSONSchema7 ) : JSONSchema7 {
5263 if ( typeof schema !== "object" || schema === null ) return schema ;
5364
54- const processed : JSONSchema7 = { ...schema } ;
55-
56- // Normalize $defs → definitions for draft 2019+ compatibility
57- if ( "$defs" in processed && ! processed . definitions ) {
58- processed . definitions = ( processed as Record < string , unknown > ) . $defs as Record <
59- string ,
60- JSONSchema7Definition
61- > ;
62- delete ( processed as Record < string , unknown > ) . $defs ;
63- }
65+ const processed = { ...schema } as JSONSchema7WithDefs ;
6466
6567 if ( "const" in processed && typeof processed . const === "boolean" ) {
6668 processed . enum = [ processed . const ] ;
@@ -93,12 +95,14 @@ export function postProcessSchema(schema: JSONSchema7): JSONSchema7 {
9395 }
9496 }
9597
96- if ( processed . definitions ) {
98+ const definitions = collectDefinitions ( processed as Record < string , unknown > ) ;
99+ if ( Object . keys ( definitions ) . length > 0 ) {
97100 const newDefs : Record < string , JSONSchema7Definition > = { } ;
98- for ( const [ key , value ] of Object . entries ( processed . definitions ) ) {
101+ for ( const [ key , value ] of Object . entries ( definitions ) ) {
99102 newDefs [ key ] = typeof value === "object" ? postProcessSchema ( value as JSONSchema7 ) : value ;
100103 }
101104 processed . definitions = newDefs ;
105+ processed . $defs = newDefs ;
102106 }
103107
104108 if ( typeof processed . additionalProperties === "object" ) {
@@ -339,6 +343,19 @@ export function resolveRef(
339343export function collectDefinitions (
340344 schema : Record < string , unknown >
341345) : Record < string , JSONSchema7Definition > {
342- const defs = ( schema . definitions ?? schema . $defs ?? { } ) as Record < string , JSONSchema7Definition > ;
343- return { ...defs }
346+ const legacyDefinitions = ( schema . definitions ?? { } ) as Record < string , JSONSchema7Definition > ;
347+ const draft2019Definitions = ( schema . $defs ?? { } ) as Record < string , JSONSchema7Definition > ;
348+ return { ...draft2019Definitions , ...legacyDefinitions } ;
349+ }
350+
351+ export function withSharedDefinitions < T extends JSONSchema7 > (
352+ schema : T ,
353+ definitions : Record < string , JSONSchema7Definition >
354+ ) : SchemaWithSharedDefinitions < T > {
355+ const sharedDefinitions = { ...definitions } ;
356+ return {
357+ ...schema ,
358+ definitions : sharedDefinitions ,
359+ $defs : sharedDefinitions ,
360+ } ;
344361}
0 commit comments