code: def _get_max_applied_events_for_max_history(
      tracker: DialogueStateTracker, max_history: Optional[int]
  ) -> Optional[int]:
      """Computes the number of events in the tracker that correspond to max_history.
  
      To ensure that the last user utterance is correctly included in the prediction
      states, return the index of the most recent `action_listen` event occuring
      before the tracker would be truncated according to the value of `max_history`.
  
      Args:
          tracker: Some tracker holding the events
          max_history: The number of actions to count
  
      Returns:
          The number of events, as counted from the end of the event list, that should
          be taken into accout according to the `max_history` setting. If all events
          should be taken into account, the return value is `None`.
      """
      if not max_history:
          return None
      num_events = 0
      num_actions = 0
      for event in reversed(tracker.applied_events()):
          num_events += 1
          if isinstance(event, ActionExecuted):
              num_actions += 1
              if (
                  num_actions > max_history
                  and event.action_name == ACTION_LISTEN_NAME
              ):
                  return num_events
      return None
  
imports: from __future__ import annotations
  import copy
  import zlib
  import base64
  import json
  import logging
  import structlog
  from tqdm import tqdm
  from typing import Optional, Any, Dict, List, Text
  from pathlib import Path
  import rasa.utils.io
  import rasa.shared.utils.io
  from rasa.engine.graph import ExecutionContext
  from rasa.engine.recipes.default_recipe import DefaultV1Recipe
  from rasa.engine.storage.resource import Resource
  from rasa.engine.storage.storage import ModelStorage
  from rasa.shared.core.domain import State, Domain
  from rasa.shared.core.events import ActionExecuted
  from rasa.core.featurizers.tracker_featurizers import TrackerFeaturizer
  from rasa.core.featurizers.tracker_featurizers import MaxHistoryTrackerFeaturizer
  from rasa.core.featurizers.tracker_featurizers import FEATURIZER_FILE
  from rasa.shared.exceptions import FileIOException
  from rasa.core.policies.policy import PolicyPrediction, Policy, SupportedData
  from rasa.shared.core.trackers import DialogueStateTracker
  from rasa.shared.core.generator import TrackerWithCachedStates
  from rasa.shared.utils.io import is_logging_disabled
  from rasa.core.constants import MEMOIZATION_POLICY_PRIORITY, DEFAULT_MAX_HISTORY, POLICY_MAX_HISTORY, POLICY_PRIORITY
  from rasa.shared.core.constants import ACTION_LISTEN_NAME
label_desc: Use image node_id 1 for calling a global function with example usage: _get_max_applied_events_for_max_history(tracker, max_history) and returns: None, None, num_events