promptcompression

package
v0.25.2 Latest Latest
Warning

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

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

Documentation

Overview

Package promptcompression shrinks the request prompt before it reaches the model: standalone and fenced JSON is minified, ANSI escape sequences are stripped, and redundant whitespace is collapsed. All transforms are deterministic, so repeated turns re-compress history to identical bytes and provider prompt-cache prefixes remain stable. The plugin always fails open: any decode, transform, or encode problem passes the original request through untouched.

Index

Constants

View Source
const PluginName = "prompt_compression"

Variables

View Source
var (
	ErrNoTransforms  = errors.New("prompt_compression: at least one transform must be enabled")
	ErrNegativeMin   = errors.New("prompt_compression: min_length must not be negative")
	ErrEmptyRole     = errors.New("prompt_compression: target_roles contains an empty role")
	ErrBadBlankLines = errors.New("prompt_compression: max_consecutive_blank_lines must be between 1 and 1000")
	ErrBadMaxBody    = errors.New("prompt_compression: max_body_bytes must not be negative")
)

Functions

This section is empty.

Types

type Data

type Data struct {
	Stage      string  `json:"stage,omitempty"`
	Mode       string  `json:"mode,omitempty"`
	Decision   string  `json:"decision,omitempty"`
	Transforms string  `json:"transforms,omitempty"`
	BytesIn    int     `json:"bytes_in,omitempty"`
	BytesOut   int     `json:"bytes_out,omitempty"`
	BytesSaved int     `json:"bytes_saved,omitempty"`
	Ratio      float64 `json:"ratio,omitempty"`
}

Data is the per-invocation telemetry payload recorded on the plugin span.

type Plugin

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

func New

func New(registry *adapter.Registry, logger *slog.Logger) *Plugin

func (*Plugin) Execute

func (p *Plugin) Execute(ctx context.Context, in appplugins.ExecInput) (*appplugins.Result, error)

func (*Plugin) MandatoryStages

func (p *Plugin) MandatoryStages() []policy.Stage

func (*Plugin) MutatesMetadata

func (p *Plugin) MutatesMetadata() bool

func (*Plugin) MutatesRequestBody

func (p *Plugin) MutatesRequestBody() bool

func (*Plugin) MutatesResponseBody

func (p *Plugin) MutatesResponseBody() bool

func (*Plugin) Name

func (p *Plugin) Name() string

func (*Plugin) SupportedModes

func (p *Plugin) SupportedModes() []policy.Mode

func (*Plugin) SupportedProtocols

func (p *Plugin) SupportedProtocols() []appplugins.Protocol

func (*Plugin) SupportedStages

func (p *Plugin) SupportedStages() []policy.Stage

func (*Plugin) ValidateConfig

func (p *Plugin) ValidateConfig(settings map[string]any) error

type Settings

type Settings struct {
	// CompressJSON minifies standalone JSON message content, fenced “`json
	// blocks, and tool-call arguments (whitespace-only, fully lossless).
	// Defaults to true.
	CompressJSON bool
	// NormalizeWhitespace trims trailing spaces per line (preserving Markdown
	// two-space hard line breaks) and collapses long runs of blank lines.
	// Defaults to true.
	NormalizeWhitespace bool
	// StripANSI removes ANSI escape (color/cursor) sequences, common in captured
	// terminal and CI logs. Defaults to true.
	StripANSI bool
	// MaxConsecutiveBlankLines caps runs of blank lines when NormalizeWhitespace
	// is on. Defaults to 1; values outside [1, 1000] are rejected.
	MaxConsecutiveBlankLines int
	// MinLength skips messages whose content is shorter than this many bytes, so
	// tiny stable messages are never rewritten (avoids cache churn for no
	// gain). Defaults to 256; an explicit 0 compresses everything.
	MinLength int
	// MaxBodyBytes skips the whole pipeline for request bodies larger than this
	// many bytes, bounding per-request CPU cost. Defaults to 1 MiB; an explicit
	// 0 disables the cap.
	MaxBodyBytes int
	// TargetRoles restricts compression to messages with these roles (e.g.
	// "tool", "user"). Empty means all roles, including the system prompt.
	TargetRoles []string
	// contains filtered or unexported fields
}

Settings is the resolved configuration for the prompt compression plugin. Every transform is lossless or near-lossless and deterministic, so the same input always compresses to the same bytes — which keeps provider prompt-cache prefixes stable across turns.

Jump to

Keyboard shortcuts

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