import logging from typing import Dict, Any, List from modules.interfaces.interfaceAppModel import User from modules.interfaces.interfaceChatModel import ChatWorkflow, UserInputRequest, TaskStep, TaskAction, ActionExecutionResult, ReviewResult, TaskPlan, WorkflowResult, TaskContext from modules.chat.serviceCenter import ServiceCenter from modules.interfaces.interfaceChatObjects import ChatObjects from .handling.handlingTasks import HandlingTasks logger = logging.getLogger(__name__) # ===== STATE MANAGEMENT AND VALIDATION CLASSES ===== class ChatManager: """Chat manager with improved AI integration and method handling""" def __init__(self, currentUser: User, chatInterface: ChatObjects): self.currentUser = currentUser self.chatInterface = chatInterface self.service: ServiceCenter = None self.workflow: ChatWorkflow = None self.handlingTasks: HandlingTasks = None async def initialize(self, workflow: ChatWorkflow) -> None: """Initialize chat manager with workflow""" self.workflow = workflow self.service = ServiceCenter(self.currentUser, self.workflow) self.handlingTasks = HandlingTasks(self.chatInterface, self.service, self.workflow) async def executeUnifiedWorkflow(self, userInput: UserInputRequest, workflow: ChatWorkflow) -> WorkflowResult: """Unified Workflow Execution""" try: logger.info(f"Starting unified workflow execution for workflow {workflow.id}") # Phase 1: High-Level Task Planning task_plan = await self.handlingTasks.planHighLevelTasks(userInput.userRequest, workflow) if not task_plan or not task_plan.tasks: raise Exception("No tasks generated in task plan.") workflow.taskPlan = task_plan # Phase 2-5: For each task, define actions, execute, review, and handover all_task_results = [] for idx, task_step in enumerate(task_plan.tasks): logger.info(f"Processing task {idx+1}/{len(task_plan.tasks)}: {task_step.description}") # Define actions previous_results = self.handlingTasks.getPreviousResults(task_step) if hasattr(self.handlingTasks, 'getPreviousResults') else [] actions = await self.handlingTasks.generateTaskActions(task_step, workflow, previous_results=previous_results) if not actions: logger.warning(f"No actions defined for task {task_step.id}, skipping.") continue # Execute actions and get results (including review_result) task_result = await self.handlingTasks.executeTaskActions(actions, workflow) # task_result should include action_results and review_result action_results = getattr(task_result, 'action_results', None) review_result = getattr(task_result, 'review_result', None) # Handover handover_data = await self.handlingTasks.prepareTaskHandover(task_step, actions, review_result, workflow) # Collect results all_task_results.append({ 'task_step': task_step, 'actions': actions, 'action_results': action_results, 'review_result': review_result, 'handover_data': handover_data }) # Final workflow result workflow_result = WorkflowResult( status="completed", task_results=all_task_results, workflow=workflow ) logger.info(f"Unified workflow execution completed for workflow {workflow.id}") return workflow_result except Exception as e: logger.error(f"Error in executeUnifiedWorkflow: {str(e)}") from modules.interfaces.interfaceChatModel import WorkflowResult return WorkflowResult(status="failed", task_results=[], workflow=workflow)