code: def _trim_tracker_by_max_history(
      tracker: DialogueStateTracker, max_history: Optional[int]
  ) -> DialogueStateTracker:
      """Removes events from the tracker until it has `max_history` actions.
  
      Args:
          tracker: Some tracker.
          max_history: Number of actions to keep.
  
      Returns:
          A new tracker with up to `max_history` actions, or the same tracker if
          `max_history` is `None`.
      """
      max_applied_events = _get_max_applied_events_for_max_history(
          tracker, max_history
      )
      if not max_applied_events:
          return tracker
  
      applied_events = tracker.applied_events()[-max_applied_events:]
      new_tracker = tracker.init_copy()
      for event in applied_events:
          new_tracker.update(event)
      return new_tracker
  
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 2 for calling a global function with example usage: _trim_tracker_by_max_history(tracker, max_history) and returns: new_tracker, tracker