From b142c5b4e59a4238bfa2c7b75d9d4813e5e825bd Mon Sep 17 00:00:00 2001
From: Dreamwxz
Date: Sat, 10 May 2025 20:53:06 +0800
Subject: [PATCH 1/5] =?UTF-8?q?=E6=9B=B4=E6=96=B0README=E5=92=8C=E9=83=A8?=
=?UTF-8?q?=E5=88=86=E5=90=8D=E7=A7=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
EULA.md | 4 ++--
PRIVACY.md | 2 +-
README.md | 6 +++---
3 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/EULA.md b/EULA.md
index e21591a5..cf0fbda3 100644
--- a/EULA.md
+++ b/EULA.md
@@ -2,7 +2,7 @@
**版本:V1.0**
**更新日期:2025年5月9日**
**生效日期:2025年3月18日**
-**适用的MaiMBot版本号:所有版本**
+**适用的MaiBot版本号:所有版本**
**2025© MaiBot项目团队**
@@ -10,7 +10,7 @@
## 一、一般条款
-**1.1** MaiMBot项目(包括MaiMBot的源代码、可执行文件、文档,以及其它在本协议中所列出的文件)(以下简称“本项目”)是由开发者及贡献者(以下简称“项目团队”)共同维护,为用户提供自动回复功能的机器人代码项目。以下最终用户许可协议(EULA,以下简称“本协议”)是用户(以下简称“您”)与项目团队之间关于使用本项目所订立的合同条件。
+**1.1** MaiBot项目(包括MaiBot的源代码、可执行文件、文档,以及其它在本协议中所列出的文件)(以下简称“本项目”)是由开发者及贡献者(以下简称“项目团队”)共同维护,为用户提供自动回复功能的机器人代码项目。以下最终用户许可协议(EULA,以下简称“本协议”)是用户(以下简称“您”)与项目团队之间关于使用本项目所订立的合同条件。
**1.2** 在运行或使用本项目之前,您**必须阅读并同意本协议的所有条款**。未成年人或其它无/不完全民事行为能力责任人请**在监护人的陪同下**阅读并同意本协议。如果您不同意,则不得运行或使用本项目。在这种情况下,您应立即从您的设备上卸载或删除本项目及其所有副本。
diff --git a/PRIVACY.md b/PRIVACY.md
index 4e34a2c3..33bc131d 100644
--- a/PRIVACY.md
+++ b/PRIVACY.md
@@ -6,7 +6,7 @@
**2025© MaiBot项目团队**
-MaiMBot项目团队(以下简称项目团队)**尊重并保护**用户(以下简称您)的隐私。若您选择使用MaiMBot项目(以下简称本项目),则您需同意本项目按照以下隐私条款处理您的输入和输出内容:
+MaiBot项目团队(以下简称项目团队)**尊重并保护**用户(以下简称您)的隐私。若您选择使用MaiBot项目(以下简称本项目),则您需同意本项目按照以下隐私条款处理您的输入和输出内容:
**1.1** 本项目**会**收集您的输入和输出内容并发送到第三方API,用于生成新的输出内容。因此您的输入和输出内容**会**同时受到本项目和第三方API的隐私政策约束。
diff --git a/README.md b/README.md
index f349e0ca..f07e7d57 100644
--- a/README.md
+++ b/README.md
@@ -36,7 +36,7 @@
-## 新版0.6.x部署前先阅读:https://docs.mai-mai.org/manual/usage/mmc_q_a
+## 新版0.6.x部署前先阅读:https://docs.mai-mai.org/faq/maibot/backup_update.html
## 📝 项目简介
@@ -85,7 +85,7 @@
### ⚠️ 重要提示
-- 升级到v0.6.x版本前请务必阅读:[升级指南](https://docs.mai-mai.org/manual/usage/mmc_q_a)
+- 升级到v0.6.x版本前请务必阅读:[升级指南](https://docs.mai-mai.org/faq/maibot/backup_update.html)
- 本版本基于MaiCore重构,通过nonebot插件与QQ平台交互
- 项目处于活跃开发阶段,功能和API可能随时调整
@@ -94,7 +94,7 @@
- [二群](https://qm.qq.com/q/RzmCiRtHEW) 571780722
- [五群](https://qm.qq.com/q/JxvHZnxyec) 1022489779
- [三群](https://qm.qq.com/q/wlH5eT8OmQ) 1035228475【已满】
-- [四群](https://qm.qq.com/q/wlH5eT8OmQ) 729957033【已满】
+- [四群](https://qm.qq.com/q/wGePTl1UyY) 729957033【已满】
From 47b3e1af28b6760bd946b6aa9beb9c4c3f0f3812 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A2=A8=E6=A2=93=E6=9F=92?= <1787882683@qq.com>
Date: Sun, 11 May 2025 17:12:39 +0800
Subject: [PATCH 2/5] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=B3=BB?=
=?UTF-8?q?=E7=BB=9F=E5=9F=BA=E6=9C=AC=E4=BF=A1=E6=81=AF=E6=8E=A5=E5=8F=A3?=
=?UTF-8?q?=EF=BC=8C=E5=8C=85=E5=90=AB=E6=93=8D=E4=BD=9C=E7=B3=BB=E7=BB=9F?=
=?UTF-8?q?=E3=80=81Python=E7=89=88=E6=9C=AC=E3=80=81CPU=E5=92=8C=E5=86=85?=
=?UTF-8?q?=E5=AD=98=E4=BD=BF=E7=94=A8=E6=83=85=E5=86=B5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/api/basic_info_api.py | 152 ++++++++++++++++++++++++++++++++
src/api/config_api.py | 176 ++++++++++++++++++++++++++++++--------
src/api/main.py | 13 +++
src/common/server.py | 16 ++++
4 files changed, 322 insertions(+), 35 deletions(-)
create mode 100644 src/api/basic_info_api.py
diff --git a/src/api/basic_info_api.py b/src/api/basic_info_api.py
new file mode 100644
index 00000000..73de18f8
--- /dev/null
+++ b/src/api/basic_info_api.py
@@ -0,0 +1,152 @@
+import platform
+import psutil
+import sys
+import os
+
+def get_system_info():
+ """获取操作系统信息"""
+ return {
+ "system": platform.system(),
+ "release": platform.release(),
+ "version": platform.version(),
+ "machine": platform.machine(),
+ "processor": platform.processor(),
+ }
+
+def get_python_version():
+ """获取 Python 版本信息"""
+ return sys.version
+
+def get_cpu_usage():
+ """获取系统总CPU使用率"""
+ return psutil.cpu_percent(interval=1)
+
+def get_process_cpu_usage():
+ """获取当前进程CPU使用率"""
+ process = psutil.Process(os.getpid())
+ return process.cpu_percent(interval=1)
+
+def get_memory_usage():
+ """获取系统内存使用情况 (单位 MB)"""
+ mem = psutil.virtual_memory()
+ bytes_to_mb = lambda x: round(x / (1024 * 1024), 2) #noqa
+ return {
+ "total_mb": bytes_to_mb(mem.total),
+ "available_mb": bytes_to_mb(mem.available),
+ "percent": mem.percent,
+ "used_mb": bytes_to_mb(mem.used),
+ "free_mb": bytes_to_mb(mem.free),
+ }
+
+def get_process_memory_usage():
+ """获取当前进程内存使用情况 (单位 MB)"""
+ process = psutil.Process(os.getpid())
+ mem_info = process.memory_info()
+ bytes_to_mb = lambda x: round(x / (1024 * 1024), 2) #noqa
+ return {
+ "rss_mb": bytes_to_mb(mem_info.rss), # Resident Set Size: 实际使用物理内存
+ "vms_mb": bytes_to_mb(mem_info.vms), # Virtual Memory Size: 虚拟内存大小
+ "percent": process.memory_percent() # 进程内存使用百分比
+ }
+
+def get_disk_usage(path="/"):
+ """获取指定路径磁盘使用情况 (单位 GB)"""
+ disk = psutil.disk_usage(path)
+ bytes_to_gb = lambda x: round(x / (1024 * 1024 * 1024), 2) #noqa
+ return {
+ "total_gb": bytes_to_gb(disk.total),
+ "used_gb": bytes_to_gb(disk.used),
+ "free_gb": bytes_to_gb(disk.free),
+ "percent": disk.percent,
+ }
+
+def get_all_basic_info():
+ """获取所有基本信息并封装返回"""
+ # 对于进程CPU使用率,需要先初始化
+ process = psutil.Process(os.getpid())
+ process.cpu_percent(interval=None) # 初始化调用
+ process_cpu = process.cpu_percent(interval=0.1) # 短暂间隔获取
+
+ return {
+ "system_info": get_system_info(),
+ "python_version": get_python_version(),
+ "cpu_usage_percent": get_cpu_usage(),
+ "process_cpu_usage_percent": process_cpu,
+ "memory_usage": get_memory_usage(),
+ "process_memory_usage": get_process_memory_usage(),
+ "disk_usage_root": get_disk_usage("/"),
+ }
+
+def get_all_basic_info_string() -> str:
+ """获取所有基本信息并以带解释的字符串形式返回"""
+ info = get_all_basic_info()
+
+ sys_info = info["system_info"]
+ mem_usage = info["memory_usage"]
+ proc_mem_usage = info["process_memory_usage"]
+ disk_usage = info["disk_usage_root"]
+
+ # 对进程内存使用百分比进行格式化,保留两位小数
+ proc_mem_percent = round(proc_mem_usage['percent'], 2)
+
+ output_string = f"""[系统信息]
+ - 操作系统: {sys_info['system']} (例如: Windows, Linux)
+ - 发行版本: {sys_info['release']} (例如: 11, Ubuntu 20.04)
+ - 详细版本: {sys_info['version']}
+ - 硬件架构: {sys_info['machine']} (例如: AMD64)
+ - 处理器信息: {sys_info['processor']}
+
+[Python 环境]
+ - Python 版本: {info['python_version']}
+
+[CPU 状态]
+ - 系统总 CPU 使用率: {info['cpu_usage_percent']}%
+ - 当前进程 CPU 使用率: {info['process_cpu_usage_percent']}%
+
+[系统内存使用情况]
+ - 总物理内存: {mem_usage['total_mb']} MB
+ - 可用物理内存: {mem_usage['available_mb']} MB
+ - 物理内存使用率: {mem_usage['percent']}%
+ - 已用物理内存: {mem_usage['used_mb']} MB
+ - 空闲物理内存: {mem_usage['free_mb']} MB
+
+[当前进程内存使用情况]
+ - 实际使用物理内存 (RSS): {proc_mem_usage['rss_mb']} MB
+ - 占用虚拟内存 (VMS): {proc_mem_usage['vms_mb']} MB
+ - 进程内存使用率: {proc_mem_percent}%
+
+[磁盘使用情况 (根目录)]
+ - 总空间: {disk_usage['total_gb']} GB
+ - 已用空间: {disk_usage['used_gb']} GB
+ - 可用空间: {disk_usage['free_gb']} GB
+ - 磁盘使用率: {disk_usage['percent']}%
+"""
+ return output_string
+
+if __name__ == '__main__':
+ print(f"System Info: {get_system_info()}")
+ print(f"Python Version: {get_python_version()}")
+ print(f"CPU Usage: {get_cpu_usage()}%")
+ # 第一次调用 process.cpu_percent() 会返回0.0或一个无意义的值,需要间隔一段时间再调用
+ # 或者在初始化Process对象后,先调用一次cpu_percent(interval=None),然后再调用cpu_percent(interval=1)
+ current_process = psutil.Process(os.getpid())
+ current_process.cpu_percent(interval=None) # 初始化
+ print(f"Process CPU Usage: {current_process.cpu_percent(interval=1)}%") # 实际获取
+
+ memory_usage_info = get_memory_usage()
+ print(f"Memory Usage: Total={memory_usage_info['total_mb']}MB, Used={memory_usage_info['used_mb']}MB, Percent={memory_usage_info['percent']}%")
+
+ process_memory_info = get_process_memory_usage()
+ print(f"Process Memory Usage: RSS={process_memory_info['rss_mb']}MB, VMS={process_memory_info['vms_mb']}MB, Percent={process_memory_info['percent']}%")
+
+ disk_usage_info = get_disk_usage('/')
+ print(f"Disk Usage (Root): Total={disk_usage_info['total_gb']}GB, Used={disk_usage_info['used_gb']}GB, Percent={disk_usage_info['percent']}%")
+
+ print("\n--- All Basic Info (JSON) ---")
+ all_info = get_all_basic_info()
+ import json
+ print(json.dumps(all_info, indent=4, ensure_ascii=False))
+
+ print("\n--- All Basic Info (String with Explanations) ---")
+ info_string = get_all_basic_info_string()
+ print(info_string)
diff --git a/src/api/config_api.py b/src/api/config_api.py
index 581c05a0..57780ad2 100644
--- a/src/api/config_api.py
+++ b/src/api/config_api.py
@@ -10,7 +10,7 @@ ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
class APIBotConfig:
"""机器人配置类"""
- INNER_VERSION: Version # 配置文件内部版本号
+ INNER_VERSION: str # 配置文件内部版本号(toml为字符串)
MAI_VERSION: str # 硬编码的版本信息
# bot
@@ -34,28 +34,28 @@ class APIBotConfig:
appearance: str # 外貌特征描述
# schedule
- ENABLE_SCHEDULE_GEN: bool # 是否启用日程生成
- ENABLE_SCHEDULE_INTERACTION: bool # 是否启用日程交互
- PROMPT_SCHEDULE_GEN: str # 日程生成提示词
- SCHEDULE_DOING_UPDATE_INTERVAL: int # 日程进行中更新间隔
- SCHEDULE_TEMPERATURE: float # 日程生成温度
- TIME_ZONE: str # 时区
+ enable_schedule_gen: bool # 是否启用日程表
+ enable_schedule_interaction: bool # 日程表是否影响回复模式
+ prompt_schedule_gen: str # 日程生成提示词
+ schedule_doing_update_interval: int # 日程表更新间隔(秒)
+ schedule_temperature: float # 日程表温度
+ time_zone: str # 时区
# platforms
platforms: Dict[str, str] # 平台信息
# chat
- allow_focus_mode: bool # 是否允许专注模式
- base_normal_chat_num: int # 基础普通聊天次数
- base_focused_chat_num: int # 基础专注聊天次数
- observation_context_size: int # 观察上下文大小
+ allow_focus_mode: bool # 是否允许专注聊天状态
+ base_normal_chat_num: int # 最多允许多少个群进行普通聊天
+ base_focused_chat_num: int # 最多允许多少个群进行专注聊天
+ observation_context_size: int # 观察到的最长上下文大小
message_buffer: bool # 是否启用消息缓冲
ban_words: List[str] # 禁止词列表
ban_msgs_regex: List[str] # 禁止消息的正则表达式列表
# normal_chat
- MODEL_R1_PROBABILITY: float # 模型推理概率
- MODEL_V3_PROBABILITY: float # 模型普通概率
+ model_reasoning_probability: float # 推理模型概率
+ model_normal_probability: float # 普通模型概率
emoji_chance: float # 表情符号出现概率
thinking_timeout: int # 思考超时时间
willing_mode: str # 意愿模式
@@ -63,8 +63,8 @@ class APIBotConfig:
response_interested_rate_amplifier: float # 回复兴趣率放大器
down_frequency_rate: float # 降低频率率
emoji_response_penalty: float # 表情回复惩罚
- mentioned_bot_inevitable_reply: bool # 提到机器人时是否必定回复
- at_bot_inevitable_reply: bool # @机器人时是否必定回复
+ mentioned_bot_inevitable_reply: bool # 提及 bot 必然回复
+ at_bot_inevitable_reply: bool # @bot 必然回复
# focus_chat
reply_trigger_threshold: float # 回复触发阈值
@@ -78,24 +78,25 @@ class APIBotConfig:
# emoji
max_emoji_num: int # 最大表情符号数量
max_reach_deletion: bool # 达到最大数量时是否删除
- EMOJI_CHECK_INTERVAL: int # 表情检查间隔
- EMOJI_REGISTER_INTERVAL: Optional[int] # 表情注册间隔(兼容性保留)
- EMOJI_SAVE: bool # 是否保存表情
- EMOJI_CHECK: bool # 是否检查表情
- EMOJI_CHECK_PROMPT: str # 表情检查提示词
+ check_interval: int # 检查表情包的时间间隔(分钟)
+ save_pic: bool # 是否保存图片
+ save_emoji: bool # 是否保存表情包
+ steal_emoji: bool # 是否偷取表情包
+ enable_check: bool # 是否启用表情包过滤
+ check_prompt: str # 表情包过滤要求
# memory
- build_memory_interval: int # 构建记忆间隔
- memory_build_distribution: List[float] # 记忆构建分布
- build_memory_sample_num: int # 构建记忆样本数量
- build_memory_sample_length: int # 构建记忆样本长度
+ build_memory_interval: int # 记忆构建间隔
+ build_memory_distribution: List[float] # 记忆构建分布
+ build_memory_sample_num: int # 采样数量
+ build_memory_sample_length: int # 采样长度
memory_compress_rate: float # 记忆压缩率
- forget_memory_interval: int # 忘记记忆间隔
- memory_forget_time: int # 记忆忘记时间
- memory_forget_percentage: float # 记忆忘记百分比
- consolidate_memory_interval: int # 巩固记忆间隔
- consolidation_similarity_threshold: float # 巩固相似度阈值
- consolidation_check_percentage: float # 巩固检查百分比
+ forget_memory_interval: int # 记忆遗忘间隔
+ memory_forget_time: int # 记忆遗忘时间(小时)
+ memory_forget_percentage: float # 记忆遗忘比例
+ consolidate_memory_interval: int # 记忆整合间隔
+ consolidation_similarity_threshold: float # 相似度阈值
+ consolidation_check_percentage: float # 检查节点比例
memory_ban_words: List[str] # 记忆禁止词列表
# mood
@@ -128,21 +129,19 @@ class APIBotConfig:
# experimental
enable_friend_chat: bool # 是否启用好友聊天
talk_allowed_private: List[int] # 允许私聊的QQ号列表
- enable_pfc_chatting: bool # 是否启用PFC聊天
+ pfc_chatting: bool # 是否启用PFC聊天
# 模型配置
llm_reasoning: Dict[str, Any] # 推理模型配置
llm_normal: Dict[str, Any] # 普通模型配置
llm_topic_judge: Dict[str, Any] # 主题判断模型配置
llm_summary: Dict[str, Any] # 总结模型配置
- llm_emotion_judge: Optional[Dict[str, Any]] # 情绪判断模型配置(兼容性保留)
- embedding: Dict[str, Any] # 嵌入模型配置
vlm: Dict[str, Any] # VLM模型配置
- moderation: Optional[Dict[str, Any]] # 审核模型配置(兼容性保留)
+ llm_heartflow: Dict[str, Any] # 心流模型配置
llm_observation: Dict[str, Any] # 观察模型配置
llm_sub_heartflow: Dict[str, Any] # 子心流模型配置
- llm_heartflow: Dict[str, Any] # 心流模型配置
llm_plan: Optional[Dict[str, Any]] # 计划模型配置
+ embedding: Dict[str, Any] # 嵌入模型配置
llm_PFC_action_planner: Optional[Dict[str, Any]] # PFC行动计划模型配置
llm_PFC_chat: Optional[Dict[str, Any]] # PFC聊天模型配置
llm_PFC_reply_checker: Optional[Dict[str, Any]] # PFC回复检查模型配置
@@ -150,6 +149,63 @@ class APIBotConfig:
api_urls: Optional[Dict[str, str]] # API地址配置
+ @staticmethod
+ def validate_config(config: dict):
+ """
+ 校验传入的 toml 配置字典是否合法。
+ :param config: toml库load后的配置字典
+ :raises: ValueError, KeyError, TypeError
+ """
+ # 检查主层级
+ required_sections = [
+ "inner", "bot", "groups", "personality", "identity", "schedule",
+ "platforms", "chat", "normal_chat", "focus_chat", "emoji", "memory",
+ "mood", "keywords_reaction", "chinese_typo", "response_splitter",
+ "remote", "experimental", "model"
+ ]
+ for section in required_sections:
+ if section not in config:
+ raise KeyError(f"缺少配置段: [{section}]")
+
+ # 检查部分关键字段
+ if "version" not in config["inner"]:
+ raise KeyError("缺少 inner.version 字段")
+ if not isinstance(config["inner"]["version"], str):
+ raise TypeError("inner.version 必须为字符串")
+
+ if "qq" not in config["bot"]:
+ raise KeyError("缺少 bot.qq 字段")
+ if not isinstance(config["bot"]["qq"], int):
+ raise TypeError("bot.qq 必须为整数")
+
+ if "personality_core" not in config["personality"]:
+ raise KeyError("缺少 personality.personality_core 字段")
+ if not isinstance(config["personality"]["personality_core"], str):
+ raise TypeError("personality.personality_core 必须为字符串")
+
+ if "identity_detail" not in config["identity"]:
+ raise KeyError("缺少 identity.identity_detail 字段")
+ if not isinstance(config["identity"]["identity_detail"], list):
+ raise TypeError("identity.identity_detail 必须为列表")
+
+ # 可继续添加更多字段的类型和值检查
+ # ...
+
+ # 检查模型配置
+ model_keys = [
+ "llm_reasoning", "llm_normal", "llm_topic_judge", "llm_summary",
+ "vlm", "llm_heartflow", "llm_observation", "llm_sub_heartflow",
+ "embedding"
+ ]
+ if "model" not in config:
+ raise KeyError("缺少 [model] 配置段")
+ for key in model_keys:
+ if key not in config["model"]:
+ raise KeyError(f"缺少 model.{key} 配置")
+
+ # 检查通过
+ return True
+
@strawberry.type
class APIEnvConfig:
@@ -182,6 +238,56 @@ class APIEnvConfig:
def get_env(self) -> str:
return "env"
+ @staticmethod
+ def validate_config(config: dict):
+ """
+ 校验环境变量配置字典是否合法。
+ :param config: 环境变量配置字典
+ :raises: KeyError, TypeError
+ """
+ required_fields = [
+ "HOST", "PORT", "PLUGINS", "MONGODB_HOST", "MONGODB_PORT", "DATABASE_NAME",
+ "CHAT_ANY_WHERE_BASE_URL", "SILICONFLOW_BASE_URL", "DEEP_SEEK_BASE_URL"
+ ]
+ for field in required_fields:
+ if field not in config:
+ raise KeyError(f"缺少环境变量配置字段: {field}")
+
+ if not isinstance(config["HOST"], str):
+ raise TypeError("HOST 必须为字符串")
+ if not isinstance(config["PORT"], int):
+ raise TypeError("PORT 必须为整数")
+ if not isinstance(config["PLUGINS"], list):
+ raise TypeError("PLUGINS 必须为列表")
+ if not isinstance(config["MONGODB_HOST"], str):
+ raise TypeError("MONGODB_HOST 必须为字符串")
+ if not isinstance(config["MONGODB_PORT"], int):
+ raise TypeError("MONGODB_PORT 必须为整数")
+ if not isinstance(config["DATABASE_NAME"], str):
+ raise TypeError("DATABASE_NAME 必须为字符串")
+ if not isinstance(config["CHAT_ANY_WHERE_BASE_URL"], str):
+ raise TypeError("CHAT_ANY_WHERE_BASE_URL 必须为字符串")
+ if not isinstance(config["SILICONFLOW_BASE_URL"], str):
+ raise TypeError("SILICONFLOW_BASE_URL 必须为字符串")
+ if not isinstance(config["DEEP_SEEK_BASE_URL"], str):
+ raise TypeError("DEEP_SEEK_BASE_URL 必须为字符串")
+
+ # 可选字段类型检查
+ optional_str_fields = [
+ "DEEP_SEEK_KEY", "CHAT_ANY_WHERE_KEY", "SILICONFLOW_KEY",
+ "CONSOLE_LOG_LEVEL", "FILE_LOG_LEVEL",
+ "DEFAULT_CONSOLE_LOG_LEVEL", "DEFAULT_FILE_LOG_LEVEL"
+ ]
+ for field in optional_str_fields:
+ if field in config and config[field] is not None and not isinstance(config[field], str):
+ raise TypeError(f"{field} 必须为字符串或None")
+
+ if "SIMPLE_OUTPUT" in config and config["SIMPLE_OUTPUT"] is not None and not isinstance(config["SIMPLE_OUTPUT"], bool):
+ raise TypeError("SIMPLE_OUTPUT 必须为布尔值或None")
+
+ # 检查通过
+ return True
+
print("当前路径:")
print(ROOT_PATH)
diff --git a/src/api/main.py b/src/api/main.py
index 4378ff1e..48b03b58 100644
--- a/src/api/main.py
+++ b/src/api/main.py
@@ -16,6 +16,7 @@ from src.api.apiforgui import (
get_all_states,
)
from src.heart_flow.sub_heartflow import ChatState
+from src.api.basic_info_api import get_all_basic_info # 新增导入
# import uvicorn
# import os
@@ -97,6 +98,18 @@ async def get_all_states_api():
return {"status": "failed", "reason": "failed to get all states"}
+@router.get("/info")
+async def get_system_basic_info():
+ """获取系统基本信息"""
+ logger.info("请求系统基本信息")
+ try:
+ info = get_all_basic_info()
+ return {"status": "success", "data": info}
+ except Exception as e:
+ logger.error(f"获取系统基本信息失败: {e}")
+ return {"status": "failed", "reason": str(e)}
+
+
def start_api_server():
"""启动API服务器"""
global_server.register_router(router, prefix="/api/v1")
diff --git a/src/common/server.py b/src/common/server.py
index ff6106a7..9f4a9459 100644
--- a/src/common/server.py
+++ b/src/common/server.py
@@ -1,4 +1,5 @@
from fastapi import FastAPI, APIRouter
+from fastapi.middleware.cors import CORSMiddleware # 新增导入
from typing import Optional
from uvicorn import Config, Server as UvicornServer
import os
@@ -15,6 +16,21 @@ class Server:
self._server: Optional[UvicornServer] = None
self.set_address(host, port)
+ # 配置 CORS
+ origins = [
+ "http://localhost:3000", # 允许的前端源
+ "http://127.0.0.1:3000",
+ # 在生产环境中,您应该添加实际的前端域名
+ ]
+
+ self.app.add_middleware(
+ CORSMiddleware,
+ allow_origins=origins,
+ allow_credentials=True, # 是否支持 cookie
+ allow_methods=["*"], # 允许所有 HTTP 方法
+ allow_headers=["*"], # 允许所有 HTTP 请求头
+ )
+
def register_router(self, router: APIRouter, prefix: str = ""):
"""注册路由
From c86b043f28d585517843666041bba0488c0fa146 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A2=A8=E6=A2=93=E6=9F=92?= <1787882683@qq.com>
Date: Sun, 11 May 2025 17:13:43 +0800
Subject: [PATCH 3/5] fix: Ruff
---
src/api/config_api.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/api/config_api.py b/src/api/config_api.py
index 57780ad2..6c4a9b8e 100644
--- a/src/api/config_api.py
+++ b/src/api/config_api.py
@@ -1,6 +1,6 @@
from typing import List, Optional, Dict, Any
import strawberry
-from packaging.version import Version
+# from packaging.version import Version
import os
ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
From f71242d5714f3b26d179db93e40e70526e8690bf Mon Sep 17 00:00:00 2001
From: "github-actions[bot]"
Date: Sun, 11 May 2025 09:13:56 +0000
Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=A4=96=20=E8=87=AA=E5=8A=A8=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E5=8C=96=E4=BB=A3=E7=A0=81=20[skip=20ci]?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/api/basic_info_api.py | 89 +++++++++++++++++++++++----------------
src/api/config_api.py | 63 +++++++++++++++++++++------
2 files changed, 103 insertions(+), 49 deletions(-)
diff --git a/src/api/basic_info_api.py b/src/api/basic_info_api.py
index 73de18f8..4e5fa4c7 100644
--- a/src/api/basic_info_api.py
+++ b/src/api/basic_info_api.py
@@ -3,6 +3,7 @@ import psutil
import sys
import os
+
def get_system_info():
"""获取操作系统信息"""
return {
@@ -13,23 +14,27 @@ def get_system_info():
"processor": platform.processor(),
}
+
def get_python_version():
"""获取 Python 版本信息"""
return sys.version
+
def get_cpu_usage():
"""获取系统总CPU使用率"""
return psutil.cpu_percent(interval=1)
+
def get_process_cpu_usage():
"""获取当前进程CPU使用率"""
process = psutil.Process(os.getpid())
return process.cpu_percent(interval=1)
+
def get_memory_usage():
"""获取系统内存使用情况 (单位 MB)"""
mem = psutil.virtual_memory()
- bytes_to_mb = lambda x: round(x / (1024 * 1024), 2) #noqa
+ bytes_to_mb = lambda x: round(x / (1024 * 1024), 2) # noqa
return {
"total_mb": bytes_to_mb(mem.total),
"available_mb": bytes_to_mb(mem.available),
@@ -38,21 +43,23 @@ def get_memory_usage():
"free_mb": bytes_to_mb(mem.free),
}
+
def get_process_memory_usage():
"""获取当前进程内存使用情况 (单位 MB)"""
process = psutil.Process(os.getpid())
mem_info = process.memory_info()
- bytes_to_mb = lambda x: round(x / (1024 * 1024), 2) #noqa
+ bytes_to_mb = lambda x: round(x / (1024 * 1024), 2) # noqa
return {
"rss_mb": bytes_to_mb(mem_info.rss), # Resident Set Size: 实际使用物理内存
"vms_mb": bytes_to_mb(mem_info.vms), # Virtual Memory Size: 虚拟内存大小
- "percent": process.memory_percent() # 进程内存使用百分比
+ "percent": process.memory_percent(), # 进程内存使用百分比
}
+
def get_disk_usage(path="/"):
"""获取指定路径磁盘使用情况 (单位 GB)"""
disk = psutil.disk_usage(path)
- bytes_to_gb = lambda x: round(x / (1024 * 1024 * 1024), 2) #noqa
+ bytes_to_gb = lambda x: round(x / (1024 * 1024 * 1024), 2) # noqa
return {
"total_gb": bytes_to_gb(disk.total),
"used_gb": bytes_to_gb(disk.used),
@@ -60,12 +67,13 @@ def get_disk_usage(path="/"):
"percent": disk.percent,
}
+
def get_all_basic_info():
"""获取所有基本信息并封装返回"""
# 对于进程CPU使用率,需要先初始化
process = psutil.Process(os.getpid())
process.cpu_percent(interval=None) # 初始化调用
- process_cpu = process.cpu_percent(interval=0.1) # 短暂间隔获取
+ process_cpu = process.cpu_percent(interval=0.1) # 短暂间隔获取
return {
"system_info": get_system_info(),
@@ -77,74 +85,83 @@ def get_all_basic_info():
"disk_usage_root": get_disk_usage("/"),
}
+
def get_all_basic_info_string() -> str:
"""获取所有基本信息并以带解释的字符串形式返回"""
info = get_all_basic_info()
-
+
sys_info = info["system_info"]
mem_usage = info["memory_usage"]
proc_mem_usage = info["process_memory_usage"]
disk_usage = info["disk_usage_root"]
# 对进程内存使用百分比进行格式化,保留两位小数
- proc_mem_percent = round(proc_mem_usage['percent'], 2)
+ proc_mem_percent = round(proc_mem_usage["percent"], 2)
output_string = f"""[系统信息]
- - 操作系统: {sys_info['system']} (例如: Windows, Linux)
- - 发行版本: {sys_info['release']} (例如: 11, Ubuntu 20.04)
- - 详细版本: {sys_info['version']}
- - 硬件架构: {sys_info['machine']} (例如: AMD64)
- - 处理器信息: {sys_info['processor']}
+ - 操作系统: {sys_info["system"]} (例如: Windows, Linux)
+ - 发行版本: {sys_info["release"]} (例如: 11, Ubuntu 20.04)
+ - 详细版本: {sys_info["version"]}
+ - 硬件架构: {sys_info["machine"]} (例如: AMD64)
+ - 处理器信息: {sys_info["processor"]}
[Python 环境]
- - Python 版本: {info['python_version']}
+ - Python 版本: {info["python_version"]}
[CPU 状态]
- - 系统总 CPU 使用率: {info['cpu_usage_percent']}%
- - 当前进程 CPU 使用率: {info['process_cpu_usage_percent']}%
+ - 系统总 CPU 使用率: {info["cpu_usage_percent"]}%
+ - 当前进程 CPU 使用率: {info["process_cpu_usage_percent"]}%
[系统内存使用情况]
- - 总物理内存: {mem_usage['total_mb']} MB
- - 可用物理内存: {mem_usage['available_mb']} MB
- - 物理内存使用率: {mem_usage['percent']}%
- - 已用物理内存: {mem_usage['used_mb']} MB
- - 空闲物理内存: {mem_usage['free_mb']} MB
+ - 总物理内存: {mem_usage["total_mb"]} MB
+ - 可用物理内存: {mem_usage["available_mb"]} MB
+ - 物理内存使用率: {mem_usage["percent"]}%
+ - 已用物理内存: {mem_usage["used_mb"]} MB
+ - 空闲物理内存: {mem_usage["free_mb"]} MB
[当前进程内存使用情况]
- - 实际使用物理内存 (RSS): {proc_mem_usage['rss_mb']} MB
- - 占用虚拟内存 (VMS): {proc_mem_usage['vms_mb']} MB
+ - 实际使用物理内存 (RSS): {proc_mem_usage["rss_mb"]} MB
+ - 占用虚拟内存 (VMS): {proc_mem_usage["vms_mb"]} MB
- 进程内存使用率: {proc_mem_percent}%
[磁盘使用情况 (根目录)]
- - 总空间: {disk_usage['total_gb']} GB
- - 已用空间: {disk_usage['used_gb']} GB
- - 可用空间: {disk_usage['free_gb']} GB
- - 磁盘使用率: {disk_usage['percent']}%
+ - 总空间: {disk_usage["total_gb"]} GB
+ - 已用空间: {disk_usage["used_gb"]} GB
+ - 可用空间: {disk_usage["free_gb"]} GB
+ - 磁盘使用率: {disk_usage["percent"]}%
"""
return output_string
-if __name__ == '__main__':
+
+if __name__ == "__main__":
print(f"System Info: {get_system_info()}")
print(f"Python Version: {get_python_version()}")
print(f"CPU Usage: {get_cpu_usage()}%")
# 第一次调用 process.cpu_percent() 会返回0.0或一个无意义的值,需要间隔一段时间再调用
# 或者在初始化Process对象后,先调用一次cpu_percent(interval=None),然后再调用cpu_percent(interval=1)
current_process = psutil.Process(os.getpid())
- current_process.cpu_percent(interval=None) # 初始化
- print(f"Process CPU Usage: {current_process.cpu_percent(interval=1)}%") # 实际获取
-
+ current_process.cpu_percent(interval=None) # 初始化
+ print(f"Process CPU Usage: {current_process.cpu_percent(interval=1)}%") # 实际获取
+
memory_usage_info = get_memory_usage()
- print(f"Memory Usage: Total={memory_usage_info['total_mb']}MB, Used={memory_usage_info['used_mb']}MB, Percent={memory_usage_info['percent']}%")
-
+ print(
+ f"Memory Usage: Total={memory_usage_info['total_mb']}MB, Used={memory_usage_info['used_mb']}MB, Percent={memory_usage_info['percent']}%"
+ )
+
process_memory_info = get_process_memory_usage()
- print(f"Process Memory Usage: RSS={process_memory_info['rss_mb']}MB, VMS={process_memory_info['vms_mb']}MB, Percent={process_memory_info['percent']}%")
-
- disk_usage_info = get_disk_usage('/')
- print(f"Disk Usage (Root): Total={disk_usage_info['total_gb']}GB, Used={disk_usage_info['used_gb']}GB, Percent={disk_usage_info['percent']}%")
+ print(
+ f"Process Memory Usage: RSS={process_memory_info['rss_mb']}MB, VMS={process_memory_info['vms_mb']}MB, Percent={process_memory_info['percent']}%"
+ )
+
+ disk_usage_info = get_disk_usage("/")
+ print(
+ f"Disk Usage (Root): Total={disk_usage_info['total_gb']}GB, Used={disk_usage_info['used_gb']}GB, Percent={disk_usage_info['percent']}%"
+ )
print("\n--- All Basic Info (JSON) ---")
all_info = get_all_basic_info()
import json
+
print(json.dumps(all_info, indent=4, ensure_ascii=False))
print("\n--- All Basic Info (String with Explanations) ---")
diff --git a/src/api/config_api.py b/src/api/config_api.py
index 6c4a9b8e..27593804 100644
--- a/src/api/config_api.py
+++ b/src/api/config_api.py
@@ -1,5 +1,6 @@
from typing import List, Optional, Dict, Any
import strawberry
+
# from packaging.version import Version
import os
@@ -158,10 +159,25 @@ class APIBotConfig:
"""
# 检查主层级
required_sections = [
- "inner", "bot", "groups", "personality", "identity", "schedule",
- "platforms", "chat", "normal_chat", "focus_chat", "emoji", "memory",
- "mood", "keywords_reaction", "chinese_typo", "response_splitter",
- "remote", "experimental", "model"
+ "inner",
+ "bot",
+ "groups",
+ "personality",
+ "identity",
+ "schedule",
+ "platforms",
+ "chat",
+ "normal_chat",
+ "focus_chat",
+ "emoji",
+ "memory",
+ "mood",
+ "keywords_reaction",
+ "chinese_typo",
+ "response_splitter",
+ "remote",
+ "experimental",
+ "model",
]
for section in required_sections:
if section not in config:
@@ -193,9 +209,15 @@ class APIBotConfig:
# 检查模型配置
model_keys = [
- "llm_reasoning", "llm_normal", "llm_topic_judge", "llm_summary",
- "vlm", "llm_heartflow", "llm_observation", "llm_sub_heartflow",
- "embedding"
+ "llm_reasoning",
+ "llm_normal",
+ "llm_topic_judge",
+ "llm_summary",
+ "vlm",
+ "llm_heartflow",
+ "llm_observation",
+ "llm_sub_heartflow",
+ "embedding",
]
if "model" not in config:
raise KeyError("缺少 [model] 配置段")
@@ -246,8 +268,15 @@ class APIEnvConfig:
:raises: KeyError, TypeError
"""
required_fields = [
- "HOST", "PORT", "PLUGINS", "MONGODB_HOST", "MONGODB_PORT", "DATABASE_NAME",
- "CHAT_ANY_WHERE_BASE_URL", "SILICONFLOW_BASE_URL", "DEEP_SEEK_BASE_URL"
+ "HOST",
+ "PORT",
+ "PLUGINS",
+ "MONGODB_HOST",
+ "MONGODB_PORT",
+ "DATABASE_NAME",
+ "CHAT_ANY_WHERE_BASE_URL",
+ "SILICONFLOW_BASE_URL",
+ "DEEP_SEEK_BASE_URL",
]
for field in required_fields:
if field not in config:
@@ -274,15 +303,23 @@ class APIEnvConfig:
# 可选字段类型检查
optional_str_fields = [
- "DEEP_SEEK_KEY", "CHAT_ANY_WHERE_KEY", "SILICONFLOW_KEY",
- "CONSOLE_LOG_LEVEL", "FILE_LOG_LEVEL",
- "DEFAULT_CONSOLE_LOG_LEVEL", "DEFAULT_FILE_LOG_LEVEL"
+ "DEEP_SEEK_KEY",
+ "CHAT_ANY_WHERE_KEY",
+ "SILICONFLOW_KEY",
+ "CONSOLE_LOG_LEVEL",
+ "FILE_LOG_LEVEL",
+ "DEFAULT_CONSOLE_LOG_LEVEL",
+ "DEFAULT_FILE_LOG_LEVEL",
]
for field in optional_str_fields:
if field in config and config[field] is not None and not isinstance(config[field], str):
raise TypeError(f"{field} 必须为字符串或None")
- if "SIMPLE_OUTPUT" in config and config["SIMPLE_OUTPUT"] is not None and not isinstance(config["SIMPLE_OUTPUT"], bool):
+ if (
+ "SIMPLE_OUTPUT" in config
+ and config["SIMPLE_OUTPUT"] is not None
+ and not isinstance(config["SIMPLE_OUTPUT"], bool)
+ ):
raise TypeError("SIMPLE_OUTPUT 必须为布尔值或None")
# 检查通过
From 08da999105e9a3ff27c42511865be0542bae48bd Mon Sep 17 00:00:00 2001
From: UnCLAS-Prommer
Date: Sun, 11 May 2025 20:13:09 +0800
Subject: [PATCH 5/5] =?UTF-8?q?=E8=A1=A5=E5=85=A8requirements.txt?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
requirements.txt | Bin 760 -> 774 bytes
1 file changed, 0 insertions(+), 0 deletions(-)
diff --git a/requirements.txt b/requirements.txt
index 8779b40e12d8f03dfe833f5736203c1720685508..12c557ded603e4fab6a0ce71154c4165229be4ae 100644
GIT binary patch
delta 22
dcmeyt+Qzow2NNF`LjglELn%WELncEG0{~H}1?>O;
delta 7
OcmZo;`@y>52NM7c!viz`