56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
"""通用响应模型。"""
|
|
|
|
from typing import Any
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field
|
|
|
|
|
|
class ErrorBody(BaseModel):
|
|
model_config = ConfigDict(
|
|
json_schema_extra={
|
|
"examples": [
|
|
{
|
|
"code": "model_error",
|
|
"message": "模型 provider 返回错误。",
|
|
"trace_id": "trace_e5e220f8656c488895a696a5e9b5eedd",
|
|
"details": {"status_code": 400, "body": "Model Not Exist"},
|
|
}
|
|
]
|
|
}
|
|
)
|
|
|
|
code: str = Field(description="标准错误码,取值见契约字典 API 标准错误码。")
|
|
message: str = Field(description="面向用户的错误信息。")
|
|
trace_id: str = Field(description="链路追踪 ID。")
|
|
details: dict[str, Any] = Field(default_factory=dict, description="脱敏后的错误详情。")
|
|
|
|
|
|
class ErrorResponse(BaseModel):
|
|
model_config = ConfigDict(
|
|
json_schema_extra={
|
|
"examples": [
|
|
{
|
|
"error": {
|
|
"code": "not_found",
|
|
"message": "会话不存在。",
|
|
"trace_id": "trace_e5e220f8656c488895a696a5e9b5eedd",
|
|
"details": {},
|
|
}
|
|
}
|
|
]
|
|
}
|
|
)
|
|
|
|
error: ErrorBody = Field(description="错误响应主体。")
|
|
|
|
|
|
class Page[T](BaseModel):
|
|
items: list[T] = Field(description="当前页数据。")
|
|
page: int = Field(description="页码,从 1 开始。")
|
|
page_size: int = Field(description="每页数量。")
|
|
total: int = Field(description="总数量。")
|
|
|
|
|
|
class OrmModel(BaseModel):
|
|
model_config = ConfigDict(from_attributes=True)
|