Skip to content

Share generated schema definitions across SDKs#1289

Open
stephentoub wants to merge 4 commits into
mainfrom
stephentoub/shared-reasoning-summary-enum
Open

Share generated schema definitions across SDKs#1289
stephentoub wants to merge 4 commits into
mainfrom
stephentoub/shared-reasoning-summary-enum

Conversation

@stephentoub
Copy link
Copy Markdown
Collaborator

@stephentoub stephentoub commented May 14, 2026

Identical definitions can now appear in both runtime schema artifacts, including upcoming RPC references to session-event models. This updates SDK codegen so those definitions keep one semantic SDK type instead of being duplicated or degraded to strings.

Summary

  • Adds shared schema definition detection and reference rewriting across the language generators.
  • Regenerates SDK outputs so RPC/API models reuse session-event generated types for shared definitions and direct external session-event refs.
  • Moves Go's real generated session-event hierarchy into go/rpc, with root copilot aliases and const re-exports so there is one Go type identity for session events.
  • Adds focused shared-codegen regression coverage and Go alias identity checks.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 14, 2026 02:39
@stephentoub stephentoub requested a review from a team as a code owner May 14, 2026 02:39
Comment thread scripts/codegen/csharp.ts Fixed
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 code generators to detect identical JSON Schema definitions shared between api.schema.json and session-events.schema.json, and to rewrite $refs so SDKs reuse a single semantic type instead of duplicating models or degrading them (e.g., to strings). It also restructures Go’s generated session-event code into the go/rpc package and adds regression tests to lock in the new shared-definition behavior.

Changes:

  • Add shared-definition comparison, reachability analysis, external-ref parsing/collection, and $ref rewriting/inlining utilities to the codegen shared helpers.
  • Update TS/Python/Rust/C# (and Go) generators to rewrite schema references so generated RPC/API types can reuse session-event (or shared) models and emit the necessary imports/registrations.
  • Regenerate outputs and add focused regression coverage (Node shared-codegen tests; Go alias identity checks; Go rpc session encoding relocation).
Show a summary per file
File Description
scripts/codegen/utils.ts Adds stable stringify + external-ref parsing/collection + shared-definition detection + $ref rewriting/inlining helpers.
scripts/codegen/typescript.ts Rewrites RPC schema refs to shared session-event definitions and emits TS type-only imports for external refs.
scripts/codegen/rust.ts Supports external $ref type naming/imports and rewrites shared definitions to session-events types when generating Rust outputs.
scripts/codegen/python.ts Rewrites shared refs and post-processes quicktype output to replace external-ref placeholders with imported session-event types.
scripts/codegen/go.ts Adds external-ref-aware Go type resolution, moves session-events generation to go/rpc, emits root aliases/stubs, and adds duplicate-definition conflict checks.
scripts/codegen/csharp.ts Rewrites shared refs and extends source-gen JSON context to include externally referenced session-event types.
python/copilot/generated/rpc.py Regenerated Python RPC models to import and reuse session-event models instead of duplicating them.
nodejs/test/shared-codegen.test.ts Adds regression tests for shared-definition detection/rewriting and external definition inlining utilities.
nodejs/src/generated/rpc.ts Regenerated Node RPC types to import and reuse session-event types instead of duplicating interfaces.
go/zsession_encoding.go Replaced root encoding implementation with a stub comment (encoding now lives in go/rpc).
go/session_event_serialization_test.go Adds compile-time alias identity assertions between copilot and rpc session-event types.
go/rpc/zsession_encoding.go New generated session-event encoding implementation under the rpc package.

Copilot's findings

Files not reviewed (2)
  • go/rpc/zsession_encoding.go: Language not supported
  • go/zsession_encoding.go: Language not supported
  • Files reviewed: 8/14 changed files
  • Comments generated: 3

Comment thread scripts/codegen/utils.ts Outdated
Comment thread scripts/codegen/typescript.ts Outdated
Comment thread scripts/codegen/go.ts
@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Generated by SDK Consistency Review Agent for issue #1289 · ● 3.8M

Comment thread python/copilot/generated/rpc.py Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@stephentoub
Copy link
Copy Markdown
Collaborator Author

@SteveSandersonMS, how do you feel about this? We have a few changes coming down the pike from the runtime that will benefit from sharing a single set of types across both session events and rpc, e.g. rpc methods that return session events.

@qmuntal, Go by default would end up with a cycle. To address that, I have it emitting the types into rpc and then aliasing them from session events. An alternative would be putting the types into a third shared package. Preferences? Recommendations?

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread go/zsession_encoding.go Outdated
@github-actions

This comment has been minimized.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

Cross-SDK Consistency Review

This PR introduces shared schema definition detection across all language code generators, allowing types that appear identically in both the RPC schema and the session-events schema to be represented once rather than duplicated. Here's how the implementation lands across SDKs:

✅ TypeScript / Node.js

The generated rpc.ts correctly removes its inline EmbeddedBlobResourceContents and EmbeddedTextResourceContents definitions and instead imports them from session-events.ts. Single type identity is achieved — code using session events and code using RPC types will share the same TypeScript interfaces.

✅ Go

All session-event types are now generated into the go/rpc package (zsession_events.go), and the root copilot package re-exports them as type aliases in the updated zsession_events.go. This is a different structural approach than TypeScript but achieves the same semantic goal: a single runtime type identity for types shared between the two schemas.

⚠️ Python — codegen updated but deduplication didn't apply to generated output

The Python codegen (scripts/codegen/python.ts) was updated with the same findSharedSchemaDefinitions + rewriteSharedDefinitionReferences + collectPythonSessionEventExportedTypeNames infrastructure. However, the regenerated python/copilot/generated/rpc.py shows only blank-line changes — EmbeddedBlobResourceContents (line 439) and EmbeddedTextResourceContents (line 466) remain as inline dataclass definitions in rpc.py.

The reason is structural: collectPythonSessionEventExportedTypeNames only deduplicates when the type is already exported by Python's session events generator — but these two types do not appear in python/copilot/generated/session_events.py. (Compare: TypeScript's session-events.ts exports both at lines 2841/2855; Go's rpc/zsession_events.go references them as struct fields at lines 2445–2446.)

So Python currently has a structural gap versus TypeScript: the same types are inline-duplicated in rpc.py instead of being shared with the session events module. This also means the Python session events module types resource as Any in ToolExecutionCompleteContent while TypeScript uses the strongly typed EmbeddedTextResourceContents | EmbeddedBlobResourceContents union.

Is there a follow-up planned to also emit EmbeddedBlobResourceContents/EmbeddedTextResourceContents from the Python session events generator? That would allow the deduplication machinery added in this PR to actually fire for Python on the next codegen run.

i️ Rust and .NET

The codegen for both was updated with shared-definition utilities, but no generated files changed. This appears correct — the current schema state doesn't have cross-schema references that would trigger deduplication for these languages. The Rust api_types.rs still has inline definitions, but that's consistent with the current Python situation and the infrastructure is in place for when it's needed.


Overall the PR is well-structured and the core mechanism (TypeScript + Go) is working correctly. The Python gap is the main consistency item worth tracking as a follow-up.

Generated by SDK Consistency Review Agent for issue #1289 · ● 2.4M ·

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.

4 participants