refactor(onebot): declare every action param the handler reads

Action handlers now only see fields from the param table. Extra client
keys are still ignored. Garbage values on declared numbers fail instead
of becoming zero.
This commit is contained in:
DrSmoothl committed 2026-08-30 00:56:01 +08:00
1 parent 117335166f
commit f6adadcd12
14 files changed
+952 -235

No files matched your search

+129 -63
View File
@@ -6,7 +6,6 @@ import type {
GroupEssenceMessage,
} from '@snowluma/protocol/web/group-essence';
import type { ApiActionContext } from '../api-handler';
import { asNumber, asString } from '../api-handler';
import type { ForwardPreviewMeta } from '../modules/message-actions';
import {
hasAuthoritativeSequence,
@@ -14,9 +13,10 @@ import {
failedResponse,
okResponse,
type JsonObject,
type JsonValue,
type MessageMeta,
} from '../types';
import { defineAction, groupAction, groupUserAction, f } from '../action-kit';
import { defineAction, groupAction, groupUserAction, f, type Field } from '../action-kit';
import { groupInfoReturnsSchema } from './group-info';
import { GROUP_MESSAGE_EVENT, hashMessageIdInt32 } from '../message-id';
@@ -262,19 +262,57 @@ async function saveDownloadBuffer(buf: Buffer, preferredName: string): Promise<s
return resolved;
}
// 从 send_*_forward_msg 的参数里提取 NapCat 兼容的转发预览元信息。四个字段都是可选的——如果没有提供,模块层会根据实际消息节点列表推断出合理的默认值。
function readForwardPreviewMeta(params: Record<string, unknown>): ForwardPreviewMeta | undefined {
const source = asString(params.source) || undefined;
const summary = asString(params.summary) || undefined;
const prompt = asString(params.prompt) || undefined;
function optionalExactString(): Field<string | undefined> {
const inner = f.string().optional();
return Object.assign(Object.create(inner) as Field<string | undefined>, {
coerce(raw: unknown, field: string) {
if (typeof raw === 'number' || typeof raw === 'boolean' || raw === null
|| (typeof raw === 'object' && raw !== null)) {
return { ok: false as const, field, reason: 'expected a string' };
}
return inner.coerce(raw as Parameters<Field<string | undefined>['coerce']>[0], field);
},
});
}
function integerFromDecimalString(value: string): number {
if (value.trim() === '') return 0;
const n = Number(value);
return Number.isFinite(n) ? Math.trunc(n) : 0;
}
function primitiveText(value: unknown): string {
if (typeof value === 'string') return value;
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
return '';
}
const forwardPreviewParams = {
source: f.string().optional(),
summary: f.string().optional(),
prompt: f.string().optional(),
news: f.raw(),
};
// NapCat-compatible forward preview. All four fields are optional — if none
// are provided, the message module infers defaults from the node list.
function readForwardPreviewMeta(p: {
source?: string;
summary?: string;
prompt?: string;
news?: JsonValue;
}): ForwardPreviewMeta | undefined {
const source = p.source || undefined;
const summary = p.summary || undefined;
const prompt = p.prompt || undefined;
let news: Array<{ text: string }> | undefined;
if (Array.isArray(params.news)) {
if (Array.isArray(p.news)) {
const collected: Array<{ text: string }> = [];
for (const item of params.news) {
for (const item of p.news) {
if (typeof item === 'string') {
collected.push({ text: item });
} else if (item && typeof item === 'object' && !Array.isArray(item)) {
const text = asString((item as Record<string, unknown>).text);
const text = primitiveText((item as Record<string, unknown>).text);
if (text) collected.push({ text });
}
}
@@ -801,13 +839,10 @@ export const actions = [
required: ['text'],
},
params: {
message_id: f.string().default('').role('message_id'),
message_id: f.messageId(),
},
// `raw` so message_id works whether the client sends a number or a string.
run: async (_p, ctx, raw) => {
const messageId = asNumber(raw.message_id);
if (!messageId) return failedResponse(RETCODE.BAD_REQUEST, 'message_id is required');
return okResponse(await ctx.fetchPttText(messageId));
run: async (p, ctx) => {
return okResponse(await ctx.fetchPttText(p.message_id));
},
}),
@@ -2268,8 +2303,7 @@ export const actions = [
},
}),
// ===== 原 legacy registerAction,现并入 kit(ctx 调用与逻辑逐字保留;
// 别名键 / 原始参数透传 / 任意对象经 run 第三参 raw 或 f.raw() 表达)=====
// ===== 原 legacy registerAction,现并入 kit =====
defineAction({
name: ['get_rkey', 'nc_get_rkey'],
@@ -2407,16 +2441,22 @@ export const actions = [
name: 'send_forward_msg',
summary: '发送合并转发(按 message_type/群号自动路由)',
returns: '{ message_id, res_id, forward_id }',
// group_id/user_id 是“路由提示”而非身份字段:原实现用 asNumber + `>0` 判断,
// 占位的 0 表示“该分支不适用”、不应报错。故从 raw 读取(保持旧语义),
// 只声明 messages/message。
params: { messages: f.message().optional(), message: f.message().optional() },
run: async (p, ctx, raw) => {
const messageType = asString(raw.message_type);
const groupId = asNumber(raw.group_id);
const userId = asNumber(raw.user_id);
// group_id/user_id are routing hints, not identity: 0 means "this branch
// does not apply" and must not error. int({min:0}) optional, not groupId.
params: {
messages: f.message().optional(),
message: f.message().optional(),
message_type: f.string().optional(),
group_id: f.int({ min: 0 }).optional(),
user_id: f.int({ min: 0 }).optional(),
...forwardPreviewParams,
},
run: async (p, ctx) => {
const messageType = p.message_type ?? '';
const groupId = p.group_id ?? 0;
const userId = p.user_id ?? 0;
const messages = p.messages ?? p.message;
const meta = readForwardPreviewMeta(raw);
const meta = readForwardPreviewMeta(p);
if (messages === undefined) return failedResponse(RETCODE.BAD_REQUEST, 'message/messages is required');
if ((messageType === 'group' || groupId > 0) && ctx.sendGroupForwardMsg) {
if (!groupId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id is required');
@@ -2437,11 +2477,15 @@ export const actions = [
name: 'send_group_forward_msg',
summary: '发送群合并转发',
returns: '{ message_id, res_id, forward_id }',
params: { messages: f.message().optional(), message: f.message().optional() },
run: async (p, ctx, raw) => {
params: {
messages: f.message().optional(),
message: f.message().optional(),
...forwardPreviewParams,
},
run: async (p, ctx) => {
const messages = p.messages ?? p.message;
if (messages === undefined) return failedResponse(RETCODE.BAD_REQUEST, 'message/messages is required');
const result = await ctx.sendGroupForwardMsg(p.group_id, messages, readForwardPreviewMeta(raw));
const result = await ctx.sendGroupForwardMsg(p.group_id, messages, readForwardPreviewMeta(p));
return okResponse({ message_id: result.messageId, res_id: result.forwardId, forward_id: result.forwardId });
},
}),
@@ -2450,11 +2494,16 @@ export const actions = [
name: 'send_private_forward_msg',
summary: '发送私聊合并转发',
returns: '{ message_id, res_id, forward_id }',
params: { user_id: f.userId(), messages: f.message().optional(), message: f.message().optional() },
run: async (p, ctx, raw) => {
params: {
user_id: f.userId(),
messages: f.message().optional(),
message: f.message().optional(),
...forwardPreviewParams,
},
run: async (p, ctx) => {
const messages = p.messages ?? p.message;
if (messages === undefined) return failedResponse(RETCODE.BAD_REQUEST, 'message/messages is required');
const result = await ctx.sendPrivateForwardMsg(p.user_id, messages, readForwardPreviewMeta(raw));
const result = await ctx.sendPrivateForwardMsg(p.user_id, messages, readForwardPreviewMeta(p));
return okResponse({ message_id: result.messageId, res_id: result.forwardId, forward_id: result.forwardId });
},
}),
@@ -2471,12 +2520,11 @@ export const actions = [
},
required: ['messages'],
},
params: { id: f.string().optional() },
run: async (p, ctx, raw) => {
params: { id: f.string().optional(), message_id: f.string().optional() },
run: async (p, ctx) => {
let id = p.id || '';
if (!id) {
const rawMessageId = raw.message_id;
const numericMessageId = asNumber(rawMessageId);
if (!id && p.message_id !== undefined) {
const numericMessageId = integerFromDecimalString(p.message_id);
if (numericMessageId > 0) {
const event = ctx.getMessage(numericMessageId);
const segments = Array.isArray(event?.message) ? event.message : [];
@@ -2487,11 +2535,11 @@ export const actions = [
const data = (typeof so.data === 'object' && so.data !== null && !Array.isArray(so.data))
? so.data as Record<string, unknown>
: null;
const candidate = asString(data?.id) || asString(data?.res_id) || asString(data?.forward_id);
const candidate = primitiveText(data?.id) || primitiveText(data?.res_id) || primitiveText(data?.forward_id);
if (candidate) { id = candidate; break; }
}
}
if (!id) id = asString(rawMessageId);
if (!id) id = p.message_id;
}
if (!id) return failedResponse(RETCODE.BAD_REQUEST, 'id or message_id is required');
const messages = await ctx.getForwardMsg(id);
@@ -2503,8 +2551,13 @@ export const actions = [
name: 'download_file',
summary: '下载文件(url 或 base64)到 data/downloads',
returns: '{ file }',
params: { url: f.string().default(''), base64: f.string().default(''), name: f.string().default('') },
run: async (p, _ctx, raw) => {
params: {
url: f.string().default(''),
base64: f.string().default(''),
name: f.string().default(''),
headers: f.raw(),
},
run: async (p) => {
const url = p.url;
const base64 = p.base64;
const name = p.name;
@@ -2516,7 +2569,7 @@ export const actions = [
buf = Buffer.from(base64, 'base64');
if (buf.length > DOWNLOAD_FILE_MAX_BYTES) return failedResponse(RETCODE.BAD_REQUEST, `base64 payload too large: ${buf.length} > ${DOWNLOAD_FILE_MAX_BYTES} bytes`);
} else {
buf = await fetchDownloadFile(url, parseDownloadHeaders(raw.headers), DOWNLOAD_FILE_MAX_BYTES, DOWNLOAD_FILE_TIMEOUT_MS);
buf = await fetchDownloadFile(url, parseDownloadHeaders(p.headers), DOWNLOAD_FILE_MAX_BYTES, DOWNLOAD_FILE_TIMEOUT_MS);
}
try {
const safe = await saveDownloadBuffer(buf, name);
@@ -2539,12 +2592,9 @@ export const actions = [
},
required: ['words'],
},
params: { words: f.raw() },
params: { words: f.array(f.string()) },
run: async (p, ctx) => {
const rawWords = p.words;
if (!Array.isArray(rawWords)) return failedResponse(RETCODE.BAD_REQUEST, 'invalid words array');
const words = rawWords.map((w) => String(w));
const translated = await ctx.bridge.apis.misc.translateEn2Zh(words);
const translated = await ctx.bridge.apis.misc.translateEn2Zh(p.words);
return okResponse({ words: translated });
},
}),
@@ -2552,7 +2602,7 @@ export const actions = [
defineAction({
name: 'set_self_longnick',
summary: '设置个性签名(longNick/long_nick,严格 string)',
params: { longNick: f.raw(), long_nick: f.raw() },
params: { longNick: optionalExactString(), long_nick: optionalExactString() },
run: async (p, ctx) => {
const longNick = p.longNick !== undefined ? p.longNick : p.long_nick;
if (typeof longNick !== 'string') return failedResponse(RETCODE.BAD_REQUEST, 'invalid longNick');
@@ -2565,14 +2615,22 @@ export const actions = [
name: 'get_mini_app_ark',
summary: '获取小程序卡片 ark',
readOnly: true,
params: {},
run: async (_p, ctx, raw) => {
const type = raw.type || 'bili';
const title = raw.title || '';
const desc = raw.desc || '';
const picUrl = raw.picUrl || raw.pic_url || '';
const jumpUrl = raw.jumpUrl || raw.jump_url || '';
const data = await ctx.bridge.apis.misc.getMiniAppArk(String(type), String(title), String(desc), String(picUrl), String(jumpUrl));
params: {
type: f.string().optional(),
title: f.string().optional(),
desc: f.string().optional(),
picUrl: f.string().optional(),
pic_url: f.string().optional(),
jumpUrl: f.string().optional(),
jump_url: f.string().optional(),
},
run: async (p, ctx) => {
const type = p.type || 'bili';
const title = p.title || '';
const desc = p.desc || '';
const picUrl = p.picUrl || p.pic_url || '';
const jumpUrl = p.jumpUrl || p.jump_url || '';
const data = await ctx.bridge.apis.misc.getMiniAppArk(type, title, desc, picUrl, jumpUrl);
return okResponse(data);
},
}),
@@ -2580,12 +2638,20 @@ export const actions = [
groupAction({
name: 'click_inline_keyboard_button',
summary: '点击内联键盘按钮',
params: { bot_appid: f.uint(), msg_seq: f.uint() },
run: async (p, ctx, raw) => {
const buttonId = raw.button_id;
const callbackData = raw.callback_data || '';
if (!buttonId) return failedResponse(RETCODE.BAD_REQUEST, 'missing required parameters');
const data = await ctx.bridge.apis.misc.clickInlineKeyboardButton(p.group_id, p.bot_appid, String(buttonId), String(callbackData), p.msg_seq);
params: {
bot_appid: f.uint(),
msg_seq: f.uint(),
button_id: f.string({ allowEmpty: false }),
callback_data: f.string().default(''),
},
run: async (p, ctx) => {
const data = await ctx.bridge.apis.misc.clickInlineKeyboardButton(
p.group_id,
p.bot_appid,
p.button_id,
p.callback_data,
p.msg_seq,
);
return okResponse(data);
},
}),
+2 -6
View File
@@ -1,5 +1,4 @@
import { defineAction, groupAction, groupUserAction, f } from '../action-kit';
import { asString } from '../api-handler';
import { RETCODE, failedResponse, okResponse } from '../types';
import { WebHonorType } from '@snowluma/protocol/web/group-honor';
@@ -190,17 +189,14 @@ export const actions = [
},
}),
// `type` keeps the legacy `asString(x) || 'all'` semantics (absent / non-string
// / empty-string all collapse to 'all'), which a typed string field can't
// replicate exactly — so it stays a raw param coerced in run().
groupAction({
name: 'get_group_honor_info',
summary: '获取群荣誉信息',
readOnly: true,
params: { type: f.raw() },
params: { type: f.string().optional() },
run: async (p, ctx) => {
const groupId = p.group_id;
const typeStr = asString(p.type) || 'all';
const typeStr = p.type || 'all';
const typeValues = Object.values(WebHonorType) as string[];
if (!typeValues.includes(typeStr)) {
+4 -6
View File
@@ -1,5 +1,4 @@
import { defineAction, f } from '../action-kit';
import { asString } from '../api-handler';
import { okResponse } from '../types';
export const actions = [
@@ -22,15 +21,14 @@ export const actions = [
params: {
// Opaque handle from the original request event; callers must echo it.
flag: f.string({ allowEmpty: false }),
// sub_type/type pass through with a nested fallback (sub_type → type → 'add');
// kept raw so the original asString resolution is preserved verbatim.
sub_type: f.raw(),
type: f.raw(),
// sub_type → type → 'add'. Empty string is kept, not treated as missing.
sub_type: f.string().optional(),
type: f.string().optional(),
approve: f.bool().default(true),
reason: f.string().default(''),
},
run: async (p, ctx) => {
const subType = asString(p.sub_type, asString(p.type, 'add'));
const subType = p.sub_type !== undefined ? p.sub_type : (p.type !== undefined ? p.type : 'add');
await ctx.handleGroupRequest(p.flag, subType, p.approve, p.reason);
return okResponse();
},
@@ -361,7 +361,7 @@ const downloadFileStream = defineStreamAction({
summary: '以流式方式下载文件(stream 目录本地文件 / URL / 缓存媒体)',
returns: '流式帧:file_info → file_chunk* → file_complete',
params: downloadParams,
run: (p, ctx, _raw, sink) => runDownload(p.file || p.file_id || '', p.chunk_size, ctx, sink, 'auto'),
run: (p, ctx, sink) => runDownload(p.file || p.file_id || '', p.chunk_size, ctx, sink, 'auto'),
});
const downloadFileImageStream = defineStreamAction({
@@ -369,7 +369,7 @@ const downloadFileImageStream = defineStreamAction({
summary: '以流式方式下载图片(缓存图片 id / URL / stream 目录本地文件)',
returns: '流式帧:file_info → file_chunk* → file_complete',
params: downloadParams,
run: (p, ctx, _raw, sink) => runDownload(p.file || p.file_id || '', p.chunk_size, ctx, sink, 'image'),
run: (p, ctx, sink) => runDownload(p.file || p.file_id || '', p.chunk_size, ctx, sink, 'image'),
});
const downloadFileRecordStream = defineStreamAction({
@@ -377,7 +377,7 @@ const downloadFileRecordStream = defineStreamAction({
summary: '以流式方式下载语音(缓存语音 id / URL / stream 目录本地文件)',
returns: '流式帧:file_info → file_chunk* → file_complete',
params: downloadParams,
run: (p, ctx, _raw, sink) => runDownload(p.file || p.file_id || '', p.chunk_size, ctx, sink, 'record'),
run: (p, ctx, sink) => runDownload(p.file || p.file_id || '', p.chunk_size, ctx, sink, 'record'),
});
const testDownloadStream = defineStreamAction({
@@ -385,7 +385,7 @@ const testDownloadStream = defineStreamAction({
summary: '测试下载流(推送 10 个数据帧,验证流式传输,不触达 QQ)',
returns: '流式帧:data_chunk*10 → data_complete(error=true 时以 error 帧结束)',
params: { error: f.bool().default(false).describe('是否触发测试错误') },
run: async (p, _ctx, _raw, sink) => {
run: async (p, _ctx, sink) => {
for (let i = 0; i < 10; i++) {
await sink.send({ type: StreamStatus.Stream, data: `Index-> ${i + 1}`, data_type: 'data_chunk' });
}