Files
plugins/packages/agent-console/web/src/components/QuoteSelect.tsx
T
qingnian108 86778e6646 agent-console 1.3.7: auto-detect unlabelled code blocks, html-only sanitize, Windows desktop reconnect through Magpie's Try again (#792)
Code blocks are drawn in the same pass as the Markdown, so a streaming reply no longer flickers. Two fixes from the review on 40eb3027:
- An unlabelled fence is detected by highlight.js again (`highlightAuto`, behind a 128-entry LRU). Only a fence that is still open at the end of a streaming message stays plain "text" (`isClosedFence`, which follows marked's fence rule).
- DOMPurify is back to the html profile only. The code-block icons are per-load random placeholders until after sanitize, and `codeBlockKey` is gone.

The Windows desktop reconnect is also fixed. `safeBackUrl` (gate, wrapper and web) takes loopback Magpie pages only, never `http://wails.localhost`. WebView2 can't route a 127.0.0.1 frame's request there, so it showed its gray error page. In the desktop window the wrapper, or an unwrapped daemon page, posts `{magpie:1, method:"error"}` instead, and Magpie's pluginpages.js answers with its Try again card. A stale stored wails back URL is dropped.

Reviewed at c520fa46 (merge base 1eb9473f = main) on mote (mote-run, HOME=$(mktemp -d), package copied out of the workspace):
- npm ci; `npm run check` clean; `npm run verify:build` OK for all 5 files.
- `npx vitest run` with CHROME_PATH set to Playwright's Chromium 1248: 77 files, 567 passed, 17 skipped, 0 failed. markdown-render-browser ran (it went red under the hand-breaks).
- `bun scripts/check.mjs agent-console` ok. Repo `bun test`: 6449 pass, 0 fail.
- Broken by hand, each red:
  - autoDetect forced on while streaming: markdown-render-browser red.
  - `svg: true` put back in the profile: markdown-render-browser red.
  - Auto-detection removed: codeBlock and markdown-render-browser red.
  - LRU eviction removed: codeBlock red.
  - `isClosedFence` accepting any backtick run: codeBlock red.
  - wails.localhost accepted again in gate.mjs, then in backToMagpie.ts: ac11 red (3 and 2 tests).
  - The same in wrapper.mjs: wrapper red (4 tests).
  - The stale stored back kept: wrapper red.
- Read magpie's pluginpages.js on main: `method:"error"` from the page's own frame calls `broke(key, why)`.

Not tried: a real WebView2 on Windows (the reconnect path), or a real streaming reply in the desktop app.
2026-11-06 22:01:43 +08:00

134 lines
5.8 KiB
TypeScript

// 1.3 "添加到对话": select text in the conversation (replies, your own messages, code, tool output) or in the right
// panel (file preview, diff) and a small button offers to add it to the composer as a quote.
// - Only inside [data-quote-root] areas; never for selections in inputs, the composer, buttons or across two areas.
// - Hidden while the mouse is still dragging (no flicker), shown on release; it never takes the selection away
// (mousedown on it is cancelled), so Ctrl+C keeps working and the button stays while the pointer travels to it.
// - Placed above the selection, or below it when there is no room, inside the visible part of the scroller
// (above the composer), never off-screen at narrow widths. Follows scrolling; Esc or an empty selection hides it.
import { memo, useCallback, useEffect, useRef, useState } from "react";
import { createPortal } from "react-dom";
import { Icon } from "./Icons";
import { t } from "../i18n/index.js";
import { placeQuotePop, type Box } from "../composer/quotes";
type Pick = { text: string; src?: string; left: number; top: number };
const POP_W_FALLBACK = 118, POP_H = 30;
const EDITABLE = "input, textarea, select, [contenteditable=''], [contenteditable='true'], button, .cp-card, .qs-pop, [data-quote-off]";
function elOf(n: Node | null): Element | null {
return !n ? null : n.nodeType === 1 ? (n as Element) : n.parentElement;
}
/** The selection to quote, or null (outside a quote area, inside an input, spanning two areas, blank) */
export function quotableSelection(sel: Selection | null): { range: Range; text: string; root: Element; src?: string } | null {
if (!sel || sel.isCollapsed || sel.rangeCount === 0) return null;
const text = sel.toString();
if (!text.trim()) return null;
const a = elOf(sel.anchorNode), f = elOf(sel.focusNode);
if (!a || !f) return null;
const root = a.closest("[data-quote-root]");
if (!root || root !== f.closest("[data-quote-root]")) return null;
if (a.closest(EDITABLE) || f.closest(EDITABLE)) return null;
const range = sel.getRangeAt(0);
const srcEl = elOf(range.commonAncestorContainer)?.closest("[data-quote-src]");
const src = srcEl?.getAttribute("data-quote-src") || undefined;
return { range, text, root, src };
}
function visibleArea(root: Element): Box {
// The scroller the area lives in; for the chat, stop above the composer
let sc: Element | null = root;
while (sc && sc !== document.body) {
const o = getComputedStyle(sc).overflowY;
if (o === "auto" || o === "scroll") break;
sc = sc.parentElement;
}
const r = (sc && sc !== document.body ? sc : document.documentElement).getBoundingClientRect();
let bottom = Math.min(r.bottom, window.innerHeight);
const cp = document.querySelector(".composer");
if (root.getAttribute("data-quote-root") === "chat" && cp) bottom = Math.min(bottom, cp.getBoundingClientRect().top);
return { left: r.left, right: r.right, top: Math.max(r.top, 0) + 4, bottom: bottom - 4 };
}
/** memo: no props, Chat's streaming re-renders don't reach it */
export const QuoteSelect = memo(function QuoteSelect() {
const [pick, setPick] = useState<Pick | null>(null);
const dragging = useRef(false);
const popRef = useRef<HTMLButtonElement>(null);
const raf = useRef(0);
const update = useCallback(() => {
cancelAnimationFrame(raf.current);
raf.current = requestAnimationFrame(() => {
if (dragging.current) return;
const q = quotableSelection(window.getSelection());
if (!q) { setPick(null); return; }
const w = popRef.current?.offsetWidth || POP_W_FALLBACK;
const boxes = [...q.range.getClientRects()].map((r) => ({ left: r.left, top: r.top, right: r.right, bottom: r.bottom }));
const at = placeQuotePop(boxes, visibleArea(q.root), { w, h: POP_H }, window.innerWidth);
if (!at) { setPick(null); return; }
setPick({ text: q.text, src: q.src, left: at.left, top: at.top });
});
}, []);
useEffect(() => {
const down = (e: PointerEvent) => {
if ((e.target as Element | null)?.closest?.(".qs-pop")) return;
if (e.button !== 0) return;
dragging.current = true;
setPick(null);
};
const up = () => {
if (!dragging.current) return;
dragging.current = false;
update();
};
const key = (e: KeyboardEvent) => {
if (e.key === "Escape") { setPick(null); return; }
update();
};
const onSel = () => update();
const onScroll = () => update();
document.addEventListener("pointerdown", down, true);
document.addEventListener("pointerup", up, true);
document.addEventListener("keyup", key, true);
document.addEventListener("selectionchange", onSel);
window.addEventListener("scroll", onScroll, true);
window.addEventListener("resize", onScroll);
return () => {
document.removeEventListener("pointerdown", down, true);
document.removeEventListener("pointerup", up, true);
document.removeEventListener("keyup", key, true);
document.removeEventListener("selectionchange", onSel);
window.removeEventListener("scroll", onScroll, true);
window.removeEventListener("resize", onScroll);
cancelAnimationFrame(raf.current);
};
}, [update]);
if (!pick) return null;
const add = () => {
window.dispatchEvent(new CustomEvent("ac-add-quote", { detail: { text: pick.text, src: pick.src } }));
window.getSelection()?.removeAllRanges();
setPick(null);
};
return createPortal(
<button
ref={popRef}
type="button"
className="qs-pop"
style={{ left: pick.left, top: pick.top }}
title={t("quote.addHint")}
// Keep the selection (and so Ctrl+C) while clicking
onMouseDown={(e) => e.preventDefault()}
onPointerDown={(e) => e.preventDefault()}
onClick={add}
>
<Icon name="chat" size={13} />
<span>{t("quote.add")}</span>
</button>,
document.body,
);
});