From 9b5711d3124622b1f1b18500d442c76492da877a Mon Sep 17 00:00:00 2001
From: chunhualin1 <chunhualin1@lenovo.com>
Date: 星期一, 17 八月 2026 16:52:33 +0800
Subject: [PATCH] 优化llm插件及bug修复

---
 samples/plugins/sample.llm-command-palette/index.html |  342 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 src/Moli.Desktop/Windows/WidgetWindow.cs              |    1 
 2 files changed, 321 insertions(+), 22 deletions(-)

diff --git a/samples/plugins/sample.llm-command-palette/index.html b/samples/plugins/sample.llm-command-palette/index.html
index fded937..87b804c 100644
--- a/samples/plugins/sample.llm-command-palette/index.html
+++ b/samples/plugins/sample.llm-command-palette/index.html
@@ -32,6 +32,7 @@
     html,
     body {
       width: 100%;
+      height: 100%;
       min-width: 0;
       margin: 0;
       overflow: hidden;
@@ -45,13 +46,20 @@
 
     .palette-shell {
       width: 100%;
+      height: 100%;
       min-width: 360px;
-      min-height: 250px;
+      min-height: 0;
       padding: 8px;
+      display: flex;
+      flex-direction: column;
     }
 
     .palette {
       width: 100%;
+      flex: 1 1 auto;
+      min-height: 0;
+      display: flex;
+      flex-direction: column;
       border: 1px solid var(--line);
       border-radius: 20px;
       background: var(--bar-bg-solid);
@@ -72,6 +80,7 @@
       grid-template-columns: 24px minmax(96px, 154px) 1fr auto minmax(92px, 132px);
       align-items: center;
       gap: 8px;
+      flex: 0 0 auto;
       min-height: 54px;
       padding: 0 14px 0 18px;
     }
@@ -264,7 +273,11 @@
       max-height: 0;
       opacity: 0;
       overflow: hidden;
-      border-top: 1px solid transparent;
+      border-top: 0 solid transparent;
+      transition: opacity 140ms ease, border-color 140ms ease;
+    }
+
+    .memory-panel {
       transition: max-height 160ms ease, opacity 140ms ease, border-color 140ms ease;
     }
 
@@ -272,17 +285,74 @@
     .memory-panel.open {
       max-height: var(--panel-max-height);
       opacity: 1;
-      border-top-color: rgba(255, 255, 255, 0.08);
+      border-top: 1px solid rgba(255, 255, 255, 0.08);
+    }
+
+    .output-panel.open {
+      flex: 1 0 auto;
+      min-height: 0;
+      display: flex;
+      flex-direction: column;
+      overflow: hidden;
+      max-height: none;
+    }
+
+    .output-scroll {
+      position: relative;
+      flex: 1 1 auto;
+      min-height: 0;
     }
 
     .output-inner {
+      height: auto;
       max-height: var(--response-max-height);
-      overflow: auto;
-      padding: 14px 18px 8px 18px;
+      overflow-x: hidden;
+      overflow-y: auto;
+      overscroll-behavior: contain;
+      padding: 8px 22px 14px 18px;
       color: rgba(255, 255, 255, 0.86);
       font: 14px/1.58 "Segoe UI", "Microsoft YaHei UI", system-ui, sans-serif;
       white-space: pre-wrap;
       user-select: text;
+      background: rgba(0, 0, 0, 0.16);
+    }
+
+    .output-inner::-webkit-scrollbar {
+      width: 0;
+      height: 0;
+    }
+
+    .output-scrollbar {
+      position: absolute;
+      top: 8px;
+      right: 6px;
+      bottom: 8px;
+      width: 10px;
+      border-radius: 999px;
+      background: rgb(58, 56, 54);
+      opacity: 0.5;
+      z-index: 4;
+    }
+
+    .output-scrollbar[hidden] {
+      display: none;
+    }
+
+    .output-thumb {
+      position: absolute;
+      left: 1px;
+      right: 1px;
+      min-height: 28px;
+      border-radius: 999px;
+      background: rgb(196, 194, 191);
+      cursor: grab;
+      touch-action: none;
+    }
+
+    .output-thumb:hover,
+    .output-thumb.dragging {
+      background: rgb(230, 228, 225);
+      cursor: grabbing;
     }
 
     .output-inner.error {
@@ -306,21 +376,35 @@
     }
 
     .actions {
+      flex: 0 0 auto;
       display: flex;
       justify-content: flex-end;
       gap: 6px;
-      padding: 0 12px 10px;
+      padding: 8px 12px 0;
     }
 
     .icon-action {
       width: 30px;
       height: 28px;
+      display: grid;
+      place-items: center;
       border: 1px solid transparent;
       border-radius: 8px;
       background: transparent;
       color: var(--muted);
       font: 600 13px/1 "Segoe UI", system-ui, sans-serif;
       cursor: pointer;
+    }
+
+    .icon-action svg {
+      width: 14px;
+      height: 14px;
+      display: block;
+      fill: none;
+      stroke: currentColor;
+      stroke-width: 1.7;
+      stroke-linecap: round;
+      stroke-linejoin: round;
     }
 
     .icon-action:hover,
@@ -494,13 +578,30 @@
       </div>
 
       <div id="outputPanel" class="output-panel" aria-live="polite" role="region" aria-label="LLM response">
-        <div id="outputText" class="output-inner" data-placeholder="Waiting for response"></div>
         <div class="actions">
-          <div id="copyButton" class="icon-action" role="button" tabindex="0" title="Copy" aria-label="Copy">C</div>
-          <div id="retryButton" class="icon-action" role="button" tabindex="0" title="Regenerate"
-            aria-label="Regenerate">R</div>
-          <div id="collapseButton" class="icon-action" role="button" tabindex="0" title="Collapse"
-            aria-label="Collapse">X</div>
+          <div id="copyButton" class="icon-action" role="button" tabindex="0" title="Copy" aria-label="Copy">
+            <svg viewBox="0 0 16 16" aria-hidden="true">
+              <rect x="5.2" y="5.2" width="8" height="8" rx="1.4"></rect>
+              <path d="M10.8 5.2V3.8A1.4 1.4 0 0 0 9.4 2.4H3.8A1.4 1.4 0 0 0 2.4 3.8v5.6A1.4 1.4 0 0 0 3.8 10.8H5.2"></path>
+            </svg>
+          </div>
+          <div id="retryButton" class="icon-action" role="button" tabindex="0" title="Regenerate" aria-label="Regenerate">
+            <svg viewBox="0 0 16 16" aria-hidden="true">
+              <path d="M13.2 8A5.2 5.2 0 1 1 11.4 3.7"></path>
+              <path d="M11.2 2.2h2.4V4.6"></path>
+            </svg>
+          </div>
+          <div id="collapseButton" class="icon-action" role="button" tabindex="0" title="Collapse" aria-label="Collapse">
+            <svg viewBox="0 0 16 16" aria-hidden="true">
+              <path d="M4 4l8 8M12 4l-8 8"></path>
+            </svg>
+          </div>
+        </div>
+        <div class="output-scroll">
+          <div id="outputText" class="output-inner" tabindex="0" data-placeholder="Waiting for response"></div>
+          <div id="outputScrollbar" class="output-scrollbar" hidden>
+            <div id="outputThumb" class="output-thumb"></div>
+          </div>
         </div>
       </div>
     </div>
@@ -530,9 +631,10 @@
       config: {}
     };
 
-    let _resizeTimer;
+    let _resizeFrame = 0;
 
     const els = {
+      paletteShell: document.querySelector(".palette-shell"),
       statusMark: document.getElementById("statusMark"),
       projectSelect: document.getElementById("projectSelect"),
       projectMenu: document.getElementById("projectMenu"),
@@ -541,6 +643,8 @@
       promptInput: document.getElementById("promptInput"),
       outputPanel: document.getElementById("outputPanel"),
       outputText: document.getElementById("outputText"),
+      outputScrollbar: document.getElementById("outputScrollbar"),
+      outputThumb: document.getElementById("outputThumb"),
       memoryButton: document.getElementById("memoryButton"),
       memoryPanel: document.getElementById("memoryPanel"),
       memoryList: document.getElementById("memoryList"),
@@ -622,6 +726,78 @@
       els.statusMark.title = title || "Idle";
     }
 
+    function getResponseMaxHeight() {
+      const raw = getComputedStyle(document.documentElement).getPropertyValue("--response-max-height");
+      const parsed = parseFloat(raw);
+      return Number.isFinite(parsed) && parsed > 0 ? parsed : 376;
+    }
+
+    function getShellChrome() {
+      const palette = document.querySelector(".palette");
+      const shellStyle = getComputedStyle(els.paletteShell);
+      const paletteStyle = getComputedStyle(palette);
+      return (parseFloat(shellStyle.paddingTop) || 0)
+        + (parseFloat(shellStyle.paddingBottom) || 0)
+        + (parseFloat(paletteStyle.borderTopWidth) || 0)
+        + (parseFloat(paletteStyle.borderBottomWidth) || 0);
+    }
+
+    function getChromeHeight() {
+      const inputRow = document.querySelector(".input-row");
+      const actions = document.querySelector(".actions");
+      const memoryH = els.memoryPanel.offsetHeight;
+      const actionsH = els.outputPanel.classList.contains("open") && actions ? actions.offsetHeight : 0;
+      const panelStyle = getComputedStyle(els.outputPanel);
+      const panelBorder = els.outputPanel.classList.contains("open")
+        ? (parseFloat(panelStyle.borderTopWidth) || 0) + (parseFloat(panelStyle.borderBottomWidth) || 0)
+        : 0;
+      return getShellChrome() + (inputRow ? inputRow.offsetHeight : 54) + memoryH + actionsH + panelBorder;
+    }
+
+    function measureOutputNaturalHeight() {
+      const el = els.outputText;
+      const prevHeight = el.style.height;
+      const prevMax = el.style.maxHeight;
+      el.style.height = "auto";
+      el.style.maxHeight = "none";
+      const natural = el.scrollHeight;
+      el.style.height = prevHeight;
+      el.style.maxHeight = prevMax;
+      return natural;
+    }
+
+    function getOutputScrollPortHeight(natural) {
+      const cssMax = getResponseMaxHeight();
+      const available = window.innerHeight - getChromeHeight();
+      if (available >= natural - 1) return Math.min(natural, cssMax);
+      if (available > 48) return Math.min(natural, cssMax, available);
+      return Math.min(natural, cssMax);
+    }
+
+    function syncOutputScrollbar() {
+      const scroller = els.outputText;
+      const track = els.outputScrollbar;
+      const thumb = els.outputThumb;
+      if (!scroller || !track || !thumb) return;
+      const canScroll = scroller.scrollHeight > scroller.clientHeight + 1;
+      track.hidden = !canScroll;
+      if (!canScroll) return;
+      const trackH = track.clientHeight;
+      const thumbH = Math.max(28, Math.floor(trackH * (scroller.clientHeight / scroller.scrollHeight)));
+      const maxTop = Math.max(0, trackH - thumbH);
+      const range = scroller.scrollHeight - scroller.clientHeight;
+      const progress = range > 0 ? scroller.scrollTop / range : 0;
+      thumb.style.height = thumbH + "px";
+      thumb.style.top = (maxTop * progress) + "px";
+    }
+
+    function syncOutputScrollbox() {
+      const natural = measureOutputNaturalHeight();
+      const port = getOutputScrollPortHeight(natural);
+      els.outputText.style.height = natural > port + 1 ? port + "px" : "";
+      syncOutputScrollbar();
+    }
+
     function setOutput(text, options = {}) {
       state.output = text || "";
       els.outputText.classList.toggle("error", options.error === true);
@@ -633,7 +809,9 @@
       }
       els.outputPanel.classList.add("open");
       requestHostResize();
+      syncOutputScrollbox();
       els.outputText.scrollTop = els.outputText.scrollHeight;
+      syncOutputScrollbar();
     }
 
     function appendOutput(delta) {
@@ -643,7 +821,63 @@
 
     function collapseOutput() {
       els.outputPanel.classList.remove("open");
-      requestHostResize(72);
+      els.outputText.style.height = "";
+      els.outputText.style.maxHeight = "";
+      if (els.outputScrollbar) els.outputScrollbar.hidden = true;
+      closeSelects();
+      requestPaletteResize();
+    }
+
+    function bindOutputScrolling() {
+      let drag = null;
+      els.outputText.addEventListener("scroll", syncOutputScrollbar);
+      document.addEventListener("wheel", (event) => {
+        if (!els.outputPanel.classList.contains("open")) return;
+        if (event.target.closest(".select-menu, .memory-list")) return;
+        if (els.outputText.scrollHeight <= els.outputText.clientHeight + 1) return;
+        els.outputText.scrollTop += event.deltaY;
+        event.preventDefault();
+        syncOutputScrollbar();
+      }, { passive: false });
+
+      els.outputThumb.addEventListener("pointerdown", (event) => {
+        drag = {
+          pointerId: event.pointerId,
+          startY: event.clientY,
+          startTop: els.outputThumb.offsetTop
+        };
+        els.outputThumb.classList.add("dragging");
+        els.outputThumb.setPointerCapture(event.pointerId);
+        event.preventDefault();
+        event.stopPropagation();
+      });
+      els.outputThumb.addEventListener("pointermove", (event) => {
+        if (!drag || event.pointerId !== drag.pointerId) return;
+        const trackH = els.outputScrollbar.clientHeight;
+        const thumbH = els.outputThumb.offsetHeight;
+        const maxTop = Math.max(0, trackH - thumbH);
+        const top = Math.min(maxTop, Math.max(0, drag.startTop + (event.clientY - drag.startY)));
+        const range = els.outputText.scrollHeight - els.outputText.clientHeight;
+        els.outputText.scrollTop = maxTop > 0 && range > 0 ? (top / maxTop) * range : 0;
+        syncOutputScrollbar();
+      });
+      const endDrag = (event) => {
+        if (!drag || event.pointerId !== drag.pointerId) return;
+        drag = null;
+        els.outputThumb.classList.remove("dragging");
+      };
+      els.outputThumb.addEventListener("pointerup", endDrag);
+      els.outputThumb.addEventListener("pointercancel", endDrag);
+      els.outputScrollbar.addEventListener("pointerdown", (event) => {
+        if (event.target === els.outputThumb) return;
+        const rect = els.outputScrollbar.getBoundingClientRect();
+        const thumbH = els.outputThumb.offsetHeight;
+        const maxTop = Math.max(0, els.outputScrollbar.clientHeight - thumbH);
+        const top = Math.min(maxTop, Math.max(0, event.clientY - rect.top - thumbH / 2));
+        const range = els.outputText.scrollHeight - els.outputText.clientHeight;
+        els.outputText.scrollTop = maxTop > 0 && range > 0 ? (top / maxTop) * range : 0;
+        syncOutputScrollbar();
+      });
     }
 
     function resizeTextarea() {
@@ -652,20 +886,76 @@
       els.promptInput.style.height = height + "px";
     }
 
+    function isSelectOpen(control) {
+      return control.classList.contains("open");
+    }
+
+    function isPaletteExpanded() {
+      return els.outputPanel.classList.contains("open")
+        || els.memoryPanel.classList.contains("open")
+        || isSelectOpen(els.projectSelect)
+        || isSelectOpen(els.modelSelect);
+    }
+
+    function getOpenMenuExtent() {
+      let bottom = 0;
+      for (const control of [els.projectSelect, els.modelSelect]) {
+        if (!isSelectOpen(control)) continue;
+        const menu = control.nextElementSibling;
+        if (!menu) continue;
+        const rect = menu.getBoundingClientRect();
+        if (rect.height > 0) {
+          bottom = Math.max(bottom, Math.ceil(rect.bottom + 12));
+        }
+      }
+      return bottom;
+    }
+
+    function syncOpenMenuSpace() {
+      const extent = getOpenMenuExtent();
+      els.paletteShell.style.minHeight = extent > 0 ? extent + "px" : "";
+    }
+
+    function measureDesiredHeight() {
+      const outputH = els.outputPanel.classList.contains("open")
+        ? Math.min(measureOutputNaturalHeight(), getResponseMaxHeight())
+        : 0;
+      return Math.max(Math.ceil(getChromeHeight() + outputH + 15), getOpenMenuExtent());
+    }
+
     function requestHostResize(explicitHeight) {
       if (!bridge || typeof bridge.requestResize !== "function") return;
       const screenHeight = window.screen && window.screen.availHeight ? window.screen.availHeight : 900;
       const heightLimit = Math.floor(screenHeight * 0.5) + 88;
-      const height = explicitHeight || Math.min(Math.ceil(document.body.scrollHeight + 2), heightLimit);
+      const apply = (height) => {
+        bridge.requestResize({ height }).catch(() => {});
+      };
+      if (_resizeFrame) {
+        cancelAnimationFrame(_resizeFrame);
+        _resizeFrame = 0;
+      }
       if (explicitHeight != null) {
-        clearTimeout(_resizeTimer);
-        bridge.requestResize({ width: Math.ceil(document.body.scrollWidth), height }).catch(() => {});
+        apply(explicitHeight);
         return;
       }
-      clearTimeout(_resizeTimer);
-      _resizeTimer = setTimeout(function() {
-        bridge.requestResize({ width: Math.ceil(document.body.scrollWidth), height }).catch(() => {});
-      }, 50);
+      _resizeFrame = requestAnimationFrame(() => {
+        _resizeFrame = 0;
+        syncOpenMenuSpace();
+        apply(Math.min(measureDesiredHeight(), heightLimit));
+      });
+    }
+
+    function requestPaletteResize() {
+      if (!isPaletteExpanded()) {
+        syncOpenMenuSpace();
+        requestHostResize(72);
+        return;
+      }
+      requestHostResize();
+    }
+
+    function scheduleSelectResize() {
+      requestAnimationFrame(() => requestPaletteResize());
     }
 
     function updatePanelLimits() {
@@ -673,6 +963,9 @@
       const panelMax = Math.max(180, Math.floor(screenHeight * 0.5));
       document.documentElement.style.setProperty("--panel-max-height", panelMax + "px");
       document.documentElement.style.setProperty("--response-max-height", Math.max(132, panelMax - 44) + "px");
+      if (els.outputPanel.classList.contains("open")) {
+        syncOutputScrollbox();
+      }
     }
 
     function closeSelects() {
@@ -742,6 +1035,7 @@
         item.addEventListener("click", () => {
           setSelectValue(control, menu, option);
           closeSelects();
+          scheduleSelectResize();
         });
         menu.appendChild(item);
       }
@@ -765,6 +1059,7 @@
         }
         control.classList.toggle("open", willOpen);
         control.setAttribute("aria-expanded", willOpen ? "true" : "false");
+        scheduleSelectResize();
       });
       control.addEventListener("keydown", (event) => {
         const options = [...menu.children];
@@ -778,6 +1073,7 @@
           options[nextIndex].click();
         } else if (event.key === "Escape") {
           closeSelects();
+          scheduleSelectResize();
         }
       });
       menu.addEventListener("scroll", () => {
@@ -1102,6 +1398,7 @@
     function bindUi() {
       bindSelect(els.projectSelect, els.projectMenu);
       bindSelect(els.modelSelect, els.modelMenu);
+      bindOutputScrolling();
 
       window.addEventListener("moli:interactable-changed", (event) => {
         const detail = event.detail || {};
@@ -1149,7 +1446,7 @@
         els.memoryPanel.classList.toggle("open", isOpen);
         els.memoryButton.classList.toggle("active", isOpen);
         if (isOpen) await loadMemories();
-        requestHostResize();
+        requestPaletteResize();
       });
 
       invokeOnAction(els.addMemoryButton, () => createMemory(getText(els.memoryInput)));
@@ -1178,6 +1475,7 @@
       document.addEventListener("click", (event) => {
         if (!els.projectSelect.parentElement.contains(event.target) && !els.modelSelect.parentElement.contains(event.target)) {
           closeSelects();
+          scheduleSelectResize();
         }
       });
     }
diff --git a/src/Moli.Desktop/Windows/WidgetWindow.cs b/src/Moli.Desktop/Windows/WidgetWindow.cs
index 4d6a24e..39dfa9a 100644
--- a/src/Moli.Desktop/Windows/WidgetWindow.cs
+++ b/src/Moli.Desktop/Windows/WidgetWindow.cs
@@ -183,6 +183,7 @@
             ? height.Value
             : null;
         Height = Math.Max(_layout.Bounds.Height, _requestedHeight ?? _layout.Bounds.Height);
+        _ = _webView.NotifyParentWindowPositionChangedAsync();
     }
 
     public Task SetEditModeAsync(bool enabled, CancellationToken cancellationToken = default)

--
Gitblit v1.9.1