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
 
 
 
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
#!/usr/bin/env python3
"""
SQLite Agent Template Management Script
Similar to manage_templates.py but optimized for local SQLite development.
Reads agents from agents_config.json and manages SQLite database.
"""

import sys
import os
import json
import requests
from datetime import datetime, UTC
from pathlib import Path

# Add the project root to the Python path
script_dir = os.path.dirname(os.path.abspath(__file__))
backend_dir = os.path.dirname(script_dir)
project_root = os.path.dirname(os.path.dirname(backend_dir))

# Change to backend directory to ensure proper path resolution
os.chdir(backend_dir)
sys.path.append(backend_dir)

from src.db.init_db import session_factory, DATABASE_URL
from src.db.schemas.models import AgentTemplate
from sqlalchemy.exc import IntegrityError

def get_database_type():
    """Detect database type from DATABASE_URL"""
    if DATABASE_URL.startswith('postgresql'):
        return "postgresql"
    elif DATABASE_URL.startswith('sqlite'):
        return "sqlite"
    else:
        return "unknown"

def load_agents_config():
    """Load agents configuration from agents_config.json"""
    # Try multiple possible locations for agents_config.json
    possible_paths = [
        os.path.join(backend_dir, 'agents_config.json'),  # Backend directory (copied file)
        os.path.join(project_root, 'agents_config.json'),  # Project root
        '/app/agents_config.json',  # Container root (HF Spaces)
        'agents_config.json'  # Current directory
    ]
    
    config_path = None
    for path in possible_paths:
        if os.path.exists(path):
            config_path = path
            print(f"πŸ“– Found agents_config.json at: {config_path}")
            break
    
    if not config_path:
        paths_str = '\n  '.join(possible_paths)
        raise FileNotFoundError(f"agents_config.json not found in any of these locations:\n  {paths_str}")
    
    with open(config_path, 'r', encoding='utf-8') as f:
        config = json.load(f)
    
    return config.get('templates', [])

def download_icon(icon_url, template_name):
    """Download icon from URL and save to frontend directory"""
    if not icon_url or not icon_url.startswith('http'):
        print(f"⏭️  Skipping icon download for {template_name} (not a URL: {icon_url})")
        return icon_url
    
    try:
        # Determine frontend directory
        frontend_dir = os.path.join(project_root, 'Auto-Analyst-CS', 'auto-analyst-frontend')
        public_dir = os.path.join(frontend_dir, 'public')
        
        if not os.path.exists(public_dir):
            print(f"⚠️  Frontend public directory not found: {public_dir}")
            return icon_url
        
        # Parse the path from icon_url
        if '/icons/templates/' in icon_url:
            relative_path = icon_url.split('/icons/templates/')[-1]
            icon_dir = os.path.join(public_dir, 'icons', 'templates')
        else:
            # Fallback: use filename from URL
            filename = icon_url.split('/')[-1]
            if not filename.endswith(('.svg', '.png', '.jpg', '.jpeg')):
                filename += '.svg'
            relative_path = filename
            icon_dir = os.path.join(public_dir, 'icons', 'templates')
        
        # Create icon directory if it doesn't exist
        os.makedirs(icon_dir, exist_ok=True)
        
        # Download and save icon
        icon_path = os.path.join(icon_dir, relative_path)
        
        # Skip if already exists
        if os.path.exists(icon_path):
            print(f"πŸ“ Icon already exists: {relative_path}")
            return f"/icons/templates/{relative_path}"
        
        response = requests.get(icon_url, timeout=10)
        response.raise_for_status()
        
        with open(icon_path, 'wb') as f:
            f.write(response.content)
        
        print(f"πŸ“₯ Downloaded icon: {relative_path}")
        return f"/icons/templates/{relative_path}"
        
    except Exception as e:
        print(f"❌ Failed to download icon for {template_name}: {str(e)}")
        return icon_url

def sync_agents_from_config():
    """Synchronize agents from agents_config.json to SQLite database"""
    session = session_factory()
    db_type = get_database_type()
    
    if db_type != "sqlite":
        print(f"⚠️  This script is designed for SQLite, but detected {db_type}")
        print("Consider using manage_templates.py for PostgreSQL")
        return
    
    try:
        # Load configuration
        print(f"πŸ“– Loading agents from agents_config.json...")
        templates_config = load_agents_config()
        
        if not templates_config:
            print("❌ No templates found in agents_config.json")
            return
        
        # Track statistics
        created_count = 0
        updated_count = 0
        skipped_count = 0
        
        print(f"πŸ” Processing {len(templates_config)} templates for SQLite database")
        print(f"πŸ“‹ Database URL: {DATABASE_URL}")
        
        # Group templates by category for display
        categories = {}
        for template_data in templates_config:
            category = template_data.get('category', 'Uncategorized')
            if category not in categories:
                categories[category] = []
            categories[category].append(template_data)
        
        # Process templates by category
        for category, templates in categories.items():
            print(f"\nπŸ“ {category}:")
            
            for template_data in templates:
                template_name = template_data["template_name"]
                
                # Check if template already exists
                existing = session.query(AgentTemplate).filter(
                    AgentTemplate.template_name == template_name
                ).first()
                
                # Download icon if it's a URL
                icon_url = template_data.get("icon_url", "")
                if icon_url.startswith('http'):
                    icon_url = download_icon(icon_url, template_name)
                
                if existing:
                    # Update existing template
                    existing.display_name = template_data["display_name"]
                    existing.description = template_data["description"]
                    existing.icon_url = icon_url
                    existing.prompt_template = template_data["prompt_template"]
                    existing.category = template_data.get("category", "Uncategorized")
                    existing.is_premium_only = template_data.get("is_premium_only", False)
                    existing.is_active = template_data.get("is_active", True)
                    existing.variant_type = template_data.get("variant_type", "individual")
                    existing.base_agent = template_data.get("base_agent", template_name)
                    existing.updated_at = datetime.now(UTC)
                    
                    variant_icon = "πŸ€–" if template_data.get("variant_type") == "planner" else "πŸ‘€"
                    premium_icon = "πŸ”’" if template_data.get("is_premium_only") else "πŸ†“"
                    print(f"πŸ”„ Updated: {template_name} {variant_icon} {premium_icon}")
                    updated_count += 1
                else:
                    # Create new template
                    template = AgentTemplate(
                        template_name=template_name,
                        display_name=template_data["display_name"],
                        description=template_data["description"],
                        icon_url=icon_url,
                        prompt_template=template_data["prompt_template"],
                        category=template_data.get("category", "Uncategorized"),
                        is_premium_only=template_data.get("is_premium_only", False),
                        is_active=template_data.get("is_active", True),
                        variant_type=template_data.get("variant_type", "individual"),
                        base_agent=template_data.get("base_agent", template_name),
                        created_at=datetime.now(UTC),
                        updated_at=datetime.now(UTC)
                    )
                    
                    session.add(template)
                    variant_icon = "πŸ€–" if template_data.get("variant_type") == "planner" else "πŸ‘€"
                    premium_icon = "πŸ”’" if template_data.get("is_premium_only") else "πŸ†“"
                    print(f"βœ… Created: {template_name} {variant_icon} {premium_icon}")
                    created_count += 1
        
        # Handle removals if specified in config
        remove_list = []
        # Re-load the full config to check for removals
        try:
            full_config_path = None
            possible_paths = [
                os.path.join(backend_dir, 'agents_config.json'),
                os.path.join(project_root, 'agents_config.json'),
                '/app/agents_config.json',
                'agents_config.json'
            ]
            
            for path in possible_paths:
                if os.path.exists(path):
                    full_config_path = path
                    break
            
            if full_config_path:
                with open(full_config_path, 'r', encoding='utf-8') as f:
                    full_config = json.load(f)
                    if 'remove' in full_config:
                        remove_list = full_config['remove']
        except Exception as e:
            print(f"⚠️ Could not load removal list: {e}")
        
        # Remove templates marked for removal
        if remove_list:
            print(f"\nπŸ—‘οΈ --- Processing Removals ---")
            for template_name in remove_list:
                existing = session.query(AgentTemplate).filter(
                    AgentTemplate.template_name == template_name
                ).first()
                
                if existing:
                    session.delete(existing)
                    print(f"πŸ—‘οΈ Removed: {template_name}")
                else:
                    print(f"⏭️ Skipping removal: {template_name} (not found)")
        
        # Commit all changes
        session.commit()
        
        print(f"\nπŸ“Š --- Summary ---")
        print(f"βœ… Templates created: {created_count}")
        print(f"πŸ”„ Templates updated: {updated_count}")
        print(f"⏭️ Templates skipped: {skipped_count}")
        
        # Show total count in database
        total_count = session.query(AgentTemplate).count()
        free_count = session.query(AgentTemplate).filter(AgentTemplate.is_premium_only == False).count()
        premium_count = session.query(AgentTemplate).filter(AgentTemplate.is_premium_only == True).count()
        individual_count = session.query(AgentTemplate).filter(AgentTemplate.variant_type == 'individual').count()
        planner_count = session.query(AgentTemplate).filter(AgentTemplate.variant_type == 'planner').count()
        
        print(f"πŸ—„οΈ Total templates in database: {total_count}")
        print(f"πŸ†“ Free templates: {free_count}")
        print(f"πŸ”’ Premium templates: {premium_count}")
        print(f"πŸ‘€ Individual variants: {individual_count}")
        print(f"πŸ€– Planner variants: {planner_count}")
        
    except Exception as e:
        session.rollback()
        print(f"❌ Error syncing templates: {str(e)}")
        raise
    finally:
        session.close()

def list_templates():
    """List all existing templates in the database"""
    session = session_factory()
    
    try:
        templates = session.query(AgentTemplate).order_by(
            AgentTemplate.category, 
            AgentTemplate.is_premium_only, 
            AgentTemplate.template_name
        ).all()
        
        if not templates:
            print("No templates found in database.")
            return
        
        print(f"\n--- Existing Templates ({len(templates)} total) ---")
        
        current_category = None
        for template in templates:
            if template.category != current_category:
                current_category = template.category
                print(f"\nπŸ“ {current_category}:")
            
            status = "πŸ”’ Premium" if template.is_premium_only else "πŸ†“ Free"
            active = "βœ… Active" if template.is_active else "❌ Inactive"
            variant = getattr(template, 'variant_type', 'individual')
            variant_icon = "πŸ€–" if variant == "planner" else "πŸ‘€"
            
            print(f"  β€’ {template.template_name} ({template.display_name})")
            print(f"    {status} - {active} - {variant_icon} {variant}")
            print(f"    πŸ“ {template.description}")
    
    except Exception as e:
        print(f"❌ Error listing templates: {str(e)}")
    finally:
        session.close()

def remove_all_templates():
    """Remove all templates from database (for testing)"""
    session = session_factory()
    
    try:
        deleted_count = session.query(AgentTemplate).delete()
        session.commit()
        print(f"πŸ—‘οΈ Removed {deleted_count} templates from database")
    
    except Exception as e:
        session.rollback()
        print(f"❌ Error removing templates: {str(e)}")
    finally:
        session.close()

def validate_config():
    """Validate the agents_config.json structure"""
    try:
        templates_config = load_agents_config()
        
        print(f"πŸ“‹ Validating agents_config.json...")
        print(f"βœ… Found {len(templates_config)} templates")
        
        # Check required fields
        required_fields = ['template_name', 'display_name', 'description', 'prompt_template']
        issues = []
        
        for i, template in enumerate(templates_config):
            for field in required_fields:
                if field not in template:
                    issues.append(f"Template {i}: Missing required field '{field}'")
        
        if issues:
            print(f"❌ Validation issues found:")
            for issue in issues:
                print(f"  β€’ {issue}")
        else:
            print(f"βœ… Configuration is valid")
            
        # Show summary by category
        categories = {}
        for template in templates_config:
            category = template.get('category', 'Uncategorized')
            if category not in categories:
                categories[category] = {'free': 0, 'premium': 0, 'individual': 0, 'planner': 0}
            
            if template.get('is_premium_only', False):
                categories[category]['premium'] += 1
            else:
                categories[category]['free'] += 1
                
            if template.get('variant_type', 'individual') == 'planner':
                categories[category]['planner'] += 1
            else:
                categories[category]['individual'] += 1
        
        print(f"\nπŸ“Š Summary by category:")
        for category, counts in categories.items():
            total = counts['free'] + counts['premium']
            print(f"  πŸ“ {category}: {total} templates")
            print(f"    πŸ†“ Free: {counts['free']} | πŸ”’ Premium: {counts['premium']}")
            print(f"    πŸ‘€ Individual: {counts['individual']} | πŸ€– Planner: {counts['planner']}")
    
    except Exception as e:
        print(f"❌ Error validating config: {str(e)}")

def create_minimal_templates():
    """Create a minimal set of essential templates for container environments"""
    session = session_factory()
    
    try:
        print("πŸ”§ Creating minimal template set...")
        
        # Define minimal essential templates
        minimal_templates = [
            {
                "template_name": "preprocessing_agent",
                "display_name": "Data Preprocessing Agent",
                "description": "Cleans and prepares DataFrame using Pandas and NumPy",
                "icon_url": "/icons/templates/preprocessing_agent.svg",
                "category": "Data Manipulation",
                "is_premium_only": False,
                "variant_type": "individual",
                "base_agent": "preprocessing_agent",
                "is_active": True,
                "prompt_template": "You are a preprocessing agent that cleans and prepares data using Pandas and NumPy. Handle missing values, detect column types, and convert date strings to datetime. Generate clean Python code for data preprocessing based on the user's analysis goals."
            },
            {
                "template_name": "data_viz_agent",
                "display_name": "Data Visualization Agent",
                "description": "Creates interactive visualizations using Plotly",
                "icon_url": "/icons/templates/data_viz_agent.svg",
                "category": "Data Visualization",
                "is_premium_only": False,
                "variant_type": "individual",
                "base_agent": "data_viz_agent",
                "is_active": True,
                "prompt_template": "You are a data visualization agent. Create interactive visualizations using Plotly based on user requirements. Generate appropriate chart types, apply styling, and ensure visualizations effectively communicate insights."
            },
            {
                "template_name": "sk_learn_agent",
                "display_name": "Machine Learning Agent",
                "description": "Trains ML models using scikit-learn",
                "icon_url": "/icons/templates/sk_learn_agent.svg",
                "category": "Data Modelling",
                "is_premium_only": False,
                "variant_type": "individual",
                "base_agent": "sk_learn_agent",
                "is_active": True,
                "prompt_template": "You are a machine learning agent. Use scikit-learn to train and evaluate ML models including classification, regression, and clustering. Provide feature importance insights and model performance metrics."
            }
        ]
        
        created_count = 0
        
        for template_data in minimal_templates:
            template_name = template_data["template_name"]
            
            # Check if template already exists
            existing = session.query(AgentTemplate).filter(
                AgentTemplate.template_name == template_name
            ).first()
            
            if not existing:
                template = AgentTemplate(
                    template_name=template_name,
                    display_name=template_data["display_name"],
                    description=template_data["description"],
                    icon_url=template_data["icon_url"],
                    prompt_template=template_data["prompt_template"],
                    category=template_data["category"],
                    is_premium_only=template_data["is_premium_only"],
                    is_active=template_data["is_active"],
                    variant_type=template_data["variant_type"],
                    base_agent=template_data["base_agent"],
                    created_at=datetime.now(UTC),
                    updated_at=datetime.now(UTC)
                )
                
                session.add(template)
                print(f"βœ… Created minimal template: {template_name}")
                created_count += 1
            else:
                print(f"⏭️ Template already exists: {template_name}")
        
        session.commit()
        print(f"πŸ“Š Created {created_count} minimal templates")
        
    except Exception as e:
        session.rollback()
        print(f"❌ Error creating minimal templates: {str(e)}")
        raise
    finally:
        session.close()

def populate_templates():
    """Legacy compatibility function for backward compatibility"""
    print("⚠️  Legacy populate_templates() called - checking for agents_config.json...")
    
    # Check if agents_config.json exists anywhere
    possible_paths = [
        os.path.join(backend_dir, 'agents_config.json'),
        os.path.join(project_root, 'agents_config.json'),
        '/app/agents_config.json',
        'agents_config.json'
    ]
    
    config_exists = any(os.path.exists(path) for path in possible_paths)
    
    if config_exists:
        print("πŸ“– Found agents_config.json - using sync_agents_from_config()")
        sync_agents_from_config()
    else:
        print("⚠️  agents_config.json not found - using fallback minimal templates")
        print("πŸ’‘ Creating essential templates for container environment")
        create_minimal_templates()

if __name__ == "__main__":
    import argparse
    
    parser = argparse.ArgumentParser(description="SQLite Agent Template Management")
    parser.add_argument("action", choices=["sync", "list", "remove-all", "validate"], 
                       help="Action to perform")
    
    args = parser.parse_args()
    
    if args.action == "sync":
        print("πŸš€ Synchronizing agents from agents_config.json to SQLite...")
        sync_agents_from_config()
    elif args.action == "list":
        list_templates()
    elif args.action == "validate":
        validate_config()
    elif args.action == "remove-all":
        confirm = input("⚠️ Are you sure you want to remove ALL templates? (yes/no): ")
        if confirm.lower() == "yes":
            remove_all_templates()
        else:
            print("Operation cancelled.")