|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from rsb.models.base_model import BaseModel |
| 4 | +from rsb.models.field import Field |
| 5 | + |
| 6 | +from agentle.agents.whatsapp.v2.message_limit import MessageLimit |
| 7 | +from agentle.tts.speech_config import SpeechConfig |
| 8 | + |
| 9 | + |
| 10 | +class BotConfig(BaseModel): |
| 11 | + """Configuration for WhatsApp bot behavior with simplified constructors and better organization. |
| 12 | +
|
| 13 | + This configuration class provides comprehensive control over WhatsApp bot behavior including: |
| 14 | + - Core bot behavior (typing indicators, message reading, quoting) |
| 15 | + - Message batching for handling rapid message sequences |
| 16 | + - Spam protection and rate limiting |
| 17 | + - Human-like delays to simulate realistic human behavior patterns |
| 18 | + - Text-to-speech integration |
| 19 | + - Error handling and retry logic |
| 20 | + - Debug and monitoring settings |
| 21 | +
|
| 22 | + Human-Like Delays Feature: |
| 23 | + The human-like delays feature simulates realistic human behavior patterns by introducing |
| 24 | + configurable delays at three critical points in message processing: |
| 25 | +
|
| 26 | + 1. Read Delay: Time between receiving a message and marking it as read |
| 27 | + - Simulates the time a human takes to read and comprehend a message |
| 28 | + - Calculated based on message length using realistic reading speeds |
| 29 | +
|
| 30 | + 2. Typing Delay: Time between generating a response and sending it |
| 31 | + - Simulates the time a human takes to compose and type a response |
| 32 | + - Calculated based on response length using realistic typing speeds |
| 33 | +
|
| 34 | + 3. Send Delay: Brief final delay before message transmission |
| 35 | + - Simulates the final review time before a human sends a message |
| 36 | + - Random delay within configured bounds |
| 37 | +
|
| 38 | + These delays help prevent platform detection and account restrictions while |
| 39 | + maintaining natural interaction timing. All delays support jitter (random variation) |
| 40 | + to prevent detectable patterns. |
| 41 | +
|
| 42 | + Configuration Presets: |
| 43 | + Use the class methods to create pre-configured instances optimized for specific use cases: |
| 44 | + - development(): Fast iteration with delays disabled |
| 45 | + - production(): Balanced configuration with delays enabled |
| 46 | + - high_volume(): Optimized for throughput with balanced delays |
| 47 | + - customer_service(): Professional timing with thoughtful delays |
| 48 | + - minimal(): Bare minimum configuration with delays disabled |
| 49 | +
|
| 50 | + Examples: |
| 51 | + >>> # Create a production configuration with default delay settings |
| 52 | + >>> config = BotConfig.production() |
| 53 | +
|
| 54 | + >>> # Create a custom configuration with specific delay bounds |
| 55 | + >>> config = BotConfig( |
| 56 | + ... enable_human_delays=True, |
| 57 | + ... min_read_delay_seconds=3.0, |
| 58 | + ... max_read_delay_seconds=20.0, |
| 59 | + ... min_typing_delay_seconds=5.0, |
| 60 | + ... max_typing_delay_seconds=60.0 |
| 61 | + ... ) |
| 62 | +
|
| 63 | + >>> # Override delay settings on an existing configuration |
| 64 | + >>> prod_config = BotConfig.production() |
| 65 | + >>> custom_config = prod_config.with_overrides( |
| 66 | + ... min_read_delay_seconds=5.0, |
| 67 | + ... max_typing_delay_seconds=90.0 |
| 68 | + ... ) |
| 69 | + """ |
| 70 | + |
| 71 | + quote_messages: bool = Field( |
| 72 | + default=False, description="Whether to quote user messages in replies" |
| 73 | + ) |
| 74 | + session_timeout_minutes: int = Field( |
| 75 | + default=30, description="Minutes of inactivity before session reset" |
| 76 | + ) |
| 77 | + max_message_length: MessageLimit = Field( |
| 78 | + default=MessageLimit.NEWLY_CREATED, |
| 79 | + description="Maximum message length (WhatsApp limit)", |
| 80 | + ) |
| 81 | + max_split_messages: int = Field( |
| 82 | + default=5, |
| 83 | + description="Maximum number of split messages to send (remaining will be grouped)", |
| 84 | + ) |
| 85 | + error_message: str = Field( |
| 86 | + default="Sorry, I encountered an error processing your message. Please try again.", |
| 87 | + description="Default error message", |
| 88 | + ) |
| 89 | + welcome_message: str | None = Field( |
| 90 | + default=None, description="Message to send on first interaction" |
| 91 | + ) |
| 92 | + |
| 93 | + # === Message Batching (Simplified) === |
| 94 | + enable_message_batching: bool = Field( |
| 95 | + default=True, description="Enable message batching to prevent spam" |
| 96 | + ) |
| 97 | + batch_delay_seconds: float = Field( |
| 98 | + default=15.0, |
| 99 | + description="Time to wait for additional messages before processing batch", |
| 100 | + ) |
| 101 | + max_batch_size: int = Field( |
| 102 | + default=10, description="Maximum number of messages to batch together" |
| 103 | + ) |
| 104 | + |
| 105 | + # === Spam Protection === |
| 106 | + spam_protection_enabled: bool = Field( |
| 107 | + default=True, description="Enable spam protection mechanisms" |
| 108 | + ) |
| 109 | + min_message_interval_seconds: float = Field( |
| 110 | + default=1, |
| 111 | + description="Minimum interval between processing messages from same user", |
| 112 | + ) |
| 113 | + max_messages_per_minute: int = Field( |
| 114 | + default=20, |
| 115 | + description="Maximum messages per minute per user before rate limiting", |
| 116 | + ) |
| 117 | + rate_limit_cooldown_seconds: int = Field( |
| 118 | + default=60, description="Cooldown period after rate limit is triggered" |
| 119 | + ) |
| 120 | + |
| 121 | + # === Text-to-Speech (TTS) === |
| 122 | + speech_play_chance: float = Field( |
| 123 | + default=0.0, |
| 124 | + ge=0.0, |
| 125 | + le=1.0, |
| 126 | + description="Probability (0.0-1.0) of sending audio response instead of text", |
| 127 | + ) |
| 128 | + speech_config: SpeechConfig | None = Field( |
| 129 | + default=None, |
| 130 | + description="Optional SpeechConfig for TTS provider customization", |
| 131 | + ) |
| 132 | + |
| 133 | + # === Error Handling === |
| 134 | + retry_failed_messages: bool = Field( |
| 135 | + default=True, description="Retry processing failed messages" |
| 136 | + ) |
| 137 | + max_retry_attempts: int = Field( |
| 138 | + default=3, description="Maximum number of retry attempts for failed messages" |
| 139 | + ) |
| 140 | + retry_delay_seconds: float = Field( |
| 141 | + default=1.0, description="Delay between retry attempts" |
| 142 | + ) |
| 143 | + |
| 144 | + # === Human-Like Delays === |
| 145 | + enable_human_delays: bool = Field( |
| 146 | + default=False, |
| 147 | + description="Enable human-like delays for message processing to simulate realistic human behavior patterns", |
| 148 | + ) |
| 149 | + min_read_delay_seconds: float = Field( |
| 150 | + default=2.0, |
| 151 | + ge=0.0, |
| 152 | + description="Minimum delay before marking message as read (seconds). Simulates time to read incoming messages.", |
| 153 | + ) |
| 154 | + max_read_delay_seconds: float = Field( |
| 155 | + default=15.0, |
| 156 | + ge=0.0, |
| 157 | + description="Maximum delay before marking message as read (seconds). Prevents excessively long read delays.", |
| 158 | + ) |
| 159 | + min_typing_delay_seconds: float = Field( |
| 160 | + default=3.0, |
| 161 | + ge=0.0, |
| 162 | + description="Minimum delay before sending response (seconds). Simulates time to compose a response.", |
| 163 | + ) |
| 164 | + max_typing_delay_seconds: float = Field( |
| 165 | + default=45.0, |
| 166 | + ge=0.0, |
| 167 | + description="Maximum delay before sending response (seconds). Prevents excessively long typing delays.", |
| 168 | + ) |
| 169 | + min_send_delay_seconds: float = Field( |
| 170 | + default=0.5, |
| 171 | + ge=0.0, |
| 172 | + description="Minimum delay before message transmission (seconds). Simulates final message review time.", |
| 173 | + ) |
| 174 | + max_send_delay_seconds: float = Field( |
| 175 | + default=4.0, |
| 176 | + ge=0.0, |
| 177 | + description="Maximum delay before message transmission (seconds). Prevents excessively long send delays.", |
| 178 | + ) |
| 179 | + enable_delay_jitter: bool = Field( |
| 180 | + default=True, |
| 181 | + description="Enable random variation (±20%) in delay calculations to prevent detectable patterns and simulate natural human behavior variability", |
| 182 | + ) |
| 183 | + batch_read_compression_factor: float = Field( |
| 184 | + default=0.7, |
| 185 | + ge=0.1, |
| 186 | + le=1.0, |
| 187 | + description="Compression factor (0.1-1.0) applied to batch read delays. Lower values simulate faster batch reading (e.g., 0.7 = 30% faster than reading individually)", |
| 188 | + ) |
0 commit comments