compress

package
v0.4.9 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EstimateTokens

func EstimateTokens(text string) int

EstimateTokens estimates token count using a multi-strategy approach. Uses cl100k_base heuristic for better accuracy across languages.

Types

type CompressionResult

type CompressionResult struct {
	MessagesCompressed int
	SummaryLength      int
	TokensSaved        int
}

CompressionResult contains compression result information

type CompressionStats

type CompressionStats struct {
	TotalSessions    int     `json:"total_sessions"`
	CompressedCount  int     `json:"compressed_count"`
	TotalTokensSaved int     `json:"total_tokens_saved"`
	AvgCompression   float64 `json:"avg_compression_ratio"`
}

CompressionStats provides compression statistics

type CompressionSummary

type CompressionSummary struct {
	OriginalCount   int    `json:"original_count"`
	CompressedCount int    `json:"compressed_count"`
	TokensSaved     int    `json:"tokens_saved"`
	Summary         string `json:"summary"`
	CompressedAt    int64  `json:"compressed_at"`
}

CompressionSummary is a summary of compressed messages

type Config

type Config struct {
	Enabled        bool `yaml:"enabled"`
	MaxTokens      int  `yaml:"max_tokens"`      // Max tokens before compression
	TargetTokens   int  `yaml:"target_tokens"`   // Target tokens after compression
	MinMessages    int  `yaml:"min_messages"`    // Min messages to keep in summary
	PreserveRecent int  `yaml:"preserve_recent"` // Keep recent N messages
}

Config holds compression configuration

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default compression config

type LLMProvider

type LLMProvider interface {
	GenerateSummary(ctx context.Context, prompt string) (string, error)
}

LLMProvider interface for LLM-based summarization

type Manager

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

Manager handles session compression

func NewManager

func NewManager(dataDir string) *Manager

NewManager creates a new compression manager

func (*Manager) AutoCompress

func (m *Manager) AutoCompress(sessionID string, messages []Message) ([]Message, bool, error)

AutoCompress automatically compresses if over limit

func (*Manager) Compress

func (m *Manager) Compress(level string) (*CompressionResult, error)

Compress applies compression to active session

func (*Manager) CompressSession

func (m *Manager) CompressSession(sessionID string, messages []Message, preserveRecent int) (*CompressionSummary, []Message, error)

CompressSession compresses a session to fit within token limit

func (*Manager) CompressWithLLM

func (m *Manager) CompressWithLLM(ctx context.Context, messages []Message, llmProvider LLMProvider) (*CompressionSummary, []Message, error)

CompressWithLLM uses an LLM to generate a better summary

func (*Manager) GetSession

func (m *Manager) GetSession(sessionID string) *Session

GetSession returns a stored session

func (*Manager) GetStats

func (m *Manager) GetStats() *CompressionStats

GetStats returns compression statistics

func (*Manager) LoadSession

func (m *Manager) LoadSession(sessionID string) error

LoadSession loads a session from disk

func (*Manager) Preview

func (m *Manager) Preview(level string) (*PreviewResult, error)

Preview shows compression preview without applying changes

func (*Manager) SaveSession

func (m *Manager) SaveSession(session *Session) error

SaveSession saves a session to disk

type Message

type Message struct {
	Role      string `json:"role"`
	Content   string `json:"content"`
	Timestamp int64  `json:"timestamp"`
	Tokens    int    `json:"tokens,omitempty"`
}

Message represents a chat message

type PreviewResult

type PreviewResult struct {
	CurrentMessages int
	AfterMessages   int
	TokensSaved     int
	TokenReduction  float64
	CostSavings     float64
}

PreviewResult contains compression preview information

type Session

type Session struct {
	ID       string              `json:"id"`
	Messages []Message           `json:"messages"`
	Summary  *CompressionSummary `json:"compression_summary,omitempty"`
}

Session represents a chat session

Jump to

Keyboard shortcuts

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