File size: 6,827 Bytes
068bc7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""TeamCreateTool - Multi-agent team coordination for Stack 2.9"""

import json
import uuid
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional

from .base import BaseTool, ToolParam, ToolResult
from .registry import tool_registry

TEAMS_FILE = Path.home() / ".stack-2.9" / "teams.json"


def _load_teams() -> Dict[str, Any]:
    """Load teams from disk."""
    TEAMS_FILE.parent.mkdir(parents=True, exist_ok=True)
    if TEAMS_FILE.exists():
        return json.loads(TEAMS_FILE.read_text())
    return {"teams": []}


def _save_teams(data: Dict[str, Any]) -> None:
    """Save teams to disk."""
    TEAMS_FILE.write_text(json.dumps(data, indent=2))


class TeamCreateTool(BaseTool):
    """Create a team of agents for coordinated work."""

    name = "team_create"
    description = "Create a team of agents that can work together on tasks"

    input_schema = {
        "type": "object",
        "properties": {
            "team_name": {
                "type": "string",
                "description": "Name for the team"
            },
            "agents": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "name": {"type": "string"},
                        "role": {"type": "string"},
                        "skills": {"type": "array", "items": {"type": "string"}}
                    }
                },
                "description": "List of agents in the team"
            },
            "task": {
                "type": "string",
                "description": "Initial task for the team"
            },
            "coordination_mode": {
                "type": "string",
                "enum": ["parallel", "sequential", "hierarchical"],
                "default": "parallel",
                "description": "How agents coordinate"
            }
        },
        "required": ["team_name", "agents"]
    }

    async def execute(self, team_name: str, agents: List[Dict], task: str = "", coordination_mode: str = "parallel") -> ToolResult:
        """Create a new team."""
        data = _load_teams()

        team_id = str(uuid.uuid4())[:8]
        team = {
            "id": team_id,
            "name": team_name,
            "agents": agents,
            "task": task,
            "coordination_mode": coordination_mode,
            "status": "created",
            "created_at": datetime.now().isoformat(),
            "results": []
        }

        data["teams"].append(team)
        _save_teams(data)

        return ToolResult(success=True, data={
            "team_id": team_id,
            "team_name": team_name,
            "status": "created",
            "agents_count": len(agents),
            "coordination_mode": coordination_mode,
            "created_at": team["created_at"]
        })


class TeamDisbandTool(BaseTool):
    """Disband a team."""

    name = "team_disband"
    description = "Disband and clean up a team"

    input_schema = {
        "type": "object",
        "properties": {
            "team_id": {
                "type": "string",
                "description": "Team ID to disband"
            }
        },
        "required": ["team_id"]
    }

    async def execute(self, team_id: str) -> ToolResult:
        """Disband team."""
        data = _load_teams()
        teams = data["teams"]
        original_count = len(teams)
        teams = [t for t in teams if t["id"] != team_id]

        if len(teams) == original_count:
            return ToolResult(success=False, error=f"Team {team_id} not found")

        data["teams"] = teams
        _save_teams(data)

        return ToolResult(success=True, data={
            "team_id": team_id,
            "status": "disbanded"
        })


class TeamListTool(BaseTool):
    """List all teams."""

    name = "team_list"
    description = "List all teams and their status"

    input_schema = {
        "type": "object",
        "properties": {},
        "required": []
    }

    async def execute(self) -> ToolResult:
        """List teams."""
        data = _load_teams()
        return ToolResult(success=True, data={
            "teams": data.get("teams", []),
            "count": len(data.get("teams", []))
        })


class TeamStatusTool(BaseTool):
    """Get status of a specific team."""

    name = "team_status"
    description = "Get detailed status of a team"

    input_schema = {
        "type": "object",
        "properties": {
            "team_id": {
                "type": "string",
                "description": "Team ID to check"
            }
        },
        "required": ["team_id"]
    }

    async def execute(self, team_id: str) -> ToolResult:
        """Get team status."""
        data = _load_teams()
        for team in data.get("teams", []):
            if team["id"] == team_id:
                return ToolResult(success=True, data=team)

        return ToolResult(success=False, error=f"Team {team_id} not found")


class TeamAssignTaskTool(BaseTool):
    """Assign a task to a team."""

    name = "team_assign"
    description = "Assign a new task to an existing team"

    input_schema = {
        "type": "object",
        "properties": {
            "team_id": {
                "type": "string",
                "description": "Team ID"
            },
            "task": {
                "type": "string",
                "description": "Task to assign"
            },
            "agent_name": {
                "type": "string",
                "description": "Specific agent to assign to (optional)"
            }
        },
        "required": ["team_id", "task"]
    }

    async def execute(self, team_id: str, task: str, agent_name: Optional[str] = None) -> ToolResult:
        """Assign task to team."""
        data = _load_teams()
        for team in data.get("teams", []):
            if team["id"] == team_id:
                if agent_name:
                    team["current_task"] = {"task": task, "agent": agent_name, "assigned_at": datetime.now().isoformat()}
                else:
                    team["task"] = task
                    team["current_task"] = {"task": task, "assigned_at": datetime.now().isoformat()}
                _save_teams(data)
                return ToolResult(success=True, data={
                    "team_id": team_id,
                    "task": task,
                    "agent": agent_name,
                    "status": "assigned"
                })

        return ToolResult(success=False, error=f"Team {team_id} not found")


# Register tools
tool_registry.register(TeamCreateTool())
tool_registry.register(TeamDisbandTool())
tool_registry.register(TeamListTool())
tool_registry.register(TeamStatusTool())
tool_registry.register(TeamAssignTaskTool())