fix(onebot): preserve public Qzone input semantics

Keep comment and reply text unchanged and support structured input in the debug console. Report malformed input before sending a request.

Refs #587
This commit is contained in:
DrSmoothl committed 2026-10-25 23:26:55 +08:00
1 parent 2f1aadffbe
commit dafe2082f1
10 files changed
+464 -113

No files matched your search

@@ -26,6 +26,7 @@ import { ParamField } from '@/components/debug/param-field';
import { Segmented } from '@/components/debug/segmented';
import { useApi } from '@/lib/api';
import { useDebugTasks } from '@/contexts/DebugTaskContext';
import { buildActionParams } from '@/lib/action-params';
import { clearHistory, loadHistory, pushHistory, type InvokeRecord } from '@/lib/debug-history';
import { cn } from '@/lib/utils';
import type { DebugActionDoc, DebugStreamFrame, QQInfo } from '@/types';
@@ -46,37 +47,6 @@ function qqAvatarUrl(uin: string) {
return `/avatar/${encodeURIComponent(uin)}`;
}
function coerceParam(type: string, raw: string): unknown {
if (raw === '') {
return undefined;
}
if (/int|uint|number|messageId/i.test(type)) {
const n = Number(raw);
return Number.isFinite(n) ? n : raw;
}
if (type === 'bool' || type === 'boolean') {
return raw === 'true' || raw === '1';
}
if (type === 'message') {
const t = raw.trim();
if ((t.startsWith('[') && t.endsWith(']')) || (t.startsWith('{') && t.endsWith('}'))) {
try {
return JSON.parse(t);
} catch {
/* fall through to literal */
}
}
return raw;
}
return raw;
}
function Field({ label, children }: { label: ReactNode; children: ReactNode }) {
return (
<label className="flex flex-col gap-1.5">
@@ -171,19 +141,11 @@ export function ActionTester({
return parsed as Record<string, unknown>;
}
const out: Record<string, unknown> = {};
if (doc) {
for (const p of doc.params) {
const v = coerceParam(p.type, fields[p.name] ?? '');
if (v !== undefined) {
out[p.name] = v;
}
}
try {
return buildActionParams(doc?.params ?? [], fields);
} catch (error) {
return { __error: error instanceof Error ? error.message : String(error) };
}
return out;
};
const runInvoke = async () => {
@@ -5,6 +5,7 @@
// group_id, an image a FileSource, a bool a switch, an enum a select.
import { useState } from 'react';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { Dropdown } from '@/components/interior/dropdown';
import { ToggleSwitch } from '@/components/ui/toggle-switch';
import { Picker } from '@/components/ui/picker';
@@ -220,6 +221,26 @@ export function ParamField({
);
}
if (param.type === 'object' || param.type === 'string | object[]') {
return (
<div className="space-y-1.5">
<Textarea
value={value}
onChange={(e) => onChange(e.target.value)}
aria-label={param.name}
placeholder={param.type === 'object' ? '{\n \n}' : '"你好"'}
className="min-h-24 font-mono text-xs"
spellCheck={false}
/>
<p className="text-xs text-muted-foreground">
{param.type === 'object'
? '粘贴完整 JSON 对象,保留原始字段。'
: '文本填写 JSON 字符串(如 "你好"),富文本填写对象数组。'}
</p>
</div>
);
}
if (isNumericType(param.type)) {
return (
<Input
+76
View File
@@ -0,0 +1,76 @@
import type { DebugActionParam } from '@/types';
function coerceParam({ name, type }: DebugActionParam, raw: string): unknown {
if (raw === '') {
return undefined;
}
if (/int|uint|number|messageId/i.test(type)) {
const n = Number(raw);
return Number.isFinite(n) ? n : raw;
}
if (type === 'bool' || type === 'boolean') {
return raw === 'true' || raw === '1';
}
if (type === 'object' || type === 'string | object[]') {
let parsed: unknown;
try {
parsed = JSON.parse(raw);
} catch {
throw new Error(`${name} 的 JSON 格式无效`);
}
const isObject = (value: unknown) =>
typeof value === 'object' && value !== null && !Array.isArray(value);
if (type === 'object' && !isObject(parsed)) {
throw new Error(`${name} 必须是 JSON 对象`);
}
if (
type === 'string | object[]' &&
typeof parsed !== 'string' &&
!(Array.isArray(parsed) && parsed.every(isObject))
) {
throw new Error(`${name} 必须是 JSON 字符串或对象数组`);
}
return parsed;
}
if (type === 'message') {
const t = raw.trim();
if ((t.startsWith('[') && t.endsWith(']')) || (t.startsWith('{') && t.endsWith('}'))) {
try {
return JSON.parse(t);
} catch {
/* fall through to literal */
}
}
return raw;
}
return raw;
}
export function buildActionParams(
params: DebugActionParam[],
fields: Record<string, string>,
): Record<string, unknown> {
const out: Record<string, unknown> = {};
for (const param of params) {
const value = coerceParam(param, fields[param.name] ?? '');
if (value !== undefined) {
out[param.name] = value;
}
}
return out;
}
+108
View File
@@ -0,0 +1,108 @@
import { renderToStaticMarkup } from 'react-dom/server';
import { describe, expect, it } from 'vitest';
import { ParamField } from '../src/components/debug/param-field';
import { buildActionParams } from '../src/lib/action-params';
describe('debug action form parameters', () => {
const objectParam = { name: 'feed', type: 'object', required: true };
it('passes pasted objects with nested and unknown fields to the action', () => {
const feed = { identity: { id: 'opaque' }, future: [1, null, { enabled: true }] };
expect(buildActionParams([objectParam], { feed: JSON.stringify(feed, null, 2) })).toEqual({
feed,
});
});
it.each(['{bad', '[]', 'null', '1', 'false', '"text"'])(
'rejects invalid object input %s with the field name',
(feed) => {
expect(() => buildActionParams([objectParam], { feed })).toThrow(/feed/);
},
);
it('omits an untouched optional object rather than sending an empty string', () => {
expect(buildActionParams([{ ...objectParam, required: false }], { feed: '' })).toEqual({});
});
it('renders an object editor that accepts multiline JSON', () => {
const html = renderToStaticMarkup(
<ParamField
param={objectParam}
value={'{\n "id": "opaque"\n}'}
onChange={() => undefined}
uin=""
groupContext=""
actionName="get_qzone_public_feed_detail"
/>,
);
expect(html).toContain('<textarea');
expect(html).toContain('aria-label="feed"');
});
const contentParam = { name: 'content', type: 'string | object[]', required: true };
it.each(['[]', '[1,2]', '[{"type":0,"content":"hello"}]', ' \n中文 '])(
'preserves JSON-looking text %j when entered as a JSON string',
(content) => {
expect(buildActionParams([contentParam], { content: JSON.stringify(content) })).toEqual(
{ content },
);
},
);
it('passes a real rich-item array without converting it to OneBot segments', () => {
const content = [{ type: 0, content: 'hello', future: { keep: true } }];
expect(buildActionParams([contentParam], { content: JSON.stringify(content) })).toEqual({
content,
});
});
it.each(['[bad', '[1,2]', 'null', '1', 'false', '{}', '[null]', '[[]]'])(
'reports invalid rich content %s before invoking an action',
(content) => {
expect(() => buildActionParams([contentParam], { content })).toThrow(/content/);
},
);
it('renders rich content as an explicit JSON editor without the OneBot message builder', () => {
const html = renderToStaticMarkup(
<ParamField
param={contentParam}
value={'"[1,2]"'}
onChange={() => undefined}
uin=""
groupContext=""
actionName="comment_qzone_public_feed"
/>,
);
expect(html).toContain('<textarea');
expect(html).toContain('aria-label="content"');
expect(html).toContain('JSON 字符串');
expect(html).not.toContain('role="radiogroup"');
});
it('keeps existing scalar and OneBot message conversions', () => {
const fields = {
message_id: '-123',
enable: 'false',
message: '[{"type":"text","data":{"text":"hello"}}]',
text: '[1,2]',
};
expect(
buildActionParams(
[
{ name: 'message_id', type: 'messageId', required: true },
{ name: 'enable', type: 'bool', required: true },
{ name: 'message', type: 'message', required: true },
{ name: 'text', type: 'string', required: true },
],
fields,
),
).toEqual({
message_id: -123,
enable: false,
message: [{ type: 'text', data: { text: 'hello' } }],
text: '[1,2]',
});
});
});