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>
135 lines
5.6 KiB
C#
135 lines
5.6 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using Microsoft.Shared.DiagnosticIds;
|
|
|
|
namespace Microsoft.Agents.AI.Compaction;
|
|
|
|
/// <summary>
|
|
/// Factory to create <see cref="CompactionTrigger"/> predicates.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// A <see cref="CompactionTrigger"/> defines a condition based on <see cref="CompactionMessageIndex"/> metrics used
|
|
/// by a <see cref="CompactionStrategy"/> to determine when to trigger compaction and when the target
|
|
/// compaction threshold has been met.
|
|
/// </para>
|
|
/// <para>
|
|
/// Combine triggers with <see cref="All"/> or <see cref="Any"/> for compound conditions.
|
|
/// </para>
|
|
/// </remarks>
|
|
[Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)]
|
|
public static class CompactionTriggers
|
|
{
|
|
/// <summary>
|
|
/// Always trigger, regardless of the message index state.
|
|
/// </summary>
|
|
public static readonly CompactionTrigger Always =
|
|
_ => true;
|
|
|
|
/// <summary>
|
|
/// Never trigger, regardless of the message index state.
|
|
/// </summary>
|
|
public static readonly CompactionTrigger Never =
|
|
_ => false;
|
|
|
|
/// <summary>
|
|
/// Creates a trigger that fires when the included token count is below the specified maximum.
|
|
/// </summary>
|
|
/// <param name="maxTokens">The token threshold.</param>
|
|
/// <returns>A <see cref="CompactionTrigger"/> that evaluates included token count.</returns>
|
|
public static CompactionTrigger TokensBelow(int maxTokens) =>
|
|
index => index.IncludedTokenCount < maxTokens;
|
|
|
|
/// <summary>
|
|
/// Creates a trigger that fires when the included token count exceeds the specified maximum.
|
|
/// </summary>
|
|
/// <param name="maxTokens">The token threshold.</param>
|
|
/// <returns>A <see cref="CompactionTrigger"/> that evaluates included token count.</returns>
|
|
public static CompactionTrigger TokensExceed(int maxTokens) =>
|
|
index => index.IncludedTokenCount > maxTokens;
|
|
|
|
/// <summary>
|
|
/// Creates a trigger that fires when the included message count exceeds the specified maximum.
|
|
/// </summary>
|
|
/// <param name="maxMessages">The message threshold.</param>
|
|
/// <returns>A <see cref="CompactionTrigger"/> that evaluates included message count.</returns>
|
|
public static CompactionTrigger MessagesExceed(int maxMessages) =>
|
|
index => index.IncludedMessageCount > maxMessages;
|
|
|
|
/// <summary>
|
|
/// Creates a trigger that fires when the included user turn count exceeds the specified maximum.
|
|
/// </summary>
|
|
/// <param name="maxTurns">The turn threshold.</param>
|
|
/// <returns>A <see cref="CompactionTrigger"/> that evaluates included turn count.</returns>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// A user turn starts with a <see cref="CompactionGroupKind.User"/> group and includes all subsequent
|
|
/// non-user, non-system groups until the next user group or end of conversation. Each group is assigned
|
|
/// a <see cref="CompactionMessageGroup.TurnIndex"/> indicating which user turn it belongs to.
|
|
/// System messages (<see cref="CompactionGroupKind.System"/>) are always assigned a <see langword="null"/>
|
|
/// <see cref="CompactionMessageGroup.TurnIndex"/> since they never belong to a user turn.
|
|
/// </para>
|
|
/// <para>
|
|
/// The turn count is the number of distinct values defined by <see cref="CompactionMessageGroup.TurnIndex"/>.
|
|
/// </para>
|
|
/// </remarks>
|
|
public static CompactionTrigger TurnsExceed(int maxTurns) =>
|
|
index => index.IncludedTurnCount > maxTurns;
|
|
|
|
/// <summary>
|
|
/// Creates a trigger that fires when the included group count exceeds the specified maximum.
|
|
/// </summary>
|
|
/// <param name="maxGroups">The group threshold.</param>
|
|
/// <returns>A <see cref="CompactionTrigger"/> that evaluates included group count.</returns>
|
|
public static CompactionTrigger GroupsExceed(int maxGroups) =>
|
|
index => index.IncludedGroupCount > maxGroups;
|
|
|
|
/// <summary>
|
|
/// Creates a trigger that fires when the included message index contains at least one
|
|
/// non-excluded <see cref="CompactionGroupKind.ToolCall"/> group.
|
|
/// </summary>
|
|
/// <returns>A <see cref="CompactionTrigger"/> that evaluates included tool call presence.</returns>
|
|
public static CompactionTrigger HasToolCalls() =>
|
|
index => index.Groups.Any(g => !g.IsExcluded && g.Kind == CompactionGroupKind.ToolCall);
|
|
|
|
/// <summary>
|
|
/// Creates a compound trigger that fires only when <b>all</b> of the specified triggers fire.
|
|
/// </summary>
|
|
/// <param name="triggers">The triggers to combine with logical AND.</param>
|
|
/// <returns>A <see cref="CompactionTrigger"/> that requires all conditions to be met.</returns>
|
|
public static CompactionTrigger All(params CompactionTrigger[] triggers) =>
|
|
index =>
|
|
{
|
|
for (int i = 0; i < triggers.Length; i++)
|
|
{
|
|
if (!triggers[i](index))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
/// <summary>
|
|
/// Creates a compound trigger that fires when <b>any</b> of the specified triggers fire.
|
|
/// </summary>
|
|
/// <param name="triggers">The triggers to combine with logical OR.</param>
|
|
/// <returns>A <see cref="CompactionTrigger"/> that requires at least one condition to be met.</returns>
|
|
public static CompactionTrigger Any(params CompactionTrigger[] triggers) =>
|
|
index =>
|
|
{
|
|
for (int i = 0; i < triggers.Length; i++)
|
|
{
|
|
if (triggers[i](index))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
};
|
|
}
|