Skip to content

Commit f80196b

Browse files
committed
feat: #4869 add AGUI session persistence
1 parent d30103f commit f80196b

5 files changed

Lines changed: 399 additions & 8 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore/AGUIEndpointRouteBuilderExtensions.cs‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System.Collections.Generic;
44
using System.Diagnostics.CodeAnalysis;
55
using System.Linq;
6+
using System.Runtime.CompilerServices;
67
using System.Threading;
8+
using System.Threading.Tasks;
79
using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.Shared;
810
using Microsoft.AspNetCore.Builder;
911
using Microsoft.AspNetCore.Http;
@@ -42,6 +44,7 @@ public static IEndpointConventionBuilder MapAGUI(
4244

4345
var jsonOptions = context.RequestServices.GetRequiredService<IOptions<Microsoft.AspNetCore.Http.Json.JsonOptions>>();
4446
var jsonSerializerOptions = jsonOptions.Value.SerializerOptions;
47+
var sessionStore = context.RequestServices.GetRequiredService<AGUIInMemorySessionStore>();
4548

4649
var messages = input.Messages.AsChatMessages(jsonSerializerOptions);
4750
var clientTools = input.Tools?.AsAITools().ToList();
@@ -63,11 +66,17 @@ public static IEndpointConventionBuilder MapAGUI(
6366
}
6467
};
6568

69+
AgentSession? session = await GetOrCreateSessionAsync(aiAgent, input.ThreadId, sessionStore, cancellationToken).ConfigureAwait(false);
70+
6671
// Run the agent and convert to AG-UI events
67-
var events = aiAgent.RunStreamingAsync(
72+
var events = RunStreamingWithSessionPersistenceAsync(
73+
aiAgent,
6874
messages,
69-
options: runOptions,
70-
cancellationToken: cancellationToken)
75+
runOptions,
76+
session,
77+
input.ThreadId,
78+
sessionStore,
79+
cancellationToken)
7180
.AsChatResponseUpdatesAsync()
7281
.FilterServerToolsFromMixedToolInvocationsAsync(clientTools, cancellationToken)
7382
.AsAGUIEventStreamAsync(
@@ -80,4 +89,59 @@ public static IEndpointConventionBuilder MapAGUI(
8089
return new AGUIServerSentEventsResult(events, sseLogger);
8190
});
8291
}
92+
93+
private static async ValueTask<AgentSession?> GetOrCreateSessionAsync(
94+
AIAgent aiAgent,
95+
string? threadId,
96+
AGUIInMemorySessionStore sessionStore,
97+
CancellationToken cancellationToken)
98+
{
99+
if (string.IsNullOrWhiteSpace(threadId))
100+
{
101+
return null;
102+
}
103+
104+
return await sessionStore.GetOrCreateSessionAsync(aiAgent, threadId, cancellationToken).ConfigureAwait(false);
105+
}
106+
107+
private static async ValueTask PersistSessionAsync(
108+
AIAgent aiAgent,
109+
string? threadId,
110+
AgentSession? session,
111+
AGUIInMemorySessionStore sessionStore,
112+
CancellationToken cancellationToken)
113+
{
114+
if (session is null || string.IsNullOrWhiteSpace(threadId))
115+
{
116+
return;
117+
}
118+
119+
await sessionStore.SaveSessionAsync(aiAgent, threadId, session, cancellationToken).ConfigureAwait(false);
120+
}
121+
122+
private static async IAsyncEnumerable<AgentResponseUpdate> RunStreamingWithSessionPersistenceAsync(
123+
AIAgent aiAgent,
124+
IEnumerable<ChatMessage> messages,
125+
AgentRunOptions runOptions,
126+
AgentSession? session,
127+
string? threadId,
128+
AGUIInMemorySessionStore sessionStore,
129+
[EnumeratorCancellation] CancellationToken cancellationToken)
130+
{
131+
try
132+
{
133+
await foreach (AgentResponseUpdate update in aiAgent.RunStreamingAsync(
134+
messages,
135+
session,
136+
runOptions,
137+
cancellationToken).ConfigureAwait(false))
138+
{
139+
yield return update;
140+
}
141+
}
142+
finally
143+
{
144+
await PersistSessionAsync(aiAgent, threadId, session, sessionStore, cancellationToken).ConfigureAwait(false);
145+
}
146+
}
83147
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Microsoft.Extensions.Caching.Memory;
7+
8+
namespace Microsoft.Agents.AI.Hosting.AGUI.AspNetCore;
9+
10+
internal sealed class AGUIInMemorySessionStore : IDisposable
11+
{
12+
private readonly MemoryCache _cache;
13+
private readonly MemoryCacheEntryOptions _entryOptions;
14+
15+
public AGUIInMemorySessionStore()
16+
: this(options: null)
17+
{
18+
}
19+
20+
internal AGUIInMemorySessionStore(AGUIInMemorySessionStoreOptions? options)
21+
{
22+
AGUIInMemorySessionStoreOptions resolvedOptions = options ?? new();
23+
this._cache = new MemoryCache(resolvedOptions.ToMemoryCacheOptions());
24+
this._entryOptions = resolvedOptions.ToMemoryCacheEntryOptions();
25+
}
26+
27+
public ValueTask<AgentSession> GetOrCreateSessionAsync(AIAgent agent, string threadId, CancellationToken cancellationToken = default)
28+
{
29+
string key = GetKey(agent, threadId);
30+
if (this._cache.TryGetValue(key, out AgentSession? session) && session is not null)
31+
{
32+
return new(session);
33+
}
34+
35+
return this.CreateAndStoreSessionAsync(agent, key, cancellationToken);
36+
}
37+
38+
public ValueTask SaveSessionAsync(AIAgent agent, string threadId, AgentSession session, CancellationToken cancellationToken = default)
39+
{
40+
this._cache.Set(GetKey(agent, threadId), session, this._entryOptions);
41+
return ValueTask.CompletedTask;
42+
}
43+
44+
private async ValueTask<AgentSession> CreateAndStoreSessionAsync(AIAgent agent, string key, CancellationToken cancellationToken)
45+
{
46+
AgentSession session = await agent.CreateSessionAsync(cancellationToken).ConfigureAwait(false);
47+
return this._cache.GetOrCreate(key, entry =>
48+
{
49+
entry.SetOptions(this._entryOptions);
50+
return session;
51+
})!;
52+
}
53+
54+
public void Dispose()
55+
{
56+
this._cache.Dispose();
57+
}
58+
59+
private static string GetKey(AIAgent agent, string threadId) => $"{agent.Id}:{threadId}";
60+
}
61+
62+
internal sealed class AGUIInMemorySessionStoreOptions
63+
{
64+
public long? SizeLimit { get; set; } = 1000;
65+
66+
public TimeSpan? AbsoluteExpirationRelativeToNow { get; set; }
67+
68+
public TimeSpan? SlidingExpiration { get; set; } = TimeSpan.FromHours(1);
69+
70+
internal MemoryCacheOptions ToMemoryCacheOptions() => new()
71+
{
72+
SizeLimit = this.SizeLimit
73+
};
74+
75+
internal MemoryCacheEntryOptions ToMemoryCacheEntryOptions() => new()
76+
{
77+
AbsoluteExpirationRelativeToNow = this.AbsoluteExpirationRelativeToNow,
78+
SlidingExpiration = this.SlidingExpiration,
79+
Size = 1
80+
};
81+
}

‎dotnet/src/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore/ServiceCollectionExtensions.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static IServiceCollection AddAGUI(this IServiceCollection services)
2222
ArgumentNullException.ThrowIfNull(services);
2323

2424
services.Configure<JsonOptions>(options => options.SerializerOptions.TypeInfoResolverChain.Add(AGUIJsonSerializerOptions.Default.TypeInfoResolver!));
25+
services.AddSingleton<AGUIInMemorySessionStore>();
2526

2627
return services;
2728
}

‎dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/BasicStreamingTests.cs‎

Lines changed: 142 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,42 @@ public async Task MultiTurnConversationPreservesAllMessagesInSessionAsync()
163163
secondResponse.Messages[0].Text.Should().Be("Hello from fake agent!");
164164
}
165165

166+
[Fact]
167+
public async Task ChatClientConversationId_RestoresServerSessionAcrossRequestsAsync()
168+
{
169+
// Arrange
170+
await this.SetupTestServerAsync(useStatefulMemoryAgent: true);
171+
var chatClient = new AGUIChatClient(this._client!, "", null);
172+
var chatOptions = new ChatOptions();
173+
174+
// Act
175+
ChatResponse firstResponse = await chatClient.GetResponseAsync([new ChatMessage(ChatRole.User, "My name is Alice")], chatOptions);
176+
chatOptions.ConversationId = firstResponse.ConversationId;
177+
ChatResponse secondResponse = await chatClient.GetResponseAsync([new ChatMessage(ChatRole.User, "What is my name?")], chatOptions);
178+
179+
// Assert
180+
firstResponse.ConversationId.Should().NotBeNullOrEmpty();
181+
secondResponse.Text.Should().Contain("Alice");
182+
}
183+
184+
[Fact]
185+
public async Task AsAIAgentSession_RestoresServerSessionAcrossRunsAsync()
186+
{
187+
// Arrange
188+
await this.SetupTestServerAsync(useStatefulMemoryAgent: true);
189+
var chatClient = new AGUIChatClient(this._client!, "", null);
190+
AIAgent agent = chatClient.AsAIAgent(instructions: null, name: "assistant", description: "Stateful assistant", tools: []);
191+
AgentSession session = await agent.CreateSessionAsync();
192+
193+
// Act
194+
AgentResponse firstResponse = await agent.RunAsync("My name is Alice", session, new AgentRunOptions(), CancellationToken.None);
195+
AgentResponse secondResponse = await agent.RunAsync("What is my name?", session, new AgentRunOptions(), CancellationToken.None);
196+
197+
// Assert
198+
firstResponse.Text.Should().Contain("Alice");
199+
secondResponse.Text.Should().Contain("Alice");
200+
}
201+
166202
[Fact]
167203
public async Task AgentSendsMultipleMessagesInOneTurnAsync()
168204
{
@@ -231,14 +267,18 @@ public async Task UserSendsMultipleMessagesAtOnceAsync()
231267
response.Messages[0].Text.Should().Be("Hello from fake agent!");
232268
}
233269

234-
private async Task SetupTestServerAsync(bool useMultiMessageAgent = false)
270+
private async Task SetupTestServerAsync(bool useMultiMessageAgent = false, bool useStatefulMemoryAgent = false)
235271
{
236272
WebApplicationBuilder builder = WebApplication.CreateBuilder();
237273
builder.WebHost.UseTestServer();
238274

239275
builder.Services.AddAGUI();
240276

241-
if (useMultiMessageAgent)
277+
if (useStatefulMemoryAgent)
278+
{
279+
builder.Services.AddSingleton<FakeStatefulMemoryAgent>();
280+
}
281+
else if (useMultiMessageAgent)
242282
{
243283
builder.Services.AddSingleton<FakeMultiMessageAgent>();
244284
}
@@ -249,9 +289,11 @@ private async Task SetupTestServerAsync(bool useMultiMessageAgent = false)
249289

250290
this._app = builder.Build();
251291

252-
AIAgent agent = useMultiMessageAgent
253-
? this._app.Services.GetRequiredService<FakeMultiMessageAgent>()
254-
: this._app.Services.GetRequiredService<FakeChatClientAgent>();
292+
AIAgent agent = useStatefulMemoryAgent
293+
? this._app.Services.GetRequiredService<FakeStatefulMemoryAgent>()
294+
: useMultiMessageAgent
295+
? this._app.Services.GetRequiredService<FakeMultiMessageAgent>()
296+
: this._app.Services.GetRequiredService<FakeChatClientAgent>();
255297

256298
this._app.MapAGUI("/agent", agent);
257299

@@ -441,3 +483,98 @@ public FakeAgentSession(AgentSessionStateBag stateBag) : base(stateBag)
441483

442484
public override object? GetService(Type serviceType, object? serviceKey = null) => null;
443485
}
486+
487+
[SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "Instantiated via dependency injection")]
488+
internal sealed class FakeStatefulMemoryAgent : AIAgent
489+
{
490+
private const string NameStateKey = "remembered-name";
491+
492+
protected override string? IdCore => "fake-stateful-memory-agent";
493+
494+
public override string? Description => "A fake agent that stores simple memory in the session";
495+
496+
protected override ValueTask<AgentSession> CreateSessionCoreAsync(CancellationToken cancellationToken = default) =>
497+
new(new FakeAgentSession());
498+
499+
protected override ValueTask<AgentSession> DeserializeSessionCoreAsync(JsonElement serializedState, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default) =>
500+
new(serializedState.Deserialize<FakeAgentSession>(jsonSerializerOptions)!);
501+
502+
protected override ValueTask<JsonElement> SerializeSessionCoreAsync(AgentSession session, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
503+
{
504+
if (session is not FakeAgentSession typedSession)
505+
{
506+
throw new InvalidOperationException($"The provided session type '{session.GetType().Name}' is not compatible with this agent. Only sessions of type '{nameof(FakeAgentSession)}' can be serialized by this agent.");
507+
}
508+
509+
return new(JsonSerializer.SerializeToElement(typedSession, jsonSerializerOptions));
510+
}
511+
512+
protected override async Task<AgentResponse> RunCoreAsync(
513+
IEnumerable<ChatMessage> messages,
514+
AgentSession? session = null,
515+
AgentRunOptions? options = null,
516+
CancellationToken cancellationToken = default)
517+
{
518+
List<AgentResponseUpdate> updates = [];
519+
await foreach (AgentResponseUpdate update in this.RunStreamingAsync(messages, session, options, cancellationToken).ConfigureAwait(false))
520+
{
521+
updates.Add(update);
522+
}
523+
524+
return updates.ToAgentResponse();
525+
}
526+
527+
protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingAsync(
528+
IEnumerable<ChatMessage> messages,
529+
AgentSession? session = null,
530+
AgentRunOptions? options = null,
531+
[EnumeratorCancellation] CancellationToken cancellationToken = default)
532+
{
533+
if (session is not FakeAgentSession typedSession)
534+
{
535+
throw new InvalidOperationException($"The provided session type '{session?.GetType().Name ?? "null"}' is not compatible with this agent. Only sessions of type '{nameof(FakeAgentSession)}' can be used by this agent.");
536+
}
537+
538+
string lastUserInput = messages.Last(m => m.Role == ChatRole.User).Text ?? string.Empty;
539+
string responseText;
540+
541+
if (lastUserInput.StartsWith("My name is ", StringComparison.OrdinalIgnoreCase))
542+
{
543+
string rememberedName = lastUserInput[11..].Trim();
544+
typedSession.StateBag.SetValue(NameStateKey, rememberedName);
545+
responseText = $"Nice to meet you {rememberedName}.";
546+
}
547+
else if (lastUserInput.Equals("What is my name?", StringComparison.OrdinalIgnoreCase))
548+
{
549+
string? rememberedName = typedSession.StateBag.GetValue<string>(NameStateKey);
550+
responseText = rememberedName is null
551+
? "I do not know your name yet."
552+
: $"Your name is {rememberedName}.";
553+
}
554+
else
555+
{
556+
responseText = "I can remember your name if you tell me 'My name is ...'.";
557+
}
558+
559+
yield return new AgentResponseUpdate
560+
{
561+
MessageId = Guid.NewGuid().ToString("N"),
562+
Role = ChatRole.Assistant,
563+
Contents = [new TextContent(responseText)]
564+
};
565+
566+
await Task.Yield();
567+
}
568+
569+
private sealed class FakeAgentSession : AgentSession
570+
{
571+
public FakeAgentSession()
572+
{
573+
}
574+
575+
[JsonConstructor]
576+
public FakeAgentSession(AgentSessionStateBag stateBag) : base(stateBag)
577+
{
578+
}
579+
}
580+
}

0 commit comments

Comments
 (0)