config.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. r"""
  4. 配置模块 - 智能路径识别
  5. 支持: Linux 原生路径、WSL 路径 (\\wsl.localhost\Ubuntu\...)
  6. """
  7. import os
  8. import re
  9. from dotenv import load_dotenv
  10. # 项目目录
  11. PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  12. OUTPUT_DIR = os.path.join(PROJECT_DIR, "output")
  13. load_dotenv(os.path.join(PROJECT_DIR, ".env"))
  14. def convert_wsl_path(path: str) -> str:
  15. match = re.match(r'^\\\\wsl[.\$]?[^\\]*\\[^\\]+\\(.+)$', path, re.IGNORECASE)
  16. if match:
  17. return '/' + match.group(1).replace('\\', '/')
  18. return path
  19. def normalize_path(path: str) -> str:
  20. path = path.strip()
  21. path = convert_wsl_path(path)
  22. return os.path.expanduser(path)
  23. def get_paths(env_key: str) -> list:
  24. val = os.getenv(env_key, "")
  25. if not val:
  26. return []
  27. return [normalize_path(p) for p in val.split(",") if p.strip()]
  28. def auto_detect_paths() -> dict:
  29. home = os.path.expanduser("~")
  30. kiro_db = os.path.join(home, ".local", "share", "kiro-cli")
  31. candidates = {
  32. "codex_paths": [os.path.join(home, ".codex", "sessions"), os.path.join(home, ".codex")],
  33. "kiro_paths": [kiro_db] if os.path.exists(kiro_db) else [],
  34. "gemini_paths": [os.path.join(home, ".gemini", "tmp"), os.path.join(home, ".gemini")],
  35. "claude_paths": [os.path.join(home, ".claude")],
  36. }
  37. detected = {}
  38. for key, paths in candidates.items():
  39. for p in paths:
  40. if os.path.exists(p):
  41. detected[key] = [p]
  42. break
  43. if key not in detected:
  44. detected[key] = []
  45. return detected
  46. def load_config() -> dict:
  47. auto = auto_detect_paths()
  48. os.makedirs(OUTPUT_DIR, exist_ok=True)
  49. os.makedirs(os.path.join(OUTPUT_DIR, "logs"), exist_ok=True)
  50. return {
  51. "codex_paths": get_paths("CODEX_PATHS") or auto.get("codex_paths", []),
  52. "kiro_paths": get_paths("KIRO_PATHS") or auto.get("kiro_paths", []),
  53. "gemini_paths": get_paths("GEMINI_PATHS") or auto.get("gemini_paths", []),
  54. "claude_paths": get_paths("CLAUDE_PATHS") or auto.get("claude_paths", []),
  55. "output_dir": OUTPUT_DIR,
  56. "log_dir": os.path.join(OUTPUT_DIR, "logs"),
  57. "db_path": os.path.join(OUTPUT_DIR, "chat_history.db"),
  58. }
  59. CONFIG = load_config()