mirror of
https://github.com/Zippland/Bubbles.git
synced 2026-03-05 16:16:24 +08:00
refactor: 重构主程序架构并优化代码结构
- 将主程序从同步模式改为异步架构 - 移除 job_mgmt.py 文件,其功能由其他模块替代 - 优化日志配置和第三方库日志级别 - 添加新的 sendTextMsg 方法以兼容旧接口 - 简化类型注解和导入语句
This commit is contained in:
14
bot.py
14
bot.py
@@ -619,6 +619,20 @@ class BubblesBot:
|
||||
|
||||
return False
|
||||
|
||||
def sendTextMsg(self, msg: str, receiver: str, at_list: str = "") -> None:
|
||||
"""同步发送消息(兼容旧接口,供 ReminderManager 使用)"""
|
||||
import asyncio
|
||||
|
||||
async def _send():
|
||||
at_users = at_list.split(",") if at_list else None
|
||||
await self.channel.send_text(msg, receiver, at_users)
|
||||
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
asyncio.create_task(_send())
|
||||
except RuntimeError:
|
||||
asyncio.run(_send())
|
||||
|
||||
def cleanup(self) -> None:
|
||||
"""清理资源"""
|
||||
self.LOG.info("正在清理 BubblesBot 资源...")
|
||||
|
||||
@@ -2,13 +2,10 @@ import logging
|
||||
import os
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from typing import Optional, Tuple
|
||||
from typing import Optional, Tuple, Any, TYPE_CHECKING
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
if TYPE_CHECKING:
|
||||
from commands.context import MessageContext
|
||||
from robot import Robot
|
||||
|
||||
PERSONA_PREFIX = "## 角色\n"
|
||||
|
||||
@@ -123,7 +120,7 @@ class PersonaManager:
|
||||
self.LOG.error(f"Failed to close persona database connection: {exc}")
|
||||
|
||||
|
||||
def fetch_persona_for_context(robot: "Robot", ctx: "MessageContext") -> Optional[str]:
|
||||
def fetch_persona_for_context(robot: Any, ctx: "MessageContext") -> Optional[str]:
|
||||
"""Return persona text for the context receiver."""
|
||||
manager = getattr(robot, "persona_manager", None)
|
||||
if not manager:
|
||||
@@ -138,7 +135,7 @@ def fetch_persona_for_context(robot: "Robot", ctx: "MessageContext") -> Optional
|
||||
return None
|
||||
|
||||
|
||||
def handle_persona_command(robot: "Robot", ctx: "MessageContext") -> bool:
|
||||
def handle_persona_command(robot: Any, ctx: "MessageContext") -> bool:
|
||||
"""Process /set and /persona commands."""
|
||||
text = (ctx.text or "").strip()
|
||||
if not text or not text.startswith("/"):
|
||||
|
||||
94
job_mgmt.py
94
job_mgmt.py
@@ -1,94 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import time
|
||||
import logging
|
||||
from typing import Any, Callable
|
||||
|
||||
import schedule
|
||||
|
||||
# 获取模块级 logger
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Job(object):
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def onEverySeconds(self, seconds: int, task: Callable[..., Any], *args, **kwargs) -> None:
|
||||
"""
|
||||
每 seconds 秒执行
|
||||
:param seconds: 间隔,秒
|
||||
:param task: 定时执行的方法
|
||||
:return: None
|
||||
"""
|
||||
schedule.every(seconds).seconds.do(task, *args, **kwargs)
|
||||
|
||||
def onEveryMinutes(self, minutes: int, task: Callable[..., Any], *args, **kwargs) -> None:
|
||||
"""
|
||||
每 minutes 分钟执行
|
||||
:param minutes: 间隔,分钟
|
||||
:param task: 定时执行的方法
|
||||
:return: None
|
||||
"""
|
||||
schedule.every(minutes).minutes.do(task, *args, **kwargs)
|
||||
|
||||
def onEveryHours(self, hours: int, task: Callable[..., Any], *args, **kwargs) -> None:
|
||||
"""
|
||||
每 hours 小时执行
|
||||
:param hours: 间隔,小时
|
||||
:param task: 定时执行的方法
|
||||
:return: None
|
||||
"""
|
||||
schedule.every(hours).hours.do(task, *args, **kwargs)
|
||||
|
||||
def onEveryDays(self, days: int, task: Callable[..., Any], *args, **kwargs) -> None:
|
||||
"""
|
||||
每 days 天执行
|
||||
:param days: 间隔,天
|
||||
:param task: 定时执行的方法
|
||||
:return: None
|
||||
"""
|
||||
schedule.every(days).days.do(task, *args, **kwargs)
|
||||
|
||||
def onEveryTime(self, times: int, task: Callable[..., Any], *args, **kwargs) -> None:
|
||||
"""
|
||||
每天定时执行
|
||||
:param times: 时间字符串列表,格式:
|
||||
- For daily jobs -> HH:MM:SS or HH:MM
|
||||
- For hourly jobs -> MM:SS or :MM
|
||||
- For minute jobs -> :SS
|
||||
:param task: 定时执行的方法
|
||||
:return: None
|
||||
|
||||
例子: times=["10:30", "10:45", "11:00"]
|
||||
"""
|
||||
if not isinstance(times, list):
|
||||
times = [times]
|
||||
|
||||
for t in times:
|
||||
schedule.every(1).days.at(t).do(task, *args, **kwargs)
|
||||
|
||||
def runPendingJobs(self) -> None:
|
||||
schedule.run_pending()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 设置测试用的日志配置
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'
|
||||
)
|
||||
|
||||
def printStr(s):
|
||||
logger.info(s)
|
||||
|
||||
job = Job()
|
||||
job.onEverySeconds(59, printStr, "onEverySeconds 59")
|
||||
job.onEveryMinutes(59, printStr, "onEveryMinutes 59")
|
||||
job.onEveryHours(23, printStr, "onEveryHours 23")
|
||||
job.onEveryDays(1, printStr, "onEveryDays 1")
|
||||
job.onEveryTime("23:59", printStr, "onEveryTime 23:59")
|
||||
|
||||
while True:
|
||||
job.runPendingJobs()
|
||||
time.sleep(1)
|
||||
178
main.py
178
main.py
@@ -1,14 +1,7 @@
|
||||
#! /usr/bin/env python3
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Bubbles WeChat Robot - Agent Loop 架构
|
||||
版本 39.3.0.0
|
||||
|
||||
主要改进:
|
||||
- 全异步架构 (async/await)
|
||||
- Agent Loop 模式处理消息
|
||||
- 统一的 LLMProvider 接口
|
||||
- 独立的 Session 管理
|
||||
Bubbles - 基于 Channel 抽象的聊天机器人
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
@@ -18,105 +11,92 @@ import sys
|
||||
import os
|
||||
from argparse import ArgumentParser
|
||||
|
||||
# 确保日志目录存在
|
||||
log_dir = "logs"
|
||||
if not os.path.exists(log_dir):
|
||||
os.makedirs(log_dir)
|
||||
|
||||
# 配置 logging
|
||||
log_format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s'
|
||||
logging.basicConfig(
|
||||
level=logging.WARNING, # 提高默认日志级别为 WARNING,只显示警告和错误信息
|
||||
format=log_format,
|
||||
handlers=[
|
||||
# logging.FileHandler(os.path.join(log_dir, "app.log"), encoding='utf-8'), # 将所有日志写入文件
|
||||
# logging.StreamHandler(sys.stdout) # 同时输出到控制台
|
||||
]
|
||||
)
|
||||
|
||||
# 为特定模块设置更具体的日志级别
|
||||
logging.getLogger("requests").setLevel(logging.ERROR) # 提高为 ERROR
|
||||
logging.getLogger("urllib3").setLevel(logging.ERROR) # 提高为 ERROR
|
||||
logging.getLogger("httpx").setLevel(logging.ERROR) # 提高为 ERROR
|
||||
|
||||
# 常见的自定义模块日志设置,按需修改
|
||||
logging.getLogger("Weather").setLevel(logging.WARNING)
|
||||
logging.getLogger("ai_providers").setLevel(logging.WARNING)
|
||||
logging.getLogger("commands").setLevel(logging.WARNING)
|
||||
# 临时调试:为AI路由器设置更详细的日志级别
|
||||
logging.getLogger("commands.ai_router").setLevel(logging.INFO)
|
||||
# Agent Loop 日志
|
||||
logging.getLogger("agent").setLevel(logging.INFO)
|
||||
|
||||
from configuration import Config
|
||||
from constants import ChatType
|
||||
from robot import Robot, __version__
|
||||
from wcferry import Wcf
|
||||
from bot import BubblesBot, __version__
|
||||
|
||||
|
||||
def setup_logging(level: int = logging.INFO):
|
||||
"""配置日志"""
|
||||
logging.basicConfig(
|
||||
level=level,
|
||||
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
||||
datefmt="%H:%M:%S",
|
||||
)
|
||||
# 降低第三方库日志级别
|
||||
for name in ["httpx", "httpcore", "openai", "urllib3", "requests"]:
|
||||
logging.getLogger(name).setLevel(logging.WARNING)
|
||||
|
||||
|
||||
async def run_wechat():
|
||||
"""微信模式"""
|
||||
from channel import WeChatChannel
|
||||
|
||||
if WeChatChannel is None:
|
||||
print("错误: wcferry 不可用,请在 Windows 环境下运行")
|
||||
print("如需本地调试,请运行: python local_main.py")
|
||||
sys.exit(1)
|
||||
|
||||
def main(chat_type: int):
|
||||
config = Config()
|
||||
wcf = Wcf(debug=False) # 将 debug 设置为 False 减少 wcf 的调试输出
|
||||
|
||||
# 定义全局变量robot,使其在handler中可访问
|
||||
global robot
|
||||
robot = Robot(config, wcf, chat_type)
|
||||
channel = WeChatChannel(debug=False)
|
||||
bot = BubblesBot(channel=channel, config=config)
|
||||
|
||||
def handler(sig, frame):
|
||||
# 先清理机器人资源(包括关闭数据库连接)
|
||||
if 'robot' in globals() and robot:
|
||||
robot.LOG.info("程序退出,开始清理资源...")
|
||||
robot.cleanup()
|
||||
|
||||
# 再清理wcf环境
|
||||
wcf.cleanup() # 退出前清理环境
|
||||
exit(0)
|
||||
# 信号处理
|
||||
def handle_signal(sig, frame):
|
||||
logging.info("收到退出信号,正在清理...")
|
||||
asyncio.create_task(shutdown(bot, channel))
|
||||
|
||||
signal.signal(signal.SIGINT, handler)
|
||||
signal.signal(signal.SIGINT, handle_signal)
|
||||
signal.signal(signal.SIGTERM, handle_signal)
|
||||
|
||||
robot.LOG.info(f"WeChatRobot【{__version__}】成功启动···")
|
||||
logging.info(f"Bubbles v{__version__} 启动中...")
|
||||
|
||||
# # 机器人启动发送测试消息
|
||||
# robot.sendTextMsg("机器人启动成功!", "filehelper")
|
||||
|
||||
# 接收消息
|
||||
# robot.enableRecvMsg() # 可能会丢消息?
|
||||
robot.enableReceivingMsg() # 加队列
|
||||
|
||||
# 每天 7 点发送天气预报
|
||||
robot.onEveryTime("07:00", robot.weatherReport)
|
||||
|
||||
# 每天 7:30 发送新闻
|
||||
robot.onEveryTime("07:30", robot.newsReport)
|
||||
try:
|
||||
await bot.start()
|
||||
except Exception as e:
|
||||
logging.error(f"运行出错: {e}", exc_info=True)
|
||||
finally:
|
||||
await shutdown(bot, channel)
|
||||
|
||||
|
||||
# 让机器人一直跑
|
||||
robot.keepRunningAndBlockProcess()
|
||||
async def shutdown(bot: BubblesBot, channel):
|
||||
"""清理资源"""
|
||||
await bot.stop()
|
||||
bot.cleanup()
|
||||
if hasattr(channel, "cleanup"):
|
||||
channel.cleanup()
|
||||
|
||||
|
||||
def main():
|
||||
parser = ArgumentParser(description="Bubbles 聊天机器人")
|
||||
parser.add_argument(
|
||||
"-d", "--debug", action="store_true", help="调试模式"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-q", "--quiet", action="store_true", help="安静模式"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--local", action="store_true", help="本地调试模式(无需微信)"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
# 日志级别
|
||||
if args.debug:
|
||||
level = logging.DEBUG
|
||||
elif args.quiet:
|
||||
level = logging.ERROR
|
||||
else:
|
||||
level = logging.INFO
|
||||
|
||||
setup_logging(level)
|
||||
|
||||
if args.local:
|
||||
# 本地模式
|
||||
import local_main
|
||||
asyncio.run(local_main.main())
|
||||
else:
|
||||
# 微信模式
|
||||
asyncio.run(run_wechat())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument('-c', type=int, default=0,
|
||||
help=f'选择默认模型参数序号: {ChatType.help_hint()}(可通过配置文件为不同群指定模型)')
|
||||
parser.add_argument('-d', '--debug', action='store_true',
|
||||
help='启用调试模式,输出更详细的日志信息')
|
||||
parser.add_argument('-q', '--quiet', action='store_true',
|
||||
help='安静模式,只输出错误信息')
|
||||
parser.add_argument('-v', '--verbose', action='store_true',
|
||||
help='详细输出模式,显示所有信息日志')
|
||||
args = parser.parse_args()
|
||||
|
||||
# 处理日志级别参数
|
||||
if args.debug:
|
||||
# 调试模式优先级最高
|
||||
logging.getLogger().setLevel(logging.DEBUG)
|
||||
print("已启用调试模式,将显示所有详细日志信息")
|
||||
elif args.quiet:
|
||||
# 安静模式,控制台只显示错误
|
||||
logging.getLogger().setLevel(logging.ERROR)
|
||||
print("已启用安静模式,控制台只显示错误信息")
|
||||
elif args.verbose:
|
||||
# 详细模式,显示所有 INFO 级别日志
|
||||
logging.getLogger().setLevel(logging.INFO)
|
||||
print("已启用详细模式,将显示所有信息日志")
|
||||
|
||||
main(args.c)
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user