middleware

package
v1.0.61 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package middleware provides HTTP middleware chain for request and response processing.

This package re-exports the public API from internal/common/middleware, providing external consumers with a flexible middleware infrastructure for AI provider requests and responses.

Overview

The middleware package enables request transformation, response processing, logging, metrics collection, and other cross-cutting concerns through a composable middleware chain pattern. Middleware can process requests before they are sent to AI providers and responses after they are received.

Key Components

  • RequestMiddleware: Processes HTTP requests before sending
  • ResponseMiddleware: Processes HTTP responses after receiving
  • Middleware: Combined interface for both request and response processing
  • MiddlewareChain: Manages ordered middleware execution

Basic Usage

// Create a new middleware chain
chain := middleware.NewMiddlewareChain()

// Add middleware to the chain
chain.Add(loggingMiddleware).
      Add(metricsMiddleware).
      Add(retryMiddleware)

// Process a request
ctx := context.Background()
req, _ := http.NewRequest("POST", "https://api.openai.com/v1/chat/completions", body)
newCtx, newReq, err := chain.ProcessRequest(ctx, req)
if err != nil {
    // Handle error
}

// Make the HTTP call
resp, err := client.Do(newReq)
if err != nil {
    // Handle error
}

// Process the response
newCtx, newResp, err := chain.ProcessResponse(newCtx, newReq, resp)

Context Keys

The package provides standard context keys for passing data between middleware:

  • ContextKeyRequestID: Unique request identifier

  • ContextKeyStartTime: Request start time

  • ContextKeyProvider: Provider name (e.g., "openai", "anthropic")

  • ContextKeyModel: Model name (e.g., "gpt-4", "claude-3")

  • ContextKeyMetadata: Arbitrary metadata map

  • ContextKeyError: Error information

  • ContextKeyRetryCount: Retry attempt count

    // Store data in context ctx = context.WithValue(ctx, middleware.ContextKeyProvider, "openai") ctx = context.WithValue(ctx, middleware.ContextKeyModel, "gpt-4") ctx = context.WithValue(ctx, middleware.ContextKeyRetryCount, 0)

    // Retrieve data from context if provider, ok := ctx.Value(middleware.ContextKeyProvider).(string); ok { // Use provider }

Execution Order

The middleware chain executes in a specific order:

  • Request middleware: Execute in the order they were added (first to last)
  • Response middleware: Execute in reverse order (last to first)

This ensures symmetric processing, similar to nested function calls:

Request:  MW1 -> MW2 -> MW3 -> [HTTP Call]
Response: MW1 <- MW2 <- MW3 <- [HTTP Call]

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCombinedMiddleware

func NewCombinedMiddleware(reqProcessor middleware.RequestMiddleware, respProcessor middleware.ResponseMiddleware) *middleware.CombinedMiddleware

NewCombinedMiddleware creates a new combined middleware. Use this to combine separate request and response processors into a single middleware.

func NewMiddlewareChain

func NewMiddlewareChain() *middleware.DefaultMiddlewareChain

NewMiddlewareChain creates a new middleware chain. The returned chain is empty and ready to have middleware added.

Types

type CombinedMiddleware

type CombinedMiddleware = middleware.CombinedMiddleware

CombinedMiddleware is a middleware that implements both RequestMiddleware and ResponseMiddleware.

type ContextKey

type ContextKey = middleware.ContextKey

ContextKey is the type used for middleware context keys.

const (
	// ContextKeyRequestID stores a unique request identifier.
	ContextKeyRequestID ContextKey = middleware.ContextKeyRequestID
	// ContextKeyStartTime stores the request start time.
	ContextKeyStartTime ContextKey = middleware.ContextKeyStartTime
	// ContextKeyProvider stores the provider name.
	ContextKeyProvider ContextKey = middleware.ContextKeyProvider
	// ContextKeyModel stores the model name.
	ContextKeyModel ContextKey = middleware.ContextKeyModel
	// ContextKeyMetadata stores arbitrary metadata.
	ContextKeyMetadata ContextKey = middleware.ContextKeyMetadata
	// ContextKeyError stores error information.
	ContextKeyError ContextKey = middleware.ContextKeyError
	// ContextKeyRetryCount stores the retry attempt count.
	ContextKeyRetryCount ContextKey = middleware.ContextKeyRetryCount
)

Standard context keys for passing data between middleware.

type DefaultMiddlewareChain

type DefaultMiddlewareChain = middleware.DefaultMiddlewareChain

DefaultMiddlewareChain is the default implementation of MiddlewareChain. It is thread-safe and can be used concurrently.

type Middleware

type Middleware = middleware.Middleware

Middleware is a combined interface for both request and response processing. Middleware implementations can implement either or both interfaces.

type MiddlewareChain

type MiddlewareChain = middleware.MiddlewareChain

MiddlewareChain manages an ordered collection of middleware. Provides methods to add, remove, and execute middleware in the chain.

type RequestMiddleware

type RequestMiddleware = middleware.RequestMiddleware

RequestMiddleware transforms requests before they are sent to the provider. Implement ProcessRequest to modify the request, context, or return an error to abort.

type RequestMiddlewareFunc

type RequestMiddlewareFunc = middleware.RequestMiddlewareFunc

RequestMiddlewareFunc is a function adapter for RequestMiddleware. Allows simple functions to be used as request middleware.

type ResponseMiddleware

type ResponseMiddleware = middleware.ResponseMiddleware

ResponseMiddleware transforms responses after they are received from the provider. Implement ProcessResponse to modify the response, context, or return an error.

type ResponseMiddlewareFunc

type ResponseMiddlewareFunc = middleware.ResponseMiddlewareFunc

ResponseMiddlewareFunc is a function adapter for ResponseMiddleware. Allows simple functions to be used as response middleware.

Jump to

Keyboard shortcuts

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