refactor(onebot): collapse action registration into a single barrel
加一个 action 文件原本要同步改三处手写清单:ApiHandler 的 12 个 register import + 12 行构造调用、各文件的 register footer、action-docs 的 13 import + GROUPS。现收敛 成一个 actions/index.ts barrel 作为「文件→动作 + 分类」的单一真相。 - action-kit:抽 RegisteredActionSpec(describe+register),ActionSpec/ StreamActionSpec extends 它,registerActions 参数放宽到 RegisteredActionSpec[] (它只调 .register),于是 stream 动作也能并入统一注册。 - 新增 actions/index.ts:ACTION_GROUPS(11 组含分类,stream 合并)+ ALL_ACTIONS。 - api-handler:12 import+12 调用 → registerActions(this, context, ALL_ACTIONS); .handle_quick_operation(唯一非 ActionSpec 的裸 handler,需要 h 本身)从 extended 搬进构造器一处显式注册。 - action-docs:改用 barrel 的 ACTION_GROUPS,删本地 GROUPS + 13 import。 - 删 12 个 register footer + 清理因此变 unused 的 ApiHandler/ApiActionContext import。 行为严格不变:注册的动作集合、.handle_quick_operation、文档分类/计数均一致。 onebot 516 全绿、全仓 12 包 typecheck Done;对抗性 Agent 逐条对比 HEAD 确认等价。
This commit is contained in:
16 files changed
+98
-139
No files matched your search
@@ -1,9 +1,9 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import type { ApiActionContext, ApiHandler } from '../api-handler';
|
||||
import type { ApiActionContext } from '../api-handler';
|
||||
import { asNumber, asString } from '../api-handler';
|
||||
import type { ForwardPreviewMeta } from '../modules/message-actions';
|
||||
import { JsonObject, RETCODE, failedResponse, okResponse } from '../types';
|
||||
import { defineAction, groupAction, groupUserAction, registerActions, f } from '../action-kit';
|
||||
import { defineAction, groupAction, groupUserAction, f } from '../action-kit';
|
||||
|
||||
const DOWNLOAD_FILE_MAX_BYTES = 1024 * 1024 * 1024; // 1 GiB
|
||||
const DOWNLOAD_FILE_TIMEOUT_MS = 60_000;
|
||||
@@ -2261,20 +2261,6 @@ async function fetchFilteredGroupRequests(ctx: ApiActionContext) {
|
||||
}
|
||||
}
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
registerActions(h, ctx, actions);
|
||||
|
||||
// .handle_quick_operation 仍保留 legacy:它需要把 ApiHandler 本身 (h) 交给
|
||||
// executeQuickOperation 去回灌动作,而 action-kit 的 run 只提供 (p, ctx, raw)、不含 h。
|
||||
h.registerAction('.handle_quick_operation', async (params) => {
|
||||
const context = params.context as JsonObject | undefined;
|
||||
const operation = params.operation as Record<string, unknown> | undefined;
|
||||
if (!context || !operation) return failedResponse(RETCODE.BAD_REQUEST, 'context and operation are required');
|
||||
const { executeQuickOperation } = await import('../network/quick-operation');
|
||||
await executeQuickOperation(context, operation, h);
|
||||
return okResponse();
|
||||
});
|
||||
}
|
||||
|
||||
function hexToBytes(hex: string): Uint8Array {
|
||||
const out = new Uint8Array(hex.length / 2);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { defineAction, registerActions, f } from '../action-kit';
|
||||
import type { ApiActionContext, ApiHandler } from '../api-handler';
|
||||
import { defineAction, f } from '../action-kit';
|
||||
import { okResponse } from '../types';
|
||||
|
||||
export const actions = [
|
||||
@@ -68,6 +67,3 @@ export const actions = [
|
||||
}),
|
||||
];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
registerActions(h, ctx, actions);
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { defineAction, groupAction, groupUserAction, registerActions, f } from '../action-kit';
|
||||
import type { ApiActionContext, ApiHandler } from '../api-handler';
|
||||
import { defineAction, groupAction, groupUserAction, f } from '../action-kit';
|
||||
import { okResponse } from '../types';
|
||||
|
||||
export const actions = [
|
||||
@@ -126,6 +125,3 @@ export const actions = [
|
||||
}),
|
||||
];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
registerActions(h, ctx, actions);
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { groupAction, registerActions, f } from '../action-kit';
|
||||
import type { ApiActionContext, ApiHandler } from '../api-handler';
|
||||
import { groupAction, f } from '../action-kit';
|
||||
import type { JsonValue } from '../types';
|
||||
import { okResponse } from '../types';
|
||||
|
||||
@@ -168,6 +167,3 @@ export const actions = [
|
||||
}),
|
||||
];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
registerActions(h, ctx, actions);
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { defineAction, groupAction, registerActions, f } from '../action-kit';
|
||||
import type { ApiHandler, ApiActionContext } from '../api-handler';
|
||||
import { defineAction, groupAction, f } from '../action-kit';
|
||||
import { RETCODE, failedResponse, okResponse } from '../types';
|
||||
|
||||
// NapCat/LLOneBot omit `busid` from their `get_group_file_url` schema entirely,
|
||||
@@ -286,6 +285,3 @@ export const actions = [
|
||||
}),
|
||||
];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
registerActions(h, ctx, actions);
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { defineAction, groupAction, groupUserAction, registerActions, f } from '../action-kit';
|
||||
import type { ApiHandler, ApiActionContext } from '../api-handler';
|
||||
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';
|
||||
@@ -179,6 +178,3 @@ export const actions = [
|
||||
}),
|
||||
];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
registerActions(h, ctx, actions);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// Action registry barrel — the single source of "which files contribute which
|
||||
// actions", replacing three hand-synced lists that had to stay in lock-step:
|
||||
// 1. ApiHandler's 12 `register as registerX` imports + 12 constructor calls,
|
||||
// 2. each file's `export function register(h, ctx)` footer,
|
||||
// 3. action-docs' own 13 imports + GROUPS array.
|
||||
// Adding an action file now means editing exactly one place: ACTION_GROUPS.
|
||||
//
|
||||
// "Source file = domain category" is the existing convention (see action-docs),
|
||||
// so the category label lives here too, making ACTION_GROUPS serve both runtime
|
||||
// registration (via ALL_ACTIONS) and the doc collector (via the groups).
|
||||
|
||||
import type { RegisteredActionSpec } from '../action-kit';
|
||||
import { actions as infoActions } from './info';
|
||||
import { actions as messageActions } from './message';
|
||||
import { actions as friendActions } from './friend';
|
||||
import { actions as groupInfoActions } from './group-info';
|
||||
import { actions as groupAdminActions } from './group-admin';
|
||||
import { actions as groupFileActions } from './group-file';
|
||||
import { actions as requestActions } from './request';
|
||||
import { actions as extendedActions } from './extended';
|
||||
import { actions as groupAlbumActions } from './group-album';
|
||||
import { actions as qzoneActions } from './qzone';
|
||||
import { actions as streamFileActions } from './stream-file';
|
||||
import { actions as streamDownloadActions } from './stream-download';
|
||||
|
||||
export interface ActionGroup {
|
||||
/** Domain category (= source file), surfaced to the MCP / UI for grouping. */
|
||||
readonly category: string;
|
||||
readonly actions: readonly RegisteredActionSpec[];
|
||||
}
|
||||
|
||||
/** Every action, grouped by domain category. The single source of truth. */
|
||||
export const ACTION_GROUPS: readonly ActionGroup[] = [
|
||||
{ category: '信息', actions: infoActions },
|
||||
{ category: '消息', actions: messageActions },
|
||||
{ category: '好友', actions: friendActions },
|
||||
{ category: '群信息', actions: groupInfoActions },
|
||||
{ category: '群管理', actions: groupAdminActions },
|
||||
{ category: '群文件', actions: groupFileActions },
|
||||
{ category: '请求', actions: requestActions },
|
||||
{ category: '扩展', actions: extendedActions },
|
||||
{ category: '群相册', actions: groupAlbumActions },
|
||||
{ category: '空间', actions: qzoneActions },
|
||||
{ category: '流式接口', actions: [...streamFileActions, ...streamDownloadActions] },
|
||||
];
|
||||
|
||||
/** Flat list of every action spec, for one-shot registration onto an ApiHandler. */
|
||||
export const ALL_ACTIONS: readonly RegisteredActionSpec[] = ACTION_GROUPS.flatMap((g) => g.actions);
|
||||
@@ -1,5 +1,4 @@
|
||||
import { defineAction, registerActions } from '../action-kit';
|
||||
import type { ApiActionContext, ApiHandler } from '../api-handler';
|
||||
import { defineAction } from '../action-kit';
|
||||
import { okResponse } from '../types';
|
||||
|
||||
// 后续考虑移动到一个统一的地方构建,避免版本信息分散在各个模块中。
|
||||
@@ -98,6 +97,3 @@ export const actions = [
|
||||
}),
|
||||
];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
registerActions(h, ctx, actions);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import type { JsonObject, JsonValue } from '../types';
|
||||
import type { ApiHandler, ApiActionContext } from '../api-handler';
|
||||
import { defineAction, groupAction, registerActions, f } from '../action-kit';
|
||||
import type { ApiActionContext } from '../api-handler';
|
||||
import { defineAction, groupAction, f } from '../action-kit';
|
||||
import { RETCODE, failedResponse, okResponse } from '../types';
|
||||
|
||||
const log = createLogger('OneBot');
|
||||
@@ -119,6 +119,3 @@ export const actions = [
|
||||
}),
|
||||
];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
registerActions(h, ctx, actions);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { defineAction, registerActions, f } from '../action-kit';
|
||||
import type { ApiActionContext, ApiHandler } from '../api-handler';
|
||||
import { defineAction, f } from '../action-kit';
|
||||
import type { ApiActionContext } from '../api-handler';
|
||||
import type { JsonValue } from '../types';
|
||||
import { okResponse } from '../types';
|
||||
|
||||
@@ -204,6 +204,3 @@ export const actions = [
|
||||
}),
|
||||
];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
registerActions(h, ctx, actions);
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { defineAction, registerActions, f } from '../action-kit';
|
||||
import type { ApiActionContext, ApiHandler } from '../api-handler';
|
||||
import { defineAction, f } from '../action-kit';
|
||||
import { asString } from '../api-handler';
|
||||
import { okResponse } from '../types';
|
||||
|
||||
@@ -37,6 +36,3 @@ export const actions = [
|
||||
}),
|
||||
];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
registerActions(h, ctx, actions);
|
||||
}
|
||||
@@ -23,7 +23,7 @@ import net from 'net';
|
||||
import dns from 'dns/promises';
|
||||
import { resolveLocalFilePath } from '@snowluma/protocol/highway/utils';
|
||||
import { defineStreamAction, f } from '../action-kit';
|
||||
import type { ApiActionContext, ApiHandler } from '../api-handler';
|
||||
import type { ApiActionContext } from '../api-handler';
|
||||
import { okResponse, type ApiResponse, type JsonObject, type JsonValue } from '../types';
|
||||
import { type StreamSink, StreamStatus } from '../streaming';
|
||||
import { STREAM_ROOT } from './stream-file';
|
||||
@@ -370,9 +370,5 @@ const testDownloadStream = defineStreamAction({
|
||||
|
||||
export const actions = [downloadFileStream, downloadFileImageStream, downloadFileRecordStream, testDownloadStream];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
for (const a of actions) a.register(h, ctx);
|
||||
}
|
||||
|
||||
// Exposed for tests.
|
||||
export const __test = { resolveDownload, isWithinStreamRoot, isPrivateIp, assertSafeUrl, streamChunks, runDownload };
|
||||
@@ -23,7 +23,7 @@ import fs from 'fs';
|
||||
import { createHash } from 'crypto';
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import { defineAction, defineStreamAction, f } from '../action-kit';
|
||||
import type { ApiActionContext, ApiHandler } from '../api-handler';
|
||||
import type { ApiActionContext } from '../api-handler';
|
||||
import { okResponse, type ApiResponse } from '../types';
|
||||
import { StreamStatus } from '../streaming';
|
||||
|
||||
@@ -361,9 +361,5 @@ const cleanStreamTempFile = defineAction({
|
||||
|
||||
export const actions = [uploadFileStream, cleanStreamTempFile];
|
||||
|
||||
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
for (const a of actions) a.register(h, ctx);
|
||||
}
|
||||
|
||||
// Exposed for tests.
|
||||
export const __test = { uploads, handleUpload, cleanDir, validateStreamId, STREAM_UPLOAD_DIR, MAX_CONCURRENT_STREAMS, MAX_CHUNKS };
|
||||
Reference in new issue
Block a user