// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Extensions.AI.Agents.Runtime;
///
/// Extension methods for .
///
internal static class AgentRuntimeExtensions
{
///
/// Sends a message to the specified agent.
///
public static ValueTask PublishMessageAsync(this IAgentRuntime runtime, object message, ActorType agentType, CancellationToken cancellationToken = default) =>
runtime.PublishMessageAsync(message, new TopicId(agentType.Name), sender: null, messageId: null, cancellationToken);
///
/// Registers an agent factory for the specified agent type and associates it with the runtime.
///
/// The runtime targeted for registration.
/// The type of agent to register.
/// The factory function for creating the agent.
/// The registered agent type.
public static async ValueTask RegisterOrchestrationAgentAsync(this IAgentRuntime runtime, ActorType agentType, Func> factoryFunc)
{
ActorType registeredType = await runtime.RegisterActorFactoryAsync(agentType, factoryFunc).ConfigureAwait(false);
// Subscribe agent to its own unique topic
await runtime.SubscribeAsync(new(registeredType.Name)).ConfigureAwait(false);
return registeredType;
}
///
/// Subscribes the specified agent type to its own dedicated topic.
///
/// The runtime for managing the subscription.
/// The agent type to subscribe.
public static Task SubscribeAsync(this IAgentRuntime runtime, ActorType agentType) =>
runtime.AddSubscriptionAsync(new TypeSubscription(agentType.Name, agentType)).AsTask();
///
/// Subscribes the specified agent type to the provided topics.
///
/// The runtime for managing the subscription.
/// The agent type to subscribe.
/// A variable list of topics for subscription.
public static async Task SubscribeAsync(this IAgentRuntime runtime, ActorType agentType, params TopicId[] topics)
{
for (int index = 0; index < topics.Length; ++index)
{
await runtime.AddSubscriptionAsync(new TypeSubscription(topics[index].Type, agentType)).ConfigureAwait(false);
}
}
}