@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.
Files changed (197) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +483 -0
  3. package/README.zh.md +478 -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 +26 -0
  8. package/dist/src/admin-resolver.d.ts +27 -0
  9. package/dist/src/admin-resolver.js +122 -0
  10. package/dist/src/api.d.ts +156 -0
  11. package/dist/src/api.js +599 -0
  12. package/dist/src/channel.d.ts +11 -0
  13. package/dist/src/channel.js +354 -0
  14. package/dist/src/config.d.ts +25 -0
  15. package/dist/src/config.js +161 -0
  16. package/dist/src/credential-backup.d.ts +31 -0
  17. package/dist/src/credential-backup.js +66 -0
  18. package/dist/src/gateway.d.ts +18 -0
  19. package/dist/src/gateway.js +1265 -0
  20. package/dist/src/image-server.d.ts +68 -0
  21. package/dist/src/image-server.js +462 -0
  22. package/dist/src/inbound-attachments.d.ts +58 -0
  23. package/dist/src/inbound-attachments.js +234 -0
  24. package/dist/src/known-users.d.ts +100 -0
  25. package/dist/src/known-users.js +263 -0
  26. package/dist/src/message-queue.d.ts +50 -0
  27. package/dist/src/message-queue.js +115 -0
  28. package/dist/src/onboarding.d.ts +10 -0
  29. package/dist/src/onboarding.js +203 -0
  30. package/dist/src/outbound-deliver.d.ts +48 -0
  31. package/dist/src/outbound-deliver.js +462 -0
  32. package/dist/src/outbound.d.ts +203 -0
  33. package/dist/src/outbound.js +1102 -0
  34. package/dist/src/proactive.d.ts +170 -0
  35. package/dist/src/proactive.js +399 -0
  36. package/dist/src/ref-index-store.d.ts +70 -0
  37. package/dist/src/ref-index-store.js +273 -0
  38. package/dist/src/reply-dispatcher.d.ts +35 -0
  39. package/dist/src/reply-dispatcher.js +311 -0
  40. package/dist/src/runtime.d.ts +3 -0
  41. package/dist/src/runtime.js +10 -0
  42. package/dist/src/session-store.d.ts +52 -0
  43. package/dist/src/session-store.js +254 -0
  44. package/dist/src/slash-commands.d.ts +71 -0
  45. package/dist/src/slash-commands.js +1179 -0
  46. package/dist/src/startup-greeting.d.ts +30 -0
  47. package/dist/src/startup-greeting.js +78 -0
  48. package/dist/src/stt.d.ts +21 -0
  49. package/dist/src/stt.js +70 -0
  50. package/dist/src/tools/channel.d.ts +16 -0
  51. package/dist/src/tools/channel.js +234 -0
  52. package/dist/src/tools/remind.d.ts +2 -0
  53. package/dist/src/tools/remind.js +247 -0
  54. package/dist/src/types.d.ts +175 -0
  55. package/dist/src/types.js +1 -0
  56. package/dist/src/typing-keepalive.d.ts +27 -0
  57. package/dist/src/typing-keepalive.js +64 -0
  58. package/dist/src/update-checker.d.ts +34 -0
  59. package/dist/src/update-checker.js +166 -0
  60. package/dist/src/user-messages.d.ts +8 -0
  61. package/dist/src/user-messages.js +8 -0
  62. package/dist/src/utils/audio-convert.d.ts +89 -0
  63. package/dist/src/utils/audio-convert.js +704 -0
  64. package/dist/src/utils/file-utils.d.ts +55 -0
  65. package/dist/src/utils/file-utils.js +150 -0
  66. package/dist/src/utils/image-size.d.ts +51 -0
  67. package/dist/src/utils/image-size.js +234 -0
  68. package/dist/src/utils/media-tags.d.ts +14 -0
  69. package/dist/src/utils/media-tags.js +164 -0
  70. package/dist/src/utils/payload.d.ts +112 -0
  71. package/dist/src/utils/payload.js +186 -0
  72. package/dist/src/utils/platform.d.ts +137 -0
  73. package/dist/src/utils/platform.js +390 -0
  74. package/dist/src/utils/text-parsing.d.ts +32 -0
  75. package/dist/src/utils/text-parsing.js +80 -0
  76. package/dist/src/utils/upload-cache.d.ts +34 -0
  77. package/dist/src/utils/upload-cache.js +93 -0
  78. package/index.ts +31 -0
  79. package/moltbot.plugin.json +16 -0
  80. package/node_modules/@eshaz/web-worker/LICENSE +201 -0
  81. package/node_modules/@eshaz/web-worker/README.md +134 -0
  82. package/node_modules/@eshaz/web-worker/browser.js +17 -0
  83. package/node_modules/@eshaz/web-worker/cjs/browser.js +16 -0
  84. package/node_modules/@eshaz/web-worker/cjs/node.js +219 -0
  85. package/node_modules/@eshaz/web-worker/index.d.ts +4 -0
  86. package/node_modules/@eshaz/web-worker/node.js +223 -0
  87. package/node_modules/@eshaz/web-worker/package.json +54 -0
  88. package/node_modules/@wasm-audio-decoders/common/index.js +5 -0
  89. package/node_modules/@wasm-audio-decoders/common/package.json +36 -0
  90. package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderCommon.js +231 -0
  91. package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderWorker.js +129 -0
  92. package/node_modules/@wasm-audio-decoders/common/src/puff/README +67 -0
  93. package/node_modules/@wasm-audio-decoders/common/src/puff/build_puff.js +31 -0
  94. package/node_modules/@wasm-audio-decoders/common/src/puff/puff.c +863 -0
  95. package/node_modules/@wasm-audio-decoders/common/src/puff/puff.h +35 -0
  96. package/node_modules/@wasm-audio-decoders/common/src/utilities.js +3 -0
  97. package/node_modules/@wasm-audio-decoders/common/types.d.ts +7 -0
  98. package/node_modules/mpg123-decoder/README.md +265 -0
  99. package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js +185 -0
  100. package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js.map +1 -0
  101. package/node_modules/mpg123-decoder/index.js +8 -0
  102. package/node_modules/mpg123-decoder/package.json +58 -0
  103. package/node_modules/mpg123-decoder/src/EmscriptenWasm.js +464 -0
  104. package/node_modules/mpg123-decoder/src/MPEGDecoder.js +200 -0
  105. package/node_modules/mpg123-decoder/src/MPEGDecoderWebWorker.js +21 -0
  106. package/node_modules/mpg123-decoder/types.d.ts +30 -0
  107. package/node_modules/silk-wasm/LICENSE +21 -0
  108. package/node_modules/silk-wasm/README.md +85 -0
  109. package/node_modules/silk-wasm/lib/index.cjs +16 -0
  110. package/node_modules/silk-wasm/lib/index.d.ts +70 -0
  111. package/node_modules/silk-wasm/lib/index.mjs +16 -0
  112. package/node_modules/silk-wasm/lib/silk.wasm +0 -0
  113. package/node_modules/silk-wasm/lib/utils.d.ts +4 -0
  114. package/node_modules/silk-wasm/package.json +39 -0
  115. package/node_modules/simple-yenc/.github/FUNDING.yml +1 -0
  116. package/node_modules/simple-yenc/.prettierignore +1 -0
  117. package/node_modules/simple-yenc/LICENSE +7 -0
  118. package/node_modules/simple-yenc/README.md +163 -0
  119. package/node_modules/simple-yenc/dist/esm.js +1 -0
  120. package/node_modules/simple-yenc/dist/index.js +1 -0
  121. package/node_modules/simple-yenc/package.json +50 -0
  122. package/node_modules/simple-yenc/rollup.config.js +27 -0
  123. package/node_modules/simple-yenc/src/simple-yenc.js +302 -0
  124. package/node_modules/ws/LICENSE +20 -0
  125. package/node_modules/ws/README.md +548 -0
  126. package/node_modules/ws/browser.js +8 -0
  127. package/node_modules/ws/index.js +13 -0
  128. package/node_modules/ws/lib/buffer-util.js +131 -0
  129. package/node_modules/ws/lib/constants.js +19 -0
  130. package/node_modules/ws/lib/event-target.js +292 -0
  131. package/node_modules/ws/lib/extension.js +203 -0
  132. package/node_modules/ws/lib/limiter.js +55 -0
  133. package/node_modules/ws/lib/permessage-deflate.js +528 -0
  134. package/node_modules/ws/lib/receiver.js +706 -0
  135. package/node_modules/ws/lib/sender.js +602 -0
  136. package/node_modules/ws/lib/stream.js +161 -0
  137. package/node_modules/ws/lib/subprotocol.js +62 -0
  138. package/node_modules/ws/lib/validation.js +152 -0
  139. package/node_modules/ws/lib/websocket-server.js +554 -0
  140. package/node_modules/ws/lib/websocket.js +1393 -0
  141. package/node_modules/ws/package.json +69 -0
  142. package/node_modules/ws/wrapper.mjs +8 -0
  143. package/openclaw.plugin.json +16 -0
  144. package/package.json +76 -0
  145. package/scripts/cleanup-legacy-plugins.sh +124 -0
  146. package/scripts/proactive-api-server.ts +369 -0
  147. package/scripts/send-proactive.ts +293 -0
  148. package/scripts/set-markdown.sh +156 -0
  149. package/scripts/test-sendmedia.ts +116 -0
  150. package/scripts/upgrade-via-alt-pkg.sh +307 -0
  151. package/scripts/upgrade-via-npm.ps1 +296 -0
  152. package/scripts/upgrade-via-npm.sh +301 -0
  153. package/scripts/upgrade-via-source.sh +774 -0
  154. package/skills/qqbot-channel/SKILL.md +263 -0
  155. package/skills/qqbot-channel/references/api_references.md +521 -0
  156. package/skills/qqbot-media/SKILL.md +56 -0
  157. package/skills/qqbot-remind/SKILL.md +149 -0
  158. package/src/admin-resolver.ts +140 -0
  159. package/src/api.ts +819 -0
  160. package/src/bot-logs-2026-03-21T11-21-47(2).txt +46 -0
  161. package/src/channel.ts +381 -0
  162. package/src/config.ts +187 -0
  163. package/src/credential-backup.ts +72 -0
  164. package/src/gateway.log +43 -0
  165. package/src/gateway.ts +1404 -0
  166. package/src/image-server.ts +539 -0
  167. package/src/inbound-attachments.ts +304 -0
  168. package/src/known-users.ts +353 -0
  169. package/src/message-queue.ts +169 -0
  170. package/src/onboarding.ts +274 -0
  171. package/src/openclaw-2026-03-21.log +3729 -0
  172. package/src/openclaw-plugin-sdk.d.ts +522 -0
  173. package/src/outbound-deliver.ts +552 -0
  174. package/src/outbound.ts +1266 -0
  175. package/src/proactive.ts +530 -0
  176. package/src/ref-index-store.ts +357 -0
  177. package/src/reply-dispatcher.ts +334 -0
  178. package/src/runtime.ts +14 -0
  179. package/src/session-store.ts +303 -0
  180. package/src/slash-commands.ts +1305 -0
  181. package/src/startup-greeting.ts +98 -0
  182. package/src/stt.ts +86 -0
  183. package/src/tools/channel.ts +281 -0
  184. package/src/tools/remind.ts +296 -0
  185. package/src/types.ts +183 -0
  186. package/src/typing-keepalive.ts +59 -0
  187. package/src/update-checker.ts +179 -0
  188. package/src/user-messages.ts +7 -0
  189. package/src/utils/audio-convert.ts +803 -0
  190. package/src/utils/file-utils.ts +167 -0
  191. package/src/utils/image-size.ts +266 -0
  192. package/src/utils/media-tags.ts +182 -0
  193. package/src/utils/payload.ts +265 -0
  194. package/src/utils/platform.ts +435 -0
  195. package/src/utils/text-parsing.ts +82 -0
  196. package/src/utils/upload-cache.ts +128 -0
  197. package/tsconfig.json +16 -0
@@ -0,0 +1,98 @@
1
+ /**
2
+ * 启动问候语系统:首次安装/版本更新 vs 普通重启
3
+ */
4
+
5
+ import * as fs from "node:fs";
6
+ import path from "node:path";
7
+ import { getQQBotDataDir } from "./utils/platform.js";
8
+ import { getPluginVersion } from "./slash-commands.js";
9
+
10
+ const STARTUP_MARKER_FILE = path.join(getQQBotDataDir("data"), "startup-marker.json");
11
+ const STARTUP_GREETING_RETRY_COOLDOWN_MS = 10 * 60 * 1000;
12
+
13
+ export function getFirstLaunchGreetingText(): string {
14
+ return `Haha,我的'灵魂'已上线,随时等你吩咐。`;
15
+ }
16
+
17
+ export function getUpgradeGreetingText(version: string): string {
18
+ return `🎉 QQBot 插件已更新至 v${version},在线等候你的吩咐。`;
19
+ }
20
+
21
+ export type StartupMarkerData = {
22
+ version?: string;
23
+ startedAt?: string;
24
+ greetedAt?: string;
25
+ lastFailureAt?: string;
26
+ lastFailureReason?: string;
27
+ lastFailureVersion?: string;
28
+ };
29
+
30
+ export function readStartupMarker(): StartupMarkerData {
31
+ try {
32
+ if (fs.existsSync(STARTUP_MARKER_FILE)) {
33
+ const data = JSON.parse(fs.readFileSync(STARTUP_MARKER_FILE, "utf8")) as StartupMarkerData;
34
+ return data || {};
35
+ }
36
+ } catch {
37
+ // 文件损坏或不存在,视为无 marker
38
+ }
39
+ return {};
40
+ }
41
+
42
+ export function writeStartupMarker(data: StartupMarkerData): void {
43
+ try {
44
+ fs.writeFileSync(STARTUP_MARKER_FILE, JSON.stringify(data) + "\n");
45
+ } catch {
46
+ // ignore
47
+ }
48
+ }
49
+
50
+ /**
51
+ * 判断是否需要发送启动问候:
52
+ * - 首次启动(无 marker)→ "灵魂已上线"
53
+ * - 版本变更 → "已更新至 vX.Y.Z"
54
+ * - 同版本 → 不发送
55
+ * - 同版本近期失败 → 冷却期内不重试
56
+ */
57
+ export function getStartupGreetingPlan(): { shouldSend: boolean; greeting?: string; version: string; reason?: string } {
58
+ const currentVersion = getPluginVersion();
59
+ const marker = readStartupMarker();
60
+
61
+ if (marker.version === currentVersion) {
62
+ return { shouldSend: false, version: currentVersion, reason: "same-version" };
63
+ }
64
+
65
+ if (marker.lastFailureVersion === currentVersion && marker.lastFailureAt) {
66
+ const lastFailureAtMs = new Date(marker.lastFailureAt).getTime();
67
+ if (!Number.isNaN(lastFailureAtMs) && Date.now() - lastFailureAtMs < STARTUP_GREETING_RETRY_COOLDOWN_MS) {
68
+ return { shouldSend: false, version: currentVersion, reason: "cooldown" };
69
+ }
70
+ }
71
+
72
+ const isFirstLaunch = !marker.version;
73
+ const greeting = isFirstLaunch
74
+ ? getFirstLaunchGreetingText()
75
+ : getUpgradeGreetingText(currentVersion);
76
+
77
+ return { shouldSend: true, greeting, version: currentVersion };
78
+ }
79
+
80
+ export function markStartupGreetingSent(version: string): void {
81
+ writeStartupMarker({
82
+ version,
83
+ startedAt: new Date().toISOString(),
84
+ greetedAt: new Date().toISOString(),
85
+ });
86
+ }
87
+
88
+ export function markStartupGreetingFailed(version: string, reason: string): void {
89
+ const marker = readStartupMarker();
90
+ // 同版本已有失败记录时,不覆盖 lastFailureAt,避免冷却期被无限续期
91
+ const shouldPreserveTimestamp = marker.lastFailureVersion === version && marker.lastFailureAt;
92
+ writeStartupMarker({
93
+ ...marker,
94
+ lastFailureVersion: version,
95
+ lastFailureAt: shouldPreserveTimestamp ? marker.lastFailureAt! : new Date().toISOString(),
96
+ lastFailureReason: reason,
97
+ });
98
+ }
package/src/stt.ts ADDED
@@ -0,0 +1,86 @@
1
+ /**
2
+ * 通用 OpenAI 兼容 STT(语音转文字)
3
+ *
4
+ * 为什么在插件侧做 STT 而不走框架管道?
5
+ * 框架的 applyMediaUnderstanding 同时执行 runCapability("audio") 和 extractFileBlocks。
6
+ * 后者会把 WAV 文件的 PCM 二进制当文本注入 Body(looksLikeUtf8Text 误判),导致 context 爆炸。
7
+ * 在插件侧完成 STT 后不把 WAV 放入 MediaPaths,即可规避此框架 bug。
8
+ *
9
+ * 配置解析策略(与 TTS 统一的两级回退):
10
+ * 1. 优先 channels.qqbot.stt(插件专属配置)
11
+ * 2. 回退 tools.media.audio.models[0](框架级配置)
12
+ * 3. 再从 models.providers.[provider] 继承 apiKey/baseUrl
13
+ * 4. 支持任何 OpenAI 兼容的 STT 服务
14
+ */
15
+
16
+ import * as fs from "node:fs";
17
+ import path from "node:path";
18
+ import { sanitizeFileName } from "./utils/platform.js";
19
+
20
+ export interface STTConfig {
21
+ baseUrl: string;
22
+ apiKey: string;
23
+ model: string;
24
+ }
25
+
26
+ export function resolveSTTConfig(cfg: Record<string, unknown>): STTConfig | null {
27
+ const c = cfg as any;
28
+
29
+ // 优先使用 channels.qqbot.stt(插件专属配置)
30
+ const channelStt = c?.channels?.qqbot?.stt;
31
+ if (channelStt && channelStt.enabled !== false) {
32
+ const providerId: string = channelStt?.provider || "openai";
33
+ const providerCfg = c?.models?.providers?.[providerId];
34
+ const baseUrl: string | undefined = channelStt?.baseUrl || providerCfg?.baseUrl;
35
+ const apiKey: string | undefined = channelStt?.apiKey || providerCfg?.apiKey;
36
+ const model: string = channelStt?.model || "whisper-1";
37
+ if (baseUrl && apiKey) {
38
+ return { baseUrl: baseUrl.replace(/\/+$/, ""), apiKey, model };
39
+ }
40
+ }
41
+
42
+ // 回退到 tools.media.audio.models[0](框架级配置)
43
+ const audioModelEntry = c?.tools?.media?.audio?.models?.[0];
44
+ if (audioModelEntry) {
45
+ const providerId: string = audioModelEntry?.provider || "openai";
46
+ const providerCfg = c?.models?.providers?.[providerId];
47
+ const baseUrl: string | undefined = audioModelEntry?.baseUrl || providerCfg?.baseUrl;
48
+ const apiKey: string | undefined = audioModelEntry?.apiKey || providerCfg?.apiKey;
49
+ const model: string = audioModelEntry?.model || "whisper-1";
50
+ if (baseUrl && apiKey) {
51
+ return { baseUrl: baseUrl.replace(/\/+$/, ""), apiKey, model };
52
+ }
53
+ }
54
+
55
+ return null;
56
+ }
57
+
58
+ export async function transcribeAudio(audioPath: string, cfg: Record<string, unknown>): Promise<string | null> {
59
+ const sttCfg = resolveSTTConfig(cfg);
60
+ if (!sttCfg) return null;
61
+
62
+ const fileBuffer = fs.readFileSync(audioPath);
63
+ const fileName = sanitizeFileName(path.basename(audioPath));
64
+ const mime = fileName.endsWith(".wav") ? "audio/wav"
65
+ : fileName.endsWith(".mp3") ? "audio/mpeg"
66
+ : fileName.endsWith(".ogg") ? "audio/ogg"
67
+ : "application/octet-stream";
68
+
69
+ const form = new FormData();
70
+ form.append("file", new Blob([fileBuffer], { type: mime }), fileName);
71
+ form.append("model", sttCfg.model);
72
+
73
+ const resp = await fetch(`${sttCfg.baseUrl}/audio/transcriptions`, {
74
+ method: "POST",
75
+ headers: { "Authorization": `Bearer ${sttCfg.apiKey}` },
76
+ body: form,
77
+ });
78
+
79
+ if (!resp.ok) {
80
+ const detail = await resp.text().catch(() => "");
81
+ throw new Error(`STT failed (HTTP ${resp.status}): ${detail.slice(0, 300)}`);
82
+ }
83
+
84
+ const result = await resp.json() as { text?: string };
85
+ return result.text?.trim() || null;
86
+ }
@@ -0,0 +1,281 @@
1
+ import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
2
+ import { resolveQQBotAccount } from "../config.js";
3
+ import { listQQBotAccountIds } from "../config.js";
4
+ import { getAccessToken } from "../api.js";
5
+
6
+ // ========== 常量 ==========
7
+
8
+ const API_BASE = "https://api.sgroup.qq.com";
9
+
10
+ const DEFAULT_TIMEOUT_MS = 30000;
11
+
12
+ // ========== 类型定义 ==========
13
+
14
+ interface ChannelApiParams {
15
+ method: string;
16
+ path: string;
17
+ body?: Record<string, unknown>;
18
+ query?: Record<string, string>;
19
+ }
20
+
21
+ // ========== JSON Schema ==========
22
+
23
+ const ChannelApiSchema = {
24
+ type: "object",
25
+ properties: {
26
+ method: {
27
+ type: "string",
28
+ description:
29
+ "HTTP 请求方法。可选值:GET, POST, PUT, PATCH, DELETE",
30
+ enum: ["GET", "POST", "PUT", "PATCH", "DELETE"],
31
+ },
32
+ path: {
33
+ type: "string",
34
+ description:
35
+ "API 路径(不含域名),占位符需替换为实际值。" +
36
+ "示例:/users/@me/guilds, /guilds/{guild_id}/channels, /channels/{channel_id}",
37
+ },
38
+ body: {
39
+ type: "object",
40
+ description:
41
+ "请求体(JSON),用于 POST/PUT/PATCH 请求。" +
42
+ "GET/DELETE 请求不需要此参数。",
43
+ },
44
+ query: {
45
+ type: "object",
46
+ description:
47
+ "URL 查询参数(键值对),会拼接到路径后面。" +
48
+ "如 { \"limit\": \"100\", \"after\": \"0\" } 会拼接为 ?limit=100&after=0",
49
+ additionalProperties: { type: "string" },
50
+ },
51
+ },
52
+ required: ["method", "path"],
53
+ } as const;
54
+
55
+ // ========== 工具函数 ==========
56
+
57
+ function json(data: unknown) {
58
+ return {
59
+ content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }],
60
+ details: data,
61
+ };
62
+ }
63
+
64
+ /**
65
+ * 构建完整的请求 URL
66
+ */
67
+ function buildUrl(path: string, query?: Record<string, string>): string {
68
+ let url = `${API_BASE}${path}`;
69
+ if (query && Object.keys(query).length > 0) {
70
+ const params = new URLSearchParams();
71
+ for (const [key, value] of Object.entries(query)) {
72
+ if (value !== undefined && value !== null && value !== "") {
73
+ params.set(key, value);
74
+ }
75
+ }
76
+ const qs = params.toString();
77
+ if (qs) {
78
+ url += `?${qs}`;
79
+ }
80
+ }
81
+ return url;
82
+ }
83
+
84
+ /**
85
+ * 校验 path 防止 SSRF
86
+ */
87
+ function validatePath(path: string): string | null {
88
+ if (!path.startsWith("/")) {
89
+ return "path 必须以 / 开头";
90
+ }
91
+ if (path.includes("..") || path.includes("//")) {
92
+ return "path 不允许包含 .. 或 //";
93
+ }
94
+ // 只允许合法的 URL path 字符
95
+ if (!/^\/[a-zA-Z0-9\-._~:@!$&'()*+,;=/%]+$/.test(path) && path !== "/") {
96
+ return "path 包含非法字符";
97
+ }
98
+ return null;
99
+ }
100
+
101
+ // ========== 注册入口 ==========
102
+
103
+ /**
104
+ * 注册 QQ 频道 API 代理工具。
105
+ *
106
+ * 该工具作为 QQ 开放平台频道 API 的 HTTP 代理,自动处理 Token 鉴权。
107
+ * AI 通过 skill 文档了解各接口的路径、方法和参数,构造请求后由此工具代理发送。
108
+ *
109
+ * 支持的能力:
110
+ * - 频道管理(Guild)
111
+ * - 子频道管理(Channel)
112
+ * - 成员管理(Member)
113
+ * - 公告管理(Announces)
114
+ * - 论坛管理(Forum Thread)
115
+ * - 日程管理(Schedule)
116
+ */
117
+ export function registerChannelTool(api: OpenClawPluginApi): void {
118
+ const cfg = api.config;
119
+ if (!cfg) {
120
+ console.log("[qqbot-channel-api] No config available, skipping");
121
+ return;
122
+ }
123
+
124
+ const accountIds = listQQBotAccountIds(cfg);
125
+ if (accountIds.length === 0) {
126
+ console.log("[qqbot-channel-api] No QQBot accounts configured, skipping");
127
+ return;
128
+ }
129
+
130
+ const firstAccountId = accountIds[0];
131
+ const account = resolveQQBotAccount(cfg, firstAccountId);
132
+
133
+ if (!account.appId || !account.clientSecret) {
134
+ console.log("[qqbot-channel-api] Account not fully configured, skipping");
135
+ return;
136
+ }
137
+
138
+ api.registerTool(
139
+ {
140
+ name: "qqbot_channel_api",
141
+ label: "QQBot Channel API",
142
+ description:
143
+ "QQ 开放平台频道 API HTTP 代理,自动填充鉴权 Token。" +
144
+ "常用接口速查:" +
145
+ "频道列表 GET /users/@me/guilds | " +
146
+ "子频道列表 GET /guilds/{guild_id}/channels | " +
147
+ "子频道详情 GET /channels/{channel_id} | " +
148
+ "创建子频道 POST /guilds/{guild_id}/channels | " +
149
+ "成员列表 GET /guilds/{guild_id}/members?after=0&limit=100 | " +
150
+ "成员详情 GET /guilds/{guild_id}/members/{user_id} | " +
151
+ "帖子列表 GET /channels/{channel_id}/threads | " +
152
+ "发帖 PUT /channels/{channel_id}/threads | " +
153
+ "创建公告 POST /guilds/{guild_id}/announces | " +
154
+ "创建日程 POST /channels/{channel_id}/schedules。" +
155
+ "更多接口和参数详情请阅读 qqbot-channel skill。",
156
+ parameters: ChannelApiSchema,
157
+ async execute(_toolCallId, params) {
158
+ const p = params as ChannelApiParams;
159
+
160
+ // 参数校验
161
+ if (!p.method) {
162
+ return json({ error: "method 为必填参数" });
163
+ }
164
+ if (!p.path) {
165
+ return json({ error: "path 为必填参数" });
166
+ }
167
+
168
+ const method = p.method.toUpperCase();
169
+ if (!["GET", "POST", "PUT", "PATCH", "DELETE"].includes(method)) {
170
+ return json({ error: `不支持的 HTTP 方法: ${method},可选值:GET, POST, PUT, PATCH, DELETE` });
171
+ }
172
+
173
+ // 路径安全校验
174
+ const pathError = validatePath(p.path);
175
+ if (pathError) {
176
+ return json({ error: pathError });
177
+ }
178
+
179
+ // GET/DELETE 不应有 body
180
+ if ((method === "GET" || method === "DELETE") && p.body && Object.keys(p.body).length > 0) {
181
+ console.log(`[qqbot-channel-api] ${method} request with body, body will be ignored`);
182
+ }
183
+
184
+ try {
185
+ // 获取鉴权 Token
186
+ const accessToken = await getAccessToken(account.appId, account.clientSecret);
187
+
188
+ // 构建请求
189
+ const url = buildUrl(p.path, p.query);
190
+ const headers: Record<string, string> = {
191
+ Authorization: `QQBot ${accessToken}`,
192
+ "Content-Type": "application/json",
193
+ };
194
+
195
+ const controller = new AbortController();
196
+ const timeoutId = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS);
197
+
198
+ const fetchOptions: RequestInit = {
199
+ method,
200
+ headers,
201
+ signal: controller.signal,
202
+ };
203
+
204
+ // POST/PUT/PATCH 才发送 body
205
+ if (p.body && ["POST", "PUT", "PATCH"].includes(method)) {
206
+ fetchOptions.body = JSON.stringify(p.body);
207
+ }
208
+
209
+ console.log(`[qqbot-channel-api] >>> ${method} ${url} (timeout: ${DEFAULT_TIMEOUT_MS}ms)`);
210
+
211
+ let res: Response;
212
+ try {
213
+ res = await fetch(url, fetchOptions);
214
+ } catch (err) {
215
+ clearTimeout(timeoutId);
216
+ if (err instanceof Error && err.name === "AbortError") {
217
+ console.error(`[qqbot-channel-api] <<< Request timeout after ${DEFAULT_TIMEOUT_MS}ms`);
218
+ return json({ error: `请求超时(${DEFAULT_TIMEOUT_MS}ms)`, path: p.path });
219
+ }
220
+ console.error(`[qqbot-channel-api] <<< Network error:`, err);
221
+ return json({
222
+ error: `网络错误: ${err instanceof Error ? err.message : String(err)}`,
223
+ path: p.path,
224
+ });
225
+ } finally {
226
+ clearTimeout(timeoutId);
227
+ }
228
+
229
+ console.log(`[qqbot-channel-api] <<< Status: ${res.status} ${res.statusText}`);
230
+
231
+ // 解析响应
232
+ const rawBody = await res.text();
233
+
234
+ // 空响应(如 DELETE 204)
235
+ if (!rawBody || rawBody.trim() === "") {
236
+ if (res.ok) {
237
+ return json({ success: true, status: res.status, path: p.path });
238
+ }
239
+ return json({
240
+ error: `API 返回 ${res.status} ${res.statusText}`,
241
+ status: res.status,
242
+ path: p.path,
243
+ });
244
+ }
245
+
246
+ let data: unknown;
247
+ try {
248
+ data = JSON.parse(rawBody);
249
+ } catch {
250
+ return json({
251
+ error: "响应解析失败",
252
+ status: res.status,
253
+ raw: rawBody.slice(0, 500),
254
+ path: p.path,
255
+ });
256
+ }
257
+
258
+ if (!res.ok) {
259
+ const errData = data as { message?: string; code?: number };
260
+ return json({
261
+ error: errData.message ?? `API 错误 (HTTP ${res.status})`,
262
+ code: errData.code,
263
+ status: res.status,
264
+ path: p.path,
265
+ detail: data,
266
+ });
267
+ }
268
+
269
+ return json(data);
270
+ } catch (err) {
271
+ const errMsg = err instanceof Error ? err.message : String(err);
272
+ console.error(`[qqbot-channel-api] Error [${method} ${p.path}]: ${errMsg}`);
273
+ return json({ error: errMsg, path: p.path });
274
+ }
275
+ },
276
+ },
277
+ { name: "qqbot_channel_api" },
278
+ );
279
+
280
+ console.log("[qqbot-channel-api] Registered QQ channel API proxy tool");
281
+ }