// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Agents.AI;
using Microsoft.Extensions.Logging;
using Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Extensions.AI;
///
/// Provides extension methods for building a from a .
///
public static class ChatClientBuilderExtensions
{
///
/// Build a from the pipeline described by this .
///
/// A builder for creating pipelines of .
///
/// Optional system instructions that guide the agent's behavior. These instructions are provided to the
/// with each invocation to establish the agent's role and behavior.
///
///
/// Optional name for the agent. This name is used for identification and logging purposes.
///
///
/// Optional human-readable description of the agent's purpose and capabilities.
/// This description can be useful for documentation and agent discovery scenarios.
///
///
/// Optional collection of tools that the agent can invoke during conversations.
/// These tools augment any tools that may be provided to the agent via when
/// the agent is run.
///
///
/// Optional logger factory for creating loggers used by the agent and its components.
///
///
/// Optional service provider for resolving dependencies required by AI functions and other agent components.
/// This is particularly important when using custom tools that require dependency injection.
///
/// A new instance.
public static ChatClientAgent BuildAIAgent(
this ChatClientBuilder builder,
string? instructions = null,
string? name = null,
string? description = null,
IList? tools = null,
ILoggerFactory? loggerFactory = null,
IServiceProvider? services = null) =>
Throw.IfNull(builder).Build(services).AsAIAgent(
instructions: instructions,
name: name,
description: description,
tools: tools,
loggerFactory: loggerFactory,
services: services);
///
/// Creates a new instance.
///
/// A builder for creating pipelines of .
///
/// Configuration options that control all aspects of the agent's behavior, including chat settings,
/// message store factories, context provider factories, and other advanced configurations.
///
///
/// Optional logger factory for creating loggers used by the agent and its components.
///
///
/// Optional service provider for resolving dependencies required by AI functions and other agent components.
/// This is particularly important when using custom tools that require dependency injection.
///
/// A new instance.
public static ChatClientAgent BuildAIAgent(
this ChatClientBuilder builder,
ChatClientAgentOptions? options,
ILoggerFactory? loggerFactory = null,
IServiceProvider? services = null) =>
Throw.IfNull(builder).Build(services).AsAIAgent(
options: options,
loggerFactory: loggerFactory,
services: services);
///
/// Adds a to the chat client pipeline.
///
///
///
/// This decorator should be positioned between the and the leaf
/// in the pipeline. It persists chat history after each individual service call
/// and updates the session per call for both framework-managed
/// and service-stored chat history scenarios.
///
///
/// This extension method is intended for use with custom chat client stacks when
/// is .
/// When is (the default),
/// the automatically includes this decorator in the pipeline and activates it when
/// is .
///
///
/// This decorator only works within the context of a running and will throw an
/// exception if used in any other stack.
///
///
/// The to add the decorator to.
/// The for chaining.
[Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)]
public static ChatClientBuilder UsePerServiceCallChatHistoryPersistence(this ChatClientBuilder builder)
{
return builder.Use(innerClient => new PerServiceCallChatHistoryPersistingChatClient(innerClient));
}
}