perf(webui): trim debug-view rendering cost (json-tree, activity, tester)

- json-tree:折叠节点不再枚举子项(改用 count),整棵树的 JSON.stringify 从每次渲染
  改为点击复制时才算。
- live-activity:搜索命中串在入队时预生成,过滤不再对每行 JSON.stringify(300 行 ×
  每次击键)。
- action-tester:流式帧封顶 2000,长/失控流不再无限增长数组与 DOM。

均行为保持(或纯改善),对抗性 Agent 逐条核验无回归。
This commit is contained in:
DrSmoothl committed 2026-07-02 15:02:13 +08:00
1 parent 99547e847e
commit c85c510c0b
3 files changed
+27 -14

No files matched your search

@@ -25,6 +25,9 @@ const cardCls = 'rounded-2xl border border-border/60 bg-card/80 shadow-[0_1px_2p
// positives just add one extra click; false negatives skip a confirm — both low
// stakes (the amber banner already warns that any write is real).
const DESTRUCTIVE_RE = /(kick|ban|mute|recall|withdraw|delete|del_|dismiss|disband|leave|quit|remove|revoke)/i;
// Cap retained stream frames so a long-running / runaway stream can't grow the
// array and DOM without bound. Generous enough to keep a full normal stream.
const MAX_STREAM_FRAMES = 2000;
function qqAvatarUrl(uin: string) {
return `/avatar/${encodeURIComponent(uin)}`;
@@ -136,7 +139,7 @@ export function ActionTester({ accounts, docs, presetAction }: { accounts: QQInf
try {
await api.debug.invokeStream(uin, actionName.trim(), params, (frame) => {
last = frame;
setFrames((prev) => [...prev, frame]);
setFrames((prev) => (prev.length >= MAX_STREAM_FRAMES ? [...prev.slice(-(MAX_STREAM_FRAMES - 1)), frame] : [...prev, frame]));
const d = frame.data as { type?: string; progress?: number; transferred?: number; total?: number } | undefined;
if (d && typeof d.progress === 'number') tasks.update(taskId, { progress: d.progress });
else if (d && typeof d.transferred === 'number' && typeof d.total === 'number' && d.total > 0) tasks.update(taskId, { progress: d.transferred / d.total });
@@ -14,7 +14,7 @@ import type { DebugStreamMessage } from '@/types';
const STREAM_CAP = 300;
const cardCls = 'rounded-2xl border border-border/60 bg-card/80 shadow-[0_1px_2px_rgb(0_0_0/0.04),0_8px_24px_-12px_rgb(0_0_0/0.10)] backdrop-blur-sm';
interface StreamRow { id: number; at: number; msg: Extract<DebugStreamMessage, { kind: 'event' | 'action' | 'dropped' }> }
interface StreamRow { id: number; at: number; msg: Extract<DebugStreamMessage, { kind: 'event' | 'action' | 'dropped' }>; search: string }
function IconBtn({ onClick, title, children }: { onClick: () => void; title: string; children: ReactNode }) {
return (
@@ -103,7 +103,9 @@ export function LiveActivity() {
(m) => {
if (m.kind === 'ready' || pausedRef.current) return;
setRows((prev) => {
const next = [{ id: idRef.current++, at: Date.now(), msg: m }, ...prev];
// Precompute the search haystack once at enqueue, so filtering never
// re-stringifies every row on each keystroke.
const next = [{ id: idRef.current++, at: Date.now(), msg: m, search: JSON.stringify(m).toLowerCase() }, ...prev];
return next.length > STREAM_CAP ? next.slice(0, STREAM_CAP) : next;
});
},
@@ -119,7 +121,7 @@ export function LiveActivity() {
if (!q) return true;
if (r.msg.kind === 'dropped') return false;
const { label } = rowLabel(r.msg);
return label.toLowerCase().includes(q) || r.msg.uin.includes(q) || JSON.stringify(r.msg).toLowerCase().includes(q);
return label.toLowerCase().includes(q) || r.msg.uin.includes(q) || r.search.includes(q);
});
}, [rows, kindFilter, query]);
+18 -10
View File
@@ -66,12 +66,18 @@ function Node({ k, value, depth, defaultOpen }: { k?: string; value: unknown; de
);
}
const entries = Array.isArray(value)
? value.map((v, i) => [String(i), v] as const)
: Object.entries(value as Record<string, unknown>);
const openB = Array.isArray(value) ? '[' : '{';
const closeB = Array.isArray(value) ? ']' : '}';
const shown = open ? Math.min(entries.length, limit) : 0;
const isArr = Array.isArray(value);
const count = isArr ? (value as unknown[]).length : Object.keys(value as object).length;
const openB = isArr ? '[' : '{';
const closeB = isArr ? ']' : '}';
const shown = open ? Math.min(count, limit) : 0;
// Only materialise child entries when expanded — a deep tree has many
// collapsed nodes, and enumerating each on every render is wasted work.
const entries: ReadonlyArray<readonly [string, unknown]> = open
? (isArr
? (value as unknown[]).map((v, i) => [String(i), v] as const)
: Object.entries(value as Record<string, unknown>))
: [];
return (
<div>
@@ -84,21 +90,21 @@ function Node({ k, value, depth, defaultOpen }: { k?: string; value: unknown; de
<ChevronRight className={cn('h-3 w-3 shrink-0 text-muted-foreground/60 transition-transform', open && 'rotate-90')} />
{keyLabel}
<Punct>{openB}</Punct>
{!open && <span className="text-muted-foreground/50">{entries.length}{Array.isArray(value) ? '' : ' keys'}<Punct>{closeB}</Punct></span>}
{!open && <span className="text-muted-foreground/50">{count}{isArr ? '' : ' keys'}<Punct>{closeB}</Punct></span>}
</button>
{open && (
<>
{entries.slice(0, shown).map(([ck, cv]) => (
<Node key={ck} k={Array.isArray(value) ? undefined : ck} value={cv} depth={depth + 1} defaultOpen={depth < 1} />
))}
{entries.length > shown && (
{count > shown && (
<button
type="button"
onClick={() => setLimit((l) => l + 500)}
className="py-0.5 text-[11px] text-primary hover:underline"
style={{ paddingLeft: `${depth * 0.9 + 1.1}rem` }}
>
还有 {entries.length - shown} 项,点击展开…
还有 {count - shown} 项,点击展开…
</button>
)}
<div style={{ paddingLeft: `${depth * 0.9 + 0.9}rem` }}><Punct>{closeB}</Punct></div>
@@ -110,9 +116,11 @@ function Node({ k, value, depth, defaultOpen }: { k?: string; value: unknown; de
export function JsonTree({ data, className, maxHeight = '20rem' }: { data: unknown; className?: string; maxHeight?: string }) {
const [copied, setCopied] = useState(false);
const text = (() => { try { return JSON.stringify(data, null, 2); } catch { return String(data); } })();
const copy = async () => {
// Stringify lazily, only on an actual copy — not on every render (a large
// payload would otherwise re-serialise the whole tree each commit).
const text = (() => { try { return JSON.stringify(data, null, 2); } catch { return String(data); } })();
try {
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(text);