// 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;
///
/// A manager that manages the flow of a group chat.
///
public abstract class GroupChatManager
{
///
/// Initializes a new instance of the class.
///
protected GroupChatManager() { }
///
/// Gets the number of iterations in the group chat so far.
///
public int IterationCount { get; internal set; }
///
/// Gets or sets the maximum number of iterations allowed.
///
///
/// Each iteration involves a single interaction with a participating agent.
/// The default is 40.
///
public int MaximumIterationCount
{
get;
set => field = Throw.IfLessThan(value, 1);
} = 40;
///
/// Selects the next agent to participate in the group chat based on the provided chat history and team.
///
/// The chat history to consider.
/// The to monitor for cancellation requests.
/// The default is .
/// The next to speak. This agent must be part of the chat.
protected internal abstract ValueTask SelectNextAgentAsync(
IReadOnlyList history,
CancellationToken cancellationToken = default);
///
/// Filters the chat history before it's passed to the next agent.
///
/// The chat history to filter.
/// The to monitor for cancellation requests.
/// The default is .
/// The filtered chat history.
protected internal virtual ValueTask> UpdateHistoryAsync(
IReadOnlyList history,
CancellationToken cancellationToken = default) =>
new(history);
///
/// Determines whether the group chat should be terminated based on the provided chat history and iteration count.
///
/// The chat history to consider.
/// The to monitor for cancellation requests.
/// The default is .
/// A indicating whether the chat should be terminated.
protected internal virtual ValueTask ShouldTerminateAsync(
IReadOnlyList history,
CancellationToken cancellationToken = default) =>
new(this.MaximumIterationCount is int max && this.IterationCount >= max);
///
/// Resets the state of the manager for a new group chat session.
///
protected internal virtual void Reset()
{
this.IterationCount = 0;
}
}