File size: 2,463 Bytes
1b7e88c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
import datetime
import inspect
import re
from abc import ABC, abstractmethod
from pathlib import Path
from time import time

from omagent_core.base import BotBase
from pydantic import model_validator

from ..utils.error import VQLError
from ..utils.logger import logging


class CallbackBase(BotBase, ABC):
    bot_id: str
    start_time: str = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
    # folder_name: str = f"./running_logs/{start_time}"

    class Config:
        """Configuration for this pydantic object."""

        arbitrary_types_allowed = True
        extra = "allow"

    # @model_validator(mode="after")
    # def init_folder(self):
    #     Path(self.folder_name).mkdir(parents=True, exist_ok=True)

    @abstractmethod
    def send_block(self, **kwargs):
        pass

    @abstractmethod
    def send_answer(self, **kwargs):
        pass

    @abstractmethod
    def info(self, **kwargs):
        pass

    @abstractmethod
    def error(self, **kwargs):
        pass

    @abstractmethod
    def finish(self, **kwargs):
        pass

    def filter_special_symbols_in_msg(self, msg):
        msg = re.sub(r"[-*#]", "", msg)
        return msg

    def remove_duplicates(self, sorted_list):
        if not sorted_list:
            return []

        result = [sorted_list[0]]
        for item in sorted_list[1:]:
            if item != result[-1]:
                result.append(item)
        return result[::-1]

    def get_calling_class(self):
        stack = inspect.stack()
        # Skipping the first two frames: current frame and the frame of get_calling_class_name
        calling_chain = self.remove_duplicates(
            [
                each.frame.f_locals.get("self").__class__.__name__
                for each in stack[2:]
                if isinstance(each.frame.f_locals.get("self"), BotBase)
            ]
        )
        for frame_info in stack[2:]:
            frame = frame_info.frame
            # Check for 'self' in local variables to identify the caller object
            self_obj = frame.f_locals.get("self")
            if self_obj:
                return self_obj, calling_chain
        return None, None


class TestCallback(CallbackBase):
    bot_id: str = ""
    start_time: int = time()

    def info(self, *args, **kwargs):
        logging.debug("Callback message [{}] | [{}]".format(args, kwargs))

    def error(self, error: VQLError):
        logging.error("Error message [{}]".format(error))