Skip to content

.NET Compaction - Allow developer to specify a custom formatter for ToolResultCompactionStrategy#4667

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/allow-custom-formatter-toolresultcompactionstrateg
Draft

.NET Compaction - Allow developer to specify a custom formatter for ToolResultCompactionStrategy#4667
Copilot wants to merge 2 commits intomainfrom
copilot/allow-custom-formatter-toolresultcompactionstrateg

Conversation

Copy link
Contributor

Copilot AI commented Mar 12, 2026

Motivation and Context

ToolResultCompactionStrategy used a hardcoded private formatter to collapse tool call groups into a YAML-like summary. There was no way to customize the output format, limiting extensibility for scenarios requiring different summary styles.

Description

  • New toolCallFormatter constructor parameter — optional Func<CompactionMessageGroup, string>?; defaults to null (uses built-in format)
  • ToolCallFormatter public property — exposes the active formatter so callers can inspect what's configured
  • DefaultToolCallFormatter public static method — the former private BuildToolCallSummary, now public so custom formatters can delegate to or wrap the default output
  • CompactCoreAsync updated — calls this.ToolCallFormatter(group) instead of the static method directly
// Use a custom formatter
var strategy = new ToolResultCompactionStrategy(
    trigger: CompactionTriggers.TokensExceed(8000),
    toolCallFormatter: group => $"[Tools used: {string.Join(", ", group.Messages.Select(m => m.Role))}]");

// Wrap the default formatter with additional context
var strategy = new ToolResultCompactionStrategy(
    trigger: CompactionTriggers.TokensExceed(8000),
    toolCallFormatter: group =>
        $"[Turn {group.TurnIndex}]\n{ToolResultCompactionStrategy.DefaultToolCallFormatter(group)}");

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? No — toolCallFormatter is an optional parameter with a null default; existing callers are unaffected.
Original prompt

This section details on the original issue you should resolve

<issue_title>.NET Compaction - Allow developer to specify a custom formatter for ToolResultCompactionStrategy</issue_title>
<issue_description>We currently default to only one mode of compacting tool results. This might benefit by being extensible/injectable while retaining the existing approach as the default.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…trategy

Co-authored-by: crickman <66376200+crickman@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for custom formatter in ToolResultCompactionStrategy Allow developer to specify a custom formatter for ToolResultCompactionStrategy Mar 12, 2026
Copilot AI requested a review from crickman March 12, 2026 23:46
@crickman crickman changed the title Allow developer to specify a custom formatter for ToolResultCompactionStrategy .NET Compaction - Allow developer to specify a custom formatter for ToolResultCompactionStrategy Mar 13, 2026
@crickman crickman moved this to In Progress in Agent Framework Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

.NET Compaction - Allow developer to specify a custom formatter for ToolResultCompactionStrategy

2 participants