aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/infini/input.py
blob: 2a926e2a96df8007a706af8994aa03d9d90d6f68 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from infini.typing import Literal, Optional
from infini.typing import Dict, Any, Generic, T, Optional
from infini.output import Output


class Input(Generic[T]):
    plain_data: T
    variables: Dict[str, Any]

    def __init__(
        self, plain_data: T, variables: Optional[Dict[str, Any]] = None
    ) -> None:
        self.plain_data = plain_data
        self.variables = variables or {}

    def get_user_id(self) -> str:
        return self.variables.get("user_id", "0")

    def get_session_id(self) -> str:
        return (
            self.variables.get("group_id")
            or self.variables.get("session_id")
            or self.variables.get("user_id")
            or "0"
        )

    def is_tome(self) -> bool:
        return bool(self.variables.get("is_tome"))

    def get_plain_text(self) -> str:
        return str(self.plain_data)

    def output(
        self,
        type: Literal["text", "workflow"],
        name: str,
        *,
        block: bool = False,
        variables: Dict[str, Any] = {},
    ):
        vars = self.variables.copy()
        vars.update(variables)
        return Output(type, name, block=block, variables=vars)