// 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 intercepts service calls to either persist messages
/// immediately or mark them for later persistence, depending on the parameter.
///
///
/// If is set to , the
/// should be configured with set to
/// as without this combination, messages will never be persisted when using a for
/// chat history persistence.
///
///
/// This extension method is intended for use with custom chat client stacks when
/// is .
/// When is (the default),
/// the automatically injects this decorator.
///
///
/// 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.
///
/// When , messages are marked with metadata but not persisted immediately,
/// and the session's is not updated.
/// The will persist only the marked messages and update the
/// conversation ID at the end of the run.
/// When (the default), messages are persisted and the conversation ID
/// is updated immediately after each service call.
///
/// The for chaining.
[Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)]
public static ChatClientBuilder UseChatHistoryPersisting(this ChatClientBuilder builder, bool markOnly = false)
{
return builder.Use(innerClient => new ChatHistoryPersistingChatClient(innerClient, markOnly));
}
}