-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
113 lines (98 loc) · 3 KB
/
Copy pathmain.py
File metadata and controls
113 lines (98 loc) · 3 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
101
102
103
104
105
106
107
108
109
110
111
112
113
"""NanoAgent CLI - 极简 Agent 框架"""
import asyncio
import sys
from typing import Optional
from dotenv import load_dotenv
import typer
from core.agent import NanoAgent
load_dotenv()
app = typer.Typer(
name="nanoagent", help="NanoAgent - 极简 Agent 框架", add_completion=False
)
@app.command()
def run(
task: str = typer.Argument(..., help="任务描述"),
max_iterations: Optional[int] = typer.Option(
None, "--max", "-m", help="最大迭代次数(可选)"
),
) -> None:
"""执行任务"""
try:
agent = NanoAgent()
result = agent.run(task, max_iterations=max_iterations)
typer.echo(f"✅ 状态: {result.get('status', 'unknown')}")
typer.echo(f"🔄 迭代: {result.get('iterations', 0)}")
typer.echo(f"🔧 工具: {', '.join(result.get('tools_used', []))}")
typer.echo(f"📁 产物: {len(result.get('artifacts', []))} 个")
finally:
# 确保事件循环正确清理
try:
loop = asyncio.get_event_loop()
if loop.is_running():
loop.close()
except RuntimeError:
pass
@app.command()
def chat() -> None:
"""交互式对话模式"""
try:
agent = NanoAgent()
agent.chat()
finally:
# 确保事件循环正确清理
try:
loop = asyncio.get_event_loop()
if loop.is_running():
loop.close()
except RuntimeError:
pass
@app.command()
def trace(
action: str = typer.Argument(..., help="操作: list, show, stats, delete"),
trace_id: Optional[str] = typer.Argument(None, help="追踪 ID"),
limit: int = typer.Option(20, "--limit", "-n", help="显示数量"),
) -> None:
"""查看追踪记录"""
from cli.observer import (
print_trace_list,
print_trace_detail,
print_stats,
delete_trace_by_id,
)
if action == "list":
print_trace_list(limit)
elif action == "show":
if not trace_id:
typer.echo("Error: trace_id required for show", err=True)
raise typer.Exit(1)
print_trace_detail(trace_id)
elif action == "stats":
print_stats()
elif action == "delete":
if not trace_id:
typer.echo("Error: trace_id required for delete", err=True)
raise typer.Exit(1)
delete_trace_by_id(trace_id)
else:
typer.echo(f"Unknown action: {action}", err=True)
typer.echo(
"Usage: nanoagent trace [list|show|stats|delete] [trace_id]", err=True
)
raise typer.Exit(1)
@app.command()
def version() -> None:
"""显示版本信息"""
typer.echo("NanoAgent v2.0.0 - 极简 Agent 框架")
if __name__ == "__main__":
try:
app()
except KeyboardInterrupt:
sys.exit(0)
finally:
# 确保资源正确清理
try:
loop = asyncio.get_event_loop()
if loop.is_running():
loop.close()
except RuntimeError:
pass