.NET: Merge AgentRunOptions.AdditionalProperties into ChatOptions.AdditionalProperties (#3184)

* Merge AgentRunOptions.AdditionalProperties into ChatOptions.AdditionalProperties

* Fix namespace and typo.
This commit is contained in:
westey
2026-01-15 12:15:24 +00:00
committed by GitHub
Unverified
parent 15d0c34d9f
commit 3dbdecedda
3 changed files with 461 additions and 435 deletions
@@ -519,16 +519,16 @@ public sealed partial class ChatClientAgent : AIAgent
{
ChatOptions? requestChatOptions = (runOptions as ChatClientAgentRunOptions)?.ChatOptions?.Clone();
// If no agent chat options were provided, return the request chat options as is.
// If no agent chat options were provided, return the request chat options with just agent run options overrides.
if (this._agentOptions?.ChatOptions is null)
{
return GetContinuationTokenAndApplyBackgroundResponsesProperties(requestChatOptions, runOptions);
return ApplyAgentRunOptionsOverrides(requestChatOptions, runOptions);
}
// If no request chat options were provided, use the agent's chat options clone.
// If no request chat options were provided, use the agent's chat options clone with agent run options overrides.
if (requestChatOptions is null)
{
return GetContinuationTokenAndApplyBackgroundResponsesProperties(this._agentOptions?.ChatOptions.Clone(), runOptions);
return ApplyAgentRunOptionsOverrides(this._agentOptions?.ChatOptions.Clone(), runOptions);
}
// If both are present, we need to merge them.
@@ -557,9 +557,9 @@ public sealed partial class ChatClientAgent : AIAgent
// Merge only the additional properties from the agent if they are not already set in the request options.
if (requestChatOptions.AdditionalProperties is not null && this._agentOptions.ChatOptions.AdditionalProperties is not null)
{
foreach (var propertyKey in this._agentOptions.ChatOptions.AdditionalProperties.Keys)
foreach (var kvp in this._agentOptions.ChatOptions.AdditionalProperties)
{
_ = requestChatOptions.AdditionalProperties.TryAdd(propertyKey, this._agentOptions.ChatOptions.AdditionalProperties[propertyKey]);
_ = requestChatOptions.AdditionalProperties.TryAdd(kvp.Key, kvp.Value);
}
}
else
@@ -624,9 +624,9 @@ public sealed partial class ChatClientAgent : AIAgent
}
}
return GetContinuationTokenAndApplyBackgroundResponsesProperties(requestChatOptions, runOptions);
return ApplyAgentRunOptionsOverrides(requestChatOptions, runOptions);
static (ChatOptions?, ChatClientAgentContinuationToken?) GetContinuationTokenAndApplyBackgroundResponsesProperties(ChatOptions? chatOptions, AgentRunOptions? agentRunOptions)
static (ChatOptions?, ChatClientAgentContinuationToken?) ApplyAgentRunOptionsOverrides(ChatOptions? chatOptions, AgentRunOptions? agentRunOptions)
{
if (agentRunOptions?.AllowBackgroundResponses is not null)
{
@@ -643,6 +643,17 @@ public sealed partial class ChatClientAgent : AIAgent
chatOptions.ContinuationToken = agentContinuationToken!.InnerToken;
}
// Add/Replace any additional properties from the AgentRunOptions, since they should always take precedence.
if (agentRunOptions?.AdditionalProperties is { Count: > 0 })
{
chatOptions ??= new ChatOptions();
chatOptions.AdditionalProperties ??= new();
foreach (var kvp in agentRunOptions.AdditionalProperties)
{
chatOptions.AdditionalProperties[kvp.Key] = kvp.Value;
}
}
return (chatOptions, agentContinuationToken);
}
}