middleware

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextBuilderMiddleware

func ContextBuilderMiddleware(policy *ContextBuilderPolicy) pipeline.Middleware

ContextBuilderMiddleware manages conversation context with token budget enforcement This middleware should be placed BEFORE ProviderMiddleware in the pipeline

func ContextExtractionMiddleware

func ContextExtractionMiddleware() pipeline.Middleware

func DebugMiddleware

func DebugMiddleware(stage string) pipeline.Middleware

DebugMiddleware creates middleware that logs the full execution context as JSON. The stage parameter identifies where in the pipeline this middleware is placed. You can add this middleware multiple times at different stages to trace state changes.

Example:

middleware.DebugMiddleware("after-prompt-assembly"),
middleware.DebugMiddleware("after-provider"),

Note: This middleware serializes the entire context to JSON, which can be expensive. Only use in development/debugging scenarios.

func DynamicValidatorMiddleware

func DynamicValidatorMiddleware(registry *validators.Registry) pipeline.Middleware

DynamicValidatorMiddleware creates middleware that dynamically instantiates validators from configurations stored in ExecutionContext. It uses the validator registry to create validators on-demand and passes their params from the config.

func DynamicValidatorMiddlewareWithSuppression added in v1.1.0

func DynamicValidatorMiddlewareWithSuppression(
	registry *validators.Registry,
	suppressExceptions bool,
) pipeline.Middleware

DynamicValidatorMiddlewareWithSuppression creates middleware with validation suppression enabled. This is primarily used by test frameworks (like Arena) to allow validation failures to be recorded without halting execution, enabling assertions on guardrail behavior.

func GetContextMetadata

func GetContextMetadata(execCtx *pipeline.ExecutionContext) (truncated bool, originalCount, truncatedCount int)

GetContextMetadata extracts context truncation metadata from ExecutionContext

func MediaExternalizerMiddleware added in v1.1.2

func MediaExternalizerMiddleware(config *MediaExternalizerConfig) pipeline.Middleware

MediaExternalizerMiddleware creates middleware that externalizes media content to storage. It processes provider responses and moves large media from inline base64 to file storage, reducing memory usage and enabling better media management.

func PromptAssemblyMiddleware

func PromptAssemblyMiddleware(promptRegistry *prompt.Registry, taskType string, baseVariables map[string]string) pipeline.Middleware

PromptAssemblyMiddleware loads and assembles prompts from the prompt registry. It populates execCtx.SystemPrompt, execCtx.AllowedTools, and base variables. This middleware should run BEFORE context extraction and template substitution.

func ProviderMiddleware

func ProviderMiddleware(provider providers.Provider, toolRegistry *tools.Registry, toolPolicy *pipeline.ToolPolicy, config *ProviderMiddlewareConfig) pipeline.Middleware

ProviderMiddleware executes LLM calls and handles tool execution via the ToolRegistry. It supports multi-round execution when tools are involved. In streaming mode, it forwards chunks to execCtx.StreamOutput.

The provider, toolRegistry, toolPolicy, and configuration are provided at construction time, simplifying the ExecutionContext and avoiding the need to pass Config around.

func StateStoreLoadMiddleware

func StateStoreLoadMiddleware(config *pipeline.StateStoreConfig) pipeline.Middleware

StateStoreLoadMiddleware loads conversation history from StateStore. It should be placed FIRST in the pipeline, before any other middleware. If the conversation doesn't exist, it starts with an empty history.

func StateStoreSaveMiddleware

func StateStoreSaveMiddleware(config *pipeline.StateStoreConfig) pipeline.Middleware

StateStoreSaveMiddleware saves conversation state to StateStore. It should be placed LAST in the pipeline, after all other middleware. It saves even if the pipeline execution failed (to preserve partial state).

func TemplateMiddleware

func TemplateMiddleware() pipeline.Middleware

TemplateMiddleware substitutes variables in the system prompt. It replaces {{variable}} placeholders with values from ExecutionContext.Variables.

Types

type ContextBuilderPolicy

type ContextBuilderPolicy struct {
	TokenBudget      int                // Max tokens for context (0 = unlimited)
	ReserveForOutput int                // Reserve tokens for response
	Strategy         TruncationStrategy // How to handle overflow
	CacheBreakpoints bool               // Insert cache markers (Anthropic)
}

ContextBuilderPolicy defines token budget and truncation behavior

type MediaExternalizerConfig added in v1.1.2

type MediaExternalizerConfig struct {
	// Enabled controls whether media externalization is active
	Enabled bool

	// StorageService is the backend for storing media files
	StorageService storage.MediaStorageService

	// SizeThresholdKB is the minimum size (in KB) for externalizing media
	// Media smaller than this threshold stays inline as base64
	SizeThresholdKB int64

	// DefaultPolicy is the retention policy applied to externalized media
	DefaultPolicy string

	// RunID identifies the test run (for organization)
	RunID string

	// SessionID identifies the session (optional, for by-session organization)
	SessionID string

	// ConversationID identifies the conversation (optional, for by-conversation organization)
	ConversationID string
}

MediaExternalizerConfig configures media externalization behavior.

type ProviderMiddlewareConfig

type ProviderMiddlewareConfig struct {
	MaxTokens    int
	Temperature  float32
	Seed         *int
	DisableTrace bool // Disable execution tracing (default: false = tracing enabled)
}

ProviderMiddlewareConfig contains configuration for the provider middleware

type TruncationStrategy

type TruncationStrategy string

TruncationStrategy defines how to handle messages when over token budget

const (
	// TruncateOldest drops oldest messages first (simple, preserves recent context)
	TruncateOldest TruncationStrategy = "oldest"

	// TruncateLeastRelevant drops least relevant messages (requires embeddings)
	TruncateLeastRelevant TruncationStrategy = "relevance"

	// TruncateSummarize compresses old messages into summaries
	TruncateSummarize TruncationStrategy = "summarize"

	// TruncateFail returns error if over budget (strict mode)
	TruncateFail TruncationStrategy = "fail"
)

Jump to

Keyboard shortcuts

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