aceflow 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #!/usr/bin/env python3
  2. """
  3. AceFlow v3.0 CLI 工具
  4. 主要功能:项目管理、流程控制、AI辅助
  5. """
  6. import json
  7. import os
  8. import sys
  9. import argparse
  10. import yaml
  11. from datetime import datetime
  12. from pathlib import Path
  13. class AceFlowCLI:
  14. def __init__(self):
  15. self.project_root = Path.cwd()
  16. self.aceflow_dir = self.project_root / ".aceflow"
  17. self.state_file = self.aceflow_dir / "state" / "project_state.json"
  18. self.config_file = self.aceflow_dir / "config" / "project.yaml"
  19. def load_state(self):
  20. """加载项目状态"""
  21. if self.state_file.exists():
  22. with open(self.state_file, 'r', encoding='utf-8') as f:
  23. return json.load(f)
  24. return {}
  25. def save_state(self, state):
  26. """保存项目状态"""
  27. state['last_updated'] = datetime.now().isoformat()
  28. with open(self.state_file, 'w', encoding='utf-8') as f:
  29. json.dump(state, f, indent=2, ensure_ascii=False)
  30. def load_config(self):
  31. """加载项目配置"""
  32. if self.config_file.exists():
  33. with open(self.config_file, 'r', encoding='utf-8') as f:
  34. return yaml.safe_load(f)
  35. return {}
  36. def init_project(self, mode='smart'):
  37. """初始化 AceFlow 项目"""
  38. print(f"🚀 初始化 AceFlow v3.0 项目...")
  39. # 创建目录结构
  40. dirs = [
  41. '.aceflow/config',
  42. '.aceflow/state',
  43. '.aceflow/scripts',
  44. '.aceflow/templates',
  45. '.aceflow/memory',
  46. 'aceflow_result'
  47. ]
  48. for dir_path in dirs:
  49. os.makedirs(dir_path, exist_ok=True)
  50. # 初始化状态
  51. state = {
  52. "project_id": f"aceflow_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
  53. "flow_mode": mode,
  54. "selected_mode": None,
  55. "current_stage": None,
  56. "overall_progress": 0,
  57. "created_at": datetime.now().isoformat(),
  58. "version": "3.0.0"
  59. }
  60. self.save_state(state)
  61. print(f"✅ AceFlow 项目初始化完成!模式: {mode}")
  62. return state
  63. def status(self, format_type='text', verbose=False):
  64. """查看项目状态"""
  65. if not self.aceflow_dir.exists():
  66. print("❌ 未检测到 AceFlow 项目,请先运行 'aceflow init'")
  67. return
  68. state = self.load_state()
  69. config = self.load_config()
  70. if format_type == 'json':
  71. print(json.dumps(state, indent=2, ensure_ascii=False))
  72. else:
  73. self._print_status_text(state, config, verbose)
  74. def _print_status_text(self, state, config, verbose):
  75. """打印文本格式的状态"""
  76. print("\n📊 AceFlow 项目状态")
  77. print("=" * 40)
  78. print(f"项目ID: {state.get('project_id', 'N/A')}")
  79. print(f"流程模式: {state.get('flow_mode', 'N/A')}")
  80. print(f"当前阶段: {state.get('current_stage', '未开始')}")
  81. print(f"整体进度: {state.get('overall_progress', 0)}%")
  82. print(f"最后更新: {state.get('last_updated', 'N/A')}")
  83. if verbose:
  84. print(f"\n📋 详细信息:")
  85. print(f"版本: {state.get('version', 'N/A')}")
  86. print(f"创建时间: {state.get('created_at', 'N/A')}")
  87. print(f"配置文件: {self.config_file}")
  88. print(f"状态文件: {self.state_file}")
  89. def analyze(self, task_description):
  90. """AI 任务分析"""
  91. print(f"🧠 正在分析任务: {task_description}")
  92. # 简单的任务分类逻辑
  93. keywords = {
  94. 'bug': ['修复', 'fix', 'bug', '问题', '错误'],
  95. 'feature': ['新功能', '开发', '实现', '添加', '功能'],
  96. 'refactor': ['重构', '优化', '改进', '重写'],
  97. 'project': ['项目', '系统', '平台', '架构']
  98. }
  99. task_type = 'unknown'
  100. for category, kw_list in keywords.items():
  101. if any(kw in task_description.lower() for kw in kw_list):
  102. task_type = category
  103. break
  104. # 推荐模式
  105. mode_mapping = {
  106. 'bug': 'minimal',
  107. 'feature': 'standard',
  108. 'refactor': 'standard',
  109. 'project': 'complete',
  110. 'unknown': 'smart'
  111. }
  112. recommended_mode = mode_mapping.get(task_type, 'smart')
  113. result = {
  114. 'task_description': task_description,
  115. 'task_type': task_type,
  116. 'recommended_mode': recommended_mode,
  117. 'confidence': 0.85,
  118. 'analysis_time': datetime.now().isoformat()
  119. }
  120. print(f"📊 分析结果:")
  121. print(f" 任务类型: {task_type}")
  122. print(f" 推荐模式: {recommended_mode}")
  123. print(f" 置信度: 85%")
  124. return result
  125. def start(self, description=None, mode=None):
  126. """开始新的工作流"""
  127. state = self.load_state()
  128. if not description:
  129. description = input("请描述您要开始的任务: ")
  130. if not mode:
  131. # 智能分析推荐模式
  132. analysis = self.analyze(description)
  133. mode = analysis['recommended_mode']
  134. # 生成迭代ID
  135. iteration_id = f"iter_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
  136. # 更新状态
  137. state.update({
  138. 'selected_mode': mode,
  139. 'current_stage': self._get_first_stage(mode),
  140. 'iteration_id': iteration_id,
  141. 'task_description': description,
  142. 'overall_progress': 0,
  143. 'stage_progress': 0
  144. })
  145. self.save_state(state)
  146. # 创建结果目录
  147. result_dir = Path(f"aceflow_result/{iteration_id}")
  148. result_dir.mkdir(parents=True, exist_ok=True)
  149. print(f"🚀 开始 AceFlow 工作流")
  150. print(f" 任务描述: {description}")
  151. print(f" 选择模式: {mode}")
  152. print(f" 迭代ID: {iteration_id}")
  153. print(f" 当前阶段: {state['current_stage']}")
  154. print(f" 结果目录: {result_dir}")
  155. return state
  156. def _get_first_stage(self, mode):
  157. """获取模式的第一个阶段"""
  158. stage_mapping = {
  159. 'minimal': 'P',
  160. 'standard': 'P1',
  161. 'complete': 'S1',
  162. 'smart': 'S1'
  163. }
  164. return stage_mapping.get(mode, 'S1')
  165. def progress(self, stage, percentage):
  166. """更新进度"""
  167. state = self.load_state()
  168. if stage == 'current':
  169. stage = state.get('current_stage')
  170. if not stage:
  171. print("❌ 未找到当前阶段")
  172. return
  173. state['stage_progress'] = percentage
  174. # 简单的整体进度计算
  175. state['overall_progress'] = min(percentage, 100)
  176. self.save_state(state)
  177. print(f"📈 进度更新: {stage} -> {percentage}%")
  178. return state
  179. def complete(self, stage=None):
  180. """完成阶段"""
  181. state = self.load_state()
  182. if not stage:
  183. stage = state.get('current_stage')
  184. if not stage:
  185. print("❌ 未找到当前阶段")
  186. return
  187. print(f"✅ 完成阶段: {stage}")
  188. # 更新状态
  189. state['stage_progress'] = 100
  190. # 移动到下一阶段
  191. next_stage = self._get_next_stage(stage, state.get('selected_mode'))
  192. if next_stage:
  193. state['current_stage'] = next_stage
  194. state['stage_progress'] = 0
  195. print(f"➡️ 进入下一阶段: {next_stage}")
  196. else:
  197. print("🎉 所有阶段完成!")
  198. state['current_stage'] = None
  199. state['overall_progress'] = 100
  200. self.save_state(state)
  201. return state
  202. def _get_next_stage(self, current_stage, mode):
  203. """获取下一个阶段"""
  204. stage_flows = {
  205. 'minimal': ['P', 'D', 'R'],
  206. 'standard': ['P1', 'P2', 'D1', 'D2', 'R1'],
  207. 'complete': ['S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8'],
  208. 'smart': ['S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8']
  209. }
  210. flow = stage_flows.get(mode, [])
  211. if current_stage in flow:
  212. current_index = flow.index(current_stage)
  213. if current_index + 1 < len(flow):
  214. return flow[current_index + 1]
  215. return None
  216. def main():
  217. parser = argparse.ArgumentParser(description='AceFlow v3.0 CLI 工具')
  218. subparsers = parser.add_subparsers(dest='command', help='可用命令')
  219. # init 命令
  220. init_parser = subparsers.add_parser('init', help='初始化项目')
  221. init_parser.add_argument('--mode', choices=['smart', 'minimal', 'standard', 'complete'],
  222. default='smart', help='流程模式')
  223. # status 命令
  224. status_parser = subparsers.add_parser('status', help='查看状态')
  225. status_parser.add_argument('--format', choices=['text', 'json'], default='text', help='输出格式')
  226. status_parser.add_argument('--verbose', action='store_true', help='详细输出')
  227. # analyze 命令
  228. analyze_parser = subparsers.add_parser('analyze', help='分析任务')
  229. analyze_parser.add_argument('task', help='任务描述')
  230. # start 命令
  231. start_parser = subparsers.add_parser('start', help='开始工作流')
  232. start_parser.add_argument('--description', help='任务描述')
  233. start_parser.add_argument('--mode', choices=['smart', 'minimal', 'standard', 'complete'],
  234. help='流程模式')
  235. # progress 命令
  236. progress_parser = subparsers.add_parser('progress', help='更新进度')
  237. progress_parser.add_argument('stage', help='阶段名称')
  238. progress_parser.add_argument('percentage', type=int, help='进度百分比')
  239. # complete 命令
  240. complete_parser = subparsers.add_parser('complete', help='完成阶段')
  241. complete_parser.add_argument('stage', nargs='?', default='current', help='阶段名称')
  242. args = parser.parse_args()
  243. cli = AceFlowCLI()
  244. if args.command == 'init':
  245. cli.init_project(args.mode)
  246. elif args.command == 'status':
  247. cli.status(args.format, args.verbose)
  248. elif args.command == 'analyze':
  249. cli.analyze(args.task)
  250. elif args.command == 'start':
  251. cli.start(args.description, args.mode)
  252. elif args.command == 'progress':
  253. cli.progress(args.stage, args.percentage)
  254. elif args.command == 'complete':
  255. cli.complete(args.stage)
  256. else:
  257. parser.print_help()
  258. if __name__ == '__main__':
  259. main()