smartrouting

package
v0.260331.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Operations = []SmartOp{

	{
		Position:  PositionModel,
		Operation: OpModelContains,
		Meta: SmartOpMeta{
			Description: "Model name contains the value",
			Type:        ValueTypeString,
		},
	},
	{
		Position:  PositionModel,
		Operation: OpModelGlob,
		Meta: SmartOpMeta{
			Description: "Model name matches glob pattern",
			Type:        ValueTypeString,
		},
	},
	{
		Position:  PositionModel,
		Operation: OpModelEquals,
		Meta: SmartOpMeta{
			Description: "Model name equals the value",
			Type:        ValueTypeString,
		},
	},

	{
		Position:  PositionThinking,
		Operation: OpThinkingEnabled,
		Meta: SmartOpMeta{
			Description: "Thinking mode is enabled",
			Type:        ValueTypeBool,
		},
	},
	{
		Position:  PositionThinking,
		Operation: OpThinkingDisabled,
		Meta: SmartOpMeta{
			Description: "Thinking mode is disabled",
			Type:        ValueTypeBool,
		},
	},

	{
		Position:  PositionContextSystem,
		Operation: OpContextSystemContains,
		Meta: SmartOpMeta{
			Description: "Any system messages contain the value",
			Type:        ValueTypeString,
		},
	},
	{
		Position:  PositionContextSystem,
		Operation: OpContextSystemRegex,
		Meta: SmartOpMeta{
			Description: "Any system messages match regex pattern",
			Type:        ValueTypeString,
		},
	},

	{
		Position:  PositionContextUser,
		Operation: OpContextUserContains,
		Meta: SmartOpMeta{
			Description: "Any user messages contain the value",
			Type:        ValueTypeString,
		},
	},
	{
		Position:  PositionContextUser,
		Operation: OpContextUserRegex,
		Meta: SmartOpMeta{
			Description: "Combined user messages match regex pattern",
			Type:        ValueTypeString,
		},
	},

	{
		Position:  PositionLatestUser,
		Operation: OpLatestUserContains,
		Meta: SmartOpMeta{
			Description: "Latest user message contains the value",
			Type:        ValueTypeString,
		},
	},
	{
		Position:  PositionLatestUser,
		Operation: OpLatestUserRequestType,
		Meta: SmartOpMeta{
			Description: "Latest user message content type (e.g., 'image')",
			Type:        ValueTypeString,
		},
	},

	{
		Position:  PositionToolUse,
		Operation: OpToolUseEquals,
		Meta: SmartOpMeta{
			Description: "Latest message is `tool use` and it is name is the value",
			Type:        ValueTypeString,
		},
	},

	{
		Position:  PositionToken,
		Operation: OpTokenGe,
		Meta: SmartOpMeta{
			Description: "Token count greater than or equal to value",
			Type:        ValueTypeInt,
		},
	},
	{
		Position:  PositionToken,
		Operation: OpTokenGt,
		Meta: SmartOpMeta{
			Description: "Token count greater than value",
			Type:        ValueTypeInt,
		},
	},
	{
		Position:  PositionToken,
		Operation: OpTokenLe,
		Meta: SmartOpMeta{
			Description: "Token count less than or equal to value",
			Type:        ValueTypeInt,
		},
	},
	{
		Position:  PositionToken,
		Operation: OpTokenLt,
		Meta: SmartOpMeta{
			Description: "Token count less than value",
			Type:        ValueTypeInt,
		},
	},
}

Operations is a comprehensive list of all available operations for smart routing. This registry defines all operations across all positions for documentation, UI rendering, and future API integrations.

Functions

func EstimateTokens

func EstimateTokens(text string) int

EstimateTokens estimates token count from text (rough approximation: 4 chars per token)

func ValidateSmartOp

func ValidateSmartOp(op *SmartOp) error

ValidateSmartOp checks if the operation is valid for its position

func ValidateSmartRouting

func ValidateSmartRouting(rule *SmartRouting) error

ValidateSmartRouting checks if the smart routing rule is valid

Types

type RequestContext

type RequestContext struct {
	Model             string
	ThinkingEnabled   bool
	SystemMessages    []string
	UserMessages      []string
	ToolUses          []string
	LatestRole        string // Latest message role (user, assistant, tool, function, etc.)
	LatestContentType string
	EstimatedTokens   int
}

RequestContext holds extracted request data for evaluation

func ExtractContextFromAnthropicRequest

func ExtractContextFromAnthropicRequest(req *anthropic.MessageNewParams) *RequestContext

ExtractContextFromAnthropicRequest extracts RequestContext from an Anthropic messages request

func ExtractContextFromBetaRequest

func ExtractContextFromBetaRequest(req *anthropic.BetaMessageNewParams) *RequestContext

ExtractContextFromBetaRequest extracts RequestContext from an Anthropic beta messages request

func ExtractContextFromOpenAIRequest

func ExtractContextFromOpenAIRequest(req *openai.ChatCompletionNewParams) *RequestContext

ExtractContextFromOpenAIRequest extracts RequestContext from an OpenAI chat completion request

func (*RequestContext) CombineMessages

func (rc *RequestContext) CombineMessages(messages []string) string

CombineMessages combines all messages of a type into a single string

func (*RequestContext) GetLatestUserMessage

func (rc *RequestContext) GetLatestUserMessage() string

GetLatestUserMessage returns the latest user message

type Router

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

Router evaluates requests against smart routing rules

func NewRouter

func NewRouter(rules []SmartRouting) (*Router, error)

NewRouter creates a new smart routing router

func (*Router) EvaluateRequest

func (r *Router) EvaluateRequest(ctx *RequestContext) ([]*loadbalance.Service, bool)

EvaluateRequest evaluates a request against smart routing rules Returns the matched services and true if a rule matched, otherwise empty and false

func (*Router) EvaluateRequestWithIndex

func (r *Router) EvaluateRequestWithIndex(ctx *RequestContext) ([]*loadbalance.Service, int, bool)

EvaluateRequestWithIndex evaluates a request against smart routing rules Returns the matched services, the matched rule index, and true if a rule matched

func (*Router) GetRules

func (r *Router) GetRules() []SmartRouting

GetRules returns the router's rules

type SmartOp

type SmartOp struct {
	UUID      string           `json:"uuid"`
	Position  SmartOpPosition  `json:"position" yaml:"position"`
	Operation SmartOpOperation `json:"operation" yaml:"operation"`
	Value     string           `json:"value,omitempty" yaml:"value,omitempty"`
	Meta      SmartOpMeta      `json:"meta,omitempty" yaml:"meta,omitempty"`
}

SmartOp represents a single operation for smart routing Each operation has 4 parts: position, operation, value, meta

func (*SmartOp) Bool

func (o *SmartOp) Bool() (bool, error)

Bool returns the value as a boolean with type checking and conversion

func (*SmartOp) Int

func (o *SmartOp) Int() (int, error)

Int returns the value as an integer with type checking and conversion

func (*SmartOp) String

func (o *SmartOp) String() (string, error)

String returns the value as a string with type checking

type SmartOpMeta

type SmartOpMeta struct {
	Description string           `json:"description,omitempty" yaml:"description,omitempty"` // Human-readable description of the operation
	Type        SmartOpValueType `json:"type,omitempty" yaml:"type,omitempty"`               // Type of the value field
}

SmartOpMeta contains metadata for a smart routing operation

type SmartOpOperation

type SmartOpOperation string

SmartOpOperation represents the operation to perform on a position

const (
	// Model operations
	OpModelContains SmartOpOperation = "contains" // Model name contains the value
	OpModelGlob     SmartOpOperation = "glob"     // Model name matches glob pattern
	OpModelEquals   SmartOpOperation = "equals"   // Model name equals the value

	// Thinking operations
	OpThinkingEnabled  SmartOpOperation = "enabled"  // Thinking mode is enabled
	OpThinkingDisabled SmartOpOperation = "disabled" // Thinking mode is disabled

	// Context system message operations
	OpContextSystemContains SmartOpOperation = "contains" // Any system messages contain the value
	OpContextSystemRegex    SmartOpOperation = "regex"    // Any system messages match regex pattern

	// Context user message operations
	OpContextUserContains SmartOpOperation = "contains" // Any user messages contain the value
	OpContextUserRegex    SmartOpOperation = "regex"    // Combined user messages match regex pattern

	// Latest user message operations
	OpLatestUserContains    SmartOpOperation = "contains" // Latest user message contains the value
	OpLatestUserRequestType SmartOpOperation = "type"     // Latest user message content type

	// Tool use operations
	OpToolUseEquals   SmartOpOperation = "equals"   // Latest message is `tool use` and its name equals the value
	OpToolUseContains SmartOpOperation = "contains" // Latest message is `tool use` and its name or arguments contains the value

	// Token operations
	OpTokenGe SmartOpOperation = "ge" // Token count greater than or equal to value
	OpTokenGt SmartOpOperation = "gt" // Token count greater than value
	OpTokenLe SmartOpOperation = "le" // Token count less than or equal to value
	OpTokenLt SmartOpOperation = "lt" // Token count less than value
)

type SmartOpPosition

type SmartOpPosition string

SmartOpPosition represents the field to check in a request for smart routing

const (
	PositionModel         SmartOpPosition = "model"          // Request model name
	PositionThinking      SmartOpPosition = "thinking"       // Thinking mode enabled
	PositionContextSystem SmartOpPosition = "context_system" // System message content in context
	PositionContextUser   SmartOpPosition = "context_user"   // User message content in context
	PositionLatestUser    SmartOpPosition = "latest_user"    // Latest user message
	PositionToolUse       SmartOpPosition = "tool_use"       // Tool use/name
	PositionToken         SmartOpPosition = "token"          // Token count
)

func (SmartOpPosition) IsValid

func (p SmartOpPosition) IsValid() bool

IsValid checks if the position is valid

type SmartOpValueType

type SmartOpValueType string

SmartOpValueType represents the type of value in a smart routing operation

const (
	ValueTypeString SmartOpValueType = "string" // String value
	ValueTypeInt    SmartOpValueType = "int"    // Integer value
	ValueTypeBool   SmartOpValueType = "bool"   // Boolean value
	ValueTypeFloat  SmartOpValueType = "float"  // Float value
)

type SmartRouting

type SmartRouting struct {
	Description string                 `json:"description" yaml:"description"`
	Ops         []SmartOp              `json:"ops" yaml:"ops"`
	Services    []*loadbalance.Service `json:"services" yaml:"services"`
}

SmartRouting represents a smart routing rule block

type TypeError

type TypeError struct {
	Expected SmartOpValueType
	Got      SmartOpValueType
	Err      error // Underlying conversion error
}

TypeError represents a type mismatch error when accessing SmartOp values

func (*TypeError) Error

func (e *TypeError) Error() string

func (*TypeError) Unwrap

func (e *TypeError) Unwrap() error

Jump to

Keyboard shortcuts

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