mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
dc2b109b50
* Upgrade to .NET 10 - Require .NET 10 SDK - Include net10.0 assets in all assemblies - Move net9.0-only targets to net10.0 - Update LangVersion to latest - Remove complicated distinctions between debug target TFMs and release target TFMs - Remove unnecessary package dependencies when built into netcoreapp - Clean up some ifdefs - Clean up some analyzer warnings * Fix CI
82 lines
3.2 KiB
C#
82 lines
3.2 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.AI;
|
|
using Microsoft.Shared.Diagnostics;
|
|
|
|
namespace Microsoft.Agents.AI.Workflows;
|
|
|
|
/// <summary>
|
|
/// A manager that manages the flow of a group chat.
|
|
/// </summary>
|
|
public abstract class GroupChatManager
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="GroupChatManager"/> class.
|
|
/// </summary>
|
|
protected GroupChatManager() { }
|
|
|
|
/// <summary>
|
|
/// Gets the number of iterations in the group chat so far.
|
|
/// </summary>
|
|
public int IterationCount { get; internal set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the maximum number of iterations allowed.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Each iteration involves a single interaction with a participating agent.
|
|
/// The default is 40.
|
|
/// </remarks>
|
|
public int MaximumIterationCount
|
|
{
|
|
get;
|
|
set => field = Throw.IfLessThan(value, 1);
|
|
} = 40;
|
|
|
|
/// <summary>
|
|
/// Selects the next agent to participate in the group chat based on the provided chat history and team.
|
|
/// </summary>
|
|
/// <param name="history">The chat history to consider.</param>
|
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.
|
|
/// The default is <see cref="CancellationToken.None"/>.</param>
|
|
/// <returns>The next <see cref="AIAgent"/> to speak. This agent must be part of the chat.</returns>
|
|
protected internal abstract ValueTask<AIAgent> SelectNextAgentAsync(
|
|
IReadOnlyList<ChatMessage> history,
|
|
CancellationToken cancellationToken = default);
|
|
|
|
/// <summary>
|
|
/// Filters the chat history before it's passed to the next agent.
|
|
/// </summary>
|
|
/// <param name="history">The chat history to filter.</param>
|
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.
|
|
/// The default is <see cref="CancellationToken.None"/>.</param>
|
|
/// <returns>The filtered chat history.</returns>
|
|
protected internal virtual ValueTask<IEnumerable<ChatMessage>> UpdateHistoryAsync(
|
|
IReadOnlyList<ChatMessage> history,
|
|
CancellationToken cancellationToken = default) =>
|
|
new(history);
|
|
|
|
/// <summary>
|
|
/// Determines whether the group chat should be terminated based on the provided chat history and iteration count.
|
|
/// </summary>
|
|
/// <param name="history">The chat history to consider.</param>
|
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.
|
|
/// The default is <see cref="CancellationToken.None"/>.</param>
|
|
/// <returns>A <see cref="bool"/> indicating whether the chat should be terminated.</returns>
|
|
protected internal virtual ValueTask<bool> ShouldTerminateAsync(
|
|
IReadOnlyList<ChatMessage> history,
|
|
CancellationToken cancellationToken = default) =>
|
|
new(this.MaximumIterationCount is int max && this.IterationCount >= max);
|
|
|
|
/// <summary>
|
|
/// Resets the state of the manager for a new group chat session.
|
|
/// </summary>
|
|
protected internal virtual void Reset()
|
|
{
|
|
this.IterationCount = 0;
|
|
}
|
|
}
|