Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,16 +519,16 @@ await thread.AIContextProvider.InvokedAsync(new(inputMessages, aiContextProvider
{
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.
Expand Down Expand Up @@ -557,9 +557,9 @@ await thread.AIContextProvider.InvokedAsync(new(inputMessages, aiContextProvider
// 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
Expand Down Expand Up @@ -624,9 +624,9 @@ await thread.AIContextProvider.InvokedAsync(new(inputMessages, aiContextProvider
}
}

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)
{
Expand All @@ -643,6 +643,17 @@ await thread.AIContextProvider.InvokedAsync(new(inputMessages, aiContextProvider
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);
}
}
Expand Down
Loading
Loading