File size: 6,618 Bytes
57b6463
 
1f02088
67d40c9
a6e7bec
 
 
 
 
 
 
1f02088
 
67d40c9
1f02088
67d40c9
 
a6e7bec
 
1f02088
67d40c9
a6e7bec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f02088
a6e7bec
 
91b6402
 
a6e7bec
 
 
 
 
 
 
 
91b6402
a6e7bec
 
 
 
 
 
91b6402
1f02088
57b6463
a6e7bec
 
 
 
 
57b6463
 
a6e7bec
 
 
 
57b6463
1f02088
 
 
a6e7bec
 
 
 
 
1f02088
a6e7bec
 
 
 
1f02088
 
 
a6e7bec
 
 
 
 
1f02088
a6e7bec
 
 
 
1f02088
 
 
 
a6e7bec
 
 
 
1f02088
 
 
 
a6e7bec
 
 
 
1f02088
 
 
 
 
 
 
 
a6e7bec
 
 
 
 
 
1f02088
 
 
a6e7bec
 
 
 
1f02088
 
 
 
 
 
 
a6e7bec
 
 
 
1f02088
 
 
 
 
 
a6e7bec
 
 
 
1f02088
 
 
 
 
a6e7bec
 
 
 
 
1f02088
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from flask import jsonify, request

from config.logger_config import configure_logging

from services.encoding_services import EncodingServices

from app_constants.logging_enums import ApplicationLayerLogging, LogEvent, LoggingComponent



from services.profile_assistant_services import ProfileAssistantService

logger = configure_logging()
class ProfileAssistantController:

    def __init__(self):
        self.service = ProfileAssistantService()
        self.encoding_service = EncodingServices()
        

    def initialize_profile_assistant(self):
        """Initializes Profile Assistant Repository."""

        logger.info(
            "[%s] [%s] initializing profile assistant [%s]",
            ApplicationLayerLogging.CONTROLLER.value,
            LoggingComponent.PROFILE_ASSISTANT.value,
            LogEvent.STARTED.value,
        )

        result = self.service.init_profile_assistant_repo()

        logger.info(
            "[%s] [%s] profile assistant initialization completed [%s]",
            ApplicationLayerLogging.CONTROLLER.value,
            LoggingComponent.PROFILE_ASSISTANT.value,
            LogEvent.COMPLETED.value,
        )

        return result
    
    
    def get_name(self):
        """Retrieving name of the profile assistant"""
        logger.info(
            "[%s] [%s] retrieving profile assistant name  [%s]",
            ApplicationLayerLogging.CONTROLLER.value,
            LoggingComponent.PROFILE_ASSISTANT.value,
            LogEvent.STARTED.value,
        )
        
        result = self.service.fetch_name()
        logger.info(
            "[%s] [%s] profile assistant initialization completed [%s]",
            ApplicationLayerLogging.CONTROLLER.value,
            LoggingComponent.PROFILE_ASSISTANT.value,
            LogEvent.COMPLETED.value,
        )
        return jsonify(result),200

    def initialize_chat(self):
        """ Initializing a new chat with profile assistant"""
        logger.info("[%s] [%s] initializing new chat with profile assistant [%s]", 
                    ApplicationLayerLogging.CONTROLLER.value,
                    LoggingComponent.PROFILE_ASSISTANT.value,
                    LogEvent.STARTED.value)

        result = self.service.init_new_chat()
        logger.info("[%s] [%s] initializing new chat with profile assistant  completed [%s]",
                    ApplicationLayerLogging.CONTROLLER.value,
                    LoggingComponent.PROFILE_ASSISTANT.value,
                    LogEvent.COMPLETED.value)

        return jsonify(result), 201
    
    def enable_voice(self):
        '''Enabling voice chat with profile assistant'''
        logger.info("[%s] [%s] enabling voice chat [%s]", 
                    ApplicationLayerLogging.CONTROLLER.value,
                    LoggingComponent.PROFILE_ASSISTANT.value,
                    LogEvent.STARTED.value)
        result = self.service.enable_speech_mode()
        logger.info("[%s] [%s] enabling voice chat completed [%s]",
                    ApplicationLayerLogging.CONTROLLER.value,
                    LoggingComponent.PROFILE_ASSISTANT.value,
                    LogEvent.COMPLETED.value)
        return jsonify(result), 200
    
    def disable_voice(self):
        '''Disabling voice chat with profile assistant'''
        logger.info("[%s] [%s]  disabling voice chat [%s]", 
                    ApplicationLayerLogging.CONTROLLER.value,
                    LoggingComponent.PROFILE_ASSISTANT.value,
                    LogEvent.COMPLETED.value)        
        result = self.service.disable_speech_mode()
        logger.info("[%s] [%s] disabling voice chat completed [%s]",
                    ApplicationLayerLogging.CONTROLLER.value,
                    LoggingComponent.PROFILE_ASSISTANT.value,
                    LogEvent.COMPLETED.value)
        return jsonify(result), 200
    

    def start_chat(self):
        logger.info("[%s] [%s] conversation with profile assistant [%s]",
                    ApplicationLayerLogging.CONTROLLER.value,
                    LoggingComponent.PROFILE_ASSISTANT.value,
                    LogEvent.STARTED.value)

        data = request.get_json(silent=True)

        if not data:
            logger.warning("[%s] [%s] request body is missing or invalid JSON [%s]",
                           ApplicationLayerLogging.CONTROLLER.value,
                           LoggingComponent.PROFILE_ASSISTANT.value,
                           LogEvent.FAILED.value)
            return jsonify({
                "success": False,
                "message": "Request body is required."
            }), 400


        chat_id = data.get("chat_id")
        text = data.get("text")
        logger.debug("[%s] [%s] profile assistant received data : [%s] [%s] [%s]",
                     ApplicationLayerLogging.CONTROLLER.value,
                     LoggingComponent.PROFILE_ASSISTANT.value,
                     self.encoding_service.fernet_encoding(chat_id),
                     self.encoding_service.fernet_encoding(text),
                     LogEvent.RECEIVED.value)

        if not chat_id:
            logger.warning(
                "[%s] [%s] received data missing chat id [%s]",
                ApplicationLayerLogging.CONTROLLER.value,
                LoggingComponent.PROFILE_ASSISTANT.value,
                LogEvent.FAILED.value)
            return jsonify({
                "success": False,
                "message": "Chat ID is required."
            }), 400

        if not text or not text.strip():
            logger.warning(
                "[%s] [%s] received data missing text [%s]",
                ApplicationLayerLogging.CONTROLLER.value,
                LoggingComponent.PROFILE_ASSISTANT.value,
                LogEvent.FAILED.value)
            return jsonify({
                "success": False,
                "message": "Text cannot be empty."
            }), 400
            
        
        logger.info("[%s] [%s] service call [%s]",
                    ApplicationLayerLogging.CONTROLLER.value,
                    LoggingComponent.PROFILE_ASSISTANT.value,
                    LogEvent.CALLED.value)
        result = self.service.start_chat(
            chat_id,
            text.strip()
        )
        
        logger.info("[%s] [%s] profile assistant response : [%s] [%s] [%s]",
                    ApplicationLayerLogging.CONTROLLER.value,
                    LoggingComponent.PROFILE_ASSISTANT.value,
                    self.encoding_service.fernet_encoding(chat_id),
                    self.encoding_service.fernet_encoding(result["message"]))

        return jsonify(result), 200