diff --git a/api/app/controllers/service/app_api_controller.py b/api/app/controllers/service/app_api_controller.py index 93caa200..f52c9338 100644 --- a/api/app/controllers/service/app_api_controller.py +++ b/api/app/controllers/service/app_api_controller.py @@ -86,10 +86,36 @@ async def chat( app_service: Annotated[AppService, Depends(get_app_service)] = None, message: str = Body(..., description="聊天消息内容"), ): + """ + Agent/Workflow 聊天接口 + + - 不传 version:使用当前发布版本(current_release) + - 传 version=N:使用指定版本号的历史快照,例如 /v1/app/chat?version=2 + """ body = await request.json() payload = AppChatRequest(**body) app = app_service.get_app(api_key_auth.resource_id, api_key_auth.workspace_id) + + # 版本切换:指定 version 时查找对应历史快照 + if payload.version is not None: + from sqlalchemy import select as _select + from app.models.app_release_model import AppRelease as _AppRelease + release = db.scalars( + _select(_AppRelease).where( + _AppRelease.app_id == app.id, + _AppRelease.version == payload.version, + _AppRelease.is_active.is_(True), + ) + ).first() + if not release: + raise BusinessException( + f"版本 {payload.version} 不存在或已下线", + BizCode.AGENT_CONFIG_MISSING, + ) + # 临时替换 current_release,后续逻辑无需改动 + app.current_release = release + app.current_release_id = release.id other_id = payload.user_id workspace_id = api_key_auth.workspace_id end_user_repo = EndUserRepository(db) diff --git a/api/app/schemas/app_schema.py b/api/app/schemas/app_schema.py index 85cff671..130bff91 100644 --- a/api/app/schemas/app_schema.py +++ b/api/app/schemas/app_schema.py @@ -616,6 +616,7 @@ class AppChatRequest(BaseModel): stream: bool = Field(default=False, description="是否流式返回") thinking: bool = Field(default=False, description="是否启用深度思考(需Agent配置支持)") files: List[FileInput] = Field(default_factory=list, description="附件列表(支持多文件)") + version: Optional[int] = Field(default=None, description="指定发布版本号,不传则使用当前发布版本") class DraftRunRequest(BaseModel):