From fae2f7552546e47392082f764e5497cf819b0b4c Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 18 Jun 2026 15:25:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(webui):=20debug=20panel=20=E2=80=94=20acti?= =?UTF-8?q?on=20tester=20+=20live=20stream=20(Wave=20A3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New Settings → 调试 tab (DebugPanel): an action tester (account picker + schema-driven form from /api/debug/actions, with a raw-JSON escape hatch for legacy actions, real-side-effect warning) and a live merged SSE view of OneBot events + action calls (pause / clear / kind filter, expandable JSON, drop marker). Typed debug API client domain (actions/invoke/stream over EventSource with ?token=). View layer — verified by tsc + vite build. --- .../src/components/pages/settings-page.tsx | 3 + .../src/components/settings/debug-panel.tsx | 254 ++++++++++++++++++ packages/webui/src/lib/api/client.ts | 25 +- packages/webui/src/lib/api/types.ts | 11 + packages/webui/src/router/index.tsx | 2 +- packages/webui/src/types.ts | 31 +++ 6 files changed, 324 insertions(+), 2 deletions(-) create mode 100644 packages/webui/src/components/settings/debug-panel.tsx diff --git a/packages/webui/src/components/pages/settings-page.tsx b/packages/webui/src/components/pages/settings-page.tsx index f0ff89b2..7f2fe148 100644 --- a/packages/webui/src/components/pages/settings-page.tsx +++ b/packages/webui/src/components/pages/settings-page.tsx @@ -43,6 +43,7 @@ import { cn } from '@/lib/utils'; import { settingsRoute, type SettingsTab } from '@/router'; import { NotificationsPanel } from '@/components/settings/notifications-panel'; import { SystemPanel } from '@/components/settings/system-panel'; +import { DebugPanel } from '@/components/settings/debug-panel'; const TABS: { key: SettingsTab; label: string; icon: typeof Sun }[] = [ { key: 'appearance', label: '外观', icon: Palette }, @@ -50,6 +51,7 @@ const TABS: { key: SettingsTab; label: string; icon: typeof Sun }[] = [ { key: 'advanced', label: '高级', icon: Code2 }, { key: 'account', label: '账号安全', icon: ShieldCheck }, { key: 'system', label: '服务', icon: Server }, + { key: 'debug', label: '调试', icon: Bug }, { key: 'notifications', label: '通知', icon: Bell }, { key: 'about', label: '关于', icon: Info }, ]; @@ -82,6 +84,7 @@ export function SettingsPage() { {tab === 'advanced' && } {tab === 'account' && } {tab === 'system' && } + {tab === 'debug' && } {tab === 'notifications' && } {tab === 'about' && } diff --git a/packages/webui/src/components/settings/debug-panel.tsx b/packages/webui/src/components/settings/debug-panel.tsx new file mode 100644 index 00000000..7350e96a --- /dev/null +++ b/packages/webui/src/components/settings/debug-panel.tsx @@ -0,0 +1,254 @@ +// Settings → 调试 tab (Wave A3). Two tools over /api/debug/*: +// 1) Action 测试台 — pick an account, pick an action (schema-driven form, or a +// raw-JSON escape hatch for legacy/freeform), invoke, see the response. +// Invocations are REAL (send messages, kick, etc.) — flagged in red. +// 2) 实时流 — merged live SSE of OneBot events + action calls across accounts, +// with pause / clear / kind filter. +import { useEffect, useMemo, useRef, useState } from 'react'; +import { Bug, ChevronRight, Loader2, Pause, Play, Trash2 } from 'lucide-react'; +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; +import { Badge } from '@/components/ui/badge'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +import { Select } from '@/components/ui/select'; +import { Textarea } from '@/components/ui/textarea'; +import { useApi } from '@/lib/api'; +import { cn } from '@/lib/utils'; +import type { DebugActionDoc, DebugInvokeResult, DebugStreamMessage, QQInfo } from '@/types'; + +const STREAM_CAP = 300; + +interface StreamRow { id: number; at: number; msg: Extract } + +function coerceParam(type: string, raw: string): unknown { + if (raw === '') return undefined; + if (type.includes('int') || type === 'number' || type === 'uint') { + const n = Number(raw); + return Number.isFinite(n) ? n : raw; + } + if (type === 'bool' || type === 'boolean') return raw === 'true' || raw === '1'; + return raw; +} + +export function DebugPanel() { + const api = useApi(); + + // ── action tester ── + const [accounts, setAccounts] = useState([]); + const [docs, setDocs] = useState([]); + const [uin, setUin] = useState(''); + const [actionName, setActionName] = useState(''); + const [fields, setFields] = useState>({}); + const [rawMode, setRawMode] = useState(false); + const [rawJson, setRawJson] = useState('{}'); + const [invoking, setInvoking] = useState(false); + const [result, setResult] = useState(null); + + const doc = useMemo(() => docs.find((d) => d.name === actionName), [docs, actionName]); + + useEffect(() => { + void (async () => { + try { + const [qq, acts] = await Promise.all([api.qqList(), api.debug.actions()]); + setAccounts(qq); + setDocs(acts.actions); + if (qq[0]) setUin(qq[0].uin); + } catch { /* surfaced lazily */ } + })(); + }, []); // eslint-disable-line react-hooks/exhaustive-deps + + const invoke = async () => { + if (!uin) { setResult({ error: '请选择账号' }); return; } + if (!actionName.trim()) { setResult({ error: '请填写 action' }); return; } + let params: Record = {}; + if (rawMode || !doc) { + try { params = JSON.parse(rawJson || '{}'); } catch { setResult({ error: 'params JSON 无效' }); return; } + if (typeof params !== 'object' || params === null || Array.isArray(params)) { setResult({ error: 'params 必须是对象' }); return; } + } else { + for (const p of doc.params) { + const v = coerceParam(p.type, fields[p.name] ?? ''); + if (v !== undefined) params[p.name] = v; + } + } + setInvoking(true); + setResult(null); + try { + setResult(await api.debug.invoke(uin, actionName.trim(), params)); + } catch (e) { + setResult({ error: e instanceof Error ? e.message : '调用失败' }); + } finally { + setInvoking(false); + } + }; + + // ── live stream ── + const [rows, setRows] = useState([]); + const [paused, setPaused] = useState(false); + const [status, setStatus] = useState<'open' | 'reconnecting' | 'closed'>('closed'); + const [kindFilter, setKindFilter] = useState<'all' | 'event' | 'action'>('all'); + const pausedRef = useRef(paused); + pausedRef.current = paused; + const idRef = useRef(0); + + useEffect(() => { + const off = api.debug.stream( + (m) => { + if (m.kind === 'ready') return; + if (pausedRef.current) return; + setRows((prev) => { + const next = [{ id: idRef.current++, at: Date.now(), msg: m }, ...prev]; + return next.length > STREAM_CAP ? next.slice(0, STREAM_CAP) : next; + }); + }, + (s) => setStatus(s), + ); + return off; + }, []); // eslint-disable-line react-hooks/exhaustive-deps + + const visible = rows.filter((r) => kindFilter === 'all' || r.msg.kind === kindFilter); + + return ( +
+
+ + 测试台调用会真实生效(真发消息 / 真踢人等),请谨慎操作。 +
+ + {/* Action tester */} + + + Action 测试台 + 选择账号与接口,填参数后调用并查看响应。 + + +
+
+ + +
+
+ + { setActionName(e.target.value); setResult(null); }} + placeholder="如 send_group_msg" + /> + + {docs.map((d) => )} + +
+
+ + {doc &&

{doc.summary}{doc.returns ? ` · 返回 ${doc.returns}` : ''}

} + +
+ + +
+ + {rawMode || !doc ? ( +