Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public Step10_MultiAgent_Declarative(ITestOutputHelper output) : base(output)
this._kernel = builder.Build();

this._kernelAgentFactory =
new AggregatorKernelAgentFactory(
new AggregatorAgentFactory(
new ChatCompletionAgentFactory(),
new OpenAIAssistantAgentFactory(),
new AzureAIAgentFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@
namespace Microsoft.SemanticKernel.Agents;

/// <summary>
/// Provides a <see cref="AgentFactory"/> which aggregates multiple kernel agent factories.
/// Provides a <see cref="AgentFactory"/> which aggregates multiple agent factories.
/// </summary>
[Experimental("SKEXP0110")]
public sealed class AggregatorKernelAgentFactory : AgentFactory
public sealed class AggregatorAgentFactory : AgentFactory
{
private readonly AgentFactory[] _kernelAgentFactories;
private readonly AgentFactory[] _agentFactories;

/// <summary>Initializes the instance.</summary>
/// <param name="kernelAgentFactories">Ordered <see cref="AgentFactory"/> instances to aggregate.</param>
/// <param name="agentFactories">Ordered <see cref="AgentFactory"/> instances to aggregate.</param>
/// <remarks>
/// Where multiple <see cref="AgentFactory"/> instances are provided, the first factory that supports the <see cref="AgentDefinition"/> will be used.
/// </remarks>
public AggregatorKernelAgentFactory(params AgentFactory[] kernelAgentFactories) : base(kernelAgentFactories.SelectMany(f => f.Types).ToArray())
public AggregatorAgentFactory(params AgentFactory[] agentFactories) : base(agentFactories.SelectMany(f => f.Types).ToArray())
{
Verify.NotNullOrEmpty(kernelAgentFactories);
Verify.NotNullOrEmpty(agentFactories);

foreach (AgentFactory kernelAgentFactory in kernelAgentFactories)
foreach (AgentFactory agentFactory in agentFactories)
{
Verify.NotNull(kernelAgentFactory, nameof(kernelAgentFactories));
Verify.NotNull(agentFactory, nameof(agentFactories));
}

this._kernelAgentFactories = kernelAgentFactories;
this._agentFactories = agentFactories;
}

/// <inheritdoc/>
public override async Task<Agent?> TryCreateAsync(Kernel kernel, AgentDefinition agentDefinition, AgentCreationOptions? agentCreationOptions = null, CancellationToken cancellationToken = default)
{
Verify.NotNull(agentDefinition);

foreach (var kernelAgentFactory in this._kernelAgentFactories)
foreach (var agentFactory in this._agentFactories)
{
if (kernelAgentFactory.IsSupported(agentDefinition))
if (agentFactory.IsSupported(agentDefinition))
{
var kernelAgent = await kernelAgentFactory.TryCreateAsync(kernel, agentDefinition, agentCreationOptions, cancellationToken).ConfigureAwait(false);
var kernelAgent = await agentFactory.TryCreateAsync(kernel, agentDefinition, agentCreationOptions, cancellationToken).ConfigureAwait(false);
if (kernelAgent is not null)
{
return kernelAgent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
namespace SemanticKernel.Agents.UnitTests.Core.Factory;

/// <summary>
/// Tests for <see cref="AggregatorKernelAgentFactory"/>.
/// Tests for <see cref="AggregatorAgentFactory"/>.
/// </summary>
public class AggregatorKernelAgentFactoryTests
public class AggregatorAgentFactoryTests
{
/// <summary>
/// Verifies that the <see cref="AggregatorKernelAgentFactory"/> can create different types of agents.
/// Verifies that the <see cref="AggregatorAgentFactory"/> can create different types of agents.
/// </summary>
[Fact]
public async Task ItCreatesKernelAgentsAsync()
Expand All @@ -25,7 +25,7 @@ public async Task ItCreatesKernelAgentsAsync()
var agentDefinition1 = new AgentDefinition() { Type = "my-type-1", Name = "my-name-1", Description = "my-description-1", Instructions = "my-instructions-1" };
var agentDefinition2 = new AgentDefinition() { Type = "my-type-2", Name = "my-name-2", Description = "my-description-2", Instructions = "my-instructions-2" };
var kernel = new Kernel();
var target = new AggregatorKernelAgentFactory(new MyAgentFactory1(), new MyAgentFactory2());
var target = new AggregatorAgentFactory(new MyAgentFactory1(), new MyAgentFactory2());

// Act
var result1 = await target.CreateAsync(kernel, agentDefinition1);
Expand All @@ -39,15 +39,15 @@ public async Task ItCreatesKernelAgentsAsync()
}

/// <summary>
/// Verifies that the <see cref="AggregatorKernelAgentFactory"/> throws <see cref="KernelException"/> for an unknown agent type.
/// Verifies that the <see cref="AggregatorAgentFactory"/> throws <see cref="KernelException"/> for an unknown agent type.
/// </summary>
[Fact]
public async Task ItReturnsNullForUnknownAgentTypeAsync()
{
// Arrange
var agentDefinition = new AgentDefinition() { Type = "my-type-unknown", Name = "my-name-1", Description = "my-description-1", Instructions = "my-instructions-1" };
var kernel = new Kernel();
var target = new AggregatorKernelAgentFactory(new MyAgentFactory1(), new MyAgentFactory2());
var target = new AggregatorAgentFactory(new MyAgentFactory1(), new MyAgentFactory2());

// Act & Assert
await Assert.ThrowsAsync<NotSupportedException>(async () => await target.CreateAsync(kernel, agentDefinition));
Expand Down
Loading