Wait for response completion
(
page: AsyncPage,
prompt_textarea_locator: Locator,
submit_button_locator: Locator,
edit_button_locator: Locator,
req_id: str,
check_client_disconnected_func: Callable,
current_chat_id: Optional[str],
prompt_length: int,
initial_wait_ms=INITIAL_WAIT_MS_BEFORE_POLLING,
timeout: Optional[float] = None,
)
| 1076 | |
| 1077 | |
| 1078 | async def _wait_for_response_completion( |
| 1079 | page: AsyncPage, |
| 1080 | prompt_textarea_locator: Locator, |
| 1081 | submit_button_locator: Locator, |
| 1082 | edit_button_locator: Locator, |
| 1083 | req_id: str, |
| 1084 | check_client_disconnected_func: Callable, |
| 1085 | current_chat_id: Optional[str], |
| 1086 | prompt_length: int, |
| 1087 | initial_wait_ms=INITIAL_WAIT_MS_BEFORE_POLLING, |
| 1088 | timeout: Optional[float] = None, |
| 1089 | ) -> bool: |
| 1090 | """Wait for response completion""" |
| 1091 | from playwright.async_api import TimeoutError |
| 1092 | |
| 1093 | # [FIX-03] Dynamic TTFB Timeout - Rotation Aware |
| 1094 | if timeout is None: |
| 1095 | base_timeout_seconds = 5 + (prompt_length / 1000.0) |
| 1096 | |
| 1097 | # Rotation-aware adjustments |
| 1098 | if GlobalState.IS_QUOTA_EXCEEDED: |
| 1099 | rotation_overhead = 20 # 20 second overhead for rotation |
| 1100 | timeout_seconds = max( |
| 1101 | base_timeout_seconds + rotation_overhead, 30 |
| 1102 | ) # Minimum 30s during rotation |
| 1103 | logger.info( |
| 1104 | f"[{req_id}] (WaitV3) Rotation detected - applying extended timeout: {timeout_seconds:.2f}s" |
| 1105 | ) |
| 1106 | else: |
| 1107 | timeout_seconds = max(base_timeout_seconds, 10) |
| 1108 | timeout_seconds = min(timeout_seconds, 120) |
| 1109 | |
| 1110 | if prompt_length > 5000: |
| 1111 | timeout_seconds *= 1.5 |
| 1112 | logger.info( |
| 1113 | f"[{req_id}] (WaitV3) Large prompt detected - extending timeout by 50%: {timeout_seconds:.2f}s" |
| 1114 | ) |
| 1115 | else: |
| 1116 | timeout_seconds = timeout |
| 1117 | if GlobalState.IS_QUOTA_EXCEEDED and timeout_seconds < 30: |
| 1118 | timeout_seconds = 30 |
| 1119 | logger.info( |
| 1120 | f"[{req_id}] (WaitV3) Rotation detected - enforcing minimum timeout: {timeout_seconds:.2f}s" |
| 1121 | ) |
| 1122 | |
| 1123 | _timeout_ms = timeout_seconds * 1000 |
| 1124 | |
| 1125 | logger.info( |
| 1126 | f"[{req_id}] (WaitV3) Waiting for response completion... (Dynamic Timeout: {timeout_seconds:.2f}s)" |
| 1127 | ) |
| 1128 | await asyncio.sleep(initial_wait_ms / 1000) |
| 1129 | |
| 1130 | start_time = time.time() |
| 1131 | |
| 1132 | from config import UI_GENERATION_WAIT_TIMEOUT_MS |
| 1133 | |
| 1134 | wait_timeout_ms_short = UI_GENERATION_WAIT_TIMEOUT_MS |
| 1135 |