MCPcopy Index your code
hub / github.com/su-kaka/gcli2api / stream_request

Function stream_request

src/api/antigravity.py:288–553  ·  view source on GitHub ↗

流式请求函数 Args: body: 请求体 native: 是否返回原生bytes流,False则返回str流 headers: 额外的请求头 Yields: Response对象(错误时)或 bytes流/str流(成功时)

(
    body: Dict[str, Any],
    native: bool = False,
    headers: Optional[Dict[str, str]] = None,
)

Source from the content-addressed store, hash-verified

286# ==================== 新的流式和非流式请求函数 ====================
287
288async def stream_request(
289 body: Dict[str, Any],
290 native: bool = False,
291 headers: Optional[Dict[str, str]] = None,
292):
293 """
294 流式请求函数
295
296 Args:
297 body: 请求体
298 native: 是否返回原生bytes流,False则返回str流
299 headers: 额外的请求头
300
301 Yields:
302 Response对象(错误时)或 bytes流/str流(成功时)
303 """
304 model_name = body.get("model", "")
305
306 # 1. 获取有效凭证
307 cred_result = await credential_manager.get_valid_credential(
308 mode="antigravity", model_name=model_name
309 )
310
311 if not cred_result:
312 # 如果返回值是None,直接返回错误500
313 log.error("[ANTIGRAVITY STREAM] 当前无可用凭证")
314 yield Response(
315 content=json.dumps({"error": "当前无可用凭证"}),
316 status_code=500,
317 media_type="application/json"
318 )
319 return
320
321 current_file, credential_data = cred_result
322 access_token = credential_data.get("access_token") or credential_data.get("token")
323 project_id = credential_data.get("project_id", "")
324
325 if not access_token:
326 log.error(f"[ANTIGRAVITY STREAM] No access token in credential: {current_file}")
327 yield Response(
328 content=json.dumps({"error": "凭证中没有访问令牌"}),
329 status_code=500,
330 media_type="application/json"
331 )
332 return
333
334 # 2. 构建URL和请求头
335 antigravity_url = await get_antigravity_api_url()
336 target_url = f"{antigravity_url}/v1internal:streamGenerateContent?alt=sse"
337
338 auth_headers = build_antigravity_headers(access_token)
339
340 # 合并自定义headers
341 if headers:
342 auth_headers.update(headers)
343
344 # 构建 CLI 格式请求体
345 inner_request = body.get("request", body)

Callers 10

stream_request_wrapperFunction · 0.90
normal_stream_generatorFunction · 0.90
stream_request_wrapperFunction · 0.90
normal_stream_generatorFunction · 0.90
stream_request_wrapperFunction · 0.90
normal_stream_generatorFunction · 0.90
non_stream_requestFunction · 0.70

Calls 15

get_antigravity_api_urlFunction · 0.90
get_retry_configFunction · 0.90
get_auto_ban_error_codesFunction · 0.90
stream_post_asyncFunction · 0.90
parse_and_log_cooldownFunction · 0.90
record_api_call_errorFunction · 0.90
handle_error_with_retryFunction · 0.90
record_api_call_successFunction · 0.90
wrap_cli_requestFunction · 0.85
get_valid_credentialMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected