smart_compact

package
v0.260313.0-preview Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2026 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package smart_compact provides conversation compression strategies and transformers for Anthropic requests.

The package includes:

  • CompressionStrategy interface for defining compression algorithms
  • Strategy implementations (thinking removal, round-only, round-files)
  • Transformer adapters that bridge strategies to the protocol.Transformer interface

Strategies compress conversation rounds by removing thinking blocks, tool calls, and tool results while preserving the essential flow of user requests and assistant responses.

Index

Constants

View Source
const (
	VirtualReadTool   = "read_file"
	ExpiredContentMsg = "Content has expired, please view again"
)

Virtual tool constants

Variables

This section is empty.

Functions

func CreateBetaVirtualToolCalls

func CreateBetaVirtualToolCalls(files []string) []any

CreateBetaVirtualToolCalls creates virtual tool calls for beta API.

func CreateVirtualToolCalls

func CreateVirtualToolCalls(files []string) []any

CreateVirtualToolCalls creates virtual tool use + result messages.

func FormatFileNote

func FormatFileNote(files []string) string

FormatFileNote formats collected file paths into a note string.

func NewRoundFilesTransformer

func NewRoundFilesTransformer() protocol.Transformer

NewRoundFilesTransformer creates a transformer for round-files strategy.

func NewRoundOnlyTransformer

func NewRoundOnlyTransformer() protocol.Transformer

NewRoundOnlyTransformer creates a transformer for round-only strategy.

Types

type CompactTransformer

type CompactTransformer struct {
	protocol.Transformer

	KeepLastNRounds int // Number of recent rounds to preserve thinking blocks (min: 1)
	// contains filtered or unexported fields
}

CompactTransformer implements the Transformer interface.

func NewCompactTransformer

func NewCompactTransformer(keepLastNRounds int) *CompactTransformer

NewCompactTransformer creates a new smart_compact transformer instance.

The keepLastNRounds parameter controls how many recent conversation rounds should have their thinking blocks preserved. Higher values retain more reasoning context but save fewer tokens.

Recommendations:

  • keepLastNRounds=1: Default, preserves only the current request's thinking
  • keepLastNRounds=2-3: Suitable for multi-step reasoning, debugging, or document analysis
  • Minimum allowed value is 1 (current round's thinking is always preserved)

func (*CompactTransformer) HandleV1

HandleV1 compacts an Anthropic v1 request by removing thinking fields from non-current rounds.

func (*CompactTransformer) HandleV1Beta

HandleV1Beta compacts an Anthropic v1beta request by removing thinking fields from non-current rounds.

type CompressionStrategy

type CompressionStrategy interface {
	// Name returns the strategy identifier
	Name() string

	// CompressV1 compresses v1 messages
	CompressV1(messages []anthropic.MessageParam) []anthropic.MessageParam

	// CompressBeta compresses beta messages
	CompressBeta(messages []anthropic.BetaMessageParam) []anthropic.BetaMessageParam
}

CompressionStrategy defines the interface for compression algorithms.

type PathUtil

type PathUtil struct {
	// contains filtered or unexported fields
}

PathUtil extracts and manipulates file paths from tool parameters.

func NewPathUtil

func NewPathUtil() *PathUtil

NewPathUtil creates a new path utility.

func (*PathUtil) Extract

func (e *PathUtil) Extract(input string) []string

Extract extracts all file paths from input string.

func (*PathUtil) ExtractFromMap

func (e *PathUtil) ExtractFromMap(m map[string]any) []string

ExtractFromMap extracts file paths from map (tool input parameters).

type RoundOnlyStrategy

type RoundOnlyStrategy struct {
	// contains filtered or unexported fields
}

RoundOnlyStrategy keeps only user request + assistant conclusion.

func NewRoundOnlyStrategy

func NewRoundOnlyStrategy() *RoundOnlyStrategy

NewRoundOnlyStrategy creates a new round-only strategy.

func (*RoundOnlyStrategy) CompressBeta

CompressBeta compresses beta messages (same logic).

func (*RoundOnlyStrategy) CompressV1

func (s *RoundOnlyStrategy) CompressV1(messages []anthropic.MessageParam) []anthropic.MessageParam

CompressV1 compresses v1 messages by keeping only user request + assistant conclusion.

func (*RoundOnlyStrategy) Name

func (s *RoundOnlyStrategy) Name() string

Name returns the strategy identifier.

type RoundWithFilesStrategy

type RoundWithFilesStrategy struct {
	// contains filtered or unexported fields
}

RoundWithFilesStrategy keeps user/assistant + file paths as virtual tool calls.

func NewRoundWithFilesStrategy

func NewRoundWithFilesStrategy() *RoundWithFilesStrategy

NewRoundWithFilesStrategy creates a new round+files strategy.

func (*RoundWithFilesStrategy) CompressBeta

CompressBeta for beta messages (similar implementation).

func (*RoundWithFilesStrategy) CompressV1

CompressV1 compresses v1 messages keeping user/assistant + virtual file tool calls.

func (*RoundWithFilesStrategy) Name

func (s *RoundWithFilesStrategy) Name() string

Name returns the strategy identifier.

type TransformerWrapper

type TransformerWrapper struct {
	// contains filtered or unexported fields
}

TransformerWrapper adapts strategies to protocol.Transformer interface.

func (*TransformerWrapper) HandleV1

HandleV1 handles compacting for Anthropic v1 requests.

func (*TransformerWrapper) HandleV1Beta

HandleV1Beta handles compacting for Anthropic v1beta requests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL