aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/infini/handler.py
blob: ae3561f894878a960002ead7082c891927052658 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from infini.input import Input
from infini.output import Output
from infini.typing import List, RouterType, Callable
from infini.queue import EventQueue


class Handler:
    handlers: List[RouterType]

    def input(self, input: Input):
        queue = self.match(input.get_plain_text())
        if queue.is_empty():
            yield Output.empty()
            return
        while not queue.is_empty():
            output = queue.pop()(input)
            yield output
            if output.block:
                return

    def match(self, text: str) -> EventQueue[Callable[[Input], Output]]:
        queue = EventQueue()

        for handler in self.handlers:
            if handler["router"].match(text):
                queue.push(handler["priority"], handler["handler"])

        return queue