AstrBot 核心生命周期管理类, 负责管理 AstrBot 的启动、停止、重启等操作. 该类负责初始化各个组件, 包括 ProviderManager、PlatformManager、ConversationManager、PluginManager、PipelineScheduler、 EventBus 等。 该类还负责加载和执行插件, 以及处理事件总线的分发。
| 44 | |
| 45 | |
| 46 | class AstrBotCoreLifecycle: |
| 47 | """AstrBot 核心生命周期管理类, 负责管理 AstrBot 的启动、停止、重启等操作. |
| 48 | |
| 49 | 该类负责初始化各个组件, 包括 ProviderManager、PlatformManager、ConversationManager、PluginManager、PipelineScheduler、 |
| 50 | EventBus 等。 |
| 51 | 该类还负责加载和执行插件, 以及处理事件总线的分发。 |
| 52 | """ |
| 53 | |
| 54 | def __init__(self, log_broker: LogBroker, db: BaseDatabase) -> None: |
| 55 | self.log_broker = log_broker # 初始化日志代理 |
| 56 | self.astrbot_config = astrbot_config # 初始化配置 |
| 57 | self.db = db # 初始化数据库 |
| 58 | |
| 59 | self.subagent_orchestrator: SubAgentOrchestrator | None = None |
| 60 | self.cron_manager: CronJobManager | None = None |
| 61 | self.temp_dir_cleaner: TempDirCleaner | None = None |
| 62 | self._default_chat_provider_warning_emitted = False |
| 63 | |
| 64 | # 设置代理 |
| 65 | proxy_config = self.astrbot_config.get("http_proxy", "") |
| 66 | if proxy_config != "": |
| 67 | os.environ["https_proxy"] = proxy_config |
| 68 | os.environ["http_proxy"] = proxy_config |
| 69 | logger.debug(f"Using proxy: {proxy_config}") |
| 70 | # 设置 no_proxy |
| 71 | no_proxy_list = self.astrbot_config.get("no_proxy", []) |
| 72 | os.environ["no_proxy"] = ",".join(no_proxy_list) |
| 73 | else: |
| 74 | # 清空代理环境变量 |
| 75 | if "https_proxy" in os.environ: |
| 76 | del os.environ["https_proxy"] |
| 77 | if "http_proxy" in os.environ: |
| 78 | del os.environ["http_proxy"] |
| 79 | if "no_proxy" in os.environ: |
| 80 | del os.environ["no_proxy"] |
| 81 | logger.debug("HTTP proxy cleared") |
| 82 | |
| 83 | async def _init_or_reload_subagent_orchestrator(self) -> None: |
| 84 | """Create (if needed) and reload the subagent orchestrator from config. |
| 85 | |
| 86 | This keeps lifecycle wiring in one place while allowing the orchestrator |
| 87 | to manage enable/disable and tool registration details. |
| 88 | """ |
| 89 | try: |
| 90 | if self.subagent_orchestrator is None: |
| 91 | self.subagent_orchestrator = SubAgentOrchestrator( |
| 92 | self.provider_manager.llm_tools, |
| 93 | self.persona_mgr, |
| 94 | ) |
| 95 | await self.subagent_orchestrator.reload_from_config( |
| 96 | self.astrbot_config.get("subagent_orchestrator", {}), |
| 97 | ) |
| 98 | except Exception as e: |
| 99 | logger.error(f"Subagent orchestrator init failed: {e}", exc_info=True) |
| 100 | |
| 101 | def _warn_about_unset_default_chat_provider(self) -> None: |
| 102 | if self._default_chat_provider_warning_emitted: |
| 103 | return |
no outgoing calls