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
@@ -13,49 +13,18 @@
|
||||
// Markdown default. Point any renderer at `collectActionDocs()`.
|
||||
|
||||
import type { ActionDoc } from './action-kit';
|
||||
import { actions as infoActions } from './actions/info';
|
||||
import { actions as messageActions } from './actions/message';
|
||||
import { actions as friendActions } from './actions/friend';
|
||||
import { actions as groupInfoActions } from './actions/group-info';
|
||||
import { actions as groupAdminActions } from './actions/group-admin';
|
||||
import { actions as groupFileActions } from './actions/group-file';
|
||||
import { actions as requestActions } from './actions/request';
|
||||
import { actions as extendedActions } from './actions/extended';
|
||||
import { actions as groupAlbumActions } from './actions/group-album';
|
||||
import { actions as qzoneActions } from './actions/qzone';
|
||||
import { actions as streamFileActions } from './actions/stream-file';
|
||||
import { actions as streamDownloadActions } from './actions/stream-download';
|
||||
|
||||
// The doc collector only ever calls `describe()`, so any spec carrying it
|
||||
// qualifies — this admits both `defineAction` (ActionSpec) and
|
||||
// `defineStreamAction` (StreamActionSpec) results without coupling to either.
|
||||
type AnySpec = { describe(): ActionDoc };
|
||||
|
||||
// Source file = domain category. Each doc is tagged so the MCP / UI can group.
|
||||
const GROUPS: ReadonlyArray<{ category: string; specs: readonly AnySpec[] }> = [
|
||||
{ category: '信息', specs: infoActions },
|
||||
{ category: '消息', specs: messageActions },
|
||||
{ category: '好友', specs: friendActions },
|
||||
{ category: '群信息', specs: groupInfoActions },
|
||||
{ category: '群管理', specs: groupAdminActions },
|
||||
{ category: '群文件', specs: groupFileActions },
|
||||
{ category: '请求', specs: requestActions },
|
||||
{ category: '扩展', specs: extendedActions },
|
||||
{ category: '群相册', specs: groupAlbumActions },
|
||||
{ category: '空间', specs: qzoneActions },
|
||||
{ category: '流式接口', specs: [...streamFileActions, ...streamDownloadActions] },
|
||||
];
|
||||
import { ACTION_GROUPS } from './actions';
|
||||
|
||||
/** Every declarative action's doc (with category), sorted by name. */
|
||||
export function collectActionDocs(): ActionDoc[] {
|
||||
return GROUPS
|
||||
.flatMap(({ category, specs }) => specs.map((s) => ({ ...s.describe(), category })))
|
||||
return ACTION_GROUPS
|
||||
.flatMap(({ category, actions }) => actions.map((s) => ({ ...s.describe(), category })))
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
}
|
||||
|
||||
/** Distinct categories with action counts. */
|
||||
export function collectCategories(): Array<{ category: string; count: number }> {
|
||||
return GROUPS.map(({ category, specs }) => ({ category, count: specs.length }));
|
||||
return ACTION_GROUPS.map(({ category, actions }) => ({ category, count: actions.length }));
|
||||
}
|
||||
|
||||
function paramRow(p: ActionDoc['params'][number]): string {
|
||||
|
||||
@@ -440,16 +440,24 @@ export interface ActionDoc {
|
||||
|
||||
type Handler = (params: JsonObject) => Promise<ApiResponse>;
|
||||
|
||||
export interface ActionSpec<S extends Spec> {
|
||||
/**
|
||||
* The narrow shape both `ActionSpec` and `StreamActionSpec` share — enough to
|
||||
* register a spec onto an `ApiHandler` and to collect its docs. The action
|
||||
* registry barrel (`actions/index.ts`) is typed against this so it can hold
|
||||
* both kinds in one flat list without coupling to either concrete generic.
|
||||
*/
|
||||
export interface RegisteredActionSpec {
|
||||
describe(): ActionDoc;
|
||||
register(h: ApiHandler, ctx: ApiActionContext): void;
|
||||
}
|
||||
|
||||
export interface ActionSpec<S extends Spec> extends RegisteredActionSpec {
|
||||
readonly names: string[];
|
||||
readonly params: S;
|
||||
/** Pure: coerce + validate + cross-field, no ctx, no I/O. The test surface. */
|
||||
parse(raw: JsonObject): CoerceResult<InferParams<S>>;
|
||||
/** Bind ctx and produce the `(params) => ApiResponse` for `registerAction`. */
|
||||
toHandler(ctx: ApiActionContext): Handler;
|
||||
/** Doc metadata (D4); a renderer walks this. */
|
||||
describe(): ActionDoc;
|
||||
register(h: ApiHandler, ctx: ApiActionContext): void;
|
||||
}
|
||||
|
||||
interface ActionDef<S extends Spec> {
|
||||
@@ -558,12 +566,10 @@ interface StreamActionDef<S extends Spec> {
|
||||
run: (p: InferParams<S>, ctx: ApiActionContext, raw: JsonObject, sink: StreamSink) => Promise<ApiResponse> | ApiResponse;
|
||||
}
|
||||
|
||||
export interface StreamActionSpec<S extends Spec> {
|
||||
export interface StreamActionSpec<S extends Spec> extends RegisteredActionSpec {
|
||||
readonly names: string[];
|
||||
readonly params: S;
|
||||
parse(raw: JsonObject): CoerceResult<InferParams<S>>;
|
||||
describe(): ActionDoc;
|
||||
register(h: ApiHandler, ctx: ApiActionContext): void;
|
||||
}
|
||||
|
||||
export function defineStreamAction<S extends Spec>(def: StreamActionDef<S>): StreamActionSpec<S> {
|
||||
@@ -639,6 +645,6 @@ export function groupUserAction<S extends Spec>(
|
||||
|
||||
/** Register many specs onto an ApiHandler. Coexists with legacy
|
||||
* `h.registerAction(name, fn)` in the same `register(h, ctx)`. */
|
||||
export function registerActions(h: ApiHandler, ctx: ApiActionContext, specs: ReadonlyArray<ActionSpec<Spec>>): void {
|
||||
export function registerActions(h: ApiHandler, ctx: ApiActionContext, specs: ReadonlyArray<RegisteredActionSpec>): void {
|
||||
for (const spec of specs) spec.register(h, ctx);
|
||||
}
|
||||
@@ -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 };
|
||||
@@ -1,21 +1,11 @@
|
||||
import { renderParamsVerbose, summarizeParams } from '@snowluma/common/log-summary';
|
||||
import { createLogger, getLogLevel, nextRequestId, runWithRequestId, type Logger } from '@snowluma/common/logger';
|
||||
import type { BridgeInterface } from '@snowluma/core/bridge-interface';
|
||||
import { register as registerExtended } from './actions/extended';
|
||||
import { register as registerFriend } from './actions/friend';
|
||||
import { register as registerGroupAdmin } from './actions/group-admin';
|
||||
import { register as registerGroupAlbum } from './actions/group-album';
|
||||
import { register as registerGroupFile } from './actions/group-file';
|
||||
import { register as registerGroupInfo } from './actions/group-info';
|
||||
import { register as registerInfo } from './actions/info';
|
||||
import { register as registerMessage } from './actions/message';
|
||||
import { register as registerQzone } from './actions/qzone';
|
||||
import { register as registerRequest } from './actions/request';
|
||||
import { register as registerStreamFile } from './actions/stream-file';
|
||||
import { register as registerStreamDownload } from './actions/stream-download';
|
||||
import { ALL_ACTIONS } from './actions';
|
||||
import { registerActions } from './action-kit';
|
||||
import type { ForwardPreviewMeta } from './modules/message-actions';
|
||||
import type { JsonObject, JsonValue, MessageMeta } from './types';
|
||||
import { RETCODE, failedResponse } from './types';
|
||||
import { RETCODE, failedResponse, okResponse } from './types';
|
||||
import { type StreamSink, wrapStreamFrame, wrapStreamTerminal } from './streaming';
|
||||
const moduleLog = createLogger('Bridge.Action');
|
||||
|
||||
@@ -112,18 +102,20 @@ export class ApiHandler {
|
||||
|
||||
constructor(context: ApiActionContext, uin?: number) {
|
||||
this.log = typeof uin === 'number' && uin > 0 ? moduleLog.child({ uin }) : moduleLog;
|
||||
registerInfo(this, context);
|
||||
registerMessage(this, context);
|
||||
registerFriend(this, context);
|
||||
registerGroupInfo(this, context);
|
||||
registerGroupAdmin(this, context);
|
||||
registerGroupFile(this, context);
|
||||
registerRequest(this, context);
|
||||
registerExtended(this, context);
|
||||
registerGroupAlbum(this, context);
|
||||
registerQzone(this, context);
|
||||
registerStreamFile(this, context);
|
||||
registerStreamDownload(this, context);
|
||||
registerActions(this, context, ALL_ACTIONS);
|
||||
|
||||
// The one non-ActionSpec handler: `.handle_quick_operation` needs the
|
||||
// ApiHandler itself (to re-drive actions via executeQuickOperation), which
|
||||
// ActionSpec.run's (params, ctx) signature can't supply — so it's the sole
|
||||
// raw registration, kept here rather than in an action file's footer.
|
||||
this.registerAction('.handle_quick_operation', async (params) => {
|
||||
const opContext = params.context as JsonObject | undefined;
|
||||
const operation = params.operation as Record<string, unknown> | undefined;
|
||||
if (!opContext || !operation) return failedResponse(RETCODE.BAD_REQUEST, 'context and operation are required');
|
||||
const { executeQuickOperation } = await import('./network/quick-operation');
|
||||
await executeQuickOperation(opContext, operation, this);
|
||||
return okResponse();
|
||||
});
|
||||
}
|
||||
|
||||
registerAction(action: string, handler: ActionHandler): void {
|
||||
|
||||
Reference in new issue
Block a user