opencode-collaboration 2.0.0__tar.gz → 2.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/PKG-INFO +1 -1
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/opencode_collaboration.egg-info/PKG-INFO +1 -1
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/opencode_collaboration.egg-info/SOURCES.txt +40 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/pyproject.toml +1 -1
- opencode_collaboration-2.2.0/src/core/agent_manager.py +553 -0
- opencode_collaboration-2.2.0/src/core/config_reloader.py +257 -0
- opencode_collaboration-2.2.0/src/core/design_review_notifier.py +303 -0
- opencode_collaboration-2.2.0/src/core/error_templates.py +208 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/exception_handler.py +217 -2
- opencode_collaboration-2.2.0/src/core/git_workflow_enforcer.py +352 -0
- opencode_collaboration-2.2.0/src/core/iteration_status_manager.py +290 -0
- opencode_collaboration-2.2.0/src/core/meeting_manager.py +502 -0
- opencode_collaboration-2.2.0/src/core/monitor.py +268 -0
- opencode_collaboration-2.2.0/src/core/project_manager.py +549 -0
- opencode_collaboration-2.2.0/src/core/resource_lock.py +468 -0
- opencode_collaboration-2.2.0/src/core/state_migrator.py +404 -0
- opencode_collaboration-2.2.0/src/core/state_validator.py +564 -0
- opencode_collaboration-2.2.0/src/core/story_manager.py +712 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/tests/test_agent_daemon_true_long_running.py +26 -23
- opencode_collaboration-2.2.0/tests/test_agent_manager.py +284 -0
- opencode_collaboration-2.2.0/tests/test_auto_git_sync.py +159 -0
- opencode_collaboration-2.2.0/tests/test_auto_retry.py +287 -0
- opencode_collaboration-2.2.0/tests/test_blackbox_v210.py +406 -0
- opencode_collaboration-2.2.0/tests/test_brain_engine.py +456 -0
- opencode_collaboration-2.2.0/tests/test_config_reloader.py +609 -0
- opencode_collaboration-2.2.0/tests/test_daemon.py +516 -0
- opencode_collaboration-2.2.0/tests/test_daemon_integration.py +190 -0
- opencode_collaboration-2.2.0/tests/test_design_review_notifier.py +231 -0
- opencode_collaboration-2.2.0/tests/test_detector.py +240 -0
- opencode_collaboration-2.2.0/tests/test_e2e.py +375 -0
- opencode_collaboration-2.2.0/tests/test_e2e_v2_2_0.py +1138 -0
- opencode_collaboration-2.2.0/tests/test_error_templates.py +307 -0
- opencode_collaboration-2.2.0/tests/test_exception_handler.py +1217 -0
- opencode_collaboration-2.2.0/tests/test_git.py +525 -0
- opencode_collaboration-2.2.0/tests/test_git_monitor.py +679 -0
- opencode_collaboration-2.2.0/tests/test_git_workflow_enforcer.py +595 -0
- opencode_collaboration-2.2.0/tests/test_iteration_status_manager.py +272 -0
- opencode_collaboration-2.2.0/tests/test_m4_enhancements.py +395 -0
- opencode_collaboration-2.2.0/tests/test_meeting_manager.py +321 -0
- opencode_collaboration-2.2.0/tests/test_monitor.py +487 -0
- opencode_collaboration-2.2.0/tests/test_package_completeness.py +267 -0
- opencode_collaboration-2.2.0/tests/test_phase_advance.py +390 -0
- opencode_collaboration-2.2.0/tests/test_project_manager.py +275 -0
- opencode_collaboration-2.2.0/tests/test_resource_lock.py +370 -0
- opencode_collaboration-2.2.0/tests/test_signoff.py +396 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/tests/test_state_machine.py +132 -18
- opencode_collaboration-2.2.0/tests/test_state_manager.py +593 -0
- opencode_collaboration-2.2.0/tests/test_state_migration.py +504 -0
- opencode_collaboration-2.2.0/tests/test_state_validator.py +622 -0
- opencode_collaboration-2.2.0/tests/test_story_manager.py +529 -0
- opencode_collaboration-2.2.0/tests/test_supervisor.py +396 -0
- opencode_collaboration-2.2.0/tests/test_task_executor.py +862 -0
- opencode_collaboration-2.2.0/tests/test_workflow.py +316 -0
- opencode_collaboration-2.0.0/tests/test_detector.py +0 -79
- opencode_collaboration-2.0.0/tests/test_e2e.py +0 -1436
- opencode_collaboration-2.0.0/tests/test_exception_handler.py +0 -855
- opencode_collaboration-2.0.0/tests/test_git_monitor.py +0 -251
- opencode_collaboration-2.0.0/tests/test_state_manager.py +0 -109
- opencode_collaboration-2.0.0/tests/test_workflow.py +0 -82
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/README.md +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/opencode_collaboration.egg-info/dependency_links.txt +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/opencode_collaboration.egg-info/entry_points.txt +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/opencode_collaboration.egg-info/requires.txt +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/opencode_collaboration.egg-info/top_level.txt +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/setup.cfg +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/__init__.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/cli/__init__.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/cli/agent.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/cli/main.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/__init__.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/auto_doc_git.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/auto_docs.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/auto_engine.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/auto_git_sync.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/auto_retry.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/brain_engine.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/daemon.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/detector.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/doc_generator.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/git.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/git_monitor.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/phase_advance.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/signoff.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/state_machine.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/state_manager.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/supervisor.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/task_executor.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/core/workflow.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/main.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/utils/__init__.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/utils/date.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/utils/file.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/utils/lock.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/src/utils/yaml.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/tests/test_agent_behavior.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/tests/test_agent_daemon.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/tests/test_agent_daemon_complete.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/tests/test_agent_daemon_long_running.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/tests/test_doc_generator.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/tests/test_state_manager_v2.py +0 -0
- {opencode_collaboration-2.0.0 → opencode_collaboration-2.2.0}/tests/test_state_structure_compatibility.py +0 -0
|
@@ -12,22 +12,35 @@ src/cli/__init__.py
|
|
|
12
12
|
src/cli/agent.py
|
|
13
13
|
src/cli/main.py
|
|
14
14
|
src/core/__init__.py
|
|
15
|
+
src/core/agent_manager.py
|
|
15
16
|
src/core/auto_doc_git.py
|
|
16
17
|
src/core/auto_docs.py
|
|
17
18
|
src/core/auto_engine.py
|
|
18
19
|
src/core/auto_git_sync.py
|
|
19
20
|
src/core/auto_retry.py
|
|
20
21
|
src/core/brain_engine.py
|
|
22
|
+
src/core/config_reloader.py
|
|
21
23
|
src/core/daemon.py
|
|
24
|
+
src/core/design_review_notifier.py
|
|
22
25
|
src/core/detector.py
|
|
23
26
|
src/core/doc_generator.py
|
|
27
|
+
src/core/error_templates.py
|
|
24
28
|
src/core/exception_handler.py
|
|
25
29
|
src/core/git.py
|
|
26
30
|
src/core/git_monitor.py
|
|
31
|
+
src/core/git_workflow_enforcer.py
|
|
32
|
+
src/core/iteration_status_manager.py
|
|
33
|
+
src/core/meeting_manager.py
|
|
34
|
+
src/core/monitor.py
|
|
27
35
|
src/core/phase_advance.py
|
|
36
|
+
src/core/project_manager.py
|
|
37
|
+
src/core/resource_lock.py
|
|
28
38
|
src/core/signoff.py
|
|
29
39
|
src/core/state_machine.py
|
|
30
40
|
src/core/state_manager.py
|
|
41
|
+
src/core/state_migrator.py
|
|
42
|
+
src/core/state_validator.py
|
|
43
|
+
src/core/story_manager.py
|
|
31
44
|
src/core/supervisor.py
|
|
32
45
|
src/core/task_executor.py
|
|
33
46
|
src/core/workflow.py
|
|
@@ -41,13 +54,40 @@ tests/test_agent_daemon.py
|
|
|
41
54
|
tests/test_agent_daemon_complete.py
|
|
42
55
|
tests/test_agent_daemon_long_running.py
|
|
43
56
|
tests/test_agent_daemon_true_long_running.py
|
|
57
|
+
tests/test_agent_manager.py
|
|
58
|
+
tests/test_auto_git_sync.py
|
|
59
|
+
tests/test_auto_retry.py
|
|
60
|
+
tests/test_blackbox_v210.py
|
|
61
|
+
tests/test_brain_engine.py
|
|
62
|
+
tests/test_config_reloader.py
|
|
63
|
+
tests/test_daemon.py
|
|
64
|
+
tests/test_daemon_integration.py
|
|
65
|
+
tests/test_design_review_notifier.py
|
|
44
66
|
tests/test_detector.py
|
|
45
67
|
tests/test_doc_generator.py
|
|
46
68
|
tests/test_e2e.py
|
|
69
|
+
tests/test_e2e_v2_2_0.py
|
|
70
|
+
tests/test_error_templates.py
|
|
47
71
|
tests/test_exception_handler.py
|
|
72
|
+
tests/test_git.py
|
|
48
73
|
tests/test_git_monitor.py
|
|
74
|
+
tests/test_git_workflow_enforcer.py
|
|
75
|
+
tests/test_iteration_status_manager.py
|
|
76
|
+
tests/test_m4_enhancements.py
|
|
77
|
+
tests/test_meeting_manager.py
|
|
78
|
+
tests/test_monitor.py
|
|
79
|
+
tests/test_package_completeness.py
|
|
80
|
+
tests/test_phase_advance.py
|
|
81
|
+
tests/test_project_manager.py
|
|
82
|
+
tests/test_resource_lock.py
|
|
83
|
+
tests/test_signoff.py
|
|
49
84
|
tests/test_state_machine.py
|
|
50
85
|
tests/test_state_manager.py
|
|
51
86
|
tests/test_state_manager_v2.py
|
|
87
|
+
tests/test_state_migration.py
|
|
52
88
|
tests/test_state_structure_compatibility.py
|
|
89
|
+
tests/test_state_validator.py
|
|
90
|
+
tests/test_story_manager.py
|
|
91
|
+
tests/test_supervisor.py
|
|
92
|
+
tests/test_task_executor.py
|
|
53
93
|
tests/test_workflow.py
|
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
"""Agent 管理模块 - v2.2.0 M1 多 Agent 动态管理
|
|
2
|
+
|
|
3
|
+
提供 Agent 角色体系、动态添加、职责约束等功能。
|
|
4
|
+
"""
|
|
5
|
+
import os
|
|
6
|
+
import uuid
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from enum import Enum
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Dict, List, Optional, Set
|
|
11
|
+
import yaml
|
|
12
|
+
import json
|
|
13
|
+
import logging
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class AgentType(Enum):
|
|
20
|
+
"""Agent 类型枚举。"""
|
|
21
|
+
PRODUCT_MANAGER = "product_manager"
|
|
22
|
+
DEVELOPMENT_LEAD = "development_lead"
|
|
23
|
+
FRONTEND_DEV = "frontend_dev"
|
|
24
|
+
BACKEND_DEV = "backend_dev"
|
|
25
|
+
DESIGNER = "designer"
|
|
26
|
+
TESTER = "tester"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ActionType(Enum):
|
|
30
|
+
"""操作类型枚举。"""
|
|
31
|
+
CREATE_REQUIREMENTS = "CREATE_REQUIREMENTS"
|
|
32
|
+
REVIEW_DESIGN = "REVIEW_DESIGN"
|
|
33
|
+
SIGN_OFF = "SIGN_OFF"
|
|
34
|
+
MANAGE_PROJECT = "MANAGE_PROJECT"
|
|
35
|
+
WRITE_CODE = "WRITE_CODE"
|
|
36
|
+
CREATE_DESIGN = "CREATE_DESIGN"
|
|
37
|
+
CODE_REVIEW = "CODE_REVIEW"
|
|
38
|
+
WRITE_CODE_FRONTEND = "WRITE_CODE_FRONTEND"
|
|
39
|
+
WRITE_CODE_BACKEND = "WRITE_CODE_BACKEND"
|
|
40
|
+
REVIEW_DESIGN_FRONTEND = "REVIEW_DESIGN_FRONTEND"
|
|
41
|
+
API_DESIGN = "API_DESIGN"
|
|
42
|
+
UPLOAD_DESIGN = "UPLOAD_DESIGN"
|
|
43
|
+
EXECUTE_TEST = "EXECUTE_TEST"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class AgentConfig:
|
|
48
|
+
"""Agent 配置。"""
|
|
49
|
+
agent_id: str
|
|
50
|
+
agent_type: AgentType
|
|
51
|
+
name: str
|
|
52
|
+
role: str
|
|
53
|
+
responsibilities: List[str]
|
|
54
|
+
forbidden: List[str]
|
|
55
|
+
tech_stack: Optional[str] = None
|
|
56
|
+
created_at: str = field(default_factory=lambda: str(uuid.uuid4()))
|
|
57
|
+
last_active: Optional[str] = None
|
|
58
|
+
status: str = "active"
|
|
59
|
+
|
|
60
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
61
|
+
"""转换为字典。"""
|
|
62
|
+
return {
|
|
63
|
+
"agent_id": self.agent_id,
|
|
64
|
+
"agent_type": self.agent_type.value,
|
|
65
|
+
"name": self.name,
|
|
66
|
+
"role": self.role,
|
|
67
|
+
"responsibilities": self.responsibilities,
|
|
68
|
+
"forbidden": self.forbidden,
|
|
69
|
+
"tech_stack": self.tech_stack,
|
|
70
|
+
"created_at": self.created_at,
|
|
71
|
+
"last_active": self.last_active,
|
|
72
|
+
"status": self.status
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@classmethod
|
|
76
|
+
def from_dict(cls, data: Dict[str, Any]) -> "AgentConfig":
|
|
77
|
+
"""从字典创建。"""
|
|
78
|
+
return cls(
|
|
79
|
+
agent_id=data["agent_id"],
|
|
80
|
+
agent_type=AgentType(data["agent_type"]),
|
|
81
|
+
name=data["name"],
|
|
82
|
+
role=data["role"],
|
|
83
|
+
responsibilities=data["responsibilities"],
|
|
84
|
+
forbidden=data["forbidden"],
|
|
85
|
+
tech_stack=data.get("tech_stack"),
|
|
86
|
+
created_at=data.get("created_at", str(uuid.uuid4())),
|
|
87
|
+
last_active=data.get("last_active"),
|
|
88
|
+
status=data.get("status", "active")
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
AGENT_ROLE_CONFIG: Dict[AgentType, Dict[str, Any]] = {
|
|
93
|
+
AgentType.PRODUCT_MANAGER: {
|
|
94
|
+
"role": "产品经理/项目经理",
|
|
95
|
+
"allowed": [
|
|
96
|
+
ActionType.CREATE_REQUIREMENTS,
|
|
97
|
+
ActionType.REVIEW_DESIGN,
|
|
98
|
+
ActionType.SIGN_OFF,
|
|
99
|
+
ActionType.MANAGE_PROJECT
|
|
100
|
+
],
|
|
101
|
+
"forbidden": [
|
|
102
|
+
ActionType.WRITE_CODE,
|
|
103
|
+
ActionType.CREATE_DESIGN
|
|
104
|
+
],
|
|
105
|
+
"required": True,
|
|
106
|
+
"initial_count": 1
|
|
107
|
+
},
|
|
108
|
+
AgentType.DEVELOPMENT_LEAD: {
|
|
109
|
+
"role": "开发负责人",
|
|
110
|
+
"allowed": [
|
|
111
|
+
ActionType.CREATE_DESIGN,
|
|
112
|
+
ActionType.WRITE_CODE,
|
|
113
|
+
ActionType.CODE_REVIEW
|
|
114
|
+
],
|
|
115
|
+
"forbidden": [
|
|
116
|
+
],
|
|
117
|
+
"required": True,
|
|
118
|
+
"initial_count": 1
|
|
119
|
+
},
|
|
120
|
+
AgentType.FRONTEND_DEV: {
|
|
121
|
+
"role": "前端开发",
|
|
122
|
+
"allowed": [
|
|
123
|
+
ActionType.WRITE_CODE_FRONTEND,
|
|
124
|
+
ActionType.REVIEW_DESIGN_FRONTEND
|
|
125
|
+
],
|
|
126
|
+
"forbidden": [
|
|
127
|
+
ActionType.WRITE_CODE_BACKEND,
|
|
128
|
+
ActionType.CREATE_REQUIREMENTS
|
|
129
|
+
],
|
|
130
|
+
"required": False,
|
|
131
|
+
"initial_count": 0
|
|
132
|
+
},
|
|
133
|
+
AgentType.BACKEND_DEV: {
|
|
134
|
+
"role": "后端开发",
|
|
135
|
+
"allowed": [
|
|
136
|
+
ActionType.WRITE_CODE_BACKEND,
|
|
137
|
+
ActionType.API_DESIGN
|
|
138
|
+
],
|
|
139
|
+
"forbidden": [
|
|
140
|
+
ActionType.WRITE_CODE_FRONTEND,
|
|
141
|
+
ActionType.CREATE_REQUIREMENTS
|
|
142
|
+
],
|
|
143
|
+
"required": False,
|
|
144
|
+
"initial_count": 0
|
|
145
|
+
},
|
|
146
|
+
AgentType.DESIGNER: {
|
|
147
|
+
"role": "UI/UE 设计",
|
|
148
|
+
"allowed": [
|
|
149
|
+
ActionType.CREATE_DESIGN,
|
|
150
|
+
ActionType.UPLOAD_DESIGN,
|
|
151
|
+
ActionType.REVIEW_DESIGN
|
|
152
|
+
],
|
|
153
|
+
"forbidden": [
|
|
154
|
+
ActionType.WRITE_CODE,
|
|
155
|
+
ActionType.CREATE_REQUIREMENTS
|
|
156
|
+
],
|
|
157
|
+
"required": False,
|
|
158
|
+
"initial_count": 0
|
|
159
|
+
},
|
|
160
|
+
AgentType.TESTER: {
|
|
161
|
+
"role": "测试",
|
|
162
|
+
"allowed": [
|
|
163
|
+
ActionType.EXECUTE_TEST,
|
|
164
|
+
ActionType.REVIEW_DESIGN
|
|
165
|
+
],
|
|
166
|
+
"forbidden": [
|
|
167
|
+
ActionType.WRITE_CODE,
|
|
168
|
+
ActionType.CREATE_REQUIREMENTS,
|
|
169
|
+
ActionType.CREATE_DESIGN
|
|
170
|
+
],
|
|
171
|
+
"required": False,
|
|
172
|
+
"initial_count": 0
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class AgentManagerError(Exception):
|
|
178
|
+
"""Agent 管理异常基类。"""
|
|
179
|
+
pass
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
class AgentNotFoundError(AgentManagerError):
|
|
183
|
+
"""Agent 未找到异常。"""
|
|
184
|
+
pass
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class AgentTypeNotSupportedError(AgentManagerError):
|
|
188
|
+
"""Agent 类型不支持异常。"""
|
|
189
|
+
pass
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
class AgentConstraintViolationError(AgentManagerError):
|
|
193
|
+
"""Agent 约束违反异常。"""
|
|
194
|
+
pass
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
class AgentManager:
|
|
198
|
+
"""Agent 管理器。"""
|
|
199
|
+
|
|
200
|
+
DEFAULT_AGENTS_DIR = "agents"
|
|
201
|
+
AGENT_CONFIG_FILE = "agent_config.yaml"
|
|
202
|
+
|
|
203
|
+
def __init__(self, project_path: str, agents_dir: Optional[str] = None):
|
|
204
|
+
"""初始化 Agent 管理器。
|
|
205
|
+
|
|
206
|
+
Args:
|
|
207
|
+
project_path: 项目路径
|
|
208
|
+
agents_dir: Agent 配置目录,默认为 project_path/agents
|
|
209
|
+
"""
|
|
210
|
+
self.project_path = Path(project_path)
|
|
211
|
+
self.agents_dir = self.project_path / (agents_dir or self.DEFAULT_AGENTS_DIR)
|
|
212
|
+
self.agents: Dict[str, AgentConfig] = {}
|
|
213
|
+
self._ensure_agents_directory()
|
|
214
|
+
|
|
215
|
+
def _ensure_agents_directory(self) -> None:
|
|
216
|
+
"""确保 Agent 目录存在。"""
|
|
217
|
+
self.agents_dir.mkdir(parents=True, exist_ok=True)
|
|
218
|
+
|
|
219
|
+
def _generate_agent_id(self, agent_type: AgentType, tech_stack: Optional[str] = None) -> str:
|
|
220
|
+
"""生成 Agent ID。
|
|
221
|
+
|
|
222
|
+
Args:
|
|
223
|
+
agent_type: Agent 类型
|
|
224
|
+
tech_stack: 技术栈(可选)
|
|
225
|
+
|
|
226
|
+
Returns:
|
|
227
|
+
Agent ID
|
|
228
|
+
"""
|
|
229
|
+
base_name = agent_type.value
|
|
230
|
+
if tech_stack:
|
|
231
|
+
base_name = f"{base_name}_{tech_stack}"
|
|
232
|
+
existing_count = sum(
|
|
233
|
+
1 for a in self.agents.values()
|
|
234
|
+
if a.agent_type == agent_type and (a.tech_stack or "") == (tech_stack or "")
|
|
235
|
+
)
|
|
236
|
+
return f"agent_{base_name}_{existing_count + 1}"
|
|
237
|
+
|
|
238
|
+
def _create_default_agents(self) -> List[AgentConfig]:
|
|
239
|
+
"""创建默认的 Agent 列表。
|
|
240
|
+
|
|
241
|
+
Returns:
|
|
242
|
+
默认 Agent 列表
|
|
243
|
+
"""
|
|
244
|
+
agents = []
|
|
245
|
+
|
|
246
|
+
for agent_type, config in AGENT_ROLE_CONFIG.items():
|
|
247
|
+
for i in range(config["initial_count"]):
|
|
248
|
+
agent_id = self._generate_agent_id(agent_type)
|
|
249
|
+
agent = AgentConfig(
|
|
250
|
+
agent_id=agent_id,
|
|
251
|
+
agent_type=agent_type,
|
|
252
|
+
name=f"{config['role']} {i + 1}",
|
|
253
|
+
role=config["role"],
|
|
254
|
+
responsibilities=config["role"].split("/"),
|
|
255
|
+
forbidden=[a.value for a in config["forbidden"]],
|
|
256
|
+
tech_stack=None
|
|
257
|
+
)
|
|
258
|
+
agents.append(agent)
|
|
259
|
+
|
|
260
|
+
return agents
|
|
261
|
+
|
|
262
|
+
def initialize_agents(self, custom_agents: Optional[List[AgentConfig]] = None) -> List[AgentConfig]:
|
|
263
|
+
"""初始化 Agent。
|
|
264
|
+
|
|
265
|
+
Args:
|
|
266
|
+
custom_agents: 自定义 Agent 列表
|
|
267
|
+
|
|
268
|
+
Returns:
|
|
269
|
+
初始化后的 Agent 列表
|
|
270
|
+
"""
|
|
271
|
+
if custom_agents:
|
|
272
|
+
agents = custom_agents
|
|
273
|
+
else:
|
|
274
|
+
agents = self._create_default_agents()
|
|
275
|
+
|
|
276
|
+
for agent in agents:
|
|
277
|
+
self.agents[agent.agent_id] = agent
|
|
278
|
+
self._save_agent_config(agent)
|
|
279
|
+
|
|
280
|
+
logger.info(f"初始化了 {len(agents)} 个 Agent")
|
|
281
|
+
return agents
|
|
282
|
+
|
|
283
|
+
def _save_agent_config(self, agent: AgentConfig) -> None:
|
|
284
|
+
"""保存 Agent 配置。
|
|
285
|
+
|
|
286
|
+
Args:
|
|
287
|
+
agent: Agent 配置
|
|
288
|
+
"""
|
|
289
|
+
config_file = self.agents_dir / f"{agent.agent_id}.yaml"
|
|
290
|
+
with open(config_file, 'w', encoding='utf-8') as f:
|
|
291
|
+
yaml.dump(agent.to_dict(), f, allow_unicode=True)
|
|
292
|
+
|
|
293
|
+
def load_agents(self) -> List[AgentConfig]:
|
|
294
|
+
"""加载所有 Agent 配置。
|
|
295
|
+
|
|
296
|
+
Returns:
|
|
297
|
+
Agent 配置列表
|
|
298
|
+
"""
|
|
299
|
+
self.agents.clear()
|
|
300
|
+
|
|
301
|
+
for config_file in self.agents_dir.glob("*.yaml"):
|
|
302
|
+
try:
|
|
303
|
+
with open(config_file, 'r', encoding='utf-8') as f:
|
|
304
|
+
data = yaml.safe_load(f)
|
|
305
|
+
if data:
|
|
306
|
+
agent = AgentConfig.from_dict(data)
|
|
307
|
+
self.agents[agent.agent_id] = agent
|
|
308
|
+
except Exception as e:
|
|
309
|
+
logger.warning(f"加载 Agent 配置失败: {config_file}, 错误: {e}")
|
|
310
|
+
|
|
311
|
+
return list(self.agents.values())
|
|
312
|
+
|
|
313
|
+
def add_agent(
|
|
314
|
+
self,
|
|
315
|
+
agent_type: AgentType,
|
|
316
|
+
tech_stack: Optional[str] = None,
|
|
317
|
+
name: Optional[str] = None
|
|
318
|
+
) -> AgentConfig:
|
|
319
|
+
"""添加新 Agent。
|
|
320
|
+
|
|
321
|
+
Args:
|
|
322
|
+
agent_type: Agent 类型
|
|
323
|
+
tech_stack: 技术栈(可选)
|
|
324
|
+
name: Agent 名称(可选)
|
|
325
|
+
|
|
326
|
+
Returns:
|
|
327
|
+
新创建的 Agent 配置
|
|
328
|
+
|
|
329
|
+
Raises:
|
|
330
|
+
AgentTypeNotSupportedError: Agent 类型不支持
|
|
331
|
+
"""
|
|
332
|
+
if agent_type not in AGENT_ROLE_CONFIG:
|
|
333
|
+
raise AgentTypeNotSupportedError(f"不支持的 Agent 类型: {agent_type}")
|
|
334
|
+
|
|
335
|
+
role_config = AGENT_ROLE_CONFIG[agent_type]
|
|
336
|
+
agent_id = self._generate_agent_id(agent_type, tech_stack)
|
|
337
|
+
|
|
338
|
+
agent = AgentConfig(
|
|
339
|
+
agent_id=agent_id,
|
|
340
|
+
agent_type=agent_type,
|
|
341
|
+
name=name or f"{role_config['role']} {len([a for a in self.agents.values() if a.agent_type == agent_type]) + 1}",
|
|
342
|
+
role=role_config["role"],
|
|
343
|
+
responsibilities=role_config["role"].split("/"),
|
|
344
|
+
forbidden=[a.value for a in role_config["forbidden"]],
|
|
345
|
+
tech_stack=tech_stack
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
self.agents[agent.agent_id] = agent
|
|
349
|
+
self._save_agent_config(agent)
|
|
350
|
+
|
|
351
|
+
logger.info(f"添加了新 Agent: {agent_id} ({agent_type.value})")
|
|
352
|
+
return agent
|
|
353
|
+
|
|
354
|
+
def remove_agent(self, agent_id: str) -> bool:
|
|
355
|
+
"""移除 Agent。
|
|
356
|
+
|
|
357
|
+
Args:
|
|
358
|
+
agent_id: Agent ID
|
|
359
|
+
|
|
360
|
+
Returns:
|
|
361
|
+
是否成功移除
|
|
362
|
+
|
|
363
|
+
Raises:
|
|
364
|
+
AgentNotFoundError: Agent 未找到
|
|
365
|
+
"""
|
|
366
|
+
if agent_id not in self.agents:
|
|
367
|
+
raise AgentNotFoundError(f"Agent 未找到: {agent_id}")
|
|
368
|
+
|
|
369
|
+
agent = self.agents.pop(agent_id)
|
|
370
|
+
config_file = self.agents_dir / f"{agent_id}.yaml"
|
|
371
|
+
|
|
372
|
+
if config_file.exists():
|
|
373
|
+
config_file.unlink()
|
|
374
|
+
|
|
375
|
+
logger.info(f"移除了 Agent: {agent_id}")
|
|
376
|
+
return True
|
|
377
|
+
|
|
378
|
+
def get_agent(self, agent_id: str) -> AgentConfig:
|
|
379
|
+
"""获取 Agent 配置。
|
|
380
|
+
|
|
381
|
+
Args:
|
|
382
|
+
agent_id: Agent ID
|
|
383
|
+
|
|
384
|
+
Returns:
|
|
385
|
+
Agent 配置
|
|
386
|
+
|
|
387
|
+
Raises:
|
|
388
|
+
AgentNotFoundError: Agent 未找到
|
|
389
|
+
"""
|
|
390
|
+
if agent_id not in self.agents:
|
|
391
|
+
raise AgentNotFoundError(f"Agent 未找到: {agent_id}")
|
|
392
|
+
return self.agents[agent_id]
|
|
393
|
+
|
|
394
|
+
def list_agents(self, agent_type: Optional[AgentType] = None, status: Optional[str] = None) -> List[AgentConfig]:
|
|
395
|
+
"""列出 Agent。
|
|
396
|
+
|
|
397
|
+
Args:
|
|
398
|
+
agent_type: Agent 类型过滤
|
|
399
|
+
status: 状态过滤
|
|
400
|
+
|
|
401
|
+
Returns:
|
|
402
|
+
Agent 配置列表
|
|
403
|
+
"""
|
|
404
|
+
agents = list(self.agents.values())
|
|
405
|
+
|
|
406
|
+
if agent_type:
|
|
407
|
+
agents = [a for a in agents if a.agent_type == agent_type]
|
|
408
|
+
|
|
409
|
+
if status:
|
|
410
|
+
agents = [a for a in agents if a.status == status]
|
|
411
|
+
|
|
412
|
+
return agents
|
|
413
|
+
|
|
414
|
+
def check_action_allowed(self, agent_id: str, action: ActionType) -> bool:
|
|
415
|
+
"""检查 Agent 是否允许执行某个操作。
|
|
416
|
+
|
|
417
|
+
Args:
|
|
418
|
+
agent_id: Agent ID
|
|
419
|
+
action: 操作类型
|
|
420
|
+
|
|
421
|
+
Returns:
|
|
422
|
+
是否允许
|
|
423
|
+
|
|
424
|
+
Raises:
|
|
425
|
+
AgentNotFoundError: Agent 未找到
|
|
426
|
+
"""
|
|
427
|
+
agent = self.get_agent(agent_id)
|
|
428
|
+
|
|
429
|
+
if action.value in agent.forbidden:
|
|
430
|
+
return False
|
|
431
|
+
|
|
432
|
+
if agent.agent_type in AGENT_ROLE_CONFIG:
|
|
433
|
+
role_config = AGENT_ROLE_CONFIG[agent.agent_type]
|
|
434
|
+
return action in role_config["allowed"]
|
|
435
|
+
|
|
436
|
+
return True
|
|
437
|
+
|
|
438
|
+
def get_allowed_actions(self, agent_id: str) -> Set[ActionType]:
|
|
439
|
+
"""获取 Agent 允许的操作列表。
|
|
440
|
+
|
|
441
|
+
Args:
|
|
442
|
+
agent_id: Agent ID
|
|
443
|
+
|
|
444
|
+
Returns:
|
|
445
|
+
允许的操作集合
|
|
446
|
+
|
|
447
|
+
Raises:
|
|
448
|
+
AgentNotFoundError: Agent 未找到
|
|
449
|
+
"""
|
|
450
|
+
agent = self.get_agent(agent_id)
|
|
451
|
+
|
|
452
|
+
if agent.agent_type in AGENT_ROLE_CONFIG:
|
|
453
|
+
return set(AGENT_ROLE_CONFIG[agent.agent_type]["allowed"])
|
|
454
|
+
|
|
455
|
+
return set()
|
|
456
|
+
|
|
457
|
+
def get_forbidden_actions(self, agent_id: str) -> Set[ActionType]:
|
|
458
|
+
"""获取 Agent 被禁止的操作列表。
|
|
459
|
+
|
|
460
|
+
Args:
|
|
461
|
+
agent_id: Agent ID
|
|
462
|
+
|
|
463
|
+
Returns:
|
|
464
|
+
被禁止的操作集合
|
|
465
|
+
|
|
466
|
+
Raises:
|
|
467
|
+
AgentNotFoundError: Agent 未找到
|
|
468
|
+
"""
|
|
469
|
+
agent = self.get_agent(agent_id)
|
|
470
|
+
return set(ActionType(a) for a in agent.forbidden)
|
|
471
|
+
|
|
472
|
+
def update_agent_status(self, agent_id: str, status: str) -> AgentConfig:
|
|
473
|
+
"""更新 Agent 状态。
|
|
474
|
+
|
|
475
|
+
Args:
|
|
476
|
+
agent_id: Agent ID
|
|
477
|
+
status: 新状态
|
|
478
|
+
|
|
479
|
+
Returns:
|
|
480
|
+
更新后的 Agent 配置
|
|
481
|
+
|
|
482
|
+
Raises:
|
|
483
|
+
AgentNotFoundError: Agent 未找到
|
|
484
|
+
"""
|
|
485
|
+
agent = self.get_agent(agent_id)
|
|
486
|
+
agent.status = status
|
|
487
|
+
self._save_agent_config(agent)
|
|
488
|
+
|
|
489
|
+
return agent
|
|
490
|
+
|
|
491
|
+
def get_agent_summary(self) -> Dict[str, Any]:
|
|
492
|
+
"""获取 Agent 管理摘要。
|
|
493
|
+
|
|
494
|
+
Returns:
|
|
495
|
+
摘要信息
|
|
496
|
+
"""
|
|
497
|
+
agent_counts = {}
|
|
498
|
+
for agent_type in AgentType:
|
|
499
|
+
count = len(self.list_agents(agent_type=agent_type))
|
|
500
|
+
agent_counts[agent_type.value] = count
|
|
501
|
+
|
|
502
|
+
return {
|
|
503
|
+
"total_agents": len(self.agents),
|
|
504
|
+
"by_type": agent_counts,
|
|
505
|
+
"agents_dir": str(self.agents_dir)
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
def export_config(self, output_path: Optional[str] = None) -> Dict[str, Any]:
|
|
509
|
+
"""导出 Agent 管理配置。
|
|
510
|
+
|
|
511
|
+
Args:
|
|
512
|
+
output_path: 输出路径(可选)
|
|
513
|
+
|
|
514
|
+
Returns:
|
|
515
|
+
配置字典
|
|
516
|
+
"""
|
|
517
|
+
config = {
|
|
518
|
+
"project_path": str(self.project_path),
|
|
519
|
+
"agents_dir": str(self.agents_dir),
|
|
520
|
+
"agents": [agent.to_dict() for agent in self.agents.values()],
|
|
521
|
+
"agent_types": {
|
|
522
|
+
at.value: {
|
|
523
|
+
"role": AGENT_ROLE_CONFIG[at]["role"],
|
|
524
|
+
"required": AGENT_ROLE_CONFIG[at]["required"]
|
|
525
|
+
}
|
|
526
|
+
for at in AgentType
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
if output_path:
|
|
531
|
+
with open(output_path, 'w', encoding='utf-8') as f:
|
|
532
|
+
yaml.dump(config, f, allow_unicode=True)
|
|
533
|
+
|
|
534
|
+
return config
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
if __name__ == "__main__":
|
|
538
|
+
import tempfile
|
|
539
|
+
|
|
540
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
541
|
+
manager = AgentManager(tmpdir)
|
|
542
|
+
|
|
543
|
+
agents = manager.initialize_agents()
|
|
544
|
+
print(f"初始化了 {len(agents)} 个 Agent")
|
|
545
|
+
|
|
546
|
+
for agent in agents:
|
|
547
|
+
print(f" - {agent.agent_id}: {agent.role}")
|
|
548
|
+
|
|
549
|
+
new_agent = manager.add_agent(AgentType.FRONTEND_DEV, "react")
|
|
550
|
+
print(f"\n添加了新 Agent: {new_agent.agent_id}")
|
|
551
|
+
|
|
552
|
+
summary = manager.get_agent_summary()
|
|
553
|
+
print(f"\nAgent 摘要: {summary}")
|