-
Notifications
You must be signed in to change notification settings - Fork 1.2k
.NET: Add helpers to more easily access in-memory ChatHistory and make ChatHistoryProvider management more configurable. #4224
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 5 commits into
microsoft:main
from
westey-m:message-access-and-chathistoryprovider-management
Feb 26, 2026
+510
−29
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b14b00
Add helpers to more easily access in-memory ChatHistory and make Chat…
westey-m e3328b1
Apply suggestion from @Copilot
westey-m 00c4602
Merge branch 'main' into message-access-and-chathistoryprovider-manag…
westey-m aaec335
Merge branch 'main' into message-access-and-chathistoryprovider-manag…
westey-m 795d607
Merge branch 'main' into message-access-and-chathistoryprovider-manag…
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
67 changes: 67 additions & 0 deletions
67
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSessionExtensions.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,67 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Text.Json; | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.Shared.Diagnostics; | ||
|
|
||
| namespace Microsoft.Agents.AI; | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for <see cref="AgentSession"/>. | ||
| /// </summary> | ||
| public static class AgentSessionExtensions | ||
| { | ||
| /// <summary> | ||
| /// Attempts to retrieve the in-memory chat history messages associated with the specified agent session, if the agent is storing memories in the session using the <see cref="InMemoryChatHistoryProvider"/> | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This method is only applicable when using <see cref="InMemoryChatHistoryProvider"/> and if the service does not require in-service chat history storage. | ||
| /// </remarks> | ||
| /// <param name="session">The agent session from which to retrieve in-memory chat history.</param> | ||
| /// <param name="messages">When this method returns, contains the list of chat history messages if available; otherwise, null.</param> | ||
| /// <param name="stateKey">An optional key used to identify the chat history state in the session's state bag. If null, the default key for | ||
| /// in-memory chat history is used.</param> | ||
| /// <param name="jsonSerializerOptions">Optional JSON serializer options to use when accessing the session state. If null, default options are used.</param> | ||
| /// <returns><see langword="true"/> if the in-memory chat history messages were found and retrieved; <see langword="false"/> otherwise.</returns> | ||
| public static bool TryGetInMemoryChatHistory(this AgentSession session, [MaybeNullWhen(false)] out List<ChatMessage> messages, string? stateKey = null, JsonSerializerOptions? jsonSerializerOptions = null) | ||
| { | ||
| _ = Throw.IfNull(session); | ||
|
|
||
| if (session.StateBag.TryGetValue(stateKey ?? nameof(InMemoryChatHistoryProvider), out InMemoryChatHistoryProvider.State? state, jsonSerializerOptions ?? AgentAbstractionsJsonUtilities.DefaultOptions) && state?.Messages is not null) | ||
| { | ||
| messages = state.Messages; | ||
| return true; | ||
| } | ||
|
|
||
| messages = null; | ||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the in-memory chat message history for the specified agent session, replacing any existing messages. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This method is only applicable when using <see cref="InMemoryChatHistoryProvider"/> and if the service does not require in-service chat history storage. | ||
| /// If messages are set, but a different <see cref="ChatHistoryProvider"/> is used, or if chat history is stored in the underlying AI service, the messages will be ignored. | ||
| /// </remarks> | ||
| /// <param name="session">The agent session whose in-memory chat history will be updated.</param> | ||
| /// <param name="messages">The list of chat messages to store in memory for the session. Replaces any existing messages for the specified | ||
| /// state key.</param> | ||
| /// <param name="stateKey">The key used to identify the in-memory chat history within the session's state bag. If null, a default key is | ||
| /// used.</param> | ||
| /// <param name="jsonSerializerOptions">The serializer options used when accessing or storing the state. If null, default options are applied.</param> | ||
| public static void SetInMemoryChatHistory(this AgentSession session, List<ChatMessage> messages, string? stateKey = null, JsonSerializerOptions? jsonSerializerOptions = null) | ||
| { | ||
| _ = Throw.IfNull(session); | ||
|
|
||
| if (session.StateBag.TryGetValue(stateKey ?? nameof(InMemoryChatHistoryProvider), out InMemoryChatHistoryProvider.State? state, jsonSerializerOptions ?? AgentAbstractionsJsonUtilities.DefaultOptions) && state is not null) | ||
| { | ||
| state.Messages = messages; | ||
| return; | ||
| } | ||
|
|
||
| session.StateBag.SetValue(stateKey ?? nameof(InMemoryChatHistoryProvider), new InMemoryChatHistoryProvider.State() { Messages = messages }, jsonSerializerOptions ?? AgentAbstractionsJsonUtilities.DefaultOptions); | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.