Skip to content

Commit b507d3e

Browse files
committed
Enrich with streaming middleware
1 parent 0417cf4 commit b507d3e

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

agent-framework/user-guide/agents/agent-middleware.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Agent run and function calling middleware types can be registered on an agent, b
2929
```csharp
3030
var middlewareEnabledAgent = originalAgent
3131
.AsBuilder()
32-
.Use(runFunc: CustomAgentRunMiddleware, runStreamingFunc: null)
32+
.Use(runFunc: CustomAgentRunMiddleware, runStreamingFunc: CustomAgentRunStreamingMiddleware)
3333
.Use(CustomFunctionCallingMiddleware)
3434
.Build();
3535
```
@@ -86,6 +86,30 @@ async Task<AgentRunResponse> CustomAgentRunMiddleware(
8686
}
8787
```
8888

89+
## Agent Run Streaming Middleware
90+
91+
Here is an example of agent run streaming middleware, that can inspect and/or modify the input and output from the agent streaming run.
92+
93+
```csharp
94+
async IAsyncEnumerable<AgentRunResponseUpdate> CustomAgentRunStreamingMiddleware(
95+
IEnumerable<ChatMessage> messages,
96+
AgentThread? thread,
97+
AgentRunOptions? options,
98+
AIAgent innerAgent,
99+
[EnumeratorCancellation] CancellationToken cancellationToken)
100+
{
101+
Console.WriteLine(messages.Count());
102+
List<AgentRunResponseUpdate> updates = [];
103+
await foreach (var update in innerAgent.RunStreamingAsync(messages, thread, options, cancellationToken))
104+
{
105+
updates.Add(update);
106+
yield return update;
107+
}
108+
109+
Console.WriteLine(updates.ToAgentRunResponse().Messages.Count);
110+
}
111+
```
112+
89113
## Function calling middleware
90114

91115
> [!NOTE]

0 commit comments

Comments
 (0)