From f4fa9bf51a5080efd6e4c2ae9693a7c336270813 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 7 Dec 2025 16:10:22 +0100 Subject: [PATCH] fix(macos): load web chat from bundled html --- .../Clawdis/Resources/WebChat/index.html | 143 +++++++++++++++ .../macos/Sources/Clawdis/WebChatWindow.swift | 172 ++---------------- 2 files changed, 158 insertions(+), 157 deletions(-) create mode 100644 apps/macos/Sources/Clawdis/Resources/WebChat/index.html diff --git a/apps/macos/Sources/Clawdis/Resources/WebChat/index.html b/apps/macos/Sources/Clawdis/Resources/WebChat/index.html new file mode 100644 index 000000000..a8fcdf731 --- /dev/null +++ b/apps/macos/Sources/Clawdis/Resources/WebChat/index.html @@ -0,0 +1,143 @@ + + + + + + Clawd Web Chat + + + + + +
Booting web chat…
+ + + diff --git a/apps/macos/Sources/Clawdis/WebChatWindow.swift b/apps/macos/Sources/Clawdis/WebChatWindow.swift index 488b77ea2..b746827dc 100644 --- a/apps/macos/Sources/Clawdis/WebChatWindow.swift +++ b/apps/macos/Sources/Clawdis/WebChatWindow.swift @@ -84,172 +84,30 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler, 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 { + guard let webChatURL = Bundle.main.url(forResource: "WebChat", withExtension: nil), + let htmlURL = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "WebChat") + else { NSLog("WebChat resources missing") webChatLogger.error("WebChat resources missing in bundle") return } - let importMap = [ - "imports": [ - "@mariozechner/pi-web-ui": "./index.js", - "@mariozechner/pi-web-ui/": "./", - "@mariozechner/pi-ai": "./pi-ai-stub.js", - "@mariozechner/pi-ai/": "./pi-ai-stub.js", - "@mariozechner/mini-lit": "./vendor/@mariozechner/mini-lit/dist/index.js", - "@mariozechner/mini-lit/": "./vendor/@mariozechner/mini-lit/dist/", - "lit": "./vendor/lit/index.js", - "lit/": "./vendor/lit/", - "lucide": "./vendor/lucide/dist/esm/lucide.js", - "pdfjs-dist": "./vendor/pdfjs-dist/build/pdf.js", - "pdfjs-dist/": "./vendor/pdfjs-dist/", - "pdfjs-dist/build/pdf.worker.min.mjs": "./vendor/pdfjs-dist/build/pdf.worker.min.mjs", - "docx-preview": "./vendor/docx-preview/dist/docx-preview.mjs", - "jszip": "./vendor/jszip/dist/jszip.min.js", - "highlight.js": "./vendor/highlight.js/es/index.js", - "@lmstudio/sdk": "./vendor/@lmstudio/sdk/dist/index.mjs", - "ollama/browser": "./vendor/ollama/dist/browser.mjs", - "@sinclair/typebox": "./vendor/@sinclair/typebox/build/esm/index.mjs", - "xlsx": "./vendor/xlsx/xlsx.mjs", - ], - ] - - let importMapJSON: String = if let data = try? JSONSerialization.data( - withJSONObject: importMap, - options: [.prettyPrinted]), - let json = String(data: data, encoding: .utf8) - { - json - } else { - "{}" - } - - 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 - private func makeHTML(importMapJSON: String, messagesJSON: String) -> String { + let bootstrapScript = """ + window.__clawdisBootstrap = { + sessionKey: "\(self.sessionKey)", + initialMessages: \(self.initialMessagesJSON) + }; """ - - - - - - Clawd Web Chat - - - - - -
Booting web chat…
- - - - """ + self.webView.loadFileURL(htmlURL, allowingReadAccessTo: webChatURL) + webChatLogger.debug("loadPage queued HTML into WKWebView fileURL=\(htmlURL.absoluteString, privacy: .public)") } - // swiftlint:enable line_length - func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { webChatLogger.debug("didFinish navigation url=\(webView.url?.absoluteString ?? "nil", privacy: .public)") webView.evaluateJavaScript("document.body.innerText") { result, error in