message / plugins /welcome /api.py
hunian
refactor(plugins): 插件短名并统一 MCP tool 为 {plugin}-{tool}
cc826a1
Raw
History Blame Contribute Delete
1.29 kB
from fastapi import APIRouter, HTTPException
router = APIRouter()
# 在运行时获取插件实例
plugin = None
def set_plugin_instance(plugin_instance):
global plugin
plugin = plugin_instance
@router.get("/")
async def api_root():
return {"message": "欢迎插件API", "status": "运行中"}
@router.get("/welcome")
async def get_welcome(name: str = "用户"):
"""获取欢迎消息"""
if plugin is None or not plugin.enabled:
raise HTTPException(status_code=400, detail="插件未启用")
message = plugin.get_welcome_message(name)
return {"message": message}
@router.get("/status")
async def get_status():
"""获取插件状态"""
if plugin is None:
return {"name": "welcome", "enabled": False, "message": "插件未加载"}
return plugin.get_status()
@router.post("/enable")
async def enable_plugin():
"""启用插件"""
if plugin is None:
raise HTTPException(status_code=400, detail="插件未加载")
plugin.on_enable()
return {"message": "插件已启用"}
@router.post("/disable")
async def disable_plugin():
"""禁用插件"""
if plugin is None:
raise HTTPException(status_code=400, detail="插件未加载")
plugin.on_disable()
return {"message": "插件已禁用"}