Documentation
¶
Index ¶
- Constants
- func BuildSummarizationInput(msgs []op.Message) string
- func Compact(ctx context.Context, msgs []op.Message, keepRecentTokens int64, ...) ([]op.Message, error)
- func FindCutPoint(msgs []op.Message, keepRecentTokens int64) int
- func IsOverflowError(err error) bool
- func ResolveSettings(cfg op.CompactionConfig) (enabled bool, reserveTokens, keepRecentTokens int64)
- func ShouldCompact(estimatedTokens, contextWindow, reserveTokens int64) bool
- type ContextEstimate
- type SummaryFunc
Constants ¶
const ( DefaultReserveTokens int64 = 16384 DefaultKeepRecentTokens int64 = 20000 )
const SummarizationPrompt = `` /* 565-byte string literal not displayed */
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 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 ¶
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 ¶
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 ¶
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.