Documentation
¶
Index ¶
- Constants
- type Conditions
- type ErrorResponse
- type Latency
- type MockRule
- type MockStats
- type MockerConfig
- type MockerPlugin
- func (p *MockerPlugin) Cleanup() error
- func (p *MockerPlugin) GetName() string
- func (p *MockerPlugin) GetStats() MockStats
- func (p *MockerPlugin) HTTPTransportPostHook(ctx *schemas.BifrostContext, req *schemas.HTTPRequest, ...) error
- func (p *MockerPlugin) HTTPTransportPreHook(ctx *schemas.BifrostContext, req *schemas.HTTPRequest) (*schemas.HTTPResponse, error)
- func (p *MockerPlugin) PostHook(ctx *schemas.BifrostContext, result *schemas.BifrostResponse, ...) (*schemas.BifrostResponse, *schemas.BifrostError, error)
- func (p *MockerPlugin) PreHook(ctx *schemas.BifrostContext, req *schemas.BifrostRequest) (*schemas.BifrostRequest, *schemas.PluginShortCircuit, error)
- type Response
- type SizeRange
- type SuccessResponse
- type Usage
Constants ¶
const ( // Response types ResponseTypeSuccess = "success" ResponseTypeError = "error" // Default behaviors DefaultBehaviorPassthrough = "passthrough" DefaultBehaviorError = "error" DefaultBehaviorSuccess = "success" // Latency types LatencyTypeFixed = "fixed" LatencyTypeUniform = "uniform" )
Constants for type checking and validation
const (
PluginName = "bifrost-mocker"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Conditions ¶
type Conditions struct {
Providers []string `json:"providers"` // Match specific providers (e.g., ["openai", "anthropic"])
Models []string `json:"models"` // Match specific models (e.g., ["gpt-4", "claude-3"])
MessageRegex *string `json:"message_regex"` // Regex pattern to match against message content
RequestSize *SizeRange `json:"request_size"` // Request size constraints in bytes
}
Conditions define when a mock rule should be applied All specified conditions must match for the rule to trigger
type ErrorResponse ¶
type ErrorResponse struct {
Message string `json:"message"` // Error message to return
Type *string `json:"type"` // Error type (e.g., "rate_limit", "auth_error")
Code *string `json:"code"` // Error code (e.g., "429", "401")
StatusCode *int `json:"status_code"` // HTTP status code for the error
}
ErrorResponse defines mock error response content
type Latency ¶
type Latency struct {
Min time.Duration `json:"min"` // Minimum latency as time.Duration (e.g., 100*time.Millisecond, NOT raw int)
Max time.Duration `json:"max"` // Maximum latency as time.Duration (e.g., 500*time.Millisecond, NOT raw int)
Type string `json:"type"` // Latency type: "fixed" or "uniform"
}
Latency defines latency simulation settings
type MockRule ¶
type MockRule struct {
Name string `json:"name"` // Unique rule name for identification and statistics tracking
Enabled bool `json:"enabled"` // Enable/disable this rule (disabled rules are skipped)
Priority int `json:"priority"` // Higher priority rules are checked first (higher numbers = higher priority)
Conditions Conditions `json:"conditions"` // Conditions that must match for this rule to apply
Responses []Response `json:"responses"` // Possible responses (selected using weighted random selection)
Latency *Latency `json:"latency"` // Rule-specific latency override (overrides global latency if set)
Probability float64 `json:"probability"` // Probability of rule activation (0.0=never, 1.0=always, 0=disabled)
}
MockRule defines a single mocking rule with conditions and responses Rules are evaluated in priority order (higher numbers = higher priority)
type MockStats ¶
type MockStats struct {
TotalRequests int64 `json:"total_requests"` // Total number of requests processed
MockedRequests int64 `json:"mocked_requests"` // Number of requests that were mocked (rules matched)
RuleHits map[string]int64 `json:"rule_hits"` // Rule name -> hit count mapping
ErrorsGenerated int64 `json:"errors_generated"` // Number of error responses generated
ResponsesGenerated int64 `json:"responses_generated"` // Number of success responses generated
}
MockStats tracks plugin statistics and rule execution counts
type MockerConfig ¶
type MockerConfig struct {
Enabled bool `json:"enabled"` // Enable/disable the mocker plugin
GlobalLatency *Latency `json:"global_latency"` // Global latency settings applied to all rules (can be overridden per rule)
Rules []MockRule `json:"rules"` // List of mock rules to be evaluated in priority order
DefaultBehavior string `json:"default_behavior"` // Action when no rules match: "passthrough", "error", or "success"
}
MockerConfig defines the overall configuration for the mocker plugin
type MockerPlugin ¶
type MockerPlugin struct {
// contains filtered or unexported fields
}
MockerPlugin provides comprehensive request/response mocking capabilities
func Init ¶
func Init(config MockerConfig) (*MockerPlugin, error)
Init creates a new mocker plugin instance with sensible defaults Returns an error if required configuration is invalid or missing
func (*MockerPlugin) Cleanup ¶
func (p *MockerPlugin) Cleanup() error
Cleanup performs plugin cleanup and frees memory IMPORTANT: Call GetStats() before Cleanup() if you need the statistics, as this method clears all statistics data to free memory
func (*MockerPlugin) GetName ¶
func (p *MockerPlugin) GetName() string
GetName returns the plugin name
func (*MockerPlugin) GetStats ¶
func (p *MockerPlugin) GetStats() MockStats
GetStats returns current plugin statistics IMPORTANT: Call this method before Cleanup() if you need the statistics, as Cleanup() clears all statistics data to free memory
func (*MockerPlugin) HTTPTransportPostHook ¶ added in v1.4.9
func (p *MockerPlugin) HTTPTransportPostHook(ctx *schemas.BifrostContext, req *schemas.HTTPRequest, resp *schemas.HTTPResponse) error
HTTPTransportPostHook is not used for this plugin
func (*MockerPlugin) HTTPTransportPreHook ¶ added in v1.4.9
func (p *MockerPlugin) HTTPTransportPreHook(ctx *schemas.BifrostContext, req *schemas.HTTPRequest) (*schemas.HTTPResponse, error)
HTTPTransportPreHook is not used for this plugin
func (*MockerPlugin) PostHook ¶
func (p *MockerPlugin) PostHook(ctx *schemas.BifrostContext, result *schemas.BifrostResponse, err *schemas.BifrostError) (*schemas.BifrostResponse, *schemas.BifrostError, error)
PostHook processes responses after provider calls
func (*MockerPlugin) PreHook ¶
func (p *MockerPlugin) PreHook(ctx *schemas.BifrostContext, req *schemas.BifrostRequest) (*schemas.BifrostRequest, *schemas.PluginShortCircuit, error)
PreHook intercepts requests and applies mocking rules based on configuration This is called before the actual provider request and can short-circuit the flow
type Response ¶
type Response struct {
Type string `json:"type"` // Response type: "success" or "error"
Weight float64 `json:"weight"` // Weight for random selection (higher = more likely)
Content *SuccessResponse `json:"content"` // Success response content (required if Type="success")
Error *ErrorResponse `json:"error"` // Error response content (required if Type="error")
AllowFallbacks *bool `json:"allow_fallbacks"` // Control fallback behavior for errors (nil=true, false=no fallbacks)
}
Response defines a mock response configuration Either Content (for success) or Error (for error) should be set, not both
type SizeRange ¶
type SizeRange struct {
Min int `json:"min"` // Minimum request size in bytes
Max int `json:"max"` // Maximum request size in bytes
}
SizeRange defines request size constraints in bytes
type SuccessResponse ¶
type SuccessResponse struct {
Message string `json:"message"` // Static response message
Model *string `json:"model"` // Override model name in response (optional)
Usage *Usage `json:"usage"` // Token usage info (optional, defaults applied if nil)
FinishReason *string `json:"finish_reason"` // Completion reason (optional, defaults to "stop")
MessageTemplate *string `json:"message_template"` // Template with variables like {{model}}, {{provider}} (overrides Message)
CustomFields map[string]interface{} `json:"custom_fields"` // Additional fields stored in response metadata
}
SuccessResponse defines mock success response content Either Message or MessageTemplate should be set (MessageTemplate takes precedence)