Skip to content

.NET: Add IChatMessageInjector for message injection during function loop - #5679

Merged
westey (westey-m) merged 7 commits into
microsoft:mainfrom
westey-m:per-service-call-persistence-nudging
May 8, 2026
Merged

.NET: Add IChatMessageInjector for message injection during function loop#5679
westey (westey-m) merged 7 commits into
microsoft:mainfrom
westey-m:per-service-call-persistence-nudging

Conversation

@westey-m

Copy link
Copy Markdown
Contributor

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: IChatMessageInjector interface

  • Experimental public interface discoverable via GetService<IChatMessageInjector>()
  • Single method: EnqueueMessages(IEnumerable<ChatMessage> messages)

Updated: PerServiceCallChatHistoryPersistingChatClient

  • Implements IChatMessageInjector with a thread-safe ConcurrentQueue<ChatMessage>
  • DrainInjectedMessages prepends queued messages (never mutates the incoming list)
  • Internal loop in both GetResponseAsync and GetStreamingResponseAsync: when no actionable FunctionCallContent remains but injected messages are pending, loops and calls the service again
  • HasActionableFunctionCalls helper checks for non-InformationalOnly FCC

New: Unit tests

  • 7 tests covering: GetService resolution, message inclusion, drain behavior, internal loop trigger, no-loop with actionable FCC, loop with InformationalOnly FCC

Updated: Agent_Step19_InFunctionLoopCheckpointing sample

  • Replaced artificial InjectFollowUp tool with natural CheckTravelAdvisory tool
  • Advisory detection injects a follow-up asking for alternative destinations
  • 4th turn added to both non-streaming and streaming modes demonstrating the feature

Copilot AI review requested due to automatic review settings May 6, 2026 18:37
@moonbox3 Evan Mattson (moonbox3) added the .NET Usage: [Issues, PRs], Target: .Net label May 6, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 IChatMessageInjector and makes it discoverable via GetService<IChatMessageInjector>() when per-service-call persistence middleware is active.
  • Updates PerServiceCallChatHistoryPersistingChatClient to 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.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Code Review

Reviewers: 4 | Confidence: 88% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by westey-m's agents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET Usage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants