MCPcopy Index your code
hub / github.com/AstrBotDevs/AstrBot / PlatformManager

Class PlatformManager

astrbot/core/platform/manager.py:22–350  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20
21
22class PlatformManager:
23 def __init__(self, config: AstrBotConfig, event_queue: Queue) -> None:
24 self.platform_insts: list[Platform] = []
25 """加载的 Platform 的实例"""
26
27 self._inst_map: dict[str, dict] = {}
28 self._platform_tasks: dict[str, PlatformTasks] = {}
29
30 self.astrbot_config = config
31 self.platforms_config = config["platform"]
32 self.settings = config["platform_settings"]
33 """NOTE: 这里是 default 的配置文件,以保证最大的兼容性;
34 这个配置中的 unique_session 需要特殊处理,
35 约定整个项目中对 unique_session 的引用都从 default 的配置中获取"""
36 self.event_queue = event_queue
37
38 def _is_valid_platform_id(self, platform_id: str | None) -> bool:
39 if not platform_id:
40 return False
41 return ":" not in platform_id and "!" not in platform_id
42
43 def _sanitize_platform_id(self, platform_id: str | None) -> tuple[str | None, bool]:
44 if not platform_id:
45 return platform_id, False
46 sanitized = platform_id.replace(":", "_").replace("!", "_")
47 return sanitized, sanitized != platform_id
48
49 def _start_platform_task(self, task_name: str, inst: Platform) -> None:
50 run_task = asyncio.create_task(inst.run(), name=task_name)
51 wrapper_task = asyncio.create_task(
52 self._task_wrapper(run_task, platform=inst),
53 name=f"{task_name}_wrapper",
54 )
55 self._platform_tasks[inst.client_self_id] = PlatformTasks(
56 run=run_task,
57 wrapper=wrapper_task,
58 )
59
60 async def _stop_platform_task(self, client_id: str) -> None:
61 tasks = self._platform_tasks.pop(client_id, None)
62 if not tasks:
63 return
64 for task in (tasks.run, tasks.wrapper):
65 if not task.done():
66 task.cancel()
67 await asyncio.gather(tasks.run, tasks.wrapper, return_exceptions=True)
68
69 async def _terminate_inst_and_tasks(self, inst: Platform) -> None:
70 client_id = inst.client_self_id
71 try:
72 if getattr(inst, "terminate", None):
73 try:
74 await inst.terminate()
75 except asyncio.CancelledError:
76 raise
77 except Exception as e:
78 logger.error(
79 "终止平台适配器失败: client_id=%s, error=%s",

Callers 1

initializeMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected