# api/component_api.py from typing import Dict, Any, List, Optional from fastapi import APIRouter, HTTPException from pydantic import BaseModel from services.component_service import ComponentService from config.component_config import ( ComponentDiscovery, ComponentCategory ) from config.component_detail_config import ComponentDetailConfig from config.api_config import ( ApiResponseCode, ApiResponseMessage ) from api.models import ApiResponse, ComponentDiscoverInput # 创建路由器 router = APIRouter(prefix="/component", tags=["组件管理"]) # 创建服务实例 component_service = ComponentService() @router.get("/components", response_model=ApiResponse) async def get_components(): """获取所有组件类型及其详细信息""" try: # 获取所有组件类型 component_types = component_service.get_all_component_types() # 获取组件类型中文名称映射 component_type_names = ComponentDetailConfig.get_component_type_names() # 构建组件类型信息,包含英文标识和中文名称 component_type_info = [] for component_type in component_types: # 获取组件类型的中文名称,如果没有则使用英文标识 type_name = component_type_names.get(component_type, component_type) component_type_info.append({ "type": component_type, # 英文标识 "name": type_name, # 中文名称 }) # 按类别分组 component_categories = {} for component_type in component_types: # 根据组件类型推断类别 category = ComponentCategory.BASE # 默认类别 # 使用配置文件中的映射关系确定组件类别 mapping = ComponentCategory.get_mapping() for prefix, cat in mapping.items(): if component_type.startswith(prefix): category = cat break if category not in component_categories: component_categories[category] = [] component_categories[category].append(component_type) # 获取所有组件的详细配置 component_details = ComponentDetailConfig.get_all_components() # 按组件类型分组 component_details_by_type = {} for component in component_details: component_type = component["type"] if component_type not in component_details_by_type: component_details_by_type[component_type] = [] component_details_by_type[component_type].append(component) return { "code": ApiResponseCode.SUCCESS, "message": ApiResponseMessage.Component.FETCHED, "data": { "component_types": component_types, "component_type_info": component_type_info, # 新增:包含中文名称的组件类型信息 "component_categories": component_categories, "component_details": component_details, "component_details_by_type": component_details_by_type } } except Exception as e: raise HTTPException( status_code=ApiResponseCode.SERVER_ERROR, detail=f"获取组件类型失败: {str(e)}" ) @router.get("/components/{component_type}", response_model=ApiResponse) async def get_components_by_type(component_type: str): """获取指定类型的组件详细信息""" try: # 获取指定类型的组件详细配置 components = ComponentDetailConfig.get_components_by_type(component_type) if not components: return { "code": ApiResponseCode.NOT_FOUND, "message": f"未找到类型为 {component_type} 的组件", "data": {"components": []} } return { "code": ApiResponseCode.SUCCESS, "message": ApiResponseMessage.Component.FETCHED, "data": {"components": components} } except Exception as e: raise HTTPException( status_code=ApiResponseCode.SERVER_ERROR, detail=f"获取组件详细信息失败: {str(e)}" ) @router.post("/components/discover", response_model=ApiResponse) async def discover_components(discover_input: ComponentDiscoverInput): """自动发现并注册组件""" try: # 自动发现并注册组件 component_service.auto_discover_components(discover_input.package_name) # 获取所有组件类型 component_types = component_service.get_all_component_types() return { "code": ApiResponseCode.SUCCESS, "message": f"{ApiResponseMessage.Component.DISCOVERED},共发现 {len(component_types)} 个组件", "data": {"component_types": component_types} } except Exception as e: raise HTTPException( status_code=ApiResponseCode.SERVER_ERROR, detail=f"自动发现并注册组件失败: {str(e)}" )