(self, request: Request, call_next: Callable[[Request], Awaitable[Response]])
| 605 | """ |
| 606 | |
| 607 | async def dispatch(self, request: Request, call_next: Callable[[Request], Awaitable[Response]]) -> Response: |
| 608 | # Only process POST requests to completion endpoints |
| 609 | if request.method != "POST": |
| 610 | return await call_next(request) |
| 611 | |
| 612 | # Check if it's a chat completions or messages endpoint |
| 613 | endpoint_format: Literal["openai", "anthropic", "unknown"] = "unknown" |
| 614 | if request.url.path.endswith("/chat/completions") or "/chat/completions?" in request.url.path: |
| 615 | endpoint_format = "openai" |
| 616 | elif request.url.path.endswith("/messages") or "/messages?" in request.url.path: |
| 617 | endpoint_format = "anthropic" |
| 618 | else: |
| 619 | endpoint_format = "unknown" |
| 620 | |
| 621 | if endpoint_format == "unknown": |
| 622 | # Directly bypass the middleware |
| 623 | return await call_next(request) |
| 624 | |
| 625 | # Read the request body |
| 626 | try: |
| 627 | json_body = await request.json() |
| 628 | except json.JSONDecodeError: |
| 629 | logger.warning(f"Request body is not valid JSON: {request.body}") |
| 630 | return await call_next(request) |
| 631 | |
| 632 | # Check if streaming is requested |
| 633 | is_streaming = json_body.get("stream", False) |
| 634 | |
| 635 | # Simple case: no streaming requested, just return the response |
| 636 | if not is_streaming: |
| 637 | return await call_next(request) |
| 638 | |
| 639 | # Now the stream case |
| 640 | return await self._handle_stream_case(request, json_body, endpoint_format, call_next) |
| 641 | |
| 642 | async def _handle_stream_case( |
| 643 | self, |
nothing calls this directly
no test coverage detected