from __future__ import annotations from typing import Annotated from fastapi import APIRouter, Depends, Query from sqlalchemy.ext.asyncio import AsyncSession from app.dependencies import get_current_user, get_db from app.enums.recent_generation import RecentGenerationModuleEnum from app.models.user import User from app.schemas.recent_generation import RecentGenerationGroupOut from app.services.recent_generation_service import ( DEFAULT_RECENT_GENERATION_LIMIT, MAX_RECENT_GENERATION_LIMIT, list_recent_generations, ) router = APIRouter( prefix="/recent-generations", tags=["recent-generations"], ) @router.get( "", response_model=RecentGenerationGroupOut, summary="获取当前用户各模块最近生成记录", description=( "获取当前登录用户在多个生成模块下最近生成成功的图片/视频记录。" "返回结构固定为 project、chat_ai、hot_opening_replicate、shot_replicate 四个数组。" "modules 不传时查询全部模块;modules 可重复传参指定一个或多个模块,例如 " "?modules=project&modules=chat_ai。" "limit 表示每个模块最多返回多少条,默认 5 条,最大 100 条。" "接口只查询和返回展示所需轻量字段,不返回 prompt、engine_snapshot、provider_response_json 等大字段。" ), responses={ 200: { "description": "查询成功,固定返回四个模块数组;没有数据的模块返回空数组。", "content": { "application/json": { "example": { "project": [], "chat_ai": [], "hot_opening_replicate": [], "shot_replicate": [ { "generated_time": "2026-06-26T14:30:00", "result_url": "https://example.com/generate/video/demo.mp4?exp=1780000000&sign=xxxx", "cover_url": "https://example.com/generate/cover/demo.jpg?exp=1780000000&sign=xxxx", "module": "shot_replicate", "shot_task_set_id": "0019ef0000000000001", "shot_segment_id": "0019ef0000000000002", "module_project_id": "0019ef0000000000003", "module_step_id": "0019ef0000000000004", "generation_id": "0019ef0000000000005", "resource_type": "video", } ], } } }, }, 401: {"description": "未登录或 Token 无效"}, 403: {"description": "账号需要先设置登录密码或无权限"}, 422: {"description": "参数校验失败,例如 limit 超出范围或 modules 枚举值非法"}, }, ) async def get_recent_generations( limit: Annotated[ int, Query( ge=1, le=MAX_RECENT_GENERATION_LIMIT, description=( "每个模块返回的最近生成记录数量,默认 5,最大 100。" "例如 limit=10 表示 project/chat_ai/hot_opening_replicate/shot_replicate 每个模块最多返回 10 条。" ), examples=[DEFAULT_RECENT_GENERATION_LIMIT], ), ] = DEFAULT_RECENT_GENERATION_LIMIT, modules: Annotated[ list[RecentGenerationModuleEnum] | None, Query( description=( "模块枚举,可不传或重复传参。" "不传表示查询全部模块。" "可选值:" "project=项目生成 GenerationRecord;" "chat_ai=AI创作 ChatGenerationTask.generation_mode=chatapi_async;" "hot_opening_replicate=爆款开头复刻 ChatGenerationTask.generation_mode=hot_opening_replicate;" "shot_replicate=拆镜复刻 ChatGenerationTask.generation_mode=shot_replicate。" ), examples=[["project", "chat_ai"]], ), ] = None, current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db), ) -> RecentGenerationGroupOut: return await list_recent_generations( db, user_id=current_user.id, modules=modules, limit=limit, )