|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from fastapi import APIRouter, HTTPException |
| 7 | +from pydantic import BaseModel |
| 8 | + |
| 9 | +from ..extensions import ExtensionRegistry, Extension, ExtensionCapability, getExtensionRegistry |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class RegisterExtensionRequest(BaseModel): |
| 15 | + name: str |
| 16 | + version: str = "0.0.0" |
| 17 | + description: str = "" |
| 18 | + author: str = "" |
| 19 | + capabilities: list[str] = [] |
| 20 | + entryPoint: str = "" |
| 21 | + config: dict[str, Any] = {} |
| 22 | + |
| 23 | + |
| 24 | +def createExtensionRouter(state: Any) -> APIRouter: |
| 25 | + router = APIRouter() |
| 26 | + |
| 27 | + @router.get("/api/extensions") |
| 28 | + def apiListExtensions(): |
| 29 | + registry = getExtensionRegistry() |
| 30 | + return { |
| 31 | + "extensions": [e.serialize() for e in registry.listExtensions()], |
| 32 | + "total": registry.count, |
| 33 | + } |
| 34 | + |
| 35 | + @router.post("/api/extensions") |
| 36 | + def apiRegisterExtension(req: RegisterExtensionRequest): |
| 37 | + registry = getExtensionRegistry() |
| 38 | + caps = tuple( |
| 39 | + ExtensionCapability(c) for c in req.capabilities |
| 40 | + if c in ExtensionCapability._value2member_map_ |
| 41 | + ) |
| 42 | + extension = Extension( |
| 43 | + name=req.name, |
| 44 | + version=req.version, |
| 45 | + description=req.description, |
| 46 | + author=req.author, |
| 47 | + capabilities=caps, |
| 48 | + entryPoint=req.entryPoint, |
| 49 | + config=req.config, |
| 50 | + ) |
| 51 | + registry.register(extension) |
| 52 | + return extension.serialize() |
| 53 | + |
| 54 | + @router.get("/api/extensions/{extensionId}") |
| 55 | + def apiGetExtension(extensionId: str): |
| 56 | + registry = getExtensionRegistry() |
| 57 | + ext = registry.get(extensionId) |
| 58 | + if ext is None: |
| 59 | + raise HTTPException(status_code=404, detail="Extension not found") |
| 60 | + return ext.serialize() |
| 61 | + |
| 62 | + @router.delete("/api/extensions/{extensionId}") |
| 63 | + def apiUnregisterExtension(extensionId: str): |
| 64 | + registry = getExtensionRegistry() |
| 65 | + removed = registry.unregister(extensionId) |
| 66 | + if not removed: |
| 67 | + raise HTTPException(status_code=404, detail="Extension not found") |
| 68 | + return {"ok": True} |
| 69 | + |
| 70 | + @router.get("/api/extensions/capability/{capability}") |
| 71 | + def apiExtensionsByCapability(capability: str): |
| 72 | + registry = getExtensionRegistry() |
| 73 | + try: |
| 74 | + cap = ExtensionCapability(capability) |
| 75 | + except ValueError: |
| 76 | + raise HTTPException(status_code=400, detail=f"Unknown capability: {capability}") |
| 77 | + extensions = registry.listByCapability(cap) |
| 78 | + return {"extensions": [e.serialize() for e in extensions]} |
| 79 | + |
| 80 | + return router |
0 commit comments