fsm — FSM Manager and Garbage Collector
BaseStateManager
BaseStateManager
Manages namespaced state persistence for individual bot features.
Each feature is allocated a dedicated sub-key (namespace) within the underlying FSM storage (e.g., Redis). This abstraction facilitates independent data management and prevents cross-feature state corruption.
Attributes:
| Name | Type | Description |
|---|---|---|
state |
The raw |
|
storage_key |
The resolved namespaced key used for persistent storage. |
Source code in src/codex_bot/fsm/state_manager.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
Functions
__init__(state, feature_key)
Initializes the BaseStateManager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
FSMContext
|
Instance of |
required |
feature_key
|
str
|
Unique identifier for the feature (e.g., "market", "quest"). |
required |
Source code in src/codex_bot/fsm/state_manager.py
28 29 30 31 32 33 34 35 36 | |
clear()
async
Completely removes the draft key from FSM storage.
Unlike resetting to {key: {}}, it completely removes the key,
leaving no empty "zombie dictionaries" in Redis.
Does not affect data of other features in the same FSM storage.
Source code in src/codex_bot/fsm/state_manager.py
68 69 70 71 72 73 74 75 | |
get_payload()
async
Returns all data of the feature draft.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with current data. Empty dictionary if no data exists. |
Source code in src/codex_bot/fsm/state_manager.py
38 39 40 41 42 43 44 45 46 | |
get_value(key, default=None)
async
Returns a specific value from the draft.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Field key. |
required |
default
|
Any
|
Default value if the key is not found. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Field value or default. |
Source code in src/codex_bot/fsm/state_manager.py
87 88 89 90 91 92 93 94 95 96 97 98 99 | |
set_value(key, value)
async
Sets one specific value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Field key. |
required |
value
|
Any
|
Value to save. |
required |
Source code in src/codex_bot/fsm/state_manager.py
77 78 79 80 81 82 83 84 85 | |
update(**kwargs)
async
Updates the draft with the passed fields (partial update).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Fields to update. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Updated dictionary with draft data. |
Example
await manager.update(name="Alice", age=30)
Source code in src/codex_bot/fsm/state_manager.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
GarbageStateRegistry
GarbageStateRegistry
Registry of FSM states for which the Garbage Collector is active.
When a user transitions to a state registered here, the system can automatically
delete messages tracked in the _garbage_messages FSM data key.
Example
GarbageStateRegistry.register(MyStates.input_form)
GarbageStateRegistry.register([MyStates.step1, MyStates.step2])
Source code in src/codex_bot/fsm/garbage_collector.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
Functions
is_garbage(state)
classmethod
Checks if a state is in the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str | None
|
Full state name string (e.g. "MyStates:step1"). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/codex_bot/fsm/garbage_collector.py
64 65 66 67 68 69 70 71 72 73 74 | |
register(state_or_group)
classmethod
Adds a state or a group of states to the registry for automatic cleaning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_or_group
|
RegisterableState
|
String state, State object, StatesGroup class, or a collection of these. |
required |
Source code in src/codex_bot/fsm/garbage_collector.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
registered_states()
classmethod
Returns a read-only view of registered states.
Source code in src/codex_bot/fsm/garbage_collector.py
76 77 78 79 | |
IsGarbageStateFilter
IsGarbageStateFilter
Bases: Filter
Aiogram filter to check if the current user state is registered for GC.
Used by the internal garbage collection middleware/handlers to decide when to trigger the cleaning process.
Source code in src/codex_bot/fsm/garbage_collector.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
Functions
__call__(event, state)
async
Checks the current state against the registry.
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/codex_bot/fsm/garbage_collector.py
89 90 91 92 93 94 95 96 | |
common_fsm_router
A ready-to-use router with a garbage-collector handler. Connect it to the main dispatcher:
from codex_bot.fsm import common_fsm_router
dp.include_router(common_fsm_router)