microsoft / microsoft/agent-framework

.NET: AddAIAgent - Rename or add overload

Open
#627 2 comments 0 reactions 1 assignee Claimed by @adityamandaleeka View on GitHub
.NET agents
Dominant language
Python
Stars
13.6k
Forks
2.3k
Avg merge
2d 45m
Merged PRs (30d)
358

Description

AddAIAgent extension methods in the Hosting package implicitly register an `AIAgent` as a `KeyedSingleton`

https://github.com/microsoft/agent-framework/blob/518fd447fd259581ae58e17aae66f8be50e9cbe1/dotnet/src/Microsoft.Extensions.AI.Agents.Hosting/HostApplicationBuilderAgentExtensions.cs#L100-L120

When resolving the agent using DI, it's not obvious you need to use `FromKeyedServices` to get the agent. Using `FromServices` throws.

i.e.

**Works**

```csharp
app.MapGet("/chat", async ([FromKeyedServices("user-agent")] AIAgent agent, string question) =>
{
AgentRunResponse agentResponse = await agent.RunAsync(question);

return Results.Ok(agentResponse.Text);
});
```

**Error**

```csharp
app.MapGet("/chat", async ([FromServices] AIAgent agent, string question) =>
{
AgentRunResponse agentResponse = await agent.RunAsync(question);

return Results.Ok(agentResponse.Text);
});
```

Suggestions:

1. Rename existing `AddAIAgent` to `AddKeyedAIAgent`
2. Add overloads for both `AddAIAgent` and `AddKeyedAIAgent`

I'd be in favor of option 2.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.