File size: 13,511 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
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
import asyncio
import json
import time
import uuid
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional

import structlog

from core.services import redis
from core.utils.config import config, EnvMode

logger = structlog.get_logger(__name__)


async def prepopulate_caches_for_new_thread(
    thread_id: str,
    project_id: str,
    message_content: str,
    image_contexts: List[Dict[str, Any]],
    mode: Optional[str],
) -> None:
    from core.cache.runtime_cache import set_cached_message_history, get_cached_project_metadata, set_cached_project_metadata
    
    if message_content and message_content.strip():
        initial_messages = [{
            "role": "user",
            "content": message_content,
            "message_id": str(uuid.uuid4())
        }]
        await set_cached_message_history(thread_id, initial_messages)
    
    has_images = len(image_contexts) > 0
    cache_key = f"thread_has_images:{thread_id}"
    await redis.set(cache_key, "1" if has_images else "0", ex=7200 if has_images else 300)
    
    if mode:
        existing = await get_cached_project_metadata(project_id)
        existing_sandbox = existing.get('sandbox', {}) if existing else {}
        project_metadata = {**existing_sandbox, "mode": mode}
        await set_cached_project_metadata(project_id, project_metadata)


async def invalidate_caches_for_existing_thread(thread_id: str) -> None:
    from core.cache.runtime_cache import invalidate_message_history_cache
    await invalidate_message_history_cache(thread_id)
    logger.debug(f"🗑️ Invalidated message cache for thread {thread_id}")


async def append_user_message_to_cache(thread_id: str, message_content: str) -> bool:
    from core.cache.runtime_cache import append_to_cached_message_history
    import uuid
    
    if not message_content or not message_content.strip():
        return False
    
    message = {
        "role": "user",
        "content": message_content,
        "message_id": str(uuid.uuid4())
    }
    return await append_to_cached_message_history(thread_id, message)


async def write_user_message_for_existing_thread(
    thread_id: str,
    message_content: str,
) -> None:
    from core.threads import repo as threads_repo
    
    if not message_content or not message_content.strip():
        return
    
    msg_start = time.time()
    await threads_repo.create_message_full(
        message_id=str(uuid.uuid4()),
        thread_id=thread_id,
        message_type="user",
        content={"role": "user", "content": message_content},
        is_llm_message=True
    )
    logger.debug(f"⏱️ User message written: {(time.time() - msg_start)*1000:.1f}ms")


async def prewarm_user_context(account_id: str) -> None:
    try:
        from core.cache.runtime_cache import get_cached_user_context, set_cached_user_context
        
        cached = await get_cached_user_context(account_id)
        if cached is not None:
            return
        
        from core.services.supabase import DBConnection
        db = DBConnection()
        client = await db.client
        
        async def fetch_locale():
            try:
                from core.utils.user_locale import get_user_locale
                return await get_user_locale(account_id, client)
            except Exception:
                return None
        
        async def fetch_username():
            try:
                user = await client.auth.admin.get_user_by_id(account_id)
                if user and user.user:
                    user_metadata = user.user.user_metadata or {}
                    email = user.user.email
                    return (
                        user_metadata.get('full_name') or
                        user_metadata.get('name') or
                        user_metadata.get('display_name') or
                        (email.split('@')[0] if email else None)
                    )
                return None
            except Exception:
                return None

        async def fetch_subscription():
            try:
                from core.billing import subscription_service
                from core.billing.shared.config import get_tier_by_name, get_tier_limits

                tier_info = await subscription_service.get_user_subscription_tier(account_id)
                tier_name = tier_info.get('name', 'free')
                tier = get_tier_by_name(tier_name)
                limits = get_tier_limits(tier_name)

                return {
                    'name': tier_name,
                    'display_name': tier.display_name if tier else 'Basic',
                    'custom_workers_limit': limits.get('custom_workers_limit', 0),
                    'scheduled_triggers_limit': limits.get('scheduled_triggers_limit', 0),
                    'app_triggers_limit': limits.get('app_triggers_limit', 0),
                    'concurrent_runs': limits.get('concurrent_runs', 1),
                    'can_purchase_credits': limits.get('can_purchase_credits', False),
                }
            except Exception:
                return None

        locale, username, subscription = await asyncio.gather(fetch_locale(), fetch_username(), fetch_subscription())
        
        context_parts = []
        if locale:
            from core.utils.user_locale import get_locale_context_prompt
            locale_prompt = get_locale_context_prompt(locale)
            context_parts.append(f"\n\n{locale_prompt}\n")
        if username:
            username_info = f"\n\n<user_info>\n"
            username_info += f"The user's name is: {username}\n"
            username_info += "Use this to personalize responses and address the user appropriately.\n"
            username_info += "</user_info>"
            context_parts.append(username_info)
        if subscription:
            sub = subscription
            tier_info = f"\n\n<user_subscription>\n"
            tier_info += f"Current plan: {sub['display_name']} ({sub['name']})\n"
            if sub['name'] in ('free', 'none'):
                tier_info += "Tier type: Free\n"
                tier_info += "Custom workers: 0 (upgrade to Plus or higher)\n"
                if config.ACTIVATE_MCPS_TRIG:
                    tier_info += "Scheduled triggers: 0 (upgrade to Plus or higher)\n"
                    tier_info += "App triggers: 0 (upgrade to Plus or higher)\n"
                tier_info += "Concurrent runs: 1\n"
                tier_info += "Credit purchases: Not available (upgrade to Ultra)\n"
            else:
                tier_info += "Tier type: Paid\n"
                tier_info += f"Custom workers limit: {sub['custom_workers_limit']}\n"
                if config.ACTIVATE_MCPS_TRIG:
                    tier_info += f"Scheduled triggers limit: {sub['scheduled_triggers_limit']}\n"
                    tier_info += f"App triggers limit: {sub['app_triggers_limit']}\n"
                tier_info += f"Concurrent runs: {sub['concurrent_runs']}\n"
                tier_info += f"Credit purchases: {'Available' if sub['can_purchase_credits'] else 'Not available (Ultra only)'}\n"
            tier_info += "</user_subscription>"
            context_parts.append(tier_info)

        context_str = ''.join(context_parts) if context_parts else ""
        await set_cached_user_context(account_id, context_str)
        logger.debug(f"⚡ Pre-warmed user context for {account_id} (subscription={'yes' if subscription else 'no'})")
    except Exception as e:
        logger.debug(f"⚠️ Failed to pre-warm user context: {e}")


async def prewarm_credit_balance(account_id: str) -> None:
    try:
        from core.billing.credits.manager import credit_manager
        await credit_manager.get_balance(account_id)
        logger.debug(f"⚡ Pre-warmed credit balance for {account_id}")
    except Exception as e:
        logger.debug(f"⚠️ Failed to pre-warm credit balance: {e}")


async def create_new_thread_records(
    project_id: str,
    thread_id: str,
    account_id: str,
    prompt: str,
    agent_run_id: str,
    message_content: str,
    agent_config: dict,
    metadata: Optional[Dict[str, Any]],
    memory_enabled: Optional[bool],
) -> None:
    from core.threads import repo as threads_repo
    from core.cache.runtime_cache import delete_pending_thread, increment_thread_count_cache
    from core.utils.project_helpers import generate_and_update_project_name
    from core.utils.thread_name_generator import generate_and_update_thread_name
    
    placeholder_name = f"{prompt[:30]}..." if len(prompt) > 30 else prompt
    
    await threads_repo.create_thread_with_message_and_run(
        project_id=project_id,
        thread_id=thread_id,
        account_id=account_id,
        project_name=placeholder_name,
        thread_name="New Chat",
        agent_run_id=agent_run_id,
        message_content=message_content,
        agent_id=agent_config.get("agent_id"),
        agent_version_id=agent_config.get("agent_version_id"),
        run_metadata=metadata,
        memory_enabled=memory_enabled
    )
    
    await delete_pending_thread(thread_id)
    
    asyncio.create_task(generate_and_update_project_name(project_id=project_id, prompt=prompt))
    if prompt:
        asyncio.create_task(generate_and_update_thread_name(thread_id=thread_id, prompt=prompt))
    asyncio.create_task(increment_thread_count_cache(account_id))


async def create_agent_run_record(
    agent_run_id: str,
    thread_id: str,
    agent_config: Optional[dict],
    effective_model: str,
    account_id: str,
    extra_metadata: Optional[Dict[str, Any]] = None
) -> str:
    from core.agents import repo as agents_repo
    from core.utils.instance import get_instance_id
    
    run_metadata = {
        "model_name": effective_model,
        "actual_user_id": account_id,
        "instance_id": get_instance_id()
    }
    if extra_metadata:
        run_metadata.update(extra_metadata)
    if agent_config:
        run_metadata["agent_config"] = {
            k: v for k, v in agent_config.items()
            if k not in ("system_prompt",)
        }
    
    await agents_repo.create_agent_run_with_id(
        agent_run_id=agent_run_id,
        thread_id=thread_id,
        agent_id=agent_config.get("agent_id") if agent_config else None,
        agent_version_id=agent_config.get("agent_version_id") if agent_config else None,
        metadata=run_metadata
    )
    return agent_run_id


async def create_image_messages(
    thread_id: str,
    image_contexts: List[Dict[str, Any]],
) -> None:
    from core.threads import repo as threads_repo
    
    if not image_contexts:
        return
    
    await threads_repo.set_thread_has_images(thread_id)
    
    for img in image_contexts:
        try:
            await threads_repo.create_message_full(
                message_id=str(uuid.uuid4()),
                thread_id=thread_id,
                message_type="image_context",
                content={
                    "role": "user",
                    "content": [
                        {"type": "text", "text": f"[Image: {img['filename']}]"},
                        {"type": "image_url", "image_url": {"url": img['url']}}
                    ]
                },
                is_llm_message=True,
                metadata={
                    "file_path": img['filename'],
                    "mime_type": img['mime_type'],
                    "source": "user_upload"
                }
            )
        except Exception:
            pass


async def check_concurrent_runs_limit(
    account_id: str,
    agent_run_id: str,
    cancellation_event: asyncio.Event,
    skip_check: bool = False,
) -> None:
    if skip_check or config.ENV_MODE == EnvMode.LOCAL:
        return
    
    try:
        from core.agents.api import _check_concurrent_runs_limit
        await _check_concurrent_runs_limit(account_id)
    except Exception as e:
        from fastapi import HTTPException
        if isinstance(e, HTTPException):
            logger.warning(f"⚠️ Concurrent runs limit exceeded for {account_id}")
            error_detail = e.detail if isinstance(e.detail, dict) else {"message": str(e.detail)}
            error_msg = {
                "type": "error",
                "error": error_detail.get("message", "Concurrent runs limit exceeded"),
                "error_code": error_detail.get("error_code", "AGENT_RUN_LIMIT_EXCEEDED"),
                "agent_run_id": agent_run_id
            }
            stream_key = f"agent_run:{agent_run_id}:stream"
            await redis.stream_add(stream_key, {"data": json.dumps(error_msg)}, maxlen=50)
            cancellation_event.set()


async def notify_setup_error(
    agent_run_id: str,
    error: Exception,
) -> None:
    stream_key = f"agent_run:{agent_run_id}:stream"
    
    try:
        error_msg = {
            "type": "error",
            "error": f"Failed to start agent: {str(error)[:200]}",
            "agent_run_id": agent_run_id
        }
        await redis.stream_add(stream_key, {"data": json.dumps(error_msg)}, maxlen=50)
        
        status_msg = {
            "type": "status",
            "status": "failed",
            "message": f"Setup failed: {str(error)[:100]}"
        }
        await redis.stream_add(stream_key, {"data": json.dumps(status_msg)}, maxlen=50)
    except Exception:
        pass
    
    try:
        from core.agents import repo as agents_repo
        await agents_repo.update_agent_run_status(agent_run_id, "failed")
    except Exception:
        pass