rewrite

package
v1.20.1 Latest Latest
Warning

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

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

Documentation

Overview

Package rewrite performs keyword substitution on request payloads.

It is used to replace sensitive or brand-specific keywords in the inbound request body before forwarding to the upstream, helping avoid content security detection that keys off specific terms. Rules are provided as a JSON object mapping a source keyword to its replacement, e.g. `{"foo":"bar"}`. Matching is case-sensitive and multiple rules are applied in a single pass (longest source first) so overlapping keywords do not double-replace.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppliedRule

type AppliedRule struct {
	From  string
	To    string
	Count int
}

AppliedRule describes a rule that changed at least one textual field in a request. Count is the number of substitutions made by that rule.

type Replacer

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

Replacer holds compiled keyword rules. A nil *Replacer is a no-op for every method, so callers can invoke ApplyToRequest without nil checks.

func Parse

func Parse(raw string) (*Replacer, error)

Parse decodes a JSON object of keyword mappings into a Replacer. An empty input returns a nil Replacer (no-op). Keys and values must be non-empty strings; duplicate source keywords keep the first occurrence.

Matching is case-sensitive, so "Foo" and "foo" are distinct keywords. Configure both explicitly if both forms should be rewritten.

func (*Replacer) ApplyToRequest

func (r *Replacer) ApplyToRequest(req *ir.Request) []AppliedRule

ApplyToRequest rewrites the textual content of an IR request in place: the system prompt, message text/thinking parts and tool result payloads. Structural fields such as model names, tool names and tool call arguments are intentionally left untouched so routing and function calls stay intact. It returns the rules that made an actual change, in match order.

func (*Replacer) Replace

func (r *Replacer) Replace(s string) string

Replace applies all keyword rules to s in a single pass. It is safe to call on a nil receiver.

type Rule

type Rule struct {
	From string
	To   string
}

Rule is a single keyword substitution.

Source Files

  • rewrite.go

Jump to

Keyboard shortcuts

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