Documentation
¶
Index ¶
- func CalculateContextTokens(usage *provider.Usage) int
- func EstimateContextTokens(messages []provider.Message) (tokens int, lastUsageIndex int)
- func EstimateContextTokensWithEstimator(messages []provider.Message, estimator TokenEstimator) (tokens int, lastUsageIndex int)
- func EstimateTokens(msg provider.Message) int
- func FindTurnStartIndex(messages []provider.Message, entryIndex, startIndex int) int
- func FindValidCutPoints(messages []provider.Message, startIndex, endIndex int) []int
- func GenerateSummary(ctx context.Context, messages []provider.Message, p provider.Provider, ...) (string, error)
- func GenerateSummaryInsertThenCompress(ctx context.Context, messages []provider.Message, p provider.Provider, ...) (string, error)
- func GenerateSummaryInsertThenCompressWithTemplate(ctx context.Context, messages []provider.Message, p provider.Provider, ...) (string, error)
- func HasCompactableMessages(messages []provider.Message, model *provider.Model, ...) bool
- func SerializeConversation(messages []provider.Message) string
- func ShouldCompact(contextTokens int, contextWindow int, reserveTokens int) bool
- type CompactionResult
- type CompactionSettings
- type CompressionTemplate
- type ContextUsage
- type CutPointResult
- type GenericTokenEstimator
- type ModelAwareTokenEstimator
- type TokenEstimator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalculateContextTokens ¶
CalculateContextTokens calculates total context tokens from usage. Uses the totalTokens field when available, falls back to computing from components.
func EstimateContextTokens ¶
EstimateContextTokens estimates context tokens from messages. Uses the last assistant's usage when available, then estimates trailing messages.
func EstimateContextTokensWithEstimator ¶
func EstimateContextTokensWithEstimator(messages []provider.Message, estimator TokenEstimator) (tokens int, lastUsageIndex int)
EstimateContextTokensWithEstimator estimates context tokens using provider usage when available, then the supplied estimator for trailing messages.
func EstimateTokens ¶
EstimateTokens estimates token count for a message using the default estimator.
func FindTurnStartIndex ¶
FindTurnStartIndex finds the user message that starts the turn containing the given index.
func FindValidCutPoints ¶
FindValidCutPoints finds valid cut points in messages. Valid cut points are user, assistant messages (never tool results).
func GenerateSummary ¶
func GenerateSummary( ctx context.Context, messages []provider.Message, p provider.Provider, model *provider.Model, reserveTokens int, previousSummary string, ) (string, error)
GenerateSummary is the legacy interface that delegates to Insert-then-Compress. Kept for backward compatibility but now uses the same system prompt. Deprecated: use GenerateSummaryInsertThenCompress directly.
func GenerateSummaryInsertThenCompress ¶
func GenerateSummaryInsertThenCompress( ctx context.Context, messages []provider.Message, p provider.Provider, model *provider.Model, systemPrompt string, tools []provider.ToolDefinition, previousSummary string, maxTokens int, ) (string, error)
GenerateSummaryInsertThenCompress generates a summary using Insert-then-Compress pattern. This implements Rule R4.1-R4.2: use the SAME system prompt and tools, not a separate call. The compression instruction is injected as a system_injected user message at the end of the conversation.
func GenerateSummaryInsertThenCompressWithTemplate ¶
func GenerateSummaryInsertThenCompressWithTemplate( ctx context.Context, messages []provider.Message, p provider.Provider, model *provider.Model, systemPrompt string, tools []provider.ToolDefinition, previousSummary string, maxTokens int, template CompressionTemplate, ) (string, error)
GenerateSummaryInsertThenCompressWithTemplate generates a summary using the supplied compression template.
func HasCompactableMessages ¶
func HasCompactableMessages(messages []provider.Message, model *provider.Model, settings CompactionSettings, previousSummary string) bool
HasCompactableMessages reports whether compaction would have older messages to summarize after preserving the configured recent context.
func SerializeConversation ¶
SerializeConversation serializes messages to text for summarization.
Types ¶
type CompactionResult ¶
CompactionResult holds the result of a compaction operation.
func Compact ¶
func Compact( ctx context.Context, messages []provider.Message, p provider.Provider, model *provider.Model, systemPrompt string, tools []provider.ToolDefinition, settings CompactionSettings, previousSummary string, ) (*CompactionResult, error)
Compact performs context compaction on the messages using Insert-then-Compress pattern. This implements Rule R4.1-R4.4.
func CompactWithLegacyInterface ¶
func CompactWithLegacyInterface( ctx context.Context, messages []provider.Message, p provider.Provider, model *provider.Model, settings CompactionSettings, previousSummary string, ) (*CompactionResult, error)
CompactWithLegacyInterface is a compatibility wrapper that calls the old Compact signature. Deprecated: use the new Compact with systemPrompt and tools parameters.
type CompactionSettings ¶
type CompactionSettings struct {
Enabled bool `json:"enabled"`
ReserveTokens int `json:"reserveTokens"`
KeepRecentTokens int `json:"keepRecentTokens"`
Tokenizer string `json:"tokenizer,omitempty"`
TokenizerModel string `json:"tokenizerModel,omitempty"`
Template string `json:"template,omitempty"`
// Idle compression settings (R5.1-R5.5)
// When enabled, triggers compression during idle periods to maintain cache warmth.
IdleCompressionEnabled bool `json:"idleCompressionEnabled,omitempty"` // R5.1: default off
IdleTimeoutSeconds int `json:"idleTimeoutSeconds,omitempty"` // seconds of inactivity before triggering (default: 90)
IdleMinTokensForCompress int `json:"idleMinTokensForCompress,omitempty"` // minimum tokens to trigger idle compression (default: 150000)
}
CompactionSettings holds compaction configuration.
func DefaultCompactionSettings ¶
func DefaultCompactionSettings() CompactionSettings
DefaultCompactionSettings returns default compaction settings.
func NormalizeCompactionSettings ¶
func NormalizeCompactionSettings(settings CompactionSettings) CompactionSettings
NormalizeCompactionSettings applies runtime defaults for zero-valued limits.
type CompressionTemplate ¶
CompressionTemplate contains instructions for initial and update compaction.
func ResolveCompressionTemplate ¶
func ResolveCompressionTemplate(name string) CompressionTemplate
ResolveCompressionTemplate returns a built-in compression template.
type ContextUsage ¶
type ContextUsage struct {
Tokens int // Current estimated context tokens
ContextWindow int // Maximum context window size
Percent *float64 // Usage percentage, nil if unknown
}
ContextUsage holds the current context usage information.
type CutPointResult ¶
CutPointResult holds information about where to cut the conversation.
func FindCutPoint ¶
func FindCutPoint(messages []provider.Message, startIndex, endIndex, keepRecentTokens int) CutPointResult
FindCutPoint finds the cut point that keeps approximately keepRecentTokens.
func FindCutPointWithEstimator ¶
func FindCutPointWithEstimator(messages []provider.Message, startIndex, endIndex, keepRecentTokens int, estimator TokenEstimator) CutPointResult
FindCutPointWithEstimator finds the cut point using the supplied token estimator.
type GenericTokenEstimator ¶
type GenericTokenEstimator struct{}
GenericTokenEstimator preserves the existing chars/4 heuristic.
func (GenericTokenEstimator) EstimateMessagesTokens ¶
func (e GenericTokenEstimator) EstimateMessagesTokens(messages []provider.Message) int
EstimateMessagesTokens estimates the total token count for messages.
func (GenericTokenEstimator) EstimateTokens ¶
func (GenericTokenEstimator) EstimateTokens(msg provider.Message) int
EstimateTokens estimates token count for a message using a chars/4 heuristic.
type ModelAwareTokenEstimator ¶
ModelAwareTokenEstimator preserves the chars/4 text heuristic while using model-family image token formulas when image dimensions are available.
func (ModelAwareTokenEstimator) EstimateMessagesTokens ¶
func (e ModelAwareTokenEstimator) EstimateMessagesTokens(messages []provider.Message) int
func (ModelAwareTokenEstimator) EstimateTokens ¶
func (e ModelAwareTokenEstimator) EstimateTokens(msg provider.Message) int
type TokenEstimator ¶
type TokenEstimator interface {
EstimateTokens(msg provider.Message) int
EstimateMessagesTokens(messages []provider.Message) int
}
TokenEstimator estimates the context footprint of provider messages.
func ResolveTokenEstimator ¶
func ResolveTokenEstimator(settings CompactionSettings, model *provider.Model) TokenEstimator
ResolveTokenEstimator returns the configured estimator. Unsupported tokenizer names intentionally fall back to generic so existing configuration remains tolerant while model-specific estimators are added.