@zhin.js/core 1.0.45 → 1.0.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/lib/adapter.d.ts +13 -13
- package/lib/adapter.d.ts.map +1 -1
- package/lib/adapter.js +18 -63
- package/lib/adapter.js.map +1 -1
- package/lib/bot.d.ts +8 -2
- package/lib/bot.d.ts.map +1 -1
- package/lib/built/common-adapter-tools.d.ts +10 -14
- package/lib/built/common-adapter-tools.d.ts.map +1 -1
- package/lib/built/common-adapter-tools.js +47 -21
- package/lib/built/common-adapter-tools.js.map +1 -1
- package/lib/built/message-filter.d.ts +175 -0
- package/lib/built/message-filter.d.ts.map +1 -0
- package/lib/built/message-filter.js +236 -0
- package/lib/built/message-filter.js.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -0
- package/lib/index.js.map +1 -1
- package/lib/notice.d.ts +81 -0
- package/lib/notice.d.ts.map +1 -0
- package/lib/notice.js +11 -0
- package/lib/notice.js.map +1 -0
- package/lib/plugin.d.ts +6 -0
- package/lib/plugin.d.ts.map +1 -1
- package/lib/plugin.js.map +1 -1
- package/lib/request.d.ts +85 -0
- package/lib/request.d.ts.map +1 -0
- package/lib/request.js +11 -0
- package/lib/request.js.map +1 -0
- package/package.json +6 -6
- package/src/adapter.ts +28 -81
- package/src/bot.ts +8 -2
- package/src/built/common-adapter-tools.ts +54 -25
- package/src/built/message-filter.ts +390 -0
- package/src/index.ts +3 -0
- package/src/notice.ts +98 -0
- package/src/plugin.ts +8 -0
- package/src/request.ts +95 -0
- package/tests/message-filter.test.ts +566 -0
- package/tests/notice.test.ts +198 -0
- package/tests/request.test.ts +221 -0
package/lib/request.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Adapters } from './adapter.js';
|
|
2
|
+
import type { MessageSender, MaybePromise } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* 请求类型枚举
|
|
5
|
+
*
|
|
6
|
+
* 常见 IM 请求事件分类:
|
|
7
|
+
* - friend_add: 好友添加请求
|
|
8
|
+
* - group_add: 主动申请入群
|
|
9
|
+
* - group_invite: 邀请入群请求
|
|
10
|
+
*
|
|
11
|
+
* 适配器可自行扩展更多子类型
|
|
12
|
+
*/
|
|
13
|
+
export type RequestType = 'friend_add' | 'group_add' | 'group_invite' | (string & {});
|
|
14
|
+
/**
|
|
15
|
+
* 请求频道信息
|
|
16
|
+
*/
|
|
17
|
+
export interface RequestChannel {
|
|
18
|
+
id: string;
|
|
19
|
+
type: 'group' | 'private' | 'channel';
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 请求基础结构
|
|
23
|
+
*
|
|
24
|
+
* 与 MessageBase / NoticeBase 同构设计。
|
|
25
|
+
* 核心区别:Request 提供 `$approve()` 和 `$reject()` 方法,用于快速处理请求。
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // 适配器中格式化请求
|
|
30
|
+
* const request = Request.from(rawEvent, {
|
|
31
|
+
* $id: rawEvent.flag,
|
|
32
|
+
* $adapter: 'icqq',
|
|
33
|
+
* $bot: botName,
|
|
34
|
+
* $type: 'group_invite',
|
|
35
|
+
* $channel: { id: groupId, type: 'group' },
|
|
36
|
+
* $sender: { id: userId, name: '邀请者' },
|
|
37
|
+
* $comment: '请求加群消息',
|
|
38
|
+
* $timestamp: Date.now(),
|
|
39
|
+
* $approve: async (remark?) => { await api.approve(flag, remark); },
|
|
40
|
+
* $reject: async (reason?) => { await api.reject(flag, reason); },
|
|
41
|
+
* });
|
|
42
|
+
* this.adapter.emit('request.receive', request);
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export interface RequestBase {
|
|
46
|
+
/** 请求唯一 ID / flag(平台提供的请求标识,用于后续处理) */
|
|
47
|
+
$id: string;
|
|
48
|
+
/** 适配器名称 */
|
|
49
|
+
$adapter: keyof Adapters;
|
|
50
|
+
/** Bot 名称 */
|
|
51
|
+
$bot: string;
|
|
52
|
+
/** 请求类型 */
|
|
53
|
+
$type: RequestType;
|
|
54
|
+
/** 请求子类型 */
|
|
55
|
+
$subType?: string;
|
|
56
|
+
/** 请求发生的频道/群/会话 */
|
|
57
|
+
$channel: RequestChannel;
|
|
58
|
+
/** 请求发送者 */
|
|
59
|
+
$sender: MessageSender;
|
|
60
|
+
/** 请求附言/验证消息 */
|
|
61
|
+
$comment?: string;
|
|
62
|
+
/** 请求时间戳 */
|
|
63
|
+
$timestamp: number;
|
|
64
|
+
/**
|
|
65
|
+
* 同意请求
|
|
66
|
+
* @param remark 备注信息(如好友备注)
|
|
67
|
+
*/
|
|
68
|
+
$approve(remark?: string): MaybePromise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* 拒绝请求
|
|
71
|
+
* @param reason 拒绝原因
|
|
72
|
+
*/
|
|
73
|
+
$reject(reason?: string): MaybePromise<void>;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* 完整请求类型,支持平台原始数据扩展
|
|
77
|
+
*/
|
|
78
|
+
export type Request<T extends object = {}> = RequestBase & T;
|
|
79
|
+
export declare namespace Request {
|
|
80
|
+
/**
|
|
81
|
+
* 工具方法:合并自定义字段与基础请求结构
|
|
82
|
+
*/
|
|
83
|
+
function from<T extends object>(input: T, format: RequestBase): Request<T>;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACxC,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE9D;;;;;;;;;GASG;AACH,MAAM,MAAM,WAAW,GACnB,YAAY,GACZ,WAAW,GACX,cAAc,GACd,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;AAElB;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS,CAAC;CACvC;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,WAAW,WAAW;IAC1B,uCAAuC;IACvC,GAAG,EAAE,MAAM,CAAC;IACZ,YAAY;IACZ,QAAQ,EAAE,MAAM,QAAQ,CAAC;IACzB,aAAa;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW;IACX,KAAK,EAAE,WAAW,CAAC;IACnB,YAAY;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,mBAAmB;IACnB,QAAQ,EAAE,cAAc,CAAC;IACzB,YAAY;IACZ,OAAO,EAAE,aAAa,CAAC;IACvB,gBAAgB;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY;IACZ,UAAU,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IAC9C;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;CAC9C;AAED;;GAEG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,EAAE,IAAI,WAAW,GAAG,CAAC,CAAC;AAE7D,yBAAiB,OAAO,CAAC;IACvB;;OAEG;IACH,SAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC,CAEhF;CACF"}
|
package/lib/request.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAuFA,MAAM,KAAW,OAAO,CAOvB;AAPD,WAAiB,OAAO;IACtB;;OAEG;IACH,SAAgB,IAAI,CAAmB,KAAQ,EAAE,MAAmB;QAClE,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACtC,CAAC;IAFe,YAAI,OAEnB,CAAA;AACH,CAAC,EAPgB,OAAO,KAAP,OAAO,QAOvB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.47",
|
|
4
4
|
"description": "Zhin机器人核心框架",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -37,11 +37,11 @@
|
|
|
37
37
|
"segment-matcher": "^1.0.5",
|
|
38
38
|
"smol-toml": "^1.6.0",
|
|
39
39
|
"yaml": "^2.3.4",
|
|
40
|
-
"@zhin.js/ai": "1.0.
|
|
41
|
-
"@zhin.js/database": "1.0.
|
|
42
|
-
"@zhin.js/kernel": "0.0.
|
|
43
|
-
"@zhin.js/logger": "0.1.
|
|
44
|
-
"@zhin.js/schema": "1.0.
|
|
40
|
+
"@zhin.js/ai": "1.0.8",
|
|
41
|
+
"@zhin.js/database": "1.0.34",
|
|
42
|
+
"@zhin.js/kernel": "0.0.8",
|
|
43
|
+
"@zhin.js/logger": "0.1.31",
|
|
44
|
+
"@zhin.js/schema": "1.0.31"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/node": "^24.3.0",
|
package/src/adapter.ts
CHANGED
|
@@ -2,29 +2,18 @@ import { Bot } from "./bot.js";
|
|
|
2
2
|
import { Plugin } from "./plugin.js";
|
|
3
3
|
import { EventEmitter } from "events";
|
|
4
4
|
import { Message } from "./message.js";
|
|
5
|
+
import { Notice, NoticeType } from "./notice.js";
|
|
6
|
+
import { Request, RequestType } from "./request.js";
|
|
5
7
|
import { BeforeSendHandler, SendOptions, Tool, ToolContext, ToolScope } from "./types.js";
|
|
6
8
|
import { segment } from "./utils.js";
|
|
7
9
|
import { ZhinTool, isZhinTool, type ToolInput } from "./built/tool.js";
|
|
8
10
|
import type { Skill, SkillFeature } from "./built/skill.js";
|
|
9
|
-
import {
|
|
10
|
-
type IGroupManagement,
|
|
11
|
-
GROUP_METHOD_SPECS,
|
|
12
|
-
GROUP_MANAGEMENT_SKILL_DESCRIPTION,
|
|
13
|
-
GROUP_MANAGEMENT_SKILL_TAGS,
|
|
14
|
-
GROUP_MANAGEMENT_SKILL_KEYWORDS,
|
|
15
|
-
buildMethodArgs,
|
|
16
|
-
} from "./built/common-adapter-tools.js";
|
|
17
11
|
/**
|
|
18
|
-
* Adapter类:适配器抽象,管理多平台Bot实例。
|
|
12
|
+
* Adapter 类:适配器抽象,管理多平台 Bot 实例。
|
|
19
13
|
* 负责根据配置启动/关闭各平台机器人,统一异常处理。
|
|
20
14
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* 群管理能力:
|
|
24
|
-
* Adapter 基类声明了 IGroupManagement 中的可选方法规范,
|
|
25
|
-
* 具体适配器(ICQQ/Discord/Telegram 等)选择性覆写。
|
|
26
|
-
* 调用 registerGroupManagementSkill() 后,基类自动检测
|
|
27
|
-
* 哪些方法已被实现,生成对应的 Tool 并注册为 "群聊管理" Skill。
|
|
15
|
+
* 适配器可提供 AI 工具并声明 Skill。群管等能力由各 IM 平台在子类中
|
|
16
|
+
* 自行实现方法并注册 Tool(如使用 createGroupManagementTools)+ declareSkill。
|
|
28
17
|
*/
|
|
29
18
|
export abstract class Adapter<R extends Bot = Bot> extends EventEmitter<Adapter.Lifecycle> {
|
|
30
19
|
/** 当前适配器下所有Bot实例,key为bot名称 */
|
|
@@ -66,6 +55,21 @@ export abstract class Adapter<R extends Bot = Bot> extends EventEmitter<Adapter.
|
|
|
66
55
|
});
|
|
67
56
|
}
|
|
68
57
|
});
|
|
58
|
+
this.on('notice.receive', (notice) => {
|
|
59
|
+
this.logger.info(`${notice.$bot} notice ${notice.$type}${notice.$subType ? '.' + notice.$subType : ''} ${notice.$channel.type}(${notice.$channel.id})`);
|
|
60
|
+
const rootPlugin = this.plugin?.root || this.plugin;
|
|
61
|
+
rootPlugin?.dispatch('notice.receive', notice);
|
|
62
|
+
// 分发细粒度事件: notice.{type} 和 notice.{channelType}.{type}
|
|
63
|
+
rootPlugin?.dispatch(`notice.${notice.$type}` as 'notice.receive', notice);
|
|
64
|
+
rootPlugin?.dispatch(`notice.${notice.$channel.type}.${notice.$type}` as 'notice.receive', notice);
|
|
65
|
+
});
|
|
66
|
+
this.on('request.receive', (request) => {
|
|
67
|
+
this.logger.info(`${request.$bot} request ${request.$type}${request.$subType ? '.' + request.$subType : ''} from ${request.$sender.id}`);
|
|
68
|
+
const rootPlugin = this.plugin?.root || this.plugin;
|
|
69
|
+
rootPlugin?.dispatch('request.receive', request);
|
|
70
|
+
// 分发细粒度事件: request.{type}
|
|
71
|
+
rootPlugin?.dispatch(`request.${request.$type}` as 'request.receive', request);
|
|
72
|
+
});
|
|
69
73
|
}
|
|
70
74
|
abstract createBot(config: Adapter.BotConfig<R>): R;
|
|
71
75
|
get logger() {
|
|
@@ -101,8 +105,6 @@ export abstract class Adapter<R extends Bot = Bot> extends EventEmitter<Adapter.
|
|
|
101
105
|
this.bots.set(bot.$id, bot);
|
|
102
106
|
}
|
|
103
107
|
this.logger.debug(`adapter ${this.name} started`);
|
|
104
|
-
|
|
105
|
-
this._autoDetectGroupManagement();
|
|
106
108
|
}
|
|
107
109
|
/**
|
|
108
110
|
* 停止适配器,断开并移除所有Bot实例
|
|
@@ -322,69 +324,6 @@ export abstract class Adapter<R extends Bot = Bot> extends EventEmitter<Adapter.
|
|
|
322
324
|
});
|
|
323
325
|
}
|
|
324
326
|
|
|
325
|
-
// ==========================================================================
|
|
326
|
-
// 群管理自动检测 — start() 结束时自动执行
|
|
327
|
-
// ==========================================================================
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* 遍历 GROUP_METHOD_SPECS,检测子类是否覆写了对应方法。
|
|
331
|
-
* 对每个已实现的方法生成 Tool 并注册,最后聚合为 "群聊管理" Skill。
|
|
332
|
-
*/
|
|
333
|
-
private _autoDetectGroupManagement(): void {
|
|
334
|
-
const self = this as unknown as IGroupManagement;
|
|
335
|
-
const prefix = this.name as string;
|
|
336
|
-
const generatedTools: Tool[] = [];
|
|
337
|
-
|
|
338
|
-
for (const spec of GROUP_METHOD_SPECS) {
|
|
339
|
-
const fn = self[spec.method];
|
|
340
|
-
if (typeof fn !== 'function') continue;
|
|
341
|
-
|
|
342
|
-
const boundFn: (...args: any[]) => Promise<any> = fn.bind(self);
|
|
343
|
-
|
|
344
|
-
const properties: Record<string, any> = {
|
|
345
|
-
bot_id: { type: 'string', description: 'Bot ID', contextKey: 'botId' },
|
|
346
|
-
scene_id: { type: 'string', description: '群/服务器 ID', contextKey: 'sceneId' },
|
|
347
|
-
};
|
|
348
|
-
const required: string[] = ['bot_id', 'scene_id'];
|
|
349
|
-
|
|
350
|
-
for (const [name, schema] of Object.entries(spec.extraParams)) {
|
|
351
|
-
properties[name] = schema;
|
|
352
|
-
}
|
|
353
|
-
if (spec.extraRequired) {
|
|
354
|
-
required.push(...spec.extraRequired);
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const tool: Tool = {
|
|
358
|
-
name: `${prefix}_${spec.toolSuffix}`,
|
|
359
|
-
description: `${spec.description} (${prefix})`,
|
|
360
|
-
parameters: { type: 'object' as const, properties, required },
|
|
361
|
-
execute: async (args: Record<string, any>) => {
|
|
362
|
-
const { bot_id, scene_id, ...rest } = args;
|
|
363
|
-
return boundFn(...buildMethodArgs(spec.method, bot_id, scene_id, rest));
|
|
364
|
-
},
|
|
365
|
-
tags: ['group', 'management', prefix],
|
|
366
|
-
keywords: spec.keywords,
|
|
367
|
-
permissionLevel: spec.permissionLevel,
|
|
368
|
-
scopes: ['group', 'channel'] as ToolScope[],
|
|
369
|
-
preExecutable: spec.preExecutable,
|
|
370
|
-
};
|
|
371
|
-
|
|
372
|
-
this.addTool(tool);
|
|
373
|
-
generatedTools.push(tool);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
if (generatedTools.length === 0) return;
|
|
377
|
-
|
|
378
|
-
this.declareSkill({
|
|
379
|
-
description: GROUP_MANAGEMENT_SKILL_DESCRIPTION,
|
|
380
|
-
keywords: GROUP_MANAGEMENT_SKILL_KEYWORDS,
|
|
381
|
-
tags: GROUP_MANAGEMENT_SKILL_TAGS,
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
this.logger.debug(
|
|
385
|
-
`自动检测到 ${generatedTools.length} 个群管理方法 → 已注册 Skill`,
|
|
386
|
-
);
|
|
387
|
-
}
|
|
388
327
|
}
|
|
389
328
|
export interface Adapters {}
|
|
390
329
|
export namespace Adapter {
|
|
@@ -400,6 +339,14 @@ export namespace Adapter {
|
|
|
400
339
|
'message.private.receive': [Message];
|
|
401
340
|
'message.group.receive': [Message];
|
|
402
341
|
'message.channel.receive': [Message];
|
|
342
|
+
'notice.receive': [Notice];
|
|
343
|
+
'notice.private.receive': [Notice];
|
|
344
|
+
'notice.group.receive': [Notice];
|
|
345
|
+
'notice.channel.receive': [Notice];
|
|
346
|
+
'request.receive': [Request];
|
|
347
|
+
'request.friend_add': [Request];
|
|
348
|
+
'request.group_add': [Request];
|
|
349
|
+
'request.group_invite': [Request];
|
|
403
350
|
'call.recallMessage': [string, string];
|
|
404
351
|
}
|
|
405
352
|
/**
|
package/src/bot.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import type { SendOptions } from "./types.js";
|
|
2
2
|
import { Message } from "./message.js";
|
|
3
|
+
import { Notice } from "./notice.js";
|
|
4
|
+
import { Request } from "./request.js";
|
|
3
5
|
import { Adapter, Adapters } from "./adapter.js";
|
|
4
6
|
/**
|
|
5
7
|
* Bot接口:所有平台机器人需实现的统一接口。
|
|
6
8
|
* 负责消息格式化、连接、断开、消息发送等。
|
|
7
|
-
* @template
|
|
8
|
-
* @template
|
|
9
|
+
* @template Config 配置类型
|
|
10
|
+
* @template Event 消息事件类型
|
|
9
11
|
*/
|
|
10
12
|
export interface Bot<Config extends object= {},Event extends object = {}> {
|
|
11
13
|
$id:string
|
|
@@ -15,6 +17,10 @@ export interface Bot<Config extends object= {},Event extends object = {}> {
|
|
|
15
17
|
$connected: boolean;
|
|
16
18
|
/** 格式化平台消息为标准Message结构 */
|
|
17
19
|
$formatMessage(event: Event): Message<Event>;
|
|
20
|
+
/** 格式化平台通知为标准Notice结构(适配器可选实现) */
|
|
21
|
+
$formatNotice?(event: any): Notice<any>;
|
|
22
|
+
/** 格式化平台请求为标准Request结构(适配器可选实现) */
|
|
23
|
+
$formatRequest?(event: any): Request<any>;
|
|
18
24
|
/** 连接机器人 */
|
|
19
25
|
$connect(): Promise<void>;
|
|
20
26
|
/** 断开机器人 */
|
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Group Management
|
|
2
|
+
* Group Management — 方法规范与 Tool 工厂
|
|
3
3
|
*
|
|
4
4
|
* 设计理念:
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* 具体适配器(ICQQ/Discord/Telegram 等)选择性覆写。
|
|
8
|
-
* Adapter.start() 自动检测哪些方法已被子类实现,
|
|
9
|
-
* 生成对应的 Tool 并注册为 "群聊管理" Skill。
|
|
5
|
+
* 各 IM 平台在适配器内自行实现群管方法(kickMember、muteMember 等),
|
|
6
|
+
* 并自行注册 Tool 与 declareSkill,保障平台特性与描述一致。
|
|
10
7
|
*
|
|
11
|
-
*
|
|
8
|
+
* 使用方式(在各适配器 start 或注册方法中):
|
|
12
9
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* }
|
|
10
|
+
* import { createGroupManagementTools, GROUP_MANAGEMENT_SKILL_KEYWORDS, GROUP_MANAGEMENT_SKILL_TAGS } from 'zhin.js';
|
|
11
|
+
* const tools = createGroupManagementTools(this, this.name);
|
|
12
|
+
* tools.forEach(t => this.addTool(t));
|
|
13
|
+
* this.declareSkill({ description: '本平台群管说明...', keywords: [...], tags: [...] });
|
|
18
14
|
*/
|
|
19
15
|
|
|
20
|
-
import type { ToolPermissionLevel } from '../types.js';
|
|
16
|
+
import type { Tool, ToolPermissionLevel, ToolScope } from '../types.js';
|
|
21
17
|
|
|
22
18
|
// ============================================================================
|
|
23
19
|
// Adapter 群管理方法规范
|
|
@@ -168,25 +164,58 @@ export const GROUP_METHOD_SPECS: GroupMethodSpec[] = [
|
|
|
168
164
|
];
|
|
169
165
|
|
|
170
166
|
// ============================================================================
|
|
171
|
-
// Skill
|
|
167
|
+
// Skill 常量(各适配器 declareSkill 时可复用 keywords/tags)
|
|
172
168
|
// ============================================================================
|
|
173
169
|
|
|
174
|
-
export const GROUP_MANAGEMENT_SKILL_DESCRIPTION =
|
|
175
|
-
'群聊管理能力:在 IM 系统中对群/服务器进行管理,包括踢人、禁言、封禁、' +
|
|
176
|
-
'设置管理员、修改群名、查看成员列表等操作。具体可用的操作取决于平台和 Bot 权限。\n\n' +
|
|
177
|
-
'使用指南:\n' +
|
|
178
|
-
'1. 用户提供昵称/名片而非 ID 时,必须先调用 list_members 查询成员列表,从返回结果中匹配目标用户的 user_id,再执行后续操作\n' +
|
|
179
|
-
'2. 禁言(mute_member)适用场景:违规发言、刷屏、骚扰他人等;传 duration=0 可解除禁言\n' +
|
|
180
|
-
'3. 设置/取消管理员(set_admin)需要群主权限,普通管理员无法操作;enable=false 为取消管理员\n' +
|
|
181
|
-
'4. 踢人(kick_member)是将成员移出群聊,封禁(ban_member)是永久拉黑,两者不同\n' +
|
|
182
|
-
'5. 操作前应确认目标用户正确,避免误操作';
|
|
183
|
-
|
|
184
170
|
export const GROUP_MANAGEMENT_SKILL_TAGS = ['group', 'management', 'im', 'admin'];
|
|
185
171
|
export const GROUP_MANAGEMENT_SKILL_KEYWORDS = [
|
|
186
172
|
'群管理', '踢人', '禁言', '封禁', '管理员', '群名',
|
|
187
173
|
'成员', 'kick', 'mute', 'ban', 'admin', 'members',
|
|
188
174
|
];
|
|
189
175
|
|
|
176
|
+
// ============================================================================
|
|
177
|
+
// 工厂:根据已实现的方法为指定适配器生成群管 Tool 列表(各平台自行调用并 addTool)
|
|
178
|
+
// ============================================================================
|
|
179
|
+
|
|
180
|
+
export function createGroupManagementTools(
|
|
181
|
+
adapter: IGroupManagement,
|
|
182
|
+
prefix: string,
|
|
183
|
+
): Tool[] {
|
|
184
|
+
const tools: Tool[] = [];
|
|
185
|
+
for (const spec of GROUP_METHOD_SPECS) {
|
|
186
|
+
const fn = adapter[spec.method];
|
|
187
|
+
if (typeof fn !== 'function') continue;
|
|
188
|
+
|
|
189
|
+
const properties: Record<string, any> = {
|
|
190
|
+
bot_id: { type: 'string', description: 'Bot ID', contextKey: 'botId' },
|
|
191
|
+
scene_id: { type: 'string', description: '群/服务器 ID', contextKey: 'sceneId' },
|
|
192
|
+
};
|
|
193
|
+
const required: string[] = ['bot_id', 'scene_id'];
|
|
194
|
+
for (const [name, schema] of Object.entries(spec.extraParams)) {
|
|
195
|
+
properties[name] = schema;
|
|
196
|
+
}
|
|
197
|
+
if (spec.extraRequired) required.push(...spec.extraRequired);
|
|
198
|
+
|
|
199
|
+
const boundFn = fn.bind(adapter);
|
|
200
|
+
tools.push({
|
|
201
|
+
name: `${prefix}_${spec.toolSuffix}`,
|
|
202
|
+
description: `${spec.description} (${prefix})`,
|
|
203
|
+
parameters: { type: 'object' as const, properties, required },
|
|
204
|
+
execute: async (args: Record<string, any>) => {
|
|
205
|
+
const { bot_id, scene_id, ...rest } = args;
|
|
206
|
+
const methodArgs = buildMethodArgs(spec.method, bot_id, scene_id, rest);
|
|
207
|
+
return (boundFn as (...a: any[]) => Promise<any>).apply(adapter, methodArgs);
|
|
208
|
+
},
|
|
209
|
+
tags: ['group', 'management', prefix],
|
|
210
|
+
keywords: spec.keywords,
|
|
211
|
+
permissionLevel: spec.permissionLevel,
|
|
212
|
+
scopes: ['group', 'channel'] as ToolScope[],
|
|
213
|
+
preExecutable: spec.preExecutable,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return tools;
|
|
217
|
+
}
|
|
218
|
+
|
|
190
219
|
// ============================================================================
|
|
191
220
|
// 参数映射(method → 有序参数列表)
|
|
192
221
|
// ============================================================================
|
|
@@ -208,6 +237,6 @@ export function buildMethodArgs(
|
|
|
208
237
|
case 'setGroupName': return [botId, sceneId, rest.name];
|
|
209
238
|
case 'muteAll': return [botId, sceneId, rest.enable ?? true];
|
|
210
239
|
case 'getGroupInfo': return [botId, sceneId];
|
|
211
|
-
default: return [botId, sceneId, ...Object.values(rest)];
|
|
240
|
+
default: return [botId, sceneId, ...(Object.values(rest) as any[])];
|
|
212
241
|
}
|
|
213
242
|
}
|