Table of Contents

Class MiddlewareAgent

Namespace
AutoGen.Core
Assembly
AutoGen.Core.dll

An agent that allows you to add middleware and modify the behavior of an existing agent.

public class MiddlewareAgent : IMiddlewareAgent, IAgent, IAgentMetaInformation
Inheritance
MiddlewareAgent
Implements
Derived
Inherited Members
Extension Methods

Constructors

MiddlewareAgent(IAgent, string?, IEnumerable<IMiddleware>?)

Create a new instance of MiddlewareAgent

public MiddlewareAgent(IAgent innerAgent, string? name = null, IEnumerable<IMiddleware>? middlewares = null)

Parameters

innerAgent IAgent

the inner agent where middleware will be added.

name string

the name of the agent if provided. Otherwise, the name of innerAgent will be used.

middlewares IEnumerable<IMiddleware>

MiddlewareAgent(MiddlewareAgent)

Create a new instance of MiddlewareAgent by copying the middlewares from another MiddlewareAgent.

public MiddlewareAgent(MiddlewareAgent other)

Parameters

other MiddlewareAgent

Properties

Agent

Get the inner agent.

public IAgent Agent { get; }

Property Value

IAgent

Middlewares

Get the middlewares.

public IEnumerable<IMiddleware> Middlewares { get; }

Property Value

IEnumerable<IMiddleware>

Name

public string Name { get; }

Property Value

string

Methods

GenerateReplyAsync(IEnumerable<IMessage>, GenerateReplyOptions?, CancellationToken)

Generate reply

public Task<IMessage> GenerateReplyAsync(IEnumerable<IMessage> messages, GenerateReplyOptions? options = null, CancellationToken cancellationToken = default)

Parameters

messages IEnumerable<IMessage>

conversation history

options GenerateReplyOptions

completion option. If provided, it should override existing option if there's any

cancellationToken CancellationToken

Returns

Task<IMessage>

ToString()

public override string ToString()

Returns

string

Use(IMiddleware)

Use middleware.

public void Use(IMiddleware middleware)

Parameters

middleware IMiddleware

Use(Func<IEnumerable<IMessage>, GenerateReplyOptions?, IAgent, CancellationToken, Task<IMessage>>, string?)

Add a middleware to the agent. If multiple middlewares are added, they will be executed in the LIFO order. Call into the next function to continue the execution of the next middleware. Short cut middleware execution by not calling into the next function.

public void Use(Func<IEnumerable<IMessage>, GenerateReplyOptions?, IAgent, CancellationToken, Task<IMessage>> func, string? middlewareName = null)

Parameters

func Func<IEnumerable<IMessage>, GenerateReplyOptions, IAgent, CancellationToken, Task<IMessage>>
middlewareName string