.NET: Add per run / thread feature collection support and improved custom ChatMessageStore support (#2345)

* Add the ability to override services on an agent per run.

* Remove Run from AgentFeatureCollection name.

* Adding features param to GetNewThread.

* Move feature collection.

* Add features to DeserializeThread

* Remove servicecollection based option

* Add feature collection unit tests and fix bug identified in code review.

* Add more unit tests for DelegatingAIAgent and AgentRunOptions

* Fix formatting.

* Address PR comments.

* Switch to dedicated ConversationIdAgentFeature and improve 3rd party storage samples.

* Fix bug in sample.
This commit is contained in:
westey
2025-11-20 18:33:14 +00:00
committed by GitHub
Unverified
parent 61dbacd6f8
commit eff5aee5aa
42 changed files with 1215 additions and 359 deletions
@@ -28,10 +28,10 @@ namespace SampleApp
{
public override string? Name => "UpperCaseParrotAgent";
public override AgentThread GetNewThread()
public override AgentThread GetNewThread(IAgentFeatureCollection? featureCollection = null)
=> new CustomAgentThread();
public override AgentThread DeserializeThread(JsonElement serializedThread, JsonSerializerOptions? jsonSerializerOptions = null)
public override AgentThread DeserializeThread(JsonElement serializedThread, JsonSerializerOptions? jsonSerializerOptions = null, IAgentFeatureCollection? featureCollection = null)
=> new CustomAgentThread(serializedThread, jsonSerializerOptions);
public override async Task<AgentRunResponse> RunAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
@@ -39,11 +39,16 @@ namespace SampleApp
// Create a thread if the user didn't supply one.
thread ??= this.GetNewThread();
if (thread is not CustomAgentThread typedThread)
{
throw new ArgumentException($"The provided thread is not of type {nameof(CustomAgentThread)}.", nameof(thread));
}
// Clone the input messages and turn them into response messages with upper case text.
List<ChatMessage> responseMessages = CloneAndToUpperCase(messages, this.DisplayName).ToList();
// Notify the thread of the input and output messages.
await NotifyThreadOfNewMessagesAsync(thread, messages.Concat(responseMessages), cancellationToken);
await typedThread.MessageStore.AddMessagesAsync(messages.Concat(responseMessages), cancellationToken);
return new AgentRunResponse
{
@@ -58,11 +63,16 @@ namespace SampleApp
// Create a thread if the user didn't supply one.
thread ??= this.GetNewThread();
if (thread is not CustomAgentThread typedThread)
{
throw new ArgumentException($"The provided thread is not of type {nameof(CustomAgentThread)}.", nameof(thread));
}
// Clone the input messages and turn them into response messages with upper case text.
List<ChatMessage> responseMessages = CloneAndToUpperCase(messages, this.DisplayName).ToList();
// Notify the thread of the input and output messages.
await NotifyThreadOfNewMessagesAsync(thread, messages.Concat(responseMessages), cancellationToken);
await typedThread.MessageStore.AddMessagesAsync(messages.Concat(responseMessages), cancellationToken);
foreach (var message in responseMessages)
{