feat(backend): update orchestrator endpoint
- endpoints/orchestrator.py: update orchestrator API endpoint with new features
This commit is contained in:
@@ -2,12 +2,18 @@ from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_db
|
||||
from app.schemas.common import ErrorResponse
|
||||
from app.schemas.orchestrator import OrchestratorRequest, OrchestratorResponse
|
||||
from app.schemas.orchestrator import (
|
||||
ConversationDeleteResponse,
|
||||
ConversationLookupResponse,
|
||||
OrchestratorRequest,
|
||||
OrchestratorResponse,
|
||||
)
|
||||
from app.services.agent_conversations import AgentConversationService
|
||||
from app.services.orchestrator import OrchestratorService
|
||||
|
||||
router = APIRouter(prefix="/orchestrator")
|
||||
@@ -31,3 +37,41 @@ def run_orchestrator(payload: OrchestratorRequest, db: DbSession) -> Orchestrato
|
||||
return OrchestratorService(db).run(payload)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@router.get(
|
||||
"/conversations/latest",
|
||||
response_model=ConversationLookupResponse,
|
||||
summary="查询当前用户最近会话",
|
||||
description="返回当前用户最近一段可恢复的对话会话及完整消息历史。",
|
||||
)
|
||||
def get_latest_conversation(
|
||||
user_id: Annotated[str, Query(min_length=1, description="当前用户 ID。")],
|
||||
db: DbSession,
|
||||
) -> ConversationLookupResponse:
|
||||
service = AgentConversationService(db)
|
||||
conversation = service.get_latest_conversation_for_user(user_id=user_id, source="user_message")
|
||||
if conversation is None:
|
||||
return ConversationLookupResponse(found=False, conversation=None)
|
||||
|
||||
return ConversationLookupResponse(
|
||||
found=True,
|
||||
conversation=service.serialize_conversation(conversation, include_messages=True),
|
||||
)
|
||||
|
||||
|
||||
@router.delete(
|
||||
"/conversations",
|
||||
response_model=ConversationDeleteResponse,
|
||||
summary="删除当前用户全部会话",
|
||||
description="删除当前用户在智能体工作台中的全部历史会话,用于显式开启全新对话。",
|
||||
)
|
||||
def delete_user_conversations(
|
||||
user_id: Annotated[str, Query(min_length=1, description="当前用户 ID。")],
|
||||
db: DbSession,
|
||||
) -> ConversationDeleteResponse:
|
||||
deleted_count = AgentConversationService(db).delete_user_conversations(
|
||||
user_id=user_id,
|
||||
source="user_message",
|
||||
)
|
||||
return ConversationDeleteResponse(deleted_count=deleted_count)
|
||||
|
||||
Reference in New Issue
Block a user