// Debug — a standalone top-level page (Wave A3, redesigned). Apple-HIG flavour: // clarity (calm type hierarchy, generous whitespace), deference (translucent // chrome, content-first), depth (soft layered cards, a sliding segmented // control). Two tools over /api/debug/*: // • Action 测试台 — pick account + action (schema form / raw JSON), invoke. // • 实时活动 — merged live SSE of events + action calls. import { useEffect, useId, useMemo, useRef, useState, type ReactNode } from 'react'; import { motion } from 'motion/react'; import { Activity, Bug, ChevronRight, FlaskConical, Loader2, Pause, Play, RadioTower, Trash2 } from 'lucide-react'; import { Input } from '@/components/ui/input'; 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; } // ── iOS-style segmented control with a sliding active pill ── function Segmented({ value, onChange, options }: { value: T; onChange: (v: T) => void; options: { value: T; label: ReactNode }[]; }) { const id = useId(); return (
{options.map((o) => { const active = o.value === value; return ( ); })}
); } function Field({ label, children }: { label: ReactNode; children: ReactNode }) { return ( ); } 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'; export function DebugPage() { 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 [paramMode, setParamMode] = useState<'form' | 'json'>('form'); 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]); const effectiveMode = paramMode === 'json' || !doc ? 'json' : 'form'; 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 on invoke */ } })(); }, []); // 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 (effectiveMode === 'json') { 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 if (doc) { 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); } }; const resultFailed = !!result && ('error' in result || result.status === 'failed'); // ── 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' || 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 (
{/* header */}

调试

接口测试台与实时活动观测

{/* tester */}

Action 测试台

调用会真实生效(真发消息 / 真踢人等),请谨慎。
{ setActionName(e.target.value); setResult(null); }} /> {docs.map((d) => )}
{doc && (

{doc.summary}{doc.returns ? <> · 返回 {doc.returns} : null}

)}
参数 setParamMode(m)} options={[{ value: 'form', label: '表单' }, { value: 'json', label: 'JSON' }]} />
{effectiveMode === 'json' ? (