.NET: AgentHostExecutor should use agent DisplayName as Executor ID (#1840)

* AgentHostExecutor should use agent DisplayName as Executor ID

* Address comments

* Remove unintended changes

* Remove unintended changes

* Fix tests and address comments
This commit is contained in:
Tao Chen
2025-11-05 14:16:53 -08:00
committed by GitHub
Unverified
parent 0bf6d437d8
commit 3f835c8118
3 changed files with 49 additions and 2 deletions
@@ -11,7 +11,7 @@ namespace Microsoft.Agents.AI.Workflows;
/// <param name="Agent">The AI agent.</param>
/// <param name="EmitEvents">Specifies whether the agent should emit events. If null, the default behavior is applied.</param>
public record AIAgentBinding(AIAgent Agent, bool EmitEvents = false)
: ExecutorBinding(Throw.IfNull(Agent).Name ?? Throw.IfNull(Agent.Id),
: ExecutorBinding(Throw.IfNull(Agent).GetDescriptiveId(),
(_) => new(new AIAgentHostExecutor(Agent, EmitEvents)),
typeof(AIAgentHostExecutor),
Agent)
@@ -14,7 +14,7 @@ internal sealed class AIAgentHostExecutor : ChatProtocolExecutor
private readonly AIAgent _agent;
private AgentThread? _thread;
public AIAgentHostExecutor(AIAgent agent, bool emitEvents = false) : base(id: agent.Id)
public AIAgentHostExecutor(AIAgent agent, bool emitEvents = false) : base(id: agent.GetDescriptiveId())
{
this._agent = agent;
this._emitEvents = emitEvents;
@@ -8,6 +8,7 @@ using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Agents.AI.Workflows.Checkpointing;
using Microsoft.Agents.AI.Workflows.Execution;
using Microsoft.Agents.AI.Workflows.Specialized;
using Microsoft.Extensions.AI;
@@ -196,4 +197,50 @@ public class SpecializedExecutorSmokeTests
collected.Text.Should().Be(expectedText);
}
}
[Fact]
public async Task Test_AIAgent_ExecutorId_Use_Agent_NameAsync()
{
const string AgentAName = "TestAgentAName";
const string AgentBName = "TestAgentBName";
TestAIAgent agentA = new(name: AgentAName);
TestAIAgent agentB = new(name: AgentBName);
var workflow = new WorkflowBuilder(agentA).AddEdge(agentA, agentB).Build();
var definition = workflow.ToWorkflowInfo();
// Verify that the agent host executor registration IDs in the workflow definition
// match the agent names when agent names are provided.
// The property DisplayName falls back to using the agent ID when Name is not set.
agentA.GetDescriptiveId().Should().Contain(AgentAName);
agentB.GetDescriptiveId().Should().Contain(AgentBName);
definition.Executors[agentA.GetDescriptiveId()].ExecutorId.Should().Be(agentA.GetDescriptiveId());
definition.Executors[agentB.GetDescriptiveId()].ExecutorId.Should().Be(agentB.GetDescriptiveId());
// This will create an instance of the start agent and verify that the ID
// of the executor instance matches the ID of the registration.
var protocolDescriptor = await workflow.DescribeProtocolAsync();
protocolDescriptor.Accepts.Should().Contain(typeof(ChatMessage));
}
[Fact]
public async Task Test_AIAgent_ExecutorId_Use_Agent_ID_When_Name_Not_ProvidedAsync()
{
TestAIAgent agentA = new();
TestAIAgent agentB = new();
var workflow = new WorkflowBuilder(agentA).AddEdge(agentA, agentB).Build();
var definition = workflow.ToWorkflowInfo();
// Verify that the agent host executor registration IDs in the workflow definition
// match the agent IDs when agent names are not provided.
// The property DisplayName falls back to using the agent ID when Name is not set.
agentA.GetDescriptiveId().Should().Contain(agentA.Id);
agentB.GetDescriptiveId().Should().Contain(agentB.Id);
definition.Executors[agentA.GetDescriptiveId()].ExecutorId.Should().Be(agentA.GetDescriptiveId());
definition.Executors[agentB.GetDescriptiveId()].ExecutorId.Should().Be(agentB.GetDescriptiveId());
// This will create an instance of the start agent and verify that the ID
// of the executor instance matches the ID of the registration.
var protocolDescriptor = await workflow.DescribeProtocolAsync();
protocolDescriptor.Accepts.Should().Contain(typeof(ChatMessage));
}
}