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 ¶
- type AnonymizerTransform
- type InputTransform
- type NormalizerTransform
- type OutputAnonymizerTransform
- type OutputTransform
- type Phase
- type RAGChunk
- type RAGProvider
- type RAGTransform
- type Registry
- func (r *Registry) ApplyInput(ctx context.Context, req *provider.CompletionRequest) error
- func (r *Registry) ApplyOutput(ctx context.Context, req *provider.CompletionRequest, ...) error
- func (r *Registry) Count() int
- func (r *Registry) InputTransforms() []InputTransform
- func (r *Registry) OutputTransforms() []OutputTransform
- func (r *Registry) Register(t Transform)
- func (r *Registry) StreamingOutputs() []StreamingOutputTransform
- type StreamingOutputTransform
- type SystemPromptTransform
- type Transform
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 ¶
func (a *AnonymizerTransform) TransformInput(_ context.Context, req *provider.CompletionRequest) error
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 ¶
func (t *NormalizerTransform) TransformOutput(_ context.Context, _ *provider.CompletionRequest, resp *provider.CompletionResponse) error
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 (t *OutputAnonymizerTransform) Name() string
func (*OutputAnonymizerTransform) Phase ¶
func (t *OutputAnonymizerTransform) Phase() Phase
func (*OutputAnonymizerTransform) TransformOutput ¶
func (t *OutputAnonymizerTransform) TransformOutput(_ context.Context, _ *provider.CompletionRequest, resp *provider.CompletionResponse) error
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 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 (*Registry) ApplyInput ¶
ApplyInput runs all input transforms in order.
func (*Registry) ApplyOutput ¶
func (r *Registry) ApplyOutput(ctx context.Context, req *provider.CompletionRequest, resp *provider.CompletionResponse) error
ApplyOutput runs all output transforms in order.
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 ¶
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 (t *SystemPromptTransform) TransformInput(ctx context.Context, req *provider.CompletionRequest) error
func (*SystemPromptTransform) WithTenantPrompt ¶
func (t *SystemPromptTransform) WithTenantPrompt(tenantID, prompt string) *SystemPromptTransform
WithTenantPrompt adds a tenant-specific system prompt.