-
Notifications
You must be signed in to change notification settings - Fork 1.1k
.NET: Adding AgentRunContext to allow accessing agent run info in external downstream components #3476
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR introduces AgentRunContext to provide ambient access to agent run information for downstream components like tools and middleware. This addresses issue #3411 regarding ConversationId propagation in Agent-as-a-Tool scenarios by making agent run context (agent, session, messages, options) accessible via AIAgent.CurrentRunContext using AsyncLocal<T>.
Changes:
- Added
AgentRunContextclass to encapsulate agent run state (agent, session, request messages, run options) - Modified
AIAgentto setCurrentRunContextusingAsyncLocal<T>at the start of each run (both streaming and non-streaming) - Added comprehensive unit tests for
AgentRunContextand updatedAIAgenttests to verify context propagation - Added architectural decision record documenting the design rationale
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunContext.cs | New sealed class providing read-only access to agent run information |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AIAgent.cs | Added AsyncLocal field and CurrentRunContext property; modified RunAsync/RunStreamingAsync to set context |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRunContextTests.cs | Comprehensive unit tests for AgentRunContext constructor validation and property accessors |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AIAgentTests.cs | Added tests verifying CurrentRunContext is set for all RunAsync/RunStreamingAsync overloads; updated TestAgentSession visibility |
| docs/decisions/0015-agent-run-context.md | Architectural decision record explaining design choices and trade-offs |
Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest adding additional middleware tests to ensure this context can be captured as expected from the normal agent middlewares and possible the function invoking middleware.
builder.Use(shared)
builder.Use(runAsync, streamingRunAsync)
Motivation and Context
#3411
Description
Contribution Checklist