.NET: Add IChatMessageInjector for message injection during function loop#5679
Open
westey-m wants to merge 5 commits intomicrosoft:mainfrom
Open
.NET: Add IChatMessageInjector for message injection during function loop#5679westey-m wants to merge 5 commits intomicrosoft:mainfrom
westey-m wants to merge 5 commits intomicrosoft:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an experimental message-injection capability to the .NET ChatClientAgent stack so tool/code can enqueue additional user messages during a function-calling run, and the chat client can internally re-call the service to process them without requiring a new user turn.
Changes:
- Introduces experimental
IChatMessageInjectorand makes it discoverable viaGetService<IChatMessageInjector>()when per-service-call persistence middleware is active. - Updates
PerServiceCallChatHistoryPersistingChatClientto enqueue/drain injected messages and to internally loop when injected messages are pending (both non-streaming and streaming). - Adds unit tests and updates the Step19 sample to demonstrate tool-driven follow-up injection.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/PerServiceCallChatHistoryPersistingChatClientTests.cs | Adds tests for injector discovery, queue draining, and internal looping behavior. |
| dotnet/src/Microsoft.Agents.AI/ChatClient/PerServiceCallChatHistoryPersistingChatClient.cs | Implements IChatMessageInjector, message queue draining, and internal looping in both response paths. |
| dotnet/src/Microsoft.Agents.AI/ChatClient/IChatMessageInjector.cs | Adds new experimental public interface for message injection. |
| dotnet/samples/02-agents/Agents/Agent_Step19_InFunctionLoopCheckpointing/Program.cs | Demonstrates tool-based injection using the ambient run context. |
rogerbarreto
reviewed
May 7, 2026
lokitoth
approved these changes
May 7, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds the ability to inject user messages during the function execution loop in
PerServiceCallChatHistoryPersistingChatClient. This enables scenarios where tool code can enqueue follow-up messages that the model processes automatically — without requiring additional user turns.Changes
New:
IChatMessageInjectorinterfaceGetService<IChatMessageInjector>()EnqueueMessages(IEnumerable<ChatMessage> messages)Updated:
PerServiceCallChatHistoryPersistingChatClientIChatMessageInjectorwith a thread-safeConcurrentQueue<ChatMessage>DrainInjectedMessagesprepends queued messages (never mutates the incoming list)GetResponseAsyncandGetStreamingResponseAsync: when no actionableFunctionCallContentremains but injected messages are pending, loops and calls the service againHasActionableFunctionCallshelper checks for non-InformationalOnly FCCNew: Unit tests
Updated:
Agent_Step19_InFunctionLoopCheckpointingsampleInjectFollowUptool with naturalCheckTravelAdvisorytool