128 lines
4.4 KiB
Python
128 lines
4.4 KiB
Python
|
|
#!/usr/bin/env python
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
"""
|
|||
|
|
配置健康检查脚本
|
|||
|
|
|
|||
|
|
基于 src.config.unified_config 对当前环境配置进行简单体检,
|
|||
|
|
输出人类可读的检查结果,供 config-audit Skill 调用。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import os
|
|||
|
|
import sys
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
|
|||
|
|
def add_project_root_to_path():
|
|||
|
|
# 假定脚本位于 .claude/skills/config-audit/scripts/ 下
|
|||
|
|
script_path = Path(__file__).resolve()
|
|||
|
|
project_root = script_path.parents[4] # 回到项目根目录
|
|||
|
|
if str(project_root) not in sys.path:
|
|||
|
|
sys.path.insert(0, str(project_root))
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
add_project_root_to_path()
|
|||
|
|
|
|||
|
|
print("=== TSP Assistant 配置健康检查 ===\n")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
from src.config.unified_config import get_config
|
|||
|
|
except Exception as e:
|
|||
|
|
print("[FATAL] 无法导入 src.config.unified_config.get_config:")
|
|||
|
|
print(f" 错误:{e}")
|
|||
|
|
print("\n请检查 Python 路径与项目结构。")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
cfg = get_config()
|
|||
|
|
except Exception as e:
|
|||
|
|
print("[FATAL] get_config() 调用失败,可能缺少关键环境变量:")
|
|||
|
|
print(f" 错误:{e}")
|
|||
|
|
print("\n请检查 .env / 环境变量是否包含 DATABASE_URL 等必需项。")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
problems = []
|
|||
|
|
|
|||
|
|
# Database
|
|||
|
|
print("---- 数据库配置 (database) ----")
|
|||
|
|
db_url = os.getenv("DATABASE_URL", "")
|
|||
|
|
if not db_url:
|
|||
|
|
print(" [ERROR] 未设置 DATABASE_URL,无法连接数据库。")
|
|||
|
|
problems.append("缺少 DATABASE_URL")
|
|||
|
|
else:
|
|||
|
|
# 不打印完整 URL,只提示前缀
|
|||
|
|
prefix = db_url.split("://")[0] if "://" in db_url else "未知协议"
|
|||
|
|
print(f" [OK] 已配置 DATABASE_URL(协议前缀:{prefix}://...)")
|
|||
|
|
|
|||
|
|
# LLM
|
|||
|
|
print("\n---- LLM 配置 (llm) ----")
|
|||
|
|
llm_api_key = os.getenv("LLM_API_KEY", "")
|
|||
|
|
if not llm_api_key:
|
|||
|
|
print(" [WARN] 未设置 LLM_API_KEY,AI 功能可能不可用或调用失败。")
|
|||
|
|
problems.append("缺少 LLM_API_KEY")
|
|||
|
|
else:
|
|||
|
|
print(" [OK] 已配置 LLM_API_KEY(具体值已隐藏)。")
|
|||
|
|
|
|||
|
|
provider = os.getenv("LLM_PROVIDER", cfg.llm.provider if hasattr(cfg, "llm") else "")
|
|||
|
|
model = os.getenv("LLM_MODEL", cfg.llm.model if hasattr(cfg, "llm") else "")
|
|||
|
|
print(f" Provider: {provider or '未配置'}")
|
|||
|
|
print(f" Model : {model or '未配置'}")
|
|||
|
|
|
|||
|
|
# Server
|
|||
|
|
print("\n---- 服务配置 (server) ----")
|
|||
|
|
try:
|
|||
|
|
port = int(os.getenv("SERVER_PORT", cfg.server.port))
|
|||
|
|
ws_port = int(os.getenv("WEBSOCKET_PORT", cfg.server.websocket_port))
|
|||
|
|
except Exception:
|
|||
|
|
port = cfg.server.port
|
|||
|
|
ws_port = cfg.server.websocket_port
|
|||
|
|
|
|||
|
|
def _check_port(p: int, name: str):
|
|||
|
|
if not (1 <= p <= 65535):
|
|||
|
|
problems.append(f"{name} 端口不在 1-65535 范围内")
|
|||
|
|
return f"[ERROR] {name} 端口 {p} 非法(应在 1-65535 范围内)。"
|
|||
|
|
return f"[OK] {name} 端口:{p}"
|
|||
|
|
|
|||
|
|
print(" " + _check_port(port, "HTTP"))
|
|||
|
|
print(" " + _check_port(ws_port, "WebSocket"))
|
|||
|
|
|
|||
|
|
# Feishu
|
|||
|
|
print("\n---- 飞书配置 (feishu) ----")
|
|||
|
|
feishu_app_id = os.getenv("FEISHU_APP_ID", "")
|
|||
|
|
feishu_app_secret = os.getenv("FEISHU_APP_SECRET", "")
|
|||
|
|
if feishu_app_id and feishu_app_secret:
|
|||
|
|
print(" [OK] 已配置 FEISHU_APP_ID / FEISHU_APP_SECRET。")
|
|||
|
|
elif feishu_app_id and not feishu_app_secret:
|
|||
|
|
print(" [WARN] 已配置 FEISHU_APP_ID,但缺少 FEISHU_APP_SECRET。")
|
|||
|
|
problems.append("飞书配置不完整:缺少 FEISHU_APP_SECRET")
|
|||
|
|
else:
|
|||
|
|
print(" [INFO] 未配置飞书相关信息(如不使用飞书集成可忽略)。")
|
|||
|
|
|
|||
|
|
# AI Accuracy
|
|||
|
|
print("\n---- AI 准确率配置 (ai_accuracy) ----")
|
|||
|
|
# 使用 cfg.ai_accuracy 中的默认或 env 覆盖值
|
|||
|
|
try:
|
|||
|
|
aa = cfg.ai_accuracy
|
|||
|
|
print(
|
|||
|
|
f" auto_approve_threshold: {aa.auto_approve_threshold}, "
|
|||
|
|
f"manual_review_threshold: {aa.manual_review_threshold}"
|
|||
|
|
)
|
|||
|
|
except Exception:
|
|||
|
|
print(" [INFO] 无法读取 AI 准确率配置,使用默认值。")
|
|||
|
|
|
|||
|
|
# 总结
|
|||
|
|
print("\n=== 检查总结 ===")
|
|||
|
|
if not problems:
|
|||
|
|
print(" [OK] 当前配置整体健康,未发现明显问题。")
|
|||
|
|
else:
|
|||
|
|
print(" 以下问题需要关注:")
|
|||
|
|
for p in problems:
|
|||
|
|
print(f" - {p}")
|
|||
|
|
|
|||
|
|
print("\n 建议:根据提示检查 .env 或部署环境变量,并重新运行 config-audit 以确认问题是否已解决。")
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|
|||
|
|
|