2424
2525logger = logging .getLogger (__name__ )
2626
27- # Maximum number of recent turns to keep screenshots for
28- MAX_RECENT_TURN_WITH_SCREENSHOTS = 3
29-
3027# Predefined Gemini computer use functions
3128PREDEFINED_COMPUTER_USE_FUNCTIONS = [
3229 "open_web_browser" ,
@@ -110,6 +107,12 @@ def __init__(
110107 self .excluded_predefined_functions = excluded_predefined_functions or []
111108 self .hud_console = HUDConsole (logger = logger )
112109
110+ # Context management: Maximum number of recent turns to keep screenshots for
111+ # Configurable via GEMINI_MAX_RECENT_TURN_WITH_SCREENSHOTS environment variable
112+ self .max_recent_turn_with_screenshots = (
113+ computer_settings .GEMINI_MAX_RECENT_TURN_WITH_SCREENSHOTS
114+ )
115+
113116 self .model_name = self .model
114117
115118 # Track mapping from Gemini tool names to MCP tool names
@@ -458,7 +461,7 @@ def _convert_tools_for_gemini(self) -> list[genai_types.Tool]:
458461 def _remove_old_screenshots (self , messages : list [genai_types .Content ]) -> None :
459462 """
460463 Remove screenshots from old turns to manage context length.
461- Keeps only the last MAX_RECENT_TURN_WITH_SCREENSHOTS turns with screenshots.
464+ Keeps only the last N turns with screenshots (configured via self.max_recent_turn_with_screenshots) .
462465 """
463466 turn_with_screenshots_found = 0
464467
@@ -478,7 +481,7 @@ def _remove_old_screenshots(self, messages: list[genai_types.Content]) -> None:
478481 if has_screenshot :
479482 turn_with_screenshots_found += 1
480483 # Remove the screenshot image if the number of screenshots exceeds the limit
481- if turn_with_screenshots_found > MAX_RECENT_TURN_WITH_SCREENSHOTS :
484+ if turn_with_screenshots_found > self . max_recent_turn_with_screenshots :
482485 for part in content .parts :
483486 if (
484487 part .function_response
0 commit comments