From 198f25476fd3d72a618446efbd7c250ec2dce381 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Mon, 3 Aug 2026 13:59:39 +0800 Subject: [PATCH] fix(webui): constrain forward message construction Keep forward nodes out of ordinary message parameters and expose a node-only root builder for forward send and upload actions. Lock nested forward content to one valid shape, clear stale form values when switching actions, and add regression coverage for the capability rules. --- .../src/components/debug/action-tester.tsx | 7 +-- .../src/components/debug/message-builder.tsx | 45 ++++++++++++++++--- .../src/components/debug/param-field.tsx | 34 ++++++++++---- .../message-builder-capabilities.test.ts | 35 +++++++++++++++ 4 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 packages/webui/tests/message-builder-capabilities.test.ts diff --git a/packages/webui/src/components/debug/action-tester.tsx b/packages/webui/src/components/debug/action-tester.tsx index d8c9a218..f50fc110 100644 --- a/packages/webui/src/components/debug/action-tester.tsx +++ b/packages/webui/src/components/debug/action-tester.tsx @@ -81,7 +81,7 @@ export function ActionTester({ accounts, docs, presetAction }: { accounts: QQInf useEffect(() => { if (accounts[0] && !uin) setUin(accounts[0].uin); }, [accounts, uin]); // Prefill from the API browser's 试一下 (nonce lets the same action re-trigger). useEffect(() => { - if (presetAction) { setActionName(presetAction.name); setParamMode('form'); setResult(null); setFrames([]); setShowHistory(false); } + if (presetAction) { setActionName(presetAction.name); setFields({}); setParamMode('form'); setResult(null); setFrames([]); setShowHistory(false); } }, [presetAction]); const doc = useMemo(() => docs.find((d) => d.name === actionName), [docs, actionName]); @@ -234,7 +234,7 @@ export function ActionTester({ accounts, docs, presetAction }: { accounts: QQInf { setActionName(v); setResult(null); setFrames([]); }} + onChange={(v) => { setActionName(v); setFields({}); setResult(null); setFrames([]); }} options={actionOptions} placeholder="search action…" validateRaw={(s) => s.trim().length > 0} @@ -265,8 +265,9 @@ export function ActionTester({ accounts, docs, presetAction }: { accounts: QQInf
{doc!.params.length === 0 &&

该接口无参数。

} {doc!.params.map((p) => ( - {p.name}{p.type}{p.required ? ' · 必填' : ''}}> + {p.name}{p.type}{p.required ? ' · 必填' : ''}}> setFields((f) => { const next = { ...f, [p.name]: v }; // Changing the group resets any sibling member picker — its diff --git a/packages/webui/src/components/debug/message-builder.tsx b/packages/webui/src/components/debug/message-builder.tsx index e9207b3e..43cd37f5 100644 --- a/packages/webui/src/components/debug/message-builder.tsx +++ b/packages/webui/src/components/debug/message-builder.tsx @@ -34,6 +34,7 @@ type SegBody = | { k: 'markdown'; content: string } | { k: 'node'; user_id: string; nickname: string; content: Seg[] }; export type Seg = SegBody & { _id: string }; +export type MessageBuilderMode = 'auto' | 'message' | 'forward' | 'node-content'; let segSeq = 0; const nid = () => `s${segSeq++}`; @@ -101,18 +102,41 @@ const ADVANCED: { k: SegBody['k']; label: string; icon: React.ReactNode }[] = [ { k: 'node', label: '合并转发节点', icon: }, ]; +/** Segment kinds exposed by the add menu for a builder context. */ +export function messageBuilderSegmentKinds( + mode: MessageBuilderMode, + segments: Seg[], +): SegBody['k'][] { + const all = [...COMMON, ...ADVANCED].map((item) => item.k); + if (mode === 'auto') return all; + if (mode === 'message') return all.filter((kind) => kind !== 'node'); + if (mode === 'forward') return ['node']; + + // A forward node may contain either an ordinary message or another list of + // nodes, never a mixture. Window shake is also rejected anywhere inside a + // forward payload. Lock the menu to the branch selected by the first child. + const forwardSafe = all.filter((kind) => kind !== 'poke'); + if (segments.length === 0) return forwardSafe; + if (segments.every((segment) => segment.k === 'node')) return ['node']; + if (segments.every((segment) => segment.k !== 'node')) { + return forwardSafe.filter((kind) => kind !== 'node'); + } + return []; +} + const LABELS: Record = { text: '文本', at: '@', image: '图片', face: '表情', reply: '回复', record: '语音', video: '视频', file: '文件', mface: '商城表情', poke: '戳一戳', json: 'JSON', xml: 'XML', markdown: 'Markdown', node: '转发节点', }; -export function MessageBuilder({ segments, onChange, uin, groupId, depth = 0 }: { +export function MessageBuilder({ segments, onChange, uin, groupId, depth = 0, mode = 'auto' }: { segments: Seg[]; onChange: (segs: Seg[]) => void; uin: string; /** group context for @ member picking (empty for private targets). */ groupId: string; depth?: number; + mode?: MessageBuilderMode; }) { const [showAdd, setShowAdd] = useState(false); const [dragFrom, setDragFrom] = useState(null); @@ -140,6 +164,11 @@ export function MessageBuilder({ segments, onChange, uin, groupId, depth = 0 }: : depth === 1 ? 'max(0px, calc(var(--radius) - 0.25rem))' : 'max(0px, calc(var(--radius) - 0.5rem))'; + const allowedKinds = new Set(messageBuilderSegmentKinds(mode, segments)); + const commonOptions = COMMON.filter((item) => allowedKinds.has(item.k)); + const advancedOptions = ADVANCED.filter((item) => allowedKinds.has(item.k)); + const primaryOptions = mode === 'forward' ? advancedOptions : commonOptions; + const secondaryOptions = mode === 'forward' ? [] : advancedOptions; return ( // The root is a bounded compose canvas, so its empty hint reads as an @@ -152,7 +181,9 @@ export function MessageBuilder({ segments, onChange, uin, groupId, depth = 0 }: style={nested ? undefined : { borderRadius: canvasRadius }} > {segments.length === 0 && ( -

空消息 — 点下方「添加段」开始

+

+ {mode === 'forward' ? '空转发 — 点下方添加转发节点' : '空消息 — 点下方「添加段」开始'} +

)} {segments.map((s, i) => ( @@ -201,24 +232,24 @@ export function MessageBuilder({ segments, onChange, uin, groupId, depth = 0 }: {showAdd && (
- {COMMON.map((c) => ( + {primaryOptions.map((c) => ( ))}
-
+ {secondaryOptions.length > 0 &&
进阶段
- {ADVANCED.map((c) => ( + {secondaryOptions.map((c) => ( ))}
-
+
}
)}
@@ -278,7 +309,7 @@ function SegmentEditor({ seg, onChange, uin, groupId, depth }: { seg: Seg; onCha
节点内容(可再嵌套) {depth < 3 ? ( - onChange({ ...seg, content: c })} uin={uin} groupId={groupId} depth={depth + 1} /> + onChange({ ...seg, content: c })} uin={uin} groupId={groupId} depth={depth + 1} mode="node-content" /> ) : (

嵌套层级过深

)} diff --git a/packages/webui/src/components/debug/param-field.tsx b/packages/webui/src/components/debug/param-field.tsx index 7a12c928..2bc1b4c5 100644 --- a/packages/webui/src/components/debug/param-field.tsx +++ b/packages/webui/src/components/debug/param-field.tsx @@ -11,7 +11,12 @@ import { Picker } from '@/components/ui/picker'; import { FaceGrid } from '@/components/ui/face-grid'; import { FileSource } from '@/components/debug/file-source'; import { Segmented } from '@/components/debug/segmented'; -import { MessageBuilder, toOneBot, type Seg } from '@/components/debug/message-builder'; +import { + MessageBuilder, + toOneBot, + type MessageBuilderMode, + type Seg, +} from '@/components/debug/message-builder'; import { useFriends, useGroups, useGroupMembers } from '@/hooks/use-debug-contacts'; import type { DebugActionParam, FieldRole } from '@/types'; @@ -47,13 +52,15 @@ function MemberPicker({ uin, groupId, value, onChange }: { uin: string; groupId: const MEDIA_ROLES: FieldRole[] = ['file', 'image', 'record', 'video']; -export function ParamField({ param, value, onChange, uin, groupContext }: { +export function ParamField({ param, value, onChange, uin, groupContext, actionName }: { param: DebugActionParam; value: string; onChange: (v: string) => void; uin: string; /** value of the sibling group_id field (for member_id pickers). */ groupContext: string; + /** Selected Action controls which message shapes the visual builder offers. */ + actionName: string; }) { const role = param.role; @@ -108,7 +115,8 @@ export function ParamField({ param, value, onChange, uin, groupContext }: { ); } if (param.type === 'message') { - return ; + return ; } if (isNumericType(param.type)) { return onChange(e.target.value)} @@ -118,12 +126,22 @@ export function ParamField({ param, value, onChange, uin, groupContext }: { placeholder={param.desc || (param.default !== undefined ? `默认 ${JSON.stringify(param.default)}` : '')} />; } +export function messageBuilderModeForAction(actionName: string): MessageBuilderMode { + return /(?:forward|foward)_msg$/.test(actionName) ? 'forward' : 'message'; +} + // Message param: the visual builder (keeps its own Seg[] model and emits the // serialized OneBot array as the string param value) with a raw-text escape // hatch. The builder is one-way (builder → value); switching to 原文 edits the // string directly — the tester's global JSON mode covers full raw editing. -function MessageParamField({ value, onChange, uin, groupContext }: { value: string; onChange: (v: string) => void; uin: string; groupContext: string }) { - const [mode, setMode] = useState<'build' | 'raw'>('build'); +function MessageParamField({ value, onChange, uin, groupContext, mode }: { + value: string; + onChange: (v: string) => void; + uin: string; + groupContext: string; + mode: MessageBuilderMode; +}) { + const [inputMode, setInputMode] = useState<'build' | 'raw'>('build'); const [segs, setSegs] = useState([]); const updateSegs = (next: Seg[]) => { @@ -134,10 +152,10 @@ function MessageParamField({ value, onChange, uin, groupContext }: { value: stri return (
- +
- {mode === 'build' ? ( - + {inputMode === 'build' ? ( + ) : (