From b84db8983d63a9a981083ebce1a139860c6bdf25 Mon Sep 17 00:00:00 2001
From: chunhualin1 <chunhualin1@lenovo.com>
Date: 星期五, 14 八月 2026 15:35:21 +0800
Subject: [PATCH] commit

---
 src/Moli.Desktop/WebView/WebViewHost.cs |   73 ++++++++++++++++++++++++++++++++++++
 1 files changed, 73 insertions(+), 0 deletions(-)

diff --git a/src/Moli.Desktop/WebView/WebViewHost.cs b/src/Moli.Desktop/WebView/WebViewHost.cs
index 59b2a10..95c87c6 100644
--- a/src/Moli.Desktop/WebView/WebViewHost.cs
+++ b/src/Moli.Desktop/WebView/WebViewHost.cs
@@ -25,6 +25,7 @@
     private PluginBridgeContext? _bridgeContext;
     private WebViewHostOptions? _options;
     private IDisposable? _audioSubscription;
+    private IDisposable? _llmSubscription;
     private bool _initialized;
 
     public WebViewHost(
@@ -67,6 +68,8 @@
         {
             _audioSubscription = _events.Subscribe(DispatchAudioSpectrumAsync);
         }
+
+        _llmSubscription = _events.Subscribe(DispatchLlmEventAsync);
 
         if (!string.IsNullOrWhiteSpace(options.InitialScript))
         {
@@ -123,10 +126,45 @@
         return Task.CompletedTask;
     }
 
+    public Task NotifyParentWindowPositionChangedAsync(CancellationToken cancellationToken = default)
+    {
+        if (!_initialized || Control.CoreWebView2 is null)
+        {
+            return Task.CompletedTask;
+        }
+
+        cancellationToken.ThrowIfCancellationRequested();
+
+        try
+        {
+            var controllerProperty = Control.GetType().GetProperty("CoreWebView2Controller");
+            if (controllerProperty?.GetValue(Control) is { } controller)
+            {
+                var method = controller.GetType().GetMethod("NotifyParentWindowPositionChanged");
+                method?.Invoke(controller, null);
+                return Task.CompletedTask;
+            }
+        }
+        catch
+        {
+        }
+
+        Control.Dispatcher.InvokeAsync(() =>
+        {
+            Control.InvalidateMeasure();
+            Control.InvalidateArrange();
+            Control.UpdateLayout();
+        }, System.Windows.Threading.DispatcherPriority.Render);
+
+        return Task.CompletedTask;
+    }
+
     public async ValueTask DisposeAsync()
     {
         _audioSubscription?.Dispose();
         _audioSubscription = null;
+        _llmSubscription?.Dispose();
+        _llmSubscription = null;
 
         if (Control.CoreWebView2 is not null)
         {
@@ -157,6 +195,41 @@
         return dispatcher.InvokeAsync(() => ExecuteScriptSafelyAsync(script)).Task.Unwrap();
     }
 
+    private Task DispatchLlmEventAsync(RuntimeEvent runtimeEvent)
+    {
+        if (_bridgeContext is null || runtimeEvent.Payload is null || !runtimeEvent.Name.StartsWith("llm.", StringComparison.OrdinalIgnoreCase))
+        {
+            return Task.CompletedTask;
+        }
+
+        var payloadJson = JsonSerializer.Serialize(runtimeEvent.Payload, _jsonOptions);
+        using var document = JsonDocument.Parse(payloadJson);
+        var root = document.RootElement;
+        if (!root.TryGetProperty("hostId", out var hostId) ||
+            !string.Equals(hostId.GetString(), HostId, StringComparison.OrdinalIgnoreCase) ||
+            !root.TryGetProperty("event", out var llmEvent))
+        {
+            return Task.CompletedTask;
+        }
+
+        var domEventName = runtimeEvent.Name switch
+        {
+            "llm.delta" => "moli:llm-delta",
+            "llm.completed" => "moli:llm-completed",
+            "llm.error" => "moli:llm-error",
+            _ => "moli:llm-event"
+        };
+        var detail = llmEvent.GetRawText();
+        var script = $"window.dispatchEvent(new CustomEvent(\"{domEventName}\", {{ detail: {detail} }}));";
+        var dispatcher = Control.Dispatcher;
+        if (dispatcher.CheckAccess())
+        {
+            return ExecuteScriptSafelyAsync(script);
+        }
+
+        return dispatcher.InvokeAsync(() => ExecuteScriptSafelyAsync(script)).Task.Unwrap();
+    }
+
     private async void OnWebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e)
     {
         if (_bridgeContext is null)

--
Gitblit v1.9.1