mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Improve resolving AITool from DI (#3175)
* remove localagenttoolregistry * also give the factory method API
This commit is contained in:
committed by
GitHub
Unverified
parent
3e13909e59
commit
c7cb5be231
@@ -1,8 +1,7 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Agents.AI.Hosting.Local;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Shared.Diagnostics;
|
||||
@@ -29,7 +28,7 @@ public static class AgentHostingServiceCollectionExtensions
|
||||
return services.AddAIAgent(name, (sp, key) =>
|
||||
{
|
||||
var chatClient = sp.GetRequiredService<IChatClient>();
|
||||
var tools = GetRegisteredToolsForAgent(sp, name);
|
||||
var tools = sp.GetKeyedServices<AITool>(name).ToList();
|
||||
return new ChatClientAgent(chatClient, instructions, key, tools: tools);
|
||||
});
|
||||
}
|
||||
@@ -49,7 +48,7 @@ public static class AgentHostingServiceCollectionExtensions
|
||||
Throw.IfNullOrEmpty(name);
|
||||
return services.AddAIAgent(name, (sp, key) =>
|
||||
{
|
||||
var tools = GetRegisteredToolsForAgent(sp, name);
|
||||
var tools = sp.GetKeyedServices<AITool>(name).ToList();
|
||||
return new ChatClientAgent(chatClient, instructions, key, tools: tools);
|
||||
});
|
||||
}
|
||||
@@ -70,7 +69,7 @@ public static class AgentHostingServiceCollectionExtensions
|
||||
return services.AddAIAgent(name, (sp, key) =>
|
||||
{
|
||||
var chatClient = chatClientServiceKey is null ? sp.GetRequiredService<IChatClient>() : sp.GetRequiredKeyedService<IChatClient>(chatClientServiceKey);
|
||||
var tools = GetRegisteredToolsForAgent(sp, name);
|
||||
var tools = sp.GetKeyedServices<AITool>(name).ToList();
|
||||
return new ChatClientAgent(chatClient, instructions, key, tools: tools);
|
||||
});
|
||||
}
|
||||
@@ -92,7 +91,7 @@ public static class AgentHostingServiceCollectionExtensions
|
||||
return services.AddAIAgent(name, (sp, key) =>
|
||||
{
|
||||
var chatClient = chatClientServiceKey is null ? sp.GetRequiredService<IChatClient>() : sp.GetRequiredKeyedService<IChatClient>(chatClientServiceKey);
|
||||
var tools = GetRegisteredToolsForAgent(sp, name);
|
||||
var tools = sp.GetKeyedServices<AITool>(name).ToList();
|
||||
return new ChatClientAgent(chatClient, instructions: instructions, name: key, description: description, tools: tools);
|
||||
});
|
||||
}
|
||||
@@ -127,10 +126,4 @@ public static class AgentHostingServiceCollectionExtensions
|
||||
|
||||
return new HostedAgentBuilder(name, services);
|
||||
}
|
||||
|
||||
private static IList<AITool> GetRegisteredToolsForAgent(IServiceProvider serviceProvider, string agentName)
|
||||
{
|
||||
var registry = serviceProvider.GetService<LocalAgentToolRegistry>();
|
||||
return registry?.GetTools(agentName) ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.Agents.AI.Hosting.Local;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Shared.Diagnostics;
|
||||
@@ -70,18 +68,7 @@ public static class HostedAgentBuilderExtensions
|
||||
Throw.IfNull(builder);
|
||||
Throw.IfNull(tool);
|
||||
|
||||
var agentName = builder.Name;
|
||||
var services = builder.ServiceCollection;
|
||||
|
||||
// Get or create the agent tool registry
|
||||
var descriptor = services.FirstOrDefault(sd => !sd.IsKeyedService && sd.ServiceType.Equals(typeof(LocalAgentToolRegistry)));
|
||||
if (descriptor?.ImplementationInstance is not LocalAgentToolRegistry toolRegistry)
|
||||
{
|
||||
toolRegistry = new();
|
||||
services.Add(ServiceDescriptor.Singleton(toolRegistry));
|
||||
}
|
||||
|
||||
toolRegistry.AddTool(agentName, tool);
|
||||
builder.ServiceCollection.AddKeyedSingleton(builder.Name, tool);
|
||||
|
||||
return builder;
|
||||
}
|
||||
@@ -105,4 +92,19 @@ public static class HostedAgentBuilderExtensions
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds AI tool to an agent being configured with the service collection.
|
||||
/// </summary>
|
||||
/// <param name="builder">The hosted agent builder.</param>
|
||||
/// <param name="factory">A factory function that creates a AI tool using the provided service provider.</param>
|
||||
public static IHostedAgentBuilder WithAITool(this IHostedAgentBuilder builder, Func<IServiceProvider, AITool> factory)
|
||||
{
|
||||
Throw.IfNull(builder);
|
||||
Throw.IfNull(factory);
|
||||
|
||||
builder.ServiceCollection.AddKeyedSingleton(builder.Name, (sp, name) => factory(sp));
|
||||
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.AI;
|
||||
|
||||
namespace Microsoft.Agents.AI.Hosting.Local;
|
||||
|
||||
internal sealed class LocalAgentToolRegistry
|
||||
{
|
||||
private readonly Dictionary<string, List<AITool>> _toolsByAgentName = [];
|
||||
|
||||
public void AddTool(string agentName, AITool tool)
|
||||
{
|
||||
if (!this._toolsByAgentName.TryGetValue(agentName, out var tools))
|
||||
{
|
||||
tools = [];
|
||||
this._toolsByAgentName[agentName] = tools;
|
||||
}
|
||||
|
||||
tools.Add(tool);
|
||||
}
|
||||
|
||||
public IList<AITool> GetTools(string agentName)
|
||||
{
|
||||
return this._toolsByAgentName.TryGetValue(agentName, out var tools) ? tools : [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user