// Copyright (c) Microsoft. All rights reserved.
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Extensions.AI.Agents;
///
/// Extension methods for .
///
public static class AgentExtensions
{
///
/// Wraps the agent with OpenTelemetry instrumentation.
///
/// The agent to wrap.
/// The to use for emitting events.
/// An optional source name that will be used on the telemetry data.
/// When indicates whether potentially sensitive information should be included in telemetry. Default is
/// An that wraps the original agent with telemetry.
public static OpenTelemetryAgent WithOpenTelemetry(this AIAgent agent, ILoggerFactory? loggerFactory = null, string? sourceName = null, bool? enableSensitiveData = null) =>
new(agent, loggerFactory?.CreateLogger(typeof(OpenTelemetryAgent)), sourceName)
{
EnableSensitiveData = enableSensitiveData ?? false
};
///
/// Creates a that will invoke the provided Agent.
///
/// The to be represented via the created .
/// Metadata to use to override defaults inferred from .
/// The to use for the function.
/// The created for invoking the .
public static AIFunction AsAIFunction(this AIAgent agent, AIFunctionFactoryOptions? options = null, AgentThread? thread = null)
{
Throw.IfNull(agent);
[Description("Invoke an agent to retrieve some information.")]
async Task InvokeAgentAsync(
[Description("Input query to invoke the agent.")] string query,
CancellationToken cancellationToken)
{
var response = await agent.RunAsync(query, thread: thread, cancellationToken: cancellationToken).ConfigureAwait(false);
return response.Text;
}
options ??= new();
options.Name ??= agent.Name;
options.Description ??= agent.Description;
return AIFunctionFactory.Create(InvokeAgentAsync, options);
}
}