context

package
v1.2.96 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateContextTokens

func CalculateContextTokens(usage *provider.Usage) int

CalculateContextTokens returns the provider-reported input footprint. It excludes output tokens because this value is used against the next request's context window.

func EstimateContextTokens

func EstimateContextTokens(messages []provider.Message) (tokens int, lastUsageIndex int)

EstimateContextTokens estimates context tokens from messages using the shared local tokenizer and provider usage when available.

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 EstimateGuardTokens added in v1.1.77

func EstimateGuardTokens(msg provider.Message, estimator TokenEstimator) int

EstimateGuardTokens returns a conservative request-size estimate for one message. It uses the shared tokenizer, with a payload-size floor for tool results whose repetitive content can compress unusually well under BPE.

func EstimateTextTokens added in v1.1.77

func EstimateTextTokens(text string) int

EstimateTextTokens returns the shared local text token estimate.

func EstimateTokens

func EstimateTokens(msg provider.Message) int

EstimateTokens estimates token count for a message using the default estimator.

func FindTurnStartIndex

func FindTurnStartIndex(messages []provider.Message, entryIndex, startIndex int) int

FindTurnStartIndex finds the user message that starts the turn containing the given index.

func FindValidCutPoints

func FindValidCutPoints(messages []provider.Message, startIndex, endIndex int) []int

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

func SerializeConversation(messages []provider.Message) string

SerializeConversation serializes messages to text for summarization.

func ShouldCompact

func ShouldCompact(contextTokens int, contextWindow int, reserveTokens int) bool

ShouldCompact checks if compaction should trigger based on context usage.

func ShouldCompactPercent added in v1.1.62

func ShouldCompactPercent(contextTokens int, contextWindow int, threshold float64) bool

ShouldCompactPercent checks if compaction should trigger based on the percentage of the context window currently occupied.

Types

type CompactOptions added in v1.1.62

type CompactOptions struct {
	Force bool

	// Keep compaction requests aligned with the main agent request. The output
	// limit is intentionally separate, but reasoning and sampling settings are
	// carried through instead of falling back to provider defaults.
	ThinkingLevel provider.ThinkingLevel
	Temperature   *float64
	TopP          *float64

	// Summarize is supplied by the agent runtime. It must execute the summary
	// through the normal sub-agent Agent loop; this package only prepares the
	// messages and never owns provider request construction.
	Summarize func(context.Context, []provider.Message, int) (string, error)
}

CompactOptions controls how aggressively compaction should preserve recent messages. Forced compaction is used for explicit user requests and may produce a summary-only checkpoint when there is no older history outside the recent keep window.

type CompactionResult

type CompactionResult struct {
	Summary        string
	FirstKeptIndex int
	TokensBefore   int
}

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.

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.

func CompactWithOptions added in v1.1.62

func CompactWithOptions(
	ctx context.Context,
	messages []provider.Message,
	p provider.Provider,
	model *provider.Model,
	systemPrompt string,
	tools []provider.ToolDefinition,
	settings CompactionSettings,
	previousSummary string,
	options CompactOptions,
) (*CompactionResult, error)

CompactWithOptions performs context compaction with optional forced behavior.

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"`
}

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

type CompressionTemplate struct {
	Name              string
	Instruction       string
	UpdateInstruction string
}

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      `json:"tokens"`         // Deprecated alias for TotalTokens.
	TotalTokens   int      `json:"total_tokens"`   // Full current input footprint.
	Input         int      `json:"input"`          // Non-cache input tokens.
	CacheRead     int      `json:"cache_read"`     // Input tokens served from cache.
	CacheWrite    int      `json:"cache_write"`    // Input tokens written to cache.
	ContextWindow int      `json:"context_window"` // Maximum context window.
	Percent       *float64 `json:"percent,omitempty"`
}

ContextUsage holds the current request-input footprint. TotalTokens is the context-window total and is deliberately input-only; output tokens from a previous response are not counted as current context.

func ContextUsageFromMessages added in v1.1.77

func ContextUsageFromMessages(messages []provider.Message, estimator TokenEstimator) ContextUsage

ContextUsageFromMessages returns a detailed input-footprint estimate. A provider usage record is authoritative for the latest completed assistant turn; only messages added after it are estimated locally.

type CutPointResult

type CutPointResult struct {
	FirstKeptIndex int
	TurnStartIndex int
	IsSplitTurn    bool
}

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 uses the embedded DeepSeek V3 byte-level BPE tokenizer for text and the existing provider-aware formulas for images.

func (GenericTokenEstimator) EstimateMessagesTokens

func (e GenericTokenEstimator) EstimateMessagesTokens(messages []provider.Message) int

func (GenericTokenEstimator) EstimateTokens

func (GenericTokenEstimator) EstimateTokens(msg provider.Message) int

type ModelAwareTokenEstimator

type ModelAwareTokenEstimator struct {
	Model *provider.Model
}

ModelAwareTokenEstimator uses the same local DeepSeek V3 tokenizer for all text. Model-specific image formulas are retained because image tokens are not represented by the text tokenizer.

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. Text always uses the embedded DeepSeek V3 tokenizer; the model only affects image accounting.

Jump to

Keyboard shortcuts

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