context

package
v1.3.6 Latest Latest
Warning

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

Go to latest
Published: May 17, 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(contextWindow int) int

func EstimateTokens

func EstimateTokens(text string) int

EstimateTokens provides a rough token estimation. Uses ~4 chars/token for ASCII and ~1.5 chars/token for CJK, which matches common BPE tokenizer behavior more closely than a flat len/4.

Types

type CompactResult added in v1.2.1

type CompactResult struct {
	Messages   []provider.Message
	TokenCount int
	Changed    bool
}

CompactResult is the output of compacting a CompactSnapshot.

type CompactSnapshot added in v1.2.1

type CompactSnapshot struct {
	Messages      []provider.Message
	OrigLen       int
	ContextWindow int
	OutputReserve int
	TodoPath      string
	Version       int64
}

CompactSnapshot is an immutable point-in-time view used by background compaction. It lets callers summarize a stable copy without mutating the live conversation while an LLM turn may still be running.

func (CompactSnapshot) Compact added in v1.2.1

type ContextManager

type ContextManager interface {
	Add(msg provider.Message)
	Messages() []provider.Message
	TokenCount() int
	// MessagesAndTokenCount returns both values under a single lock,
	// guaranteeing a consistent snapshot.
	MessagesAndTokenCount() ([]provider.Message, int)
	ContextWindow() int
	SetContextWindow(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(contextWindow int) *Manager

NewManager creates a ContextManager with the given context window limit.

func (*Manager) Add

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

func (*Manager) ApplyCompactResult added in v1.2.1

func (m *Manager) ApplyCompactResult(snapshot CompactSnapshot, result CompactResult) (bool, int)

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) CompactSnapshot added in v1.2.1

func (m *Manager) CompactSnapshot() CompactSnapshot

func (*Manager) ContextWindow added in v1.3.0

func (m *Manager) ContextWindow() int

func (*Manager) Messages

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

func (*Manager) MessagesAndTokenCount added in v1.1.43

func (m *Manager) MessagesAndTokenCount() ([]provider.Message, int)

func (*Manager) Microcompact

func (m *Manager) Microcompact() bool

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

func (*Manager) PromptBudget added in v1.2.1

func (m *Manager) PromptBudget() int

func (*Manager) RecordUsage added in v1.1.17

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

func (*Manager) SetContextWindow added in v1.3.0

func (m *Manager) SetContextWindow(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) UpdateFirstSystemMessage added in v1.1.34

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

UpdateFirstSystemMessage replaces the first system message in the context. If no system message exists, it prepends one.

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