mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
* Checkpoint * Checkpoint * Stable * Strategies * Updated * Encoding * Formatting * Cleanup * Formatting * Tests * Tuning * Update tests * Test update * Remove working solution * Add sample to solution * Sample readyme * Experimental * Format * Formatting * Encoding * Support IChatReducer * Sample output formatting * Initial plan * Replace CompactingChatClient with MessageCompactionContextProvider Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * Boundary condition * Fix encoding * Fix cast * Test coverage * Namespace * Improvements * Efficiency * Cleanup * Detect service managed conversation * Fix namespace * Fix merge * Fix test expectation * Update dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryChatHistoryProvider.cs Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> * Address PR comments (x1) * Update comment * Update comments * Clean-up * Format output * Sync sample comment * Fix condition * Adjust data-flow * Address comments (x2) * Direct compaction * Fix summarization content * Argument check / fix count calculation * Minor follow-up * Diagnostics * Minor updates * Fix state test * Fix sliding window perf * Stable state keys * Increase size computation * Formatting * Add README.md for Agent_Step18_CompactionPipeline sample (#4574) * Sample comments * Updated * Update dotnet/src/Microsoft.Agents.AI/Compaction/MessageIndex.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update dotnet/tests/Microsoft.Agents.AI.UnitTests/Compaction/CompactionProviderTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update dotnet/src/Microsoft.Agents.AI/Compaction/MessageIndex.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Address copilot comments * Fix namespace * Comments / convensions * Prefix `MessageGroup` and `MessageIndex` * Fix sliding window * Update dotnet/src/Microsoft.Agents.AI/Compaction/SummarizationCompactionStrategy.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryChatHistoryProvider.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Python alignment * Fix merge * Fix equality, readme, and sample * Readme update and ToolResult fix * Update dotnet/src/Microsoft.Agents.AI/Compaction/SummarizationCompactionStrategy.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update dotnet/samples/02-agents/Agents/Agent_Step18_CompactionPipeline/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Simplify readme * Update dotnet/samples/02-agents/Agents/Agent_Step18_CompactionPipeline/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Remove example * Remove unused --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
83 lines
3.3 KiB
C#
83 lines
3.3 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.AI;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Shared.DiagnosticIds;
|
|
using Microsoft.Shared.Diagnostics;
|
|
|
|
namespace Microsoft.Agents.AI.Compaction;
|
|
|
|
/// <summary>
|
|
/// A compaction strategy that delegates to an <see cref="IChatReducer"/> to reduce the conversation's
|
|
/// included messages.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// This strategy bridges the <see cref="IChatReducer"/> abstraction from <c>Microsoft.Extensions.AI</c>
|
|
/// into the compaction pipeline. It collects the currently included messages from the
|
|
/// <see cref="CompactionMessageIndex"/>, passes them to the reducer, and rebuilds the index from the
|
|
/// reduced message list when the reducer produces fewer messages.
|
|
/// </para>
|
|
/// <para>
|
|
/// The <see cref="CompactionTrigger"/> controls when reduction is attempted.
|
|
/// Use <see cref="CompactionTriggers"/> for common trigger conditions such as token or message thresholds.
|
|
/// </para>
|
|
/// <para>
|
|
/// Use this strategy when you have an existing <see cref="IChatReducer"/> implementation
|
|
/// (such as <c>MessageCountingChatReducer</c>) and want to apply it as part of a
|
|
/// <see cref="CompactionStrategy"/> pipeline or as an in-run compaction strategy.
|
|
/// </para>
|
|
/// </remarks>
|
|
[Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)]
|
|
public sealed class ChatReducerCompactionStrategy : CompactionStrategy
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="ChatReducerCompactionStrategy"/> class.
|
|
/// </summary>
|
|
/// <param name="chatReducer">
|
|
/// The <see cref="IChatReducer"/> that performs the message reduction.
|
|
/// </param>
|
|
/// <param name="trigger">
|
|
/// The <see cref="CompactionTrigger"/> that controls when compaction proceeds.
|
|
/// </param>
|
|
public ChatReducerCompactionStrategy(IChatReducer chatReducer, CompactionTrigger trigger)
|
|
: base(trigger)
|
|
{
|
|
this.ChatReducer = Throw.IfNull(chatReducer);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the chat reducer used to reduce messages.
|
|
/// </summary>
|
|
public IChatReducer ChatReducer { get; }
|
|
|
|
/// <inheritdoc/>
|
|
protected override async ValueTask<bool> CompactCoreAsync(CompactionMessageIndex index, ILogger logger, CancellationToken cancellationToken)
|
|
{
|
|
// No need to short-circuit on empty conversations, this is handled by <see cref="CompactionStrategy.CompactAsync"/>.
|
|
List<ChatMessage> includedMessages = [.. index.GetIncludedMessages()];
|
|
|
|
IEnumerable<ChatMessage> reduced = await this.ChatReducer.ReduceAsync(includedMessages, cancellationToken).ConfigureAwait(false);
|
|
IList<ChatMessage> reducedMessages = reduced as IList<ChatMessage> ?? [.. reduced];
|
|
|
|
if (reducedMessages.Count >= includedMessages.Count)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Rebuild the index from the reduced messages
|
|
CompactionMessageIndex rebuilt = CompactionMessageIndex.Create(reducedMessages, index.Tokenizer);
|
|
index.Groups.Clear();
|
|
foreach (CompactionMessageGroup group in rebuilt.Groups)
|
|
{
|
|
index.Groups.Add(group);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|