mac: fix web chat boot in WKWebView

This commit is contained in:
Peter Steinberger
2025-12-06 21:33:35 +01:00
parent e528b439bc
commit 6182b205c8
2 changed files with 96 additions and 65 deletions

View File

@@ -80,11 +80,16 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler
let importMap = [ let importMap = [
"imports": [ "imports": [
"@mariozechner/pi-web-ui": "file://\(distPath)/index.js", "@mariozechner/pi-web-ui": "file://\(distPath)/index.js",
"@mariozechner/pi-web-ui/": "file://\(distPath)/",
"@mariozechner/pi-ai": "file://\(piAi)", "@mariozechner/pi-ai": "file://\(piAi)",
"@mariozechner/pi-ai/": "file://\(vendor.appendingPathComponent("pi-ai/").path(percentEncoded: false))",
"@mariozechner/mini-lit": "file://\(miniLit)", "@mariozechner/mini-lit": "file://\(miniLit)",
"@mariozechner/mini-lit/": "file://\(vendor.appendingPathComponent("mini-lit/").path(percentEncoded: false))",
"lit": "file://\(lit)", "lit": "file://\(lit)",
"lit/": "file://\(vendor.appendingPathComponent("lit/").path(percentEncoded: false))",
"lucide": "file://\(lucide)", "lucide": "file://\(lucide)",
"pdfjs-dist": "file://\(pdfjs)", "pdfjs-dist": "file://\(pdfjs)",
"pdfjs-dist/": "file://\(vendor.appendingPathComponent("pdfjs-dist/").path(percentEncoded: false))",
"pdfjs-dist/build/pdf.worker.min.mjs": "file://\(pdfWorker)", "pdfjs-dist/build/pdf.worker.min.mjs": "file://\(pdfWorker)",
], ],
] ]
@@ -114,9 +119,23 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler
<body> <body>
<div id="app"></div> <div id="app"></div>
<script type="module"> <script type="module">
const status = (msg) => {
console.log(msg);
window.__clawdisLog(msg);
const el = document.getElementById('app');
if (el && !el.dataset.booted) {
el.textContent = msg;
}
};
status('boot: starting imports');
(async () => {
try { try {
const { Agent, ChatPanel, AppStorage, setAppStorage } = await import('@mariozechner/pi-web-ui'); const { Agent, ChatPanel, AppStorage, setAppStorage } = await import('@mariozechner/pi-web-ui');
status('boot: pi-web-ui imported');
const { getModel } = await import('@mariozechner/pi-ai'); const { getModel } = await import('@mariozechner/pi-ai');
status('boot: pi-ai imported');
class NativeTransport { class NativeTransport {
async *run(messages, userMessage, cfg, signal) { async *run(messages, userMessage, cfg, signal) {
@@ -172,7 +191,11 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler
panel.style.height = '100%'; panel.style.height = '100%';
panel.style.display = 'block'; panel.style.display = 'block';
await panel.setAgent(agent); await panel.setAgent(agent);
document.getElementById('app').appendChild(panel); const mount = document.getElementById('app');
mount.dataset.booted = '1';
mount.textContent = '';
mount.appendChild(panel);
status('boot: ready');
} catch (err) { } catch (err) {
const msg = err?.stack || err?.message || String(err); const msg = err?.stack || err?.message || String(err);
window.__clawdisLog(msg); window.__clawdisLog(msg);
@@ -181,6 +204,7 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler
document.body.style.padding = '16px'; document.body.style.padding = '16px';
document.body.innerText = 'Web chat failed to load:\\n' + msg; document.body.innerText = 'Web chat failed to load:\\n' + msg;
} }
})();
</script> </script>
</body> </body>
</html> </html>
@@ -191,8 +215,15 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard let body = message.body as? [String: Any], guard let body = message.body as? [String: Any],
let id = body["id"] as? String, let id = body["id"] as? String,
let type = body["type"] as? String, let type = body["type"] as? String
type == "chat", else { return }
if id == "log", let log = body["log"] as? String {
NSLog("WebChat JS: %@", log)
return
}
guard type == "chat",
let payload = body["payload"] as? [String: Any], let payload = body["payload"] as? [String: Any],
let text = payload["text"] as? String let text = payload["text"] as? String
else { return } else { return }

View File

@@ -23,5 +23,5 @@ The macOS Clawdis app ships a built-in web chat window that reuses your primary
## Usage ## Usage
- Launch the macOS Clawdis menu bar app, click the lobster icon → “Open Web Chat”. - Launch the macOS Clawdis menu bar app, click the lobster icon → “Open Chat”.
- Type and send; replies continue the primary Clawd session. - Type and send; replies continue the primary Clawd session.