// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI;
///
/// Contains extension methods for the class.
///
public static class ChatHistoryProviderExtensions
{
///
/// Adds message filtering to an existing , so that messages passed to the and messages
/// provided by the can be filtered, updated or replaced.
///
/// The to add the message filter to.
/// An optional filter function to apply to messages produced by the . If null, no filter is applied at this
/// stage.
/// An optional filter function to apply to the invoked context messages before they are passed to the . If null, no
/// filter is applied at this stage.
/// The with filtering applied.
public static ChatHistoryProvider WithMessageFilters(
this ChatHistoryProvider provider,
Func, IEnumerable>? invokingMessagesFilter = null,
Func? invokedMessagesFilter = null)
{
return new ChatHistoryProviderMessageFilter(
innerProvider: provider,
invokingMessagesFilter: invokingMessagesFilter,
invokedMessagesFilter: invokedMessagesFilter);
}
///
/// Decorates the provided so that it does not add
/// messages with to chat history.
///
/// The to add the message filter to.
/// A new instance that filters out messages so they do not get added.
public static ChatHistoryProvider WithAIContextProviderMessageRemoval(this ChatHistoryProvider provider)
{
return new ChatHistoryProviderMessageFilter(
innerProvider: provider,
invokedMessagesFilter: (ctx) =>
{
ctx.RequestMessages = ctx.RequestMessages.Where(x => x.GetAgentRequestMessageSourceType() != AgentRequestMessageSourceType.AIContextProvider);
return ctx;
});
}
}