init.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import os
  2. import json
  3. from datetime import datetime
  4. from ..core.state_engine import PATEOASStateEngine
  5. from ..core.memory_pool import GlobalMemoryPool
  6. from ..utils.logger import get_logger
  7. # 初始化日志
  8. logger = get_logger("aceflow.init")
  9. def initialize_project(project_root='.', reset_state=False):
  10. """
  11. 初始化AceFlow-PATEOAS项目结构
  12. 参数:
  13. project_root (str): 项目根目录路径,默认为当前目录
  14. reset_state (bool): 是否重置现有状态,默认为False
  15. """
  16. try:
  17. # 1. 创建核心目录结构
  18. directories = [
  19. ".aceflow/config",
  20. ".aceflow/scripts/core",
  21. ".aceflow/scripts/cli",
  22. ".aceflow/scripts/utils",
  23. ".aceflow/scripts/migrations",
  24. ".aceflow/memory_pool/REQ",
  25. ".aceflow/memory_pool/CON",
  26. ".aceflow/memory_pool/TASK",
  27. ".aceflow/memory_pool/CODE",
  28. ".aceflow/memory_pool/TEST",
  29. ".aceflow/memory_pool/DEFECT",
  30. ".aceflow/memory_pool/FDBK",
  31. ".aceflow/templates/stage_templates",
  32. ".aceflow/templates/auxiliary_templates",
  33. ".aceflow/logs",
  34. "aceflow_result",
  35. ".vscode"
  36. ]
  37. for dir_path in directories:
  38. full_path = os.path.join(project_root, dir_path)
  39. os.makedirs(full_path, exist_ok=True)
  40. logger.info(f"创建目录: {full_path}")
  41. # 2. 初始化状态引擎
  42. state_engine = PATEOASStateEngine(project_root)
  43. if reset_state:
  44. logger.info("重置现有状态...")
  45. state_engine.initialize_state()
  46. # 3. 初始化记忆池
  47. memory_pool = GlobalMemoryPool()
  48. logger.info("全局记忆池初始化完成")
  49. # 4. 创建基础配置文件(如不存在)
  50. create_default_configs(project_root)
  51. # 5. 创建.gitignore文件
  52. create_gitignore(project_root)
  53. logger.info("项目初始化完成!")
  54. print("✅ AceFlow-PATEOAS项目初始化成功")
  55. print("📁 项目结构已创建")
  56. print("🔧 状态引擎和记忆池已初始化")
  57. print("📝 基础配置文件已生成")
  58. return True
  59. except Exception as e:
  60. logger.error(f"初始化失败: {str(e)}", exc_info=True)
  61. print(f"❌ 初始化失败: {str(e)}")
  62. return False
  63. def create_default_configs(project_root):
  64. """创建默认配置文件"""
  65. # workflow_rules.json
  66. workflow_rules_path = os.path.join(project_root, ".aceflow", "config", "workflow_rules.json")
  67. if not os.path.exists(workflow_rules_path):
  68. default_workflow_rules = {
  69. "workflow_rules": {
  70. "full_workflow": ["S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8"],
  71. "quick_workflow": ["S2", "S4", "S5", "S8"],
  72. "change_workflow": ["S1", "S2", "S3", "S4"],
  73. "emergency_workflow": ["S4", "S5", "S6", "S8"]
  74. },
  75. "memory_pool_config": {
  76. "storage_path": "./.aceflow/memory_pool",
  77. "retention_policy": "critical_forever,temporary_7d"
  78. },
  79. "ai_decision_config": {
  80. "trust_level": "L2",
  81. "success_threshold": 0.85
  82. }
  83. }
  84. with open(workflow_rules_path, 'w', encoding='utf-8') as f:
  85. json.dump(default_workflow_rules, f, ensure_ascii=False, indent=2)
  86. logger.info(f"创建默认配置: {workflow_rules_path}")
  87. # dynamic_thresholds.json
  88. thresholds_path = os.path.join(project_root, ".aceflow", "config", "dynamic_thresholds.json")
  89. if not os.path.exists(thresholds_path):
  90. default_thresholds = {
  91. "global": {
  92. "time_adjustment_range": 20,
  93. "memory_retention_days": 30
  94. },
  95. "stage_specific": {
  96. "S3": {
  97. "test_case_coverage": {
  98. "default": 80,
  99. "payment_module": 99,
  100. "ui_module": 75
  101. }
  102. },
  103. "S4": {
  104. "unit_test_pass_rate": {
  105. "default": 90,
  106. "critical_task": 95,
  107. "minor_task": 85
  108. }
  109. }
  110. }
  111. }
  112. with open(thresholds_path, 'w', encoding='utf-8') as f:
  113. json.dump(default_thresholds, f, ensure_ascii=False, indent=2)
  114. logger.info(f"创建默认配置: {thresholds_path}")
  115. # aceflow_agent.json (VS Code配置)
  116. agent_config_path = os.path.join(project_root, ".vscode", "aceflow_agent.json")
  117. if not os.path.exists(agent_config_path):
  118. default_agent_config = {
  119. "agent_type": "pateoas_aceflow_agent",
  120. "capabilities": [
  121. "state_awareness",
  122. "memory_management",
  123. "autonomous_navigation",
  124. "abnormality_handling"
  125. ],
  126. "state_templates": ".aceflow/templates/stage_templates/",
  127. "memory_pool_config": {
  128. "storage_path": ".aceflow/memory_pool/",
  129. "retention_policy": "critical_forever,temporary_7d"
  130. },
  131. "output_config": {
  132. "root_dir": "aceflow_result",
  133. "stage_dir_format": "S{stage_number}_{stage_name}",
  134. "compatibility_mode": true
  135. }
  136. }
  137. with open(agent_config_path, 'w', encoding='utf-8') as f:
  138. json.dump(default_agent_config, f, ensure_ascii=False, indent=2)
  139. logger.info(f"创建默认配置: {agent_config_path}")
  140. # process_index.json (流程索引)
  141. index_path = os.path.join(project_root, ".aceflow", "process_index.json")
  142. if not os.path.exists(index_path):
  143. default_index = {
  144. "process_spec": {
  145. "path": ".aceflow/templates/document_templates/process_spec.md",
  146. "sections": [
  147. {"name": "核心原则", "anchor": "#_2-核心原则"},
  148. {"name": "阶段定义", "anchor": "#_4-阶段定义与执行规范"},
  149. {"name": "状态管理", "anchor": "#_5-状态管理规范"}
  150. ]
  151. },
  152. "templates": {
  153. "stage_templates": ".aceflow/templates/stage_templates/",
  154. "auxiliary_templates": ".aceflow/templates/auxiliary_templates/",
  155. "available_templates": [
  156. "s1_user_story.md", "s2_tasks.md", "s3_testcases.md",
  157. "s4_implementation.md", "s5_test_report.md", "s6_codereview.md",
  158. "s7_demo_feedback.md", "s8_progress_index.md", "task-status-table.md"
  159. ]
  160. },
  161. "config_files": {
  162. "dynamic_thresholds": ".aceflow/config/dynamic_thresholds.json",
  163. "workflow_rules": ".aceflow/config/workflow_rules.json",
  164. "agent_config": ".vscode/aceflow_agent.json"
  165. }
  166. }
  167. with open(index_path, 'w', encoding='utf-8') as f:
  168. json.dump(default_index, f, ensure_ascii=False, indent=2)
  169. logger.info(f"创建流程索引: {index_path}")
  170. def create_gitignore(project_root):
  171. """创建.gitignore文件,排除临时文件和敏感信息"""
  172. gitignore_path = os.path.join(project_root, ".gitignore")
  173. ignore_content = """# AceFlow-PATEOAS 临时文件
  174. .aceflow/logs/
  175. .aceflow/memory_pool/
  176. .aceflow/current_state.json
  177. .aceflow/*.log
  178. # 产物目录
  179. aceflow_result/
  180. # Python 环境文件
  181. __pycache__/
  182. *.py[cod]
  183. *$py.class
  184. *.so
  185. .Python
  186. env/
  187. build/
  188. develop-eggs/
  189. dist/
  190. downloads/
  191. eggs/
  192. .eggs/
  193. lib/
  194. lib64/
  195. parts/
  196. sdist/
  197. var/
  198. *.egg-info/
  199. .installed.cfg
  200. *.egg
  201. # VS Code 配置
  202. .vscode/settings.json
  203. .vscode/launch.json
  204. !.vscode/extensions.json
  205. !.vscode/aceflow_agent.json
  206. """
  207. # 如果.gitignore不存在或不包含AceFlow规则,则添加
  208. if not os.path.exists(gitignore_path):
  209. with open(gitignore_path, 'w', encoding='utf-8') as f:
  210. f.write(ignore_content)
  211. logger.info("创建.gitignore文件")
  212. else:
  213. with open(gitignore_path, 'r', encoding='utf-8') as f:
  214. content = f.read()
  215. if "# AceFlow-PATEOAS 临时文件" not in content:
  216. with open(gitignore_path, 'a', encoding='utf-8') as f:
  217. f.write("\n" + ignore_content)
  218. logger.info("更新.gitignore文件,添加AceFlow规则")
  219. # 命令行入口函数
  220. def init_project(args):
  221. """CLI命令:初始化项目"""
  222. initialize_project(reset_state=getattr(args, 'reset', False))
  223. # 命令配置
  224. def add_init_command(subparsers):
  225. """添加init命令到解析器"""
  226. init_parser = subparsers.add_parser('init', help='初始化AceFlow-PATEOAS项目结构')
  227. init_parser.add_argument('--reset', action='store_true', help='重置现有状态文件')
  228. init_parser.set_defaults(func=init_project)