code: def _get_previous_event(
      state: Optional[State],
  ) -> Tuple[Optional[Text], Optional[Text]]:
      """Returns previous event type and name.
  
      Returns the type and name of the event (action or intent) previous to the
      given state (excluding action_listen).
  
      Args:
          state: Element of sliced states.
  
      Returns:
          Tuple of (type, name) strings of the prior event.
      """
      previous_event_type = None
      previous_event_name = None
  
      # A typical state might be
      # `{'user': {'intent': 'greet'}, 'prev_action': {'action_name': 'action_listen'}}`.
      if not state:
          previous_event_type = None
          previous_event_name = None
      elif (
          PREVIOUS_ACTION in state.keys()
          and "action_name" in state[PREVIOUS_ACTION]
          and state[PREVIOUS_ACTION]["action_name"]
          != ACTION_LISTEN_NAME
      ):
          previous_event_type = "action"
          previous_event_name = state[PREVIOUS_ACTION]["action_name"]
      elif (
          PREVIOUS_ACTION in state.keys()
          and "action_text" in state[PREVIOUS_ACTION]
      ):
          previous_event_type = "bot utterance"
          previous_event_name = state[PREVIOUS_ACTION]["action_text"]
      elif USER in state.keys():
          if "intent" in state[USER]:
              previous_event_type = "intent"
              previous_event_name = state[USER]["intent"]
          elif "text" in state[USER]:
              previous_event_type = "user utterance"
              previous_event_name = state[USER]["text"]
  
      if not isinstance(previous_event_name, (str, type(None))):
          # While the Substate type doesn't restrict the value of `action_text` /
          # `intent`, etc. to be a string, it always should be
          raise TypeError(
              f"The value '{previous_event_name}' in the substate should be a string or "
              f"None, not {type(previous_event_name)}. Did you modify Rasa source code?"
          )
  
      return previous_event_type, previous_event_name
  
imports: from collections import defaultdict
  import logging
  import json
  from typing import DefaultDict, Dict, Generator, List, NamedTuple, Optional, Text, Tuple
  from rasa.core.featurizers.tracker_featurizers import MaxHistoryTrackerFeaturizer
  from rasa.shared.core.constants import ACTION_LISTEN_NAME, PREVIOUS_ACTION, ACTION_UNLIKELY_INTENT_NAME, USER
  from rasa.shared.core.domain import Domain, State
  from rasa.shared.core.events import ActionExecuted, Event
  from rasa.shared.core.generator import TrackerWithCachedStates
  from rasa.nlu.tokenizers.tokenizer import Tokenizer
  from rasa.shared.nlu.constants import TEXT
  from rasa.shared.nlu.training_data.message import Message
label_desc: Use image node_id 7 for calling a global function with example usage: _get_previous_event(state) and returns: previous_event_type,  previous_event_name