@tencent-connect/openclaw-qqbot 1.0.0-alpha.0

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.
Files changed (141) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +393 -0
  3. package/README.zh.md +390 -0
  4. package/bin/qqbot-cli.js +243 -0
  5. package/clawdbot.plugin.json +16 -0
  6. package/dist/index.d.ts +17 -0
  7. package/dist/index.js +22 -0
  8. package/dist/src/api.d.ts +138 -0
  9. package/dist/src/api.js +523 -0
  10. package/dist/src/channel.d.ts +3 -0
  11. package/dist/src/channel.js +337 -0
  12. package/dist/src/config.d.ts +25 -0
  13. package/dist/src/config.js +156 -0
  14. package/dist/src/gateway.d.ts +18 -0
  15. package/dist/src/gateway.js +2315 -0
  16. package/dist/src/image-server.d.ts +62 -0
  17. package/dist/src/image-server.js +401 -0
  18. package/dist/src/known-users.d.ts +100 -0
  19. package/dist/src/known-users.js +263 -0
  20. package/dist/src/onboarding.d.ts +10 -0
  21. package/dist/src/onboarding.js +203 -0
  22. package/dist/src/outbound.d.ts +150 -0
  23. package/dist/src/outbound.js +1175 -0
  24. package/dist/src/proactive.d.ts +170 -0
  25. package/dist/src/proactive.js +399 -0
  26. package/dist/src/runtime.d.ts +3 -0
  27. package/dist/src/runtime.js +10 -0
  28. package/dist/src/session-store.d.ts +52 -0
  29. package/dist/src/session-store.js +254 -0
  30. package/dist/src/types.d.ts +145 -0
  31. package/dist/src/types.js +1 -0
  32. package/dist/src/utils/audio-convert.d.ts +73 -0
  33. package/dist/src/utils/audio-convert.js +645 -0
  34. package/dist/src/utils/file-utils.d.ts +46 -0
  35. package/dist/src/utils/file-utils.js +107 -0
  36. package/dist/src/utils/image-size.d.ts +51 -0
  37. package/dist/src/utils/image-size.js +234 -0
  38. package/dist/src/utils/media-tags.d.ts +14 -0
  39. package/dist/src/utils/media-tags.js +120 -0
  40. package/dist/src/utils/payload.d.ts +112 -0
  41. package/dist/src/utils/payload.js +186 -0
  42. package/dist/src/utils/platform.d.ts +126 -0
  43. package/dist/src/utils/platform.js +358 -0
  44. package/dist/src/utils/upload-cache.d.ts +34 -0
  45. package/dist/src/utils/upload-cache.js +93 -0
  46. package/index.ts +27 -0
  47. package/moltbot.plugin.json +16 -0
  48. package/node_modules/@eshaz/web-worker/LICENSE +201 -0
  49. package/node_modules/@eshaz/web-worker/README.md +134 -0
  50. package/node_modules/@eshaz/web-worker/browser.js +17 -0
  51. package/node_modules/@eshaz/web-worker/cjs/browser.js +16 -0
  52. package/node_modules/@eshaz/web-worker/cjs/node.js +219 -0
  53. package/node_modules/@eshaz/web-worker/index.d.ts +4 -0
  54. package/node_modules/@eshaz/web-worker/node.js +223 -0
  55. package/node_modules/@eshaz/web-worker/package.json +54 -0
  56. package/node_modules/@wasm-audio-decoders/common/index.js +5 -0
  57. package/node_modules/@wasm-audio-decoders/common/package.json +36 -0
  58. package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderCommon.js +231 -0
  59. package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderWorker.js +129 -0
  60. package/node_modules/@wasm-audio-decoders/common/src/puff/README +67 -0
  61. package/node_modules/@wasm-audio-decoders/common/src/puff/build_puff.js +31 -0
  62. package/node_modules/@wasm-audio-decoders/common/src/puff/puff.c +863 -0
  63. package/node_modules/@wasm-audio-decoders/common/src/puff/puff.h +35 -0
  64. package/node_modules/@wasm-audio-decoders/common/src/utilities.js +3 -0
  65. package/node_modules/@wasm-audio-decoders/common/types.d.ts +7 -0
  66. package/node_modules/mpg123-decoder/README.md +265 -0
  67. package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js +185 -0
  68. package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js.map +1 -0
  69. package/node_modules/mpg123-decoder/index.js +8 -0
  70. package/node_modules/mpg123-decoder/package.json +58 -0
  71. package/node_modules/mpg123-decoder/src/EmscriptenWasm.js +464 -0
  72. package/node_modules/mpg123-decoder/src/MPEGDecoder.js +200 -0
  73. package/node_modules/mpg123-decoder/src/MPEGDecoderWebWorker.js +21 -0
  74. package/node_modules/mpg123-decoder/types.d.ts +30 -0
  75. package/node_modules/silk-wasm/LICENSE +21 -0
  76. package/node_modules/silk-wasm/README.md +85 -0
  77. package/node_modules/silk-wasm/lib/index.cjs +16 -0
  78. package/node_modules/silk-wasm/lib/index.d.ts +70 -0
  79. package/node_modules/silk-wasm/lib/index.mjs +16 -0
  80. package/node_modules/silk-wasm/lib/silk.wasm +0 -0
  81. package/node_modules/silk-wasm/lib/utils.d.ts +4 -0
  82. package/node_modules/silk-wasm/package.json +39 -0
  83. package/node_modules/simple-yenc/.github/FUNDING.yml +1 -0
  84. package/node_modules/simple-yenc/.prettierignore +1 -0
  85. package/node_modules/simple-yenc/LICENSE +7 -0
  86. package/node_modules/simple-yenc/README.md +163 -0
  87. package/node_modules/simple-yenc/dist/esm.js +1 -0
  88. package/node_modules/simple-yenc/dist/index.js +1 -0
  89. package/node_modules/simple-yenc/package.json +50 -0
  90. package/node_modules/simple-yenc/rollup.config.js +27 -0
  91. package/node_modules/simple-yenc/src/simple-yenc.js +302 -0
  92. package/node_modules/ws/LICENSE +20 -0
  93. package/node_modules/ws/README.md +548 -0
  94. package/node_modules/ws/browser.js +8 -0
  95. package/node_modules/ws/index.js +13 -0
  96. package/node_modules/ws/lib/buffer-util.js +131 -0
  97. package/node_modules/ws/lib/constants.js +19 -0
  98. package/node_modules/ws/lib/event-target.js +292 -0
  99. package/node_modules/ws/lib/extension.js +203 -0
  100. package/node_modules/ws/lib/limiter.js +55 -0
  101. package/node_modules/ws/lib/permessage-deflate.js +528 -0
  102. package/node_modules/ws/lib/receiver.js +706 -0
  103. package/node_modules/ws/lib/sender.js +602 -0
  104. package/node_modules/ws/lib/stream.js +161 -0
  105. package/node_modules/ws/lib/subprotocol.js +62 -0
  106. package/node_modules/ws/lib/validation.js +152 -0
  107. package/node_modules/ws/lib/websocket-server.js +554 -0
  108. package/node_modules/ws/lib/websocket.js +1393 -0
  109. package/node_modules/ws/package.json +69 -0
  110. package/node_modules/ws/wrapper.mjs +8 -0
  111. package/openclaw.plugin.json +16 -0
  112. package/package.json +76 -0
  113. package/scripts/proactive-api-server.ts +356 -0
  114. package/scripts/pull-latest.sh +316 -0
  115. package/scripts/send-proactive.ts +273 -0
  116. package/scripts/set-markdown.sh +156 -0
  117. package/scripts/upgrade-and-run.sh +525 -0
  118. package/scripts/upgrade.sh +127 -0
  119. package/skills/qqbot-cron/SKILL.md +513 -0
  120. package/skills/qqbot-media/SKILL.md +194 -0
  121. package/src/api.ts +704 -0
  122. package/src/channel.ts +368 -0
  123. package/src/config.ts +182 -0
  124. package/src/gateway.ts +2459 -0
  125. package/src/image-server.ts +474 -0
  126. package/src/known-users.ts +353 -0
  127. package/src/onboarding.ts +274 -0
  128. package/src/openclaw-plugin-sdk.d.ts +483 -0
  129. package/src/outbound.ts +1301 -0
  130. package/src/proactive.ts +530 -0
  131. package/src/runtime.ts +14 -0
  132. package/src/session-store.ts +303 -0
  133. package/src/types.ts +153 -0
  134. package/src/utils/audio-convert.ts +738 -0
  135. package/src/utils/file-utils.ts +122 -0
  136. package/src/utils/image-size.ts +266 -0
  137. package/src/utils/media-tags.ts +134 -0
  138. package/src/utils/payload.ts +265 -0
  139. package/src/utils/platform.ts +404 -0
  140. package/src/utils/upload-cache.ts +128 -0
  141. package/tsconfig.json +16 -0
@@ -0,0 +1,337 @@
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
+ /**
8
+ * 简单的文本分块函数
9
+ * 用于预先分块长文本
10
+ */
11
+ function chunkText(text, limit) {
12
+ if (text.length <= limit)
13
+ return [text];
14
+ const chunks = [];
15
+ let remaining = text;
16
+ while (remaining.length > 0) {
17
+ if (remaining.length <= limit) {
18
+ chunks.push(remaining);
19
+ break;
20
+ }
21
+ // 尝试在换行处分割
22
+ let splitAt = remaining.lastIndexOf("\n", limit);
23
+ if (splitAt <= 0 || splitAt < limit * 0.5) {
24
+ // 没找到合适的换行,尝试在空格处分割
25
+ splitAt = remaining.lastIndexOf(" ", limit);
26
+ }
27
+ if (splitAt <= 0 || splitAt < limit * 0.5) {
28
+ // 还是没找到,强制在 limit 处分割
29
+ splitAt = limit;
30
+ }
31
+ chunks.push(remaining.slice(0, splitAt));
32
+ remaining = remaining.slice(splitAt).trimStart();
33
+ }
34
+ return chunks;
35
+ }
36
+ export const qqbotPlugin = {
37
+ id: "qqbot",
38
+ meta: {
39
+ id: "qqbot",
40
+ label: "QQ Bot",
41
+ selectionLabel: "QQ Bot",
42
+ docsPath: "/docs/channels/qqbot",
43
+ blurb: "Connect to QQ via official QQ Bot API",
44
+ order: 50,
45
+ },
46
+ capabilities: {
47
+ chatTypes: ["direct", "group"],
48
+ media: true,
49
+ reactions: false,
50
+ threads: false,
51
+ /**
52
+ * blockStreaming: true 表示该 Channel 支持块流式
53
+ * 框架会收集流式响应,然后通过 deliver 回调发送
54
+ */
55
+ blockStreaming: false,
56
+ },
57
+ reload: { configPrefixes: ["channels.qqbot"] },
58
+ // CLI onboarding wizard
59
+ onboarding: qqbotOnboardingAdapter,
60
+ config: {
61
+ listAccountIds: (cfg) => listQQBotAccountIds(cfg),
62
+ resolveAccount: (cfg, accountId) => resolveQQBotAccount(cfg, accountId),
63
+ defaultAccountId: (cfg) => resolveDefaultQQBotAccountId(cfg),
64
+ // 新增:设置账户启用状态
65
+ setAccountEnabled: ({ cfg, accountId, enabled }) => setAccountEnabledInConfigSection({
66
+ cfg,
67
+ sectionKey: "qqbot",
68
+ accountId,
69
+ enabled,
70
+ allowTopLevel: true,
71
+ }),
72
+ // 新增:删除账户
73
+ deleteAccount: ({ cfg, accountId }) => deleteAccountFromConfigSection({
74
+ cfg,
75
+ sectionKey: "qqbot",
76
+ accountId,
77
+ clearBaseFields: ["appId", "clientSecret", "clientSecretFile", "name"],
78
+ }),
79
+ isConfigured: (account) => Boolean(account?.appId && account?.clientSecret),
80
+ describeAccount: (account) => ({
81
+ accountId: account?.accountId ?? DEFAULT_ACCOUNT_ID,
82
+ name: account?.name,
83
+ enabled: account?.enabled ?? false,
84
+ configured: Boolean(account?.appId && account?.clientSecret),
85
+ tokenSource: account?.secretSource,
86
+ }),
87
+ // 关键:解析 allowFrom 配置,用于命令授权
88
+ resolveAllowFrom: ({ cfg, accountId }) => {
89
+ const account = resolveQQBotAccount(cfg, accountId);
90
+ const allowFrom = account.config?.allowFrom ?? [];
91
+ console.log(`[qqbot] resolveAllowFrom: accountId=${accountId}, allowFrom=${JSON.stringify(allowFrom)}`);
92
+ return allowFrom.map((entry) => String(entry));
93
+ },
94
+ // 格式化 allowFrom 条目(移除 qqbot: 前缀,统一大写)
95
+ formatAllowFrom: ({ allowFrom }) => allowFrom
96
+ .map((entry) => String(entry).trim())
97
+ .filter(Boolean)
98
+ .map((entry) => entry.replace(/^qqbot:/i, ""))
99
+ .map((entry) => entry.toUpperCase()), // QQ openid 是大写的
100
+ },
101
+ setup: {
102
+ // 新增:规范化账户 ID
103
+ resolveAccountId: ({ accountId }) => accountId?.trim().toLowerCase() || DEFAULT_ACCOUNT_ID,
104
+ // 新增:应用账户名称
105
+ applyAccountName: ({ cfg, accountId, name }) => applyAccountNameToChannelSection({
106
+ cfg,
107
+ channelKey: "qqbot",
108
+ accountId,
109
+ name,
110
+ }),
111
+ validateInput: ({ input }) => {
112
+ if (!input.token && !input.tokenFile && !input.useEnv) {
113
+ return "QQBot requires --token (format: appId:clientSecret) or --use-env";
114
+ }
115
+ return null;
116
+ },
117
+ applyAccountConfig: ({ cfg, accountId, input }) => {
118
+ let appId = "";
119
+ let clientSecret = "";
120
+ if (input.token) {
121
+ const parts = input.token.split(":");
122
+ if (parts.length === 2) {
123
+ appId = parts[0];
124
+ clientSecret = parts[1];
125
+ }
126
+ }
127
+ return applyQQBotAccountConfig(cfg, accountId, {
128
+ appId,
129
+ clientSecret,
130
+ clientSecretFile: input.tokenFile,
131
+ name: input.name,
132
+ imageServerBaseUrl: input.imageServerBaseUrl,
133
+ });
134
+ },
135
+ },
136
+ // Messaging 配置:用于解析目标地址
137
+ messaging: {
138
+ /**
139
+ * 规范化目标地址
140
+ * 支持以下格式:
141
+ * - qqbot:c2c:openid -> 私聊
142
+ * - qqbot:group:groupid -> 群聊
143
+ * - qqbot:channel:channelid -> 频道
144
+ * - c2c:openid -> 私聊
145
+ * - group:groupid -> 群聊
146
+ * - channel:channelid -> 频道
147
+ * - 纯 openid(32位十六进制)-> 私聊
148
+ */
149
+ normalizeTarget: (target) => {
150
+ // 去掉 qqbot: 前缀(如果有)
151
+ const id = target.replace(/^qqbot:/i, "");
152
+ // 检查是否是已知格式
153
+ if (id.startsWith("c2c:") || id.startsWith("group:") || id.startsWith("channel:")) {
154
+ return { ok: true, to: `qqbot:${id}` };
155
+ }
156
+ // 检查是否是纯 openid(32位十六进制,不带连字符)
157
+ // QQ Bot OpenID 格式类似: 207A5B8339D01F6582911C014668B77B
158
+ const openIdHexPattern = /^[0-9a-fA-F]{32}$/;
159
+ if (openIdHexPattern.test(id)) {
160
+ return { ok: true, to: `qqbot:c2c:${id}` };
161
+ }
162
+ // 检查是否是 UUID 格式的 openid(带连字符)
163
+ 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}$/;
164
+ if (openIdUuidPattern.test(id)) {
165
+ return { ok: true, to: `qqbot:c2c:${id}` };
166
+ }
167
+ // 不认识的格式
168
+ return { ok: false, error: `unrecognized target format: ${target}` };
169
+ },
170
+ /**
171
+ * 目标解析器配置
172
+ * 用于判断一个目标 ID 是否看起来像 QQ Bot 的格式
173
+ */
174
+ targetResolver: {
175
+ /**
176
+ * 判断目标 ID 是否可能是 QQ Bot 格式
177
+ * 支持以下格式:
178
+ * - qqbot:c2c:xxx
179
+ * - qqbot:group:xxx
180
+ * - qqbot:channel:xxx
181
+ * - c2c:xxx
182
+ * - group:xxx
183
+ * - channel:xxx
184
+ * - UUID 格式的 openid
185
+ */
186
+ looksLikeId: (id) => {
187
+ // 带 qqbot: 前缀的格式
188
+ if (/^qqbot:(c2c|group|channel):/i.test(id)) {
189
+ return true;
190
+ }
191
+ // 不带前缀但有类型标识
192
+ if (/^(c2c|group|channel):/i.test(id)) {
193
+ return true;
194
+ }
195
+ // 32位十六进制 openid(不带连字符)
196
+ if (/^[0-9a-fA-F]{32}$/.test(id)) {
197
+ return true;
198
+ }
199
+ // UUID 格式的 openid(带连字符)
200
+ 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}$/;
201
+ return openIdPattern.test(id);
202
+ },
203
+ hint: "QQ Bot 目标格式: qqbot:c2c:openid (私聊) 或 qqbot:group:groupid (群聊)",
204
+ },
205
+ },
206
+ outbound: {
207
+ deliveryMode: "direct",
208
+ chunker: chunkText,
209
+ chunkerMode: "markdown",
210
+ textChunkLimit: 2000,
211
+ sendText: async ({ to, text, accountId, replyToId, cfg }) => {
212
+ console.log(`[qqbot:channel] sendText called — accountId=${accountId}, to=${to}, replyToId=${replyToId}, text.length=${text?.length ?? 0}`);
213
+ console.log(`[qqbot:channel] sendText text preview: ${text?.slice(0, 100)}${(text?.length ?? 0) > 100 ? "..." : ""}`);
214
+ const account = resolveQQBotAccount(cfg, accountId);
215
+ console.log(`[qqbot:channel] sendText resolved account: id=${account.accountId}, appId=${account.appId}, enabled=${account.enabled}`);
216
+ const result = await sendText({ to, text, accountId, replyToId, account });
217
+ console.log(`[qqbot:channel] sendText result: messageId=${result.messageId}, error=${result.error ?? "none"}`);
218
+ return {
219
+ channel: "qqbot",
220
+ messageId: result.messageId,
221
+ error: result.error ? new Error(result.error) : undefined,
222
+ };
223
+ },
224
+ sendMedia: async ({ to, text, mediaUrl, accountId, replyToId, cfg }) => {
225
+ console.log(`[qqbot:channel] sendMedia called — accountId=${accountId}, to=${to}, replyToId=${replyToId}, mediaUrl=${mediaUrl?.slice(0, 80)}, text.length=${text?.length ?? 0}`);
226
+ const account = resolveQQBotAccount(cfg, accountId);
227
+ console.log(`[qqbot:channel] sendMedia resolved account: id=${account.accountId}, appId=${account.appId}, enabled=${account.enabled}`);
228
+ const result = await sendMedia({ to, text: text ?? "", mediaUrl: mediaUrl ?? "", accountId, replyToId, account });
229
+ console.log(`[qqbot:channel] sendMedia result: messageId=${result.messageId}, error=${result.error ?? "none"}`);
230
+ return {
231
+ channel: "qqbot",
232
+ messageId: result.messageId,
233
+ error: result.error ? new Error(result.error) : undefined,
234
+ };
235
+ },
236
+ },
237
+ gateway: {
238
+ startAccount: async (ctx) => {
239
+ const { account, abortSignal, log, cfg } = ctx;
240
+ log?.info(`[qqbot:${account.accountId}] Starting gateway — appId=${account.appId}, enabled=${account.enabled}, name=${account.name ?? "unnamed"}`);
241
+ console.log(`[qqbot:channel] startAccount: accountId=${account.accountId}, appId=${account.appId}, secretSource=${account.secretSource}`);
242
+ await startGateway({
243
+ account,
244
+ abortSignal,
245
+ cfg,
246
+ log,
247
+ onReady: () => {
248
+ log?.info(`[qqbot:${account.accountId}] Gateway ready`);
249
+ ctx.setStatus({
250
+ ...ctx.getStatus(),
251
+ running: true,
252
+ connected: true,
253
+ lastConnectedAt: Date.now(),
254
+ });
255
+ },
256
+ onError: (error) => {
257
+ log?.error(`[qqbot:${account.accountId}] Gateway error: ${error.message}`);
258
+ ctx.setStatus({
259
+ ...ctx.getStatus(),
260
+ lastError: error.message,
261
+ });
262
+ },
263
+ });
264
+ },
265
+ // 新增:登出账户(清除配置中的凭证)
266
+ logoutAccount: async ({ accountId, cfg }) => {
267
+ const nextCfg = { ...cfg };
268
+ const nextQQBot = cfg.channels?.qqbot ? { ...cfg.channels.qqbot } : undefined;
269
+ let cleared = false;
270
+ let changed = false;
271
+ if (nextQQBot) {
272
+ const qqbot = nextQQBot;
273
+ if (accountId === DEFAULT_ACCOUNT_ID && qqbot.clientSecret) {
274
+ delete qqbot.clientSecret;
275
+ cleared = true;
276
+ changed = true;
277
+ }
278
+ const accounts = qqbot.accounts;
279
+ if (accounts && accountId in accounts) {
280
+ const entry = accounts[accountId];
281
+ if (entry && "clientSecret" in entry) {
282
+ delete entry.clientSecret;
283
+ cleared = true;
284
+ changed = true;
285
+ }
286
+ if (entry && Object.keys(entry).length === 0) {
287
+ delete accounts[accountId];
288
+ changed = true;
289
+ }
290
+ }
291
+ }
292
+ if (changed && nextQQBot) {
293
+ nextCfg.channels = { ...nextCfg.channels, qqbot: nextQQBot };
294
+ const runtime = getQQBotRuntime();
295
+ const configApi = runtime.config;
296
+ await configApi.writeConfigFile(nextCfg);
297
+ }
298
+ const resolved = resolveQQBotAccount(changed ? nextCfg : cfg, accountId);
299
+ const loggedOut = resolved.secretSource === "none";
300
+ const envToken = Boolean(process.env.QQBOT_CLIENT_SECRET);
301
+ return { ok: true, cleared, envToken, loggedOut };
302
+ },
303
+ },
304
+ status: {
305
+ defaultRuntime: {
306
+ accountId: DEFAULT_ACCOUNT_ID,
307
+ running: false,
308
+ connected: false,
309
+ lastConnectedAt: null,
310
+ lastError: null,
311
+ lastInboundAt: null,
312
+ lastOutboundAt: null,
313
+ },
314
+ // 新增:构建通道摘要
315
+ buildChannelSummary: ({ snapshot }) => ({
316
+ configured: snapshot.configured ?? false,
317
+ tokenSource: snapshot.tokenSource ?? "none",
318
+ running: snapshot.running ?? false,
319
+ connected: snapshot.connected ?? false,
320
+ lastConnectedAt: snapshot.lastConnectedAt ?? null,
321
+ lastError: snapshot.lastError ?? null,
322
+ }),
323
+ buildAccountSnapshot: ({ account, runtime }) => ({
324
+ accountId: account?.accountId ?? DEFAULT_ACCOUNT_ID,
325
+ name: account?.name,
326
+ enabled: account?.enabled ?? false,
327
+ configured: Boolean(account?.appId && account?.clientSecret),
328
+ tokenSource: account?.secretSource,
329
+ running: runtime?.running ?? false,
330
+ connected: runtime?.connected ?? false,
331
+ lastConnectedAt: runtime?.lastConnectedAt ?? null,
332
+ lastError: runtime?.lastError ?? null,
333
+ lastInboundAt: runtime?.lastInboundAt ?? null,
334
+ lastOutboundAt: runtime?.lastOutboundAt ?? null,
335
+ }),
336
+ },
337
+ };
@@ -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,156 @@
1
+ export const DEFAULT_ACCOUNT_ID = "default";
2
+ /**
3
+ * 列出所有 QQBot 账户 ID
4
+ */
5
+ export function listQQBotAccountIds(cfg) {
6
+ const ids = new Set();
7
+ const qqbot = cfg.channels?.qqbot;
8
+ if (qqbot?.appId) {
9
+ ids.add(DEFAULT_ACCOUNT_ID);
10
+ }
11
+ if (qqbot?.accounts) {
12
+ for (const accountId of Object.keys(qqbot.accounts)) {
13
+ if (qqbot.accounts[accountId]?.appId) {
14
+ ids.add(accountId);
15
+ }
16
+ }
17
+ }
18
+ return Array.from(ids);
19
+ }
20
+ /**
21
+ * 获取默认账户 ID
22
+ */
23
+ export function resolveDefaultQQBotAccountId(cfg) {
24
+ const qqbot = cfg.channels?.qqbot;
25
+ // 如果有默认账户配置,返回 default
26
+ if (qqbot?.appId) {
27
+ return DEFAULT_ACCOUNT_ID;
28
+ }
29
+ // 否则返回第一个配置的账户
30
+ if (qqbot?.accounts) {
31
+ const ids = Object.keys(qqbot.accounts);
32
+ if (ids.length > 0) {
33
+ return ids[0];
34
+ }
35
+ }
36
+ return DEFAULT_ACCOUNT_ID;
37
+ }
38
+ /**
39
+ * 解析 QQBot 账户配置
40
+ */
41
+ export function resolveQQBotAccount(cfg, accountId) {
42
+ const resolvedAccountId = accountId ?? DEFAULT_ACCOUNT_ID;
43
+ const qqbot = cfg.channels?.qqbot;
44
+ // 基础配置
45
+ let accountConfig = {};
46
+ let appId = "";
47
+ let clientSecret = "";
48
+ let secretSource = "none";
49
+ if (resolvedAccountId === DEFAULT_ACCOUNT_ID) {
50
+ // 默认账户从顶层读取
51
+ accountConfig = {
52
+ enabled: qqbot?.enabled,
53
+ name: qqbot?.name,
54
+ appId: qqbot?.appId,
55
+ clientSecret: qqbot?.clientSecret,
56
+ clientSecretFile: qqbot?.clientSecretFile,
57
+ dmPolicy: qqbot?.dmPolicy,
58
+ allowFrom: qqbot?.allowFrom,
59
+ systemPrompt: qqbot?.systemPrompt,
60
+ imageServerBaseUrl: qqbot?.imageServerBaseUrl,
61
+ markdownSupport: qqbot?.markdownSupport ?? true,
62
+ };
63
+ appId = qqbot?.appId ?? "";
64
+ }
65
+ else {
66
+ // 命名账户从 accounts 读取
67
+ const account = qqbot?.accounts?.[resolvedAccountId];
68
+ accountConfig = account ?? {};
69
+ appId = account?.appId ?? "";
70
+ }
71
+ // 解析 clientSecret
72
+ if (accountConfig.clientSecret) {
73
+ clientSecret = accountConfig.clientSecret;
74
+ secretSource = "config";
75
+ }
76
+ else if (accountConfig.clientSecretFile) {
77
+ // 从文件读取(运行时处理)
78
+ secretSource = "file";
79
+ }
80
+ else if (process.env.QQBOT_CLIENT_SECRET && resolvedAccountId === DEFAULT_ACCOUNT_ID) {
81
+ clientSecret = process.env.QQBOT_CLIENT_SECRET;
82
+ secretSource = "env";
83
+ }
84
+ // AppId 也可以从环境变量读取
85
+ if (!appId && process.env.QQBOT_APP_ID && resolvedAccountId === DEFAULT_ACCOUNT_ID) {
86
+ appId = process.env.QQBOT_APP_ID;
87
+ }
88
+ return {
89
+ accountId: resolvedAccountId,
90
+ name: accountConfig.name,
91
+ enabled: accountConfig.enabled !== false,
92
+ appId,
93
+ clientSecret,
94
+ secretSource,
95
+ systemPrompt: accountConfig.systemPrompt,
96
+ imageServerBaseUrl: accountConfig.imageServerBaseUrl || process.env.QQBOT_IMAGE_SERVER_BASE_URL,
97
+ markdownSupport: accountConfig.markdownSupport !== false,
98
+ config: accountConfig,
99
+ };
100
+ }
101
+ /**
102
+ * 应用账户配置
103
+ */
104
+ export function applyQQBotAccountConfig(cfg, accountId, input) {
105
+ const next = { ...cfg };
106
+ if (accountId === DEFAULT_ACCOUNT_ID) {
107
+ // 如果没有设置过 allowFrom,默认设置为 ["*"]
108
+ const existingConfig = next.channels?.qqbot || {};
109
+ const allowFrom = existingConfig.allowFrom ?? ["*"];
110
+ next.channels = {
111
+ ...next.channels,
112
+ qqbot: {
113
+ ...(next.channels?.qqbot || {}),
114
+ enabled: true,
115
+ allowFrom,
116
+ ...(input.appId ? { appId: input.appId } : {}),
117
+ ...(input.clientSecret
118
+ ? { clientSecret: input.clientSecret }
119
+ : input.clientSecretFile
120
+ ? { clientSecretFile: input.clientSecretFile }
121
+ : {}),
122
+ ...(input.name ? { name: input.name } : {}),
123
+ ...(input.imageServerBaseUrl ? { imageServerBaseUrl: input.imageServerBaseUrl } : {}),
124
+ },
125
+ };
126
+ }
127
+ else {
128
+ // 如果没有设置过 allowFrom,默认设置为 ["*"]
129
+ const existingAccountConfig = next.channels?.qqbot?.accounts?.[accountId] || {};
130
+ const allowFrom = existingAccountConfig.allowFrom ?? ["*"];
131
+ next.channels = {
132
+ ...next.channels,
133
+ qqbot: {
134
+ ...(next.channels?.qqbot || {}),
135
+ enabled: true,
136
+ accounts: {
137
+ ...(next.channels?.qqbot?.accounts || {}),
138
+ [accountId]: {
139
+ ...(next.channels?.qqbot?.accounts?.[accountId] || {}),
140
+ enabled: true,
141
+ allowFrom,
142
+ ...(input.appId ? { appId: input.appId } : {}),
143
+ ...(input.clientSecret
144
+ ? { clientSecret: input.clientSecret }
145
+ : input.clientSecretFile
146
+ ? { clientSecretFile: input.clientSecretFile }
147
+ : {}),
148
+ ...(input.name ? { name: input.name } : {}),
149
+ ...(input.imageServerBaseUrl ? { imageServerBaseUrl: input.imageServerBaseUrl } : {}),
150
+ },
151
+ },
152
+ },
153
+ };
154
+ }
155
+ return next;
156
+ }
@@ -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>;