-
Notifications
You must be signed in to change notification settings - Fork 992
.NET: [BREAKING] Add GetNewThread overload for taking a ChatMessageStore at thread creation time #2429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
westey-m
merged 7 commits into
microsoft:main
from
westey-m:chatclientagent-getnewthread-store-overload
Nov 25, 2025
Merged
.NET: [BREAKING] Add GetNewThread overload for taking a ChatMessageStore at thread creation time #2429
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2c7d73c
Add GetNewThread overload for taking a ChatMessageStore at thread cre…
westey-m 887453b
Fix formatting.
westey-m bfd4993
Apply suggestions from code review
westey-m 667ba9d
Merge branch 'main' into chatclientagent-getnewthread-store-overload
westey-m fe6e743
Merge branch 'main' into chatclientagent-getnewthread-store-overload
westey-m c024790
Merge branch 'main' into chatclientagent-getnewthread-store-overload
westey-m 246fc8c
Apply suggestion from @rogerbarreto
westey-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
.../tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_DeserializeThreadTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Text.Json; | ||
| using Microsoft.Extensions.AI; | ||
| using Moq; | ||
|
|
||
| namespace Microsoft.Agents.AI.UnitTests.ChatClient; | ||
|
|
||
| /// <summary> | ||
| /// Contains unit tests for the ChatClientAgent.DeserializeThread methods. | ||
| /// </summary> | ||
| public class ChatClientAgent_DeserializeThreadTests | ||
| { | ||
| [Fact] | ||
| public void DeserializeThread_UsesAIContextProviderFactory_IfProvided() | ||
| { | ||
| // Arrange | ||
| var mockChatClient = new Mock<IChatClient>(); | ||
| var mockContextProvider = new Mock<AIContextProvider>(); | ||
| var factoryCalled = false; | ||
| var agent = new ChatClientAgent(mockChatClient.Object, new ChatClientAgentOptions | ||
| { | ||
| Instructions = "Test instructions", | ||
| AIContextProviderFactory = _ => | ||
| { | ||
| factoryCalled = true; | ||
| return mockContextProvider.Object; | ||
| } | ||
| }); | ||
|
|
||
| var json = JsonSerializer.Deserialize(""" | ||
| { | ||
| "aiContextProviderState": ["CP1"] | ||
| } | ||
| """, TestJsonSerializerContext.Default.JsonElement); | ||
|
|
||
| // Act | ||
| var thread = agent.DeserializeThread(json); | ||
|
|
||
| // Assert | ||
| Assert.True(factoryCalled, "AIContextProviderFactory was not called."); | ||
| Assert.IsType<ChatClientAgentThread>(thread); | ||
| var typedThread = (ChatClientAgentThread)thread; | ||
| Assert.Same(mockContextProvider.Object, typedThread.AIContextProvider); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DeserializeThread_UsesChatMessageStoreFactory_IfProvided() | ||
| { | ||
| // Arrange | ||
| var mockChatClient = new Mock<IChatClient>(); | ||
| var mockMessageStore = new Mock<ChatMessageStore>(); | ||
| var factoryCalled = false; | ||
| var agent = new ChatClientAgent(mockChatClient.Object, new ChatClientAgentOptions | ||
| { | ||
| Instructions = "Test instructions", | ||
| ChatMessageStoreFactory = _ => | ||
| { | ||
| factoryCalled = true; | ||
| return mockMessageStore.Object; | ||
| } | ||
| }); | ||
|
|
||
| var json = JsonSerializer.Deserialize(""" | ||
| { | ||
| "storeState": { } | ||
| } | ||
| """, TestJsonSerializerContext.Default.JsonElement); | ||
|
|
||
| // Act | ||
| var thread = agent.DeserializeThread(json); | ||
|
|
||
| // Assert | ||
| Assert.True(factoryCalled, "ChatMessageStoreFactory was not called."); | ||
| Assert.IsType<ChatClientAgentThread>(thread); | ||
| var typedThread = (ChatClientAgentThread)thread; | ||
| Assert.Same(mockMessageStore.Object, typedThread.MessageStore); | ||
| } | ||
| } |
100 changes: 100 additions & 0 deletions
100
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_GetNewThreadTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using Microsoft.Extensions.AI; | ||
| using Moq; | ||
|
|
||
| namespace Microsoft.Agents.AI.UnitTests.ChatClient; | ||
|
|
||
| /// <summary> | ||
| /// Contains unit tests for the ChatClientAgent.GetNewThread methods. | ||
| /// </summary> | ||
| public class ChatClientAgent_GetNewThreadTests | ||
| { | ||
| [Fact] | ||
| public void GetNewThread_UsesAIContextProviderFactory_IfProvided() | ||
| { | ||
| // Arrange | ||
| var mockChatClient = new Mock<IChatClient>(); | ||
| var mockContextProvider = new Mock<AIContextProvider>(); | ||
| var factoryCalled = false; | ||
| var agent = new ChatClientAgent(mockChatClient.Object, new ChatClientAgentOptions | ||
| { | ||
| Instructions = "Test instructions", | ||
| AIContextProviderFactory = _ => | ||
| { | ||
| factoryCalled = true; | ||
| return mockContextProvider.Object; | ||
| } | ||
| }); | ||
|
|
||
| // Act | ||
| var thread = agent.GetNewThread(); | ||
|
|
||
| // Assert | ||
| Assert.True(factoryCalled, "AIContextProviderFactory was not called."); | ||
| Assert.IsType<ChatClientAgentThread>(thread); | ||
| var typedThread = (ChatClientAgentThread)thread; | ||
| Assert.Same(mockContextProvider.Object, typedThread.AIContextProvider); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetNewThread_UsesChatMessageStoreFactory_IfProvided() | ||
| { | ||
| // Arrange | ||
| var mockChatClient = new Mock<IChatClient>(); | ||
| var mockMessageStore = new Mock<ChatMessageStore>(); | ||
| var factoryCalled = false; | ||
| var agent = new ChatClientAgent(mockChatClient.Object, new ChatClientAgentOptions | ||
| { | ||
| Instructions = "Test instructions", | ||
| ChatMessageStoreFactory = _ => | ||
| { | ||
| factoryCalled = true; | ||
| return mockMessageStore.Object; | ||
| } | ||
| }); | ||
|
|
||
| // Act | ||
| var thread = agent.GetNewThread(); | ||
|
|
||
| // Assert | ||
| Assert.True(factoryCalled, "ChatMessageStoreFactory was not called."); | ||
| Assert.IsType<ChatClientAgentThread>(thread); | ||
| var typedThread = (ChatClientAgentThread)thread; | ||
| Assert.Same(mockMessageStore.Object, typedThread.MessageStore); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetNewThread_UsesChatMessageStore_FromTypedOverload() | ||
| { | ||
| // Arrange | ||
| var mockChatClient = new Mock<IChatClient>(); | ||
| var mockMessageStore = new Mock<ChatMessageStore>(); | ||
| var agent = new ChatClientAgent(mockChatClient.Object); | ||
|
|
||
| // Act | ||
| var thread = agent.GetNewThread(mockMessageStore.Object); | ||
|
|
||
| // Assert | ||
| Assert.IsType<ChatClientAgentThread>(thread); | ||
| var typedThread = (ChatClientAgentThread)thread; | ||
| Assert.Same(mockMessageStore.Object, typedThread.MessageStore); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetNewThread_UsesConversationId_FromTypedOverload() | ||
| { | ||
| // Arrange | ||
| var mockChatClient = new Mock<IChatClient>(); | ||
| const string TestConversationId = "test_conversation_id"; | ||
| var agent = new ChatClientAgent(mockChatClient.Object); | ||
|
|
||
| // Act | ||
| var thread = agent.GetNewThread(TestConversationId); | ||
|
|
||
| // Assert | ||
| Assert.IsType<ChatClientAgentThread>(thread); | ||
| var typedThread = (ChatClientAgentThread)thread; | ||
| Assert.Equal(TestConversationId, typedThread.ConversationId); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.