// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
namespace SampleApp;
///
/// Provides extension methods for adding structured output capabilities to instances.
///
internal static class AIAgentBuilderExtensions
{
///
/// Adds structured output capabilities to the agent pipeline, enabling conversion of text responses to structured JSON format.
///
/// The to which structured output support will be added.
///
/// The chat client used to transform text responses into structured JSON format.
/// If , the chat client will be resolved from the service provider.
///
///
/// An optional factory function that returns the instance to use.
/// This allows for fine-tuning the structured output behavior such as setting the response format or system message.
///
/// The with structured output capabilities added, enabling method chaining.
///
///
/// A must be specified either through the
/// at runtime or the
/// provided during configuration.
///
///
public static AIAgentBuilder UseStructuredOutput(
this AIAgentBuilder builder,
IChatClient? chatClient = null,
Func? optionsFactory = null)
{
ArgumentNullException.ThrowIfNull(builder);
return builder.Use((innerAgent, services) =>
{
chatClient ??= services?.GetService()
?? throw new InvalidOperationException($"No {nameof(IChatClient)} was provided and none could be resolved from the service provider. Either provide an {nameof(IChatClient)} explicitly or register one in the dependency injection container.");
return new StructuredOutputAgent(innerAgent, chatClient, optionsFactory?.Invoke());
});
}
}