-
Notifications
You must be signed in to change notification settings - Fork 1.5k
.NET: Add AGUI session persistence #5113
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
Open
Kazunari001
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
Kazunari001:feature/add-AGUI-session-persistence
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
167 changes: 167 additions & 0 deletions
167
dotnet/src/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore/AGUIInMemorySessionStore.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,167 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Agents.AI.Hosting; | ||
| using Microsoft.Extensions.Caching.Memory; | ||
|
|
||
| namespace Microsoft.Agents.AI.Hosting.AGUI.AspNetCore; | ||
|
|
||
| /// <summary> | ||
| /// Provides an in-memory <see cref="AgentSessionStore"/> implementation for AG-UI hosted agents. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This store is intended for single-instance development and testing scenarios. Applications that need | ||
| /// durable or distributed session persistence can replace the registered <see cref="AgentSessionStore"/> | ||
| /// service with a custom implementation. | ||
| /// </remarks> | ||
| public sealed class AGUIInMemorySessionStore : AgentSessionStore, IDisposable | ||
| { | ||
| private readonly MemoryCache _cache; | ||
| private readonly MemoryCacheEntryOptions _entryOptions; | ||
| private readonly ConcurrentDictionary<SessionCacheKey, TaskCompletionSource<AgentSession>> _sessionInitializationTasks = new(); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AGUIInMemorySessionStore"/> class with default options. | ||
| /// </summary> | ||
| public AGUIInMemorySessionStore() | ||
| : this(options: null) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AGUIInMemorySessionStore"/> class. | ||
| /// </summary> | ||
| /// <param name="options">The cache options to apply. If <see langword="null"/>, default options are used.</param> | ||
| public AGUIInMemorySessionStore(AGUIInMemorySessionStoreOptions? options) | ||
| { | ||
| AGUIInMemorySessionStoreOptions resolvedOptions = options ?? new(); | ||
| this._cache = new MemoryCache(resolvedOptions.ToMemoryCacheOptions()); | ||
| this._entryOptions = resolvedOptions.ToMemoryCacheEntryOptions(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override ValueTask<AgentSession> GetSessionAsync(AIAgent agent, string conversationId, CancellationToken cancellationToken = default) | ||
| => this.GetOrCreateSessionAsync(agent, conversationId, cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Gets the session for the specified conversation or creates a new one when none exists. | ||
| /// </summary> | ||
| /// <param name="agent">The agent that owns the session.</param> | ||
| /// <param name="threadId">The conversation or thread identifier.</param> | ||
| /// <param name="cancellationToken">The cancellation token.</param> | ||
| /// <returns>The existing or newly created session.</returns> | ||
| public ValueTask<AgentSession> GetOrCreateSessionAsync(AIAgent agent, string threadId, CancellationToken cancellationToken = default) | ||
| { | ||
| SessionCacheKey key = GetKey(agent, threadId); | ||
| if (this._cache.TryGetValue(key, out AgentSession? session) && session is not null) | ||
| { | ||
| return new(session); | ||
| } | ||
|
|
||
| return this.GetOrCreateSessionCoreAsync(agent, key, cancellationToken); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override ValueTask SaveSessionAsync(AIAgent agent, string conversationId, AgentSession session, CancellationToken cancellationToken = default) | ||
| { | ||
| this._cache.Set(GetKey(agent, conversationId), session, this._entryOptions); | ||
| return ValueTask.CompletedTask; | ||
| } | ||
|
|
||
| private async ValueTask<AgentSession> GetOrCreateSessionCoreAsync(AIAgent agent, SessionCacheKey key, CancellationToken cancellationToken) | ||
| { | ||
| TaskCompletionSource<AgentSession> initializationTask = new(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| TaskCompletionSource<AgentSession> sharedInitializationTask = this._sessionInitializationTasks.GetOrAdd(key, initializationTask); | ||
|
|
||
| if (ReferenceEquals(sharedInitializationTask, initializationTask)) | ||
| { | ||
| if (this._cache.TryGetValue(key, out AgentSession? existingSession) && existingSession is not null) | ||
| { | ||
| initializationTask.TrySetResult(existingSession); | ||
| this._sessionInitializationTasks.TryRemove(new KeyValuePair<SessionCacheKey, TaskCompletionSource<AgentSession>>(key, initializationTask)); | ||
| } | ||
| else | ||
| { | ||
| _ = this.InitializeSessionAsync(agent, key, initializationTask); | ||
| } | ||
| } | ||
|
|
||
| return await sharedInitializationTask.Task.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
| private async Task<AgentSession> CreateAndStoreSessionAsync(AIAgent agent, SessionCacheKey key) | ||
| { | ||
| AgentSession session = await agent.CreateSessionAsync(CancellationToken.None).ConfigureAwait(false); | ||
| return this._cache.GetOrCreate(key, entry => | ||
| { | ||
| entry.SetOptions(this._entryOptions); | ||
| return session; | ||
| })!; | ||
| } | ||
|
|
||
Kazunari001 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private async Task InitializeSessionAsync(AIAgent agent, SessionCacheKey key, TaskCompletionSource<AgentSession> initializationTask) | ||
| { | ||
| try | ||
| { | ||
| AgentSession session = await this.CreateAndStoreSessionAsync(agent, key).ConfigureAwait(false); | ||
| initializationTask.TrySetResult(session); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| initializationTask.TrySetException(ex); | ||
| } | ||
| finally | ||
| { | ||
| this._sessionInitializationTasks.TryRemove(new KeyValuePair<SessionCacheKey, TaskCompletionSource<AgentSession>>(key, initializationTask)); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Releases the underlying memory cache. | ||
| /// </summary> | ||
| public void Dispose() | ||
| { | ||
| this._cache.Dispose(); | ||
| } | ||
|
|
||
| private static SessionCacheKey GetKey(AIAgent agent, string threadId) => new(agent.Id, threadId); | ||
|
|
||
| private sealed record SessionCacheKey(string AgentId, string ThreadId); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures the default <see cref="AGUIInMemorySessionStore"/> registration used by <c>AddAGUI</c>. | ||
| /// </summary> | ||
| public sealed class AGUIInMemorySessionStoreOptions | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the maximum number of sessions to retain in memory. | ||
| /// </summary> | ||
| public long? SizeLimit { get; set; } = 1000; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the absolute expiration applied to cached sessions. | ||
| /// </summary> | ||
| public TimeSpan? AbsoluteExpirationRelativeToNow { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the sliding expiration applied to cached sessions. | ||
| /// </summary> | ||
| public TimeSpan? SlidingExpiration { get; set; } = TimeSpan.FromHours(1); | ||
|
|
||
| internal MemoryCacheOptions ToMemoryCacheOptions() => new() | ||
| { | ||
| SizeLimit = this.SizeLimit | ||
| }; | ||
|
|
||
| internal MemoryCacheEntryOptions ToMemoryCacheEntryOptions() => new() | ||
| { | ||
| AbsoluteExpirationRelativeToNow = this.AbsoluteExpirationRelativeToNow, | ||
| SlidingExpiration = this.SlidingExpiration, | ||
| Size = 1 | ||
| }; | ||
| } | ||
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.