middleware

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PutMiddlewareRequest

func PutMiddlewareRequest(req *MiddlewareRequest)

PutMiddlewareRequest returns a MiddlewareRequest to the object pool

func PutMiddlewareResponse

func PutMiddlewareResponse(resp *MiddlewareResponse)

PutMiddlewareResponse returns a MiddlewareResponse to the object pool

Types

type AuthenticationMiddleware

type AuthenticationMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

AuthenticationMiddleware handles authentication

func NewAuthenticationMiddleware

func NewAuthenticationMiddleware(authFunc func(context.Context, *MiddlewareRequest) (bool, error)) *AuthenticationMiddleware

NewAuthenticationMiddleware creates an authentication middleware

func (*AuthenticationMiddleware) OnBefore

OnBefore checks authentication

type BaseMiddleware

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

BaseMiddleware provides a default implementation of Middleware

func NewBaseMiddleware

func NewBaseMiddleware(name string) *BaseMiddleware

NewBaseMiddleware creates a new BaseMiddleware

func (*BaseMiddleware) Name

func (m *BaseMiddleware) Name() string

Name returns the middleware name

func (*BaseMiddleware) OnAfter

OnAfter is a no-op by default

func (*BaseMiddleware) OnBefore

func (m *BaseMiddleware) OnBefore(ctx context.Context, request *MiddlewareRequest) (*MiddlewareRequest, error)

OnBefore is a no-op by default

func (*BaseMiddleware) OnError

func (m *BaseMiddleware) OnError(ctx context.Context, err error) error

OnError is a no-op by default

type CacheEntry

type CacheEntry struct {
	Response  *MiddlewareResponse
	ExpiresAt time.Time
}

CacheEntry represents a cached response

type CacheMiddleware

type CacheMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

CacheMiddleware provides response caching with sharded locks for better concurrency

func NewCacheMiddleware

func NewCacheMiddleware(ttl time.Duration) *CacheMiddleware

NewCacheMiddleware creates a cache middleware with sharded locks

func NewCacheMiddlewareWithShards added in v0.5.0

func NewCacheMiddlewareWithShards(ttl time.Duration, numShards uint32) *CacheMiddleware

NewCacheMiddlewareWithShards creates a cache middleware with specified number of shards

func (*CacheMiddleware) Clear

func (m *CacheMiddleware) Clear()

Clear removes all cached entries

func (*CacheMiddleware) OnAfter

OnAfter caches successful responses

func (*CacheMiddleware) OnBefore

OnBefore checks cache before execution

func (*CacheMiddleware) Size

func (m *CacheMiddleware) Size() int

Size returns the number of cached entries

type CircuitBreakerMiddleware

type CircuitBreakerMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

CircuitBreakerMiddleware implements circuit breaker pattern

func NewCircuitBreakerMiddleware

func NewCircuitBreakerMiddleware(maxFailures int, resetTimeout time.Duration) *CircuitBreakerMiddleware

NewCircuitBreakerMiddleware creates a circuit breaker middleware

func (*CircuitBreakerMiddleware) OnAfter

OnAfter handles successful responses

func (*CircuitBreakerMiddleware) OnBefore

OnBefore checks if circuit is open

func (*CircuitBreakerMiddleware) OnError

func (m *CircuitBreakerMiddleware) OnError(ctx context.Context, err error) error

OnError handles errors and updates circuit state

type DynamicPromptMiddleware

type DynamicPromptMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

DynamicPromptMiddleware modifies prompts based on context and state

func NewDynamicPromptMiddleware

func NewDynamicPromptMiddleware(modifier func(*MiddlewareRequest) string) *DynamicPromptMiddleware

NewDynamicPromptMiddleware creates a middleware that modifies prompts dynamically

func (*DynamicPromptMiddleware) OnBefore

OnBefore modifies the prompt based on the current context

type FastMiddlewareChain

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

FastMiddlewareChain 快速中间件链(无 OnAfter 支持)

用于只需要 OnBefore 的场景,进一步减少开销

func NewFastMiddlewareChain

func NewFastMiddlewareChain(handler Handler, middlewares ...Middleware) *FastMiddlewareChain

NewFastMiddlewareChain 创建快速中间件链

func (*FastMiddlewareChain) Execute

Execute 执行(仅 OnBefore)

func (*FastMiddlewareChain) Middlewares

func (c *FastMiddlewareChain) Middlewares() []Middleware

Middlewares 返回中间件列表

type Handler

type Handler func(ctx context.Context, request *MiddlewareRequest) (*MiddlewareResponse, error)

Handler is a function that processes a request and returns a response

type ImmutableMiddlewareChain

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

ImmutableMiddlewareChain 不可变中间件链

优化点: - 预编译的执行链,避免运行时构建 - 减少接口调用 - 内联小函数

性能提升:预期 20% 的中间件开销减少

func NewImmutableMiddlewareChain

func NewImmutableMiddlewareChain(handler Handler, middlewares ...Middleware) *ImmutableMiddlewareChain

NewImmutableMiddlewareChain 创建不可变中间件链

func (*ImmutableMiddlewareChain) Execute

Execute 执行中间件链

func (*ImmutableMiddlewareChain) Middlewares

func (c *ImmutableMiddlewareChain) Middlewares() []Middleware

Middlewares 返回中间件列表

type LoggingMiddleware

type LoggingMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

LoggingMiddleware logs requests and responses

func NewLoggingMiddleware

func NewLoggingMiddleware(logger func(string)) *LoggingMiddleware

NewLoggingMiddleware creates a logging middleware

func (*LoggingMiddleware) OnAfter

OnAfter logs the outgoing response

func (*LoggingMiddleware) OnBefore

OnBefore logs the incoming request

func (*LoggingMiddleware) OnError

func (m *LoggingMiddleware) OnError(ctx context.Context, err error) error

OnError logs errors

type Middleware

type Middleware interface {
	// Name returns the middleware name for identification
	Name() string

	// OnBefore is called before the main execution
	// It can modify the request or short-circuit the chain by returning an error
	OnBefore(ctx context.Context, request *MiddlewareRequest) (*MiddlewareRequest, error)

	// OnAfter is called after the main execution
	// It can modify the response or handle errors
	OnAfter(ctx context.Context, response *MiddlewareResponse) (*MiddlewareResponse, error)

	// OnError is called when an error occurs during execution
	// It can handle, wrap, or suppress the error
	OnError(ctx context.Context, err error) error
}

Middleware defines the interface for request/response interceptors.

Inspired by LangChain's middleware system, it provides:

  • Before/after execution hooks
  • Request/response modification
  • Chain of responsibility pattern
  • Error handling and recovery

Use cases:

  • Dynamic prompt modification
  • Tool selection and filtering
  • Rate limiting and throttling
  • Logging and monitoring
  • Authentication and authorization

type MiddlewareChain

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

MiddlewareChain manages a sequence of middleware 使用 atomic.Pointer 实现零拷贝读取,避免热路径上的 slice 复制

func NewMiddlewareChain

func NewMiddlewareChain(handler Handler) *MiddlewareChain

NewMiddlewareChain creates a new middleware chain

func (*MiddlewareChain) Execute

Execute runs the request through the middleware chain 热路径零拷贝:直接读取不可变 slice,无需加锁或复制

func (*MiddlewareChain) Size added in v0.5.0

func (c *MiddlewareChain) Size() int

Size 返回当前 middleware 数量

func (*MiddlewareChain) Use

func (c *MiddlewareChain) Use(middleware ...Middleware) *MiddlewareChain

Use adds middleware to the chain 写操作使用 copy-on-write 模式,确保读取端无锁

type MiddlewareFunc

type MiddlewareFunc struct {
	*BaseMiddleware
	BeforeFunc func(ctx context.Context, request *MiddlewareRequest) (*MiddlewareRequest, error)
	AfterFunc  func(ctx context.Context, response *MiddlewareResponse) (*MiddlewareResponse, error)
	ErrorFunc  func(ctx context.Context, err error) error
}

MiddlewareFunc allows using a function as middleware

func NewMiddlewareFunc

func NewMiddlewareFunc(
	name string,
	before func(ctx context.Context, request *MiddlewareRequest) (*MiddlewareRequest, error),
	after func(ctx context.Context, response *MiddlewareResponse) (*MiddlewareResponse, error),
	onError func(ctx context.Context, err error) error,
) *MiddlewareFunc

NewMiddlewareFunc creates middleware from functions

func (*MiddlewareFunc) OnAfter

OnAfter calls the after function if provided

func (*MiddlewareFunc) OnBefore

func (m *MiddlewareFunc) OnBefore(ctx context.Context, request *MiddlewareRequest) (*MiddlewareRequest, error)

OnBefore calls the before function if provided

func (*MiddlewareFunc) OnError

func (m *MiddlewareFunc) OnError(ctx context.Context, err error) error

OnError calls the error function if provided

type MiddlewareRequest

type MiddlewareRequest struct {
	// Input is the original input to the agent/tool
	Input interface{} `json:"input"`

	// State is the current agent state
	State state.State `json:"-"`

	// Runtime provides access to the execution environment
	Runtime interface{} `json:"-"`

	// Metadata holds additional request information
	Metadata map[string]interface{} `json:"metadata"`

	// Headers contains request headers (for HTTP-like semantics)
	Headers map[string]string `json:"headers"`

	// Timestamp is when the request was created
	Timestamp time.Time `json:"timestamp"`
}

MiddlewareRequest represents a request passing through middleware

func GetMiddlewareRequest

func GetMiddlewareRequest() *MiddlewareRequest

GetMiddlewareRequest retrieves a MiddlewareRequest from the object pool

type MiddlewareResponse

type MiddlewareResponse struct {
	// Output is the result from the agent/tool
	Output interface{} `json:"output"`

	// State is the updated agent state
	State state.State `json:"-"`

	// Metadata holds additional response information
	Metadata map[string]interface{} `json:"metadata"`

	// Headers contains response headers
	Headers map[string]string `json:"headers"`

	// Duration is the execution time
	Duration time.Duration `json:"duration"`

	// TokenUsage holds LLM token usage statistics
	TokenUsage *interfaces.TokenUsage `json:"token_usage,omitempty"`

	// Error holds any error that occurred
	Error error `json:"-"`
}

MiddlewareResponse represents a response passing through middleware

func GetMiddlewareResponse

func GetMiddlewareResponse() *MiddlewareResponse

GetMiddlewareResponse retrieves a MiddlewareResponse from the object pool

type RandomDelayMiddleware

type RandomDelayMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

RandomDelayMiddleware adds random delay (for testing/simulation)

func NewRandomDelayMiddleware

func NewRandomDelayMiddleware(minDelay, maxDelay time.Duration) *RandomDelayMiddleware

NewRandomDelayMiddleware creates a random delay middleware

func (*RandomDelayMiddleware) OnBefore

OnBefore adds random delay

type RateLimiterMiddleware

type RateLimiterMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

RateLimiterMiddleware implements rate limiting

func NewRateLimiterMiddleware

func NewRateLimiterMiddleware(maxRequests int, windowSize time.Duration) *RateLimiterMiddleware

NewRateLimiterMiddleware creates a rate limiting middleware

func (*RateLimiterMiddleware) OnBefore

OnBefore checks rate limits

func (*RateLimiterMiddleware) Reset

func (m *RateLimiterMiddleware) Reset()

Reset clears all rate limit windows

type RetryMiddleware

type RetryMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

RetryMiddleware implements retry logic

func NewRetryMiddleware

func NewRetryMiddleware(maxRetries int, backoff time.Duration) *RetryMiddleware

NewRetryMiddleware creates a retry middleware

func (*RetryMiddleware) OnError

func (m *RetryMiddleware) OnError(ctx context.Context, err error) error

OnError implements retry logic

func (*RetryMiddleware) WithRetryCondition

func (m *RetryMiddleware) WithRetryCondition(condition func(error) bool) *RetryMiddleware

WithRetryCondition sets a custom retry condition

type TimingMiddleware

type TimingMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

TimingMiddleware tracks execution time

func NewTimingMiddleware

func NewTimingMiddleware() *TimingMiddleware

NewTimingMiddleware creates a timing middleware

func (*TimingMiddleware) GetAverageLatency

func (m *TimingMiddleware) GetAverageLatency() time.Duration

GetAverageLatency returns the average latency

func (*TimingMiddleware) GetTimings

func (m *TimingMiddleware) GetTimings() map[string]time.Duration

GetTimings returns all recorded timings

func (*TimingMiddleware) OnAfter

OnAfter records the duration

func (*TimingMiddleware) OnBefore

OnBefore records the start time

type ToolSelectorMiddleware

type ToolSelectorMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

ToolSelectorMiddleware filters and selects appropriate tools

func NewToolSelectorMiddleware

func NewToolSelectorMiddleware(tools []string, maxTools int) *ToolSelectorMiddleware

NewToolSelectorMiddleware creates a middleware that selects tools based on the query

func (*ToolSelectorMiddleware) OnBefore

OnBefore selects appropriate tools for the request

func (*ToolSelectorMiddleware) WithSelector

func (m *ToolSelectorMiddleware) WithSelector(selector func(query string, tools []string) []string) *ToolSelectorMiddleware

WithSelector sets a custom tool selection function

type TransformMiddleware

type TransformMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

TransformMiddleware transforms input/output

func NewTransformMiddleware

func NewTransformMiddleware(
	inputTransform func(interface{}) (interface{}, error),
	outputTransform func(interface{}) (interface{}, error),
) *TransformMiddleware

NewTransformMiddleware creates a transform middleware

func (*TransformMiddleware) OnAfter

OnAfter transforms the output

func (*TransformMiddleware) OnBefore

OnBefore transforms the input

type ValidationMiddleware

type ValidationMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

ValidationMiddleware validates requests

func NewValidationMiddleware

func NewValidationMiddleware(validators ...func(*MiddlewareRequest) error) *ValidationMiddleware

NewValidationMiddleware creates a validation middleware

func (*ValidationMiddleware) OnBefore

OnBefore validates the request

Jump to

Keyboard shortcuts

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