MessageInjectingChatClient Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
A delegating chat client that supports injecting messages into the function execution loop.
public sealed class MessageInjectingChatClient : Microsoft.Extensions.AI.DelegatingChatClient
type MessageInjectingChatClient = class
inherit DelegatingChatClient
Public NotInheritable Class MessageInjectingChatClient
Inherits DelegatingChatClient
- Inheritance
-
MessageInjectingChatClient
Remarks
This decorator enables external code (such as tool delegates) to enqueue messages that will be sent to the underlying model at the next opportunity. It sits between the FunctionInvokingChatClient and the Microsoft.Agents.AI.PerServiceCallChatHistoryPersistingChatClient (or the leaf IChatClient) in a ChatClientAgent pipeline.
The injected messages queue is stored per-session in the StateBag, ensuring isolation between concurrent sessions.
After each service call, if no actionable FunctionCallContent is returned but injected messages are pending, the decorator loops internally and calls the inner client again with the new messages. When actionable function calls are present, control returns to the parent FunctionInvokingChatClient loop.
This chat client must be used within the context of a running ChatClientAgent. It retrieves the current session from CurrentRunContext, which is set automatically when an agent's RunAsync(IEnumerable<ChatMessage>, AgentSession, AgentRunOptions, CancellationToken) or RunStreamingAsync(IEnumerable<ChatMessage>, AgentSession, AgentRunOptions, CancellationToken) method is called.
Constructors
| Name | Description |
|---|---|
| MessageInjectingChatClient(IChatClient) |
Initializes a new instance of the MessageInjectingChatClient class. |
Methods
| Name | Description |
|---|---|
| EnqueueMessages(AgentSession, IEnumerable<ChatMessage>) |
Enqueues one or more messages to be used at the next opportunity. |
| GetPendingMessages(AgentSession) |
Gets a snapshot of the pending injected messages for the specified session. |
| GetResponseAsync(IEnumerable<ChatMessage>, ChatOptions, CancellationToken) | Sends chat messages and returns the response. |
| GetStreamingResponseAsync(IEnumerable<ChatMessage>, ChatOptions, CancellationToken) | Sends chat messages and streams the response. |