File size: 2,492 Bytes
383cb38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import logging
from typing import Any, Dict
from finance.forensics_service import FinancialForensicsService
from operations.business_health_service import BusinessHealthService
from protection.customer_protection_service import CustomerProtectionService
from sqlalchemy.orm import Session

logger = logging.getLogger(__name__)

class SystemIntelligenceService:
    def __init__(self, db: Session):
        self.db = db
        self.health_service = BusinessHealthService(db)
        self.forensics_service = FinancialForensicsService(db)
        self.protection_service = CustomerProtectionService(db)

    def get_aggregated_context(self, workspace_id: str = "default") -> str:
        """
        Aggregates critical business intelligence into a natural language summary
        for the Main Chat agent.
        """
        context_parts = []
        
        # 1. Business Health
        try:
            health = self.health_service.get_business_health_score(workspace_id)
            score = health.get("score", 0)
            status = health.get("status", "Unknown")
            context_parts.append(f"Business Health is {status} (Score: {score}/100).")
        except Exception as e:
            logger.error(f"Error fetching health context: {e}")

        # 2. Daily Priorities
        try:
            priorities = self.health_service.get_daily_priorities(workspace_id)
            if priorities:
                top_3 = ", ".join([p["title"] for p in priorities[:3]])
                context_parts.append(f"Top priorities today: {top_3}.")
        except Exception as e:
            logger.error(f"Error fetching priority context: {e}")

        # 3. Financial Alerts
        try:
            drift = self.forensics_service.analyze_vendor_price_drift(workspace_id)
            if drift:
                vendors = ", ".join([d["vendor_name"] for d in drift])
                context_parts.append(f"ALERT: Price drift detected for {vendors}.")
        except Exception as e:
            logger.error(f"Error fetching forensics context: {e}")

        # 4. Risk / Churn
        try:
            churn = self.protection_service.predict_churn_risk(workspace_id)
            if churn:
                risky_clients = ", ".join([c["client_name"] for c in churn])
                context_parts.append(f"WARNING: Churn risk detected for {risky_clients}.")
        except Exception as e:
            logger.error(f"Error fetching churn context: {e}")

        return " ".join(context_parts)