File size: 26,529 Bytes
09801ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
"""
πŸ”¬ DEEP RESEARCH AGENT - Claude-Style Deep Research System
==========================================================

A sophisticated multi-agent system for deep research and analysis.

Architecture (as shown in diagram):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                                             β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”‚
β”‚   β”‚  Planning   │◄──────────────────►│ Sub Agents  β”‚       β”‚
β”‚   β”‚    Tool     β”‚                    β”‚             β”‚       β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜                    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜       β”‚
β”‚          β”‚                                  β”‚               β”‚
β”‚          β”‚        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”‚               β”‚
β”‚          └───────►│ Deep Agents β”‚β—„β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚
β”‚                   β”‚ (Orchestrator)β”‚                         β”‚
β”‚          β”Œβ”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚             │◄─────────┐               β”‚
β”‚          β”‚        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β”‚               β”‚
β”‚          β”‚                                  β”‚               β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”                    β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”       β”‚
β”‚   β”‚    File     β”‚                    β”‚   System    β”‚       β”‚
β”‚   β”‚   System    β”‚                    β”‚   Prompt    β”‚       β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚
β”‚                                                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Components:
- Planning Tool: Creates hierarchical execution plans
- Sub Agents: Specialized agents for different tasks
- File System: Read/write/analyze files
- System Prompt: Dynamic context and instruction management

Features:
- Multi-agent orchestration
- Hierarchical task decomposition
- File I/O capabilities
- Dynamic sub-agent spawning
- Research synthesis
- Source tracking
"""

import logging
import os
import json
from enum import Enum
from typing import Dict, Any, List, Optional, Callable, Tuple
from dataclasses import dataclass, field
from datetime import datetime
import re

logger = logging.getLogger(__name__)

# LLM for reasoning
try:
    from core.llm import chat as llm_chat
    LLM_AVAILABLE = True
except ImportError:
    LLM_AVAILABLE = False
    def llm_chat(*args, **kwargs):
        return "LLM not available"


# =============================================================================
# COMPONENT TYPES
# =============================================================================

class SubAgentType(Enum):
    """Types of specialized sub-agents."""
    RESEARCHER = "researcher"         # Deep research on topics
    ANALYST = "analyst"               # Data analysis
    WRITER = "writer"                 # Content generation
    CRITIC = "critic"                 # Evaluation and critique
    SYNTHESIZER = "synthesizer"       # Combine findings
    FACT_CHECKER = "fact_checker"     # Verify information
    CODER = "coder"                   # Code generation/analysis


class PlanStatus(Enum):
    """Status of execution plan steps."""
    PENDING = "pending"
    IN_PROGRESS = "in_progress"
    COMPLETED = "completed"
    FAILED = "failed"
    SKIPPED = "skipped"


@dataclass
class PlanStep:
    """A step in the execution plan."""
    step_id: int
    description: str
    agent_type: SubAgentType
    dependencies: List[int] = field(default_factory=list)
    status: PlanStatus = PlanStatus.PENDING
    result: Optional[str] = None
    error: Optional[str] = None


@dataclass
class ExecutionPlan:
    """Hierarchical execution plan."""
    goal: str
    steps: List[PlanStep]
    created_at: datetime = field(default_factory=datetime.now)
    completed_at: Optional[datetime] = None
    
    def get_next_step(self) -> Optional[PlanStep]:
        """Get next executable step (dependencies met)."""
        completed_ids = {s.step_id for s in self.steps if s.status == PlanStatus.COMPLETED}
        
        for step in self.steps:
            if step.status == PlanStatus.PENDING:
                if all(dep in completed_ids for dep in step.dependencies):
                    return step
        return None
    
    def is_complete(self) -> bool:
        """Check if all steps are done."""
        return all(s.status in [PlanStatus.COMPLETED, PlanStatus.SKIPPED] for s in self.steps)


@dataclass
class ResearchResult:
    """Result from deep research."""
    query: str
    findings: List[str]
    sources: List[str]
    confidence: float
    synthesis: str
    plan_trace: List[str]
    sub_agent_results: Dict[str, Any] = field(default_factory=dict)


# =============================================================================
# PLANNING TOOL
# =============================================================================

class PlanningTool:
    """
    Creates hierarchical execution plans for complex tasks.
    
    Decomposes goals into sub-tasks with dependencies.
    """
    
    def __init__(self):
        self.plans: List[ExecutionPlan] = []
    
    def create_plan(self, goal: str, context: str = "") -> ExecutionPlan:
        """Create execution plan using LLM."""
        prompt = f"""Create a detailed execution plan for this goal.

GOAL: {goal}
CONTEXT: {context if context else "No additional context"}

Break this into 3-6 concrete steps. For each step, specify:
1. What needs to be done
2. Which type of agent should do it:
   - researcher: Deep research on topics
   - analyst: Data analysis  
   - writer: Content generation
   - critic: Evaluation
   - synthesizer: Combine findings
   - fact_checker: Verify information
   - coder: Code tasks
3. Dependencies (which previous steps must be completed first)

FORMAT (strict JSON):
{{
  "steps": [
    {{"id": 1, "description": "...", "agent": "researcher", "depends_on": []}},
    {{"id": 2, "description": "...", "agent": "analyst", "depends_on": [1]}},
    ...
  ]
}}"""

        response = llm_chat(prompt, temperature=0.2, max_tokens=500)
        
        # Parse response
        steps = self._parse_plan(response, goal)
        
        plan = ExecutionPlan(goal=goal, steps=steps)
        self.plans.append(plan)
        
        return plan
    
    def _parse_plan(self, response: str, goal: str) -> List[PlanStep]:
        """Parse LLM response into plan steps."""
        steps = []
        
        try:
            # Extract JSON
            if "```json" in response:
                json_str = response.split("```json")[1].split("```")[0].strip()
            elif "{" in response:
                json_str = response[response.find("{"):response.rfind("}")+1]
            else:
                json_str = response
            
            data = json.loads(json_str)
            
            for s in data.get("steps", []):
                agent_type = SubAgentType.RESEARCHER  # Default
                agent_str = s.get("agent", "researcher").lower()
                for at in SubAgentType:
                    if at.value == agent_str:
                        agent_type = at
                        break
                
                steps.append(PlanStep(
                    step_id=s.get("id", len(steps) + 1),
                    description=s.get("description", ""),
                    agent_type=agent_type,
                    dependencies=s.get("depends_on", [])
                ))
                
        except (json.JSONDecodeError, KeyError) as e:
            logger.warning(f"Plan parsing failed: {e}, using default plan")
            # Default plan
            steps = [
                PlanStep(1, f"Research: {goal}", SubAgentType.RESEARCHER),
                PlanStep(2, f"Analyze findings", SubAgentType.ANALYST, [1]),
                PlanStep(3, f"Synthesize results", SubAgentType.SYNTHESIZER, [2])
            ]
        
        return steps
    
    def update_step(self, plan: ExecutionPlan, step_id: int, 
                    status: PlanStatus, result: str = None):
        """Update a step's status."""
        for step in plan.steps:
            if step.step_id == step_id:
                step.status = status
                step.result = result
                break


# =============================================================================
# SUB AGENTS
# =============================================================================

class SubAgentFactory:
    """Factory for creating specialized sub-agents."""
    
    @staticmethod
    def create(agent_type: SubAgentType, user_id: str) -> 'BaseSubAgent':
        """Create a sub-agent of the specified type."""
        agents = {
            SubAgentType.RESEARCHER: ResearcherAgent,
            SubAgentType.ANALYST: AnalystSubAgent,
            SubAgentType.WRITER: WriterAgent,
            SubAgentType.CRITIC: CriticAgent,
            SubAgentType.SYNTHESIZER: SynthesizerAgent,
            SubAgentType.FACT_CHECKER: FactCheckerAgent,
            SubAgentType.CODER: CoderAgent
        }
        
        agent_class = agents.get(agent_type, ResearcherAgent)
        return agent_class(user_id)


class BaseSubAgent:
    """Base class for sub-agents."""
    
    def __init__(self, user_id: str):
        self.user_id = user_id
        self.agent_type: SubAgentType = SubAgentType.RESEARCHER
    
    def execute(self, task: str, context: str = "") -> str:
        """Execute the task."""
        raise NotImplementedError


class ResearcherAgent(BaseSubAgent):
    """Deep research on topics using available data and knowledge."""
    
    def __init__(self, user_id: str):
        super().__init__(user_id)
        self.agent_type = SubAgentType.RESEARCHER
    
    def execute(self, task: str, context: str = "") -> str:
        # Try to get data context
        data_context = ""
        try:
            from core.rag import rag_search
            data_context, sources = rag_search(self.user_id, task, k=5)
        except:
            pass
        
        prompt = f"""You are a Deep Research Agent. Research this topic thoroughly.

TASK: {task}

DATA FROM USER'S FILES:
{data_context if data_context else "No user data available"}

PREVIOUS CONTEXT:
{context if context else "No previous context"}

Provide comprehensive research findings with:
1. Key facts and insights
2. Data-backed observations (if data available)
3. Important patterns or trends
4. Source references

Be thorough but concise."""

        return llm_chat(prompt, temperature=0.3, max_tokens=600)


class AnalystSubAgent(BaseSubAgent):
    """Data analysis specialist."""
    
    def __init__(self, user_id: str):
        super().__init__(user_id)
        self.agent_type = SubAgentType.ANALYST
    
    def execute(self, task: str, context: str = "") -> str:
        # Get user data statistics
        stats_context = ""
        try:
            from api.v1.endpoints.charts import get_user_data
            df = get_user_data(self.user_id)
            if df is not None and not df.empty:
                stats_context = f"""
Dataset: {len(df)} rows Γ— {len(df.columns)} columns
Columns: {', '.join(df.columns.tolist()[:10])}
"""
                # Add numeric stats
                numeric_cols = df.select_dtypes(include=['number']).columns[:5]
                for col in numeric_cols:
                    stats_context += f"- {col}: mean={df[col].mean():,.2f}, sum={df[col].sum():,.2f}\n"
        except:
            pass
        
        prompt = f"""You are a Data Analyst Agent. Analyze this task.

TASK: {task}

DATA STATISTICS:
{stats_context if stats_context else "No data available"}

PREVIOUS FINDINGS:
{context if context else "No previous findings"}

Provide analytical insights:
1. Key metrics and values
2. Statistical observations
3. Patterns and anomalies
4. Data-driven conclusions

Use actual numbers from the data."""

        return llm_chat(prompt, temperature=0.2, max_tokens=500)


class WriterAgent(BaseSubAgent):
    """Content generation specialist."""
    
    def __init__(self, user_id: str):
        super().__init__(user_id)
        self.agent_type = SubAgentType.WRITER
    
    def execute(self, task: str, context: str = "") -> str:
        prompt = f"""You are a Writer Agent. Create content for this task.

TASK: {task}

CONTEXT AND RESEARCH:
{context if context else "No context provided"}

Write clear, professional content that:
1. Is well-structured
2. Uses the provided research
3. Is engaging and informative
4. Includes key data points

Keep it concise but comprehensive."""

        return llm_chat(prompt, temperature=0.4, max_tokens=600)


class CriticAgent(BaseSubAgent):
    """Evaluation and critique specialist."""
    
    def __init__(self, user_id: str):
        super().__init__(user_id)
        self.agent_type = SubAgentType.CRITIC
    
    def execute(self, task: str, context: str = "") -> str:
        prompt = f"""You are a Critic Agent. Evaluate this content critically.

TASK: {task}

CONTENT TO EVALUATE:
{context if context else "No content provided"}

Provide critical evaluation:
1. Strengths of the analysis
2. Weaknesses or gaps
3. Accuracy concerns
4. Suggestions for improvement

Be constructive but thorough."""

        return llm_chat(prompt, temperature=0.3, max_tokens=400)


class SynthesizerAgent(BaseSubAgent):
    """Combines findings from multiple sources."""
    
    def __init__(self, user_id: str):
        super().__init__(user_id)
        self.agent_type = SubAgentType.SYNTHESIZER
    
    def execute(self, task: str, context: str = "") -> str:
        prompt = f"""You are a Synthesizer Agent. Combine and summarize findings.

TASK: {task}

FINDINGS TO SYNTHESIZE:
{context if context else "No findings provided"}

Create a unified synthesis that:
1. Combines key insights
2. Resolves contradictions
3. Highlights consensus
4. Provides actionable conclusions

Be comprehensive but concise."""

        return llm_chat(prompt, temperature=0.3, max_tokens=500)


class FactCheckerAgent(BaseSubAgent):
    """Verifies information accuracy."""
    
    def __init__(self, user_id: str):
        super().__init__(user_id)
        self.agent_type = SubAgentType.FACT_CHECKER
    
    def execute(self, task: str, context: str = "") -> str:
        prompt = f"""You are a Fact Checker Agent. Verify the accuracy of claims.

TASK: {task}

CLAIMS TO VERIFY:
{context if context else "No claims provided"}

For each claim:
1. βœ… Verified - backed by data
2. ⚠️ Uncertain - needs more evidence
3. ❌ Incorrect - contradicts data

Provide verification results with reasoning."""

        return llm_chat(prompt, temperature=0.2, max_tokens=400)


class CoderAgent(BaseSubAgent):
    """Code generation and analysis."""
    
    def __init__(self, user_id: str):
        super().__init__(user_id)
        self.agent_type = SubAgentType.CODER
    
    def execute(self, task: str, context: str = "") -> str:
        prompt = f"""You are a Coder Agent. Handle code-related tasks.

TASK: {task}

CONTEXT:
{context if context else "No context provided"}

Provide:
1. Code solution (if applicable)
2. Explanation of approach
3. Key considerations

Format code in markdown code blocks."""

        return llm_chat(prompt, temperature=0.2, max_tokens=600)


# =============================================================================
# FILE SYSTEM TOOL
# =============================================================================

class FileSystemTool:
    """
    File system capabilities for deep research.
    
    Features:
    - Read user's uploaded files
    - Analyze file contents
    - Search across files
    """
    
    def __init__(self, user_id: str):
        self.user_id = user_id
    
    def list_files(self) -> List[str]:
        """List available user files."""
        try:
            from utils.paths import get_user_paths
            paths = get_user_paths(self.user_id)
            files = []
            for ext in ['*.csv', '*.xlsx', '*.pdf', '*.txt']:
                import glob
                files.extend(glob.glob(os.path.join(paths.get('uploads', ''), ext)))
            return [os.path.basename(f) for f in files]
        except:
            return []
    
    def read_file_summary(self, filename: str) -> str:
        """Get summary of a file."""
        try:
            from api.v1.endpoints.charts import get_user_data
            df = get_user_data(self.user_id)
            
            if df is not None:
                return f"""File Summary:
- Rows: {len(df)}
- Columns: {len(df.columns)}
- Columns: {', '.join(df.columns.tolist()[:15])}
- Memory: {df.memory_usage(deep=True).sum() / 1024:.1f} KB"""
            return "File not found or not readable"
        except Exception as e:
            return f"Error reading file: {str(e)[:100]}"
    
    def search_in_files(self, query: str) -> str:
        """Search for information in user's files."""
        try:
            from core.rag import rag_search
            context, sources = rag_search(self.user_id, query, k=5)
            return context if context else "No matching content found"
        except Exception as e:
            return f"Search error: {str(e)[:100]}"


# =============================================================================
# SYSTEM PROMPT MANAGER
# =============================================================================

class SystemPromptManager:
    """
    Manages dynamic system prompts for the deep agent.
    
    Maintains context across interactions and agent executions.
    """
    
    def __init__(self, user_id: str):
        self.user_id = user_id
        self.base_prompt = """You are a Deep Research Agent - an advanced AI system capable of:
1. Breaking down complex research tasks
2. Coordinating specialized sub-agents
3. Synthesizing findings from multiple sources
4. Providing accurate, data-grounded insights

Always prioritize:
- User's data over general knowledge
- Accuracy over speculation
- Concise, actionable insights"""
        
        self.context_stack: List[str] = []
        self.research_history: List[str] = []
    
    def get_full_prompt(self, task: str = "") -> str:
        """Get complete system prompt with context."""
        prompt = self.base_prompt
        
        if self.context_stack:
            prompt += "\n\nACCUMULATED CONTEXT:\n"
            prompt += "\n".join(self.context_stack[-5:])  # Last 5 contexts
        
        if task:
            prompt += f"\n\nCURRENT TASK: {task}"
        
        return prompt
    
    def add_context(self, context: str):
        """Add context to the stack."""
        self.context_stack.append(context)
    
    def add_research(self, finding: str):
        """Add research finding to history."""
        self.research_history.append(finding)
    
    def get_research_summary(self) -> str:
        """Get summary of research history."""
        if not self.research_history:
            return "No research conducted yet."
        return "\n---\n".join(self.research_history[-10:])
    
    def clear(self):
        """Clear all context."""
        self.context_stack = []
        self.research_history = []


# =============================================================================
# DEEP RESEARCH AGENT (MAIN ORCHESTRATOR)
# =============================================================================

class DeepResearchAgent:
    """
    πŸ”¬ DEEP RESEARCH AGENT - Claude-Style Research System
    
    Main orchestrator that coordinates:
    - Planning Tool: Creates execution plans
    - Sub Agents: Specialized task executors
    - File System: Data access
    - System Prompt: Context management
    
    Flow:
    1. Receive research query
    2. Planning Tool creates hierarchical plan
    3. Sub Agents execute each step
    4. File System provides data context
    5. System Prompt maintains context
    6. Synthesize final results
    """
    
    def __init__(self, user_id: str):
        self.user_id = user_id
        
        # Initialize components
        self.planning_tool = PlanningTool()
        self.file_system = FileSystemTool(user_id)
        self.system_prompt = SystemPromptManager(user_id)
        
        # Track execution
        self.execution_trace: List[str] = []
    
    def research(self, query: str, depth: str = "standard") -> ResearchResult:
        """
        Execute deep research on a query.
        
        Args:
            query: Research question or topic
            depth: "quick" (1-2 steps), "standard" (3-4 steps), "deep" (5+ steps)
        
        Returns:
            ResearchResult with findings, sources, and synthesis
        """
        self.execution_trace = []
        self.execution_trace.append(f"πŸ”¬ Starting deep research: {query}")
        
        # Get file context
        files = self.file_system.list_files()
        file_context = f"Available files: {', '.join(files[:5])}" if files else ""
        self.system_prompt.add_context(f"User has {len(files)} files available")
        
        # Create execution plan
        self.execution_trace.append("πŸ“‹ Creating execution plan...")
        plan = self.planning_tool.create_plan(
            goal=query,
            context=file_context
        )
        self.execution_trace.append(f"πŸ“‹ Plan created with {len(plan.steps)} steps")
        
        # Execute plan
        findings = []
        sources = []
        sub_agent_results = {}
        
        while not plan.is_complete():
            step = plan.get_next_step()
            if not step:
                break
            
            self.execution_trace.append(f"▢️ Step {step.step_id}: {step.description}")
            
            # Update status
            self.planning_tool.update_step(plan, step.step_id, PlanStatus.IN_PROGRESS)
            
            # Create and execute sub-agent
            try:
                sub_agent = SubAgentFactory.create(step.agent_type, self.user_id)
                
                # Build context from previous steps
                prev_context = "\n\n".join([
                    s.result for s in plan.steps 
                    if s.status == PlanStatus.COMPLETED and s.result
                ])
                
                # Execute
                result = sub_agent.execute(step.description, prev_context)
                
                # Store result
                self.planning_tool.update_step(plan, step.step_id, PlanStatus.COMPLETED, result)
                sub_agent_results[f"step_{step.step_id}"] = {
                    "agent": step.agent_type.value,
                    "result": result[:500]  # Truncate for storage
                }
                
                # Add to findings
                findings.append(result)
                sources.append(f"{step.agent_type.value}_agent")
                
                # Update system context
                self.system_prompt.add_research(f"Step {step.step_id}: {result[:200]}")
                
                self.execution_trace.append(f"βœ… Step {step.step_id} completed")
                
            except Exception as e:
                self.planning_tool.update_step(plan, step.step_id, PlanStatus.FAILED, error=str(e))
                self.execution_trace.append(f"❌ Step {step.step_id} failed: {str(e)[:100]}")
        
        plan.completed_at = datetime.now()
        
        # Final synthesis
        self.execution_trace.append("πŸ”„ Synthesizing results...")
        synthesizer = SynthesizerAgent(self.user_id)
        synthesis = synthesizer.execute(
            f"Synthesize research findings for: {query}",
            "\n\n---\n\n".join(findings)
        )
        
        self.execution_trace.append("βœ… Research complete")
        
        # Calculate confidence
        completed_steps = sum(1 for s in plan.steps if s.status == PlanStatus.COMPLETED)
        confidence = completed_steps / len(plan.steps) if plan.steps else 0.5
        
        return ResearchResult(
            query=query,
            findings=findings,
            sources=list(set(sources)),
            confidence=confidence,
            synthesis=synthesis,
            plan_trace=self.execution_trace,
            sub_agent_results=sub_agent_results
        )
    
    def format_response(self, result: ResearchResult) -> str:
        """Format research result for display."""
        response = f"""## πŸ”¬ Deep Research Results

**Query:** {result.query}
**Confidence:** {result.confidence:.0%}

---

### πŸ“Š Synthesis

{result.synthesis}

---

### πŸ“‹ Research Process

"""
        for trace in result.plan_trace:
            response += f"- {trace}\n"
        
        response += f"\n---\n**Sources:** {', '.join(result.sources)}"
        
        return response


# =============================================================================
# CONVENIENCE FUNCTIONS
# =============================================================================

def deep_research(user_id: str, query: str, depth: str = "standard") -> Dict[str, Any]:
    """Quick function for deep research."""
    agent = DeepResearchAgent(user_id)
    result = agent.research(query, depth)
    
    return {
        "query": result.query,
        "synthesis": result.synthesis,
        "findings": result.findings,
        "sources": result.sources,
        "confidence": result.confidence,
        "trace": result.plan_trace
    }


def deep_research_formatted(user_id: str, query: str) -> str:
    """Deep research with formatted response."""
    agent = DeepResearchAgent(user_id)
    result = agent.research(query)
    return agent.format_response(result)


# Module exports
__all__ = [
    'DeepResearchAgent',
    'PlanningTool',
    'SubAgentFactory',
    'FileSystemTool',
    'SystemPromptManager',
    'ResearchResult',
    'ExecutionPlan',
    'SubAgentType',
    'deep_research',
    'deep_research_formatted'
]