You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SourceMatchTermination.cs 1.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // SourceMatchTermination.cs
  3. using Microsoft.AutoGen.AgentChat.Abstractions;
  4. namespace Microsoft.AutoGen.AgentChat.Terminations;
  5. /// <summary>
  6. /// Terminate the conversation after a specific source responds.
  7. /// </summary>
  8. public sealed class SourceMatchTermination : ITerminationCondition
  9. {
  10. /// <summary>
  11. /// Initializes a new instance of the <see cref="SourceMatchTermination"/> class.
  12. /// </summary>
  13. /// <param name="sources">List of source names to terminate the conversation.</param>
  14. public SourceMatchTermination(params IEnumerable<string> sources)
  15. {
  16. this.Sources = [.. sources];
  17. }
  18. public HashSet<string> Sources { get; }
  19. public bool IsTerminated { get; private set; }
  20. public ValueTask<StopMessage?> CheckAndUpdateAsync(IList<AgentMessage> messages)
  21. {
  22. if (this.IsTerminated)
  23. {
  24. throw new TerminatedException();
  25. }
  26. foreach (AgentMessage item in messages)
  27. {
  28. if (this.Sources.Contains(item.Source))
  29. {
  30. this.IsTerminated = true;
  31. string message = $"'{item.Source}' answered.";
  32. StopMessage result = new() { Content = message, Source = nameof(SourceMatchTermination) };
  33. return ValueTask.FromResult<StopMessage?>(result);
  34. }
  35. }
  36. return ValueTask.FromResult<StopMessage?>(null);
  37. }
  38. public void Reset()
  39. {
  40. this.IsTerminated = false;
  41. }
  42. }