From 71c5511e6c2e341a09703202b0c79e711cd0c88a Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 7 Dec 2025 03:31:03 +0100 Subject: [PATCH] chore(mac): add webchat auto-open flag and verbose logging --- apps/macos/Sources/Clawdis/MenuBar.swift | 8 ++++ .../macos/Sources/Clawdis/WebChatWindow.swift | 48 ++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/apps/macos/Sources/Clawdis/MenuBar.swift b/apps/macos/Sources/Clawdis/MenuBar.swift index ec7358fa9..0d028c489 100644 --- a/apps/macos/Sources/Clawdis/MenuBar.swift +++ b/apps/macos/Sources/Clawdis/MenuBar.swift @@ -4,6 +4,7 @@ import MenuBarExtraAccess import SwiftUI import Security import OSLog +import OSLog import Darwin @main @@ -390,6 +391,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSXPCListenerDelegate private var listener: NSXPCListener? private var state: AppState? private let xpcLogger = Logger(subsystem: "com.steipete.clawdis", category: "xpc") + private let webChatAutoLogger = Logger(subsystem: "com.steipete.clawdis", category: "WebChat") private let allowedTeamIDs: Set = ["Y5PE65HELJ"] @MainActor @@ -405,6 +407,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSXPCListenerDelegate } self.startListener() self.scheduleFirstRunOnboardingIfNeeded() + + // Developer/testing helper: auto-open WebChat when launched with --webchat + if CommandLine.arguments.contains("--webchat") { + webChatAutoLogger.debug("Auto-opening web chat via --webchat flag") + WebChatManager.shared.show(sessionKey: "main") + } } func applicationWillTerminate(_ notification: Notification) { diff --git a/apps/macos/Sources/Clawdis/WebChatWindow.swift b/apps/macos/Sources/Clawdis/WebChatWindow.swift index 83cbf100a..60422c7fb 100644 --- a/apps/macos/Sources/Clawdis/WebChatWindow.swift +++ b/apps/macos/Sources/Clawdis/WebChatWindow.swift @@ -11,6 +11,7 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler, private let initialMessagesJSON: String init(sessionKey: String) { + webChatLogger.debug("init WebChatWindowController sessionKey=\(sessionKey, privacy: .public)") self.sessionKey = sessionKey self.initialMessagesJSON = WebChatWindowController.loadInitialMessagesJSON(for: sessionKey) @@ -50,6 +51,16 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler, try { window.__clawdisLog(args.join(' ')); } catch (_) {} __origConsoleLog.apply(console, args); }; + window.addEventListener('error', (e) => { + try { + window.__clawdisLog(`page error: ${e.message} @ ${e.filename}:${e.lineno}:${e.colno}`); + } catch (_) {} + }); + window.addEventListener('unhandledrejection', (e) => { + try { + window.__clawdisLog(`unhandled rejection: ${e.reason}`); + } catch (_) {} + }); """ let userScript = WKUserScript(source: callbackScript, injectionTime: .atDocumentStart, forMainFrameOnly: true) contentController.addUserScript(userScript) @@ -72,9 +83,11 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler, required init?(coder: NSCoder) { fatalError("init(coder:) is not supported") } private func loadPage() { + webChatLogger.debug("loadPage begin") let messagesJSON = self.initialMessagesJSON.replacingOccurrences(of: "", with: "<\\/script>") guard let webChatURL = Bundle.main.url(forResource: "WebChat", withExtension: nil) else { NSLog("WebChat resources missing") + webChatLogger.error("WebChat resources missing in bundle") return } @@ -114,6 +127,7 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler, let html = self.makeHTML(importMapJSON: importMapJSON, messagesJSON: messagesJSON) self.webView.loadHTMLString(html, baseURL: webChatURL) + webChatLogger.debug("loadPage queued HTML into WKWebView baseURL=\(webChatURL.absoluteString, privacy: .public)") } // swiftlint:disable line_length @@ -122,6 +136,7 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler, + Clawd Web Chat @@ -131,7 +146,7 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler, -
Booting web chat…
+
Booting web chat…