add ai agent

This commit is contained in:
jxxghp
2025-10-18 21:26:36 +08:00
parent 78aaad7b59
commit 6fee532c96
29 changed files with 1819 additions and 3 deletions
+105
View File
@@ -0,0 +1,105 @@
"""
AI智能体初始化器
"""
import asyncio
import threading
from typing import Optional
from app.core.config import settings
from app.log import logger
class AgentInitializer:
"""AI智能体初始化器"""
def __init__(self):
self.agent_manager = None
self._initialized = False
async def initialize(self) -> bool:
"""
初始化AI智能体管理器
"""
try:
if not settings.AI_AGENT_ENABLE:
logger.info("AI智能体功能未启用")
return True
from app.agent.agent import agent_manager
self.agent_manager = agent_manager
await agent_manager.initialize()
self._initialized = True
logger.info("AI智能体管理器初始化成功")
return True
except Exception as e:
logger.error(f"AI智能体管理器初始化失败: {e}")
return False
async def cleanup(self) -> None:
"""
清理AI智能体管理器
"""
try:
if not self._initialized or not self.agent_manager:
return
await self.agent_manager.close()
self._initialized = False
logger.info("AI智能体管理器已关闭")
except Exception as e:
logger.error(f"关闭AI智能体管理器时发生错误: {e}")
# 全局AI智能体初始化器实例
agent_initializer = AgentInitializer()
def init_agent():
"""
初始化AI智能体(同步版本,用于在后台线程中运行)
"""
try:
if not settings.AI_AGENT_ENABLE:
logger.info("AI智能体功能未启用")
return True
# 在新的事件循环中初始化AI智能体管理器
def run_init():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
success = loop.run_until_complete(agent_initializer.initialize())
if success:
logger.info("AI智能体管理器初始化成功")
else:
logger.error("AI智能体管理器初始化失败")
return success
except Exception as e:
logger.error(f"AI智能体管理器初始化失败: {e}")
return False
finally:
loop.close()
# 在后台线程中初始化
init_thread = threading.Thread(target=run_init, daemon=True)
init_thread.start()
return True
except Exception as e:
logger.error(f"初始化AI智能体时发生错误: {e}")
return False
async def stop_agent():
"""
停止AI智能体(异步版本,用于在应用关闭时调用)
"""
try:
await agent_initializer.cleanup()
except Exception as e:
logger.error(f"停止AI智能体时发生错误: {e}")
+5
View File
@@ -27,6 +27,7 @@ from app.db.systemconfig_oper import SystemConfigOper
from app.command import CommandChain
from app.schemas import Notification, NotificationType
from app.schemas.types import SystemConfigKey
from app.startup.agent_initializer import init_agent, stop_agent
def start_frontend():
@@ -110,6 +111,8 @@ async def stop_modules():
"""
服务关闭
"""
# 停止AI智能体
await stop_agent()
# 停止模块
ModuleManager().stop()
# 停止事件消费
@@ -151,6 +154,8 @@ def init_modules():
EventManager().start()
# 初始化订阅分享
SubscribeHelper()
# 初始化AI智能体
init_agent()
# 启动前端服务
start_frontend()
# 检查认证状态