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:
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
|
// positives just add one extra click; false negatives skip a confirm — both low
|
||||||
// stakes (the amber banner already warns that any write is real).
|
// 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;
|
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) {
|
function qqAvatarUrl(uin: string) {
|
||||||
return `/avatar/${encodeURIComponent(uin)}`;
|
return `/avatar/${encodeURIComponent(uin)}`;
|
||||||
@@ -136,7 +139,7 @@ export function ActionTester({ accounts, docs, presetAction }: { accounts: QQInf
|
|||||||
try {
|
try {
|
||||||
await api.debug.invokeStream(uin, actionName.trim(), params, (frame) => {
|
await api.debug.invokeStream(uin, actionName.trim(), params, (frame) => {
|
||||||
last = 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;
|
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 });
|
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 });
|
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 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';
|
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 }) {
|
function IconBtn({ onClick, title, children }: { onClick: () => void; title: string; children: ReactNode }) {
|
||||||
return (
|
return (
|
||||||
@@ -103,7 +103,9 @@ export function LiveActivity() {
|
|||||||
(m) => {
|
(m) => {
|
||||||
if (m.kind === 'ready' || pausedRef.current) return;
|
if (m.kind === 'ready' || pausedRef.current) return;
|
||||||
setRows((prev) => {
|
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;
|
return next.length > STREAM_CAP ? next.slice(0, STREAM_CAP) : next;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -119,7 +121,7 @@ export function LiveActivity() {
|
|||||||
if (!q) return true;
|
if (!q) return true;
|
||||||
if (r.msg.kind === 'dropped') return false;
|
if (r.msg.kind === 'dropped') return false;
|
||||||
const { label } = rowLabel(r.msg);
|
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]);
|
}, [rows, kindFilter, query]);
|
||||||
|
|
||||||
|
|||||||
@@ -66,12 +66,18 @@ function Node({ k, value, depth, defaultOpen }: { k?: string; value: unknown; de
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const entries = Array.isArray(value)
|
const isArr = Array.isArray(value);
|
||||||
? value.map((v, i) => [String(i), v] as const)
|
const count = isArr ? (value as unknown[]).length : Object.keys(value as object).length;
|
||||||
: Object.entries(value as Record<string, unknown>);
|
const openB = isArr ? '[' : '{';
|
||||||
const openB = Array.isArray(value) ? '[' : '{';
|
const closeB = isArr ? ']' : '}';
|
||||||
const closeB = Array.isArray(value) ? ']' : '}';
|
const shown = open ? Math.min(count, limit) : 0;
|
||||||
const shown = open ? Math.min(entries.length, 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 (
|
return (
|
||||||
<div>
|
<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')} />
|
<ChevronRight className={cn('h-3 w-3 shrink-0 text-muted-foreground/60 transition-transform', open && 'rotate-90')} />
|
||||||
{keyLabel}
|
{keyLabel}
|
||||||
<Punct>{openB}</Punct>
|
<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>
|
</button>
|
||||||
{open && (
|
{open && (
|
||||||
<>
|
<>
|
||||||
{entries.slice(0, shown).map(([ck, cv]) => (
|
{entries.slice(0, shown).map(([ck, cv]) => (
|
||||||
<Node key={ck} k={Array.isArray(value) ? undefined : ck} value={cv} depth={depth + 1} defaultOpen={depth < 1} />
|
<Node key={ck} k={Array.isArray(value) ? undefined : ck} value={cv} depth={depth + 1} defaultOpen={depth < 1} />
|
||||||
))}
|
))}
|
||||||
{entries.length > shown && (
|
{count > shown && (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => setLimit((l) => l + 500)}
|
onClick={() => setLimit((l) => l + 500)}
|
||||||
className="py-0.5 text-[11px] text-primary hover:underline"
|
className="py-0.5 text-[11px] text-primary hover:underline"
|
||||||
style={{ paddingLeft: `${depth * 0.9 + 1.1}rem` }}
|
style={{ paddingLeft: `${depth * 0.9 + 1.1}rem` }}
|
||||||
>
|
>
|
||||||
还有 {entries.length - shown} 项,点击展开…
|
还有 {count - shown} 项,点击展开…
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
<div style={{ paddingLeft: `${depth * 0.9 + 0.9}rem` }}><Punct>{closeB}</Punct></div>
|
<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 }) {
|
export function JsonTree({ data, className, maxHeight = '20rem' }: { data: unknown; className?: string; maxHeight?: string }) {
|
||||||
const [copied, setCopied] = useState(false);
|
const [copied, setCopied] = useState(false);
|
||||||
const text = (() => { try { return JSON.stringify(data, null, 2); } catch { return String(data); } })();
|
|
||||||
|
|
||||||
const copy = async () => {
|
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 {
|
try {
|
||||||
if (navigator.clipboard?.writeText) {
|
if (navigator.clipboard?.writeText) {
|
||||||
await navigator.clipboard.writeText(text);
|
await navigator.clipboard.writeText(text);
|
||||||
|
|||||||
Reference in new issue
Block a user