-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
277 lines (220 loc) Β· 9.24 KB
/
main.py
File metadata and controls
277 lines (220 loc) Β· 9.24 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python3
"""
Multi-Agent Software Development System - Main Orchestrator
This is the central orchestration point that coordinates all agents:
- PlannerAgent: Analyzes requirements and generates project plans
- CoderAgent: Generates code based on the plan
- FileTools: Manages generated files in the workspace
Workflow:
1. Parse user requirement
2. Generate project plan (file structure, tech stack, tasks)
3. Generate code for each file
4. Save all files to workspace
5. Report completion
"""
import logging
from typing import Optional, Dict, Any
from agents.planner import PlannerAgent
from agents.coder import CoderAgent
from tools.file_tools import FileTools
from config import Config
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class Orchestrator:
"""Main orchestrator for the Multi-Agent System."""
def __init__(self, config: Optional[Config] = None):
"""
Initialize the orchestrator with all agents.
Args:
config (Config, optional): Configuration object.
"""
self.config = config or Config()
self.planner = PlannerAgent(config=self.config)
self.coder = CoderAgent(config=self.config)
self.stats = {
"files_planned": 0,
"files_generated": 0,
"files_saved": 0,
"errors": 0,
}
logger.info("β Orchestrator initialized")
def orchestrate(self, user_requirement: str) -> Dict[str, Any]:
"""
Main orchestration workflow.
Args:
user_requirement (str): Natural language project requirement.
Returns:
Dict[str, Any]: Results summary including plan and generated files.
"""
try:
print("=" * 70)
print("π Multi-Agent Software Development System")
print("=" * 70)
# ========== Phase 1: Planning ==========
print("\n" + "=" * 70)
print("π PHASE 1: PROJECT PLANNING")
print("=" * 70)
print(f"\nπ User Requirement:")
print(f"{user_requirement}")
print(f"\nπ Analyzing requirements and generating project plan...")
plan = self.planner.plan_project(user_requirement)
# Display plan summary
print("\n" + self.planner.get_plan_summary(plan))
self.stats["files_planned"] = len(plan.get("files", []))
# ========== Phase 2: Code Generation ==========
print("\n" + "=" * 70)
print("π PHASE 2: CODE GENERATION")
print("=" * 70)
files = plan.get("files", [])
print(f"\nπ Generating code for {len(files)} files...")
generated_files = {}
for idx, file_entry in enumerate(files, 1):
filename = file_entry.get("filename")
purpose = file_entry.get("purpose")
priority = file_entry.get("priority", "medium")
print(f"\n [{idx}/{len(files)}] Generating {filename}...")
print(f" Purpose: {purpose}")
print(f" Priority: {priority}")
try:
# Prepare context for code generation
plan_context = self._prepare_context(plan)
# Generate code
code = self.coder.generate_code(
filename=filename,
task_description=purpose,
full_plan_context=plan_context,
language=self._detect_language(filename)
)
generated_files[filename] = code
self.stats["files_generated"] += 1
self.stats["files_saved"] += 1
print(f" β Generated ({len(code)} bytes)")
except Exception as e:
logger.error(f"β Failed to generate {filename}: {e}")
self.stats["errors"] += 1
print(f" β Error: {type(e).__name__}")
print(f" {str(e)[:60]}...")
# ========== Phase 3: Summary ==========
print("\n" + "=" * 70)
print("π PHASE 3: COMPLETION SUMMARY")
print("=" * 70)
self._print_summary(plan, generated_files)
# ========== Return Results ==========
return {
"success": self.stats["errors"] == 0,
"plan": plan,
"generated_files": generated_files,
"statistics": self.stats,
"workspace_path": str(FileTools.WORKSPACE_DIR),
}
except Exception as e:
logger.error(f"β Orchestration failed: {e}")
raise
def _prepare_context(self, plan: Dict[str, Any]) -> str:
"""
Prepare project context for code generation.
Args:
plan (Dict): Project plan.
Returns:
str: Formatted context string.
"""
context_parts = [
f"Project: {plan.get('project_name', 'Unknown')}",
f"Description: {plan.get('description', 'N/A')}",
]
tech_stack = plan.get('tech_stack', [])
if tech_stack:
context_parts.append(f"Tech Stack: {', '.join(tech_stack)}")
dependencies = plan.get('dependencies', [])
if dependencies:
context_parts.append(f"Dependencies: {', '.join(dependencies)}")
return "\n".join(context_parts)
def _detect_language(self, filename: str) -> str:
"""
Detect programming language from filename.
Args:
filename (str): Target filename.
Returns:
str: Detected language.
"""
extension_map = {
'.py': 'python',
'.js': 'javascript',
'.ts': 'typescript',
'.html': 'html',
'.css': 'css',
'.java': 'java',
'.go': 'go',
'.rs': 'rust',
}
for ext, lang in extension_map.items():
if filename.endswith(ext):
return lang
return 'python' # Default to python
def _print_summary(self, plan: Dict[str, Any], generated_files: Dict[str, str]) -> None:
"""
Print final summary of the orchestration.
Args:
plan: Project plan.
generated_files: Generated code mapping.
"""
print(f"\nβ
Project Generation Complete!")
print(f"\nπ Statistics:")
print(f" Files Planned: {self.stats['files_planned']}")
print(f" Files Generated: {self.stats['files_generated']}")
print(f" Files Saved: {self.stats['files_saved']}")
print(f" Errors: {self.stats['errors']}")
if self.stats['errors'] == 0:
print(f"\n⨠All files generated successfully!")
# Get workspace info
workspace_info = FileTools.get_workspace_info()
print(f"\nπ Workspace Information:")
print(f" Location: {workspace_info['workspace_path']}")
print(f" Total Files: {workspace_info['file_count']}")
print(f"\nπ Generated Files:")
for file in sorted(workspace_info['files']):
if file != "__init__.py": # Skip __init__
file_path = FileTools.WORKSPACE_DIR / file
size = file_path.stat().st_size
print(f" β {file:40s} ({size:6d} bytes)")
print("\n" + "=" * 70)
print("π Ready to use! Start developing with the generated files.")
print("=" * 70)
def main():
"""
Main entry point for the orchestrator.
Define your project requirement here.
"""
# ========== USER REQUIREMENT ==========
user_requirement = """
Build a simple arXiv CS Daily website.
The website should:
1. Display the latest computer science papers from arXiv
2. Support category navigation (cs.AI, cs.CV, cs.NLP)
3. Show paper details including title, authors, abstract, PDF link
4. Provide citation formatting (BibTeX)
5. Have a responsive design for mobile and desktop
6. Use Flask for backend and SQLite for data storage
"""
try:
# Initialize orchestrator
orchestrator = Orchestrator()
# Run orchestration
results = orchestrator.orchestrate(user_requirement)
# Check success
if results["success"]:
print("\n⨠Orchestration completed successfully!")
else:
print("\nβ οΈ Orchestration completed with some errors")
return results
except Exception as e:
logger.error(f"β Fatal error in orchestration: {e}")
import traceback
traceback.print_exc()
return {"success": False, "error": str(e)}
if __name__ == "__main__":
results = main()