fix(webui): support WSL browser launch and clearer startup output (#6150)

* feat(webui): add local first-run access setup

* feat(webui): defer password setup until network access

Keep generated local credentials and provide the full browser login link in every launch mode. Require a chosen password when enabling network access, preserve existing authentication, and show pending listener changes until restart.

Remove mandatory first-run initialization gates and simplify startup output while retaining gateway warnings and errors.

* fix(webui): align access settings with channel contracts

* fix(webui): clarify startup guidance and spacing

* fix(webui): keep setup guidance readable in narrow terminals

* fix(webui): clarify startup and preserve remote password access

* fix(webui): move access explanations into settings hints

* fix(webui): require strong ASCII passwords and reject malformed auth

* fix(webui): simplify access password character rules

* test(webui): reflect pending restart after password save

* fix(webui): open Windows browser from WSL

* fix(webui): clarify public access copy

* refactor(webui): focus on WSL launch and startup output

* fix(webui): preserve platform browser fallback
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-12 10:53:11 +08:00
1 parent a27f294f52
commit 41f2eeb7ba
12 files changed
+693 -374

No files matched your search

+58 -139
View File
@@ -8,7 +8,6 @@ from rich.console import Console
from nanobot.cli import terminal as cli_terminal
from nanobot.cli.runtime_config import (
_load_runtime_config,
_print_config_error,
_print_runtime_config_validation_error,
_provider_setup_error,
@@ -18,22 +17,20 @@ from nanobot.cli.webui_support import (
_attach_to_background_gateway,
_confirm_webui_action,
_ensure_local_webui_channel,
_gateway_health_bind_note,
_gateway_health_ready,
_gateway_health_url,
_gateway_instance_command,
_host_for_local_browser,
_load_webui_setup_config,
_open_webui_browser,
_prepare_webui_bundle_for_gateway,
_print_foreground_port_conflict,
_print_webui_manual_access,
_print_webui_ready,
_resolve_webui_config_path,
_start_gateway_log_cursor,
_tcp_endpoint_reachable,
_wait_for_webui,
_warn_webui_bind_scope,
_webui_browser_url,
_webui_build_mode_for_interactive,
_webui_display_url,
_webui_endpoint_reachable,
)
from nanobot.config.paths import get_workspace_path
@@ -57,10 +54,6 @@ def _wait_with_existing_foreground_gateway(
"""Keep a Vite sidecar alive without taking ownership of an external gateway."""
import time
console.print(
"[dim]Vite is attached to the existing foreground gateway. "
"Press Ctrl+C to stop Vite; the gateway will keep running.[/dim]"
)
try:
while True:
dev_server.ensure_running()
@@ -127,8 +120,7 @@ def webui(
raise typer.Exit(1)
created_config = not config_path.exists()
if created_config:
console.print(f"[yellow]No config found at {config_path}.[/yellow]")
_confirm_webui_action("Create a nanobot config and workspace now?", yes=yes)
_confirm_webui_action("Set up nanobot and open WebUI on this device?", yes=yes)
setup_config = _load_webui_setup_config(config_path)
if workspace:
@@ -145,18 +137,18 @@ def webui(
_validate_session_storage(resolved_setup_config, workspace_override=workspace)
provider_error = _provider_setup_error(resolved_setup_config)
if provider_error:
console.print(f"[yellow]Model setup is incomplete: {provider_error}[/yellow]")
console.print("Configure a provider and model in WebUI Settings → Models.")
try:
changed_webui = _ensure_local_webui_channel(
setup_config,
port=port,
yes=yes,
yes=yes or created_config,
)
_warn_webui_bind_scope(setup_config)
webui_url = _webui_browser_url(setup_config)
resolved_webui_config = resolve_config_env_vars(
setup_config.model_copy(deep=True), config_path=config_path,
)
_warn_webui_bind_scope(resolved_webui_config)
webui_url = _webui_browser_url(resolved_webui_config)
except ValidationError as exc:
retry_command = f'nanobot webui --config "{config_path}"'
_print_runtime_config_validation_error(
@@ -173,35 +165,15 @@ def webui(
if created_config or provider_error or changed_webui or workspace:
save_config(setup_config, config_path)
console.print(f"[green]✓[/green] Saved config: {config_path}")
workspace_path = get_workspace_path(setup_config.workspace_path)
workspace_path.mkdir(parents=True, exist_ok=True)
sync_workspace_templates(workspace_path)
sync_workspace_templates(workspace_path, silent=True)
runtime_config = _load_runtime_config(str(config_path), workspace)
runtime_config = resolved_webui_config
effective_gateway_port = gateway_port if gateway_port is not None else runtime_config.gateway.port
dev_browser_url = webui_dev_browser_url(webui_url) if dev else None
console.print()
if dev_browser_url:
console.print(f"WebUI dev: [cyan]{_webui_display_url(dev_browser_url)}[/cyan]")
console.print(f"WebUI gateway: [cyan]{_webui_display_url(webui_url)}[/cyan]")
else:
console.print(f"WebUI: [cyan]{_webui_display_url(webui_url)}[/cyan]")
gateway_health_url = _gateway_health_url(
runtime_config.gateway.host,
effective_gateway_port,
)
console.print(
f"Gateway health: [cyan]{gateway_health_url}[/cyan]"
f"{_gateway_health_bind_note(runtime_config.gateway.host)}"
)
if no_open:
console.print("[dim]Browser opening disabled by --no-open.[/dim]")
if not dev:
_print_webui_manual_access(runtime_config, config_path, webui_url)
if not dev:
webui_bundle_mode = _webui_build_mode_for_interactive(yes=yes)
_prepare_webui_bundle_for_gateway(runtime_config, mode=webui_bundle_mode)
@@ -211,92 +183,77 @@ def webui(
workspace=workspace,
)
runtime = GatewayRuntime(paths=instance.paths)
log_cursor = _start_gateway_log_cursor(runtime.paths.log_path)
start_options = instance.start_options(port=effective_gateway_port)
def ensure_shared_gateway(*, client_lease: GatewayClientLease) -> None:
"""Start or refresh the one managed gateway shared by local clients."""
result = client_lease.ensure_on_demand_gateway(start_options)
restarted = False
restart_attempted = False
if not result.ok and result.message == "gateway_already_running" and changed_webui:
restart_attempted = True
console.print("[yellow]WebUI config changed; restarting the background gateway.[/yellow]")
result = runtime.restart(start_options, timeout_s=20)
restarted = result.ok
if not result.ok and (restart_attempted or result.message != "gateway_already_running"):
action = "restarted" if restart_attempted else "started"
console.print(f"[yellow]Gateway was not {action}: {result.message}[/yellow]")
console.print(f"Logs: {result.status.log_path}")
raise typer.Exit(1)
if restarted:
console.print("[green]Gateway restarted in the background.[/green]")
elif result.ok:
console.print("[green]Gateway started in the background.[/green]")
else:
console.print("[yellow]Gateway is already running in the background.[/yellow]")
if not _wait_for_webui(webui_url, timeout_s=20):
console.print("[red]WebUI did not become ready. Check the gateway logs.[/red]")
console.print(f"Logs: {runtime.paths.log_path}", markup=False)
raise typer.Exit(1)
def print_shared_gateway_controls() -> None:
console.print(
"Manage this instance: "
f"[cyan]{_gateway_instance_command('status', config_path=config_path, workspace=workspace)}[/cyan]"
)
console.print(
"View logs: "
f"[cyan]{_gateway_instance_command('logs', config_path=config_path, workspace=workspace)}[/cyan]"
)
console.print("[dim]Closing the browser does not stop channels or automations.[/dim]")
console.print(
"Stop nanobot: "
f"[cyan]{_gateway_instance_command('stop', config_path=config_path, workspace=workspace)}[/cyan]"
def announce_ready(url: str, *, managed: bool) -> None:
_print_webui_ready(
runtime_config, config_path, url, workspace=workspace, dev=dev, managed=managed,
provider_error=provider_error,
)
if not no_open:
_open_webui_browser(url, wait=False)
gateway_ready = _gateway_health_ready(runtime_config.gateway.host, effective_gateway_port)
webui_ready = _webui_endpoint_reachable(webui_url)
if gateway_ready and webui_ready:
lease = GatewayClientLease(runtime, kind="webui")
lease.acquire()
try:
if changed_webui and runtime.status().running:
ensure_shared_gateway(client_lease=lease)
gateway_ready = _gateway_health_ready(
runtime_config.gateway.host,
effective_gateway_port,
)
webui_ready = _webui_endpoint_reachable(webui_url)
if not gateway_ready or not webui_ready:
console.print("[red]Gateway did not become ready after the config update.[/red]")
raise typer.Exit(1)
console.print(
"[yellow]Gateway is already running; attaching to the existing WebUI.[/yellow]"
existing_gateway = gateway_ready and webui_ready
if not existing_gateway:
gateway_port_taken = gateway_ready or _tcp_endpoint_reachable(
_host_for_local_browser(runtime_config.gateway.host),
effective_gateway_port,
)
if gateway_port_taken or webui_ready:
_print_foreground_port_conflict(
webui_url=webui_url,
gateway_host=runtime_config.gateway.host,
gateway_port=effective_gateway_port,
)
if not dev:
console.print(
"Restart the gateway if you need it to pick up local source changes: "
f"[cyan]{_gateway_instance_command('restart', config_path=config_path, workspace=workspace)}[/cyan]"
)
if not no_open and not _open_webui_browser(webui_url, wait=False):
_print_webui_manual_access(runtime_config, config_path, webui_url)
if runtime.status().running:
_attach_to_background_gateway(runtime)
else:
console.print(
"[yellow]This gateway is controlled by another foreground command. "
"Stop it from that terminal.[/yellow]"
)
return
raise typer.Exit(1)
lease = GatewayClientLease(runtime, kind="webui")
lease.acquire()
try:
if not existing_gateway:
console.print("[dim]Starting WebUI…[/dim]")
ensure_shared_gateway(client_lease=lease)
elif changed_webui and runtime.status().running:
ensure_shared_gateway(client_lease=lease)
if not (_gateway_health_ready(runtime_config.gateway.host, effective_gateway_port)
and _webui_endpoint_reachable(webui_url)):
console.print("[red]Gateway did not become ready after the config update.[/red]")
raise typer.Exit(1)
if dev_browser_url:
try:
assert dev_browser_url is not None
with run_webui_dev_server(
target_url=webui_dev_proxy_target(webui_url),
browser_url=dev_browser_url,
output=lambda message: console.print(f"[green]✓[/green] {message}"),
output=lambda message: console.print(message, style="dim", markup=False),
) as dev_server:
if not no_open:
_open_webui_browser(dev_browser_url, wait=False)
if runtime.status().running:
managed = not existing_gateway or runtime.status().running
announce_ready(dev_browser_url, managed=managed)
if managed:
_attach_to_background_gateway(
runtime,
log_cursor=log_cursor,
poll_hook=dev_server.ensure_running,
)
else:
@@ -309,48 +266,10 @@ def webui(
console.print(f"[red]Error: {exc}[/red]")
raise typer.Exit(1) from exc
return
finally:
lease.release(wait_for_stop=False)
gateway_port_taken = gateway_ready or _tcp_endpoint_reachable(
_host_for_local_browser(runtime_config.gateway.host),
effective_gateway_port,
)
webui_port_taken = webui_ready
if gateway_port_taken or webui_port_taken:
_print_foreground_port_conflict(
webui_url=webui_url,
gateway_host=runtime_config.gateway.host,
gateway_port=effective_gateway_port,
)
raise typer.Exit(1)
lease = GatewayClientLease(runtime, kind="webui")
lease.acquire()
try:
ensure_shared_gateway(client_lease=lease)
print_shared_gateway_controls()
if dev_browser_url:
dev_proxy_target = webui_dev_proxy_target(webui_url)
try:
with run_webui_dev_server(
target_url=dev_proxy_target,
browser_url=dev_browser_url,
output=lambda message: console.print(f"[green]✓[/green] {message}"),
) as dev_server:
if not no_open:
_open_webui_browser(dev_browser_url)
_attach_to_background_gateway(
runtime,
poll_hook=dev_server.ensure_running,
)
except WebUIDevError as exc:
console.print(f"[red]Error: {exc}[/red]")
raise typer.Exit(1) from exc
return
if not no_open and not _open_webui_browser(webui_url):
_print_webui_manual_access(runtime_config, config_path, webui_url)
_attach_to_background_gateway(runtime)
managed = not existing_gateway or runtime.status().running
announce_ready(webui_url, managed=managed)
if managed:
_attach_to_background_gateway(runtime, log_cursor=log_cursor)
finally:
lease.release(wait_for_stop=False)