stock-ai-agent/backend/app/api/chat.py
2026-02-03 10:08:15 +08:00

68 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
对话API路由
"""
from fastapi import APIRouter, HTTPException
from typing import Optional
import uuid
from app.models.chat import ChatRequest, ChatResponse
from app.agent.smart_agent import smart_agent # 使用智能Agent
from app.utils.logger import logger
router = APIRouter()
@router.post("/message", response_model=ChatResponse)
async def send_message(request: ChatRequest):
"""
发送消息给Agent
Args:
request: 聊天请求
Returns:
Agent响应
"""
try:
# 生成或使用现有session_id
session_id = request.session_id or str(uuid.uuid4())
# 处理消息使用智能Agent
response = await smart_agent.process_message(
message=request.message,
session_id=session_id,
user_id=request.user_id
)
return ChatResponse(
message=response["message"],
session_id=session_id,
metadata=response.get("metadata")
)
except Exception as e:
logger.error(f"处理消息失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/history/{session_id}")
async def get_history(session_id: str, limit: int = 50):
"""
获取对话历史
Args:
session_id: 会话ID
limit: 最大消息数
Returns:
对话历史
"""
try:
context = smart_agent.context_manager.get_context(session_id)
return {
"session_id": session_id,
"messages": context
}
except Exception as e:
logger.error(f"获取历史失败: {e}")
raise HTTPException(status_code=500, detail=str(e))