Skip to main content

Collaboration State Management

Tracking Collaboration State

Build upon your existing SSE event handling to track collaboration state:
class CollaborationManager {
    constructor() {
        this.activeCollaborations = new Map();
    }

    handleSSEEvent(event) {
        const { type, eventMessage } = event;
        const { collaborationId, collaboratingAssistants } = eventMessage;

        switch (type) {
            case 'NOTIFICATION_MULTI_ASSISTANT_COLLABORATION_STARTED':
                this.startCollaboration(collaborationId, collaboratingAssistants);
                break;
                
            case 'NOTIFICATION_MULTI_ASSISTANT_COLLABORATION_ASSISTANT_RESPONSE_IN_PROGRESS':
                this.updateAgentStatus(collaborationId, eventMessage.agent.handle, 'responding');
                break;
                
            case 'NOTIFICATION_MULTI_ASSISTANT_COLLABORATION_ASSISTANT_RESPONSE_COMPLETED':
                this.updateAgentStatus(collaborationId, eventMessage.agent.handle, 'completed');
                break;
                
            case 'NOTIFICATION_MULTI_ASSISTANT_COLLABORATION_COMPLETED':
                this.completeCollaboration(collaborationId);
                break;
                
            case 'NOTIFICATION_MULTI_ASSISTANT_COLLABORATION_FAILED':
                this.failCollaboration(collaborationId, eventMessage.content);
                break;
        }
    }

    startCollaboration(collaborationId, agents) {
        const collaboration = {
            id: collaborationId,
            agents: new Map(agents.map(agent => [agent.handle, {
                ...agent,
                status: 'pending'
            }])),
            phase: 'started',
            startTime: new Date().toISOString()
        };
        
        this.activeCollaborations.set(collaborationId, collaboration);
        this.notifyUI('collaboration_started', collaboration);
    }

    updateAgentStatus(collaborationId, agentHandle, status) {
        const collaboration = this.activeCollaborations.get(collaborationId);
        if (collaboration && collaboration.agents.has(agentHandle)) {
            collaboration.agents.get(agentHandle).status = status;
            this.notifyUI('agent_status_updated', { collaborationId, agentHandle, status });
        }
    }

    completeCollaboration(collaborationId) {
        const collaboration = this.activeCollaborations.get(collaborationId);
        if (collaboration) {
            collaboration.phase = 'completed';
            collaboration.endTime = new Date().toISOString();
            this.notifyUI('collaboration_completed', collaboration);
            this.activeCollaborations.delete(collaborationId);
        }
    }

    failCollaboration(collaborationId, error) {
        const collaboration = this.activeCollaborations.get(collaborationId);
        if (collaboration) {
            collaboration.phase = 'failed';
            collaboration.error = error;
            collaboration.endTime = new Date().toISOString();
            this.notifyUI('collaboration_failed', collaboration);
            this.activeCollaborations.delete(collaborationId);
        }
    }

    notifyUI(eventType, data) {
        // Emit events to update your UI
        document.dispatchEvent(new CustomEvent(eventType, { detail: data }));
    }
}
I