Documentation
¶
Overview ¶
Package middleware provides a flexible middleware pattern for message processing. Middleware can intercept, transform, validate, and augment messages as they flow through the orchestrator.
Index ¶
- type Chain
- type ContentFilterMiddlewareConfig
- type FilterFunc
- type MessageContext
- type Middleware
- func ContentFilterMiddleware(config ContentFilterMiddlewareConfig) Middleware
- func ContextEnrichmentMiddleware(enricher func(*MessageContext, *agent.Message)) Middleware
- func EmptyContentValidationMiddleware() Middleware
- func ErrorRecoveryMiddleware() Middleware
- func LoggingMiddleware() Middleware
- func MessageHistoryMiddleware(maxHistory int) Middleware
- func MetricsMiddleware() Middleware
- func NewFilterMiddleware(name string, filter FilterFunc) Middleware
- func NewMiddlewareFunc(name string, ...) Middleware
- func NewTransformMiddleware(name string, transform TransformFunc) Middleware
- func NewValidationMiddleware(name string, validate ValidationFunc) Middleware
- func RateLimitMiddleware(config RateLimitConfig) Middleware
- func RoleValidationMiddleware(allowedRoles []string) Middleware
- func SanitizationMiddleware(removeSpecialChars bool) Middleware
- type MiddlewareFunc
- type ProcessFunc
- type RateLimitConfig
- type TransformFunc
- type ValidationFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain represents a chain of middleware.
func NewChain ¶
func NewChain(middleware ...Middleware) *Chain
NewChain creates a new middleware chain.
type ContentFilterMiddlewareConfig ¶
type ContentFilterMiddlewareConfig struct {
// MaxLength is the maximum allowed message length (0 = unlimited)
MaxLength int
// MinLength is the minimum required message length (0 = no minimum)
MinLength int
// BlockedWords is a list of words that are not allowed
BlockedWords []string
// RequiredWords is a list of words that must be present
RequiredWords []string
// CaseSensitive determines if word matching is case-sensitive
CaseSensitive bool
}
ContentFilterMiddlewareConfig configures content filtering.
type FilterFunc ¶
type FilterFunc func(ctx *MessageContext, msg *agent.Message) (bool, error)
FilterFunc is a simple filter function that can reject messages. Return true to allow the message, false to reject it.
type MessageContext ¶
type MessageContext struct {
// Ctx is the request context
Ctx context.Context
// AgentID is the ID of the agent processing the message
AgentID string
// AgentName is the name of the agent
AgentName string
// TurnNumber is the current turn in the conversation
TurnNumber int
// Metadata contains additional context information
Metadata map[string]interface{}
}
MessageContext contains contextual information for middleware processing.
type Middleware ¶
type Middleware interface {
// Process handles a message and optionally passes it to the next middleware.
// Returns the processed message and any error.
Process(ctx *MessageContext, msg *agent.Message, next ProcessFunc) (*agent.Message, error)
// Name returns the middleware name for logging and debugging.
Name() string
}
Middleware processes messages in a chain. It can modify the message, add metadata, or stop processing by returning an error.
func ContentFilterMiddleware ¶
func ContentFilterMiddleware(config ContentFilterMiddlewareConfig) Middleware
ContentFilterMiddleware creates middleware that filters messages based on content rules.
func ContextEnrichmentMiddleware ¶
func ContextEnrichmentMiddleware(enricher func(*MessageContext, *agent.Message)) Middleware
ContextEnrichmentMiddleware creates middleware that enriches the message context. It adds additional metadata fields to the context.
func EmptyContentValidationMiddleware ¶
func EmptyContentValidationMiddleware() Middleware
EmptyContentValidationMiddleware creates middleware that rejects empty messages.
func ErrorRecoveryMiddleware ¶
func ErrorRecoveryMiddleware() Middleware
ErrorRecoveryMiddleware creates middleware that recovers from panics. It catches panics in downstream middleware and converts them to errors.
func LoggingMiddleware ¶
func LoggingMiddleware() Middleware
LoggingMiddleware creates middleware that logs all messages. It logs before and after processing with structured fields.
func MessageHistoryMiddleware ¶
func MessageHistoryMiddleware(maxHistory int) Middleware
MessageHistoryMiddleware creates middleware that tracks message history. It stores recent messages in the context metadata for pattern analysis.
func MetricsMiddleware ¶
func MetricsMiddleware() Middleware
MetricsMiddleware creates middleware that tracks message processing metrics. It stores metrics in the message context metadata.
func NewFilterMiddleware ¶
func NewFilterMiddleware(name string, filter FilterFunc) Middleware
NewFilterMiddleware creates middleware from a filter function. If the filter returns false, processing stops with an error.
func NewMiddlewareFunc ¶
func NewMiddlewareFunc(name string, fn func(ctx *MessageContext, msg *agent.Message, next ProcessFunc) (*agent.Message, error)) Middleware
NewMiddlewareFunc creates a middleware from a function.
func NewTransformMiddleware ¶
func NewTransformMiddleware(name string, transform TransformFunc) Middleware
NewTransformMiddleware creates middleware from a transform function.
func NewValidationMiddleware ¶
func NewValidationMiddleware(name string, validate ValidationFunc) Middleware
NewValidationMiddleware creates middleware from a validation function.
func RateLimitMiddleware ¶
func RateLimitMiddleware(config RateLimitConfig) Middleware
RateLimitMiddleware creates middleware that enforces basic rate limiting. Note: This is a simple in-memory implementation. For production use, consider using the pkg/ratelimit package with persistent storage.
func RoleValidationMiddleware ¶
func RoleValidationMiddleware(allowedRoles []string) Middleware
RoleValidationMiddleware creates middleware that validates message roles. It ensures messages have valid roles from the allowed list.
func SanitizationMiddleware ¶
func SanitizationMiddleware(removeSpecialChars bool) Middleware
SanitizationMiddleware creates middleware that sanitizes message content. It trims whitespace and optionally removes special characters.
type MiddlewareFunc ¶
type MiddlewareFunc struct {
// contains filtered or unexported fields
}
MiddlewareFunc is a function adapter for the Middleware interface.
func (*MiddlewareFunc) Process ¶
func (m *MiddlewareFunc) Process(ctx *MessageContext, msg *agent.Message, next ProcessFunc) (*agent.Message, error)
Process implements Middleware.
type ProcessFunc ¶
ProcessFunc is a function that processes a message. It's used to chain middleware together.
type RateLimitConfig ¶
RateLimitMiddleware creates middleware that enforces rate limiting. It tracks message counts per agent and rejects if limits are exceeded.
type TransformFunc ¶
TransformFunc transforms a message.
type ValidationFunc ¶
type ValidationFunc func(ctx *MessageContext, msg *agent.Message) error
ValidationFunc validates a message. Return an error if the message is invalid.