// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.ChatCompletion;
namespace Step04;
///
/// Convenience extensions for agent based process patterns.
///
internal static class KernelExtensions
{
///
/// Return chat history from a singleton .
///
public static IChatHistoryProvider GetHistory(this Kernel kernel) =>
kernel.Services.GetRequiredService();
///
/// Access an agent as a keyed service.
///
public static TAgent GetAgent(this Kernel kernel, string key) where TAgent : Agent =>
kernel.Services.GetRequiredKeyedService(key);
///
/// Summarize chat history using reducer accessed as a keyed service.
///
public static async Task SummarizeHistoryAsync(this Kernel kernel, string key, IReadOnlyList history)
{
ChatHistorySummarizationReducer reducer = kernel.Services.GetRequiredKeyedService(key);
IEnumerable? reducedResponse = await reducer.ReduceAsync(history);
ChatMessageContent summary = reducedResponse?.First() ?? throw new InvalidDataException("No summary available");
return summary.ToString();
}
}