AI CHAT对话降序获取升序输出,追加历史记录接口生成记录模块

This commit is contained in:
2026-05-27 16:34:22 +08:00
parent c17d467f59
commit 56b9bdaef0
4 changed files with 404 additions and 9 deletions
+33 -3
View File
@@ -140,7 +140,15 @@ async def list_tasks(
page,
page_size,
)
return GenerationAITaskListOut(total=total, items=[record_to_out(i) for i in items])
# ====================== 在这里加排序(最新在前)======================
# 按 created_at 降序(没有则用 id 降序)
items_sorted = sorted(
items,
key=lambda x: x.created_at if x.created_at is not None else x.id,
reverse=False # 降序
)
return GenerationAITaskListOut(total=total, items=[record_to_out(i) for i in items_sorted])
@router.get(
@@ -149,6 +157,8 @@ async def list_tasks(
summary="获取AI生成历史日期分组",
description=(
"按生成完成日期倒序返回当前用户的AI生成历史记录。"
"默认查询 chat_generation_tasks / ChatGenerationTask 新任务历史。"
"当 history_source=generation_record 时,查询 generation_records / GenerationRecord 旧历史。"
"该接口只返回生成成功的任务,即 status=completed 且 generated_at 不为空的数据。"
"必须通过 gen_type 区分图片和视频。"
"分页对象是生成日期,不是单条记录。"
@@ -160,7 +170,7 @@ async def list_tasks(
"description": "查询成功,返回按生成日期分组的历史记录",
},
400: {
"description": "参数错误,例如 gen_type 不是 image 或 video",
"description": "参数错误,例如 gen_type 不是 image 或 video,或 history_source 不支持",
},
401: {
"description": "未登录或 Token 无效",
@@ -186,6 +196,14 @@ async def list_history_grouped_days(
description="每页返回的生成日期数量,范围 1~10,最大只能获取10天",
examples=[10],
),
history_source: str | None = Query(
None,
description=(
"历史数据来源。默认不传或传 chat_task 查询 chat_generation_tasks / ChatGenerationTask"
"传 generation_record 查询 generation_records / GenerationRecord 旧历史数据"
),
examples=["generation_record"],
),
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
@@ -195,6 +213,7 @@ async def list_history_grouped_days(
gen_type=gen_type,
page=page,
page_size=page_size,
history_source=history_source,
)
@@ -204,6 +223,8 @@ async def list_history_grouped_days(
summary="获取指定日期下的AI生成历史分页",
description=(
"获取某一个生成日期下的生成成功记录分页。"
"默认查询 chat_generation_tasks / ChatGenerationTask 新任务历史。"
"当 history_source=generation_record 时,查询 generation_records / GenerationRecord 旧历史。"
"该接口用于前端在历史分组列表中继续加载某一天的后续记录。"
"例如 /history 接口中某一天 total=18,但 items 只返回前10条,"
"则前端可以调用本接口 page=2&page_size=10 获取该日期下剩余记录。"
@@ -214,7 +235,7 @@ async def list_history_grouped_days(
"description": "查询成功,返回指定日期下的历史记录分页",
},
400: {
"description": "参数错误,例如 generated_date 格式不是 YYYY-MM-DDgen_type 不合法",
"description": "参数错误,例如 generated_date 格式不是 YYYY-MM-DDgen_type 不合法,或 history_source 不支持",
},
401: {
"description": "未登录或 Token 无效",
@@ -245,6 +266,14 @@ async def list_history_day_items(
description="当前日期下每页返回的生成记录数量,范围 1~100",
examples=[10],
),
history_source: str | None = Query(
None,
description=(
"历史数据来源。默认不传或传 chat_task 查询 chat_generation_tasks / ChatGenerationTask"
"传 generation_record 查询 generation_records / GenerationRecord 旧历史数据"
),
examples=["generation_record"],
),
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
@@ -255,6 +284,7 @@ async def list_history_day_items(
generated_date=generated_date,
page=page,
page_size=page_size,
history_source=history_source,
)