-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautonomous_agent_demo.py
More file actions
100 lines (81 loc) · 2.95 KB
/
autonomous_agent_demo.py
File metadata and controls
100 lines (81 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
"""
Autonomous Coding Agent Demo (Long-Running Agent Harness)
=========================================================
基于 Anthropic 文章 "Effective harnesses for long-running agents" 的
长时间运行 Agent 系统。实现「初始化 Agent + 编码 Agent」双阶段模式,
通过 feature_list.json、claude-progress.txt、init.sh 与 git 在多次
会话间保持状态并增量推进。
用法:
python autonomous_agent_demo.py --project-dir ./my_project
python autonomous_agent_demo.py --project-dir ./my_project --max-iterations 5
"""
import argparse
import asyncio
import os
from pathlib import Path
from agent import run_autonomous_agent
DEFAULT_MODEL = "claude-sonnet-4-5-20250929"
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="长时间运行 Autonomous Coding Agent(双 Agent 架构)",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
示例:
# 新项目
python autonomous_agent_demo.py --project-dir ./my_app
# 指定模型
python autonomous_agent_demo.py --project-dir ./my_app --model claude-sonnet-4-5-20250929
# 限制轮数(测试用)
python autonomous_agent_demo.py --project-dir ./my_app --max-iterations 5
# 继续已有项目(再次运行相同命令即可)
python autonomous_agent_demo.py --project-dir ./my_app
环境变量:
ANTHROPIC_API_KEY 必填,从 https://console.anthropic.com/ 获取
""",
)
parser.add_argument(
"--project-dir",
type=Path,
default=Path("./generations/autonomous_demo_project"),
help="项目目录(默认: ./generations/autonomous_demo_project)",
)
parser.add_argument(
"--max-iterations",
type=int,
default=None,
help="最大会话轮数(默认: 无限制)",
)
parser.add_argument(
"--model",
type=str,
default=DEFAULT_MODEL,
help=f"Claude 模型(默认: {DEFAULT_MODEL})",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
if not os.environ.get("ANTHROPIC_API_KEY"):
print("错误: 未设置环境变量 ANTHROPIC_API_KEY")
print("\n获取 API Key: https://console.anthropic.com/")
print("然后执行: export ANTHROPIC_API_KEY='your-api-key'")
return
project_dir = args.project_dir
if not project_dir.is_absolute() and not str(project_dir).startswith("generations/"):
project_dir = Path("generations") / project_dir
try:
asyncio.run(
run_autonomous_agent(
project_dir=project_dir,
model=args.model,
max_iterations=args.max_iterations,
)
)
except KeyboardInterrupt:
print("\n\n已由用户中断")
print("恢复运行: 再次执行相同命令即可")
except Exception as e:
print(f"\n致命错误: {e}")
raise
if __name__ == "__main__":
main()