aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/infini/generator.py
blob: 81a6ab14859973145de4c08cdd7c5d10b1832040 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from infini.output import Output
from infini.typing import Dict, Callable
from infini.exceptions import UnknownEvent
from jinja2 import Template


class Generator:
    events: Dict[str, str]
    global_variables: Dict[str, str | Callable]

    def output(self, output: Output) -> str:
        assert output.type != "workflow", "Workflow 事件无法产出文本"
        template = self.match(output)
        return template.render(output.variables)

    def match(self, output: Output) -> Template:
        if context := self.events.get(output.name):
            return Template(context)
        raise UnknownEvent(f"事件不存在: {output.name}")