compaction

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultReserveTokens    int64 = 16384
	DefaultKeepRecentTokens int64 = 20000
)
View Source
const SummarizationPrompt = `` /* 565-byte string literal not displayed */
View Source
const SummarizationSystemPrompt = "You are a context summarization assistant. Output plain text only. Do not continue the conversation."

Variables

This section is empty.

Functions

func BuildSummarizationInput

func BuildSummarizationInput(msgs []op.Message) string

func Compact

func Compact(ctx context.Context, msgs []op.Message, keepRecentTokens int64, summarize SummaryFunc) ([]op.Message, error)

Compact summarizes older messages and returns compacted context: [system summary message] + kept recent messages.

func FindCutPoint

func FindCutPoint(msgs []op.Message, keepRecentTokens int64) int

FindCutPoint returns the index of the first message to keep. Never cuts at a tool result (must stay with its preceding assistant message). Returns -1 when no compaction cut point is suitable.

func IsOverflowError

func IsOverflowError(err error) bool

IsOverflowError checks whether an error likely indicates context window overflow.

func ResolveSettings

func ResolveSettings(cfg op.CompactionConfig) (enabled bool, reserveTokens, keepRecentTokens int64)

ResolveSettings applies defaults and returns normalized compaction settings.

func ShouldCompact

func ShouldCompact(estimatedTokens, contextWindow, reserveTokens int64) bool

ShouldCompact returns true when estimated context tokens exceed available budget.

Types

type ContextEstimate

type ContextEstimate struct {
	Tokens         int64 // total estimated context tokens
	UsageTokens    int64 // from last assistant's API usage (0 if none)
	TrailingTokens int64 // estimated tokens for messages after last usage
	LastUsageIndex int   // index of last assistant with usage, -1 if none
}

ContextEstimate holds the result of hybrid context token estimation. When API usage is available, UsageTokens comes from the last assistant's real token count and TrailingTokens is estimated only for messages after it. When no usage is available, everything is estimated with chars/4.

func EstimateContextTokens

func EstimateContextTokens(msgs []op.Message) ContextEstimate

EstimateContextTokens implements pi-mono's hybrid estimation: use real API usage from the last assistant message when available, then only estimate trailing messages with chars/4.

type SummaryFunc

type SummaryFunc func(ctx context.Context, conversation string) (string, error)

Jump to

Keyboard shortcuts

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