transform

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package transform provides request/response transformation middleware for Nexus. Transforms modify requests before they reach providers and responses after they come back — system prompt injection, RAG context, output normalization, data anonymization, etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnonymizerTransform

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

AnonymizerTransform redacts sensitive data from requests and responses.

func NewAnonymizer

func NewAnonymizer() *AnonymizerTransform

NewAnonymizer creates a data anonymizer with common PII patterns.

func (*AnonymizerTransform) AddPattern

func (a *AnonymizerTransform) AddPattern(name, pattern, replacement string)

AddPattern adds a named regex pattern for anonymization.

func (*AnonymizerTransform) Name

func (a *AnonymizerTransform) Name() string

func (*AnonymizerTransform) Phase

func (a *AnonymizerTransform) Phase() Phase

func (*AnonymizerTransform) TransformInput

type InputTransform

type InputTransform interface {
	Transform
	// TransformInput modifies the request in place.
	TransformInput(ctx context.Context, req *provider.CompletionRequest) error
}

InputTransform modifies a request before it reaches a provider.

type NormalizerTransform

type NormalizerTransform struct {
	// TrimWhitespace trims leading/trailing whitespace from content.
	TrimWhitespace bool

	// StripProviderMeta removes provider-specific metadata.
	StripProviderMeta bool

	// NormalizeFinishReason maps provider-specific finish reasons to standard ones.
	NormalizeFinishReason bool
}

NormalizerTransform standardizes output format across providers.

func NewNormalizer

func NewNormalizer() *NormalizerTransform

NewNormalizer creates an output normalizer transform.

func (*NormalizerTransform) Name

func (t *NormalizerTransform) Name() string

func (*NormalizerTransform) Phase

func (t *NormalizerTransform) Phase() Phase

func (*NormalizerTransform) TransformOutput

type OutputAnonymizerTransform

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

OutputAnonymizerTransform redacts PII from provider responses.

func NewAnonymizerOutput

func NewAnonymizerOutput() *OutputAnonymizerTransform

NewAnonymizerOutput creates an output anonymizer that redacts PII from responses.

func (*OutputAnonymizerTransform) AddPattern

func (t *OutputAnonymizerTransform) AddPattern(name, pattern, replacement string)

AddPattern delegates to the base anonymizer.

func (*OutputAnonymizerTransform) Name

func (*OutputAnonymizerTransform) Phase

func (t *OutputAnonymizerTransform) Phase() Phase

func (*OutputAnonymizerTransform) TransformOutput

type OutputTransform

type OutputTransform interface {
	Transform
	// TransformOutput modifies the response in place.
	TransformOutput(ctx context.Context, req *provider.CompletionRequest, resp *provider.CompletionResponse) error
}

OutputTransform modifies a response after it comes back from a provider.

type Phase

type Phase string

Phase indicates when a transform runs.

const (
	// PhaseInput transforms run before the provider call.
	PhaseInput Phase = "input"

	// PhaseOutput transforms run after the provider call.
	PhaseOutput Phase = "output"
)

type RAGChunk

type RAGChunk struct {
	Content  string  // The text content
	Source   string  // Where it came from
	Score    float64 // Relevance score (0-1)
	Metadata map[string]string
}

RAGChunk is a piece of retrieved context.

type RAGProvider

type RAGProvider interface {
	// Retrieve returns relevant context chunks for the query.
	Retrieve(ctx context.Context, query string, maxResults int) ([]RAGChunk, error)
}

RAGProvider retrieves context for a query.

type RAGTransform

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

RAGTransform injects retrieved context into requests.

func NewRAG

func NewRAG(rag RAGProvider) *RAGTransform

NewRAG creates a RAG context injection transform.

func (*RAGTransform) Name

func (t *RAGTransform) Name() string

func (*RAGTransform) Phase

func (t *RAGTransform) Phase() Phase

func (*RAGTransform) TransformInput

func (t *RAGTransform) TransformInput(ctx context.Context, req *provider.CompletionRequest) error

func (*RAGTransform) WithMaxResults

func (t *RAGTransform) WithMaxResults(n int) *RAGTransform

WithMaxResults sets the maximum number of RAG results.

func (*RAGTransform) WithTemplate

func (t *RAGTransform) WithTemplate(tmpl string) *RAGTransform

WithTemplate sets a custom template for the RAG context injection. The template should contain a single %s placeholder for the context.

type Registry

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

Registry manages transforms.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new transform registry.

func (*Registry) ApplyInput

func (r *Registry) ApplyInput(ctx context.Context, req *provider.CompletionRequest) error

ApplyInput runs all input transforms in order.

func (*Registry) ApplyOutput

ApplyOutput runs all output transforms in order.

func (*Registry) Count

func (r *Registry) Count() int

Count returns total number of registered transforms.

func (*Registry) InputTransforms

func (r *Registry) InputTransforms() []InputTransform

InputTransforms returns registered input transforms.

func (*Registry) OutputTransforms

func (r *Registry) OutputTransforms() []OutputTransform

OutputTransforms returns registered output transforms.

func (*Registry) Register

func (r *Registry) Register(t Transform)

Register adds a transform. It is type-asserted into input/output and (optionally) streaming-output lists.

func (*Registry) StreamingOutputs

func (r *Registry) StreamingOutputs() []StreamingOutputTransform

StreamingOutputs returns registered streaming output transforms.

type StreamingOutputTransform

type StreamingOutputTransform interface {
	OutputTransform
	TransformChunk(ctx context.Context, req *provider.CompletionRequest, chunk *provider.StreamChunk) (*provider.StreamChunk, error)
	TransformAccumulated(ctx context.Context, req *provider.CompletionRequest, resp *provider.CompletionResponse) error
}

StreamingOutputTransform extends OutputTransform with per-chunk and post-stream hooks. The middleware applies streaming transforms only when resp.Stream is non-nil; non-streaming responses still go through TransformOutput on the merged response.

TransformChunk runs for every chunk that arrives. It may:

  • Return the chunk unchanged.
  • Return a modified copy.
  • Return nil to drop the chunk silently.
  • Return an error to abort the stream.

TransformAccumulated runs once on Close with the merged final response. Use it for post-hoc audit signal or final-state mutation.

type SystemPromptTransform

type SystemPromptTransform struct {
	// GlobalPrompt is prepended to all requests.
	GlobalPrompt string

	// TenantPrompts maps tenant IDs to tenant-specific system prompts.
	// If set, this replaces the global prompt for that tenant.
	TenantPrompts map[string]string

	// Append if true appends to existing system messages instead of prepending.
	Append bool
}

SystemPromptTransform prepends a system message to every request. Supports global prompts and per-tenant overrides.

func NewSystemPrompt

func NewSystemPrompt(prompt string) *SystemPromptTransform

NewSystemPrompt creates a system prompt transform.

func (*SystemPromptTransform) Name

func (t *SystemPromptTransform) Name() string

func (*SystemPromptTransform) Phase

func (t *SystemPromptTransform) Phase() Phase

func (*SystemPromptTransform) TransformInput

func (*SystemPromptTransform) WithTenantPrompt

func (t *SystemPromptTransform) WithTenantPrompt(tenantID, prompt string) *SystemPromptTransform

WithTenantPrompt adds a tenant-specific system prompt.

type Transform

type Transform interface {
	// Name returns a unique identifier for this transform.
	Name() string

	// Phase returns whether this runs on input, output, or both.
	Phase() Phase
}

Transform modifies requests or responses.

Jump to

Keyboard shortcuts

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