.NET: fix(agui): create session in MapAGUI endpoint so middleware receives …#4013
Conversation
…non-null session (microsoft#3823) MapAGUI was calling RunStreamingAsync without passing the session parameter, causing it to always be null for all middleware decorators registered via AIAgentBuilder.Use(). Changes: - AGUIEndpointRouteBuilderExtensions.cs: create an AgentSession before invoking RunStreamingAsync and pass it through, so middleware receives a non-null session. - Add integration tests (SessionMiddlewareTests) covering: - Session propagation to streaming middleware - Session available via CurrentRunContext - Session propagation to shared Use() overload - Session.StateBag is functional Fixes microsoft#3823
|
@kallebelins please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where the session parameter was always null in middleware when agents are hosted via the AGUI endpoint (MapAGUI). The root cause was that RunStreamingAsync was called without passing the session parameter, causing it to default to null for all middleware decorators registered via AIAgentBuilder.Use().
Changes:
- Modified
AGUIEndpointRouteBuilderExtensions.csto create anAgentSessionbefore invokingRunStreamingAsyncand pass it through the middleware pipeline - Added comprehensive integration test suite
SessionMiddlewareTests.cswith 4 tests verifying session propagation through different middleware scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| dotnet/src/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore/AGUIEndpointRouteBuilderExtensions.cs | Creates session via CreateSessionAsync before calling RunStreamingAsync, ensuring middleware receives non-null session |
| dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/SessionMiddlewareTests.cs | Adds 4 integration tests validating session propagation to streaming middleware, CurrentRunContext, shared Use() overload, and StateBag functionality |
@microsoft-github-policy-service agree |
…non-null session (#3823)
MapAGUI was calling RunStreamingAsync without passing the session parameter, causing it to always be null for all middleware decorators registered via AIAgentBuilder.Use().
Changes:
Fixes #3823
Motivation and Context
When an agent is hosted via the AG-UI endpoint (
MapAGUI) and middleware is registered throughAIAgentBuilder.Use(), thesessionparameter passed to middleware delegates is alwaysnull. This prevents middleware from accessing session state, readingCurrentRunContext.Session, or usingSession.StateBagfor cross-cutting concerns such as logging, telemetry, or state management.This change fixes the root cause by creating an
AgentSessionin theMapAGUIendpoint before invokingRunStreamingAsync, ensuring the session is properly propagated through the entire middleware pipeline.Fixes #3823
Description
The fix modifies
AGUIEndpointRouteBuilderExtensions.csto create a session instance viaagent.CreateSessionAsync()before callingRunStreamingAsync, and passes it as thesessionparameter. Previously, the call omitted the session argument entirely, defaulting it tonull.A new integration test file
SessionMiddlewareTests.csis added toMicrosoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTestswith 4 regression tests that exercise the full round-trip (AGUIChatClient → HTTP → MapAGUI → middleware → agent) and validate:AGUIMiddleware_WithWorkflow_SessionIsNotNullAsync— session is non-null in streaming middleware viaUse(runStreamingFunc:).AGUIMiddleware_CurrentRunContext_SessionIsNotNullAsync—AIAgent.CurrentRunContext.Sessionis non-null during AGUI-initiated runs.AGUIMiddleware_SharedUseOverload_SessionIsNotNullAsync— session is non-null in the shared pre/post-processingUse()overload.AGUIMiddleware_SessionStateBag_IsAccessibleAsync—Session.StateBagis functional (set/get values) when invoked via AGUI endpoint.Contribution Checklist