Documentation
¶
Overview ¶
Package d_router provides routing configuration types for the chatbot framework. It includes route handlers, options, and trigger configurations.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DEFAULT_TIMEOUT is the default maximum duration allowed for route handler execution. DEFAULT_TIMEOUT = TimeoutRouteOps{ Duration: 5 * time.Minute, Route: "timeout_route", } // DEFAULT_LOOP_COUNT is the default number of loops allowed before stopping handler execution. DEFAULT_LOOP_COUNT = LoopCountRouteOps{ Count: 3, Route: "loop_route", } )
Default values for route handler options.
Functions ¶
This section is empty.
Types ¶
type LoopCountRouteOps ¶
type LoopCountRouteOps struct {
// Count is the maximum number of consecutive route executions allowed.
// When this limit is exceeded, the user is redirected to the fallback Route.
Count int
// Route is the route name to redirect to when the loop limit is exceeded.
Route string
}
LoopCountRouteOps configures loop protection for route handler execution. This prevents infinite redirect loops by limiting the number of consecutive route executions and redirecting to a fallback route when exceeded.
type ProtectedRouteOps ¶
type ProtectedRouteOps struct {
// Route is the route name to redirect to if the user is not allowed access.
Route string
}
ProtectedRouteOps configures route protection settings. When enabled, users who don't meet the protection criteria will be redirected to the specified route instead of accessing the protected handler.
type RouteHandler ¶
type RouteHandler[Obs any] func(ctx *d_context.ChatContext[Obs]) route_return.RouteReturn
RouteHandler defines the signature of a route handler function. Route handlers process incoming messages and return a RouteReturn to indicate the next action. They can return:
- EndAction: Ends the conversation session
- RedirectResponse: Redirects to another route immediately
- Route: Sets the next route for the user's next message
- TransferToMenu: Transfers the user to a different menu
The Obs type parameter allows custom observation data to be passed through the context.
type RouteTrigger ¶
type RouteTrigger struct {
// Regex is the regular expression pattern to match against user messages.
// The pattern is evaluated using Go's regexp package.
Regex string
// Route is the target route name to redirect to when the pattern matches.
Route string
}
RouteTrigger defines a pattern-based automatic route redirection. When a user's message matches the Regex pattern, the conversation is automatically redirected to the specified Route.
This is useful for implementing global commands like "help", "cancel", or "menu" that should work regardless of the current route.
type RouterHandlerAdmnistrator ¶
type RouterHandlerAdmnistrator[Obs any] struct { // HandlerOptions contains the configuration for handler execution. HandlerOptions RouterHandlerOptions // Handler is the route handler function to be executed. Handler RouteHandler[Obs] }
RouterHandlerAdmnistrator wraps a route handler with its configuration options. It associates a RouteHandler with its execution settings like timeout and error tracking.
type RouterHandlerOptions ¶
type RouterHandlerOptions struct {
// LoopCount configures loop protection to prevent infinite redirect loops.
// If nil, DEFAULT_LOOP_COUNT is used.
LoopCount *LoopCountRouteOps
// Timeout specifies the maximum duration allowed for the handler execution.
// Defaults to DEFAULT_TIMEOUT if not specified.
Timeout *TimeoutRouteOps
// Protected configures route protection settings.
// When enabled, unauthorized users will be redirected to the specified route.
Protected *ProtectedRouteOps
// Triggers is a list of regex-based triggers that can automatically redirect
// the conversation to a different route based on message content.
Triggers []RouteTrigger
}
RouterHandlerOptions configures the behavior and constraints for router handler execution. It provides settings for error tracking, execution time limits, and route protection to ensure robust and controlled request processing.
func (*RouterHandlerOptions) GetRhoRoutes ¶
func (o *RouterHandlerOptions) GetRhoRoutes() []string
func (*RouterHandlerOptions) SetOps ¶
func (o *RouterHandlerOptions) SetOps(other RouterHandlerOptions)
SetOps merges the options from another RouterHandlerOptions into this one. Non-nil values in the other options will override the corresponding values in this instance. This allows for selective option overriding while preserving defaults.
type TimeoutRouteOps ¶
type TimeoutRouteOps struct {
// Duration specifies the maximum duration allowed for the handler execution.
// If the handler does not complete within this time, it will be cancelled.
Duration time.Duration
// Route is the route name to redirect to when a timeout occurs.
Route string
}
TimeoutRouteOps configures timeout behavior for route handler execution. When a handler exceeds the specified duration, the user is redirected to the specified route and the handler execution is cancelled.