def on_message()

in mephisto/abstractions/architects/router/flask/mephisto_flask_blueprint.py [0:0]


    def on_message(self, message: str) -> None:
        """
        Determine the type of message, and then handle via the correct handler
        """
        if message is None:
            return

        state = self.mephisto_state
        current_client = self.ws.handler.active_client
        client = current_client
        packet = json.loads(message)
        if packet["packet_type"] == PACKET_TYPE_REQUEST_AGENT_STATUS:
            debug_log("Mephisto requesting status")
            self._handle_get_agent_status(packet)
        elif packet["packet_type"] == PACKET_TYPE_AGENT_ACTION:
            debug_log("Agent action: ", packet)
            self._handle_forward(packet)
            if packet["receiver_id"] == SYSTEM_CHANNEL_ID:
                self._update_wanted_acts(packet["sender_id"], False)
                self._send_status_for_agent(packet["sender_id"])
        elif packet["packet_type"] == PACKET_TYPE_ERROR_LOG:
            self._handle_forward(packet)
        elif packet["packet_type"] == PACKET_TYPE_ALIVE:
            debug_log("Agent alive: ", packet)
            self._handle_alive(self.ws.handler.active_client, packet)
        elif packet["packet_type"] == PACKET_TYPE_UPDATE_AGENT_STATUS:
            debug_log("Update agent status", packet)
            self._handle_update_local_status(packet)
            packet["data"]["state"] = self._get_agent_state(packet["receiver_id"])
            self._handle_forward(packet)
        elif packet["packet_type"] == PACKET_TYPE_REQUEST_ACTION:
            debug_log("Requesting act", packet)
            agent_id = packet["receiver_id"]
            self._update_wanted_acts(agent_id, True)
            self._send_status_for_agent(agent_id)
        elif packet["packet_type"] in [
            PACKET_TYPE_PROVIDER_DETAILS,
            PACKET_TYPE_INIT_DATA,
        ]:
            request_id = packet["data"].get("request_id")
            if request_id is None:
                request_id = packet["receiver_id"]
            res_event = state.pending_provider_requests.get(request_id)
            if res_event is not None:
                state.received_provider_responses[request_id] = packet
                del state.pending_provider_requests[request_id]
        elif packet["packet_type"] == PACKET_TYPE_HEARTBEAT:
            packet["data"] = {"last_mephisto_ping": js_time(state.last_mephisto_ping)}
            agent_id = packet["sender_id"]
            packet["sender_id"] = packet["receiver_id"]
            packet["receiver_id"] = agent_id
            agent = state.agent_id_to_agent.get(agent_id)
            if agent is not None:
                agent.is_alive = True
                packet["data"]["status"] = agent.status
                packet["data"]["state"] = agent.state
                if state.agent_id_to_client.get(agent.agent_id) != client:
                    # Not communicating to the correct socket, update
                    debug_log("Updating client for ", agent)
                    state.agent_id_to_client[agent.agent_id] = client
                    state.client_id_to_agent[client.mephisto_id] = agent
            self._handle_forward(packet)
        else:
            debug_log("Unknown message", packet)