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
- func OptimizeCoding(text string, filler bool) string
- func OptimizeGeneral(text string, filler bool) string
- func Prunable(name string) bool
- func PruneText(s string, maxChars int) string
- func PruneToolOutput(result map[string]any, maxChars int) (map[string]any, int)
- func ValidMode(m string) bool
- type Settings
- type Stats
Constants ¶
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).
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.
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 ¶
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 ¶
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 ¶
Prunable reports whether the built-in tool name's output may be pruned. The engine adds MCP tools on top.
func PruneText ¶
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 ¶
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.
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) MaxToolChars ¶
MaxToolChars is the tool output budget in characters (four per token).
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.