models

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 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

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidDataType = errors.New("not a valid DataType")

Functions

This section is empty.

Types

type APIFormat

type APIFormat string

APIFormat identifies the wire format of an LLM API endpoint. It drives request text extraction, full-response demasking and SSE processor selection; the raw request path is resolved to a format exactly once in the request-headers phase.

const (
	// APIFormatChatCompletions is the OpenAI /v1/chat/completions format.
	APIFormatChatCompletions APIFormat = "chat_completions"
	// APIFormatMessages is the Anthropic /v1/messages format.
	APIFormatMessages APIFormat = "messages"
	// APIFormatResponses is the OpenAI /v1/responses format.
	APIFormatResponses APIFormat = "responses"
)

func ParseAPIFormat

func ParseAPIFormat(s string) (APIFormat, error)

ParseAPIFormat parses a format name case-insensitively.

type AuditRecord

type AuditRecord struct {
	RequestID          string             `json:"request_id"`
	Timestamp          time.Time          `json:"timestamp"` // UTC, set by the recorder
	Model              string             `json:"model,omitempty"`
	Path               string             `json:"path"`
	TriggeredRuleIDs   []string           `json:"triggered_rule_ids"`
	TriggeredDataTypes []DataType         `json:"triggered_data_types"`
	Replacements       []AuditReplacement `json:"replacements"`
	// Mode records whether masking was applied ("enforce") or only
	// detected ("detect"); empty in records written before the field existed.
	Mode string `json:"mode,omitempty"`
	// MaskedTexts holds the post-masking (placeholder-substituted) request
	// text fields. Populated only when GUARDRAILS_AUDIT_STORE_MASKED_TEXTS
	// is enabled; still user prompt content — see the operator checklist.
	MaskedTexts []string `json:"masked_texts,omitempty"`
	// MaskedResponseTexts holds the post-masking (placeholder-substituted)
	// model response text fields — the response counterpart of MaskedTexts.
	// Populated only when GUARDRAILS_AUDIT_STORE_MASKED_RESPONSE_TEXTS is
	// enabled, and set by a best-effort response-phase update (see
	// audit.Recorder.RecordResponse). Placeholders in place, no originals.
	MaskedResponseTexts []string `json:"masked_response_texts,omitempty"`
}

AuditRecord is one per-request masking audit entry. JSON tags define the wire format used by external stores and the HTTP API and must stay stable across versions.

type AuditReplacement

type AuditReplacement struct {
	RuleID      string   `json:"rule_id"`
	DataType    DataType `json:"data_type"` // resolved from the rule at write time; UNSPECIFIED if the rule is gone
	Placeholder string   `json:"placeholder"`
	// Original is the raw pre-masking value behind Placeholder. It is populated
	// ONLY when GUARDRAILS_AUDIT_STORE_ORIGINAL_TEXTS is "plain" or "encrypted"
	// (default "off" leaves it empty and unserialized). In "encrypted" mode the
	// stored value is a statecodec AES-256-GCM envelope, decrypted by the audit
	// read path before it leaves the API. SECURITY: this is raw sensitive data —
	// never log it; keep the store access-controlled.
	Original string `json:"original,omitempty"`
}

AuditReplacement is the audit-safe projection of a Replacement. By default it carries no original value — audit records must not leak raw sensitive values. The Original field stays empty unless the operator explicitly opts in via GUARDRAILS_AUDIT_STORE_ORIGINAL_TEXTS (off by default); see Original.

type DataType

type DataType uint32

DataType is a user-facing toggle category of detection rules. CUSTOM is reserved for rules created via the configuration API that do not fit a built-in category.

ENUM(

UNSPECIFIED=0
CREDENTIALS=1
API_KEYS=2
ACCESS_TOKENS=3
IP_ADDRESSES=4
PERSONAL_DATA=5
CUSTOM=6

)

const (
	// DataTypeUNSPECIFIED is a DataType of type UNSPECIFIED.
	DataTypeUNSPECIFIED DataType = iota
	// DataTypeCREDENTIALS is a DataType of type CREDENTIALS.
	DataTypeCREDENTIALS
	// DataTypeAPIKEYS is a DataType of type API_KEYS.
	DataTypeAPIKEYS
	// DataTypeACCESSTOKENS is a DataType of type ACCESS_TOKENS.
	DataTypeACCESSTOKENS
	// DataTypeIPADDRESSES is a DataType of type IP_ADDRESSES.
	DataTypeIPADDRESSES
	// DataTypePERSONALDATA is a DataType of type PERSONAL_DATA.
	DataTypePERSONALDATA
	// DataTypeCUSTOM is a DataType of type CUSTOM.
	DataTypeCUSTOM
)

func ParseDataType

func ParseDataType(name string) (DataType, error)

ParseDataType attempts to convert a string to a DataType.

func (DataType) IsValid

func (x DataType) IsValid() bool

IsValid provides a quick way to determine if the typed value is part of the allowed enumerated values

func (DataType) String

func (x DataType) String() string

String implements the Stringer interface.

type EffectiveSettings

type EffectiveSettings struct {
	Enabled   bool
	DataTypes []DataType
	Mode      GuardrailsMode
}

EffectiveSettings is the per-request resolution of the global settings combined with the optional narrow-only header override.

type GuardrailsMode

type GuardrailsMode string

GuardrailsMode selects what the service does when sensitive data is found.

const (
	// ModeEnforce masks sensitive values in request bodies and demasks
	// responses. The default.
	ModeEnforce GuardrailsMode = "enforce"
	// ModeDetect (shadow mode) scans and records metrics/audit but never
	// mutates traffic. Used to evaluate what would be masked before
	// enabling enforcement.
	ModeDetect GuardrailsMode = "detect"
)

func ParseGuardrailsMode

func ParseGuardrailsMode(s string) (GuardrailsMode, error)

ParseGuardrailsMode parses a mode value case-insensitively. The empty string resolves to ModeEnforce: settings persisted before the mode field existed unmarshal to "", and missing configuration must fail toward the more protective behavior.

func (GuardrailsMode) IsValid

func (m GuardrailsMode) IsValid() bool

IsValid reports whether m is a known mode value.

type GuardrailsSettings

type GuardrailsSettings struct {
	Enabled   bool           `json:"enabled"`
	DataTypes []DataType     `json:"data_types"`
	Mode      GuardrailsMode `json:"mode,omitempty"`
}

GuardrailsSettings is the global (instance-wide) guardrails configuration. It replaces the per-(project, model) settings of the managed deployment: the OSS service applies one policy to all traffic, optionally narrowed per request via a trusted gateway header.

type MaskingState

type MaskingState struct {
	TriggeredRuleIDs   []string      `json:"triggered_rule_ids"`
	TriggeredDataTypes []DataType    `json:"triggered_data_types"`
	Replacements       []Replacement `json:"replacements"`
	// Format is the API wire format resolved in the request phase. It is
	// persisted so a replica that only runs the response phase (cross-replica
	// store fallback) can select the correct demask/SSE processor instead of
	// defaulting to chat-completions and leaking placeholders.
	Format APIFormat `json:"format,omitempty"`
}

MaskingState is the state of the masking process for a request. JSON tags define the wire format used by external masking-state stores (redis/postgres) and must stay stable across versions.

func (*MaskingState) IsEmpty

func (s *MaskingState) IsEmpty() bool

IsEmpty reports whether any masking occurred. Format is metadata only and does not, by itself, make the state non-empty.

type Metadata

type Metadata struct {
	// RequestID is the x-request-id header value (or a generated fallback);
	// it is the audit-record identity and tags log lines. It is NOT used as
	// the masking-state store key — see StateKey.
	RequestID string
	// StateKey keys the masking state in external stores. It is derived from
	// RequestID (HMAC-SHA256(salt, request_id)) so a client-supplied or
	// predictable x-request-id cannot be used to guess or collide with
	// another request's stored state.
	StateKey    string
	Model       string
	IsStreaming bool
	Path        string
	// Mode is the guardrails mode this request was processed under
	// (resolved once in the request-headers phase).
	Mode GuardrailsMode
	// Format is the API wire format resolved from Path in the
	// request-headers phase; it drives extraction and demask dispatch.
	Format APIFormat
}

Metadata carries per-request context extracted from Envoy headers.

type PathResolver

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

PathResolver maps request paths to API formats. Matching is exact first, then longest-suffix: a configured key `/v1/messages` also matches `/openai/v1/messages` (keys start with `/`, so suffix matches are segment-anchored), letting proxy-prefixed mounts work with zero extra configuration. Query strings are stripped before matching.

func NewPathResolver

func NewPathResolver(paths map[string]string) (*PathResolver, error)

NewPathResolver validates a path→format map (e.g. from GUARDRAILS_PATHS) and builds a resolver. Errors are configuration mistakes and must fail the boot: an empty or invalid map would silently reject all traffic.

func (*PathResolver) Resolve

func (r *PathResolver) Resolve(rawPath string) (APIFormat, bool)

Resolve maps a raw :path header value to its API format. The second return is false when no configured path matches.

type Replacement

type Replacement struct {
	RuleID      string `json:"rule_id"`
	Original    string `json:"original"`
	Placeholder string `json:"placeholder"` // placeholder in <NAME_N> format
}

Replacement is one unique original->synthetic mapping produced by masking.

Jump to

Keyboard shortcuts

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