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:
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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user