Spaces:
Paused

File size: 7,007 Bytes
22087d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Trigger execution service - executes agents when triggers fire.

This is a thin wrapper that reuses existing agent_runs infrastructure.
"""
import json
import uuid
from datetime import datetime, timezone
from typing import Dict, Any, Optional

from core.services.supabase import DBConnection
from core.services import redis
from core.utils.logger import logger, structlog
from core.utils.config import config, EnvMode
from .trigger_service import TriggerEvent, TriggerResult


class ExecutionService:
    """Executes agents when triggers fire, reusing core agent_runs infrastructure."""

    def __init__(self, db_connection: DBConnection):
        self._db = db_connection
    
    async def execute_trigger_result(
        self,
        agent_id: str,
        trigger_result: TriggerResult,
        trigger_event: TriggerEvent
    ) -> Dict[str, Any]:
        """
        Execute an agent based on trigger result.
        
        Reuses the core agent start infrastructure from agent_runs.py.
        """
        if not config.ACTIVATE_MCPS_TRIG:
            logger.warning("Trigger execution blocked: ACTIVATE_MCPS_TRIG is disabled")
            return {"success": False, "error": "Trigger functionality is disabled"}

        try:
            client = await self._db.client
            agent_result = await client.table('agents').select('account_id').eq('agent_id', agent_id).single().execute()
            if not agent_result.data:
                return {
                    "success": False,
                    "error": f"Agent {agent_id} not found",
                    "message": "Failed to execute trigger"
                }
            account_id = agent_result.data['account_id']
            
            if config.ENV_MODE != EnvMode.LOCAL:
                from core.utils.limits_checker import check_project_count_limit, check_thread_limit
                
                project_limit = await check_project_count_limit(account_id)
                if not project_limit['can_create']:
                    logger.warning(f"Trigger execution blocked: project limit reached for account {account_id} ({project_limit['current_count']}/{project_limit['limit']})")
                    return {    
                        "success": False,
                        "error": f"Project limit reached ({project_limit['current_count']}/{project_limit['limit']}). Upgrade your plan to run more triggers.",
                        "message": "Failed to execute trigger - project limit exceeded"
                    }
                
                thread_limit = await check_thread_limit(account_id)
                if not thread_limit['can_create']:
                    logger.warning(f"Trigger execution blocked: thread limit reached for account {account_id} ({thread_limit['current_count']}/{thread_limit['limit']})")
                    return {
                        "success": False,
                        "error": f"Thread limit reached ({thread_limit['current_count']}/{thread_limit['limit']}). Upgrade your plan to run more triggers.",
                        "message": "Failed to execute trigger - thread limit exceeded"
                    }
            
            rendered_prompt = self._render_prompt(
                trigger_result.agent_prompt,
                trigger_result.execution_variables,
                trigger_event
            )
            
            from core.agents.api import start_agent_run
            
            model_name = trigger_result.model if hasattr(trigger_result, 'model') and trigger_result.model else None
            
            result = await start_agent_run(
                account_id=account_id,
                prompt=rendered_prompt,
                agent_id=agent_id,
                model_name=model_name,
                metadata={
                    "trigger_execution": True,
                    "trigger_id": trigger_event.trigger_id,
                    "trigger_variables": trigger_result.execution_variables
                },
                skip_limits_check=True
            )
            
            return {
                "success": True,
                "thread_id": result.get("thread_id"),
                "agent_run_id": result.get("agent_run_id"),
                "message": "Worker execution started successfully"
            }
                
        except Exception as e:
            logger.error(f"Failed to execute trigger result: {e}", exc_info=True)
            return {
                "success": False,
                "error": str(e),
                "message": "Failed to execute trigger"
            }
    
    def _render_prompt(
        self,
        prompt: str,
        trigger_variables: Optional[Dict[str, Any]],
        trigger_event: TriggerEvent
    ) -> str:
        """Render trigger variables into the prompt template."""
        rendered = prompt
        
        try:
            # Get context from trigger event
            ctx = {}
            if hasattr(trigger_event, "context") and isinstance(trigger_event.context, dict):
                ctx = trigger_event.context
            
            # Merge with execution variables
            if trigger_variables:
                ctx.update(trigger_variables)
            
            payload = ctx.get("payload")
            trigger_slug = ctx.get("trigger_slug")
            webhook_id = ctx.get("webhook_id")
            
            def _to_json(obj: Any) -> str:
                try:
                    return json.dumps(obj, ensure_ascii=False, indent=2)
                except Exception:
                    return str(obj)
            
            # Replace template variables
            if "{{payload}}" in rendered:
                rendered = rendered.replace("{{payload}}", _to_json(payload))
            if "{{trigger_slug}}" in rendered:
                rendered = rendered.replace("{{trigger_slug}}", str(trigger_slug or ""))
            if "{{webhook_id}}" in rendered:
                rendered = rendered.replace("{{webhook_id}}", str(webhook_id or ""))
            
            # Append full context for reference
            if ctx:
                context_json = _to_json(ctx)
                rendered = f"""{rendered}\n\n---\nContext: 
                
                **Important Trigger task execution Reminder:** \n
                For those tasks whose instructions given by the user aren't clear enough, and you decide to use ask() tool to further clarify any question/aspect before proceeding, send those questions in plain text via email to the personal gmail of the user.
                
                \n \n\n{context_json}"""
                
        except Exception as e:  
            logger.warning(f"Failed to render prompt variables: {e}")
            # Return original prompt on error
            
        return rendered


def get_execution_service(db_connection: DBConnection) -> ExecutionService:
    """Factory function for ExecutionService."""
    return ExecutionService(db_connection)