处理 /responses 系列请求的核心逻辑
()
| 313 | |
| 314 | # ---- 路由处理 ---- |
| 315 | def _make_response(): |
| 316 | """处理 /responses 系列请求的核心逻辑""" |
| 317 | if request.method == "OPTIONS": |
| 318 | return Response() |
| 319 | |
| 320 | req_data = request.get_json(silent=True) or {} |
| 321 | messages, tools, tool_choice = extract_messages(req_data) |
| 322 | effective_model = req_data.get("model") or DEEPSEEK_MODEL |
| 323 | response_id = f"resp_{uuid.uuid4().hex[:12]}" |
| 324 | |
| 325 | if DEEPSEEK_DEBUG: |
| 326 | debug_path = request.path |
| 327 | with open(DEBUG_LOG, "a", encoding="utf-8") as f: |
| 328 | f.write(f"\n--- [{__import__('datetime').datetime.now()}] PATH={debug_path} ---\n") |
| 329 | f.write(f"Request body:\n{json.dumps(req_data, indent=2, ensure_ascii=False)}\n") |
| 330 | f.write(f"Messages:\n{json.dumps(messages, indent=2, ensure_ascii=False)}\n") |
| 331 | if tools: |
| 332 | f.write(f"Tools count: {len(tools)}\n") |
| 333 | f.write(f"Tool choice: {tool_choice}\n") |
| 334 | |
| 335 | def generate(): |
| 336 | if not messages: |
| 337 | yield "event: response.completed\n" |
| 338 | yield ( |
| 339 | "data: " |
| 340 | + json.dumps({ |
| 341 | "type": "response.completed", |
| 342 | "response": { |
| 343 | "id": response_id, "object": "response", |
| 344 | "status": "completed", "model": effective_model, |
| 345 | "output": [], "usage": {"input_tokens": 0, "output_tokens": 0, "total_tokens": 0}, |
| 346 | }, |
| 347 | }, ensure_ascii=False) |
| 348 | + "\n\n" |
| 349 | ) |
| 350 | return |
| 351 | |
| 352 | # response.created |
| 353 | yield "event: response.created\n" |
| 354 | yield ( |
| 355 | "data: " |
| 356 | + json.dumps({ |
| 357 | "type": "response.created", |
| 358 | "response": { |
| 359 | "id": response_id, "object": "response", |
| 360 | "status": "in_progress", "model": effective_model, |
| 361 | "output": [], "usage": None, |
| 362 | }, |
| 363 | }, ensure_ascii=False) |
| 364 | + "\n\n" |
| 365 | ) |
| 366 | |
| 367 | # response.in_progress |
| 368 | yield "event: response.in_progress\n" |
| 369 | yield ( |
| 370 | "data: " |
| 371 | + json.dumps({ |
| 372 | "type": "response.in_progress", |
nothing calls this directly
no test coverage detected