File size: 2,876 Bytes
e92be04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { db } from '../config/gun.js';
import { gunSafe } from '../utils/gunUtils.js';

/**
 * TeamService — Phase 24: Swarm Intelligence
 * 
 * Manages the formation and coordination of multi-agent squads 
 * dedicated to specific research tasks or investigations.
 */

class TeamService {
    /**
     * Creates a new research team for a specific task.
     */
    async createTeam(leaderId, taskId, teamName = null) {
        const teamId = `team-${Math.random().toString(36).substring(2, 10)}`;
        const now = Date.now();
        
        const teamData = {
            id: teamId,
            name: teamName || `Squad-${teamId.slice(5, 9)}`,
            leaderId,
            taskId,
            createdAt: now,
            status: 'ACTIVE',
            memberCount: 1
        };

        return new Promise((resolve) => {
            // 1. Create team record
            db.get('swarm_teams').get(teamId).put(gunSafe(teamData));
            
            // 2. Add leader as first member
            db.get('swarm_teams').get(teamId).get('members').get(leaderId).put({
                joinedAt: now,
                role: 'LEADER'
            });

            // 3. Link task to team (optional but helpful)
            db.get('swarm_tasks').get(taskId).get('active_teams').get(teamId).put(true);

            console.log(`[SWARM] Team created: ${teamId} by ${leaderId} for task ${taskId}`);
            resolve(teamData);
        });
    }

    /**
     * Adds an agent to an existing team.
     */
    async joinTeam(agentId, teamId) {
        return new Promise((resolve, reject) => {
            db.get('swarm_teams').get(teamId).once((team) => {
                if (!team) return reject(new Error('Team not found'));
                
                const now = Date.now();
                db.get('swarm_teams').get(teamId).get('members').get(agentId).put({
                    joinedAt: now,
                    role: 'CONTRIBUTOR'
                });

                // Increment member count
                const newCount = (team.memberCount || 0) + 1;
                db.get('swarm_teams').get(teamId).put({ memberCount: newCount });

                console.log(`[SWARM] Agent ${agentId} joined team ${teamId}`);
                resolve({ success: true, teamId, memberCount: newCount });
            });
        });
    }

    /**
     * Returns all active teams with their members.
     */
    async getTeams() {
        return new Promise((resolve) => {
            const teams = [];
            db.get('swarm_teams').map().once((team, id) => {
                if (team && team.status === 'ACTIVE') {
                    teams.push(team);
                }
            });

            // Delay for map recursion to populate
            setTimeout(() => resolve(teams), 500);
        });
    }
}

export const teamService = new TeamService();