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

Function use_stream_response

api_utils/utils_ext/stream.py:17–464  ·  view source on GitHub ↗

Enhanced stream response handler with UI-based generation active checks.

(
    req_id: str,
    timeout: float = 5.0,
    silence_threshold: float = 60.0,
    page: Any = None,
    check_client_disconnected: Optional[Callable] = None,
    stream_start_time: float = 0.0,
    enable_silence_detection: bool = True,
)

Source from the content-addressed store, hash-verified

15
16
17async def use_stream_response(
18 req_id: str,
19 timeout: float = 5.0,
20 silence_threshold: float = 60.0,
21 page: Any = None,
22 check_client_disconnected: Optional[Callable] = None,
23 stream_start_time: float = 0.0,
24 enable_silence_detection: bool = True,
25) -> AsyncGenerator[Any, None]:
26 """Enhanced stream response handler with UI-based generation active checks."""
27 from api_utils.server_state import state
28
29 STREAM_QUEUE = state.STREAM_QUEUE
30 logger = state.logger
31 from browser_utils.page_controller import PageController
32 from config import (
33 CHAT_SESSION_CONTENT_SELECTOR,
34 LAST_CHAT_TURN_SELECTOR,
35 SCROLL_CONTAINER_SELECTOR,
36 UI_GENERATION_WAIT_TIMEOUT_MS,
37 )
38 from config.global_state import GlobalState
39 from models import (
40 ClientDisconnectedError,
41 QuotaExceededError,
42 UpstreamError,
43 )
44
45 set_request_id(req_id)
46 if STREAM_QUEUE is None:
47 logger.warning(f"[{req_id}] STREAM_QUEUE is None, cannot use stream response")
48 return
49
50 if stream_start_time == 0.0:
51 stream_start_time = time.time() - 10.0
52
53 accumulated_body = ""
54 accumulated_reason_len = 0
55 total_reason_processed = 0
56 total_body_processed = 0
57 boundary_transitions = 0
58 boundary_buffer = ""
59
60 acc_reason_state = ""
61 acc_body_state = ""
62 force_body_mode = False
63 split_index = -1
64 empty_count = 0
65 initial_wait_limit = int(timeout * 10)
66 silence_wait_limit = int(silence_threshold * 10)
67 max_empty_retries = max(silence_wait_limit, initial_wait_limit)
68 hard_timeout_limit = int(timeout * 10 * 3)
69
70 _data_received = False
71 has_content = False
72 has_seen_functions = False
73 received_items_count = 0
74 stale_done_ignored = False

Calls 15

set_request_idFunction · 0.90
QuotaExceededErrorClass · 0.90
UpstreamErrorClass · 0.90
PageControllerClass · 0.90
clear_stream_queueFunction · 0.85
warningMethod · 0.80
infoMethod · 0.80
errorMethod · 0.80