memory

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountLines

func CountLines(content string) int

CountLines counts the number of lines in content

func CountTokensInContent

func CountTokensInContent(content string) int

CountTokensInContent estimates tokens in content

Types

type CompactionImpact

type CompactionImpact struct {
	OriginalTokens              int `json:"original_tokens"`
	MicroCompactTokens          int `json:"micro_compact_tokens"`
	EstimatedFullCompactTokens  int `json:"estimated_full_compact_tokens"`
	MicroCompactSavings         int `json:"micro_compact_savings"`
	EstimatedFullCompactSavings int `json:"estimated_full_compact_savings"`
}

CompactionImpact represents the impact of compaction.

type CompactionResult

type CompactionResult struct {
	Messages            []types.Message           `json:"messages"`
	DidCompact          bool                      `json:"did_compact"`
	UsedMicroCompact    bool                      `json:"used_micro_compact"`
	UsedSummaryCompact  bool                      `json:"used_summary_compact"`
	PreCompactTokens    int                       `json:"pre_compact_tokens"`
	PostCompactTokens   int                       `json:"post_compact_tokens"`
	TargetTokens        int                       `json:"target_tokens"`
	Summary             string                    `json:"summary,omitempty"`
	ConsecutiveFailures int                       `json:"consecutive_failures"`
	RuntimeMetadata     *types.CompactionMetadata `json:"runtime_metadata,omitempty"`
}

CompactionResult is the canonical compaction outcome.

type CompactionStrategy

type CompactionStrategy interface {
	// AutoCompact compacts messages if the token budget is exceeded.
	// It respects the circuit breaker via TrackingState.ConsecutiveFailures.
	// When no compaction is needed it returns the original messages unchanged
	// with DidCompact == false.
	AutoCompact(
		ctx context.Context,
		systemPrompt string,
		messages []types.Message,
		model types.ModelIdentifier,
		sessionID types.SessionID,
		turnID types.TurnID,
		tracking *TrackingState,
	) (CompactionResult, error)
}

CompactionStrategy abstracts the compaction algorithm used by the query loop. Callers pass the current conversation state; the strategy decides whether and how to compact, returning a canonical CompactionResult.

The Engine type is the default implementation. Swap the strategy in tests or to add new algorithms (e.g., importance-weighted, semantic chunking) without touching the loop.

type Config

type Config struct {
	AutoCompactThreshold      float64               `json:"auto_compact_threshold"`
	CompactTargetPercentage   float64               `json:"compact_target_percentage"`
	MaxSummaryTokens          int                   `json:"max_summary_tokens"`
	AutoCompactBufferTokens   int                   `json:"auto_compact_buffer_tokens"`
	ManualCompactBufferTokens int                   `json:"manual_compact_buffer_tokens"`
	MaxConsecutiveFailures    int                   `json:"max_consecutive_failures"`
	SummaryModel              types.ModelIdentifier `json:"summary_model"`
}

Config represents compaction configuration.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default compaction configuration.

type Engine

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

Engine performs runtime-driven conversation compaction.

func NewEngine

func NewEngine(apiClient *providers.Client, config *Config) *Engine

NewEngine creates a new compaction engine.

func (*Engine) AutoCompact

func (e *Engine) AutoCompact(
	ctx context.Context,
	systemPrompt string,
	messages []types.Message,
	model types.ModelIdentifier,
	sessionID types.SessionID,
	turnID types.TurnID,
	tracking *TrackingState,
) (CompactionResult, error)

AutoCompact performs compaction if needed and respects the circuit breaker.

func (*Engine) AutoCompactThresholdTokens

func (e *Engine) AutoCompactThresholdTokens(model types.ModelIdentifier) int

AutoCompactThresholdTokens returns the absolute token threshold for auto-compaction.

func (*Engine) BlockingLimitTokens

func (e *Engine) BlockingLimitTokens(model types.ModelIdentifier) int

BlockingLimitTokens returns the hard blocking limit for the current model.

func (*Engine) CalculateCompactionImpact

func (e *Engine) CalculateCompactionImpact(messages []types.Message) CompactionImpact

CalculateCompactionImpact calculates the impact of compaction.

func (*Engine) CalculateUsage

func (e *Engine) CalculateUsage(systemPrompt string, messages []types.Message) int

CalculateUsage calculates total token usage for the current request.

func (*Engine) Compact

func (e *Engine) Compact(
	ctx context.Context,
	systemPrompt string,
	messages []types.Message,
	model types.ModelIdentifier,
	sessionID types.SessionID,
	turnID types.TurnID,
) (CompactionResult, error)

Compact performs compaction and returns a canonical result.

func (*Engine) EffectiveContextWindow

func (e *Engine) EffectiveContextWindow(model types.ModelIdentifier) int

EffectiveContextWindow returns the effective context window after reserving summary output budget.

func (*Engine) ShouldCompact

func (e *Engine) ShouldCompact(systemPrompt string, messages []types.Message, model types.ModelIdentifier) bool

ShouldCompact returns true if compaction should be attempted.

func (*Engine) TargetTokens

func (e *Engine) TargetTokens(model types.ModelIdentifier) int

TargetTokens returns the post-compaction target token count.

type MessagePreparer

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

MessagePreparer prepares messages for compaction

func NewMessagePreparer

func NewMessagePreparer() *MessagePreparer

NewMessagePreparer creates a new message preparer

func (*MessagePreparer) CalculateMessageTokens

func (p *MessagePreparer) CalculateMessageTokens(msg types.Message) int

CalculateMessageTokens estimates tokens for a message

func (*MessagePreparer) CalculateMessagesTokens

func (p *MessagePreparer) CalculateMessagesTokens(messages []types.Message) int

CalculateMessagesTokens calculates total tokens for messages

func (*MessagePreparer) CreateToolUseSummary

func (p *MessagePreparer) CreateToolUseSummary(toolUses []types.ToolUseContent) string

CreateToolUseSummary creates a summary of tool uses

func (*MessagePreparer) CreateTurnSummary

func (p *MessagePreparer) CreateTurnSummary(
	turnNumber int,
	userMessage types.Message,
	assistantMessage types.Message,
	toolResults []types.ToolResultContent,
) string

CreateTurnSummary creates a summary of a turn

func (*MessagePreparer) GetCompactionTarget

func (p *MessagePreparer) GetCompactionTarget(
	messages []types.Message,
	contextWindow types.ContextWindow,
	targetPercentage float64,
) int

GetCompactionTarget calculates how many tokens need to be removed

func (*MessagePreparer) PrepareMessageForAPI

func (p *MessagePreparer) PrepareMessageForAPI(msg types.Message) types.Message

PrepareMessageForAPI prepares a single message for the API

func (*MessagePreparer) PrepareMessages

func (p *MessagePreparer) PrepareMessages(
	messages []types.Message,
	maxTurns int,
) []types.Message

PrepareMessages prepares messages for the API request

func (*MessagePreparer) PrepareToolResult

func (p *MessagePreparer) PrepareToolResult(result types.ToolResultContent) types.ToolResultContent

PrepareToolResult prepares a tool result for storage

func (*MessagePreparer) PrepareToolUse

func (p *MessagePreparer) PrepareToolUse(toolUse types.ToolUseContent) types.ToolUseContent

PrepareToolUse prepares a tool use for execution

func (*MessagePreparer) ShouldCompact

func (p *MessagePreparer) ShouldCompact(
	messages []types.Message,
	contextWindow types.ContextWindow,
	threshold float64,
) bool

ShouldCompact returns true if messages should be compacted

func (*MessagePreparer) SummarizeToolUses

func (p *MessagePreparer) SummarizeToolUses(msg types.Message) string

SummarizeToolUses summarizes tool uses in a message

type MicroCompactor

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

MicroCompactor performs micro-compaction of tool results

func NewMicroCompactor

func NewMicroCompactor() *MicroCompactor

NewMicroCompactor creates a new micro compactor

func (*MicroCompactor) CalculateToolResultSummary

func (m *MicroCompactor) CalculateToolResultSummary(result types.ToolResultContent) string

CalculateToolResultSummary generates a summary of a tool result

func (*MicroCompactor) CalculateTrimSize

func (m *MicroCompactor) CalculateTrimSize(content string) int

CalculateTrimSize calculates how much would be trimmed

func (*MicroCompactor) EstimateToolResultTokens

func (m *MicroCompactor) EstimateToolResultTokens(result string) int

EstimateToolResultTokens estimates tokens in a tool result

func (*MicroCompactor) SetMaxToolResultSize

func (m *MicroCompactor) SetMaxToolResultSize(size int)

SetMaxToolResultSize sets the maximum tool result size

func (*MicroCompactor) SetTrimStrategy

func (m *MicroCompactor) SetTrimStrategy(strategy TrimStrategy)

SetTrimStrategy sets the trim strategy

func (*MicroCompactor) ShouldTrim

func (m *MicroCompactor) ShouldTrim(content string) bool

ShouldTrim returns true if content should be trimmed

func (*MicroCompactor) TrimContentWithMeta

func (m *MicroCompactor) TrimContentWithMeta(content string) TrimmedContent

TrimContentWithMeta trims content and returns metadata

func (*MicroCompactor) TrimToolResult

func (m *MicroCompactor) TrimToolResult(result types.ToolResultContent) types.ToolResultContent

TrimToolResult trims a tool result if it's too large

func (*MicroCompactor) TrimToolResults

func (m *MicroCompactor) TrimToolResults(results []types.ToolResultContent) []types.ToolResultContent

TrimToolResults trims multiple tool results

type TrackingState

type TrackingState struct {
	ConsecutiveFailures int `json:"consecutive_failures"`
}

TrackingState carries circuit-breaker state across loop iterations.

type TrimStrategy

type TrimStrategy string

TrimStrategy represents how to trim tool results

const (
	TrimStrategyTruncate TrimStrategy = "truncate"
	TrimStrategySnip     TrimStrategy = "snip"
	TrimStrategyPreview  TrimStrategy = "preview"
)

type TrimmedContent

type TrimmedContent struct {
	// Content is the trimmed content
	Content string `json:"content"`

	// OriginalSize is the original size
	OriginalSize int `json:"original_size"`

	// TrimmedSize is the trimmed size
	TrimmedSize int `json:"trimmed_size"`

	// Strategy is the trim strategy used
	Strategy TrimStrategy `json:"strategy"`

	// Preview is a preview of the original content
	Preview string `json:"preview,omitempty"`
}

TrimmedContent represents trimmed content with metadata

Jump to

Keyboard shortcuts

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