// Copyright (c) Microsoft Corporation. All rights reserved.
// SourceMatchTermination.cs
using Microsoft.AutoGen.AgentChat.Abstractions;
namespace Microsoft.AutoGen.AgentChat.Terminations;
///
/// Terminate the conversation after a specific source responds.
///
public sealed class SourceMatchTermination : ITerminationCondition
{
///
/// Initializes a new instance of the class.
///
/// List of source names to terminate the conversation.
public SourceMatchTermination(params IEnumerable sources)
{
this.Sources = [.. sources];
}
public HashSet Sources { get; }
public bool IsTerminated { get; private set; }
public ValueTask CheckAndUpdateAsync(IList messages)
{
if (this.IsTerminated)
{
throw new TerminatedException();
}
foreach (AgentMessage item in messages)
{
if (this.Sources.Contains(item.Source))
{
this.IsTerminated = true;
string message = $"'{item.Source}' answered.";
StopMessage result = new() { Content = message, Source = nameof(SourceMatchTermination) };
return ValueTask.FromResult(result);
}
}
return ValueTask.FromResult(null);
}
public void Reset()
{
this.IsTerminated = false;
}
}