大改,未验证

This commit is contained in:
2026-03-20 16:50:26 +08:00
parent c7ee292c4f
commit e14e3ee7a5
36 changed files with 1419 additions and 4805 deletions

View File

@@ -12,6 +12,7 @@ from datetime import datetime
from src.config.unified_config import get_config
from src.agent.llm_client import LLMManager
from src.web.service_manager import service_manager
from src.agent.react_agent import ReactAgent
logger = logging.getLogger(__name__)
@@ -25,7 +26,10 @@ class TSPAgentAssistant:
self.is_agent_mode = True
self.execution_history = []
# 工具注册表
# ReAct Agent核心
self.react_agent = ReactAgent()
# 工具注册表(保留兼容旧 API
self.tools = {}
self.tool_performance = {}
@@ -194,13 +198,15 @@ class TSPAgentAssistant:
def get_agent_status(self) -> Dict[str, Any]:
"""获取Agent状态"""
try:
react_status = self.react_agent.get_status()
return {
"success": True,
"is_active": self.is_agent_mode,
"ai_monitoring_active": self.ai_monitoring_active,
"total_tools": len(self.tools),
"total_executions": len(self.execution_history),
"tools": self.get_available_tools(),
"total_tools": react_status["tool_count"],
"available_tools": react_status["available_tools"],
"total_executions": len(self.execution_history) + react_status["history_count"],
"react_agent": react_status,
"performance": self.get_tool_performance_report()
}
except Exception as e:
@@ -321,29 +327,23 @@ class TSPAgentAssistant:
async def process_message_agent(self, message: str, user_id: str = "admin",
work_order_id: Optional[int] = None,
enable_proactive: bool = True) -> Dict[str, Any]:
"""处理消息 (实战化)"""
"""处理消息 - 使用 ReAct Agent"""
try:
logger.info(f"Agent收到消息: {message}")
# 1. 识别意图和推荐工具
prompt = f"用户消息: {message}\n请分析用户意图,并从工具列表中选择最合适的工具。工具列表: {json.dumps(self.get_available_tools())}\n请直接返回你的分析和建议响应。"
response_text = await self.llm_manager.generate(prompt)
# 2. 模拟动作生成
actions = []
if "工单" in message or "查询" in message:
actions.append({"type": "tool_call", "tool": "search_work_order", "status": "suggested"})
return {
"success": True,
"response": response_text,
"actions": actions,
"user_id": user_id,
"work_order_id": work_order_id,
"status": "completed",
"timestamp": datetime.now().isoformat()
}
result = await self.react_agent.chat(
message=message,
user_id=user_id,
)
result["user_id"] = user_id
result["work_order_id"] = work_order_id
result["status"] = "completed" if result.get("success") else "error"
result["timestamp"] = datetime.now().isoformat()
# 兼容旧字段
result["actions"] = [
{"type": "tool_call", "tool": tc["tool"], "status": "executed"}
for tc in result.get("tool_calls", [])
]
return result
except Exception as e:
logger.error(f"处理消息失败: {e}")
return {"error": str(e)}