diff --git a/modules/chat/handling/handlingTasks.py b/modules/chat/handling/handlingTasks.py index 2d7882d5..290fdf0a 100644 --- a/modules/chat/handling/handlingTasks.py +++ b/modules/chat/handling/handlingTasks.py @@ -65,17 +65,6 @@ class HandlingTasks: logger.info(f"User Input: {userInput}") available_docs = self.service.getAvailableDocuments(workflow) - # Set initial workflow context - handle new workflow vs continuation properly - current_round = getattr(workflow, 'currentRound', 0) - if current_round == 0: - # New workflow session, start with round 1 - self.service.setWorkflowContext(round_number=1, task_number=0, action_number=0) - else: - # This should not happen for a new workflow - reset to ensure clean state - logger.warning(f"Workflow has currentRound={current_round} but should be 0 for new workflow. Resetting...") - self.resetWorkflowForNewSession() - self.service.setWorkflowContext(round_number=1, task_number=0, action_number=0) - # Check workflow status before calling AI service self._checkWorkflowStopped() diff --git a/modules/chat/serviceCenter.py b/modules/chat/serviceCenter.py index 5c105bf2..0569ccde 100644 --- a/modules/chat/serviceCenter.py +++ b/modules/chat/serviceCenter.py @@ -1134,7 +1134,7 @@ Please provide a comprehensive summary of this conversation.""" # Persist changes to database if any updates were made if update_data: - self.workflowManager.updateWorkflow(self.workflow.id, update_data) + self.interfaceChat.updateWorkflow(self.workflow.id, update_data) logger.debug(f"Updated workflow context: Round {self.workflow.currentRound if hasattr(self.workflow, 'currentRound') else 'N/A'}, Task {self.workflow.currentTask if hasattr(self.workflow, 'currentTask') else 'N/A'}, Action {self.workflow.currentAction if hasattr(self.workflow, 'currentAction') else 'N/A'}") except Exception as e: @@ -1192,7 +1192,7 @@ Please provide a comprehensive summary of this conversation.""" # Persist changes to database if update_data: - self.workflowManager.updateWorkflow(self.workflow.id, update_data) + self.interfaceChat.updateWorkflow(self.workflow.id, update_data) except Exception as e: logger.error(f"Error incrementing workflow context: {str(e)}") diff --git a/modules/interfaces/interfaceChatModel.py b/modules/interfaces/interfaceChatModel.py index bc751f4f..ddf68edf 100644 --- a/modules/interfaces/interfaceChatModel.py +++ b/modules/interfaces/interfaceChatModel.py @@ -398,7 +398,7 @@ register_model_labels( ) class ChatStat(BaseModel, ModelMixin): - """Data model for chat statistics""" + """Data model for chat statistics - ONLY statistics, not workflow progress""" id: str = Field(default_factory=lambda: str(uuid.uuid4()), description="Primary key") processingTime: Optional[float] = Field(None, description="Processing time in seconds") tokenCount: Optional[int] = Field(None, description="Number of tokens processed") @@ -406,13 +406,6 @@ class ChatStat(BaseModel, ModelMixin): bytesReceived: Optional[int] = Field(None, description="Number of bytes received") successRate: Optional[float] = Field(None, description="Success rate of operations") errorCount: Optional[int] = Field(None, description="Number of errors encountered") - - # Enhanced workflow progress tracking - currentRound: Optional[int] = Field(None, description="Current round number in workflow") - totalTasks: Optional[int] = Field(None, description="Total number of tasks in current round") - currentTask: Optional[int] = Field(None, description="Current task number within round") - totalActions: Optional[int] = Field(None, description="Total number of actions in current task") - currentAction: Optional[int] = Field(None, description="Current action number within task") # Register labels for ChatStat register_model_labels( @@ -425,12 +418,7 @@ register_model_labels( "bytesSent": {"en": "Bytes Sent", "fr": "Octets envoyés"}, "bytesReceived": {"en": "Bytes Received", "fr": "Octets reçus"}, "successRate": {"en": "Success Rate", "fr": "Taux de succès"}, - "errorCount": {"en": "Error Count", "fr": "Nombre d'erreurs"}, - "currentRound": {"en": "Current Round", "fr": "Tour actuel"}, - "totalTasks": {"en": "Total Tasks", "fr": "Tâches totales"}, - "currentTask": {"en": "Current Task", "fr": "Tâche actuelle"}, - "totalActions": {"en": "Total Actions", "fr": "Actions totales"}, - "currentAction": {"en": "Current Action", "fr": "Action actuelle"} + "errorCount": {"en": "Error Count", "fr": "Nombre d'erreurs"} } ) @@ -487,25 +475,12 @@ class ChatMessage(BaseModel, ModelMixin): # New workflow progress fields: taskProgress: Optional[str] = Field( None, - description="Task progress status: pending, running, success, fail, retry", - frontend_options=[ - {"value": "pending", "label": {"en": "Pending", "fr": "En attente"}}, - {"value": "running", "label": {"en": "Running", "fr": "En cours"}}, - {"value": "success", "label": {"en": "Success", "fr": "Succès"}}, - {"value": "fail", "label": {"en": "Failed", "fr": "Échec"}}, - {"value": "retry", "label": {"en": "Retry", "fr": "Nouvel essai"}} - ] + description="Task progress status: pending, running, success, fail, retry" ) actionProgress: Optional[str] = Field( None, - description="Action progress status: pending, running, success, fail", - frontend_options=[ - {"value": "pending", "label": {"en": "Pending", "fr": "En attente"}}, - {"value": "running", "label": {"en": "Running", "fr": "En cours"}}, - {"value": "success", "label": {"en": "Success", "fr": "Succès"}}, - {"value": "fail", "label": {"en": "Failed", "fr": "Échec"}} - ] + description="Action progress status: pending, running, success, fail" ) # Register labels for ChatMessage diff --git a/modules/interfaces/interfaceChatObjects.py b/modules/interfaces/interfaceChatObjects.py index e98565cd..039fd725 100644 --- a/modules/interfaces/interfaceChatObjects.py +++ b/modules/interfaces/interfaceChatObjects.py @@ -157,7 +157,7 @@ class ChatObjects: id=workflow["id"], status=workflow.get("status", "running"), name=workflow.get("name"), - currentRound=workflow.get("currentRound", 0), # Fixed: Default to 0 for new workflows + currentRound=workflow.get("currentRound", 0), # Default value currentTask=workflow.get("currentTask", 0), currentAction=workflow.get("currentAction", 0), totalTasks=workflow.get("totalTasks", 0), @@ -166,12 +166,7 @@ class ChatObjects: startedAt=workflow.get("startedAt", get_utc_timestamp()), logs=[ChatLog(**log) for log in workflow.get("logs", [])], messages=[ChatMessage(**msg) for msg in workflow.get("messages", [])], - stats=ChatStat(**workflow.get("dataStats", {})) if workflow.get("dataStats") else ChatStat( - bytesSent=0, - bytesReceived=0, - tokenCount=0, - processingTime=0 - ), + stats=ChatStat(**workflow.get("stats", {})) if workflow.get("stats") else None, mandateId=workflow.get("mandateId", self.currentUser.mandateId) ) except Exception as e: @@ -202,7 +197,7 @@ class ChatObjects: id=created["id"], status=created.get("status", "running"), name=created.get("name"), - currentRound=created.get("currentRound", 0), # Fixed: Default to 0 for new workflows + currentRound=created.get("currentRound", 0), # Default value currentTask=created.get("currentTask", 0), currentAction=created.get("currentAction", 0), totalTasks=created.get("totalTasks", 0), @@ -211,7 +206,7 @@ class ChatObjects: startedAt=created.get("startedAt", currentTime), logs=[], messages=[], - stats=ChatStat(**created.get("dataStats", {})) if created.get("dataStats") else None, + stats=ChatStat(**created.get("stats", {})) if created.get("stats") else None, mandateId=created.get("mandateId", self.currentUser.mandateId) ) @@ -248,12 +243,7 @@ class ChatObjects: startedAt=updated.get("startedAt", workflow.startedAt), logs=[ChatLog(**log) for log in updated.get("logs", workflow.logs)], messages=[ChatMessage(**msg) for msg in updated.get("messages", workflow.messages)], - stats=ChatStat(**updated.get("dataStats", workflow.stats.dict() if workflow.stats else {})) if updated.get("dataStats") or workflow.stats else ChatStat( - bytesSent=0, - bytesReceived=0, - tokenCount=0, - processingTime=0 - ), + stats=ChatStat(**updated.get("stats", workflow.stats.dict() if workflow.stats else {})) if updated.get("stats") or workflow.stats else None, mandateId=updated.get("mandateId", workflow.mandateId) ) @@ -709,7 +699,7 @@ class ChatObjects: # Update workflow in database self.db.recordModify("workflows", workflowId, { - "dataStats": currentStats + "stats": currentStats }) # Log to stats table @@ -910,25 +900,29 @@ class ChatObjects: "status": "running", "startedAt": currentTime, "lastActivity": currentTime, - "currentRound": 0, # Fixed: Start with 0, will be set to 1 when workflow starts + "currentRound": 0, # Default value, will be set to 1 in workflowStart() + "currentTask": 0, + "currentAction": 0, + "totalTasks": 0, + "totalActions": 0, "mandateId": self.mandateId, "messageIds": [], - "dataStats": { - "totalMessages": 0, - "totalDocuments": 0, - "totalTokens": 0 + "stats": { + "processingTime": None, + "tokenCount": None, + "bytesSent": None, + "bytesReceived": None, + "successRate": None, + "errorCount": None } } # Create workflow workflow = self.createWorkflow(workflowData) - # Ensure workflow is in clean state for new session - if hasattr(workflow, 'currentRound') and workflow.currentRound != 0: - logger.warning(f"New workflow has currentRound={workflow.currentRound}, resetting to 0") - workflow.currentRound = 0 - workflow.currentTask = 0 - workflow.currentAction = 0 + # Set currentRound to 1 for new workflows + workflow.currentRound = 1 + self.updateWorkflow(workflow.id, {"currentRound": 1}) # Initialize stats for the new workflow self.updateWorkflowStats(workflow.id, bytesSent=0, bytesReceived=0) diff --git a/modules/routes/routeWorkflows.py b/modules/routes/routeWorkflows.py index d69ca14f..95947dde 100644 --- a/modules/routes/routeWorkflows.py +++ b/modules/routes/routeWorkflows.py @@ -67,7 +67,7 @@ async def get_workflows( id=workflow_data["id"], status=workflow_data.get("status", "running"), name=workflow_data.get("name"), - currentRound=workflow_data.get("currentRound", 0), # Fixed: Default to 0 for new workflows + currentRound=workflow_data.get("currentRound", 0), # Default value currentTask=workflow_data.get("currentTask", 0), currentAction=workflow_data.get("currentAction", 0), totalTasks=workflow_data.get("totalTasks", 0), @@ -76,12 +76,7 @@ async def get_workflows( startedAt=workflow_data.get("startedAt", get_utc_timestamp()), logs=[ChatLog(**log) for log in workflow_data.get("logs", [])], messages=[ChatMessage(**msg) for msg in workflow_data.get("messages", [])], - stats=ChatStat(**workflow_data.get("dataStats", {})) if workflow_data.get("dataStats") else ChatStat( - bytesSent=0, - bytesReceived=0, - tokenCount=0, - processingTime=0 - ), + stats=ChatStat(**workflow_data.get("stats", {})) if workflow_data.get("stats") else None, mandateId=workflow_data.get("mandateId", currentUser.mandateId or "") ) workflows.append(workflow)