From 63f8a1682981a9109ef9e8c96baf37baaaa002d8 Mon Sep 17 00:00:00 2001 From: wangying <16269138+wwwangyyy@user.noreply.gitee.com> Date: Wed, 29 Oct 2025 02:58:24 +0000 Subject: [PATCH] update core/agent/main.py. Signed-off-by: wangying <16269138+wwwangyyy@user.noreply.gitee.com> --- core/agent/main.py | 103 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 82 insertions(+), 21 deletions(-) diff --git a/core/agent/main.py b/core/agent/main.py index 2ef6487a..588b57b6 100644 --- a/core/agent/main.py +++ b/core/agent/main.py @@ -1,7 +1,16 @@ #!/usr/bin/env python3 """ -Agent main entry point -Load configuration files and start FastAPI service +Agent 主入口点脚本 +功能:加载配置文件并启动 FastAPI 服务 + +主要职责: +1. 环境变量配置管理 +2. Python路径设置 +3. FastAPI服务启动 +4. 配置验证和错误处理 + +作者:人员1 - core核心功能 +标注日期:2024年 """ import os @@ -11,41 +20,67 @@ from pathlib import Path def load_env_file(env_file: str) -> None: - """Load environment variables from .env file""" + """ + 从.env文件加载环境变量 + + 参数: + env_file: 环境配置文件路径 + + 功能说明: + - 检查配置文件是否存在 + - 解析键值对格式的环境变量 + - 跳过空行和注释行 + - 避免覆盖已存在的环境变量 + - 提供详细的加载日志输出 + """ + # 检查配置文件是否存在 if not os.path.exists(env_file): print(f"❌ Configuration file {env_file} does not exist") return print(f"📋 Loading configuration file: {env_file}") + # 逐行读取配置文件 with open(env_file, "r", encoding="utf-8") as f: for line_num, line in enumerate(f, 1): line = line.strip() - # Skip empty lines and comments + # 跳过空行和注释行 if not line or line.startswith("#"): continue - # Parse environment variables + # 解析环境变量键值对 if "=" in line: key, value = line.split("=", 1) key = key.strip() value = value.strip() + # 检查环境变量是否已存在 if key in os.environ: print(f" 🔄 {key}={os.environ[key]} (using existing env var)") else: + # 设置新的环境变量 os.environ[key] = value print(f" ✅ {key}={value} (loaded from config)") else: + # 处理格式错误的行 print(f" ⚠️ Line {line_num} format error: {line}") def setup_python_path() -> None: - """Set up Python path""" + """ + 设置Python路径 + + 功能说明: + - 获取项目根目录 + - 更新PYTHONPATH环境变量 + - 确保项目模块可以被正确导入 + """ + # 获取项目根目录 project_root = Path(__file__).parent python_path = os.environ.get("PYTHONPATH", "") + # 如果项目根目录不在PYTHONPATH中,则添加 if str(project_root) not in python_path: if python_path: os.environ["PYTHONPATH"] = f"{project_root}:{python_path}" @@ -55,23 +90,32 @@ def setup_python_path() -> None: def start_service() -> None: - """Start FastAPI service""" + """ + 启动FastAPI服务 + + 功能说明: + - 显示关键环境变量配置 + - 启动FastAPI应用进程 + - 处理服务启动异常 + - 支持优雅的进程终止 + """ print("\n🚀 Starting Agent service...") - # Display key environment variables + # 定义需要显示的关键环境变量列表 env_vars = [ - "PYTHONUNBUFFERED", - "polaris_cluster", - "polaris_url", - "polaris_username", - "RUN_ENVIRON", - "USE_POLARIS", + "PYTHONUNBUFFERED", # Python无缓冲输出控制 + "polaris_cluster", # Polaris集群配置 + "polaris_url", # Polaris服务地址 + "polaris_username", # Polaris用户名 + "RUN_ENVIRON", # 运行环境标识 + "USE_POLARIS", # Polaris功能开关 ] + # 显示环境变量配置信息 print("📋 Environment configuration:") for var in env_vars: value = os.environ.get(var, "None") - # Hide passwords + # 敏感信息脱敏处理 if "password" in var.lower(): value = "***" print(f" - {var}: {value}") @@ -79,31 +123,48 @@ def start_service() -> None: print("") try: - # Start FastAPI application + # 启动FastAPI应用进程 + # 使用当前Python解释器执行app.py subprocess.run([sys.executable, "api/app.py"], check=True) except subprocess.CalledProcessError as e: + # 处理子进程执行错误 print(f"❌ Service startup failed: {e}") sys.exit(1) except KeyboardInterrupt: + # 处理用户中断信号(Ctrl+C) print("\n🛑 Service stopped") sys.exit(0) def main() -> None: - """Main function""" + """ + 主函数 - 程序执行入口点 + + 执行流程: + 1. 显示启动信息 + 2. 设置Python路径 + 3. 加载环境配置 + 4. 启动服务 + + 异常处理: + - 配置加载失败会继续执行 + - 服务启动失败会退出程序 + """ print("🌟 Agent Development Environment Launcher") print("=" * 50) - # Set up Python path + # 步骤1: 设置Python路径 setup_python_path() - # Load environment configuration + # 步骤2: 加载环境配置文件 config_file = Path(__file__).parent / "config.env" load_env_file(str(config_file)) - # Start service + # 步骤3: 启动FastAPI服务 start_service() +# 程序入口点 if __name__ == "__main__": - main() + # 执行主函数 + main() \ No newline at end of file -- Gitee