File size: 26,651 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
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
"""
Unified agent loading and transformation.

This module consolidates all agent data loading logic into one place,
eliminating duplication across agent_crud, agent_service, and agent_runs.
"""
from typing import Dict, Any, Optional
from dataclasses import dataclass
from core.utils.logger import logger
from core.services.supabase import DBConnection


@dataclass
class AgentData:
    """
    Complete agent data including configuration.
    
    This is the single source of truth for agent representation.
    """
    # Core fields from agents table
    agent_id: str
    name: str
    description: Optional[str]
    account_id: str
    is_default: bool
    is_public: bool
    tags: list
    icon_name: Optional[str]
    icon_color: Optional[str]
    icon_background: Optional[str]
    created_at: str
    updated_at: str
    current_version_id: Optional[str]
    version_count: int
    metadata: Optional[Dict[str, Any]]
    
    # Configuration fields (from version or fallback)
    system_prompt: Optional[str] = None
    model: Optional[str] = None
    configured_mcps: Optional[list] = None
    custom_mcps: Optional[list] = None
    agentpress_tools: Optional[Dict[str, Any]] = None
    triggers: Optional[list] = None
    
    # Version info
    version_name: Optional[str] = None
    version_number: Optional[int] = None
    version_created_at: Optional[str] = None
    version_updated_at: Optional[str] = None
    version_created_by: Optional[str] = None
    
    # Metadata flags
    is_suna_default: bool = False
    centrally_managed: bool = False
    config_loaded: bool = False
    restrictions: Optional[Dict[str, Any]] = None
    
    def to_pydantic_model(self):
        """Convert to AgentResponse Pydantic model."""
        from core.api_models.agents import AgentResponse, AgentVersionResponse
        
        current_version = None
        if self.config_loaded and self.version_number is not None:
            current_version = AgentVersionResponse(
                version_id=self.current_version_id,
                agent_id=self.agent_id,
                version_number=self.version_number,
                version_name=self.version_name or 'v1',
                system_prompt=self.system_prompt or '',
                model=self.model,
                configured_mcps=self.configured_mcps or [],
                custom_mcps=self.custom_mcps or [],
                agentpress_tools=self.agentpress_tools or {},
                is_active=True,
                created_at=self.version_created_at or self.created_at,
                updated_at=self.version_updated_at or self.updated_at,
                created_by=self.version_created_by
            )
        
        return AgentResponse(
            agent_id=self.agent_id,
            name=self.name,
            description=self.description,
            system_prompt=self.system_prompt,
            model=self.model,
            configured_mcps=self.configured_mcps,
            custom_mcps=self.custom_mcps,
            agentpress_tools=self.agentpress_tools,
            is_default=self.is_default,
            is_public=self.is_public,
            tags=self.tags,
            icon_name=self.icon_name,
            icon_color=self.icon_color,
            icon_background=self.icon_background,
            created_at=self.created_at,
            updated_at=self.updated_at,
            current_version_id=self.current_version_id,
            version_count=self.version_count,
            current_version=current_version,
            metadata=self.metadata
        )
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary for API responses."""
        result = {
            "agent_id": self.agent_id,
            "name": self.name,
            "description": self.description,
            "account_id": self.account_id,
            "is_default": self.is_default,
            "is_public": self.is_public,
            "tags": self.tags,
            "icon_name": self.icon_name,
            "icon_color": self.icon_color,
            "icon_background": self.icon_background,
            "created_at": self.created_at,
            "updated_at": self.updated_at,
            "current_version_id": self.current_version_id,
            "version_count": self.version_count,
            "metadata": self.metadata,
        }
        
        # Only include config if loaded
        if self.config_loaded:
            result.update({
                "system_prompt": self.system_prompt,
                "model": self.model,
                "configured_mcps": self.configured_mcps,
                "custom_mcps": self.custom_mcps,
                "agentpress_tools": self.agentpress_tools,
                "triggers": self.triggers,
                "version_name": self.version_name,
                "is_suna_default": self.is_suna_default,
                "centrally_managed": self.centrally_managed,
                "restrictions": self.restrictions,
            })
            
            # Include version details if available
            if self.version_number is not None:
                result["current_version"] = {
                    "version_id": self.current_version_id,
                    "version_number": self.version_number,
                    "version_name": self.version_name,
                    "created_at": self.version_created_at,
                    "updated_at": self.version_updated_at,
                    "created_by": self.version_created_by,
                }
        else:
            # Indicate config not loaded
            result.update({
                "system_prompt": None,
                "configured_mcps": [],  # Must be list, not None for Pydantic
                "custom_mcps": [],      # Must be list, not None for Pydantic  
                "agentpress_tools": {}, # Must be dict, not None for Pydantic
            })
        
        return result


class AgentLoader:
    """
    Unified agent loading service.
    
    Handles all agent data loading with consistent behavior:
    - Single agent: loads full config
    - List operations: loads metadata only (fast)
    - Batch loading: efficient version fetching
    """
    
    def __init__(self, db: Optional[DBConnection] = None):
        self.db = db or DBConnection()
    
    async def load_agent(
        self, 
        agent_id: str, 
        user_id: str,
        load_config: bool = True,
        skip_cache: bool = False
    ) -> AgentData:
        """
        Load a single agent with full configuration.
        
        Args:
            agent_id: Agent ID to load
            user_id: User ID for authorization
            load_config: Whether to load full version configuration
            skip_cache: If True, bypass cache (for cache warm-up)
            
        Returns:
            AgentData with complete information
            
        Raises:
            ValueError: If agent not found or access denied
        """
        import time
        t_start = time.time()
        
        # Check cache first (if loading config and not skipping cache)
        if load_config and not skip_cache:
            from core.cache.runtime_cache import get_cached_agent_config
            cached = await get_cached_agent_config(agent_id)
            if cached:
                logger.debug(f"⚡ Using cached config for agent {agent_id} ({(time.time() - t_start)*1000:.1f}ms)")
                return self._dict_to_agent_data(cached)
        
        from core.agents import repo as agents_repo
        
        agent_row = await agents_repo.get_agent_by_id(agent_id)
        
        if not agent_row:
            raise ValueError(f"Agent {agent_id} not found")
        
        if agent_row['account_id'] != user_id and not agent_row.get('is_public', False):
            raise ValueError(f"Access denied to agent {agent_id}")
        
        # Create base AgentData
        agent_data = self._row_to_agent_data(agent_row)
        
        # Load configuration if requested
        if load_config and agent_row.get('current_version_id'):
            await self._load_agent_config(agent_data, user_id)
            
            # Cache the result
            from core.cache.runtime_cache import set_cached_agent_config
            await set_cached_agent_config(
                agent_id,
                agent_data.to_dict(),
                version_id=agent_row.get('current_version_id'),
                is_suna_default=agent_data.is_suna_default
            )
        
        logger.debug(f"⏱️ load_agent completed in {(time.time() - t_start)*1000:.1f}ms")
        return agent_data
    
    async def load_agents_list(
        self,
        agent_rows: list,
        load_config: bool = False
    ) -> list[AgentData]:
        """
        Load multiple agents efficiently.
        
        Args:
            agent_rows: List of agent database rows
            load_config: Whether to batch-load configurations
            
        Returns:
            List of AgentData objects
        """
        agents = [self._row_to_agent_data(row) for row in agent_rows]
        
        if load_config:
            await self._batch_load_configs(agents)
        
        return agents
    
    async def load_template(
        self,
        template_row: Dict[str, Any],
        fetch_creator_name: bool = False
    ) -> AgentData:
        """
        Load a template as AgentData.
        
        Templates are basically agents with pre-configured settings.
        
        Args:
            template_row: Template database row
            fetch_creator_name: Whether to fetch creator name
            
        Returns:
            AgentData representing the template
        """
        metadata = template_row.get('metadata', {}) or {}
        
        # Fetch creator name if requested (template marketplace operation)
        creator_name = None
        if fetch_creator_name and template_row.get('creator_id'):
            try:
                # Keep this on Supabase for template marketplace operations
                client = await self.db.client
                creator_result = await client.schema('basejump').from_('accounts').select(
                    'name, slug'
                ).eq('id', template_row['creator_id']).single().execute()
                if creator_result.data:
                    creator_name = creator_result.data.get('name') or creator_result.data.get('slug')
            except Exception as e:
                logger.warning(f"Failed to fetch creator name: {e}")
        
        # Update metadata
        metadata['is_template'] = True
        if creator_name:
            metadata['creator_name'] = creator_name
        
        # Create AgentData from template
        agent_data = AgentData(
            agent_id=template_row.get('template_id', ''),
            name=template_row.get('name', ''),
            description=template_row.get('description'),
            account_id=template_row.get('creator_id', ''),
            is_default=False,
            is_public=template_row.get('is_public', False),
            tags=template_row.get('tags', []),
            icon_name=template_row.get('icon_name'),
            icon_color=template_row.get('icon_color'),
            icon_background=template_row.get('icon_background'),
            created_at=template_row.get('created_at', ''),
            updated_at=template_row.get('updated_at'),
            current_version_id=None,
            version_count=0,
            metadata=metadata,
            # Template config is directly available
            system_prompt=template_row.get('system_prompt', ''),
            model=metadata.get('model'),
            configured_mcps=template_row.get('mcp_requirements', []),
            custom_mcps=[],
            agentpress_tools=template_row.get('agentpress_tools', {}),
            triggers=[],
            version_name='template',
            is_suna_default=False,
            centrally_managed=False,
            config_loaded=True,  # Templates have config built-in
            restrictions={}
        )
        
        return agent_data
    
    def _dict_to_agent_data(self, data: Dict[str, Any]) -> AgentData:
        """Convert cached dict back to AgentData."""
        current_version = data.get('current_version', {}) or {}
        
        return AgentData(
            agent_id=data['agent_id'],
            name=data['name'],
            description=data.get('description'),
            account_id=data['account_id'],
            is_default=data.get('is_default', False),
            is_public=data.get('is_public', False),
            tags=data.get('tags', []),
            icon_name=data.get('icon_name'),
            icon_color=data.get('icon_color'),
            icon_background=data.get('icon_background'),
            created_at=data.get('created_at', ''),
            updated_at=data.get('updated_at', ''),
            current_version_id=data.get('current_version_id'),
            version_count=data.get('version_count', 1),
            metadata=data.get('metadata', {}),
            system_prompt=data.get('system_prompt'),
            model=data.get('model'),
            configured_mcps=data.get('configured_mcps', []),
            custom_mcps=data.get('custom_mcps', []),
            agentpress_tools=data.get('agentpress_tools', {}),
            triggers=data.get('triggers', []),
            version_name=data.get('version_name') or current_version.get('version_name'),
            version_number=current_version.get('version_number'),
            version_created_at=current_version.get('created_at'),
            version_updated_at=current_version.get('updated_at'),
            version_created_by=current_version.get('created_by'),
            is_suna_default=data.get('is_suna_default', False),
            centrally_managed=data.get('centrally_managed', False),
            config_loaded=True,  # Cached data always has config
            restrictions=data.get('restrictions', {})
        )
    
    def _row_to_agent_data(self, row: Dict[str, Any]) -> AgentData:
        """Convert database row to AgentData.
        
        For Suna agents, always overrides name and description from SUNA_CONFIG
        regardless of what's stored in the database.
        """
        metadata = row.get('metadata', {}) or {}
        is_suna_default = metadata.get('is_suna_default', False)
        
        # For Suna agents, always use name from SUNA_CONFIG (never DB value)
        if is_suna_default:
            from core.config.suna_config import SUNA_CONFIG
            name = SUNA_CONFIG['name']
            description = SUNA_CONFIG.get('description')
        else:
            name = row['name']
            description = row.get('description')
        
        return AgentData(
            agent_id=row['agent_id'],
            name=name,
            description=description,
            account_id=row['account_id'],
            is_default=row.get('is_default', False),
            is_public=row.get('is_public', False),
            tags=row.get('tags', []),
            icon_name=row.get('icon_name'),
            icon_color=row.get('icon_color'),
            icon_background=row.get('icon_background'),
            created_at=row['created_at'],
            updated_at=row.get('updated_at', row['created_at']),
            current_version_id=row.get('current_version_id'),
            version_count=row.get('version_count', 1),
            metadata=metadata,
            is_suna_default=is_suna_default,
            config_loaded=False
        )
    
    async def _load_agent_config(self, agent: AgentData, user_id: str):
        """Load full configuration for a single agent."""
        if agent.is_suna_default:
            await self._load_suna_config(agent, user_id)
        else:
            await self._load_custom_config(agent, user_id)
        
        agent.config_loaded = True
    
    async def _load_suna_config(self, agent: AgentData, user_id: Optional[str] = None):
        """
        Load Suna config using static in-memory config + cached user MCPs.
        
        Static parts (prompt, model, tools) = instant from memory
        User MCPs = check cache first, then DB if miss
        Always overrides name from SUNA_CONFIG regardless of DB value.
        """
        import time
        t_start = time.time()
        
        # 1. Load static config from memory (instant, no DB)
        from core.cache.runtime_cache import get_static_suna_config, load_static_suna_config
        from core.config.suna_config import SUNA_CONFIG
        
        static_config = get_static_suna_config()
        if not static_config:
            static_config = load_static_suna_config()
        
        # Always override name from SUNA_CONFIG (never use DB value)
        agent.name = SUNA_CONFIG['name']
        agent.description = SUNA_CONFIG.get('description')
        agent.system_prompt = static_config['system_prompt']
        agent.model = static_config['model']
        agent.agentpress_tools = static_config['agentpress_tools']
        agent.centrally_managed = static_config['centrally_managed']
        agent.restrictions = static_config['restrictions']
        
        # 2. Load user-specific MCPs (check cache first)
        if agent.current_version_id and user_id:
            from core.cache.runtime_cache import get_cached_user_mcps, set_cached_user_mcps
            
            # Try cache first
            cached_mcps = await get_cached_user_mcps(agent.agent_id)
            if cached_mcps:
                agent.configured_mcps = cached_mcps.get('configured_mcps', [])
                agent.custom_mcps = cached_mcps.get('custom_mcps', [])
                agent.triggers = cached_mcps.get('triggers', [])
                logger.debug(f"⚡ Suna config loaded in {(time.time() - t_start)*1000:.1f}ms (MCPs from cache)")
                return
            
            # Cache miss - fetch from DB
            try:
                from core.versioning.version_service import get_version_service
                version_service = await get_version_service()
                
                version = await version_service.get_version(
                    agent_id=agent.agent_id,
                    version_id=agent.current_version_id,
                    user_id=user_id
                )
                
                version_dict = version.to_dict()
                
                if 'config' in version_dict and version_dict['config']:
                    config = version_dict['config']
                    tools = config.get('tools', {})
                    agent.configured_mcps = tools.get('mcp', [])
                    agent.custom_mcps = tools.get('custom_mcp', [])
                    agent.triggers = config.get('triggers', [])
                else:
                    agent.configured_mcps = version_dict.get('configured_mcps', [])
                    agent.custom_mcps = version_dict.get('custom_mcps', [])
                    agent.triggers = []
                
                # Cache for next time
                await set_cached_user_mcps(
                    agent.agent_id,
                    agent.configured_mcps,
                    agent.custom_mcps,
                    agent.triggers
                )
                
                logger.debug(f"Suna config loaded in {(time.time() - t_start)*1000:.1f}ms (MCPs from DB, now cached)")
            except Exception as e:
                logger.warning(f"Failed to load MCPs for Suna agent {agent.agent_id}: {e}")
                agent.configured_mcps = []
                agent.custom_mcps = []
                agent.triggers = []
        else:
            agent.configured_mcps = []
            agent.custom_mcps = []
            agent.triggers = []
            logger.debug(f"⚡ Suna config loaded in {(time.time() - t_start)*1000:.1f}ms (no MCPs)")
    
    async def _load_custom_config(self, agent: AgentData, user_id: str):
        """Load custom agent configuration from version."""
        if not agent.current_version_id:
            self._load_fallback_config(agent)
            return
        
        try:
            from core.versioning.version_service import get_version_service
            version_service = await get_version_service()
            
            version = await version_service.get_version(
                agent_id=agent.agent_id,
                version_id=agent.current_version_id,
                user_id=user_id
            )
            
            version_dict = version.to_dict()
            
            # Extract from new config format
            if 'config' in version_dict and version_dict['config']:
                config = version_dict['config']
                tools = config.get('tools', {})
                
                agent.system_prompt = config.get('system_prompt', '')
                agent.model = config.get('model')
                agent.configured_mcps = tools.get('mcp', [])
                agent.custom_mcps = tools.get('custom_mcp', [])
                
                from core.config.config_helper import _extract_agentpress_tools_for_run
                agent.agentpress_tools = _extract_agentpress_tools_for_run(tools.get('agentpress', {}))
                
                agent.triggers = config.get('triggers', [])
            else:
                # Old format compatibility
                agent.system_prompt = version_dict.get('system_prompt', '')
                agent.model = version_dict.get('model')
                agent.configured_mcps = version_dict.get('configured_mcps', [])
                agent.custom_mcps = version_dict.get('custom_mcps', [])
                
                from core.config.config_helper import _extract_agentpress_tools_for_run
                agent.agentpress_tools = _extract_agentpress_tools_for_run(
                    version_dict.get('agentpress_tools', {})
                )
                
                agent.triggers = []
            
            agent.version_name = version_dict.get('version_name', 'v1')
            agent.version_number = version_dict.get('version_number')
            agent.version_created_at = version_dict.get('created_at')
            agent.version_updated_at = version_dict.get('updated_at')
            agent.version_created_by = version_dict.get('created_by')
            agent.restrictions = {}
            
        except Exception as e:
            logger.warning(f"Failed to load version for agent {agent.agent_id}: {e}")
            self._load_fallback_config(agent)
    
    def _load_fallback_config(self, agent: AgentData):
        """Load safe fallback configuration."""
        from core.config.config_helper import _get_default_agentpress_tools, _extract_agentpress_tools_for_run
        
        agent.system_prompt = 'You are a helpful AI assistant.'
        agent.model = None
        agent.configured_mcps = []
        agent.custom_mcps = []
        agent.agentpress_tools = _extract_agentpress_tools_for_run(_get_default_agentpress_tools())
        agent.triggers = []
        agent.version_name = 'v1'
        agent.restrictions = {}
    
    async def _batch_load_configs(self, agents: list[AgentData]):
        """Batch load configurations for multiple agents."""
        
        # Get all version IDs for non-Suna agents
        version_ids = [a.current_version_id for a in agents if a.current_version_id and not a.is_suna_default]
        
        if not version_ids:
            # Only Suna agents, load their configs
            for agent in agents:
                if agent.is_suna_default:
                    await self._load_suna_config(agent, agent.account_id)
                    agent.config_loaded = True
            return
        
        try:
            # Use versioning service instead of direct config access
            from core.versioning.version_service import get_version_service
            version_service = await get_version_service()
            
            # Create version map using versioning service
            version_map = {}
            for agent in agents:
                if agent.current_version_id and not agent.is_suna_default:
                    try:
                        version = await version_service.get_version(
                            agent_id=agent.agent_id,
                            version_id=agent.current_version_id,
                            user_id=agent.account_id
                        )
                        if version:
                            version_map[agent.agent_id] = version.to_dict()
                    except Exception as e:
                        logger.warning(f"Failed to load version {agent.current_version_id} for agent {agent.agent_id}: {e}")
                        continue
            
            # Apply configs
            for agent in agents:
                if agent.is_suna_default:
                    await self._load_suna_config(agent, agent.account_id)
                    agent.config_loaded = True
                elif agent.agent_id in version_map:
                    self._apply_version_config(agent, version_map[agent.agent_id])
                    agent.config_loaded = True
                # else: leave config_loaded = False
                
        except Exception as e:
            logger.warning(f"Failed to batch load agent configs: {e}")
            # Fallback: load Suna configs only
            for agent in agents:
                if agent.is_suna_default:
                    await self._load_suna_config(agent, agent.account_id)
                    agent.config_loaded = True
    
    def _apply_version_config(self, agent: AgentData, version_row: Dict[str, Any]):
        """Apply version configuration to agent."""
        config = version_row.get('config') or {}
        tools = config.get('tools', {})
        
        from core.config.config_helper import _extract_agentpress_tools_for_run
        
        agent.system_prompt = config.get('system_prompt', '')
        agent.model = config.get('model')
        agent.configured_mcps = tools.get('mcp', [])
        agent.custom_mcps = tools.get('custom_mcp', [])
        agent.agentpress_tools = _extract_agentpress_tools_for_run(tools.get('agentpress', {}))
        agent.triggers = config.get('triggers', [])
        agent.version_name = version_row.get('version_name', 'v1')
        agent.version_number = version_row.get('version_number')
        agent.restrictions = {}


# Singleton instance
_loader = None

async def get_agent_loader() -> AgentLoader:
    """Get the global agent loader instance."""
    global _loader
    if _loader is None:
        _loader = AgentLoader()
    return _loader