(self)
| 462 | self.send_error(HTTPStatus.NOT_FOUND) |
| 463 | |
| 464 | def do_POST(self) -> None: |
| 465 | if self.path != "/v1/responses": |
| 466 | self.send_error(HTTPStatus.NOT_FOUND) |
| 467 | return |
| 468 | request_id = uuid.uuid4().hex |
| 469 | length = int(self.headers.get("Content-Length") or "0") |
| 470 | raw_body = self.rfile.read(length) |
| 471 | try: |
| 472 | body = json.loads(raw_body.decode("utf-8")) |
| 473 | except json.JSONDecodeError: |
| 474 | self._send_error( |
| 475 | HTTPStatus.BAD_REQUEST, |
| 476 | {"ok": False, "message": "Invalid JSON payload."}, |
| 477 | ) |
| 478 | return |
| 479 | _append_jsonl( |
| 480 | self.config.request_log_path, |
| 481 | { |
| 482 | "request_id": request_id, |
| 483 | "received_at": _utc_now(), |
| 484 | "headers": dict(self.headers), |
| 485 | "body": body, |
| 486 | }, |
| 487 | ) |
| 488 | try: |
| 489 | upstream_payload = _call_upstream_chat_completion(self.config, body) |
| 490 | sse_payload = _build_sse_response(body, upstream_payload) |
| 491 | except HTTPError as exc: |
| 492 | message = exc.read().decode("utf-8", errors="replace") |
| 493 | _append_jsonl( |
| 494 | self.config.response_log_path, |
| 495 | { |
| 496 | "request_id": request_id, |
| 497 | "received_at": _utc_now(), |
| 498 | "upstream_http_error": { |
| 499 | "status": exc.code, |
| 500 | "message": message, |
| 501 | }, |
| 502 | }, |
| 503 | ) |
| 504 | self._send_error(exc.code, {"ok": False, "message": message or str(exc)}) |
| 505 | return |
| 506 | except URLError as exc: |
| 507 | self._send_error(HTTPStatus.BAD_GATEWAY, {"ok": False, "message": str(exc)}) |
| 508 | return |
| 509 | except Exception as exc: # pragma: no cover - runtime safeguard |
| 510 | _append_jsonl( |
| 511 | self.config.response_log_path, |
| 512 | { |
| 513 | "request_id": request_id, |
| 514 | "received_at": _utc_now(), |
| 515 | "internal_error": str(exc), |
| 516 | }, |
| 517 | ) |
| 518 | self._send_error(HTTPStatus.INTERNAL_SERVER_ERROR, {"ok": False, "message": str(exc)}) |
| 519 | return |
| 520 | |
| 521 | _write_response_log(self.config.response_log_path, request_id=request_id, payload=sse_payload) |
nothing calls this directly
no test coverage detected