-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
192 lines (159 loc) · 6.21 KB
/
Copy pathmain.py
File metadata and controls
192 lines (159 loc) · 6.21 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
"""
DevAgent — Entry Point
=======================
CLI that accepts a task description and runs the Think-Act-Observe agent loop
with project awareness, planning, memory, and lifecycle hooks.
Usage
-----
# Basic run (planning + memory enabled by default)
python main.py "List all Python files in this project"
# Skip planning phase
python main.py --no-plan "Quick: what's in README.md?"
# Enable safety gate
python main.py -H "Install pandas and run tests"
# Choose provider + persona
python main.py --provider anthropic "Refactor this file"
"""
from __future__ import annotations
import argparse
import sys
from dotenv import load_dotenv
from rich.console import Console
from rich.panel import Panel
from rich.text import Text
# Load environment variables BEFORE any provider instantiation
load_dotenv()
# Import the tools package — triggers @tool registrations
import tools # noqa: F401
from core.agent import Agent
from core.hooks import hooks
from core.llm_provider import get_provider
from core.tool_registry import registry
console = Console()
# ── Built-in hooks (examples) ───────────────────────────────────────────
def _on_error(error: str) -> None:
"""Built-in hook: log errors visibly."""
console.print(f"[bold red]⚠️ Hook caught error:[/bold red] {error}")
# Register built-in hooks
hooks.on("on_error", _on_error)
# ── Banner ───────────────────────────────────────────────────────────────
def _banner() -> None:
banner_text = Text()
banner_text.append("╔══════════════════════════════════════════════════╗\n", style="bold cyan")
banner_text.append("║ ", style="bold cyan")
banner_text.append("🤖 DevAgent v2", style="bold white")
banner_text.append(" ║\n", style="bold cyan")
banner_text.append("║ ", style="bold cyan")
banner_text.append("Autonomous Dev Assistant • Context-Aware", style="dim white")
banner_text.append(" ║\n", style="bold cyan")
banner_text.append("║ ", style="bold cyan")
banner_text.append("Planning • Memory • Hooks • Streaming", style="dim cyan")
banner_text.append(" ║\n", style="bold cyan")
banner_text.append("╚══════════════════════════════════════════════════╝", style="bold cyan")
console.print(banner_text)
# ── Main ─────────────────────────────────────────────────────────────────
def main() -> None:
parser = argparse.ArgumentParser(
description="DevAgent — AI-powered autonomous development assistant.",
)
parser.add_argument(
"task",
nargs="?",
default=None,
help="The task to execute (omit for interactive prompt).",
)
parser.add_argument(
"--provider", "-p",
default=None,
help="LLM provider: openai | anthropic | ollama (default: from .env).",
)
parser.add_argument(
"--max-iterations", "-n",
type=int,
default=15,
help="Maximum agent loop iterations (default: 15).",
)
parser.add_argument(
"--human-in-the-loop", "-H",
action="store_true",
default=False,
help="Ask for user confirmation before executing protected tools.",
)
parser.add_argument(
"--auto-approve", "-y",
action="store_true",
default=False,
help="Auto-approve plans and protected tool executions (no prompts).",
)
parser.add_argument(
"--no-plan",
action="store_true",
default=False,
help="Skip the planning phase and execute immediately.",
)
parser.add_argument(
"--no-memory",
action="store_true",
default=False,
help="Disable session memory (no .devagent/ persistence).",
)
parser.add_argument(
"--project-root",
default=".",
help="Root directory for project context scanning (default: cwd).",
)
args = parser.parse_args()
_banner()
# Resolve task
task: str = args.task or ""
if not task.strip():
console.print("\n[bold yellow]Enter your task:[/bold yellow]")
task = input("> ").strip()
if not task:
console.print("[red]No task provided. Exiting.[/red]")
sys.exit(1)
# Show registered tools
tool_names = registry.list_tools()
console.print(
Panel(
"\n".join(f" 🔧 {name}" for name in tool_names) or " (none)",
title="[bold green]Registered Tools[/bold green]",
border_style="green",
)
)
# Show registered hooks
active_hooks = hooks.list_hooks()
if active_hooks:
hook_lines = []
for event, cbs in active_hooks.items():
hook_lines.append(f" 🪝 {event}: {', '.join(cbs)}")
console.print(
Panel(
"\n".join(hook_lines),
title="[bold magenta]Active Hooks[/bold magenta]",
border_style="magenta",
)
)
# Initialise provider
try:
llm = get_provider(args.provider)
except (ValueError, ImportError) as exc:
console.print(f"[bold red]Provider error:[/bold red] {exc}")
sys.exit(1)
console.print(f"[dim]Using LLM provider:[/dim] [bold]{llm.name}[/bold]\n")
# Create and run agent
agent = Agent(
llm=llm,
registry=registry,
max_iterations=args.max_iterations,
human_in_the_loop=args.human_in_the_loop,
auto_approve=args.auto_approve,
enable_planning=not args.no_plan,
enable_memory=not args.no_memory,
project_root=args.project_root,
)
result = agent.run(task)
console.print("\n[bold green]Done.[/bold green]\n")
if __name__ == "__main__":
main()