codex-mobile-bridge 0.3.15

Remote bridge and service manager for codex-mobile.
Documentation
use super::*;

impl BridgeManagementRunner {
    pub(super) fn run_start_service(&mut self) -> Result<()> {
        let snapshot = self.snapshot_now();
        let current_version = snapshot
            .as_ref()
            .and_then(|value| value.install_record.as_ref())
            .and_then(|record| record.current_version.clone());
        let expected_protocol = snapshot
            .as_ref()
            .and_then(|value| value.install_record.as_ref())
            .and_then(|record| record.current_protocol_version);
        self.task.current_version = current_version;
        self.update_task(
            BridgeManagementStatus::Running,
            BridgeManagementPhase::ControlService,
            "正在启动远端 bridge 服务".to_string(),
            Some("将启用并启动 systemd user service".to_string()),
            None,
            false,
        )?;
        environment::start_service()?;
        let expected_version = self
            .snapshot_now()
            .and_then(|value| value.install_record)
            .and_then(|record| record.current_version);
        self.update_task(
            BridgeManagementStatus::Running,
            BridgeManagementPhase::VerifyHealth,
            "远端 bridge 服务已启动,正在校验健康状态".to_string(),
            Some("等待 /health 返回最新 runtime 与协议状态".to_string()),
            None,
            false,
        )?;
        let health = wait_for_expected_health(
            &self.paths,
            expected_version.as_deref(),
            expected_protocol.or(Some(BRIDGE_PROTOCOL_VERSION)),
        )?;
        self.complete_success(
            "远端 bridge 服务启动完成".to_string(),
            Some(success_detail(&health)),
        )
    }

    pub(super) fn run_stop_service(&mut self) -> Result<()> {
        self.update_task(
            BridgeManagementStatus::Running,
            BridgeManagementPhase::ControlService,
            "正在停止远端 bridge 服务".to_string(),
            Some("将停止当前 systemd user service,保留安装文件".to_string()),
            None,
            false,
        )?;
        environment::stop_service()?;
        self.complete_success(
            "远端 bridge 服务已停止".to_string(),
            Some("服务已停止,可稍后重新启动。".to_string()),
        )
    }

    pub(super) fn run_restart_service(&mut self) -> Result<()> {
        let snapshot = self.snapshot_now();
        let current_version = snapshot
            .as_ref()
            .and_then(|value| value.install_record.as_ref())
            .and_then(|record| record.current_version.clone());
        let expected_protocol = snapshot
            .as_ref()
            .and_then(|value| value.install_record.as_ref())
            .and_then(|record| record.current_protocol_version);
        self.task.current_version = current_version;
        self.update_task(
            BridgeManagementStatus::Running,
            BridgeManagementPhase::ControlService,
            "正在重启远端 bridge 服务".to_string(),
            Some("将重启 systemd user service 并重新建立健康检查".to_string()),
            None,
            false,
        )?;
        environment::restart_service()?;
        let expected_version = self
            .snapshot_now()
            .and_then(|value| value.install_record)
            .and_then(|record| record.current_version);
        self.update_task(
            BridgeManagementStatus::Running,
            BridgeManagementPhase::VerifyHealth,
            "远端 bridge 服务已重启,正在校验健康状态".to_string(),
            Some("等待 /health 返回新一轮握手结果".to_string()),
            None,
            false,
        )?;
        let health = wait_for_expected_health(
            &self.paths,
            expected_version.as_deref(),
            expected_protocol.or(Some(BRIDGE_PROTOCOL_VERSION)),
        )?;
        self.complete_success(
            "远端 bridge 服务重启完成".to_string(),
            Some(success_detail(&health)),
        )
    }

    pub(super) fn run_uninstall(&mut self) -> Result<()> {
        self.update_task(
            BridgeManagementStatus::Running,
            BridgeManagementPhase::ControlService,
            "正在停止并禁用远端 bridge 服务".to_string(),
            Some("将停止服务,随后移除受管 unit/env/current 文件".to_string()),
            None,
            false,
        )?;
        environment::stop_service()?;
        environment::disable_service()?;
        self.update_task(
            BridgeManagementStatus::Running,
            BridgeManagementPhase::CleanupManagedFiles,
            "正在清理远端 bridge 托管文件".to_string(),
            Some("将保留 releases 与 bridge.db,只移除当前激活入口。".to_string()),
            None,
            false,
        )?;
        environment::remove_path_if_exists(&self.paths.current_link)?;
        environment::remove_path_if_exists(&self.paths.unit_file)?;
        environment::remove_path_if_exists(&self.paths.env_file)?;
        environment::daemon_reload()?;
        let existing_record = environment::read_install_record(&self.paths)?;
        let uninstall_record = records::build_uninstall_record(existing_record.as_ref());
        environment::write_install_record(&self.paths.install_record_file, &uninstall_record)?;
        self.complete_success(
            "远端 bridge 已卸载".to_string(),
            Some(
                "服务已停止,受管 unit/env/current 已移除,保留 releases 与 bridge 状态库。"
                    .to_string(),
            ),
        )
    }
}