|
| 1 | +"""社区插件安装与可选重启。""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from src.console.cli.bot_process import bot_lifecycle_available, schedule_bot_restart |
| 6 | +from src.console.webui.community_plugin_install import ( |
| 7 | + CommunityPluginInstallError, |
| 8 | + install_community_plugin, |
| 9 | + uninstall_community_plugin, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def append_restart_note(message: str, *, scheduled: bool) -> str: |
| 14 | + base = (message or "").strip() |
| 15 | + if scheduled: |
| 16 | + suffix = "已安排 Bot 重启。" |
| 17 | + return f"{base} {suffix}".strip() if base else suffix |
| 18 | + if base: |
| 19 | + return base |
| 20 | + return "请重启 Bot 后生效。" |
| 21 | + |
| 22 | + |
| 23 | +async def install_community_plugin_with_options( |
| 24 | + plugin_id: str, |
| 25 | + *, |
| 26 | + repository_url: str, |
| 27 | + ref: str = "main", |
| 28 | + restart: bool = False, |
| 29 | +) -> dict[str, str | bool]: |
| 30 | + result = await install_community_plugin( |
| 31 | + plugin_id, |
| 32 | + repository_url=repository_url, |
| 33 | + ref=ref, |
| 34 | + ) |
| 35 | + scheduled = False |
| 36 | + if restart and bot_lifecycle_available(): |
| 37 | + scheduled = schedule_bot_restart() |
| 38 | + result = dict(result) |
| 39 | + result["restart_scheduled"] = scheduled |
| 40 | + if restart or result.get("needs_restart"): |
| 41 | + result["message"] = append_restart_note(str(result.get("message") or ""), scheduled=scheduled) |
| 42 | + return result |
| 43 | + |
| 44 | + |
| 45 | +async def uninstall_community_plugin_with_options( |
| 46 | + plugin_id: str, |
| 47 | + *, |
| 48 | + restart: bool = False, |
| 49 | +) -> dict[str, str | bool]: |
| 50 | + result = await uninstall_community_plugin(plugin_id) |
| 51 | + scheduled = False |
| 52 | + if restart and bot_lifecycle_available(): |
| 53 | + scheduled = schedule_bot_restart() |
| 54 | + result = dict(result) |
| 55 | + result["restart_scheduled"] = scheduled |
| 56 | + if restart or result.get("needs_restart"): |
| 57 | + result["message"] = append_restart_note(str(result.get("message") or ""), scheduled=scheduled) |
| 58 | + return result |
| 59 | + |
| 60 | + |
| 61 | +__all__ = [ |
| 62 | + "CommunityPluginInstallError", |
| 63 | + "install_community_plugin_with_options", |
| 64 | + "uninstall_community_plugin_with_options", |
| 65 | +] |
0 commit comments