smartcompact

package
v1.261002.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MPL-2.0 Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package smartcompact provides conversation compression strategies and transformers for Anthropic requests, consumed by the virtual compact models (vmodel/anthropic: "compact-round-only", "compact-round-files", "claude-code-compact", "claude-code-strategy").

The package includes strategy implementations (conversation replay, document, round-only, round-files) and transform.Transform implementations:

NewRoundOnlyTransform() - keeps only user/assistant text
NewRoundFilesTransform() - keeps text + virtual file tools
NewConversationReplayTransformer() - replay-based compression
NewConversationDocumentTransformer() - document-based compression
NewDeduplicationTransform() - removes duplicate tool calls
NewPurgeErrorsTransform() - removes errored tool inputs
NewXMLCompactTransform() - XML-document compaction

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

The flag-driven thinking trim (the SmartCompact scenario flag) is NOT here: it lives in internal/protocolserver/transform.ThinkingCompactTransform, since it is a server-domain transform rather than a vmodel compression strategy.

This package sits alongside vmodel/anthropic and vmodel/openai as a vmodel-level capability, but its implementation is currently Anthropic-SDK-bound (every file imports anthropic-sdk-go) and vmodel/anthropic remains its only consumer. If vmodel/openai needs similar compression, extract the vendor-neutral parts (operating on internal/protocol's Round types) before adding a second consumer here.

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 NewBetaCompactionBlock

func NewBetaCompactionBlock(content string) anthropic.BetaContentBlockParamUnion

func NewConversationDocumentTransformer

func NewConversationDocumentTransformer() transform.Transform

NewConversationDocumentTransformer creates a new document-based compact transformer.

func NewConversationReplayTransformer

func NewConversationReplayTransformer() transform.Transform

NewConversationReplayTransformer creates a new replay transformer.

func NewDeduplicationTransform

func NewDeduplicationTransform() transform.Transform

NewDeduplicationTransform creates a new DeduplicationTransform.

func NewPurgeErrorsTransform

func NewPurgeErrorsTransform(gracePeriodTurns int) transform.Transform

NewPurgeErrorsTransform creates a PurgeErrorsTransform. gracePeriodTurns is the number of turns after which errored tool inputs are removed. 0 disables the transform.

func NewRoundFilesTransformer

func NewRoundFilesTransformer() transform.Transform

NewRoundFilesTransformer creates a transform.Transform for round-files compression.

func NewRoundOnlyTransformer

func NewRoundOnlyTransformer() transform.Transform

NewRoundOnlyTransformer creates a transform.Transform for round-only compression.

func NewXMLCompactTransform

func NewXMLCompactTransform() transform.Transform

NewXMLCompactTransform creates a new XMLCompactTransform.

func NewXMLCompactionTransformer

func NewXMLCompactionTransformer() transform.Transform

NewXMLCompactionTransformer creates a new compaction transformer.

Types

type ConversationDocumentStrategy

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

ConversationDocumentStrategy compresses conversation history into an Anthropic document content block placed in a user message.

Unlike ConditionalCompactStrategy (which produces an assistant text message), this strategy uses the Anthropic document block type, allowing the model to reference the conversation history as a cited document.

Only activates when: 1. Last user message contains "compact" (case-insensitive) 2. Request has tool definitions

func NewConversationDocumentStrategy

func NewConversationDocumentStrategy() *ConversationDocumentStrategy

NewConversationDocumentStrategy creates a new document-based compact strategy.

func (*ConversationDocumentStrategy) CompressBeta

CompressBeta compresses beta messages into a user message with a document block.

func (*ConversationDocumentStrategy) CompressV1

CompressV1 compresses v1 messages into a user message with a document block.

func (*ConversationDocumentStrategy) Name

Name returns the strategy identifier.

type ConversationDocumentTransformer

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

ConversationDocumentTransformer applies document-based compression using transform.Transform interface.

Conditions for activation (same as ConditionalCompactTransformer): 1. Last user message contains "compact" (case-insensitive) 2. Request has tool definitions

func (*ConversationDocumentTransformer) Apply

Apply applies the document-based compression to the request.

func (*ConversationDocumentTransformer) Name

Name returns the transform identifier.

type ConversationReplayStrategy

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

ConversationReplayStrategy compresses conversation history by reconstructing historical message blocks into a single assistant message.

Each conversation turn is represented as: - A text block with "[User]" or "[Assistant]" role prefix - Followed by the original tool_use and tool_result blocks (for assistant turns)

This preserves the structural information (tool calls, results) while fitting everything into a single assistant message that the model can reference.

Beta API: produces assistant message with reconstructed blocks. V1 API: falls back to XML text message (v1 lacks rich block types for this pattern).

func NewConversationReplayStrategy

func NewConversationReplayStrategy() *ConversationReplayStrategy

NewConversationReplayStrategy creates a new replay-based strategy.

func (*ConversationReplayStrategy) CompressBeta

CompressBeta reconstructs historical beta messages into a single assistant message with typed blocks preserving tool_use/tool_result structure.

func (*ConversationReplayStrategy) CompressV1

CompressV1 falls back to XML text message since v1 lacks the block variety needed.

func (*ConversationReplayStrategy) Name

Name returns the strategy identifier.

type ConversationReplayTransformer

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

ConversationReplayTransformer applies replay-based compression using transform.Transform interface.

Same trigger conditions as other compact transformers: 1. Last user message contains "compact" (case-insensitive) 2. Request has tool definitions

func (*ConversationReplayTransformer) Apply

Apply applies the replay-based compression to the request.

func (*ConversationReplayTransformer) Name

Name returns the transform identifier.

type DeduplicationTransform

type DeduplicationTransform struct{}

DeduplicationTransform removes duplicate tool calls from messages. For each unique tool signature (name + params), only the most recent call's output is kept; earlier occurrences have their tool_result content replaced with a placeholder.

func (*DeduplicationTransform) Apply

func (*DeduplicationTransform) Name

func (t *DeduplicationTransform) Name() string

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 PurgeErrorsTransform

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

PurgeErrorsTransform removes the input of errored tool calls that are older than a configurable number of turns. The error output (tool_result content) is preserved so the LLM still has context about what went wrong.

A "turn" here is counted by user-message boundaries. A value of 0 disables the transform entirely.

func (*PurgeErrorsTransform) Apply

func (*PurgeErrorsTransform) Name

func (t *PurgeErrorsTransform) Name() string

type RoundFilesTransform

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

RoundFilesTransform keeps user/assistant + file paths as virtual tool calls. This removes tool_use/tool_result blocks but injects virtual tool calls summarizing which files were used in each round.

func NewRoundFilesTransform

func NewRoundFilesTransform() *RoundFilesTransform

NewRoundFilesTransform creates a new RoundFilesTransform.

func NewRoundWithFilesStrategy

func NewRoundWithFilesStrategy() *RoundFilesTransform

NewRoundWithFilesStrategy creates a RoundFilesTransform for direct use (mainly for tests). This returns the concrete type so tests can call CompressV1/CompressBeta directly.

func (*RoundFilesTransform) Apply

Apply applies the round-files compression to the request.

func (*RoundFilesTransform) CompressBeta

CompressBeta compresses beta messages keeping user/assistant + virtual file tool calls.

func (*RoundFilesTransform) CompressV1

func (t *RoundFilesTransform) CompressV1(messages []anthropic.MessageParam) []anthropic.MessageParam

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

func (*RoundFilesTransform) Name

func (t *RoundFilesTransform) Name() string

Name returns the transform identifier.

type RoundOnlyTransform

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

RoundOnlyTransform keeps only user request + assistant conclusion. This removes all tool_use, tool_result, and thinking blocks from historical rounds.

func NewRoundOnlyStrategy

func NewRoundOnlyStrategy() *RoundOnlyTransform

NewRoundOnlyStrategy creates a RoundOnlyTransform for direct use (mainly for tests). This returns the concrete type so tests can call CompressV1/CompressBeta directly.

func NewRoundOnlyTransform

func NewRoundOnlyTransform() *RoundOnlyTransform

NewRoundOnlyTransform creates a new RoundOnlyTransform.

func (*RoundOnlyTransform) Apply

Apply applies the round-only compression to the request.

func (*RoundOnlyTransform) CompressBeta

CompressBeta compresses beta messages (same logic as CompressV1).

func (*RoundOnlyTransform) CompressV1

func (t *RoundOnlyTransform) CompressV1(messages []anthropic.MessageParam) []anthropic.MessageParam

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

func (*RoundOnlyTransform) Name

func (t *RoundOnlyTransform) Name() string

Name returns the transform identifier.

type XMLCompactTransform

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

XMLCompactTransform compresses conversation history into XML format.

This is a pure compression strategy that always compresses the conversation into an XML-formatted assistant message. The conditional logic should be handled at the Virtual Model layer using ConditionalWrapper.

func (*XMLCompactTransform) Apply

Apply applies XML compression to the request.

func (*XMLCompactTransform) Name

func (t *XMLCompactTransform) Name() string

Name returns the transform identifier.

type XMLCompactionStrategy

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

XMLCompactionStrategy compresses conversation history using Anthropic's native compaction block type (BetaCompactionBlockParam).

Beta API: produces assistant message with compaction block. V1 API: compaction block is unsupported; falls back to assistant text message with XML.

func NewXMLCompactionStrategy

func NewXMLCompactionStrategy() *XMLCompactionStrategy

NewXMLCompactionStrategy creates a new compaction-based strategy.

func (*XMLCompactionStrategy) CompressBeta

CompressBeta compresses beta messages into a single assistant message with a compaction block.

func (*XMLCompactionStrategy) CompressV1

CompressV1 falls back to XML text message since compaction block is beta-only.

type XMLCompactionTransformer

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

XMLCompactionTransformer applies compaction block compression using transform.Transform interface.

Same trigger conditions as other compact transformers: 1. Last user message contains "compact" (case-insensitive) 2. Request has tool definitions

func (*XMLCompactionTransformer) Apply

Apply applies the compaction block compression to the request.

func (*XMLCompactionTransformer) Name

func (t *XMLCompactionTransformer) Name() string

Name returns the transform identifier.

Jump to

Keyboard shortcuts

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