UNPKG

flow-nexus

Version:

šŸš€ AI-Powered Swarm Intelligence Platform - Gamified MCP Development with 70+ Tools

420 lines (368 loc) • 14.9 kB
#!/usr/bin/env node /** * Stream Processing Service for Claude Code and Claude Flow * Handles real-time stream-json parsing and file tracking */ import { db } from './supabase.js'; export class StreamProcessor { constructor() { this.activeStreams = new Map(); } // Process stream-json output and extract meaningful data async processStreamOutput(streamId, output, templateType) { try { const chunks = []; const files = []; // Handle different types of output if (typeof output === 'string') { // Try to parse as stream-json format const lines = output.split('\n').filter(line => line.trim()); for (const line of lines) { try { const parsed = JSON.parse(line); chunks.push(parsed); // Extract files from stream data if (parsed.type === 'code' && parsed.content) { files.push({ file_name: `generated_${Date.now()}.${this.getFileExtension(parsed.language)}`, file_path: `/workspace/generated_${Date.now()}.${this.getFileExtension(parsed.language)}`, file_type: this.getFileExtension(parsed.language), content: parsed.content, language: parsed.language || 'text', created_by: templateType.includes('claude-flow') ? 'claude-flow' : 'claude-code', metadata: { stream_type: parsed.type, timestamp: parsed.timestamp, template_type: templateType } }); } } catch (parseError) { // If not JSON, treat as regular output chunks.push({ type: 'output', content: line, timestamp: new Date().toISOString() }); } } } else if (typeof output === 'object') { chunks.push(output); // Handle object output for file extraction if (output.content && output.language) { files.push({ file_name: `generated_${Date.now()}.${this.getFileExtension(output.language)}`, file_path: `/workspace/generated_${Date.now()}.${this.getFileExtension(output.language)}`, file_type: this.getFileExtension(output.language), content: output.content, language: output.language, created_by: templateType.includes('claude-flow') ? 'claude-flow' : 'claude-code', metadata: output.metadata || {} }); } } // Store chunks in database for (const chunk of chunks) { await db.client.rpc('update_stream_progress', { p_stream_id: streamId, p_chunk: chunk }); } // Store files in database for (const file of files) { await db.client.rpc('add_execution_file', { p_stream_id: streamId, p_file_path: file.file_path, p_file_name: file.file_name, p_file_type: file.file_type, p_content: file.content, p_language: file.language, p_created_by: file.created_by, p_metadata: file.metadata }); } return { chunks, files }; } catch (error) { console.error('Stream processing error:', error); throw error; } } // Simulate real-time stream processing for different template types async simulateStreamExecution(streamId, templateType, command, variables) { const streamPhases = this.getStreamPhases(templateType); for (let i = 0; i < streamPhases.length; i++) { const phase = streamPhases[i]; const progress = Math.round((i + 1) / streamPhases.length * 100); // Update stream progress await db.client.rpc('update_stream_progress', { p_stream_id: streamId, p_status: 'streaming', p_phase: phase.name, p_progress: progress, p_chunk: { type: phase.type, phase: phase.name, content: phase.content.replace('{objective}', variables.objective || variables.prompt || 'task'), timestamp: new Date().toISOString() } }); // Generate files for specific phases if (phase.generates_files) { await this.generatePhaseFiles(streamId, phase, variables, templateType); } // Simulate processing time await new Promise(resolve => setTimeout(resolve, 1000)); } // Mark as completed await db.client.rpc('update_stream_progress', { p_stream_id: streamId, p_status: 'completed', p_phase: 'finished', p_progress: 100 }); } // Get file extension based on language getFileExtension(language) { const extensions = { 'javascript': 'js', 'typescript': 'ts', 'python': 'py', 'java': 'java', 'csharp': 'cs', 'go': 'go', 'rust': 'rs', 'cpp': 'cpp', 'c': 'c', 'php': 'php', 'ruby': 'rb', 'html': 'html', 'css': 'css', 'sql': 'sql', 'json': 'json', 'yaml': 'yml', 'markdown': 'md', 'bash': 'sh', 'dockerfile': 'dockerfile' }; return extensions[language?.toLowerCase()] || 'txt'; } // Get stream phases for different template types getStreamPhases(templateType) { const phases = { 'claude-code': [ { name: 'analyzing_request', type: 'thinking', content: 'Analyzing code generation request...', generates_files: false }, { name: 'planning_structure', type: 'planning', content: 'Planning code structure and approach...', generates_files: false }, { name: 'generating_code', type: 'code', content: 'Generating code for: {objective}', generates_files: true }, { name: 'optimizing', type: 'optimization', content: 'Optimizing and refining generated code...', generates_files: false }, { name: 'finalizing', type: 'completion', content: 'Code generation complete', generates_files: false } ], 'claude-flow-swarm': [ { name: 'initializing_swarm', type: 'swarm', content: 'Initializing Claude Flow swarm...', generates_files: false }, { name: 'spawning_agents', type: 'coordination', content: 'Spawning specialized agents...', generates_files: false }, { name: 'coordinating_agents', type: 'coordination', content: 'Coordinating multi-agent execution...', generates_files: false }, { name: 'executing_objective', type: 'execution', content: 'Agents executing: {objective}', generates_files: true }, { name: 'synthesizing_results', type: 'synthesis', content: 'Synthesizing agent results...', generates_files: true }, { name: 'swarm_complete', type: 'completion', content: 'Swarm execution completed', generates_files: false } ], 'claude-flow-hive-mind': [ { name: 'activating_hive', type: 'hive', content: 'Activating Claude Flow Hive Mind...', generates_files: false }, { name: 'queen_coordination', type: 'coordination', content: 'Queen coordinator establishing control...', generates_files: false }, { name: 'memory_allocation', type: 'memory', content: 'Allocating collective memory...', generates_files: false }, { name: 'worker_deployment', type: 'deployment', content: 'Deploying specialized workers...', generates_files: false }, { name: 'collective_execution', type: 'execution', content: 'Collective intelligence processing: {objective}', generates_files: true }, { name: 'consensus_building', type: 'consensus', content: 'Building consensus and synthesizing...', generates_files: true }, { name: 'hive_complete', type: 'completion', content: 'Hive Mind execution completed', generates_files: false } ], 'github-integration': [ { name: 'authenticating', type: 'auth', content: 'Authenticating with GitHub...', generates_files: false }, { name: 'cloning_repository', type: 'git', content: 'Cloning repository...', generates_files: false }, { name: 'initializing_claude_flow', type: 'initialization', content: 'Initializing Claude Flow GitHub integration...', generates_files: true }, { name: 'analyzing_repository', type: 'analysis', content: 'Analyzing repository structure...', generates_files: true }, { name: 'setup_workflows', type: 'workflows', content: 'Setting up automation workflows...', generates_files: true }, { name: 'integration_complete', type: 'completion', content: 'GitHub integration completed', generates_files: false } ] }; return phases[templateType] || phases['claude-code']; } // Generate files for specific phases async generatePhaseFiles(streamId, phase, variables, templateType) { const files = []; if (phase.name === 'generating_code' || phase.name === 'executing_objective' || phase.name === 'collective_execution') { // Generate main code file files.push({ file_name: 'main.js', file_path: '/workspace/main.js', file_type: 'js', content: this.generateMainCode(variables), language: 'javascript', created_by: templateType.includes('claude-flow') ? 'claude-flow' : 'claude-code' }); // Generate package.json files.push({ file_name: 'package.json', file_path: '/workspace/package.json', file_type: 'json', content: this.generatePackageJson(variables), language: 'json', created_by: templateType.includes('claude-flow') ? 'claude-flow' : 'claude-code' }); } if (phase.name === 'synthesizing_results' || phase.name === 'consensus_building') { // Generate analysis report files.push({ file_name: 'analysis_report.md', file_path: '/workspace/analysis_report.md', file_type: 'md', content: this.generateAnalysisReport(variables, templateType), language: 'markdown', created_by: templateType.includes('claude-flow') ? 'claude-flow' : 'claude-code' }); } if (phase.name === 'initializing_claude_flow' || phase.name === 'analyzing_repository') { // Generate Claude Flow config files.push({ file_name: 'claude-flow.config.json', file_path: '/workspace/.claude-flow/config.json', file_type: 'json', content: this.generateClaudeFlowConfig(variables), language: 'json', created_by: 'claude-flow' }); } // Store all generated files for (const file of files) { await db.client.rpc('add_execution_file', { p_stream_id: streamId, p_file_path: file.file_path, p_file_name: file.file_name, p_file_type: file.file_type, p_content: file.content, p_language: file.language, p_created_by: file.created_by, p_metadata: { phase: phase.name, generated_at: new Date().toISOString() } }); } } // Generate main code content generateMainCode(variables) { const prompt = variables.prompt || variables.objective || 'Create a Node.js application'; return `// Generated by Claude Code // Objective: ${prompt} const express = require('express'); const app = express(); const port = process.env.PORT || 3000; // Middleware app.use(express.json()); app.use(express.static('public')); // Health check endpoint app.get('/health', (req, res) => { res.json({ status: 'healthy', timestamp: new Date().toISOString(), version: '1.0.0' }); }); // Main API endpoint app.get('/api', (req, res) => { res.json({ message: 'API is working', objective: '${prompt}', generated_by: 'claude-code' }); }); // Start server app.listen(port, () => { console.log(\`Server running on port \${port}\`); console.log(\`Objective: ${prompt}\`); }); module.exports = app;`; } // Generate package.json content generatePackageJson(variables) { const projectName = variables.project_name || 'claude-generated-app'; return JSON.stringify({ name: projectName, version: '1.0.0', description: `Generated by Claude Code - ${variables.prompt || variables.objective || 'Node.js application'}`, main: 'main.js', scripts: { start: 'node main.js', dev: 'nodemon main.js', test: 'jest' }, dependencies: { express: '^4.18.2', cors: '^2.8.5', helmet: '^7.0.0' }, devDependencies: { nodemon: '^3.0.1', jest: '^29.5.0' }, keywords: ['claude-code', 'generated', 'api'], author: 'Claude Code AI', license: 'MIT' }, null, 2); } // Generate analysis report generateAnalysisReport(variables, templateType) { const objective = variables.objective || variables.prompt || 'Analysis task'; return `# Analysis Report **Generated by**: ${templateType} **Objective**: ${objective} **Date**: ${new Date().toISOString()} ## Executive Summary This report provides a comprehensive analysis of the requested objective using ${templateType} methodology. ## Key Findings 1. **Architecture Analysis**: The system demonstrates scalable design patterns 2. **Performance Metrics**: Optimized for high throughput and low latency 3. **Security Assessment**: Implements industry best practices 4. **Maintainability**: Clean code structure with comprehensive documentation ## Recommendations - Implement continuous integration/deployment pipeline - Add comprehensive test coverage - Monitor performance metrics in production - Regular security audits and updates ## Implementation Status āœ… Core functionality implemented āœ… Error handling added āœ… Documentation generated āœ… Testing framework prepared ## Next Steps 1. Deploy to staging environment 2. Conduct user acceptance testing 3. Performance optimization 4. Production deployment --- *Report generated by ${templateType} on ${new Date().toLocaleDateString()}*`; } // Generate Claude Flow config generateClaudeFlowConfig(variables) { return JSON.stringify({ version: '2.0.0', project: { name: variables.project_name || 'claude-flow-project', description: variables.objective || 'Claude Flow integrated project' }, github: { integration: true, workflows: ['ci-cd', 'code-review', 'deployment'], auto_pr: true, branch_protection: true }, swarm: { max_agents: variables.max_agents || 5, coordination_mode: variables.coordination_mode || 'hierarchical', strategy: variables.strategy || 'development' }, monitoring: { real_time: true, metrics: ['performance', 'coverage', 'security'], alerts: true }, generated_at: new Date().toISOString(), generated_by: 'claude-flow-github-integration' }, null, 2); } } export const streamProcessor = new StreamProcessor();