Files
openclaw/apps/macos/Sources/OpenClaw/DashboardManager.swift
Peter Steinberger bc4ed5cf61 feat(macos): native-feel dashboard hosting — instant reopen, preload, frame autosave, ⌘N/⌘K, route memory (#106997)
* feat(macos): native-feel dashboard hosting: instant reopen, preload, frame autosave, ⌘N/⌘K, route memory

The hosted Control UI dashboard now skips SPA reloads when reopening an
unchanged endpoint (auth-equality gated), preloads after launch when a
credentialed local/direct config exists, autosaves its window frame, maps
⌘N/⌘K menu items onto the shipped native web events, filters browser-tell
context-menu items, restores the last committed route via web-side
localStorage memory, and themes the pre-paint background.

* refactor(ui): satisfy TS LOC ratchet: move optional lazy-element helpers out of app-host

app-host.ts is over the 500-line ratchet ceiling and may not grow; move the
self-contained optional custom-element block into lazy-custom-element.ts (its
natural owner) and fold the route-restore decision into native-route-memory's
considerRouteRestore so the shell only replaces-or-persists.

* fix(macos): recover stuck dashboard command queue via deliverability gate

Replace the failure-page-only fast-path check with canDeliverNativeCommands
(live document or in-flight load); a terminally cancelled reload now falls
through to the reload path instead of queueing ⌘N/⌘K forever.

* fix(ui): let in-flight navigation win over native route restore

The one-shot startup restore now checks the rendered/pending match against
the committed bootstrap route; an explicit navigation already in flight
(replayed native new-session, fast click) is no longer clobbered.

* docs(ui): note idempotent collapse of pending native new-session replays

* fix(macos): drop queued dashboard commands on terminal load failure

Queued ⌘N/⌘K are moment-bound; surviving a failure page meant a later
recovery reload replayed stale commands (double palette toggle nets closed,
surprise navigation). Both failure entry points now clear the queue.

* fix(ui): persist native route memory only for settled router states

Mid-navigation emissions still carry the stale committed bootstrap route; an
interrupted restore could overwrite the remembered destination with it.

* fix(macos): coalesce dashboard opens for queued native commands

One in-flight open drains a manager-level queue in press order; a Task per
key press could race window creation (duplicate windows) and reorder ⌘N/⌘K
delivery on the non-immediate remote path.

* fix(macos): correct dashboard placement after frame-autosave restore

setFrameAutosaveName re-applies the stored frame; run ensureOnScreen after
both autosave-name assignments so a frame saved on a disconnected monitor
cannot restore the window off-screen.

* fix(macos): clear dashboard failure state when history restores a real document

Swipe-back/⌘[ off the failure page commits an http(s) document without
passing through load(); the flag previously stayed set and forced a reload
on the next native command.

* fix(macos): keep in-flight dashboard loads instead of restarting them

An open/update during the launch preload previously cancelled and restarted
the same-URL load; in-flight non-failure documents now count as usable in
the reload decision.

* fix(macos,ui): ⌘K legacy fallback + transient search param filtering

Command palette dispatch now sends a cancelable toggle event and falls back
to the shipped open-search event when no handler acknowledges it, so ⌘K
keeps working against older gateway-served bundles. Route memory strips
one-shot action params (?draft=) before persisting; navigation state like
?session= still restores.

* chore(i18n): register dashboard menu strings + drop unused route-memory export

native:i18n:sync inventory + per-locale artifacts for New Session / Command
Palette…; StoredNativeRoute is module-internal after the considerRouteRestore
refactor (deadcode exports gate).

* chore(i18n): rebaseline raw-copy for relocated lazy-element labels

The lazy-custom-element extraction moved three internal load-failure labels
out of app-host.ts; refresh the keyless raw-copy baseline to match (main's
new ui:i18n:verify gate).
2026-07-14 03:33:25 -07:00

391 lines
16 KiB
Swift

import AppKit
import Foundation
import OpenClawKit
import OSLog
private let dashboardManagerLogger = Logger(subsystem: "ai.openclaw", category: "DashboardManager")
@MainActor
final class DashboardManager {
static let shared = DashboardManager()
private var controller: DashboardWindowController?
private var endpointTask: Task<Void, Never>?
private var pendingOpenCommands: [DashboardNativeCommand] = []
private var openForCommandTask: Task<Void, Never>?
private var updater: UpdaterProviding?
private var displayedRouteRevision: UInt64?
private let authTokenProvider: @Sendable (GatewayConnection.Config) async -> String?
private let routeProbe: @Sendable () async -> Void
private static let failureURL = URL(string: "about:blank")!
private init(
authTokenProvider: @escaping @Sendable (GatewayConnection.Config) async -> String? = { config in
await GatewayConnection.shared.controlUiAutoAuthToken(config: config)
},
routeProbe: @escaping @Sendable () async -> Void = {
_ = try? await GatewayConnection.shared.request(
method: "health",
params: nil,
timeoutMs: 3000,
retryTransportFailures: false)
})
{
self.authTokenProvider = authTokenProvider
self.routeProbe = routeProbe
}
func configure(updater: UpdaterProviding) {
self.updater = updater
}
/// The card's native update path only makes sense when the app owns the
/// local gateway and the post-relaunch repair is allowed to run; otherwise
/// (external CLI, write-disabled launchd, extended-stable pin) the card
/// must keep the direct gateway `update.run` flow, so no bridge is exposed.
static func updateBridgeEnabled(mode: AppState.ConnectionMode) -> Bool {
guard mode == .local else { return false }
return CLIInstallPrompter.managedRepairGatesOpen(
launchAgentUsesManagedCLI: CLIInstallPrompter.launchAgentUsesManagedCLI(
programArguments: GatewayLaunchAgentManager.launchdConfigSnapshot()?.programArguments ?? []),
gatewayUpdateChannel: OpenClawConfigFile.gatewayUpdateChannel(),
installPolicy: CLIInstallPolicy.storedPolicy(),
launchAgentWriteDisabled: GatewayLaunchAgentManager.isLaunchAgentWriteDisabled())
}
/// The remote SSH tunnel can be recreated on a new ephemeral local port while
/// the dashboard stays open; without following endpoint changes the WebView
/// keeps reconnecting to the dead old port forever (#100476).
private func observeEndpointChanges() {
guard self.endpointTask == nil else { return }
self.endpointTask = Task { [weak self] in
let stream = await GatewayEndpointStore.shared.subscribe()
for await state in stream {
guard let self else { return }
await self.handleEndpointState(state)
}
}
}
func handleEndpointState(_ state: GatewayEndpointState) async {
guard let controller, controller.isWindowOpen else { return }
guard case let .ready(mode, url, token, password, routeRevision) = state else {
self.replaceWithRouteFailure(controller)
self.displayedRouteRevision = nil
return
}
let config: GatewayConnection.Config = (url, token, password)
let routeChanged = self.displayedRouteRevision.map { $0 != routeRevision }
?? (routeRevision > 0)
var authToken = await self.authTokenProvider(config)
if authToken == nil, password?.trimmingCharacters(in: .whitespacesAndNewlines).nonEmpty == nil {
await self.routeProbe()
authToken = await self.authTokenProvider(config)
}
guard let dashboardURL = try? GatewayEndpointStore.dashboardURL(
for: config,
mode: mode,
authToken: authToken)
else {
return
}
let auth = DashboardWindowAuth(
gatewayUrl: Self.websocketURLString(for: dashboardURL),
token: authToken,
password: password?.trimmingCharacters(in: .whitespacesAndNewlines).nonEmpty)
if routeChanged {
self.displayedRouteRevision = routeRevision
guard auth.hasCredential else {
self.replaceWithRouteFailure(controller)
return
}
self.replaceController(
controller,
url: dashboardURL,
auth: auth,
mode: mode)
return
}
if dashboardURL == controller.currentURL {
self.displayedRouteRevision = routeRevision
controller.setUpdateBridgeEnabled(Self.updateBridgeEnabled(mode: mode))
return
}
guard auth.hasCredential, controller.isWindowOpen else { return }
dashboardManagerLogger.info(
"dashboard endpoint changed; reloading url=\(dashboardLogString(for: dashboardURL), privacy: .public)")
controller.update(url: dashboardURL, auth: auth, updateBridgeEnabled: Self.updateBridgeEnabled(mode: mode))
self.displayedRouteRevision = routeRevision
}
private func replaceController(
_ current: DashboardWindowController,
url: URL,
auth: DashboardWindowAuth,
mode: AppState.ConnectionMode)
{
current.releaseFrameAutosaveForReplacement()
current.closeDashboard()
let replacement = DashboardWindowController(
url: url,
auth: auth,
updater: self.updater,
updateBridgeEnabled: Self.updateBridgeEnabled(mode: mode))
self.controller = replacement
replacement.show(url: url, auth: auth)
}
private func replaceWithRouteFailure(_ current: DashboardWindowController) {
current.releaseFrameAutosaveForReplacement()
current.closeDashboard()
let replacement = DashboardWindowController(
url: Self.failureURL,
auth: DashboardWindowAuth(gatewayUrl: nil, token: nil, password: nil),
updater: self.updater,
updateBridgeEnabled: false)
self.controller = replacement
replacement.showFailure(
title: "Dashboard reconnecting",
message: "The selected Gateway changed.",
detail: "Waiting for a fresh authenticated connection.")
}
@discardableResult
func showConfiguredWindowIfPossible() -> Bool {
let mode = AppStateStore.shared.connectionMode
guard let config = self.immediateDashboardConfig(mode: mode),
let url = try? GatewayEndpointStore.dashboardURL(
for: config,
mode: mode,
authToken: config.token)
else {
return false
}
let auth = DashboardWindowAuth(
gatewayUrl: Self.websocketURLString(for: url),
token: config.token,
password: config.password?.trimmingCharacters(in: .whitespacesAndNewlines).nonEmpty)
guard auth.hasCredential else {
return false
}
if let controller {
controller.show(url: url, auth: auth, updateBridgeEnabled: Self.updateBridgeEnabled(mode: mode))
} else {
let controller = DashboardWindowController(
url: url,
auth: auth,
updater: self.updater,
updateBridgeEnabled: Self.updateBridgeEnabled(mode: mode))
self.controller = controller
controller.show(url: url, auth: auth)
}
self.observeEndpointChanges()
Task { _ = try? await ControlChannel.shared.health(timeout: 3) }
return true
}
/// Preload failures stay invisible: navigation errors land in the
/// controller's `showLoadFailure`, which never orders the window front, and
/// preload skips `observeEndpointChanges()` so no observer path can call
/// `showFailure`. The failure page is only seen on a later explicit show.
func preloadIfConfigured() {
guard self.controller == nil,
AppStateStore.shared.onboardingSeen,
let (mode, url, auth) = self.immediateWindowConfiguration()
else { return }
let controller = DashboardWindowController(
url: url,
auth: auth,
updater: self.updater,
updateBridgeEnabled: Self.updateBridgeEnabled(mode: mode))
self.controller = controller
controller.loadInBackground(url: url, auth: auth)
}
func show() async throws {
let mode = AppStateStore.shared.connectionMode
dashboardManagerLogger.info("dashboard show requested mode=\(String(describing: mode), privacy: .public)")
let config = try await self.dashboardConfig(mode: mode)
dashboardManagerLogger.info("dashboard config url=\(config.url.absoluteString, privacy: .public)")
let token = await GatewayConnection.shared.controlUiAutoAuthToken(config: config)
let url = try GatewayEndpointStore.dashboardURL(for: config, mode: mode, authToken: token)
let auth = DashboardWindowAuth(
gatewayUrl: Self.websocketURLString(for: url),
token: token,
password: config.password?.trimmingCharacters(in: .whitespacesAndNewlines).nonEmpty)
if let controller {
dashboardManagerLogger.info("dashboard reuse window url=\(dashboardLogString(for: url), privacy: .public)")
controller.show(url: url, auth: auth, updateBridgeEnabled: Self.updateBridgeEnabled(mode: mode))
self.observeEndpointChanges()
return
}
dashboardManagerLogger.info("dashboard create window url=\(dashboardLogString(for: url), privacy: .public)")
let controller = DashboardWindowController(
url: url,
auth: auth,
updater: self.updater,
updateBridgeEnabled: Self.updateBridgeEnabled(mode: mode))
self.controller = controller
controller.show(url: url, auth: auth)
self.observeEndpointChanges()
// Refresh the cached hello payload without blocking window creation.
Task { _ = try? await ControlChannel.shared.health(timeout: 3) }
}
func showFailure(_ error: Error) {
let message = (error as NSError).localizedDescription
dashboardManagerLogger.error("dashboard setup failed error=\(message, privacy: .public)")
let controller = self.controller ?? DashboardWindowController(
url: Self.failureURL,
auth: DashboardWindowAuth(gatewayUrl: nil, token: nil, password: nil),
updater: self.updater,
updateBridgeEnabled: Self.updateBridgeEnabled(mode: AppStateStore.shared.connectionMode))
self.controller = controller
// Keep observing while the failure page is up so a recovered tunnel
// swaps the window back to the live dashboard.
self.observeEndpointChanges()
controller.showFailure(
title: "Dashboard unavailable",
message: message,
detail: "Check Settings → Connection or use Debug → Reset Remote Tunnel, then try again.")
}
func close() {
self.controller?.closeDashboard()
}
func handleOnboardingCompletion() {
self.controller?.handleOnboardingCompletion()
}
func navigateBack() {
guard self.controller?.window?.isKeyWindow == true else { return }
self.controller?.navigateBack()
}
func navigateForward() {
guard self.controller?.window?.isKeyWindow == true else { return }
self.controller?.navigateForward()
}
func dispatchNativeCommand(_ command: DashboardNativeCommand) {
NSApp.activate(ignoringOtherApps: true)
if let controller, controller.isWindowOpen, controller.canDeliverNativeCommands {
controller.show()
controller.dispatchNativeCommand(command)
return
}
// One coalesced open drains the queue in press order; a Task per key
// press would race window creation and reorder N/K delivery.
self.pendingOpenCommands.append(command)
guard self.openForCommandTask == nil else { return }
self.openForCommandTask = Task { @MainActor in
defer { self.openForCommandTask = nil }
if !self.showConfiguredWindowIfPossible() {
do {
try await self.show()
} catch {
// Commands are moment-bound; drop them with the failed open.
self.pendingOpenCommands = []
self.showFailure(error)
return
}
}
let commands = self.pendingOpenCommands
self.pendingOpenCommands = []
for command in commands {
self.controller?.dispatchNativeCommand(command)
}
}
}
private static func websocketURLString(for dashboardURL: URL) -> String {
guard var components = URLComponents(url: dashboardURL, resolvingAgainstBaseURL: false) else {
return dashboardURL.absoluteString
}
switch components.scheme?.lowercased() {
case "https":
components.scheme = "wss"
default:
components.scheme = "ws"
}
components.queryItems = nil
components.fragment = nil
return components.url?.absoluteString ?? dashboardURL.absoluteString
}
private func dashboardConfig(mode: AppState.ConnectionMode) async throws -> GatewayConnection.Config {
if let config = self.immediateDashboardConfig(mode: mode) {
return config
}
return try await Task.detached(priority: .userInitiated) {
await GatewayEndpointStore.shared.refresh()
return try await GatewayEndpointStore.shared.requireConfig()
}.value
}
private func immediateDashboardConfig(mode: AppState.ConnectionMode) -> GatewayConnection.Config? {
let root = OpenClawConfigFile.loadDict()
let resolution = GatewayRemoteConfig.resolveTransportResolution(root: root)
if mode == .remote,
resolution.transport == .direct,
let url = resolution.directURL
{
return (
url,
GatewayRemoteConfig.resolveTokenString(root: root),
GatewayRemoteConfig.resolvePasswordString(root: root))
}
if mode == .local {
return GatewayEndpointStore.localConfig()
}
return nil
}
private func immediateWindowConfiguration()
-> (AppState.ConnectionMode, URL, DashboardWindowAuth)?
{
let mode = AppStateStore.shared.connectionMode
guard let config = self.immediateDashboardConfig(mode: mode),
let url = try? GatewayEndpointStore.dashboardURL(
for: config,
mode: mode,
authToken: config.token)
else { return nil }
let auth = DashboardWindowAuth(
gatewayUrl: Self.websocketURLString(for: url),
token: config.token,
password: (config.password?.trimmingCharacters(in: .whitespacesAndNewlines).nonEmpty))
return auth.hasCredential ? (mode, url, auth) : nil
}
}
#if DEBUG
extension DashboardManager {
/// Test instances skip `observeEndpointChanges()` so the shared endpoint
/// store cannot race test-driven `handleEndpointState` calls.
static func _testMake(
authTokenProvider: @escaping @Sendable (GatewayConnection.Config) async -> String? = { $0.token },
routeProbe: @escaping @Sendable () async -> Void = {}) -> DashboardManager
{
DashboardManager(
authTokenProvider: authTokenProvider,
routeProbe: routeProbe)
}
func _testSetController(_ controller: DashboardWindowController?) {
self.controller = controller
}
func _testController() -> DashboardWindowController? {
self.controller
}
}
#endif