From f71242d5714f3b26d179db93e40e70526e8690bf Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 11 May 2025 09:13:56 +0000 Subject: [PATCH] =?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 73de18f80..4e5fa4c7d 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 6c4a9b8e1..275938045 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") # 检查通过