Skip to content

Add $ref support to all four language code generators#1062

Open
stephentoub wants to merge 3 commits intomainfrom
stephentoub/add-ref-support-codegen
Open

Add $ref support to all four language code generators#1062
stephentoub wants to merge 3 commits intomainfrom
stephentoub/add-ref-support-codegen

Conversation

@stephentoub
Copy link
Copy Markdown
Collaborator

@stephentoub stephentoub commented Apr 10, 2026

Summary

Enable JSON Schema $ref for type deduplication across all SDK code generators (TypeScript, Python, Go, C#). This allows the runtime to declare a shared type once using $ref and have each generator produce a single deduplicated type per language.

Changes

scripts/codegen/utils.ts

  • Add resolveRef(), refTypeName(), collectDefinitions() helpers
  • Normalize $defs to definitions in postProcessSchema for cross-draft compatibility
  • Add definitions/$defs fields to ApiSchema interface

scripts/codegen/typescript.ts

  • Build a single combined schema with shared definitions and compile once via unreachableDefinitions, instead of per-method compilation
  • Preserves @experimental JSDoc annotations via post-processing

scripts/codegen/python.ts

  • Include all definitions alongside SessionEvent for quicktype $ref resolution
  • Include shared API definitions in each RPC addSource call

scripts/codegen/go.ts

  • Add $ref handling to the custom Go session events generator (resolveGoPropertyType) with definitions on GoCodegenCtx
  • Include shared API definitions in RPC combined schema for quicktype $ref resolution

scripts/codegen/csharp.ts

  • Add $ref handling to resolveSessionPropertyType and resolveRpcType
  • Generate classes for referenced types on demand using module-level sessionDefinitions/rpcDefinitions state
  • Handle $ref in array items

Motivation

The copilot-agent-runtime is moving to use $ref for type deduplication in tool schemas. Without this change, generators would either fail to resolve $ref pointers or inline duplicate type definitions.

@stephentoub stephentoub requested a review from a team as a code owner April 10, 2026 22:55
Copilot AI review requested due to automatic review settings April 10, 2026 22:55
@stephentoub stephentoub changed the title Add $ ef support to all four language code generators Add $ref support to all four language code generators Apr 10, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the multi-language schema-to-SDK generators (TypeScript, Python, Go, C#) to support JSON Schema $ref so shared definitions can be deduplicated and emitted once per language.

Changes:

  • Adds shared $ref utilities (resolveRef, refTypeName, collectDefinitions) and expands ApiSchema to surface schema definitions.
  • Refactors TypeScript RPC generation to compile a single combined schema (shared definitions + all RPC param/result types) in one pass.
  • Updates Python/Go quicktype inputs and C# custom emitters to include/resolve referenced definitions.
Show a summary per file
File Description
scripts/codegen/utils.ts Adds $ref helpers and attempts $defsdefinitions normalization in schema post-processing.
scripts/codegen/typescript.ts Generates RPC types from one combined schema to dedupe $ref-shared types; re-adds experimental annotations post-compile.
scripts/codegen/python.ts Includes shared definitions for quicktype to resolve $ref during session-event and RPC generation.
scripts/codegen/go.ts Adds $ref handling to Go session-events custom generator; includes shared definitions for RPC quicktype generation.
scripts/codegen/csharp.ts Adds $ref handling for session events + RPC generation, with on-demand referenced type emission.

Copilot's findings

Comments suppressed due to low confidence (1)

scripts/codegen/python.ts:23

  • The import list includes isRpcMethod twice, which will cause a TypeScript compile error (“Duplicate identifier 'isRpcMethod'”). Remove the duplicate specifier.
import {
    getApiSchemaPath,
    getSessionEventsSchemaPath,
    isRpcMethod,
    postProcessSchema,
    writeGeneratedFile,
    collectDefinitions,
    isRpcMethod,
    isNodeFullyExperimental,
    type ApiSchema,
    type RpcMethod,
} from "./utils.js";
  • Files reviewed: 5/5 changed files
  • Comments generated: 7

Enable JSON Schema $ref for type deduplication across all SDK code
generators (TypeScript, Python, Go, C#).

Changes:
- utils.ts: Add resolveRef(), refTypeName(), collectDefinitions() helpers;
  normalize $defs to definitions in postProcessSchema
- typescript.ts: Build combined schema with shared definitions and compile
  once via unreachableDefinitions, instead of per-method compilation
- python.ts/go.ts: Include all definitions alongside SessionEvent for
  quicktype  resolution; include shared API defs in RPC combined schema
- csharp.ts: Add  handling to resolveSessionPropertyType and
  resolveRpcType; generate classes for referenced types on demand

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep the rebased $ref generator follow-up aligned with the latest C# typing changes and clean up the Python/TypeScript generator adjustments.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@stephentoub stephentoub force-pushed the stephentoub/add-ref-support-codegen branch from c614eb4 to 17ba6bc Compare April 13, 2026 03:06
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants