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
- func CreateBetaVirtualToolCalls(files []string) []any
- func CreateVirtualToolCalls(files []string) []any
- func FormatFileNote(files []string) string
- func NewRoundFilesTransformer() protocol.Transformer
- func NewRoundOnlyTransformer() protocol.Transformer
- type CompactTransformer
- type CompressionStrategy
- type PathUtil
- type RoundOnlyStrategy
- type RoundWithFilesStrategy
- type TransformerWrapper
Constants ¶
const ( VirtualReadTool = "read_file" ExpiredContentMsg = "Content has expired, please view again" )
Virtual tool constants
Variables ¶
This section is empty.
Functions ¶
func CreateBetaVirtualToolCalls ¶
CreateBetaVirtualToolCalls creates virtual tool calls for beta API.
func CreateVirtualToolCalls ¶
CreateVirtualToolCalls creates virtual tool use + result messages.
func FormatFileNote ¶
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 ¶
func (t *CompactTransformer) HandleV1(req *anthropic.MessageNewParams) error
HandleV1 compacts an Anthropic v1 request by removing thinking fields from non-current rounds.
func (*CompactTransformer) HandleV1Beta ¶
func (t *CompactTransformer) HandleV1Beta(req *anthropic.BetaMessageNewParams) error
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.
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 ¶
func (s *RoundOnlyStrategy) CompressBeta(messages []anthropic.BetaMessageParam) []anthropic.BetaMessageParam
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 ¶
func (s *RoundWithFilesStrategy) CompressBeta(messages []anthropic.BetaMessageParam) []anthropic.BetaMessageParam
CompressBeta for beta messages (similar implementation).
func (*RoundWithFilesStrategy) CompressV1 ¶
func (s *RoundWithFilesStrategy) CompressV1(messages []anthropic.MessageParam) []anthropic.MessageParam
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 ¶
func (w *TransformerWrapper) HandleV1(req *anthropic.MessageNewParams) error
HandleV1 handles compacting for Anthropic v1 requests.
func (*TransformerWrapper) HandleV1Beta ¶
func (w *TransformerWrapper) HandleV1Beta(req *anthropic.BetaMessageNewParams) error
HandleV1Beta handles compacting for Anthropic v1beta requests.