// Copyright (c) Microsoft. All rights reserved. using System; using System.Threading; using System.Threading.Tasks; using Microsoft.SemanticKernel.Agents.Runtime; using Microsoft.SemanticKernel.Agents.Runtime.Core; namespace Microsoft.Agents.Orchestration.Extensions; /// /// Extension methods for . /// public static class RuntimeExtensions { /// /// Sends a message to the specified agent. /// public static async ValueTask PublishMessageAsync(this IAgentRuntime runtime, object message, AgentType agentType, CancellationToken cancellationToken = default) { await runtime.PublishMessageAsync(message, new TopicId(agentType), sender: null, messageId: null, cancellationToken).ConfigureAwait(false); } /// /// 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, AgentType agentType, Func> factoryFunc) { AgentType registeredType = await runtime.RegisterAgentFactoryAsync(agentType, factoryFunc).ConfigureAwait(false); // Subscribe agent to its own unique topic await runtime.SubscribeAsync(registeredType).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 async Task SubscribeAsync(this IAgentRuntime runtime, string agentType) { await runtime.AddSubscriptionAsync(new TypeSubscription(agentType, agentType)).ConfigureAwait(false); } /// /// 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, string agentType, params TopicId[] topics) { for (int index = 0; index < topics.Length; ++index) { await runtime.AddSubscriptionAsync(new TypeSubscription(topics[index].Type, agentType)).ConfigureAwait(false); } } }