From c063fc77e6f0f9578d9f741b5b377c08e5fafba5 Mon Sep 17 00:00:00 2001
From: westey <164392973+westey-m@users.noreply.github.com>
Date: Tue, 13 Jan 2026 19:47:57 +0000
Subject: [PATCH] .NET: Make ChatMessageStore and AIContextProvider context
props settable (#3196)
* Make ChatMessageStore and AIContextProvider context props setable
* Add validation to preserve non-null requirement of certain properties.
* Fix broken tests.
---
.../Microsoft.Agents.AI.Abstractions/AIContextProvider.cs | 6 +++---
.../Microsoft.Agents.AI.Abstractions/ChatMessageStore.cs | 8 ++++----
.../src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs | 2 +-
3 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider.cs b/dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider.cs
index fd3ff10fc2..f104f12890 100644
--- a/dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider.cs
+++ b/dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider.cs
@@ -142,7 +142,7 @@ public abstract class AIContextProvider
///
/// A collection of instances representing new messages that were provided by the caller.
///
- public IEnumerable RequestMessages { get; }
+ public IEnumerable RequestMessages { get; set { field = Throw.IfNull(value); } }
}
///
@@ -174,7 +174,7 @@ public abstract class AIContextProvider
/// A collection of instances representing new messages that were provided by the caller.
/// This does not include any supplied messages.
///
- public IEnumerable RequestMessages { get; }
+ public IEnumerable RequestMessages { get; set { field = Throw.IfNull(value); } }
///
/// Gets the messages provided by the for this invocation, if any.
@@ -183,7 +183,7 @@ public abstract class AIContextProvider
/// A collection of instances that were provided by the ,
/// and were used by the agent as part of the invocation.
///
- public IEnumerable? AIContextProviderMessages { get; }
+ public IEnumerable? AIContextProviderMessages { get; set; }
///
/// Gets the collection of response messages generated during this invocation if the invocation succeeded.
diff --git a/dotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageStore.cs b/dotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageStore.cs
index d28cd191b7..54cee063d7 100644
--- a/dotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageStore.cs
+++ b/dotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageStore.cs
@@ -152,7 +152,7 @@ public abstract class ChatMessageStore
///
/// A collection of instances representing new messages that were provided by the caller.
///
- public IEnumerable RequestMessages { get; }
+ public IEnumerable RequestMessages { get; set { field = Throw.IfNull(value); } }
}
///
@@ -174,7 +174,7 @@ public abstract class ChatMessageStore
public InvokedContext(IEnumerable requestMessages, IEnumerable chatMessageStoreMessages)
{
this.RequestMessages = Throw.IfNull(requestMessages);
- this.ChatMessageStoreMessages = chatMessageStoreMessages;
+ this.ChatMessageStoreMessages = Throw.IfNull(chatMessageStoreMessages);
}
///
@@ -184,7 +184,7 @@ public abstract class ChatMessageStore
/// A collection of instances representing new messages that were provided by the caller.
/// This does not include any supplied messages.
///
- public IEnumerable RequestMessages { get; }
+ public IEnumerable RequestMessages { get; set { field = Throw.IfNull(value); } }
///
/// Gets the messages retrieved from the for this invocation, if any.
@@ -193,7 +193,7 @@ public abstract class ChatMessageStore
/// A collection of instances that were retrieved from the ,
/// and were used by the agent as part of the invocation.
///
- public IEnumerable ChatMessageStoreMessages { get; }
+ public IEnumerable ChatMessageStoreMessages { get; set { field = Throw.IfNull(value); } }
///
/// Gets or sets the messages provided by the for this invocation, if any.
diff --git a/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs b/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
index 0c02932b0a..8f6d6a5160 100644
--- a/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
+++ b/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
@@ -692,7 +692,7 @@ public sealed partial class ChatClientAgent : AIAgent
List inputMessagesForChatClient = [];
IList? aiContextProviderMessages = null;
- IList? chatMessageStoreMessages = null;
+ IList? chatMessageStoreMessages = [];
// Populate the thread messages only if we are not continuing an existing response as it's not allowed
if (chatOptions?.ContinuationToken is null)