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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54ec9cb
 
 
 
 
 
 
 
 
 
 
942fc42
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Plugin management API routes."""

from typing import Any

from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, Field

from app.plugins.registry import (
    get_all_plugins,
    get_all_tools,
    get_plugin,
    get_plugin_summary,
    get_tool,
    get_tools_by_category,
    PluginCategory,
)

router = APIRouter(prefix="/plugins", tags=["plugins"])

# Plugin registry - available plugins
PLUGIN_REGISTRY = {
    # API Providers
    "apis": [
        {
            "id": "openai-api",
            "name": "OpenAI API",
            "category": "apis",
            "description": "GPT-4, GPT-4o, and embedding models",
            "version": "1.0.0",
            "size": "50KB",
            "installed": False,
            "requires_key": True,
        },
        {
            "id": "anthropic-api",
            "name": "Anthropic API",
            "category": "apis",
            "description": "Claude 3.5 Sonnet and Opus models",
            "version": "1.0.0",
            "size": "45KB",
            "installed": False,
            "requires_key": True,
        },
        {
            "id": "google-api",
            "name": "Google Gemini API",
            "category": "apis",
            "description": "Gemini Pro and Flash models",
            "version": "1.0.0",
            "size": "48KB",
            "installed": True,  # Pre-installed
            "requires_key": True,
        },
        {
            "id": "groq-api",
            "name": "Groq API",
            "category": "apis",
            "description": "Fast inference for open models",
            "version": "1.0.0",
            "size": "40KB",
            "installed": True,  # Pre-installed
            "requires_key": True,
        },
        {
            "id": "nvidia-api",
            "name": "NVIDIA API",
            "category": "apis",
            "description": "DeepSeek, Nemotron, and Llama models via NVIDIA",
            "version": "1.0.0",
            "size": "44KB",
            "installed": True,  # Pre-installed
            "requires_key": True,
        },
        {
            "id": "ollama-api",
            "name": "Ollama (Local)",
            "category": "apis",
            "description": "Run models locally with Ollama",
            "version": "1.0.0",
            "size": "35KB",
            "installed": False,
            "requires_key": False,
        },
    ],
    # MCP Tools
    "mcps": [
        {
            "id": "mcp-browser",
            "name": "Browser Tools",
            "category": "mcps",
            "description": "Playwright-based browser automation",
            "version": "1.0.0",
            "size": "120KB",
            "installed": True,
            "requires_key": False,
        },
        {
            "id": "mcp-search",
            "name": "Search Tools",
            "category": "mcps",
            "description": "Google, Bing, DuckDuckGo search",
            "version": "1.0.0",
            "size": "80KB",
            "installed": True,
            "requires_key": False,
        },
        {
            "id": "mcp-html",
            "name": "HTML Processing",
            "category": "mcps",
            "description": "Parse, extract, and transform HTML",
            "version": "1.0.0",
            "size": "60KB",
            "installed": True,
            "requires_key": False,
        },
        {
            "id": "mcp-python-sandbox",
            "name": "Python Sandbox Executor",
            "category": "mcps",
            "description": "Run sandboxed Python analysis for datasets and pages",
            "version": "1.0.0",
            "size": "95KB",
            "installed": True,
            "requires_key": False,
        },
        {
            "id": "mcp-screenshot",
            "name": "Screenshot Tools",
            "category": "mcps",
            "description": "Capture and analyze page screenshots",
            "version": "1.0.0",
            "size": "90KB",
            "installed": False,
            "requires_key": False,
        },
        {
            "id": "mcp-pdf",
            "name": "PDF Tools",
            "category": "mcps",
            "description": "Extract data from PDF documents",
            "version": "1.0.0",
            "size": "150KB",
            "installed": False,
            "requires_key": False,
        },
        {
            "id": "mcp-database",
            "name": "Database Tools",
            "category": "mcps",
            "description": "Connect to SQL/NoSQL databases",
            "version": "1.0.0",
            "size": "100KB",
            "installed": False,
            "requires_key": False,
        },
    ],
    # Data Processors
    "processors": [
        {
            "id": "proc-json",
            "name": "JSON Processor",
            "category": "processors",
            "description": "Parse and transform JSON data",
            "version": "1.0.0",
            "size": "25KB",
            "installed": True,
            "requires_key": False,
        },
        {
            "id": "proc-csv",
            "name": "CSV Processor",
            "category": "processors",
            "description": "Parse and export CSV files",
            "version": "1.0.0",
            "size": "30KB",
            "installed": True,
            "requires_key": False,
        },
        {
            "id": "proc-python",
            "name": "Python Analysis Processor",
            "category": "processors",
            "description": "Execute safe Python transformations on extracted data",
            "version": "1.0.0",
            "size": "55KB",
            "installed": True,
            "requires_key": False,
        },
        {
            "id": "proc-pandas",
            "name": "Pandas Processor",
            "category": "processors",
            "description": "Tabular analysis and aggregation with pandas",
            "version": "1.0.0",
            "size": "130KB",
            "installed": True,
            "requires_key": False,
        },
        {
            "id": "proc-numpy",
            "name": "NumPy Processor",
            "category": "processors",
            "description": "Numerical analysis and statistics with NumPy",
            "version": "1.0.0",
            "size": "90KB",
            "installed": True,
            "requires_key": False,
        },
        {
            "id": "proc-bs4",
            "name": "BeautifulSoup Processor",
            "category": "processors",
            "description": "Advanced HTML parsing and link/content analysis via bs4",
            "version": "1.0.0",
            "size": "45KB",
            "installed": True,
            "requires_key": False,
        },
        {
            "id": "python_sandbox",
            "name": "Python Sandbox",
            "category": "processors",
            "description": "Execute Python code in secure sandbox environment",
            "version": "1.0.0",
            "size": "85KB",
            "installed": True,
            "requires_key": False,
        },
        {
            "id": "proc-excel",
            "name": "Excel Processor",
            "category": "processors",
            "description": "Read/write Excel files",
            "version": "1.0.0",
            "size": "500KB",
            "installed": False,
            "requires_key": False,
        },
        {
            "id": "proc-xml",
            "name": "XML Processor",
            "category": "processors",
            "description": "Parse and transform XML data",
            "version": "1.0.0",
            "size": "40KB",
            "installed": False,
            "requires_key": False,
        },
    ],
}

# Track installed plugins (in-memory, would be persistent in production)
_installed_plugins: set[str] = {
    "google-api",
    "groq-api",
    "nvidia-api",
    "mcp-browser",
    "mcp-search",
    "mcp-html",
    "mcp-python-sandbox",
    "proc-json",
    "proc-csv",
    "proc-python",
    "proc-pandas",
    "proc-numpy",
    "proc-bs4",
}


class PluginAction(BaseModel):
    """Request to install/uninstall a plugin."""

    plugin_id: str = Field(..., description="Plugin identifier")


class PluginResponse(BaseModel):
    """Plugin information response."""

    id: str
    name: str
    category: str
    description: str
    version: str
    size: str
    installed: bool
    requires_key: bool


@router.get("")
@router.get("/")
async def list_plugins(category: str | None = None) -> dict[str, Any]:
    """List all available plugins, optionally filtered by category."""
    result = {}

    for cat, plugins in PLUGIN_REGISTRY.items():
        if category and cat != category:
            continue

        result[cat] = [
            {**plugin, "installed": plugin["id"] in _installed_plugins} for plugin in plugins
        ]

    # Calculate stats
    total = sum(len(plugins) for plugins in PLUGIN_REGISTRY.values())
    installed = len(_installed_plugins)

    return {
        "plugins": result,
        "categories": list(PLUGIN_REGISTRY.keys()),
        "stats": {
            "total": total,
            "installed": installed,
            "available": total - installed,
        },
    }


@router.get("/installed")
async def list_installed_plugins() -> dict[str, Any]:
    """List only installed plugins."""
    installed = []

    for plugins in PLUGIN_REGISTRY.values():
        for plugin in plugins:
            if plugin["id"] in _installed_plugins:
                installed.append({**plugin, "installed": True})

    return {
        "plugins": installed,
        "count": len(installed),
    }


@router.get("/categories")
async def get_categories() -> dict[str, Any]:
    """Get plugin categories with descriptions."""
    return {
        "categories": [
            {
                "id": "apis",
                "name": "API Providers",
                "description": "LLM and AI service providers",
                "icon": "πŸ”Œ",
            },
            {
                "id": "mcps",
                "name": "MCP Tools",
                "description": "Model Context Protocol tools",
                "icon": "πŸ”§",
            },
            {
                "id": "processors",
                "name": "Data Processors",
                "description": "Data transformation tools",
                "icon": "πŸ“Š",
            },
        ],
    }


# ==============================================================================
# Tool Registry Endpoints (must be before /{plugin_id} catch-all)
# ==============================================================================


@router.get("/tools")
async def list_tools(category: str | None = None) -> dict[str, Any]:
    """List all available tools from plugin registry."""
    if category:
        try:
            cat = PluginCategory(category)
            tools = get_tools_by_category(cat)
        except ValueError:
            tools = []
    else:
        tools = get_all_tools()

    return {
        "tools": [
            {
                "name": t.name,
                "description": t.description,
                "category": t.category.value,
                "parameters": t.parameters,
                "returns": t.returns,
            }
            for t in tools
        ],
        "count": len(tools),
    }


@router.get("/tools/{tool_name:path}")
async def get_tool_details(tool_name: str) -> dict[str, Any]:
    """Get details about a specific tool."""
    tool = get_tool(tool_name)
    if not tool:
        raise HTTPException(status_code=404, detail=f"Tool not found: {tool_name}")

    return {
        "name": tool.name,
        "description": tool.description,
        "category": tool.category.value,
        "parameters": tool.parameters,
        "returns": tool.returns,
        "examples": tool.examples,
    }


@router.get("/registry")
async def get_registry_endpoint() -> dict[str, Any]:
    """Get full plugin registry with all tools."""
    plugins = get_all_plugins()

    return {
        "plugins": [
            {
                "id": p.id,
                "name": p.name,
                "description": p.description,
                "category": p.category.value,
                "version": p.version,
                "enabled": p.enabled,
                "tools": [
                    {
                        "name": t.name,
                        "description": t.description,
                        "parameters": t.parameters,
                        "returns": t.returns,
                    }
                    for t in p.tools
                ],
                "tools_count": len(p.tools),
            }
            for p in plugins
        ],
        "summary": get_plugin_summary(),
    }


@router.get("/summary")
async def get_summary_endpoint() -> dict[str, Any]:
    """Get summary of plugins and tools."""
    return get_plugin_summary()


@router.get("/{plugin_id}")
async def get_plugin_by_id(plugin_id: str) -> PluginResponse:
    """Get details about a specific plugin."""
    for plugins in PLUGIN_REGISTRY.values():
        for plugin in plugins:
            if plugin["id"] == plugin_id:
                return PluginResponse(**{**plugin, "installed": plugin_id in _installed_plugins})

    raise HTTPException(status_code=404, detail=f"Plugin not found: {plugin_id}")


@router.post("/install")
async def install_plugin(action: PluginAction) -> dict[str, Any]:
    """Install a plugin."""
    plugin_id = action.plugin_id

    # Find the plugin
    plugin = None
    for plugins in PLUGIN_REGISTRY.values():
        for p in plugins:
            if p["id"] == plugin_id:
                plugin = p
                break

    if not plugin:
        raise HTTPException(status_code=404, detail=f"Plugin not found: {plugin_id}")

    if plugin_id in _installed_plugins:
        return {
            "status": "already_installed",
            "message": f"Plugin {plugin['name']} is already installed",
            "plugin": {**plugin, "installed": True},
        }

    # Install the plugin (in production, this would download/configure)
    _installed_plugins.add(plugin_id)

    return {
        "status": "success",
        "message": f"Plugin {plugin['name']} installed successfully",
        "plugin": {**plugin, "installed": True},
    }


@router.post("/uninstall")
async def uninstall_plugin(action: PluginAction) -> dict[str, Any]:
    """Uninstall a plugin."""
    plugin_id = action.plugin_id

    # Find the plugin
    plugin = None
    for plugins in PLUGIN_REGISTRY.values():
        for p in plugins:
            if p["id"] == plugin_id:
                plugin = p
                break

    if not plugin:
        raise HTTPException(status_code=404, detail=f"Plugin not found: {plugin_id}")

    if plugin_id not in _installed_plugins:
        return {
            "status": "not_installed",
            "message": f"Plugin {plugin['name']} is not installed",
            "plugin": {**plugin, "installed": False},
        }

    # Check if it's a core plugin
    core_plugins = {
        "mcp-browser",
        "mcp-search",
        "mcp-html",
        "mcp-python-sandbox",
        "proc-json",
        "proc-python",
        "proc-pandas",
        "proc-numpy",
        "proc-bs4",
    }
    if plugin_id in core_plugins:
        raise HTTPException(
            status_code=400,
            detail=f"Cannot uninstall core plugin: {plugin['name']}",
        )

    # Uninstall the plugin
    _installed_plugins.discard(plugin_id)

    return {
        "status": "success",
        "message": f"Plugin {plugin['name']} uninstalled successfully",
        "plugin": {**plugin, "installed": False},
    }