Repository navigation
Closed
Labels
enhancementNew feature or request
Description
Right now WebChatWindow and CanvasWindow both load gateway content in WebView2, but there's no formal communication channel between the native C# side and the embedded SPA. The two halves run disconnected, which shows up in a few places already:
| Surface | Current situation |
|---|---|
WebChatWindow |
No bridge at all. Native code has no way to signal UI state to the SPA (recording indicator, draft text, activity spinner) |
CanvasWindow |
One-way native→web channel via ExecuteScriptAsync with 11 heuristic fallbacks. No web→native channel |
| Voice PR #120 (NichUK) | Works around the gap with DOM scraping (querySelector, MutationObserver, descriptor hacks). The author describes it as fragile |
| Screen record PR #159 | No way to show a recording indicator in the SPA while the app is capturing |
QuickSendDialog |
Can send messages over WebSocket but can't coordinate UI state with the embedded chat |
The fix
WebView2 ships a proper bidirectional bridge that doesn't touch the DOM at all:
Native → SPA
// Inject bootstrap before any page scripts run
await webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(BridgeInitScript);
// Send a message at any point
webView.CoreWebView2.PostWebMessageAsJson(JsonSerializer.Serialize(msg));SPA → Native
webView.CoreWebView2.WebMessageReceived += (_, e) => {
var msg = JsonSerializer.Deserialize<BridgeMessage>(e.WebMessageAsJson);
HandleBridgeMessage(msg);
};Gateway SPA side
// Receive from native
window.chrome.webview.addEventListener('message', e => {
const msg = e.data; // { type, payload }
// dispatch by type
});
// Send to native
window.chrome.webview.postMessage({ type: 'ready' });Minimal message contract to start
{ "type": "recording-start", "payload": {} }
{ "type": "recording-stop", "payload": {} }
{ "type": "voice-start", "payload": {} }
{ "type": "voice-stop", "payload": {} }
{ "type": "draft-text", "payload": { "text": "..." } }
{ "type": "ready", "payload": {} }Scope
This touches two repos:
- This repo — wire the bridge in
WebChatWindow.xaml.csandCanvasWindow.xaml.cs - Gateway — expose the
window.chrome.webviewlistener in the SPA
The existing WebSocket transport (chat.send) stays as-is — it handles chat messages. The bridge is purely for UI state coordination.
Happy to put together a PR for the Windows side once there's alignment on the gateway contract.
Related
- Add first-pass Windows Voice Mode #120 (NichUK, Voice Mode) — the DOM scraping approach there would be cleanly replaced by this
- feat: add screen recording capability #159 (screen recording) — needs a recording-active indicator in the chat view
Metadata
Metadata
Assignees
Labels
enhancementNew feature or request
Type
Fields
Priority
None yet
Activity
feat: wire WebView2 bidirectional native↔SPA bridge in WebChatWindow
github-actions commentedon Apr 21, 2026
🤖 This is an automated response from Repo Assist.
Great proposal,
@AlexAlves87— the gap you've described is real and the WebView2PostWebMessageAsJson/WebMessageReceivedAPI is exactly the right fix.A few notes from looking at the existing code:
Current state
WebChatWindowCanvasWindowExecuteScriptAsyncwith 11 heuristic fallbacks (the fragile pattern you described)window.chrome.webviewis automatically available in any WebView2-hosted page — no bootstrap injection needed on the native side. That keeps the wiring minimal.Impact on open PRs
MutationObserver/querySelectorhacks would be cleanly replaced byvoice-start/voice-stopbridge messagesrecording-start/recording-stopPR
I've opened a draft PR ([#aw_pr191br]) that wires the full bidirectional bridge on the Windows side of
WebChatWindow:CoreWebView2.WebMessageReceived→ fires aBridgeMessageReceivedevent on the window (SPA→native)PostBridgeMessage(string type, object? payload)method (native→SPA viaPostWebMessageAsJson)WebBridgeMessagerecord inOpenClaw.SharedwithTryParse/ToJsonand the well-known type constants from your proposalOnWindowClosed— no leakCallers in
App.xaml.cscan subscribe toBridgeMessageReceivedand callPostBridgeMessageonce the gateway SPA side exposes the listener.The SPA-side change (adding
window.chrome.webview.addEventListener('message', ...)to the gateway) is out of scope for this repo — that's the remaining piece to close the loop.shanselman commentedon Apr 28, 2026
Implemented the Windows WebChat side of this proposal in #192 / c7630fa.
Credit to @AlexAlves87 for the original WebView2 native ↔ SPA bridge proposal, the
{ type, payload }message-contract shape, and the parity rationale around avoiding DOM scraping for voice, recording indicators, draft text, and future UI-state coordination.The merged implementation follows the core direction from this issue and adds Windows-side hardening around origin validation, dispatcher marshaling for WebView2 calls, closed-window guards, sanitized bridge logging, and payload JSON validation.
shanselman commentedon May 1, 2026
Thanks again for writing this up, @AlexAlves87. This proposal has been useful architecture guidance, and the first Windows-side slice landed in #192 / c7630fa.
Current status after #192:
{ type, payload }WebBridgeMessagecontractWebChatWindowSPA → native bridge viaWebMessageReceivedWebChatWindownative → SPA bridge viaPostWebMessageAsJsonCanvasWindow, replacing fragileExecuteScriptAsyncmessage paths where appropriatewindow.chrome.webview.addEventListener('message', ...)I don’t think we should close this as fully completed yet unless we intentionally narrow it to “WebChat bridge only.” The issue’s original scope also called out
CanvasWindowand gateway/SPA-side listener work, and #192 deliberately left those as follow-ups.Suggested next steps:
CanvasWindowbridge parity.That keeps the original architectural direction intact while letting us continue landing the work in smaller reviewable slices.
AlexAlves87 commentedon May 1, 2026
Thanks for the detailed status breakdown, @shanselman. I'll take the
CanvasWindowbridge parity as the next slice, same pattern asWebChatWindow, replacing theExecuteScriptAsyncpaths where appropriate.feat: wire WebView2 native↔SPA bridge in CanvasWindow
feat: wire WebView2 native↔SPA bridge in CanvasWindow
feat: wire WebView2 native↔SPA bridge in CanvasWindow (#259)
AlexAlves87 commentedon May 7, 2026
{ type, payload }WebBridgeMessagecontractWebChatWindowSPA → native bridge viaWebMessageReceivedWebChatWindownative → SPA bridge viaPostWebMessageAsJsonCanvasWindow— closed by feat: wire WebView2 native↔SPA bridge in CanvasWindow #259window.chrome.webview.addEventListener('message', ...)— in progress at feat(ui): add SPA-side support for WebView2 native bridge openclaw#69633shanselman commentedon May 7, 2026
Status update after the latest triage:
window.chrome.webview.addEventListener('message', ...), tracked in feat(ui): add SPA-side support for WebView2 native bridge openclaw#69633) plus concrete producers/consumers for recording, voice, and draft-text messages.Keeping this issue open as the tracker for those remaining bridge integration pieces.
shanselman commentedon May 18, 2026
Closing this as completed enough for the original architecture proposal.
What landed:
Future work should be tracked as feature-specific producer/consumer issues, not by keeping this broad bridge proposal open indefinitely.