AIstudioProxyAPI / api_utils /page_response.py
peijun1's picture
Deploy AI Studio Proxy API to Hugging Face Spaces
a5784e9
Raw
History Blame Contribute Delete
1.52 kB
import asyncio
import logging
from typing import Callable
from playwright.async_api import Error as PlaywrightAsyncError
from playwright.async_api import Page as AsyncPage
from playwright.async_api import expect as expect_async
from config import RESPONSE_CONTAINER_SELECTOR, RESPONSE_TEXT_SELECTOR
async def locate_response_elements(
page: AsyncPage,
req_id: str,
logger: logging.Logger,
check_client_disconnected: Callable[[str], bool],
) -> None:
"""Locate response container and text elements, including timeout and error handling."""
logger.info(f"[{req_id}] Locating response elements...")
response_container = page.locator(RESPONSE_CONTAINER_SELECTOR).last
response_element = response_container.locator(RESPONSE_TEXT_SELECTOR)
try:
await expect_async(response_container).to_be_attached(timeout=20000)
check_client_disconnected("After Response Container Attached: ")
await expect_async(response_element).to_be_attached(timeout=90000)
logger.info(f"[{req_id}] Response elements located.")
except (PlaywrightAsyncError, asyncio.TimeoutError) as locate_err:
from .error_utils import upstream_error
raise upstream_error(
req_id, f"Failed to locate AI Studio response elements: {locate_err}"
)
except Exception as locate_exc:
from .error_utils import server_error
raise server_error(
req_id, f"Unexpected error while locating response elements: {locate_exc}"
)