diff --git a/.dockerignore b/.dockerignore index 0ed9090fd..6c2d07736 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,4 +3,6 @@ __pycache__ *.pyc *.pyo *.pyd -.DS_Store \ No newline at end of file +.DS_Store +mongodb +napcat \ No newline at end of file diff --git a/.gitignore b/.gitignore index 7eba6537c..3f6ad3edd 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,8 @@ __pycache__/ *.py[cod] *$py.class llm_statistics.txt +mongodb +napcat # C extensions *.so diff --git a/flagged/log.csv b/flagged/log.csv deleted file mode 100644 index daeef4a9b..000000000 --- a/flagged/log.csv +++ /dev/null @@ -1,2 +0,0 @@ -输入消息,推理内容,flag,username,timestamp -显示内容,,,,2025-02-18 16:50:53.643238 diff --git a/run.bat b/run.bat new file mode 100644 index 000000000..1d1385671 --- /dev/null +++ b/run.bat @@ -0,0 +1,6 @@ +@ECHO OFF +chcp 65001 +REM python -m venv venv +call venv\Scripts\activate.bat +REM pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple --upgrade -r requirements.txt +python run.py \ No newline at end of file diff --git a/run.py b/run.py new file mode 100644 index 000000000..d8513beed --- /dev/null +++ b/run.py @@ -0,0 +1,122 @@ +import subprocess +import os +import shutil +import zipfile +import requests +from tqdm import tqdm + + +def extract_files(zip_path, target_dir): + """ + 解压 + + Args: + zip_path: 源ZIP压缩包路径(需确保是有效压缩包) + target_dir: 目标文件夹路径(会自动创建不存在的目录) + """ + # 打开ZIP压缩包(上下文管理器自动处理关闭) + with zipfile.ZipFile(zip_path) as zip_ref: + # 通过第一个文件路径推断顶层目录名(格式如:top_dir/) + top_dir = zip_ref.namelist()[0].split("/")[0] + "/" + + # 遍历压缩包内所有文件条目 + for file in zip_ref.namelist(): + # 跳过目录条目,仅处理文件 + if file.startswith(top_dir) and not file.endswith("/"): + # 截取顶层目录后的相对路径(如:sub_dir/file.txt) + rel_path = file[len(top_dir) :] + + # 创建目标目录结构(含多级目录) + os.makedirs( + os.path.dirname(f"{target_dir}/{rel_path}"), + exist_ok=True, # 忽略已存在目录的错误 + ) + + # 读取压缩包内文件内容并写入目标路径 + with open(f"{target_dir}/{rel_path}", "wb") as f: + f.write(zip_ref.read(file)) + + +def run_cmd(command: str, open_new_window: bool = False): + """ + 运行 cmd 命令 + + Args: + command (str): 指定要运行的命令 + open_new_window (bool): 指定是否新建一个 cmd 窗口运行 + """ + creationflags = 0 + if open_new_window: + creationflags = subprocess.CREATE_NEW_CONSOLE + subprocess.Popen( + [ + "cmd.exe", + "/c", + command, + ], + creationflags=creationflags, + ) + + +def run_maimbot(): + run_cmd(r"napcat\NapCatWinBootMain.exe 10001", False) + run_cmd( + r"mongodb\bin\mongod.exe --dbpath=" + os.getcwd() + r"\mongodb\db --port 27017", + True, + ) + run_cmd("nb run", True) + + +def install_mongodb(): + """ + 安装 MongoDB + """ + print("下载 MongoDB") + resp = requests.get( + "https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-latest.zip", + stream=True, + ) + total = int(resp.headers.get("content-length", 0)) # 计算文件大小 + with open("mongodb.zip", "w+b") as file, tqdm( # 展示下载进度条,并解压文件 + desc="mongodb.zip", + total=total, + unit="iB", + unit_scale=True, + unit_divisor=1024, + ) as bar: + for data in resp.iter_content(chunk_size=1024): + size = file.write(data) + bar.update(size) + extract_files("mongodb.zip", "mongodb") + print("MongoDB 下载完成") + os.remove("mongodb.zip") + + +def install_napcat(): + run_cmd("start https://github.com/NapNeko/NapCatQQ/releases", True) + print("请检查弹出的浏览器窗口,点击**第一个**蓝色的“Win64无头” 下载 napcat") + napcat_filename = input( + "下载完成后请把文件复制到此文件夹,并将**不包含后缀的文件名**输入至此窗口,如 NapCat.32793.Shell:" + ) + extract_files(napcat_filename + ".zip", "napcat") + print("NapCat 安装完成") + os.remove(napcat_filename + ".zip") + + +if __name__ == "__main__": + os.system("cls") + choice = input( + "请输入要进行的操作:\n" + "1.首次安装\n" + "2.运行麦麦\n" + "3.运行麦麦并启动可视化推理界面\n" + ) + os.system("cls") + if choice == "1": + install_napcat() + install_mongodb() + elif choice == "2": + run_maimbot() + elif choice == "3": + run_maimbot() + run_cmd("python src/gui/reasoning_gui.py", True) diff --git a/run_db.bat b/script/run_db.bat similarity index 100% rename from run_db.bat rename to script/run_db.bat diff --git a/run_maimai.bat b/script/run_maimai.bat similarity index 100% rename from run_maimai.bat rename to script/run_maimai.bat diff --git a/run_thingking.bat b/script/run_thingking.bat similarity index 100% rename from run_thingking.bat rename to script/run_thingking.bat diff --git a/run_windows.bat b/script/run_windows.bat similarity index 100% rename from run_windows.bat rename to script/run_windows.bat diff --git a/src/plugins/chat/config.py b/src/plugins/chat/config.py index 0186001ab..6ebf5b165 100644 --- a/src/plugins/chat/config.py +++ b/src/plugins/chat/config.py @@ -85,7 +85,11 @@ class BotConfig: config = cls() if os.path.exists(config_path): with open(config_path, "rb") as f: - toml_dict = tomli.load(f) + try: + toml_dict = tomli.load(f) + except(tomli.TOMLDecodeError) as e: + logger.critical(f"配置文件bot_config.toml填写有误,请检查第{e.lineno}行第{e.colno}处:{e.msg}") + exit(1) if 'personality' in toml_dict: personality_config=toml_dict['personality']