optimizer

package
v1.3.7 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package optimizer shrinks what a session sends to the model just before each call: prompts by profile (coding keeps code byte-for-byte, general compresses prose) and noisy tool output by head/tail windowing. It works on copies, so stored history keeps everything (.agents/specs/008).

Index

Constants

View Source
const (
	// ModeGeneral compresses prose: filler, whitespace, JSON (OPT-05).
	ModeGeneral = "general"
	// ModeCoding only trims what cannot change meaning (OPT-04).
	ModeCoding = "coding"
)

Profiles a session's optimizer runs (opt:mode).

View Source
const (
	KeyEnabled       = "opt:enabled"
	KeyMode          = "opt:mode"
	KeyMaxToolTokens = "opt:max_tool_tokens"
	KeyStripFiller   = "opt:strip_filler"
)

The settings' names, as status output and the specification spell them.

View Source
const DefaultMaxToolTokens = 2000

DefaultMaxToolTokens is the estimated-token budget of one tool output value (about 8000 characters).

Variables

This section is empty.

Functions

func OptimizeCoding

func OptimizeCoding(text string, filler bool) string

OptimizeCoding is text with only what cannot change meaning trimmed (OPT-04): trailing spaces and tabs on prose lines and, with filler, pleasantries (see stripFiller). Fences, tag blocks, inline code, trace and path lines stay byte-for-byte.

func OptimizeGeneral

func OptimizeGeneral(text string, filler bool) string

OptimizeGeneral is text with its prose compressed (OPT-05): runs of whitespace become one space, prose that is JSON is minified and, with filler, pleasantries go (see stripFiller). Code and other protected pieces stay byte-for-byte, and each block still starts a line.

func Prunable

func Prunable(name string) bool

Prunable reports whether the built-in tool name's output may be pruned. The engine adds MCP tools on top.

func PruneText

func PruneText(s string, maxChars int) string

PruneText is tool output s with its noise removed (OPT-06): terminal escapes and carriage-return redraws, runs of blank lines, repeated lines, and then all but the first 30% and last 70% of maxChars.

func PruneToolOutput

func PruneToolOutput(result map[string]any, maxChars int) (map[string]any, int)

PruneToolOutput is result with each top-level string value pruned to maxChars, and how many characters that saved. result itself is never changed: a copy is returned when anything was pruned.

func ValidMode

func ValidMode(m string) bool

ValidMode reports whether m names a profile.

Types

type Settings

type Settings struct {
	// Enabled is the master switch (opt:enabled).
	Enabled bool `json:"enabled,omitempty"`
	// Mode is ModeGeneral or ModeCoding (opt:mode).
	Mode string `json:"mode,omitempty"`
	// MaxToolTokens bounds one tool output value (opt:max_tool_tokens).
	MaxToolTokens int `json:"max_tool_tokens,omitempty"`
	// StripFiller, when set, overrides the default of stripping leading
	// pleasantries (opt:strip_filler).
	StripFiller *bool `json:"strip_filler,omitempty"`
}

Settings are a session's optimizer settings (OPT-01). The zero value is off; Resolve fills in the defaults.

func (Settings) Filler

func (s Settings) Filler() bool

Filler reports whether leading filler is stripped (default true).

func (Settings) MaxToolChars

func (s Settings) MaxToolChars() int

MaxToolChars is the tool output budget in characters (four per token).

func (Settings) Resolve

func (s Settings) Resolve() Settings

Resolve is s with defaults in place of unset or invalid values.

type Stats

type Stats struct {
	// PromptChars were saved from user prompts.
	PromptChars int
	// ToolChars were saved from tool results.
	ToolChars int
}

Stats are the characters one Apply saved.

func Apply

func Apply(contents []*genai.Content, s Settings, prunable func(name string) bool) (out []*genai.Content, st Stats, err error)

Apply is contents optimized by s (OPT-02..07): user prompt text by s's profile, and the results of tools prunable reports true for by PruneToolOutput. Nothing in contents is changed; changed messages and parts are copies, so the session's stored events keep everything. With s off, contents is returned as is. A panic is recovered: contents comes back unchanged with the error, to be logged and sent as it was.

func (Stats) Total

func (s Stats) Total() int

Total is all the characters saved.

Jump to

Keyboard shortcuts

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