Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
1085 lines (918 loc) · 40 KB
/
Copy pathmain.py
File metadata and controls
1085 lines (918 loc) · 40 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Multi-Agent Development System - Main Entry Point
This module serves as the entry point for the multi-agent development system,
responsible for initializing components, connecting services, and starting
the agent network. It handles command-line arguments, configuration loading,
system lifecycle management, and provides a CLI interface for interaction.
"""
import argparse
import asyncio
import logging
import os
import signal
import sys
import json
import yaml
from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Union
import time
import uuid
import traceback
# System components
from config.system_config import SystemConfig, get_system_config
from config.agent_config import AgentConfig
from memory.shared_memory import SharedMemory
from memory.context_store import ContextStore, ContextType
from agents.base_agent import BaseAgent, ModelProvider, AgentRole
from tools.code_tools import CodeTool
from human_interface.review_interface import ReviewInterface
from human_interface.feedback_processor import FeedbackProcessor
from orchestrator.workflow_engine import WorkflowEngine, LoggingWorkflowListener
from orchestrator.task_scheduler import TaskScheduler
# Import specific agents
from agents.manager_agent import ProjectManagerAgent
from agents.designer_agent import ArchitectureDesignerAgent
from agents.frontend_agents import (
UIComponentDeveloper,
FrontendLogicDeveloper,
FrontendIntegrationDeveloper
)
from agents.backend_agents import (
APIDeveloper,
DatabaseDesigner,
BackendLogicDeveloper
)
from agents.devops_agents import (
InfrastructureDeveloper,
DeploymentSpecialist,
SecurityAnalyst
)
from agents.qa_agents import (
CodeReviewer,
TestDeveloper,
UXTester
)
from agents.research_agent import ResearchSpecialistAgent
from agents.doc_agent import DocumentationWriterAgent
#from agents.human_interface_agent import HumanInterfaceAgent
# Set up logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
logging.StreamHandler(),
logging.FileHandler("multi_agent_dev.log")
]
)
logger = logging.getLogger(__name__)
# Version information
VERSION = "0.1.0"
BUILD_DATE = "2025-04-10"
# Banner for CLI
BANNER = r"""
╔═══════════════════════════════════════════════════════════════════════╗
║ ║
║ ███╗ ███╗██╗ ██╗██╗ ████████╗██╗ █████╗ ██████╗ ███████╗ ║
║ ████╗ ████║██║ ██║██║ ╚══██╔══╝██║ ██╔══██╗██╔════╝ ██╔════╝ ║
║ ██╔████╔██║██║ ██║██║ ██║ ██║ ███████║██║ ███╗█████╗ ║
║ ██║╚██╔╝██║██║ ██║██║ ██║ ██║ ██╔══██║██║ ██║██╔══╝ ║
║ ██║ ╚═╝ ██║╚██████╔╝███████╗██║ ███████╗██║ ██║╚██████╔╝███████╗ ║
║ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ ║
║ ║
║ AI Development Team System ║
║ ║
╚═══════════════════════════════════════════════════════════════════════╝
"""
class MultiAgentSystem:
"""Main class for the multi-agent development system."""
def __init__(
self,
config_path: Optional[str] = None,
data_dir: Optional[str] = None,
debug: bool = False,
agent_types: Optional[List[str]] = None,
model_provider: Optional[str] = None
):
"""Initialize the multi-agent system.
Args:
config_path: Path to configuration file
data_dir: Path to data directory
debug: Whether to enable debug mode
agent_types: List of agent types to initialize
model_provider: Default model provider to use
"""
self.start_time = time.time()
self.instance_id = str(uuid.uuid4())
self.debug = debug
self.running = False
self.shutdown_requested = False
# Set log level
if debug:
logging.getLogger().setLevel(logging.DEBUG)
logger.debug("Debug mode enabled")
# Load configuration
self.system_config = get_system_config(config_path)
self.agent_config = AgentConfig(config_path)
# Set data directory
self.data_dir = data_dir or self.system_config.get_config(
"paths", "data_directory", default=os.path.join(os.getcwd(), "data")
)
os.makedirs(self.data_dir, exist_ok=True)
# Set model provider
self.model_provider = model_provider or self.system_config.get_config(
"model_providers", "default", default="anthropic"
)
# Set default agent types
self.agent_types = agent_types or [
"project_manager",
"architecture_designer",
"ui_developer",
"frontend_logic",
"frontend_integration",
"api_developer",
"database_designer",
"backend_logic",
"infrastructure",
"deployment",
"security",
"code_reviewer",
"test_developer",
"ux_tester",
"researcher",
"documentation",
"human_interface"
]
# Initialize components
self.shared_memory = None
self.context_store = None
self.task_scheduler = None
self.workflow_engine = None
self.review_interface = None
self.feedback_processor = None
self.code_tools = None
self.agent_registry = {}
logger.info(f"Multi-Agent System initialized (instance: {self.instance_id})")
async def initialize(self) -> None:
"""Initialize system components and agents."""
try:
# Create component paths
shared_memory_path = os.path.join(self.data_dir, "shared_memory")
context_store_path = os.path.join(self.data_dir, "context_store")
workflow_store_path = os.path.join(self.data_dir, "workflows")
task_storage_path = os.path.join(self.data_dir, "tasks.json")
os.makedirs(shared_memory_path, exist_ok=True)
os.makedirs(context_store_path, exist_ok=True)
os.makedirs(workflow_store_path, exist_ok=True)
# Initialize memory and context components
logger.info("Initializing memory and context components...")
self.shared_memory = SharedMemory(
storage_path=shared_memory_path,
#enable_locking=True
)
self.context_store = ContextStore(
storage_backend="filesystem",
storage_path=context_store_path,
compression=True
)
# Initialize tools
logger.info("Initializing tools...")
self.code_tools = CodeTool(
working_dir=self.data_dir,
temp_dir=os.path.join(self.data_dir, "temp")
)
# Initialize orchestration components
logger.info("Initializing orchestration components...")
self.task_scheduler = TaskScheduler(
agent_registry=self.agent_registry,
shared_memory=self.shared_memory,
)
workflow_listeners = [LoggingWorkflowListener()]
self.workflow_engine = WorkflowEngine(
task_scheduler=self.task_scheduler,
agent_registry=self.agent_registry,
shared_memory=self.shared_memory,
listeners=workflow_listeners,
workflow_store_path=workflow_store_path,
#enable_locking=True
)
# Initialize human interface components
logger.info("Initializing human interface components...")
self.review_interface = ReviewInterface(
shared_memory=self.shared_memory
)
self.feedback_processor = FeedbackProcessor(
agent_registry=self.agent_registry,
shared_memory=self.shared_memory,
task_scheduler=self.task_scheduler
)
await self.feedback_processor.load_stored_metrics()
# Initialize agents
logger.info("Initializing agents...")
await self._initialize_agents()
# Start workflow engine
self.workflow_engine.start()
self.running = True
logger.info("System initialization complete")
except Exception as e:
logger.error(f"Error during system initialization: {str(e)}")
traceback.print_exc()
raise
async def _initialize_agents(self) -> None:
"""Initialize all agents."""
# Convert model provider string to enum
model_provider = ModelProvider(self.model_provider)
for agent_type in self.agent_types:
try:
# Get agent configuration
agent_config = self.agent_config.get_agent_config(agent_type)
if not agent_config:
logger.warning(f"No configuration found for agent type: {agent_type}")
continue
# Get model configuration
provider_str, model_name, temperature, max_tokens = self.agent_config.get_model_config(agent_type)
# Override with system default if specified
if self.model_provider:
provider_str = self.model_provider
provider = ModelProvider(provider_str)
# Create the appropriate agent based on type
agent = None
if agent_type == "project_manager":
agent = ProjectManagerAgent(
name=f"Project Manager",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
task_scheduler=self.task_scheduler,
workflow_engine=self.workflow_engine
)
elif agent_type == "architecture_designer":
agent = ArchitectureDesignerAgent(
name=f"Architecture Designer",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
elif agent_type == "ui_developer":
agent = UIComponentDeveloper(
name=f"UI Component Developer",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
elif agent_type == "frontend_logic":
agent = FrontendLogicDeveloper(
name=f"Frontend Logic Developer",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
elif agent_type == "frontend_integration":
agent = FrontendIntegrationDeveloper(
name=f"Frontend Integration Developer",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
elif agent_type == "api_developer":
agent = APIDeveloper(
name=f"API Developer",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
elif agent_type == "database_designer":
agent = DatabaseDesigner(
name=f"Database Designer",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
elif agent_type == "backend_logic":
agent = BackendLogicDeveloper(
name=f"Backend Logic Developer",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
elif agent_type == "infrastructure":
agent = InfrastructureDeveloper(
name=f"Infrastructure Developer",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
elif agent_type == "deployment":
agent = DeploymentSpecialist(
name=f"Deployment Specialist",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
elif agent_type == "security":
agent = SecurityAnalyst(
name=f"Security Analyst",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
elif agent_type == "code_reviewer":
agent = CodeReviewer(
name=f"Code Reviewer",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
elif agent_type == "test_developer":
agent = TestDeveloper(
name=f"Test Developer",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
elif agent_type == "ux_tester":
agent = UXTester(
name=f"UX Tester",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory
)
elif agent_type == "researcher":
agent = ResearchSpecialistAgent(
name=f"Research Specialist",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory
)
elif agent_type == "documentation":
agent = DocumentationWriterAgent(
name=f"Documentation Writer",
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
code_tools=self.code_tools
)
else:
# Default to base agent for unknown types
agent = BaseAgent(
name=f"{agent_type.replace('_', ' ').title()}",
agent_type=agent_type,
model_provider=provider,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
shared_memory=self.shared_memory,
system_prompt=agent_config.get("system_prompt")
)
# Register the agent
self.agent_registry[agent.state.agent_id] = agent
# Store agent info in shared memory
if self.shared_memory:
await self.shared_memory.store(
key=agent.state.agent_id,
value={
"id": agent.state.agent_id,
"name": agent.name,
"type": agent.state.agent_type,
"model": f"{provider.value}/{model_name}"
},
category="agents"
)
logger.info(f"Initialized agent: {agent.name} ({agent.state.agent_type})")
except Exception as e:
logger.error(f"Error initializing agent {agent_type}: {str(e)}")
traceback.print_exc()
async def shutdown(self) -> None:
"""Gracefully shut down the system."""
if not self.running:
return
self.running = False
logger.info("Shutting down the system...")
# Stop workflow engine
if self.workflow_engine:
self.workflow_engine.stop()
# Save task state
if self.task_scheduler:
# Save any pending tasks
pass
# Close shared memory
if self.shared_memory:
await self.shared_memory.close()
# Close context store
if self.context_store:
# Check if context_store has a close method
if hasattr(self.context_store, 'close'):
await self.context_store.close()
if hasattr(self.context_store, 'clear_all'):
await self.context_store.clear_all()
else:
# No close method found, just log it
logger.info("No close method found for context_store")
# Close code tools
if self.code_tools:
self.code_tools.close()
uptime = time.time() - self.start_time
logger.info(f"System shutdown complete. Uptime: {uptime:.2f} seconds")
async def submit_task(self, task_def: Dict[str, Any]) -> str:
"""Submit a task to the system.
Args:
task_def: Task definition
Returns:
Task ID
"""
if not self.running:
raise RuntimeError("System is not running")
task_id = await self.task_scheduler.schedule_task(task_def)
return task_id
async def get_task_status(self, task_id: str) -> Dict[str, Any]:
"""Get the status of a task.
Args:
task_id: Task ID
Returns:
Task status information
"""
if not self.running:
raise RuntimeError("System is not running")
task = await self.task_scheduler.get_task(task_id)
if not task:
raise ValueError(f"Task {task_id} not found")
return task.to_dict()
async def get_agent_info(self, agent_id: Optional[str] = None) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
"""Get information about an agent or all agents.
Args:
agent_id: Optional agent ID
Returns:
Agent information or list of all agent information
"""
if not self.running:
raise RuntimeError("System is not running")
if agent_id:
if agent_id not in self.agent_registry:
raise ValueError(f"Agent {agent_id} not found")
agent = self.agent_registry[agent_id]
return {
"id": agent.state.agent_id,
"name": agent.name,
"type": agent.state.agent_type,
"model": f"{agent.llm.__class__.__name__}",
"stats": agent.get_performance_stats()
}
else:
return [
{
"id": agent.state.agent_id,
"name": agent.name,
"type": agent.state.agent_type,
"model": f"{agent.llm.__class__.__name__}"
}
for agent in self.agent_registry.values()
]
async def get_system_status(self) -> Dict[str, Any]:
"""Get system status information.
Returns:
System status information
"""
if not self.running:
return {"status": "stopped"}
uptime = time.time() - self.start_time
status = {
"status": "running",
"instance_id": self.instance_id,
"uptime_seconds": uptime,
"agent_count": len(self.agent_registry),
"version": VERSION,
"debug_mode": self.debug
}
# Add task stats if available
if self.task_scheduler:
task_count = len(getattr(self.task_scheduler, 'tasks', {}))
# Check if the attributes exist, otherwise use empty collections
if hasattr(self.task_scheduler, 'completed_task_ids'):
completed_count = len(self.task_scheduler.completed_task_ids)
else:
completed_count = 0
if hasattr(self.task_scheduler, 'failed_task_ids'):
failed_count = len(self.task_scheduler.failed_task_ids)
else:
failed_count = 0
status.update({
"tasks": {
"total": task_count,
"completed": completed_count,
"failed": failed_count,
"running": 0, # Would be calculated at runtime
"pending": 0, # Would be calculated at runtime
}
})
# Add workflow stats if available
if self.workflow_engine:
workflow_stats = await self.workflow_engine.get_stats()
status["workflows"] = workflow_stats
return status
async def create_workflow(self, workflow_def: Dict[str, Any]) -> str:
"""Create a new workflow.
Args:
workflow_def: Workflow definition
Returns:
Workflow ID
"""
if not self.running:
raise RuntimeError("System is not running")
workflow_id = await self.workflow_engine.register_workflow(workflow_def)
return workflow_id
async def execute_workflow(self, workflow_id: str, variables: Optional[Dict[str, Any]] = None) -> str:
"""Execute a workflow.
Args:
workflow_id: Workflow ID
variables: Initial variables for the workflow
Returns:
Workflow execution ID
"""
if not self.running:
raise RuntimeError("System is not running")
execution_id = await self.workflow_engine.execute_workflow(
workflow_id=workflow_id,
input_variables=variables
)
return execution_id
async def submit_feedback(self, feedback_data: Dict[str, Any]) -> Dict[str, Any]:
"""Submit feedback for a task.
Args:
feedback_data: Feedback data
Returns:
Result of feedback processing
"""
if not self.running:
raise RuntimeError("System is not running")
result = await self.feedback_processor.process_review(feedback_data)
return {"processed_items": len(result)}
async def interactive_session(system: MultiAgentSystem) -> None:
"""Run an interactive CLI session with the system.
Args:
system: Multi-agent system instance
"""
print(BANNER)
print(f"\nMulti-Agent Development System v{VERSION}")
print(f"Instance ID: {system.instance_id}")
print(f"Type 'help' for a list of commands\n")
commands = {
"help": "Show help",
"status": "Show system status",
"agents": "List all agents",
"agent <id>": "Show agent details",
"task <definition>": "Submit a new task",
"task-status <id>": "Check task status",
"workflow <definition>": "Create a new workflow",
"run-workflow <id> [variables]": "Execute a workflow",
"feedback <definition>": "Submit feedback",
"exit": "Exit the system"
}
while not system.shutdown_requested:
try:
command = input("\n> ").strip()
if not command:
continue
parts = command.split(' ', 1)
cmd = parts[0].lower()
args = parts[1] if len(parts) > 1 else ""
if cmd == "help":
print("\nAvailable commands:")
for cmd_name, description in commands.items():
print(f" {cmd_name:<20} - {description}")
elif cmd == "status":
status = await system.get_system_status()
print("\nSystem Status:")
print(json.dumps(status, indent=2))
elif cmd == "agents":
agents = await system.get_agent_info()
print("\nRegistered Agents:")
for agent in agents:
print(f" {agent['name']} ({agent['type']}) - ID: {agent['id']}")
elif cmd == "agent":
if not args:
print("Error: Agent ID is required")
continue
try:
agent_info = await system.get_agent_info(args)
print("\nAgent Details:")
print(json.dumps(agent_info, indent=2))
except ValueError as e:
print(f"Error: {str(e)}")
elif cmd == "task":
if not args:
print("Error: Task definition is required")
continue
try:
task_def = json.loads(args)
task_id = await system.submit_task(task_def)
print(f"Task submitted successfully. Task ID: {task_id}")
except json.JSONDecodeError:
print("Error: Invalid JSON for task definition")
except Exception as e:
print(f"Error submitting task: {str(e)}")
elif cmd == "task-status":
if not args:
print("Error: Task ID is required")
continue
try:
status = await system.get_task_status(args)
print("\nTask Status:")
print(json.dumps(status, indent=2))
except ValueError as e:
print(f"Error: {str(e)}")
except Exception as e:
print(f"Error getting task status: {str(e)}")
elif cmd == "workflow":
if not args:
print("Error: Workflow definition is required")
continue
try:
workflow_def = json.loads(args)
workflow_id = await system.create_workflow(workflow_def)
print(f"Workflow created successfully. Workflow ID: {workflow_id}")
except json.JSONDecodeError:
print("Error: Invalid JSON for workflow definition")
except Exception as e:
print(f"Error creating workflow: {str(e)}")
elif cmd == "run-workflow":
parts = args.split(' ', 1)
workflow_id = parts[0]
variables_str = parts[1] if len(parts) > 1 else "{}"
if not workflow_id:
print("Error: Workflow ID is required")
continue
try:
variables = json.loads(variables_str)
execution_id = await system.execute_workflow(workflow_id, variables)
print(f"Workflow execution started. Execution ID: {execution_id}")
except json.JSONDecodeError:
print("Error: Invalid JSON for variables")
except Exception as e:
print(f"Error executing workflow: {str(e)}")
elif cmd == "feedback":
if not args:
print("Error: Feedback definition is required")
continue
try:
feedback_data = json.loads(args)
result = await system.submit_feedback(feedback_data)
print(f"Feedback processed successfully: {result}")
except json.JSONDecodeError:
print("Error: Invalid JSON for feedback data")
except Exception as e:
print(f"Error processing feedback: {str(e)}")
elif cmd == "exit":
print("Shutting down...")
system.shutdown_requested = True
break
else:
print(f"Unknown command: {cmd}")
print("Type 'help' for a list of commands")
except KeyboardInterrupt:
print("\nOperation interrupted. Type 'exit' to quit.")
except Exception as e:
print(f"Error: {str(e)}")
# Perform shutdown
await system.shutdown()
async def run_api_server(system: MultiAgentSystem, host: str, port: int) -> None:
"""Run the API server for the system.
Args:
system: Multi-agent system instance
host: Host to bind to
port: Port to listen on
"""
try:
# Dynamically import FastAPI to avoid dependency when not using API
from fastapi import FastAPI, HTTPException, Depends, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from pydantic import BaseModel as PydanticBaseModel
app = FastAPI(
title="Multi-Agent Development API",
description="API for the Multi-Agent Development System",
version=VERSION
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Restrict in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# API models
class TaskDefinition(PydanticBaseModel):
title: str
description: str
agent_id: Optional[str] = None
agent_type: Optional[str] = None
action: str
params: Dict[str, Any] = {}
priority: Optional[str] = None
tags: List[str] = []
category: str = "default"
class WorkflowDefinition(PydanticBaseModel):
name: str
description: str
steps: Dict[str, Any]
start_step_id: str
variables: Dict[str, Any] = {}
class FeedbackSubmission(PydanticBaseModel):
task_id: str
reviewer_id: str
status: str
feedback_items: List[Dict[str, Any]]
summary: Optional[str] = None
class WorkflowVariables(PydanticBaseModel):
variables: Dict[str, Any] = {}
# API routes
@app.get("/")
async def root():
"""API root endpoint."""
return {
"name": "Multi-Agent Development API",
"version": VERSION,
"status": "running",
"instance_id": system.instance_id
}
@app.get("/status")
async def get_status():
"""Get system status."""
return await system.get_system_status()
@app.get("/agents")
async def list_agents():
"""List all agents."""
return await system.get_agent_info()
@app.get("/agents/{agent_id}")
async def get_agent(agent_id: str):
"""Get agent information."""
try:
return await system.get_agent_info(agent_id)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))
@app.post("/tasks")
async def create_task(task: TaskDefinition):
"""Create a new task."""
try:
task_id = await system.submit_task(task.dict())
return {"task_id": task_id, "status": "pending"}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/tasks/{task_id}")
async def get_task(task_id: str):
"""Get task status."""
try:
return await system.get_task_status(task_id)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))
@app.post("/workflows")
async def create_workflow(workflow: WorkflowDefinition):
"""Create a new workflow."""
try:
workflow_id = await system.create_workflow(workflow.dict())
return {"workflow_id": workflow_id}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post("/workflows/{workflow_id}/execute")
async def execute_workflow(workflow_id: str, variables: WorkflowVariables):
"""Execute a workflow."""
try:
execution_id = await system.execute_workflow(workflow_id, variables.variables)
return {"execution_id": execution_id, "status": "running"}
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post("/feedback")
async def submit_feedback(feedback: FeedbackSubmission):
"""Submit feedback for a task."""
try:
result = await system.submit_feedback(feedback.dict())
return result
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
# Handle shutdown
@app.on_event("shutdown")
async def shutdown_event():
await system.shutdown()
# Start the API server
logger.info(f"Starting API server on {host}:{port}")
config = uvicorn.Config(app, host=host, port=port)
server = uvicorn.Server(config)
await server.serve()
except ImportError:
logger.error("API server requires FastAPI and uvicorn. Install with 'pip install fastapi uvicorn'")
except Exception as e: