/** * Claude Flow + Claude Code - Browser IDE * Combines swarm orchestration with interactive code editing */ class ClaudeCodeDashboard { constructor() { this.ws = null; this.editor = null; this.agents = new Map(); this.consensus = { votesFor: 0, votesAgainst: 0, total: 20, threshold: 13 }; this.currentPanel = 'terminal'; this.initialize(); } async initialize() { await this.setupMonacoEditor(); this.setupCanvas(); this.startSimulation(); this.addLog('Dashboard initialized', 'info'); // Auto-connect setTimeout(() => { const wsUrl = `ws://${window.location.hostname}:${window.location.port || 8080}`; this.addLog(`Connecting to ${wsUrl}...`, 'info'); this.connectWebSocket(wsUrl); }, 1000); } /** * Monaco Editor Setup */ async setupMonacoEditor() { return new Promise((resolve) => { require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.45.0/min/vs' } }); require(['vs/editor/editor.main'], () => { this.editor = monaco.editor.create(document.getElementById('editor'), { value: this.getDefaultCode(), language: 'javascript', theme: 'vs-dark', automaticLayout: true, fontSize: 13, minimap: { enabled: true }, scrollBeyondLastLine: false, renderWhitespace: 'selection', lineNumbers: 'on', folding: true, bracketPairColorization: { enabled: true } }); this.addLog('Monaco Editor loaded', 'success'); resolve(); }); }); } getDefaultCode() { return `// Claude Flow + Claude Code - Browser IDE // Write your code here and click "Run Code" to execute via MCP console.log('Hello from Claude Flow!'); // Example: Spawn agents via MCP async function spawnResearchAgent() { const result = await sendMCPCommand('agent_spawn', { type: 'researcher', name: 'Browser-Researcher-1', capabilities: ['web-search', 'data-analysis'] }); console.log('Agent spawned:', result); return result; } // Example: Run consensus verification async function verifyConsensus() { const result = await sendMCPCommand('verify_consensus', { agent_public_keys: Array(20).fill('pk_' + Date.now()), consensus_threshold: 0.65 }); console.log('Consensus result:', result); return result; } // Test the functions spawnResearchAgent(); `; } /** * WebSocket Connection */ connectWebSocket(url = 'ws://localhost:8080') { try { this.ws = new WebSocket(url); this.ws.onopen = () => { this.updateConnectionStatus(true); this.addLog('Connected to Claude Flow MCP server', 'success'); this.sendCommand('swarm_status'); }; this.ws.onmessage = (event) => { const data = JSON.parse(event.data); this.handleMessage(data); this.addMCPLog('RECV', data); }; this.ws.onerror = (error) => { this.addLog(`WebSocket error: ${error.message}`, 'error'); this.updateConnectionStatus(false); }; this.ws.onclose = () => { this.addLog('Disconnected from server', 'warning'); this.updateConnectionStatus(false); }; } catch (error) { this.addLog(`Connection failed: ${error.message}`, 'error'); this.updateConnectionStatus(false); } } sendCommand(command, params = {}) { if (this.ws && this.ws.readyState === WebSocket.OPEN) { const message = { jsonrpc: '2.0', method: `mcp__claude-flow__${command}`, params, id: Date.now() }; this.ws.send(JSON.stringify(message)); this.addMCPLog('SEND', message); } else { this.addLog('Not connected to server', 'warning'); } } handleMessage(data) { switch (data.type) { case 'agent_update': this.updateAgent(data.agent); break; case 'consensus_update': this.updateConsensus(data.consensus); break; case 'execution_result': this.displayExecutionResult(data.result); break; case 'swarm_status': this.updateSwarmStatus(data.status); break; } } /** * Code Execution */ executeCode() { const code = this.editor.getValue(); this.addTerminalOutput('> Executing code via Claude Flow MCP...\n', 'command'); // Send code to MCP server for execution this.sendCommand('execute_code', { code, language: 'javascript', timeout: 5000 }); this.addLog('Code execution requested', 'info'); } displayExecutionResult(result) { const output = document.getElementById('outputPanel'); if (result.success) { output.innerHTML += `
${JSON.stringify(message, null, 2)}
`;
panel.appendChild(entry);
panel.scrollTop = panel.scrollHeight;
}
updateConnectionStatus(connected) {
const dot = document.getElementById('statusDot');
const label = document.getElementById('statusLabel');
if (connected) {
dot.classList.add('connected');
label.textContent = 'Connected';
} else {
dot.classList.remove('connected');
label.textContent = 'Disconnected';
}
}
/**
* Simulation
*/
startSimulation() {
const types = ['researcher', 'coder', 'reviewer', 'analyst', 'optimizer'];
for (let i = 0; i < 5; i++) {
this.updateAgent({
id: `agent-${i}`,
name: `Agent-${i}`,
type: types[i % types.length],
status: i < 3 ? 'active' : 'idle'
});
}
}
}
/**
* Global Functions
*/
function spawnAgents() {
dashboard.addLog('Spawning 5 new agents...', 'info');
dashboard.sendCommand('agents_spawn_parallel', {
agents: [
{ type: 'researcher', name: 'Researcher-1', priority: 'high' },
{ type: 'coder', name: 'Coder-1', priority: 'medium' },
{ type: 'reviewer', name: 'Reviewer-1', priority: 'high' },
{ type: 'tester', name: 'Tester-1', priority: 'medium' },
{ type: 'analyst', name: 'Analyst-1', priority: 'low' }
],
maxConcurrency: 5
});
}
function testConsensus() {
dashboard.addLog('Testing Byzantine consensus...', 'info');
let votes = 0;
const interval = setInterval(() => {
votes += Math.floor(Math.random() * 3) + 1;
if (votes > 20) votes = 20;
dashboard.updateConsensus({
votesFor: votes,
votesAgainst: 20 - votes,
total: 20,
threshold: 13
});
if (votes >= 20) {
clearInterval(interval);
dashboard.addLog('Consensus reached: 20/20 votes in 1ms', 'success');
}
}, 150);
}
function executeCode() {
dashboard.executeCode();
}
function saveFile() {
const code = dashboard.editor.getValue();
const blob = new Blob([code], { type: 'text/javascript' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'code.js';
a.click();
dashboard.addLog('File saved', 'success');
}
function loadFile() {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.js,.ts,.json,.md';
input.onchange = (e) => {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = (event) => {
dashboard.editor.setValue(event.target.result);
dashboard.addLog(`Loaded ${file.name}`, 'success');
};
reader.readAsText(file);
};
input.click();
}
function clearOutput() {
document.getElementById('terminalPanel').innerHTML = '';
document.getElementById('outputPanel').innerHTML = '';
dashboard.addLog('Output cleared', 'info');
}
function switchPanel(panel) {
dashboard.switchPanel(panel);
}
// Initialize
const dashboard = new ClaudeCodeDashboard();