compaction

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SerializeMessages

func SerializeMessages(messages []types.Message) string

SerializeMessages converts a slice of messages into a compactable text representation. Tool outputs are truncated to maxToolOutputChars to keep the summary lean.

func SplitMessages

func SplitMessages(messages []types.Message, keepTokens int, estimateFn func(string) int) (head []types.Message, recent []types.Message)

SplitMessages divides messages into a "head" (to summarize) and "recent" (to keep verbatim). The split point is determined by walking backwards from the end until keepTokens worth of content is accumulated.

func Template

func Template() string

Template returns the compaction summary prompt template.

Types

type Compactor

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

Compactor manages automatic session compaction. It monitors token usage against model context limits and generates structured summaries when usage exceeds the threshold.

func New

func New(cfg Config, tokenEst *tokens.Estimator) *Compactor

New creates a Compactor with the given config and token estimator.

func (*Compactor) Compact

func (c *Compactor) Compact(ctx context.Context, messages []types.Message, p provider.LLMProvider, modelID string) (*Result, error)

Compact generates a structured summary of old messages and returns a new message list with the old messages replaced by the summary. Uses the LLM provider to generate the summary.

func (*Compactor) LastResult

func (c *Compactor) LastResult() *Result

LastResult returns the most recent compaction result, or nil if no compaction has been performed.

func (*Compactor) ShouldCompact

func (c *Compactor) ShouldCompact(messages []types.Message, contextLength int64) bool

ShouldCompact returns true if the messages exceed the compaction threshold. threshold = contextLength - outputReserve - buffer

type Config

type Config struct {
	Auto       bool
	Buffer     int // tokens reserved before compaction triggers
	KeepTokens int // tokens of recent history to preserve verbatim
}

Config holds compaction settings.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns compaction defaults.

type Result

type Result struct {
	Compacted       bool
	TokensBefore    int
	TokensAfter     int
	Summary         string
	MessagesRemoved int
	DurationMs      int64
}

Result holds the outcome of a compaction operation.

Jump to

Keyboard shortcuts

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