Files
plugins/packages/agent-console/web/src/App.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

85 lines
3.4 KiB
TypeScript

// App shell: connection state → three-column layout. Each area is filled in by P2-2..P2-6.
import { useConnection } from "./stores/connection";
import { Layout } from "./components/Layout";
import { Sidebar } from "./components/Sidebar";
import { Topbar } from "./components/Topbar";
import { Chat } from "./components/Chat";
import { useTimeline } from "./stores/timeline";
import { useData } from "./stores/data";
import { useEffect, useMemo, useState } from "react";
import { CommandPalette } from "./components/CommandPalette";
import { Panel } from "./components/Panel";
import { SettingsDialog } from "./components/Settings";
import { Toasts } from "./components/Toast";
import { t } from "./i18n/index.js";
import { goBackToMagpie, isWrapped, reportLostToWrapper } from "./api/backToMagpie.js";
/** No thread waiting: one shared Set, so Sidebar's prop stays the same reference */
const NO_PENDING_THREADS: Set<string> = new Set<string>();
export function App() {
const { state, version } = useConnection();
// Field-level selectors: a token delta (assistant/reasoning/tool output) changes none of these, so the shell
// (Layout, Sidebar, Topbar, Panel) doesn't re-render while a reply streams
const pendingCount = useTimeline((s) => s.pending.length);
const pendingKey = useTimeline((s) => s.pending[0]?.key);
const pendingThreadId = useTimeline((s) => (s.pending.length > 0 ? s.threadId : null));
const pendingByThread = useMemo<Set<string>>(
() => (pendingThreadId ? new Set([pendingThreadId]) : NO_PENDING_THREADS),
[pendingThreadId],
);
const providers = useData((s) => s.providers);
const [palette, setPalette] = useState(false);
const [settingsOpen, setSettingsOpen] = useState(false);
// Ctrl+K command palette; open-settings custom event; Esc closes panels
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === "k") {
e.preventDefault();
setPalette((p) => !p);
}
};
const onSettings = () => setSettingsOpen(true);
window.addEventListener("keydown", onKey);
window.addEventListener("open-settings", onSettings);
return () => {
window.removeEventListener("keydown", onKey);
window.removeEventListener("open-settings", onSettings);
};
}, []);
if (state === "down") {
return (
<div className="boot-screen">
<div className="boot-wrap">
<span>{t("boot.serviceDown")}</span>
<button className="btn primary" onClick={() => { if (isWrapped()) reportLostToWrapper(); else if (!goBackToMagpie()) location.reload(); }}>{t("boot.retry")}</button>
</div>
</div>
);
}
if (state === "connecting" && !version) {
return (
<div className="boot-screen">
<div className="boot-dot" data-state={state} />
<span className="boot-label">{t("boot.connecting")}</span>
</div>
);
}
return (
<Layout
sidebar={<Sidebar pendingByThread={pendingByThread} />}
topbar={<Topbar pendingCount={pendingCount} pendingKey={pendingKey} />}
panel={<Panel />}
>
{state === "reconnecting" && <div className="connbar">{t("boot.reconnecting")}</div>}
<Chat />
{palette && <CommandPalette onClose={() => setPalette(false)} />}
{settingsOpen && <SettingsDialog onClose={() => setSettingsOpen(false)} providers={providers} />}
<Toasts />
</Layout>
);
}