@zhin.js/adapter-qq 7.0.3 → 7.0.5
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 +80 -0
- package/README.md +36 -9
- package/adapters/qq.js +9 -7
- package/adapters/qq.ts +9 -7
- package/commands/endpoint/cancel.js +1 -1
- package/commands/endpoint/cancel.ts +1 -1
- package/lib/endpoint.js +55 -45
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/lib/platform-permit.d.ts +2 -3
- package/lib/platform-permit.js +3 -11
- package/lib/protocol.d.ts +9 -5
- package/lib/protocol.js +11 -7
- package/lib/qq-agent-deps.d.ts +2 -2
- package/lib/qq-agent-deps.js +7 -7
- package/lib/qq-bind-persist.d.ts +2 -2
- package/lib/qq-bind-persist.js +5 -5
- package/lib/qq-bot-kind-prompt.d.ts +9 -0
- package/lib/qq-bot-kind-prompt.js +37 -0
- package/lib/qq-endpoint-commands.d.ts +13 -6
- package/lib/qq-endpoint-commands.js +89 -31
- package/lib/qq-endpoint-config.d.ts +4 -4
- package/lib/qq-endpoint-config.js +4 -4
- package/lib/qq-intents.d.ts +27 -0
- package/lib/qq-intents.js +50 -0
- package/lib/qq-runtime-state.d.ts +17 -4
- package/lib/qq-runtime-state.js +3 -2
- package/lib/webhook.js +2 -0
- package/lib/ws.js +2 -2
- package/middlewares/bot-kind-reply.js +46 -0
- package/middlewares/bot-kind-reply.ts +60 -0
- package/package.json +23 -16
- package/plugin.js +6 -4
- package/schema.json +66 -9
- package/src/endpoint.ts +69 -46
- package/src/index.ts +10 -1
- package/src/platform-permit.ts +4 -9
- package/src/protocol.ts +19 -11
- package/src/qq-agent-deps.ts +8 -8
- package/src/qq-bind-persist.ts +5 -5
- package/src/qq-bot-kind-prompt.ts +47 -0
- package/src/qq-endpoint-commands.ts +125 -34
- package/src/qq-endpoint-config.ts +5 -5
- package/src/qq-intents.ts +76 -0
- package/src/qq-runtime-state.ts +21 -6
- package/src/webhook.ts +2 -0
- package/src/ws.ts +2 -2
- /package/commands/endpoint/add/{[[name]].js → [[id]].js} +0 -0
- /package/commands/endpoint/add/{[[name]].ts → [[id]].ts} +0 -0
- /package/commands/endpoint/remove/{[name].js → [id].js} +0 -0
- /package/commands/endpoint/remove/{[name].ts → [id].ts} +0 -0
package/lib/protocol.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { pickCredential } from '@zhin.js/adapter';
|
|
6
6
|
import { formatCompact, getLogger } from '@zhin.js/logger';
|
|
7
|
+
import { resolveQqIntents } from './qq-intents.js';
|
|
7
8
|
const logger = getLogger('qq');
|
|
8
9
|
export function resolveQqConfig(config = {}) {
|
|
9
10
|
const entry = config.endpoints?.find((item) => item.context === 'qq' || !item.context);
|
|
@@ -12,8 +13,8 @@ export function resolveQqConfig(config = {}) {
|
|
|
12
13
|
if (!appid || !secret) {
|
|
13
14
|
throw new TypeError('QQ adapter requires appid + secret (plugins.<key>.appid/secret or endpoints with context: qq)');
|
|
14
15
|
}
|
|
15
|
-
const
|
|
16
|
-
|| (typeof entry?.
|
|
16
|
+
const id = (typeof config.id === 'string' && config.id)
|
|
17
|
+
|| (typeof entry?.id === 'string' && entry.id)
|
|
17
18
|
|| process.env.QQ_BOT_NAME
|
|
18
19
|
|| 'qq-bot';
|
|
19
20
|
const mode = config.mode ?? entry?.mode ?? 'websocket';
|
|
@@ -23,7 +24,7 @@ export function resolveQqConfig(config = {}) {
|
|
|
23
24
|
return {
|
|
24
25
|
context: 'qq',
|
|
25
26
|
mode,
|
|
26
|
-
|
|
27
|
+
id,
|
|
27
28
|
appid,
|
|
28
29
|
secret,
|
|
29
30
|
webhookPath,
|
|
@@ -33,11 +34,14 @@ export function resolveQqConfig(config = {}) {
|
|
|
33
34
|
return {
|
|
34
35
|
context: 'qq',
|
|
35
36
|
mode: 'websocket',
|
|
36
|
-
|
|
37
|
+
id,
|
|
37
38
|
appid,
|
|
38
39
|
secret,
|
|
39
40
|
sandbox: config.sandbox === true || entry?.sandbox === true,
|
|
40
|
-
intents:
|
|
41
|
+
intents: resolveQqIntents({
|
|
42
|
+
intents: config.intents ?? entry?.intents,
|
|
43
|
+
botKind: config.botKind ?? entry?.botKind,
|
|
44
|
+
}),
|
|
41
45
|
accessTokenUrl: config.accessTokenUrl ?? entry?.accessTokenUrl,
|
|
42
46
|
gatewayUrl: config.gatewayUrl ?? entry?.gatewayUrl,
|
|
43
47
|
};
|
|
@@ -46,10 +50,10 @@ export function resolveQqConfig(config = {}) {
|
|
|
46
50
|
* 入站归一化 → ConversationRef:`direct`(频道私信)映射为 guild 容器内的
|
|
47
51
|
* private 会话;频道消息的所属 guild 进 `parent`。
|
|
48
52
|
*/
|
|
49
|
-
export function qqInboundConversation(
|
|
53
|
+
export function qqInboundConversation(endpointKey, msg) {
|
|
50
54
|
const kind = msg.channelKind === 'direct' ? 'private' : msg.channelKind;
|
|
51
55
|
return {
|
|
52
|
-
endpoint: { id:
|
|
56
|
+
endpoint: { id: endpointKey, adapter: endpointKey.split('\0')[0] ?? endpointKey },
|
|
53
57
|
kind,
|
|
54
58
|
id: msg.channelId,
|
|
55
59
|
...(msg.guildId && (msg.channelKind === 'channel' || msg.channelKind === 'direct')
|
package/lib/qq-agent-deps.d.ts
CHANGED
|
@@ -13,9 +13,9 @@ export interface QqAgentEndpoint {
|
|
|
13
13
|
removeMemberRole(guildId: string, channelId: string, userId: string, roleId: string): Promise<boolean>;
|
|
14
14
|
}
|
|
15
15
|
export interface QqAgentDeps {
|
|
16
|
-
getEndpoint: (
|
|
16
|
+
getEndpoint: (endpointKey: string) => QqAgentEndpoint;
|
|
17
17
|
}
|
|
18
|
-
export declare function registerQqAgentEndpoint(
|
|
18
|
+
export declare function registerQqAgentEndpoint(endpointKey: string, endpoint: QqAgentEndpoint): () => void;
|
|
19
19
|
/** Optional override used by tests / transitional callers. Pass `null` to clear. */
|
|
20
20
|
export declare function setQqAgentDeps(deps: QqAgentDeps | null): void;
|
|
21
21
|
export declare function getQqAgentDeps(): QqAgentDeps;
|
package/lib/qq-agent-deps.js
CHANGED
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
*/
|
|
5
5
|
const endpoints = new Map();
|
|
6
6
|
let override = null;
|
|
7
|
-
export function registerQqAgentEndpoint(
|
|
8
|
-
endpoints.set(
|
|
7
|
+
export function registerQqAgentEndpoint(endpointKey, endpoint) {
|
|
8
|
+
endpoints.set(endpointKey, endpoint);
|
|
9
9
|
return () => {
|
|
10
|
-
if (endpoints.get(
|
|
11
|
-
endpoints.delete(
|
|
10
|
+
if (endpoints.get(endpointKey) === endpoint) {
|
|
11
|
+
endpoints.delete(endpointKey);
|
|
12
12
|
}
|
|
13
13
|
};
|
|
14
14
|
}
|
|
@@ -20,10 +20,10 @@ export function getQqAgentDeps() {
|
|
|
20
20
|
if (override)
|
|
21
21
|
return override;
|
|
22
22
|
return {
|
|
23
|
-
getEndpoint(
|
|
24
|
-
const registered = endpoints.get(
|
|
23
|
+
getEndpoint(endpointKey) {
|
|
24
|
+
const registered = endpoints.get(endpointKey);
|
|
25
25
|
if (!registered)
|
|
26
|
-
throw new Error(`Endpoint ${
|
|
26
|
+
throw new Error(`Endpoint ${endpointKey} 不存在`);
|
|
27
27
|
return registered;
|
|
28
28
|
},
|
|
29
29
|
};
|
package/lib/qq-bind-persist.d.ts
CHANGED
|
@@ -7,6 +7,6 @@ export interface QqCredentialEnvKeys {
|
|
|
7
7
|
secretRef: string;
|
|
8
8
|
}
|
|
9
9
|
/** 按 endpoint 名称生成唯一 env 键(如 `QQ_MY_BOT_APPID`) */
|
|
10
|
-
export declare function buildQqCredentialEnvKeys(
|
|
10
|
+
export declare function buildQqCredentialEnvKeys(endpointId: string): QqCredentialEnvKeys;
|
|
11
11
|
/** 写入或更新 `.env` 中的键值,并同步到当前进程 `process.env` */
|
|
12
|
-
export declare function persistQqCredentialsToEnv(
|
|
12
|
+
export declare function persistQqCredentialsToEnv(endpointId: string, appId: string, appSecret: string, projectRoot?: string): QqCredentialEnvKeys;
|
package/lib/qq-bind-persist.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { buildEndpointEnvKey, persistEndpointEnvValues, resolveProjectRoot, } from '@zhin.js/adapter';
|
|
2
2
|
export { resolveProjectRoot };
|
|
3
3
|
/** 按 endpoint 名称生成唯一 env 键(如 `QQ_MY_BOT_APPID`) */
|
|
4
|
-
export function buildQqCredentialEnvKeys(
|
|
5
|
-
const appidKey = buildEndpointEnvKey('qq',
|
|
6
|
-
const secretKey = buildEndpointEnvKey('qq',
|
|
4
|
+
export function buildQqCredentialEnvKeys(endpointId) {
|
|
5
|
+
const appidKey = buildEndpointEnvKey('qq', endpointId, 'appid');
|
|
6
|
+
const secretKey = buildEndpointEnvKey('qq', endpointId, 'secret');
|
|
7
7
|
return {
|
|
8
8
|
appidKey,
|
|
9
9
|
secretKey,
|
|
@@ -12,8 +12,8 @@ export function buildQqCredentialEnvKeys(endpointName) {
|
|
|
12
12
|
};
|
|
13
13
|
}
|
|
14
14
|
/** 写入或更新 `.env` 中的键值,并同步到当前进程 `process.env` */
|
|
15
|
-
export function persistQqCredentialsToEnv(
|
|
16
|
-
const keys = buildQqCredentialEnvKeys(
|
|
15
|
+
export function persistQqCredentialsToEnv(endpointId, appId, appSecret, projectRoot) {
|
|
16
|
+
const keys = buildQqCredentialEnvKeys(endpointId);
|
|
17
17
|
persistEndpointEnvValues({
|
|
18
18
|
[keys.appidKey]: appId,
|
|
19
19
|
[keys.secretKey]: appSecret,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type QqBotKind } from './qq-intents.js';
|
|
2
|
+
/** 从命令/中间件入站消息提取会话键;无法识别时返回 undefined。 */
|
|
3
|
+
export declare function qqCommandSessionKey(input: unknown): string | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* 解析用户对公域/私域的回复。
|
|
6
|
+
* 接受:public / private / 公域 / 私域(大小写不敏感,可带前后空白)。
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseQqBotKindAnswer(raw: unknown): QqBotKind | undefined;
|
|
9
|
+
export declare const QQ_BOT_KIND_PROMPT: string;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { parseQqBotKind } from './qq-intents.js';
|
|
2
|
+
/** 从命令/中间件入站消息提取会话键;无法识别时返回 undefined。 */
|
|
3
|
+
export function qqCommandSessionKey(input) {
|
|
4
|
+
const message = input;
|
|
5
|
+
const conversation = message?.conversation;
|
|
6
|
+
const adapter = conversation?.endpoint?.adapter;
|
|
7
|
+
const endpointKey = conversation?.endpoint?.id;
|
|
8
|
+
const kind = conversation?.kind;
|
|
9
|
+
const id = conversation?.id;
|
|
10
|
+
const sender = message?.sender;
|
|
11
|
+
if (adapter == null
|
|
12
|
+
|| endpointKey == null
|
|
13
|
+
|| (kind !== 'private' && kind !== 'group' && kind !== 'channel')
|
|
14
|
+
|| typeof id !== 'string'
|
|
15
|
+
|| !id
|
|
16
|
+
|| sender == null
|
|
17
|
+
|| String(sender).trim() === '') {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
return `${String(adapter)}\0${String(endpointKey)}\0${kind}\0${id}\0${String(sender).trim()}`;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 解析用户对公域/私域的回复。
|
|
24
|
+
* 接受:public / private / 公域 / 私域(大小写不敏感,可带前后空白)。
|
|
25
|
+
*/
|
|
26
|
+
export function parseQqBotKindAnswer(raw) {
|
|
27
|
+
const text = String(raw ?? '').trim().toLowerCase();
|
|
28
|
+
if (!text)
|
|
29
|
+
return undefined;
|
|
30
|
+
if (text === '公域' || text === '公开' || text === '1')
|
|
31
|
+
return 'public';
|
|
32
|
+
if (text === '私域' || text === '2')
|
|
33
|
+
return 'private';
|
|
34
|
+
return parseQqBotKind(text);
|
|
35
|
+
}
|
|
36
|
+
export const QQ_BOT_KIND_PROMPT = '请回复 public(公域)或 private(私域),确认后一次性写入 .env 与配置。\n'
|
|
37
|
+
+ '也可发 qq.endpoint cancel 取消。';
|
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
* qqEndpointCommands 供 commands/endpoint/ 下的命令文件默认导出。
|
|
7
7
|
*/
|
|
8
8
|
import { type EndpointCommandReply } from '@zhin.js/adapter';
|
|
9
|
-
import { type
|
|
9
|
+
import { type QqBotKind } from './qq-intents.js';
|
|
10
|
+
import { type QqPendingBotKind, type QqRuntimeState } from './qq-runtime-state.js';
|
|
10
11
|
export type QqCommandReply = EndpointCommandReply;
|
|
11
12
|
/**
|
|
12
13
|
* endpoint 管理命令的操作者校验:实例配置声明了 master(顶层或任一端点项)时
|
|
@@ -22,15 +23,21 @@ export declare function extractQqCommandReply(input: unknown): QqCommandReply;
|
|
|
22
23
|
/** `qq.endpoint list`:运行中的 endpoints(本 generation adapter create 注册)+ 配置里的 endpoints */
|
|
23
24
|
export declare function runQqEndpointList(state: QqRuntimeState, projectRoot?: string): string;
|
|
24
25
|
/**
|
|
25
|
-
*
|
|
26
|
+
* 完成 pending 的公域/私域选择:一次性写 .env + yaml。
|
|
27
|
+
* @returns 成功文案;调用方负责清 pending。
|
|
28
|
+
*/
|
|
29
|
+
export declare function completeQqPendingBotKind(pending: QqPendingBotKind, botKind: QqBotKind, projectRoot?: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* `qq.endpoint add [id]`:启动扫码绑定流程。
|
|
26
32
|
* 返回的 Promise 在二维码链接就绪(或前置失败)时 resolve 为首条回复;
|
|
27
33
|
* 后续状态(已扫码 / 成功 / 失败 / 过期刷新)通过 reply 推回当前会话。
|
|
34
|
+
* 扫码成功后凭据暂存内存并询问公域/私域;确认后一次性写 .env + yaml。
|
|
28
35
|
*/
|
|
29
|
-
export declare function runQqEndpointAdd(state: QqRuntimeState,
|
|
30
|
-
/** `qq.endpoint cancel
|
|
36
|
+
export declare function runQqEndpointAdd(state: QqRuntimeState, id: string | undefined, reply: QqCommandReply, projectRoot?: string, input?: unknown): Promise<string>;
|
|
37
|
+
/** `qq.endpoint cancel`:中止进行中的绑定或待选 botKind */
|
|
31
38
|
export declare function runQqEndpointCancel(state: QqRuntimeState): string;
|
|
32
|
-
/** `qq.endpoint remove <
|
|
33
|
-
export declare function runQqEndpointRemove(_state: QqRuntimeState,
|
|
39
|
+
/** `qq.endpoint remove <id>`:从 zhin.config.yml 移除对应 endpoints 项 */
|
|
40
|
+
export declare function runQqEndpointRemove(_state: QqRuntimeState, id: string, projectRoot?: string): string;
|
|
34
41
|
/**
|
|
35
42
|
* 通用套件生成的 QQ endpoint 命令(add 经 bindFlow 钩子走扫码绑定)。
|
|
36
43
|
* commands/endpoint/ 下的 list / add / remove 直接默认导出这三项;cancel 为 QQ 特化,单独定义。
|
|
@@ -5,12 +5,14 @@
|
|
|
5
5
|
* 扫码绑定 add 流程(经套件 bindFlow 钩子接入)与 cancel,并导出
|
|
6
6
|
* qqEndpointCommands 供 commands/endpoint/ 下的命令文件默认导出。
|
|
7
7
|
*/
|
|
8
|
-
import { createEndpointCommands, endpointCommandForbidden, extractEndpointCommandReply, formatEndpointList, isEndpointOperator,
|
|
8
|
+
import { createEndpointCommands, endpointCommandForbidden, extractEndpointCommandReply, formatEndpointList, isEndpointOperator, removeEndpointById, } from '@zhin.js/adapter';
|
|
9
9
|
import { defineCommand } from '@zhin.js/command';
|
|
10
|
+
import { QQ_BOT_KIND_PROMPT, qqCommandSessionKey } from './qq-bot-kind-prompt.js';
|
|
10
11
|
import { startQqBindFlow } from './qq-bind-flow.js';
|
|
11
12
|
import { persistQqCredentialsToEnv } from './qq-bind-persist.js';
|
|
12
13
|
import { addQqEndpointToConfig, listQqEndpointEntries } from './qq-endpoint-config.js';
|
|
13
|
-
import {
|
|
14
|
+
import { defaultQqEndpointIntentFields } from './qq-intents.js';
|
|
15
|
+
import { qqRuntimeStateToken, } from './qq-runtime-state.js';
|
|
14
16
|
/**
|
|
15
17
|
* endpoint 管理命令的操作者校验:实例配置声明了 master(顶层或任一端点项)时
|
|
16
18
|
* 仅 master 可执行 add/cancel/remove;未配置则放行(首个扫码绑定者即为 owner,
|
|
@@ -26,12 +28,20 @@ export const QQ_ENDPOINT_FORBIDDEN = endpointCommandForbidden('QQ');
|
|
|
26
28
|
export function extractQqCommandReply(input) {
|
|
27
29
|
return extractEndpointCommandReply(input);
|
|
28
30
|
}
|
|
31
|
+
function busyFooter(state) {
|
|
32
|
+
if (state.bindFlow)
|
|
33
|
+
return '⚠️ 有进行中的扫码绑定,可用 qq.endpoint cancel 取消';
|
|
34
|
+
if (state.pendingBotKind) {
|
|
35
|
+
return `⚠️ endpoint「${state.pendingBotKind.endpointId}」等待公域/私域选择,回复 public/private 或 qq.endpoint cancel`;
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
29
39
|
/** `qq.endpoint list`:运行中的 endpoints(本 generation adapter create 注册)+ 配置里的 endpoints */
|
|
30
40
|
export function runQqEndpointList(state, projectRoot) {
|
|
31
41
|
return formatEndpointList(qqEndpointListSpec, {
|
|
32
42
|
running: state.endpoints.values(),
|
|
33
43
|
configured: listQqEndpointEntries(projectRoot),
|
|
34
|
-
footer: state
|
|
44
|
+
footer: busyFooter(state),
|
|
35
45
|
});
|
|
36
46
|
}
|
|
37
47
|
const qqEndpointListSpec = {
|
|
@@ -39,16 +49,39 @@ const qqEndpointListSpec = {
|
|
|
39
49
|
adapterDisplayName: 'QQ',
|
|
40
50
|
describeEntry: (entry) => `appid: ${String(entry.appid)}`,
|
|
41
51
|
};
|
|
52
|
+
function writeEndpointWithBotKind(pending, botKind, projectRoot) {
|
|
53
|
+
const envKeys = persistQqCredentialsToEnv(pending.endpointId, pending.appId, pending.appSecret, projectRoot);
|
|
54
|
+
const intentFields = defaultQqEndpointIntentFields(botKind);
|
|
55
|
+
const filePath = addQqEndpointToConfig({
|
|
56
|
+
id: pending.endpointId,
|
|
57
|
+
appid: envKeys.appidRef,
|
|
58
|
+
secret: envKeys.secretRef,
|
|
59
|
+
botKind: intentFields.botKind,
|
|
60
|
+
intents: [...intentFields.intents],
|
|
61
|
+
}, projectRoot);
|
|
62
|
+
return (`✅ 已按 botKind=${intentFields.botKind} 将 endpoint「${pending.endpointId}」写入 .env 与 ${filePath}。\n` +
|
|
63
|
+
`intents: ${intentFields.intents.join(', ')}。\n` +
|
|
64
|
+
'⚠️ 需重启 zhin 后新 endpoint 才会生效。');
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 完成 pending 的公域/私域选择:一次性写 .env + yaml。
|
|
68
|
+
* @returns 成功文案;调用方负责清 pending。
|
|
69
|
+
*/
|
|
70
|
+
export function completeQqPendingBotKind(pending, botKind, projectRoot) {
|
|
71
|
+
return writeEndpointWithBotKind(pending, botKind, projectRoot);
|
|
72
|
+
}
|
|
42
73
|
/**
|
|
43
|
-
* `qq.endpoint add [
|
|
74
|
+
* `qq.endpoint add [id]`:启动扫码绑定流程。
|
|
44
75
|
* 返回的 Promise 在二维码链接就绪(或前置失败)时 resolve 为首条回复;
|
|
45
76
|
* 后续状态(已扫码 / 成功 / 失败 / 过期刷新)通过 reply 推回当前会话。
|
|
77
|
+
* 扫码成功后凭据暂存内存并询问公域/私域;确认后一次性写 .env + yaml。
|
|
46
78
|
*/
|
|
47
|
-
export function runQqEndpointAdd(state,
|
|
48
|
-
if (state.bindFlow) {
|
|
49
|
-
return Promise.resolve('已有进行中的 QQ
|
|
79
|
+
export function runQqEndpointAdd(state, id, reply, projectRoot, input) {
|
|
80
|
+
if (state.bindFlow || state.pendingBotKind) {
|
|
81
|
+
return Promise.resolve('已有进行中的 QQ 机器人绑定或待确认的公域/私域选择,请先发送 qq.endpoint cancel 取消后再试');
|
|
50
82
|
}
|
|
51
|
-
const
|
|
83
|
+
const endpointId = id?.trim() || undefined;
|
|
84
|
+
const sessionKey = qqCommandSessionKey(input);
|
|
52
85
|
return new Promise((resolve) => {
|
|
53
86
|
let firstReplied = false;
|
|
54
87
|
const settle = (text) => {
|
|
@@ -57,7 +90,10 @@ export function runQqEndpointAdd(state, name, reply, projectRoot) {
|
|
|
57
90
|
resolve(text);
|
|
58
91
|
return;
|
|
59
92
|
}
|
|
60
|
-
|
|
93
|
+
// Follow-ups outlive the inbound Message reply scope; durable reply
|
|
94
|
+
// (OutboundHost) is provided by createEndpointCommands.bindFlow. Never
|
|
95
|
+
// let a late notify crash the process.
|
|
96
|
+
void Promise.resolve(reply(text)).catch(() => undefined);
|
|
61
97
|
};
|
|
62
98
|
const stop = startQqBindFlow({
|
|
63
99
|
onQrDisplayed: (url) => {
|
|
@@ -65,17 +101,37 @@ export function runQqEndpointAdd(state, name, reply, projectRoot) {
|
|
|
65
101
|
settle(`请用手机 QQ 打开以下链接完成扫码绑定(二维码图片出站暂未支持,故发送链接):\n${url}`);
|
|
66
102
|
},
|
|
67
103
|
onQrExpired: async () => {
|
|
68
|
-
|
|
104
|
+
try {
|
|
105
|
+
await reply('二维码已过期,正在刷新,请扫描新链接…');
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// same as settle follow-up: best-effort after command scope ends
|
|
109
|
+
}
|
|
69
110
|
},
|
|
70
111
|
onSuccess: async (credentials) => {
|
|
71
112
|
state.bindFlow = null;
|
|
72
113
|
try {
|
|
73
114
|
const [{ appId, appSecret }] = credentials;
|
|
74
|
-
const finalName =
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
115
|
+
const finalName = endpointId ?? appId;
|
|
116
|
+
// 无会话上下文(Host 等)时无法追问,直接按公域一次性写入
|
|
117
|
+
if (!sessionKey) {
|
|
118
|
+
settle(writeEndpointWithBotKind({
|
|
119
|
+
endpointId: finalName,
|
|
120
|
+
appId,
|
|
121
|
+
appSecret,
|
|
122
|
+
sessionKey: '',
|
|
123
|
+
}, 'public', projectRoot) +
|
|
124
|
+
'\n(当前会话无法交互选择公/私域;私域请手动改配置。)');
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
state.pendingBotKind = {
|
|
128
|
+
endpointId: finalName,
|
|
129
|
+
appId,
|
|
130
|
+
appSecret,
|
|
131
|
+
sessionKey,
|
|
132
|
+
};
|
|
133
|
+
settle(`✅ 扫码成功!endpoint「${finalName}」。\n` +
|
|
134
|
+
QQ_BOT_KIND_PROMPT);
|
|
79
135
|
}
|
|
80
136
|
catch (error) {
|
|
81
137
|
settle(`绑定成功但写入配置失败:${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -86,21 +142,26 @@ export function runQqEndpointAdd(state, name, reply, projectRoot) {
|
|
|
86
142
|
settle(`❌ QQ 绑定失败:${error.message}`);
|
|
87
143
|
},
|
|
88
144
|
}, { source: 'zhin' });
|
|
89
|
-
state.bindFlow = {
|
|
145
|
+
state.bindFlow = { id: endpointId, stop };
|
|
90
146
|
});
|
|
91
147
|
}
|
|
92
|
-
/** `qq.endpoint cancel
|
|
148
|
+
/** `qq.endpoint cancel`:中止进行中的绑定或待选 botKind */
|
|
93
149
|
export function runQqEndpointCancel(state) {
|
|
94
|
-
if (
|
|
95
|
-
|
|
150
|
+
if (state.bindFlow) {
|
|
151
|
+
state.bindFlow.stop();
|
|
152
|
+
state.bindFlow = null;
|
|
153
|
+
return '已取消进行中的 QQ 绑定流程';
|
|
154
|
+
}
|
|
155
|
+
if (state.pendingBotKind) {
|
|
156
|
+
const name = state.pendingBotKind.endpointId;
|
|
157
|
+
state.pendingBotKind = null;
|
|
158
|
+
return `已取消 endpoint「${name}」的绑定(尚未写入任何文件)`;
|
|
96
159
|
}
|
|
97
|
-
|
|
98
|
-
state.bindFlow = null;
|
|
99
|
-
return '已取消进行中的 QQ 绑定流程';
|
|
160
|
+
return '当前没有进行中的 QQ 绑定流程';
|
|
100
161
|
}
|
|
101
|
-
/** `qq.endpoint remove <
|
|
102
|
-
export function runQqEndpointRemove(_state,
|
|
103
|
-
return
|
|
162
|
+
/** `qq.endpoint remove <id>`:从 zhin.config.yml 移除对应 endpoints 项 */
|
|
163
|
+
export function runQqEndpointRemove(_state, id, projectRoot) {
|
|
164
|
+
return removeEndpointById(qqEndpointListSpec, id, projectRoot);
|
|
104
165
|
}
|
|
105
166
|
/**
|
|
106
167
|
* 通用套件生成的 QQ endpoint 命令(add 经 bindFlow 钩子走扫码绑定)。
|
|
@@ -113,10 +174,7 @@ export const qqEndpointCommands = createEndpointCommands({
|
|
|
113
174
|
{ key: 'secret', required: true, env: true, description: 'QQ 机器人 secret' },
|
|
114
175
|
],
|
|
115
176
|
running: (use) => use(qqRuntimeStateToken).endpoints.values(),
|
|
116
|
-
listFooter: (use) =>
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
},
|
|
120
|
-
addDescription: '手机 QQ 扫码绑定机器人,凭据写入 .env 并追加到 zhin.config.yml(重启生效)',
|
|
121
|
-
bindFlow: ({ name, reply, use }) => runQqEndpointAdd(use(qqRuntimeStateToken), name, reply),
|
|
177
|
+
listFooter: (use) => busyFooter(use(qqRuntimeStateToken)),
|
|
178
|
+
addDescription: '手机 QQ 扫码绑定机器人:确认公域/私域后一次性写入 .env 与 zhin.config.yml(重启生效)',
|
|
179
|
+
bindFlow: ({ id, reply, input, use }) => runQqEndpointAdd(use(qqRuntimeStateToken), id, reply, undefined, input),
|
|
122
180
|
}, defineCommand);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export interface QqEndpointConfigEntry {
|
|
2
|
-
|
|
2
|
+
id: string;
|
|
3
3
|
appid: string;
|
|
4
4
|
secret: string;
|
|
5
5
|
[key: string]: unknown;
|
|
@@ -8,10 +8,10 @@ export interface QqEndpointConfigEntry {
|
|
|
8
8
|
export declare function findQqConfigFile(projectRoot?: string): string;
|
|
9
9
|
/** 读取 plugins.qq.endpoints(plain JS);plugins/qq 缺失或形态不符时返回 [] */
|
|
10
10
|
export declare function listQqEndpointEntries(projectRoot?: string): QqEndpointConfigEntry[];
|
|
11
|
-
/** 追加 endpoint 到 plugins.qq.endpoints;
|
|
11
|
+
/** 追加 endpoint 到 plugins.qq.endpoints;id 已存在时报错 */
|
|
12
12
|
export declare function addQqEndpointToConfig(entry: QqEndpointConfigEntry, projectRoot?: string): string;
|
|
13
|
-
/** 按
|
|
14
|
-
export declare function removeQqEndpointFromConfig(
|
|
13
|
+
/** 按 id 移除 plugins.qq.endpoints 项;不存在返回 false */
|
|
14
|
+
export declare function removeQqEndpointFromConfig(id: string, projectRoot?: string): {
|
|
15
15
|
removed: boolean;
|
|
16
16
|
filePath: string;
|
|
17
17
|
};
|
|
@@ -12,11 +12,11 @@ export function findQqConfigFile(projectRoot) {
|
|
|
12
12
|
export function listQqEndpointEntries(projectRoot) {
|
|
13
13
|
return listConfiguredEndpoints('qq', projectRoot);
|
|
14
14
|
}
|
|
15
|
-
/** 追加 endpoint 到 plugins.qq.endpoints;
|
|
15
|
+
/** 追加 endpoint 到 plugins.qq.endpoints;id 已存在时报错 */
|
|
16
16
|
export function addQqEndpointToConfig(entry, projectRoot) {
|
|
17
17
|
return addEndpointToConfig('qq', entry, projectRoot);
|
|
18
18
|
}
|
|
19
|
-
/** 按
|
|
20
|
-
export function removeQqEndpointFromConfig(
|
|
21
|
-
return removeEndpointFromConfig('qq',
|
|
19
|
+
/** 按 id 移除 plugins.qq.endpoints 项;不存在返回 false */
|
|
20
|
+
export function removeQqEndpointFromConfig(id, projectRoot) {
|
|
21
|
+
return removeEndpointFromConfig('qq', id, projectRoot);
|
|
22
22
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QQ 官方机器人 intents 与公域/私域映射。
|
|
3
|
+
*
|
|
4
|
+
* 群聊无公/私域之分,两类都订阅 `GROUP_AND_C2C_EVENT`。
|
|
5
|
+
* 频道消息 intent 不同:
|
|
6
|
+
* - public(公域):`PUBLIC_GUILD_MESSAGES`(仅 @ 机器人)
|
|
7
|
+
* - private(私域):`GUILD_MESSAGES`(频道全量消息;公域订阅会 Identify 失败)
|
|
8
|
+
*/
|
|
9
|
+
export type QqBotKind = 'public' | 'private';
|
|
10
|
+
export type QqIntent = 'GUILDS' | 'GUILD_MEMBERS' | 'GUILD_MESSAGES' | 'GUILD_MESSAGE_REACTIONS' | 'DIRECT_MESSAGE' | 'GROUP_AND_C2C_EVENT' | 'INTERACTION' | 'MESSAGE_AUDIT' | 'FORUMS_EVENT' | 'AUDIO_ACTION' | 'PUBLIC_GUILD_MESSAGES' | 'GROUP_MEMBER';
|
|
11
|
+
/** 扫码绑定 / 未声明 botKind 时的默认(公域更常见)。 */
|
|
12
|
+
export declare const DEFAULT_QQ_BOT_KIND: QqBotKind;
|
|
13
|
+
export declare const QQ_INTENTS_BY_KIND: Readonly<Record<QqBotKind, readonly QqIntent[]>>;
|
|
14
|
+
/** 解析 botKind:仅接受 `public` / `private`。 */
|
|
15
|
+
export declare function parseQqBotKind(value: unknown): QqBotKind | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* 解析最终 intents:显式 `intents` 优先;否则按 `botKind`(默认 public)展开。
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolveQqIntents(input: {
|
|
20
|
+
readonly intents?: readonly string[] | undefined;
|
|
21
|
+
readonly botKind?: unknown;
|
|
22
|
+
}): readonly QqIntent[];
|
|
23
|
+
/** 添加 endpoint 时写入配置的默认字段(显式展开,便于用户改)。 */
|
|
24
|
+
export declare function defaultQqEndpointIntentFields(botKind?: QqBotKind): {
|
|
25
|
+
readonly botKind: QqBotKind;
|
|
26
|
+
readonly intents: readonly QqIntent[];
|
|
27
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QQ 官方机器人 intents 与公域/私域映射。
|
|
3
|
+
*
|
|
4
|
+
* 群聊无公/私域之分,两类都订阅 `GROUP_AND_C2C_EVENT`。
|
|
5
|
+
* 频道消息 intent 不同:
|
|
6
|
+
* - public(公域):`PUBLIC_GUILD_MESSAGES`(仅 @ 机器人)
|
|
7
|
+
* - private(私域):`GUILD_MESSAGES`(频道全量消息;公域订阅会 Identify 失败)
|
|
8
|
+
*/
|
|
9
|
+
/** 扫码绑定 / 未声明 botKind 时的默认(公域更常见)。 */
|
|
10
|
+
export const DEFAULT_QQ_BOT_KIND = 'public';
|
|
11
|
+
/** 公/私域共用:群@+私聊 C2C、频道基础事件、频道私信。 */
|
|
12
|
+
const QQ_SHARED_INTENTS = Object.freeze([
|
|
13
|
+
'GROUP_AND_C2C_EVENT',
|
|
14
|
+
'GUILDS',
|
|
15
|
+
'GUILD_MEMBERS',
|
|
16
|
+
'DIRECT_MESSAGE',
|
|
17
|
+
]);
|
|
18
|
+
export const QQ_INTENTS_BY_KIND = Object.freeze({
|
|
19
|
+
public: Object.freeze([
|
|
20
|
+
...QQ_SHARED_INTENTS,
|
|
21
|
+
'PUBLIC_GUILD_MESSAGES',
|
|
22
|
+
]),
|
|
23
|
+
private: Object.freeze([
|
|
24
|
+
...QQ_SHARED_INTENTS,
|
|
25
|
+
'GUILD_MESSAGES',
|
|
26
|
+
]),
|
|
27
|
+
});
|
|
28
|
+
/** 解析 botKind:仅接受 `public` / `private`。 */
|
|
29
|
+
export function parseQqBotKind(value) {
|
|
30
|
+
if (value === 'public' || value === 'private')
|
|
31
|
+
return value;
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 解析最终 intents:显式 `intents` 优先;否则按 `botKind`(默认 public)展开。
|
|
36
|
+
*/
|
|
37
|
+
export function resolveQqIntents(input) {
|
|
38
|
+
if (Array.isArray(input.intents) && input.intents.length > 0) {
|
|
39
|
+
return Object.freeze(input.intents.map(String));
|
|
40
|
+
}
|
|
41
|
+
const kind = parseQqBotKind(input.botKind) ?? DEFAULT_QQ_BOT_KIND;
|
|
42
|
+
return QQ_INTENTS_BY_KIND[kind];
|
|
43
|
+
}
|
|
44
|
+
/** 添加 endpoint 时写入配置的默认字段(显式展开,便于用户改)。 */
|
|
45
|
+
export function defaultQqEndpointIntentFields(botKind = DEFAULT_QQ_BOT_KIND) {
|
|
46
|
+
return Object.freeze({
|
|
47
|
+
botKind,
|
|
48
|
+
intents: QQ_INTENTS_BY_KIND[botKind],
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -1,17 +1,30 @@
|
|
|
1
1
|
export interface QqRunningEndpoint {
|
|
2
|
-
|
|
2
|
+
id: string;
|
|
3
3
|
mode: string;
|
|
4
4
|
}
|
|
5
5
|
export interface QqBindFlowHandle {
|
|
6
|
-
/** 绑定期望的 endpoint
|
|
7
|
-
|
|
6
|
+
/** 绑定期望的 endpoint id(未指定时为 undefined,成功后取 appId) */
|
|
7
|
+
id?: string;
|
|
8
8
|
stop: () => void;
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* 扫码成功后暂存在内存:确认公域/私域后再一次性写 .env + yaml。
|
|
12
|
+
* (提前写 .env 会触发 HMR,冲掉 pending。)
|
|
13
|
+
*/
|
|
14
|
+
export interface QqPendingBotKind {
|
|
15
|
+
readonly endpointId: string;
|
|
16
|
+
readonly appId: string;
|
|
17
|
+
readonly appSecret: string;
|
|
18
|
+
/** 发起 add 的会话键;仅同会话回复可完成选择 */
|
|
19
|
+
readonly sessionKey: string;
|
|
20
|
+
}
|
|
10
21
|
export interface QqRuntimeState {
|
|
11
|
-
/** 当前 generation 已成功创建的 endpoint(
|
|
22
|
+
/** 当前 generation 已成功创建的 endpoint(id → 描述) */
|
|
12
23
|
readonly endpoints: Map<string, QqRunningEndpoint>;
|
|
13
24
|
/** 全局单例:同一时间只允许一个扫码绑定流程 */
|
|
14
25
|
bindFlow: QqBindFlowHandle | null;
|
|
26
|
+
/** 扫码完成后等待 botKind 选择(凭据仅在内存) */
|
|
27
|
+
pendingBotKind: QqPendingBotKind | null;
|
|
15
28
|
}
|
|
16
29
|
export declare function createQqRuntimeState(): QqRuntimeState;
|
|
17
30
|
export declare const qqRuntimeStateToken: import("@zhin.js/plugin-runtime").Token<QqRuntimeState>;
|
package/lib/qq-runtime-state.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* QQ 插件实例的运行时状态:adapter create() 注册的 endpoint 列表 +
|
|
3
|
-
*
|
|
4
|
-
* adapter create 与 commands 通过 use(qqRuntimeStateToken)
|
|
3
|
+
* 扫码绑定 / 待选 botKind 流程。由 plugin.ts setup() provide,
|
|
4
|
+
* adapter create 与 commands / middleware 通过 use(qqRuntimeStateToken) 共享。
|
|
5
5
|
*/
|
|
6
6
|
import { createToken } from '@zhin.js/plugin-runtime';
|
|
7
7
|
export function createQqRuntimeState() {
|
|
8
8
|
return {
|
|
9
9
|
endpoints: new Map(),
|
|
10
10
|
bindFlow: null,
|
|
11
|
+
pendingBotKind: null,
|
|
11
12
|
};
|
|
12
13
|
}
|
|
13
14
|
export const qqRuntimeStateToken = createToken('zhin.qq.runtime-state', 'QQ adapter runtime state (running endpoints + bind flow singleton)');
|
package/lib/webhook.js
CHANGED
|
@@ -60,6 +60,8 @@ export function defaultCreateHttpBot(config) {
|
|
|
60
60
|
platform: 'koa',
|
|
61
61
|
sandbox: config.sandbox,
|
|
62
62
|
dataDir: path.join(process.cwd(), 'data', 'qq'),
|
|
63
|
+
// 关闭内置 log4js,与 WebSocket 模式一致走 zhin adapter logger
|
|
64
|
+
logLevel: 'off',
|
|
63
65
|
});
|
|
64
66
|
return {
|
|
65
67
|
start: () => bot.start().then(() => undefined),
|
package/lib/ws.js
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { Bot, ReceiverMode } from 'qq-official-bot';
|
|
6
|
-
import { getLevel, toLog4jsLevel } from '@zhin.js/logger';
|
|
7
6
|
/**
|
|
8
7
|
* qq-official-bot 1.2.x keeps a second `userClose` flag in Connection.
|
|
9
8
|
* Session.stop() does not update it, so a normal shutdown is treated as an
|
|
@@ -190,7 +189,8 @@ export function defaultCreateBot(config) {
|
|
|
190
189
|
mode: ReceiverMode.WEBSOCKET,
|
|
191
190
|
sandbox: config.sandbox,
|
|
192
191
|
dataDir: path.join(process.cwd(), 'data', 'qq'),
|
|
193
|
-
|
|
192
|
+
// 关闭 qq-official-bot 内置 log4js,入/出站由 getAdapterLogger('qq', …) 统一格式
|
|
193
|
+
logLevel: 'off',
|
|
194
194
|
...(config.intents ? { intents: config.intents } : {}),
|
|
195
195
|
...(config.accessTokenUrl ? { accessTokenUrl: config.accessTokenUrl } : {}),
|
|
196
196
|
...(config.gatewayUrl ? { gatewayUrl: config.gatewayUrl } : {}),
|