| 219 | |
| 220 | |
| 221 | def maybe_open_browser(url: str, timeout_seconds: int, enabled: bool, dry_run: bool) -> threading.Thread | None: |
| 222 | if not enabled: |
| 223 | return None |
| 224 | if dry_run: |
| 225 | log(f"[dry-run] 将在服务可访问后打开浏览器: {url}") |
| 226 | return None |
| 227 | |
| 228 | def _runner() -> None: |
| 229 | try: |
| 230 | wait_for_http(url, timeout_seconds) |
| 231 | except Exception as exc: |
| 232 | log(f"浏览器自动打开前的可访问性检测失败: {exc}", "WARN") |
| 233 | return |
| 234 | webbrowser.open(url) |
| 235 | log(f"已尝试打开浏览器: {url}") |
| 236 | |
| 237 | thread = threading.Thread(target=_runner, name="webui-browser", daemon=True) |
| 238 | thread.start() |
| 239 | return thread |
| 240 | |
| 241 | |
| 242 | def launch_webui(host: str, port: int, dry_run: bool) -> int: |