UNPKG

claude-flow-multilang

Version:

Revolutionary multilingual AI orchestration framework with cultural awareness and DDD architecture

985 lines (852 loc) 29.3 kB
// SPARC Pseudocode Phase // Generate pseudocode and flow diagrams based on specifications import { SparcPhase } from './phase-base.js'; export class SparcPseudocode extends SparcPhase { constructor(taskDescription, options) { super('pseudocode', taskDescription, options); this.flowDiagram = null; this.pseudocode = []; this.algorithms = []; this.dataStructures = []; this.interfaces = []; } /** * Execute pseudocode phase */ async execute() { console.log('🔄 Starting Pseudocode Phase'); await this.initializePhase(); const result = { flowDiagram: null, pseudocode: [], algorithms: [], dataStructures: [], interfaces: [], logicFlow: [], edgeCases: [], complexityAnalysis: {}, dependencies: [], }; try { // Load specification from previous phase const specification = await this.retrieveFromMemory('specification_complete'); if (!specification) { throw new Error('Specification phase must be completed first'); } // Generate flow diagram result.flowDiagram = await this.generateFlowDiagram(specification); // Generate pseudocode result.pseudocode = await this.generatePseudocode(specification); // Define algorithms result.algorithms = await this.defineAlgorithms(specification); // Define data structures result.dataStructures = await this.defineDataStructures(specification); // Define interfaces result.interfaces = await this.defineInterfaces(specification); // Map logic flow result.logicFlow = await this.mapLogicFlow(specification); // Identify edge cases result.edgeCases = await this.identifyEdgeCases(specification); // Analyze complexity result.complexityAnalysis = await this.analyzeComplexity(result.pseudocode); // Identify dependencies result.dependencies = await this.identifyDependencies(specification); // Generate pseudocode document await this.generatePseudocodeDocument(result); // Store in memory await this.storeInMemory('pseudocode_complete', result); console.log('✅ Pseudocode phase completed'); return result; } catch (error) { console.error('❌ Pseudocode phase failed:', error.message); throw error; } } /** * Generate flow diagram */ async generateFlowDiagram(specification) { const flowDiagram = { title: `Flow Diagram: ${this.taskDescription}`, nodes: [], edges: [], entry: 'start', exit: 'end', type: 'sequential', }; // Determine flow type based on requirements const requirements = specification.requirements || []; const hasApiRequirements = requirements.some((req) => req.toLowerCase().includes('api')); const hasUiRequirements = requirements.some( (req) => req.toLowerCase().includes('ui') || req.toLowerCase().includes('interface'), ); const hasDataRequirements = requirements.some((req) => req.toLowerCase().includes('data')); if (hasApiRequirements) { flowDiagram.type = 'api'; flowDiagram.nodes = this.generateApiFlowNodes(requirements); } else if (hasUiRequirements) { flowDiagram.type = 'ui'; flowDiagram.nodes = this.generateUiFlowNodes(requirements); } else if (hasDataRequirements) { flowDiagram.type = 'data'; flowDiagram.nodes = this.generateDataFlowNodes(requirements); } else { flowDiagram.type = 'general'; flowDiagram.nodes = this.generateGeneralFlowNodes(requirements); } // Generate edges between nodes flowDiagram.edges = this.generateFlowEdges(flowDiagram.nodes); return flowDiagram; } /** * Generate API flow nodes */ generateApiFlowNodes(requirements) { return [ { id: 'start', type: 'start', label: 'Start' }, { id: 'validate_input', type: 'process', label: 'Validate Input' }, { id: 'authenticate', type: 'process', label: 'Authenticate Request' }, { id: 'authorize', type: 'process', label: 'Authorize Access' }, { id: 'process_request', type: 'process', label: 'Process Request' }, { id: 'validate_data', type: 'decision', label: 'Data Valid?' }, { id: 'handle_error', type: 'process', label: 'Handle Error' }, { id: 'execute_logic', type: 'process', label: 'Execute Business Logic' }, { id: 'prepare_response', type: 'process', label: 'Prepare Response' }, { id: 'log_activity', type: 'process', label: 'Log Activity' }, { id: 'return_response', type: 'process', label: 'Return Response' }, { id: 'end', type: 'end', label: 'End' }, ]; } /** * Generate UI flow nodes */ generateUiFlowNodes(requirements) { return [ { id: 'start', type: 'start', label: 'Start' }, { id: 'load_interface', type: 'process', label: 'Load Interface' }, { id: 'initialize_state', type: 'process', label: 'Initialize State' }, { id: 'render_components', type: 'process', label: 'Render Components' }, { id: 'wait_user_input', type: 'input', label: 'Wait for User Input' }, { id: 'validate_input', type: 'process', label: 'Validate Input' }, { id: 'input_valid', type: 'decision', label: 'Input Valid?' }, { id: 'show_error', type: 'process', label: 'Show Error Message' }, { id: 'update_state', type: 'process', label: 'Update State' }, { id: 'make_api_call', type: 'process', label: 'Make API Call' }, { id: 'handle_response', type: 'process', label: 'Handle Response' }, { id: 'update_ui', type: 'process', label: 'Update UI' }, { id: 'end', type: 'end', label: 'End' }, ]; } /** * Generate data flow nodes */ generateDataFlowNodes(requirements) { return [ { id: 'start', type: 'start', label: 'Start' }, { id: 'connect_database', type: 'process', label: 'Connect to Database' }, { id: 'validate_schema', type: 'process', label: 'Validate Schema' }, { id: 'begin_transaction', type: 'process', label: 'Begin Transaction' }, { id: 'execute_query', type: 'process', label: 'Execute Query' }, { id: 'check_results', type: 'decision', label: 'Results Valid?' }, { id: 'rollback_transaction', type: 'process', label: 'Rollback Transaction' }, { id: 'transform_data', type: 'process', label: 'Transform Data' }, { id: 'validate_constraints', type: 'process', label: 'Validate Constraints' }, { id: 'commit_transaction', type: 'process', label: 'Commit Transaction' }, { id: 'close_connection', type: 'process', label: 'Close Connection' }, { id: 'end', type: 'end', label: 'End' }, ]; } /** * Generate general flow nodes */ generateGeneralFlowNodes(requirements) { return [ { id: 'start', type: 'start', label: 'Start' }, { id: 'initialize', type: 'process', label: 'Initialize System' }, { id: 'load_configuration', type: 'process', label: 'Load Configuration' }, { id: 'validate_prerequisites', type: 'process', label: 'Validate Prerequisites' }, { id: 'execute_main_logic', type: 'process', label: 'Execute Main Logic' }, { id: 'handle_exceptions', type: 'process', label: 'Handle Exceptions' }, { id: 'cleanup_resources', type: 'process', label: 'Cleanup Resources' }, { id: 'end', type: 'end', label: 'End' }, ]; } /** * Generate flow edges */ generateFlowEdges(nodes) { const edges = []; for (let i = 0; i < nodes.length - 1; i++) { const current = nodes[i]; const next = nodes[i + 1]; if (current.type === 'decision') { // Decision nodes have multiple paths edges.push({ from: current.id, to: next.id, label: 'Yes', condition: true, }); // Find alternative path (usually error handling) const errorNode = nodes.find((n) => n.id.includes('error') || n.id.includes('handle')); if (errorNode) { edges.push({ from: current.id, to: errorNode.id, label: 'No', condition: false, }); } } else { edges.push({ from: current.id, to: next.id, label: '', }); } } return edges; } /** * Generate pseudocode */ async generatePseudocode(specification) { const pseudocode = []; const requirements = specification.requirements || []; // Main function pseudocode.push({ function: 'main', description: 'Main entry point for the system', steps: [ 'BEGIN main', ' INITIALIZE system_configuration', ' VALIDATE prerequisites', ' CALL execute_primary_logic()', ' HANDLE exceptions', ' CLEANUP resources', ' RETURN success_status', 'END main', ], }); // Generate pseudocode for each requirement for (const [index, requirement] of requirements.entries()) { const functionName = this.generateFunctionName(requirement); const steps = this.generatePseudocodeSteps(requirement); pseudocode.push({ function: functionName, description: requirement, steps: steps, complexity: this.estimateComplexity(steps), }); } return pseudocode; } /** * Generate function name from requirement */ generateFunctionName(requirement) { const words = requirement.toLowerCase().split(' '); const actionWords = words.filter((word) => [ 'provide', 'handle', 'ensure', 'validate', 'process', 'manage', 'create', 'update', 'delete', ].includes(word), ); const objectWords = words.filter((word) => [ 'api', 'data', 'user', 'system', 'request', 'response', 'authentication', 'authorization', ].includes(word), ); const action = actionWords[0] || 'execute'; const object = objectWords[0] || 'operation'; return `${action}_${object}`; } /** * Generate pseudocode steps */ generatePseudocodeSteps(requirement) { const steps = []; const reqLower = requirement.toLowerCase(); // Common patterns steps.push(`BEGIN ${this.generateFunctionName(requirement)}`); if (reqLower.includes('validate')) { steps.push(' VALIDATE input_parameters'); steps.push(' IF validation_fails THEN'); steps.push(' RETURN error_response'); steps.push(' END IF'); } if (reqLower.includes('authenticate')) { steps.push(' AUTHENTICATE user_credentials'); steps.push(' IF authentication_fails THEN'); steps.push(' RETURN unauthorized_error'); steps.push(' END IF'); } if (reqLower.includes('data')) { steps.push(' CONNECT to_database'); steps.push(' BEGIN transaction'); steps.push(' EXECUTE data_operation'); steps.push(' IF operation_successful THEN'); steps.push(' COMMIT transaction'); steps.push(' ELSE'); steps.push(' ROLLBACK transaction'); steps.push(' END IF'); } if (reqLower.includes('api')) { steps.push(' PARSE request_parameters'); steps.push(' VALIDATE request_format'); steps.push(' EXECUTE business_logic'); steps.push(' FORMAT response_data'); steps.push(' RETURN api_response'); } // Generic steps if no specific patterns found if (steps.length === 1) { steps.push(' INITIALIZE operation_context'); steps.push(' EXECUTE primary_operation'); steps.push(' HANDLE potential_errors'); steps.push(' RETURN operation_result'); } steps.push(`END ${this.generateFunctionName(requirement)}`); return steps; } /** * Estimate complexity */ estimateComplexity(steps) { const decisionSteps = steps.filter( (step) => step.includes('IF') || step.includes('WHILE') || step.includes('FOR'), ); const operationSteps = steps.filter( (step) => step.includes('EXECUTE') || step.includes('CALL'), ); return { cyclomatic: decisionSteps.length + 1, operations: operationSteps.length, lines: steps.length, level: decisionSteps.length > 5 ? 'high' : decisionSteps.length > 2 ? 'medium' : 'low', }; } /** * Define algorithms */ async defineAlgorithms(specification) { const algorithms = []; const requirements = specification.requirements || []; // Search for algorithmic requirements for (const requirement of requirements) { const reqLower = requirement.toLowerCase(); if ( reqLower.includes('sort') || reqLower.includes('search') || reqLower.includes('optimize') ) { algorithms.push({ name: 'Search and Sort Algorithm', purpose: 'Efficient data retrieval and ordering', complexity: 'O(n log n)', approach: 'Quick sort with binary search optimization', }); } if (reqLower.includes('cache') || reqLower.includes('performance')) { algorithms.push({ name: 'Caching Algorithm', purpose: 'Improve response time through intelligent caching', complexity: 'O(1) average case', approach: 'LRU cache with time-based expiration', }); } if (reqLower.includes('validate') || reqLower.includes('check')) { algorithms.push({ name: 'Validation Algorithm', purpose: 'Ensure data integrity and format compliance', complexity: 'O(n)', approach: 'Rule-based validation with early termination', }); } } // Default algorithms for common patterns if (algorithms.length === 0) { algorithms.push({ name: 'Sequential Processing Algorithm', purpose: 'Process operations in defined sequence', complexity: 'O(n)', approach: 'Linear processing with error handling', }); } return algorithms; } /** * Define data structures */ async defineDataStructures(specification) { const dataStructures = []; const requirements = specification.requirements || []; // Analyze requirements for data structure needs for (const requirement of requirements) { const reqLower = requirement.toLowerCase(); if ( reqLower.includes('list') || reqLower.includes('array') || reqLower.includes('collection') ) { dataStructures.push({ name: 'Dynamic Array', purpose: 'Store variable-length collections', operations: ['add', 'remove', 'search', 'iterate'], complexity: { access: 'O(1)', insertion: 'O(n)', deletion: 'O(n)' }, }); } if (reqLower.includes('map') || reqLower.includes('dictionary') || reqLower.includes('key')) { dataStructures.push({ name: 'Hash Map', purpose: 'Key-value pair storage with fast lookup', operations: ['put', 'get', 'remove', 'contains'], complexity: { access: 'O(1)', insertion: 'O(1)', deletion: 'O(1)' }, }); } if (reqLower.includes('queue') || reqLower.includes('fifo') || reqLower.includes('buffer')) { dataStructures.push({ name: 'Queue', purpose: 'First-in-first-out data processing', operations: ['enqueue', 'dequeue', 'peek', 'isEmpty'], complexity: { access: 'O(n)', insertion: 'O(1)', deletion: 'O(1)' }, }); } if (reqLower.includes('stack') || reqLower.includes('lifo') || reqLower.includes('undo')) { dataStructures.push({ name: 'Stack', purpose: 'Last-in-first-out operations', operations: ['push', 'pop', 'peek', 'isEmpty'], complexity: { access: 'O(n)', insertion: 'O(1)', deletion: 'O(1)' }, }); } } // Default data structures if (dataStructures.length === 0) { dataStructures.push({ name: 'Object', purpose: 'Basic data encapsulation', operations: ['create', 'read', 'update', 'delete'], complexity: { access: 'O(1)', insertion: 'O(1)', deletion: 'O(1)' }, }); } return dataStructures; } /** * Define interfaces */ async defineInterfaces(specification) { const interfaces = []; const requirements = specification.requirements || []; // API interfaces if (requirements.some((req) => req.toLowerCase().includes('api'))) { interfaces.push({ name: 'APIInterface', type: 'REST API', methods: [ { name: 'GET', purpose: 'Retrieve data', parameters: ['id', 'filters'], returns: 'data object', }, { name: 'POST', purpose: 'Create new resource', parameters: ['data'], returns: 'created resource', }, { name: 'PUT', purpose: 'Update existing resource', parameters: ['id', 'data'], returns: 'updated resource', }, { name: 'DELETE', purpose: 'Remove resource', parameters: ['id'], returns: 'success status', }, ], }); } // Database interfaces if (requirements.some((req) => req.toLowerCase().includes('data'))) { interfaces.push({ name: 'DatabaseInterface', type: 'Data Access Layer', methods: [ { name: 'connect', purpose: 'Establish connection', parameters: ['config'], returns: 'connection object', }, { name: 'query', purpose: 'Execute query', parameters: ['sql', 'params'], returns: 'result set', }, { name: 'transaction', purpose: 'Execute transaction', parameters: ['operations'], returns: 'transaction result', }, { name: 'disconnect', purpose: 'Close connection', parameters: [], returns: 'void' }, ], }); } // Service interfaces interfaces.push({ name: 'ServiceInterface', type: 'Business Logic Layer', methods: [ { name: 'initialize', purpose: 'Initialize service', parameters: ['config'], returns: 'service instance', }, { name: 'execute', purpose: 'Execute main operation', parameters: ['request'], returns: 'response', }, { name: 'validate', purpose: 'Validate input', parameters: ['data'], returns: 'validation result', }, { name: 'cleanup', purpose: 'Clean up resources', parameters: [], returns: 'void' }, ], }); return interfaces; } /** * Map logic flow */ async mapLogicFlow(specification) { const logicFlow = []; const requirements = specification.requirements || []; // Main flow logicFlow.push({ step: 1, name: 'Initialization', description: 'System startup and configuration', inputs: ['configuration', 'environment variables'], outputs: ['initialized system'], conditions: ['valid configuration', 'available resources'], }); // Process each requirement as a flow step for (const [index, requirement] of requirements.entries()) { logicFlow.push({ step: index + 2, name: this.generateFunctionName(requirement), description: requirement, inputs: this.identifyInputs(requirement), outputs: this.identifyOutputs(requirement), conditions: this.identifyConditions(requirement), }); } // Final step logicFlow.push({ step: requirements.length + 2, name: 'Finalization', description: 'Cleanup and result reporting', inputs: ['execution results'], outputs: ['final status', 'cleanup confirmation'], conditions: ['all operations completed'], }); return logicFlow; } /** * Identify inputs for requirement */ identifyInputs(requirement) { const inputs = []; const reqLower = requirement.toLowerCase(); if (reqLower.includes('api')) inputs.push('HTTP request', 'request parameters'); if (reqLower.includes('data')) inputs.push('data payload', 'database connection'); if (reqLower.includes('user')) inputs.push('user credentials', 'user input'); if (reqLower.includes('validate')) inputs.push('validation rules', 'input data'); if (reqLower.includes('authenticate')) inputs.push('authentication credentials'); return inputs.length > 0 ? inputs : ['system input']; } /** * Identify outputs for requirement */ identifyOutputs(requirement) { const outputs = []; const reqLower = requirement.toLowerCase(); if (reqLower.includes('api')) outputs.push('HTTP response', 'status code'); if (reqLower.includes('data')) outputs.push('processed data', 'transaction result'); if (reqLower.includes('validate')) outputs.push('validation result', 'error messages'); if (reqLower.includes('authenticate')) outputs.push('authentication token', 'user session'); return outputs.length > 0 ? outputs : ['operation result']; } /** * Identify conditions for requirement */ identifyConditions(requirement) { const conditions = []; const reqLower = requirement.toLowerCase(); if (reqLower.includes('validate')) conditions.push('data is valid', 'rules are satisfied'); if (reqLower.includes('authenticate')) conditions.push('credentials are valid', 'user exists'); if (reqLower.includes('data')) conditions.push('database is available', 'data integrity maintained'); if (reqLower.includes('api')) conditions.push('request format is valid', 'service is available'); return conditions.length > 0 ? conditions : ['preconditions met']; } /** * Identify edge cases */ async identifyEdgeCases(specification) { const edgeCases = []; const requirements = specification.requirements || []; // Common edge cases edgeCases.push({ case: 'Empty or null input', description: 'System receives no input or null values', handling: 'Validate inputs and return appropriate error messages', severity: 'high', }); edgeCases.push({ case: 'Network connectivity issues', description: 'External services are unavailable', handling: 'Implement retry logic with exponential backoff', severity: 'medium', }); edgeCases.push({ case: 'Concurrent access', description: 'Multiple users access same resources simultaneously', handling: 'Implement proper locking and transaction management', severity: 'high', }); edgeCases.push({ case: 'Resource exhaustion', description: 'System runs out of memory or disk space', handling: 'Monitor resources and implement graceful degradation', severity: 'high', }); // Requirement-specific edge cases for (const requirement of requirements) { const reqLower = requirement.toLowerCase(); if (reqLower.includes('api')) { edgeCases.push({ case: 'API rate limiting', description: 'Too many requests from single client', handling: 'Implement rate limiting and throttling', severity: 'medium', }); } if (reqLower.includes('data')) { edgeCases.push({ case: 'Data corruption', description: 'Database contains invalid or corrupted data', handling: 'Implement data validation and recovery procedures', severity: 'high', }); } if (reqLower.includes('authenticate')) { edgeCases.push({ case: 'Authentication timeout', description: 'User session expires during operation', handling: 'Implement session refresh and re-authentication', severity: 'medium', }); } } return edgeCases; } /** * Analyze complexity */ async analyzeComplexity(pseudocode) { const analysis = { overall: 'medium', functions: {}, recommendations: [], }; let totalComplexity = 0; let functionCount = 0; for (const func of pseudocode) { if (func.complexity) { analysis.functions[func.function] = func.complexity; totalComplexity += func.complexity.cyclomatic; functionCount++; } } const averageComplexity = functionCount > 0 ? totalComplexity / functionCount : 1; if (averageComplexity > 10) { analysis.overall = 'high'; analysis.recommendations.push('Consider breaking down complex functions'); } else if (averageComplexity > 5) { analysis.overall = 'medium'; analysis.recommendations.push('Monitor function complexity during implementation'); } else { analysis.overall = 'low'; analysis.recommendations.push('Complexity is manageable'); } return analysis; } /** * Identify dependencies */ async identifyDependencies(specification) { const dependencies = []; const requirements = specification.requirements || []; // Analyze requirements for dependencies for (const requirement of requirements) { const reqLower = requirement.toLowerCase(); if (reqLower.includes('api')) { dependencies.push('HTTP client library'); dependencies.push('JSON parsing library'); dependencies.push('Authentication middleware'); } if (reqLower.includes('data')) { dependencies.push('Database driver'); dependencies.push('ORM or query builder'); dependencies.push('Connection pooling'); } if (reqLower.includes('validate')) { dependencies.push('Validation library'); dependencies.push('Schema definition'); } if (reqLower.includes('test')) { dependencies.push('Testing framework'); dependencies.push('Mocking library'); } } // Remove duplicates return [...new Set(dependencies)]; } /** * Generate pseudocode document */ async generatePseudocodeDocument(result) { const document = `# ${this.taskDescription} - Pseudocode ## Flow Diagram **Type**: ${result.flowDiagram.type} **Entry**: ${result.flowDiagram.entry} **Exit**: ${result.flowDiagram.exit} ### Nodes ${result.flowDiagram.nodes.map((node) => `- **${node.id}** (${node.type}): ${node.label}`).join('\n')} ### Edges ${result.flowDiagram.edges.map((edge) => `- ${edge.from} → ${edge.to} ${edge.label ? `(${edge.label})` : ''}`).join('\n')} ## Pseudocode ${result.pseudocode .map( (func, index) => ` ### ${index + 1}. ${func.function} **Description**: ${func.description} **Complexity**: ${func.complexity ? `Cyclomatic: ${func.complexity.cyclomatic}, Lines: ${func.complexity.lines}` : 'N/A'} \`\`\` ${func.steps.join('\n')} \`\`\` `, ) .join('\n')} ## Algorithms ${result.algorithms .map( (algo, index) => ` ### ${index + 1}. ${algo.name} **Purpose**: ${algo.purpose} **Complexity**: ${algo.complexity} **Approach**: ${algo.approach} `, ) .join('\n')} ## Data Structures ${result.dataStructures .map( (ds, index) => ` ### ${index + 1}. ${ds.name} **Purpose**: ${ds.purpose} **Operations**: ${ds.operations.join(', ')} **Complexity**: Access: ${ds.complexity.access}, Insertion: ${ds.complexity.insertion}, Deletion: ${ds.complexity.deletion} `, ) .join('\n')} ## Interfaces ${result.interfaces .map( (iface, index) => ` ### ${index + 1}. ${iface.name} **Type**: ${iface.type} ${iface.methods .map( (method) => ` #### ${method.name} - **Purpose**: ${method.purpose} - **Parameters**: ${method.parameters.join(', ')} - **Returns**: ${method.returns} `, ) .join('\n')} `, ) .join('\n')} ## Logic Flow ${result.logicFlow .map( (step) => ` ### Step ${step.step}: ${step.name} **Description**: ${step.description} **Inputs**: ${step.inputs.join(', ')} **Outputs**: ${step.outputs.join(', ')} **Conditions**: ${step.conditions.join(', ')} `, ) .join('\n')} ## Edge Cases ${result.edgeCases .map( (edge, index) => ` ### ${index + 1}. ${edge.case} **Description**: ${edge.description} **Handling**: ${edge.handling} **Severity**: ${edge.severity} `, ) .join('\n')} ## Complexity Analysis **Overall Complexity**: ${result.complexityAnalysis.overall} ### Function Complexity ${Object.entries(result.complexityAnalysis.functions || {}) .map( ([func, complexity]) => ` - **${func}**: Cyclomatic: ${complexity.cyclomatic}, Lines: ${complexity.lines}, Level: ${complexity.level} `, ) .join('\n')} ### Recommendations ${result.complexityAnalysis.recommendations.map((rec) => `- ${rec}`).join('\n')} ## Dependencies ${result.dependencies.map((dep, index) => `${index + 1}. ${dep}`).join('\n')} `; // Save document await this.saveArtifact('pseudocode.md', document); return document; } } export default SparcPseudocode;