Files
mab/docs/ContextVariables.md
nikita d5367ebc6b Big refactoring
- Changed event callback signature to `(ctx: EventContext, vars: ContextVars)`
- Changed event filter signature to `(ctx: EventContext, vars: ContextVars)`
- Partially fixed examples
- Refactored `src/mab/__init__.py` to be more structured
- Added support for local event context variables so that different filters don't mess with each other
- Refactored file structure for context variables
- Added support for combination of ContextVars
- `ContextVarsPopulator` does not look builders dynamically anymore; `ContextVarsPopulator.autobuilder` is used to mark function as a builder that must be automatically called
- `ContextVarsPopulator` does not required to be created anymore
- Added context variable `CTX_COMMAND`
- `MessageTypeFilter` supports message types passed as strings from now on
- `MessageHasFile` renamed to `MessageHasFileFilter`
- Added docs for context variables
2026-10-31 20:37:57 +03:00

2.9 KiB

For mab user

What is Context Variable?

Context Variables system is used to get rid of matrix-nio when possible. To keep it short let's just say that mab looks at the event returned by matrix-nio and tries to convert it to simplified pydantic model. Simplified model is more than enough in most cases but you can use raw events if needed.

How do Context Variables work?

From user's perspective the system is quite easy. Your event callback takes two positional arguments: ctx (of type EventContext) and vars (of type ContextVars). One should use ctx argument to access raw matrix-nio data and MatrixBot that has invoked the callback. vars should be used when you'd like to access simplified data model (e.g. get message body or attached file size). Example callback syntax that uses both:

from mab import *

...

async def callback(ctx: EventContext, vars: ContextVars) -> None:
    response = f"Your message text is '{vars[CTX_MESSAGE].body}'"
    await ctx.bot.send.text(ctx.room, response)
...

CTX_MESSAGE is so called variable key that you should use to access the context variable. If the key is not set then RuntimeError will be raised.

For mab developer

The following rules must be considered when adding new context variables or models:

  1. If a model can be added to existing subpackage of mab.context.vars then it should be added there
  2. If a model does not fit into any existing subpackage then a new subpackage must be introduced
  3. src/mab/context/vars/SUBPACKAGE/types.py must ALWAYS exist and define types related to the subpackage (e.g. message subpackage introduces Message model)
  4. src/mab/context/vars/SUBPACKAGE/keys.py must exist if the subpackage introduces keys used with ContextVars (e.g. message subpackage introduces CTX_MESSAGE key)
  5. src/mab/context/vars/SUBPACKAGE/builders.py must exist if the subpackage introduces so called autobuilders - functions decorated with the following syntax:
    @mab.context.vars.ContextVarsPopulator.autobuilder
    async def _example_autobuilder(ctx: EventContext, vars: ContextVars) -> None:
        vars[CTX_EXAMPLE] = Example(example=1)
    
    Functions decorated like this will be called when a new event is returned by matrix-nio.
  6. __init__.py of a subpackage must ALWAYS exist and have the following structure:
    from .types import *
    from .keys import *
    from . import builders # if exists
    
    The goal of __init__.py is to make sure that builders.py is imported and to introduce members of types.py and keys.py into its namespace.
  7. All subpackages must be imported in src/mab/__init__.py using the following syntax:
    from .context.vars.command import *
    from .context.vars.message import *
    

Please refer to src/mab/context/vars/ for examples