context

package
v1.1.31 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoCompactThresholdRatio

func AutoCompactThresholdRatio() float64

func AutoCompactThresholdTokens

func AutoCompactThresholdTokens(maxTokens int) int

func EstimateTokens

func EstimateTokens(text string) int

EstimateTokens provides a more accurate token estimation than simple len/4. It distinguishes ASCII characters (~0.25 token/char) from CJK characters (~0.5 token/char).

Types

type ContextManager

type ContextManager interface {
	Add(msg provider.Message)
	Messages() []provider.Message
	TokenCount() int
	MaxTokens() int
	SetMaxTokens(n int)
	SetOutputReserve(n int)
	RecordUsage(usage provider.TokenUsage)
	Summarize(ctx context.Context, prov provider.Provider) error
	CheckAndSummarize(ctx context.Context, prov provider.Provider) (bool, error)
	TruncateOldestGroupForRetry() bool
	Clear()
	UsageRatio() float64
	AutoCompactThreshold() int
}

ContextManager manages conversation history, tracking tokens and auto-summarizing.

⚠️ Consuming packages must import this as "ctxpkg" to avoid collision with the standard library "context" package.

type Manager

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

Manager implements ContextManager.

func NewManager

func NewManager(maxTokens int) *Manager

NewManager creates a ContextManager with the given context window limit.

func (*Manager) Add

func (m *Manager) Add(msg provider.Message)

func (*Manager) AutoCompactThreshold added in v1.1.17

func (m *Manager) AutoCompactThreshold() int

func (*Manager) CheckAndSummarize

func (m *Manager) CheckAndSummarize(ctx context.Context, prov provider.Provider) (bool, error)

CheckAndSummarize triggers summarization if usage ratio >= threshold.

func (*Manager) Clear

func (m *Manager) Clear()

func (*Manager) MaxTokens

func (m *Manager) MaxTokens() int

func (*Manager) Messages

func (m *Manager) Messages() []provider.Message

func (*Manager) Microcompact

func (m *Manager) Microcompact() bool

Microcompact reduces old bulky tool results in-place before falling back to full summarization.

func (*Manager) RecordUsage added in v1.1.17

func (m *Manager) RecordUsage(usage provider.TokenUsage)

func (*Manager) SetMaxTokens

func (m *Manager) SetMaxTokens(n int)

func (*Manager) SetOutputReserve added in v1.1.17

func (m *Manager) SetOutputReserve(n int)

func (*Manager) SetProvider

func (m *Manager) SetProvider(p provider.Provider)

SetProvider sets the provider for provider-aware token counting.

func (*Manager) SetTodoFilePath added in v1.1.7

func (m *Manager) SetTodoFilePath(path string)

func (*Manager) Summarize

func (m *Manager) Summarize(ctx context.Context, prov provider.Provider) error

Summarize compresses old messages into a summary while retaining an adaptive recent suffix sized to fit within the target token budget.

func (*Manager) TokenCount

func (m *Manager) TokenCount() int

func (*Manager) TruncateOldestGroupForRetry added in v1.1.15

func (m *Manager) TruncateOldestGroupForRetry() bool

func (*Manager) UsageRatio

func (m *Manager) UsageRatio() float64

Jump to

Keyboard shortcuts

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