Skip to content

Conversation

@stephentoub
Copy link
Contributor

Consuming libraries like Agent Framework sometimes have a need to clone the various options bags, e.g. their caller passes in options and that middle library needs to tweak the settings before passing them along (e.g. set Streaming to true or false) but it doesn't want to mutate the caller's object. Without clone methods, such libraries need to manually copy every property, which then means when new properties are added, they get ignored and options are lost.

This PR adds such public Clone methods, and accomodates the non-sealed nature of the types by adding protected copy constructors that the virtual Clone methods use. (If instead we want to seal these types, that'd be viable as well.)

…tion bags

Consuming libraries like Agent Framework sometimes have a need to clone the various options bags, e.g. their caller passes in options and that middle library needs to tweak the settings before passing them along (e.g. set Streaming to true or false) but it doesn't want to mutate the caller's object. Without clone methods, such libraries need to manually copy every property, which then means when new properties are added, they get ignored and options are lost.

This PR adds such public Clone methods, and accomodates the non-sealed nature of the types by adding protected copy constructors that the virtual Clone methods use. (If instead we want to seal these types, that'd be viable as well.)
@stephentoub stephentoub requested a review from a team as a code owner February 10, 2026 15:13
Copilot AI review requested due to automatic review settings February 10, 2026 15:13
Copy link
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 adds cloning support to key .NET SDK option/config “bags” to let middle-layer libraries adjust settings without mutating caller-provided instances, by introducing protected copy constructors and public virtual Clone() methods.

Changes:

  • Added protected copy constructors for CopilotClientOptions, SessionConfig, ResumeSessionConfig, and MessageOptions.
  • Added public virtual Clone() methods that use those copy constructors.
  • Implemented defensive copies for several collection properties (arrays/lists/dictionaries).
Comments suppressed due to low confidence (1)

dotnet/src/Types.cs:878

  • Same as in SessionConfig: cloning McpServers with new Dictionary<string, object>(...) drops any custom key comparer from the original dictionary. If callers can pass in dictionaries with non-default comparers, preserve that comparer when cloning from a Dictionary<string, object> instance.
        InfiniteSessions = other.InfiniteSessions;
        McpServers = other.McpServers is not null ? new Dictionary<string, object>(other.McpServers) : null;
        Model = other.Model;

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.

1 participant