// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.Agents.AI.DurableTask;
///
/// Extension methods for the class.
///
public static class AIAgentExtensions
{
///
/// Converts an AIAgent to a durable agent proxy.
///
/// The agent to convert.
/// The service provider.
/// The durable agent proxy.
///
/// Thrown when the agent is a instance or if the agent has no name.
///
///
/// Thrown if does not contain an
/// or if durable agents have not been configured on the service collection.
///
///
/// Thrown when the agent with the specified name has not been registered.
///
public static AIAgent AsDurableAgentProxy(this AIAgent agent, IServiceProvider services)
{
// Don't allow this method to be used on DurableAIAgent instances.
if (agent is DurableAIAgent)
{
throw new ArgumentException(
$"{nameof(DurableAIAgent)} instances cannot be converted to a durable agent proxy.",
nameof(agent));
}
string agentName = agent.Name ?? throw new ArgumentException("Agent must have a name.", nameof(agent));
// Validate that the agent is registered
ServiceCollectionExtensions.ValidateAgentIsRegistered(services, agentName);
IDurableAgentClient agentClient = services.GetRequiredService();
return new DurableAIAgentProxy(agentName, agentClient);
}
}