MCPcopy Create free account
hub / github.com/CJackHwang/AIstudioProxyAPI / _process_request_refactored

Function _process_request_refactored

api_utils/request_processor.py:727–975  ·  view source on GitHub ↗

Core Request Processing Function - Refactored Version

(
    req_id: str,
    request: ChatCompletionRequest,
    http_request: Request,
    result_future: Future,
)

Source from the content-addressed store, hash-verified

725
726
727async def _process_request_refactored(
728 req_id: str,
729 request: ChatCompletionRequest,
730 http_request: Request,
731 result_future: Future,
732) -> Optional[Tuple[Event, Locator, Callable[[str], bool]]]:
733 """Core Request Processing Function - Refactored Version"""
734 from api_utils.server_state import state
735
736 logger = state.logger
737
738 # 0. Check Auth Rotation Lock
739 if not GlobalState.AUTH_ROTATION_LOCK.is_set():
740 logger.info(f"[{req_id}] Request held: Waiting for auth rotation...")
741 await GlobalState.AUTH_ROTATION_LOCK.wait()
742 logger.info(f"[{req_id}] ▶️ Resuming after Auth Rotation.")
743
744 # [GR-03] Pre-Flight Graceful Rotation Check
745 if GlobalState.NEEDS_ROTATION:
746 logger.info(f"[{req_id}] 🔄 Graceful Rotation Pending. Initiating rotation...")
747 from api_utils.server_state import state
748
749 current_model_id = state.current_ai_studio_model_id
750 from browser_utils.auth_rotation import perform_auth_rotation
751
752 if await perform_auth_rotation(target_model_id=current_model_id):
753 GlobalState.NEEDS_ROTATION = False
754 logger.info(f"[{req_id}] ✅ Pre-flight rotation complete.")
755
756 is_connected = await _test_client_connection(req_id, http_request)
757 if not is_connected:
758 logger.info(f"[{req_id}] Client disconnected before processing.")
759 if not result_future.done():
760 result_future.set_exception(
761 HTTPException(status_code=499, detail="Client disconnected")
762 )
763 return None
764
765 stream_port = get_environment_variable("STREAM_PORT")
766 use_stream = stream_port != "0"
767 if use_stream:
768 try:
769 from api_utils import clear_stream_queue
770
771 await clear_stream_queue()
772 except asyncio.CancelledError:
773 raise
774 except Exception as clear_err:
775 logger.warning(f"[Stream] Error clearing queue: {clear_err}")
776
777 context = await _initialize_request_context(req_id, request)
778 context = await _analyze_model_requirements(req_id, context, request)
779
780 (
781 _,
782 disconnect_check_task,
783 check_client_disconnected,
784 ) = await _setup_disconnect_monitoring(req_id, http_request, result_future)

Calls 15

adjust_parametersMethod · 0.95
submit_promptMethod · 0.95
perform_auth_rotationFunction · 0.90
get_environment_variableFunction · 0.90
clear_stream_queueFunction · 0.90
PageControllerClass · 0.90
log_contextFunction · 0.90
save_error_snapshotFunction · 0.90
_test_client_connectionFunction · 0.85
_validate_page_statusFunction · 0.85