@ryantest/openclaw-qqbot 0.0.1
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/LICENSE +22 -0
- package/README.md +483 -0
- package/README.zh.md +478 -0
- package/bin/qqbot-cli.js +243 -0
- package/clawdbot.plugin.json +16 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +26 -0
- package/dist/src/admin-resolver.d.ts +27 -0
- package/dist/src/admin-resolver.js +122 -0
- package/dist/src/api.d.ts +156 -0
- package/dist/src/api.js +599 -0
- package/dist/src/channel.d.ts +11 -0
- package/dist/src/channel.js +354 -0
- package/dist/src/config.d.ts +25 -0
- package/dist/src/config.js +161 -0
- package/dist/src/credential-backup.d.ts +31 -0
- package/dist/src/credential-backup.js +66 -0
- package/dist/src/gateway.d.ts +18 -0
- package/dist/src/gateway.js +1265 -0
- package/dist/src/image-server.d.ts +68 -0
- package/dist/src/image-server.js +462 -0
- package/dist/src/inbound-attachments.d.ts +58 -0
- package/dist/src/inbound-attachments.js +234 -0
- package/dist/src/known-users.d.ts +100 -0
- package/dist/src/known-users.js +263 -0
- package/dist/src/message-queue.d.ts +50 -0
- package/dist/src/message-queue.js +115 -0
- package/dist/src/onboarding.d.ts +10 -0
- package/dist/src/onboarding.js +203 -0
- package/dist/src/outbound-deliver.d.ts +48 -0
- package/dist/src/outbound-deliver.js +462 -0
- package/dist/src/outbound.d.ts +203 -0
- package/dist/src/outbound.js +1102 -0
- package/dist/src/proactive.d.ts +170 -0
- package/dist/src/proactive.js +399 -0
- package/dist/src/ref-index-store.d.ts +70 -0
- package/dist/src/ref-index-store.js +273 -0
- package/dist/src/reply-dispatcher.d.ts +35 -0
- package/dist/src/reply-dispatcher.js +311 -0
- package/dist/src/runtime.d.ts +3 -0
- package/dist/src/runtime.js +10 -0
- package/dist/src/session-store.d.ts +52 -0
- package/dist/src/session-store.js +254 -0
- package/dist/src/slash-commands.d.ts +71 -0
- package/dist/src/slash-commands.js +1179 -0
- package/dist/src/startup-greeting.d.ts +30 -0
- package/dist/src/startup-greeting.js +78 -0
- package/dist/src/stt.d.ts +21 -0
- package/dist/src/stt.js +70 -0
- package/dist/src/tools/channel.d.ts +16 -0
- package/dist/src/tools/channel.js +234 -0
- package/dist/src/tools/remind.d.ts +2 -0
- package/dist/src/tools/remind.js +247 -0
- package/dist/src/types.d.ts +175 -0
- package/dist/src/types.js +1 -0
- package/dist/src/typing-keepalive.d.ts +27 -0
- package/dist/src/typing-keepalive.js +64 -0
- package/dist/src/update-checker.d.ts +34 -0
- package/dist/src/update-checker.js +166 -0
- package/dist/src/user-messages.d.ts +8 -0
- package/dist/src/user-messages.js +8 -0
- package/dist/src/utils/audio-convert.d.ts +89 -0
- package/dist/src/utils/audio-convert.js +704 -0
- package/dist/src/utils/file-utils.d.ts +55 -0
- package/dist/src/utils/file-utils.js +150 -0
- package/dist/src/utils/image-size.d.ts +51 -0
- package/dist/src/utils/image-size.js +234 -0
- package/dist/src/utils/media-tags.d.ts +14 -0
- package/dist/src/utils/media-tags.js +164 -0
- package/dist/src/utils/payload.d.ts +112 -0
- package/dist/src/utils/payload.js +186 -0
- package/dist/src/utils/platform.d.ts +137 -0
- package/dist/src/utils/platform.js +390 -0
- package/dist/src/utils/text-parsing.d.ts +32 -0
- package/dist/src/utils/text-parsing.js +80 -0
- package/dist/src/utils/upload-cache.d.ts +34 -0
- package/dist/src/utils/upload-cache.js +93 -0
- package/index.ts +31 -0
- package/moltbot.plugin.json +16 -0
- package/node_modules/@eshaz/web-worker/LICENSE +201 -0
- package/node_modules/@eshaz/web-worker/README.md +134 -0
- package/node_modules/@eshaz/web-worker/browser.js +17 -0
- package/node_modules/@eshaz/web-worker/cjs/browser.js +16 -0
- package/node_modules/@eshaz/web-worker/cjs/node.js +219 -0
- package/node_modules/@eshaz/web-worker/index.d.ts +4 -0
- package/node_modules/@eshaz/web-worker/node.js +223 -0
- package/node_modules/@eshaz/web-worker/package.json +54 -0
- package/node_modules/@wasm-audio-decoders/common/index.js +5 -0
- package/node_modules/@wasm-audio-decoders/common/package.json +36 -0
- package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderCommon.js +231 -0
- package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderWorker.js +129 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/README +67 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/build_puff.js +31 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/puff.c +863 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/puff.h +35 -0
- package/node_modules/@wasm-audio-decoders/common/src/utilities.js +3 -0
- package/node_modules/@wasm-audio-decoders/common/types.d.ts +7 -0
- package/node_modules/mpg123-decoder/README.md +265 -0
- package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js +185 -0
- package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js.map +1 -0
- package/node_modules/mpg123-decoder/index.js +8 -0
- package/node_modules/mpg123-decoder/package.json +58 -0
- package/node_modules/mpg123-decoder/src/EmscriptenWasm.js +464 -0
- package/node_modules/mpg123-decoder/src/MPEGDecoder.js +200 -0
- package/node_modules/mpg123-decoder/src/MPEGDecoderWebWorker.js +21 -0
- package/node_modules/mpg123-decoder/types.d.ts +30 -0
- package/node_modules/silk-wasm/LICENSE +21 -0
- package/node_modules/silk-wasm/README.md +85 -0
- package/node_modules/silk-wasm/lib/index.cjs +16 -0
- package/node_modules/silk-wasm/lib/index.d.ts +70 -0
- package/node_modules/silk-wasm/lib/index.mjs +16 -0
- package/node_modules/silk-wasm/lib/silk.wasm +0 -0
- package/node_modules/silk-wasm/lib/utils.d.ts +4 -0
- package/node_modules/silk-wasm/package.json +39 -0
- package/node_modules/simple-yenc/.github/FUNDING.yml +1 -0
- package/node_modules/simple-yenc/.prettierignore +1 -0
- package/node_modules/simple-yenc/LICENSE +7 -0
- package/node_modules/simple-yenc/README.md +163 -0
- package/node_modules/simple-yenc/dist/esm.js +1 -0
- package/node_modules/simple-yenc/dist/index.js +1 -0
- package/node_modules/simple-yenc/package.json +50 -0
- package/node_modules/simple-yenc/rollup.config.js +27 -0
- package/node_modules/simple-yenc/src/simple-yenc.js +302 -0
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +548 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +13 -0
- package/node_modules/ws/lib/buffer-util.js +131 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +528 -0
- package/node_modules/ws/lib/receiver.js +706 -0
- package/node_modules/ws/lib/sender.js +602 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +554 -0
- package/node_modules/ws/lib/websocket.js +1393 -0
- package/node_modules/ws/package.json +69 -0
- package/node_modules/ws/wrapper.mjs +8 -0
- package/openclaw.plugin.json +16 -0
- package/package.json +76 -0
- package/scripts/cleanup-legacy-plugins.sh +124 -0
- package/scripts/proactive-api-server.ts +369 -0
- package/scripts/send-proactive.ts +293 -0
- package/scripts/set-markdown.sh +156 -0
- package/scripts/test-sendmedia.ts +116 -0
- package/scripts/upgrade-via-alt-pkg.sh +307 -0
- package/scripts/upgrade-via-npm.ps1 +296 -0
- package/scripts/upgrade-via-npm.sh +301 -0
- package/scripts/upgrade-via-source.sh +774 -0
- package/skills/qqbot-channel/SKILL.md +263 -0
- package/skills/qqbot-channel/references/api_references.md +521 -0
- package/skills/qqbot-media/SKILL.md +56 -0
- package/skills/qqbot-remind/SKILL.md +149 -0
- package/src/admin-resolver.ts +140 -0
- package/src/api.ts +819 -0
- package/src/bot-logs-2026-03-21T11-21-47(2).txt +46 -0
- package/src/channel.ts +381 -0
- package/src/config.ts +187 -0
- package/src/credential-backup.ts +72 -0
- package/src/gateway.log +43 -0
- package/src/gateway.ts +1404 -0
- package/src/image-server.ts +539 -0
- package/src/inbound-attachments.ts +304 -0
- package/src/known-users.ts +353 -0
- package/src/message-queue.ts +169 -0
- package/src/onboarding.ts +274 -0
- package/src/openclaw-2026-03-21.log +3729 -0
- package/src/openclaw-plugin-sdk.d.ts +522 -0
- package/src/outbound-deliver.ts +552 -0
- package/src/outbound.ts +1266 -0
- package/src/proactive.ts +530 -0
- package/src/ref-index-store.ts +357 -0
- package/src/reply-dispatcher.ts +334 -0
- package/src/runtime.ts +14 -0
- package/src/session-store.ts +303 -0
- package/src/slash-commands.ts +1305 -0
- package/src/startup-greeting.ts +98 -0
- package/src/stt.ts +86 -0
- package/src/tools/channel.ts +281 -0
- package/src/tools/remind.ts +296 -0
- package/src/types.ts +183 -0
- package/src/typing-keepalive.ts +59 -0
- package/src/update-checker.ts +179 -0
- package/src/user-messages.ts +7 -0
- package/src/utils/audio-convert.ts +803 -0
- package/src/utils/file-utils.ts +167 -0
- package/src/utils/image-size.ts +266 -0
- package/src/utils/media-tags.ts +182 -0
- package/src/utils/payload.ts +265 -0
- package/src/utils/platform.ts +435 -0
- package/src/utils/text-parsing.ts +82 -0
- package/src/utils/upload-cache.ts +128 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { applyAccountNameToChannelSection, deleteAccountFromConfigSection, setAccountEnabledInConfigSection, } from "openclaw/plugin-sdk";
|
|
2
|
+
import { DEFAULT_ACCOUNT_ID, listQQBotAccountIds, resolveQQBotAccount, applyQQBotAccountConfig, resolveDefaultQQBotAccountId } from "./config.js";
|
|
3
|
+
import { sendText, sendMedia } from "./outbound.js";
|
|
4
|
+
import { startGateway } from "./gateway.js";
|
|
5
|
+
import { qqbotOnboardingAdapter } from "./onboarding.js";
|
|
6
|
+
import { getQQBotRuntime } from "./runtime.js";
|
|
7
|
+
import { saveCredentialBackup, loadCredentialBackup } from "./credential-backup.js";
|
|
8
|
+
import { initApiConfig } from "./api.js";
|
|
9
|
+
/** QQ Bot 单条消息文本长度上限 */
|
|
10
|
+
export const TEXT_CHUNK_LIMIT = 5000;
|
|
11
|
+
/**
|
|
12
|
+
* Markdown 感知的文本分块函数
|
|
13
|
+
* 委托给 SDK 内置的 channel.text.chunkMarkdownText
|
|
14
|
+
* 支持代码块自动关闭/重开、括号感知等
|
|
15
|
+
*/
|
|
16
|
+
export function chunkText(text, limit) {
|
|
17
|
+
const runtime = getQQBotRuntime();
|
|
18
|
+
return runtime.channel.text.chunkMarkdownText(text, limit);
|
|
19
|
+
}
|
|
20
|
+
export const qqbotPlugin = {
|
|
21
|
+
id: "qqbot",
|
|
22
|
+
meta: {
|
|
23
|
+
id: "qqbot",
|
|
24
|
+
label: "QQ Bot",
|
|
25
|
+
selectionLabel: "QQ Bot",
|
|
26
|
+
docsPath: "/docs/channels/qqbot",
|
|
27
|
+
blurb: "Connect to QQ via official QQ Bot API",
|
|
28
|
+
order: 50,
|
|
29
|
+
},
|
|
30
|
+
capabilities: {
|
|
31
|
+
chatTypes: ["direct", "group"],
|
|
32
|
+
media: true,
|
|
33
|
+
reactions: false,
|
|
34
|
+
threads: false,
|
|
35
|
+
/**
|
|
36
|
+
* blockStreaming: true 表示该 Channel 支持块流式
|
|
37
|
+
* 框架会收集流式响应,然后通过 deliver 回调发送
|
|
38
|
+
*/
|
|
39
|
+
blockStreaming: true,
|
|
40
|
+
},
|
|
41
|
+
reload: { configPrefixes: ["channels.qqbot"] },
|
|
42
|
+
// CLI onboarding wizard
|
|
43
|
+
onboarding: qqbotOnboardingAdapter,
|
|
44
|
+
config: {
|
|
45
|
+
listAccountIds: (cfg) => listQQBotAccountIds(cfg),
|
|
46
|
+
resolveAccount: (cfg, accountId) => resolveQQBotAccount(cfg, accountId),
|
|
47
|
+
defaultAccountId: (cfg) => resolveDefaultQQBotAccountId(cfg),
|
|
48
|
+
// 新增:设置账户启用状态
|
|
49
|
+
setAccountEnabled: ({ cfg, accountId, enabled }) => setAccountEnabledInConfigSection({
|
|
50
|
+
cfg,
|
|
51
|
+
sectionKey: "qqbot",
|
|
52
|
+
accountId,
|
|
53
|
+
enabled,
|
|
54
|
+
allowTopLevel: true,
|
|
55
|
+
}),
|
|
56
|
+
// 新增:删除账户
|
|
57
|
+
deleteAccount: ({ cfg, accountId }) => deleteAccountFromConfigSection({
|
|
58
|
+
cfg,
|
|
59
|
+
sectionKey: "qqbot",
|
|
60
|
+
accountId,
|
|
61
|
+
clearBaseFields: ["appId", "clientSecret", "clientSecretFile", "name"],
|
|
62
|
+
}),
|
|
63
|
+
isConfigured: (account) => {
|
|
64
|
+
if (account?.appId && account?.clientSecret)
|
|
65
|
+
return true;
|
|
66
|
+
// 配置为空但有凭证备份时仍返回 true,让 startAccount 有机会恢复凭证
|
|
67
|
+
const backup = loadCredentialBackup(account?.accountId);
|
|
68
|
+
return backup !== null;
|
|
69
|
+
},
|
|
70
|
+
describeAccount: (account) => ({
|
|
71
|
+
accountId: account?.accountId ?? DEFAULT_ACCOUNT_ID,
|
|
72
|
+
name: account?.name,
|
|
73
|
+
enabled: account?.enabled ?? false,
|
|
74
|
+
configured: Boolean(account?.appId && account?.clientSecret),
|
|
75
|
+
tokenSource: account?.secretSource,
|
|
76
|
+
}),
|
|
77
|
+
// 关键:解析 allowFrom 配置,用于命令授权
|
|
78
|
+
resolveAllowFrom: ({ cfg, accountId }) => {
|
|
79
|
+
const account = resolveQQBotAccount(cfg, accountId);
|
|
80
|
+
const allowFrom = account.config?.allowFrom ?? [];
|
|
81
|
+
console.log(`[qqbot] resolveAllowFrom: accountId=${accountId}, allowFrom=${JSON.stringify(allowFrom)}`);
|
|
82
|
+
return allowFrom.map((entry) => String(entry));
|
|
83
|
+
},
|
|
84
|
+
// 格式化 allowFrom 条目(移除 qqbot: 前缀,统一大写)
|
|
85
|
+
formatAllowFrom: ({ allowFrom }) => allowFrom
|
|
86
|
+
.map((entry) => String(entry).trim())
|
|
87
|
+
.filter(Boolean)
|
|
88
|
+
.map((entry) => entry.replace(/^qqbot:/i, ""))
|
|
89
|
+
.map((entry) => entry.toUpperCase()), // QQ openid 是大写的
|
|
90
|
+
},
|
|
91
|
+
setup: {
|
|
92
|
+
// 新增:规范化账户 ID
|
|
93
|
+
resolveAccountId: ({ accountId }) => accountId?.trim().toLowerCase() || DEFAULT_ACCOUNT_ID,
|
|
94
|
+
// 新增:应用账户名称
|
|
95
|
+
applyAccountName: ({ cfg, accountId, name }) => applyAccountNameToChannelSection({
|
|
96
|
+
cfg,
|
|
97
|
+
channelKey: "qqbot",
|
|
98
|
+
accountId,
|
|
99
|
+
name,
|
|
100
|
+
}),
|
|
101
|
+
validateInput: ({ input }) => {
|
|
102
|
+
if (!input.token && !input.tokenFile && !input.useEnv) {
|
|
103
|
+
return "QQBot requires --token (format: appId:clientSecret) or --use-env";
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
},
|
|
107
|
+
applyAccountConfig: ({ cfg, accountId, input }) => {
|
|
108
|
+
let appId = "";
|
|
109
|
+
let clientSecret = "";
|
|
110
|
+
if (input.token) {
|
|
111
|
+
const parts = input.token.split(":");
|
|
112
|
+
if (parts.length === 2) {
|
|
113
|
+
appId = parts[0];
|
|
114
|
+
clientSecret = parts[1];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return applyQQBotAccountConfig(cfg, accountId, {
|
|
118
|
+
appId,
|
|
119
|
+
clientSecret,
|
|
120
|
+
clientSecretFile: input.tokenFile,
|
|
121
|
+
name: input.name,
|
|
122
|
+
imageServerBaseUrl: input.imageServerBaseUrl,
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
// Messaging 配置:用于解析目标地址
|
|
127
|
+
messaging: {
|
|
128
|
+
/**
|
|
129
|
+
* 规范化目标地址
|
|
130
|
+
* 支持以下格式:
|
|
131
|
+
* - qqbot:c2c:openid -> 私聊
|
|
132
|
+
* - qqbot:group:groupid -> 群聊
|
|
133
|
+
* - qqbot:channel:channelid -> 频道
|
|
134
|
+
* - c2c:openid -> 私聊
|
|
135
|
+
* - group:groupid -> 群聊
|
|
136
|
+
* - channel:channelid -> 频道
|
|
137
|
+
* - 纯 openid(32位十六进制)-> 私聊
|
|
138
|
+
*/
|
|
139
|
+
normalizeTarget: (target) => {
|
|
140
|
+
// 去掉 qqbot: 前缀(如果有)
|
|
141
|
+
const id = target.replace(/^qqbot:/i, "");
|
|
142
|
+
// 检查是否是已知格式
|
|
143
|
+
if (id.startsWith("c2c:") || id.startsWith("group:") || id.startsWith("channel:")) {
|
|
144
|
+
return `qqbot:${id}`;
|
|
145
|
+
}
|
|
146
|
+
// 检查是否是纯 openid(32位十六进制,不带连字符)
|
|
147
|
+
// QQ Bot OpenID 格式类似: 207A5B8339D01F6582911C014668B77B
|
|
148
|
+
const openIdHexPattern = /^[0-9a-fA-F]{32}$/;
|
|
149
|
+
if (openIdHexPattern.test(id)) {
|
|
150
|
+
return `qqbot:c2c:${id}`;
|
|
151
|
+
}
|
|
152
|
+
// 检查是否是 UUID 格式的 openid(带连字符)
|
|
153
|
+
const openIdUuidPattern = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
|
154
|
+
if (openIdUuidPattern.test(id)) {
|
|
155
|
+
return `qqbot:c2c:${id}`;
|
|
156
|
+
}
|
|
157
|
+
// 不认识的格式,返回 undefined 让核心使用原始值
|
|
158
|
+
return undefined;
|
|
159
|
+
},
|
|
160
|
+
/**
|
|
161
|
+
* 目标解析器配置
|
|
162
|
+
* 用于判断一个目标 ID 是否看起来像 QQ Bot 的格式
|
|
163
|
+
*/
|
|
164
|
+
targetResolver: {
|
|
165
|
+
/**
|
|
166
|
+
* 判断目标 ID 是否可能是 QQ Bot 格式
|
|
167
|
+
* 支持以下格式:
|
|
168
|
+
* - qqbot:c2c:xxx
|
|
169
|
+
* - qqbot:group:xxx
|
|
170
|
+
* - qqbot:channel:xxx
|
|
171
|
+
* - c2c:xxx
|
|
172
|
+
* - group:xxx
|
|
173
|
+
* - channel:xxx
|
|
174
|
+
* - UUID 格式的 openid
|
|
175
|
+
*/
|
|
176
|
+
looksLikeId: (id) => {
|
|
177
|
+
// 带 qqbot: 前缀的格式
|
|
178
|
+
if (/^qqbot:(c2c|group|channel):/i.test(id)) {
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
// 不带前缀但有类型标识
|
|
182
|
+
if (/^(c2c|group|channel):/i.test(id)) {
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
// 32位十六进制 openid(不带连字符)
|
|
186
|
+
if (/^[0-9a-fA-F]{32}$/.test(id)) {
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
// UUID 格式的 openid(带连字符)
|
|
190
|
+
const openIdPattern = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
|
191
|
+
return openIdPattern.test(id);
|
|
192
|
+
},
|
|
193
|
+
hint: "QQ Bot 目标格式: qqbot:c2c:openid (私聊) 或 qqbot:group:groupid (群聊)",
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
outbound: {
|
|
197
|
+
deliveryMode: "direct",
|
|
198
|
+
chunker: (text, limit) => getQQBotRuntime().channel.text.chunkMarkdownText(text, limit),
|
|
199
|
+
chunkerMode: "markdown",
|
|
200
|
+
textChunkLimit: 5000,
|
|
201
|
+
sendText: async ({ to, text, accountId, replyToId, cfg }) => {
|
|
202
|
+
console.log(`[qqbot:channel] sendText called — accountId=${accountId}, to=${to}, replyToId=${replyToId}, text.length=${text?.length ?? 0}`);
|
|
203
|
+
console.log(`[qqbot:channel] sendText text preview: ${text?.slice(0, 100)}${(text?.length ?? 0) > 100 ? "..." : ""}`);
|
|
204
|
+
const account = resolveQQBotAccount(cfg, accountId);
|
|
205
|
+
initApiConfig({ markdownSupport: account.markdownSupport });
|
|
206
|
+
console.log(`[qqbot:channel] sendText resolved account: id=${account.accountId}, appId=${account.appId}, enabled=${account.enabled}`);
|
|
207
|
+
const result = await sendText({ to, text, accountId, replyToId, account });
|
|
208
|
+
console.log(`[qqbot:channel] sendText result: messageId=${result.messageId}, error=${result.error ?? "none"}`);
|
|
209
|
+
return {
|
|
210
|
+
channel: "qqbot",
|
|
211
|
+
messageId: result.messageId,
|
|
212
|
+
error: result.error ? new Error(result.error) : undefined,
|
|
213
|
+
};
|
|
214
|
+
},
|
|
215
|
+
sendMedia: async ({ to, text, mediaUrl, accountId, replyToId, cfg }) => {
|
|
216
|
+
console.log(`[qqbot:channel] sendMedia called — accountId=${accountId}, to=${to}, replyToId=${replyToId}, mediaUrl=${mediaUrl?.slice(0, 80)}, text.length=${text?.length ?? 0}`);
|
|
217
|
+
const account = resolveQQBotAccount(cfg, accountId);
|
|
218
|
+
initApiConfig({ markdownSupport: account.markdownSupport });
|
|
219
|
+
console.log(`[qqbot:channel] sendMedia resolved account: id=${account.accountId}, appId=${account.appId}, enabled=${account.enabled}`);
|
|
220
|
+
const result = await sendMedia({ to, text: text ?? "", mediaUrl: mediaUrl ?? "", accountId, replyToId, account });
|
|
221
|
+
console.log(`[qqbot:channel] sendMedia result: messageId=${result.messageId}, error=${result.error ?? "none"}`);
|
|
222
|
+
return {
|
|
223
|
+
channel: "qqbot",
|
|
224
|
+
messageId: result.messageId,
|
|
225
|
+
error: result.error ? new Error(result.error) : undefined,
|
|
226
|
+
};
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
gateway: {
|
|
230
|
+
startAccount: async (ctx) => {
|
|
231
|
+
let { account } = ctx;
|
|
232
|
+
const { abortSignal, log, cfg } = ctx;
|
|
233
|
+
// 凭证恢复:如果 appId/secret 为空(热更新打断可能导致配置丢失),尝试从暂存文件恢复
|
|
234
|
+
if (!account.appId || !account.clientSecret) {
|
|
235
|
+
const backup = loadCredentialBackup(account.accountId);
|
|
236
|
+
if (backup) {
|
|
237
|
+
log?.info(`[qqbot:${account.accountId}] 配置中凭证为空,从暂存文件恢复 (appId=${backup.appId}, savedAt=${backup.savedAt})`);
|
|
238
|
+
try {
|
|
239
|
+
const runtime = getQQBotRuntime();
|
|
240
|
+
const restoredCfg = applyQQBotAccountConfig(cfg, account.accountId, {
|
|
241
|
+
appId: backup.appId,
|
|
242
|
+
clientSecret: backup.clientSecret,
|
|
243
|
+
});
|
|
244
|
+
const configApi = runtime.config;
|
|
245
|
+
await configApi.writeConfigFile(restoredCfg);
|
|
246
|
+
// 重新解析 account 以获取恢复后的值
|
|
247
|
+
account = resolveQQBotAccount(restoredCfg, account.accountId);
|
|
248
|
+
log?.info(`[qqbot:${account.accountId}] 凭证已恢复`);
|
|
249
|
+
}
|
|
250
|
+
catch (e) {
|
|
251
|
+
log?.error(`[qqbot:${account.accountId}] 凭证恢复失败: ${e}`);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
log?.info(`[qqbot:${account.accountId}] Starting gateway — appId=${account.appId}, enabled=${account.enabled}, name=${account.name ?? "unnamed"}`);
|
|
256
|
+
console.log(`[qqbot:channel] startAccount: accountId=${account.accountId}, appId=${account.appId}, secretSource=${account.secretSource}`);
|
|
257
|
+
await startGateway({
|
|
258
|
+
account,
|
|
259
|
+
abortSignal,
|
|
260
|
+
cfg,
|
|
261
|
+
log,
|
|
262
|
+
onReady: () => {
|
|
263
|
+
log?.info(`[qqbot:${account.accountId}] Gateway ready`);
|
|
264
|
+
// 启动成功,保存凭证快照供后续恢复使用
|
|
265
|
+
saveCredentialBackup(account.accountId, account.appId, account.clientSecret);
|
|
266
|
+
ctx.setStatus({
|
|
267
|
+
...ctx.getStatus(),
|
|
268
|
+
running: true,
|
|
269
|
+
connected: true,
|
|
270
|
+
lastConnectedAt: Date.now(),
|
|
271
|
+
});
|
|
272
|
+
},
|
|
273
|
+
onError: (error) => {
|
|
274
|
+
log?.error(`[qqbot:${account.accountId}] Gateway error: ${error.message}`);
|
|
275
|
+
ctx.setStatus({
|
|
276
|
+
...ctx.getStatus(),
|
|
277
|
+
lastError: error.message,
|
|
278
|
+
});
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
},
|
|
282
|
+
// 新增:登出账户(清除配置中的凭证)
|
|
283
|
+
logoutAccount: async ({ accountId, cfg }) => {
|
|
284
|
+
const nextCfg = { ...cfg };
|
|
285
|
+
const nextQQBot = cfg.channels?.qqbot ? { ...cfg.channels.qqbot } : undefined;
|
|
286
|
+
let cleared = false;
|
|
287
|
+
let changed = false;
|
|
288
|
+
if (nextQQBot) {
|
|
289
|
+
const qqbot = nextQQBot;
|
|
290
|
+
if (accountId === DEFAULT_ACCOUNT_ID && qqbot.clientSecret) {
|
|
291
|
+
delete qqbot.clientSecret;
|
|
292
|
+
cleared = true;
|
|
293
|
+
changed = true;
|
|
294
|
+
}
|
|
295
|
+
const accounts = qqbot.accounts;
|
|
296
|
+
if (accounts && accountId in accounts) {
|
|
297
|
+
const entry = accounts[accountId];
|
|
298
|
+
if (entry && "clientSecret" in entry) {
|
|
299
|
+
delete entry.clientSecret;
|
|
300
|
+
cleared = true;
|
|
301
|
+
changed = true;
|
|
302
|
+
}
|
|
303
|
+
if (entry && Object.keys(entry).length === 0) {
|
|
304
|
+
delete accounts[accountId];
|
|
305
|
+
changed = true;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (changed && nextQQBot) {
|
|
310
|
+
nextCfg.channels = { ...nextCfg.channels, qqbot: nextQQBot };
|
|
311
|
+
const runtime = getQQBotRuntime();
|
|
312
|
+
const configApi = runtime.config;
|
|
313
|
+
await configApi.writeConfigFile(nextCfg);
|
|
314
|
+
}
|
|
315
|
+
const resolved = resolveQQBotAccount(changed ? nextCfg : cfg, accountId);
|
|
316
|
+
const loggedOut = resolved.secretSource === "none";
|
|
317
|
+
const envToken = Boolean(process.env.QQBOT_CLIENT_SECRET);
|
|
318
|
+
return { ok: true, cleared, envToken, loggedOut };
|
|
319
|
+
},
|
|
320
|
+
},
|
|
321
|
+
status: {
|
|
322
|
+
defaultRuntime: {
|
|
323
|
+
accountId: DEFAULT_ACCOUNT_ID,
|
|
324
|
+
running: false,
|
|
325
|
+
connected: false,
|
|
326
|
+
lastConnectedAt: null,
|
|
327
|
+
lastError: null,
|
|
328
|
+
lastInboundAt: null,
|
|
329
|
+
lastOutboundAt: null,
|
|
330
|
+
},
|
|
331
|
+
// 新增:构建通道摘要
|
|
332
|
+
buildChannelSummary: ({ snapshot }) => ({
|
|
333
|
+
configured: snapshot.configured ?? false,
|
|
334
|
+
tokenSource: snapshot.tokenSource ?? "none",
|
|
335
|
+
running: snapshot.running ?? false,
|
|
336
|
+
connected: snapshot.connected ?? false,
|
|
337
|
+
lastConnectedAt: snapshot.lastConnectedAt ?? null,
|
|
338
|
+
lastError: snapshot.lastError ?? null,
|
|
339
|
+
}),
|
|
340
|
+
buildAccountSnapshot: ({ account, runtime }) => ({
|
|
341
|
+
accountId: account?.accountId ?? DEFAULT_ACCOUNT_ID,
|
|
342
|
+
name: account?.name,
|
|
343
|
+
enabled: account?.enabled ?? false,
|
|
344
|
+
configured: Boolean(account?.appId && account?.clientSecret),
|
|
345
|
+
tokenSource: account?.secretSource,
|
|
346
|
+
running: runtime?.running ?? false,
|
|
347
|
+
connected: runtime?.connected ?? false,
|
|
348
|
+
lastConnectedAt: runtime?.lastConnectedAt ?? null,
|
|
349
|
+
lastError: runtime?.lastError ?? null,
|
|
350
|
+
lastInboundAt: runtime?.lastInboundAt ?? null,
|
|
351
|
+
lastOutboundAt: runtime?.lastOutboundAt ?? null,
|
|
352
|
+
}),
|
|
353
|
+
},
|
|
354
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ResolvedQQBotAccount } from "./types.js";
|
|
2
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk";
|
|
3
|
+
export declare const DEFAULT_ACCOUNT_ID = "default";
|
|
4
|
+
/**
|
|
5
|
+
* 列出所有 QQBot 账户 ID
|
|
6
|
+
*/
|
|
7
|
+
export declare function listQQBotAccountIds(cfg: OpenClawConfig): string[];
|
|
8
|
+
/**
|
|
9
|
+
* 获取默认账户 ID
|
|
10
|
+
*/
|
|
11
|
+
export declare function resolveDefaultQQBotAccountId(cfg: OpenClawConfig): string;
|
|
12
|
+
/**
|
|
13
|
+
* 解析 QQBot 账户配置
|
|
14
|
+
*/
|
|
15
|
+
export declare function resolveQQBotAccount(cfg: OpenClawConfig, accountId?: string | null): ResolvedQQBotAccount;
|
|
16
|
+
/**
|
|
17
|
+
* 应用账户配置
|
|
18
|
+
*/
|
|
19
|
+
export declare function applyQQBotAccountConfig(cfg: OpenClawConfig, accountId: string, input: {
|
|
20
|
+
appId?: string;
|
|
21
|
+
clientSecret?: string;
|
|
22
|
+
clientSecretFile?: string;
|
|
23
|
+
name?: string;
|
|
24
|
+
imageServerBaseUrl?: string;
|
|
25
|
+
}): OpenClawConfig;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
export const DEFAULT_ACCOUNT_ID = "default";
|
|
2
|
+
function normalizeAppId(raw) {
|
|
3
|
+
if (raw === null || raw === undefined)
|
|
4
|
+
return "";
|
|
5
|
+
return String(raw).trim();
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* 列出所有 QQBot 账户 ID
|
|
9
|
+
*/
|
|
10
|
+
export function listQQBotAccountIds(cfg) {
|
|
11
|
+
const ids = new Set();
|
|
12
|
+
const qqbot = cfg.channels?.qqbot;
|
|
13
|
+
if (qqbot?.appId) {
|
|
14
|
+
ids.add(DEFAULT_ACCOUNT_ID);
|
|
15
|
+
}
|
|
16
|
+
if (qqbot?.accounts) {
|
|
17
|
+
for (const accountId of Object.keys(qqbot.accounts)) {
|
|
18
|
+
if (qqbot.accounts[accountId]?.appId) {
|
|
19
|
+
ids.add(accountId);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return Array.from(ids);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 获取默认账户 ID
|
|
27
|
+
*/
|
|
28
|
+
export function resolveDefaultQQBotAccountId(cfg) {
|
|
29
|
+
const qqbot = cfg.channels?.qqbot;
|
|
30
|
+
// 如果有默认账户配置,返回 default
|
|
31
|
+
if (qqbot?.appId) {
|
|
32
|
+
return DEFAULT_ACCOUNT_ID;
|
|
33
|
+
}
|
|
34
|
+
// 否则返回第一个配置的账户
|
|
35
|
+
if (qqbot?.accounts) {
|
|
36
|
+
const ids = Object.keys(qqbot.accounts);
|
|
37
|
+
if (ids.length > 0) {
|
|
38
|
+
return ids[0];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return DEFAULT_ACCOUNT_ID;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 解析 QQBot 账户配置
|
|
45
|
+
*/
|
|
46
|
+
export function resolveQQBotAccount(cfg, accountId) {
|
|
47
|
+
const resolvedAccountId = accountId ?? DEFAULT_ACCOUNT_ID;
|
|
48
|
+
const qqbot = cfg.channels?.qqbot;
|
|
49
|
+
// 基础配置
|
|
50
|
+
let accountConfig = {};
|
|
51
|
+
let appId = "";
|
|
52
|
+
let clientSecret = "";
|
|
53
|
+
let secretSource = "none";
|
|
54
|
+
if (resolvedAccountId === DEFAULT_ACCOUNT_ID) {
|
|
55
|
+
// 默认账户从顶层读取
|
|
56
|
+
accountConfig = {
|
|
57
|
+
enabled: qqbot?.enabled,
|
|
58
|
+
name: qqbot?.name,
|
|
59
|
+
appId: qqbot?.appId,
|
|
60
|
+
clientSecret: qqbot?.clientSecret,
|
|
61
|
+
clientSecretFile: qqbot?.clientSecretFile,
|
|
62
|
+
dmPolicy: qqbot?.dmPolicy,
|
|
63
|
+
allowFrom: qqbot?.allowFrom,
|
|
64
|
+
systemPrompt: qqbot?.systemPrompt,
|
|
65
|
+
imageServerBaseUrl: qqbot?.imageServerBaseUrl,
|
|
66
|
+
markdownSupport: qqbot?.markdownSupport ?? true,
|
|
67
|
+
};
|
|
68
|
+
appId = normalizeAppId(qqbot?.appId);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// 命名账户从 accounts 读取
|
|
72
|
+
const account = qqbot?.accounts?.[resolvedAccountId];
|
|
73
|
+
accountConfig = account ?? {};
|
|
74
|
+
appId = normalizeAppId(account?.appId);
|
|
75
|
+
}
|
|
76
|
+
// 解析 clientSecret
|
|
77
|
+
if (accountConfig.clientSecret) {
|
|
78
|
+
clientSecret = accountConfig.clientSecret;
|
|
79
|
+
secretSource = "config";
|
|
80
|
+
}
|
|
81
|
+
else if (accountConfig.clientSecretFile) {
|
|
82
|
+
// 从文件读取(运行时处理)
|
|
83
|
+
secretSource = "file";
|
|
84
|
+
}
|
|
85
|
+
else if (process.env.QQBOT_CLIENT_SECRET && resolvedAccountId === DEFAULT_ACCOUNT_ID) {
|
|
86
|
+
clientSecret = process.env.QQBOT_CLIENT_SECRET;
|
|
87
|
+
secretSource = "env";
|
|
88
|
+
}
|
|
89
|
+
// AppId 也可以从环境变量读取
|
|
90
|
+
if (!appId && process.env.QQBOT_APP_ID && resolvedAccountId === DEFAULT_ACCOUNT_ID) {
|
|
91
|
+
appId = normalizeAppId(process.env.QQBOT_APP_ID);
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
accountId: resolvedAccountId,
|
|
95
|
+
name: accountConfig.name,
|
|
96
|
+
enabled: accountConfig.enabled !== false,
|
|
97
|
+
appId,
|
|
98
|
+
clientSecret,
|
|
99
|
+
secretSource,
|
|
100
|
+
systemPrompt: accountConfig.systemPrompt,
|
|
101
|
+
imageServerBaseUrl: accountConfig.imageServerBaseUrl || process.env.QQBOT_IMAGE_SERVER_BASE_URL,
|
|
102
|
+
markdownSupport: accountConfig.markdownSupport !== false,
|
|
103
|
+
config: accountConfig,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* 应用账户配置
|
|
108
|
+
*/
|
|
109
|
+
export function applyQQBotAccountConfig(cfg, accountId, input) {
|
|
110
|
+
const next = { ...cfg };
|
|
111
|
+
if (accountId === DEFAULT_ACCOUNT_ID) {
|
|
112
|
+
// 如果没有设置过 allowFrom,默认设置为 ["*"]
|
|
113
|
+
const existingConfig = next.channels?.qqbot || {};
|
|
114
|
+
const allowFrom = existingConfig.allowFrom ?? ["*"];
|
|
115
|
+
next.channels = {
|
|
116
|
+
...next.channels,
|
|
117
|
+
qqbot: {
|
|
118
|
+
...(next.channels?.qqbot || {}),
|
|
119
|
+
enabled: true,
|
|
120
|
+
allowFrom,
|
|
121
|
+
...(input.appId ? { appId: input.appId } : {}),
|
|
122
|
+
...(input.clientSecret
|
|
123
|
+
? { clientSecret: input.clientSecret }
|
|
124
|
+
: input.clientSecretFile
|
|
125
|
+
? { clientSecretFile: input.clientSecretFile }
|
|
126
|
+
: {}),
|
|
127
|
+
...(input.name ? { name: input.name } : {}),
|
|
128
|
+
...(input.imageServerBaseUrl ? { imageServerBaseUrl: input.imageServerBaseUrl } : {}),
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
// 如果没有设置过 allowFrom,默认设置为 ["*"]
|
|
134
|
+
const existingAccountConfig = next.channels?.qqbot?.accounts?.[accountId] || {};
|
|
135
|
+
const allowFrom = existingAccountConfig.allowFrom ?? ["*"];
|
|
136
|
+
next.channels = {
|
|
137
|
+
...next.channels,
|
|
138
|
+
qqbot: {
|
|
139
|
+
...(next.channels?.qqbot || {}),
|
|
140
|
+
enabled: true,
|
|
141
|
+
accounts: {
|
|
142
|
+
...(next.channels?.qqbot?.accounts || {}),
|
|
143
|
+
[accountId]: {
|
|
144
|
+
...(next.channels?.qqbot?.accounts?.[accountId] || {}),
|
|
145
|
+
enabled: true,
|
|
146
|
+
allowFrom,
|
|
147
|
+
...(input.appId ? { appId: input.appId } : {}),
|
|
148
|
+
...(input.clientSecret
|
|
149
|
+
? { clientSecret: input.clientSecret }
|
|
150
|
+
: input.clientSecretFile
|
|
151
|
+
? { clientSecretFile: input.clientSecretFile }
|
|
152
|
+
: {}),
|
|
153
|
+
...(input.name ? { name: input.name } : {}),
|
|
154
|
+
...(input.imageServerBaseUrl ? { imageServerBaseUrl: input.imageServerBaseUrl } : {}),
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
return next;
|
|
161
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 凭证暂存与恢复
|
|
3
|
+
*
|
|
4
|
+
* 解决热更新被打断时 openclaw.json 中 appId/secret 丢失的问题。
|
|
5
|
+
*
|
|
6
|
+
* 原理:
|
|
7
|
+
* - 每次 gateway 成功启动后,把当前账户的 appId/secret 写入暂存文件
|
|
8
|
+
* - 插件启动时如果检测到配置中 appId/secret 为空,尝试从暂存文件恢复
|
|
9
|
+
* - 暂存文件存储在 ~/.openclaw/qqbot/data/ 下,不受插件目录替换影响
|
|
10
|
+
*
|
|
11
|
+
* 安全保障:
|
|
12
|
+
* - 只在 appId/secret **确实为空** 时才尝试恢复(不干扰正常配置变更)
|
|
13
|
+
* - 恢复后通过 openclaw 的 config API 写回配置文件,确保框架感知到变更
|
|
14
|
+
* - 暂存文件使用原子写入(先写 .tmp 再 rename)防止损坏
|
|
15
|
+
*/
|
|
16
|
+
interface CredentialBackup {
|
|
17
|
+
accountId: string;
|
|
18
|
+
appId: string;
|
|
19
|
+
clientSecret: string;
|
|
20
|
+
savedAt: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 保存凭证快照到暂存文件(gateway 成功启动后调用)
|
|
24
|
+
*/
|
|
25
|
+
export declare function saveCredentialBackup(accountId: string, appId: string, clientSecret: string): void;
|
|
26
|
+
/**
|
|
27
|
+
* 从暂存文件读取凭证(仅在配置为空时调用)
|
|
28
|
+
* 返回 null 表示无可用备份
|
|
29
|
+
*/
|
|
30
|
+
export declare function loadCredentialBackup(accountId?: string): CredentialBackup | null;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 凭证暂存与恢复
|
|
3
|
+
*
|
|
4
|
+
* 解决热更新被打断时 openclaw.json 中 appId/secret 丢失的问题。
|
|
5
|
+
*
|
|
6
|
+
* 原理:
|
|
7
|
+
* - 每次 gateway 成功启动后,把当前账户的 appId/secret 写入暂存文件
|
|
8
|
+
* - 插件启动时如果检测到配置中 appId/secret 为空,尝试从暂存文件恢复
|
|
9
|
+
* - 暂存文件存储在 ~/.openclaw/qqbot/data/ 下,不受插件目录替换影响
|
|
10
|
+
*
|
|
11
|
+
* 安全保障:
|
|
12
|
+
* - 只在 appId/secret **确实为空** 时才尝试恢复(不干扰正常配置变更)
|
|
13
|
+
* - 恢复后通过 openclaw 的 config API 写回配置文件,确保框架感知到变更
|
|
14
|
+
* - 暂存文件使用原子写入(先写 .tmp 再 rename)防止损坏
|
|
15
|
+
*/
|
|
16
|
+
import fs from "node:fs";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import { getQQBotDataDir } from "./utils/platform.js";
|
|
19
|
+
const BACKUP_FILENAME = "credential-backup.json";
|
|
20
|
+
function getBackupPath() {
|
|
21
|
+
return path.join(getQQBotDataDir("data"), BACKUP_FILENAME);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 保存凭证快照到暂存文件(gateway 成功启动后调用)
|
|
25
|
+
*/
|
|
26
|
+
export function saveCredentialBackup(accountId, appId, clientSecret) {
|
|
27
|
+
if (!appId || !clientSecret)
|
|
28
|
+
return; // 不保存空凭证
|
|
29
|
+
try {
|
|
30
|
+
const backupPath = getBackupPath();
|
|
31
|
+
const data = {
|
|
32
|
+
accountId,
|
|
33
|
+
appId,
|
|
34
|
+
clientSecret,
|
|
35
|
+
savedAt: new Date().toISOString(),
|
|
36
|
+
};
|
|
37
|
+
const tmpPath = backupPath + ".tmp";
|
|
38
|
+
fs.writeFileSync(tmpPath, JSON.stringify(data, null, 2) + "\n", "utf8");
|
|
39
|
+
fs.renameSync(tmpPath, backupPath);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// 非关键操作,静默忽略
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 从暂存文件读取凭证(仅在配置为空时调用)
|
|
47
|
+
* 返回 null 表示无可用备份
|
|
48
|
+
*/
|
|
49
|
+
export function loadCredentialBackup(accountId) {
|
|
50
|
+
try {
|
|
51
|
+
const backupPath = getBackupPath();
|
|
52
|
+
if (!fs.existsSync(backupPath))
|
|
53
|
+
return null;
|
|
54
|
+
const raw = fs.readFileSync(backupPath, "utf8");
|
|
55
|
+
const data = JSON.parse(raw);
|
|
56
|
+
if (!data.appId || !data.clientSecret)
|
|
57
|
+
return null;
|
|
58
|
+
// 如果指定了 accountId,校验是否匹配
|
|
59
|
+
if (accountId && data.accountId !== accountId)
|
|
60
|
+
return null;
|
|
61
|
+
return data;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ResolvedQQBotAccount } from "./types.js";
|
|
2
|
+
export interface GatewayContext {
|
|
3
|
+
account: ResolvedQQBotAccount;
|
|
4
|
+
abortSignal: AbortSignal;
|
|
5
|
+
cfg: unknown;
|
|
6
|
+
onReady?: (data: unknown) => void;
|
|
7
|
+
onError?: (error: Error) => void;
|
|
8
|
+
log?: {
|
|
9
|
+
info: (msg: string) => void;
|
|
10
|
+
error: (msg: string) => void;
|
|
11
|
+
debug?: (msg: string) => void;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* 启动 Gateway WebSocket 连接(带自动重连)
|
|
16
|
+
* 支持流式消息发送
|
|
17
|
+
*/
|
|
18
|
+
export declare function startGateway(ctx: GatewayContext): Promise<void>;
|