Files
grok2api/app/dataplane/reverse/classifier.py
T
Chenyme b407443820 refactor: update configuration and improve account management features
- Removed unnecessary environment variable patterns from .gitignore.
- Renamed configuration keys in config.defaults.toml for clarity (function_enabled to webui_enabled, function_key to webui_key).
- Increased usage concurrency limit from 10 to 50 in AccountRefreshService.
- Updated docker-compose.yml to remove default Redis URL.
- Enhanced Dockerfile for better build efficiency and added health check.
- Removed deprecated aiofiles dependency and updated other dependencies in pyproject.toml.
- Deleted outdated readme.md file.
- Improved account management logic in various modules, including state machine and refresh service.
- Added support for new quota management features and improved logging for account refresh operations.
2026-04-10 00:19:25 +08:00

52 lines
1.3 KiB
Python

"""Reverse pipeline result classifier.
Maps upstream HTTP status codes and response bodies to a ResultCategory.
"""
from typing import Any
from app.dataplane.reverse.protocol.xai_usage import is_invalid_credentials_body
from .types import ResultCategory
def classify_result(
status_code: int,
body: str = "",
*,
payload: Any = None,
) -> ResultCategory:
"""Classify an upstream response into a ResultCategory.
``body`` is the raw response body (or first ~400 chars for error responses).
``payload`` is the parsed JSON, if available.
"""
if status_code == 200:
return ResultCategory.SUCCESS
if status_code == 429:
return ResultCategory.RATE_LIMITED
if status_code == 401:
return ResultCategory.AUTH_FAILURE
if status_code == 400 and is_invalid_credentials_body(body):
return ResultCategory.AUTH_FAILURE
if status_code == 403:
# Check if the body indicates a Cloudflare challenge.
if body and ("cf-challenge" in body.lower() or "cloudflare" in body.lower()):
return ResultCategory.FORBIDDEN
return ResultCategory.AUTH_FAILURE
if status_code == 404:
return ResultCategory.NOT_FOUND
if status_code >= 500:
return ResultCategory.UPSTREAM_5XX
return ResultCategory.UNKNOWN
__all__ = ["classify_result"]