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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# AI Workflow Agent - Chat API
"""

Conversational interface for the AI Workflow Agent.

Supports multi-turn conversations with session management.

"""

import uuid
import json
import logging
from datetime import datetime
from typing import Dict, Any, Optional, List
from dataclasses import dataclass, field, asdict
from enum import Enum

logger = logging.getLogger(__name__)


class MessageRole(Enum):
    """Message roles in conversation."""
    USER = "user"
    ASSISTANT = "assistant"
    SYSTEM = "system"


class ConversationState(Enum):
    """Current state of conversation."""
    INITIAL = "initial"
    ANALYZING = "analyzing"
    CLARIFYING = "clarifying"
    PLANNING = "planning"
    BUILDING = "building"
    COMPLETE = "complete"
    ERROR = "error"


@dataclass
class Message:
    """Single message in conversation."""
    role: str
    content: str
    timestamp: str = field(default_factory=lambda: datetime.now().isoformat())
    metadata: Dict[str, Any] = field(default_factory=dict)


@dataclass
class Session:
    """Conversation session."""
    session_id: str
    created_at: str
    state: str = ConversationState.INITIAL.value
    messages: List[Dict[str, Any]] = field(default_factory=list)
    context: Dict[str, Any] = field(default_factory=dict)
    project_type: Optional[str] = None
    workflow: Optional[Dict[str, Any]] = None
    pending_questions: List[str] = field(default_factory=list)
    
    def add_message(self, role: str, content: str, metadata: Dict = None):
        """Add a message to the conversation."""
        self.messages.append({
            "role": role,
            "content": content,
            "timestamp": datetime.now().isoformat(),
            "metadata": metadata or {}
        })
    
    def get_history_text(self, limit: int = 10) -> str:
        """Get conversation history as text for LLM context."""
        recent = self.messages[-limit:]
        lines = []
        for msg in recent:
            role = msg["role"].upper()
            content = msg["content"]
            lines.append(f"{role}: {content}")
        return "\n".join(lines)
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary."""
        return asdict(self)


class SessionManager:
    """Manages conversation sessions."""
    
    def __init__(self, max_sessions: int = 100):
        self.sessions: Dict[str, Session] = {}
        self.max_sessions = max_sessions
    
    def create_session(self) -> Session:
        """Create a new conversation session."""
        # Cleanup old sessions if limit reached
        if len(self.sessions) >= self.max_sessions:
            self._cleanup_old_sessions()
        
        session_id = str(uuid.uuid4())[:8]
        session = Session(
            session_id=session_id,
            created_at=datetime.now().isoformat()
        )
        
        # Add system message
        session.add_message(
            MessageRole.SYSTEM.value,
            "AI Workflow Agent initialized. Ready to help build n8n, ComfyUI, "
            "or hybrid workflows. Describe what you want to create."
        )
        
        self.sessions[session_id] = session
        logger.info(f"Created session: {session_id}")
        return session
    
    def get_session(self, session_id: str) -> Optional[Session]:
        """Get existing session by ID."""
        return self.sessions.get(session_id)
    
    def get_or_create(self, session_id: Optional[str] = None) -> Session:
        """Get existing session or create new one."""
        if session_id and session_id in self.sessions:
            return self.sessions[session_id]
        return self.create_session()
    
    def update_state(self, session_id: str, state: ConversationState):
        """Update session state."""
        if session_id in self.sessions:
            self.sessions[session_id].state = state.value
    
    def delete_session(self, session_id: str) -> bool:
        """Delete a session."""
        if session_id in self.sessions:
            del self.sessions[session_id]
            logger.info(f"Deleted session: {session_id}")
            return True
        return False
    
    def list_sessions(self) -> List[Dict[str, Any]]:
        """List all active sessions."""
        return [
            {
                "session_id": s.session_id,
                "created_at": s.created_at,
                "state": s.state,
                "message_count": len(s.messages),
                "project_type": s.project_type
            }
            for s in self.sessions.values()
        ]
    
    def _cleanup_old_sessions(self):
        """Remove oldest sessions to make room."""
        if not self.sessions:
            return
        
        # Sort by creation time and remove oldest 20%
        sorted_sessions = sorted(
            self.sessions.items(),
            key=lambda x: x[1].created_at
        )
        
        to_remove = len(sorted_sessions) // 5
        for session_id, _ in sorted_sessions[:to_remove]:
            del self.sessions[session_id]
        
        logger.info(f"Cleaned up {to_remove} old sessions")


class ChatHandler:
    """Handles chat interactions with the agent system."""
    
    def __init__(self):
        self.session_manager = SessionManager()
        self._agent_system = None  # Lazy load
    
    @property
    def agent_system(self):
        """Lazy load agent system to avoid circular imports."""
        if self._agent_system is None:
            from crew_agents import crew_agent_system
            self._agent_system = crew_agent_system
        return self._agent_system
    
    async def chat(

        self,

        message: str,

        session_id: Optional[str] = None

    ) -> Dict[str, Any]:
        """

        Process a chat message and return response.

        

        Args:

            message: User message

            session_id: Optional existing session ID

            

        Returns:

            Dict with response, session_id, state, and optionally questions/workflow

        """
        # Get or create session
        session = self.session_manager.get_or_create(session_id)
        
        # Add user message
        session.add_message(MessageRole.USER.value, message)
        
        try:
            # Handle based on current state
            if session.state == ConversationState.CLARIFYING.value:
                # User is answering clarifying questions
                return await self._handle_clarification(session, message)
            else:
                # New request or continuation
                return await self._handle_request(session, message)
                
        except Exception as e:
            logger.error(f"Chat error: {e}")
            session.state = ConversationState.ERROR.value
            session.add_message(
                MessageRole.ASSISTANT.value,
                f"Sorry, I encountered an error: {str(e)}. Please try again."
            )
            return {
                "success": False,
                "session_id": session.session_id,
                "response": f"Error: {str(e)}",
                "state": session.state
            }
    
    async def _handle_request(self, session: Session, message: str) -> Dict[str, Any]:
        """Handle a new or continuing request."""
        session.state = ConversationState.ANALYZING.value
        
        # Analyze the request
        analysis = await self.agent_system.analyze_request(
            query=message,
            session_id=session.session_id,
            context={"history": session.get_history_text()}
        )
        
        if not analysis.get("success"):
            error_msg = analysis.get("error", "Analysis failed")
            session.add_message(MessageRole.ASSISTANT.value, f"Error: {error_msg}")
            return {
                "success": False,
                "session_id": session.session_id,
                "response": error_msg,
                "state": session.state
            }
        
        # Check if clarification needed
        if analysis.get("needs_clarification") and analysis.get("confidence", 0) < 0.7:
            session.state = ConversationState.CLARIFYING.value
            questions = analysis.get("questions", [])
            session.pending_questions = questions
            
            # Build response with questions
            response_parts = [analysis.get("analysis", "I need some clarification:")]
            for i, q in enumerate(questions, 1):
                response_parts.append(f"\n{i}. {q}")
            
            response = "\n".join(response_parts)
            session.add_message(MessageRole.ASSISTANT.value, response)
            
            return {
                "success": True,
                "session_id": session.session_id,
                "response": response,
                "state": session.state,
                "needs_clarification": True,
                "questions": questions,
                "project_type": analysis.get("project_type")
            }
        
        # Proceed to build
        return await self._build_workflow(session, analysis)
    
    async def _handle_clarification(self, session: Session, answer: str) -> Dict[str, Any]:
        """Handle user's answer to clarifying questions."""
        # Store the clarification
        if session.pending_questions:
            question = session.pending_questions[0]
            self.agent_system.add_clarification(
                session.session_id,
                question,
                answer
            )
            session.pending_questions = session.pending_questions[1:]
        
        # If more questions pending, ask next one
        if session.pending_questions:
            next_question = session.pending_questions[0]
            response = f"Thanks! Next question: {next_question}"
            session.add_message(MessageRole.ASSISTANT.value, response)
            
            return {
                "success": True,
                "session_id": session.session_id,
                "response": response,
                "state": session.state,
                "needs_clarification": True,
                "questions": session.pending_questions
            }
        
        # All questions answered, proceed to build
        session.add_message(
            MessageRole.ASSISTANT.value,
            "Great, I have all the information I need. Building your workflow..."
        )
        
        # Re-analyze with new information
        conv_context = self.agent_system.get_session(session.session_id)
        if conv_context:
            analysis = {
                "project_type": conv_context.project_type,
                "confidence": 0.9,
                "requirements": conv_context.requirements
            }
            return await self._build_workflow(session, analysis)
        
        return await self._handle_request(session, session.messages[-2]["content"])
    
    async def _build_workflow(self, session: Session, analysis: Dict[str, Any]) -> Dict[str, Any]:
        """Build the workflow based on analysis."""
        session.state = ConversationState.PLANNING.value
        session.project_type = analysis.get("project_type")
        
        # Use the simple builders for reliability (CrewAI for complex cases)
        from tools.n8n_builder import N8NWorkflowBuilder
        from tools.comfyui_builder import ComfyUIWorkflowBuilder
        from tools.github_search import GitHubSearchTool
        
        project_type = analysis.get("project_type", "unknown")
        original_query = session.messages[1]["content"] if len(session.messages) > 1 else ""
        
        session.state = ConversationState.BUILDING.value
        
        try:
            if project_type == "n8n":
                builder = N8NWorkflowBuilder()
                workflow = await builder.generate_workflow(original_query)
                response = "I've generated an n8n workflow for you. Here's the configuration:"
                
            elif project_type == "comfyui":
                builder = ComfyUIWorkflowBuilder()
                workflow = await builder.generate_workflow(original_query)
                response = "I've generated a ComfyUI workflow. Here's the configuration:"
                
            elif project_type == "hybrid":
                n8n_builder = N8NWorkflowBuilder()
                comfyui_builder = ComfyUIWorkflowBuilder()
                
                n8n_wf = await n8n_builder.generate_workflow(original_query)
                comfyui_wf = await comfyui_builder.generate_workflow(original_query)
                
                workflow = {
                    "type": "hybrid",
                    "n8n_workflow": n8n_wf,
                    "comfyui_workflow": comfyui_wf,
                    "integration_note": "n8n can call ComfyUI via HTTP Request node to /prompt endpoint"
                }
                response = "I've generated a hybrid workflow combining n8n automation with ComfyUI for AI generation."
                
            elif project_type == "external_repo":
                github = GitHubSearchTool()
                repos = await github.search(original_query, max_results=3)
                recommendation = await github.generate_recommendation(repos)
                
                workflow = {
                    "type": "external_repo",
                    "repositories": repos,
                    "recommendation": recommendation
                }
                response = f"I found some relevant repositories:\n\n{recommendation}"
                
            else:
                workflow = None
                response = "I couldn't determine the project type. Could you provide more details?"
            
            session.workflow = workflow
            session.state = ConversationState.COMPLETE.value
            
            if workflow and project_type not in ["external_repo"]:
                response += f"\n\n```json\n{json.dumps(workflow, indent=2)[:2000]}\n```"
            
            session.add_message(MessageRole.ASSISTANT.value, response[:500] + "..." if len(response) > 500 else response)
            
            return {
                "success": True,
                "session_id": session.session_id,
                "response": response,
                "state": session.state,
                "project_type": project_type,
                "workflow": workflow
            }
            
        except Exception as e:
            logger.error(f"Build error: {e}")
            session.state = ConversationState.ERROR.value
            response = f"Error building workflow: {str(e)}"
            session.add_message(MessageRole.ASSISTANT.value, response)
            
            return {
                "success": False,
                "session_id": session.session_id,
                "response": response,
                "state": session.state,
                "error": str(e)
            }
    
    def get_session_info(self, session_id: str) -> Optional[Dict[str, Any]]:
        """Get session information."""
        session = self.session_manager.get_session(session_id)
        if session:
            return session.to_dict()
        return None
    
    def list_sessions(self) -> List[Dict[str, Any]]:
        """List all sessions."""
        return self.session_manager.list_sessions()
    
    def clear_session(self, session_id: str) -> bool:
        """Clear a session."""
        # Also clear from agent system
        if hasattr(self, '_agent_system') and self._agent_system:
            self._agent_system.clear_session(session_id)
        return self.session_manager.delete_session(session_id)


# Singleton instance
chat_handler = ChatHandler()