Table of Contents

Class FunctionCallMiddleware

Namespace
AutoGen.Core
Assembly
AutoGen.Core.dll

The middleware that process function call message that both send to an agent or reply from an agent.

If the last message is ToolCallMessage and the tool calls is available in this middleware's function map, the tools from the last message will be invoked and a ToolCallResultMessage will be returned. In this situation, the inner agent will be short-cut and won't be invoked.

Otherwise, the message will be sent to the inner agent. In this situation

if the reply from the inner agent is ToolCallMessage, and the tool calls is available in this middleware's function map, the tools from the reply will be invoked, and a ToolCallAggregateMessage will be returned.

If the reply from the inner agent is ToolCallMessage but the tool calls is not available in this middleware's function map, or the reply from the inner agent is not ToolCallMessage, the original reply from the inner agent will be returned.

When used as a streaming middleware, if the streaming reply from the inner agent is ToolCallMessageUpdate or TextMessageUpdate, This middleware will update the message accordingly and invoke the function if the tool call is available in this middleware's function map. If the streaming reply from the inner agent is other types of message, the most recent message will be used to invoke the function.

public class FunctionCallMiddleware : IStreamingMiddleware, IMiddleware
Inheritance
FunctionCallMiddleware
Implements
Inherited Members

Constructors

FunctionCallMiddleware(IEnumerable<FunctionContract>?, IDictionary<string, Func<string, Task<string>>>?, string?)

public FunctionCallMiddleware(IEnumerable<FunctionContract>? functions = null, IDictionary<string, Func<string, Task<string>>>? functionMap = null, string? name = null)

Parameters

functions IEnumerable<FunctionContract>
functionMap IDictionary<string, Func<string, Task<string>>>
name string

FunctionCallMiddleware(IEnumerable<AIFunction>, string?)

Create a new instance of FunctionCallMiddleware with a list of Microsoft.Extensions.AI.AIFunction.

public FunctionCallMiddleware(IEnumerable<AIFunction> functions, string? name = null)

Parameters

functions IEnumerable<AIFunction>

function list

name string

optional middleware name. If not provided, the class name FunctionCallMiddleware will be used.

Properties

Name

the name of the middleware

public string? Name { get; }

Property Value

string

Methods

InvokeAsync(MiddlewareContext, IAgent, CancellationToken)

The method to invoke the middleware

public Task<IMessage> InvokeAsync(MiddlewareContext context, IAgent agent, CancellationToken cancellationToken = default)

Parameters

context MiddlewareContext
agent IAgent
cancellationToken CancellationToken

Returns

Task<IMessage>

InvokeAsync(MiddlewareContext, IStreamingAgent, CancellationToken)

public IAsyncEnumerable<IMessage> InvokeAsync(MiddlewareContext context, IStreamingAgent agent, CancellationToken cancellationToken = default)

Parameters

context MiddlewareContext
agent IStreamingAgent
cancellationToken CancellationToken

Returns

IAsyncEnumerable<IMessage>