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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
"""
Advanced Agentic System Interface
-------------------------------
Provides a chat interface to interact with the autonomous agent teams:
- Team A: Coders (App/Software Developers)
- Team B: Business (Entrepreneurs)
- Team C: Research (Deep Online Research)
- Team D: Crypto & Sports Trading
"""

import os
import gradio as gr
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from typing import Dict, Any, List, Tuple, Optional
import logging
from pathlib import Path
import asyncio
from datetime import datetime
import json
from requests.adapters import HTTPAdapter, Retry
from urllib3.util.retry import Retry
import time

from agentic_system import AgenticSystem
from team_management import TeamManager, TeamType, TeamObjective
from orchestrator import AgentOrchestrator
from reasoning import UnifiedReasoningEngine as ReasoningEngine

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Configure network settings
TIMEOUT = int(os.getenv('REQUESTS_TIMEOUT', '30'))
MAX_RETRIES = 5
RETRY_BACKOFF = 1

def setup_requests_session():
    """Configure requests session with retries."""
    session = requests.Session()
    retry_strategy = Retry(
        total=MAX_RETRIES,
        backoff_factor=RETRY_BACKOFF,
        status_forcelist=[408, 429, 500, 502, 503, 504],
        allowed_methods=["HEAD", "GET", "PUT", "DELETE", "OPTIONS", "TRACE"]
    )
    adapter = HTTPAdapter(max_retries=retry_strategy)
    session.mount("https://", adapter)
    session.mount("http://", adapter)
    return session

def check_network(max_attempts=3):
    """Check network connectivity with retries."""
    session = setup_requests_session()
    
    for attempt in range(max_attempts):
        try:
            # Try multiple DNS servers
            for dns in ['8.8.8.8', '8.8.4.4', '1.1.1.1']:
                try:
                    socket.gethostbyname('huggingface.co')
                    break
                except socket.gaierror:
                    continue
            
            # Test connection to Hugging Face
            response = session.get('https://huggingface.co./api/health', 
                                 timeout=TIMEOUT)
            if response.status_code == 200:
                return True
                
        except (requests.RequestException, socket.gaierror) as e:
            logger.warning(f"Network check attempt {attempt + 1} failed: {e}")
            if attempt < max_attempts - 1:
                time.sleep(RETRY_BACKOFF * (attempt + 1))
            continue
            
    logger.error("Network connectivity check failed after all attempts")
    return False

class ChatInterface:
    def __init__(self):
        # Check network connectivity
        if not check_network():
            logger.warning("Network connectivity issues detected - continuing with degraded functionality")
            
        # Initialize core components with consistent configuration
        config = {
            "min_confidence": 0.7,
            "parallel_threshold": 3,
            "learning_rate": 0.1,
            "strategy_weights": {
                "LOCAL_LLM": 0.8,
                "CHAIN_OF_THOUGHT": 0.6,
                "TREE_OF_THOUGHTS": 0.5,
                "META_LEARNING": 0.4
            }
        }
        
        self.orchestrator = AgentOrchestrator(config)
        self.agentic_system = AgenticSystem(config)
        self.team_manager = TeamManager(self.orchestrator)
        self.chat_history = []
        self.active_objectives = {}
        
        # Set up network session
        self.session = setup_requests_session()
        
        # Initialize teams
        asyncio.run(self.team_manager.initialize_team_agents())

    async def process_message(
        self,
        message: str,
        history: List[List[str]]
    ) -> Tuple[str, List[List[str]]]:
        """Process incoming chat message."""
        try:
            # Update chat history
            self.chat_history = history

            # Process message
            response = await self._handle_message(message)
            
            # Update history
            if response:
                history.append([message, response])
            
            return response, history
            
        except Exception as e:
            logger.error(f"Error processing message: {str(e)}")
            error_msg = "I apologize, but I encountered an error. Please try again."
            history.append([message, error_msg])
            return error_msg, history

    async def _handle_message(self, message: str) -> str:
        """Handle message processing with error recovery."""
        try:
            # Analyze intent
            intent = await self._analyze_intent(message)
            intent_type = self._get_intent_type(intent)
            
            # Route to appropriate handler
            if intent_type == "query":
                return await self._handle_query(message)
            elif intent_type == "objective":
                return await self._handle_objective(message)
            elif intent_type == "status":
                return await self._handle_status_request(message)
            else:
                return await self._handle_general_chat(message)
                
        except Exception as e:
            logger.error(f"Error in message handling: {str(e)}")
            return "I apologize, but I encountered an error processing your message. Please try again."

    def _get_intent_type(self, intent) -> str:
        """Safely extract intent type from various result formats."""
        if isinstance(intent, dict):
            return intent.get("type", "general")
        return "general"

    async def _analyze_intent(self, message: str) -> Dict[str, Any]:
        """Analyze user message intent with error handling."""
        try:
            # Use reasoning engine to analyze intent
            analysis = await self.orchestrator.reasoning_engine.reason(
                query=message,
                context={
                    "chat_history": self.chat_history,
                    "active_objectives": self.active_objectives
                }
            )
            
            return {
                "type": analysis.get("intent_type", "general"),
                "confidence": analysis.get("confidence", 0.5),
                "entities": analysis.get("entities", []),
                "action_required": analysis.get("action_required", False)
            }
        except Exception as e:
            logger.error(f"Error analyzing intent: {str(e)}")
            return {"type": "general", "confidence": 0.5}

    async def _handle_query(self, message: str) -> str:
        """Handle information queries."""
        try:
            # Get relevant teams for the query
            recommended_teams = await self.team_manager.get_team_recommendations(message)
            
            # Get responses from relevant teams
            responses = []
            for team_type in recommended_teams:
                response = await self._get_team_response(team_type, message)
                if response:
                    responses.append(response)
            
            if not responses:
                return "I apologize, but I couldn't find a relevant answer to your query."
            
            # Combine and format responses
            return self._format_team_responses(responses)
            
        except Exception as e:
            logger.error(f"Error handling query: {str(e)}")
            return "I apologize, but I encountered an error processing your query. Please try again."

    async def _handle_objective(self, message: str) -> str:
        """Handle new objective creation."""
        try:
            # Create new objective
            objective_id = await self.team_manager.create_objective(message)
            if not objective_id:
                return "I apologize, but I couldn't create the objective. Please try again."
            
            # Format and return response
            return self._format_objective_creation(objective_id)
            
        except Exception as e:
            logger.error(f"Error creating objective: {str(e)}")
            return "I apologize, but I encountered an error creating the objective. Please try again."

    async def _handle_status_request(self, message: str) -> str:
        """Handle status check requests."""
        try:
            # Get system status
            system_status = await self.agentic_system.get_system_status()
            
            # Get team status
            team_status = {}
            for team_id, team in self.team_manager.teams.items():
                team_status[team.name] = await self.team_manager.monitor_objective_progress(team_id)
            
            # Get objective status
            objective_status = {}
            for obj_id, obj in self.active_objectives.items():
                objective_status[obj_id] = await self.team_manager.monitor_objective_progress(obj_id)
            
            return self._format_status_response(system_status, team_status, objective_status)
            
        except Exception as e:
            logger.error(f"Error getting status: {str(e)}")
            return "I apologize, but I encountered an error getting the status. Please try again."

    async def _handle_general_chat(self, message: str) -> str:
        """Handle general chat interactions with error recovery."""
        try:
            # Use reasoning engine for response generation
            response = await self.orchestrator.reasoning_engine.reason(
                query=message,
                context={
                    "chat_history": self.chat_history,
                    "system_state": await self.agentic_system.get_system_status()
                }
            )
            
            if not response or not response.get("response"):
                return "I apologize, but I couldn't generate a meaningful response. Please try again."
            
            return response["response"]
            
        except Exception as e:
            logger.error(f"Error in general chat: {str(e)}")
            return "I apologize, but I encountered an error processing your message. Please try again."

    async def _get_team_response(self, team_type: TeamType, query: str) -> Dict[str, Any]:
        """Get response from a specific team."""
        try:
            team = self.team_manager.teams.get(team_type.value)
            if not team:
                return None
            
            # Get response from team's agents
            responses = []
            for agent in team.agents:
                response = await agent.process_query(query)
                if response:
                    responses.append(response)
            
            if not responses:
                return None
            
            # Return best response
            return self._combine_agent_responses(responses)
            
        except Exception as e:
            logger.error(f"Error getting team response: {str(e)}")
            return None

    def _combine_agent_responses(self, responses: List[Dict[str, Any]]) -> Dict[str, Any]:
        """Combine multiple agent responses into a coherent response."""
        try:
            # Sort by confidence
            valid_responses = [
                r for r in responses 
                if r.get("success", False) and r.get("response")
            ]
            
            if not valid_responses:
                return None
                
            sorted_responses = sorted(
                valid_responses,
                key=lambda x: x.get("confidence", 0),
                reverse=True
            )
            
            # Take the highest confidence response
            return sorted_responses[0]
            
        except Exception as e:
            logger.error(f"Error combining responses: {str(e)}")
            return None

    def _format_team_responses(self, responses: List[Dict[str, Any]]) -> str:
        """Format team responses into a readable message."""
        try:
            if not responses:
                return "No team responses available."
            
            formatted = []
            for resp in responses:
                if resp and resp.get("response"):
                    team_name = resp.get("team_name", "Unknown Team")
                    confidence = resp.get("confidence", 0)
                    formatted.append(
                        f"\n{team_name} (Confidence: {confidence:.2%}):\n{resp['response']}"
                    )
            
            if not formatted:
                return "No valid team responses available."
                
            return "\n".join(formatted)
            
        except Exception as e:
            logger.error(f"Error formatting responses: {str(e)}")
            return "Error formatting team responses."

    def _format_objective_creation(self, objective_id: str) -> str:
        """Format objective creation response."""
        try:
            obj = self.active_objectives.get(objective_id)
            if not obj:
                return "Objective created but details not available."
            
            return "\n".join([
                "New Objective Created:",
                f"Description: {obj['description']}",
                f"Status: {obj['status']}",
                f"Assigned Teams: {', '.join(t.value for t in obj['teams'])}"
            ])
            
        except Exception as e:
            logger.error(f"Error formatting objective: {str(e)}")
            return "Error formatting objective details."

    def _format_status_response(
        self,
        system_status: Dict[str, Any],
        team_status: Dict[str, Any],
        objective_status: Dict[str, Any]
    ) -> str:
        """Format status response."""
        try:
            # Format system status
            status = [
                "System Status:",
                f"- State: {system_status['state']}",
                f"- Active Agents: {system_status['agent_count']}",
                f"- Active Tasks: {system_status['active_tasks']}",
                "\nTeam Status:"
            ]
            
            # Add team status
            for team_name, team_info in team_status.items():
                status.extend([
                    f"\n{team_name}:",
                    f"- Active Agents: {team_info['active_agents']}",
                    f"- Completion Rate: {team_info['completion_rate']:.2%}",
                    f"- Collaboration Score: {team_info['collaboration_score']:.2f}"
                ])
            
            # Add objective status
            if objective_status:
                status.append("\nActive Objectives:")
                for obj_id, obj_info in objective_status.items():
                    obj = self.active_objectives[obj_id]
                    status.extend([
                        f"\n{obj['description']}:",
                        f"- Status: {obj['status']}",
                        f"- Teams: {', '.join(t.value for t in obj['teams'])}",
                        f"- Progress: {sum(t['completion_rate'] for t in obj_info.values())/len(obj_info):.2%}"
                    ])
            
            return "\n".join(status)
            
        except Exception as e:
            logger.error(f"Error formatting status: {str(e)}")
            return "Error formatting status information."

class VentureUI:
    def __init__(self, app):
        self.app = app

    def create_interface(self):
        """Create the Gradio interface."""
        with gr.Blocks(
            theme=gr.themes.Soft(),
            analytics_enabled=False,
            title="Advanced Agentic System"
        ) as interface:
            # Verify Gradio version
            gr.Markdown(f"""
            # Advanced Agentic System Chat Interface v{gr.__version__}
            
            Chat with our autonomous agent teams:
            - Team A: Coders (App/Software Developers)
            - Team B: Business (Entrepreneurs)
            - Team C: Research (Deep Online Research)
            - Team D: Crypto & Sports Trading
            
            You can:
            1. Ask questions
            2. Create new objectives
            3. Check status of teams and objectives
            4. Get insights and recommendations
            """)
            
            chatbot = gr.Chatbot(
                label="Chat History",
                height=400,
                bubble_full_width=False,
                show_copy_button=True,
                render_markdown=True
            )
            
            with gr.Row():
                msg = gr.Textbox(
                    label="Message",
                    placeholder="Chat with the Agentic System...",
                    lines=2,
                    scale=9,
                    autofocus=True,
                    container=True
                )
                submit = gr.Button(
                    "Send",
                    scale=1,
                    variant="primary"
                )
            
            with gr.Row():
                clear = gr.ClearButton(
                    [msg, chatbot],
                    value="Clear Chat",
                    variant="secondary",
                    scale=1
                )
                retry = gr.Button(
                    "Retry Last",
                    variant="secondary",
                    scale=1
                )

            async def respond(message, history):
                try:
                    # Convert history to the format expected by process_message
                    history_list = [[x, y] for x, y in history] if history else []
                    response, history_list = await self.app(message, history_list)
                    
                    # Update history
                    if history is None:
                        history = []
                    history.append((message, response))
                    
                    return "", history
                except Exception as e:
                    logger.error(f"Error in chat response: {str(e)}")
                    error_msg = "I apologize, but I encountered an error. Please try again."
                    
                    if history is None:
                        history = []
                    history.append((message, error_msg))
                    
                    return "", history

            async def retry_last(history):
                if not history:
                    return history
                last_user_msg = history[-1][0]
                history = history[:-1]  # Remove last exchange
                return await respond(last_user_msg, history)

            msg.submit(
                respond,
                [msg, chatbot],
                [msg, chatbot],
                api_name="chat"
            ).then(
                lambda: gr.update(interactive=True),
                None,
                [msg, submit],
                queue=False
            )

            submit.click(
                respond,
                [msg, chatbot],
                [msg, chatbot],
                api_name="submit"
            ).then(
                lambda: gr.update(interactive=True),
                None,
                [msg, submit],
                queue=False
            )

            retry.click(
                retry_last,
                [chatbot],
                [chatbot],
                api_name="retry"
            )

            # Event handlers for better UX
            msg.change(lambda x: gr.update(interactive=bool(x.strip())), [msg], [submit])
            
            # Add example inputs
            gr.Examples(
                examples=[
                    "What can Team A (Coders) help me with?",
                    "Create a new objective: Analyze market trends",
                    "What's the status of all teams?",
                    "Give me insights about recent developments"
                ],
                inputs=msg,
                label="Example Queries"
            )

        return interface

def create_chat_interface() -> gr.Blocks:
    """Create Gradio chat interface."""
    chat = ChatInterface()
    ui = VentureUI(chat.process_message)
    return ui.create_interface()

# Initialize FastAPI
app = FastAPI(
    title="Advanced Agentic System",
    description="Venture Strategy Optimizer with OpenAI-compatible API",
    version="1.0.0"
)

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Include OpenAI-compatible routes
from api.openai_compatible import OpenAICompatibleAPI
reasoning_engine = UnifiedReasoningEngine()
openai_api = OpenAICompatibleAPI(reasoning_engine)
app.include_router(openai_api.router, tags=["OpenAI Compatible"])

# Original API routes
@app.get("/api/health")
async def health_check():
    """Health check endpoint."""
    return {
        "status": "healthy",
        "version": "1.0.0",
        "endpoints": {
            "openai_compatible": "/v1/chat/completions",
            "venture": "/api/venture",
            "ui": "/"
        }
    }

@app.post("/api/reason")
async def reason(query: str, context: Optional[Dict[str, Any]] = None):
    """Reasoning endpoint."""
    try:
        result = await reasoning_engine.reason(query, context or {})
        return result
    except Exception as e:
        logger.error(f"Reasoning error: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/venture/analyze")
async def analyze_venture(
    venture_type: str,
    description: str,
    metrics: Optional[Dict[str, Any]] = None
):
    """Venture analysis endpoint."""
    try:
        result = await VentureAPI(reasoning_engine).analyze_venture(
            venture_type=venture_type,
            description=description,
            metrics=metrics or {}
        )
        return result
    except Exception as e:
        logger.error(f"Analysis error: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/venture/types")
async def get_venture_types():
    """Get available venture types."""
    return VentureAPI(reasoning_engine).get_venture_types()

# Create Gradio interface
interface = create_chat_interface()

# Mount Gradio app to FastAPI
app = gr.mount_gradio_app(app, interface, path="/")

if __name__ == "__main__":
    # Run with uvicorn when called directly
    uvicorn.run(
        "app:app",
        host="0.0.0.0",
        port=7860,
        reload=True,
        workers=4
    )