@@ -53,8 +53,9 @@ async def compact_messages_for_request(
5353 return messages , None , False
5454
5555 messages , previous_summary = _apply_latest_summary_checkpoint (messages )
56+ token_threshold = _resolve_token_threshold (config ['token_threshold' ], metadata )
5657 if (
57- not _exceeds_token_threshold (messages , system_prompt , previous_summary , config [ ' token_threshold' ] )
58+ not _exceeds_token_threshold (messages , system_prompt , previous_summary , token_threshold )
5859 or len (messages ) <= 3
5960 ):
6061 return messages , previous_summary , False
@@ -147,6 +148,21 @@ async def _load_config() -> dict:
147148 }
148149
149150
151+ def _parse_positive_int (value : Any ) -> int | None :
152+ try :
153+ parsed = int (value )
154+ except (TypeError , ValueError ):
155+ return None
156+ return parsed if parsed > 0 else None
157+
158+
159+ def _resolve_token_threshold (global_threshold : int , metadata : dict ) -> int :
160+ configured_threshold = _parse_positive_int ((metadata .get ('params' ) or {}).get ('compact_token_threshold' ))
161+ if configured_threshold is None :
162+ return global_threshold
163+ return min (configured_threshold , global_threshold )
164+
165+
150166def _apply_latest_summary_checkpoint (messages : list [dict ]) -> tuple [list [dict ], str | None ]:
151167 summary = None
152168 summary_idx = None
0 commit comments